bootloader_flash.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stddef.h>
  7. #include <bootloader_flash_priv.h>
  8. #include <esp_log.h>
  9. #include <esp_flash_encrypt.h>
  10. #include "sdkconfig.h"
  11. #include "soc/soc_caps.h"
  12. #if CONFIG_IDF_TARGET_ESP32
  13. # include "soc/spi_struct.h"
  14. # include "soc/spi_reg.h"
  15. /* SPI flash controller */
  16. # define SPIFLASH SPI1
  17. #else
  18. # include "soc/spi_mem_struct.h"
  19. # include "soc/spi_mem_reg.h"
  20. /* SPI flash controller */
  21. # define SPIFLASH SPIMEM1
  22. #endif
  23. #include "esp_rom_spiflash.h"
  24. #ifdef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
  25. #define ENCRYPTION_IS_VIRTUAL 1
  26. #else
  27. #define ENCRYPTION_IS_VIRTUAL 0
  28. #endif
  29. #define BYTESHIFT(VAR, IDX) (((VAR) >> ((IDX) * 8)) & 0xFF)
  30. #define ISSI_ID 0x9D
  31. #define MXIC_ID 0xC2
  32. #define GD_Q_ID_HIGH 0xC8
  33. #define GD_Q_ID_MID 0x40
  34. #define GD_Q_ID_LOW 0x16
  35. #define ESP_BOOTLOADER_SPIFLASH_BP_MASK_ISSI (BIT7 | BIT5 | BIT4 | BIT3 | BIT2)
  36. #define ESP_BOOTLOADER_SPIFLASH_QE_GD_SR2 BIT1 // QE position when you write 8 bits(for SR2) at one time.
  37. #define ESP_BOOTLOADER_SPIFLASH_QE_SR1_2BYTE BIT9 // QE position when you write 16 bits at one time.
  38. #ifndef BOOTLOADER_BUILD
  39. /* Normal app version maps to esp_spi_flash.h operations...
  40. */
  41. static const char *TAG = "bootloader_mmap";
  42. static spi_flash_mmap_handle_t map;
  43. uint32_t bootloader_mmap_get_free_pages(void)
  44. {
  45. return spi_flash_mmap_get_free_pages(SPI_FLASH_MMAP_DATA);
  46. }
  47. const void *bootloader_mmap(uint32_t src_addr, uint32_t size)
  48. {
  49. if (map) {
  50. ESP_LOGE(TAG, "tried to bootloader_mmap twice");
  51. return NULL; /* existing mapping in use... */
  52. }
  53. const void *result = NULL;
  54. uint32_t src_page = src_addr & ~(SPI_FLASH_MMU_PAGE_SIZE - 1);
  55. size += (src_addr - src_page);
  56. esp_err_t err = spi_flash_mmap(src_page, size, SPI_FLASH_MMAP_DATA, &result, &map);
  57. if (err != ESP_OK) {
  58. ESP_LOGE(TAG, "spi_flash_mmap failed: 0x%x", err);
  59. return NULL;
  60. }
  61. return (void *)((intptr_t)result + (src_addr - src_page));
  62. }
  63. void bootloader_munmap(const void *mapping)
  64. {
  65. if (mapping && map) {
  66. spi_flash_munmap(map);
  67. }
  68. map = 0;
  69. }
  70. esp_err_t bootloader_flash_read(size_t src, void *dest, size_t size, bool allow_decrypt)
  71. {
  72. if (allow_decrypt && esp_flash_encryption_enabled()) {
  73. return spi_flash_read_encrypted(src, dest, size);
  74. } else {
  75. return spi_flash_read(src, dest, size);
  76. }
  77. }
  78. esp_err_t bootloader_flash_write(size_t dest_addr, void *src, size_t size, bool write_encrypted)
  79. {
  80. if (write_encrypted && !ENCRYPTION_IS_VIRTUAL) {
  81. #if CONFIG_IDF_TARGET_ESP32
  82. return spi_flash_write_encrypted(dest_addr, src, size);
  83. #else
  84. return esp_rom_spiflash_write_encrypted(dest_addr, src, size);
  85. #endif
  86. } else {
  87. return spi_flash_write(dest_addr, src, size);
  88. }
  89. }
  90. esp_err_t bootloader_flash_erase_sector(size_t sector)
  91. {
  92. return spi_flash_erase_sector(sector);
  93. }
  94. esp_err_t bootloader_flash_erase_range(uint32_t start_addr, uint32_t size)
  95. {
  96. return spi_flash_erase_range(start_addr, size);
  97. }
  98. #else //BOOTLOADER_BUILD
  99. /* Bootloader version, uses ROM functions only */
  100. #if CONFIG_IDF_TARGET_ESP32
  101. #include "esp32/rom/cache.h"
  102. #endif
  103. #include "esp_rom_spiflash.h"
  104. #include "hal/mmu_hal.h"
  105. #include "hal/mmu_ll.h"
  106. #include "hal/cache_hal.h"
  107. static const char *TAG = "bootloader_flash";
  108. #if CONFIG_IDF_TARGET_ESP32
  109. /* Use first 50 blocks in MMU for bootloader_mmap,
  110. 50th block for bootloader_flash_read
  111. */
  112. #define MMU_BLOCK0_VADDR SOC_DROM_LOW
  113. #define MMU_SIZE (0x320000)
  114. #define MMU_BLOCK50_VADDR (MMU_BLOCK0_VADDR + MMU_SIZE)
  115. #define FLASH_READ_VADDR MMU_BLOCK50_VADDR
  116. #else // !CONFIG_IDF_TARGET_ESP32
  117. /* Use first 63 blocks in MMU for bootloader_mmap,
  118. 63th block for bootloader_flash_read
  119. */
  120. #define MMU_BLOCK0_VADDR SOC_DROM_LOW
  121. #define MMU_SIZE (0x3f0000)
  122. #define MMU_BLOCK63_VADDR (MMU_BLOCK0_VADDR + MMU_SIZE)
  123. #define FLASH_READ_VADDR MMU_BLOCK63_VADDR
  124. #endif
  125. #define MMU_FREE_PAGES (MMU_SIZE / FLASH_BLOCK_SIZE)
  126. static bool mapped;
  127. // Current bootloader mapping (ab)used for bootloader_read()
  128. static uint32_t current_read_mapping = UINT32_MAX;
  129. uint32_t bootloader_mmap_get_free_pages(void)
  130. {
  131. /**
  132. * Allow mapping up to 50 of the 51 available MMU blocks (last one used for reads)
  133. * Since, bootloader_mmap function below assumes it to be 0x320000 (50 pages), we can safely do this.
  134. */
  135. return MMU_FREE_PAGES;
  136. }
  137. const void *bootloader_mmap(uint32_t src_paddr, uint32_t size)
  138. {
  139. if (mapped) {
  140. ESP_EARLY_LOGE(TAG, "tried to bootloader_mmap twice");
  141. return NULL; /* can't map twice */
  142. }
  143. if (size > MMU_SIZE) {
  144. ESP_EARLY_LOGE(TAG, "bootloader_mmap excess size %x", size);
  145. return NULL;
  146. }
  147. uint32_t src_paddr_aligned = src_paddr & MMU_FLASH_MASK;
  148. //The addr is aligned, so we add the mask off length to the size, to make sure the corresponding buses are enabled.
  149. uint32_t size_after_paddr_aligned = (src_paddr - src_paddr_aligned) + size;
  150. /**
  151. * @note 1
  152. * Will add here a check to make sure the vaddr is on read-only and executable buses, since we use others for psram
  153. * Now simply check if it's valid vaddr, didn't check if it's readable, writable or executable.
  154. * TODO: IDF-4710
  155. */
  156. if (mmu_ll_check_valid_ext_vaddr_region(0, MMU_BLOCK0_VADDR, size_after_paddr_aligned) == 0) {
  157. ESP_EARLY_LOGE(TAG, "vaddr not valid");
  158. return NULL;
  159. }
  160. //-------------stop cache to do the MMU mapping--------------
  161. #if CONFIG_IDF_TARGET_ESP32
  162. Cache_Read_Disable(0);
  163. Cache_Flush(0);
  164. #else
  165. cache_hal_disable(CACHE_TYPE_ALL);
  166. #endif
  167. //---------------Do mapping------------------------
  168. ESP_EARLY_LOGD(TAG, "rodata starts from paddr=0x%08x, size=0x%x, will be mapped to vaddr=0x%08x", src_paddr, size, MMU_BLOCK0_VADDR);
  169. #if CONFIG_IDF_TARGET_ESP32
  170. uint32_t count = GET_REQUIRED_MMU_PAGES(size, src_paddr);
  171. int e = cache_flash_mmu_set(0, 0, MMU_BLOCK0_VADDR, src_paddr_aligned, 64, count);
  172. ESP_EARLY_LOGV(TAG, "after mapping, starting from paddr=0x%08x and vaddr=0x%08x, 0x%x bytes are mapped", src_paddr_aligned, MMU_BLOCK0_VADDR, count * MMU_PAGE_SIZE);
  173. if (e != 0) {
  174. ESP_EARLY_LOGE(TAG, "cache_flash_mmu_set failed: %d\n", e);
  175. Cache_Read_Enable(0);
  176. return NULL;
  177. }
  178. #else
  179. /**
  180. * This hal won't return error, it assumes the inputs are valid. The related check should be done in `bootloader_mmap()`.
  181. * See above comments (note 1) about IDF-4710
  182. */
  183. uint32_t actual_mapped_len = 0;
  184. mmu_hal_map_region(0, MMU_TARGET_FLASH0, MMU_BLOCK0_VADDR, src_paddr_aligned, size_after_paddr_aligned, &actual_mapped_len);
  185. ESP_EARLY_LOGV(TAG, "after mapping, starting from paddr=0x%08x and vaddr=0x%08x, 0x%x bytes are mapped", src_paddr_aligned, MMU_BLOCK0_VADDR, actual_mapped_len);
  186. #endif
  187. /**
  188. * If after mapping, your code stucks, it possibly means that some of the buses are not enabled, check `cache_ll_l1_enable_bus()`
  189. * For now, keep this unchanged.
  190. */
  191. //-------------enable cache--------------
  192. #if CONFIG_IDF_TARGET_ESP32
  193. Cache_Read_Enable(0);
  194. #else
  195. cache_hal_enable(CACHE_TYPE_ALL);
  196. #endif
  197. mapped = true;
  198. return (void *)(MMU_BLOCK0_VADDR + (src_paddr - src_paddr_aligned));
  199. }
  200. void bootloader_munmap(const void *mapping)
  201. {
  202. if (mapped) {
  203. #if CONFIG_IDF_TARGET_ESP32
  204. /* Full MMU reset */
  205. Cache_Read_Disable(0);
  206. Cache_Flush(0);
  207. mmu_init(0);
  208. #else
  209. cache_hal_disable(CACHE_TYPE_ALL);
  210. mmu_hal_init();
  211. #endif
  212. mapped = false;
  213. current_read_mapping = UINT32_MAX;
  214. }
  215. }
  216. static esp_err_t spi_to_esp_err(esp_rom_spiflash_result_t r)
  217. {
  218. switch (r) {
  219. case ESP_ROM_SPIFLASH_RESULT_OK:
  220. return ESP_OK;
  221. case ESP_ROM_SPIFLASH_RESULT_ERR:
  222. return ESP_ERR_FLASH_OP_FAIL;
  223. case ESP_ROM_SPIFLASH_RESULT_TIMEOUT:
  224. return ESP_ERR_FLASH_OP_TIMEOUT;
  225. default:
  226. return ESP_FAIL;
  227. }
  228. }
  229. static esp_err_t bootloader_flash_read_no_decrypt(size_t src_addr, void *dest, size_t size)
  230. {
  231. #if CONFIG_IDF_TARGET_ESP32
  232. Cache_Read_Disable(0);
  233. Cache_Flush(0);
  234. #else
  235. cache_hal_disable(CACHE_TYPE_ALL);
  236. #endif
  237. esp_rom_spiflash_result_t r = esp_rom_spiflash_read(src_addr, dest, size);
  238. #if CONFIG_IDF_TARGET_ESP32
  239. Cache_Read_Enable(0);
  240. #else
  241. cache_hal_enable(CACHE_TYPE_ALL);
  242. #endif
  243. return spi_to_esp_err(r);
  244. }
  245. static esp_err_t bootloader_flash_read_allow_decrypt(size_t src_addr, void *dest, size_t size)
  246. {
  247. uint32_t *dest_words = (uint32_t *)dest;
  248. for (size_t word = 0; word < size / 4; word++) {
  249. uint32_t word_src = src_addr + word * 4; /* Read this offset from flash */
  250. uint32_t map_at = word_src & MMU_FLASH_MASK; /* Map this 64KB block from flash */
  251. uint32_t *map_ptr;
  252. /* Move the 64KB mmu mapping window to fit map_at */
  253. if (map_at != current_read_mapping) {
  254. //----------Stop cache for mapping----------------
  255. #if CONFIG_IDF_TARGET_ESP32
  256. Cache_Read_Disable(0);
  257. Cache_Flush(0);
  258. #else
  259. cache_hal_disable(CACHE_TYPE_ALL);
  260. #endif
  261. //---------------Do mapping------------------------
  262. ESP_EARLY_LOGD(TAG, "mmu set block paddr=0x%08x (was 0x%08x)", map_at, current_read_mapping);
  263. #if CONFIG_IDF_TARGET_ESP32
  264. //Should never fail if we only map a MMU_PAGE_SIZE to the vaddr starting from FLASH_READ_VADDR
  265. int e = cache_flash_mmu_set(0, 0, FLASH_READ_VADDR, map_at, 64, 1);
  266. assert(e == 0);
  267. #else
  268. uint32_t actual_mapped_len = 0;
  269. mmu_hal_map_region(0, MMU_TARGET_FLASH0, FLASH_READ_VADDR, map_at, MMU_PAGE_SIZE - 1, &actual_mapped_len);
  270. #endif
  271. current_read_mapping = map_at;
  272. //-------------enable cache-------------------------
  273. #if CONFIG_IDF_TARGET_ESP32
  274. Cache_Read_Enable(0);
  275. #else
  276. cache_hal_enable(CACHE_TYPE_ALL);
  277. #endif
  278. }
  279. map_ptr = (uint32_t *)(FLASH_READ_VADDR + (word_src - map_at));
  280. dest_words[word] = *map_ptr;
  281. }
  282. return ESP_OK;
  283. }
  284. esp_err_t bootloader_flash_read(size_t src_addr, void *dest, size_t size, bool allow_decrypt)
  285. {
  286. if (src_addr & 3) {
  287. ESP_LOGE(TAG, "bootloader_flash_read src_addr 0x%x not 4-byte aligned", src_addr);
  288. return ESP_FAIL;
  289. }
  290. if (size & 3) {
  291. ESP_LOGE(TAG, "bootloader_flash_read size 0x%x not 4-byte aligned", size);
  292. return ESP_FAIL;
  293. }
  294. if ((intptr_t)dest & 3) {
  295. ESP_LOGE(TAG, "bootloader_flash_read dest 0x%x not 4-byte aligned", (intptr_t)dest);
  296. return ESP_FAIL;
  297. }
  298. if (allow_decrypt) {
  299. return bootloader_flash_read_allow_decrypt(src_addr, dest, size);
  300. } else {
  301. return bootloader_flash_read_no_decrypt(src_addr, dest, size);
  302. }
  303. }
  304. esp_err_t bootloader_flash_write(size_t dest_addr, void *src, size_t size, bool write_encrypted)
  305. {
  306. esp_err_t err;
  307. size_t alignment = write_encrypted ? 32 : 4;
  308. if ((dest_addr % alignment) != 0) {
  309. ESP_LOGE(TAG, "bootloader_flash_write dest_addr 0x%x not %d-byte aligned", dest_addr, alignment);
  310. return ESP_FAIL;
  311. }
  312. if ((size % alignment) != 0) {
  313. ESP_LOGE(TAG, "bootloader_flash_write size 0x%x not %d-byte aligned", size, alignment);
  314. return ESP_FAIL;
  315. }
  316. if (((intptr_t)src % 4) != 0) {
  317. ESP_LOGE(TAG, "bootloader_flash_write src 0x%x not 4 byte aligned", (intptr_t)src);
  318. return ESP_FAIL;
  319. }
  320. err = bootloader_flash_unlock();
  321. if (err != ESP_OK) {
  322. return err;
  323. }
  324. if (write_encrypted && !ENCRYPTION_IS_VIRTUAL) {
  325. return spi_to_esp_err(esp_rom_spiflash_write_encrypted(dest_addr, src, size));
  326. } else {
  327. return spi_to_esp_err(esp_rom_spiflash_write(dest_addr, src, size));
  328. }
  329. }
  330. esp_err_t bootloader_flash_erase_sector(size_t sector)
  331. {
  332. return spi_to_esp_err(esp_rom_spiflash_erase_sector(sector));
  333. }
  334. esp_err_t bootloader_flash_erase_range(uint32_t start_addr, uint32_t size)
  335. {
  336. if (start_addr % FLASH_SECTOR_SIZE != 0) {
  337. return ESP_ERR_INVALID_ARG;
  338. }
  339. if (size % FLASH_SECTOR_SIZE != 0) {
  340. return ESP_ERR_INVALID_SIZE;
  341. }
  342. size_t start = start_addr / FLASH_SECTOR_SIZE;
  343. size_t end = start + size / FLASH_SECTOR_SIZE;
  344. const size_t sectors_per_block = FLASH_BLOCK_SIZE / FLASH_SECTOR_SIZE;
  345. esp_rom_spiflash_result_t rc = ESP_ROM_SPIFLASH_RESULT_OK;
  346. for (size_t sector = start; sector != end && rc == ESP_ROM_SPIFLASH_RESULT_OK; ) {
  347. if (sector % sectors_per_block == 0 && end - sector >= sectors_per_block) {
  348. rc = esp_rom_spiflash_erase_block(sector / sectors_per_block);
  349. sector += sectors_per_block;
  350. } else {
  351. rc = esp_rom_spiflash_erase_sector(sector);
  352. ++sector;
  353. }
  354. }
  355. return spi_to_esp_err(rc);
  356. }
  357. #endif // BOOTLOADER_BUILD
  358. FORCE_INLINE_ATTR bool is_issi_chip(const esp_rom_spiflash_chip_t* chip)
  359. {
  360. return BYTESHIFT(chip->device_id, 2) == ISSI_ID;
  361. }
  362. // For GD25Q32, GD25Q64, GD25Q127C, GD25Q128, which use single command to read/write different SR.
  363. FORCE_INLINE_ATTR bool is_gd_q_chip(const esp_rom_spiflash_chip_t* chip)
  364. {
  365. return BYTESHIFT(chip->device_id, 2) == GD_Q_ID_HIGH && BYTESHIFT(chip->device_id, 1) == GD_Q_ID_MID && BYTESHIFT(chip->device_id, 0) >= GD_Q_ID_LOW;
  366. }
  367. FORCE_INLINE_ATTR bool is_mxic_chip(const esp_rom_spiflash_chip_t* chip)
  368. {
  369. return BYTESHIFT(chip->device_id, 2) == MXIC_ID;
  370. }
  371. esp_err_t IRAM_ATTR __attribute__((weak)) bootloader_flash_unlock(void)
  372. {
  373. // At the beginning status == new_status == status_sr2 == new_status_sr2 == 0.
  374. // If the register doesn't need to be updated, keep them the same (0), so that no command will be actually sent.
  375. uint16_t status = 0; // status for SR1 or SR1+SR2 if writing SR with 01H + 2Bytes.
  376. uint16_t new_status = 0;
  377. uint8_t status_sr2 = 0; // status_sr2 for SR2.
  378. uint8_t new_status_sr2 = 0;
  379. uint8_t sr1_bit_num = 0;
  380. esp_err_t err = ESP_OK;
  381. esp_rom_spiflash_wait_idle(&g_rom_flashchip);
  382. if (is_issi_chip(&g_rom_flashchip) || is_mxic_chip(&g_rom_flashchip)) {
  383. // Currently ISSI & MXIC share the same command and register layout, which is different from the default model.
  384. // If any code here needs to be modified, check both chips.
  385. status = bootloader_execute_flash_command(CMD_RDSR, 0, 0, 8);
  386. /* Clear all bits in the mask.
  387. (This is different from ROM esp_rom_spiflash_unlock, which keeps all bits as-is.)
  388. */
  389. sr1_bit_num = 8;
  390. new_status = status & (~ESP_BOOTLOADER_SPIFLASH_BP_MASK_ISSI);
  391. } else if (is_gd_q_chip(&g_rom_flashchip)) {
  392. /* The GD chips behaviour is to clear all bits in SR1 and clear bits in SR2 except QE bit.
  393. Use 01H to write SR1 and 31H to write SR2.
  394. */
  395. status = bootloader_execute_flash_command(CMD_RDSR, 0, 0, 8);
  396. sr1_bit_num = 8;
  397. new_status = 0;
  398. status_sr2 = bootloader_execute_flash_command(CMD_RDSR2, 0, 0, 8);
  399. new_status_sr2 = status_sr2 & ESP_BOOTLOADER_SPIFLASH_QE_GD_SR2;
  400. } else {
  401. /* For common behaviour, like XMC chips, Use 01H+2Bytes to write both SR1 and SR2*/
  402. status = bootloader_execute_flash_command(CMD_RDSR, 0, 0, 8) | (bootloader_execute_flash_command(CMD_RDSR2, 0, 0, 8) << 8);
  403. /* Clear all bits except QE, if it is set.
  404. (This is different from ROM esp_rom_spiflash_unlock, which keeps all bits as-is.)
  405. */
  406. sr1_bit_num = 16;
  407. new_status = status & ESP_BOOTLOADER_SPIFLASH_QE_SR1_2BYTE;
  408. }
  409. // When SR is written, set to true to indicate that WRDI need to be sent to ensure the protection is ON before return.
  410. bool status_written = false;
  411. // Skip if nothing needs to be changed. Meaningless writing to SR increases the risk during write and wastes time.
  412. if (status != new_status) {
  413. esp_rom_spiflash_wait_idle(&g_rom_flashchip);
  414. bootloader_execute_flash_command(CMD_WREN, 0, 0, 0);
  415. bootloader_execute_flash_command(CMD_WRSR, new_status, sr1_bit_num, 0);
  416. status_written = true;
  417. }
  418. if (status_sr2 != new_status_sr2) {
  419. esp_rom_spiflash_wait_idle(&g_rom_flashchip);
  420. bootloader_execute_flash_command(CMD_WREN, 0, 0, 0);
  421. bootloader_execute_flash_command(CMD_WRSR2, new_status_sr2, 8, 0);
  422. status_written = true;
  423. }
  424. if (status_written) {
  425. //Call esp_rom_spiflash_wait_idle to make sure previous WRSR is completed.
  426. esp_rom_spiflash_wait_idle(&g_rom_flashchip);
  427. bootloader_execute_flash_command(CMD_WRDI, 0, 0, 0);
  428. }
  429. return err;
  430. }
  431. IRAM_ATTR uint32_t bootloader_flash_execute_command_common(
  432. uint8_t command,
  433. uint32_t addr_len, uint32_t address,
  434. uint8_t dummy_len,
  435. uint8_t mosi_len, uint32_t mosi_data,
  436. uint8_t miso_len)
  437. {
  438. assert(mosi_len <= 32);
  439. assert(miso_len <= 32);
  440. uint32_t old_ctrl_reg = SPIFLASH.ctrl.val;
  441. uint32_t old_user_reg = SPIFLASH.user.val;
  442. uint32_t old_user1_reg = SPIFLASH.user1.val;
  443. #if CONFIG_IDF_TARGET_ESP32
  444. SPIFLASH.ctrl.val = SPI_WP_REG_M; // keep WP high while idle, otherwise leave DIO mode
  445. #else
  446. SPIFLASH.ctrl.val = SPI_MEM_WP_REG_M; // keep WP high while idle, otherwise leave DIO mode
  447. #endif
  448. //command phase
  449. SPIFLASH.user.usr_command = 1;
  450. SPIFLASH.user2.usr_command_bitlen = 7;
  451. SPIFLASH.user2.usr_command_value = command;
  452. //addr phase
  453. SPIFLASH.user.usr_addr = addr_len > 0;
  454. SPIFLASH.user1.usr_addr_bitlen = addr_len - 1;
  455. #if CONFIG_IDF_TARGET_ESP32
  456. SPIFLASH.addr = (addr_len > 0)? (address << (32-addr_len)) : 0;
  457. #else
  458. SPIFLASH.addr = address;
  459. #endif
  460. //dummy phase
  461. uint32_t total_dummy = dummy_len;
  462. if (miso_len > 0) {
  463. total_dummy += g_rom_spiflash_dummy_len_plus[1];
  464. }
  465. SPIFLASH.user.usr_dummy = total_dummy > 0;
  466. SPIFLASH.user1.usr_dummy_cyclelen = total_dummy - 1;
  467. //output data
  468. SPIFLASH.user.usr_mosi = mosi_len > 0;
  469. #if CONFIG_IDF_TARGET_ESP32
  470. SPIFLASH.mosi_dlen.usr_mosi_dbitlen = mosi_len ? (mosi_len - 1) : 0;
  471. #else
  472. SPIFLASH.mosi_dlen.usr_mosi_bit_len = mosi_len ? (mosi_len - 1) : 0;
  473. #endif
  474. SPIFLASH.data_buf[0] = mosi_data;
  475. //input data
  476. SPIFLASH.user.usr_miso = miso_len > 0;
  477. #if CONFIG_IDF_TARGET_ESP32
  478. SPIFLASH.miso_dlen.usr_miso_dbitlen = miso_len ? (miso_len - 1) : 0;
  479. #else
  480. SPIFLASH.miso_dlen.usr_miso_bit_len = miso_len ? (miso_len - 1) : 0;
  481. #endif
  482. SPIFLASH.cmd.usr = 1;
  483. while (SPIFLASH.cmd.usr != 0) {
  484. }
  485. SPIFLASH.ctrl.val = old_ctrl_reg;
  486. SPIFLASH.user.val = old_user_reg;
  487. SPIFLASH.user1.val = old_user1_reg;
  488. uint32_t ret = SPIFLASH.data_buf[0];
  489. if (miso_len < 32) {
  490. //set unused bits to 0
  491. ret &= ~(UINT32_MAX << miso_len);
  492. }
  493. return ret;
  494. }
  495. uint32_t IRAM_ATTR bootloader_execute_flash_command(uint8_t command, uint32_t mosi_data, uint8_t mosi_len, uint8_t miso_len)
  496. {
  497. const uint8_t addr_len = 0;
  498. const uint8_t address = 0;
  499. const uint8_t dummy_len = 0;
  500. return bootloader_flash_execute_command_common(command, addr_len, address,
  501. dummy_len, mosi_len, mosi_data, miso_len);
  502. }
  503. // cmd(0x5A) + 24bit address + 8 cycles dummy
  504. uint32_t IRAM_ATTR bootloader_flash_read_sfdp(uint32_t sfdp_addr, unsigned int miso_byte_num)
  505. {
  506. assert(miso_byte_num <= 4);
  507. const uint8_t command = CMD_RDSFDP;
  508. const uint8_t addr_len = 24;
  509. const uint8_t dummy_len = 8;
  510. const uint8_t mosi_len = 0;
  511. const uint32_t mosi_data = 0;
  512. const uint8_t miso_len = miso_byte_num * 8;
  513. return bootloader_flash_execute_command_common(command, addr_len, sfdp_addr,
  514. dummy_len, mosi_len, mosi_data, miso_len);
  515. }
  516. void bootloader_enable_wp(void)
  517. {
  518. bootloader_execute_flash_command(CMD_WRDI, 0, 0, 0); /* Exit OTP mode */
  519. }
  520. uint32_t IRAM_ATTR bootloader_read_flash_id(void)
  521. {
  522. uint32_t id = bootloader_execute_flash_command(CMD_RDID, 0, 0, 24);
  523. id = ((id & 0xff) << 16) | ((id >> 16) & 0xff) | (id & 0xff00);
  524. return id;
  525. }
  526. #if SOC_CACHE_SUPPORT_WRAP
  527. esp_err_t bootloader_flash_wrap_set(spi_flash_wrap_mode_t mode)
  528. {
  529. uint32_t reg_bkp_ctrl = SPIFLASH.ctrl.val;
  530. uint32_t reg_bkp_usr = SPIFLASH.user.val;
  531. SPIFLASH.user.fwrite_dio = 0;
  532. SPIFLASH.user.fwrite_dual = 0;
  533. SPIFLASH.user.fwrite_qio = 1;
  534. SPIFLASH.user.fwrite_quad = 0;
  535. SPIFLASH.ctrl.fcmd_dual = 0;
  536. SPIFLASH.ctrl.fcmd_quad = 0;
  537. SPIFLASH.user.usr_dummy = 0;
  538. SPIFLASH.user.usr_addr = 1;
  539. SPIFLASH.user.usr_command = 1;
  540. SPIFLASH.user2.usr_command_bitlen = 7;
  541. SPIFLASH.user2.usr_command_value = CMD_WRAP;
  542. SPIFLASH.user1.usr_addr_bitlen = 23;
  543. SPIFLASH.addr = 0;
  544. SPIFLASH.user.usr_miso = 0;
  545. SPIFLASH.user.usr_mosi = 1;
  546. SPIFLASH.mosi_dlen.usr_mosi_bit_len = 7;
  547. SPIFLASH.data_buf[0] = (uint32_t) mode << 4;;
  548. SPIFLASH.cmd.usr = 1;
  549. while(SPIFLASH.cmd.usr != 0)
  550. { }
  551. SPIFLASH.ctrl.val = reg_bkp_ctrl;
  552. SPIFLASH.user.val = reg_bkp_usr;
  553. return ESP_OK;
  554. }
  555. #endif //SOC_CACHE_SUPPORT_WRAP
  556. /*******************************************************************************
  557. * XMC startup flow
  558. ******************************************************************************/
  559. #define XMC_SUPPORT CONFIG_BOOTLOADER_FLASH_XMC_SUPPORT
  560. #define XMC_VENDOR_ID 0x20
  561. #if BOOTLOADER_BUILD
  562. #define BOOTLOADER_FLASH_LOG(level, ...) ESP_LOG##level(TAG, ##__VA_ARGS__)
  563. #else
  564. static DRAM_ATTR char bootloader_flash_tag[] = "bootloader_flash";
  565. #define BOOTLOADER_FLASH_LOG(level, ...) ESP_DRAM_LOG##level(bootloader_flash_tag, ##__VA_ARGS__)
  566. #endif
  567. #if XMC_SUPPORT
  568. //strictly check the model
  569. static IRAM_ATTR bool is_xmc_chip_strict(uint32_t rdid)
  570. {
  571. uint32_t vendor_id = BYTESHIFT(rdid, 2);
  572. uint32_t mfid = BYTESHIFT(rdid, 1);
  573. uint32_t cpid = BYTESHIFT(rdid, 0);
  574. if (vendor_id != XMC_VENDOR_ID) {
  575. return false;
  576. }
  577. bool matched = false;
  578. if (mfid == 0x40) {
  579. if (cpid >= 0x13 && cpid <= 0x20) {
  580. matched = true;
  581. }
  582. } else if (mfid == 0x41) {
  583. if (cpid >= 0x17 && cpid <= 0x20) {
  584. matched = true;
  585. }
  586. } else if (mfid == 0x50) {
  587. if (cpid >= 0x15 && cpid <= 0x16) {
  588. matched = true;
  589. }
  590. }
  591. return matched;
  592. }
  593. esp_err_t IRAM_ATTR bootloader_flash_xmc_startup(void)
  594. {
  595. // If the RDID value is a valid XMC one, may skip the flow
  596. const bool fast_check = true;
  597. if (fast_check && is_xmc_chip_strict(g_rom_flashchip.device_id)) {
  598. BOOTLOADER_FLASH_LOG(D, "XMC chip detected by RDID (%08X), skip.", g_rom_flashchip.device_id);
  599. return ESP_OK;
  600. }
  601. // Check the Manufacturer ID in SFDP registers (JEDEC standard). If not XMC chip, no need to run the flow
  602. const int sfdp_mfid_addr = 0x10;
  603. uint8_t mf_id = (bootloader_flash_read_sfdp(sfdp_mfid_addr, 1) & 0xff);
  604. if (mf_id != XMC_VENDOR_ID) {
  605. BOOTLOADER_FLASH_LOG(D, "non-XMC chip detected by SFDP Read (%02X), skip.", mf_id);
  606. return ESP_OK;
  607. }
  608. BOOTLOADER_FLASH_LOG(I, "XM25QHxxC startup flow");
  609. // Enter DPD
  610. bootloader_execute_flash_command(0xB9, 0, 0, 0);
  611. // Enter UDPD
  612. bootloader_execute_flash_command(0x79, 0, 0, 0);
  613. // Exit UDPD
  614. bootloader_execute_flash_command(0xFF, 0, 0, 0);
  615. // Delay tXUDPD
  616. esp_rom_delay_us(2000);
  617. // Release Power-down
  618. bootloader_execute_flash_command(0xAB, 0, 0, 0);
  619. esp_rom_delay_us(20);
  620. // Read flash ID and check again
  621. g_rom_flashchip.device_id = bootloader_read_flash_id();
  622. if (!is_xmc_chip_strict(g_rom_flashchip.device_id)) {
  623. BOOTLOADER_FLASH_LOG(E, "XMC flash startup fail");
  624. return ESP_FAIL;
  625. }
  626. return ESP_OK;
  627. }
  628. #else
  629. //only compare the vendor id
  630. static IRAM_ATTR bool is_xmc_chip(uint32_t rdid)
  631. {
  632. uint32_t vendor_id = (rdid >> 16) & 0xFF;
  633. return (vendor_id == XMC_VENDOR_ID);
  634. }
  635. esp_err_t IRAM_ATTR bootloader_flash_xmc_startup(void)
  636. {
  637. if (is_xmc_chip(g_rom_flashchip.device_id)) {
  638. BOOTLOADER_FLASH_LOG(E, "XMC chip detected (%08X) while support disabled.", g_rom_flashchip.device_id);
  639. return ESP_FAIL;
  640. }
  641. return ESP_OK;
  642. }
  643. #endif //XMC_SUPPORT