esp_flash_api.c 40 KB

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