bootloader_flash.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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. #endif
  34. #ifdef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
  35. #define ENCRYPTION_IS_VIRTUAL 1
  36. #else
  37. #define ENCRYPTION_IS_VIRTUAL 0
  38. #endif
  39. #define BYTESHIFT(VAR, IDX) (((VAR) >> ((IDX) * 8)) & 0xFF)
  40. #define ISSI_ID 0x9D
  41. #define GD_Q_ID_HIGH 0xC8
  42. #define GD_Q_ID_MID 0x40
  43. #define GD_Q_ID_LOW 0x16
  44. #define ESP_BOOTLOADER_SPIFLASH_BP_MASK_ISSI (BIT7 | BIT5 | BIT4 | BIT3 | BIT2)
  45. #define ESP_BOOTLOADER_SPIFLASH_QE_16B BIT9 // QE position when you write 16 bits at one time.
  46. #define ESP_BOOTLOADER_SPIFLASH_QE_8B BIT1 // QE position when you write 8 bits(for SR2) at one time.
  47. #define ESP_BOOTLOADER_SPIFLASH_WRITE_8B (8)
  48. #define ESP_BOOTLOADER_SPIFLASH_WRITE_16B (16)
  49. #ifndef BOOTLOADER_BUILD
  50. /* Normal app version maps to esp_spi_flash.h operations...
  51. */
  52. static const char *TAG = "bootloader_mmap";
  53. static spi_flash_mmap_handle_t map;
  54. uint32_t bootloader_mmap_get_free_pages(void)
  55. {
  56. return spi_flash_mmap_get_free_pages(SPI_FLASH_MMAP_DATA);
  57. }
  58. const void *bootloader_mmap(uint32_t src_addr, uint32_t size)
  59. {
  60. if (map) {
  61. ESP_LOGE(TAG, "tried to bootloader_mmap twice");
  62. return NULL; /* existing mapping in use... */
  63. }
  64. const void *result = NULL;
  65. uint32_t src_page = src_addr & ~(SPI_FLASH_MMU_PAGE_SIZE - 1);
  66. size += (src_addr - src_page);
  67. esp_err_t err = spi_flash_mmap(src_page, size, SPI_FLASH_MMAP_DATA, &result, &map);
  68. if (err != ESP_OK) {
  69. ESP_LOGE(TAG, "spi_flash_mmap failed: 0x%x", err);
  70. return NULL;
  71. }
  72. return (void *)((intptr_t)result + (src_addr - src_page));
  73. }
  74. void bootloader_munmap(const void *mapping)
  75. {
  76. if (mapping && map) {
  77. spi_flash_munmap(map);
  78. }
  79. map = 0;
  80. }
  81. esp_err_t bootloader_flash_read(size_t src, void *dest, size_t size, bool allow_decrypt)
  82. {
  83. if (allow_decrypt && esp_flash_encryption_enabled()) {
  84. return spi_flash_read_encrypted(src, dest, size);
  85. } else {
  86. return spi_flash_read(src, dest, size);
  87. }
  88. }
  89. esp_err_t bootloader_flash_write(size_t dest_addr, void *src, size_t size, bool write_encrypted)
  90. {
  91. if (write_encrypted && !ENCRYPTION_IS_VIRTUAL) {
  92. #if CONFIG_IDF_TARGET_ESP32
  93. return spi_flash_write_encrypted(dest_addr, src, size);
  94. #else
  95. return esp_rom_spiflash_write_encrypted(dest_addr, src, size);
  96. #endif
  97. } else {
  98. return spi_flash_write(dest_addr, src, size);
  99. }
  100. }
  101. esp_err_t bootloader_flash_erase_sector(size_t sector)
  102. {
  103. return spi_flash_erase_sector(sector);
  104. }
  105. esp_err_t bootloader_flash_erase_range(uint32_t start_addr, uint32_t size)
  106. {
  107. return spi_flash_erase_range(start_addr, size);
  108. }
  109. #else
  110. /* Bootloader version, uses ROM functions only */
  111. #if CONFIG_IDF_TARGET_ESP32
  112. #include "esp32/rom/spi_flash.h"
  113. #include "esp32/rom/cache.h"
  114. #elif CONFIG_IDF_TARGET_ESP32S2
  115. #include "esp32s2/rom/spi_flash.h"
  116. #include "esp32s2/rom/cache.h"
  117. #include "soc/cache_memory.h"
  118. #elif CONFIG_IDF_TARGET_ESP32S3
  119. #include "esp32s3/rom/spi_flash.h"
  120. #include "esp32s3/rom/cache.h"
  121. #include "soc/cache_memory.h"
  122. #elif CONFIG_IDF_TARGET_ESP32C3
  123. #include "esp32c3/rom/spi_flash.h"
  124. #include "esp32c3/rom/cache.h"
  125. #include "soc/cache_memory.h"
  126. #elif CONFIG_IDF_TARGET_ESP32H2
  127. #include "esp32h2/rom/spi_flash.h"
  128. #include "esp32h2/rom/cache.h"
  129. #include "soc/cache_memory.h"
  130. #endif
  131. static const char *TAG = "bootloader_flash";
  132. #if CONFIG_IDF_TARGET_ESP32
  133. /* Use first 50 blocks in MMU for bootloader_mmap,
  134. 50th block for bootloader_flash_read
  135. */
  136. #define MMU_BLOCK0_VADDR SOC_DROM_LOW
  137. #define MMU_SIZE (0x320000)
  138. #define MMU_BLOCK50_VADDR (MMU_BLOCK0_VADDR + MMU_SIZE)
  139. #define FLASH_READ_VADDR MMU_BLOCK50_VADDR
  140. #else // !CONFIG_IDF_TARGET_ESP32
  141. /* Use first 63 blocks in MMU for bootloader_mmap,
  142. 63th block for bootloader_flash_read
  143. */
  144. #define MMU_BLOCK0_VADDR SOC_DROM_LOW
  145. #define MMU_SIZE (0x3f0000)
  146. #define MMU_BLOCK63_VADDR (MMU_BLOCK0_VADDR + MMU_SIZE)
  147. #define FLASH_READ_VADDR MMU_BLOCK63_VADDR
  148. #endif
  149. #define MMU_FREE_PAGES (MMU_SIZE / FLASH_BLOCK_SIZE)
  150. static bool mapped;
  151. // Current bootloader mapping (ab)used for bootloader_read()
  152. static uint32_t current_read_mapping = UINT32_MAX;
  153. uint32_t bootloader_mmap_get_free_pages(void)
  154. {
  155. /**
  156. * Allow mapping up to 50 of the 51 available MMU blocks (last one used for reads)
  157. * Since, bootloader_mmap function below assumes it to be 0x320000 (50 pages), we can safely do this.
  158. */
  159. return MMU_FREE_PAGES;
  160. }
  161. const void *bootloader_mmap(uint32_t src_addr, uint32_t size)
  162. {
  163. if (mapped) {
  164. ESP_LOGE(TAG, "tried to bootloader_mmap twice");
  165. return NULL; /* can't map twice */
  166. }
  167. if (size > MMU_SIZE) {
  168. ESP_LOGE(TAG, "bootloader_mmap excess size %x", size);
  169. return NULL;
  170. }
  171. uint32_t src_addr_aligned = src_addr & MMU_FLASH_MASK;
  172. uint32_t count = bootloader_cache_pages_to_map(size, src_addr);
  173. #if CONFIG_IDF_TARGET_ESP32
  174. Cache_Read_Disable(0);
  175. Cache_Flush(0);
  176. #elif CONFIG_IDF_TARGET_ESP32S2
  177. uint32_t autoload = Cache_Suspend_ICache();
  178. Cache_Invalidate_ICache_All();
  179. #elif CONFIG_IDF_TARGET_ESP32S3
  180. uint32_t autoload = Cache_Suspend_DCache();
  181. Cache_Invalidate_DCache_All();
  182. #elif CONFIG_IDF_TARGET_ESP32C3
  183. uint32_t autoload = Cache_Suspend_ICache();
  184. Cache_Invalidate_ICache_All();
  185. #elif CONFIG_IDF_TARGET_ESP32H2
  186. uint32_t autoload = Cache_Suspend_ICache();
  187. Cache_Invalidate_ICache_All();
  188. #endif
  189. ESP_LOGD(TAG, "mmu set paddr=%08x count=%d size=%x src_addr=%x src_addr_aligned=%x",
  190. src_addr & MMU_FLASH_MASK, count, size, src_addr, src_addr_aligned );
  191. #if CONFIG_IDF_TARGET_ESP32
  192. int e = cache_flash_mmu_set(0, 0, MMU_BLOCK0_VADDR, src_addr_aligned, 64, count);
  193. #elif CONFIG_IDF_TARGET_ESP32S2
  194. int e = Cache_Ibus_MMU_Set(MMU_ACCESS_FLASH, MMU_BLOCK0_VADDR, src_addr_aligned, 64, count, 0);
  195. #else // S3, C3, H2
  196. int e = Cache_Dbus_MMU_Set(MMU_ACCESS_FLASH, MMU_BLOCK0_VADDR, src_addr_aligned, 64, count, 0);
  197. #endif
  198. if (e != 0) {
  199. ESP_LOGE(TAG, "cache_flash_mmu_set failed: %d\n", e);
  200. #if CONFIG_IDF_TARGET_ESP32
  201. Cache_Read_Enable(0);
  202. #elif CONFIG_IDF_TARGET_ESP32S2
  203. Cache_Resume_ICache(autoload);
  204. #elif CONFIG_IDF_TARGET_ESP32S3
  205. Cache_Resume_DCache(autoload);
  206. #elif CONFIG_IDF_TARGET_ESP32C3
  207. Cache_Resume_ICache(autoload);
  208. #elif CONFIG_IDF_TARGET_ESP32H2
  209. Cache_Resume_ICache(autoload);
  210. #endif
  211. return NULL;
  212. }
  213. #if CONFIG_IDF_TARGET_ESP32
  214. Cache_Read_Enable(0);
  215. #elif CONFIG_IDF_TARGET_ESP32S2
  216. Cache_Resume_ICache(autoload);
  217. #elif CONFIG_IDF_TARGET_ESP32S3
  218. Cache_Resume_DCache(autoload);
  219. #elif CONFIG_IDF_TARGET_ESP32C3
  220. Cache_Resume_ICache(autoload);
  221. #elif CONFIG_IDF_TARGET_ESP32H2
  222. Cache_Resume_ICache(autoload);
  223. #endif
  224. mapped = true;
  225. return (void *)(MMU_BLOCK0_VADDR + (src_addr - src_addr_aligned));
  226. }
  227. void bootloader_munmap(const void *mapping)
  228. {
  229. if (mapped) {
  230. #if CONFIG_IDF_TARGET_ESP32
  231. /* Full MMU reset */
  232. Cache_Read_Disable(0);
  233. Cache_Flush(0);
  234. mmu_init(0);
  235. #elif CONFIG_IDF_TARGET_ESP32S2
  236. //TODO, save the autoload value.
  237. Cache_Suspend_ICache();
  238. Cache_Invalidate_ICache_All();
  239. Cache_MMU_Init();
  240. #elif CONFIG_IDF_TARGET_ESP32S3
  241. Cache_Suspend_DCache();
  242. Cache_Invalidate_DCache_All();
  243. Cache_MMU_Init();
  244. #elif CONFIG_IDF_TARGET_ESP32C3
  245. //TODO, save the autoload value.
  246. Cache_Suspend_ICache();
  247. Cache_Invalidate_ICache_All();
  248. Cache_MMU_Init();
  249. #elif CONFIG_IDF_TARGET_ESP32H2
  250. Cache_Suspend_ICache();
  251. Cache_Invalidate_ICache_All();
  252. Cache_MMU_Init();
  253. #endif
  254. mapped = false;
  255. current_read_mapping = UINT32_MAX;
  256. }
  257. }
  258. static esp_err_t spi_to_esp_err(esp_rom_spiflash_result_t r)
  259. {
  260. switch (r) {
  261. case ESP_ROM_SPIFLASH_RESULT_OK:
  262. return ESP_OK;
  263. case ESP_ROM_SPIFLASH_RESULT_ERR:
  264. return ESP_ERR_FLASH_OP_FAIL;
  265. case ESP_ROM_SPIFLASH_RESULT_TIMEOUT:
  266. return ESP_ERR_FLASH_OP_TIMEOUT;
  267. default:
  268. return ESP_FAIL;
  269. }
  270. }
  271. static esp_err_t bootloader_flash_read_no_decrypt(size_t src_addr, void *dest, size_t size)
  272. {
  273. #if CONFIG_IDF_TARGET_ESP32
  274. Cache_Read_Disable(0);
  275. Cache_Flush(0);
  276. #elif CONFIG_IDF_TARGET_ESP32S2
  277. uint32_t autoload = Cache_Suspend_ICache();
  278. #elif CONFIG_IDF_TARGET_ESP32S3
  279. uint32_t autoload = Cache_Suspend_DCache();
  280. #elif CONFIG_IDF_TARGET_ESP32C3
  281. uint32_t autoload = Cache_Suspend_ICache();
  282. #elif CONFIG_IDF_TARGET_ESP32H2
  283. uint32_t autoload = Cache_Suspend_ICache();
  284. #endif
  285. esp_rom_spiflash_result_t r = esp_rom_spiflash_read(src_addr, dest, size);
  286. #if CONFIG_IDF_TARGET_ESP32
  287. Cache_Read_Enable(0);
  288. #elif CONFIG_IDF_TARGET_ESP32S2
  289. Cache_Resume_ICache(autoload);
  290. #elif CONFIG_IDF_TARGET_ESP32S3
  291. Cache_Resume_DCache(autoload);
  292. #elif CONFIG_IDF_TARGET_ESP32C3
  293. Cache_Resume_ICache(autoload);
  294. #elif CONFIG_IDF_TARGET_ESP32H2
  295. Cache_Resume_ICache(autoload);
  296. #endif
  297. return spi_to_esp_err(r);
  298. }
  299. static esp_err_t bootloader_flash_read_allow_decrypt(size_t src_addr, void *dest, size_t size)
  300. {
  301. uint32_t *dest_words = (uint32_t *)dest;
  302. for (size_t word = 0; word < size / 4; word++) {
  303. uint32_t word_src = src_addr + word * 4; /* Read this offset from flash */
  304. uint32_t map_at = word_src & MMU_FLASH_MASK; /* Map this 64KB block from flash */
  305. uint32_t *map_ptr;
  306. if (map_at != current_read_mapping) {
  307. /* Move the 64KB mmu mapping window to fit map_at */
  308. #if CONFIG_IDF_TARGET_ESP32
  309. Cache_Read_Disable(0);
  310. Cache_Flush(0);
  311. #elif CONFIG_IDF_TARGET_ESP32S2
  312. uint32_t autoload = Cache_Suspend_ICache();
  313. Cache_Invalidate_ICache_All();
  314. #elif CONFIG_IDF_TARGET_ESP32S3
  315. uint32_t autoload = Cache_Suspend_DCache();
  316. Cache_Invalidate_DCache_All();
  317. #elif CONFIG_IDF_TARGET_ESP32C3
  318. uint32_t autoload = Cache_Suspend_ICache();
  319. Cache_Invalidate_ICache_All();
  320. #elif CONFIG_IDF_TARGET_ESP32H2
  321. uint32_t autoload = Cache_Suspend_ICache();
  322. Cache_Invalidate_ICache_All();
  323. #endif
  324. ESP_LOGD(TAG, "mmu set block paddr=0x%08x (was 0x%08x)", map_at, current_read_mapping);
  325. #if CONFIG_IDF_TARGET_ESP32
  326. int e = cache_flash_mmu_set(0, 0, FLASH_READ_VADDR, map_at, 64, 1);
  327. #elif CONFIG_IDF_TARGET_ESP32S2
  328. int e = Cache_Ibus_MMU_Set(MMU_ACCESS_FLASH, MMU_BLOCK63_VADDR, map_at, 64, 1, 0);
  329. #elif CONFIG_IDF_TARGET_ESP32S3
  330. int e = Cache_Dbus_MMU_Set(MMU_ACCESS_FLASH, MMU_BLOCK63_VADDR, map_at, 64, 1, 0);
  331. #elif CONFIG_IDF_TARGET_ESP32C3
  332. int e = Cache_Dbus_MMU_Set(MMU_ACCESS_FLASH, MMU_BLOCK63_VADDR, map_at, 64, 1, 0);
  333. #elif CONFIG_IDF_TARGET_ESP32H2
  334. int e = Cache_Dbus_MMU_Set(MMU_ACCESS_FLASH, MMU_BLOCK63_VADDR, map_at, 64, 1, 0);
  335. #endif
  336. if (e != 0) {
  337. ESP_LOGE(TAG, "cache_flash_mmu_set failed: %d\n", e);
  338. #if CONFIG_IDF_TARGET_ESP32
  339. Cache_Read_Enable(0);
  340. #elif CONFIG_IDF_TARGET_ESP32S2
  341. Cache_Resume_ICache(autoload);
  342. #elif CONFIG_IDF_TARGET_ESP32S3
  343. Cache_Resume_DCache(autoload);
  344. #elif CONFIG_IDF_TARGET_ESP32C3
  345. Cache_Resume_ICache(autoload);
  346. #elif CONFIG_IDF_TARGET_ESP32H2
  347. Cache_Resume_ICache(autoload);
  348. #endif
  349. return ESP_FAIL;
  350. }
  351. current_read_mapping = map_at;
  352. #if CONFIG_IDF_TARGET_ESP32
  353. Cache_Read_Enable(0);
  354. #elif CONFIG_IDF_TARGET_ESP32S2
  355. Cache_Resume_ICache(autoload);
  356. #elif CONFIG_IDF_TARGET_ESP32S3
  357. Cache_Resume_DCache(autoload);
  358. #elif CONFIG_IDF_TARGET_ESP32C3
  359. Cache_Resume_ICache(autoload);
  360. #elif CONFIG_IDF_TARGET_ESP32H2
  361. Cache_Resume_ICache(autoload);
  362. #endif
  363. }
  364. map_ptr = (uint32_t *)(FLASH_READ_VADDR + (word_src - map_at));
  365. dest_words[word] = *map_ptr;
  366. }
  367. return ESP_OK;
  368. }
  369. esp_err_t bootloader_flash_read(size_t src_addr, void *dest, size_t size, bool allow_decrypt)
  370. {
  371. if (src_addr & 3) {
  372. ESP_LOGE(TAG, "bootloader_flash_read src_addr 0x%x not 4-byte aligned", src_addr);
  373. return ESP_FAIL;
  374. }
  375. if (size & 3) {
  376. ESP_LOGE(TAG, "bootloader_flash_read size 0x%x not 4-byte aligned", size);
  377. return ESP_FAIL;
  378. }
  379. if ((intptr_t)dest & 3) {
  380. ESP_LOGE(TAG, "bootloader_flash_read dest 0x%x not 4-byte aligned", (intptr_t)dest);
  381. return ESP_FAIL;
  382. }
  383. if (allow_decrypt) {
  384. return bootloader_flash_read_allow_decrypt(src_addr, dest, size);
  385. } else {
  386. return bootloader_flash_read_no_decrypt(src_addr, dest, size);
  387. }
  388. }
  389. esp_err_t bootloader_flash_write(size_t dest_addr, void *src, size_t size, bool write_encrypted)
  390. {
  391. esp_err_t err;
  392. size_t alignment = write_encrypted ? 32 : 4;
  393. if ((dest_addr % alignment) != 0) {
  394. ESP_LOGE(TAG, "bootloader_flash_write dest_addr 0x%x not %d-byte aligned", dest_addr, alignment);
  395. return ESP_FAIL;
  396. }
  397. if ((size % alignment) != 0) {
  398. ESP_LOGE(TAG, "bootloader_flash_write size 0x%x not %d-byte aligned", size, alignment);
  399. return ESP_FAIL;
  400. }
  401. if (((intptr_t)src % 4) != 0) {
  402. ESP_LOGE(TAG, "bootloader_flash_write src 0x%x not 4 byte aligned", (intptr_t)src);
  403. return ESP_FAIL;
  404. }
  405. err = bootloader_flash_unlock();
  406. if (err != ESP_OK) {
  407. return err;
  408. }
  409. if (write_encrypted && !ENCRYPTION_IS_VIRTUAL) {
  410. return spi_to_esp_err(esp_rom_spiflash_write_encrypted(dest_addr, src, size));
  411. } else {
  412. return spi_to_esp_err(esp_rom_spiflash_write(dest_addr, src, size));
  413. }
  414. }
  415. esp_err_t bootloader_flash_erase_sector(size_t sector)
  416. {
  417. return spi_to_esp_err(esp_rom_spiflash_erase_sector(sector));
  418. }
  419. esp_err_t bootloader_flash_erase_range(uint32_t start_addr, uint32_t size)
  420. {
  421. if (start_addr % FLASH_SECTOR_SIZE != 0) {
  422. return ESP_ERR_INVALID_ARG;
  423. }
  424. if (size % FLASH_SECTOR_SIZE != 0) {
  425. return ESP_ERR_INVALID_SIZE;
  426. }
  427. size_t start = start_addr / FLASH_SECTOR_SIZE;
  428. size_t end = start + size / FLASH_SECTOR_SIZE;
  429. const size_t sectors_per_block = FLASH_BLOCK_SIZE / FLASH_SECTOR_SIZE;
  430. esp_rom_spiflash_result_t rc = ESP_ROM_SPIFLASH_RESULT_OK;
  431. for (size_t sector = start; sector != end && rc == ESP_ROM_SPIFLASH_RESULT_OK; ) {
  432. if (sector % sectors_per_block == 0 && end - sector >= sectors_per_block) {
  433. rc = esp_rom_spiflash_erase_block(sector / sectors_per_block);
  434. sector += sectors_per_block;
  435. } else {
  436. rc = esp_rom_spiflash_erase_sector(sector);
  437. ++sector;
  438. }
  439. }
  440. return spi_to_esp_err(rc);
  441. }
  442. #endif
  443. FORCE_INLINE_ATTR bool is_issi_chip(const esp_rom_spiflash_chip_t* chip)
  444. {
  445. return BYTESHIFT(chip->device_id, 2) == ISSI_ID;
  446. }
  447. // For GD25Q32, GD25Q64, GD25Q127C, GD25Q128, which use single command to read/write different SR.
  448. FORCE_INLINE_ATTR bool is_gd_q_chip(const esp_rom_spiflash_chip_t* chip)
  449. {
  450. 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;
  451. }
  452. esp_err_t IRAM_ATTR __attribute__((weak)) bootloader_flash_unlock(void)
  453. {
  454. uint16_t status = 0; // status for SR1 or SR1+SR2 if writing SR with 01H + 2Bytes.
  455. uint16_t new_status = 0;
  456. uint8_t status_sr2 = 0; // status_sr2 for SR2.
  457. uint8_t new_status_sr2 = 0;
  458. uint8_t write_sr_bit = 0;
  459. esp_err_t err = ESP_OK;
  460. esp_rom_spiflash_wait_idle(&g_rom_flashchip);
  461. if (is_issi_chip(&g_rom_flashchip)) {
  462. write_sr_bit = ESP_BOOTLOADER_SPIFLASH_WRITE_8B;
  463. // ISSI chips have different QE position
  464. status = bootloader_execute_flash_command(CMD_RDSR, 0, 0, 8);
  465. /* Clear all bits in the mask.
  466. (This is different from ROM esp_rom_spiflash_unlock, which keeps all bits as-is.)
  467. */
  468. new_status = status & (~ESP_BOOTLOADER_SPIFLASH_BP_MASK_ISSI);
  469. // Skip if nothing needs to be cleared. Otherwise will waste time waiting for the flash to clear nothing.
  470. } else if (is_gd_q_chip(&g_rom_flashchip)) {
  471. /* The GD chips behaviour is to clear all bits in SR1 and clear bits in SR2 except QE bit.
  472. Use 01H to write SR1 and 31H to write SR2.
  473. */
  474. write_sr_bit = ESP_BOOTLOADER_SPIFLASH_WRITE_8B;
  475. status = bootloader_execute_flash_command(CMD_RDSR, 0, 0, 8);
  476. new_status = 0;
  477. status_sr2 = bootloader_execute_flash_command(CMD_RDSR2, 0, 0, 8);
  478. new_status_sr2 = status_sr2 & ESP_BOOTLOADER_SPIFLASH_QE_8B;
  479. } else {
  480. /* For common behaviour, like XMC chips, Use 01H+2Bytes to write both SR1 and SR2*/
  481. write_sr_bit = ESP_BOOTLOADER_SPIFLASH_WRITE_16B;
  482. status = bootloader_execute_flash_command(CMD_RDSR, 0, 0, 8) | (bootloader_execute_flash_command(CMD_RDSR2, 0, 0, 8) << 8);
  483. /* Clear all bits except QE, if it is set.
  484. (This is different from ROM esp_rom_spiflash_unlock, which keeps all bits as-is.)
  485. */
  486. new_status = status & ESP_BOOTLOADER_SPIFLASH_QE_16B;
  487. }
  488. if (status != new_status) {
  489. /* if the status in SR not equal to the ideal status, the status need to be updated */
  490. esp_rom_spiflash_wait_idle(&g_rom_flashchip);
  491. bootloader_execute_flash_command(CMD_WREN, 0, 0, 0);
  492. esp_rom_spiflash_wait_idle(&g_rom_flashchip);
  493. bootloader_execute_flash_command(CMD_WRSR, new_status, write_sr_bit, 0);
  494. esp_rom_spiflash_wait_idle(&g_rom_flashchip);
  495. }
  496. if (status_sr2 != new_status_sr2) {
  497. /* If the status in SR2 not equal to the ideal status, the status need to be updated.
  498. It doesn't need to be updated if status in SR2 is 0.
  499. Note: if we need to update both SR1 and SR2, the `CMD_WREN` needs to be sent again.
  500. */
  501. esp_rom_spiflash_wait_idle(&g_rom_flashchip);
  502. bootloader_execute_flash_command(CMD_WREN, 0, 0, 0);
  503. esp_rom_spiflash_wait_idle(&g_rom_flashchip);
  504. bootloader_execute_flash_command(CMD_WRSR2, new_status_sr2, write_sr_bit, 0);
  505. esp_rom_spiflash_wait_idle(&g_rom_flashchip);
  506. }
  507. bootloader_execute_flash_command(CMD_WRDI, 0, 0, 0);
  508. esp_rom_spiflash_wait_idle(&g_rom_flashchip);
  509. return err;
  510. }
  511. #ifndef g_rom_spiflash_dummy_len_plus // ESP32-C3 uses a macro to access ROM data here
  512. extern uint8_t g_rom_spiflash_dummy_len_plus[];
  513. #endif
  514. uint32_t IRAM_ATTR bootloader_execute_flash_command(uint8_t command, uint32_t mosi_data, uint8_t mosi_len, uint8_t miso_len)
  515. {
  516. uint32_t old_ctrl_reg = SPIFLASH.ctrl.val;
  517. #if CONFIG_IDF_TARGET_ESP32
  518. SPIFLASH.ctrl.val = SPI_WP_REG_M; // keep WP high while idle, otherwise leave DIO mode
  519. #else
  520. SPIFLASH.ctrl.val = SPI_MEM_WP_REG_M; // keep WP high while idle, otherwise leave DIO mode
  521. #endif
  522. SPIFLASH.user.usr_dummy = 0;
  523. SPIFLASH.user.usr_addr = 0;
  524. SPIFLASH.user.usr_command = 1;
  525. SPIFLASH.user2.usr_command_bitlen = 7;
  526. SPIFLASH.user2.usr_command_value = command;
  527. SPIFLASH.user.usr_miso = miso_len > 0;
  528. #if CONFIG_IDF_TARGET_ESP32
  529. SPIFLASH.miso_dlen.usr_miso_dbitlen = miso_len ? (miso_len - 1) : 0;
  530. #else
  531. SPIFLASH.miso_dlen.usr_miso_bit_len = miso_len ? (miso_len - 1) : 0;
  532. #endif
  533. SPIFLASH.user.usr_mosi = mosi_len > 0;
  534. #if CONFIG_IDF_TARGET_ESP32
  535. SPIFLASH.mosi_dlen.usr_mosi_dbitlen = mosi_len ? (mosi_len - 1) : 0;
  536. #else
  537. SPIFLASH.mosi_dlen.usr_mosi_bit_len = mosi_len ? (mosi_len - 1) : 0;
  538. #endif
  539. SPIFLASH.data_buf[0] = mosi_data;
  540. if (g_rom_spiflash_dummy_len_plus[1]) {
  541. /* When flash pins are mapped via GPIO matrix, need a dummy cycle before reading via MISO */
  542. if (miso_len > 0) {
  543. SPIFLASH.user.usr_dummy = 1;
  544. SPIFLASH.user1.usr_dummy_cyclelen = g_rom_spiflash_dummy_len_plus[1] - 1;
  545. } else {
  546. SPIFLASH.user.usr_dummy = 0;
  547. SPIFLASH.user1.usr_dummy_cyclelen = 0;
  548. }
  549. }
  550. SPIFLASH.cmd.usr = 1;
  551. while (SPIFLASH.cmd.usr != 0) {
  552. }
  553. SPIFLASH.ctrl.val = old_ctrl_reg;
  554. return SPIFLASH.data_buf[0];
  555. }
  556. void bootloader_enable_wp(void)
  557. {
  558. bootloader_execute_flash_command(CMD_WRDI, 0, 0, 0); /* Exit OTP mode */
  559. }
  560. #if SOC_CACHE_SUPPORT_WRAP
  561. esp_err_t bootloader_flash_wrap_set(spi_flash_wrap_mode_t mode)
  562. {
  563. uint32_t reg_bkp_ctrl = SPIFLASH.ctrl.val;
  564. uint32_t reg_bkp_usr = SPIFLASH.user.val;
  565. SPIFLASH.user.fwrite_dio = 0;
  566. SPIFLASH.user.fwrite_dual = 0;
  567. SPIFLASH.user.fwrite_qio = 1;
  568. SPIFLASH.user.fwrite_quad = 0;
  569. SPIFLASH.ctrl.fcmd_dual = 0;
  570. SPIFLASH.ctrl.fcmd_quad = 0;
  571. SPIFLASH.user.usr_dummy = 0;
  572. SPIFLASH.user.usr_addr = 1;
  573. SPIFLASH.user.usr_command = 1;
  574. SPIFLASH.user2.usr_command_bitlen = 7;
  575. SPIFLASH.user2.usr_command_value = CMD_WRAP;
  576. SPIFLASH.user1.usr_addr_bitlen = 23;
  577. SPIFLASH.addr = 0;
  578. SPIFLASH.user.usr_miso = 0;
  579. SPIFLASH.user.usr_mosi = 1;
  580. SPIFLASH.mosi_dlen.usr_mosi_bit_len = 7;
  581. SPIFLASH.data_buf[0] = (uint32_t) mode << 4;;
  582. SPIFLASH.cmd.usr = 1;
  583. while(SPIFLASH.cmd.usr != 0)
  584. { }
  585. SPIFLASH.ctrl.val = reg_bkp_ctrl;
  586. SPIFLASH.user.val = reg_bkp_usr;
  587. return ESP_OK;
  588. }
  589. #endif //SOC_CACHE_SUPPORT_WRAP