esp_flash_api.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <sys/param.h>
  9. #include <string.h>
  10. #include "spi_flash_chip_driver.h"
  11. #include "memspi_host_driver.h"
  12. #include "esp_log.h"
  13. #include "sdkconfig.h"
  14. #include "esp_flash_internal.h"
  15. #include "spi_flash_defs.h"
  16. #include "esp_rom_caps.h"
  17. #include "esp_rom_spiflash.h"
  18. #if CONFIG_IDF_TARGET_ESP32S2
  19. #include "esp_crypto_lock.h" // for locking flash encryption peripheral
  20. #endif //CONFIG_IDF_TARGET_ESP32S2
  21. static const char TAG[] = "spi_flash";
  22. #ifdef CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE
  23. #define MAX_WRITE_CHUNK CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE /* write in chunks */
  24. #else
  25. #define MAX_WRITE_CHUNK 8192 /* write in chunks */
  26. #endif // CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE
  27. #define MAX_READ_CHUNK 16384
  28. #ifdef CONFIG_SPI_FLASH_DANGEROUS_WRITE_ABORTS
  29. #define UNSAFE_WRITE_ADDRESS abort()
  30. #else
  31. #define UNSAFE_WRITE_ADDRESS return ESP_ERR_INVALID_ARG
  32. #endif
  33. /* CHECK_WRITE_ADDRESS macro to fail writes which land in the
  34. bootloader, partition table, or running application region.
  35. */
  36. #if CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED
  37. #define CHECK_WRITE_ADDRESS(CHIP, ADDR, SIZE)
  38. #else /* FAILS or ABORTS */
  39. #define CHECK_WRITE_ADDRESS(CHIP, ADDR, SIZE) do { \
  40. if (CHIP && CHIP->os_func->region_protected && CHIP->os_func->region_protected(CHIP->os_func_data, ADDR, SIZE)) { \
  41. UNSAFE_WRITE_ADDRESS; \
  42. } \
  43. } while(0)
  44. #endif // CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED
  45. #define IO_STR_LEN 10
  46. static const char io_mode_str[][IO_STR_LEN] = {
  47. "slowrd",
  48. "fastrd",
  49. "dout",
  50. "dio",
  51. "qout",
  52. "qio",
  53. [6 ... 15] = "not used", // reserved io mode for future, not used currently.
  54. "opi_str",
  55. "opi_dtr",
  56. };
  57. _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_types.h");
  58. esp_err_t esp_flash_read_chip_id(esp_flash_t* chip, uint32_t* flash_id);
  59. #ifndef CONFIG_SPI_FLASH_ROM_IMPL
  60. static esp_err_t spiflash_start_default(esp_flash_t *chip);
  61. static esp_err_t spiflash_end_default(esp_flash_t *chip, esp_err_t err);
  62. static esp_err_t check_chip_pointer_default(esp_flash_t **inout_chip);
  63. static esp_err_t flash_end_flush_cache(esp_flash_t* chip, esp_err_t err, bool bus_acquired, uint32_t address, uint32_t length);
  64. #endif //CONFIG_SPI_FLASH_ROM_IMPL
  65. typedef struct {
  66. esp_err_t (*start)(esp_flash_t *chip);
  67. esp_err_t (*end)(esp_flash_t *chip, esp_err_t err);
  68. esp_err_t (*chip_check)(esp_flash_t **inout_chip);
  69. esp_err_t (*flash_end_flush_cache)(esp_flash_t* chip, esp_err_t err, bool bus_acquired, uint32_t address, uint32_t length);
  70. } rom_spiflash_api_func_t;
  71. #ifndef CONFIG_SPI_FLASH_ROM_IMPL
  72. // These functions can be placed in the ROM. For now we use the code in IDF.
  73. DRAM_ATTR static rom_spiflash_api_func_t default_spiflash_rom_api = {
  74. .start = spiflash_start_default,
  75. .end = spiflash_end_default,
  76. .chip_check = check_chip_pointer_default,
  77. .flash_end_flush_cache = flash_end_flush_cache,
  78. };
  79. DRAM_ATTR rom_spiflash_api_func_t *rom_spiflash_api_funcs = &default_spiflash_rom_api;
  80. #else
  81. extern rom_spiflash_api_func_t *esp_flash_api_funcs;
  82. #define rom_spiflash_api_funcs esp_flash_api_funcs
  83. #endif // CONFIG_SPI_FLASH_ROM_IMPL
  84. /* Static function to notify OS of a new SPI flash operation.
  85. If returns an error result, caller must abort. If returns ESP_OK, caller must
  86. call rom_spiflash_api_funcs->end() before returning.
  87. */
  88. #ifndef CONFIG_SPI_FLASH_ROM_IMPL
  89. static esp_err_t IRAM_ATTR spiflash_start_default(esp_flash_t *chip)
  90. {
  91. if (chip->os_func != NULL && chip->os_func->start != NULL) {
  92. esp_err_t err = chip->os_func->start(chip->os_func_data);
  93. if (err != ESP_OK) {
  94. return err;
  95. }
  96. }
  97. chip->host->driver->dev_config(chip->host);
  98. return ESP_OK;
  99. }
  100. /* Static function to notify OS that SPI flash operation is complete.
  101. */
  102. static esp_err_t IRAM_ATTR spiflash_end_default(esp_flash_t *chip, esp_err_t err)
  103. {
  104. if (chip->os_func != NULL
  105. && chip->os_func->end != NULL) {
  106. esp_err_t end_err = chip->os_func->end(chip->os_func_data);
  107. if (err == ESP_OK) {
  108. err = end_err; // Only return the 'end' error if we haven't already failed
  109. }
  110. }
  111. return err;
  112. }
  113. // check that the 'chip' parameter is properly initialised
  114. static esp_err_t check_chip_pointer_default(esp_flash_t **inout_chip)
  115. {
  116. esp_flash_t *chip = *inout_chip;
  117. if (chip == NULL) {
  118. chip = esp_flash_default_chip;
  119. }
  120. *inout_chip = chip;
  121. if (chip == NULL || !esp_flash_chip_driver_initialized(chip)) {
  122. return ESP_ERR_FLASH_NOT_INITIALISED;
  123. }
  124. return ESP_OK;
  125. }
  126. static IRAM_ATTR esp_err_t flash_end_flush_cache(esp_flash_t* chip, esp_err_t err, bool bus_acquired, uint32_t address, uint32_t length)
  127. {
  128. if (!bus_acquired) {
  129. // Try to acquire the bus again to flush the cache before exit.
  130. esp_err_t acquire_err = rom_spiflash_api_funcs->start(chip);
  131. if (acquire_err != ESP_OK) {
  132. return (err == ESP_OK)? acquire_err: err;
  133. }
  134. }
  135. if (chip->host->driver->flush_cache) {
  136. esp_err_t flush_err = chip->host->driver->flush_cache(chip->host, address, length);
  137. if (err == ESP_OK) {
  138. err = flush_err;
  139. }
  140. }
  141. return rom_spiflash_api_funcs->end(chip, err);
  142. }
  143. #endif //CONFIG_SPI_FLASH_ROM_IMPL
  144. /* Top-level API functions, calling into chip_drv functions via chip->drv */
  145. static esp_err_t detect_spi_flash_chip(esp_flash_t *chip);
  146. bool esp_flash_chip_driver_initialized(const esp_flash_t *chip)
  147. {
  148. if (!chip->chip_drv) return false;
  149. return true;
  150. }
  151. esp_err_t IRAM_ATTR esp_flash_init(esp_flash_t *chip)
  152. {
  153. // Chip init flow
  154. // 1. Read chip id
  155. // 2. (optional) Detect chip vendor
  156. // 3. Get basic parameters of the chip (size, dummy count, etc.)
  157. // 4. Init chip into desired mode (without breaking the cache!)
  158. esp_err_t err = ESP_OK;
  159. if (chip == NULL || chip->host == NULL || chip->host->driver == NULL ||
  160. ((memspi_host_inst_t*)chip->host)->spi == NULL) {
  161. return ESP_ERR_INVALID_ARG;
  162. }
  163. //read chip id
  164. uint32_t flash_id;
  165. int retries = 10;
  166. do {
  167. err = esp_flash_read_chip_id(chip, &flash_id);
  168. } while (err == ESP_ERR_FLASH_NOT_INITIALISED && retries-- > 0);
  169. if (err != ESP_OK) {
  170. return err;
  171. }
  172. chip->chip_id = flash_id;
  173. if (!esp_flash_chip_driver_initialized(chip)) {
  174. // Detect chip_drv
  175. err = detect_spi_flash_chip(chip);
  176. if (err != ESP_OK) {
  177. return err;
  178. }
  179. }
  180. // Detect flash size
  181. uint32_t size;
  182. err = esp_flash_get_size(chip, &size);
  183. if (err != ESP_OK) {
  184. ESP_LOGE(TAG, "failed to get chip size");
  185. return err;
  186. }
  187. if (chip->chip_drv->get_chip_caps == NULL) {
  188. // chip caps get failed, pass the flash capability check.
  189. ESP_EARLY_LOGW(TAG, "get_chip_caps function pointer hasn't been initialized");
  190. } else {
  191. if (((chip->chip_drv->get_chip_caps(chip) & SPI_FLASH_CHIP_CAP_32MB_SUPPORT) == 0) && (size > (16 *1024 * 1024))) {
  192. ESP_EARLY_LOGW(TAG, "Detected flash size > 16 MB, but access beyond 16 MB is not supported for this flash model yet.");
  193. size = (16 * 1024 * 1024);
  194. }
  195. }
  196. ESP_LOGI(TAG, "flash io: %s", io_mode_str[chip->read_mode]);
  197. err = rom_spiflash_api_funcs->start(chip);
  198. if (err != ESP_OK) {
  199. return err;
  200. }
  201. if (err == ESP_OK) {
  202. // Try to set the flash mode to whatever default mode was chosen
  203. err = chip->chip_drv->set_io_mode(chip);
  204. if (err == ESP_ERR_FLASH_NO_RESPONSE && !esp_flash_is_quad_mode(chip)) {
  205. //some chips (e.g. Winbond) don't support to clear QE, treat as success
  206. err = ESP_OK;
  207. }
  208. }
  209. // Done: all fields on 'chip' are initialised
  210. return rom_spiflash_api_funcs->end(chip, err);
  211. }
  212. // Note: This function is only used for internal. Only call this function to initialize the main flash.
  213. // (flash chip on SPI1 CS0)
  214. esp_err_t IRAM_ATTR esp_flash_init_main(esp_flash_t *chip)
  215. {
  216. // Chip init flow
  217. // 1. Read chip id
  218. // 2. (optional) Detect chip vendor
  219. // 3. Get basic parameters of the chip (size, dummy count, etc.)
  220. // 4. Init chip into desired mode (without breaking the cache!)
  221. esp_err_t err = ESP_OK;
  222. bool octal_mode;
  223. if (chip == NULL || chip->host == NULL || chip->host->driver == NULL ||
  224. ((memspi_host_inst_t*)chip->host)->spi == NULL) {
  225. return ESP_ERR_INVALID_ARG;
  226. }
  227. octal_mode = (chip->read_mode >= SPI_FLASH_OPI_FLAG);
  228. //read chip id
  229. // This can indicate the MSPI support OPI, if the flash works on MSPI in OPI mode, we directly bypass read id.
  230. uint32_t flash_id = 0;
  231. if (octal_mode) {
  232. // bypass the reading but get the flash_id from the ROM variable, to avoid resetting the chip to QSPI mode and read the ID again
  233. flash_id = g_rom_flashchip.device_id;
  234. } else {
  235. int retries = 10;
  236. do {
  237. err = esp_flash_read_chip_id(chip, &flash_id);
  238. } while (err == ESP_ERR_FLASH_NOT_INITIALISED && retries-- > 0);
  239. }
  240. if (err != ESP_OK) {
  241. return err;
  242. }
  243. chip->chip_id = flash_id;
  244. if (!esp_flash_chip_driver_initialized(chip)) {
  245. // Detect chip_drv
  246. err = detect_spi_flash_chip(chip);
  247. if (err != ESP_OK) {
  248. return err;
  249. }
  250. }
  251. // Detect flash size
  252. uint32_t size;
  253. err = esp_flash_get_size(chip, &size);
  254. if (err != ESP_OK) {
  255. ESP_LOGE(TAG, "failed to get chip size");
  256. return err;
  257. }
  258. if (chip->chip_drv->get_chip_caps == NULL) {
  259. // chip caps get failed, pass the flash capability check.
  260. ESP_LOGW(TAG, "get_chip_caps function pointer hasn't been initialized");
  261. } else {
  262. if (((chip->chip_drv->get_chip_caps(chip) & SPI_FLASH_CHIP_CAP_32MB_SUPPORT) == 0) && (size > (16 *1024 * 1024))) {
  263. ESP_LOGW(TAG, "Detected flash size > 16 MB, but access beyond 16 MB is not supported for this flash model yet.");
  264. size = (16 * 1024 * 1024);
  265. }
  266. }
  267. ESP_LOGI(TAG, "flash io: %s", io_mode_str[chip->read_mode]);
  268. err = rom_spiflash_api_funcs->start(chip);
  269. if (err != ESP_OK) {
  270. return err;
  271. }
  272. if (err == ESP_OK && !octal_mode) {
  273. // Try to set the flash mode to whatever default mode was chosen
  274. err = chip->chip_drv->set_io_mode(chip);
  275. if (err == ESP_ERR_FLASH_NO_RESPONSE && !esp_flash_is_quad_mode(chip)) {
  276. //some chips (e.g. Winbond) don't support to clear QE, treat as success
  277. err = ESP_OK;
  278. }
  279. }
  280. // Done: all fields on 'chip' are initialised
  281. return rom_spiflash_api_funcs->end(chip, err);
  282. }
  283. static esp_err_t IRAM_ATTR read_id_core(esp_flash_t* chip, uint32_t* out_id, bool sanity_check)
  284. {
  285. bool installed = esp_flash_chip_driver_initialized(chip);
  286. esp_err_t err = rom_spiflash_api_funcs->start(chip);
  287. if (err != ESP_OK) {
  288. return err;
  289. }
  290. esp_err_t (*read_id_func)(void*, uint32_t*);
  291. void* read_id_arg;
  292. if (installed && chip->chip_drv->read_id) {
  293. read_id_func = (void*)chip->chip_drv->read_id;
  294. read_id_arg = (void*)chip;
  295. } else {
  296. //default option if the chip is not detected/chosen yet.
  297. read_id_func = (void*)chip->host->driver->read_id;
  298. read_id_arg = (void*)chip->host;
  299. }
  300. // Inner function fails if it sees all-ones or all-zeroes.
  301. err = read_id_func(read_id_arg, out_id);
  302. if (sanity_check && err == ESP_OK) {
  303. // Send RDID command twice, check for a matching result and retry in case we just powered on
  304. uint32_t new_id;
  305. err = read_id_func(read_id_arg, &new_id);
  306. if (err == ESP_OK && (new_id != *out_id)) {
  307. err = ESP_ERR_FLASH_NOT_INITIALISED;
  308. }
  309. }
  310. return rom_spiflash_api_funcs->end(chip, err);
  311. }
  312. // Faster version with sanity check.
  313. // Called in esp_flash_init and unit test (though not public)
  314. esp_err_t esp_flash_read_chip_id(esp_flash_t* chip, uint32_t* out_id)
  315. {
  316. return read_id_core(chip, out_id, true);
  317. }
  318. #ifndef CONFIG_SPI_FLASH_ROM_IMPL
  319. esp_err_t esp_flash_read_id(esp_flash_t* chip, uint32_t* out_id)
  320. {
  321. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  322. //Accept uninitialized chip when reading chip id
  323. if (err != ESP_OK && !(err == ESP_ERR_FLASH_NOT_INITIALISED && chip != NULL)) return err;
  324. if (out_id == NULL) return ESP_ERR_INVALID_ARG;
  325. return read_id_core(chip, out_id, false);
  326. }
  327. #endif //CONFIG_SPI_FLASH_ROM_IMPL
  328. static esp_err_t IRAM_ATTR NOINLINE_ATTR read_unique_id(esp_flash_t* chip, uint64_t* out_uid)
  329. {
  330. esp_err_t err = rom_spiflash_api_funcs->start(chip);
  331. if (err != ESP_OK) {
  332. return err;
  333. }
  334. err = chip->chip_drv->read_unique_id(chip, out_uid);
  335. return rom_spiflash_api_funcs->end(chip, err);
  336. }
  337. esp_err_t esp_flash_read_unique_chip_id(esp_flash_t *chip, uint64_t* out_uid)
  338. {
  339. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  340. if (err != ESP_OK) {
  341. return err;
  342. }
  343. if (chip->chip_drv->get_chip_caps == NULL) {
  344. // chip caps get failed, pass the flash capability check.
  345. ESP_EARLY_LOGW(TAG, "get_chip_caps function pointer hasn't been initialized");
  346. } else {
  347. if ((chip->chip_drv->get_chip_caps(chip) & SPI_FLASH_CHIP_CAP_UNIQUE_ID) == 0) {
  348. ESP_EARLY_LOGE(TAG, "chip %s doesn't support reading unique id", chip->chip_drv->name);
  349. return ESP_ERR_NOT_SUPPORTED;
  350. }
  351. }
  352. if (out_uid == NULL) {
  353. return ESP_ERR_INVALID_ARG;
  354. };
  355. return read_unique_id(chip, out_uid);
  356. }
  357. static esp_err_t IRAM_ATTR detect_spi_flash_chip(esp_flash_t *chip)
  358. {
  359. esp_err_t err;
  360. uint32_t flash_id = chip->chip_id;
  361. // Detect the chip and set the chip_drv structure for it
  362. const spi_flash_chip_t **drivers = esp_flash_registered_chips;
  363. while (*drivers != NULL && !esp_flash_chip_driver_initialized(chip)) {
  364. chip->chip_drv = *drivers;
  365. // start/end SPI operation each time, for multitasking
  366. // and also so esp_flash_registered_flash_drivers can live in flash
  367. ESP_LOGD(TAG, "trying chip: %s", chip->chip_drv->name);
  368. err = rom_spiflash_api_funcs->start(chip);
  369. if (err != ESP_OK) {
  370. return err;
  371. }
  372. if (chip->chip_drv->probe(chip, flash_id) != ESP_OK) {
  373. chip->chip_drv = NULL;
  374. }
  375. // if probe succeeded, chip->drv stays set
  376. drivers++;
  377. err = rom_spiflash_api_funcs->end(chip, err);
  378. if (err != ESP_OK) {
  379. return err;
  380. }
  381. }
  382. if (!esp_flash_chip_driver_initialized(chip)) {
  383. return ESP_ERR_NOT_FOUND;
  384. }
  385. ESP_LOGI(TAG, "detected chip: %s", chip->chip_drv->name);
  386. return ESP_OK;
  387. }
  388. #ifndef CONFIG_SPI_FLASH_ROM_IMPL
  389. /* Convenience macro for beginning of all API functions.
  390. * Check the return value of `rom_spiflash_api_funcs->chip_check` is correct,
  391. * and the chip supports the operation in question.
  392. */
  393. #define VERIFY_CHIP_OP(OP) do { \
  394. if (err != ESP_OK) return err; \
  395. if (chip->chip_drv->OP == NULL) { \
  396. return ESP_ERR_FLASH_UNSUPPORTED_CHIP; \
  397. } \
  398. } while (0)
  399. /* Return true if regions 'a' and 'b' overlap at all, based on their start offsets and lengths. */
  400. inline static bool regions_overlap(uint32_t a_start, uint32_t a_len,uint32_t b_start, uint32_t b_len);
  401. esp_err_t IRAM_ATTR esp_flash_get_size(esp_flash_t *chip, uint32_t *out_size)
  402. {
  403. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  404. VERIFY_CHIP_OP(detect_size);
  405. if (out_size == NULL) {
  406. return ESP_ERR_INVALID_ARG;
  407. }
  408. if (chip->size != 0) {
  409. *out_size = chip->size;
  410. return ESP_OK;
  411. }
  412. err = rom_spiflash_api_funcs->start(chip);
  413. if (err != ESP_OK) {
  414. return err;
  415. }
  416. uint32_t detect_size;
  417. err = chip->chip_drv->detect_size(chip, &detect_size);
  418. if (err == ESP_OK) {
  419. chip->size = detect_size;
  420. *out_size = chip->size;
  421. }
  422. return rom_spiflash_api_funcs->end(chip, err);
  423. }
  424. esp_err_t IRAM_ATTR esp_flash_erase_chip(esp_flash_t *chip)
  425. {
  426. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  427. VERIFY_CHIP_OP(erase_chip);
  428. CHECK_WRITE_ADDRESS(chip, 0, chip->size);
  429. //check before the operation, in case this is called too close to the last operation
  430. if (chip->chip_drv->yield) {
  431. err = chip->chip_drv->yield(chip, 0);
  432. if (err != ESP_OK) {
  433. return err;
  434. }
  435. }
  436. err = rom_spiflash_api_funcs->start(chip);
  437. if (err != ESP_OK) {
  438. return err;
  439. }
  440. err = chip->chip_drv->erase_chip(chip);
  441. if (chip->host->driver->flush_cache) {
  442. esp_err_t flush_cache_err = chip->host->driver->flush_cache(chip->host, 0, chip->size);
  443. if (err == ESP_OK) {
  444. err = flush_cache_err;
  445. }
  446. }
  447. return rom_spiflash_api_funcs->end(chip, err);
  448. }
  449. esp_err_t IRAM_ATTR esp_flash_erase_region(esp_flash_t *chip, uint32_t start, uint32_t len)
  450. {
  451. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  452. VERIFY_CHIP_OP(erase_sector);
  453. VERIFY_CHIP_OP(erase_block);
  454. CHECK_WRITE_ADDRESS(chip, start, len);
  455. uint32_t block_erase_size = chip->chip_drv->erase_block == NULL ? 0 : chip->chip_drv->block_erase_size;
  456. uint32_t sector_size = chip->chip_drv->sector_size;
  457. if (sector_size == 0 || (block_erase_size % sector_size) != 0) {
  458. return ESP_ERR_FLASH_NOT_INITIALISED;
  459. }
  460. if (start > chip->size || start + len > chip->size) {
  461. return ESP_ERR_INVALID_ARG;
  462. }
  463. if ((start % chip->chip_drv->sector_size) != 0 || (len % chip->chip_drv->sector_size) != 0) {
  464. // Can only erase multiples of the sector size, starting at sector boundary
  465. return ESP_ERR_INVALID_ARG;
  466. }
  467. if (len == 0) {
  468. return ESP_OK;
  469. }
  470. err = ESP_OK;
  471. // Check for write protected regions overlapping the erase region
  472. if (chip->chip_drv->get_protected_regions != NULL &&
  473. chip->chip_drv->num_protectable_regions > 0) {
  474. err = rom_spiflash_api_funcs->start(chip);
  475. if (err != ESP_OK) {
  476. return err;
  477. }
  478. uint64_t protected = 0;
  479. err = chip->chip_drv->get_protected_regions(chip, &protected);
  480. if (err == ESP_OK && protected != 0) {
  481. for (int i = 0; i < chip->chip_drv->num_protectable_regions && err == ESP_OK; i++) {
  482. const esp_flash_region_t *region = &chip->chip_drv->protectable_regions[i];
  483. if ((protected & BIT64(i))
  484. && regions_overlap(start, len, region->offset, region->size)) {
  485. err = ESP_ERR_FLASH_PROTECTED;
  486. }
  487. }
  488. }
  489. // Don't lock the SPI flash for the entire erase, as this may be very long
  490. err = rom_spiflash_api_funcs->end(chip, err);
  491. }
  492. if (err != ESP_OK) {
  493. return err;
  494. }
  495. uint32_t erase_addr = start;
  496. uint32_t len_remain = len;
  497. // Indicate whether the bus is acquired by the driver, needs to be released before return
  498. bool bus_acquired = false;
  499. while (1) {
  500. //check before the operation, in case this is called too close to the last operation
  501. if (chip->chip_drv->yield) {
  502. err = chip->chip_drv->yield(chip, 0);
  503. if (err != ESP_OK) {
  504. return err;
  505. }
  506. }
  507. err = rom_spiflash_api_funcs->start(chip);
  508. if (err != ESP_OK) {
  509. break;
  510. }
  511. bus_acquired = true;
  512. #ifndef CONFIG_SPI_FLASH_BYPASS_BLOCK_ERASE
  513. // If possible erase an entire multi-sector block
  514. if (block_erase_size > 0 && len_remain >= block_erase_size && (erase_addr % block_erase_size) == 0) {
  515. err = chip->chip_drv->erase_block(chip, erase_addr);
  516. erase_addr += block_erase_size;
  517. len_remain -= block_erase_size;
  518. } else
  519. #endif
  520. {
  521. // Otherwise erase individual sector only
  522. err = chip->chip_drv->erase_sector(chip, erase_addr);
  523. erase_addr += sector_size;
  524. len_remain -= sector_size;
  525. }
  526. assert(len_remain < len);
  527. if (err != ESP_OK || len_remain == 0) {
  528. // On ESP32, the cache re-enable is in the end() function, while flush_cache should
  529. // happen when the cache is still disabled on ESP32. Break before the end() function and
  530. // do end() later
  531. assert(bus_acquired);
  532. break;
  533. }
  534. err = rom_spiflash_api_funcs->end(chip, ESP_OK);
  535. if (err != ESP_OK) {
  536. break;
  537. }
  538. bus_acquired = false;
  539. }
  540. return rom_spiflash_api_funcs->flash_end_flush_cache(chip, err, bus_acquired, start, len);
  541. }
  542. #endif // !CONFIG_SPI_FLASH_ROM_IMPL
  543. #if defined(CONFIG_SPI_FLASH_ROM_IMPL) && ESP_ROM_HAS_ERASE_0_REGION_BUG
  544. /* ROM esp_flash_erase_region implementation doesn't handle 0 erase size correctly.
  545. * Check the size and call ROM function instead of overriding it completely.
  546. * The behavior is slightly different from esp_flash_erase_region above, thought:
  547. * here the check for 0 size is done first, but in esp_flash_erase_region the check is
  548. * done after the other arguments are checked.
  549. */
  550. extern esp_err_t rom_esp_flash_erase_region(esp_flash_t *chip, uint32_t start, uint32_t len);
  551. esp_err_t IRAM_ATTR esp_flash_erase_region(esp_flash_t *chip, uint32_t start, uint32_t len)
  552. {
  553. if (len == 0) {
  554. return ESP_OK;
  555. }
  556. return rom_esp_flash_erase_region(chip, start, len);
  557. }
  558. #endif // defined(CONFIG_SPI_FLASH_ROM_IMPL) && ESP_ROM_HAS_ERASE_0_REGION_BUG
  559. #ifndef CONFIG_SPI_FLASH_ROM_IMPL
  560. esp_err_t IRAM_ATTR esp_flash_get_chip_write_protect(esp_flash_t *chip, bool *out_write_protected)
  561. {
  562. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  563. VERIFY_CHIP_OP(get_chip_write_protect);
  564. if (out_write_protected == NULL) {
  565. return ESP_ERR_INVALID_ARG;
  566. }
  567. err = rom_spiflash_api_funcs->start(chip);
  568. if (err != ESP_OK) {
  569. return err;
  570. }
  571. err = chip->chip_drv->get_chip_write_protect(chip, out_write_protected);
  572. return rom_spiflash_api_funcs->end(chip, err);
  573. }
  574. esp_err_t IRAM_ATTR esp_flash_set_chip_write_protect(esp_flash_t *chip, bool write_protect)
  575. {
  576. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  577. VERIFY_CHIP_OP(set_chip_write_protect);
  578. //TODO: skip writing if already locked or unlocked
  579. err = rom_spiflash_api_funcs->start(chip);
  580. if (err != ESP_OK) {
  581. return err;
  582. }
  583. err = chip->chip_drv->set_chip_write_protect(chip, write_protect);
  584. return rom_spiflash_api_funcs->end(chip, err);
  585. }
  586. 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)
  587. {
  588. if(out_num_regions != NULL) {
  589. *out_num_regions = 0; // In case caller doesn't check result
  590. }
  591. esp_err_t err = rom_spiflash_api_funcs->chip_check((esp_flash_t **)&chip);
  592. VERIFY_CHIP_OP(get_protected_regions);
  593. if(out_regions == NULL || out_num_regions == NULL) {
  594. return ESP_ERR_INVALID_ARG;
  595. }
  596. *out_num_regions = chip->chip_drv->num_protectable_regions;
  597. *out_regions = chip->chip_drv->protectable_regions;
  598. return ESP_OK;
  599. }
  600. static esp_err_t find_region(const esp_flash_t *chip, const esp_flash_region_t *region, uint8_t *index)
  601. {
  602. if (region == NULL) {
  603. return ESP_ERR_INVALID_ARG;
  604. }
  605. for(*index = 0; *index < chip->chip_drv->num_protectable_regions; (*index)++) {
  606. if (memcmp(&chip->chip_drv->protectable_regions[*index],
  607. region, sizeof(esp_flash_region_t)) == 0) {
  608. return ESP_OK;
  609. }
  610. }
  611. return ESP_ERR_NOT_FOUND;
  612. }
  613. esp_err_t IRAM_ATTR esp_flash_get_protected_region(esp_flash_t *chip, const esp_flash_region_t *region, bool *out_protected)
  614. {
  615. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  616. VERIFY_CHIP_OP(get_protected_regions);
  617. if (out_protected == NULL) {
  618. return ESP_ERR_INVALID_ARG;
  619. }
  620. uint8_t index;
  621. err = find_region(chip, region, &index);
  622. if (err != ESP_OK) {
  623. return err;
  624. }
  625. uint64_t protection_mask = 0;
  626. err = rom_spiflash_api_funcs->start(chip);
  627. if (err != ESP_OK) {
  628. return err;
  629. }
  630. err = chip->chip_drv->get_protected_regions(chip, &protection_mask);
  631. if (err == ESP_OK) {
  632. *out_protected = protection_mask & (1LL << index);
  633. }
  634. return rom_spiflash_api_funcs->end(chip, err);
  635. }
  636. esp_err_t IRAM_ATTR esp_flash_set_protected_region(esp_flash_t *chip, const esp_flash_region_t *region, bool protect)
  637. {
  638. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  639. VERIFY_CHIP_OP(set_protected_regions);
  640. uint8_t index;
  641. err = find_region(chip, region, &index);
  642. if (err != ESP_OK) {
  643. return err;
  644. }
  645. uint64_t protection_mask = 0;
  646. err = rom_spiflash_api_funcs->start(chip);
  647. if (err != ESP_OK) {
  648. return err;
  649. }
  650. err = chip->chip_drv->get_protected_regions(chip, &protection_mask);
  651. if (err == ESP_OK) {
  652. if (protect) {
  653. protection_mask |= (1LL << index);
  654. } else {
  655. protection_mask &= ~(1LL << index);
  656. }
  657. err = chip->chip_drv->set_protected_regions(chip, protection_mask);
  658. }
  659. return rom_spiflash_api_funcs->end(chip, err);
  660. }
  661. esp_err_t IRAM_ATTR esp_flash_read(esp_flash_t *chip, void *buffer, uint32_t address, uint32_t length)
  662. {
  663. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  664. VERIFY_CHIP_OP(read);
  665. if (buffer == NULL || address > chip->size || address+length > chip->size) {
  666. return ESP_ERR_INVALID_ARG;
  667. }
  668. if (length == 0) {
  669. return ESP_OK;
  670. }
  671. //when the cache is disabled, only the DRAM can be read, check whether we need to receive in another buffer in DRAM.
  672. bool direct_read = chip->host->driver->supports_direct_read(chip->host, buffer);
  673. uint8_t* temp_buffer = NULL;
  674. //each time, we at most read this length
  675. //after that, we release the lock to allow some other operations
  676. size_t read_chunk_size = MIN(MAX_READ_CHUNK, length);
  677. if (!direct_read) {
  678. size_t actual_len = 0;
  679. if (chip->os_func->get_temp_buffer != NULL) {
  680. temp_buffer = chip->os_func->get_temp_buffer(chip->os_func_data, read_chunk_size, &actual_len);
  681. read_chunk_size = actual_len;
  682. }
  683. if (temp_buffer == NULL) {
  684. return ESP_ERR_NO_MEM;
  685. }
  686. }
  687. err = ESP_OK;
  688. do {
  689. err = rom_spiflash_api_funcs->start(chip);
  690. if (err != ESP_OK) {
  691. break;
  692. }
  693. //if required (dma buffer allocated), read to the buffer instead of the original buffer
  694. uint8_t* buffer_to_read = (temp_buffer)? temp_buffer : buffer;
  695. // Length we will read this iteration is either the chunk size or the remaining length, whichever is smaller
  696. size_t length_to_read = MIN(read_chunk_size, length);
  697. if (err == ESP_OK) {
  698. err = chip->chip_drv->read(chip, buffer_to_read, address, length_to_read);
  699. }
  700. if (err != ESP_OK) {
  701. rom_spiflash_api_funcs->end(chip, err);
  702. break;
  703. }
  704. //even if this is failed, the data is still valid, copy before quit
  705. err = rom_spiflash_api_funcs->end(chip, err);
  706. //copy back to the original buffer
  707. if (temp_buffer) {
  708. memcpy(buffer, temp_buffer, length_to_read);
  709. }
  710. address += length_to_read;
  711. length -= length_to_read;
  712. buffer = (void*)((intptr_t)buffer + length_to_read);
  713. } while (err == ESP_OK && length > 0);
  714. if (chip->os_func->release_temp_buffer != NULL) {
  715. chip->os_func->release_temp_buffer(chip->os_func_data, temp_buffer);
  716. }
  717. return err;
  718. }
  719. esp_err_t IRAM_ATTR esp_flash_write(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length)
  720. {
  721. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  722. VERIFY_CHIP_OP(write);
  723. CHECK_WRITE_ADDRESS(chip, address, length);
  724. if (buffer == NULL || address > chip->size || address+length > chip->size) {
  725. return ESP_ERR_INVALID_ARG;
  726. }
  727. if (length == 0) {
  728. return ESP_OK;
  729. }
  730. //when the cache is disabled, only the DRAM can be read, check whether we need to copy the data first
  731. bool direct_write = chip->host->driver->supports_direct_write(chip->host, buffer);
  732. // Indicate whether the bus is acquired by the driver, needs to be released before return
  733. bool bus_acquired = false;
  734. err = ESP_OK;
  735. /* Write output in chunks, either by buffering on stack or
  736. by artificially cutting into MAX_WRITE_CHUNK parts (in an OS
  737. environment, this prevents writing from causing interrupt or higher priority task
  738. starvation.) */
  739. uint32_t write_addr = address;
  740. uint32_t len_remain = length;
  741. while (1) {
  742. uint32_t write_len;
  743. const void *write_buf;
  744. uint32_t temp_buf[8];
  745. if (direct_write) {
  746. write_len = MIN(len_remain, MAX_WRITE_CHUNK);
  747. write_buf = buffer;
  748. } else {
  749. write_len = MIN(len_remain, sizeof(temp_buf));
  750. memcpy(temp_buf, buffer, write_len);
  751. write_buf = temp_buf;
  752. }
  753. //check before the operation, in case this is called too close to the last operation
  754. if (chip->chip_drv->yield) {
  755. err = chip->chip_drv->yield(chip, 0);
  756. if (err != ESP_OK) {
  757. return err;
  758. }
  759. }
  760. err = rom_spiflash_api_funcs->start(chip);
  761. if (err != ESP_OK) {
  762. break;
  763. }
  764. bus_acquired = true;
  765. err = chip->chip_drv->write(chip, write_buf, write_addr, write_len);
  766. len_remain -= write_len;
  767. assert(len_remain < length);
  768. if (err != ESP_OK || len_remain == 0) {
  769. // On ESP32, the cache re-enable is in the end() function, while flush_cache should
  770. // happen when the cache is still disabled on ESP32. Break before the end() function and
  771. // do end() later
  772. assert(bus_acquired);
  773. break;
  774. }
  775. err = rom_spiflash_api_funcs->end(chip, err);
  776. if (err != ESP_OK) {
  777. break;
  778. }
  779. bus_acquired = false;
  780. write_addr += write_len;
  781. buffer = (void *)((intptr_t)buffer + write_len);
  782. }
  783. return rom_spiflash_api_funcs->flash_end_flush_cache(chip, err, bus_acquired, address, length);
  784. }
  785. esp_err_t IRAM_ATTR esp_flash_write_encrypted(esp_flash_t *chip, uint32_t address, const void *buffer, uint32_t length)
  786. {
  787. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  788. // Flash encryption only support on main flash.
  789. if (chip != esp_flash_default_chip) {
  790. return ESP_ERR_NOT_SUPPORTED;
  791. }
  792. if (err != ESP_OK) return err;
  793. if (buffer == NULL || address + length > chip->size) {
  794. return ESP_ERR_INVALID_ARG;
  795. }
  796. if ((address % 16) != 0) {
  797. ESP_EARLY_LOGE(TAG, "flash encrypted write address must be 16 bytes aligned");
  798. return ESP_ERR_INVALID_ARG;
  799. }
  800. if (length == 0) {
  801. return ESP_OK;
  802. }
  803. if ((length % 16) != 0) {
  804. ESP_EARLY_LOGE(TAG, "flash encrypted write length must be multiple of 16");
  805. return ESP_ERR_INVALID_SIZE;
  806. }
  807. bool bus_acquired = false;
  808. const uint8_t *ssrc = (const uint8_t *)buffer;
  809. /* On ESP32, write_encrypted encrypts data in RAM as it writes,
  810. so copy to a temporary buffer - 32 bytes at a time.
  811. Each call to write_encrypted takes a 32 byte "row" of
  812. data to encrypt, and each row is two 16 byte AES blocks
  813. that share a key (as derived from flash address).
  814. On ESP32-S2 and later, the temporary buffer need to be
  815. seperated into 16-bytes, 32-bytes, 64-bytes(if supported).
  816. So, on ESP32-S2 and later, here has a totally different
  817. data prepare implementation.
  818. */
  819. uint8_t encrypt_buf[64] __attribute__((aligned(4)));
  820. uint32_t row_size_length;
  821. for (size_t i = 0; i < length; i += row_size_length) {
  822. uint32_t row_addr = address + i;
  823. uint8_t row_size;
  824. uint8_t encrypt_byte;
  825. #if CONFIG_IDF_TARGET_ESP32
  826. if (i == 0 && (row_addr % 32) != 0) {
  827. /* writing to second block of a 32 byte row */
  828. row_size = 16;
  829. row_addr -= 16;
  830. /* copy to second block in buffer */
  831. memcpy(encrypt_buf + 16, ssrc + i, row_size);
  832. /* decrypt the first block from flash, will reencrypt to same bytes */
  833. esp_flash_read_encrypted(chip, row_addr, encrypt_buf, 16);
  834. } else if (length - i == 16) {
  835. /* 16 bytes left, is first block of a 32 byte row */
  836. row_size = 16;
  837. /* copy to first block in buffer */
  838. memcpy(encrypt_buf, ssrc + i, row_size);
  839. /* decrypt the second block from flash, will reencrypt to same bytes */
  840. esp_flash_read_encrypted(chip, row_addr + 16, encrypt_buf + 16, 16);
  841. } else {
  842. /* Writing a full 32 byte row (2 blocks) */
  843. row_size = 32;
  844. memcpy(encrypt_buf, ssrc + i, row_size);
  845. }
  846. encrypt_byte = 32;
  847. row_size_length = row_size;
  848. #else // FOR ESP32-S2, ESP32-S3, ESP32-C3
  849. if ((row_addr % 64) == 0 && (length - i) >= 64 && SOC_FLASH_ENCRYPTED_XTS_AES_BLOCK_MAX == 64) {
  850. row_size = 64;
  851. memcpy(encrypt_buf, ssrc + i, row_size);
  852. } else if ((row_addr % 32) == 0 && (length - i) >= 32) {
  853. row_size = 32;
  854. memcpy(encrypt_buf, ssrc + i, row_size);
  855. } else {
  856. row_size = 16;
  857. memcpy(encrypt_buf, ssrc + i, row_size);
  858. }
  859. encrypt_byte = row_size;
  860. row_size_length = row_size;
  861. #endif //CONFIG_IDF_TARGET_ESP32
  862. #if CONFIG_IDF_TARGET_ESP32S2
  863. esp_crypto_dma_lock_acquire();
  864. #endif //CONFIG_IDF_TARGET_ESP32S2
  865. err = rom_spiflash_api_funcs->start(chip);
  866. if (err != ESP_OK) {
  867. #if CONFIG_IDF_TARGET_ESP32S2
  868. esp_crypto_dma_lock_release();
  869. #endif //CONFIG_IDF_TARGET_ESP32S2
  870. break;
  871. }
  872. bus_acquired = true;
  873. err = chip->chip_drv->write_encrypted(chip, (uint32_t *)encrypt_buf, row_addr, encrypt_byte);
  874. if (err!= ESP_OK) {
  875. #if CONFIG_IDF_TARGET_ESP32S2
  876. esp_crypto_dma_lock_release();
  877. #endif //CONFIG_IDF_TARGET_ESP32S2
  878. bus_acquired = false;
  879. assert(bus_acquired);
  880. break;
  881. }
  882. err = rom_spiflash_api_funcs->end(chip, ESP_OK);
  883. #if CONFIG_IDF_TARGET_ESP32S2
  884. esp_crypto_dma_lock_release();
  885. #endif //CONFIG_IDF_TARGET_ESP32S2
  886. if (err != ESP_OK) {
  887. bus_acquired = false;
  888. break;
  889. }
  890. bus_acquired = false;
  891. }
  892. return rom_spiflash_api_funcs->flash_end_flush_cache(chip, err, bus_acquired, address, length);
  893. }
  894. inline static IRAM_ATTR bool regions_overlap(uint32_t a_start, uint32_t a_len,uint32_t b_start, uint32_t b_len)
  895. {
  896. uint32_t a_end = a_start + a_len;
  897. uint32_t b_end = b_start + b_len;
  898. return (a_end > b_start && b_end > a_start);
  899. }
  900. //currently the legacy implementation is used, from flash_ops.c
  901. esp_err_t spi_flash_read_encrypted(size_t src, void *dstv, size_t size);
  902. esp_err_t IRAM_ATTR esp_flash_read_encrypted(esp_flash_t *chip, uint32_t address, void *out_buffer, uint32_t length)
  903. {
  904. /*
  905. * Since currently this feature is supported only by the hardware, there
  906. * is no way to support non-standard chips. We use the legacy
  907. * implementation and skip the chip and driver layers.
  908. */
  909. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  910. if (err != ESP_OK) return err;
  911. return spi_flash_read_encrypted(address, out_buffer, length);
  912. }
  913. // test only, non-public
  914. IRAM_ATTR esp_err_t esp_flash_get_io_mode(esp_flash_t* chip, bool* qe)
  915. {
  916. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  917. VERIFY_CHIP_OP(get_io_mode);
  918. esp_flash_io_mode_t io_mode;
  919. err = rom_spiflash_api_funcs->start(chip);
  920. if (err != ESP_OK) {
  921. return err;
  922. }
  923. err = chip->chip_drv->get_io_mode(chip, &io_mode);
  924. err = rom_spiflash_api_funcs->end(chip, err);
  925. if (err == ESP_OK) {
  926. *qe = (io_mode == SPI_FLASH_QOUT);
  927. }
  928. return err;
  929. }
  930. IRAM_ATTR esp_err_t esp_flash_set_io_mode(esp_flash_t* chip, bool qe)
  931. {
  932. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  933. VERIFY_CHIP_OP(set_io_mode);
  934. chip->read_mode = (qe? SPI_FLASH_QOUT: SPI_FLASH_SLOWRD);
  935. err = rom_spiflash_api_funcs->start(chip);
  936. if (err != ESP_OK) {
  937. return err;
  938. }
  939. err = chip->chip_drv->set_io_mode(chip);
  940. return rom_spiflash_api_funcs->end(chip, err);
  941. }
  942. #endif //CONFIG_SPI_FLASH_ROM_IMPL
  943. //init suspend mode cmd, uses internal.
  944. esp_err_t esp_flash_suspend_cmd_init(esp_flash_t* chip)
  945. {
  946. ESP_EARLY_LOGW(TAG, "Flash suspend feature is enabled");
  947. if (chip->chip_drv->get_chip_caps == NULL) {
  948. // chip caps get failed, pass the flash capability check.
  949. ESP_EARLY_LOGW(TAG, "get_chip_caps function pointer hasn't been initialized");
  950. } else {
  951. if ((chip->chip_drv->get_chip_caps(chip) & SPI_FLASH_CHIP_CAP_SUSPEND) == 0) {
  952. ESP_EARLY_LOGW(TAG, "Suspend and resume may not supported for this flash model yet.");
  953. }
  954. }
  955. return chip->chip_drv->sus_setup(chip);
  956. }
  957. #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  958. esp_err_t esp_flash_app_disable_protect(bool disable)
  959. {
  960. if (disable) {
  961. return esp_flash_app_disable_os_functions(esp_flash_default_chip);
  962. } else {
  963. return esp_flash_app_enable_os_functions(esp_flash_default_chip);
  964. }
  965. }
  966. #endif
  967. /*------------------------------------------------------------------------------
  968. Adapter layer to original api before IDF v4.0
  969. ------------------------------------------------------------------------------*/
  970. #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  971. /* Translate any ESP_ERR_FLASH_xxx error code (new API) to a generic ESP_ERR_xyz error code
  972. */
  973. static IRAM_ATTR esp_err_t spi_flash_translate_rc(esp_err_t err)
  974. {
  975. switch (err) {
  976. case ESP_OK:
  977. case ESP_ERR_INVALID_ARG:
  978. case ESP_ERR_INVALID_SIZE:
  979. case ESP_ERR_NO_MEM:
  980. return err;
  981. case ESP_ERR_FLASH_NOT_INITIALISED:
  982. case ESP_ERR_FLASH_PROTECTED:
  983. return ESP_ERR_INVALID_STATE;
  984. case ESP_ERR_NOT_FOUND:
  985. case ESP_ERR_FLASH_UNSUPPORTED_HOST:
  986. case ESP_ERR_FLASH_UNSUPPORTED_CHIP:
  987. return ESP_ERR_NOT_SUPPORTED;
  988. case ESP_ERR_FLASH_NO_RESPONSE:
  989. return ESP_ERR_INVALID_RESPONSE;
  990. default:
  991. ESP_EARLY_LOGE(TAG, "unexpected spi flash error code: 0x%x", err);
  992. abort();
  993. }
  994. }
  995. esp_err_t IRAM_ATTR spi_flash_erase_range(uint32_t start_addr, uint32_t size)
  996. {
  997. esp_err_t err = esp_flash_erase_region(NULL, start_addr, size);
  998. return spi_flash_translate_rc(err);
  999. }
  1000. esp_err_t IRAM_ATTR spi_flash_write(size_t dst, const void *srcv, size_t size)
  1001. {
  1002. esp_err_t err = esp_flash_write(NULL, srcv, dst, size);
  1003. return spi_flash_translate_rc(err);
  1004. }
  1005. esp_err_t IRAM_ATTR spi_flash_read(size_t src, void *dstv, size_t size)
  1006. {
  1007. esp_err_t err = esp_flash_read(NULL, dstv, src, size);
  1008. return spi_flash_translate_rc(err);
  1009. }
  1010. esp_err_t IRAM_ATTR spi_flash_write_encrypted(size_t dest_addr, const void *src, size_t size)
  1011. {
  1012. esp_err_t err = esp_flash_write_encrypted(NULL, dest_addr, src, size);
  1013. return spi_flash_translate_rc(err);
  1014. }
  1015. #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL