boot/espressif: Update main.c to call init and loader functions
Add ESP specific loader code to load host application
Signed-off-by: Shubham Kulkarni <shubham.kulkarni@espressif.com>
diff --git a/boot/espressif/CMakeLists.txt b/boot/espressif/CMakeLists.txt
index de2b3cf..24952f3 100644
--- a/boot/espressif/CMakeLists.txt
+++ b/boot/espressif/CMakeLists.txt
@@ -20,6 +20,14 @@
execute_process(
COMMAND git describe --tags
+ WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
+ OUTPUT_VARIABLE MCUBOOT_VER
+ OUTPUT_STRIP_TRAILING_WHITESPACE
+ )
+add_definitions(-DMCUBOOT_VER=\"${MCUBOOT_VER}\")
+
+execute_process(
+ COMMAND git describe --tags
WORKING_DIRECTORY ${IDF_PATH}
OUTPUT_VARIABLE IDF_VER
OUTPUT_STRIP_TRAILING_WHITESPACE
@@ -145,6 +153,7 @@
${bootutil_srcs}
${mbedtls_srcs}
${CMAKE_CURRENT_LIST_DIR}/port/esp_mcuboot.c
+ ${CMAKE_CURRENT_LIST_DIR}/port/esp_loader.c
)
target_include_directories(
diff --git a/boot/espressif/include/esp_loader.h b/boot/espressif/include/esp_loader.h
new file mode 100644
index 0000000..e22ce06
--- /dev/null
+++ b/boot/espressif/include/esp_loader.h
@@ -0,0 +1,9 @@
+/*
+ * Copyright (c) 2021 Espressif Systems (Shanghai) Co., Ltd.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#pragma once
+
+void esp_app_image_load(int slot, unsigned int hdr_offset);
diff --git a/boot/espressif/main.c b/boot/espressif/main.c
index 9b98fbf..c5bb3e0 100644
--- a/boot/espressif/main.c
+++ b/boot/espressif/main.c
@@ -5,14 +5,35 @@
*/
#include <bootutil/bootutil.h>
+#include <bootutil/image.h>
+
+#include <mcuboot_config/mcuboot_logging.h>
+
+#include <bootloader_init.h>
+#include <esp_loader.h>
+
+void do_boot(struct boot_rsp *rsp)
+{
+ MCUBOOT_LOG_INF("br_image_off = 0x%x", rsp->br_image_off);
+ MCUBOOT_LOG_INF("ih_hdr_size = 0x%x", rsp->br_hdr->ih_hdr_size);
+ int slot = (rsp->br_image_off == CONFIG_ESP_APPLICATION_PRIMARY_START_ADDRESS) ? 0 : 1;
+ esp_app_image_load(slot, rsp->br_hdr->ih_hdr_size);
+}
int main()
{
+ bootloader_init();
struct boot_rsp rsp;
- int rv = boot_go(&rsp);
+#ifdef MCUBOOT_VER
+ MCUBOOT_LOG_INF("*** Booting MCUBoot build %s ***", MCUBOOT_VER);
+#endif
- if (rv == 0) {
- //
+ fih_int fih_rc = FIH_FAILURE;
+ FIH_CALL(boot_go, fih_rc, &rsp);
+ if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
+ MCUBOOT_LOG_ERR("Unable to find bootable image");
+ FIH_PANIC;
}
+ do_boot(&rsp);
while(1);
}
diff --git a/boot/espressif/port/esp_loader.c b/boot/espressif/port/esp_loader.c
new file mode 100644
index 0000000..878dd55
--- /dev/null
+++ b/boot/espressif/port/esp_loader.c
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2021 Espressif Systems (Shanghai) Co., Ltd.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include <string.h>
+#include <soc/soc.h>
+#include <soc/dport_reg.h>
+#include "soc/soc_memory_layout.h"
+#include <bootloader_flash.h>
+#include <bootloader_flash_priv.h>
+
+#include "esp32/rom/cache.h"
+#include "esp32/rom/efuse.h"
+#include "esp32/rom/ets_sys.h"
+#include "esp32/rom/spi_flash.h"
+#include "esp32/rom/crc.h"
+#include "esp32/rom/rtc.h"
+#include "esp32/rom/gpio.h"
+#include "esp32/rom/uart.h"
+
+#include <esp_loader.h>
+#include <bootutil/fault_injection_hardening.h>
+#include <flash_map_backend/flash_map_backend.h>
+#include <mcuboot_config/mcuboot_logging.h>
+
+#define ESP_LOAD_HEADER_MAGIC 0xace637d3 /* Magic is derived from sha256sum of espmcuboot */
+
+/*
+ * Load header that should be a part of application image.
+ */
+typedef struct image_load_header {
+ uint32_t header_magic; /* Magic for load header */
+ uint32_t entry_addr; /* Application entry address */
+ uint32_t iram_dest_addr; /* Destination address(VMA) for IRAM region */
+ uint32_t iram_flash_offset; /* Flash offset(LMA) for start of IRAM region */
+ uint32_t iram_size; /* Size of IRAM region */
+ uint32_t dram_dest_addr; /* Destination address(VMA) for DRAM region */
+ uint32_t dram_flash_offset; /* Flash offset(LMA) for start of DRAM region */
+ uint32_t dram_size; /* Size of DRAM region */
+} image_load_header_t;
+
+static int load_segment(const struct flash_area *fap, uint32_t data_addr, uint32_t data_len, uint32_t load_addr)
+{
+ const uint32_t *data = (const uint32_t *)bootloader_mmap((fap->fa_off + data_addr), data_len);
+ if (!data) {
+ MCUBOOT_LOG_ERR("%s: Bootloader nmap failed", __func__);
+ return -1;
+ }
+ memcpy((void *)load_addr, data, data_len);
+ bootloader_munmap(data);
+ return 0;
+}
+
+void esp_app_image_load(int slot, unsigned int hdr_offset)
+{
+ const struct flash_area *fap;
+ int area_id;
+ int rc;
+
+ image_load_header_t load_header = {0};
+
+ area_id = flash_area_id_from_image_slot(slot);
+ rc = flash_area_open(area_id, &fap);
+ if (rc != 0) {
+ MCUBOOT_LOG_ERR("%s: flash_area_open failed with %d", __func__, rc);
+ }
+
+ const uint32_t *data = (const uint32_t *)bootloader_mmap((fap->fa_off + hdr_offset), sizeof(image_load_header_t));
+ memcpy((void *)&load_header, data, sizeof(image_load_header_t));
+ bootloader_munmap(data);
+
+ if (load_header.header_magic != ESP_LOAD_HEADER_MAGIC) {
+ MCUBOOT_LOG_ERR("Load header magic verification failed. Aborting");
+ FIH_PANIC;
+ }
+
+ 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))) {
+ MCUBOOT_LOG_ERR("IRAM region in load header is not valid. Aborting");
+ FIH_PANIC;
+ }
+
+ 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)) {
+ MCUBOOT_LOG_ERR("DRAM region in load header is not valid. Aborting");
+ FIH_PANIC;
+ }
+
+ if (!esp_ptr_in_iram((void *)load_header.entry_addr)) {
+ MCUBOOT_LOG_ERR("Application entry point is not in IRAM. Aborting");
+ FIH_PANIC;
+ }
+
+ 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);
+ load_segment(fap, load_header.dram_flash_offset, load_header.dram_size, load_header.dram_dest_addr);
+
+ 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);
+ load_segment(fap, load_header.iram_flash_offset, load_header.iram_size, load_header.iram_dest_addr);
+
+ MCUBOOT_LOG_INF("start=0x%x", load_header.entry_addr);
+ uart_tx_wait_idle(0);
+ void *start = (void *) load_header.entry_addr;
+ ((void (*)(void))start)(); /* Call to application entry address should not return */
+ FIH_PANIC;
+}