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