flash_ops.c 23 KB

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