bootloader_flash.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stddef.h>
  14. #include <bootloader_flash_priv.h>
  15. #include <esp_log.h>
  16. #include <esp_flash_encrypt.h>
  17. #include "sdkconfig.h"
  18. #include "soc/soc_caps.h"
  19. #if CONFIG_IDF_TARGET_ESP32
  20. # include "soc/spi_struct.h"
  21. # include "soc/spi_reg.h"
  22. /* SPI flash controller */
  23. # define SPIFLASH SPI1
  24. #else
  25. # include "soc/spi_mem_struct.h"
  26. # include "soc/spi_mem_reg.h"
  27. /* SPI flash controller */
  28. # define SPIFLASH SPIMEM1
  29. #endif
  30. #if CONFIG_IDF_TARGET_ESP32S2
  31. #include "esp32s2/rom/spi_flash.h"
  32. #elif CONFIG_IDF_TARGET_ESP32S3
  33. #include "esp32s3/rom/spi_flash.h"
  34. #elif CONFIG_IDF_TARGET_ESP32C3
  35. #include "esp32c3/rom/spi_flash.h"
  36. #endif
  37. #ifndef BOOTLOADER_BUILD
  38. /* Normal app version maps to esp_spi_flash.h operations...
  39. */
  40. static const char *TAG = "bootloader_mmap";
  41. static spi_flash_mmap_handle_t map;
  42. uint32_t bootloader_mmap_get_free_pages(void)
  43. {
  44. return spi_flash_mmap_get_free_pages(SPI_FLASH_MMAP_DATA);
  45. }
  46. const void *bootloader_mmap(uint32_t src_addr, uint32_t size)
  47. {
  48. if (map) {
  49. ESP_LOGE(TAG, "tried to bootloader_mmap twice");
  50. return NULL; /* existing mapping in use... */
  51. }
  52. const void *result = NULL;
  53. uint32_t src_page = src_addr & ~(SPI_FLASH_MMU_PAGE_SIZE - 1);
  54. size += (src_addr - src_page);
  55. esp_err_t err = spi_flash_mmap(src_page, size, SPI_FLASH_MMAP_DATA, &result, &map);
  56. if (err != ESP_OK) {
  57. ESP_LOGE(TAG, "spi_flash_mmap failed: 0x%x", err);
  58. return NULL;
  59. }
  60. return (void *)((intptr_t)result + (src_addr - src_page));
  61. }
  62. void bootloader_munmap(const void *mapping)
  63. {
  64. if (mapping && map) {
  65. spi_flash_munmap(map);
  66. }
  67. map = 0;
  68. }
  69. esp_err_t bootloader_flash_read(size_t src, void *dest, size_t size, bool allow_decrypt)
  70. {
  71. if (allow_decrypt && esp_flash_encryption_enabled()) {
  72. return spi_flash_read_encrypted(src, dest, size);
  73. } else {
  74. return spi_flash_read(src, dest, size);
  75. }
  76. }
  77. esp_err_t bootloader_flash_write(size_t dest_addr, void *src, size_t size, bool write_encrypted)
  78. {
  79. if (write_encrypted) {
  80. #if CONFIG_IDF_TARGET_ESP32
  81. return spi_flash_write_encrypted(dest_addr, src, size);
  82. #else
  83. return esp_rom_spiflash_write_encrypted(dest_addr, src, size);
  84. #endif
  85. } else {
  86. return spi_flash_write(dest_addr, src, size);
  87. }
  88. }
  89. esp_err_t bootloader_flash_erase_sector(size_t sector)
  90. {
  91. return spi_flash_erase_sector(sector);
  92. }
  93. esp_err_t bootloader_flash_erase_range(uint32_t start_addr, uint32_t size)
  94. {
  95. return spi_flash_erase_range(start_addr, size);
  96. }
  97. #else
  98. /* Bootloader version, uses ROM functions only */
  99. #if CONFIG_IDF_TARGET_ESP32
  100. #include "esp32/rom/spi_flash.h"
  101. #include "esp32/rom/cache.h"
  102. #elif CONFIG_IDF_TARGET_ESP32S2
  103. #include "esp32s2/rom/spi_flash.h"
  104. #include "esp32s2/rom/cache.h"
  105. #include "soc/cache_memory.h"
  106. #elif CONFIG_IDF_TARGET_ESP32S3
  107. #include "esp32s3/rom/spi_flash.h"
  108. #include "esp32s3/rom/cache.h"
  109. #include "soc/cache_memory.h"
  110. #elif CONFIG_IDF_TARGET_ESP32C3
  111. #include "esp32c3/rom/spi_flash.h"
  112. #include "esp32c3/rom/cache.h"
  113. #include "soc/cache_memory.h"
  114. #endif
  115. static const char *TAG = "bootloader_flash";
  116. #if CONFIG_IDF_TARGET_ESP32
  117. /* Use first 50 blocks in MMU for bootloader_mmap,
  118. 50th block for bootloader_flash_read
  119. */
  120. #define MMU_BLOCK0_VADDR SOC_DROM_LOW
  121. #define MMU_SIZE (0x320000)
  122. #define MMU_BLOCK50_VADDR (MMU_BLOCK0_VADDR + MMU_SIZE)
  123. #define FLASH_READ_VADDR MMU_BLOCK50_VADDR
  124. #else // !CONFIG_IDF_TARGET_ESP32
  125. /* Use first 63 blocks in MMU for bootloader_mmap,
  126. 63th block for bootloader_flash_read
  127. */
  128. #define MMU_BLOCK0_VADDR SOC_DROM_LOW
  129. #define MMU_SIZE (0x3f0000)
  130. #define MMU_BLOCK63_VADDR (MMU_BLOCK0_VADDR + MMU_SIZE)
  131. #define FLASH_READ_VADDR MMU_BLOCK63_VADDR
  132. #endif
  133. #define MMU_FREE_PAGES (MMU_SIZE / FLASH_BLOCK_SIZE)
  134. static bool mapped;
  135. // Current bootloader mapping (ab)used for bootloader_read()
  136. static uint32_t current_read_mapping = UINT32_MAX;
  137. uint32_t bootloader_mmap_get_free_pages(void)
  138. {
  139. /**
  140. * Allow mapping up to 50 of the 51 available MMU blocks (last one used for reads)
  141. * Since, bootloader_mmap function below assumes it to be 0x320000 (50 pages), we can safely do this.
  142. */
  143. return MMU_FREE_PAGES;
  144. }
  145. const void *bootloader_mmap(uint32_t src_addr, uint32_t size)
  146. {
  147. if (mapped) {
  148. ESP_LOGE(TAG, "tried to bootloader_mmap twice");
  149. return NULL; /* can't map twice */
  150. }
  151. if (size > MMU_SIZE) {
  152. ESP_LOGE(TAG, "bootloader_mmap excess size %x", size);
  153. return NULL;
  154. }
  155. uint32_t src_addr_aligned = src_addr & MMU_FLASH_MASK;
  156. uint32_t count = bootloader_cache_pages_to_map(size, src_addr);
  157. #if CONFIG_IDF_TARGET_ESP32
  158. Cache_Read_Disable(0);
  159. Cache_Flush(0);
  160. #elif CONFIG_IDF_TARGET_ESP32S2
  161. uint32_t autoload = Cache_Suspend_ICache();
  162. Cache_Invalidate_ICache_All();
  163. #elif CONFIG_IDF_TARGET_ESP32S3
  164. uint32_t autoload = Cache_Suspend_DCache();
  165. Cache_Invalidate_DCache_All();
  166. #elif CONFIG_IDF_TARGET_ESP32C3
  167. uint32_t autoload = Cache_Suspend_ICache();
  168. Cache_Invalidate_ICache_All();
  169. #endif
  170. ESP_LOGD(TAG, "mmu set paddr=%08x count=%d size=%x src_addr=%x src_addr_aligned=%x",
  171. src_addr & MMU_FLASH_MASK, count, size, src_addr, src_addr_aligned );
  172. #if CONFIG_IDF_TARGET_ESP32
  173. int e = cache_flash_mmu_set(0, 0, MMU_BLOCK0_VADDR, src_addr_aligned, 64, count);
  174. #elif CONFIG_IDF_TARGET_ESP32S2
  175. int e = Cache_Ibus_MMU_Set(MMU_ACCESS_FLASH, MMU_BLOCK0_VADDR, src_addr_aligned, 64, count, 0);
  176. #else // S3, C3
  177. int e = Cache_Dbus_MMU_Set(MMU_ACCESS_FLASH, MMU_BLOCK0_VADDR, src_addr_aligned, 64, count, 0);
  178. #endif
  179. if (e != 0) {
  180. ESP_LOGE(TAG, "cache_flash_mmu_set failed: %d\n", e);
  181. #if CONFIG_IDF_TARGET_ESP32
  182. Cache_Read_Enable(0);
  183. #elif CONFIG_IDF_TARGET_ESP32S2
  184. Cache_Resume_ICache(autoload);
  185. #elif CONFIG_IDF_TARGET_ESP32S3
  186. Cache_Resume_DCache(autoload);
  187. #elif CONFIG_IDF_TARGET_ESP32C3
  188. Cache_Resume_ICache(autoload);
  189. #endif
  190. return NULL;
  191. }
  192. #if CONFIG_IDF_TARGET_ESP32
  193. Cache_Read_Enable(0);
  194. #elif CONFIG_IDF_TARGET_ESP32S2
  195. Cache_Resume_ICache(autoload);
  196. #elif CONFIG_IDF_TARGET_ESP32S3
  197. Cache_Resume_DCache(autoload);
  198. #elif CONFIG_IDF_TARGET_ESP32C3
  199. Cache_Resume_ICache(autoload);
  200. #endif
  201. mapped = true;
  202. return (void *)(MMU_BLOCK0_VADDR + (src_addr - src_addr_aligned));
  203. }
  204. void bootloader_munmap(const void *mapping)
  205. {
  206. if (mapped) {
  207. #if CONFIG_IDF_TARGET_ESP32
  208. /* Full MMU reset */
  209. Cache_Read_Disable(0);
  210. Cache_Flush(0);
  211. mmu_init(0);
  212. #elif CONFIG_IDF_TARGET_ESP32S2
  213. //TODO, save the autoload value.
  214. Cache_Suspend_ICache();
  215. Cache_Invalidate_ICache_All();
  216. Cache_MMU_Init();
  217. #elif CONFIG_IDF_TARGET_ESP32S3
  218. Cache_Suspend_DCache();
  219. Cache_Invalidate_DCache_All();
  220. Cache_MMU_Init();
  221. #elif CONFIG_IDF_TARGET_ESP32C3
  222. //TODO, save the autoload value.
  223. Cache_Suspend_ICache();
  224. Cache_Invalidate_ICache_All();
  225. Cache_MMU_Init();
  226. #endif
  227. mapped = false;
  228. current_read_mapping = UINT32_MAX;
  229. }
  230. }
  231. static esp_err_t spi_to_esp_err(esp_rom_spiflash_result_t r)
  232. {
  233. switch (r) {
  234. case ESP_ROM_SPIFLASH_RESULT_OK:
  235. return ESP_OK;
  236. case ESP_ROM_SPIFLASH_RESULT_ERR:
  237. return ESP_ERR_FLASH_OP_FAIL;
  238. case ESP_ROM_SPIFLASH_RESULT_TIMEOUT:
  239. return ESP_ERR_FLASH_OP_TIMEOUT;
  240. default:
  241. return ESP_FAIL;
  242. }
  243. }
  244. static esp_err_t bootloader_flash_read_no_decrypt(size_t src_addr, void *dest, size_t size)
  245. {
  246. #if CONFIG_IDF_TARGET_ESP32
  247. Cache_Read_Disable(0);
  248. Cache_Flush(0);
  249. #elif CONFIG_IDF_TARGET_ESP32S2
  250. uint32_t autoload = Cache_Suspend_ICache();
  251. #elif CONFIG_IDF_TARGET_ESP32S3
  252. uint32_t autoload = Cache_Suspend_DCache();
  253. #elif CONFIG_IDF_TARGET_ESP32C3
  254. uint32_t autoload = Cache_Suspend_ICache();
  255. #endif
  256. esp_rom_spiflash_result_t r = esp_rom_spiflash_read(src_addr, dest, size);
  257. #if CONFIG_IDF_TARGET_ESP32
  258. Cache_Read_Enable(0);
  259. #elif CONFIG_IDF_TARGET_ESP32S2
  260. Cache_Resume_ICache(autoload);
  261. #elif CONFIG_IDF_TARGET_ESP32S3
  262. Cache_Resume_DCache(autoload);
  263. #elif CONFIG_IDF_TARGET_ESP32C3
  264. Cache_Resume_ICache(autoload);
  265. #endif
  266. return spi_to_esp_err(r);
  267. }
  268. static esp_err_t bootloader_flash_read_allow_decrypt(size_t src_addr, void *dest, size_t size)
  269. {
  270. uint32_t *dest_words = (uint32_t *)dest;
  271. for (size_t word = 0; word < size / 4; word++) {
  272. uint32_t word_src = src_addr + word * 4; /* Read this offset from flash */
  273. uint32_t map_at = word_src & MMU_FLASH_MASK; /* Map this 64KB block from flash */
  274. uint32_t *map_ptr;
  275. if (map_at != current_read_mapping) {
  276. /* Move the 64KB mmu mapping window to fit map_at */
  277. #if CONFIG_IDF_TARGET_ESP32
  278. Cache_Read_Disable(0);
  279. Cache_Flush(0);
  280. #elif CONFIG_IDF_TARGET_ESP32S2
  281. uint32_t autoload = Cache_Suspend_ICache();
  282. Cache_Invalidate_ICache_All();
  283. #elif CONFIG_IDF_TARGET_ESP32S3
  284. uint32_t autoload = Cache_Suspend_DCache();
  285. Cache_Invalidate_DCache_All();
  286. #elif CONFIG_IDF_TARGET_ESP32C3
  287. uint32_t autoload = Cache_Suspend_ICache();
  288. Cache_Invalidate_ICache_All();
  289. #endif
  290. ESP_LOGD(TAG, "mmu set block paddr=0x%08x (was 0x%08x)", map_at, current_read_mapping);
  291. #if CONFIG_IDF_TARGET_ESP32
  292. int e = cache_flash_mmu_set(0, 0, FLASH_READ_VADDR, map_at, 64, 1);
  293. #elif CONFIG_IDF_TARGET_ESP32S2
  294. int e = Cache_Ibus_MMU_Set(MMU_ACCESS_FLASH, MMU_BLOCK63_VADDR, map_at, 64, 1, 0);
  295. #elif CONFIG_IDF_TARGET_ESP32S3
  296. int e = Cache_Dbus_MMU_Set(MMU_ACCESS_FLASH, MMU_BLOCK63_VADDR, map_at, 64, 1, 0);
  297. #elif CONFIG_IDF_TARGET_ESP32C3
  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 CONFIG_IDF_TARGET_ESP32S2
  305. Cache_Resume_ICache(autoload);
  306. #elif CONFIG_IDF_TARGET_ESP32S3
  307. Cache_Resume_DCache(autoload);
  308. #elif CONFIG_IDF_TARGET_ESP32C3
  309. Cache_Resume_ICache(autoload);
  310. #endif
  311. return ESP_FAIL;
  312. }
  313. current_read_mapping = map_at;
  314. #if CONFIG_IDF_TARGET_ESP32
  315. Cache_Read_Enable(0);
  316. #elif CONFIG_IDF_TARGET_ESP32S2
  317. Cache_Resume_ICache(autoload);
  318. #elif CONFIG_IDF_TARGET_ESP32S3
  319. Cache_Resume_DCache(autoload);
  320. #elif CONFIG_IDF_TARGET_ESP32C3
  321. Cache_Resume_ICache(autoload);
  322. #endif
  323. }
  324. map_ptr = (uint32_t *)(FLASH_READ_VADDR + (word_src - map_at));
  325. dest_words[word] = *map_ptr;
  326. }
  327. return ESP_OK;
  328. }
  329. esp_err_t bootloader_flash_read(size_t src_addr, void *dest, size_t size, bool allow_decrypt)
  330. {
  331. if (src_addr & 3) {
  332. ESP_LOGE(TAG, "bootloader_flash_read src_addr 0x%x not 4-byte aligned", src_addr);
  333. return ESP_FAIL;
  334. }
  335. if (size & 3) {
  336. ESP_LOGE(TAG, "bootloader_flash_read size 0x%x not 4-byte aligned", size);
  337. return ESP_FAIL;
  338. }
  339. if ((intptr_t)dest & 3) {
  340. ESP_LOGE(TAG, "bootloader_flash_read dest 0x%x not 4-byte aligned", (intptr_t)dest);
  341. return ESP_FAIL;
  342. }
  343. if (allow_decrypt) {
  344. return bootloader_flash_read_allow_decrypt(src_addr, dest, size);
  345. } else {
  346. return bootloader_flash_read_no_decrypt(src_addr, dest, size);
  347. }
  348. }
  349. esp_err_t bootloader_flash_write(size_t dest_addr, void *src, size_t size, bool write_encrypted)
  350. {
  351. esp_err_t err;
  352. size_t alignment = write_encrypted ? 32 : 4;
  353. if ((dest_addr % alignment) != 0) {
  354. ESP_LOGE(TAG, "bootloader_flash_write dest_addr 0x%x not %d-byte aligned", dest_addr, alignment);
  355. return ESP_FAIL;
  356. }
  357. if ((size % alignment) != 0) {
  358. ESP_LOGE(TAG, "bootloader_flash_write size 0x%x not %d-byte aligned", size, alignment);
  359. return ESP_FAIL;
  360. }
  361. if (((intptr_t)src % 4) != 0) {
  362. ESP_LOGE(TAG, "bootloader_flash_write src 0x%x not 4 byte aligned", (intptr_t)src);
  363. return ESP_FAIL;
  364. }
  365. err = spi_to_esp_err(esp_rom_spiflash_unlock());
  366. if (err != ESP_OK) {
  367. return err;
  368. }
  369. if (write_encrypted) {
  370. return spi_to_esp_err(esp_rom_spiflash_write_encrypted(dest_addr, src, size));
  371. } else {
  372. return spi_to_esp_err(esp_rom_spiflash_write(dest_addr, src, size));
  373. }
  374. }
  375. esp_err_t bootloader_flash_erase_sector(size_t sector)
  376. {
  377. return spi_to_esp_err(esp_rom_spiflash_erase_sector(sector));
  378. }
  379. esp_err_t bootloader_flash_erase_range(uint32_t start_addr, uint32_t size)
  380. {
  381. if (start_addr % FLASH_SECTOR_SIZE != 0) {
  382. return ESP_ERR_INVALID_ARG;
  383. }
  384. if (size % FLASH_SECTOR_SIZE != 0) {
  385. return ESP_ERR_INVALID_SIZE;
  386. }
  387. size_t start = start_addr / FLASH_SECTOR_SIZE;
  388. size_t end = start + size / FLASH_SECTOR_SIZE;
  389. const size_t sectors_per_block = FLASH_BLOCK_SIZE / FLASH_SECTOR_SIZE;
  390. esp_rom_spiflash_result_t rc = ESP_ROM_SPIFLASH_RESULT_OK;
  391. for (size_t sector = start; sector != end && rc == ESP_ROM_SPIFLASH_RESULT_OK; ) {
  392. if (sector % sectors_per_block == 0 && end - sector >= sectors_per_block) {
  393. rc = esp_rom_spiflash_erase_block(sector / sectors_per_block);
  394. sector += sectors_per_block;
  395. } else {
  396. rc = esp_rom_spiflash_erase_sector(sector);
  397. ++sector;
  398. }
  399. }
  400. return spi_to_esp_err(rc);
  401. }
  402. #endif
  403. #ifndef g_rom_spiflash_dummy_len_plus // ESP32-C3 uses a macro to access ROM data here
  404. extern uint8_t g_rom_spiflash_dummy_len_plus[];
  405. #endif
  406. uint32_t bootloader_execute_flash_command(uint8_t command, uint32_t mosi_data, uint8_t mosi_len, uint8_t miso_len)
  407. {
  408. uint32_t old_ctrl_reg = SPIFLASH.ctrl.val;
  409. #if CONFIG_IDF_TARGET_ESP32
  410. SPIFLASH.ctrl.val = SPI_WP_REG_M; // keep WP high while idle, otherwise leave DIO mode
  411. #else
  412. SPIFLASH.ctrl.val = SPI_MEM_WP_REG_M; // keep WP high while idle, otherwise leave DIO mode
  413. #endif
  414. SPIFLASH.user.usr_dummy = 0;
  415. SPIFLASH.user.usr_addr = 0;
  416. SPIFLASH.user.usr_command = 1;
  417. SPIFLASH.user2.usr_command_bitlen = 7;
  418. SPIFLASH.user2.usr_command_value = command;
  419. SPIFLASH.user.usr_miso = miso_len > 0;
  420. #if CONFIG_IDF_TARGET_ESP32
  421. SPIFLASH.miso_dlen.usr_miso_dbitlen = miso_len ? (miso_len - 1) : 0;
  422. #else
  423. SPIFLASH.miso_dlen.usr_miso_bit_len = miso_len ? (miso_len - 1) : 0;
  424. #endif
  425. SPIFLASH.user.usr_mosi = mosi_len > 0;
  426. #if CONFIG_IDF_TARGET_ESP32
  427. SPIFLASH.mosi_dlen.usr_mosi_dbitlen = mosi_len ? (mosi_len - 1) : 0;
  428. #else
  429. SPIFLASH.mosi_dlen.usr_mosi_bit_len = mosi_len ? (mosi_len - 1) : 0;
  430. #endif
  431. SPIFLASH.data_buf[0] = mosi_data;
  432. if (g_rom_spiflash_dummy_len_plus[1]) {
  433. /* When flash pins are mapped via GPIO matrix, need a dummy cycle before reading via MISO */
  434. if (miso_len > 0) {
  435. SPIFLASH.user.usr_dummy = 1;
  436. SPIFLASH.user1.usr_dummy_cyclelen = g_rom_spiflash_dummy_len_plus[1] - 1;
  437. } else {
  438. SPIFLASH.user.usr_dummy = 0;
  439. SPIFLASH.user1.usr_dummy_cyclelen = 0;
  440. }
  441. }
  442. SPIFLASH.cmd.usr = 1;
  443. while (SPIFLASH.cmd.usr != 0) {
  444. }
  445. SPIFLASH.ctrl.val = old_ctrl_reg;
  446. return SPIFLASH.data_buf[0];
  447. }
  448. void bootloader_enable_wp(void)
  449. {
  450. bootloader_execute_flash_command(CMD_WRDI, 0, 0, 0); /* Exit OTP mode */
  451. }
  452. #if SOC_CACHE_SUPPORT_WRAP
  453. esp_err_t bootloader_flash_wrap_set(spi_flash_wrap_mode_t mode)
  454. {
  455. uint32_t reg_bkp_ctrl = SPIFLASH.ctrl.val;
  456. uint32_t reg_bkp_usr = SPIFLASH.user.val;
  457. SPIFLASH.user.fwrite_dio = 0;
  458. SPIFLASH.user.fwrite_dual = 0;
  459. SPIFLASH.user.fwrite_qio = 1;
  460. SPIFLASH.user.fwrite_quad = 0;
  461. SPIFLASH.ctrl.fcmd_dual = 0;
  462. SPIFLASH.ctrl.fcmd_quad = 0;
  463. SPIFLASH.user.usr_dummy = 0;
  464. SPIFLASH.user.usr_addr = 1;
  465. SPIFLASH.user.usr_command = 1;
  466. SPIFLASH.user2.usr_command_bitlen = 7;
  467. SPIFLASH.user2.usr_command_value = CMD_WRAP;
  468. SPIFLASH.user1.usr_addr_bitlen = 23;
  469. SPIFLASH.addr = 0;
  470. SPIFLASH.user.usr_miso = 0;
  471. SPIFLASH.user.usr_mosi = 1;
  472. SPIFLASH.mosi_dlen.usr_mosi_bit_len = 7;
  473. SPIFLASH.data_buf[0] = (uint32_t) mode << 4;;
  474. SPIFLASH.cmd.usr = 1;
  475. while(SPIFLASH.cmd.usr != 0)
  476. { }
  477. SPIFLASH.ctrl.val = reg_bkp_ctrl;
  478. SPIFLASH.user.val = reg_bkp_usr;
  479. return ESP_OK;
  480. }
  481. #endif //SOC_CACHE_SUPPORT_WRAP