esp_image_format.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. // Copyright 2015-2016 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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <string.h>
  14. #include <sys/param.h>
  15. #include <rom/rtc.h>
  16. #include <soc/cpu.h>
  17. #include <esp_image_format.h>
  18. #include <esp_secure_boot.h>
  19. #include <esp_log.h>
  20. #include <esp_spi_flash.h>
  21. #include <bootloader_flash.h>
  22. #include <bootloader_random.h>
  23. #include <bootloader_sha.h>
  24. static const char *TAG = "esp_image";
  25. #define HASH_LEN 32 /* SHA-256 digest length */
  26. #define SIXTEEN_MB 0x1000000
  27. #define ESP_ROM_CHECKSUM_INITIAL 0xEF
  28. /* Headroom to ensure between stack SP (at time of checking) and data loaded from flash */
  29. #define STACK_LOAD_HEADROOM 32768
  30. #ifdef BOOTLOADER_BUILD
  31. /* 64 bits of random data to obfuscate loaded RAM with, until verification is complete
  32. (Means loaded code isn't executable until after the secure boot check.)
  33. */
  34. static uint32_t ram_obfs_value[2];
  35. #endif
  36. /* Return true if load_addr is an address the bootloader should load into */
  37. static bool should_load(uint32_t load_addr);
  38. /* Return true if load_addr is an address the bootloader should map via flash cache */
  39. static bool should_map(uint32_t load_addr);
  40. /* Load or verify a segment */
  41. static esp_err_t process_segment(int index, uint32_t flash_addr, esp_image_segment_header_t *header, bool silent, bool do_load, bootloader_sha256_handle_t sha_handle, uint32_t *checksum);
  42. /* split segment and verify if data_len is too long */
  43. static esp_err_t process_segment_data(intptr_t load_addr, uint32_t data_addr, uint32_t data_len, bool do_load, bootloader_sha256_handle_t sha_handle, uint32_t *checksum);
  44. /* Verify the main image header */
  45. static esp_err_t verify_image_header(uint32_t src_addr, const esp_image_header_t *image, bool silent);
  46. /* Verify a segment header */
  47. static esp_err_t verify_segment_header(int index, const esp_image_segment_header_t *segment, uint32_t segment_data_offs, bool silent);
  48. /* Log-and-fail macro for use in esp_image_load */
  49. #define FAIL_LOAD(...) do { \
  50. if (!silent) { \
  51. ESP_LOGE(TAG, __VA_ARGS__); \
  52. } \
  53. goto err; \
  54. } \
  55. while(0)
  56. static esp_err_t verify_checksum(bootloader_sha256_handle_t sha_handle, uint32_t checksum_word, esp_image_metadata_t *data);
  57. static esp_err_t __attribute__((unused)) verify_secure_boot_signature(bootloader_sha256_handle_t sha_handle, esp_image_metadata_t *data);
  58. static esp_err_t __attribute__((unused)) verify_simple_hash(bootloader_sha256_handle_t sha_handle, esp_image_metadata_t *data);
  59. esp_err_t esp_image_load(esp_image_load_mode_t mode, const esp_partition_pos_t *part, esp_image_metadata_t *data)
  60. {
  61. #ifdef BOOTLOADER_BUILD
  62. bool do_load = (mode == ESP_IMAGE_LOAD);
  63. #else
  64. bool do_load = false; // Can't load the image in app mode
  65. #endif
  66. bool silent = (mode == ESP_IMAGE_VERIFY_SILENT);
  67. esp_err_t err = ESP_OK;
  68. // checksum the image a word at a time. This shaves 30-40ms per MB of image size
  69. uint32_t checksum_word = ESP_ROM_CHECKSUM_INITIAL;
  70. bootloader_sha256_handle_t sha_handle = NULL;
  71. if (data == NULL || part == NULL) {
  72. return ESP_ERR_INVALID_ARG;
  73. }
  74. if (part->size > SIXTEEN_MB) {
  75. err = ESP_ERR_INVALID_ARG;
  76. FAIL_LOAD("partition size 0x%x invalid, larger than 16MB", part->size);
  77. }
  78. bzero(data, sizeof(esp_image_metadata_t));
  79. data->start_addr = part->offset;
  80. ESP_LOGD(TAG, "reading image header @ 0x%x", data->start_addr);
  81. err = bootloader_flash_read(data->start_addr, &data->image, sizeof(esp_image_header_t), true);
  82. if (err != ESP_OK) {
  83. goto err;
  84. }
  85. // Calculate SHA-256 of image if secure boot is on, or if image has a hash appended
  86. #ifdef CONFIG_SECURE_BOOT_ENABLED
  87. if (1) {
  88. #else
  89. if (data->image.hash_appended) {
  90. #endif
  91. sha_handle = bootloader_sha256_start();
  92. if (sha_handle == NULL) {
  93. return ESP_ERR_NO_MEM;
  94. }
  95. bootloader_sha256_data(sha_handle, &data->image, sizeof(esp_image_header_t));
  96. }
  97. ESP_LOGD(TAG, "image header: 0x%02x 0x%02x 0x%02x 0x%02x %08x",
  98. data->image.magic,
  99. data->image.segment_count,
  100. data->image.spi_mode,
  101. data->image.spi_size,
  102. data->image.entry_addr);
  103. err = verify_image_header(data->start_addr, &data->image, silent);
  104. if (err != ESP_OK) {
  105. goto err;
  106. }
  107. if (data->image.segment_count > ESP_IMAGE_MAX_SEGMENTS) {
  108. FAIL_LOAD("image at 0x%x segment count %d exceeds max %d",
  109. data->start_addr, data->image.segment_count, ESP_IMAGE_MAX_SEGMENTS);
  110. }
  111. uint32_t next_addr = data->start_addr + sizeof(esp_image_header_t);
  112. for(int i = 0; i < data->image.segment_count; i++) {
  113. esp_image_segment_header_t *header = &data->segments[i];
  114. ESP_LOGV(TAG, "loading segment header %d at offset 0x%x", i, next_addr);
  115. err = process_segment(i, next_addr, header, silent, do_load, sha_handle, &checksum_word);
  116. if (err != ESP_OK) {
  117. goto err;
  118. }
  119. next_addr += sizeof(esp_image_segment_header_t);
  120. data->segment_data[i] = next_addr;
  121. next_addr += header->data_len;
  122. }
  123. // Segments all loaded, verify length
  124. uint32_t end_addr = next_addr;
  125. if (end_addr < data->start_addr) {
  126. FAIL_LOAD("image offset has wrapped");
  127. }
  128. data->image_len = end_addr - data->start_addr;
  129. ESP_LOGV(TAG, "image start 0x%08x end of last section 0x%08x", data->start_addr, end_addr);
  130. err = verify_checksum(sha_handle, checksum_word, data);
  131. if (err != ESP_OK) {
  132. goto err;
  133. }
  134. if (data->image_len > part->size) {
  135. FAIL_LOAD("Image length %d doesn't fit in partition length %d", data->image_len, part->size);
  136. }
  137. bool is_bootloader = (data->start_addr == ESP_BOOTLOADER_OFFSET);
  138. /* For secure boot, we don't verify signature on bootloaders.
  139. For non-secure boot, we don't verify any SHA-256 hash appended to the bootloader because esptool.py may have
  140. rewritten the header - rely on esptool.py having verified the bootloader at flashing time, instead.
  141. */
  142. if (!is_bootloader) {
  143. #ifdef CONFIG_SECURE_BOOT_ENABLED
  144. // secure boot images have a signature appended
  145. err = verify_secure_boot_signature(sha_handle, data);
  146. #else
  147. // No secure boot, but SHA-256 can be appended for basic corruption detection
  148. if (sha_handle != NULL) {
  149. err = verify_simple_hash(sha_handle, data);
  150. }
  151. #endif // CONFIG_SECURE_BOOT_ENABLED
  152. } else { // is_bootloader
  153. // bootloader may still have a sha256 digest handle open
  154. if (sha_handle != NULL) {
  155. bootloader_sha256_finish(sha_handle, NULL);
  156. }
  157. }
  158. sha_handle = NULL;
  159. if (err != ESP_OK) {
  160. goto err;
  161. }
  162. #ifdef BOOTLOADER_BUILD
  163. if (do_load) { // Need to deobfuscate RAM
  164. for (int i = 0; i < data->image.segment_count; i++) {
  165. uint32_t load_addr = data->segments[i].load_addr;
  166. if (should_load(load_addr)) {
  167. uint32_t *loaded = (uint32_t *)load_addr;
  168. for (int j = 0; j < data->segments[i].data_len/sizeof(uint32_t); j++) {
  169. loaded[j] ^= (j & 1) ? ram_obfs_value[0] : ram_obfs_value[1];
  170. }
  171. }
  172. }
  173. }
  174. #endif
  175. // Success!
  176. return ESP_OK;
  177. err:
  178. if (err == ESP_OK) {
  179. err = ESP_ERR_IMAGE_INVALID;
  180. }
  181. if (sha_handle != NULL) {
  182. // Need to finish the hash process to free the handle
  183. bootloader_sha256_finish(sha_handle, NULL);
  184. }
  185. // Prevent invalid/incomplete data leaking out
  186. bzero(data, sizeof(esp_image_metadata_t));
  187. return err;
  188. }
  189. static esp_err_t verify_image_header(uint32_t src_addr, const esp_image_header_t *image, bool silent)
  190. {
  191. esp_err_t err = ESP_OK;
  192. if (image->magic != ESP_IMAGE_HEADER_MAGIC) {
  193. if (!silent) {
  194. ESP_LOGE(TAG, "image at 0x%x has invalid magic byte", src_addr);
  195. }
  196. err = ESP_ERR_IMAGE_INVALID;
  197. }
  198. if (!silent) {
  199. if (image->spi_mode > ESP_IMAGE_SPI_MODE_SLOW_READ) {
  200. ESP_LOGW(TAG, "image at 0x%x has invalid SPI mode %d", src_addr, image->spi_mode);
  201. }
  202. if (image->spi_speed > ESP_IMAGE_SPI_SPEED_80M) {
  203. ESP_LOGW(TAG, "image at 0x%x has invalid SPI speed %d", src_addr, image->spi_speed);
  204. }
  205. if (image->spi_size > ESP_IMAGE_FLASH_SIZE_MAX) {
  206. ESP_LOGW(TAG, "image at 0x%x has invalid SPI size %d", src_addr, image->spi_size);
  207. }
  208. }
  209. return err;
  210. }
  211. static esp_err_t process_segment(int index, uint32_t flash_addr, esp_image_segment_header_t *header, bool silent, bool do_load, bootloader_sha256_handle_t sha_handle, uint32_t *checksum)
  212. {
  213. esp_err_t err;
  214. /* read segment header */
  215. err = bootloader_flash_read(flash_addr, header, sizeof(esp_image_segment_header_t), true);
  216. if (err != ESP_OK) {
  217. ESP_LOGE(TAG, "bootloader_flash_read failed at 0x%08x", flash_addr);
  218. return err;
  219. }
  220. if (sha_handle != NULL) {
  221. bootloader_sha256_data(sha_handle, header, sizeof(esp_image_segment_header_t));
  222. }
  223. intptr_t load_addr = header->load_addr;
  224. uint32_t data_len = header->data_len;
  225. uint32_t data_addr = flash_addr + sizeof(esp_image_segment_header_t);
  226. ESP_LOGV(TAG, "segment data length 0x%x data starts 0x%x", data_len, data_addr);
  227. err = verify_segment_header(index, header, data_addr, silent);
  228. if (err != ESP_OK) {
  229. return err;
  230. }
  231. if (data_len % 4 != 0) {
  232. FAIL_LOAD("unaligned segment length 0x%x", data_len);
  233. }
  234. bool is_mapping = should_map(load_addr);
  235. do_load = do_load && should_load(load_addr);
  236. if (!silent) {
  237. ESP_LOGI(TAG, "segment %d: paddr=0x%08x vaddr=0x%08x size=0x%05x (%6d) %s",
  238. index, data_addr, load_addr,
  239. data_len, data_len,
  240. (do_load)?"load":(is_mapping)?"map":"");
  241. }
  242. if (do_load) {
  243. /* Before loading segment, check it doesn't clobber bootloader RAM... */
  244. uint32_t end_addr = load_addr + data_len;
  245. if (end_addr < 0x40000000) {
  246. intptr_t sp = (intptr_t)get_sp();
  247. if (end_addr > sp - STACK_LOAD_HEADROOM) {
  248. ESP_LOGE(TAG, "Segment %d end address 0x%08x too high (bootloader stack 0x%08x liimit 0x%08x)",
  249. index, end_addr, sp, sp - STACK_LOAD_HEADROOM);
  250. return ESP_ERR_IMAGE_INVALID;
  251. }
  252. }
  253. }
  254. #ifndef BOOTLOADER_BUILD
  255. uint32_t page_count = spi_flash_mmap_get_free_pages(SPI_FLASH_MMAP_DATA);
  256. ESP_LOGI(TAG, "free data page_count 0x%08x",page_count);
  257. while (data_len >= page_count * SPI_FLASH_MMU_PAGE_SIZE) {
  258. err = process_segment_data(load_addr, data_addr, page_count * SPI_FLASH_MMU_PAGE_SIZE, do_load, sha_handle, checksum);
  259. if (err != ESP_OK) {
  260. return err;
  261. }
  262. data_addr += page_count * SPI_FLASH_MMU_PAGE_SIZE;
  263. data_len -= page_count * SPI_FLASH_MMU_PAGE_SIZE;
  264. }
  265. #endif
  266. err = process_segment_data(load_addr, data_addr, data_len, do_load, sha_handle, checksum);
  267. if (err != ESP_OK) {
  268. return err;
  269. }
  270. return ESP_OK;
  271. err:
  272. if (err == ESP_OK) {
  273. err = ESP_ERR_IMAGE_INVALID;
  274. }
  275. return err;
  276. }
  277. static esp_err_t process_segment_data(intptr_t load_addr, uint32_t data_addr, uint32_t data_len, bool do_load, bootloader_sha256_handle_t sha_handle, uint32_t *checksum)
  278. {
  279. const uint32_t *data = (const uint32_t *)bootloader_mmap(data_addr, data_len);
  280. if(!data) {
  281. ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed",
  282. data_addr, data_len);
  283. return ESP_FAIL;
  284. }
  285. #ifdef BOOTLOADER_BUILD
  286. // Set up the obfuscation value to use for loading
  287. while (ram_obfs_value[0] == 0 || ram_obfs_value[1] == 0) {
  288. bootloader_fill_random(ram_obfs_value, sizeof(ram_obfs_value));
  289. }
  290. uint32_t *dest = (uint32_t *)load_addr;
  291. #endif
  292. const uint32_t *src = data;
  293. for (int i = 0; i < data_len; i += 4) {
  294. int w_i = i/4; // Word index
  295. uint32_t w = src[w_i];
  296. *checksum ^= w;
  297. #ifdef BOOTLOADER_BUILD
  298. if (do_load) {
  299. dest[w_i] = w ^ ((w_i & 1) ? ram_obfs_value[0] : ram_obfs_value[1]);
  300. }
  301. #endif
  302. // SHA_CHUNK determined experimentally as the optimum size
  303. // to call bootloader_sha256_data() with. This is a bit
  304. // counter-intuitive, but it's ~3ms better than using the
  305. // SHA256 block size.
  306. const size_t SHA_CHUNK = 1024;
  307. if (sha_handle != NULL && i % SHA_CHUNK == 0) {
  308. bootloader_sha256_data(sha_handle, &src[w_i],
  309. MIN(SHA_CHUNK, data_len - i));
  310. }
  311. }
  312. bootloader_munmap(data);
  313. return ESP_OK;
  314. }
  315. static esp_err_t verify_segment_header(int index, const esp_image_segment_header_t *segment, uint32_t segment_data_offs, bool silent)
  316. {
  317. if ((segment->data_len & 3) != 0
  318. || segment->data_len >= SIXTEEN_MB) {
  319. if (!silent) {
  320. ESP_LOGE(TAG, "invalid segment length 0x%x", segment->data_len);
  321. }
  322. return ESP_ERR_IMAGE_INVALID;
  323. }
  324. uint32_t load_addr = segment->load_addr;
  325. bool map_segment = should_map(load_addr);
  326. /* Check that flash cache mapped segment aligns correctly from flash to its mapped address,
  327. relative to the 64KB page mapping size.
  328. */
  329. ESP_LOGV(TAG, "segment %d map_segment %d segment_data_offs 0x%x load_addr 0x%x",
  330. index, map_segment, segment_data_offs, load_addr);
  331. if (map_segment
  332. && ((segment_data_offs % SPI_FLASH_MMU_PAGE_SIZE) != (load_addr % SPI_FLASH_MMU_PAGE_SIZE))) {
  333. if (!silent) {
  334. ESP_LOGE(TAG, "Segment %d load address 0x%08x, doesn't match data 0x%08x",
  335. index, load_addr, segment_data_offs);
  336. }
  337. return ESP_ERR_IMAGE_INVALID;
  338. }
  339. return ESP_OK;
  340. }
  341. static bool should_map(uint32_t load_addr)
  342. {
  343. return (load_addr >= SOC_IROM_LOW && load_addr < SOC_IROM_HIGH)
  344. || (load_addr >= SOC_DROM_LOW && load_addr < SOC_DROM_HIGH);
  345. }
  346. static bool should_load(uint32_t load_addr)
  347. {
  348. /* Reload the RTC memory segments whenever a non-deepsleep reset
  349. is occurring */
  350. bool load_rtc_memory = rtc_get_reset_reason(0) != DEEPSLEEP_RESET;
  351. if (should_map(load_addr)) {
  352. return false;
  353. }
  354. if (load_addr < 0x10000000) {
  355. // Reserved for non-loaded addresses.
  356. // Current reserved values are
  357. // 0x0 (padding block)
  358. // 0x4 (unused, but reserved for an MD5 block)
  359. return false;
  360. }
  361. if (!load_rtc_memory) {
  362. if (load_addr >= SOC_RTC_IRAM_LOW && load_addr < SOC_RTC_IRAM_HIGH) {
  363. ESP_LOGD(TAG, "Skipping RTC code segment at 0x%08x\n", load_addr);
  364. return false;
  365. }
  366. if (load_addr >= SOC_RTC_DATA_LOW && load_addr < SOC_RTC_DATA_HIGH) {
  367. ESP_LOGD(TAG, "Skipping RTC data segment at 0x%08x\n", load_addr);
  368. return false;
  369. }
  370. }
  371. return true;
  372. }
  373. esp_err_t esp_image_verify_bootloader(uint32_t *length)
  374. {
  375. esp_image_metadata_t data;
  376. const esp_partition_pos_t bootloader_part = {
  377. .offset = ESP_BOOTLOADER_OFFSET,
  378. .size = ESP_PARTITION_TABLE_OFFSET - ESP_BOOTLOADER_OFFSET,
  379. };
  380. esp_err_t err = esp_image_load(ESP_IMAGE_VERIFY,
  381. &bootloader_part,
  382. &data);
  383. if (length != NULL) {
  384. *length = (err == ESP_OK) ? data.image_len : 0;
  385. }
  386. return err;
  387. }
  388. static esp_err_t verify_checksum(bootloader_sha256_handle_t sha_handle, uint32_t checksum_word, esp_image_metadata_t *data)
  389. {
  390. uint32_t unpadded_length = data->image_len;
  391. uint32_t length = unpadded_length + 1; // Add a byte for the checksum
  392. length = (length + 15) & ~15; // Pad to next full 16 byte block
  393. // Verify checksum
  394. uint8_t buf[16];
  395. esp_err_t err = bootloader_flash_read(data->start_addr + unpadded_length, buf, length - unpadded_length, true);
  396. uint8_t calc = buf[length - unpadded_length - 1];
  397. uint8_t checksum = (checksum_word >> 24)
  398. ^ (checksum_word >> 16)
  399. ^ (checksum_word >> 8)
  400. ^ (checksum_word >> 0);
  401. if (err != ESP_OK || checksum != calc) {
  402. ESP_LOGE(TAG, "Checksum failed. Calculated 0x%x read 0x%x", checksum, calc);
  403. return ESP_ERR_IMAGE_INVALID;
  404. }
  405. if (sha_handle != NULL) {
  406. bootloader_sha256_data(sha_handle, buf, length - unpadded_length);
  407. }
  408. if (data->image.hash_appended) {
  409. // Account for the hash in the total image length
  410. length += HASH_LEN;
  411. }
  412. data->image_len = length;
  413. return ESP_OK;
  414. }
  415. static void debug_log_hash(const uint8_t *image_hash, const char *caption);
  416. static esp_err_t verify_secure_boot_signature(bootloader_sha256_handle_t sha_handle, esp_image_metadata_t *data)
  417. {
  418. uint8_t image_hash[HASH_LEN] = { 0 };
  419. // For secure boot, we calculate the signature hash over the whole file, which includes any "simple" hash
  420. // appended to the image for corruption detection
  421. if (data->image.hash_appended) {
  422. const void *simple_hash = bootloader_mmap(data->start_addr + data->image_len - HASH_LEN, HASH_LEN);
  423. bootloader_sha256_data(sha_handle, simple_hash, HASH_LEN);
  424. bootloader_munmap(simple_hash);
  425. }
  426. bootloader_sha256_finish(sha_handle, image_hash);
  427. // Log the hash for debugging
  428. debug_log_hash(image_hash, "Calculated secure boot hash");
  429. // Use hash to verify signature block
  430. const esp_secure_boot_sig_block_t *sig_block = bootloader_mmap(data->start_addr + data->image_len, sizeof(esp_secure_boot_sig_block_t));
  431. esp_err_t err = esp_secure_boot_verify_signature_block(sig_block, image_hash);
  432. bootloader_munmap(sig_block);
  433. if (err != ESP_OK) {
  434. ESP_LOGE(TAG, "Secure boot signature verification failed");
  435. // Go back and check if the simple hash matches or not (we're off the fast path so we can re-hash the whole image now)
  436. ESP_LOGI(TAG, "Calculating simple hash to check for corruption...");
  437. const void *whole_image = bootloader_mmap(data->start_addr, data->image_len - HASH_LEN);
  438. if (whole_image != NULL) {
  439. sha_handle = bootloader_sha256_start();
  440. bootloader_sha256_data(sha_handle, whole_image, data->image_len - HASH_LEN);
  441. bootloader_munmap(whole_image);
  442. if (verify_simple_hash(sha_handle, data) != ESP_OK) {
  443. ESP_LOGW(TAG, "image corrupted on flash");
  444. } else {
  445. ESP_LOGW(TAG, "image valid, signature bad");
  446. }
  447. }
  448. return ESP_ERR_IMAGE_INVALID;
  449. }
  450. return ESP_OK;
  451. }
  452. static esp_err_t verify_simple_hash(bootloader_sha256_handle_t sha_handle, esp_image_metadata_t *data)
  453. {
  454. uint8_t image_hash[HASH_LEN] = { 0 };
  455. bootloader_sha256_finish(sha_handle, image_hash);
  456. // Log the hash for debugging
  457. debug_log_hash(image_hash, "Calculated hash");
  458. // Simple hash for verification only
  459. const void *hash = bootloader_mmap(data->start_addr + data->image_len - HASH_LEN, HASH_LEN);
  460. if (memcmp(hash, image_hash, HASH_LEN) != 0) {
  461. ESP_LOGE(TAG, "Image hash failed - image is corrupt");
  462. debug_log_hash(hash, "Expected hash");
  463. bootloader_munmap(hash);
  464. return ESP_ERR_IMAGE_INVALID;
  465. }
  466. bootloader_munmap(hash);
  467. return ESP_OK;
  468. }
  469. // Log a hash as a hex string
  470. static void debug_log_hash(const uint8_t *image_hash, const char *label)
  471. {
  472. #if BOOT_LOG_LEVEL >= LOG_LEVEL_DEBUG
  473. char hash_print[sizeof(image_hash)*2 + 1];
  474. hash_print[sizeof(image_hash)*2] = 0;
  475. for (int i = 0; i < sizeof(image_hash); i++) {
  476. for (int shift = 0; shift < 2; shift++) {
  477. uint8_t nibble = (image_hash[i] >> (shift ? 0 : 4)) & 0x0F;
  478. if (nibble < 10) {
  479. hash_print[i*2+shift] = '0' + nibble;
  480. } else {
  481. hash_print[i*2+shift] = 'a' + nibble - 10;
  482. }
  483. }
  484. }
  485. ESP_LOGD(TAG, "%s: %s", label, hash_print);
  486. #endif
  487. }