1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-15 04:15:44 +03:00

Added support for signing board / serial number.

This commit is contained in:
mikeller 2018-05-28 01:14:01 +12:00
parent 1a2c129306
commit 0bf4708033
11 changed files with 199 additions and 7 deletions

27
src/main/common/strtol.h Normal file
View file

@ -0,0 +1,27 @@
/*
* This file is part of Cleanflight and Betaflight.
*
* Cleanflight and Betaflight are free software. You can redistribute
* this software and/or modify this software under the terms of the
* GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* Cleanflight and Betaflight are distributed in the hope that they
* will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software.
*
* If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
long strtol(const char * str, char ** endptr, int base);
unsigned long strtoul(const char * str, char ** endptr, int base);
int atoi(const char *str);

View file

@ -23,21 +23,27 @@
#include "platform.h"
#if defined(USE_BOARD_INFO)
#include "pg/board.h"
static bool boardInformationSet = false;
static char manufacturerId[MAX_MANUFACTURER_ID_LENGTH + 1];
static char boardName[MAX_BOARD_NAME_LENGTH + 1];
static bool signatureSet = false;
static uint8_t signature[SIGNATURE_LENGTH];
void initBoardInformation(void)
{
boardInformationSet = boardConfig()->boardInformationSet;
if (boardInformationSet) {
strncpy(manufacturerId, boardConfig()->manufacturerId, MAX_MANUFACTURER_ID_LENGTH);
strncpy(boardName, boardConfig()->boardName, MAX_BOARD_NAME_LENGTH);
} else {
strcpy(manufacturerId, "");
strcpy(boardName, "");
}
signatureSet = boardConfig()->signatureSet;
if (signatureSet) {
memcpy(signature, boardConfig()->signature, SIGNATURE_LENGTH);
}
}
@ -92,3 +98,41 @@ bool persistBoardInformation(void)
return false;
}
}
#if defined(USE_SIGNATURE)
uint8_t *getSignature(void)
{
return signature;
}
bool signatureIsSet(void)
{
return signatureSet;
}
bool setSignature(uint8_t *newSignature)
{
if (!signatureSet) {
memcpy(signature, newSignature, SIGNATURE_LENGTH);
return true;
} else {
return false;
}
}
bool persistSignature(void)
{
if (!signatureSet) {
memcpy(boardConfigMutable()->signature, signature, SIGNATURE_LENGTH);
boardConfigMutable()->signatureSet = true;
initBoardInformation();
return true;
} else {
return false;
}
}
#endif
#endif // USE_BOARD_INFO

View file

@ -29,3 +29,9 @@ bool boardInformationIsSet(void);
bool setBoardName(char *newBoardName);
bool setManufacturerId(char *newManufacturerId);
bool persistBoardInformation(void);
uint8_t * getSignature(void);
bool signatureIsSet(void);
bool setSignature(uint8_t *newSignature);
bool persistSignature(void);

View file

@ -242,7 +242,9 @@ void init(void)
ensureEEPROMStructureIsValid();
bool readSuccess = readEEPROM();
#if defined(USE_BOARD_INFO)
initBoardInformation();
#endif
if (!readSuccess || strncasecmp(systemConfig()->boardIdentifier, TARGET_BOARD_IDENTIFIER, sizeof(TARGET_BOARD_IDENTIFIER))) {
resetEEPROM();

View file

@ -50,6 +50,7 @@ extern uint8_t __config_end;
#include "common/color.h"
#include "common/maths.h"
#include "common/printf.h"
#include "common/strtol.h"
#include "common/time.h"
#include "common/typeconversion.h"
#include "common/utils.h"
@ -121,6 +122,7 @@ extern uint8_t __config_end;
#include "pg/adc.h"
#include "pg/beeper.h"
#include "pg/beeper_dev.h"
#include "pg/board.h"
#include "pg/bus_i2c.h"
#include "pg/bus_spi.h"
#include "pg/max7456.h"
@ -172,7 +174,12 @@ static uint32_t bufferIndex = 0;
static bool configIsInCopy = false;
#if defined(USE_BOARD_INFO)
static bool boardInformationUpdated = false;
#if defined(USE_SIGNATURE)
static bool signatureUpdated = false;
#endif
#endif // USE_BOARD_INFO
static const char* const emptyName = "-";
static const char* const emptyString = "";
@ -2232,6 +2239,8 @@ static void cliName(char *cmdline)
printName(DUMP_MASTER, pilotConfig());
}
#if defined(USE_BOARD_INFO)
#define ERROR_MESSAGE "Error, %s is already set: %s"
static void cliBoardName(char *cmdline)
@ -2262,8 +2271,52 @@ static void cliManufacturerId(char *cmdline)
}
}
#if defined(USE_SIGNATURE)
static void writeSignature(char *signatureStr, uint8_t *signature)
{
for (unsigned i = 0; i < SIGNATURE_LENGTH; i++) {
tfp_sprintf(&signatureStr[2 * i], "%02x", signature[i]);
}
}
static void cliSignature(char *cmdline)
{
const unsigned int len = strlen(cmdline);
char signatureStr[SIGNATURE_LENGTH * 2 + 1] = {0};
if (len > 0) {
uint8_t signature[SIGNATURE_LENGTH];
#define BLOCK_SIZE 2
for (unsigned i = 0; i < SIGNATURE_LENGTH; i++) {
char temp[BLOCK_SIZE + 1];
strncpy(temp, &cmdline[i * BLOCK_SIZE], BLOCK_SIZE);
temp[BLOCK_SIZE] = '\0';
signature[i] = strtoul(temp, NULL, 16);
}
#undef BLOCK_SIZE
if (signatureIsSet() && memcmp(signature, getSignature(), SIGNATURE_LENGTH)) {
writeSignature(signatureStr, getSignature());
cliPrintLinef(ERROR_MESSAGE, "signature", signatureStr);
return;
} else {
if (len > 0) {
setSignature(signature);
signatureUpdated = true;
}
}
}
writeSignature(signatureStr, getSignature());
cliPrintLinef("signature %s", signatureStr);
}
#endif
#undef ERROR_MESSAGE
#endif // USE_BOARD_INFO
static void cliMcuId(char *cmdline)
{
UNUSED(cmdline);
@ -3146,9 +3199,18 @@ static void cliSave(char *cmdline)
UNUSED(cmdline);
cliPrintHashLine("saving");
#if defined(USE_BOARD_INFO)
if (boardInformationUpdated) {
persistBoardInformation();
}
#if defined(USE_SIGNATURE)
if (signatureUpdated) {
persistSignature();
}
#endif
#endif // USE_BOARD_INFO
writeEEPROM();
cliReboot();
}
@ -4024,8 +4086,14 @@ static void printConfig(char *cmdline, bool doDiff)
cliVersion(NULL);
cliPrintLinefeed();
#if defined(USE_BOARD_INFO)
cliBoardName("");
cliManufacturerId("");
#if defined(USE_SIGNATURE)
cliSignature("");
#endif
#endif // USE_BOARD_INFO
if (dumpMask & DUMP_ALL) {
cliMcuId(NULL);
}
@ -4232,7 +4300,9 @@ const clicmd_t cmdTable[] = {
"\t<+|->[name]", cliBeeper),
#endif
CLI_COMMAND_DEF("bl", "reboot into bootloader", NULL, cliBootloader),
CLI_COMMAND_DEF("board_name", "name of the board model", NULL, cliBoardName),
#if defined(USE_BOARD_INFO)
CLI_COMMAND_DEF("board_name", "get / set the name of the board model", "[board name]", cliBoardName),
#endif
#ifdef USE_LED_STRIP
CLI_COMMAND_DEF("color", "configure colors", NULL, cliColor),
#endif
@ -4275,7 +4345,9 @@ const clicmd_t cmdTable[] = {
#ifdef USE_LED_STRIP
CLI_COMMAND_DEF("led", "configure leds", NULL, cliLed),
#endif
CLI_COMMAND_DEF("manufacturer_id", "id of the board manufacturer", NULL, cliManufacturerId),
#if defined(USE_BOARD_INFO)
CLI_COMMAND_DEF("manufacturer_id", "get / set the id of the board manufacturer", "[manufacturer id]", cliManufacturerId),
#endif
CLI_COMMAND_DEF("map", "configure rc channel order", "[<map>]", cliMap),
CLI_COMMAND_DEF("mcu_id", "id of the microcontroller", NULL, cliMcuId),
#ifndef USE_QUAD_MIXER_ONLY
@ -4312,6 +4384,9 @@ const clicmd_t cmdTable[] = {
CLI_COMMAND_DEF("servo", "configure servos", NULL, cliServo),
#endif
CLI_COMMAND_DEF("set", "change setting", "[<name>=<value>]", cliSet),
#if defined(USE_BOARD_INFO) && defined(USE_SIGNATURE)
CLI_COMMAND_DEF("signature", "get / set the board type signature", "[signature]", cliSignature),
#endif
#ifdef USE_SERVOS
CLI_COMMAND_DEF("smix", "servo mixer", "<rule> <servo> <source> <rate> <speed> <min> <max> <box>\r\n"
"\treset\r\n"

View file

@ -460,6 +460,7 @@ static bool mspCommonProcessOutCommand(uint8_t cmdMSP, sbuf_t *dst, mspPostProce
sbufWriteU8(dst, strlen(targetName));
sbufWriteData(dst, targetName, strlen(targetName));
#if defined(USE_BOARD_INFO)
// Board name with explicit length
char *value = getBoardName();
sbufWriteU8(dst, strlen(value));
@ -470,6 +471,12 @@ static bool mspCommonProcessOutCommand(uint8_t cmdMSP, sbuf_t *dst, mspPostProce
sbufWriteU8(dst, strlen(value));
sbufWriteData(dst, value, strlen(value));
#if defined(USE_SIGNATURE)
// Signature
sbufWriteData(dst, getSignature(), SIGNATURE_LENGTH);
#endif
#endif // USE_BOARD_INFO
break;
}
@ -2068,6 +2075,7 @@ static mspResult_e mspProcessInCommand(uint8_t cmdMSP, sbuf_t *src)
break;
#if defined(USE_BOARD_INFO)
case MSP_SET_BOARD_INFO:
if (!boardInformationIsSet()) {
char boardName[MAX_BOARD_NAME_LENGTH + 1] = {0};
@ -2089,7 +2097,20 @@ static mspResult_e mspProcessInCommand(uint8_t cmdMSP, sbuf_t *src)
}
break;
#if defined(USE_SIGNATURE)
case MSP_SET_SIGNATURE:
if (!signatureIsSet()) {
uint8_t signature[SIGNATURE_LENGTH];
sbufReadData(src, signature, SIGNATURE_LENGTH);
setSignature(signature);
persistSignature();
} else {
return MSP_RESULT_ERROR;
}
break;
#endif
#endif // USE_BOARD_INFO
default:
// we do not know how to handle the (valid) message, indicate error MSP $M!
return MSP_RESULT_ERROR;

View file

@ -330,3 +330,4 @@
#define MSP_SET_RTC 246 //in message Sets the RTC clock
#define MSP_RTC 247 //out message Gets the RTC clock
#define MSP_SET_BOARD_INFO 248 //in message Sets the board information for this board
#define MSP_SET_SIGNATURE 249 //in message Sets the signature of the board and serial number

View file

@ -23,6 +23,7 @@
#include "platform.h"
#if defined(USE_BOARD_INFO)
#include "build/version.h"
#include "fc/board_info.h"
@ -41,7 +42,6 @@ void pgResetFn_boardConfig(boardConfig_t *boardConfig)
strncpy(boardConfig->boardName, getBoardName(), MAX_BOARD_NAME_LENGTH);
boardConfig->boardInformationSet = true;
} else {
#if !defined(GENERIC_TARGET)
strncpy(boardConfig->boardName, targetName, MAX_BOARD_NAME_LENGTH);
@ -53,4 +53,14 @@ void pgResetFn_boardConfig(boardConfig_t *boardConfig)
boardConfig->boardInformationSet = false;
#endif // GENERIC_TARGET
}
#if defined(USE_SIGNATURE)
if (signatureIsSet()) {
memcpy(boardConfig->signature, getSignature(), SIGNATURE_LENGTH);
boardConfig->signatureSet = true;
} else {
boardConfig->signatureSet = false;
}
#endif
}
#endif // USE_BOARD_INFO:

View file

@ -24,6 +24,7 @@
#define MAX_MANUFACTURER_ID_LENGTH 4
#define MAX_BOARD_NAME_LENGTH 20
#define SIGNATURE_LENGTH 32
// Warning: This configuration is meant to be applied when loading the initial
// configuration for a generic board, and stay fixed after this, to enable
@ -31,9 +32,11 @@
// Do not modify this parameter group directly, use 'fc/board_info.h' instead.
typedef struct boardConfig_s {
uint8_t boardInformationSet;
uint8_t signature[SIGNATURE_LENGTH];
char manufacturerId[MAX_MANUFACTURER_ID_LENGTH + 1];
char boardName[MAX_BOARD_NAME_LENGTH + 1];
uint8_t boardInformationSet;
uint8_t signatureSet;
} boardConfig_t;
PG_DECLARE(boardConfig_t, boardConfig);

View file

@ -31,6 +31,7 @@
#undef USE_TELEMETRY_LTM
#undef USE_SERIALRX_XBUS
#undef USE_BOARD_INFO
#undef USE_EXTENDED_CMS_MENUS
#undef USE_COPY_PROFILE_CMS_MENU
#undef USE_RTC_TIME

View file

@ -187,6 +187,7 @@
#define USE_ESC_SENSOR
#define USE_ESC_SENSOR_INFO
#define USE_CRSF_CMS_TELEMETRY
#define USE_BOARD_INFO
#ifdef USE_SERIALRX_SPEKTRUM
#define USE_SPEKTRUM_BIND
@ -214,4 +215,5 @@
#define USE_TELEMETRY_JETIEXBUS
#define USE_TELEMETRY_MAVLINK
#define USE_UNCOMMON_MIXERS
#define USE_SIGNATURE
#endif