startup.c 14 KB

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