esp_flash_api.c 40 KB

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