cpu_start.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. // Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdint.h>
  15. #include <string.h>
  16. #include <stdbool.h>
  17. #include "esp_attr.h"
  18. #include "esp_err.h"
  19. #include "esp_log.h"
  20. #include "esp_system.h"
  21. #include "esp_rom_uart.h"
  22. #include "esp_clk_internal.h"
  23. #include "esp_rom_efuse.h"
  24. #include "esp_rom_sys.h"
  25. #include "sdkconfig.h"
  26. #if CONFIG_IDF_TARGET_ESP32
  27. #include "soc/dport_reg.h"
  28. #include "esp32/rtc.h"
  29. #include "esp32/cache_err_int.h"
  30. #include "esp32/rom/cache.h"
  31. #include "esp32/rom/rtc.h"
  32. #include "esp32/spiram.h"
  33. #elif CONFIG_IDF_TARGET_ESP32S2
  34. #include "esp32s2/rtc.h"
  35. #include "esp32s2/brownout.h"
  36. #include "esp32s2/cache_err_int.h"
  37. #include "esp32s2/rom/cache.h"
  38. #include "esp32s2/rom/rtc.h"
  39. #include "esp32s2/spiram.h"
  40. #include "esp32s2/dport_access.h"
  41. #include "esp32s2/memprot.h"
  42. #elif CONFIG_IDF_TARGET_ESP32S3
  43. #include "esp32s3/rtc.h"
  44. #include "esp32s3/brownout.h"
  45. #include "esp32s3/cache_err_int.h"
  46. #include "esp32s3/rom/cache.h"
  47. #include "esp32s3/rom/rtc.h"
  48. #include "esp32s3/spiram.h"
  49. #include "esp32s3/dport_access.h"
  50. #include "esp32s3/memprot.h"
  51. #include "soc/assist_debug_reg.h"
  52. #include "soc/cache_memory.h"
  53. #include "soc/system_reg.h"
  54. #elif CONFIG_IDF_TARGET_ESP32C3
  55. #include "esp32c3/rtc.h"
  56. #include "esp32c3/cache_err_int.h"
  57. #include "esp32c3/rom/cache.h"
  58. #include "esp32c3/rom/rtc.h"
  59. #include "soc/cache_memory.h"
  60. #include "esp32c3/memprot.h"
  61. #endif
  62. #include "bootloader_flash_config.h"
  63. #include "esp_private/crosscore_int.h"
  64. #include "esp_flash_encrypt.h"
  65. #include "hal/rtc_io_hal.h"
  66. #include "hal/gpio_hal.h"
  67. #include "hal/wdt_hal.h"
  68. #include "soc/rtc.h"
  69. #include "soc/efuse_reg.h"
  70. #include "soc/periph_defs.h"
  71. #include "soc/cpu.h"
  72. #include "soc/rtc.h"
  73. #include "soc/spinlock.h"
  74. #if CONFIG_ESP32_TRAX || CONFIG_ESP32S2_TRAX
  75. #include "trax.h"
  76. #endif
  77. #include "bootloader_mem.h"
  78. #if CONFIG_APP_BUILD_TYPE_ELF_RAM
  79. #if CONFIG_IDF_TARGET_ESP32
  80. #include "esp32/rom/spi_flash.h"
  81. #elif CONFIG_IDF_TARGET_ESP32S2
  82. #include "esp32s2/rom/spi_flash.h"
  83. #elif CONFIG_IDF_TARGET_ESP32S3
  84. #include "esp32s3/rom/spi_flash.h"
  85. #elif CONFIG_IDF_TARGET_ESP32C3
  86. #include "esp32c3/rom/spi_flash.h"
  87. #endif
  88. #endif // CONFIG_APP_BUILD_TYPE_ELF_RAM
  89. #include "esp_private/startup_internal.h"
  90. #include "esp_private/system_internal.h"
  91. extern int _bss_start;
  92. extern int _bss_end;
  93. extern int _rtc_bss_start;
  94. extern int _rtc_bss_end;
  95. extern int _vector_table;
  96. static const char *TAG = "cpu_start";
  97. #if CONFIG_IDF_TARGET_ESP32
  98. #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  99. extern int _ext_ram_bss_start;
  100. extern int _ext_ram_bss_end;
  101. #endif
  102. #ifdef CONFIG_ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY
  103. extern int _iram_bss_start;
  104. extern int _iram_bss_end;
  105. #endif
  106. #endif // CONFIG_IDF_TARGET_ESP32
  107. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  108. static volatile bool s_cpu_up[SOC_CPU_CORES_NUM] = { false };
  109. static volatile bool s_cpu_inited[SOC_CPU_CORES_NUM] = { false };
  110. static volatile bool s_resume_cores;
  111. #endif
  112. // If CONFIG_SPIRAM_IGNORE_NOTFOUND is set and external RAM is not found or errors out on testing, this is set to false.
  113. bool g_spiram_ok = true;
  114. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  115. void startup_resume_other_cores(void)
  116. {
  117. s_resume_cores = true;
  118. }
  119. void IRAM_ATTR call_start_cpu1(void)
  120. {
  121. cpu_hal_set_vecbase(&_vector_table);
  122. ets_set_appcpu_boot_addr(0);
  123. bootloader_init_mem();
  124. #if CONFIG_ESP_CONSOLE_UART_NONE
  125. esp_rom_install_channel_putc(1, NULL);
  126. esp_rom_install_channel_putc(2, NULL);
  127. #else // CONFIG_ESP_CONSOLE_UART_NONE
  128. esp_rom_install_uart_printf();
  129. esp_rom_uart_set_as_console(CONFIG_ESP_CONSOLE_UART_NUM);
  130. #endif
  131. #if CONFIG_IDF_TARGET_ESP32
  132. DPORT_REG_SET_BIT(DPORT_APP_CPU_RECORD_CTRL_REG, DPORT_APP_CPU_PDEBUG_ENABLE | DPORT_APP_CPU_RECORD_ENABLE);
  133. DPORT_REG_CLR_BIT(DPORT_APP_CPU_RECORD_CTRL_REG, DPORT_APP_CPU_RECORD_ENABLE);
  134. #else
  135. REG_WRITE(ASSIST_DEBUG_CORE_1_RCD_PDEBUGENABLE_REG, 1);
  136. REG_WRITE(ASSIST_DEBUG_CORE_1_RCD_RECORDING_REG, 1);
  137. #endif
  138. s_cpu_up[1] = true;
  139. ESP_EARLY_LOGI(TAG, "App cpu up.");
  140. //Take care putting stuff here: if asked, FreeRTOS will happily tell you the scheduler
  141. //has started, but it isn't active *on this CPU* yet.
  142. esp_cache_err_int_init();
  143. #if CONFIG_IDF_TARGET_ESP32
  144. #if CONFIG_ESP32_TRAX_TWOBANKS
  145. trax_start_trace(TRAX_DOWNCOUNT_WORDS);
  146. #endif
  147. #endif
  148. s_cpu_inited[1] = true;
  149. while (!s_resume_cores) {
  150. esp_rom_delay_us(100);
  151. }
  152. SYS_STARTUP_FN();
  153. }
  154. static void start_other_core(void)
  155. {
  156. // If not the single core variant of ESP32 - check this since there is
  157. // no separate soc_caps.h for the single core variant.
  158. bool is_single_core = false;
  159. #if CONFIG_IDF_TARGET_ESP32
  160. is_single_core = REG_GET_BIT(EFUSE_BLK0_RDATA3_REG, EFUSE_RD_CHIP_VER_DIS_APP_CPU);
  161. #endif
  162. if (!is_single_core) {
  163. ESP_EARLY_LOGI(TAG, "Starting app cpu, entry point is %p", call_start_cpu1);
  164. #if CONFIG_IDF_TARGET_ESP32
  165. Cache_Flush(1);
  166. Cache_Read_Enable(1);
  167. #endif
  168. esp_cpu_unstall(1);
  169. // Enable clock and reset APP CPU. Note that OpenOCD may have already
  170. // enabled clock and taken APP CPU out of reset. In this case don't reset
  171. // APP CPU again, as that will clear the breakpoints which may have already
  172. // been set.
  173. #if CONFIG_IDF_TARGET_ESP32
  174. if (!DPORT_GET_PERI_REG_MASK(DPORT_APPCPU_CTRL_B_REG, DPORT_APPCPU_CLKGATE_EN)) {
  175. DPORT_SET_PERI_REG_MASK(DPORT_APPCPU_CTRL_B_REG, DPORT_APPCPU_CLKGATE_EN);
  176. DPORT_CLEAR_PERI_REG_MASK(DPORT_APPCPU_CTRL_C_REG, DPORT_APPCPU_RUNSTALL);
  177. DPORT_SET_PERI_REG_MASK(DPORT_APPCPU_CTRL_A_REG, DPORT_APPCPU_RESETTING);
  178. DPORT_CLEAR_PERI_REG_MASK(DPORT_APPCPU_CTRL_A_REG, DPORT_APPCPU_RESETTING);
  179. }
  180. #elif CONFIG_IDF_TARGET_ESP32S3
  181. if (!REG_GET_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_CLKGATE_EN)) {
  182. REG_SET_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_CLKGATE_EN);
  183. REG_CLR_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_RUNSTALL);
  184. REG_SET_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_RESETING);
  185. REG_CLR_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_RESETING);
  186. }
  187. #endif
  188. ets_set_appcpu_boot_addr((uint32_t)call_start_cpu1);
  189. volatile bool cpus_up = false;
  190. while (!cpus_up) {
  191. cpus_up = true;
  192. for (int i = 0; i < SOC_CPU_CORES_NUM; i++) {
  193. cpus_up &= s_cpu_up[i];
  194. }
  195. esp_rom_delay_us(100);
  196. }
  197. }
  198. }
  199. #endif // !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  200. static void intr_matrix_clear(void)
  201. {
  202. for (int i = 0; i < ETS_MAX_INTR_SOURCE; i++) {
  203. intr_matrix_set(0, i, ETS_INVALID_INUM);
  204. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  205. intr_matrix_set(1, i, ETS_INVALID_INUM);
  206. #endif
  207. }
  208. }
  209. /*
  210. * We arrive here after the bootloader finished loading the program from flash. The hardware is mostly uninitialized,
  211. * and the app CPU is in reset. We do have a stack, so we can do the initialization in C.
  212. */
  213. void IRAM_ATTR call_start_cpu0(void)
  214. {
  215. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  216. RESET_REASON rst_reas[SOC_CPU_CORES_NUM];
  217. #else
  218. RESET_REASON rst_reas[1];
  219. #endif
  220. #ifdef __riscv
  221. // Configure the global pointer register
  222. // (This should be the first thing IDF app does, as any other piece of code could be
  223. // relaxed by the linker to access something relative to __global_pointer$)
  224. __asm__ __volatile__ (
  225. ".option push\n"
  226. ".option norelax\n"
  227. "la gp, __global_pointer$\n"
  228. ".option pop"
  229. );
  230. #endif
  231. // Move exception vectors to IRAM
  232. cpu_hal_set_vecbase(&_vector_table);
  233. rst_reas[0] = rtc_get_reset_reason(0);
  234. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  235. rst_reas[1] = rtc_get_reset_reason(1);
  236. #endif
  237. #ifndef CONFIG_BOOTLOADER_WDT_ENABLE
  238. // from panic handler we can be reset by RWDT or TG0WDT
  239. if (rst_reas[0] == RTCWDT_SYS_RESET || rst_reas[0] == TG0WDT_SYS_RESET
  240. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  241. || rst_reas[1] == RTCWDT_SYS_RESET || rst_reas[1] == TG0WDT_SYS_RESET
  242. #endif
  243. ) {
  244. wdt_hal_context_t rtc_wdt_ctx = {.inst = WDT_RWDT, .rwdt_dev = &RTCCNTL};
  245. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  246. wdt_hal_disable(&rtc_wdt_ctx);
  247. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  248. }
  249. #endif
  250. //Clear BSS. Please do not attempt to do any complex stuff (like early logging) before this.
  251. memset(&_bss_start, 0, (&_bss_end - &_bss_start) * sizeof(_bss_start));
  252. #if defined(CONFIG_IDF_TARGET_ESP32) && defined(CONFIG_ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY)
  253. // Clear IRAM BSS
  254. memset(&_iram_bss_start, 0, (&_iram_bss_end - &_iram_bss_start) * sizeof(_iram_bss_start));
  255. #endif
  256. /* Unless waking from deep sleep (implying RTC memory is intact), clear RTC bss */
  257. if (rst_reas[0] != DEEPSLEEP_RESET) {
  258. memset(&_rtc_bss_start, 0, (&_rtc_bss_end - &_rtc_bss_start) * sizeof(_rtc_bss_start));
  259. }
  260. #if CONFIG_IDF_TARGET_ESP32S2
  261. /* Configure the mode of instruction cache : cache size, cache associated ways, cache line size. */
  262. extern void esp_config_instruction_cache_mode(void);
  263. esp_config_instruction_cache_mode();
  264. /* If we need use SPIRAM, we should use data cache, or if we want to access rodata, we also should use data cache.
  265. Configure the mode of data : cache size, cache associated ways, cache line size.
  266. Enable data cache, so if we don't use SPIRAM, it just works. */
  267. #if CONFIG_SPIRAM_BOOT_INIT
  268. extern void esp_config_data_cache_mode(void);
  269. esp_config_data_cache_mode();
  270. Cache_Enable_DCache(0);
  271. #endif
  272. #endif
  273. #if CONFIG_IDF_TARGET_ESP32S3
  274. /* Configure the mode of instruction cache : cache size, cache line size. */
  275. extern void rom_config_instruction_cache_mode(uint32_t cfg_cache_size, uint8_t cfg_cache_ways, uint8_t cfg_cache_line_size);
  276. rom_config_instruction_cache_mode(CONFIG_ESP32S3_INSTRUCTION_CACHE_SIZE, CONFIG_ESP32S3_ICACHE_ASSOCIATED_WAYS, CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_SIZE);
  277. /* If we need use SPIRAM, we should use data cache.
  278. Configure the mode of data : cache size, cache line size.*/
  279. Cache_Suspend_DCache();
  280. extern void rom_config_data_cache_mode(uint32_t cfg_cache_size, uint8_t cfg_cache_ways, uint8_t cfg_cache_line_size);
  281. rom_config_data_cache_mode(CONFIG_ESP32S3_DATA_CACHE_SIZE, CONFIG_ESP32S3_DCACHE_ASSOCIATED_WAYS, CONFIG_ESP32S3_DATA_CACHE_LINE_SIZE);
  282. Cache_Resume_DCache(0);
  283. #endif // CONFIG_IDF_TARGET_ESP32S3
  284. #if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3
  285. /* Configure the Cache MMU size for instruction and rodata in flash. */
  286. extern uint32_t Cache_Set_IDROM_MMU_Size(uint32_t irom_size, uint32_t drom_size);
  287. extern int _rodata_reserved_start;
  288. uint32_t rodata_reserved_start_align = (uint32_t)&_rodata_reserved_start & ~(MMU_PAGE_SIZE - 1);
  289. uint32_t cache_mmu_irom_size = ((rodata_reserved_start_align - SOC_DROM_LOW) / MMU_PAGE_SIZE) * sizeof(uint32_t);
  290. Cache_Set_IDROM_MMU_Size(cache_mmu_irom_size, CACHE_DROM_MMU_MAX_END - cache_mmu_irom_size);
  291. #endif // CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3
  292. bootloader_init_mem();
  293. #if CONFIG_SPIRAM_BOOT_INIT
  294. if (esp_spiram_init() != ESP_OK) {
  295. #if CONFIG_IDF_TARGET_ESP32
  296. #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  297. ESP_EARLY_LOGE(TAG, "Failed to init external RAM, needed for external .bss segment");
  298. abort();
  299. #endif
  300. #endif
  301. #if CONFIG_SPIRAM_IGNORE_NOTFOUND
  302. ESP_EARLY_LOGI(TAG, "Failed to init external RAM; continuing without it.");
  303. g_spiram_ok = false;
  304. #else
  305. ESP_EARLY_LOGE(TAG, "Failed to init external RAM!");
  306. abort();
  307. #endif
  308. }
  309. if (g_spiram_ok) {
  310. esp_spiram_init_cache();
  311. }
  312. #endif
  313. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  314. s_cpu_up[0] = true;
  315. #endif
  316. ESP_EARLY_LOGI(TAG, "Pro cpu up.");
  317. #if SOC_CPU_CORES_NUM > 1 // there is no 'single-core mode' for natively single-core processors
  318. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  319. start_other_core();
  320. #else
  321. ESP_EARLY_LOGI(TAG, "Single core mode");
  322. #if CONFIG_IDF_TARGET_ESP32
  323. DPORT_CLEAR_PERI_REG_MASK(DPORT_APPCPU_CTRL_B_REG, DPORT_APPCPU_CLKGATE_EN); // stop the other core
  324. #elif CONFIG_IDF_TARGET_ESP32S3
  325. REG_CLR_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_CLKGATE_EN);
  326. #endif
  327. #endif // !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  328. #endif // SOC_CPU_CORES_NUM > 1
  329. #if CONFIG_SPIRAM_MEMTEST
  330. if (g_spiram_ok) {
  331. bool ext_ram_ok = esp_spiram_test();
  332. if (!ext_ram_ok) {
  333. ESP_EARLY_LOGE(TAG, "External RAM failed memory test!");
  334. abort();
  335. }
  336. }
  337. #endif
  338. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  339. extern void instruction_flash_page_info_init(void);
  340. instruction_flash_page_info_init();
  341. #endif
  342. #if CONFIG_SPIRAM_RODATA
  343. extern void rodata_flash_page_info_init(void);
  344. rodata_flash_page_info_init();
  345. #endif
  346. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  347. extern void esp_spiram_enable_instruction_access(void);
  348. esp_spiram_enable_instruction_access();
  349. #endif
  350. #if CONFIG_SPIRAM_RODATA
  351. extern void esp_spiram_enable_rodata_access(void);
  352. esp_spiram_enable_rodata_access();
  353. #endif
  354. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_WRAP || CONFIG_ESP32S2_DATA_CACHE_WRAP
  355. uint32_t icache_wrap_enable = 0, dcache_wrap_enable = 0;
  356. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_WRAP
  357. icache_wrap_enable = 1;
  358. #endif
  359. #if CONFIG_ESP32S2_DATA_CACHE_WRAP
  360. dcache_wrap_enable = 1;
  361. #endif
  362. extern void esp_enable_cache_wrap(uint32_t icache_wrap_enable, uint32_t dcache_wrap_enable);
  363. esp_enable_cache_wrap(icache_wrap_enable, dcache_wrap_enable);
  364. #endif
  365. #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  366. memset(&_ext_ram_bss_start, 0, (&_ext_ram_bss_end - &_ext_ram_bss_start) * sizeof(_ext_ram_bss_start));
  367. #endif
  368. //Enable trace memory and immediately start trace.
  369. #if CONFIG_ESP32_TRAX || CONFIG_ESP32S2_TRAX
  370. #if CONFIG_IDF_TARGET_ESP32
  371. #if CONFIG_ESP32_TRAX_TWOBANKS
  372. trax_enable(TRAX_ENA_PRO_APP);
  373. #else
  374. trax_enable(TRAX_ENA_PRO);
  375. #endif
  376. #elif CONFIG_IDF_TARGET_ESP32S2
  377. trax_enable(TRAX_ENA_PRO);
  378. #endif
  379. trax_start_trace(TRAX_DOWNCOUNT_WORDS);
  380. #endif // CONFIG_ESP32_TRAX || CONFIG_ESP32S2_TRAX
  381. esp_clk_init();
  382. esp_perip_clk_init();
  383. // Now that the clocks have been set-up, set the startup time from RTC
  384. // and default RTC-backed system time provider.
  385. g_startup_time = esp_rtc_get_time_us();
  386. intr_matrix_clear();
  387. #ifdef CONFIG_ESP_CONSOLE_UART
  388. uint32_t clock_hz = rtc_clk_apb_freq_get();
  389. #if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3
  390. clock_hz = UART_CLK_FREQ_ROM; // From esp32-s3 on, UART clock source is selected to XTAL in ROM
  391. #endif
  392. esp_rom_uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM);
  393. esp_rom_uart_set_clock_baudrate(CONFIG_ESP_CONSOLE_UART_NUM, clock_hz, CONFIG_ESP_CONSOLE_UART_BAUDRATE);
  394. #endif
  395. #if SOC_RTCIO_HOLD_SUPPORTED
  396. rtcio_hal_unhold_all();
  397. #else
  398. gpio_hal_force_unhold_all();
  399. #endif
  400. esp_cache_err_int_init();
  401. #if CONFIG_ESP_SYSTEM_MEMPROT_FEATURE
  402. #if CONFIG_ESP_SYSTEM_MEMPROT_FEATURE_LOCK
  403. esp_memprot_set_prot(true, true, NULL);
  404. #else
  405. esp_memprot_set_prot(true, false, NULL);
  406. #endif
  407. #endif
  408. bootloader_flash_update_id();
  409. // Read the application binary image header. This will also decrypt the header if the image is encrypted.
  410. __attribute__((unused)) esp_image_header_t fhdr = {0};
  411. #ifdef CONFIG_APP_BUILD_TYPE_ELF_RAM
  412. fhdr.spi_mode = ESP_IMAGE_SPI_MODE_DIO;
  413. fhdr.spi_speed = ESP_IMAGE_SPI_SPEED_40M;
  414. fhdr.spi_size = ESP_IMAGE_FLASH_SIZE_4MB;
  415. extern void esp_rom_spiflash_attach(uint32_t, bool);
  416. esp_rom_spiflash_attach(esp_rom_efuse_get_flash_gpio_info(), false);
  417. esp_rom_spiflash_unlock();
  418. #else
  419. // This assumes that DROM is the first segment in the application binary, i.e. that we can read
  420. // the binary header through cache by accessing SOC_DROM_LOW address.
  421. memcpy(&fhdr, (void *) SOC_DROM_LOW, sizeof(fhdr));
  422. #endif // CONFIG_APP_BUILD_TYPE_ELF_RAM
  423. #if CONFIG_IDF_TARGET_ESP32
  424. #if !CONFIG_SPIRAM_BOOT_INIT
  425. // If psram is uninitialized, we need to improve some flash configuration.
  426. bootloader_flash_clock_config(&fhdr);
  427. bootloader_flash_gpio_config(&fhdr);
  428. bootloader_flash_dummy_config(&fhdr);
  429. bootloader_flash_cs_timing_config();
  430. #endif //!CONFIG_SPIRAM_BOOT_INIT
  431. #endif //CONFIG_IDF_TARGET_ESP32
  432. #if CONFIG_SPI_FLASH_SIZE_OVERRIDE
  433. int app_flash_size = esp_image_get_flash_size(fhdr.spi_size);
  434. if (app_flash_size < 1 * 1024 * 1024) {
  435. ESP_LOGE(TAG, "Invalid flash size in app image header.");
  436. abort();
  437. }
  438. bootloader_flash_update_size(app_flash_size);
  439. #endif //CONFIG_SPI_FLASH_SIZE_OVERRIDE
  440. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  441. s_cpu_inited[0] = true;
  442. volatile bool cpus_inited = false;
  443. while (!cpus_inited) {
  444. cpus_inited = true;
  445. for (int i = 0; i < SOC_CPU_CORES_NUM; i++) {
  446. cpus_inited &= s_cpu_inited[i];
  447. }
  448. esp_rom_delay_us(100);
  449. }
  450. #endif
  451. SYS_STARTUP_FN();
  452. }