flash_ops.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdlib.h>
  15. #include <assert.h>
  16. #include <string.h>
  17. #include <stdio.h>
  18. #include <sys/param.h> // For MIN/MAX(a, b)
  19. #include <freertos/FreeRTOS.h>
  20. #include <freertos/task.h>
  21. #include <freertos/semphr.h>
  22. #include <rom/spi_flash.h>
  23. #include <rom/cache.h>
  24. #include <soc/soc.h>
  25. #include <soc/dport_reg.h>
  26. #include "sdkconfig.h"
  27. #include "esp_ipc.h"
  28. #include "esp_attr.h"
  29. #include "esp_spi_flash.h"
  30. #include "esp_log.h"
  31. #include "cache_utils.h"
  32. /* bytes erased by SPIEraseBlock() ROM function */
  33. #define BLOCK_ERASE_SIZE 65536
  34. #if CONFIG_SPI_FLASH_ENABLE_COUNTERS
  35. static const char* TAG = "spi_flash";
  36. static spi_flash_counters_t s_flash_stats;
  37. #define COUNTER_START() uint32_t ts_begin = xthal_get_ccount()
  38. #define COUNTER_STOP(counter) \
  39. do{ \
  40. s_flash_stats.counter.count++; \
  41. s_flash_stats.counter.time += (xthal_get_ccount() - ts_begin) / (XT_CLOCK_FREQ / 1000000); \
  42. } while(0)
  43. #define COUNTER_ADD_BYTES(counter, size) \
  44. do { \
  45. s_flash_stats.counter.bytes += size; \
  46. } while (0)
  47. #else
  48. #define COUNTER_START()
  49. #define COUNTER_STOP(counter)
  50. #define COUNTER_ADD_BYTES(counter, size)
  51. #endif //CONFIG_SPI_FLASH_ENABLE_COUNTERS
  52. static esp_err_t spi_flash_translate_rc(SpiFlashOpResult rc);
  53. const DRAM_ATTR spi_flash_guard_funcs_t g_flash_guard_default_ops = {
  54. .start = spi_flash_disable_interrupts_caches_and_other_cpu,
  55. .end = spi_flash_enable_interrupts_caches_and_other_cpu,
  56. .op_lock = spi_flash_op_lock,
  57. .op_unlock = spi_flash_op_unlock
  58. };
  59. const DRAM_ATTR spi_flash_guard_funcs_t g_flash_guard_no_os_ops = {
  60. .start = spi_flash_disable_interrupts_caches_and_other_cpu_no_os,
  61. .end = spi_flash_enable_interrupts_caches_no_os,
  62. .op_lock = 0,
  63. .op_unlock = 0
  64. };
  65. static const spi_flash_guard_funcs_t *s_flash_guard_ops;
  66. void spi_flash_init()
  67. {
  68. spi_flash_init_lock();
  69. #if CONFIG_SPI_FLASH_ENABLE_COUNTERS
  70. spi_flash_reset_counters();
  71. #endif
  72. }
  73. void IRAM_ATTR spi_flash_guard_set(const spi_flash_guard_funcs_t* funcs)
  74. {
  75. s_flash_guard_ops = funcs;
  76. }
  77. size_t IRAM_ATTR spi_flash_get_chip_size()
  78. {
  79. return g_rom_flashchip.chip_size;
  80. }
  81. static SpiFlashOpResult IRAM_ATTR spi_flash_unlock()
  82. {
  83. static bool unlocked = false;
  84. if (!unlocked) {
  85. SpiFlashOpResult rc = SPIUnlock();
  86. if (rc != SPI_FLASH_RESULT_OK) {
  87. return rc;
  88. }
  89. unlocked = true;
  90. }
  91. return SPI_FLASH_RESULT_OK;
  92. }
  93. static inline void IRAM_ATTR spi_flash_guard_start()
  94. {
  95. if (s_flash_guard_ops && s_flash_guard_ops->start) {
  96. s_flash_guard_ops->start();
  97. }
  98. }
  99. static inline void IRAM_ATTR spi_flash_guard_end()
  100. {
  101. if (s_flash_guard_ops && s_flash_guard_ops->end) {
  102. s_flash_guard_ops->end();
  103. }
  104. }
  105. static inline void IRAM_ATTR spi_flash_guard_op_lock()
  106. {
  107. if (s_flash_guard_ops && s_flash_guard_ops->op_lock) {
  108. s_flash_guard_ops->op_lock();
  109. }
  110. }
  111. static inline void IRAM_ATTR spi_flash_guard_op_unlock()
  112. {
  113. if (s_flash_guard_ops && s_flash_guard_ops->op_unlock) {
  114. s_flash_guard_ops->op_unlock();
  115. }
  116. }
  117. esp_err_t IRAM_ATTR spi_flash_erase_sector(size_t sec)
  118. {
  119. return spi_flash_erase_range(sec * SPI_FLASH_SEC_SIZE, SPI_FLASH_SEC_SIZE);
  120. }
  121. esp_err_t IRAM_ATTR spi_flash_erase_range(uint32_t start_addr, uint32_t size)
  122. {
  123. if (start_addr % SPI_FLASH_SEC_SIZE != 0) {
  124. return ESP_ERR_INVALID_ARG;
  125. }
  126. if (size % SPI_FLASH_SEC_SIZE != 0) {
  127. return ESP_ERR_INVALID_SIZE;
  128. }
  129. if (size + start_addr > spi_flash_get_chip_size()) {
  130. return ESP_ERR_INVALID_SIZE;
  131. }
  132. size_t start = start_addr / SPI_FLASH_SEC_SIZE;
  133. size_t end = start + size / SPI_FLASH_SEC_SIZE;
  134. const size_t sectors_per_block = BLOCK_ERASE_SIZE / SPI_FLASH_SEC_SIZE;
  135. COUNTER_START();
  136. spi_flash_guard_start();
  137. SpiFlashOpResult rc;
  138. rc = spi_flash_unlock();
  139. if (rc == SPI_FLASH_RESULT_OK) {
  140. for (size_t sector = start; sector != end && rc == SPI_FLASH_RESULT_OK; ) {
  141. if (sector % sectors_per_block == 0 && end - sector > sectors_per_block) {
  142. rc = SPIEraseBlock(sector / sectors_per_block);
  143. sector += sectors_per_block;
  144. COUNTER_ADD_BYTES(erase, sectors_per_block * SPI_FLASH_SEC_SIZE);
  145. } else {
  146. rc = SPIEraseSector(sector);
  147. ++sector;
  148. COUNTER_ADD_BYTES(erase, SPI_FLASH_SEC_SIZE);
  149. }
  150. }
  151. }
  152. spi_flash_guard_end();
  153. COUNTER_STOP(erase);
  154. return spi_flash_translate_rc(rc);
  155. }
  156. esp_err_t IRAM_ATTR spi_flash_write(size_t dst, const void *srcv, size_t size)
  157. {
  158. // Out of bound writes are checked in ROM code, but we can give better
  159. // error code here
  160. if (dst + size > g_rom_flashchip.chip_size) {
  161. return ESP_ERR_INVALID_SIZE;
  162. }
  163. if (size == 0) {
  164. return ESP_OK;
  165. }
  166. SpiFlashOpResult rc = SPI_FLASH_RESULT_OK;
  167. COUNTER_START();
  168. const char *srcc = (const char *) srcv;
  169. /*
  170. * Large operations are split into (up to) 3 parts:
  171. * - Left padding: 4 bytes up to the first 4-byte aligned destination offset.
  172. * - Middle part
  173. * - Right padding: 4 bytes from the last 4-byte aligned offset covered.
  174. */
  175. size_t left_off = dst & ~3U;
  176. size_t left_size = MIN(((dst + 3) & ~3U) - dst, size);
  177. size_t mid_off = left_size;
  178. size_t mid_size = (size - left_size) & ~3U;
  179. size_t right_off = left_size + mid_size;
  180. size_t right_size = size - mid_size - left_size;
  181. spi_flash_guard_start();
  182. rc = spi_flash_unlock();
  183. spi_flash_guard_end();
  184. if (rc != SPI_FLASH_RESULT_OK) {
  185. goto out;
  186. }
  187. if (left_size > 0) {
  188. uint32_t t = 0xffffffff;
  189. memcpy(((uint8_t *) &t) + (dst - left_off), srcc, left_size);
  190. spi_flash_guard_start();
  191. rc = SPIWrite(left_off, &t, 4);
  192. spi_flash_guard_end();
  193. if (rc != SPI_FLASH_RESULT_OK) {
  194. goto out;
  195. }
  196. COUNTER_ADD_BYTES(write, 4);
  197. }
  198. if (mid_size > 0) {
  199. /* If src buffer is 4-byte aligned as well and is not in a region that
  200. * requires cache access to be enabled, we can write it all at once. */
  201. #ifdef ESP_PLATFORM
  202. bool in_dram = ((uintptr_t) srcc >= 0x3FFAE000 &&
  203. (uintptr_t) srcc < 0x40000000);
  204. #else
  205. bool in_dram = true;
  206. #endif
  207. if (in_dram && (((uintptr_t) srcc) + mid_off) % 4 == 0) {
  208. spi_flash_guard_start();
  209. rc = SPIWrite(dst + mid_off, (const uint32_t *) (srcc + mid_off), mid_size);
  210. spi_flash_guard_end();
  211. if (rc != SPI_FLASH_RESULT_OK) {
  212. goto out;
  213. }
  214. COUNTER_ADD_BYTES(write, mid_size);
  215. } else {
  216. /*
  217. * Otherwise, unlike for read, we cannot manipulate data in the
  218. * user-provided buffer, so we write in 32 byte blocks.
  219. */
  220. while (mid_size > 0) {
  221. uint32_t t[8];
  222. uint32_t write_size = MIN(mid_size, sizeof(t));
  223. memcpy(t, srcc + mid_off, write_size);
  224. spi_flash_guard_start();
  225. rc = SPIWrite(dst + mid_off, t, write_size);
  226. spi_flash_guard_end();
  227. if (rc != SPI_FLASH_RESULT_OK) {
  228. goto out;
  229. }
  230. COUNTER_ADD_BYTES(write, write_size);
  231. mid_size -= write_size;
  232. mid_off += write_size;
  233. }
  234. }
  235. }
  236. if (right_size > 0) {
  237. uint32_t t = 0xffffffff;
  238. memcpy(&t, srcc + right_off, right_size);
  239. spi_flash_guard_start();
  240. rc = SPIWrite(dst + right_off, &t, 4);
  241. spi_flash_guard_end();
  242. if (rc != SPI_FLASH_RESULT_OK) {
  243. goto out;
  244. }
  245. COUNTER_ADD_BYTES(write, 4);
  246. }
  247. out:
  248. COUNTER_STOP(write);
  249. spi_flash_guard_op_lock();
  250. spi_flash_mark_modified_region(dst, size);
  251. spi_flash_guard_op_unlock();
  252. return spi_flash_translate_rc(rc);
  253. }
  254. esp_err_t IRAM_ATTR spi_flash_write_encrypted(size_t dest_addr, const void *src, size_t size)
  255. {
  256. const uint8_t *ssrc = (const uint8_t *)src;
  257. if ((dest_addr % 16) != 0) {
  258. return ESP_ERR_INVALID_ARG;
  259. }
  260. if ((size % 16) != 0) {
  261. return ESP_ERR_INVALID_SIZE;
  262. }
  263. COUNTER_START();
  264. spi_flash_disable_interrupts_caches_and_other_cpu();
  265. SpiFlashOpResult rc;
  266. spi_flash_guard_start();
  267. rc = spi_flash_unlock();
  268. spi_flash_guard_end();
  269. spi_flash_enable_interrupts_caches_and_other_cpu();
  270. if (rc == SPI_FLASH_RESULT_OK) {
  271. /* SPI_Encrypt_Write encrypts data in RAM as it writes,
  272. so copy to a temporary buffer - 32 bytes at a time.
  273. Each call to SPI_Encrypt_Write takes a 32 byte "row" of
  274. data to encrypt, and each row is two 16 byte AES blocks
  275. that share a key (as derived from flash address).
  276. */
  277. uint8_t encrypt_buf[32] __attribute__((aligned(4)));
  278. uint32_t row_size;
  279. for (size_t i = 0; i < size; i += row_size) {
  280. uint32_t row_addr = dest_addr + i;
  281. if (i == 0 && (row_addr % 32) != 0) {
  282. /* writing to second block of a 32 byte row */
  283. row_size = 16;
  284. row_addr -= 16;
  285. /* copy to second block in buffer */
  286. memcpy(encrypt_buf + 16, ssrc + i, 16);
  287. /* decrypt the first block from flash, will reencrypt to same bytes */
  288. spi_flash_read_encrypted(row_addr, encrypt_buf, 16);
  289. }
  290. else if (size - i == 16) {
  291. /* 16 bytes left, is first block of a 32 byte row */
  292. row_size = 16;
  293. /* copy to first block in buffer */
  294. memcpy(encrypt_buf, ssrc + i, 16);
  295. /* decrypt the second block from flash, will reencrypt to same bytes */
  296. spi_flash_read_encrypted(row_addr + 16, encrypt_buf + 16, 16);
  297. }
  298. else {
  299. /* Writing a full 32 byte row (2 blocks) */
  300. row_size = 32;
  301. memcpy(encrypt_buf, ssrc + i, 32);
  302. }
  303. spi_flash_disable_interrupts_caches_and_other_cpu();
  304. rc = SPI_Encrypt_Write(row_addr, (uint32_t *)encrypt_buf, 32);
  305. spi_flash_enable_interrupts_caches_and_other_cpu();
  306. if (rc != SPI_FLASH_RESULT_OK) {
  307. break;
  308. }
  309. }
  310. bzero(encrypt_buf, sizeof(encrypt_buf));
  311. }
  312. COUNTER_ADD_BYTES(write, size);
  313. spi_flash_guard_op_lock();
  314. spi_flash_mark_modified_region(dest_addr, size);
  315. spi_flash_guard_op_unlock();
  316. return spi_flash_translate_rc(rc);
  317. }
  318. esp_err_t IRAM_ATTR spi_flash_read(size_t src, void *dstv, size_t size)
  319. {
  320. // Out of bound reads are checked in ROM code, but we can give better
  321. // error code here
  322. if (src + size > g_rom_flashchip.chip_size) {
  323. return ESP_ERR_INVALID_SIZE;
  324. }
  325. if (size == 0) {
  326. return ESP_OK;
  327. }
  328. SpiFlashOpResult rc = SPI_FLASH_RESULT_OK;
  329. COUNTER_START();
  330. spi_flash_guard_start();
  331. /* To simplify boundary checks below, we handle small reads separately. */
  332. if (size < 16) {
  333. uint32_t t[6]; /* Enough for 16 bytes + 4 on either side for padding. */
  334. uint32_t read_src = src & ~3U;
  335. uint32_t left_off = src & 3U;
  336. uint32_t read_size = (left_off + size + 3) & ~3U;
  337. rc = SPIRead(read_src, t, read_size);
  338. if (rc != SPI_FLASH_RESULT_OK) {
  339. goto out;
  340. }
  341. COUNTER_ADD_BYTES(read, read_size);
  342. memcpy(dstv, ((char *) t) + left_off, size);
  343. goto out;
  344. }
  345. char *dstc = (char *) dstv;
  346. intptr_t dsti = (intptr_t) dstc;
  347. /*
  348. * Large operations are split into (up to) 3 parts:
  349. * - The middle part: from the first 4-aligned position in src to the first
  350. * 4-aligned position in dst.
  351. */
  352. size_t src_mid_off = (src % 4 == 0 ? 0 : 4 - (src % 4));
  353. size_t dst_mid_off = (dsti % 4 == 0 ? 0 : 4 - (dsti % 4));
  354. size_t mid_size = (size - MAX(src_mid_off, dst_mid_off)) & ~3U;
  355. /*
  356. * - Once the middle part is in place, src_mid_off bytes from the preceding
  357. * 4-aligned source location are added on the left.
  358. */
  359. size_t pad_left_src = src & ~3U;
  360. size_t pad_left_size = src_mid_off;
  361. /*
  362. * - Finally, the right part is added: from the end of the middle part to
  363. * the end. Depending on the alignment of source and destination, this may
  364. * be a 4 or 8 byte read from pad_right_src.
  365. */
  366. size_t pad_right_src = (src + pad_left_size + mid_size) & ~3U;
  367. size_t pad_right_off = (pad_right_src - src);
  368. size_t pad_right_size = (size - pad_right_off);
  369. if (mid_size > 0) {
  370. rc = SPIRead(src + src_mid_off, (uint32_t *) (dstc + dst_mid_off), mid_size);
  371. if (rc != SPI_FLASH_RESULT_OK) {
  372. goto out;
  373. }
  374. COUNTER_ADD_BYTES(read, mid_size);
  375. /*
  376. * If offsets in src and dst are different, perform an in-place shift
  377. * to put destination data into its final position.
  378. * Note that the shift can be left (src_mid_off < dst_mid_off) or right.
  379. */
  380. if (src_mid_off != dst_mid_off) {
  381. memmove(dstc + src_mid_off, dstc + dst_mid_off, mid_size);
  382. }
  383. }
  384. if (pad_left_size > 0) {
  385. uint32_t t;
  386. rc = SPIRead(pad_left_src, &t, 4);
  387. if (rc != SPI_FLASH_RESULT_OK) {
  388. goto out;
  389. }
  390. COUNTER_ADD_BYTES(read, 4);
  391. memcpy(dstc, ((uint8_t *) &t) + (4 - pad_left_size), pad_left_size);
  392. }
  393. if (pad_right_size > 0) {
  394. uint32_t t[2];
  395. int32_t read_size = (pad_right_size <= 4 ? 4 : 8);
  396. rc = SPIRead(pad_right_src, t, read_size);
  397. if (rc != SPI_FLASH_RESULT_OK) {
  398. goto out;
  399. }
  400. COUNTER_ADD_BYTES(read, read_size);
  401. memcpy(dstc + pad_right_off, t, pad_right_size);
  402. }
  403. out:
  404. spi_flash_guard_end();
  405. COUNTER_STOP(read);
  406. return spi_flash_translate_rc(rc);
  407. }
  408. esp_err_t IRAM_ATTR spi_flash_read_encrypted(size_t src, void *dstv, size_t size)
  409. {
  410. if (src + size > g_rom_flashchip.chip_size) {
  411. return ESP_ERR_INVALID_SIZE;
  412. }
  413. if (size == 0) {
  414. return ESP_OK;
  415. }
  416. esp_err_t err;
  417. const uint8_t *map;
  418. spi_flash_mmap_handle_t map_handle;
  419. size_t map_src = src & ~(SPI_FLASH_MMU_PAGE_SIZE-1);
  420. size_t map_size = size + (src - map_src);
  421. err = spi_flash_mmap(map_src, map_size, SPI_FLASH_MMAP_DATA, (const void **)&map, &map_handle);
  422. if (err != ESP_OK) {
  423. return err;
  424. }
  425. memcpy(dstv, map + (src - map_src), size);
  426. spi_flash_munmap(map_handle);
  427. return err;
  428. }
  429. static esp_err_t IRAM_ATTR spi_flash_translate_rc(SpiFlashOpResult rc)
  430. {
  431. switch (rc) {
  432. case SPI_FLASH_RESULT_OK:
  433. return ESP_OK;
  434. case SPI_FLASH_RESULT_TIMEOUT:
  435. return ESP_ERR_FLASH_OP_TIMEOUT;
  436. case SPI_FLASH_RESULT_ERR:
  437. default:
  438. return ESP_ERR_FLASH_OP_FAIL;
  439. }
  440. }
  441. #if CONFIG_SPI_FLASH_ENABLE_COUNTERS
  442. static inline void dump_counter(spi_flash_counter_t* counter, const char* name)
  443. {
  444. ESP_LOGI(TAG, "%s count=%8d time=%8dms bytes=%8d\n", name,
  445. counter->count, counter->time, counter->bytes);
  446. }
  447. const spi_flash_counters_t* spi_flash_get_counters()
  448. {
  449. return &s_flash_stats;
  450. }
  451. void spi_flash_reset_counters()
  452. {
  453. memset(&s_flash_stats, 0, sizeof(s_flash_stats));
  454. }
  455. void spi_flash_dump_counters()
  456. {
  457. dump_counter(&s_flash_stats.read, "read ");
  458. dump_counter(&s_flash_stats.write, "write");
  459. dump_counter(&s_flash_stats.erase, "erase");
  460. }
  461. #endif //CONFIG_SPI_FLASH_ENABLE_COUNTERS