esp_flash_api.c 23 KB

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