bootloader_init.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. // Copyright 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 <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 "esp32/rom/cache.h"
  21. #include "esp32/rom/efuse.h"
  22. #include "esp32/rom/ets_sys.h"
  23. #include "esp32/rom/spi_flash.h"
  24. #include "esp32/rom/crc.h"
  25. #include "esp32/rom/rtc.h"
  26. #include "esp32/rom/uart.h"
  27. #include "esp32/rom/gpio.h"
  28. #include "esp32/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/gpio_periph.h"
  34. #include "soc/efuse_periph.h"
  35. #include "soc/rtc_periph.h"
  36. #include "soc/timer_periph.h"
  37. #include "soc/rtc_wdt.h"
  38. #include "soc/spi_periph.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 "bootloader_common.h"
  49. #include "bootloader_flash_config.h"
  50. #include "flash_qio_mode.h"
  51. extern int _bss_start;
  52. extern int _bss_end;
  53. extern int _data_start;
  54. extern int _data_end;
  55. static const char* TAG = "boot";
  56. static esp_err_t bootloader_main();
  57. static void print_flash_info(const esp_image_header_t* pfhdr);
  58. static void update_flash_config(const esp_image_header_t* pfhdr);
  59. static void bootloader_init_flash_configure(const esp_image_header_t* pfhdr);
  60. static void uart_console_configure(void);
  61. static void wdt_reset_check(void);
  62. esp_err_t bootloader_init()
  63. {
  64. cpu_configure_region_protection();
  65. cpu_init_memctl();
  66. /* Sanity check that static RAM is after the stack */
  67. #ifndef NDEBUG
  68. {
  69. int *sp = get_sp();
  70. assert(&_bss_start <= &_bss_end);
  71. assert(&_data_start <= &_data_end);
  72. assert(sp < &_bss_start);
  73. assert(sp < &_data_start);
  74. }
  75. #endif
  76. //Clear bss
  77. memset(&_bss_start, 0, (&_bss_end - &_bss_start) * sizeof(_bss_start));
  78. /* completely reset MMU for both CPUs
  79. (in case serial bootloader was running) */
  80. Cache_Read_Disable(0);
  81. Cache_Read_Disable(1);
  82. Cache_Flush(0);
  83. Cache_Flush(1);
  84. mmu_init(0);
  85. DPORT_REG_SET_BIT(DPORT_APP_CACHE_CTRL1_REG, DPORT_APP_CACHE_MMU_IA_CLR);
  86. mmu_init(1);
  87. DPORT_REG_CLR_BIT(DPORT_APP_CACHE_CTRL1_REG, DPORT_APP_CACHE_MMU_IA_CLR);
  88. /* (above steps probably unnecessary for most serial bootloader
  89. usage, all that's absolutely needed is that we unmask DROM0
  90. cache on the following two lines - normal ROM boot exits with
  91. DROM0 cache unmasked, but serial bootloader exits with it
  92. masked. However can't hurt to be thorough and reset
  93. everything.)
  94. The lines which manipulate DPORT_APP_CACHE_MMU_IA_CLR bit are
  95. necessary to work around a hardware bug.
  96. */
  97. DPORT_REG_CLR_BIT(DPORT_PRO_CACHE_CTRL1_REG, DPORT_PRO_CACHE_MASK_DROM0);
  98. DPORT_REG_CLR_BIT(DPORT_APP_CACHE_CTRL1_REG, DPORT_APP_CACHE_MASK_DROM0);
  99. if(bootloader_main() != ESP_OK){
  100. return ESP_FAIL;
  101. }
  102. return ESP_OK;
  103. }
  104. static esp_err_t bootloader_main()
  105. {
  106. bootloader_common_vddsdio_configure();
  107. /* Read and keep flash ID, for further use. */
  108. g_rom_flashchip.device_id = bootloader_read_flash_id();
  109. esp_image_header_t fhdr;
  110. if (bootloader_flash_read(ESP_BOOTLOADER_OFFSET, &fhdr, sizeof(esp_image_header_t), true) != ESP_OK) {
  111. ESP_LOGE(TAG, "failed to load bootloader header!");
  112. return ESP_FAIL;
  113. }
  114. bootloader_init_flash_configure(&fhdr);
  115. #if (CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ == 240)
  116. //Check if ESP32 is rated for a CPU frequency of 160MHz only
  117. if (REG_GET_BIT(EFUSE_BLK0_RDATA3_REG, EFUSE_RD_CHIP_CPU_FREQ_RATED) &&
  118. REG_GET_BIT(EFUSE_BLK0_RDATA3_REG, EFUSE_RD_CHIP_CPU_FREQ_LOW)) {
  119. ESP_LOGE(TAG, "Chip CPU frequency rated for 160MHz. Modify CPU frequency in menuconfig");
  120. return ESP_FAIL;
  121. }
  122. #endif
  123. bootloader_clock_configure();
  124. uart_console_configure();
  125. wdt_reset_check();
  126. ESP_LOGI(TAG, "ESP-IDF %s 2nd stage bootloader", IDF_VER);
  127. ESP_LOGI(TAG, "compile time " __TIME__ );
  128. ets_set_appcpu_boot_addr(0);
  129. #ifdef CONFIG_BOOTLOADER_WDT_ENABLE
  130. ESP_LOGD(TAG, "Enabling RTCWDT(%d ms)", CONFIG_BOOTLOADER_WDT_TIME_MS);
  131. rtc_wdt_protect_off();
  132. rtc_wdt_disable();
  133. rtc_wdt_set_length_of_reset_signal(RTC_WDT_SYS_RESET_SIG, RTC_WDT_LENGTH_3_2us);
  134. rtc_wdt_set_length_of_reset_signal(RTC_WDT_CPU_RESET_SIG, RTC_WDT_LENGTH_3_2us);
  135. rtc_wdt_set_stage(RTC_WDT_STAGE0, RTC_WDT_STAGE_ACTION_RESET_RTC);
  136. rtc_wdt_set_time(RTC_WDT_STAGE0, CONFIG_BOOTLOADER_WDT_TIME_MS);
  137. rtc_wdt_enable();
  138. rtc_wdt_protect_on();
  139. #else
  140. /* disable watch dog here */
  141. rtc_wdt_disable();
  142. #endif
  143. REG_SET_FIELD(TIMG_WDTWPROTECT_REG(0), TIMG_WDT_WKEY, TIMG_WDT_WKEY_VALUE);
  144. REG_CLR_BIT( TIMG_WDTCONFIG0_REG(0), TIMG_WDT_FLASHBOOT_MOD_EN );
  145. #ifndef CONFIG_SPI_FLASH_ROM_DRIVER_PATCH
  146. const uint32_t spiconfig = ets_efuse_get_spiconfig();
  147. if(spiconfig != EFUSE_SPICONFIG_SPI_DEFAULTS && spiconfig != EFUSE_SPICONFIG_HSPI_DEFAULTS) {
  148. ESP_LOGE(TAG, "SPI flash pins are overridden. \"Enable SPI flash ROM driver patched functions\" must be enabled in menuconfig");
  149. return ESP_FAIL;
  150. }
  151. #endif
  152. esp_rom_spiflash_unlock();
  153. ESP_LOGI(TAG, "Enabling RNG early entropy source...");
  154. bootloader_random_enable();
  155. #if CONFIG_ESPTOOLPY_FLASHMODE_QIO || CONFIG_ESPTOOLPY_FLASHMODE_QOUT
  156. bootloader_enable_qio_mode();
  157. #endif
  158. print_flash_info(&fhdr);
  159. update_flash_config(&fhdr);
  160. return ESP_OK;
  161. }
  162. static void update_flash_config(const esp_image_header_t* pfhdr)
  163. {
  164. uint32_t size;
  165. switch(pfhdr->spi_size) {
  166. case ESP_IMAGE_FLASH_SIZE_1MB:
  167. size = 1;
  168. break;
  169. case ESP_IMAGE_FLASH_SIZE_2MB:
  170. size = 2;
  171. break;
  172. case ESP_IMAGE_FLASH_SIZE_4MB:
  173. size = 4;
  174. break;
  175. case ESP_IMAGE_FLASH_SIZE_8MB:
  176. size = 8;
  177. break;
  178. case ESP_IMAGE_FLASH_SIZE_16MB:
  179. size = 16;
  180. break;
  181. default:
  182. size = 2;
  183. }
  184. Cache_Read_Disable( 0 );
  185. // Set flash chip size
  186. esp_rom_spiflash_config_param(g_rom_flashchip.device_id, size * 0x100000, 0x10000, 0x1000, 0x100, 0xffff);
  187. // TODO: set mode
  188. // TODO: set frequency
  189. Cache_Flush(0);
  190. Cache_Read_Enable( 0 );
  191. }
  192. static void print_flash_info(const esp_image_header_t* phdr)
  193. {
  194. #if (BOOT_LOG_LEVEL >= BOOT_LOG_LEVEL_NOTICE)
  195. ESP_LOGD(TAG, "magic %02x", phdr->magic );
  196. ESP_LOGD(TAG, "segments %02x", phdr->segment_count );
  197. ESP_LOGD(TAG, "spi_mode %02x", phdr->spi_mode );
  198. ESP_LOGD(TAG, "spi_speed %02x", phdr->spi_speed );
  199. ESP_LOGD(TAG, "spi_size %02x", phdr->spi_size );
  200. const char* str;
  201. switch ( phdr->spi_speed ) {
  202. case ESP_IMAGE_SPI_SPEED_40M:
  203. str = "40MHz";
  204. break;
  205. case ESP_IMAGE_SPI_SPEED_26M:
  206. str = "26.7MHz";
  207. break;
  208. case ESP_IMAGE_SPI_SPEED_20M:
  209. str = "20MHz";
  210. break;
  211. case ESP_IMAGE_SPI_SPEED_80M:
  212. str = "80MHz";
  213. break;
  214. default:
  215. str = "20MHz";
  216. break;
  217. }
  218. ESP_LOGI(TAG, "SPI Speed : %s", str );
  219. /* SPI mode could have been set to QIO during boot already,
  220. so test the SPI registers not the flash header */
  221. uint32_t spi_ctrl = REG_READ(SPI_CTRL_REG(0));
  222. if (spi_ctrl & SPI_FREAD_QIO) {
  223. str = "QIO";
  224. } else if (spi_ctrl & SPI_FREAD_QUAD) {
  225. str = "QOUT";
  226. } else if (spi_ctrl & SPI_FREAD_DIO) {
  227. str = "DIO";
  228. } else if (spi_ctrl & SPI_FREAD_DUAL) {
  229. str = "DOUT";
  230. } else if (spi_ctrl & SPI_FASTRD_MODE) {
  231. str = "FAST READ";
  232. } else {
  233. str = "SLOW READ";
  234. }
  235. ESP_LOGI(TAG, "SPI Mode : %s", str );
  236. switch ( phdr->spi_size ) {
  237. case ESP_IMAGE_FLASH_SIZE_1MB:
  238. str = "1MB";
  239. break;
  240. case ESP_IMAGE_FLASH_SIZE_2MB:
  241. str = "2MB";
  242. break;
  243. case ESP_IMAGE_FLASH_SIZE_4MB:
  244. str = "4MB";
  245. break;
  246. case ESP_IMAGE_FLASH_SIZE_8MB:
  247. str = "8MB";
  248. break;
  249. case ESP_IMAGE_FLASH_SIZE_16MB:
  250. str = "16MB";
  251. break;
  252. default:
  253. str = "2MB";
  254. break;
  255. }
  256. ESP_LOGI(TAG, "SPI Flash Size : %s", str );
  257. #endif
  258. }
  259. /*
  260. * Bootloader reads SPI configuration from bin header, so that
  261. * the burning configuration can be different with compiling configuration.
  262. */
  263. static void IRAM_ATTR bootloader_init_flash_configure(const esp_image_header_t* pfhdr)
  264. {
  265. bootloader_flash_gpio_config(pfhdr);
  266. bootloader_flash_dummy_config(pfhdr);
  267. bootloader_flash_cs_timing_config();
  268. }
  269. static void uart_console_configure(void)
  270. {
  271. #if CONFIG_ESP_CONSOLE_UART_NONE
  272. ets_install_putc1(NULL);
  273. ets_install_putc2(NULL);
  274. #else // CONFIG_ESP_CONSOLE_UART_NONE
  275. const int uart_num = CONFIG_ESP_CONSOLE_UART_NUM;
  276. uartAttach();
  277. ets_install_uart_printf();
  278. // Wait for UART FIFO to be empty.
  279. uart_tx_wait_idle(0);
  280. #if CONFIG_ESP_CONSOLE_UART_CUSTOM
  281. // Some constants to make the following code less upper-case
  282. const int uart_tx_gpio = CONFIG_ESP_CONSOLE_UART_TX_GPIO;
  283. const int uart_rx_gpio = CONFIG_ESP_CONSOLE_UART_RX_GPIO;
  284. // Switch to the new UART (this just changes UART number used for
  285. // ets_printf in ROM code).
  286. uart_tx_switch(uart_num);
  287. // If console is attached to UART1 or if non-default pins are used,
  288. // need to reconfigure pins using GPIO matrix
  289. if (uart_num != 0 || uart_tx_gpio != 1 || uart_rx_gpio != 3) {
  290. // Change pin mode for GPIO1/3 from UART to GPIO
  291. PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_U0RXD_GPIO3);
  292. PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD_GPIO1);
  293. // Route GPIO signals to/from pins
  294. // (arrays should be optimized away by the compiler)
  295. const uint32_t tx_idx_list[3] = { U0TXD_OUT_IDX, U1TXD_OUT_IDX, U2TXD_OUT_IDX };
  296. const uint32_t rx_idx_list[3] = { U0RXD_IN_IDX, U1RXD_IN_IDX, U2RXD_IN_IDX };
  297. const uint32_t uart_reset[3] = { DPORT_UART_RST, DPORT_UART1_RST, DPORT_UART2_RST };
  298. const uint32_t tx_idx = tx_idx_list[uart_num];
  299. const uint32_t rx_idx = rx_idx_list[uart_num];
  300. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[uart_rx_gpio]);
  301. gpio_pad_pullup(uart_rx_gpio);
  302. gpio_matrix_out(uart_tx_gpio, tx_idx, 0, 0);
  303. gpio_matrix_in(uart_rx_gpio, rx_idx, 0);
  304. DPORT_SET_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, uart_reset[uart_num]);
  305. DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, uart_reset[uart_num]);
  306. }
  307. #endif // CONFIG_ESP_CONSOLE_UART_CUSTOM
  308. // Set configured UART console baud rate
  309. const int uart_baud = CONFIG_ESP_CONSOLE_UART_BAUDRATE;
  310. uart_div_modify(uart_num, (rtc_clk_apb_freq_get() << 4) / uart_baud);
  311. #endif // CONFIG_ESP_CONSOLE_UART_NONE
  312. }
  313. static void wdt_reset_cpu0_info_enable(void)
  314. {
  315. //We do not reset core1 info here because it didn't work before cpu1 was up. So we put it into call_start_cpu1.
  316. DPORT_REG_SET_BIT(DPORT_PRO_CPU_RECORD_CTRL_REG, DPORT_PRO_CPU_PDEBUG_ENABLE | DPORT_PRO_CPU_RECORD_ENABLE);
  317. DPORT_REG_CLR_BIT(DPORT_PRO_CPU_RECORD_CTRL_REG, DPORT_PRO_CPU_RECORD_ENABLE);
  318. }
  319. static void wdt_reset_info_dump(int cpu)
  320. {
  321. uint32_t inst = 0, pid = 0, stat = 0, data = 0, pc = 0,
  322. lsstat = 0, lsaddr = 0, lsdata = 0, dstat = 0;
  323. const char *cpu_name = cpu ? "APP" : "PRO";
  324. if (cpu == 0) {
  325. stat = DPORT_REG_READ(DPORT_PRO_CPU_RECORD_STATUS_REG);
  326. pid = DPORT_REG_READ(DPORT_PRO_CPU_RECORD_PID_REG);
  327. inst = DPORT_REG_READ(DPORT_PRO_CPU_RECORD_PDEBUGINST_REG);
  328. dstat = DPORT_REG_READ(DPORT_PRO_CPU_RECORD_PDEBUGSTATUS_REG);
  329. data = DPORT_REG_READ(DPORT_PRO_CPU_RECORD_PDEBUGDATA_REG);
  330. pc = DPORT_REG_READ(DPORT_PRO_CPU_RECORD_PDEBUGPC_REG);
  331. lsstat = DPORT_REG_READ(DPORT_PRO_CPU_RECORD_PDEBUGLS0STAT_REG);
  332. lsaddr = DPORT_REG_READ(DPORT_PRO_CPU_RECORD_PDEBUGLS0ADDR_REG);
  333. lsdata = DPORT_REG_READ(DPORT_PRO_CPU_RECORD_PDEBUGLS0DATA_REG);
  334. } else {
  335. stat = DPORT_REG_READ(DPORT_APP_CPU_RECORD_STATUS_REG);
  336. pid = DPORT_REG_READ(DPORT_APP_CPU_RECORD_PID_REG);
  337. inst = DPORT_REG_READ(DPORT_APP_CPU_RECORD_PDEBUGINST_REG);
  338. dstat = DPORT_REG_READ(DPORT_APP_CPU_RECORD_PDEBUGSTATUS_REG);
  339. data = DPORT_REG_READ(DPORT_APP_CPU_RECORD_PDEBUGDATA_REG);
  340. pc = DPORT_REG_READ(DPORT_APP_CPU_RECORD_PDEBUGPC_REG);
  341. lsstat = DPORT_REG_READ(DPORT_APP_CPU_RECORD_PDEBUGLS0STAT_REG);
  342. lsaddr = DPORT_REG_READ(DPORT_APP_CPU_RECORD_PDEBUGLS0ADDR_REG);
  343. lsdata = DPORT_REG_READ(DPORT_APP_CPU_RECORD_PDEBUGLS0DATA_REG);
  344. }
  345. if (DPORT_RECORD_PDEBUGINST_SZ(inst) == 0 &&
  346. DPORT_RECORD_PDEBUGSTATUS_BBCAUSE(dstat) == DPORT_RECORD_PDEBUGSTATUS_BBCAUSE_WAITI) {
  347. ESP_LOGW(TAG, "WDT reset info: %s CPU PC=0x%x (waiti mode)", cpu_name, pc);
  348. } else {
  349. ESP_LOGW(TAG, "WDT reset info: %s CPU PC=0x%x", cpu_name, pc);
  350. }
  351. ESP_LOGD(TAG, "WDT reset info: %s CPU STATUS 0x%08x", cpu_name, stat);
  352. ESP_LOGD(TAG, "WDT reset info: %s CPU PID 0x%08x", cpu_name, pid);
  353. ESP_LOGD(TAG, "WDT reset info: %s CPU PDEBUGINST 0x%08x", cpu_name, inst);
  354. ESP_LOGD(TAG, "WDT reset info: %s CPU PDEBUGSTATUS 0x%08x", cpu_name, dstat);
  355. ESP_LOGD(TAG, "WDT reset info: %s CPU PDEBUGDATA 0x%08x", cpu_name, data);
  356. ESP_LOGD(TAG, "WDT reset info: %s CPU PDEBUGPC 0x%08x", cpu_name, pc);
  357. ESP_LOGD(TAG, "WDT reset info: %s CPU PDEBUGLS0STAT 0x%08x", cpu_name, lsstat);
  358. ESP_LOGD(TAG, "WDT reset info: %s CPU PDEBUGLS0ADDR 0x%08x", cpu_name, lsaddr);
  359. ESP_LOGD(TAG, "WDT reset info: %s CPU PDEBUGLS0DATA 0x%08x", cpu_name, lsdata);
  360. }
  361. static void wdt_reset_check(void)
  362. {
  363. int wdt_rst = 0;
  364. RESET_REASON rst_reas[2];
  365. rst_reas[0] = rtc_get_reset_reason(0);
  366. rst_reas[1] = rtc_get_reset_reason(1);
  367. if (rst_reas[0] == RTCWDT_SYS_RESET || rst_reas[0] == TG0WDT_SYS_RESET || rst_reas[0] == TG1WDT_SYS_RESET ||
  368. rst_reas[0] == TGWDT_CPU_RESET || rst_reas[0] == RTCWDT_CPU_RESET) {
  369. ESP_LOGW(TAG, "PRO CPU has been reset by WDT.");
  370. wdt_rst = 1;
  371. }
  372. if (rst_reas[1] == RTCWDT_SYS_RESET || rst_reas[1] == TG0WDT_SYS_RESET || rst_reas[1] == TG1WDT_SYS_RESET ||
  373. rst_reas[1] == TGWDT_CPU_RESET || rst_reas[1] == RTCWDT_CPU_RESET) {
  374. ESP_LOGW(TAG, "APP CPU has been reset by WDT.");
  375. wdt_rst = 1;
  376. }
  377. if (wdt_rst) {
  378. // if reset by WDT dump info from trace port
  379. wdt_reset_info_dump(0);
  380. wdt_reset_info_dump(1);
  381. }
  382. wdt_reset_cpu0_info_enable();
  383. }
  384. void __assert_func(const char *file, int line, const char *func, const char *expr)
  385. {
  386. ESP_LOGE(TAG, "Assert failed in %s, %s:%d (%s)", func, file, line, expr);
  387. while(1) {}
  388. }
  389. void abort()
  390. {
  391. #if !CONFIG_ESP32_PANIC_SILENT_REBOOT
  392. ets_printf("abort() was called at PC 0x%08x\r\n", (intptr_t)__builtin_return_address(0) - 3);
  393. #endif
  394. if (esp_cpu_in_ocd_debug_mode()) {
  395. __asm__ ("break 0,0");
  396. }
  397. while(1) {}
  398. }