cpu_start.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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 "bootloader_flash.h"
  64. #include "esp_private/crosscore_int.h"
  65. #include "esp_flash_encrypt.h"
  66. #include "hal/rtc_io_hal.h"
  67. #include "hal/gpio_hal.h"
  68. #include "hal/wdt_hal.h"
  69. #include "soc/rtc.h"
  70. #include "soc/efuse_reg.h"
  71. #include "soc/periph_defs.h"
  72. #include "soc/cpu.h"
  73. #include "soc/rtc.h"
  74. #include "soc/spinlock.h"
  75. #if CONFIG_ESP32_TRAX || CONFIG_ESP32S2_TRAX
  76. #include "trax.h"
  77. #endif
  78. #include "bootloader_mem.h"
  79. #if CONFIG_APP_BUILD_TYPE_ELF_RAM
  80. #if CONFIG_IDF_TARGET_ESP32
  81. #include "esp32/rom/spi_flash.h"
  82. #elif CONFIG_IDF_TARGET_ESP32S2
  83. #include "esp32s2/rom/spi_flash.h"
  84. #elif CONFIG_IDF_TARGET_ESP32S3
  85. #include "esp32s3/rom/spi_flash.h"
  86. #elif CONFIG_IDF_TARGET_ESP32C3
  87. #include "esp32c3/rom/spi_flash.h"
  88. #endif
  89. #endif // CONFIG_APP_BUILD_TYPE_ELF_RAM
  90. #include "esp_private/startup_internal.h"
  91. #include "esp_private/system_internal.h"
  92. extern int _bss_start;
  93. extern int _bss_end;
  94. extern int _rtc_bss_start;
  95. extern int _rtc_bss_end;
  96. extern int _vector_table;
  97. static const char *TAG = "cpu_start";
  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. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  107. static volatile bool s_cpu_up[SOC_CPU_CORES_NUM] = { false };
  108. static volatile bool s_cpu_inited[SOC_CPU_CORES_NUM] = { false };
  109. static volatile bool s_resume_cores;
  110. #endif
  111. // If CONFIG_SPIRAM_IGNORE_NOTFOUND is set and external RAM is not found or errors out on testing, this is set to false.
  112. bool g_spiram_ok = true;
  113. static void core_intr_matrix_clear(void)
  114. {
  115. uint32_t core_id = cpu_hal_get_core_id();
  116. for (int i = 0; i < ETS_MAX_INTR_SOURCE; i++) {
  117. intr_matrix_set(core_id, i, ETS_INVALID_INUM);
  118. }
  119. }
  120. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  121. void startup_resume_other_cores(void)
  122. {
  123. s_resume_cores = true;
  124. }
  125. void IRAM_ATTR call_start_cpu1(void)
  126. {
  127. cpu_hal_set_vecbase(&_vector_table);
  128. ets_set_appcpu_boot_addr(0);
  129. bootloader_init_mem();
  130. #if CONFIG_ESP_CONSOLE_UART_NONE
  131. esp_rom_install_channel_putc(1, NULL);
  132. esp_rom_install_channel_putc(2, NULL);
  133. #else // CONFIG_ESP_CONSOLE_UART_NONE
  134. esp_rom_install_uart_printf();
  135. esp_rom_uart_set_as_console(CONFIG_ESP_CONSOLE_UART_NUM);
  136. #endif
  137. #if CONFIG_IDF_TARGET_ESP32
  138. DPORT_REG_SET_BIT(DPORT_APP_CPU_RECORD_CTRL_REG, DPORT_APP_CPU_PDEBUG_ENABLE | DPORT_APP_CPU_RECORD_ENABLE);
  139. DPORT_REG_CLR_BIT(DPORT_APP_CPU_RECORD_CTRL_REG, DPORT_APP_CPU_RECORD_ENABLE);
  140. #else
  141. REG_WRITE(ASSIST_DEBUG_CORE_1_RCD_PDEBUGENABLE_REG, 1);
  142. REG_WRITE(ASSIST_DEBUG_CORE_1_RCD_RECORDING_REG, 1);
  143. #endif
  144. s_cpu_up[1] = true;
  145. ESP_EARLY_LOGI(TAG, "App cpu up.");
  146. // Clear interrupt matrix for APP CPU core
  147. core_intr_matrix_clear();
  148. //Take care putting stuff here: if asked, FreeRTOS will happily tell you the scheduler
  149. //has started, but it isn't active *on this CPU* yet.
  150. esp_cache_err_int_init();
  151. #if CONFIG_IDF_TARGET_ESP32
  152. #if CONFIG_ESP32_TRAX_TWOBANKS
  153. trax_start_trace(TRAX_DOWNCOUNT_WORDS);
  154. #endif
  155. #endif
  156. s_cpu_inited[1] = true;
  157. while (!s_resume_cores) {
  158. esp_rom_delay_us(100);
  159. }
  160. SYS_STARTUP_FN();
  161. }
  162. static void start_other_core(void)
  163. {
  164. // If not the single core variant of ESP32 - check this since there is
  165. // no separate soc_caps.h for the single core variant.
  166. bool is_single_core = false;
  167. #if CONFIG_IDF_TARGET_ESP32
  168. is_single_core = REG_GET_BIT(EFUSE_BLK0_RDATA3_REG, EFUSE_RD_CHIP_VER_DIS_APP_CPU);
  169. #endif
  170. if (!is_single_core) {
  171. ESP_EARLY_LOGI(TAG, "Starting app cpu, entry point is %p", call_start_cpu1);
  172. #if CONFIG_IDF_TARGET_ESP32
  173. Cache_Flush(1);
  174. Cache_Read_Enable(1);
  175. #endif
  176. esp_cpu_unstall(1);
  177. // Enable clock and reset APP CPU. Note that OpenOCD may have already
  178. // enabled clock and taken APP CPU out of reset. In this case don't reset
  179. // APP CPU again, as that will clear the breakpoints which may have already
  180. // been set.
  181. #if CONFIG_IDF_TARGET_ESP32
  182. if (!DPORT_GET_PERI_REG_MASK(DPORT_APPCPU_CTRL_B_REG, DPORT_APPCPU_CLKGATE_EN)) {
  183. DPORT_SET_PERI_REG_MASK(DPORT_APPCPU_CTRL_B_REG, DPORT_APPCPU_CLKGATE_EN);
  184. DPORT_CLEAR_PERI_REG_MASK(DPORT_APPCPU_CTRL_C_REG, DPORT_APPCPU_RUNSTALL);
  185. DPORT_SET_PERI_REG_MASK(DPORT_APPCPU_CTRL_A_REG, DPORT_APPCPU_RESETTING);
  186. DPORT_CLEAR_PERI_REG_MASK(DPORT_APPCPU_CTRL_A_REG, DPORT_APPCPU_RESETTING);
  187. }
  188. #elif CONFIG_IDF_TARGET_ESP32S3
  189. if (!REG_GET_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_CLKGATE_EN)) {
  190. REG_SET_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_CLKGATE_EN);
  191. REG_CLR_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_RUNSTALL);
  192. REG_SET_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_RESETING);
  193. REG_CLR_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_RESETING);
  194. }
  195. #endif
  196. ets_set_appcpu_boot_addr((uint32_t)call_start_cpu1);
  197. volatile bool cpus_up = false;
  198. while (!cpus_up) {
  199. cpus_up = true;
  200. for (int i = 0; i < SOC_CPU_CORES_NUM; i++) {
  201. cpus_up &= s_cpu_up[i];
  202. }
  203. esp_rom_delay_us(100);
  204. }
  205. }
  206. }
  207. #endif // !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  208. /*
  209. * We arrive here after the bootloader finished loading the program from flash. The hardware is mostly uninitialized,
  210. * and the app CPU is in reset. We do have a stack, so we can do the initialization in C.
  211. */
  212. void IRAM_ATTR call_start_cpu0(void)
  213. {
  214. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  215. RESET_REASON rst_reas[SOC_CPU_CORES_NUM];
  216. #else
  217. RESET_REASON rst_reas[1];
  218. #endif
  219. #ifdef __riscv
  220. // Configure the global pointer register
  221. // (This should be the first thing IDF app does, as any other piece of code could be
  222. // relaxed by the linker to access something relative to __global_pointer$)
  223. __asm__ __volatile__ (
  224. ".option push\n"
  225. ".option norelax\n"
  226. "la gp, __global_pointer$\n"
  227. ".option pop"
  228. );
  229. #endif
  230. // Move exception vectors to IRAM
  231. cpu_hal_set_vecbase(&_vector_table);
  232. rst_reas[0] = rtc_get_reset_reason(0);
  233. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  234. rst_reas[1] = rtc_get_reset_reason(1);
  235. #endif
  236. #ifndef CONFIG_BOOTLOADER_WDT_ENABLE
  237. // from panic handler we can be reset by RWDT or TG0WDT
  238. if (rst_reas[0] == RTCWDT_SYS_RESET || rst_reas[0] == TG0WDT_SYS_RESET
  239. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  240. || rst_reas[1] == RTCWDT_SYS_RESET || rst_reas[1] == TG0WDT_SYS_RESET
  241. #endif
  242. ) {
  243. wdt_hal_context_t rtc_wdt_ctx = {.inst = WDT_RWDT, .rwdt_dev = &RTCCNTL};
  244. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  245. wdt_hal_disable(&rtc_wdt_ctx);
  246. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  247. }
  248. #endif
  249. //Clear BSS. Please do not attempt to do any complex stuff (like early logging) before this.
  250. memset(&_bss_start, 0, (&_bss_end - &_bss_start) * sizeof(_bss_start));
  251. #if defined(CONFIG_IDF_TARGET_ESP32) && defined(CONFIG_ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY)
  252. // Clear IRAM BSS
  253. memset(&_iram_bss_start, 0, (&_iram_bss_end - &_iram_bss_start) * sizeof(_iram_bss_start));
  254. #endif
  255. /* Unless waking from deep sleep (implying RTC memory is intact), clear RTC bss */
  256. if (rst_reas[0] != DEEPSLEEP_RESET) {
  257. memset(&_rtc_bss_start, 0, (&_rtc_bss_end - &_rtc_bss_start) * sizeof(_rtc_bss_start));
  258. }
  259. #if CONFIG_IDF_TARGET_ESP32S2
  260. /* Configure the mode of instruction cache : cache size, cache associated ways, cache line size. */
  261. extern void esp_config_instruction_cache_mode(void);
  262. esp_config_instruction_cache_mode();
  263. /* If we need use SPIRAM, we should use data cache, or if we want to access rodata, we also should use data cache.
  264. Configure the mode of data : cache size, cache associated ways, cache line size.
  265. Enable data cache, so if we don't use SPIRAM, it just works. */
  266. #if CONFIG_SPIRAM_BOOT_INIT
  267. extern void esp_config_data_cache_mode(void);
  268. esp_config_data_cache_mode();
  269. Cache_Enable_DCache(0);
  270. #endif
  271. #endif
  272. #if CONFIG_IDF_TARGET_ESP32S3
  273. /* Configure the mode of instruction cache : cache size, cache line size. */
  274. extern void rom_config_instruction_cache_mode(uint32_t cfg_cache_size, uint8_t cfg_cache_ways, uint8_t cfg_cache_line_size);
  275. rom_config_instruction_cache_mode(CONFIG_ESP32S3_INSTRUCTION_CACHE_SIZE, CONFIG_ESP32S3_ICACHE_ASSOCIATED_WAYS, CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_SIZE);
  276. /* If we need use SPIRAM, we should use data cache.
  277. Configure the mode of data : cache size, cache line size.*/
  278. Cache_Suspend_DCache();
  279. extern void rom_config_data_cache_mode(uint32_t cfg_cache_size, uint8_t cfg_cache_ways, uint8_t cfg_cache_line_size);
  280. rom_config_data_cache_mode(CONFIG_ESP32S3_DATA_CACHE_SIZE, CONFIG_ESP32S3_DCACHE_ASSOCIATED_WAYS, CONFIG_ESP32S3_DATA_CACHE_LINE_SIZE);
  281. Cache_Resume_DCache(0);
  282. #endif // CONFIG_IDF_TARGET_ESP32S3
  283. #if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3
  284. /* Configure the Cache MMU size for instruction and rodata in flash. */
  285. extern uint32_t Cache_Set_IDROM_MMU_Size(uint32_t irom_size, uint32_t drom_size);
  286. extern int _rodata_reserved_start;
  287. uint32_t rodata_reserved_start_align = (uint32_t)&_rodata_reserved_start & ~(MMU_PAGE_SIZE - 1);
  288. uint32_t cache_mmu_irom_size = ((rodata_reserved_start_align - SOC_DROM_LOW) / MMU_PAGE_SIZE) * sizeof(uint32_t);
  289. Cache_Set_IDROM_MMU_Size(cache_mmu_irom_size, CACHE_DROM_MMU_MAX_END - cache_mmu_irom_size);
  290. #endif // CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3
  291. bootloader_init_mem();
  292. #if CONFIG_SPIRAM_BOOT_INIT
  293. if (esp_spiram_init() != ESP_OK) {
  294. #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
  295. #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  296. ESP_EARLY_LOGE(TAG, "Failed to init external RAM, needed for external .bss segment");
  297. abort();
  298. #endif
  299. #endif
  300. #if CONFIG_SPIRAM_IGNORE_NOTFOUND
  301. ESP_EARLY_LOGI(TAG, "Failed to init external RAM; continuing without it.");
  302. g_spiram_ok = false;
  303. #else
  304. ESP_EARLY_LOGE(TAG, "Failed to init external RAM!");
  305. abort();
  306. #endif
  307. }
  308. if (g_spiram_ok) {
  309. esp_spiram_init_cache();
  310. }
  311. #endif
  312. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  313. s_cpu_up[0] = true;
  314. #endif
  315. ESP_EARLY_LOGI(TAG, "Pro cpu up.");
  316. #if SOC_CPU_CORES_NUM > 1 // there is no 'single-core mode' for natively single-core processors
  317. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  318. start_other_core();
  319. #else
  320. ESP_EARLY_LOGI(TAG, "Single core mode");
  321. #if CONFIG_IDF_TARGET_ESP32
  322. DPORT_CLEAR_PERI_REG_MASK(DPORT_APPCPU_CTRL_B_REG, DPORT_APPCPU_CLKGATE_EN); // stop the other core
  323. #elif CONFIG_IDF_TARGET_ESP32S3
  324. REG_CLR_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_CLKGATE_EN);
  325. #endif
  326. #endif // !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  327. #endif // SOC_CPU_CORES_NUM > 1
  328. #if CONFIG_SPIRAM_MEMTEST
  329. if (g_spiram_ok) {
  330. bool ext_ram_ok = esp_spiram_test();
  331. if (!ext_ram_ok) {
  332. ESP_EARLY_LOGE(TAG, "External RAM failed memory test!");
  333. abort();
  334. }
  335. }
  336. #endif
  337. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  338. extern void instruction_flash_page_info_init(void);
  339. instruction_flash_page_info_init();
  340. #endif
  341. #if CONFIG_SPIRAM_RODATA
  342. extern void rodata_flash_page_info_init(void);
  343. rodata_flash_page_info_init();
  344. #endif
  345. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  346. extern void esp_spiram_enable_instruction_access(void);
  347. esp_spiram_enable_instruction_access();
  348. #endif
  349. #if CONFIG_SPIRAM_RODATA
  350. extern void esp_spiram_enable_rodata_access(void);
  351. esp_spiram_enable_rodata_access();
  352. #endif
  353. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_WRAP || CONFIG_ESP32S2_DATA_CACHE_WRAP
  354. uint32_t icache_wrap_enable = 0, dcache_wrap_enable = 0;
  355. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_WRAP
  356. icache_wrap_enable = 1;
  357. #endif
  358. #if CONFIG_ESP32S2_DATA_CACHE_WRAP
  359. dcache_wrap_enable = 1;
  360. #endif
  361. extern void esp_enable_cache_wrap(uint32_t icache_wrap_enable, uint32_t dcache_wrap_enable);
  362. esp_enable_cache_wrap(icache_wrap_enable, dcache_wrap_enable);
  363. #endif
  364. #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  365. memset(&_ext_ram_bss_start, 0, (&_ext_ram_bss_end - &_ext_ram_bss_start) * sizeof(_ext_ram_bss_start));
  366. #endif
  367. //Enable trace memory and immediately start trace.
  368. #if CONFIG_ESP32_TRAX || CONFIG_ESP32S2_TRAX
  369. #if CONFIG_IDF_TARGET_ESP32
  370. #if CONFIG_ESP32_TRAX_TWOBANKS
  371. trax_enable(TRAX_ENA_PRO_APP);
  372. #else
  373. trax_enable(TRAX_ENA_PRO);
  374. #endif
  375. #elif CONFIG_IDF_TARGET_ESP32S2
  376. trax_enable(TRAX_ENA_PRO);
  377. #endif
  378. trax_start_trace(TRAX_DOWNCOUNT_WORDS);
  379. #endif // CONFIG_ESP32_TRAX || CONFIG_ESP32S2_TRAX
  380. esp_clk_init();
  381. esp_perip_clk_init();
  382. // Now that the clocks have been set-up, set the startup time from RTC
  383. // and default RTC-backed system time provider.
  384. g_startup_time = esp_rtc_get_time_us();
  385. // Clear interrupt matrix for PRO CPU core
  386. core_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. // Memprot cannot be locked during OS startup as the lock-on prevents any PMS changes until a next reboot
  403. // If such a situation appears, it is likely an malicious attempt to bypass the system safety setup -> print error & reset
  404. if ( esp_memprot_is_locked_any() ) {
  405. ESP_EARLY_LOGE(TAG, "Memprot feature locked after the system reset! Potential safety corruption, rebooting.");
  406. esp_restart_noos_dig();
  407. }
  408. #if CONFIG_ESP_SYSTEM_MEMPROT_FEATURE_LOCK
  409. esp_memprot_set_prot(true, true, NULL);
  410. #else
  411. esp_memprot_set_prot(true, false, NULL);
  412. #endif
  413. #endif
  414. bootloader_flash_update_id();
  415. // Read the application binary image header. This will also decrypt the header if the image is encrypted.
  416. __attribute__((unused)) esp_image_header_t fhdr = {0};
  417. #ifdef CONFIG_APP_BUILD_TYPE_ELF_RAM
  418. fhdr.spi_mode = ESP_IMAGE_SPI_MODE_DIO;
  419. fhdr.spi_speed = ESP_IMAGE_SPI_SPEED_40M;
  420. fhdr.spi_size = ESP_IMAGE_FLASH_SIZE_4MB;
  421. extern void esp_rom_spiflash_attach(uint32_t, bool);
  422. esp_rom_spiflash_attach(esp_rom_efuse_get_flash_gpio_info(), false);
  423. bootloader_flash_unlock();
  424. #else
  425. // This assumes that DROM is the first segment in the application binary, i.e. that we can read
  426. // the binary header through cache by accessing SOC_DROM_LOW address.
  427. memcpy(&fhdr, (void *) SOC_DROM_LOW, sizeof(fhdr));
  428. #endif // CONFIG_APP_BUILD_TYPE_ELF_RAM
  429. #if CONFIG_IDF_TARGET_ESP32
  430. #if !CONFIG_SPIRAM_BOOT_INIT
  431. // If psram is uninitialized, we need to improve some flash configuration.
  432. bootloader_flash_clock_config(&fhdr);
  433. bootloader_flash_gpio_config(&fhdr);
  434. bootloader_flash_dummy_config(&fhdr);
  435. bootloader_flash_cs_timing_config();
  436. #endif //!CONFIG_SPIRAM_BOOT_INIT
  437. #endif //CONFIG_IDF_TARGET_ESP32
  438. #if CONFIG_SPI_FLASH_SIZE_OVERRIDE
  439. int app_flash_size = esp_image_get_flash_size(fhdr.spi_size);
  440. if (app_flash_size < 1 * 1024 * 1024) {
  441. ESP_LOGE(TAG, "Invalid flash size in app image header.");
  442. abort();
  443. }
  444. bootloader_flash_update_size(app_flash_size);
  445. #endif //CONFIG_SPI_FLASH_SIZE_OVERRIDE
  446. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  447. s_cpu_inited[0] = true;
  448. volatile bool cpus_inited = false;
  449. while (!cpus_inited) {
  450. cpus_inited = true;
  451. for (int i = 0; i < SOC_CPU_CORES_NUM; i++) {
  452. cpus_inited &= s_cpu_inited[i];
  453. }
  454. esp_rom_delay_us(100);
  455. }
  456. #endif
  457. SYS_STARTUP_FN();
  458. }