flash_ops.c 23 KB

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