Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 1 | /* |
| 2 | * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD |
| 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | */ |
| 6 | |
| 7 | #include <string.h> |
| 8 | |
| 9 | #include <bootutil/bootutil_log.h> |
| 10 | #include <bootutil/fault_injection_hardening.h> |
| 11 | |
| 12 | #include "bootloader_flash_priv.h" |
| 13 | #include "esp_flash_encrypt.h" |
| 14 | #include "soc/soc_memory_layout.h" |
| 15 | |
| 16 | #if CONFIG_IDF_TARGET_ESP32 |
| 17 | #include "esp32/rom/uart.h" |
| 18 | #elif CONFIG_IDF_TARGET_ESP32S2 |
| 19 | #include "esp32s2/rom/uart.h" |
| 20 | #elif CONFIG_IDF_TARGET_ESP32S3 |
| 21 | #include "esp32s3/rom/uart.h" |
| 22 | #elif CONFIG_IDF_TARGET_ESP32C3 |
| 23 | #include "esp32c3/rom/uart.h" |
| 24 | #endif |
| 25 | |
| 26 | #include "esp_mcuboot_image.h" |
| 27 | #include "esp_loader.h" |
| 28 | #include "flash_map_backend/flash_map_backend.h" |
| 29 | |
| 30 | |
| 31 | static int load_segment(const struct flash_area *fap, uint32_t data_addr, uint32_t data_len, uint32_t load_addr) |
| 32 | { |
| 33 | const uint32_t *data = (const uint32_t *)bootloader_mmap((fap->fa_off + data_addr), data_len); |
| 34 | if (!data) { |
| 35 | BOOT_LOG_ERR("%s: Bootloader mmap failed", __func__); |
| 36 | return -1; |
| 37 | } |
| 38 | memcpy((void *)load_addr, data, data_len); |
| 39 | bootloader_munmap(data); |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | void esp_app_image_load(int slot, unsigned int hdr_offset) |
| 44 | { |
| 45 | const struct flash_area *fap; |
| 46 | int area_id; |
| 47 | int rc; |
| 48 | |
| 49 | area_id = flash_area_id_from_image_slot(slot); |
| 50 | rc = flash_area_open(area_id, &fap); |
| 51 | if (rc != 0) { |
| 52 | BOOT_LOG_ERR("%s: flash_area_open failed with %d", __func__, rc); |
| 53 | } |
| 54 | |
| 55 | const uint32_t *data = (const uint32_t *)bootloader_mmap((fap->fa_off + hdr_offset), sizeof(esp_image_load_header_t)); |
| 56 | esp_image_load_header_t load_header = {0}; |
| 57 | memcpy((void *)&load_header, data, sizeof(esp_image_load_header_t)); |
| 58 | bootloader_munmap(data); |
| 59 | |
| 60 | if (load_header.header_magic != ESP_LOAD_HEADER_MAGIC) { |
| 61 | BOOT_LOG_ERR("Load header magic verification failed. Aborting"); |
| 62 | FIH_PANIC; |
| 63 | } |
| 64 | |
| 65 | if (!esp_ptr_in_iram((void *)load_header.iram_dest_addr) || !esp_ptr_in_iram((void *)(load_header.iram_dest_addr + load_header.iram_size))) { |
| 66 | BOOT_LOG_ERR("IRAM region in load header is not valid. Aborting"); |
| 67 | FIH_PANIC; |
| 68 | } |
| 69 | |
| 70 | if (!esp_ptr_in_dram((void *)load_header.dram_dest_addr) || !esp_ptr_in_dram((void *)load_header.dram_dest_addr + load_header.dram_size)) { |
| 71 | BOOT_LOG_ERR("DRAM region in load header is not valid. Aborting"); |
| 72 | FIH_PANIC; |
| 73 | } |
| 74 | |
| 75 | if (!esp_ptr_in_iram((void *)load_header.entry_addr)) { |
| 76 | BOOT_LOG_ERR("Application entry point (0x%x) is not in IRAM. Aborting", load_header.entry_addr); |
| 77 | FIH_PANIC; |
| 78 | } |
| 79 | |
| 80 | BOOT_LOG_INF("DRAM segment: start=0x%x, size=0x%x, vaddr=0x%x", load_header.dram_flash_offset, load_header.dram_size, load_header.dram_dest_addr); |
| 81 | load_segment(fap, load_header.dram_flash_offset, load_header.dram_size, load_header.dram_dest_addr); |
| 82 | |
| 83 | BOOT_LOG_INF("IRAM segment: start=0x%x, size=0x%x, vaddr=0x%x", load_header.iram_flash_offset, load_header.iram_size, load_header.iram_dest_addr); |
| 84 | load_segment(fap, load_header.iram_flash_offset, load_header.iram_size, load_header.iram_dest_addr); |
| 85 | |
| 86 | BOOT_LOG_INF("start=0x%x", load_header.entry_addr); |
| 87 | uart_tx_wait_idle(0); |
| 88 | void *start = (void *) load_header.entry_addr; |
| 89 | ((void (*)(void))start)(); /* Call to application entry address should not return */ |
| 90 | FIH_PANIC; |
| 91 | } |