1 /*
2  * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <assert.h>
8 #include <sys/param.h>
9 #include "esp_app_desc.h"
10 #include "esp_attr.h"
11 #include "sdkconfig.h"
12 
13 
14 // Application version info
15 const __attribute__((weak)) __attribute__((section(".rodata_desc")))  esp_app_desc_t esp_app_desc = {
16     .magic_word = ESP_APP_DESC_MAGIC_WORD,
17 #ifdef CONFIG_APP_EXCLUDE_PROJECT_VER_VAR
18     .version = "",
19 #else
20     .version = PROJECT_VER,
21 #endif
22 
23 #ifdef CONFIG_APP_EXCLUDE_PROJECT_NAME_VAR
24     .project_name = "",
25 #else
26     .project_name = PROJECT_NAME,
27 #endif
28     .idf_ver = IDF_VER,
29 
30 #ifdef CONFIG_BOOTLOADER_APP_SECURE_VERSION
31     .secure_version = CONFIG_BOOTLOADER_APP_SECURE_VERSION,
32 #else
33     .secure_version = 0,
34 #endif
35 
36 #if defined(CONFIG_APP_COMPILE_TIME_DATE) && !defined(CONFIG_APP_REPRODUCIBLE_BUILD)
37     .time = __TIME__,
38     .date = __DATE__,
39 #else
40     .time = "",
41     .date = "",
42 #endif
43 };
44 
45 
46 #ifndef CONFIG_APP_EXCLUDE_PROJECT_VER_VAR
47 _Static_assert(sizeof(PROJECT_VER) <= sizeof(esp_app_desc.version), "PROJECT_VER is longer than version field in structure");
48 #endif
49 _Static_assert(sizeof(IDF_VER) <= sizeof(esp_app_desc.idf_ver), "IDF_VER is longer than idf_ver field in structure");
50 #ifndef CONFIG_APP_EXCLUDE_PROJECT_NAME_VAR
51 _Static_assert(sizeof(PROJECT_NAME) <= sizeof(esp_app_desc.project_name), "PROJECT_NAME is longer than project_name field in structure");
52 #endif
53 
esp_app_get_description(void)54 const esp_app_desc_t *esp_app_get_description(void)
55 {
56     return &esp_app_desc;
57 }
58 
59 /* The following two functions may be called from the panic handler
60  * or core dump, hence IRAM_ATTR.
61  */
62 
to_hex_digit(unsigned val)63 static inline char IRAM_ATTR to_hex_digit(unsigned val)
64 {
65     return (val < 10) ? ('0' + val) : ('a' + val - 10);
66 }
67 
esp_init_app_elf_sha256(void)68 __attribute__((constructor)) void esp_init_app_elf_sha256(void)
69 {
70     esp_app_get_elf_sha256(NULL, 0);
71 }
72 
73 /* The esp_app_desc.app_elf_sha256 should be possible to print in panic handler during cache is disabled.
74  * But because the cache is disabled the reading esp_app_desc.app_elf_sha256 is not right and
75  * can lead to a complete lock-up of the CPU.
76  * For this reason we do a reading of esp_app_desc.app_elf_sha256 while start up in esp_init_app_elf_sha256()
77  * and keep it in the static s_app_elf_sha256 value.
78  */
esp_app_get_elf_sha256(char * dst,size_t size)79 int IRAM_ATTR esp_app_get_elf_sha256(char* dst, size_t size)
80 {
81     static char s_app_elf_sha256[CONFIG_APP_RETRIEVE_LEN_ELF_SHA / 2];
82     static bool first_call = true;
83     if (first_call) {
84         first_call = false;
85         // At -O2 optimization level, GCC optimizes out the copying of the first byte of the app_elf_sha256,
86         // because it is zero at compile time, and only modified afterwards by esptool.
87         // Casting to volatile disables the optimization.
88         const volatile uint8_t* src = (const volatile uint8_t*)esp_app_desc.app_elf_sha256;
89         for (size_t i = 0; i < sizeof(s_app_elf_sha256); ++i) {
90             s_app_elf_sha256[i] = src[i];
91         }
92     }
93     if (dst == NULL || size == 0) {
94         return 0;
95     }
96     size_t n = MIN((size - 1) / 2, sizeof(s_app_elf_sha256));
97     for (size_t i = 0; i < n; ++i) {
98         dst[2*i] = to_hex_digit(s_app_elf_sha256[i] >> 4);
99         dst[2*i + 1] = to_hex_digit(s_app_elf_sha256[i] & 0xf);
100     }
101     dst[2*n] = 0;
102     return 2*n + 1;
103 }
104