flash_ops.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  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. #include "soc/spi_mem_struct.h"
  38. #endif
  39. #include "esp_flash_partitions.h"
  40. #include "cache_utils.h"
  41. #include "esp_flash.h"
  42. #include "esp_attr.h"
  43. /* bytes erased by SPIEraseBlock() ROM function */
  44. #define BLOCK_ERASE_SIZE 65536
  45. /* Limit number of bytes written/read in a single SPI operation,
  46. as these operations disable all higher priority tasks from running.
  47. */
  48. #define MAX_WRITE_CHUNK 8192
  49. #define MAX_READ_CHUNK 16384
  50. static const char *TAG __attribute__((unused)) = "spi_flash";
  51. #if CONFIG_SPI_FLASH_ENABLE_COUNTERS
  52. static spi_flash_counters_t s_flash_stats;
  53. #define COUNTER_START() uint32_t ts_begin = xthal_get_ccount()
  54. #define COUNTER_STOP(counter) \
  55. do{ \
  56. s_flash_stats.counter.count++; \
  57. s_flash_stats.counter.time += (xthal_get_ccount() - ts_begin) / (esp_clk_cpu_freq() / 1000000); \
  58. } while(0)
  59. #define COUNTER_ADD_BYTES(counter, size) \
  60. do { \
  61. s_flash_stats.counter.bytes += size; \
  62. } while (0)
  63. #else
  64. #define COUNTER_START()
  65. #define COUNTER_STOP(counter)
  66. #define COUNTER_ADD_BYTES(counter, size)
  67. #endif //CONFIG_SPI_FLASH_ENABLE_COUNTERS
  68. static esp_err_t spi_flash_translate_rc(esp_rom_spiflash_result_t rc);
  69. static bool is_safe_write_address(size_t addr, size_t size);
  70. const DRAM_ATTR spi_flash_guard_funcs_t g_flash_guard_default_ops = {
  71. .start = spi_flash_disable_interrupts_caches_and_other_cpu,
  72. .end = spi_flash_enable_interrupts_caches_and_other_cpu,
  73. .op_lock = spi_flash_op_lock,
  74. .op_unlock = spi_flash_op_unlock,
  75. #if !CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED
  76. .is_safe_write_address = is_safe_write_address
  77. #endif
  78. };
  79. const DRAM_ATTR spi_flash_guard_funcs_t g_flash_guard_no_os_ops = {
  80. .start = spi_flash_disable_interrupts_caches_and_other_cpu_no_os,
  81. .end = spi_flash_enable_interrupts_caches_no_os,
  82. .op_lock = 0,
  83. .op_unlock = 0,
  84. #if !CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED
  85. .is_safe_write_address = 0
  86. #endif
  87. };
  88. static const spi_flash_guard_funcs_t *s_flash_guard_ops;
  89. #ifdef CONFIG_SPI_FLASH_DANGEROUS_WRITE_ABORTS
  90. #define UNSAFE_WRITE_ADDRESS abort()
  91. #else
  92. #define UNSAFE_WRITE_ADDRESS return false
  93. #endif
  94. /* CHECK_WRITE_ADDRESS macro to fail writes which land in the
  95. bootloader, partition table, or running application region.
  96. */
  97. #if CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED
  98. #define CHECK_WRITE_ADDRESS(ADDR, SIZE)
  99. #else /* FAILS or ABORTS */
  100. #define CHECK_WRITE_ADDRESS(ADDR, SIZE) do { \
  101. if (s_flash_guard_ops && s_flash_guard_ops->is_safe_write_address && !s_flash_guard_ops->is_safe_write_address(ADDR, SIZE)) { \
  102. return ESP_ERR_INVALID_ARG; \
  103. } \
  104. } while(0)
  105. #endif // CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED
  106. static __attribute__((unused)) bool is_safe_write_address(size_t addr, size_t size)
  107. {
  108. if (!esp_partition_main_flash_region_safe(addr, size)) {
  109. UNSAFE_WRITE_ADDRESS;
  110. }
  111. return true;
  112. }
  113. void spi_flash_init(void)
  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(void)
  125. {
  126. return s_flash_guard_ops;
  127. }
  128. size_t IRAM_ATTR spi_flash_get_chip_size(void)
  129. {
  130. return g_rom_flashchip.chip_size;
  131. }
  132. static inline void IRAM_ATTR spi_flash_guard_start(void)
  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(void)
  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(void)
  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(void)
  151. {
  152. if (s_flash_guard_ops && s_flash_guard_ops->op_unlock) {
  153. s_flash_guard_ops->op_unlock();
  154. }
  155. }
  156. #ifdef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  157. static esp_rom_spiflash_result_t IRAM_ATTR spi_flash_unlock(void)
  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. #else
  172. static esp_rom_spiflash_result_t IRAM_ATTR spi_flash_unlock(void)
  173. {
  174. esp_err_t err = esp_flash_set_chip_write_protect(NULL, false);
  175. if (err != ESP_OK) {
  176. return ESP_ROM_SPIFLASH_RESULT_ERR;
  177. }
  178. return ESP_ROM_SPIFLASH_RESULT_OK;
  179. }
  180. #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  181. esp_err_t IRAM_ATTR spi_flash_erase_sector(size_t sec)
  182. {
  183. CHECK_WRITE_ADDRESS(sec * SPI_FLASH_SEC_SIZE, SPI_FLASH_SEC_SIZE);
  184. return spi_flash_erase_range(sec * SPI_FLASH_SEC_SIZE, SPI_FLASH_SEC_SIZE);
  185. }
  186. #ifdef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  187. //deprecated, only used in compatible mode
  188. esp_err_t IRAM_ATTR spi_flash_erase_range(size_t start_addr, size_t size)
  189. {
  190. CHECK_WRITE_ADDRESS(start_addr, size);
  191. if (start_addr % SPI_FLASH_SEC_SIZE != 0) {
  192. return ESP_ERR_INVALID_ARG;
  193. }
  194. if (size % SPI_FLASH_SEC_SIZE != 0) {
  195. return ESP_ERR_INVALID_SIZE;
  196. }
  197. if (size + start_addr > spi_flash_get_chip_size()) {
  198. return ESP_ERR_INVALID_SIZE;
  199. }
  200. size_t start = start_addr / SPI_FLASH_SEC_SIZE;
  201. size_t end = start + size / SPI_FLASH_SEC_SIZE;
  202. const size_t sectors_per_block = BLOCK_ERASE_SIZE / SPI_FLASH_SEC_SIZE;
  203. COUNTER_START();
  204. esp_rom_spiflash_result_t rc;
  205. rc = spi_flash_unlock();
  206. if (rc == ESP_ROM_SPIFLASH_RESULT_OK) {
  207. for (size_t sector = start; sector != end && rc == ESP_ROM_SPIFLASH_RESULT_OK; ) {
  208. spi_flash_guard_start();
  209. if (sector % sectors_per_block == 0 && end - sector >= sectors_per_block) {
  210. rc = esp_rom_spiflash_erase_block(sector / sectors_per_block);
  211. sector += sectors_per_block;
  212. COUNTER_ADD_BYTES(erase, sectors_per_block * SPI_FLASH_SEC_SIZE);
  213. } else {
  214. rc = esp_rom_spiflash_erase_sector(sector);
  215. ++sector;
  216. COUNTER_ADD_BYTES(erase, SPI_FLASH_SEC_SIZE);
  217. }
  218. spi_flash_guard_end();
  219. }
  220. }
  221. COUNTER_STOP(erase);
  222. spi_flash_guard_start();
  223. spi_flash_check_and_flush_cache(start_addr, size);
  224. spi_flash_guard_end();
  225. return spi_flash_translate_rc(rc);
  226. }
  227. /* Wrapper around esp_rom_spiflash_write() that verifies data as written if CONFIG_SPI_FLASH_VERIFY_WRITE is set.
  228. If CONFIG_SPI_FLASH_VERIFY_WRITE is not set, this is esp_rom_spiflash_write().
  229. */
  230. static IRAM_ATTR esp_rom_spiflash_result_t spi_flash_write_inner(uint32_t target, const uint32_t *src_addr, int32_t len)
  231. {
  232. #ifndef CONFIG_SPI_FLASH_VERIFY_WRITE
  233. return esp_rom_spiflash_write(target, src_addr, len);
  234. #else // CONFIG_SPI_FLASH_VERIFY_WRITE
  235. esp_rom_spiflash_result_t res = ESP_ROM_SPIFLASH_RESULT_OK;
  236. assert(len % sizeof(uint32_t) == 0);
  237. uint32_t before_buf[ESP_ROM_SPIFLASH_BUFF_BYTE_READ_NUM / sizeof(uint32_t)];
  238. uint32_t after_buf[ESP_ROM_SPIFLASH_BUFF_BYTE_READ_NUM / sizeof(uint32_t)];
  239. uint32_t *expected_buf = before_buf;
  240. int32_t remaining = len;
  241. for(int i = 0; i < len; i += sizeof(before_buf)) {
  242. int i_w = i / sizeof(uint32_t); // index in words (i is an index in bytes)
  243. int32_t read_len = MIN(sizeof(before_buf), remaining);
  244. // Read "before" contents from flash
  245. res = esp_rom_spiflash_read(target + i, before_buf, read_len);
  246. if (res != ESP_ROM_SPIFLASH_RESULT_OK) {
  247. break;
  248. }
  249. for (int r = 0; r < read_len; r += sizeof(uint32_t)) {
  250. int r_w = r / sizeof(uint32_t); // index in words (r is index in bytes)
  251. uint32_t write = src_addr[i_w + r_w];
  252. uint32_t before = before_buf[r_w];
  253. uint32_t expected = write & before;
  254. #ifdef CONFIG_SPI_FLASH_WARN_SETTING_ZERO_TO_ONE
  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. #endif
  262. expected_buf[r_w] = expected;
  263. }
  264. res = esp_rom_spiflash_write(target + i, &src_addr[i_w], read_len);
  265. if (res != ESP_ROM_SPIFLASH_RESULT_OK) {
  266. break;
  267. }
  268. res = esp_rom_spiflash_read(target + i, after_buf, read_len);
  269. if (res != ESP_ROM_SPIFLASH_RESULT_OK) {
  270. break;
  271. }
  272. for (int r = 0; r < read_len; r += sizeof(uint32_t)) {
  273. int r_w = r / sizeof(uint32_t); // index in words (r is index in bytes)
  274. uint32_t expected = expected_buf[r_w];
  275. uint32_t actual = after_buf[r_w];
  276. if (expected != actual) {
  277. #ifdef CONFIG_SPI_FLASH_LOG_FAILED_WRITE
  278. spi_flash_guard_end();
  279. ESP_LOGE(TAG, "Bad write at offset 0x%x expected 0x%08x readback 0x%08x", target + i + r, expected, actual);
  280. spi_flash_guard_start();
  281. #endif
  282. res = ESP_ROM_SPIFLASH_RESULT_ERR;
  283. }
  284. }
  285. if (res != ESP_ROM_SPIFLASH_RESULT_OK) {
  286. break;
  287. }
  288. remaining -= read_len;
  289. }
  290. return res;
  291. #endif // CONFIG_SPI_FLASH_VERIFY_WRITE
  292. }
  293. esp_err_t IRAM_ATTR spi_flash_write(size_t dst, const void *srcv, size_t size)
  294. {
  295. CHECK_WRITE_ADDRESS(dst, size);
  296. // Out of bound writes are checked in ROM code, but we can give better
  297. // error code here
  298. if (dst + size > g_rom_flashchip.chip_size) {
  299. return ESP_ERR_INVALID_SIZE;
  300. }
  301. if (size == 0) {
  302. return ESP_OK;
  303. }
  304. esp_rom_spiflash_result_t rc = ESP_ROM_SPIFLASH_RESULT_OK;
  305. COUNTER_START();
  306. const uint8_t *srcc = (const uint8_t *) srcv;
  307. /*
  308. * Large operations are split into (up to) 3 parts:
  309. * - Left padding: 4 bytes up to the first 4-byte aligned destination offset.
  310. * - Middle part
  311. * - Right padding: 4 bytes from the last 4-byte aligned offset covered.
  312. */
  313. size_t left_off = dst & ~3U;
  314. size_t left_size = MIN(((dst + 3) & ~3U) - dst, size);
  315. size_t mid_off = left_size;
  316. size_t mid_size = (size - left_size) & ~3U;
  317. size_t right_off = left_size + mid_size;
  318. size_t right_size = size - mid_size - left_size;
  319. rc = spi_flash_unlock();
  320. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  321. goto out;
  322. }
  323. if (left_size > 0) {
  324. uint32_t t = 0xffffffff;
  325. memcpy(((uint8_t *) &t) + (dst - left_off), srcc, left_size);
  326. spi_flash_guard_start();
  327. rc = spi_flash_write_inner(left_off, &t, 4);
  328. spi_flash_guard_end();
  329. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  330. goto out;
  331. }
  332. COUNTER_ADD_BYTES(write, 4);
  333. }
  334. if (mid_size > 0) {
  335. /* If src buffer is 4-byte aligned as well and is not in a region that requires cache access to be enabled, we
  336. * can write directly without buffering in RAM. */
  337. #ifdef ESP_PLATFORM
  338. bool direct_write = esp_ptr_internal(srcc)
  339. && esp_ptr_byte_accessible(srcc)
  340. && ((uintptr_t) srcc + mid_off) % 4 == 0;
  341. #else
  342. bool direct_write = true;
  343. #endif
  344. while(mid_size > 0 && rc == ESP_ROM_SPIFLASH_RESULT_OK) {
  345. uint32_t write_buf[8];
  346. uint32_t write_size = MIN(mid_size, MAX_WRITE_CHUNK);
  347. const uint8_t *write_src = srcc + mid_off;
  348. if (!direct_write) {
  349. write_size = MIN(write_size, sizeof(write_buf));
  350. memcpy(write_buf, write_src, write_size);
  351. write_src = (const uint8_t *)write_buf;
  352. }
  353. spi_flash_guard_start();
  354. rc = spi_flash_write_inner(dst + mid_off, (const uint32_t *) write_src, write_size);
  355. spi_flash_guard_end();
  356. COUNTER_ADD_BYTES(write, write_size);
  357. mid_size -= write_size;
  358. mid_off += write_size;
  359. }
  360. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  361. goto out;
  362. }
  363. }
  364. if (right_size > 0) {
  365. uint32_t t = 0xffffffff;
  366. memcpy(&t, srcc + right_off, right_size);
  367. spi_flash_guard_start();
  368. rc = spi_flash_write_inner(dst + right_off, &t, 4);
  369. spi_flash_guard_end();
  370. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  371. goto out;
  372. }
  373. COUNTER_ADD_BYTES(write, 4);
  374. }
  375. out:
  376. COUNTER_STOP(write);
  377. spi_flash_guard_start();
  378. spi_flash_check_and_flush_cache(dst, size);
  379. spi_flash_guard_end();
  380. return spi_flash_translate_rc(rc);
  381. }
  382. #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  383. static IRAM_ATTR esp_err_t spi_flash_write_encrypted_in_rows(size_t dest_addr, const uint8_t *src, size_t size)
  384. {
  385. assert((dest_addr % 16) == 0);
  386. assert((size % 16) == 0);
  387. /* esp_rom_spiflash_write_encrypted encrypts data in RAM as it writes,
  388. so copy to a temporary buffer - 32 bytes at a time.
  389. Each call to esp_rom_spiflash_write_encrypted takes a 32 byte "row" of
  390. data to encrypt, and each row is two 16 byte AES blocks
  391. that share a key (as derived from flash address).
  392. */
  393. esp_rom_spiflash_result_t rc = ESP_ROM_SPIFLASH_RESULT_OK;
  394. WORD_ALIGNED_ATTR uint8_t encrypt_buf[32];
  395. uint32_t row_size;
  396. for (size_t i = 0; i < size; i += row_size) {
  397. uint32_t row_addr = dest_addr + i;
  398. if (i == 0 && (row_addr % 32) != 0) {
  399. /* writing to second block of a 32 byte row */
  400. row_size = 16;
  401. row_addr -= 16;
  402. /* copy to second block in buffer */
  403. memcpy(encrypt_buf + 16, src + i, 16);
  404. /* decrypt the first block from flash, will reencrypt to same bytes */
  405. spi_flash_read_encrypted(row_addr, encrypt_buf, 16);
  406. } else if (size - i == 16) {
  407. /* 16 bytes left, is first block of a 32 byte row */
  408. row_size = 16;
  409. /* copy to first block in buffer */
  410. memcpy(encrypt_buf, src + i, 16);
  411. /* decrypt the second block from flash, will reencrypt to same bytes */
  412. spi_flash_read_encrypted(row_addr + 16, encrypt_buf + 16, 16);
  413. } else {
  414. /* Writing a full 32 byte row (2 blocks) */
  415. row_size = 32;
  416. memcpy(encrypt_buf, src + i, 32);
  417. }
  418. spi_flash_guard_start();
  419. rc = esp_rom_spiflash_write_encrypted(row_addr, (uint32_t *)encrypt_buf, 32);
  420. spi_flash_guard_end();
  421. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  422. break;
  423. }
  424. }
  425. bzero(encrypt_buf, sizeof(encrypt_buf));
  426. return spi_flash_translate_rc(rc);
  427. }
  428. esp_err_t IRAM_ATTR spi_flash_write_encrypted(size_t dest_addr, const void *src, size_t size)
  429. {
  430. esp_err_t err = ESP_OK;
  431. CHECK_WRITE_ADDRESS(dest_addr, size);
  432. if ((dest_addr % 16) != 0) {
  433. return ESP_ERR_INVALID_ARG;
  434. }
  435. if ((size % 16) != 0) {
  436. return ESP_ERR_INVALID_SIZE;
  437. }
  438. COUNTER_START();
  439. esp_rom_spiflash_result_t rc = spi_flash_unlock();
  440. err = spi_flash_translate_rc(rc);
  441. if (err != ESP_OK) {
  442. goto fail;
  443. }
  444. #ifndef CONFIG_SPI_FLASH_VERIFY_WRITE
  445. err = spi_flash_write_encrypted_in_rows(dest_addr, (const uint8_t*)src, size);
  446. COUNTER_ADD_BYTES(write, size);
  447. spi_flash_guard_start();
  448. spi_flash_check_and_flush_cache(dest_addr, size);
  449. spi_flash_guard_end();
  450. #else
  451. const uint32_t* src_w = (const uint32_t*)src;
  452. uint32_t read_buf[ESP_ROM_SPIFLASH_BUFF_BYTE_READ_NUM / sizeof(uint32_t)];
  453. int32_t remaining = size;
  454. for(int i = 0; i < size; i += sizeof(read_buf)) {
  455. int i_w = i / sizeof(uint32_t); // index in words (i is an index in bytes)
  456. int32_t read_len = MIN(sizeof(read_buf), remaining);
  457. // Read "before" contents from flash
  458. esp_err_t err = spi_flash_read(dest_addr + i, read_buf, read_len);
  459. if (err != ESP_OK) {
  460. break;
  461. }
  462. #ifdef CONFIG_SPI_FLASH_WARN_SETTING_ZERO_TO_ONE
  463. //The written data cannot be predicted, so warning is shown if any of the bits is not 1.
  464. for (int r = 0; r < read_len; r += sizeof(uint32_t)) {
  465. uint32_t before = read_buf[r / sizeof(uint32_t)];
  466. if (before != 0xFFFFFFFF) {
  467. ESP_LOGW(TAG, "Encrypted write at offset 0x%x but not erased (0x%08x)",
  468. dest_addr + i + r, before);
  469. }
  470. }
  471. #endif
  472. err = spi_flash_write_encrypted_in_rows(dest_addr + i, src + i, read_len);
  473. if (err != ESP_OK) {
  474. break;
  475. }
  476. COUNTER_ADD_BYTES(write, size);
  477. spi_flash_guard_start();
  478. spi_flash_check_and_flush_cache(dest_addr, size);
  479. spi_flash_guard_end();
  480. err = spi_flash_read_encrypted(dest_addr + i, read_buf, read_len);
  481. if (err != ESP_OK) {
  482. break;
  483. }
  484. for (int r = 0; r < read_len; r += sizeof(uint32_t)) {
  485. int r_w = r / sizeof(uint32_t); // index in words (r is index in bytes)
  486. uint32_t expected = src_w[i_w + r_w];
  487. uint32_t actual = read_buf[r_w];
  488. if (expected != actual) {
  489. #ifdef CONFIG_SPI_FLASH_LOG_FAILED_WRITE
  490. ESP_LOGE(TAG, "Bad write at offset 0x%x expected 0x%08x readback 0x%08x", dest_addr + i + r, expected, actual);
  491. #endif
  492. err = ESP_FAIL;
  493. }
  494. }
  495. if (err != ESP_OK) {
  496. break;
  497. }
  498. remaining -= read_len;
  499. }
  500. #endif // CONFIG_SPI_FLASH_VERIFY_WRITE
  501. fail:
  502. COUNTER_STOP(write);
  503. return err;
  504. }
  505. #ifdef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  506. esp_err_t IRAM_ATTR spi_flash_read(size_t src, void *dstv, size_t size)
  507. {
  508. // Out of bound reads are checked in ROM code, but we can give better
  509. // error code here
  510. if (src + size > g_rom_flashchip.chip_size) {
  511. return ESP_ERR_INVALID_SIZE;
  512. }
  513. if (size == 0) {
  514. return ESP_OK;
  515. }
  516. esp_rom_spiflash_result_t rc = ESP_ROM_SPIFLASH_RESULT_OK;
  517. COUNTER_START();
  518. spi_flash_guard_start();
  519. /* To simplify boundary checks below, we handle small reads separately. */
  520. if (size < 16) {
  521. uint32_t t[6]; /* Enough for 16 bytes + 4 on either side for padding. */
  522. uint32_t read_src = src & ~3U;
  523. uint32_t left_off = src & 3U;
  524. uint32_t read_size = (left_off + size + 3) & ~3U;
  525. rc = esp_rom_spiflash_read(read_src, t, read_size);
  526. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  527. goto out;
  528. }
  529. COUNTER_ADD_BYTES(read, read_size);
  530. #ifdef ESP_PLATFORM
  531. if (esp_ptr_external_ram(dstv)) {
  532. spi_flash_guard_end();
  533. memcpy(dstv, ((uint8_t *) t) + left_off, size);
  534. spi_flash_guard_start();
  535. } else {
  536. memcpy(dstv, ((uint8_t *) t) + left_off, size);
  537. }
  538. #else
  539. memcpy(dstv, ((uint8_t *) t) + left_off, size);
  540. #endif
  541. goto out;
  542. }
  543. uint8_t *dstc = (uint8_t *) dstv;
  544. intptr_t dsti = (intptr_t) dstc;
  545. /*
  546. * Large operations are split into (up to) 3 parts:
  547. * - The middle part: from the first 4-aligned position in src to the first
  548. * 4-aligned position in dst.
  549. */
  550. size_t src_mid_off = (src % 4 == 0 ? 0 : 4 - (src % 4));
  551. size_t dst_mid_off = (dsti % 4 == 0 ? 0 : 4 - (dsti % 4));
  552. size_t mid_size = (size - MAX(src_mid_off, dst_mid_off)) & ~3U;
  553. /*
  554. * - Once the middle part is in place, src_mid_off bytes from the preceding
  555. * 4-aligned source location are added on the left.
  556. */
  557. size_t pad_left_src = src & ~3U;
  558. size_t pad_left_size = src_mid_off;
  559. /*
  560. * - Finally, the right part is added: from the end of the middle part to
  561. * the end. Depending on the alignment of source and destination, this may
  562. * be a 4 or 8 byte read from pad_right_src.
  563. */
  564. size_t pad_right_src = (src + pad_left_size + mid_size) & ~3U;
  565. size_t pad_right_off = (pad_right_src - src);
  566. size_t pad_right_size = (size - pad_right_off);
  567. #ifdef ESP_PLATFORM
  568. bool direct_read = esp_ptr_internal(dstc)
  569. && esp_ptr_byte_accessible(dstc)
  570. && ((uintptr_t) dstc + dst_mid_off) % 4 == 0;
  571. #else
  572. bool direct_read = true;
  573. #endif
  574. if (mid_size > 0) {
  575. uint32_t mid_remaining = mid_size;
  576. uint32_t mid_read = 0;
  577. while (mid_remaining > 0) {
  578. uint32_t read_size = MIN(mid_remaining, MAX_READ_CHUNK);
  579. uint32_t read_buf[8];
  580. uint8_t *read_dst_final = dstc + dst_mid_off + mid_read;
  581. uint8_t *read_dst = read_dst_final;
  582. if (!direct_read) {
  583. read_size = MIN(read_size, sizeof(read_buf));
  584. read_dst = (uint8_t *) read_buf;
  585. }
  586. rc = esp_rom_spiflash_read(src + src_mid_off + mid_read,
  587. (uint32_t *) read_dst, read_size);
  588. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  589. goto out;
  590. }
  591. mid_remaining -= read_size;
  592. mid_read += read_size;
  593. if (!direct_read) {
  594. spi_flash_guard_end();
  595. memcpy(read_dst_final, read_buf, read_size);
  596. spi_flash_guard_start();
  597. } else if (mid_remaining > 0) {
  598. /* Drop guard momentarily, allows other tasks to preempt */
  599. spi_flash_guard_end();
  600. spi_flash_guard_start();
  601. }
  602. }
  603. COUNTER_ADD_BYTES(read, mid_size);
  604. /*
  605. * If offsets in src and dst are different, perform an in-place shift
  606. * to put destination data into its final position.
  607. * Note that the shift can be left (src_mid_off < dst_mid_off) or right.
  608. */
  609. if (src_mid_off != dst_mid_off) {
  610. if (!direct_read) {
  611. spi_flash_guard_end();
  612. }
  613. memmove(dstc + src_mid_off, dstc + dst_mid_off, mid_size);
  614. if (!direct_read) {
  615. spi_flash_guard_start();
  616. }
  617. }
  618. }
  619. if (pad_left_size > 0) {
  620. uint32_t t;
  621. rc = esp_rom_spiflash_read(pad_left_src, &t, 4);
  622. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  623. goto out;
  624. }
  625. COUNTER_ADD_BYTES(read, 4);
  626. if (!direct_read) {
  627. spi_flash_guard_end();
  628. }
  629. memcpy(dstc, ((uint8_t *) &t) + (4 - pad_left_size), pad_left_size);
  630. if (!direct_read) {
  631. spi_flash_guard_start();
  632. }
  633. }
  634. if (pad_right_size > 0) {
  635. uint32_t t[2];
  636. int32_t read_size = (pad_right_size <= 4 ? 4 : 8);
  637. rc = esp_rom_spiflash_read(pad_right_src, t, read_size);
  638. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  639. goto out;
  640. }
  641. COUNTER_ADD_BYTES(read, read_size);
  642. if (!direct_read) {
  643. spi_flash_guard_end();
  644. }
  645. memcpy(dstc + pad_right_off, t, pad_right_size);
  646. if (!direct_read) {
  647. spi_flash_guard_start();
  648. }
  649. }
  650. out:
  651. spi_flash_guard_end();
  652. COUNTER_STOP(read);
  653. return spi_flash_translate_rc(rc);
  654. }
  655. #endif
  656. esp_err_t IRAM_ATTR spi_flash_read_encrypted(size_t src, void *dstv, size_t size)
  657. {
  658. if (src + size > g_rom_flashchip.chip_size) {
  659. return ESP_ERR_INVALID_SIZE;
  660. }
  661. if (size == 0) {
  662. return ESP_OK;
  663. }
  664. esp_err_t err;
  665. const uint8_t *map;
  666. spi_flash_mmap_handle_t map_handle;
  667. size_t map_src = src & ~(SPI_FLASH_MMU_PAGE_SIZE - 1);
  668. size_t map_size = size + (src - map_src);
  669. err = spi_flash_mmap(map_src, map_size, SPI_FLASH_MMAP_DATA, (const void **)&map, &map_handle);
  670. if (err != ESP_OK) {
  671. return err;
  672. }
  673. memcpy(dstv, map + (src - map_src), size);
  674. spi_flash_munmap(map_handle);
  675. return err;
  676. }
  677. static esp_err_t IRAM_ATTR spi_flash_translate_rc(esp_rom_spiflash_result_t rc)
  678. {
  679. switch (rc) {
  680. case ESP_ROM_SPIFLASH_RESULT_OK:
  681. return ESP_OK;
  682. case ESP_ROM_SPIFLASH_RESULT_TIMEOUT:
  683. return ESP_ERR_FLASH_OP_TIMEOUT;
  684. case ESP_ROM_SPIFLASH_RESULT_ERR:
  685. default:
  686. return ESP_ERR_FLASH_OP_FAIL;
  687. }
  688. }
  689. #if CONFIG_SPI_FLASH_ENABLE_COUNTERS
  690. static inline void dump_counter(spi_flash_counter_t *counter, const char *name)
  691. {
  692. ESP_LOGI(TAG, "%s count=%8d time=%8dus bytes=%8d\n", name,
  693. counter->count, counter->time, counter->bytes);
  694. }
  695. const spi_flash_counters_t *spi_flash_get_counters(void)
  696. {
  697. return &s_flash_stats;
  698. }
  699. void spi_flash_reset_counters(void)
  700. {
  701. memset(&s_flash_stats, 0, sizeof(s_flash_stats));
  702. }
  703. void spi_flash_dump_counters(void)
  704. {
  705. dump_counter(&s_flash_stats.read, "read ");
  706. dump_counter(&s_flash_stats.write, "write");
  707. dump_counter(&s_flash_stats.erase, "erase");
  708. }
  709. #endif //CONFIG_SPI_FLASH_ENABLE_COUNTERS
  710. #if CONFIG_IDF_TARGET_ESP32S2BETA
  711. #define SPICACHE SPIMEM0
  712. #define SPIFLASH SPIMEM1
  713. #define FLASH_WRAP_CMD 0x77
  714. esp_err_t spi_flash_wrap_set(spi_flash_wrap_mode_t mode)
  715. {
  716. uint32_t reg_bkp_ctrl = SPIFLASH.ctrl.val;
  717. uint32_t reg_bkp_usr = SPIFLASH.user.val;
  718. SPIFLASH.user.fwrite_dio = 0;
  719. SPIFLASH.user.fwrite_dual = 0;
  720. SPIFLASH.user.fwrite_qio = 1;
  721. SPIFLASH.user.fwrite_quad = 0;
  722. SPIFLASH.ctrl.fcmd_dual = 0;
  723. SPIFLASH.ctrl.fcmd_quad = 0;
  724. SPIFLASH.user.usr_dummy = 0;
  725. SPIFLASH.user.usr_addr = 1;
  726. SPIFLASH.user.usr_command = 1;
  727. SPIFLASH.user2.usr_command_bitlen = 7;
  728. SPIFLASH.user2.usr_command_value = FLASH_WRAP_CMD;
  729. SPIFLASH.user1.usr_addr_bitlen = 23;
  730. SPIFLASH.addr = 0;
  731. SPIFLASH.user.usr_miso = 0;
  732. SPIFLASH.user.usr_mosi = 1;
  733. SPIFLASH.mosi_dlen.usr_mosi_bit_len = 7;
  734. SPIFLASH.data_buf[0] = (uint32_t) mode << 4;;
  735. SPIFLASH.cmd.usr = 1;
  736. while(SPIFLASH.cmd.usr != 0)
  737. { }
  738. SPIFLASH.ctrl.val = reg_bkp_ctrl;
  739. SPIFLASH.user.val = reg_bkp_usr;
  740. return ESP_OK;
  741. }
  742. esp_err_t spi_flash_enable_wrap(uint32_t wrap_size)
  743. {
  744. switch(wrap_size) {
  745. case 8:
  746. return spi_flash_wrap_set(FLASH_WRAP_MODE_8B);
  747. case 16:
  748. return spi_flash_wrap_set(FLASH_WRAP_MODE_16B);
  749. case 32:
  750. return spi_flash_wrap_set(FLASH_WRAP_MODE_32B);
  751. case 64:
  752. return spi_flash_wrap_set(FLASH_WRAP_MODE_64B);
  753. default:
  754. return ESP_FAIL;
  755. }
  756. }
  757. void spi_flash_disable_wrap(void)
  758. {
  759. spi_flash_wrap_set(FLASH_WRAP_MODE_DISABLE);
  760. }
  761. bool spi_flash_support_wrap_size(uint32_t wrap_size)
  762. {
  763. if (!REG_GET_BIT(SPI_MEM_CTRL_REG(0), SPI_MEM_FREAD_QIO) || !REG_GET_BIT(SPI_MEM_CTRL_REG(0), SPI_MEM_FASTRD_MODE)){
  764. return ESP_FAIL;
  765. }
  766. switch(wrap_size) {
  767. case 0:
  768. case 8:
  769. case 16:
  770. case 32:
  771. case 64:
  772. return true;
  773. default:
  774. return false;
  775. }
  776. }
  777. #endif
  778. #if defined(CONFIG_SPI_FLASH_USE_LEGACY_IMPL) && defined(CONFIG_IDF_TARGET_ESP32S2BETA)
  779. // TODO esp32s2beta: Remove once ESP32S2Beta has new SPI Flash API support
  780. esp_flash_t *esp_flash_default_chip = NULL;
  781. #endif