esp_flash_api.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. // Copyright 2015-2019 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 <stdio.h>
  16. #include <sys/param.h>
  17. #include <string.h>
  18. #include "spi_flash_chip_driver.h"
  19. #include "memspi_host_driver.h"
  20. #include "esp_log.h"
  21. #include "sdkconfig.h"
  22. #include "esp_heap_caps.h"
  23. #include "esp_flash_internal.h"
  24. static const char TAG[] = "spi_flash";
  25. #define MAX_WRITE_CHUNK 8192 /* write in chunks */
  26. #define MAX_READ_CHUNK 16384
  27. #ifdef CONFIG_SPI_FLASH_DANGEROUS_WRITE_ABORTS
  28. #define UNSAFE_WRITE_ADDRESS abort()
  29. #else
  30. #define UNSAFE_WRITE_ADDRESS return ESP_ERR_INVALID_ARG
  31. #endif
  32. /* CHECK_WRITE_ADDRESS macro to fail writes which land in the
  33. bootloader, partition table, or running application region.
  34. */
  35. #if CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED
  36. #define CHECK_WRITE_ADDRESS(CHIP, ADDR, SIZE)
  37. #else /* FAILS or ABORTS */
  38. #define CHECK_WRITE_ADDRESS(CHIP, ADDR, SIZE) do { \
  39. if (CHIP && CHIP->os_func->region_protected && CHIP->os_func->region_protected(CHIP->os_func_data, ADDR, SIZE)) { \
  40. UNSAFE_WRITE_ADDRESS; \
  41. } \
  42. } while(0)
  43. #endif // CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED
  44. #define IO_STR_LEN 7
  45. static const char io_mode_str[][IO_STR_LEN] = {
  46. "slowrd",
  47. "fastrd",
  48. "dout",
  49. "dio",
  50. "qout",
  51. "qio",
  52. };
  53. _Static_assert(sizeof(io_mode_str)/IO_STR_LEN == SPI_FLASH_READ_MODE_MAX, "the io_mode_str should be consistent with the esp_flash_read_mode_t defined in spi_flash_ll.h");
  54. /* Static function to notify OS of a new SPI flash operation.
  55. If returns an error result, caller must abort. If returns ESP_OK, caller must
  56. call spiflash_end() before returning.
  57. */
  58. static esp_err_t IRAM_ATTR spiflash_start(esp_flash_t *chip)
  59. {
  60. if (chip->os_func != NULL && chip->os_func->start != NULL) {
  61. esp_err_t err = chip->os_func->start(chip->os_func_data);
  62. if (err != ESP_OK) {
  63. return err;
  64. }
  65. }
  66. chip->host->dev_config(chip->host);
  67. return ESP_OK;
  68. }
  69. /* Static function to notify OS that SPI flash operation is complete.
  70. */
  71. static esp_err_t IRAM_ATTR spiflash_end(const esp_flash_t *chip, esp_err_t err)
  72. {
  73. if (chip->os_func != NULL
  74. && chip->os_func->end != NULL) {
  75. esp_err_t end_err = chip->os_func->end(chip->os_func_data);
  76. if (err == ESP_OK) {
  77. err = end_err; // Only return the 'end' error if we haven't already failed
  78. }
  79. }
  80. return err;
  81. }
  82. /* Return true if regions 'a' and 'b' overlap at all, based on their start offsets and lengths. */
  83. inline static bool regions_overlap(uint32_t a_start, uint32_t a_len,uint32_t b_start, uint32_t b_len);
  84. /* Top-level API functions, calling into chip_drv functions via chip->drv */
  85. static esp_err_t detect_spi_flash_chip(esp_flash_t *chip);
  86. bool esp_flash_chip_driver_initialized(const esp_flash_t *chip)
  87. {
  88. if (!chip->chip_drv) return false;
  89. return true;
  90. }
  91. esp_err_t IRAM_ATTR esp_flash_init(esp_flash_t *chip)
  92. {
  93. esp_err_t err = ESP_OK;
  94. if (chip == NULL || chip->host == NULL || chip->host->driver_data == NULL ||
  95. ((memspi_host_data_t*)chip->host->driver_data)->spi == NULL) {
  96. return ESP_ERR_INVALID_ARG;
  97. }
  98. if (!esp_flash_chip_driver_initialized(chip)) {
  99. // Detect chip_drv
  100. err = detect_spi_flash_chip(chip);
  101. if (err != ESP_OK) {
  102. return err;
  103. }
  104. }
  105. // Detect flash size
  106. uint32_t size;
  107. err = esp_flash_get_size(chip, &size);
  108. if (err != ESP_OK) {
  109. ESP_LOGE(TAG, "failed to get chip size");
  110. return err;
  111. }
  112. ESP_LOGI(TAG, "flash io: %s", io_mode_str[chip->read_mode]);
  113. err = spiflash_start(chip);
  114. if (err != ESP_OK) {
  115. return err;
  116. }
  117. if (err == ESP_OK) {
  118. // Try to set the flash mode to whatever default mode was chosen
  119. err = chip->chip_drv->set_read_mode(chip);
  120. }
  121. // Done: all fields on 'chip' are initialised
  122. return spiflash_end(chip, err);
  123. }
  124. static esp_err_t IRAM_ATTR detect_spi_flash_chip(esp_flash_t *chip)
  125. {
  126. esp_err_t err;
  127. uint32_t flash_id;
  128. int retries = 10;
  129. do {
  130. err = spiflash_start(chip);
  131. if (err != ESP_OK) {
  132. return err;
  133. }
  134. // Send generic RDID command twice, check for a matching result and retry in case we just powered on (inner
  135. // function fails if it sees all-ones or all-zeroes.)
  136. err = chip->host->read_id(chip->host, &flash_id);
  137. if (err == ESP_OK) { // check we see the same ID twice, in case of transient power-on errors
  138. uint32_t new_id;
  139. err = chip->host->read_id(chip->host, &new_id);
  140. if (err == ESP_OK && (new_id != flash_id)) {
  141. err = ESP_ERR_FLASH_NOT_INITIALISED;
  142. }
  143. }
  144. err = spiflash_end(chip, err);
  145. } while (err != ESP_OK && retries-- > 0);
  146. // Detect the chip and set the chip_drv structure for it
  147. const spi_flash_chip_t **drivers = esp_flash_registered_chips;
  148. while (*drivers != NULL && !esp_flash_chip_driver_initialized(chip)) {
  149. chip->chip_drv = *drivers;
  150. // start/end SPI operation each time, for multitasking
  151. // and also so esp_flash_registered_flash_drivers can live in flash
  152. ESP_LOGD(TAG, "trying chip: %s", chip->chip_drv->name);
  153. err = spiflash_start(chip);
  154. if (err != ESP_OK) {
  155. return err;
  156. }
  157. if (chip->chip_drv->probe(chip, flash_id) != ESP_OK) {
  158. chip->chip_drv = NULL;
  159. }
  160. // if probe succeeded, chip->drv stays set
  161. drivers++;
  162. err = spiflash_end(chip, err);
  163. if (err != ESP_OK) {
  164. return err;
  165. }
  166. }
  167. if (!esp_flash_chip_driver_initialized(chip)) {
  168. return ESP_ERR_NOT_FOUND;
  169. }
  170. ESP_LOGI(TAG, "detected chip: %s", chip->chip_drv->name);
  171. return ESP_OK;
  172. }
  173. // Convenience macro for beginning of all API functions,
  174. // check that the 'chip' parameter is properly initialised
  175. // and supports the operation in question
  176. #define VERIFY_OP(OP) do { \
  177. if (chip == NULL) { \
  178. chip = esp_flash_default_chip; \
  179. } \
  180. if (chip == NULL || !esp_flash_chip_driver_initialized(chip)) { \
  181. return ESP_ERR_FLASH_NOT_INITIALISED; \
  182. } \
  183. if (chip->chip_drv->OP == NULL) { \
  184. return ESP_ERR_FLASH_UNSUPPORTED_CHIP; \
  185. } \
  186. } while (0)
  187. esp_err_t IRAM_ATTR esp_flash_read_id(esp_flash_t *chip, uint32_t *out_id)
  188. {
  189. if (chip == NULL) {
  190. chip = esp_flash_default_chip;
  191. }
  192. if (chip == NULL || !esp_flash_chip_driver_initialized(chip)) {
  193. return ESP_ERR_FLASH_NOT_INITIALISED;
  194. }
  195. if (out_id == NULL) {
  196. return ESP_ERR_INVALID_ARG;
  197. }
  198. esp_err_t err = spiflash_start(chip);
  199. if (err != ESP_OK) {
  200. return err;
  201. }
  202. err = chip->host->read_id(chip->host, out_id);
  203. return spiflash_end(chip, err);
  204. }
  205. esp_err_t IRAM_ATTR esp_flash_get_size(esp_flash_t *chip, uint32_t *out_size)
  206. {
  207. VERIFY_OP(detect_size);
  208. if (out_size == NULL) {
  209. return ESP_ERR_INVALID_ARG;
  210. }
  211. if (chip->size != 0) {
  212. *out_size = chip->size;
  213. return ESP_OK;
  214. }
  215. esp_err_t err = spiflash_start(chip);
  216. if (err != ESP_OK) {
  217. return err;
  218. }
  219. uint32_t detect_size;
  220. err = chip->chip_drv->detect_size(chip, &detect_size);
  221. if (err == ESP_OK) {
  222. chip->size = detect_size;
  223. }
  224. return spiflash_end(chip, err);
  225. }
  226. esp_err_t IRAM_ATTR esp_flash_erase_chip(esp_flash_t *chip)
  227. {
  228. VERIFY_OP(erase_chip);
  229. CHECK_WRITE_ADDRESS(chip, 0, chip->size);
  230. esp_err_t err = spiflash_start(chip);
  231. if (err != ESP_OK) {
  232. return err;
  233. }
  234. err = chip->chip_drv->erase_chip(chip);
  235. return spiflash_end(chip, err);
  236. }
  237. esp_err_t IRAM_ATTR esp_flash_erase_region(esp_flash_t *chip, uint32_t start, uint32_t len)
  238. {
  239. VERIFY_OP(erase_sector);
  240. VERIFY_OP(erase_block);
  241. CHECK_WRITE_ADDRESS(chip, start, len);
  242. uint32_t block_erase_size = chip->chip_drv->erase_block == NULL ? 0 : chip->chip_drv->block_erase_size;
  243. uint32_t sector_size = chip->chip_drv->sector_size;
  244. if (sector_size == 0 || (block_erase_size % sector_size) != 0) {
  245. return ESP_ERR_FLASH_NOT_INITIALISED;
  246. }
  247. if (start > chip->size || start + len > chip->size) {
  248. return ESP_ERR_INVALID_ARG;
  249. }
  250. if ((start % chip->chip_drv->sector_size) != 0 || (len % chip->chip_drv->sector_size) != 0) {
  251. // Can only erase multiples of the sector size, starting at sector boundary
  252. return ESP_ERR_INVALID_ARG;
  253. }
  254. esp_err_t err = ESP_OK;
  255. // Check for write protected regions overlapping the erase region
  256. if (chip->chip_drv->get_protected_regions != NULL &&
  257. chip->chip_drv->num_protectable_regions > 0) {
  258. err = spiflash_start(chip);
  259. if (err != ESP_OK) {
  260. return err;
  261. }
  262. uint64_t protected = 0;
  263. err = chip->chip_drv->get_protected_regions(chip, &protected);
  264. if (err == ESP_OK && protected != 0) {
  265. for (int i = 0; i < chip->chip_drv->num_protectable_regions && err == ESP_OK; i++) {
  266. const esp_flash_region_t *region = &chip->chip_drv->protectable_regions[i];
  267. if ((protected & BIT64(i))
  268. && regions_overlap(start, len, region->offset, region->size)) {
  269. err = ESP_ERR_FLASH_PROTECTED;
  270. }
  271. }
  272. }
  273. // Don't lock the SPI flash for the entire erase, as this may be very long
  274. err = spiflash_end(chip, err);
  275. }
  276. while (err == ESP_OK && len >= sector_size) {
  277. err = spiflash_start(chip);
  278. if (err != ESP_OK) {
  279. return err;
  280. }
  281. // If possible erase an entire multi-sector block
  282. if (block_erase_size > 0 && len >= block_erase_size && (start % block_erase_size) == 0) {
  283. err = chip->chip_drv->erase_block(chip, start);
  284. start += block_erase_size;
  285. len -= block_erase_size;
  286. }
  287. else {
  288. // Otherwise erase individual sector only
  289. err = chip->chip_drv->erase_sector(chip, start);
  290. start += sector_size;
  291. len -= sector_size;
  292. }
  293. err = spiflash_end(chip, err);
  294. }
  295. return err;
  296. }
  297. esp_err_t IRAM_ATTR esp_flash_get_chip_write_protect(esp_flash_t *chip, bool *out_write_protected)
  298. {
  299. VERIFY_OP(get_chip_write_protect);
  300. if (out_write_protected == NULL) {
  301. return ESP_ERR_INVALID_ARG;
  302. }
  303. esp_err_t err = spiflash_start(chip);
  304. if (err != ESP_OK) {
  305. return err;
  306. }
  307. err = chip->chip_drv->get_chip_write_protect(chip, out_write_protected);
  308. return spiflash_end(chip, err);
  309. }
  310. esp_err_t IRAM_ATTR esp_flash_set_chip_write_protect(esp_flash_t *chip, bool write_protect)
  311. {
  312. VERIFY_OP(set_chip_write_protect);
  313. //TODO: skip writing if already locked or unlocked
  314. esp_err_t err = spiflash_start(chip);
  315. if (err != ESP_OK) {
  316. return err;
  317. }
  318. err = chip->chip_drv->set_chip_write_protect(chip, write_protect);
  319. return spiflash_end(chip, err);
  320. }
  321. esp_err_t esp_flash_get_protectable_regions(const esp_flash_t *chip, const esp_flash_region_t **out_regions, uint32_t *out_num_regions)
  322. {
  323. if(out_num_regions != NULL) {
  324. *out_num_regions = 0; // In case caller doesn't check result
  325. }
  326. VERIFY_OP(get_protected_regions);
  327. if(out_regions == NULL || out_num_regions == NULL) {
  328. return ESP_ERR_INVALID_ARG;
  329. }
  330. *out_num_regions = chip->chip_drv->num_protectable_regions;
  331. *out_regions = chip->chip_drv->protectable_regions;
  332. return ESP_OK;
  333. }
  334. static esp_err_t find_region(const esp_flash_t *chip, const esp_flash_region_t *region, uint8_t *index)
  335. {
  336. if (region == NULL) {
  337. return ESP_ERR_INVALID_ARG;
  338. }
  339. for(*index = 0; *index < chip->chip_drv->num_protectable_regions; (*index)++) {
  340. if (memcmp(&chip->chip_drv->protectable_regions[*index],
  341. region, sizeof(esp_flash_region_t)) == 0) {
  342. return ESP_OK;
  343. }
  344. }
  345. return ESP_ERR_NOT_FOUND;
  346. }
  347. esp_err_t IRAM_ATTR esp_flash_get_protected_region(esp_flash_t *chip, const esp_flash_region_t *region, bool *out_protected)
  348. {
  349. VERIFY_OP(get_protected_regions);
  350. if (out_protected == NULL) {
  351. return ESP_ERR_INVALID_ARG;
  352. }
  353. uint8_t index;
  354. esp_err_t err = find_region(chip, region, &index);
  355. if (err != ESP_OK) {
  356. return err;
  357. }
  358. uint64_t protection_mask = 0;
  359. err = spiflash_start(chip);
  360. if (err != ESP_OK) {
  361. return err;
  362. }
  363. err = chip->chip_drv->get_protected_regions(chip, &protection_mask);
  364. if (err == ESP_OK) {
  365. *out_protected = protection_mask & (1LL << index);
  366. }
  367. return spiflash_end(chip, err);
  368. }
  369. esp_err_t IRAM_ATTR esp_flash_set_protected_region(esp_flash_t *chip, const esp_flash_region_t *region, bool protect)
  370. {
  371. VERIFY_OP(set_protected_regions);
  372. uint8_t index;
  373. esp_err_t err = find_region(chip, region, &index);
  374. if (err != ESP_OK) {
  375. return err;
  376. }
  377. uint64_t protection_mask = 0;
  378. err = spiflash_start(chip);
  379. if (err != ESP_OK) {
  380. return err;
  381. }
  382. err = chip->chip_drv->get_protected_regions(chip, &protection_mask);
  383. if (err == ESP_OK) {
  384. if (protect) {
  385. protection_mask |= (1LL << index);
  386. } else {
  387. protection_mask &= ~(1LL << index);
  388. }
  389. err = chip->chip_drv->set_protected_regions(chip, protection_mask);
  390. }
  391. return spiflash_end(chip, err);
  392. }
  393. esp_err_t IRAM_ATTR esp_flash_read(esp_flash_t *chip, void *buffer, uint32_t address, uint32_t length)
  394. {
  395. if (length == 0) {
  396. return ESP_OK;
  397. }
  398. VERIFY_OP(read);
  399. if (buffer == NULL || address > chip->size || address+length > chip->size) {
  400. return ESP_ERR_INVALID_ARG;
  401. }
  402. //when the cache is disabled, only the DRAM can be read, check whether we need to receive in another buffer in DRAM.
  403. bool direct_read = chip->host->supports_direct_read(chip->host, buffer);
  404. uint8_t* temp_buffer = NULL;
  405. if (!direct_read) {
  406. uint32_t length_to_allocate = MAX(MAX_READ_CHUNK, length);
  407. length_to_allocate = (length_to_allocate+3)&(~3);
  408. temp_buffer = heap_caps_malloc(length_to_allocate, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
  409. ESP_LOGV(TAG, "allocate temp buffer: %p", temp_buffer);
  410. if (temp_buffer == NULL) return ESP_ERR_NO_MEM;
  411. }
  412. esp_err_t err = ESP_OK;
  413. do {
  414. err = spiflash_start(chip);
  415. if (err != ESP_OK) {
  416. break;
  417. }
  418. //if required (dma buffer allocated), read to the buffer instead of the original buffer
  419. uint8_t* buffer_to_read = (temp_buffer)? temp_buffer : buffer;
  420. //each time, we at most read this length
  421. //after that, we release the lock to allow some other operations
  422. uint32_t length_to_read = MIN(MAX_READ_CHUNK, length);
  423. if (err == ESP_OK) {
  424. err = chip->chip_drv->read(chip, buffer_to_read, address, length_to_read);
  425. }
  426. if (err != ESP_OK) {
  427. spiflash_end(chip, err);
  428. break;
  429. }
  430. //even if this is failed, the data is still valid, copy before quit
  431. err = spiflash_end(chip, err);
  432. //copy back to the original buffer
  433. if (temp_buffer) {
  434. memcpy(buffer, temp_buffer, length_to_read);
  435. }
  436. address += length_to_read;
  437. length -= length_to_read;
  438. buffer += length_to_read;
  439. } while (err == ESP_OK && length > 0);
  440. free(temp_buffer);
  441. return err;
  442. }
  443. esp_err_t IRAM_ATTR esp_flash_write(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length)
  444. {
  445. if (length == 0) {
  446. return ESP_OK;
  447. }
  448. VERIFY_OP(write);
  449. CHECK_WRITE_ADDRESS(chip, address, length);
  450. if (buffer == NULL || address > chip->size || address+length > chip->size) {
  451. return ESP_ERR_INVALID_ARG;
  452. }
  453. //when the cache is disabled, only the DRAM can be read, check whether we need to copy the data first
  454. bool direct_write = chip->host->supports_direct_write(chip->host, buffer);
  455. esp_err_t err = ESP_OK;
  456. /* Write output in chunks, either by buffering on stack or
  457. by artificially cutting into MAX_WRITE_CHUNK parts (in an OS
  458. environment, this prevents writing from causing interrupt or higher priority task
  459. starvation.) */
  460. do {
  461. uint32_t write_len;
  462. const void *write_buf;
  463. if (direct_write) {
  464. write_len = MIN(length, MAX_WRITE_CHUNK);
  465. write_buf = buffer;
  466. } else {
  467. uint32_t buf[8];
  468. write_len = MIN(length, sizeof(buf));
  469. memcpy(buf, buffer, write_len);
  470. write_buf = buf;
  471. }
  472. err = spiflash_start(chip);
  473. if (err != ESP_OK) {
  474. return err;
  475. }
  476. err = chip->chip_drv->write(chip, write_buf, address, write_len);
  477. address += write_len;
  478. buffer = (void *)((intptr_t)buffer + write_len);
  479. length -= write_len;
  480. err = spiflash_end(chip, err);
  481. } while (err == ESP_OK && length > 0);
  482. return err;
  483. }
  484. //currently the legacy implementation is used, from flash_ops.c
  485. esp_err_t spi_flash_write_encrypted(size_t dest_addr, const void *src, size_t size);
  486. esp_err_t IRAM_ATTR esp_flash_write_encrypted(esp_flash_t *chip, uint32_t address, const void *buffer, uint32_t length)
  487. {
  488. /*
  489. * Since currently this feature is supported only by the hardware, there
  490. * is no way to support non-standard chips. We use the legacy
  491. * implementation and skip the chip and driver layers.
  492. */
  493. if (chip == NULL) {
  494. chip = esp_flash_default_chip;
  495. } else if (chip != esp_flash_default_chip) {
  496. return ESP_ERR_NOT_SUPPORTED;
  497. }
  498. if (buffer == NULL || address > chip->size || address+length > chip->size) {
  499. return ESP_ERR_INVALID_ARG;
  500. }
  501. return spi_flash_write_encrypted(address, buffer, length);
  502. }
  503. inline static IRAM_ATTR bool regions_overlap(uint32_t a_start, uint32_t a_len,uint32_t b_start, uint32_t b_len)
  504. {
  505. uint32_t a_end = a_start + a_len;
  506. uint32_t b_end = b_start + b_len;
  507. return (a_end > b_start && b_end > a_start);
  508. }
  509. //currently the legacy implementation is used, from flash_ops.c
  510. esp_err_t spi_flash_read_encrypted(size_t src, void *dstv, size_t size);
  511. esp_err_t IRAM_ATTR esp_flash_read_encrypted(esp_flash_t *chip, uint32_t address, void *out_buffer, uint32_t length)
  512. {
  513. /*
  514. * Since currently this feature is supported only by the hardware, there
  515. * is no way to support non-standard chips. We use the legacy
  516. * implementation and skip the chip and driver layers.
  517. */
  518. if (chip == NULL) {
  519. chip = esp_flash_default_chip;
  520. } else if (chip != esp_flash_default_chip) {
  521. return ESP_ERR_NOT_SUPPORTED;
  522. }
  523. return spi_flash_read_encrypted(address, out_buffer, length);
  524. }
  525. #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  526. esp_err_t esp_flash_app_disable_protect(bool disable)
  527. {
  528. if (disable) {
  529. return esp_flash_app_disable_os_functions(esp_flash_default_chip);
  530. } else {
  531. return esp_flash_app_init_os_functions(esp_flash_default_chip);
  532. }
  533. }
  534. #endif
  535. /*------------------------------------------------------------------------------
  536. Adapter layer to original api before IDF v4.0
  537. ------------------------------------------------------------------------------*/
  538. #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  539. static IRAM_ATTR esp_err_t spi_flash_translate_rc(esp_err_t err)
  540. {
  541. switch (err) {
  542. case ESP_OK:
  543. return ESP_OK;
  544. case ESP_ERR_INVALID_ARG:
  545. return ESP_ERR_INVALID_ARG;
  546. case ESP_ERR_FLASH_NOT_INITIALISED:
  547. case ESP_ERR_FLASH_PROTECTED:
  548. return ESP_ERR_INVALID_STATE;
  549. case ESP_ERR_NOT_FOUND:
  550. case ESP_ERR_FLASH_UNSUPPORTED_HOST:
  551. case ESP_ERR_FLASH_UNSUPPORTED_CHIP:
  552. return ESP_ERR_NOT_SUPPORTED;
  553. case ESP_ERR_FLASH_NO_RESPONSE:
  554. return ESP_ERR_INVALID_RESPONSE;
  555. default:
  556. ESP_EARLY_LOGE(TAG, "unexpected spi flash error code: %x", err);
  557. abort();
  558. }
  559. return ESP_OK;
  560. }
  561. esp_err_t IRAM_ATTR spi_flash_erase_range(uint32_t start_addr, uint32_t size)
  562. {
  563. esp_err_t err = esp_flash_erase_region(NULL, start_addr, size);
  564. return spi_flash_translate_rc(err);
  565. }
  566. esp_err_t IRAM_ATTR spi_flash_write(size_t dst, const void *srcv, size_t size)
  567. {
  568. esp_err_t err = esp_flash_write(NULL, srcv, dst, size);
  569. return spi_flash_translate_rc(err);
  570. }
  571. esp_err_t IRAM_ATTR spi_flash_read(size_t src, void *dstv, size_t size)
  572. {
  573. esp_err_t err = esp_flash_read(NULL, dstv, src, size);
  574. return spi_flash_translate_rc(err);
  575. }
  576. #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL