espressif:esp32: Move app entry point call back to iram_loader_seg region

Entry point call was moved back from main to esp_loader, so it is
called from iram_loader_seg memory region

Signed-off-by: Almir Okato <almir.okato@espressif.com>
diff --git a/boot/espressif/include/esp_loader.h b/boot/espressif/include/esp_loader.h
index dc55373..480022c 100644
--- a/boot/espressif/include/esp_loader.h
+++ b/boot/espressif/include/esp_loader.h
@@ -6,4 +6,9 @@
 
 #pragma once
 
+void start_cpu0_image(int image_index, int slot, unsigned int hdr_offset);
+#ifdef CONFIG_ESP_MULTI_PROCESSOR_BOOT
+void start_cpu1_image(int image_index, int slot, unsigned int hdr_offset);
+#endif
+
 void esp_app_image_load(int image_index, int slot, unsigned int hdr_offset, unsigned int *entry_addr);
diff --git a/boot/espressif/main.c b/boot/espressif/main.c
index 6e59b00..b3d2b76 100644
--- a/boot/espressif/main.c
+++ b/boot/espressif/main.c
@@ -22,9 +22,6 @@
 #ifdef CONFIG_SECURE_FLASH_ENC_ENABLED
 #include "esp_flash_encrypt.h"
 #endif
-#ifdef CONFIG_ESP_MULTI_PROCESSOR_BOOT
-#include "app_cpu_start.h"
-#endif
 
 #include "esp_loader.h"
 #include "os/os_malloc.h"
@@ -41,13 +38,10 @@
 
 void do_boot(struct boot_rsp *rsp)
 {
-    unsigned int entry_addr;
     BOOT_LOG_INF("br_image_off = 0x%x", rsp->br_image_off);
     BOOT_LOG_INF("ih_hdr_size = 0x%x", rsp->br_hdr->ih_hdr_size);
     int slot = (rsp->br_image_off == CONFIG_ESP_IMAGE0_PRIMARY_START_ADDRESS) ? PRIMARY_SLOT : SECONDARY_SLOT;
-    esp_app_image_load(IMAGE_INDEX_0, slot, rsp->br_hdr->ih_hdr_size, &entry_addr);
-    ((void (*)(void))entry_addr)(); /* Call to application entry address should not return */
-    FIH_PANIC; /* It should not get here */
+    start_cpu0_image(IMAGE_INDEX_0, slot, rsp->br_hdr->ih_hdr_size);
 }
 
 #ifdef CONFIG_ESP_MULTI_PROCESSOR_BOOT
@@ -79,15 +73,13 @@
 
 void do_boot_appcpu(uint32_t img_index, uint32_t slot)
 {
-    unsigned int entry_addr;
     struct image_header img_header;
 
     if (read_image_header(img_index, slot, &img_header) != 0) {
         FIH_PANIC;
     }
 
-    esp_app_image_load(img_index, slot, img_header.ih_hdr_size, &entry_addr);
-    appcpu_start(entry_addr);
+    start_cpu1_image(img_index, slot, img_header.ih_hdr_size);
 }
 #endif
 
diff --git a/boot/espressif/port/esp32/ld/bootloader.ld b/boot/espressif/port/esp32/ld/bootloader.ld
index 2b7797b..30f08dc 100644
--- a/boot/espressif/port/esp32/ld/bootloader.ld
+++ b/boot/espressif/port/esp32/ld/bootloader.ld
@@ -56,6 +56,7 @@
     *libhal.a:esp_efuse_api.*(.literal .text .literal.* .text.*)
     *libhal.a:esp_efuse_utility.*(.literal .text .literal.* .text.*)
     *libhal.a:esp_efuse_api_key_esp32.*(.literal .text .literal.* .text.*)
+    *libhal.a:app_cpu_start.*(.literal .text .literal.* .text.*)
     *esp_mcuboot.*(.literal .text .literal.* .text.*)
     *esp_loader.*(.literal .text .literal.* .text.*)
     *(.fini.literal)
diff --git a/boot/espressif/port/esp_loader.c b/boot/espressif/port/esp_loader.c
index a0806d3..4978df6 100644
--- a/boot/espressif/port/esp_loader.c
+++ b/boot/espressif/port/esp_loader.c
@@ -27,6 +27,10 @@
 #include "esp_loader.h"
 #include "flash_map_backend/flash_map_backend.h"
 
+#ifdef CONFIG_ESP_MULTI_PROCESSOR_BOOT
+#include "app_cpu_start.h"
+#endif
+
 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);
@@ -90,3 +94,20 @@
     assert(entry_addr != NULL);
     *entry_addr = load_header.entry_addr;
 }
+
+void start_cpu0_image(int image_index, int slot, unsigned int hdr_offset)
+{
+    unsigned int entry_addr;
+    esp_app_image_load(image_index, slot, hdr_offset, &entry_addr);
+    ((void (*)(void))entry_addr)(); /* Call to application entry address should not return */
+    FIH_PANIC; /* It should not get here */
+}
+
+#ifdef CONFIG_ESP_MULTI_PROCESSOR_BOOT
+void start_cpu1_image(int image_index, int slot, unsigned int hdr_offset)
+{
+    unsigned int entry_addr;
+    esp_app_image_load(image_index, slot, hdr_offset, &entry_addr);
+    appcpu_start(entry_addr);
+}
+#endif