From 27fe4374125f3ee1edd809c5dd91cfbf58a98da7 Mon Sep 17 00:00:00 2001 From: Itay Perl Date: Wed, 13 Jan 2021 02:01:52 +0200 Subject: [PATCH] app_update: fix incorrect first byte from esp_ota_get_app_elf_sha256 At -O2 optimization level, GCC seems to optimize out the copying of the first byte of the checksum, assuming it is zero. This "miscompilation" happens because the esp_app_desc struct is declared const, but then modified post-compilation. Casting to volatile disables the optimization. Closes: https://github.com/espressif/esp-idf/pull/6389 --- components/app_update/esp_app_desc.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/components/app_update/esp_app_desc.c b/components/app_update/esp_app_desc.c index 8a60b298bc..38d072ce2c 100644 --- a/components/app_update/esp_app_desc.c +++ b/components/app_update/esp_app_desc.c @@ -89,7 +89,10 @@ int IRAM_ATTR esp_ota_get_app_elf_sha256(char* dst, size_t size) static bool first_call = true; if (first_call) { first_call = false; - const uint8_t* src = esp_app_desc.app_elf_sha256; + // At -O2 optimization level, GCC optimizes out the copying of the first byte of the app_elf_sha256, + // because it is zero at compile time, and only modified afterwards by esptool. + // Casting to volatile disables the optimization. + const volatile uint8_t* src = (const volatile uint8_t*)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]; }