• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <assert.h>
16 #include <sys/param.h>
17 #include "esp_ota_ops.h"
18 #include "esp_attr.h"
19 #include "sdkconfig.h"
20 
21 // Application version info
22 const __attribute__((section(".rodata_desc"))) esp_app_desc_t esp_app_desc = {
23     .magic_word = ESP_APP_DESC_MAGIC_WORD,
24 #ifdef CONFIG_APP_EXCLUDE_PROJECT_VER_VAR
25     .version = "",
26 #else
27     .version = PROJECT_VER,
28 #endif
29 
30 #ifdef CONFIG_APP_EXCLUDE_PROJECT_NAME_VAR
31     .project_name = "",
32 #else
33     .project_name = PROJECT_NAME,
34 #endif
35     .idf_ver = IDF_VER,
36 
37 #ifdef CONFIG_BOOTLOADER_APP_SECURE_VERSION
38     .secure_version = CONFIG_BOOTLOADER_APP_SECURE_VERSION,
39 #else
40     .secure_version = 0,
41 #endif
42 
43 #ifdef CONFIG_APP_COMPILE_TIME_DATE
44     .time = __TIME__,
45     .date = __DATE__,
46 #else
47     .time = "",
48     .date = "",
49 #endif
50 };
51 
52 
53 #ifndef CONFIG_APP_EXCLUDE_PROJECT_VER_VAR
54 _Static_assert(sizeof(PROJECT_VER) <= sizeof(esp_app_desc.version), "PROJECT_VER is longer than version field in structure");
55 #endif
56 _Static_assert(sizeof(IDF_VER) <= sizeof(esp_app_desc.idf_ver), "IDF_VER is longer than idf_ver field in structure");
57 #ifndef CONFIG_APP_EXCLUDE_PROJECT_NAME_VAR
58 _Static_assert(sizeof(PROJECT_NAME) <= sizeof(esp_app_desc.project_name), "PROJECT_NAME is longer than project_name field in structure");
59 #endif
60 
esp_ota_get_app_description(void)61 const esp_app_desc_t *esp_ota_get_app_description(void)
62 {
63     return &esp_app_desc;
64 }
65 
66 /* The following two functions may be called from the panic handler
67  * or core dump, hence IRAM_ATTR.
68  */
69 
to_hex_digit(unsigned val)70 static inline char IRAM_ATTR to_hex_digit(unsigned val)
71 {
72     return (val < 10) ? ('0' + val) : ('a' + val - 10);
73 }
74 
esp_ota_init_app_elf_sha256(void)75 __attribute__((constructor)) void esp_ota_init_app_elf_sha256(void)
76 {
77     esp_ota_get_app_elf_sha256(NULL, 0);
78 }
79 
80 /* The esp_app_desc.app_elf_sha256 should be possible to print in panic handler during cache is disabled.
81  * But because the cache is disabled the reading esp_app_desc.app_elf_sha256 is not right and
82  * can lead to a complete lock-up of the CPU.
83  * For this reason we do a reading of esp_app_desc.app_elf_sha256 while start up in esp_ota_init_app_elf_sha256()
84  * and keep it in the static s_app_elf_sha256 value.
85  */
esp_ota_get_app_elf_sha256(char * dst,size_t size)86 int IRAM_ATTR esp_ota_get_app_elf_sha256(char* dst, size_t size)
87 {
88     static char s_app_elf_sha256[CONFIG_APP_RETRIEVE_LEN_ELF_SHA / 2];
89     static bool first_call = true;
90     if (first_call) {
91         first_call = false;
92         // At -O2 optimization level, GCC optimizes out the copying of the first byte of the app_elf_sha256,
93         // because it is zero at compile time, and only modified afterwards by esptool.
94         // Casting to volatile disables the optimization.
95         const volatile uint8_t* src = (const volatile uint8_t*)esp_app_desc.app_elf_sha256;
96         for (size_t i = 0; i < sizeof(s_app_elf_sha256); ++i) {
97             s_app_elf_sha256[i] = src[i];
98         }
99     }
100     if (dst == NULL || size == 0) {
101         return 0;
102     }
103     size_t n = MIN((size - 1) / 2, sizeof(s_app_elf_sha256));
104     for (size_t i = 0; i < n; ++i) {
105         dst[2*i] = to_hex_digit(s_app_elf_sha256[i] >> 4);
106         dst[2*i + 1] = to_hex_digit(s_app_elf_sha256[i] & 0xf);
107     }
108     dst[2*n] = 0;
109     return 2*n + 1;
110 }
111