bootloader_flash.c 26 KB

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