blob: 1d3e55f252f0ea6e32cc5c7d1c366a5bb8611089 [file] [log] [blame]
Shubham Kulkarni8787bb02021-07-20 11:46:03 +05301/*
Almir Okato14763b12021-11-25 00:45:26 -03002 * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
Shubham Kulkarni8787bb02021-07-20 11:46:03 +05303 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#include <string.h>
Shubham Kulkarni8787bb02021-07-20 11:46:03 +05308
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -03009#include <bootutil/bootutil_log.h>
10#include <bootutil/fault_injection_hardening.h>
11
12#include "bootloader_flash_priv.h"
Almir Okato14763b12021-11-25 00:45:26 -030013#include "esp_flash_encrypt.h"
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -030014#include "soc/soc_memory_layout.h"
15
16#if CONFIG_IDF_TARGET_ESP32
17#include "esp32/rom/uart.h"
18#elif CONFIG_IDF_TARGET_ESP32S2
19#include "esp32s2/rom/uart.h"
Almir Okato42e679d2022-01-18 00:16:58 -030020#elif CONFIG_IDF_TARGET_ESP32S3
21#include "esp32s3/rom/uart.h"
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -030022#elif CONFIG_IDF_TARGET_ESP32C3
23#include "esp32c3/rom/uart.h"
Almir Okato712fdb52021-08-06 10:22:56 -030024#endif
25
Almir Okato14763b12021-11-25 00:45:26 -030026#include "esp_mcuboot_image.h"
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -030027#include "esp_loader.h"
28#include "flash_map_backend/flash_map_backend.h"
Shubham Kulkarni8787bb02021-07-20 11:46:03 +053029
Shubham Kulkarni8787bb02021-07-20 11:46:03 +053030
31static int load_segment(const struct flash_area *fap, uint32_t data_addr, uint32_t data_len, uint32_t load_addr)
32{
33 const uint32_t *data = (const uint32_t *)bootloader_mmap((fap->fa_off + data_addr), data_len);
34 if (!data) {
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -030035 BOOT_LOG_ERR("%s: Bootloader mmap failed", __func__);
Shubham Kulkarni8787bb02021-07-20 11:46:03 +053036 return -1;
37 }
38 memcpy((void *)load_addr, data, data_len);
39 bootloader_munmap(data);
40 return 0;
41}
42
43void esp_app_image_load(int slot, unsigned int hdr_offset)
44{
45 const struct flash_area *fap;
46 int area_id;
47 int rc;
48
Shubham Kulkarni8787bb02021-07-20 11:46:03 +053049 area_id = flash_area_id_from_image_slot(slot);
50 rc = flash_area_open(area_id, &fap);
51 if (rc != 0) {
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -030052 BOOT_LOG_ERR("%s: flash_area_open failed with %d", __func__, rc);
Shubham Kulkarni8787bb02021-07-20 11:46:03 +053053 }
54
Almir Okato14763b12021-11-25 00:45:26 -030055 const uint32_t *data = (const uint32_t *)bootloader_mmap((fap->fa_off + hdr_offset), sizeof(esp_image_load_header_t));
56 esp_image_load_header_t load_header = {0};
57 memcpy((void *)&load_header, data, sizeof(esp_image_load_header_t));
Shubham Kulkarni8787bb02021-07-20 11:46:03 +053058 bootloader_munmap(data);
59
60 if (load_header.header_magic != ESP_LOAD_HEADER_MAGIC) {
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -030061 BOOT_LOG_ERR("Load header magic verification failed. Aborting");
Shubham Kulkarni8787bb02021-07-20 11:46:03 +053062 FIH_PANIC;
63 }
64
65 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))) {
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -030066 BOOT_LOG_ERR("IRAM region in load header is not valid. Aborting");
Shubham Kulkarni8787bb02021-07-20 11:46:03 +053067 FIH_PANIC;
68 }
69
70 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)) {
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -030071 BOOT_LOG_ERR("DRAM region in load header is not valid. Aborting");
Shubham Kulkarni8787bb02021-07-20 11:46:03 +053072 FIH_PANIC;
73 }
74
75 if (!esp_ptr_in_iram((void *)load_header.entry_addr)) {
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -030076 BOOT_LOG_ERR("Application entry point (0x%x) is not in IRAM. Aborting", load_header.entry_addr);
Shubham Kulkarni8787bb02021-07-20 11:46:03 +053077 FIH_PANIC;
78 }
79
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -030080 BOOT_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);
Shubham Kulkarni8787bb02021-07-20 11:46:03 +053081 load_segment(fap, load_header.dram_flash_offset, load_header.dram_size, load_header.dram_dest_addr);
82
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -030083 BOOT_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);
Shubham Kulkarni8787bb02021-07-20 11:46:03 +053084 load_segment(fap, load_header.iram_flash_offset, load_header.iram_size, load_header.iram_dest_addr);
85
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -030086 BOOT_LOG_INF("start=0x%x", load_header.entry_addr);
Shubham Kulkarni8787bb02021-07-20 11:46:03 +053087 uart_tx_wait_idle(0);
88 void *start = (void *) load_header.entry_addr;
89 ((void (*)(void))start)(); /* Call to application entry address should not return */
90 FIH_PANIC;
91}