flash_ops.c 27 KB

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