startup.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdint.h>
  7. #include <string.h>
  8. #include "esp_attr.h"
  9. #include "esp_err.h"
  10. #include "esp_system.h"
  11. #include "esp_log.h"
  12. #include "esp_ota_ops.h"
  13. #include "sdkconfig.h"
  14. #include "soc/soc_caps.h"
  15. #include "hal/wdt_hal.h"
  16. #include "hal/uart_types.h"
  17. #include "hal/uart_ll.h"
  18. #include "esp_system.h"
  19. #include "esp_log.h"
  20. #include "esp_heap_caps_init.h"
  21. #include "esp_spi_flash.h"
  22. #include "esp_flash_internal.h"
  23. #include "esp_newlib.h"
  24. #include "esp_vfs_dev.h"
  25. #include "esp_timer.h"
  26. #include "esp_efuse.h"
  27. #include "esp_flash_encrypt.h"
  28. #include "esp_secure_boot.h"
  29. #include "esp_sleep.h"
  30. #include "esp_xt_wdt.h"
  31. /***********************************************/
  32. // Headers for other components init functions
  33. #include "nvs_flash.h"
  34. #include "esp_coexist_internal.h"
  35. #if CONFIG_ESP_COREDUMP_ENABLE
  36. #include "esp_core_dump.h"
  37. #endif
  38. #include "esp_app_trace.h"
  39. #include "esp_private/dbg_stubs.h"
  40. #include "esp_pm.h"
  41. #include "esp_private/pm_impl.h"
  42. #include "esp_pthread.h"
  43. #include "esp_vfs_console.h"
  44. #include "esp_private/esp_clk.h"
  45. #include "esp_private/brownout.h"
  46. #include "esp_rom_sys.h"
  47. // [refactor-todo] make this file completely target-independent
  48. #if CONFIG_IDF_TARGET_ESP32
  49. #include "esp32/spiram.h"
  50. #elif CONFIG_IDF_TARGET_ESP32S2
  51. #include "esp32s2/spiram.h"
  52. #elif CONFIG_IDF_TARGET_ESP32S3
  53. #include "esp32s3/spiram.h"
  54. #endif
  55. /***********************************************/
  56. #include "esp_private/startup_internal.h"
  57. // Ensure that system configuration matches the underlying number of cores.
  58. // This should enable us to avoid checking for both everytime.
  59. #if !(SOC_CPU_CORES_NUM > 1) && !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  60. #error "System has been configured to run on multiple cores, but target SoC only has a single core."
  61. #endif
  62. uint64_t g_startup_time = 0;
  63. #if SOC_APB_BACKUP_DMA
  64. // APB DMA lock initialising API
  65. extern void esp_apb_backup_dma_lock_init(void);
  66. #endif
  67. // App entry point for core 0
  68. extern void esp_startup_start_app(void);
  69. // Entry point for core 0 from hardware init (port layer)
  70. void start_cpu0(void) __attribute__((weak, alias("start_cpu0_default"))) __attribute__((noreturn));
  71. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  72. // Entry point for core [1..X] from hardware init (port layer)
  73. void start_cpu_other_cores(void) __attribute__((weak, alias("start_cpu_other_cores_default"))) __attribute__((noreturn));
  74. // App entry point for core [1..X]
  75. void esp_startup_start_app_other_cores(void) __attribute__((weak, alias("esp_startup_start_app_other_cores_default"))) __attribute__((noreturn));
  76. static volatile bool s_system_inited[SOC_CPU_CORES_NUM] = { false };
  77. const sys_startup_fn_t g_startup_fn[SOC_CPU_CORES_NUM] = { [0] = start_cpu0,
  78. #if SOC_CPU_CORES_NUM > 1
  79. [1 ... SOC_CPU_CORES_NUM - 1] = start_cpu_other_cores
  80. #endif
  81. };
  82. static volatile bool s_system_full_inited = false;
  83. #else
  84. const sys_startup_fn_t g_startup_fn[1] = { start_cpu0 };
  85. #endif
  86. #ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
  87. // workaround for C++ exception crashes
  88. void _Unwind_SetNoFunctionContextInstall(unsigned char enable) __attribute__((weak, alias("_Unwind_SetNoFunctionContextInstall_Default")));
  89. // workaround for C++ exception large memory allocation
  90. void _Unwind_SetEnableExceptionFdeSorting(unsigned char enable);
  91. static IRAM_ATTR void _Unwind_SetNoFunctionContextInstall_Default(unsigned char enable __attribute__((unused)))
  92. {
  93. (void)0;
  94. }
  95. #endif // CONFIG_COMPILER_CXX_EXCEPTIONS
  96. static const char* TAG = "cpu_start";
  97. /**
  98. * This function overwrites a the same function of libsupc++ (part of libstdc++).
  99. * Consequently, libsupc++ will then follow our configured exception emergency pool size.
  100. *
  101. * It will be called even with -fno-exception for user code since the stdlib still uses exceptions.
  102. */
  103. size_t __cxx_eh_arena_size_get(void)
  104. {
  105. #ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
  106. return CONFIG_COMPILER_CXX_EXCEPTIONS_EMG_POOL_SIZE;
  107. #else
  108. return 0;
  109. #endif
  110. }
  111. /**
  112. * Xtensa gcc is configured to emit a .ctors section, RISC-V gcc is configured with --enable-initfini-array
  113. * so it emits an .init_array section instead.
  114. * But the init_priority sections will be sorted for iteration in ascending order during startup.
  115. * The rest of the init_array sections is sorted for iteration in descending order during startup, however.
  116. * Hence a different section is generated for the init_priority functions which is looped
  117. * over in ascending direction instead of descending direction.
  118. * The RISC-V-specific behavior is dependent on the linker script ld/esp32c3/sections.ld.in.
  119. */
  120. static void do_global_ctors(void)
  121. {
  122. #if __riscv
  123. extern void (*__init_priority_array_start)(void);
  124. extern void (*__init_priority_array_end)(void);
  125. #endif
  126. extern void (*__init_array_start)(void);
  127. extern void (*__init_array_end)(void);
  128. #ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
  129. struct object { long placeholder[ 10 ]; };
  130. void __register_frame_info (const void *begin, struct object *ob);
  131. extern char __eh_frame[];
  132. static struct object ob;
  133. __register_frame_info( __eh_frame, &ob );
  134. #endif // CONFIG_COMPILER_CXX_EXCEPTIONS
  135. void (**p)(void);
  136. #if __riscv
  137. for (p = &__init_priority_array_start; p < &__init_priority_array_end; ++p) {
  138. ESP_EARLY_LOGD(TAG, "calling init function: %p", *p);
  139. (*p)();
  140. }
  141. #endif
  142. for (p = &__init_array_end - 1; p >= &__init_array_start; --p) {
  143. ESP_EARLY_LOGD(TAG, "calling init function: %p", *p);
  144. (*p)();
  145. }
  146. }
  147. static void do_system_init_fn(void)
  148. {
  149. extern esp_system_init_fn_t _esp_system_init_fn_array_start;
  150. extern esp_system_init_fn_t _esp_system_init_fn_array_end;
  151. esp_system_init_fn_t *p;
  152. for (p = &_esp_system_init_fn_array_end - 1; p >= &_esp_system_init_fn_array_start; --p) {
  153. if (p->cores & BIT(cpu_hal_get_core_id())) {
  154. (*(p->fn))();
  155. }
  156. }
  157. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  158. s_system_inited[cpu_hal_get_core_id()] = true;
  159. #endif
  160. }
  161. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  162. static void esp_startup_start_app_other_cores_default(void)
  163. {
  164. while (1) {
  165. esp_rom_delay_us(UINT32_MAX);
  166. }
  167. }
  168. static void IRAM_ATTR start_cpu_other_cores_default(void)
  169. {
  170. do_system_init_fn();
  171. while (!s_system_full_inited) {
  172. esp_rom_delay_us(100);
  173. }
  174. esp_startup_start_app_other_cores();
  175. }
  176. #endif
  177. static void do_core_init(void)
  178. {
  179. /* Initialize heap allocator. WARNING: This *needs* to happen *after* the app cpu has booted.
  180. If the heap allocator is initialized first, it will put free memory linked list items into
  181. memory also used by the ROM. Starting the app cpu will let its ROM initialize that memory,
  182. corrupting those linked lists. Initializing the allocator *after* the app cpu has booted
  183. works around this problem.
  184. With SPI RAM enabled, there's a second reason: half of the SPI RAM will be managed by the
  185. app CPU, and when that is not up yet, the memory will be inaccessible and heap_caps_init may
  186. fail initializing it properly. */
  187. heap_caps_init();
  188. // When apptrace module is enabled, there will be SEGGER_SYSVIEW calls in the newlib init.
  189. // SEGGER_SYSVIEW relies on apptrace module
  190. // apptrace module uses esp_timer_get_time to determine timeout conditions.
  191. // esp_timer early initialization is required for esp_timer_get_time to work.
  192. esp_timer_early_init();
  193. esp_newlib_init();
  194. if (g_spiram_ok) {
  195. #if CONFIG_SPIRAM_BOOT_INIT && (CONFIG_SPIRAM_USE_CAPS_ALLOC || CONFIG_SPIRAM_USE_MALLOC)
  196. esp_err_t r=esp_spiram_add_to_heapalloc();
  197. if (r != ESP_OK) {
  198. ESP_EARLY_LOGE(TAG, "External RAM could not be added to heap!");
  199. abort();
  200. }
  201. #if CONFIG_SPIRAM_USE_MALLOC
  202. heap_caps_malloc_extmem_enable(CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL);
  203. #endif
  204. #endif
  205. }
  206. #if CONFIG_ESP32_BROWNOUT_DET || \
  207. CONFIG_ESP32S2_BROWNOUT_DET || \
  208. CONFIG_ESP32S3_BROWNOUT_DET || \
  209. CONFIG_ESP32C3_BROWNOUT_DET || \
  210. CONFIG_ESP32H2_BROWNOUT_DET || \
  211. CONFIG_ESP8684_BROWNOUT_DET
  212. // [refactor-todo] leads to call chain rtc_is_register (driver) -> esp_intr_alloc (esp32/esp32s2) ->
  213. // malloc (newlib) -> heap_caps_malloc (heap), so heap must be at least initialized
  214. esp_brownout_init();
  215. #endif
  216. esp_newlib_time_init();
  217. #if CONFIG_VFS_SUPPORT_IO
  218. // VFS console register.
  219. esp_err_t vfs_err = esp_vfs_console_register();
  220. assert(vfs_err == ESP_OK && "Failed to register vfs console");
  221. #endif
  222. #if defined(CONFIG_VFS_SUPPORT_IO) && !defined(CONFIG_ESP_CONSOLE_NONE)
  223. const static char *default_stdio_dev = "/dev/console/";
  224. esp_reent_init(_GLOBAL_REENT);
  225. _GLOBAL_REENT->_stdin = fopen(default_stdio_dev, "r");
  226. _GLOBAL_REENT->_stdout = fopen(default_stdio_dev, "w");
  227. _GLOBAL_REENT->_stderr = fopen(default_stdio_dev, "w");
  228. #else // defined(CONFIG_VFS_SUPPORT_IO) && !defined(CONFIG_ESP_CONSOLE_NONE)
  229. _REENT_SMALL_CHECK_INIT(_GLOBAL_REENT);
  230. #endif // defined(CONFIG_VFS_SUPPORT_IO) && !defined(CONFIG_ESP_CONSOLE_NONE)
  231. esp_err_t err __attribute__((unused));
  232. err = esp_pthread_init();
  233. assert(err == ESP_OK && "Failed to init pthread module!");
  234. spi_flash_init();
  235. /* init default OS-aware flash access critical section */
  236. spi_flash_guard_set(&g_flash_guard_default_ops);
  237. esp_flash_app_init();
  238. esp_err_t flash_ret = esp_flash_init_default_chip();
  239. assert(flash_ret == ESP_OK);
  240. (void)flash_ret;
  241. #ifdef CONFIG_EFUSE_VIRTUAL
  242. ESP_LOGW(TAG, "eFuse virtual mode is enabled. If Secure boot or Flash encryption is enabled then it does not provide any security. FOR TESTING ONLY!");
  243. #ifdef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
  244. const esp_partition_t *efuse_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_EFUSE_EM, NULL);
  245. if (efuse_partition) {
  246. esp_efuse_init_virtual_mode_in_flash(efuse_partition->address, efuse_partition->size);
  247. }
  248. #endif
  249. #endif
  250. #if CONFIG_SECURE_DISABLE_ROM_DL_MODE
  251. err = esp_efuse_disable_rom_download_mode();
  252. assert(err == ESP_OK && "Failed to disable ROM download mode");
  253. #endif
  254. #if CONFIG_SECURE_ENABLE_SECURE_ROM_DL_MODE
  255. err = esp_efuse_enable_rom_secure_download_mode();
  256. assert(err == ESP_OK && "Failed to enable Secure Download mode");
  257. #endif
  258. #if CONFIG_ESP32_DISABLE_BASIC_ROM_CONSOLE
  259. esp_efuse_disable_basic_rom_console();
  260. #endif
  261. #ifdef CONFIG_SECURE_FLASH_ENC_ENABLED
  262. esp_flash_encryption_init_checks();
  263. #endif
  264. #if defined(CONFIG_SECURE_BOOT) || defined(CONFIG_SECURE_SIGNED_ON_UPDATE_NO_SECURE_BOOT)
  265. // Note: in some configs this may read flash, so placed after flash init
  266. esp_secure_boot_init_checks();
  267. #endif
  268. #if CONFIG_ESP_XT_WDT
  269. esp_xt_wdt_config_t cfg = {
  270. .timeout = CONFIG_ESP_XT_WDT_TIMEOUT,
  271. .auto_backup_clk_enable = CONFIG_ESP_XT_WDT_BACKUP_CLK_ENABLE,
  272. };
  273. err = esp_xt_wdt_init(&cfg);
  274. assert(err == ESP_OK && "Failed to init xtwdt");
  275. #endif
  276. }
  277. static void do_secondary_init(void)
  278. {
  279. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  280. // The port layer transferred control to this function with other cores 'paused',
  281. // resume execution so that cores might execute component initialization functions.
  282. startup_resume_other_cores();
  283. #endif
  284. // Execute initialization functions esp_system_init_fn_t assigned to the main core. While
  285. // this is happening, all other cores are executing the initialization functions
  286. // assigned to them since they have been resumed already.
  287. do_system_init_fn();
  288. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  289. // Wait for all cores to finish secondary init.
  290. volatile bool system_inited = false;
  291. while (!system_inited) {
  292. system_inited = true;
  293. for (int i = 0; i < SOC_CPU_CORES_NUM; i++) {
  294. system_inited &= s_system_inited[i];
  295. }
  296. esp_rom_delay_us(100);
  297. }
  298. #endif
  299. }
  300. static void start_cpu0_default(void)
  301. {
  302. ESP_EARLY_LOGI(TAG, "Pro cpu start user code");
  303. int cpu_freq = esp_clk_cpu_freq();
  304. ESP_EARLY_LOGI(TAG, "cpu freq: %d Hz", cpu_freq);
  305. // Display information about the current running image.
  306. if (LOG_LOCAL_LEVEL >= ESP_LOG_INFO) {
  307. const esp_app_desc_t *app_desc = esp_ota_get_app_description();
  308. ESP_EARLY_LOGI(TAG, "Application information:");
  309. #ifndef CONFIG_APP_EXCLUDE_PROJECT_NAME_VAR
  310. ESP_EARLY_LOGI(TAG, "Project name: %s", app_desc->project_name);
  311. #endif
  312. #ifndef CONFIG_APP_EXCLUDE_PROJECT_VER_VAR
  313. ESP_EARLY_LOGI(TAG, "App version: %s", app_desc->version);
  314. #endif
  315. #ifdef CONFIG_BOOTLOADER_APP_SECURE_VERSION
  316. ESP_EARLY_LOGI(TAG, "Secure version: %d", app_desc->secure_version);
  317. #endif
  318. #ifdef CONFIG_APP_COMPILE_TIME_DATE
  319. ESP_EARLY_LOGI(TAG, "Compile time: %s %s", app_desc->date, app_desc->time);
  320. #endif
  321. char buf[17];
  322. esp_ota_get_app_elf_sha256(buf, sizeof(buf));
  323. ESP_EARLY_LOGI(TAG, "ELF file SHA256: %s...", buf);
  324. ESP_EARLY_LOGI(TAG, "ESP-IDF: %s", app_desc->idf_ver);
  325. }
  326. // Initialize core components and services.
  327. do_core_init();
  328. // Execute constructors.
  329. do_global_ctors();
  330. // Execute init functions of other components; blocks
  331. // until all cores finish (when !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE).
  332. do_secondary_init();
  333. // Now that the application is about to start, disable boot watchdog
  334. #ifndef CONFIG_BOOTLOADER_WDT_DISABLE_IN_USER_CODE
  335. wdt_hal_context_t rtc_wdt_ctx = {.inst = WDT_RWDT, .rwdt_dev = &RTCCNTL};
  336. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  337. wdt_hal_disable(&rtc_wdt_ctx);
  338. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  339. #endif
  340. #if SOC_CPU_CORES_NUM > 1 && !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  341. s_system_full_inited = true;
  342. #endif
  343. esp_startup_start_app();
  344. while (1);
  345. }
  346. IRAM_ATTR ESP_SYSTEM_INIT_FN(init_components0, BIT(0))
  347. {
  348. esp_timer_init();
  349. #if CONFIG_ESP_SLEEP_GPIO_RESET_WORKAROUND && !CONFIG_PM_SLP_DISABLE_GPIO
  350. // Configure to isolate (disable the Input/Output/Pullup/Pulldown
  351. // function of the pin) all GPIO pins in sleep state
  352. esp_sleep_config_gpio_isolate();
  353. // Enable automatic switching of GPIO configuration
  354. esp_sleep_enable_gpio_switch(true);
  355. #endif
  356. #if CONFIG_APPTRACE_ENABLE
  357. esp_err_t err = esp_apptrace_init();
  358. assert(err == ESP_OK && "Failed to init apptrace module on PRO CPU!");
  359. #endif
  360. #if CONFIG_APPTRACE_SV_ENABLE
  361. SEGGER_SYSVIEW_Conf();
  362. #endif
  363. #if CONFIG_ESP_DEBUG_STUBS_ENABLE
  364. esp_dbg_stubs_init();
  365. #endif
  366. #if defined(CONFIG_PM_ENABLE)
  367. esp_pm_impl_init();
  368. #endif
  369. #if CONFIG_ESP_COREDUMP_ENABLE
  370. esp_core_dump_init();
  371. #endif
  372. #if SOC_APB_BACKUP_DMA
  373. esp_apb_backup_dma_lock_init();
  374. #endif
  375. #if CONFIG_SW_COEXIST_ENABLE || CONFIG_EXTERNAL_COEX_ENABLE
  376. esp_coex_adapter_register(&g_coex_adapter_funcs);
  377. coex_pre_init();
  378. #endif
  379. #ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
  380. ESP_EARLY_LOGD(TAG, "Setting C++ exception workarounds.");
  381. _Unwind_SetNoFunctionContextInstall(1);
  382. _Unwind_SetEnableExceptionFdeSorting(0);
  383. #endif // CONFIG_COMPILER_CXX_EXCEPTIONS
  384. }