From 356476b7f876d7e0364c1f1358e7f60aa5962441 Mon Sep 17 00:00:00 2001 From: gornekich Date: Mon, 24 May 2021 21:06:53 +0300 Subject: [PATCH] [FL-1355] Fix flipper name C string (#485) * api-hal-version: guarantee zero terminated flipper name * cli_commands: fix hw_info command --- applications/cli/cli_commands.c | 5 ++++- firmware/targets/api-hal-include/api-hal-version.h | 3 +++ firmware/targets/f5/api-hal/api-hal-version.c | 11 +++++++++-- firmware/targets/f5/api-hal/api-hal.c | 2 ++ firmware/targets/f6/api-hal/api-hal-version.c | 11 +++++++++-- firmware/targets/f6/api-hal/api-hal.c | 2 ++ 6 files changed, 29 insertions(+), 5 deletions(-) diff --git a/applications/cli/cli_commands.c b/applications/cli/cli_commands.c index 2e4d6b90..aed63513 100644 --- a/applications/cli/cli_commands.c +++ b/applications/cli/cli_commands.c @@ -102,7 +102,10 @@ void cli_command_hw_info(Cli* cli, string_t args, void* context) { api_hal_version_get_hw_connect()); time_t time = api_hal_version_get_hw_timestamp(); printf("%-20s %s\r", "Production date:", ctime(&time)); - printf("%-20s %s", "Name:", api_hal_version_get_name_ptr()); + const char* name = api_hal_version_get_name_ptr(); + if(name) { + printf("%-20s %s", "Name:", name); + } } void cli_command_vibro(Cli* cli, string_t args, void* context) { diff --git a/firmware/targets/api-hal-include/api-hal-version.h b/firmware/targets/api-hal-include/api-hal-version.h index 19e1c3f3..0f2f7097 100644 --- a/firmware/targets/api-hal-include/api-hal-version.h +++ b/firmware/targets/api-hal-include/api-hal-version.h @@ -9,6 +9,9 @@ extern "C" { #endif +/** Init flipper version */ +void api_hal_version_init(); + /** Check target firmware version */ bool api_hal_version_do_i_belong_here(); diff --git a/firmware/targets/f5/api-hal/api-hal-version.c b/firmware/targets/f5/api-hal/api-hal-version.c index 19fdbc3d..6818fe30 100644 --- a/firmware/targets/f5/api-hal/api-hal-version.c +++ b/firmware/targets/f5/api-hal/api-hal-version.c @@ -11,6 +11,14 @@ typedef struct { char name[8]; } ApiHalVersionOTP; +// Initialiazed from OTP, used to guarantee zero terminated C string +static char flipper_name[9]; + +void api_hal_version_init() { + char* name = ((ApiHalVersionOTP*)OTP_AREA_BASE)->name; + strlcpy(flipper_name, name, 9); +} + bool api_hal_version_do_i_belong_here() { return api_hal_version_get_hw_target() == 5; } @@ -36,8 +44,7 @@ const uint32_t api_hal_version_get_hw_timestamp() { } const char * api_hal_version_get_name_ptr() { - char * name = ((ApiHalVersionOTP*)OTP_AREA_BASE)->name; - return *name == 0xFFU ? NULL : name; + return *flipper_name == 0xFFU ? NULL : flipper_name; } const struct Version* api_hal_version_get_fw_version(void) { diff --git a/firmware/targets/f5/api-hal/api-hal.c b/firmware/targets/f5/api-hal/api-hal.c index cf01b7fe..222a41e5 100644 --- a/firmware/targets/f5/api-hal/api-hal.c +++ b/firmware/targets/f5/api-hal/api-hal.c @@ -1,6 +1,8 @@ #include void api_hal_init() { + api_hal_version_init(); + FURI_LOG_I("FURI_HAL", "VERSION OK"); api_hal_delay_init(); FURI_LOG_I("FURI_HAL", "DELAY OK"); api_hal_os_init(); diff --git a/firmware/targets/f6/api-hal/api-hal-version.c b/firmware/targets/f6/api-hal/api-hal-version.c index 19fdbc3d..6818fe30 100644 --- a/firmware/targets/f6/api-hal/api-hal-version.c +++ b/firmware/targets/f6/api-hal/api-hal-version.c @@ -11,6 +11,14 @@ typedef struct { char name[8]; } ApiHalVersionOTP; +// Initialiazed from OTP, used to guarantee zero terminated C string +static char flipper_name[9]; + +void api_hal_version_init() { + char* name = ((ApiHalVersionOTP*)OTP_AREA_BASE)->name; + strlcpy(flipper_name, name, 9); +} + bool api_hal_version_do_i_belong_here() { return api_hal_version_get_hw_target() == 5; } @@ -36,8 +44,7 @@ const uint32_t api_hal_version_get_hw_timestamp() { } const char * api_hal_version_get_name_ptr() { - char * name = ((ApiHalVersionOTP*)OTP_AREA_BASE)->name; - return *name == 0xFFU ? NULL : name; + return *flipper_name == 0xFFU ? NULL : flipper_name; } const struct Version* api_hal_version_get_fw_version(void) { diff --git a/firmware/targets/f6/api-hal/api-hal.c b/firmware/targets/f6/api-hal/api-hal.c index cf01b7fe..222a41e5 100644 --- a/firmware/targets/f6/api-hal/api-hal.c +++ b/firmware/targets/f6/api-hal/api-hal.c @@ -1,6 +1,8 @@ #include void api_hal_init() { + api_hal_version_init(); + FURI_LOG_I("FURI_HAL", "VERSION OK"); api_hal_delay_init(); FURI_LOG_I("FURI_HAL", "DELAY OK"); api_hal_os_init();