flash_ops.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdlib.h>
  15. #include <assert.h>
  16. #include <string.h>
  17. #include <stdio.h>
  18. #include <sys/param.h> // For MIN/MAX(a, b)
  19. #include <freertos/FreeRTOS.h>
  20. #include <freertos/task.h>
  21. #include <freertos/semphr.h>
  22. #include <soc/soc.h>
  23. #include <soc/soc_memory_layout.h>
  24. #include "sdkconfig.h"
  25. #include "esp_attr.h"
  26. #include "esp_spi_flash.h"
  27. #include "esp_log.h"
  28. #include "esp_private/system_internal.h"
  29. #if CONFIG_IDF_TARGET_ESP32
  30. #include "esp32/rom/cache.h"
  31. #include "esp32/rom/spi_flash.h"
  32. #include "esp32/clk.h"
  33. #elif CONFIG_IDF_TARGET_ESP32S2
  34. #include "esp32s2/rom/cache.h"
  35. #include "esp32s2/rom/spi_flash.h"
  36. #include "esp32s2/clk.h"
  37. #elif CONFIG_IDF_TARGET_ESP32S3
  38. #include "esp32s3/rom/spi_flash.h"
  39. #include "esp32s3/rom/cache.h"
  40. #include "esp32s3/clk.h"
  41. #elif CONFIG_IDF_TARGET_ESP32C3
  42. #include "esp32c3/rom/cache.h"
  43. #include "esp32c3/rom/spi_flash.h"
  44. #include "esp32c3/clk.h"
  45. #endif
  46. #include "esp_flash_partitions.h"
  47. #include "cache_utils.h"
  48. #include "esp_flash.h"
  49. #include "esp_attr.h"
  50. esp_rom_spiflash_result_t IRAM_ATTR spi_flash_write_encrypted_chip(size_t dest_addr, const void *src, size_t size);
  51. /* bytes erased by SPIEraseBlock() ROM function */
  52. #define BLOCK_ERASE_SIZE 65536
  53. /* Limit number of bytes written/read in a single SPI operation,
  54. as these operations disable all higher priority tasks from running.
  55. */
  56. #ifdef CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE
  57. #define MAX_WRITE_CHUNK CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE
  58. #else
  59. #define MAX_WRITE_CHUNK 8192
  60. #endif // CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE
  61. #define MAX_READ_CHUNK 16384
  62. static const char *TAG __attribute__((unused)) = "spi_flash";
  63. #if CONFIG_SPI_FLASH_ENABLE_COUNTERS
  64. static spi_flash_counters_t s_flash_stats;
  65. #define COUNTER_START() uint32_t ts_begin = cpu_hal_get_cycle_count()
  66. #define COUNTER_STOP(counter) \
  67. do{ \
  68. s_flash_stats.counter.count++; \
  69. s_flash_stats.counter.time += (cpu_hal_get_cycle_count() - ts_begin) / (esp_clk_cpu_freq() / 1000000); \
  70. } while(0)
  71. #define COUNTER_ADD_BYTES(counter, size) \
  72. do { \
  73. s_flash_stats.counter.bytes += size; \
  74. } while (0)
  75. #else
  76. #define COUNTER_START()
  77. #define COUNTER_STOP(counter)
  78. #define COUNTER_ADD_BYTES(counter, size)
  79. #endif //CONFIG_SPI_FLASH_ENABLE_COUNTERS
  80. static esp_err_t spi_flash_translate_rc(esp_rom_spiflash_result_t rc);
  81. static bool is_safe_write_address(size_t addr, size_t size);
  82. static void spi_flash_os_yield(void);
  83. const DRAM_ATTR spi_flash_guard_funcs_t g_flash_guard_default_ops = {
  84. .start = spi_flash_disable_interrupts_caches_and_other_cpu,
  85. .end = spi_flash_enable_interrupts_caches_and_other_cpu,
  86. .op_lock = spi_flash_op_lock,
  87. .op_unlock = spi_flash_op_unlock,
  88. #if !CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED
  89. .is_safe_write_address = is_safe_write_address,
  90. #endif
  91. .yield = spi_flash_os_yield,
  92. };
  93. const DRAM_ATTR spi_flash_guard_funcs_t g_flash_guard_no_os_ops = {
  94. .start = spi_flash_disable_interrupts_caches_and_other_cpu_no_os,
  95. .end = spi_flash_enable_interrupts_caches_no_os,
  96. .op_lock = NULL,
  97. .op_unlock = NULL,
  98. #if !CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED
  99. .is_safe_write_address = NULL,
  100. #endif
  101. .yield = NULL,
  102. };
  103. static const spi_flash_guard_funcs_t *s_flash_guard_ops;
  104. #ifdef CONFIG_SPI_FLASH_DANGEROUS_WRITE_ABORTS
  105. #define UNSAFE_WRITE_ADDRESS abort()
  106. #else
  107. #define UNSAFE_WRITE_ADDRESS return false
  108. #endif
  109. /* CHECK_WRITE_ADDRESS macro to fail writes which land in the
  110. bootloader, partition table, or running application region.
  111. */
  112. #if CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED
  113. #define CHECK_WRITE_ADDRESS(ADDR, SIZE)
  114. #else /* FAILS or ABORTS */
  115. #define CHECK_WRITE_ADDRESS(ADDR, SIZE) do { \
  116. if (s_flash_guard_ops && s_flash_guard_ops->is_safe_write_address && !s_flash_guard_ops->is_safe_write_address(ADDR, SIZE)) { \
  117. return ESP_ERR_INVALID_ARG; \
  118. } \
  119. } while(0)
  120. #endif // CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED
  121. static __attribute__((unused)) bool is_safe_write_address(size_t addr, size_t size)
  122. {
  123. if (!esp_partition_main_flash_region_safe(addr, size)) {
  124. UNSAFE_WRITE_ADDRESS;
  125. }
  126. return true;
  127. }
  128. void spi_flash_init(void)
  129. {
  130. spi_flash_init_lock();
  131. #if CONFIG_SPI_FLASH_ENABLE_COUNTERS
  132. spi_flash_reset_counters();
  133. #endif
  134. }
  135. void IRAM_ATTR spi_flash_guard_set(const spi_flash_guard_funcs_t *funcs)
  136. {
  137. s_flash_guard_ops = funcs;
  138. }
  139. const spi_flash_guard_funcs_t *IRAM_ATTR spi_flash_guard_get(void)
  140. {
  141. return s_flash_guard_ops;
  142. }
  143. size_t IRAM_ATTR spi_flash_get_chip_size(void)
  144. {
  145. return g_rom_flashchip.chip_size;
  146. }
  147. static inline void IRAM_ATTR spi_flash_guard_start(void)
  148. {
  149. if (s_flash_guard_ops && s_flash_guard_ops->start) {
  150. s_flash_guard_ops->start();
  151. }
  152. }
  153. static inline void IRAM_ATTR spi_flash_guard_end(void)
  154. {
  155. if (s_flash_guard_ops && s_flash_guard_ops->end) {
  156. s_flash_guard_ops->end();
  157. }
  158. }
  159. static inline void IRAM_ATTR spi_flash_guard_op_lock(void)
  160. {
  161. if (s_flash_guard_ops && s_flash_guard_ops->op_lock) {
  162. s_flash_guard_ops->op_lock();
  163. }
  164. }
  165. static inline void IRAM_ATTR spi_flash_guard_op_unlock(void)
  166. {
  167. if (s_flash_guard_ops && s_flash_guard_ops->op_unlock) {
  168. s_flash_guard_ops->op_unlock();
  169. }
  170. }
  171. static void IRAM_ATTR spi_flash_os_yield(void)
  172. {
  173. #ifdef CONFIG_SPI_FLASH_YIELD_DURING_ERASE
  174. vTaskDelay(CONFIG_SPI_FLASH_ERASE_YIELD_TICKS);
  175. #endif
  176. }
  177. #ifdef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  178. static esp_rom_spiflash_result_t IRAM_ATTR spi_flash_unlock(void)
  179. {
  180. static bool unlocked = false;
  181. if (!unlocked) {
  182. spi_flash_guard_start();
  183. esp_rom_spiflash_result_t rc = esp_rom_spiflash_unlock();
  184. spi_flash_guard_end();
  185. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  186. return rc;
  187. }
  188. unlocked = true;
  189. }
  190. return ESP_ROM_SPIFLASH_RESULT_OK;
  191. }
  192. #else
  193. static esp_rom_spiflash_result_t IRAM_ATTR spi_flash_unlock(void)
  194. {
  195. esp_err_t err = esp_flash_set_chip_write_protect(NULL, false);
  196. if (err != ESP_OK) {
  197. return ESP_ROM_SPIFLASH_RESULT_ERR;
  198. }
  199. return ESP_ROM_SPIFLASH_RESULT_OK;
  200. }
  201. #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  202. esp_err_t IRAM_ATTR spi_flash_erase_sector(size_t sec)
  203. {
  204. CHECK_WRITE_ADDRESS(sec * SPI_FLASH_SEC_SIZE, SPI_FLASH_SEC_SIZE);
  205. return spi_flash_erase_range(sec * SPI_FLASH_SEC_SIZE, SPI_FLASH_SEC_SIZE);
  206. }
  207. #ifdef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  208. //deprecated, only used in compatible mode
  209. esp_err_t IRAM_ATTR spi_flash_erase_range(size_t start_addr, size_t size)
  210. {
  211. CHECK_WRITE_ADDRESS(start_addr, size);
  212. if (start_addr % SPI_FLASH_SEC_SIZE != 0) {
  213. return ESP_ERR_INVALID_ARG;
  214. }
  215. if (size % SPI_FLASH_SEC_SIZE != 0) {
  216. return ESP_ERR_INVALID_SIZE;
  217. }
  218. if (size + start_addr > spi_flash_get_chip_size()) {
  219. return ESP_ERR_INVALID_SIZE;
  220. }
  221. size_t start = start_addr / SPI_FLASH_SEC_SIZE;
  222. size_t end = start + size / SPI_FLASH_SEC_SIZE;
  223. const size_t sectors_per_block = BLOCK_ERASE_SIZE / SPI_FLASH_SEC_SIZE;
  224. COUNTER_START();
  225. esp_rom_spiflash_result_t rc;
  226. rc = spi_flash_unlock();
  227. if (rc == ESP_ROM_SPIFLASH_RESULT_OK) {
  228. #ifdef CONFIG_SPI_FLASH_YIELD_DURING_ERASE
  229. int64_t no_yield_time_us = 0;
  230. #endif
  231. for (size_t sector = start; sector != end && rc == ESP_ROM_SPIFLASH_RESULT_OK; ) {
  232. #ifdef CONFIG_SPI_FLASH_YIELD_DURING_ERASE
  233. int64_t start_time_us = esp_system_get_time();
  234. #endif
  235. spi_flash_guard_start();
  236. #ifndef CONFIG_SPI_FLASH_BYPASS_BLOCK_ERASE
  237. if (sector % sectors_per_block == 0 && end - sector >= sectors_per_block) {
  238. rc = esp_rom_spiflash_erase_block(sector / sectors_per_block);
  239. sector += sectors_per_block;
  240. COUNTER_ADD_BYTES(erase, sectors_per_block * SPI_FLASH_SEC_SIZE);
  241. } else
  242. #endif
  243. {
  244. rc = esp_rom_spiflash_erase_sector(sector);
  245. ++sector;
  246. COUNTER_ADD_BYTES(erase, SPI_FLASH_SEC_SIZE);
  247. }
  248. spi_flash_guard_end();
  249. #ifdef CONFIG_SPI_FLASH_YIELD_DURING_ERASE
  250. no_yield_time_us += (esp_system_get_time() - start_time_us);
  251. if (no_yield_time_us / 1000 >= CONFIG_SPI_FLASH_ERASE_YIELD_DURATION_MS) {
  252. no_yield_time_us = 0;
  253. if (s_flash_guard_ops && s_flash_guard_ops->yield) {
  254. s_flash_guard_ops->yield();
  255. }
  256. }
  257. #endif
  258. }
  259. }
  260. COUNTER_STOP(erase);
  261. spi_flash_guard_start();
  262. // Ensure WEL is 0 after the operation, even if the erase failed.
  263. esp_rom_spiflash_write_disable();
  264. spi_flash_check_and_flush_cache(start_addr, size);
  265. spi_flash_guard_end();
  266. return spi_flash_translate_rc(rc);
  267. }
  268. /* Wrapper around esp_rom_spiflash_write() that verifies data as written if CONFIG_SPI_FLASH_VERIFY_WRITE is set.
  269. If CONFIG_SPI_FLASH_VERIFY_WRITE is not set, this is esp_rom_spiflash_write().
  270. */
  271. static IRAM_ATTR esp_rom_spiflash_result_t spi_flash_write_inner(uint32_t target, const uint32_t *src_addr, int32_t len)
  272. {
  273. #ifndef CONFIG_SPI_FLASH_VERIFY_WRITE
  274. return esp_rom_spiflash_write(target, src_addr, len);
  275. #else // CONFIG_SPI_FLASH_VERIFY_WRITE
  276. esp_rom_spiflash_result_t res = ESP_ROM_SPIFLASH_RESULT_OK;
  277. assert(len % sizeof(uint32_t) == 0);
  278. uint32_t before_buf[ESP_ROM_SPIFLASH_BUFF_BYTE_READ_NUM / sizeof(uint32_t)];
  279. uint32_t after_buf[ESP_ROM_SPIFLASH_BUFF_BYTE_READ_NUM / sizeof(uint32_t)];
  280. uint32_t *expected_buf = before_buf;
  281. int32_t remaining = len;
  282. for(int i = 0; i < len; i += sizeof(before_buf)) {
  283. int i_w = i / sizeof(uint32_t); // index in words (i is an index in bytes)
  284. int32_t read_len = MIN(sizeof(before_buf), remaining);
  285. // Read "before" contents from flash
  286. res = esp_rom_spiflash_read(target + i, before_buf, read_len);
  287. if (res != ESP_ROM_SPIFLASH_RESULT_OK) {
  288. break;
  289. }
  290. for (int r = 0; r < read_len; r += sizeof(uint32_t)) {
  291. int r_w = r / sizeof(uint32_t); // index in words (r is index in bytes)
  292. uint32_t write = src_addr[i_w + r_w];
  293. uint32_t before = before_buf[r_w];
  294. uint32_t expected = write & before;
  295. #ifdef CONFIG_SPI_FLASH_WARN_SETTING_ZERO_TO_ONE
  296. if ((before & write) != write) {
  297. spi_flash_guard_end();
  298. ESP_LOGW(TAG, "Write at offset 0x%x requests 0x%08x but will write 0x%08x -> 0x%08x",
  299. target + i + r, write, before, before & write);
  300. spi_flash_guard_start();
  301. }
  302. #endif
  303. expected_buf[r_w] = expected;
  304. }
  305. res = esp_rom_spiflash_write(target + i, &src_addr[i_w], read_len);
  306. if (res != ESP_ROM_SPIFLASH_RESULT_OK) {
  307. break;
  308. }
  309. res = esp_rom_spiflash_read(target + i, after_buf, read_len);
  310. if (res != ESP_ROM_SPIFLASH_RESULT_OK) {
  311. break;
  312. }
  313. for (int r = 0; r < read_len; r += sizeof(uint32_t)) {
  314. int r_w = r / sizeof(uint32_t); // index in words (r is index in bytes)
  315. uint32_t expected = expected_buf[r_w];
  316. uint32_t actual = after_buf[r_w];
  317. if (expected != actual) {
  318. #ifdef CONFIG_SPI_FLASH_LOG_FAILED_WRITE
  319. spi_flash_guard_end();
  320. ESP_LOGE(TAG, "Bad write at offset 0x%x expected 0x%08x readback 0x%08x", target + i + r, expected, actual);
  321. spi_flash_guard_start();
  322. #endif
  323. res = ESP_ROM_SPIFLASH_RESULT_ERR;
  324. }
  325. }
  326. if (res != ESP_ROM_SPIFLASH_RESULT_OK) {
  327. break;
  328. }
  329. remaining -= read_len;
  330. }
  331. return res;
  332. #endif // CONFIG_SPI_FLASH_VERIFY_WRITE
  333. }
  334. esp_err_t IRAM_ATTR spi_flash_write(size_t dst, const void *srcv, size_t size)
  335. {
  336. CHECK_WRITE_ADDRESS(dst, size);
  337. // Out of bound writes are checked in ROM code, but we can give better
  338. // error code here
  339. if (dst + size > g_rom_flashchip.chip_size) {
  340. return ESP_ERR_INVALID_SIZE;
  341. }
  342. if (size == 0) {
  343. return ESP_OK;
  344. }
  345. esp_rom_spiflash_result_t rc = ESP_ROM_SPIFLASH_RESULT_OK;
  346. COUNTER_START();
  347. const uint8_t *srcc = (const uint8_t *) srcv;
  348. /*
  349. * Large operations are split into (up to) 3 parts:
  350. * - Left padding: 4 bytes up to the first 4-byte aligned destination offset.
  351. * - Middle part
  352. * - Right padding: 4 bytes from the last 4-byte aligned offset covered.
  353. */
  354. size_t left_off = dst & ~3U;
  355. size_t left_size = MIN(((dst + 3) & ~3U) - dst, size);
  356. size_t mid_off = left_size;
  357. size_t mid_size = (size - left_size) & ~3U;
  358. size_t right_off = left_size + mid_size;
  359. size_t right_size = size - mid_size - left_size;
  360. rc = spi_flash_unlock();
  361. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  362. goto out;
  363. }
  364. if (left_size > 0) {
  365. uint32_t t = 0xffffffff;
  366. memcpy(((uint8_t *) &t) + (dst - left_off), srcc, left_size);
  367. spi_flash_guard_start();
  368. rc = spi_flash_write_inner(left_off, &t, 4);
  369. spi_flash_guard_end();
  370. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  371. goto out;
  372. }
  373. COUNTER_ADD_BYTES(write, 4);
  374. }
  375. if (mid_size > 0) {
  376. /* If src buffer is 4-byte aligned as well and is not in a region that requires cache access to be enabled, we
  377. * can write directly without buffering in RAM. */
  378. #ifdef ESP_PLATFORM
  379. bool direct_write = esp_ptr_internal(srcc)
  380. && esp_ptr_byte_accessible(srcc)
  381. && ((uintptr_t) srcc + mid_off) % 4 == 0;
  382. #else
  383. bool direct_write = true;
  384. #endif
  385. while(mid_size > 0 && rc == ESP_ROM_SPIFLASH_RESULT_OK) {
  386. uint32_t write_buf[8];
  387. uint32_t write_size = MIN(mid_size, MAX_WRITE_CHUNK);
  388. const uint8_t *write_src = srcc + mid_off;
  389. if (!direct_write) {
  390. write_size = MIN(write_size, sizeof(write_buf));
  391. memcpy(write_buf, write_src, write_size);
  392. write_src = (const uint8_t *)write_buf;
  393. }
  394. spi_flash_guard_start();
  395. rc = spi_flash_write_inner(dst + mid_off, (const uint32_t *) write_src, write_size);
  396. spi_flash_guard_end();
  397. COUNTER_ADD_BYTES(write, write_size);
  398. mid_size -= write_size;
  399. mid_off += write_size;
  400. }
  401. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  402. goto out;
  403. }
  404. }
  405. if (right_size > 0) {
  406. uint32_t t = 0xffffffff;
  407. memcpy(&t, srcc + right_off, right_size);
  408. spi_flash_guard_start();
  409. rc = spi_flash_write_inner(dst + right_off, &t, 4);
  410. spi_flash_guard_end();
  411. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  412. goto out;
  413. }
  414. COUNTER_ADD_BYTES(write, 4);
  415. }
  416. out:
  417. COUNTER_STOP(write);
  418. spi_flash_guard_start();
  419. // Ensure WEL is 0 after the operation, even if the write failed.
  420. esp_rom_spiflash_write_disable();
  421. spi_flash_check_and_flush_cache(dst, size);
  422. spi_flash_guard_end();
  423. return spi_flash_translate_rc(rc);
  424. }
  425. #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  426. esp_err_t IRAM_ATTR spi_flash_write_encrypted(size_t dest_addr, const void *src, size_t size)
  427. {
  428. esp_err_t err = ESP_OK;
  429. CHECK_WRITE_ADDRESS(dest_addr, size);
  430. if ((dest_addr % 16) != 0) {
  431. return ESP_ERR_INVALID_ARG;
  432. }
  433. if ((size % 16) != 0) {
  434. return ESP_ERR_INVALID_SIZE;
  435. }
  436. COUNTER_START();
  437. esp_rom_spiflash_result_t rc = spi_flash_unlock();
  438. err = spi_flash_translate_rc(rc);
  439. if (err != ESP_OK) {
  440. goto fail;
  441. }
  442. #ifndef CONFIG_SPI_FLASH_VERIFY_WRITE
  443. err = spi_flash_write_encrypted_chip(dest_addr, src, size);
  444. COUNTER_ADD_BYTES(write, size);
  445. spi_flash_guard_start();
  446. esp_rom_spiflash_write_disable();
  447. spi_flash_check_and_flush_cache(dest_addr, size);
  448. spi_flash_guard_end();
  449. #else
  450. const uint32_t* src_w = (const uint32_t*)src;
  451. uint32_t read_buf[ESP_ROM_SPIFLASH_BUFF_BYTE_READ_NUM / sizeof(uint32_t)];
  452. int32_t remaining = size;
  453. for(int i = 0; i < size; i += sizeof(read_buf)) {
  454. int i_w = i / sizeof(uint32_t); // index in words (i is an index in bytes)
  455. int32_t read_len = MIN(sizeof(read_buf), remaining);
  456. // Read "before" contents from flash
  457. esp_err_t err = spi_flash_read(dest_addr + i, read_buf, read_len);
  458. if (err != ESP_OK) {
  459. break;
  460. }
  461. #ifdef CONFIG_SPI_FLASH_WARN_SETTING_ZERO_TO_ONE
  462. //The written data cannot be predicted, so warning is shown if any of the bits is not 1.
  463. for (int r = 0; r < read_len; r += sizeof(uint32_t)) {
  464. uint32_t before = read_buf[r / sizeof(uint32_t)];
  465. if (before != 0xFFFFFFFF) {
  466. ESP_LOGW(TAG, "Encrypted write at offset 0x%x but not erased (0x%08x)",
  467. dest_addr + i + r, before);
  468. }
  469. }
  470. #endif
  471. err = spi_flash_write_encrypted_chip(dest_addr + i, src + i, read_len);
  472. if (err != ESP_OK) {
  473. break;
  474. }
  475. COUNTER_ADD_BYTES(write, size);
  476. spi_flash_guard_start();
  477. esp_rom_spiflash_write_disable();
  478. spi_flash_check_and_flush_cache(dest_addr, size);
  479. spi_flash_guard_end();
  480. err = spi_flash_read_encrypted(dest_addr + i, read_buf, read_len);
  481. if (err != ESP_OK) {
  482. break;
  483. }
  484. for (int r = 0; r < read_len; r += sizeof(uint32_t)) {
  485. int r_w = r / sizeof(uint32_t); // index in words (r is index in bytes)
  486. uint32_t expected = src_w[i_w + r_w];
  487. uint32_t actual = read_buf[r_w];
  488. if (expected != actual) {
  489. #ifdef CONFIG_SPI_FLASH_LOG_FAILED_WRITE
  490. ESP_LOGE(TAG, "Bad write at offset 0x%x expected 0x%08x readback 0x%08x", dest_addr + i + r, expected, actual);
  491. #endif
  492. err = ESP_FAIL;
  493. }
  494. }
  495. if (err != ESP_OK) {
  496. break;
  497. }
  498. remaining -= read_len;
  499. }
  500. #endif // CONFIG_SPI_FLASH_VERIFY_WRITE
  501. fail:
  502. COUNTER_STOP(write);
  503. return err;
  504. }
  505. #ifdef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  506. esp_err_t IRAM_ATTR spi_flash_read(size_t src, void *dstv, size_t size)
  507. {
  508. // Out of bound reads are checked in ROM code, but we can give better
  509. // error code here
  510. if (src + size > g_rom_flashchip.chip_size) {
  511. return ESP_ERR_INVALID_SIZE;
  512. }
  513. if (size == 0) {
  514. return ESP_OK;
  515. }
  516. esp_rom_spiflash_result_t rc = ESP_ROM_SPIFLASH_RESULT_OK;
  517. COUNTER_START();
  518. spi_flash_guard_start();
  519. /* To simplify boundary checks below, we handle small reads separately. */
  520. if (size < 16) {
  521. uint32_t t[6]; /* Enough for 16 bytes + 4 on either side for padding. */
  522. uint32_t read_src = src & ~3U;
  523. uint32_t left_off = src & 3U;
  524. uint32_t read_size = (left_off + size + 3) & ~3U;
  525. rc = esp_rom_spiflash_read(read_src, t, read_size);
  526. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  527. goto out;
  528. }
  529. COUNTER_ADD_BYTES(read, read_size);
  530. #ifdef ESP_PLATFORM
  531. if (esp_ptr_external_ram(dstv)) {
  532. spi_flash_guard_end();
  533. memcpy(dstv, ((uint8_t *) t) + left_off, size);
  534. spi_flash_guard_start();
  535. } else {
  536. memcpy(dstv, ((uint8_t *) t) + left_off, size);
  537. }
  538. #else
  539. memcpy(dstv, ((uint8_t *) t) + left_off, size);
  540. #endif
  541. goto out;
  542. }
  543. uint8_t *dstc = (uint8_t *) dstv;
  544. intptr_t dsti = (intptr_t) dstc;
  545. /*
  546. * Large operations are split into (up to) 3 parts:
  547. * - The middle part: from the first 4-aligned position in src to the first
  548. * 4-aligned position in dst.
  549. */
  550. size_t src_mid_off = (src % 4 == 0 ? 0 : 4 - (src % 4));
  551. size_t dst_mid_off = (dsti % 4 == 0 ? 0 : 4 - (dsti % 4));
  552. size_t mid_size = (size - MAX(src_mid_off, dst_mid_off)) & ~3U;
  553. /*
  554. * - Once the middle part is in place, src_mid_off bytes from the preceding
  555. * 4-aligned source location are added on the left.
  556. */
  557. size_t pad_left_src = src & ~3U;
  558. size_t pad_left_size = src_mid_off;
  559. /*
  560. * - Finally, the right part is added: from the end of the middle part to
  561. * the end. Depending on the alignment of source and destination, this may
  562. * be a 4 or 8 byte read from pad_right_src.
  563. */
  564. size_t pad_right_src = (src + pad_left_size + mid_size) & ~3U;
  565. size_t pad_right_off = (pad_right_src - src);
  566. size_t pad_right_size = (size - pad_right_off);
  567. #ifdef ESP_PLATFORM
  568. bool direct_read = esp_ptr_internal(dstc)
  569. && esp_ptr_byte_accessible(dstc)
  570. && ((uintptr_t) dstc + dst_mid_off) % 4 == 0;
  571. #else
  572. bool direct_read = true;
  573. #endif
  574. if (mid_size > 0) {
  575. uint32_t mid_remaining = mid_size;
  576. uint32_t mid_read = 0;
  577. while (mid_remaining > 0) {
  578. uint32_t read_size = MIN(mid_remaining, MAX_READ_CHUNK);
  579. uint32_t read_buf[8];
  580. uint8_t *read_dst_final = dstc + dst_mid_off + mid_read;
  581. uint8_t *read_dst = read_dst_final;
  582. if (!direct_read) {
  583. read_size = MIN(read_size, sizeof(read_buf));
  584. read_dst = (uint8_t *) read_buf;
  585. }
  586. rc = esp_rom_spiflash_read(src + src_mid_off + mid_read,
  587. (uint32_t *) read_dst, read_size);
  588. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  589. goto out;
  590. }
  591. mid_remaining -= read_size;
  592. mid_read += read_size;
  593. if (!direct_read) {
  594. spi_flash_guard_end();
  595. memcpy(read_dst_final, read_buf, read_size);
  596. spi_flash_guard_start();
  597. } else if (mid_remaining > 0) {
  598. /* Drop guard momentarily, allows other tasks to preempt */
  599. spi_flash_guard_end();
  600. spi_flash_guard_start();
  601. }
  602. }
  603. COUNTER_ADD_BYTES(read, mid_size);
  604. /*
  605. * If offsets in src and dst are different, perform an in-place shift
  606. * to put destination data into its final position.
  607. * Note that the shift can be left (src_mid_off < dst_mid_off) or right.
  608. */
  609. if (src_mid_off != dst_mid_off) {
  610. if (!direct_read) {
  611. spi_flash_guard_end();
  612. }
  613. memmove(dstc + src_mid_off, dstc + dst_mid_off, mid_size);
  614. if (!direct_read) {
  615. spi_flash_guard_start();
  616. }
  617. }
  618. }
  619. if (pad_left_size > 0) {
  620. uint32_t t;
  621. rc = esp_rom_spiflash_read(pad_left_src, &t, 4);
  622. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  623. goto out;
  624. }
  625. COUNTER_ADD_BYTES(read, 4);
  626. if (!direct_read) {
  627. spi_flash_guard_end();
  628. }
  629. memcpy(dstc, ((uint8_t *) &t) + (4 - pad_left_size), pad_left_size);
  630. if (!direct_read) {
  631. spi_flash_guard_start();
  632. }
  633. }
  634. if (pad_right_size > 0) {
  635. uint32_t t[2];
  636. int32_t read_size = (pad_right_size <= 4 ? 4 : 8);
  637. rc = esp_rom_spiflash_read(pad_right_src, t, read_size);
  638. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  639. goto out;
  640. }
  641. COUNTER_ADD_BYTES(read, read_size);
  642. if (!direct_read) {
  643. spi_flash_guard_end();
  644. }
  645. memcpy(dstc + pad_right_off, t, pad_right_size);
  646. if (!direct_read) {
  647. spi_flash_guard_start();
  648. }
  649. }
  650. out:
  651. spi_flash_guard_end();
  652. COUNTER_STOP(read);
  653. return spi_flash_translate_rc(rc);
  654. }
  655. #endif
  656. esp_err_t IRAM_ATTR spi_flash_read_encrypted(size_t src, void *dstv, size_t size)
  657. {
  658. if (src + size > g_rom_flashchip.chip_size) {
  659. return ESP_ERR_INVALID_SIZE;
  660. }
  661. if (size == 0) {
  662. return ESP_OK;
  663. }
  664. esp_err_t err;
  665. const uint8_t *map;
  666. spi_flash_mmap_handle_t map_handle;
  667. size_t map_src = src & ~(SPI_FLASH_MMU_PAGE_SIZE - 1);
  668. size_t map_size = size + (src - map_src);
  669. err = spi_flash_mmap(map_src, map_size, SPI_FLASH_MMAP_DATA, (const void **)&map, &map_handle);
  670. if (err != ESP_OK) {
  671. return err;
  672. }
  673. memcpy(dstv, map + (src - map_src), size);
  674. spi_flash_munmap(map_handle);
  675. return err;
  676. }
  677. static esp_err_t IRAM_ATTR spi_flash_translate_rc(esp_rom_spiflash_result_t rc)
  678. {
  679. switch (rc) {
  680. case ESP_ROM_SPIFLASH_RESULT_OK:
  681. return ESP_OK;
  682. case ESP_ROM_SPIFLASH_RESULT_TIMEOUT:
  683. return ESP_ERR_FLASH_OP_TIMEOUT;
  684. case ESP_ROM_SPIFLASH_RESULT_ERR:
  685. default:
  686. return ESP_ERR_FLASH_OP_FAIL;
  687. }
  688. }
  689. #if CONFIG_SPI_FLASH_ENABLE_COUNTERS
  690. static inline void dump_counter(spi_flash_counter_t *counter, const char *name)
  691. {
  692. ESP_LOGI(TAG, "%s count=%8d time=%8dus bytes=%8d\n", name,
  693. counter->count, counter->time, counter->bytes);
  694. }
  695. const spi_flash_counters_t *spi_flash_get_counters(void)
  696. {
  697. return &s_flash_stats;
  698. }
  699. void spi_flash_reset_counters(void)
  700. {
  701. memset(&s_flash_stats, 0, sizeof(s_flash_stats));
  702. }
  703. void spi_flash_dump_counters(void)
  704. {
  705. dump_counter(&s_flash_stats.read, "read ");
  706. dump_counter(&s_flash_stats.write, "write");
  707. dump_counter(&s_flash_stats.erase, "erase");
  708. }
  709. #endif //CONFIG_SPI_FLASH_ENABLE_COUNTERS
  710. #if CONFIG_SPI_FLASH_USE_LEGACY_IMPL && !CONFIG_IDF_TARGET_ESP32
  711. // TODO esp32s2: Remove once ESP32-S2 & later chips has new SPI Flash API support
  712. esp_flash_t *esp_flash_default_chip = NULL;
  713. #endif