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