esp_flash_api.c 38 KB

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