flash_ops.c 23 KB

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