bootloader_flash.c 25 KB

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