Shubham Kulkarni | 8787bb0 | 2021-07-20 11:46:03 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 Espressif Systems (Shanghai) Co., Ltd. |
| 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | */ |
| 6 | |
| 7 | #include <string.h> |
| 8 | #include <soc/soc.h> |
Shubham Kulkarni | 8787bb0 | 2021-07-20 11:46:03 +0530 | [diff] [blame] | 9 | #include "soc/soc_memory_layout.h" |
| 10 | #include <bootloader_flash.h> |
| 11 | #include <bootloader_flash_priv.h> |
| 12 | |
Almir Okato | 712fdb5 | 2021-08-06 10:22:56 -0300 | [diff] [blame] | 13 | #if defined(CONFIG_IDF_TARGET_ESP32) || defined(CONFIG_IDF_TARGET_ESP32S2) |
| 14 | #include <soc/dport_reg.h> |
| 15 | #endif |
| 16 | |
Almir Okato | d532029 | 2021-06-18 02:00:40 -0300 | [diff] [blame] | 17 | #include "rom/cache.h" |
| 18 | #include "rom/efuse.h" |
| 19 | #include "rom/ets_sys.h" |
| 20 | #include "rom/spi_flash.h" |
| 21 | #include "rom/crc.h" |
| 22 | #include "rom/rtc.h" |
| 23 | #include "rom/gpio.h" |
| 24 | #include "rom/uart.h" |
Shubham Kulkarni | 8787bb0 | 2021-07-20 11:46:03 +0530 | [diff] [blame] | 25 | |
| 26 | #include <esp_loader.h> |
| 27 | #include <bootutil/fault_injection_hardening.h> |
| 28 | #include <flash_map_backend/flash_map_backend.h> |
| 29 | #include <mcuboot_config/mcuboot_logging.h> |
| 30 | |
| 31 | #define ESP_LOAD_HEADER_MAGIC 0xace637d3 /* Magic is derived from sha256sum of espmcuboot */ |
| 32 | |
| 33 | /* |
| 34 | * Load header that should be a part of application image. |
| 35 | */ |
| 36 | typedef struct image_load_header { |
| 37 | uint32_t header_magic; /* Magic for load header */ |
| 38 | uint32_t entry_addr; /* Application entry address */ |
| 39 | uint32_t iram_dest_addr; /* Destination address(VMA) for IRAM region */ |
| 40 | uint32_t iram_flash_offset; /* Flash offset(LMA) for start of IRAM region */ |
| 41 | uint32_t iram_size; /* Size of IRAM region */ |
| 42 | uint32_t dram_dest_addr; /* Destination address(VMA) for DRAM region */ |
| 43 | uint32_t dram_flash_offset; /* Flash offset(LMA) for start of DRAM region */ |
| 44 | uint32_t dram_size; /* Size of DRAM region */ |
| 45 | } image_load_header_t; |
| 46 | |
| 47 | static int load_segment(const struct flash_area *fap, uint32_t data_addr, uint32_t data_len, uint32_t load_addr) |
| 48 | { |
| 49 | const uint32_t *data = (const uint32_t *)bootloader_mmap((fap->fa_off + data_addr), data_len); |
| 50 | if (!data) { |
| 51 | MCUBOOT_LOG_ERR("%s: Bootloader nmap failed", __func__); |
| 52 | return -1; |
| 53 | } |
| 54 | memcpy((void *)load_addr, data, data_len); |
| 55 | bootloader_munmap(data); |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | void esp_app_image_load(int slot, unsigned int hdr_offset) |
| 60 | { |
| 61 | const struct flash_area *fap; |
| 62 | int area_id; |
| 63 | int rc; |
| 64 | |
| 65 | image_load_header_t load_header = {0}; |
| 66 | |
| 67 | area_id = flash_area_id_from_image_slot(slot); |
| 68 | rc = flash_area_open(area_id, &fap); |
| 69 | if (rc != 0) { |
| 70 | MCUBOOT_LOG_ERR("%s: flash_area_open failed with %d", __func__, rc); |
| 71 | } |
| 72 | |
| 73 | const uint32_t *data = (const uint32_t *)bootloader_mmap((fap->fa_off + hdr_offset), sizeof(image_load_header_t)); |
| 74 | memcpy((void *)&load_header, data, sizeof(image_load_header_t)); |
| 75 | bootloader_munmap(data); |
| 76 | |
| 77 | if (load_header.header_magic != ESP_LOAD_HEADER_MAGIC) { |
| 78 | MCUBOOT_LOG_ERR("Load header magic verification failed. Aborting"); |
| 79 | FIH_PANIC; |
| 80 | } |
| 81 | |
| 82 | 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))) { |
| 83 | MCUBOOT_LOG_ERR("IRAM region in load header is not valid. Aborting"); |
| 84 | FIH_PANIC; |
| 85 | } |
| 86 | |
| 87 | 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)) { |
| 88 | MCUBOOT_LOG_ERR("DRAM region in load header is not valid. Aborting"); |
| 89 | FIH_PANIC; |
| 90 | } |
| 91 | |
| 92 | if (!esp_ptr_in_iram((void *)load_header.entry_addr)) { |
Almir Okato | 712fdb5 | 2021-08-06 10:22:56 -0300 | [diff] [blame] | 93 | MCUBOOT_LOG_ERR("Application entry point (0x%x) is not in IRAM. Aborting", load_header.entry_addr); |
Shubham Kulkarni | 8787bb0 | 2021-07-20 11:46:03 +0530 | [diff] [blame] | 94 | FIH_PANIC; |
| 95 | } |
| 96 | |
| 97 | MCUBOOT_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); |
| 98 | load_segment(fap, load_header.dram_flash_offset, load_header.dram_size, load_header.dram_dest_addr); |
| 99 | |
| 100 | MCUBOOT_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); |
| 101 | load_segment(fap, load_header.iram_flash_offset, load_header.iram_size, load_header.iram_dest_addr); |
| 102 | |
| 103 | MCUBOOT_LOG_INF("start=0x%x", load_header.entry_addr); |
| 104 | uart_tx_wait_idle(0); |
| 105 | void *start = (void *) load_header.entry_addr; |
| 106 | ((void (*)(void))start)(); /* Call to application entry address should not return */ |
| 107 | FIH_PANIC; |
| 108 | } |