flash_ops.c 27 KB

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