From a2a71fc68f2fd5f16dc9e7c26e1276f352ca3164 Mon Sep 17 00:00:00 2001 From: KonstantinKondrashov Date: Fri, 7 Feb 2020 18:57:55 +0800 Subject: [PATCH] app_update: Fix case when elf file SHA256 should be printed by panic handler while cache is disabled Closes: IDF-1342 --- components/app_update/Kconfig.projbuild | 10 +++++++++ components/app_update/esp_app_desc.c | 30 +++++++++++++++++++++---- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/components/app_update/Kconfig.projbuild b/components/app_update/Kconfig.projbuild index 2c77c8ae2..8b40e0434 100644 --- a/components/app_update/Kconfig.projbuild +++ b/components/app_update/Kconfig.projbuild @@ -22,4 +22,14 @@ menu "Application manager" The PROJECT_NAME variable from the build system will not affect the firmware image. This value will not be contained in the esp_app_desc structure. + config APP_RETRIEVE_LEN_ELF_SHA + int "The length of APP ELF SHA is stored in RAM(chars)" + default 16 + range 8 64 + help + At startup, the app will read this many hex characters from the embedded APP ELF SHA-256 hash value + and store it in static RAM. This ensures the app ELF SHA-256 value is always available + if it needs to be printed by the panic handler code. + Changing this value will change the size of a static buffer, in bytes. + endmenu # "Application manager" diff --git a/components/app_update/esp_app_desc.c b/components/app_update/esp_app_desc.c index eaa3a70b2..8a60b298b 100644 --- a/components/app_update/esp_app_desc.c +++ b/components/app_update/esp_app_desc.c @@ -72,13 +72,35 @@ static inline char IRAM_ATTR to_hex_digit(unsigned val) return (val < 10) ? ('0' + val) : ('a' + val - 10); } +__attribute__((constructor)) void esp_ota_init_app_elf_sha256(void) +{ + esp_ota_get_app_elf_sha256(NULL, 0); +} + +/* The esp_app_desc.app_elf_sha256 should be possible to print in panic handler during cache is disabled. + * But because the cache is disabled the reading esp_app_desc.app_elf_sha256 is not right and + * can lead to a complete lock-up of the CPU. + * For this reason we do a reading of esp_app_desc.app_elf_sha256 while start up in esp_ota_init_app_elf_sha256() + * and keep it in the static s_app_elf_sha256 value. + */ int IRAM_ATTR esp_ota_get_app_elf_sha256(char* dst, size_t size) { - size_t n = MIN((size - 1) / 2, sizeof(esp_app_desc.app_elf_sha256)); - const uint8_t* src = esp_app_desc.app_elf_sha256; + static char s_app_elf_sha256[CONFIG_APP_RETRIEVE_LEN_ELF_SHA / 2]; + static bool first_call = true; + if (first_call) { + first_call = false; + const uint8_t* src = esp_app_desc.app_elf_sha256; + for (size_t i = 0; i < sizeof(s_app_elf_sha256); ++i) { + s_app_elf_sha256[i] = src[i]; + } + } + if (dst == NULL || size == 0) { + return 0; + } + size_t n = MIN((size - 1) / 2, sizeof(s_app_elf_sha256)); for (size_t i = 0; i < n; ++i) { - dst[2*i] = to_hex_digit(src[i] >> 4); - dst[2*i + 1] = to_hex_digit(src[i] & 0xf); + dst[2*i] = to_hex_digit(s_app_elf_sha256[i] >> 4); + dst[2*i + 1] = to_hex_digit(s_app_elf_sha256[i] & 0xf); } dst[2*n] = 0; return 2*n + 1;