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_memory_utils.h"
13 #include "bootloader_flash_priv.h"
14 #include "esp_flash_encrypt.h"
15
16 #include "rom/uart.h"
17
18 #include "esp_mcuboot_image.h"
19 #include "esp_loader.h"
20 #include "flash_map_backend/flash_map_backend.h"
21
22 #ifdef CONFIG_ESP_MULTI_PROCESSOR_BOOT
23 #include "app_cpu_start.h"
24 #endif
25
load_segment(const struct flash_area * fap,uint32_t data_addr,uint32_t data_len,uint32_t load_addr)26 static int load_segment(const struct flash_area *fap, uint32_t data_addr, uint32_t data_len, uint32_t load_addr)
27 {
28 const uint32_t *data = (const uint32_t *)bootloader_mmap((fap->fa_off + data_addr), data_len);
29 if (!data) {
30 BOOT_LOG_ERR("%s: Bootloader mmap failed", __func__);
31 return -1;
32 }
33 memcpy((void *)load_addr, data, data_len);
34 bootloader_munmap(data);
35 return 0;
36 }
37
esp_app_image_load(int image_index,int slot,unsigned int hdr_offset,unsigned int * entry_addr)38 void esp_app_image_load(int image_index, int slot, unsigned int hdr_offset, unsigned int *entry_addr)
39 {
40 const struct flash_area *fap;
41 int area_id;
42 int rc;
43
44 area_id = flash_area_id_from_multi_image_slot(image_index, slot);
45 rc = flash_area_open(area_id, &fap);
46 if (rc != 0) {
47 BOOT_LOG_ERR("%s: flash_area_open failed with %d", __func__, rc);
48 }
49
50 BOOT_LOG_INF("Loading image %d - slot %d from flash, area id: %d", image_index, slot, area_id);
51
52 const uint32_t *data = (const uint32_t *)bootloader_mmap((fap->fa_off + hdr_offset), sizeof(esp_image_load_header_t));
53 esp_image_load_header_t load_header = {0};
54 memcpy((void *)&load_header, data, sizeof(esp_image_load_header_t));
55 bootloader_munmap(data);
56
57 if (load_header.header_magic != ESP_LOAD_HEADER_MAGIC) {
58 BOOT_LOG_ERR("Load header magic verification failed. Aborting");
59 FIH_PANIC;
60 }
61
62 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))) {
63 BOOT_LOG_ERR("IRAM region in load header is not valid. Aborting");
64 FIH_PANIC;
65 }
66
67 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))) {
68 BOOT_LOG_ERR("DRAM region in load header is not valid. Aborting");
69 FIH_PANIC;
70 }
71
72 if (!esp_ptr_in_iram((void *)load_header.entry_addr)) {
73 BOOT_LOG_ERR("Application entry point (0x%x) is not in IRAM. Aborting", load_header.entry_addr);
74 FIH_PANIC;
75 }
76
77 BOOT_LOG_INF("DRAM segment: start=0x%x, size=0x%x, vaddr=0x%x", fap->fa_off + load_header.dram_flash_offset, load_header.dram_size, load_header.dram_dest_addr);
78 load_segment(fap, load_header.dram_flash_offset, load_header.dram_size, load_header.dram_dest_addr);
79
80 BOOT_LOG_INF("IRAM segment: start=0x%x, size=0x%x, vaddr=0x%x", fap->fa_off + load_header.iram_flash_offset, load_header.iram_size, load_header.iram_dest_addr);
81 load_segment(fap, load_header.iram_flash_offset, load_header.iram_size, load_header.iram_dest_addr);
82
83 BOOT_LOG_INF("start=0x%x", load_header.entry_addr);
84 uart_tx_wait_idle(0);
85
86 assert(entry_addr != NULL);
87 *entry_addr = load_header.entry_addr;
88 }
89
start_cpu0_image(int image_index,int slot,unsigned int hdr_offset)90 void start_cpu0_image(int image_index, int slot, unsigned int hdr_offset)
91 {
92 unsigned int entry_addr;
93 esp_app_image_load(image_index, slot, hdr_offset, &entry_addr);
94 ((void (*)(void))entry_addr)(); /* Call to application entry address should not return */
95 FIH_PANIC; /* It should not get here */
96 }
97
98 #ifdef CONFIG_ESP_MULTI_PROCESSOR_BOOT
start_cpu1_image(int image_index,int slot,unsigned int hdr_offset)99 void start_cpu1_image(int image_index, int slot, unsigned int hdr_offset)
100 {
101 unsigned int entry_addr;
102 esp_app_image_load(image_index, slot, hdr_offset, &entry_addr);
103 appcpu_start(entry_addr);
104 }
105 #endif
106