bootloader_start.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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 "flash_qio_mode.h"
  48. extern int _bss_start;
  49. extern int _bss_end;
  50. extern int _data_start;
  51. extern int _data_end;
  52. static const char* TAG = "boot";
  53. /* Reduce literal size for some generic string literals */
  54. #define MAP_MSG "Mapping segment %d as %s"
  55. #define MAP_ERR_MSG "Image contains multiple %s segments. Only the last one will be mapped."
  56. void bootloader_main();
  57. static void unpack_load_app(const esp_partition_pos_t *app_node);
  58. static void print_flash_info(const esp_image_header_t* pfhdr);
  59. static void set_cache_and_start_app(uint32_t drom_addr,
  60. uint32_t drom_load_addr,
  61. uint32_t drom_size,
  62. uint32_t irom_addr,
  63. uint32_t irom_load_addr,
  64. uint32_t irom_size,
  65. uint32_t entry_addr);
  66. static void update_flash_config(const esp_image_header_t* pfhdr);
  67. static void clock_configure(void);
  68. static void uart_console_configure(void);
  69. static void wdt_reset_check(void);
  70. /*
  71. * We arrive here after the ROM bootloader finished loading this second stage bootloader from flash.
  72. * The hardware is mostly uninitialized, flash cache is down and the app CPU is in reset.
  73. * We do have a stack, so we can do the initialization in C.
  74. */
  75. void call_start_cpu0()
  76. {
  77. cpu_configure_region_protection();
  78. /* Sanity check that static RAM is after the stack */
  79. #ifndef NDEBUG
  80. {
  81. int *sp = get_sp();
  82. assert(&_bss_start <= &_bss_end);
  83. assert(&_data_start <= &_data_end);
  84. assert(sp < &_bss_start);
  85. assert(sp < &_data_start);
  86. }
  87. #endif
  88. //Clear bss
  89. memset(&_bss_start, 0, (&_bss_end - &_bss_start) * sizeof(_bss_start));
  90. /* completely reset MMU for both CPUs
  91. (in case serial bootloader was running) */
  92. Cache_Read_Disable(0);
  93. Cache_Read_Disable(1);
  94. Cache_Flush(0);
  95. Cache_Flush(1);
  96. mmu_init(0);
  97. DPORT_REG_SET_BIT(DPORT_APP_CACHE_CTRL1_REG, DPORT_APP_CACHE_MMU_IA_CLR);
  98. mmu_init(1);
  99. DPORT_REG_CLR_BIT(DPORT_APP_CACHE_CTRL1_REG, DPORT_APP_CACHE_MMU_IA_CLR);
  100. /* (above steps probably unnecessary for most serial bootloader
  101. usage, all that's absolutely needed is that we unmask DROM0
  102. cache on the following two lines - normal ROM boot exits with
  103. DROM0 cache unmasked, but serial bootloader exits with it
  104. masked. However can't hurt to be thorough and reset
  105. everything.)
  106. The lines which manipulate DPORT_APP_CACHE_MMU_IA_CLR bit are
  107. necessary to work around a hardware bug.
  108. */
  109. DPORT_REG_CLR_BIT(DPORT_PRO_CACHE_CTRL1_REG, DPORT_PRO_CACHE_MASK_DROM0);
  110. DPORT_REG_CLR_BIT(DPORT_APP_CACHE_CTRL1_REG, DPORT_APP_CACHE_MASK_DROM0);
  111. bootloader_main();
  112. }
  113. /** @brief Load partition table
  114. *
  115. * Parse partition table, get useful data such as location of
  116. * OTA data partition, factory app partition, and test app partition.
  117. *
  118. * @param bs bootloader state structure used to save read data
  119. * @return return true if the partition table was succesfully loaded and MD5 checksum is valid.
  120. */
  121. bool load_partition_table(bootloader_state_t* bs)
  122. {
  123. const esp_partition_info_t *partitions;
  124. const int ESP_PARTITION_TABLE_DATA_LEN = 0xC00; /* length of actual data (signature is appended to this) */
  125. char *partition_usage;
  126. esp_err_t err;
  127. int num_partitions;
  128. #ifdef CONFIG_SECURE_BOOT_ENABLED
  129. if(esp_secure_boot_enabled()) {
  130. ESP_LOGI(TAG, "Verifying partition table signature...");
  131. err = esp_secure_boot_verify_signature(ESP_PARTITION_TABLE_ADDR, ESP_PARTITION_TABLE_DATA_LEN);
  132. if (err != ESP_OK) {
  133. ESP_LOGE(TAG, "Failed to verify partition table signature.");
  134. return false;
  135. }
  136. ESP_LOGD(TAG, "Partition table signature verified");
  137. }
  138. #endif
  139. partitions = bootloader_mmap(ESP_PARTITION_TABLE_ADDR, ESP_PARTITION_TABLE_DATA_LEN);
  140. if (!partitions) {
  141. ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", ESP_PARTITION_TABLE_ADDR, ESP_PARTITION_TABLE_DATA_LEN);
  142. return false;
  143. }
  144. ESP_LOGD(TAG, "mapped partition table 0x%x at 0x%x", ESP_PARTITION_TABLE_ADDR, (intptr_t)partitions);
  145. err = esp_partition_table_basic_verify(partitions, true, &num_partitions);
  146. if (err != ESP_OK) {
  147. ESP_LOGE(TAG, "Failed to verify partition table");
  148. return false;
  149. }
  150. ESP_LOGI(TAG, "Partition Table:");
  151. ESP_LOGI(TAG, "## Label Usage Type ST Offset Length");
  152. for(int i = 0; i < num_partitions; i++) {
  153. const esp_partition_info_t *partition = &partitions[i];
  154. ESP_LOGD(TAG, "load partition table entry 0x%x", (intptr_t)partition);
  155. ESP_LOGD(TAG, "type=%x subtype=%x", partition->type, partition->subtype);
  156. partition_usage = "unknown";
  157. /* valid partition table */
  158. switch(partition->type) {
  159. case PART_TYPE_APP: /* app partition */
  160. switch(partition->subtype) {
  161. case PART_SUBTYPE_FACTORY: /* factory binary */
  162. bs->factory = partition->pos;
  163. partition_usage = "factory app";
  164. break;
  165. case PART_SUBTYPE_TEST: /* test binary */
  166. bs->test = partition->pos;
  167. partition_usage = "test app";
  168. break;
  169. default:
  170. /* OTA binary */
  171. if ((partition->subtype & ~PART_SUBTYPE_OTA_MASK) == PART_SUBTYPE_OTA_FLAG) {
  172. bs->ota[partition->subtype & PART_SUBTYPE_OTA_MASK] = partition->pos;
  173. ++bs->app_count;
  174. partition_usage = "OTA app";
  175. }
  176. else {
  177. partition_usage = "Unknown app";
  178. }
  179. break;
  180. }
  181. break; /* PART_TYPE_APP */
  182. case PART_TYPE_DATA: /* data partition */
  183. switch(partition->subtype) {
  184. case PART_SUBTYPE_DATA_OTA: /* ota data */
  185. bs->ota_info = partition->pos;
  186. partition_usage = "OTA data";
  187. break;
  188. case PART_SUBTYPE_DATA_RF:
  189. partition_usage = "RF data";
  190. break;
  191. case PART_SUBTYPE_DATA_WIFI:
  192. partition_usage = "WiFi data";
  193. break;
  194. default:
  195. partition_usage = "Unknown data";
  196. break;
  197. }
  198. break; /* PARTITION_USAGE_DATA */
  199. default: /* other partition type */
  200. break;
  201. }
  202. /* print partition type info */
  203. ESP_LOGI(TAG, "%2d %-16s %-16s %02x %02x %08x %08x", i, partition->label, partition_usage,
  204. partition->type, partition->subtype,
  205. partition->pos.offset, partition->pos.size);
  206. }
  207. bootloader_munmap(partitions);
  208. ESP_LOGI(TAG,"End of partition table");
  209. return true;
  210. }
  211. static uint32_t ota_select_crc(const esp_ota_select_entry_t *s)
  212. {
  213. return crc32_le(UINT32_MAX, (uint8_t*)&s->ota_seq, 4);
  214. }
  215. static bool ota_select_valid(const esp_ota_select_entry_t *s)
  216. {
  217. return s->ota_seq != UINT32_MAX && s->crc == ota_select_crc(s);
  218. }
  219. /**
  220. * @function : bootloader_main
  221. * @description: entry function of 2nd bootloader
  222. *
  223. * @inputs: void
  224. */
  225. void bootloader_main()
  226. {
  227. clock_configure();
  228. uart_console_configure();
  229. wdt_reset_check();
  230. ESP_LOGI(TAG, "ESP-IDF %s 2nd stage bootloader", IDF_VER);
  231. #if defined(CONFIG_SECURE_BOOT_ENABLED) || defined(CONFIG_FLASH_ENCRYPTION_ENABLED)
  232. esp_err_t err;
  233. #endif
  234. esp_image_header_t fhdr;
  235. bootloader_state_t bs;
  236. esp_rom_spiflash_result_t spiRet1,spiRet2;
  237. esp_ota_select_entry_t sa,sb;
  238. const esp_ota_select_entry_t *ota_select_map;
  239. memset(&bs, 0, sizeof(bs));
  240. ESP_LOGI(TAG, "compile time " __TIME__ );
  241. ets_set_appcpu_boot_addr(0);
  242. /* disable watch dog here */
  243. REG_CLR_BIT( RTC_CNTL_WDTCONFIG0_REG, RTC_CNTL_WDT_FLASHBOOT_MOD_EN );
  244. REG_CLR_BIT( TIMG_WDTCONFIG0_REG(0), TIMG_WDT_FLASHBOOT_MOD_EN );
  245. #ifndef CONFIG_SPI_FLASH_ROM_DRIVER_PATCH
  246. const uint32_t spiconfig = ets_efuse_get_spiconfig();
  247. if(spiconfig != EFUSE_SPICONFIG_SPI_DEFAULTS && spiconfig != EFUSE_SPICONFIG_HSPI_DEFAULTS) {
  248. ESP_LOGE(TAG, "SPI flash pins are overridden. \"Enable SPI flash ROM driver patched functions\" must be enabled in menuconfig");
  249. return;
  250. }
  251. #endif
  252. esp_rom_spiflash_unlock();
  253. ESP_LOGI(TAG, "Enabling RNG early entropy source...");
  254. bootloader_random_enable();
  255. #if CONFIG_FLASHMODE_QIO || CONFIG_FLASHMODE_QOUT
  256. bootloader_enable_qio_mode();
  257. #endif
  258. if (bootloader_flash_read(ESP_BOOTLOADER_OFFSET, &fhdr,
  259. sizeof(esp_image_header_t), true) != ESP_OK) {
  260. ESP_LOGE(TAG, "failed to load bootloader header!");
  261. return;
  262. }
  263. print_flash_info(&fhdr);
  264. update_flash_config(&fhdr);
  265. if (!load_partition_table(&bs)) {
  266. ESP_LOGE(TAG, "load partition table error!");
  267. return;
  268. }
  269. esp_partition_pos_t load_part_pos;
  270. if (bs.ota_info.offset != 0) { // check if partition table has OTA info partition
  271. //ESP_LOGE("OTA info sector handling is not implemented");
  272. if (bs.ota_info.size < 2 * SPI_SEC_SIZE) {
  273. ESP_LOGE(TAG, "ERROR: ota_info partition size %d is too small (minimum %d bytes)", bs.ota_info.size, sizeof(esp_ota_select_entry_t));
  274. return;
  275. }
  276. ota_select_map = bootloader_mmap(bs.ota_info.offset, bs.ota_info.size);
  277. if (!ota_select_map) {
  278. ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", bs.ota_info.offset, bs.ota_info.size);
  279. return;
  280. }
  281. memcpy(&sa, ota_select_map, sizeof(esp_ota_select_entry_t));
  282. memcpy(&sb, (uint8_t *)ota_select_map + SPI_SEC_SIZE, sizeof(esp_ota_select_entry_t));
  283. bootloader_munmap(ota_select_map);
  284. ESP_LOGD(TAG, "OTA sequence values A 0x%08x B 0x%08x", sa.ota_seq, sb.ota_seq);
  285. if(sa.ota_seq == 0xFFFFFFFF && sb.ota_seq == 0xFFFFFFFF) {
  286. ESP_LOGD(TAG, "OTA sequence numbers both empty (all-0xFF");
  287. // init status flash
  288. if (bs.factory.offset != 0) { // if have factory bin,boot factory bin
  289. ESP_LOGD(TAG, "Defaulting to factory image");
  290. load_part_pos = bs.factory;
  291. } else {
  292. ESP_LOGD(TAG, "No factory image, defaulting to OTA 0");
  293. load_part_pos = bs.ota[0];
  294. sa.ota_seq = 0x01;
  295. sa.crc = ota_select_crc(&sa);
  296. sb.ota_seq = 0x00;
  297. sb.crc = ota_select_crc(&sb);
  298. Cache_Read_Disable(0);
  299. spiRet1 = esp_rom_spiflash_erase_sector(bs.ota_info.offset/0x1000);
  300. spiRet2 = esp_rom_spiflash_erase_sector(bs.ota_info.offset/0x1000+1);
  301. if (spiRet1 != ESP_ROM_SPIFLASH_RESULT_OK || spiRet2 != ESP_ROM_SPIFLASH_RESULT_OK ) {
  302. ESP_LOGE(TAG, SPI_ERROR_LOG);
  303. return;
  304. }
  305. spiRet1 = esp_rom_spiflash_write(bs.ota_info.offset,(uint32_t *)&sa,sizeof(esp_ota_select_entry_t));
  306. spiRet2 = esp_rom_spiflash_write(bs.ota_info.offset + 0x1000,(uint32_t *)&sb,sizeof(esp_ota_select_entry_t));
  307. if (spiRet1 != ESP_ROM_SPIFLASH_RESULT_OK || spiRet2 != ESP_ROM_SPIFLASH_RESULT_OK ) {
  308. ESP_LOGE(TAG, SPI_ERROR_LOG);
  309. return;
  310. }
  311. Cache_Read_Enable(0);
  312. }
  313. //TODO:write data in ota info
  314. } else {
  315. if(ota_select_valid(&sa) && ota_select_valid(&sb)) {
  316. ESP_LOGD(TAG, "Both OTA sequence valid, using OTA slot %d", MAX(sa.ota_seq, sb.ota_seq)-1);
  317. load_part_pos = bs.ota[(MAX(sa.ota_seq, sb.ota_seq) - 1)%bs.app_count];
  318. } else if(ota_select_valid(&sa)) {
  319. ESP_LOGD(TAG, "Only OTA sequence A is valid, using OTA slot %d", sa.ota_seq - 1);
  320. load_part_pos = bs.ota[(sa.ota_seq - 1) % bs.app_count];
  321. } else if(ota_select_valid(&sb)) {
  322. ESP_LOGD(TAG, "Only OTA sequence B is valid, using OTA slot %d", sa.ota_seq - 1);
  323. load_part_pos = bs.ota[(sb.ota_seq - 1) % bs.app_count];
  324. } else if (bs.factory.offset != 0) {
  325. ESP_LOGE(TAG, "ota data partition invalid, falling back to factory");
  326. load_part_pos = bs.factory;
  327. } else {
  328. ESP_LOGE(TAG, "ota data partition invalid and no factory, can't boot");
  329. return;
  330. }
  331. }
  332. } else if (bs.factory.offset != 0) { // otherwise, look for factory app partition
  333. load_part_pos = bs.factory;
  334. } else if (bs.test.offset != 0) { // otherwise, look for test app parition
  335. load_part_pos = bs.test;
  336. } else { // nothing to load, bail out
  337. ESP_LOGE(TAG, "nothing to load");
  338. return;
  339. }
  340. #ifdef CONFIG_SECURE_BOOT_ENABLED
  341. /* Generate secure digest from this bootloader to protect future
  342. modifications */
  343. ESP_LOGI(TAG, "Checking secure boot...");
  344. err = esp_secure_boot_permanently_enable();
  345. if (err != ESP_OK) {
  346. ESP_LOGE(TAG, "Bootloader digest generation failed (%d). SECURE BOOT IS NOT ENABLED.", err);
  347. /* Allow booting to continue, as the failure is probably
  348. due to user-configured EFUSEs for testing...
  349. */
  350. }
  351. #endif
  352. #ifdef CONFIG_FLASH_ENCRYPTION_ENABLED
  353. /* encrypt flash */
  354. ESP_LOGI(TAG, "Checking flash encryption...");
  355. bool flash_encryption_enabled = esp_flash_encryption_enabled();
  356. err = esp_flash_encrypt_check_and_update();
  357. if (err != ESP_OK) {
  358. ESP_LOGE(TAG, "Flash encryption check failed (%d).", err);
  359. return;
  360. }
  361. if (!flash_encryption_enabled && esp_flash_encryption_enabled()) {
  362. /* Flash encryption was just enabled for the first time,
  363. so issue a system reset to ensure flash encryption
  364. cache resets properly */
  365. ESP_LOGI(TAG, "Resetting with flash encryption enabled...");
  366. REG_WRITE(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_SW_SYS_RST);
  367. return;
  368. }
  369. #endif
  370. ESP_LOGI(TAG, "Disabling RNG early entropy source...");
  371. bootloader_random_disable();
  372. // copy loaded segments to RAM, set up caches for mapped segments, and start application
  373. ESP_LOGI(TAG, "Loading app partition at offset %08x", load_part_pos);
  374. unpack_load_app(&load_part_pos);
  375. }
  376. static void unpack_load_app(const esp_partition_pos_t* partition)
  377. {
  378. esp_err_t err;
  379. esp_image_metadata_t data;
  380. /* TODO: load the app image as part of OTA boot decision, so can fallback if loading fails */
  381. /* Loading the image here also includes secure boot verification */
  382. err = esp_image_load(ESP_IMAGE_LOAD, partition, &data);
  383. if (err != ESP_OK) {
  384. ESP_LOGE(TAG, "Failed to verify app image @ 0x%x (%d)", partition->offset, err);
  385. return;
  386. }
  387. uint32_t drom_addr = 0;
  388. uint32_t drom_load_addr = 0;
  389. uint32_t drom_size = 0;
  390. uint32_t irom_addr = 0;
  391. uint32_t irom_load_addr = 0;
  392. uint32_t irom_size = 0;
  393. // Find DROM & IROM addresses, to configure cache mappings
  394. for (int i = 0; i < data.image.segment_count; i++) {
  395. esp_image_segment_header_t *header = &data.segments[i];
  396. if (header->load_addr >= SOC_IROM_LOW && header->load_addr < SOC_IROM_HIGH) {
  397. if (drom_addr != 0) {
  398. ESP_LOGE(TAG, MAP_ERR_MSG, "DROM");
  399. } else {
  400. ESP_LOGD(TAG, "Mapping segment %d as %s", i, "DROM");
  401. }
  402. drom_addr = data.segment_data[i];
  403. drom_load_addr = header->load_addr;
  404. drom_size = header->data_len;
  405. }
  406. if (header->load_addr >= SOC_DROM_LOW && header->load_addr < SOC_DROM_HIGH) {
  407. if (irom_addr != 0) {
  408. ESP_LOGE(TAG, MAP_ERR_MSG, "IROM");
  409. } else {
  410. ESP_LOGD(TAG, "Mapping segment %d as %s", i, "IROM");
  411. }
  412. irom_addr = data.segment_data[i];
  413. irom_load_addr = header->load_addr;
  414. irom_size = header->data_len;
  415. }
  416. }
  417. ESP_LOGD(TAG, "calling set_cache_and_start_app");
  418. set_cache_and_start_app(drom_addr,
  419. drom_load_addr,
  420. drom_size,
  421. irom_addr,
  422. irom_load_addr,
  423. irom_size,
  424. data.image.entry_addr);
  425. }
  426. static void set_cache_and_start_app(
  427. uint32_t drom_addr,
  428. uint32_t drom_load_addr,
  429. uint32_t drom_size,
  430. uint32_t irom_addr,
  431. uint32_t irom_load_addr,
  432. uint32_t irom_size,
  433. uint32_t entry_addr)
  434. {
  435. ESP_LOGD(TAG, "configure drom and irom and start");
  436. Cache_Read_Disable( 0 );
  437. Cache_Flush( 0 );
  438. /* Clear the MMU entries that are already set up,
  439. so the new app only has the mappings it creates.
  440. */
  441. for (int i = 0; i < DPORT_FLASH_MMU_TABLE_SIZE; i++) {
  442. DPORT_PRO_FLASH_MMU_TABLE[i] = DPORT_FLASH_MMU_TABLE_INVALID_VAL;
  443. }
  444. uint32_t drom_page_count = (drom_size + 64*1024 - 1) / (64*1024); // round up to 64k
  445. 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 );
  446. int rc = cache_flash_mmu_set( 0, 0, drom_load_addr & 0xffff0000, drom_addr & 0xffff0000, 64, drom_page_count );
  447. ESP_LOGV(TAG, "rc=%d", rc );
  448. rc = cache_flash_mmu_set( 1, 0, drom_load_addr & 0xffff0000, drom_addr & 0xffff0000, 64, drom_page_count );
  449. ESP_LOGV(TAG, "rc=%d", rc );
  450. uint32_t irom_page_count = (irom_size + 64*1024 - 1) / (64*1024); // round up to 64k
  451. 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 );
  452. rc = cache_flash_mmu_set( 0, 0, irom_load_addr & 0xffff0000, irom_addr & 0xffff0000, 64, irom_page_count );
  453. ESP_LOGV(TAG, "rc=%d", rc );
  454. rc = cache_flash_mmu_set( 1, 0, irom_load_addr & 0xffff0000, irom_addr & 0xffff0000, 64, irom_page_count );
  455. ESP_LOGV(TAG, "rc=%d", rc );
  456. 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 );
  457. 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 );
  458. Cache_Read_Enable( 0 );
  459. // Application will need to do Cache_Flush(1) and Cache_Read_Enable(1)
  460. ESP_LOGD(TAG, "start: 0x%08x", entry_addr);
  461. typedef void (*entry_t)(void);
  462. entry_t entry = ((entry_t) entry_addr);
  463. // TODO: we have used quite a bit of stack at this point.
  464. // use "movsp" instruction to reset stack back to where ROM stack starts.
  465. (*entry)();
  466. }
  467. static void update_flash_config(const esp_image_header_t* pfhdr)
  468. {
  469. uint32_t size;
  470. switch(pfhdr->spi_size) {
  471. case ESP_IMAGE_FLASH_SIZE_1MB:
  472. size = 1;
  473. break;
  474. case ESP_IMAGE_FLASH_SIZE_2MB:
  475. size = 2;
  476. break;
  477. case ESP_IMAGE_FLASH_SIZE_4MB:
  478. size = 4;
  479. break;
  480. case ESP_IMAGE_FLASH_SIZE_8MB:
  481. size = 8;
  482. break;
  483. case ESP_IMAGE_FLASH_SIZE_16MB:
  484. size = 16;
  485. break;
  486. default:
  487. size = 2;
  488. }
  489. Cache_Read_Disable( 0 );
  490. // Set flash chip size
  491. esp_rom_spiflash_config_param(g_rom_flashchip.device_id, size * 0x100000, 0x10000, 0x1000, 0x100, 0xffff);
  492. // TODO: set mode
  493. // TODO: set frequency
  494. Cache_Flush(0);
  495. Cache_Read_Enable( 0 );
  496. }
  497. static void print_flash_info(const esp_image_header_t* phdr)
  498. {
  499. #if (BOOT_LOG_LEVEL >= BOOT_LOG_LEVEL_NOTICE)
  500. ESP_LOGD(TAG, "magic %02x", phdr->magic );
  501. ESP_LOGD(TAG, "segments %02x", phdr->segment_count );
  502. ESP_LOGD(TAG, "spi_mode %02x", phdr->spi_mode );
  503. ESP_LOGD(TAG, "spi_speed %02x", phdr->spi_speed );
  504. ESP_LOGD(TAG, "spi_size %02x", phdr->spi_size );
  505. const char* str;
  506. switch ( phdr->spi_speed ) {
  507. case ESP_IMAGE_SPI_SPEED_40M:
  508. str = "40MHz";
  509. break;
  510. case ESP_IMAGE_SPI_SPEED_26M:
  511. str = "26.7MHz";
  512. break;
  513. case ESP_IMAGE_SPI_SPEED_20M:
  514. str = "20MHz";
  515. break;
  516. case ESP_IMAGE_SPI_SPEED_80M:
  517. str = "80MHz";
  518. break;
  519. default:
  520. str = "20MHz";
  521. break;
  522. }
  523. ESP_LOGI(TAG, "SPI Speed : %s", str );
  524. /* SPI mode could have been set to QIO during boot already,
  525. so test the SPI registers not the flash header */
  526. uint32_t spi_ctrl = REG_READ(SPI_CTRL_REG(0));
  527. if (spi_ctrl & SPI_FREAD_QIO) {
  528. str = "QIO";
  529. } else if (spi_ctrl & SPI_FREAD_QUAD) {
  530. str = "QOUT";
  531. } else if (spi_ctrl & SPI_FREAD_DIO) {
  532. str = "DIO";
  533. } else if (spi_ctrl & SPI_FREAD_DUAL) {
  534. str = "DOUT";
  535. } else if (spi_ctrl & SPI_FASTRD_MODE) {
  536. str = "FAST READ";
  537. } else {
  538. str = "SLOW READ";
  539. }
  540. ESP_LOGI(TAG, "SPI Mode : %s", str );
  541. switch ( phdr->spi_size ) {
  542. case ESP_IMAGE_FLASH_SIZE_1MB:
  543. str = "1MB";
  544. break;
  545. case ESP_IMAGE_FLASH_SIZE_2MB:
  546. str = "2MB";
  547. break;
  548. case ESP_IMAGE_FLASH_SIZE_4MB:
  549. str = "4MB";
  550. break;
  551. case ESP_IMAGE_FLASH_SIZE_8MB:
  552. str = "8MB";
  553. break;
  554. case ESP_IMAGE_FLASH_SIZE_16MB:
  555. str = "16MB";
  556. break;
  557. default:
  558. str = "2MB";
  559. break;
  560. }
  561. ESP_LOGI(TAG, "SPI Flash Size : %s", str );
  562. #endif
  563. }
  564. static void clock_configure(void)
  565. {
  566. /* Set CPU to 80MHz. Keep other clocks unmodified. */
  567. rtc_cpu_freq_t cpu_freq = RTC_CPU_FREQ_80M;
  568. /* On ESP32 rev 0, switching to 80MHz if clock was previously set to
  569. * 240 MHz may cause the chip to lock up (see section 3.5 of the errata
  570. * document). For rev. 0, switch to 240 instead if it was chosen in
  571. * menuconfig.
  572. */
  573. uint32_t chip_ver_reg = REG_READ(EFUSE_BLK0_RDATA3_REG);
  574. if ((chip_ver_reg & EFUSE_RD_CHIP_VER_REV1_M) == 0 &&
  575. CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ == 240) {
  576. cpu_freq = RTC_CPU_FREQ_240M;
  577. }
  578. uart_tx_wait_idle(0);
  579. rtc_clk_config_t clk_cfg = RTC_CLK_CONFIG_DEFAULT();
  580. clk_cfg.xtal_freq = CONFIG_ESP32_XTAL_FREQ;
  581. clk_cfg.cpu_freq = cpu_freq;
  582. clk_cfg.slow_freq = rtc_clk_slow_freq_get();
  583. clk_cfg.fast_freq = rtc_clk_fast_freq_get();
  584. rtc_clk_init(clk_cfg);
  585. /* As a slight optimization, if 32k XTAL was enabled in sdkconfig, we enable
  586. * it here. Usually it needs some time to start up, so we amortize at least
  587. * part of the start up time by enabling 32k XTAL early.
  588. * App startup code will wait until the oscillator has started up.
  589. */
  590. #ifdef CONFIG_ESP32_RTC_CLOCK_SOURCE_EXTERNAL_CRYSTAL
  591. if (!rtc_clk_32k_enabled()) {
  592. rtc_clk_32k_bootstrap();
  593. }
  594. #endif
  595. }
  596. static void uart_console_configure(void)
  597. {
  598. #if CONFIG_CONSOLE_UART_NONE
  599. ets_install_putc1(NULL);
  600. ets_install_putc2(NULL);
  601. #else // CONFIG_CONSOLE_UART_NONE
  602. const int uart_num = CONFIG_CONSOLE_UART_NUM;
  603. uartAttach();
  604. ets_install_uart_printf();
  605. // ROM bootloader may have put a lot of text into UART0 FIFO.
  606. // Wait for it to be printed.
  607. uart_tx_wait_idle(0);
  608. #if CONFIG_CONSOLE_UART_CUSTOM
  609. // Some constants to make the following code less upper-case
  610. const int uart_tx_gpio = CONFIG_CONSOLE_UART_TX_GPIO;
  611. const int uart_rx_gpio = CONFIG_CONSOLE_UART_RX_GPIO;
  612. // Switch to the new UART (this just changes UART number used for
  613. // ets_printf in ROM code).
  614. uart_tx_switch(uart_num);
  615. // If console is attached to UART1 or if non-default pins are used,
  616. // need to reconfigure pins using GPIO matrix
  617. if (uart_num != 0 || uart_tx_gpio != 1 || uart_rx_gpio != 3) {
  618. // Change pin mode for GPIO1/3 from UART to GPIO
  619. PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_U0RXD_GPIO3);
  620. PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD_GPIO1);
  621. // Route GPIO signals to/from pins
  622. // (arrays should be optimized away by the compiler)
  623. const uint32_t tx_idx_list[3] = { U0TXD_OUT_IDX, U1TXD_OUT_IDX, U2TXD_OUT_IDX };
  624. const uint32_t rx_idx_list[3] = { U0RXD_IN_IDX, U1RXD_IN_IDX, U2RXD_IN_IDX };
  625. const uint32_t tx_idx = tx_idx_list[uart_num];
  626. const uint32_t rx_idx = rx_idx_list[uart_num];
  627. gpio_matrix_out(uart_tx_gpio, tx_idx, 0, 0);
  628. gpio_matrix_in(uart_rx_gpio, rx_idx, 0);
  629. }
  630. #endif // CONFIG_CONSOLE_UART_CUSTOM
  631. // Set configured UART console baud rate
  632. const int uart_baud = CONFIG_CONSOLE_UART_BAUDRATE;
  633. uart_div_modify(uart_num, (rtc_clk_apb_freq_get() << 4) / uart_baud);
  634. #endif // CONFIG_CONSOLE_UART_NONE
  635. }
  636. static void wdt_reset_cpu0_info_enable(void)
  637. {
  638. //We do not reset core1 info here because it didn't work before cpu1 was up. So we put it into call_start_cpu1.
  639. DPORT_REG_SET_BIT(DPORT_PRO_CPU_RECORD_CTRL_REG, DPORT_PRO_CPU_PDEBUG_ENABLE | DPORT_PRO_CPU_RECORD_ENABLE);
  640. DPORT_REG_CLR_BIT(DPORT_PRO_CPU_RECORD_CTRL_REG, DPORT_PRO_CPU_RECORD_ENABLE);
  641. }
  642. static void wdt_reset_info_dump(int cpu)
  643. {
  644. uint32_t inst = 0, pid = 0, stat = 0, data = 0, pc = 0,
  645. lsstat = 0, lsaddr = 0, lsdata = 0, dstat = 0;
  646. char *cpu_name = cpu ? "APP" : "PRO";
  647. if (cpu == 0) {
  648. stat = DPORT_REG_READ(DPORT_PRO_CPU_RECORD_STATUS_REG);
  649. pid = DPORT_REG_READ(DPORT_PRO_CPU_RECORD_PID_REG);
  650. inst = DPORT_REG_READ(DPORT_PRO_CPU_RECORD_PDEBUGINST_REG);
  651. dstat = DPORT_REG_READ(DPORT_PRO_CPU_RECORD_PDEBUGSTATUS_REG);
  652. data = DPORT_REG_READ(DPORT_PRO_CPU_RECORD_PDEBUGDATA_REG);
  653. pc = DPORT_REG_READ(DPORT_PRO_CPU_RECORD_PDEBUGPC_REG);
  654. lsstat = DPORT_REG_READ(DPORT_PRO_CPU_RECORD_PDEBUGLS0STAT_REG);
  655. lsaddr = DPORT_REG_READ(DPORT_PRO_CPU_RECORD_PDEBUGLS0ADDR_REG);
  656. lsdata = DPORT_REG_READ(DPORT_PRO_CPU_RECORD_PDEBUGLS0DATA_REG);
  657. } else {
  658. stat = DPORT_REG_READ(DPORT_APP_CPU_RECORD_STATUS_REG);
  659. pid = DPORT_REG_READ(DPORT_APP_CPU_RECORD_PID_REG);
  660. inst = DPORT_REG_READ(DPORT_APP_CPU_RECORD_PDEBUGINST_REG);
  661. dstat = DPORT_REG_READ(DPORT_APP_CPU_RECORD_PDEBUGSTATUS_REG);
  662. data = DPORT_REG_READ(DPORT_APP_CPU_RECORD_PDEBUGDATA_REG);
  663. pc = DPORT_REG_READ(DPORT_APP_CPU_RECORD_PDEBUGPC_REG);
  664. lsstat = DPORT_REG_READ(DPORT_APP_CPU_RECORD_PDEBUGLS0STAT_REG);
  665. lsaddr = DPORT_REG_READ(DPORT_APP_CPU_RECORD_PDEBUGLS0ADDR_REG);
  666. lsdata = DPORT_REG_READ(DPORT_APP_CPU_RECORD_PDEBUGLS0DATA_REG);
  667. }
  668. if (DPORT_RECORD_PDEBUGINST_SZ(inst) == 0 &&
  669. DPORT_RECORD_PDEBUGSTATUS_BBCAUSE(dstat) == DPORT_RECORD_PDEBUGSTATUS_BBCAUSE_WAITI) {
  670. ESP_LOGW(TAG, "WDT reset info: %s CPU PC=0x%x (waiti mode)", cpu_name, pc);
  671. } else {
  672. ESP_LOGW(TAG, "WDT reset info: %s CPU PC=0x%x", cpu_name, pc);
  673. }
  674. ESP_LOGD(TAG, "WDT reset info: %s CPU STATUS 0x%08x", cpu_name, stat);
  675. ESP_LOGD(TAG, "WDT reset info: %s CPU PID 0x%08x", cpu_name, pid);
  676. ESP_LOGD(TAG, "WDT reset info: %s CPU PDEBUGINST 0x%08x", cpu_name, inst);
  677. ESP_LOGD(TAG, "WDT reset info: %s CPU PDEBUGSTATUS 0x%08x", cpu_name, dstat);
  678. ESP_LOGD(TAG, "WDT reset info: %s CPU PDEBUGDATA 0x%08x", cpu_name, data);
  679. ESP_LOGD(TAG, "WDT reset info: %s CPU PDEBUGPC 0x%08x", cpu_name, pc);
  680. ESP_LOGD(TAG, "WDT reset info: %s CPU PDEBUGLS0STAT 0x%08x", cpu_name, lsstat);
  681. ESP_LOGD(TAG, "WDT reset info: %s CPU PDEBUGLS0ADDR 0x%08x", cpu_name, lsaddr);
  682. ESP_LOGD(TAG, "WDT reset info: %s CPU PDEBUGLS0DATA 0x%08x", cpu_name, lsdata);
  683. }
  684. static void wdt_reset_check(void)
  685. {
  686. int wdt_rst = 0;
  687. RESET_REASON rst_reas[2];
  688. rst_reas[0] = rtc_get_reset_reason(0);
  689. rst_reas[1] = rtc_get_reset_reason(1);
  690. if (rst_reas[0] == RTCWDT_SYS_RESET || rst_reas[0] == TG0WDT_SYS_RESET || rst_reas[0] == TG1WDT_SYS_RESET ||
  691. rst_reas[0] == TGWDT_CPU_RESET || rst_reas[0] == RTCWDT_CPU_RESET) {
  692. ESP_LOGW(TAG, "PRO CPU has been reset by WDT.");
  693. wdt_rst = 1;
  694. }
  695. if (rst_reas[1] == RTCWDT_SYS_RESET || rst_reas[1] == TG0WDT_SYS_RESET || rst_reas[1] == TG1WDT_SYS_RESET ||
  696. rst_reas[1] == TGWDT_CPU_RESET || rst_reas[1] == RTCWDT_CPU_RESET) {
  697. ESP_LOGW(TAG, "APP CPU has been reset by WDT.");
  698. wdt_rst = 1;
  699. }
  700. if (wdt_rst) {
  701. // if reset by WDT dump info from trace port
  702. wdt_reset_info_dump(0);
  703. wdt_reset_info_dump(1);
  704. }
  705. wdt_reset_cpu0_info_enable();
  706. }
  707. void __assert_func(const char *file, int line, const char *func, const char *expr)
  708. {
  709. ESP_LOGE(TAG, "Assert failed in %s, %s:%d (%s)", func, file, line, expr);
  710. while(1) {}
  711. }