esp_flash_api.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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 "spi_flash_mmap.h"
  18. #include "esp_rom_caps.h"
  19. #include "esp_rom_spiflash.h"
  20. #if CONFIG_IDF_TARGET_ESP32S2
  21. #include "esp_crypto_lock.h" // for locking flash encryption peripheral
  22. #endif //CONFIG_IDF_TARGET_ESP32S2
  23. static const char TAG[] = "spi_flash";
  24. #ifdef CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE
  25. #define MAX_WRITE_CHUNK CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE /* write in chunks */
  26. #else
  27. #define MAX_WRITE_CHUNK 8192 /* write in chunks */
  28. #endif // CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE
  29. #define MAX_READ_CHUNK 16384
  30. #ifdef CONFIG_SPI_FLASH_DANGEROUS_WRITE_ABORTS
  31. #define UNSAFE_WRITE_ADDRESS abort()
  32. #else
  33. #define UNSAFE_WRITE_ADDRESS return ESP_ERR_INVALID_ARG
  34. #endif
  35. /* CHECK_WRITE_ADDRESS macro to fail writes which land in the
  36. bootloader, partition table, or running application region.
  37. */
  38. #if CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED
  39. #define CHECK_WRITE_ADDRESS(CHIP, ADDR, SIZE)
  40. #else /* FAILS or ABORTS */
  41. #define CHECK_WRITE_ADDRESS(CHIP, ADDR, SIZE) do { \
  42. if (CHIP && CHIP->os_func->region_protected && CHIP->os_func->region_protected(CHIP->os_func_data, ADDR, SIZE)) { \
  43. UNSAFE_WRITE_ADDRESS; \
  44. } \
  45. } while(0)
  46. #endif // CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED
  47. /* Convenience macro for beginning of all API functions.
  48. * Check the return value of `rom_spiflash_api_funcs->chip_check` is correct,
  49. * and the chip supports the operation in question.
  50. */
  51. #define VERIFY_CHIP_OP(op) do { \
  52. if (err != ESP_OK) return err; \
  53. if (chip->chip_drv->op == NULL) { \
  54. return ESP_ERR_FLASH_UNSUPPORTED_CHIP; \
  55. } \
  56. } while (0)
  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_physical_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;
  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. octal_mode = (chip->read_mode >= SPI_FLASH_OPI_FLAG);
  240. //read chip id
  241. // This can indicate the MSPI support OPI, if the flash works on MSPI in OPI mode, we directly bypass read id.
  242. uint32_t flash_id = 0;
  243. if (octal_mode) {
  244. // 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
  245. flash_id = g_rom_flashchip.device_id;
  246. } else {
  247. int retries = 10;
  248. do {
  249. err = esp_flash_read_chip_id(chip, &flash_id);
  250. } while (err == ESP_ERR_FLASH_NOT_INITIALISED && retries-- > 0);
  251. }
  252. if (err != ESP_OK) {
  253. return err;
  254. }
  255. chip->chip_id = flash_id;
  256. if (!esp_flash_chip_driver_initialized(chip)) {
  257. // Detect chip_drv
  258. err = detect_spi_flash_chip(chip);
  259. if (err != ESP_OK) {
  260. return err;
  261. }
  262. }
  263. // Detect flash size
  264. uint32_t size;
  265. err = esp_flash_get_physical_size(chip, &size);
  266. if (err != ESP_OK) {
  267. ESP_EARLY_LOGE(TAG, "failed to get chip size");
  268. return err;
  269. }
  270. if (chip->chip_drv->get_chip_caps == NULL) {
  271. // chip caps get failed, pass the flash capability check.
  272. ESP_EARLY_LOGW(TAG, "get_chip_caps function pointer hasn't been initialized");
  273. } else {
  274. if (((chip->chip_drv->get_chip_caps(chip) & SPI_FLASH_CHIP_CAP_32MB_SUPPORT) == 0) && (size > (16 *1024 * 1024))) {
  275. ESP_EARLY_LOGW(TAG, "Detected flash size > 16 MB, but access beyond 16 MB is not supported for this flash model yet.");
  276. size = (16 * 1024 * 1024);
  277. }
  278. }
  279. ESP_EARLY_LOGI(TAG, "flash io: %s", io_mode_str[chip->read_mode]);
  280. err = rom_spiflash_api_funcs->start(chip);
  281. if (err != ESP_OK) {
  282. return err;
  283. }
  284. if (err == ESP_OK && !octal_mode) {
  285. // Try to set the flash mode to whatever default mode was chosen
  286. err = chip->chip_drv->set_io_mode(chip);
  287. if (err == ESP_ERR_FLASH_NO_RESPONSE && !esp_flash_is_quad_mode(chip)) {
  288. //some chips (e.g. Winbond) don't support to clear QE, treat as success
  289. err = ESP_OK;
  290. }
  291. }
  292. // Done: all fields on 'chip' are initialised
  293. return rom_spiflash_api_funcs->end(chip, err);
  294. }
  295. static esp_err_t IRAM_ATTR read_id_core(esp_flash_t* chip, uint32_t* out_id, bool sanity_check)
  296. {
  297. bool installed = esp_flash_chip_driver_initialized(chip);
  298. esp_err_t err = rom_spiflash_api_funcs->start(chip);
  299. if (err != ESP_OK) {
  300. return err;
  301. }
  302. esp_err_t (*read_id_func)(void*, uint32_t*);
  303. void* read_id_arg;
  304. if (installed && chip->chip_drv->read_id) {
  305. read_id_func = (void*)chip->chip_drv->read_id;
  306. read_id_arg = (void*)chip;
  307. } else {
  308. //default option if the chip is not detected/chosen yet.
  309. read_id_func = (void*)chip->host->driver->read_id;
  310. read_id_arg = (void*)chip->host;
  311. }
  312. // Inner function fails if it sees all-ones or all-zeroes.
  313. err = read_id_func(read_id_arg, out_id);
  314. if (sanity_check && err == ESP_OK) {
  315. // Send RDID command twice, check for a matching result and retry in case we just powered on
  316. uint32_t new_id;
  317. err = read_id_func(read_id_arg, &new_id);
  318. if (err == ESP_OK && (new_id != *out_id)) {
  319. err = ESP_ERR_FLASH_NOT_INITIALISED;
  320. }
  321. }
  322. return rom_spiflash_api_funcs->end(chip, err);
  323. }
  324. // Faster version with sanity check.
  325. // Called in esp_flash_init and unit test (though not public)
  326. esp_err_t esp_flash_read_chip_id(esp_flash_t* chip, uint32_t* out_id)
  327. {
  328. return read_id_core(chip, out_id, true);
  329. }
  330. #ifndef CONFIG_SPI_FLASH_ROM_IMPL
  331. esp_err_t esp_flash_read_id(esp_flash_t* chip, uint32_t* out_id)
  332. {
  333. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  334. //Accept uninitialized chip when reading chip id
  335. if (err != ESP_OK && !(err == ESP_ERR_FLASH_NOT_INITIALISED && chip != NULL)) return err;
  336. if (out_id == NULL) return ESP_ERR_INVALID_ARG;
  337. return read_id_core(chip, out_id, false);
  338. }
  339. #endif //CONFIG_SPI_FLASH_ROM_IMPL
  340. static esp_err_t IRAM_ATTR NOINLINE_ATTR read_unique_id(esp_flash_t* chip, uint64_t* out_uid)
  341. {
  342. esp_err_t err = rom_spiflash_api_funcs->start(chip);
  343. if (err != ESP_OK) {
  344. return err;
  345. }
  346. err = chip->chip_drv->read_unique_id(chip, out_uid);
  347. return rom_spiflash_api_funcs->end(chip, err);
  348. }
  349. esp_err_t esp_flash_read_unique_chip_id(esp_flash_t *chip, uint64_t* out_uid)
  350. {
  351. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  352. if (err != ESP_OK) {
  353. return err;
  354. }
  355. if (chip->chip_drv->get_chip_caps == NULL) {
  356. // chip caps get failed, pass the flash capability check.
  357. ESP_EARLY_LOGW(TAG, "get_chip_caps function pointer hasn't been initialized");
  358. } else {
  359. if ((chip->chip_drv->get_chip_caps(chip) & SPI_FLASH_CHIP_CAP_UNIQUE_ID) == 0) {
  360. ESP_EARLY_LOGE(TAG, "chip %s doesn't support reading unique id", chip->chip_drv->name);
  361. return ESP_ERR_NOT_SUPPORTED;
  362. }
  363. }
  364. if (out_uid == NULL) {
  365. return ESP_ERR_INVALID_ARG;
  366. };
  367. return read_unique_id(chip, out_uid);
  368. }
  369. static esp_err_t IRAM_ATTR detect_spi_flash_chip(esp_flash_t *chip)
  370. {
  371. esp_err_t err;
  372. uint32_t flash_id = chip->chip_id;
  373. // Detect the chip and set the chip_drv structure for it
  374. const spi_flash_chip_t **drivers = esp_flash_registered_chips;
  375. while (*drivers != NULL && !esp_flash_chip_driver_initialized(chip)) {
  376. chip->chip_drv = *drivers;
  377. // start/end SPI operation each time, for multitasking
  378. // and also so esp_flash_registered_flash_drivers can live in flash
  379. ESP_EARLY_LOGD(TAG, "trying chip: %s", chip->chip_drv->name);
  380. err = rom_spiflash_api_funcs->start(chip);
  381. if (err != ESP_OK) {
  382. return err;
  383. }
  384. if (chip->chip_drv->probe(chip, flash_id) != ESP_OK) {
  385. chip->chip_drv = NULL;
  386. }
  387. // if probe succeeded, chip->drv stays set
  388. drivers++;
  389. err = rom_spiflash_api_funcs->end(chip, err);
  390. if (err != ESP_OK) {
  391. return err;
  392. }
  393. }
  394. if (!esp_flash_chip_driver_initialized(chip)) {
  395. return ESP_ERR_NOT_FOUND;
  396. }
  397. ESP_EARLY_LOGI(TAG, "detected chip: %s", chip->chip_drv->name);
  398. return ESP_OK;
  399. }
  400. esp_err_t IRAM_ATTR esp_flash_get_physical_size(esp_flash_t *chip, uint32_t *flash_size)
  401. {
  402. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  403. if (err != ESP_OK) {
  404. return err;
  405. }
  406. VERIFY_CHIP_OP(detect_size);
  407. if (flash_size == NULL) {
  408. return ESP_ERR_INVALID_ARG;
  409. }
  410. err = rom_spiflash_api_funcs->start(chip);
  411. if (err != ESP_OK) {
  412. return err;
  413. }
  414. uint32_t detect_size;
  415. err = chip->chip_drv->detect_size(chip, &detect_size);
  416. if (err == ESP_OK) {
  417. if (chip->size == 0) {
  418. // chip->size will not be changed if detected, it will always be equal to configured flash size.
  419. chip->size = detect_size;
  420. }
  421. *flash_size = detect_size;
  422. }
  423. return rom_spiflash_api_funcs->end(chip, err);
  424. }
  425. #ifndef CONFIG_SPI_FLASH_ROM_IMPL
  426. /* Return true if regions 'a' and 'b' overlap at all, based on their start offsets and lengths. */
  427. inline static bool regions_overlap(uint32_t a_start, uint32_t a_len,uint32_t b_start, uint32_t b_len);
  428. esp_err_t IRAM_ATTR esp_flash_get_size(esp_flash_t *chip, uint32_t *out_size)
  429. {
  430. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  431. if (err != ESP_OK) {
  432. return err;
  433. }
  434. if (out_size == NULL) {
  435. return ESP_ERR_INVALID_ARG;
  436. }
  437. if (chip->size != 0) {
  438. *out_size = chip->size;
  439. return ESP_OK;
  440. }
  441. //Return flash chip physical size, when this API is called before flash initialisation,
  442. //After initialization will return available size.
  443. return esp_flash_get_physical_size(chip, out_size);
  444. }
  445. esp_err_t IRAM_ATTR esp_flash_erase_chip(esp_flash_t *chip)
  446. {
  447. esp_err_t err = ESP_OK;
  448. uint32_t size = 0;
  449. err = esp_flash_get_size(chip, &size);
  450. if (err != ESP_OK) {
  451. ESP_LOGE(TAG, "esp_flash_get_size failed, flash error code: %d", err);
  452. return err;
  453. }
  454. err = esp_flash_erase_region(chip, 0, size);
  455. return err;
  456. }
  457. esp_err_t IRAM_ATTR esp_flash_erase_region(esp_flash_t *chip, uint32_t start, uint32_t len)
  458. {
  459. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  460. VERIFY_CHIP_OP(erase_sector);
  461. VERIFY_CHIP_OP(erase_block);
  462. CHECK_WRITE_ADDRESS(chip, start, len);
  463. uint32_t block_erase_size = chip->chip_drv->erase_block == NULL ? 0 : chip->chip_drv->block_erase_size;
  464. uint32_t sector_size = chip->chip_drv->sector_size;
  465. if (sector_size == 0 || (block_erase_size % sector_size) != 0) {
  466. return ESP_ERR_FLASH_NOT_INITIALISED;
  467. }
  468. if (start > chip->size || start + len > chip->size) {
  469. return ESP_ERR_INVALID_ARG;
  470. }
  471. if ((start % chip->chip_drv->sector_size) != 0 || (len % chip->chip_drv->sector_size) != 0) {
  472. // Can only erase multiples of the sector size, starting at sector boundary
  473. return ESP_ERR_INVALID_ARG;
  474. }
  475. if (len == 0) {
  476. return ESP_OK;
  477. }
  478. err = ESP_OK;
  479. // Check for write protected regions overlapping the erase region
  480. if (chip->chip_drv->get_protected_regions != NULL &&
  481. chip->chip_drv->num_protectable_regions > 0) {
  482. err = rom_spiflash_api_funcs->start(chip);
  483. if (err != ESP_OK) {
  484. return err;
  485. }
  486. uint64_t protected = 0;
  487. err = chip->chip_drv->get_protected_regions(chip, &protected);
  488. if (err == ESP_OK && protected != 0) {
  489. for (int i = 0; i < chip->chip_drv->num_protectable_regions && err == ESP_OK; i++) {
  490. const esp_flash_region_t *region = &chip->chip_drv->protectable_regions[i];
  491. if ((protected & BIT64(i))
  492. && regions_overlap(start, len, region->offset, region->size)) {
  493. err = ESP_ERR_FLASH_PROTECTED;
  494. }
  495. }
  496. }
  497. // Don't lock the SPI flash for the entire erase, as this may be very long
  498. err = rom_spiflash_api_funcs->end(chip, err);
  499. }
  500. if (err != ESP_OK) {
  501. return err;
  502. }
  503. uint32_t erase_addr = start;
  504. uint32_t len_remain = len;
  505. // Indicate whether the bus is acquired by the driver, needs to be released before return
  506. bool bus_acquired = false;
  507. while (1) {
  508. //check before the operation, in case this is called too close to the last operation
  509. if (chip->chip_drv->yield) {
  510. err = chip->chip_drv->yield(chip, 0);
  511. if (err != ESP_OK) {
  512. return err;
  513. }
  514. }
  515. err = rom_spiflash_api_funcs->start(chip);
  516. if (err != ESP_OK) {
  517. break;
  518. }
  519. bus_acquired = true;
  520. #ifndef CONFIG_SPI_FLASH_BYPASS_BLOCK_ERASE
  521. // If possible erase an entire multi-sector block
  522. if (block_erase_size > 0 && len_remain >= block_erase_size && (erase_addr % block_erase_size) == 0) {
  523. err = chip->chip_drv->erase_block(chip, erase_addr);
  524. erase_addr += block_erase_size;
  525. len_remain -= block_erase_size;
  526. } else
  527. #endif
  528. {
  529. // Otherwise erase individual sector only
  530. err = chip->chip_drv->erase_sector(chip, erase_addr);
  531. erase_addr += sector_size;
  532. len_remain -= sector_size;
  533. }
  534. assert(len_remain < len);
  535. if (err != ESP_OK || len_remain == 0) {
  536. // On ESP32, the cache re-enable is in the end() function, while flush_cache should
  537. // happen when the cache is still disabled on ESP32. Break before the end() function and
  538. // do end() later
  539. assert(bus_acquired);
  540. break;
  541. }
  542. err = rom_spiflash_api_funcs->end(chip, ESP_OK);
  543. if (err != ESP_OK) {
  544. break;
  545. }
  546. bus_acquired = false;
  547. }
  548. return rom_spiflash_api_funcs->flash_end_flush_cache(chip, err, bus_acquired, start, len);
  549. }
  550. #endif // !CONFIG_SPI_FLASH_ROM_IMPL
  551. #if defined(CONFIG_SPI_FLASH_ROM_IMPL) && ESP_ROM_HAS_ERASE_0_REGION_BUG
  552. /* ROM esp_flash_erase_region implementation doesn't handle 0 erase size correctly.
  553. * Check the size and call ROM function instead of overriding it completely.
  554. * The behavior is slightly different from esp_flash_erase_region above, thought:
  555. * here the check for 0 size is done first, but in esp_flash_erase_region the check is
  556. * done after the other arguments are checked.
  557. */
  558. extern esp_err_t rom_esp_flash_erase_region(esp_flash_t *chip, uint32_t start, uint32_t len);
  559. esp_err_t IRAM_ATTR esp_flash_erase_region(esp_flash_t *chip, uint32_t start, uint32_t len)
  560. {
  561. if (len == 0) {
  562. return ESP_OK;
  563. }
  564. return rom_esp_flash_erase_region(chip, start, len);
  565. }
  566. #endif // defined(CONFIG_SPI_FLASH_ROM_IMPL) && ESP_ROM_HAS_ERASE_0_REGION_BUG
  567. #ifndef CONFIG_SPI_FLASH_ROM_IMPL
  568. esp_err_t IRAM_ATTR esp_flash_get_chip_write_protect(esp_flash_t *chip, bool *out_write_protected)
  569. {
  570. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  571. VERIFY_CHIP_OP(get_chip_write_protect);
  572. if (out_write_protected == NULL) {
  573. return ESP_ERR_INVALID_ARG;
  574. }
  575. err = rom_spiflash_api_funcs->start(chip);
  576. if (err != ESP_OK) {
  577. return err;
  578. }
  579. err = chip->chip_drv->get_chip_write_protect(chip, out_write_protected);
  580. return rom_spiflash_api_funcs->end(chip, err);
  581. }
  582. esp_err_t IRAM_ATTR esp_flash_set_chip_write_protect(esp_flash_t *chip, bool write_protect)
  583. {
  584. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  585. VERIFY_CHIP_OP(set_chip_write_protect);
  586. //TODO: skip writing if already locked or unlocked
  587. err = rom_spiflash_api_funcs->start(chip);
  588. if (err != ESP_OK) {
  589. return err;
  590. }
  591. err = chip->chip_drv->set_chip_write_protect(chip, write_protect);
  592. return rom_spiflash_api_funcs->end(chip, err);
  593. }
  594. 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)
  595. {
  596. if(out_num_regions != NULL) {
  597. *out_num_regions = 0; // In case caller doesn't check result
  598. }
  599. esp_err_t err = rom_spiflash_api_funcs->chip_check((esp_flash_t **)&chip);
  600. VERIFY_CHIP_OP(get_protected_regions);
  601. if(out_regions == NULL || out_num_regions == NULL) {
  602. return ESP_ERR_INVALID_ARG;
  603. }
  604. *out_num_regions = chip->chip_drv->num_protectable_regions;
  605. *out_regions = chip->chip_drv->protectable_regions;
  606. return ESP_OK;
  607. }
  608. static esp_err_t find_region(const esp_flash_t *chip, const esp_flash_region_t *region, uint8_t *index)
  609. {
  610. if (region == NULL) {
  611. return ESP_ERR_INVALID_ARG;
  612. }
  613. for(*index = 0; *index < chip->chip_drv->num_protectable_regions; (*index)++) {
  614. if (memcmp(&chip->chip_drv->protectable_regions[*index],
  615. region, sizeof(esp_flash_region_t)) == 0) {
  616. return ESP_OK;
  617. }
  618. }
  619. return ESP_ERR_NOT_FOUND;
  620. }
  621. esp_err_t IRAM_ATTR esp_flash_get_protected_region(esp_flash_t *chip, const esp_flash_region_t *region, bool *out_protected)
  622. {
  623. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  624. VERIFY_CHIP_OP(get_protected_regions);
  625. if (out_protected == NULL) {
  626. return ESP_ERR_INVALID_ARG;
  627. }
  628. uint8_t index;
  629. err = find_region(chip, region, &index);
  630. if (err != ESP_OK) {
  631. return err;
  632. }
  633. uint64_t protection_mask = 0;
  634. err = rom_spiflash_api_funcs->start(chip);
  635. if (err != ESP_OK) {
  636. return err;
  637. }
  638. err = chip->chip_drv->get_protected_regions(chip, &protection_mask);
  639. if (err == ESP_OK) {
  640. *out_protected = protection_mask & (1LL << index);
  641. }
  642. return rom_spiflash_api_funcs->end(chip, err);
  643. }
  644. esp_err_t IRAM_ATTR esp_flash_set_protected_region(esp_flash_t *chip, const esp_flash_region_t *region, bool protect)
  645. {
  646. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  647. VERIFY_CHIP_OP(set_protected_regions);
  648. uint8_t index;
  649. err = find_region(chip, region, &index);
  650. if (err != ESP_OK) {
  651. return err;
  652. }
  653. uint64_t protection_mask = 0;
  654. err = rom_spiflash_api_funcs->start(chip);
  655. if (err != ESP_OK) {
  656. return err;
  657. }
  658. err = chip->chip_drv->get_protected_regions(chip, &protection_mask);
  659. if (err == ESP_OK) {
  660. if (protect) {
  661. protection_mask |= (1LL << index);
  662. } else {
  663. protection_mask &= ~(1LL << index);
  664. }
  665. err = chip->chip_drv->set_protected_regions(chip, protection_mask);
  666. }
  667. return rom_spiflash_api_funcs->end(chip, err);
  668. }
  669. esp_err_t IRAM_ATTR esp_flash_read(esp_flash_t *chip, void *buffer, uint32_t address, uint32_t length)
  670. {
  671. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  672. VERIFY_CHIP_OP(read);
  673. if (buffer == NULL || address > chip->size || address+length > chip->size) {
  674. return ESP_ERR_INVALID_ARG;
  675. }
  676. if (length == 0) {
  677. return ESP_OK;
  678. }
  679. //when the cache is disabled, only the DRAM can be read, check whether we need to receive in another buffer in DRAM.
  680. bool direct_read = false;
  681. //If the buffer is internal already, it's ok to use it directly
  682. direct_read |= esp_ptr_in_dram(buffer);
  683. //If not, we need to check if the HW support direct write
  684. direct_read |= chip->host->driver->supports_direct_read(chip->host, buffer);
  685. uint8_t* temp_buffer = NULL;
  686. //each time, we at most read this length
  687. //after that, we release the lock to allow some other operations
  688. size_t read_chunk_size = MIN(MAX_READ_CHUNK, length);
  689. if (!direct_read) {
  690. size_t actual_len = 0;
  691. if (chip->os_func->get_temp_buffer != NULL) {
  692. temp_buffer = chip->os_func->get_temp_buffer(chip->os_func_data, read_chunk_size, &actual_len);
  693. read_chunk_size = actual_len;
  694. }
  695. if (temp_buffer == NULL) {
  696. return ESP_ERR_NO_MEM;
  697. }
  698. }
  699. err = ESP_OK;
  700. do {
  701. err = rom_spiflash_api_funcs->start(chip);
  702. if (err != ESP_OK) {
  703. break;
  704. }
  705. //if required (dma buffer allocated), read to the buffer instead of the original buffer
  706. uint8_t* buffer_to_read = (temp_buffer)? temp_buffer : buffer;
  707. // Length we will read this iteration is either the chunk size or the remaining length, whichever is smaller
  708. size_t length_to_read = MIN(read_chunk_size, length);
  709. if (err == ESP_OK) {
  710. err = chip->chip_drv->read(chip, buffer_to_read, address, length_to_read);
  711. }
  712. if (err != ESP_OK) {
  713. rom_spiflash_api_funcs->end(chip, err);
  714. break;
  715. }
  716. //even if this is failed, the data is still valid, copy before quit
  717. err = rom_spiflash_api_funcs->end(chip, err);
  718. //copy back to the original buffer
  719. if (temp_buffer) {
  720. memcpy(buffer, temp_buffer, length_to_read);
  721. }
  722. address += length_to_read;
  723. length -= length_to_read;
  724. buffer = (void*)((intptr_t)buffer + length_to_read);
  725. } while (err == ESP_OK && length > 0);
  726. if (chip->os_func->release_temp_buffer != NULL) {
  727. chip->os_func->release_temp_buffer(chip->os_func_data, temp_buffer);
  728. }
  729. return err;
  730. }
  731. esp_err_t IRAM_ATTR esp_flash_write(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length)
  732. {
  733. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  734. VERIFY_CHIP_OP(write);
  735. CHECK_WRITE_ADDRESS(chip, address, length);
  736. if (buffer == NULL || address > chip->size || address+length > chip->size) {
  737. return ESP_ERR_INVALID_ARG;
  738. }
  739. if (length == 0) {
  740. return ESP_OK;
  741. }
  742. //when the cache is disabled, only the DRAM can be read, check whether we need to copy the data first
  743. bool direct_write = false;
  744. //If the buffer is internal already, it's ok to write it directly
  745. direct_write |= esp_ptr_in_dram(buffer);
  746. //If not, we need to check if the HW support direct write
  747. direct_write |= chip->host->driver->supports_direct_write(chip->host, buffer);
  748. // Indicate whether the bus is acquired by the driver, needs to be released before return
  749. bool bus_acquired = false;
  750. err = ESP_OK;
  751. /* Write output in chunks, either by buffering on stack or
  752. by artificially cutting into MAX_WRITE_CHUNK parts (in an OS
  753. environment, this prevents writing from causing interrupt or higher priority task
  754. starvation.) */
  755. uint32_t write_addr = address;
  756. uint32_t len_remain = length;
  757. while (1) {
  758. uint32_t write_len;
  759. const void *write_buf;
  760. uint32_t temp_buf[8];
  761. if (direct_write) {
  762. write_len = MIN(len_remain, MAX_WRITE_CHUNK);
  763. write_buf = buffer;
  764. } else {
  765. write_len = MIN(len_remain, sizeof(temp_buf));
  766. memcpy(temp_buf, buffer, write_len);
  767. write_buf = temp_buf;
  768. }
  769. //check before the operation, in case this is called too close to the last operation
  770. if (chip->chip_drv->yield) {
  771. err = chip->chip_drv->yield(chip, 0);
  772. if (err != ESP_OK) {
  773. return err;
  774. }
  775. }
  776. err = rom_spiflash_api_funcs->start(chip);
  777. if (err != ESP_OK) {
  778. break;
  779. }
  780. bus_acquired = true;
  781. err = chip->chip_drv->write(chip, write_buf, write_addr, write_len);
  782. len_remain -= write_len;
  783. assert(len_remain < length);
  784. if (err != ESP_OK || len_remain == 0) {
  785. // On ESP32, the cache re-enable is in the end() function, while flush_cache should
  786. // happen when the cache is still disabled on ESP32. Break before the end() function and
  787. // do end() later
  788. assert(bus_acquired);
  789. break;
  790. }
  791. err = rom_spiflash_api_funcs->end(chip, err);
  792. if (err != ESP_OK) {
  793. break;
  794. }
  795. bus_acquired = false;
  796. write_addr += write_len;
  797. buffer = (void *)((intptr_t)buffer + write_len);
  798. }
  799. return rom_spiflash_api_funcs->flash_end_flush_cache(chip, err, bus_acquired, address, length);
  800. }
  801. esp_err_t IRAM_ATTR esp_flash_write_encrypted(esp_flash_t *chip, uint32_t address, const void *buffer, uint32_t length)
  802. {
  803. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  804. // Flash encryption only support on main flash.
  805. if (chip != esp_flash_default_chip) {
  806. return ESP_ERR_NOT_SUPPORTED;
  807. }
  808. if (err != ESP_OK) return err;
  809. if (buffer == NULL || address + length > chip->size) {
  810. return ESP_ERR_INVALID_ARG;
  811. }
  812. if ((address % 16) != 0) {
  813. ESP_EARLY_LOGE(TAG, "flash encrypted write address must be 16 bytes aligned");
  814. return ESP_ERR_INVALID_ARG;
  815. }
  816. if (length == 0) {
  817. return ESP_OK;
  818. }
  819. if ((length % 16) != 0) {
  820. ESP_EARLY_LOGE(TAG, "flash encrypted write length must be multiple of 16");
  821. return ESP_ERR_INVALID_SIZE;
  822. }
  823. bool bus_acquired = false;
  824. const uint8_t *ssrc = (const uint8_t *)buffer;
  825. /* On ESP32, write_encrypted encrypts data in RAM as it writes,
  826. so copy to a temporary buffer - 32 bytes at a time.
  827. Each call to write_encrypted takes a 32 byte "row" of
  828. data to encrypt, and each row is two 16 byte AES blocks
  829. that share a key (as derived from flash address).
  830. On ESP32-S2 and later, the temporary buffer need to be
  831. seperated into 16-bytes, 32-bytes, 64-bytes(if supported).
  832. So, on ESP32-S2 and later, here has a totally different
  833. data prepare implementation.
  834. */
  835. uint8_t encrypt_buf[64] __attribute__((aligned(4)));
  836. uint32_t row_size_length;
  837. for (size_t i = 0; i < length; i += row_size_length) {
  838. uint32_t row_addr = address + i;
  839. uint8_t row_size;
  840. uint8_t encrypt_byte;
  841. #if CONFIG_IDF_TARGET_ESP32
  842. if (i == 0 && (row_addr % 32) != 0) {
  843. /* writing to second block of a 32 byte row */
  844. row_size = 16;
  845. row_addr -= 16;
  846. /* copy to second block in buffer */
  847. memcpy(encrypt_buf + 16, ssrc + i, row_size);
  848. /* decrypt the first block from flash, will reencrypt to same bytes */
  849. esp_flash_read_encrypted(chip, row_addr, encrypt_buf, 16);
  850. } else if (length - i == 16) {
  851. /* 16 bytes left, is first block of a 32 byte row */
  852. row_size = 16;
  853. /* copy to first block in buffer */
  854. memcpy(encrypt_buf, ssrc + i, row_size);
  855. /* decrypt the second block from flash, will reencrypt to same bytes */
  856. esp_flash_read_encrypted(chip, row_addr + 16, encrypt_buf + 16, 16);
  857. } else {
  858. /* Writing a full 32 byte row (2 blocks) */
  859. row_size = 32;
  860. memcpy(encrypt_buf, ssrc + i, row_size);
  861. }
  862. encrypt_byte = 32;
  863. row_size_length = row_size;
  864. #else // FOR ESP32-S2, ESP32-S3, ESP32-C3
  865. if ((row_addr % 64) == 0 && (length - i) >= 64 && SOC_FLASH_ENCRYPTED_XTS_AES_BLOCK_MAX == 64) {
  866. row_size = 64;
  867. memcpy(encrypt_buf, ssrc + i, row_size);
  868. } else if ((row_addr % 32) == 0 && (length - i) >= 32) {
  869. row_size = 32;
  870. memcpy(encrypt_buf, ssrc + i, row_size);
  871. } else {
  872. row_size = 16;
  873. memcpy(encrypt_buf, ssrc + i, row_size);
  874. }
  875. encrypt_byte = row_size;
  876. row_size_length = row_size;
  877. #endif //CONFIG_IDF_TARGET_ESP32
  878. #if CONFIG_IDF_TARGET_ESP32S2
  879. esp_crypto_dma_lock_acquire();
  880. #endif //CONFIG_IDF_TARGET_ESP32S2
  881. err = rom_spiflash_api_funcs->start(chip);
  882. if (err != ESP_OK) {
  883. #if CONFIG_IDF_TARGET_ESP32S2
  884. esp_crypto_dma_lock_release();
  885. #endif //CONFIG_IDF_TARGET_ESP32S2
  886. break;
  887. }
  888. bus_acquired = true;
  889. err = chip->chip_drv->write_encrypted(chip, (uint32_t *)encrypt_buf, row_addr, encrypt_byte);
  890. if (err!= ESP_OK) {
  891. #if CONFIG_IDF_TARGET_ESP32S2
  892. esp_crypto_dma_lock_release();
  893. #endif //CONFIG_IDF_TARGET_ESP32S2
  894. bus_acquired = false;
  895. assert(bus_acquired);
  896. break;
  897. }
  898. err = rom_spiflash_api_funcs->end(chip, ESP_OK);
  899. #if CONFIG_IDF_TARGET_ESP32S2
  900. esp_crypto_dma_lock_release();
  901. #endif //CONFIG_IDF_TARGET_ESP32S2
  902. if (err != ESP_OK) {
  903. bus_acquired = false;
  904. break;
  905. }
  906. bus_acquired = false;
  907. }
  908. return rom_spiflash_api_funcs->flash_end_flush_cache(chip, err, bus_acquired, address, length);
  909. }
  910. inline static IRAM_ATTR bool regions_overlap(uint32_t a_start, uint32_t a_len,uint32_t b_start, uint32_t b_len)
  911. {
  912. uint32_t a_end = a_start + a_len;
  913. uint32_t b_end = b_start + b_len;
  914. return (a_end > b_start && b_end > a_start);
  915. }
  916. esp_err_t IRAM_ATTR esp_flash_read_encrypted(esp_flash_t *chip, uint32_t address, void *out_buffer, uint32_t length)
  917. {
  918. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  919. if (err != ESP_OK) return err;
  920. if (address + length > g_rom_flashchip.chip_size) {
  921. return ESP_ERR_INVALID_SIZE;
  922. }
  923. if (length == 0) {
  924. return ESP_OK;
  925. }
  926. if (out_buffer == NULL) {
  927. return ESP_ERR_INVALID_ARG;
  928. }
  929. const uint8_t *map;
  930. spi_flash_mmap_handle_t map_handle;
  931. size_t map_src = address & ~(SPI_FLASH_MMU_PAGE_SIZE - 1);
  932. size_t map_size = length + (address - map_src);
  933. err = spi_flash_mmap(map_src, map_size, SPI_FLASH_MMAP_DATA, (const void **)&map, &map_handle);
  934. if (err != ESP_OK) {
  935. return err;
  936. }
  937. memcpy(out_buffer, map + (address - map_src), length);
  938. spi_flash_munmap(map_handle);
  939. return err;
  940. }
  941. // test only, non-public
  942. IRAM_ATTR esp_err_t esp_flash_get_io_mode(esp_flash_t* chip, bool* qe)
  943. {
  944. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  945. VERIFY_CHIP_OP(get_io_mode);
  946. esp_flash_io_mode_t io_mode;
  947. err = rom_spiflash_api_funcs->start(chip);
  948. if (err != ESP_OK) {
  949. return err;
  950. }
  951. err = chip->chip_drv->get_io_mode(chip, &io_mode);
  952. err = rom_spiflash_api_funcs->end(chip, err);
  953. if (err == ESP_OK) {
  954. *qe = (io_mode == SPI_FLASH_QOUT);
  955. }
  956. return err;
  957. }
  958. IRAM_ATTR esp_err_t esp_flash_set_io_mode(esp_flash_t* chip, bool qe)
  959. {
  960. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  961. VERIFY_CHIP_OP(set_io_mode);
  962. chip->read_mode = (qe? SPI_FLASH_QOUT: SPI_FLASH_SLOWRD);
  963. err = rom_spiflash_api_funcs->start(chip);
  964. if (err != ESP_OK) {
  965. return err;
  966. }
  967. err = chip->chip_drv->set_io_mode(chip);
  968. return rom_spiflash_api_funcs->end(chip, err);
  969. }
  970. #endif //CONFIG_SPI_FLASH_ROM_IMPL
  971. //init suspend mode cmd, uses internal.
  972. esp_err_t esp_flash_suspend_cmd_init(esp_flash_t* chip)
  973. {
  974. ESP_EARLY_LOGW(TAG, "Flash suspend feature is enabled");
  975. if (chip->chip_drv->get_chip_caps == NULL) {
  976. // chip caps get failed, pass the flash capability check.
  977. ESP_EARLY_LOGW(TAG, "get_chip_caps function pointer hasn't been initialized");
  978. } else {
  979. if ((chip->chip_drv->get_chip_caps(chip) & SPI_FLASH_CHIP_CAP_SUSPEND) == 0) {
  980. ESP_EARLY_LOGW(TAG, "Suspend and resume may not supported for this flash model yet.");
  981. }
  982. }
  983. return chip->chip_drv->sus_setup(chip);
  984. }
  985. esp_err_t esp_flash_app_disable_protect(bool disable)
  986. {
  987. if (disable) {
  988. return esp_flash_app_disable_os_functions(esp_flash_default_chip);
  989. } else {
  990. return esp_flash_app_enable_os_functions(esp_flash_default_chip);
  991. }
  992. }