flash_ops.c 25 KB

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