|
22 | 22 | #ifdef CONFIG_SECURE_FLASH_ENC_ENABLED
|
23 | 23 | #include "esp_flash_encrypt.h"
|
24 | 24 | #endif
|
| 25 | +#ifdef CONFIG_ESP_MULTI_PROCESSOR_BOOT |
| 26 | +#include "app_cpu_start.h" |
| 27 | +#endif |
25 | 28 |
|
26 | 29 | #include "esp_loader.h"
|
27 | 30 | #include "os/os_malloc.h"
|
28 | 31 |
|
| 32 | +#define IMAGE_INDEX_0 0 |
| 33 | +#define IMAGE_INDEX_1 1 |
| 34 | + |
| 35 | +#define PRIMARY_SLOT 0 |
| 36 | +#define SECONDARY_SLOT 1 |
| 37 | + |
29 | 38 | #ifdef CONFIG_SECURE_BOOT
|
30 | 39 | extern esp_err_t check_and_generate_secure_boot_keys(void);
|
31 | 40 | #endif
|
32 | 41 |
|
33 | 42 | void do_boot(struct boot_rsp *rsp)
|
34 | 43 | {
|
| 44 | + unsigned int entry_addr; |
35 | 45 | BOOT_LOG_INF("br_image_off = 0x%x", rsp->br_image_off);
|
36 | 46 | BOOT_LOG_INF("ih_hdr_size = 0x%x", rsp->br_hdr->ih_hdr_size);
|
37 |
| - int slot = (rsp->br_image_off == CONFIG_ESP_APPLICATION_PRIMARY_START_ADDRESS) ? 0 : 1; |
38 |
| - esp_app_image_load(slot, rsp->br_hdr->ih_hdr_size); |
| 47 | + int slot = (rsp->br_image_off == CONFIG_ESP_IMAGE0_PRIMARY_START_ADDRESS) ? PRIMARY_SLOT : SECONDARY_SLOT; |
| 48 | + esp_app_image_load(IMAGE_INDEX_0, slot, rsp->br_hdr->ih_hdr_size, &entry_addr); |
| 49 | + ((void (*)(void))entry_addr)(); /* Call to application entry address should not return */ |
| 50 | + FIH_PANIC; /* It should not get here */ |
| 51 | +} |
| 52 | + |
| 53 | +#ifdef CONFIG_ESP_MULTI_PROCESSOR_BOOT |
| 54 | +int read_image_header(uint32_t img_index, uint32_t slot, struct image_header *img_header) |
| 55 | +{ |
| 56 | + const struct flash_area *fap; |
| 57 | + int area_id; |
| 58 | + int rc = 0; |
| 59 | + |
| 60 | + area_id = flash_area_id_from_multi_image_slot(img_index, slot); |
| 61 | + rc = flash_area_open(area_id, &fap); |
| 62 | + if (rc != 0) { |
| 63 | + rc = BOOT_EFLASH; |
| 64 | + goto done; |
| 65 | + } |
| 66 | + |
| 67 | + if (flash_area_read(fap, 0, img_header, sizeof(struct image_header))) { |
| 68 | + rc = BOOT_EFLASH; |
| 69 | + goto done; |
| 70 | + } |
| 71 | + |
| 72 | + BOOT_LOG_INF("Image offset = 0x%x", fap->fa_off); |
| 73 | + BOOT_LOG_INF("Image header size = 0x%x", img_header->ih_hdr_size); |
| 74 | + |
| 75 | +done: |
| 76 | + flash_area_close(fap); |
| 77 | + return rc; |
| 78 | +} |
| 79 | + |
| 80 | +void do_boot_appcpu(uint32_t img_index, uint32_t slot) |
| 81 | +{ |
| 82 | + unsigned int entry_addr; |
| 83 | + struct image_header img_header; |
| 84 | + |
| 85 | + if (read_image_header(img_index, slot, &img_header) != 0) { |
| 86 | + FIH_PANIC; |
| 87 | + } |
| 88 | + |
| 89 | + esp_app_image_load(img_index, slot, img_header.ih_hdr_size, &entry_addr); |
| 90 | + appcpu_start(entry_addr); |
39 | 91 | }
|
| 92 | +#endif |
40 | 93 |
|
41 | 94 | int main()
|
42 | 95 | {
|
@@ -97,8 +150,10 @@ int main()
|
97 | 150 | * 2) MCUboot validates the application images and prepares the booting process.
|
98 | 151 | */
|
99 | 152 |
|
| 153 | + /* MCUboot's boot_go validates and checks all images for update and returns |
| 154 | + * the load information for booting the main image |
| 155 | + */ |
100 | 156 | FIH_CALL(boot_go, fih_rc, &rsp);
|
101 |
| - |
102 | 157 | if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
|
103 | 158 | BOOT_LOG_ERR("Unable to find bootable image");
|
104 | 159 | #ifdef CONFIG_SECURE_BOOT
|
@@ -165,6 +220,13 @@ int main()
|
165 | 220 | BOOT_LOG_INF("Disabling RNG early entropy source...");
|
166 | 221 | bootloader_random_disable();
|
167 | 222 |
|
| 223 | +#ifdef CONFIG_ESP_MULTI_PROCESSOR_BOOT |
| 224 | + /* Multi image independent boot |
| 225 | + * Boot on the second processor happens before the image0 boot |
| 226 | + */ |
| 227 | + do_boot_appcpu(IMAGE_INDEX_1, PRIMARY_SLOT); |
| 228 | +#endif |
| 229 | + |
168 | 230 | do_boot(&rsp);
|
169 | 231 |
|
170 | 232 | while(1);
|
|
0 commit comments