From 0bf4708033068ed8f8fe0cffa824996dff39de54 Mon Sep 17 00:00:00 2001 From: mikeller Date: Mon, 28 May 2018 01:14:01 +1200 Subject: [PATCH] Added support for signing board / serial number. --- src/main/{fc/board_info.h => common/strtol.h} | 10 +--- src/main/fc/board_info.c | 50 ++++++++++++++++- src/main/fc/board_info.h | 6 ++ src/main/fc/fc_init.c | 2 + src/main/interface/cli.c | 79 ++++++++++++++++++++++++++- src/main/interface/msp.c | 21 +++++++ src/main/interface/msp_protocol.h | 1 + src/main/pg/board.c | 12 +++- src/main/pg/board.h | 5 +- src/main/target/OMNIBUS/target.h | 1 + src/main/target/common_fc_pre.h | 2 + 11 files changed, 175 insertions(+), 14 deletions(-) copy src/main/{fc/board_info.h => common/strtol.h} (76%) diff --git a/src/main/fc/board_info.h b/src/main/common/strtol.h similarity index 76% copy from src/main/fc/board_info.h copy to src/main/common/strtol.h index e5f43f39d..ae82e95f1 100644 --- a/src/main/fc/board_info.h +++ b/src/main/common/strtol.h @@ -20,12 +20,8 @@ #pragma once -void initBoardInformation(void); +long strtol(const char * str, char ** endptr, int base); -char *getBoardName(void); -char *getManufacturerId(void); -bool boardInformationIsSet(void); +unsigned long strtoul(const char * str, char ** endptr, int base); -bool setBoardName(char *newBoardName); -bool setManufacturerId(char *newManufacturerId); -bool persistBoardInformation(void); +int atoi(const char *str); diff --git a/src/main/fc/board_info.c b/src/main/fc/board_info.c index c703ec177..b4db85aae 100644 --- a/src/main/fc/board_info.c +++ b/src/main/fc/board_info.c @@ -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 diff --git a/src/main/fc/board_info.h b/src/main/fc/board_info.h index e5f43f39d..b855d50c5 100644 --- a/src/main/fc/board_info.h +++ b/src/main/fc/board_info.h @@ -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); diff --git a/src/main/fc/fc_init.c b/src/main/fc/fc_init.c index f77a1b0c1..2844959f7 100644 --- a/src/main/fc/fc_init.c +++ b/src/main/fc/fc_init.c @@ -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(); diff --git a/src/main/interface/cli.c b/src/main/interface/cli.c index 98751d1ae..b74bf8643 100644 --- a/src/main/interface/cli.c +++ b/src/main/interface/cli.c @@ -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", "[]", 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", "[=]", 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", " \r\n" "\treset\r\n" diff --git a/src/main/interface/msp.c b/src/main/interface/msp.c index 7a39974ba..79e331fa9 100644 --- a/src/main/interface/msp.c +++ b/src/main/interface/msp.c @@ -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; diff --git a/src/main/interface/msp_protocol.h b/src/main/interface/msp_protocol.h index 234efb28a..bae04e0c1 100644 --- a/src/main/interface/msp_protocol.h +++ b/src/main/interface/msp_protocol.h @@ -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 diff --git a/src/main/pg/board.c b/src/main/pg/board.c index 622a96c60..659c27f37 100644 --- a/src/main/pg/board.c +++ b/src/main/pg/board.c @@ -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: diff --git a/src/main/pg/board.h b/src/main/pg/board.h index 6d70916c1..2c3650aa4 100644 --- a/src/main/pg/board.h +++ b/src/main/pg/board.h @@ -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); diff --git a/src/main/target/OMNIBUS/target.h b/src/main/target/OMNIBUS/target.h index f27db2cdd..0de7d5c98 100644 --- a/src/main/target/OMNIBUS/target.h +++ b/src/main/target/OMNIBUS/target.h @@ -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 diff --git a/src/main/target/common_fc_pre.h b/src/main/target/common_fc_pre.h index f1991d6cf..6eab448ae 100644 --- a/src/main/target/common_fc_pre.h +++ b/src/main/target/common_fc_pre.h @@ -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 -- 2.11.4.GIT