esp_flash_api.c 40 KB

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