bootloader_start.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. // Copyright 2015-2016 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 <string.h>
  15. #include <stdint.h>
  16. #include <limits.h>
  17. #include <sys/param.h>
  18. #include "esp_attr.h"
  19. #include "esp_log.h"
  20. #include "rom/cache.h"
  21. #include "rom/efuse.h"
  22. #include "rom/ets_sys.h"
  23. #include "rom/spi_flash.h"
  24. #include "rom/crc.h"
  25. #include "rom/rtc.h"
  26. #include "rom/uart.h"
  27. #include "rom/gpio.h"
  28. #include "rom/secure_boot.h"
  29. #include "soc/soc.h"
  30. #include "soc/cpu.h"
  31. #include "soc/rtc.h"
  32. #include "soc/dport_reg.h"
  33. #include "soc/io_mux_reg.h"
  34. #include "soc/efuse_reg.h"
  35. #include "soc/rtc_cntl_reg.h"
  36. #include "soc/timer_group_reg.h"
  37. #include "soc/gpio_reg.h"
  38. #include "soc/gpio_sig_map.h"
  39. #include "sdkconfig.h"
  40. #include "esp_image_format.h"
  41. #include "esp_secure_boot.h"
  42. #include "esp_flash_encrypt.h"
  43. #include "esp_flash_partitions.h"
  44. #include "bootloader_flash.h"
  45. #include "bootloader_random.h"
  46. #include "bootloader_config.h"
  47. #include "bootloader_clock.h"
  48. #include "flash_qio_mode.h"
  49. extern int _bss_start;
  50. extern int _bss_end;
  51. extern int _data_start;
  52. extern int _data_end;
  53. static const char* TAG = "boot";
  54. /* Reduce literal size for some generic string literals */
  55. #define MAP_MSG "Mapping segment %d as %s"
  56. #define MAP_ERR_MSG "Image contains multiple %s segments. Only the last one will be mapped."
  57. void bootloader_main();
  58. static void unpack_load_app(const esp_image_metadata_t *data);
  59. static void print_flash_info(const esp_image_header_t* pfhdr);
  60. static void set_cache_and_start_app(uint32_t drom_addr,
  61. uint32_t drom_load_addr,
  62. uint32_t drom_size,
  63. uint32_t irom_addr,
  64. uint32_t irom_load_addr,
  65. uint32_t irom_size,
  66. uint32_t entry_addr);
  67. static void update_flash_config(const esp_image_header_t* pfhdr);
  68. static void vddsdio_configure();
  69. static void flash_gpio_configure();
  70. static void uart_console_configure(void);
  71. static void wdt_reset_check(void);
  72. /*
  73. * We arrive here after the ROM bootloader finished loading this second stage bootloader from flash.
  74. * The hardware is mostly uninitialized, flash cache is down and the app CPU is in reset.
  75. * We do have a stack, so we can do the initialization in C.
  76. */
  77. void call_start_cpu0()
  78. {
  79. cpu_configure_region_protection();
  80. /* Sanity check that static RAM is after the stack */
  81. #ifndef NDEBUG
  82. {
  83. int *sp = get_sp();
  84. assert(&_bss_start <= &_bss_end);
  85. assert(&_data_start <= &_data_end);
  86. assert(sp < &_bss_start);
  87. assert(sp < &_data_start);
  88. }
  89. #endif
  90. //Clear bss
  91. memset(&_bss_start, 0, (&_bss_end - &_bss_start) * sizeof(_bss_start));
  92. /* completely reset MMU for both CPUs
  93. (in case serial bootloader was running) */
  94. Cache_Read_Disable(0);
  95. Cache_Read_Disable(1);
  96. Cache_Flush(0);
  97. Cache_Flush(1);
  98. mmu_init(0);
  99. DPORT_REG_SET_BIT(DPORT_APP_CACHE_CTRL1_REG, DPORT_APP_CACHE_MMU_IA_CLR);
  100. mmu_init(1);
  101. DPORT_REG_CLR_BIT(DPORT_APP_CACHE_CTRL1_REG, DPORT_APP_CACHE_MMU_IA_CLR);
  102. /* (above steps probably unnecessary for most serial bootloader
  103. usage, all that's absolutely needed is that we unmask DROM0
  104. cache on the following two lines - normal ROM boot exits with
  105. DROM0 cache unmasked, but serial bootloader exits with it
  106. masked. However can't hurt to be thorough and reset
  107. everything.)
  108. The lines which manipulate DPORT_APP_CACHE_MMU_IA_CLR bit are
  109. necessary to work around a hardware bug.
  110. */
  111. DPORT_REG_CLR_BIT(DPORT_PRO_CACHE_CTRL1_REG, DPORT_PRO_CACHE_MASK_DROM0);
  112. DPORT_REG_CLR_BIT(DPORT_APP_CACHE_CTRL1_REG, DPORT_APP_CACHE_MASK_DROM0);
  113. bootloader_main();
  114. }
  115. /** @brief Load partition table
  116. *
  117. * Parse partition table, get useful data such as location of
  118. * OTA data partition, factory app partition, and test app partition.
  119. *
  120. * @param bs bootloader state structure used to save read data
  121. * @return return true if the partition table was succesfully loaded and MD5 checksum is valid.
  122. */
  123. bool load_partition_table(bootloader_state_t* bs)
  124. {
  125. const esp_partition_info_t *partitions;
  126. const int ESP_PARTITION_TABLE_DATA_LEN = 0xC00; /* length of actual data (signature is appended to this) */
  127. char *partition_usage;
  128. esp_err_t err;
  129. int num_partitions;
  130. #ifdef CONFIG_SECURE_BOOT_ENABLED
  131. if(esp_secure_boot_enabled()) {
  132. ESP_LOGI(TAG, "Verifying partition table signature...");
  133. err = esp_secure_boot_verify_signature(ESP_PARTITION_TABLE_ADDR, ESP_PARTITION_TABLE_DATA_LEN);
  134. if (err != ESP_OK) {
  135. ESP_LOGE(TAG, "Failed to verify partition table signature.");
  136. return false;
  137. }
  138. ESP_LOGD(TAG, "Partition table signature verified");
  139. }
  140. #endif
  141. partitions = bootloader_mmap(ESP_PARTITION_TABLE_ADDR, ESP_PARTITION_TABLE_DATA_LEN);
  142. if (!partitions) {
  143. ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", ESP_PARTITION_TABLE_ADDR, ESP_PARTITION_TABLE_DATA_LEN);
  144. return false;
  145. }
  146. ESP_LOGD(TAG, "mapped partition table 0x%x at 0x%x", ESP_PARTITION_TABLE_ADDR, (intptr_t)partitions);
  147. err = esp_partition_table_basic_verify(partitions, true, &num_partitions);
  148. if (err != ESP_OK) {
  149. ESP_LOGE(TAG, "Failed to verify partition table");
  150. return false;
  151. }
  152. ESP_LOGI(TAG, "Partition Table:");
  153. ESP_LOGI(TAG, "## Label Usage Type ST Offset Length");
  154. for(int i = 0; i < num_partitions; i++) {
  155. const esp_partition_info_t *partition = &partitions[i];
  156. ESP_LOGD(TAG, "load partition table entry 0x%x", (intptr_t)partition);
  157. ESP_LOGD(TAG, "type=%x subtype=%x", partition->type, partition->subtype);
  158. partition_usage = "unknown";
  159. /* valid partition table */
  160. switch(partition->type) {
  161. case PART_TYPE_APP: /* app partition */
  162. switch(partition->subtype) {
  163. case PART_SUBTYPE_FACTORY: /* factory binary */
  164. bs->factory = partition->pos;
  165. partition_usage = "factory app";
  166. break;
  167. case PART_SUBTYPE_TEST: /* test binary */
  168. bs->test = partition->pos;
  169. partition_usage = "test app";
  170. break;
  171. default:
  172. /* OTA binary */
  173. if ((partition->subtype & ~PART_SUBTYPE_OTA_MASK) == PART_SUBTYPE_OTA_FLAG) {
  174. bs->ota[partition->subtype & PART_SUBTYPE_OTA_MASK] = partition->pos;
  175. ++bs->app_count;
  176. partition_usage = "OTA app";
  177. }
  178. else {
  179. partition_usage = "Unknown app";
  180. }
  181. break;
  182. }
  183. break; /* PART_TYPE_APP */
  184. case PART_TYPE_DATA: /* data partition */
  185. switch(partition->subtype) {
  186. case PART_SUBTYPE_DATA_OTA: /* ota data */
  187. bs->ota_info = partition->pos;
  188. partition_usage = "OTA data";
  189. break;
  190. case PART_SUBTYPE_DATA_RF:
  191. partition_usage = "RF data";
  192. break;
  193. case PART_SUBTYPE_DATA_WIFI:
  194. partition_usage = "WiFi data";
  195. break;
  196. default:
  197. partition_usage = "Unknown data";
  198. break;
  199. }
  200. break; /* PARTITION_USAGE_DATA */
  201. default: /* other partition type */
  202. break;
  203. }
  204. /* print partition type info */
  205. ESP_LOGI(TAG, "%2d %-16s %-16s %02x %02x %08x %08x", i, partition->label, partition_usage,
  206. partition->type, partition->subtype,
  207. partition->pos.offset, partition->pos.size);
  208. }
  209. bootloader_munmap(partitions);
  210. ESP_LOGI(TAG,"End of partition table");
  211. return true;
  212. }
  213. static uint32_t ota_select_crc(const esp_ota_select_entry_t *s)
  214. {
  215. return crc32_le(UINT32_MAX, (uint8_t*)&s->ota_seq, 4);
  216. }
  217. static bool ota_select_valid(const esp_ota_select_entry_t *s)
  218. {
  219. return s->ota_seq != UINT32_MAX && s->crc == ota_select_crc(s);
  220. }
  221. /* indexes used by index_to_partition are the OTA index
  222. number, or these special constants */
  223. #define FACTORY_INDEX (-1)
  224. #define TEST_APP_INDEX (-2)
  225. #define INVALID_INDEX (-99)
  226. /* Given a partition index, return the partition position data from the bootloader_state_t structure */
  227. static esp_partition_pos_t index_to_partition(const bootloader_state_t *bs, int index)
  228. {
  229. if (index == FACTORY_INDEX) {
  230. return bs->factory;
  231. }
  232. if (index == TEST_APP_INDEX) {
  233. return bs->test;
  234. }
  235. if (index >= 0 && index < MAX_OTA_SLOTS && index < bs->app_count) {
  236. return bs->ota[index];
  237. }
  238. esp_partition_pos_t invalid = { 0 };
  239. return invalid;
  240. }
  241. static void log_invalid_app_partition(int index)
  242. {
  243. const char *not_bootable = " is not bootable"; /* save a few string literal bytes */
  244. switch(index) {
  245. case FACTORY_INDEX:
  246. ESP_LOGE(TAG, "Factory app partition%s", not_bootable);
  247. break;
  248. case TEST_APP_INDEX:
  249. ESP_LOGE(TAG, "Factory test app partition%s", not_bootable);
  250. break;
  251. default:
  252. ESP_LOGE(TAG, "OTA app partition slot %d%s", index, not_bootable);
  253. break;
  254. }
  255. }
  256. /* Return the index of the selected boot partition.
  257. This is the preferred boot partition, as determined by the partition table &
  258. any OTA sequence number found in OTA data.
  259. This partition will only be booted if it contains a valid app image, otherwise load_boot_image() will search
  260. for a valid partition using this selection as the starting point.
  261. */
  262. static int get_selected_boot_partition(const bootloader_state_t *bs)
  263. {
  264. esp_ota_select_entry_t sa,sb;
  265. const esp_ota_select_entry_t *ota_select_map;
  266. if (bs->ota_info.offset != 0) {
  267. // partition table has OTA data partition
  268. if (bs->ota_info.size < 2 * SPI_SEC_SIZE) {
  269. ESP_LOGE(TAG, "ota_info partition size %d is too small (minimum %d bytes)", bs->ota_info.size, sizeof(esp_ota_select_entry_t));
  270. return INVALID_INDEX; // can't proceed
  271. }
  272. ESP_LOGD(TAG, "OTA data offset 0x%x", bs->ota_info.offset);
  273. ota_select_map = bootloader_mmap(bs->ota_info.offset, bs->ota_info.size);
  274. if (!ota_select_map) {
  275. ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", bs->ota_info.offset, bs->ota_info.size);
  276. return INVALID_INDEX; // can't proceed
  277. }
  278. memcpy(&sa, ota_select_map, sizeof(esp_ota_select_entry_t));
  279. memcpy(&sb, (uint8_t *)ota_select_map + SPI_SEC_SIZE, sizeof(esp_ota_select_entry_t));
  280. bootloader_munmap(ota_select_map);
  281. ESP_LOGD(TAG, "OTA sequence values A 0x%08x B 0x%08x", sa.ota_seq, sb.ota_seq);
  282. if(sa.ota_seq == UINT32_MAX && sb.ota_seq == UINT32_MAX) {
  283. ESP_LOGD(TAG, "OTA sequence numbers both empty (all-0xFF)");
  284. if (bs->factory.offset != 0) {
  285. ESP_LOGI(TAG, "Defaulting to factory image");
  286. return FACTORY_INDEX;
  287. } else {
  288. ESP_LOGI(TAG, "No factory image, trying OTA 0");
  289. return 0;
  290. }
  291. } else {
  292. bool ota_valid = false;
  293. const char *ota_msg;
  294. int ota_seq; // Raw OTA sequence number. May be more than # of OTA slots
  295. if(ota_select_valid(&sa) && ota_select_valid(&sb)) {
  296. ota_valid = true;
  297. ota_msg = "Both OTA values";
  298. ota_seq = MAX(sa.ota_seq, sb.ota_seq) - 1;
  299. } else if(ota_select_valid(&sa)) {
  300. ota_valid = true;
  301. ota_msg = "Only OTA sequence A is";
  302. ota_seq = sa.ota_seq - 1;
  303. } else if(ota_select_valid(&sb)) {
  304. ota_valid = true;
  305. ota_msg = "Only OTA sequence B is";
  306. ota_seq = sb.ota_seq - 1;
  307. }
  308. if (ota_valid) {
  309. int ota_slot = ota_seq % bs->app_count; // Actual OTA partition selection
  310. ESP_LOGD(TAG, "%s valid. Mapping seq %d -> OTA slot %d", ota_msg, ota_seq, ota_slot);
  311. return ota_slot;
  312. } else if (bs->factory.offset != 0) {
  313. ESP_LOGE(TAG, "ota data partition invalid, falling back to factory");
  314. return FACTORY_INDEX;
  315. } else {
  316. ESP_LOGE(TAG, "ota data partition invalid and no factory, will try all partitions");
  317. return FACTORY_INDEX;
  318. }
  319. }
  320. }
  321. // otherwise, start from factory app partition and let the search logic
  322. // proceed from there
  323. return FACTORY_INDEX;
  324. }
  325. /* Return true if a partition has a valid app image that was successfully loaded */
  326. static bool try_load_partition(const esp_partition_pos_t *partition, esp_image_metadata_t *data)
  327. {
  328. if (partition->size == 0) {
  329. ESP_LOGD(TAG, "Can't boot from zero-length partition");
  330. return false;
  331. }
  332. if (esp_image_load(ESP_IMAGE_LOAD, partition, data) == ESP_OK) {
  333. ESP_LOGI(TAG, "Loaded app from partition at offset 0x%x",
  334. partition->offset);
  335. return true;
  336. }
  337. return false;
  338. }
  339. #define TRY_LOG_FORMAT "Trying partition index %d offs 0x%x size 0x%x"
  340. /* Load the app for booting. Start from partition 'start_index', if not bootable then work backwards to FACTORY_INDEX
  341. * (ie try any OTA slots in descending order and then the factory partition).
  342. *
  343. * If still nothing, start from 'start_index + 1' and work up to highest numbered OTA partition.
  344. *
  345. * If still nothing, try TEST_APP_INDEX
  346. *
  347. * Returns true on success, false if there's no bootable app in the partition table.
  348. */
  349. static bool load_boot_image(const bootloader_state_t *bs, int start_index, esp_image_metadata_t *result)
  350. {
  351. int index = start_index;
  352. esp_partition_pos_t part;
  353. /* work backwards from start_index, down to the factory app */
  354. for(index = start_index; index >= FACTORY_INDEX; index--) {
  355. part = index_to_partition(bs, index);
  356. if (part.size == 0) {
  357. continue;
  358. }
  359. ESP_LOGD(TAG, TRY_LOG_FORMAT, index, part.offset, part.size);
  360. if (try_load_partition(&part, result)) {
  361. return true;
  362. }
  363. log_invalid_app_partition(index);
  364. }
  365. /* failing that work forwards from start_index, try valid OTA slots */
  366. for(index = start_index + 1; index < bs->app_count; index++) {
  367. part = index_to_partition(bs, index);
  368. if (part.size == 0) {
  369. continue;
  370. }
  371. ESP_LOGD(TAG, TRY_LOG_FORMAT, index, part.offset, part.size);
  372. if (try_load_partition(&part, result)) {
  373. return true;
  374. }
  375. log_invalid_app_partition(index);
  376. }
  377. if (try_load_partition(&bs->test, result)) {
  378. ESP_LOGW(TAG, "Falling back to test app as only bootable partition");
  379. return true;
  380. }
  381. ESP_LOGE(TAG, "No bootable app partitions in the partition table");
  382. bzero(result, sizeof(esp_image_metadata_t));
  383. return false;
  384. }
  385. /**
  386. * @function : bootloader_main
  387. * @description: entry function of 2nd bootloader
  388. *
  389. * @inputs: void
  390. */
  391. void bootloader_main()
  392. {
  393. vddsdio_configure();
  394. flash_gpio_configure();
  395. #if (CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ == 240)
  396. //Check if ESP32 is rated for a CPU frequency of 160MHz only
  397. if (REG_GET_BIT(EFUSE_BLK0_RDATA3_REG, EFUSE_RD_CHIP_CPU_FREQ_RATED) &&
  398. REG_GET_BIT(EFUSE_BLK0_RDATA3_REG, EFUSE_RD_CHIP_CPU_FREQ_LOW)) {
  399. ESP_LOGE(TAG, "Chip CPU frequency rated for 160MHz. Modify CPU frequency in menuconfig");
  400. return;
  401. }
  402. #endif
  403. bootloader_clock_configure();
  404. uart_console_configure();
  405. wdt_reset_check();
  406. ESP_LOGI(TAG, "ESP-IDF %s 2nd stage bootloader", IDF_VER);
  407. #if defined(CONFIG_SECURE_BOOT_ENABLED) || defined(CONFIG_FLASH_ENCRYPTION_ENABLED)
  408. esp_err_t err;
  409. #endif
  410. esp_image_header_t fhdr;
  411. bootloader_state_t bs = { 0 };
  412. ESP_LOGI(TAG, "compile time " __TIME__ );
  413. ets_set_appcpu_boot_addr(0);
  414. /* disable watch dog here */
  415. REG_CLR_BIT( RTC_CNTL_WDTCONFIG0_REG, RTC_CNTL_WDT_FLASHBOOT_MOD_EN );
  416. REG_CLR_BIT( TIMG_WDTCONFIG0_REG(0), TIMG_WDT_FLASHBOOT_MOD_EN );
  417. #ifndef CONFIG_SPI_FLASH_ROM_DRIVER_PATCH
  418. const uint32_t spiconfig = ets_efuse_get_spiconfig();
  419. if(spiconfig != EFUSE_SPICONFIG_SPI_DEFAULTS && spiconfig != EFUSE_SPICONFIG_HSPI_DEFAULTS) {
  420. ESP_LOGE(TAG, "SPI flash pins are overridden. \"Enable SPI flash ROM driver patched functions\" must be enabled in menuconfig");
  421. return;
  422. }
  423. #endif
  424. esp_rom_spiflash_unlock();
  425. ESP_LOGI(TAG, "Enabling RNG early entropy source...");
  426. bootloader_random_enable();
  427. #if CONFIG_FLASHMODE_QIO || CONFIG_FLASHMODE_QOUT
  428. bootloader_enable_qio_mode();
  429. #endif
  430. if (bootloader_flash_read(ESP_BOOTLOADER_OFFSET, &fhdr,
  431. sizeof(esp_image_header_t), true) != ESP_OK) {
  432. ESP_LOGE(TAG, "failed to load bootloader header!");
  433. return;
  434. }
  435. print_flash_info(&fhdr);
  436. update_flash_config(&fhdr);
  437. if (!load_partition_table(&bs)) {
  438. ESP_LOGE(TAG, "load partition table error!");
  439. return;
  440. }
  441. int boot_index = get_selected_boot_partition(&bs);
  442. if (boot_index == INVALID_INDEX) {
  443. return; // Unrecoverable failure (not due to corrupt ota data or bad partition contents)
  444. }
  445. // Start from the default, look for the first bootable partition
  446. esp_image_metadata_t image_data;
  447. if (!load_boot_image(&bs, boot_index, &image_data)) {
  448. return;
  449. }
  450. #ifdef CONFIG_SECURE_BOOT_ENABLED
  451. /* Generate secure digest from this bootloader to protect future
  452. modifications */
  453. ESP_LOGI(TAG, "Checking secure boot...");
  454. err = esp_secure_boot_permanently_enable();
  455. if (err != ESP_OK) {
  456. ESP_LOGE(TAG, "Bootloader digest generation failed (%d). SECURE BOOT IS NOT ENABLED.", err);
  457. /* Allow booting to continue, as the failure is probably
  458. due to user-configured EFUSEs for testing...
  459. */
  460. }
  461. #endif
  462. #ifdef CONFIG_FLASH_ENCRYPTION_ENABLED
  463. /* encrypt flash */
  464. ESP_LOGI(TAG, "Checking flash encryption...");
  465. bool flash_encryption_enabled = esp_flash_encryption_enabled();
  466. err = esp_flash_encrypt_check_and_update();
  467. if (err != ESP_OK) {
  468. ESP_LOGE(TAG, "Flash encryption check failed (%d).", err);
  469. return;
  470. }
  471. if (!flash_encryption_enabled && esp_flash_encryption_enabled()) {
  472. /* Flash encryption was just enabled for the first time,
  473. so issue a system reset to ensure flash encryption
  474. cache resets properly */
  475. ESP_LOGI(TAG, "Resetting with flash encryption enabled...");
  476. REG_WRITE(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_SW_SYS_RST);
  477. return;
  478. }
  479. #endif
  480. ESP_LOGI(TAG, "Disabling RNG early entropy source...");
  481. bootloader_random_disable();
  482. // copy loaded segments to RAM, set up caches for mapped segments, and start application
  483. unpack_load_app(&image_data);
  484. }
  485. static void unpack_load_app(const esp_image_metadata_t* data)
  486. {
  487. uint32_t drom_addr = 0;
  488. uint32_t drom_load_addr = 0;
  489. uint32_t drom_size = 0;
  490. uint32_t irom_addr = 0;
  491. uint32_t irom_load_addr = 0;
  492. uint32_t irom_size = 0;
  493. // Find DROM & IROM addresses, to configure cache mappings
  494. for (int i = 0; i < data->image.segment_count; i++) {
  495. const esp_image_segment_header_t *header = &data->segments[i];
  496. if (header->load_addr >= SOC_IROM_LOW && header->load_addr < SOC_IROM_HIGH) {
  497. if (drom_addr != 0) {
  498. ESP_LOGE(TAG, MAP_ERR_MSG, "DROM");
  499. } else {
  500. ESP_LOGD(TAG, "Mapping segment %d as %s", i, "DROM");
  501. }
  502. drom_addr = data->segment_data[i];
  503. drom_load_addr = header->load_addr;
  504. drom_size = header->data_len;
  505. }
  506. if (header->load_addr >= SOC_DROM_LOW && header->load_addr < SOC_DROM_HIGH) {
  507. if (irom_addr != 0) {
  508. ESP_LOGE(TAG, MAP_ERR_MSG, "IROM");
  509. } else {
  510. ESP_LOGD(TAG, "Mapping segment %d as %s", i, "IROM");
  511. }
  512. irom_addr = data->segment_data[i];
  513. irom_load_addr = header->load_addr;
  514. irom_size = header->data_len;
  515. }
  516. }
  517. ESP_LOGD(TAG, "calling set_cache_and_start_app");
  518. set_cache_and_start_app(drom_addr,
  519. drom_load_addr,
  520. drom_size,
  521. irom_addr,
  522. irom_load_addr,
  523. irom_size,
  524. data->image.entry_addr);
  525. }
  526. static void set_cache_and_start_app(
  527. uint32_t drom_addr,
  528. uint32_t drom_load_addr,
  529. uint32_t drom_size,
  530. uint32_t irom_addr,
  531. uint32_t irom_load_addr,
  532. uint32_t irom_size,
  533. uint32_t entry_addr)
  534. {
  535. ESP_LOGD(TAG, "configure drom and irom and start");
  536. Cache_Read_Disable( 0 );
  537. Cache_Flush( 0 );
  538. /* Clear the MMU entries that are already set up,
  539. so the new app only has the mappings it creates.
  540. */
  541. for (int i = 0; i < DPORT_FLASH_MMU_TABLE_SIZE; i++) {
  542. DPORT_PRO_FLASH_MMU_TABLE[i] = DPORT_FLASH_MMU_TABLE_INVALID_VAL;
  543. }
  544. uint32_t drom_page_count = (drom_size + 64*1024 - 1) / (64*1024); // round up to 64k
  545. ESP_LOGV(TAG, "d mmu set paddr=%08x vaddr=%08x size=%d n=%d", drom_addr & 0xffff0000, drom_load_addr & 0xffff0000, drom_size, drom_page_count );
  546. int rc = cache_flash_mmu_set( 0, 0, drom_load_addr & 0xffff0000, drom_addr & 0xffff0000, 64, drom_page_count );
  547. ESP_LOGV(TAG, "rc=%d", rc );
  548. rc = cache_flash_mmu_set( 1, 0, drom_load_addr & 0xffff0000, drom_addr & 0xffff0000, 64, drom_page_count );
  549. ESP_LOGV(TAG, "rc=%d", rc );
  550. uint32_t irom_page_count = (irom_size + 64*1024 - 1) / (64*1024); // round up to 64k
  551. ESP_LOGV(TAG, "i mmu set paddr=%08x vaddr=%08x size=%d n=%d", irom_addr & 0xffff0000, irom_load_addr & 0xffff0000, irom_size, irom_page_count );
  552. rc = cache_flash_mmu_set( 0, 0, irom_load_addr & 0xffff0000, irom_addr & 0xffff0000, 64, irom_page_count );
  553. ESP_LOGV(TAG, "rc=%d", rc );
  554. rc = cache_flash_mmu_set( 1, 0, irom_load_addr & 0xffff0000, irom_addr & 0xffff0000, 64, irom_page_count );
  555. ESP_LOGV(TAG, "rc=%d", rc );
  556. DPORT_REG_CLR_BIT( DPORT_PRO_CACHE_CTRL1_REG, (DPORT_PRO_CACHE_MASK_IRAM0) | (DPORT_PRO_CACHE_MASK_IRAM1 & 0) | (DPORT_PRO_CACHE_MASK_IROM0 & 0) | DPORT_PRO_CACHE_MASK_DROM0 | DPORT_PRO_CACHE_MASK_DRAM1 );
  557. DPORT_REG_CLR_BIT( DPORT_APP_CACHE_CTRL1_REG, (DPORT_APP_CACHE_MASK_IRAM0) | (DPORT_APP_CACHE_MASK_IRAM1 & 0) | (DPORT_APP_CACHE_MASK_IROM0 & 0) | DPORT_APP_CACHE_MASK_DROM0 | DPORT_APP_CACHE_MASK_DRAM1 );
  558. Cache_Read_Enable( 0 );
  559. // Application will need to do Cache_Flush(1) and Cache_Read_Enable(1)
  560. ESP_LOGD(TAG, "start: 0x%08x", entry_addr);
  561. typedef void (*entry_t)(void);
  562. entry_t entry = ((entry_t) entry_addr);
  563. // TODO: we have used quite a bit of stack at this point.
  564. // use "movsp" instruction to reset stack back to where ROM stack starts.
  565. (*entry)();
  566. }
  567. static void update_flash_config(const esp_image_header_t* pfhdr)
  568. {
  569. uint32_t size;
  570. switch(pfhdr->spi_size) {
  571. case ESP_IMAGE_FLASH_SIZE_1MB:
  572. size = 1;
  573. break;
  574. case ESP_IMAGE_FLASH_SIZE_2MB:
  575. size = 2;
  576. break;
  577. case ESP_IMAGE_FLASH_SIZE_4MB:
  578. size = 4;
  579. break;
  580. case ESP_IMAGE_FLASH_SIZE_8MB:
  581. size = 8;
  582. break;
  583. case ESP_IMAGE_FLASH_SIZE_16MB:
  584. size = 16;
  585. break;
  586. default:
  587. size = 2;
  588. }
  589. Cache_Read_Disable( 0 );
  590. // Set flash chip size
  591. esp_rom_spiflash_config_param(g_rom_flashchip.device_id, size * 0x100000, 0x10000, 0x1000, 0x100, 0xffff);
  592. // TODO: set mode
  593. // TODO: set frequency
  594. Cache_Flush(0);
  595. Cache_Read_Enable( 0 );
  596. }
  597. static void print_flash_info(const esp_image_header_t* phdr)
  598. {
  599. #if (BOOT_LOG_LEVEL >= BOOT_LOG_LEVEL_NOTICE)
  600. ESP_LOGD(TAG, "magic %02x", phdr->magic );
  601. ESP_LOGD(TAG, "segments %02x", phdr->segment_count );
  602. ESP_LOGD(TAG, "spi_mode %02x", phdr->spi_mode );
  603. ESP_LOGD(TAG, "spi_speed %02x", phdr->spi_speed );
  604. ESP_LOGD(TAG, "spi_size %02x", phdr->spi_size );
  605. const char* str;
  606. switch ( phdr->spi_speed ) {
  607. case ESP_IMAGE_SPI_SPEED_40M:
  608. str = "40MHz";
  609. break;
  610. case ESP_IMAGE_SPI_SPEED_26M:
  611. str = "26.7MHz";
  612. break;
  613. case ESP_IMAGE_SPI_SPEED_20M:
  614. str = "20MHz";
  615. break;
  616. case ESP_IMAGE_SPI_SPEED_80M:
  617. str = "80MHz";
  618. break;
  619. default:
  620. str = "20MHz";
  621. break;
  622. }
  623. ESP_LOGI(TAG, "SPI Speed : %s", str );
  624. /* SPI mode could have been set to QIO during boot already,
  625. so test the SPI registers not the flash header */
  626. uint32_t spi_ctrl = REG_READ(SPI_CTRL_REG(0));
  627. if (spi_ctrl & SPI_FREAD_QIO) {
  628. str = "QIO";
  629. } else if (spi_ctrl & SPI_FREAD_QUAD) {
  630. str = "QOUT";
  631. } else if (spi_ctrl & SPI_FREAD_DIO) {
  632. str = "DIO";
  633. } else if (spi_ctrl & SPI_FREAD_DUAL) {
  634. str = "DOUT";
  635. } else if (spi_ctrl & SPI_FASTRD_MODE) {
  636. str = "FAST READ";
  637. } else {
  638. str = "SLOW READ";
  639. }
  640. ESP_LOGI(TAG, "SPI Mode : %s", str );
  641. switch ( phdr->spi_size ) {
  642. case ESP_IMAGE_FLASH_SIZE_1MB:
  643. str = "1MB";
  644. break;
  645. case ESP_IMAGE_FLASH_SIZE_2MB:
  646. str = "2MB";
  647. break;
  648. case ESP_IMAGE_FLASH_SIZE_4MB:
  649. str = "4MB";
  650. break;
  651. case ESP_IMAGE_FLASH_SIZE_8MB:
  652. str = "8MB";
  653. break;
  654. case ESP_IMAGE_FLASH_SIZE_16MB:
  655. str = "16MB";
  656. break;
  657. default:
  658. str = "2MB";
  659. break;
  660. }
  661. ESP_LOGI(TAG, "SPI Flash Size : %s", str );
  662. #endif
  663. }
  664. static void vddsdio_configure()
  665. {
  666. #if CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V
  667. rtc_vddsdio_config_t cfg = rtc_vddsdio_get_config();
  668. if (cfg.enable == 1 && cfg.tieh == 0) { // VDDSDIO regulator is enabled @ 1.8V
  669. cfg.drefh = 3;
  670. cfg.drefm = 3;
  671. cfg.drefl = 3;
  672. cfg.force = 1;
  673. rtc_vddsdio_set_config(cfg);
  674. ets_delay_us(10); // wait for regulator to become stable
  675. }
  676. #endif // CONFIG_BOOTLOADER_VDDSDIO_BOOST
  677. }
  678. #define FLASH_CLK_IO 6
  679. #define FLASH_CS_IO 11
  680. #define FLASH_SPIQ_IO 7
  681. #define FLASH_SPID_IO 8
  682. #define FLASH_SPIWP_IO 10
  683. #define FLASH_SPIHD_IO 9
  684. #define FLASH_IO_MATRIX_DUMMY_40M 1
  685. #define FLASH_IO_MATRIX_DUMMY_80M 2
  686. static void IRAM_ATTR flash_gpio_configure()
  687. {
  688. int spi_cache_dummy = 0;
  689. int drv = 2;
  690. #if CONFIG_FLASHMODE_QIO
  691. spi_cache_dummy = SPI0_R_QIO_DUMMY_CYCLELEN; //qio 3
  692. #elif CONFIG_FLASHMODE_QOUT
  693. spi_cache_dummy = SPI0_R_FAST_DUMMY_CYCLELEN; //qout 7
  694. #elif CONFIG_FLASHMODE_DIO
  695. spi_cache_dummy = SPI0_R_DIO_DUMMY_CYCLELEN; //dio 3
  696. #elif CONFIG_FLASHMODE_DOUT
  697. spi_cache_dummy = SPI0_R_FAST_DUMMY_CYCLELEN; //dout 7
  698. #endif
  699. /* dummy_len_plus values defined in ROM for SPI flash configuration */
  700. extern uint8_t g_rom_spiflash_dummy_len_plus[];
  701. #if CONFIG_ESPTOOLPY_FLASHFREQ_40M
  702. g_rom_spiflash_dummy_len_plus[0] = FLASH_IO_MATRIX_DUMMY_40M;
  703. g_rom_spiflash_dummy_len_plus[1] = FLASH_IO_MATRIX_DUMMY_40M;
  704. SET_PERI_REG_BITS(SPI_USER1_REG(0), SPI_USR_DUMMY_CYCLELEN_V, spi_cache_dummy + FLASH_IO_MATRIX_DUMMY_40M, SPI_USR_DUMMY_CYCLELEN_S); //DUMMY
  705. #elif CONFIG_ESPTOOLPY_FLASHFREQ_80M
  706. g_rom_spiflash_dummy_len_plus[0] = FLASH_IO_MATRIX_DUMMY_80M;
  707. g_rom_spiflash_dummy_len_plus[1] = FLASH_IO_MATRIX_DUMMY_80M;
  708. SET_PERI_REG_BITS(SPI_USER1_REG(0), SPI_USR_DUMMY_CYCLELEN_V, spi_cache_dummy + FLASH_IO_MATRIX_DUMMY_80M, SPI_USR_DUMMY_CYCLELEN_S); //DUMMY
  709. drv = 3;
  710. #endif
  711. uint32_t chip_ver = REG_GET_FIELD(EFUSE_BLK0_RDATA3_REG, EFUSE_RD_CHIP_VER_PKG);
  712. uint32_t pkg_ver = chip_ver & 0x7;
  713. if (pkg_ver == EFUSE_RD_CHIP_VER_PKG_ESP32D2WDQ5) {
  714. // For ESP32D2WD the SPI pins are already configured
  715. // flash clock signal should come from IO MUX.
  716. PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CLK_U, FUNC_SD_CLK_SPICLK);
  717. SET_PERI_REG_BITS(PERIPHS_IO_MUX_SD_CLK_U, FUN_DRV, drv, FUN_DRV_S);
  718. } else if (pkg_ver == EFUSE_RD_CHIP_VER_PKG_ESP32PICOD2) {
  719. // For ESP32PICOD2 the SPI pins are already configured
  720. // flash clock signal should come from IO MUX.
  721. PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CLK_U, FUNC_SD_CLK_SPICLK);
  722. SET_PERI_REG_BITS(PERIPHS_IO_MUX_SD_CLK_U, FUN_DRV, drv, FUN_DRV_S);
  723. } else if (pkg_ver == EFUSE_RD_CHIP_VER_PKG_ESP32PICOD4) {
  724. // For ESP32PICOD4 the SPI pins are already configured
  725. // flash clock signal should come from IO MUX.
  726. PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CLK_U, FUNC_SD_CLK_SPICLK);
  727. SET_PERI_REG_BITS(PERIPHS_IO_MUX_SD_CLK_U, FUN_DRV, drv, FUN_DRV_S);
  728. } else {
  729. const uint32_t spiconfig = ets_efuse_get_spiconfig();
  730. if (spiconfig == EFUSE_SPICONFIG_SPI_DEFAULTS) {
  731. gpio_matrix_out(FLASH_CS_IO, SPICS0_OUT_IDX, 0, 0);
  732. gpio_matrix_out(FLASH_SPIQ_IO, SPIQ_OUT_IDX, 0, 0);
  733. gpio_matrix_in(FLASH_SPIQ_IO, SPIQ_IN_IDX, 0);
  734. gpio_matrix_out(FLASH_SPID_IO, SPID_OUT_IDX, 0, 0);
  735. gpio_matrix_in(FLASH_SPID_IO, SPID_IN_IDX, 0);
  736. gpio_matrix_out(FLASH_SPIWP_IO, SPIWP_OUT_IDX, 0, 0);
  737. gpio_matrix_in(FLASH_SPIWP_IO, SPIWP_IN_IDX, 0);
  738. gpio_matrix_out(FLASH_SPIHD_IO, SPIHD_OUT_IDX, 0, 0);
  739. gpio_matrix_in(FLASH_SPIHD_IO, SPIHD_IN_IDX, 0);
  740. //select pin function gpio
  741. PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA0_U, PIN_FUNC_GPIO);
  742. PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA1_U, PIN_FUNC_GPIO);
  743. PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA2_U, PIN_FUNC_GPIO);
  744. PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA3_U, PIN_FUNC_GPIO);
  745. PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CMD_U, PIN_FUNC_GPIO);
  746. // flash clock signal should come from IO MUX.
  747. // set drive ability for clock
  748. PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CLK_U, FUNC_SD_CLK_SPICLK);
  749. SET_PERI_REG_BITS(PERIPHS_IO_MUX_SD_CLK_U, FUN_DRV, drv, FUN_DRV_S);
  750. }
  751. }
  752. }
  753. static void uart_console_configure(void)
  754. {
  755. #if CONFIG_CONSOLE_UART_NONE
  756. ets_install_putc1(NULL);
  757. ets_install_putc2(NULL);
  758. #else // CONFIG_CONSOLE_UART_NONE
  759. const int uart_num = CONFIG_CONSOLE_UART_NUM;
  760. uartAttach();
  761. ets_install_uart_printf();
  762. // Wait for UART FIFO to be empty.
  763. uart_tx_wait_idle(0);
  764. #if CONFIG_CONSOLE_UART_CUSTOM
  765. // Some constants to make the following code less upper-case
  766. const int uart_tx_gpio = CONFIG_CONSOLE_UART_TX_GPIO;
  767. const int uart_rx_gpio = CONFIG_CONSOLE_UART_RX_GPIO;
  768. // Switch to the new UART (this just changes UART number used for
  769. // ets_printf in ROM code).
  770. uart_tx_switch(uart_num);
  771. // If console is attached to UART1 or if non-default pins are used,
  772. // need to reconfigure pins using GPIO matrix
  773. if (uart_num != 0 || uart_tx_gpio != 1 || uart_rx_gpio != 3) {
  774. // Change pin mode for GPIO1/3 from UART to GPIO
  775. PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_U0RXD_GPIO3);
  776. PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD_GPIO1);
  777. // Route GPIO signals to/from pins
  778. // (arrays should be optimized away by the compiler)
  779. const uint32_t tx_idx_list[3] = { U0TXD_OUT_IDX, U1TXD_OUT_IDX, U2TXD_OUT_IDX };
  780. const uint32_t rx_idx_list[3] = { U0RXD_IN_IDX, U1RXD_IN_IDX, U2RXD_IN_IDX };
  781. const uint32_t tx_idx = tx_idx_list[uart_num];
  782. const uint32_t rx_idx = rx_idx_list[uart_num];
  783. gpio_matrix_out(uart_tx_gpio, tx_idx, 0, 0);
  784. gpio_matrix_in(uart_rx_gpio, rx_idx, 0);
  785. }
  786. #endif // CONFIG_CONSOLE_UART_CUSTOM
  787. // Set configured UART console baud rate
  788. const int uart_baud = CONFIG_CONSOLE_UART_BAUDRATE;
  789. uart_div_modify(uart_num, (rtc_clk_apb_freq_get() << 4) / uart_baud);
  790. #endif // CONFIG_CONSOLE_UART_NONE
  791. }
  792. static void wdt_reset_cpu0_info_enable(void)
  793. {
  794. //We do not reset core1 info here because it didn't work before cpu1 was up. So we put it into call_start_cpu1.
  795. DPORT_REG_SET_BIT(DPORT_PRO_CPU_RECORD_CTRL_REG, DPORT_PRO_CPU_PDEBUG_ENABLE | DPORT_PRO_CPU_RECORD_ENABLE);
  796. DPORT_REG_CLR_BIT(DPORT_PRO_CPU_RECORD_CTRL_REG, DPORT_PRO_CPU_RECORD_ENABLE);
  797. }
  798. static void wdt_reset_info_dump(int cpu)
  799. {
  800. uint32_t inst = 0, pid = 0, stat = 0, data = 0, pc = 0,
  801. lsstat = 0, lsaddr = 0, lsdata = 0, dstat = 0;
  802. char *cpu_name = cpu ? "APP" : "PRO";
  803. if (cpu == 0) {
  804. stat = DPORT_REG_READ(DPORT_PRO_CPU_RECORD_STATUS_REG);
  805. pid = DPORT_REG_READ(DPORT_PRO_CPU_RECORD_PID_REG);
  806. inst = DPORT_REG_READ(DPORT_PRO_CPU_RECORD_PDEBUGINST_REG);
  807. dstat = DPORT_REG_READ(DPORT_PRO_CPU_RECORD_PDEBUGSTATUS_REG);
  808. data = DPORT_REG_READ(DPORT_PRO_CPU_RECORD_PDEBUGDATA_REG);
  809. pc = DPORT_REG_READ(DPORT_PRO_CPU_RECORD_PDEBUGPC_REG);
  810. lsstat = DPORT_REG_READ(DPORT_PRO_CPU_RECORD_PDEBUGLS0STAT_REG);
  811. lsaddr = DPORT_REG_READ(DPORT_PRO_CPU_RECORD_PDEBUGLS0ADDR_REG);
  812. lsdata = DPORT_REG_READ(DPORT_PRO_CPU_RECORD_PDEBUGLS0DATA_REG);
  813. } else {
  814. stat = DPORT_REG_READ(DPORT_APP_CPU_RECORD_STATUS_REG);
  815. pid = DPORT_REG_READ(DPORT_APP_CPU_RECORD_PID_REG);
  816. inst = DPORT_REG_READ(DPORT_APP_CPU_RECORD_PDEBUGINST_REG);
  817. dstat = DPORT_REG_READ(DPORT_APP_CPU_RECORD_PDEBUGSTATUS_REG);
  818. data = DPORT_REG_READ(DPORT_APP_CPU_RECORD_PDEBUGDATA_REG);
  819. pc = DPORT_REG_READ(DPORT_APP_CPU_RECORD_PDEBUGPC_REG);
  820. lsstat = DPORT_REG_READ(DPORT_APP_CPU_RECORD_PDEBUGLS0STAT_REG);
  821. lsaddr = DPORT_REG_READ(DPORT_APP_CPU_RECORD_PDEBUGLS0ADDR_REG);
  822. lsdata = DPORT_REG_READ(DPORT_APP_CPU_RECORD_PDEBUGLS0DATA_REG);
  823. }
  824. if (DPORT_RECORD_PDEBUGINST_SZ(inst) == 0 &&
  825. DPORT_RECORD_PDEBUGSTATUS_BBCAUSE(dstat) == DPORT_RECORD_PDEBUGSTATUS_BBCAUSE_WAITI) {
  826. ESP_LOGW(TAG, "WDT reset info: %s CPU PC=0x%x (waiti mode)", cpu_name, pc);
  827. } else {
  828. ESP_LOGW(TAG, "WDT reset info: %s CPU PC=0x%x", cpu_name, pc);
  829. }
  830. ESP_LOGD(TAG, "WDT reset info: %s CPU STATUS 0x%08x", cpu_name, stat);
  831. ESP_LOGD(TAG, "WDT reset info: %s CPU PID 0x%08x", cpu_name, pid);
  832. ESP_LOGD(TAG, "WDT reset info: %s CPU PDEBUGINST 0x%08x", cpu_name, inst);
  833. ESP_LOGD(TAG, "WDT reset info: %s CPU PDEBUGSTATUS 0x%08x", cpu_name, dstat);
  834. ESP_LOGD(TAG, "WDT reset info: %s CPU PDEBUGDATA 0x%08x", cpu_name, data);
  835. ESP_LOGD(TAG, "WDT reset info: %s CPU PDEBUGPC 0x%08x", cpu_name, pc);
  836. ESP_LOGD(TAG, "WDT reset info: %s CPU PDEBUGLS0STAT 0x%08x", cpu_name, lsstat);
  837. ESP_LOGD(TAG, "WDT reset info: %s CPU PDEBUGLS0ADDR 0x%08x", cpu_name, lsaddr);
  838. ESP_LOGD(TAG, "WDT reset info: %s CPU PDEBUGLS0DATA 0x%08x", cpu_name, lsdata);
  839. }
  840. static void wdt_reset_check(void)
  841. {
  842. int wdt_rst = 0;
  843. RESET_REASON rst_reas[2];
  844. rst_reas[0] = rtc_get_reset_reason(0);
  845. rst_reas[1] = rtc_get_reset_reason(1);
  846. if (rst_reas[0] == RTCWDT_SYS_RESET || rst_reas[0] == TG0WDT_SYS_RESET || rst_reas[0] == TG1WDT_SYS_RESET ||
  847. rst_reas[0] == TGWDT_CPU_RESET || rst_reas[0] == RTCWDT_CPU_RESET) {
  848. ESP_LOGW(TAG, "PRO CPU has been reset by WDT.");
  849. wdt_rst = 1;
  850. }
  851. if (rst_reas[1] == RTCWDT_SYS_RESET || rst_reas[1] == TG0WDT_SYS_RESET || rst_reas[1] == TG1WDT_SYS_RESET ||
  852. rst_reas[1] == TGWDT_CPU_RESET || rst_reas[1] == RTCWDT_CPU_RESET) {
  853. ESP_LOGW(TAG, "APP CPU has been reset by WDT.");
  854. wdt_rst = 1;
  855. }
  856. if (wdt_rst) {
  857. // if reset by WDT dump info from trace port
  858. wdt_reset_info_dump(0);
  859. wdt_reset_info_dump(1);
  860. }
  861. wdt_reset_cpu0_info_enable();
  862. }
  863. void __assert_func(const char *file, int line, const char *func, const char *expr)
  864. {
  865. ESP_LOGE(TAG, "Assert failed in %s, %s:%d (%s)", func, file, line, expr);
  866. while(1) {}
  867. }