esp_flash_api.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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_chip_generic.h" //for spi_flash_chip_generic_yield()
  24. static const char TAG[] = "spi_flash";
  25. #ifdef CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE
  26. #define MAX_WRITE_CHUNK CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE /* write in chunks */
  27. #else
  28. #define MAX_WRITE_CHUNK 8192 /* write in chunks */
  29. #endif // CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE
  30. #define MAX_READ_CHUNK 16384
  31. #ifdef CONFIG_SPI_FLASH_DANGEROUS_WRITE_ABORTS
  32. #define UNSAFE_WRITE_ADDRESS abort()
  33. #else
  34. #define UNSAFE_WRITE_ADDRESS return ESP_ERR_INVALID_ARG
  35. #endif
  36. /* CHECK_WRITE_ADDRESS macro to fail writes which land in the
  37. bootloader, partition table, or running application region.
  38. */
  39. #if CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED
  40. #define CHECK_WRITE_ADDRESS(CHIP, ADDR, SIZE)
  41. #else /* FAILS or ABORTS */
  42. #define CHECK_WRITE_ADDRESS(CHIP, ADDR, SIZE) do { \
  43. if (CHIP && CHIP->os_func->region_protected && CHIP->os_func->region_protected(CHIP->os_func_data, ADDR, SIZE)) { \
  44. UNSAFE_WRITE_ADDRESS; \
  45. } \
  46. } while(0)
  47. #endif // CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED
  48. #define IO_STR_LEN 7
  49. static const char io_mode_str[][IO_STR_LEN] = {
  50. "slowrd",
  51. "fastrd",
  52. "dout",
  53. "dio",
  54. "qout",
  55. "qio",
  56. };
  57. _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_ll.h");
  58. esp_err_t esp_flash_read_chip_id(esp_flash_t* chip, uint32_t* flash_id);
  59. static esp_err_t spiflash_start_default(esp_flash_t *chip);
  60. static esp_err_t spiflash_end_default(esp_flash_t *chip, esp_err_t err);
  61. static esp_err_t check_chip_pointer_default(esp_flash_t **inout_chip);
  62. 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);
  63. typedef struct {
  64. esp_err_t (*start)(esp_flash_t *chip);
  65. esp_err_t (*end)(esp_flash_t *chip, esp_err_t err);
  66. esp_err_t (*chip_check)(esp_flash_t **inout_chip);
  67. esp_err_t (*flash_end_flush_cache)(esp_flash_t* chip, esp_err_t err, bool bus_acquired, uint32_t address, uint32_t length);
  68. } rom_spiflash_api_func_t;
  69. // These functions can be placed in the ROM. For now we use the code in IDF.
  70. DRAM_ATTR static rom_spiflash_api_func_t default_spiflash_rom_api = {
  71. .start = spiflash_start_default,
  72. .end = spiflash_end_default,
  73. .chip_check = check_chip_pointer_default,
  74. .flash_end_flush_cache = flash_end_flush_cache,
  75. };
  76. DRAM_ATTR rom_spiflash_api_func_t *rom_spiflash_api_funcs = &default_spiflash_rom_api;
  77. /* Static function to notify OS of a new SPI flash operation.
  78. If returns an error result, caller must abort. If returns ESP_OK, caller must
  79. call rom_spiflash_api_funcs->end() before returning.
  80. */
  81. static esp_err_t IRAM_ATTR spiflash_start_default(esp_flash_t *chip)
  82. {
  83. if (chip->os_func != NULL && chip->os_func->start != NULL) {
  84. esp_err_t err = chip->os_func->start(chip->os_func_data);
  85. if (err != ESP_OK) {
  86. return err;
  87. }
  88. }
  89. chip->host->driver->dev_config(chip->host);
  90. return ESP_OK;
  91. }
  92. /* Static function to notify OS that SPI flash operation is complete.
  93. */
  94. static esp_err_t IRAM_ATTR spiflash_end_default(esp_flash_t *chip, esp_err_t err)
  95. {
  96. if (chip->os_func != NULL
  97. && chip->os_func->end != NULL) {
  98. esp_err_t end_err = chip->os_func->end(chip->os_func_data);
  99. if (err == ESP_OK) {
  100. err = end_err; // Only return the 'end' error if we haven't already failed
  101. }
  102. }
  103. return err;
  104. }
  105. // check that the 'chip' parameter is properly initialised
  106. static esp_err_t check_chip_pointer_default(esp_flash_t **inout_chip)
  107. {
  108. esp_flash_t *chip = *inout_chip;
  109. if (chip == NULL) {
  110. chip = esp_flash_default_chip;
  111. }
  112. *inout_chip = chip;
  113. if (chip == NULL || !esp_flash_chip_driver_initialized(chip)) {
  114. return ESP_ERR_FLASH_NOT_INITIALISED;
  115. }
  116. return ESP_OK;
  117. }
  118. 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)
  119. {
  120. if (!bus_acquired) {
  121. // Try to acquire the bus again to flush the cache before exit.
  122. esp_err_t acquire_err = rom_spiflash_api_funcs->start(chip);
  123. if (acquire_err != ESP_OK) {
  124. return (err == ESP_OK)? acquire_err: err;
  125. }
  126. }
  127. if (chip->host->driver->flush_cache) {
  128. esp_err_t flush_err = chip->host->driver->flush_cache(chip->host, address, length);
  129. if (err == ESP_OK) {
  130. err = flush_err;
  131. }
  132. }
  133. return rom_spiflash_api_funcs->end(chip, err);
  134. }
  135. /* Return true if regions 'a' and 'b' overlap at all, based on their start offsets and lengths. */
  136. inline static bool regions_overlap(uint32_t a_start, uint32_t a_len,uint32_t b_start, uint32_t b_len);
  137. /* Top-level API functions, calling into chip_drv functions via chip->drv */
  138. static esp_err_t detect_spi_flash_chip(esp_flash_t *chip);
  139. bool esp_flash_chip_driver_initialized(const esp_flash_t *chip)
  140. {
  141. if (!chip->chip_drv) return false;
  142. return true;
  143. }
  144. esp_err_t IRAM_ATTR esp_flash_init(esp_flash_t *chip)
  145. {
  146. // Chip init flow
  147. // 1. Read chip id
  148. // 2. (optional) Detect chip vendor
  149. // 3. Get basic parameters of the chip (size, dummy count, etc.)
  150. // 4. Init chip into desired mode (without breaking the cache!)
  151. esp_err_t err = ESP_OK;
  152. if (chip == NULL || chip->host == NULL || chip->host->driver == NULL ||
  153. ((memspi_host_inst_t*)chip->host)->spi == NULL) {
  154. return ESP_ERR_INVALID_ARG;
  155. }
  156. //read chip id
  157. uint32_t flash_id;
  158. int retries = 10;
  159. do {
  160. err = esp_flash_read_chip_id(chip, &flash_id);
  161. } while (err == ESP_ERR_FLASH_NOT_INITIALISED && retries-- > 0);
  162. if (err != ESP_OK) {
  163. return err;
  164. }
  165. chip->chip_id = flash_id;
  166. if (!esp_flash_chip_driver_initialized(chip)) {
  167. // Detect chip_drv
  168. err = detect_spi_flash_chip(chip);
  169. if (err != ESP_OK) {
  170. return err;
  171. }
  172. }
  173. // Detect flash size
  174. uint32_t size;
  175. err = esp_flash_get_size(chip, &size);
  176. if (err != ESP_OK) {
  177. ESP_LOGE(TAG, "failed to get chip size");
  178. return err;
  179. }
  180. ESP_LOGI(TAG, "flash io: %s", io_mode_str[chip->read_mode]);
  181. err = rom_spiflash_api_funcs->start(chip);
  182. if (err != ESP_OK) {
  183. return err;
  184. }
  185. if (err == ESP_OK) {
  186. // Try to set the flash mode to whatever default mode was chosen
  187. err = chip->chip_drv->set_io_mode(chip);
  188. if (err == ESP_ERR_FLASH_NO_RESPONSE && !esp_flash_is_quad_mode(chip)) {
  189. //some chips (e.g. Winbond) don't support to clear QE, treat as success
  190. err = ESP_OK;
  191. }
  192. }
  193. // Done: all fields on 'chip' are initialised
  194. return rom_spiflash_api_funcs->end(chip, err);
  195. }
  196. static esp_err_t IRAM_ATTR read_id_core(esp_flash_t* chip, uint32_t* out_id, bool sanity_check)
  197. {
  198. bool installed = esp_flash_chip_driver_initialized(chip);
  199. esp_err_t err = rom_spiflash_api_funcs->start(chip);
  200. if (err != ESP_OK) {
  201. return err;
  202. }
  203. esp_err_t (*read_id_func)(void*, uint32_t*);
  204. void* read_id_arg;
  205. if (installed && chip->chip_drv->read_id) {
  206. read_id_func = (void*)chip->chip_drv->read_id;
  207. read_id_arg = (void*)chip;
  208. } else {
  209. //default option if the chip is not detected/chosen yet.
  210. read_id_func = (void*)chip->host->driver->read_id;
  211. read_id_arg = (void*)chip->host;
  212. }
  213. // Inner function fails if it sees all-ones or all-zeroes.
  214. err = read_id_func(read_id_arg, out_id);
  215. if (sanity_check && err == ESP_OK) {
  216. // Send RDID command twice, check for a matching result and retry in case we just powered on
  217. uint32_t new_id;
  218. err = read_id_func(read_id_arg, &new_id);
  219. if (err == ESP_OK && (new_id != *out_id)) {
  220. err = ESP_ERR_FLASH_NOT_INITIALISED;
  221. }
  222. }
  223. return rom_spiflash_api_funcs->end(chip, err);
  224. }
  225. // Faster version with sanity check.
  226. // Called in esp_flash_init and unit test (though not public)
  227. esp_err_t esp_flash_read_chip_id(esp_flash_t* chip, uint32_t* out_id)
  228. {
  229. return read_id_core(chip, out_id, true);
  230. }
  231. esp_err_t esp_flash_read_id(esp_flash_t* chip, uint32_t* out_id)
  232. {
  233. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  234. //Accept uninitialized chip when reading chip id
  235. if (err != ESP_OK && !(err == ESP_ERR_FLASH_NOT_INITIALISED && chip != NULL)) return err;
  236. if (out_id == NULL) return ESP_ERR_INVALID_ARG;
  237. return read_id_core(chip, out_id, false);
  238. }
  239. static esp_err_t IRAM_ATTR detect_spi_flash_chip(esp_flash_t *chip)
  240. {
  241. esp_err_t err;
  242. uint32_t flash_id = chip->chip_id;
  243. // Detect the chip and set the chip_drv structure for it
  244. const spi_flash_chip_t **drivers = esp_flash_registered_chips;
  245. while (*drivers != NULL && !esp_flash_chip_driver_initialized(chip)) {
  246. chip->chip_drv = *drivers;
  247. // start/end SPI operation each time, for multitasking
  248. // and also so esp_flash_registered_flash_drivers can live in flash
  249. ESP_LOGD(TAG, "trying chip: %s", chip->chip_drv->name);
  250. err = rom_spiflash_api_funcs->start(chip);
  251. if (err != ESP_OK) {
  252. return err;
  253. }
  254. if (chip->chip_drv->probe(chip, flash_id) != ESP_OK) {
  255. chip->chip_drv = NULL;
  256. }
  257. // if probe succeeded, chip->drv stays set
  258. drivers++;
  259. err = rom_spiflash_api_funcs->end(chip, err);
  260. if (err != ESP_OK) {
  261. return err;
  262. }
  263. }
  264. if (!esp_flash_chip_driver_initialized(chip)) {
  265. return ESP_ERR_NOT_FOUND;
  266. }
  267. ESP_LOGI(TAG, "detected chip: %s", chip->chip_drv->name);
  268. return ESP_OK;
  269. }
  270. /* Convenience macro for beginning of all API functions.
  271. * Check the return value of `rom_spiflash_api_funcs->chip_check` is correct,
  272. * and the chip supports the operation in question.
  273. */
  274. #define VERIFY_CHIP_OP(OP) do { \
  275. if (err != ESP_OK) return err; \
  276. if (chip->chip_drv->OP == NULL) { \
  277. return ESP_ERR_FLASH_UNSUPPORTED_CHIP; \
  278. } \
  279. } while (0)
  280. esp_err_t IRAM_ATTR esp_flash_get_size(esp_flash_t *chip, uint32_t *out_size)
  281. {
  282. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  283. VERIFY_CHIP_OP(detect_size);
  284. if (out_size == NULL) {
  285. return ESP_ERR_INVALID_ARG;
  286. }
  287. if (chip->size != 0) {
  288. *out_size = chip->size;
  289. return ESP_OK;
  290. }
  291. err = rom_spiflash_api_funcs->start(chip);
  292. if (err != ESP_OK) {
  293. return err;
  294. }
  295. uint32_t detect_size;
  296. err = chip->chip_drv->detect_size(chip, &detect_size);
  297. if (err == ESP_OK) {
  298. chip->size = detect_size;
  299. }
  300. return rom_spiflash_api_funcs->end(chip, err);
  301. }
  302. esp_err_t IRAM_ATTR esp_flash_erase_chip(esp_flash_t *chip)
  303. {
  304. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  305. VERIFY_CHIP_OP(erase_chip);
  306. CHECK_WRITE_ADDRESS(chip, 0, chip->size);
  307. //check before the operation, in case this is called too close to the last operation
  308. err = spi_flash_chip_generic_yield(chip, false);
  309. if (err != ESP_OK) {
  310. return err;
  311. }
  312. err = rom_spiflash_api_funcs->start(chip);
  313. if (err != ESP_OK) {
  314. return err;
  315. }
  316. err = chip->chip_drv->erase_chip(chip);
  317. if (chip->host->driver->flush_cache) {
  318. esp_err_t flush_cache_err = chip->host->driver->flush_cache(chip->host, 0, chip->size);
  319. if (err == ESP_OK) {
  320. err = flush_cache_err;
  321. }
  322. }
  323. return rom_spiflash_api_funcs->end(chip, err);
  324. }
  325. esp_err_t IRAM_ATTR esp_flash_erase_region(esp_flash_t *chip, uint32_t start, uint32_t len)
  326. {
  327. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  328. VERIFY_CHIP_OP(erase_sector);
  329. VERIFY_CHIP_OP(erase_block);
  330. CHECK_WRITE_ADDRESS(chip, start, len);
  331. uint32_t block_erase_size = chip->chip_drv->erase_block == NULL ? 0 : chip->chip_drv->block_erase_size;
  332. uint32_t sector_size = chip->chip_drv->sector_size;
  333. if (sector_size == 0 || (block_erase_size % sector_size) != 0) {
  334. return ESP_ERR_FLASH_NOT_INITIALISED;
  335. }
  336. if (start > chip->size || start + len > chip->size) {
  337. return ESP_ERR_INVALID_ARG;
  338. }
  339. if ((start % chip->chip_drv->sector_size) != 0 || (len % chip->chip_drv->sector_size) != 0) {
  340. // Can only erase multiples of the sector size, starting at sector boundary
  341. return ESP_ERR_INVALID_ARG;
  342. }
  343. err = ESP_OK;
  344. // Check for write protected regions overlapping the erase region
  345. if (chip->chip_drv->get_protected_regions != NULL &&
  346. chip->chip_drv->num_protectable_regions > 0) {
  347. err = rom_spiflash_api_funcs->start(chip);
  348. if (err != ESP_OK) {
  349. return err;
  350. }
  351. uint64_t protected = 0;
  352. err = chip->chip_drv->get_protected_regions(chip, &protected);
  353. if (err == ESP_OK && protected != 0) {
  354. for (int i = 0; i < chip->chip_drv->num_protectable_regions && err == ESP_OK; i++) {
  355. const esp_flash_region_t *region = &chip->chip_drv->protectable_regions[i];
  356. if ((protected & BIT64(i))
  357. && regions_overlap(start, len, region->offset, region->size)) {
  358. err = ESP_ERR_FLASH_PROTECTED;
  359. }
  360. }
  361. }
  362. // Don't lock the SPI flash for the entire erase, as this may be very long
  363. err = rom_spiflash_api_funcs->end(chip, err);
  364. }
  365. if (err != ESP_OK) {
  366. return err;
  367. }
  368. uint32_t erase_addr = start;
  369. uint32_t len_remain = len;
  370. // Indicate whether the bus is acquired by the driver, needs to be released before return
  371. bool bus_acquired = false;
  372. while (1) {
  373. //check before the operation, in case this is called too close to the last operation
  374. err = spi_flash_chip_generic_yield(chip, false);
  375. if (err != ESP_OK) {
  376. break;
  377. }
  378. err = rom_spiflash_api_funcs->start(chip);
  379. if (err != ESP_OK) {
  380. break;
  381. }
  382. bus_acquired = true;
  383. #ifndef CONFIG_SPI_FLASH_BYPASS_BLOCK_ERASE
  384. // If possible erase an entire multi-sector block
  385. if (block_erase_size > 0 && len_remain >= block_erase_size && (erase_addr % block_erase_size) == 0) {
  386. err = chip->chip_drv->erase_block(chip, erase_addr);
  387. erase_addr += block_erase_size;
  388. len_remain -= block_erase_size;
  389. } else
  390. #endif
  391. {
  392. // Otherwise erase individual sector only
  393. err = chip->chip_drv->erase_sector(chip, erase_addr);
  394. erase_addr += sector_size;
  395. len_remain -= sector_size;
  396. }
  397. if (err != ESP_OK || len_remain == 0) {
  398. // On ESP32, the cache re-enable is in the end() function, while flush_cache should
  399. // happen when the cache is still disabled on ESP32. Break before the end() function and
  400. // do end() later
  401. assert(bus_acquired);
  402. break;
  403. }
  404. err = rom_spiflash_api_funcs->end(chip, ESP_OK);
  405. if (err != ESP_OK) {
  406. break;
  407. }
  408. bus_acquired = false;
  409. }
  410. return rom_spiflash_api_funcs->flash_end_flush_cache(chip, err, bus_acquired, start, len);
  411. }
  412. esp_err_t IRAM_ATTR esp_flash_get_chip_write_protect(esp_flash_t *chip, bool *out_write_protected)
  413. {
  414. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  415. VERIFY_CHIP_OP(get_chip_write_protect);
  416. if (out_write_protected == NULL) {
  417. return ESP_ERR_INVALID_ARG;
  418. }
  419. err = rom_spiflash_api_funcs->start(chip);
  420. if (err != ESP_OK) {
  421. return err;
  422. }
  423. err = chip->chip_drv->get_chip_write_protect(chip, out_write_protected);
  424. return rom_spiflash_api_funcs->end(chip, err);
  425. }
  426. esp_err_t IRAM_ATTR esp_flash_set_chip_write_protect(esp_flash_t *chip, bool write_protect)
  427. {
  428. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  429. VERIFY_CHIP_OP(set_chip_write_protect);
  430. //TODO: skip writing if already locked or unlocked
  431. err = rom_spiflash_api_funcs->start(chip);
  432. if (err != ESP_OK) {
  433. return err;
  434. }
  435. err = chip->chip_drv->set_chip_write_protect(chip, write_protect);
  436. return rom_spiflash_api_funcs->end(chip, err);
  437. }
  438. 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)
  439. {
  440. if(out_num_regions != NULL) {
  441. *out_num_regions = 0; // In case caller doesn't check result
  442. }
  443. esp_err_t err = rom_spiflash_api_funcs->chip_check((esp_flash_t **)&chip);
  444. VERIFY_CHIP_OP(get_protected_regions);
  445. if(out_regions == NULL || out_num_regions == NULL) {
  446. return ESP_ERR_INVALID_ARG;
  447. }
  448. *out_num_regions = chip->chip_drv->num_protectable_regions;
  449. *out_regions = chip->chip_drv->protectable_regions;
  450. return ESP_OK;
  451. }
  452. static esp_err_t find_region(const esp_flash_t *chip, const esp_flash_region_t *region, uint8_t *index)
  453. {
  454. if (region == NULL) {
  455. return ESP_ERR_INVALID_ARG;
  456. }
  457. for(*index = 0; *index < chip->chip_drv->num_protectable_regions; (*index)++) {
  458. if (memcmp(&chip->chip_drv->protectable_regions[*index],
  459. region, sizeof(esp_flash_region_t)) == 0) {
  460. return ESP_OK;
  461. }
  462. }
  463. return ESP_ERR_NOT_FOUND;
  464. }
  465. esp_err_t IRAM_ATTR esp_flash_get_protected_region(esp_flash_t *chip, const esp_flash_region_t *region, bool *out_protected)
  466. {
  467. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  468. VERIFY_CHIP_OP(get_protected_regions);
  469. if (out_protected == NULL) {
  470. return ESP_ERR_INVALID_ARG;
  471. }
  472. uint8_t index;
  473. err = find_region(chip, region, &index);
  474. if (err != ESP_OK) {
  475. return err;
  476. }
  477. uint64_t protection_mask = 0;
  478. err = rom_spiflash_api_funcs->start(chip);
  479. if (err != ESP_OK) {
  480. return err;
  481. }
  482. err = chip->chip_drv->get_protected_regions(chip, &protection_mask);
  483. if (err == ESP_OK) {
  484. *out_protected = protection_mask & (1LL << index);
  485. }
  486. return rom_spiflash_api_funcs->end(chip, err);
  487. }
  488. esp_err_t IRAM_ATTR esp_flash_set_protected_region(esp_flash_t *chip, const esp_flash_region_t *region, bool protect)
  489. {
  490. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  491. VERIFY_CHIP_OP(set_protected_regions);
  492. uint8_t index;
  493. err = find_region(chip, region, &index);
  494. if (err != ESP_OK) {
  495. return err;
  496. }
  497. uint64_t protection_mask = 0;
  498. err = rom_spiflash_api_funcs->start(chip);
  499. if (err != ESP_OK) {
  500. return err;
  501. }
  502. err = chip->chip_drv->get_protected_regions(chip, &protection_mask);
  503. if (err == ESP_OK) {
  504. if (protect) {
  505. protection_mask |= (1LL << index);
  506. } else {
  507. protection_mask &= ~(1LL << index);
  508. }
  509. err = chip->chip_drv->set_protected_regions(chip, protection_mask);
  510. }
  511. return rom_spiflash_api_funcs->end(chip, err);
  512. }
  513. esp_err_t IRAM_ATTR esp_flash_read(esp_flash_t *chip, void *buffer, uint32_t address, uint32_t length)
  514. {
  515. if (length == 0) {
  516. return ESP_OK;
  517. }
  518. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  519. VERIFY_CHIP_OP(read);
  520. if (buffer == NULL || address > chip->size || address+length > chip->size) {
  521. return ESP_ERR_INVALID_ARG;
  522. }
  523. //when the cache is disabled, only the DRAM can be read, check whether we need to receive in another buffer in DRAM.
  524. bool direct_read = chip->host->driver->supports_direct_read(chip->host, buffer);
  525. uint8_t* temp_buffer = NULL;
  526. //each time, we at most read this length
  527. //after that, we release the lock to allow some other operations
  528. size_t read_chunk_size = MIN(MAX_READ_CHUNK, length);
  529. if (!direct_read) {
  530. size_t actual_len = 0;
  531. if (chip->os_func->get_temp_buffer != NULL) {
  532. temp_buffer = chip->os_func->get_temp_buffer(chip->os_func_data, read_chunk_size, &actual_len);
  533. read_chunk_size = actual_len;
  534. }
  535. if (temp_buffer == NULL) {
  536. return ESP_ERR_NO_MEM;
  537. }
  538. }
  539. err = ESP_OK;
  540. do {
  541. err = rom_spiflash_api_funcs->start(chip);
  542. if (err != ESP_OK) {
  543. break;
  544. }
  545. //if required (dma buffer allocated), read to the buffer instead of the original buffer
  546. uint8_t* buffer_to_read = (temp_buffer)? temp_buffer : buffer;
  547. // Length we will read this iteration is either the chunk size or the remaining length, whichever is smaller
  548. size_t length_to_read = MIN(read_chunk_size, length);
  549. if (err == ESP_OK) {
  550. err = chip->chip_drv->read(chip, buffer_to_read, address, length_to_read);
  551. }
  552. if (err != ESP_OK) {
  553. rom_spiflash_api_funcs->end(chip, err);
  554. break;
  555. }
  556. //even if this is failed, the data is still valid, copy before quit
  557. err = rom_spiflash_api_funcs->end(chip, err);
  558. //copy back to the original buffer
  559. if (temp_buffer) {
  560. memcpy(buffer, temp_buffer, length_to_read);
  561. }
  562. address += length_to_read;
  563. length -= length_to_read;
  564. buffer = (void*)((intptr_t)buffer + length_to_read);
  565. } while (err == ESP_OK && length > 0);
  566. if (chip->os_func->release_temp_buffer != NULL) {
  567. chip->os_func->release_temp_buffer(chip->os_func_data, temp_buffer);
  568. }
  569. return err;
  570. }
  571. esp_err_t IRAM_ATTR esp_flash_write(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length)
  572. {
  573. if (length == 0) {
  574. return ESP_OK;
  575. }
  576. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  577. VERIFY_CHIP_OP(write);
  578. CHECK_WRITE_ADDRESS(chip, address, length);
  579. if (buffer == NULL || address > chip->size || address+length > chip->size) {
  580. return ESP_ERR_INVALID_ARG;
  581. }
  582. //when the cache is disabled, only the DRAM can be read, check whether we need to copy the data first
  583. bool direct_write = chip->host->driver->supports_direct_write(chip->host, buffer);
  584. // Indicate whether the bus is acquired by the driver, needs to be released before return
  585. bool bus_acquired = false;
  586. err = ESP_OK;
  587. /* Write output in chunks, either by buffering on stack or
  588. by artificially cutting into MAX_WRITE_CHUNK parts (in an OS
  589. environment, this prevents writing from causing interrupt or higher priority task
  590. starvation.) */
  591. uint32_t write_addr = address;
  592. uint32_t len_remain = length;
  593. while (1) {
  594. uint32_t write_len;
  595. const void *write_buf;
  596. uint32_t temp_buf[8];
  597. if (direct_write) {
  598. write_len = MIN(len_remain, MAX_WRITE_CHUNK);
  599. write_buf = buffer;
  600. } else {
  601. write_len = MIN(len_remain, sizeof(temp_buf));
  602. memcpy(temp_buf, buffer, write_len);
  603. write_buf = temp_buf;
  604. }
  605. //check before the operation, in case this is called too close to the last operation
  606. err = spi_flash_chip_generic_yield(chip, false);
  607. if (err != ESP_OK) {
  608. break;
  609. }
  610. err = rom_spiflash_api_funcs->start(chip);
  611. if (err != ESP_OK) {
  612. break;
  613. }
  614. bus_acquired = true;
  615. err = chip->chip_drv->write(chip, write_buf, write_addr, write_len);
  616. len_remain -= write_len;
  617. if (err != ESP_OK || len_remain == 0) {
  618. // On ESP32, the cache re-enable is in the end() function, while flush_cache should
  619. // happen when the cache is still disabled on ESP32. Break before the end() function and
  620. // do end() later
  621. assert(bus_acquired);
  622. break;
  623. }
  624. err = rom_spiflash_api_funcs->end(chip, err);
  625. if (err != ESP_OK) {
  626. break;
  627. }
  628. bus_acquired = false;
  629. write_addr += write_len;
  630. buffer = (void *)((intptr_t)buffer + write_len);
  631. }
  632. return rom_spiflash_api_funcs->flash_end_flush_cache(chip, err, bus_acquired, address, length);
  633. }
  634. //currently the legacy implementation is used, from flash_ops.c
  635. esp_err_t spi_flash_write_encrypted(size_t dest_addr, const void *src, size_t size);
  636. esp_err_t IRAM_ATTR esp_flash_write_encrypted(esp_flash_t *chip, uint32_t address, const void *buffer, uint32_t length)
  637. {
  638. /*
  639. * Since currently this feature is supported only by the hardware, there
  640. * is no way to support non-standard chips. We use the legacy
  641. * implementation and skip the chip and driver layers.
  642. */
  643. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  644. if (err != ESP_OK) return err;
  645. if (buffer == NULL || address > chip->size || address+length > chip->size) {
  646. return ESP_ERR_INVALID_ARG;
  647. }
  648. return spi_flash_write_encrypted(address, buffer, length);
  649. }
  650. inline static IRAM_ATTR bool regions_overlap(uint32_t a_start, uint32_t a_len,uint32_t b_start, uint32_t b_len)
  651. {
  652. uint32_t a_end = a_start + a_len;
  653. uint32_t b_end = b_start + b_len;
  654. return (a_end > b_start && b_end > a_start);
  655. }
  656. //currently the legacy implementation is used, from flash_ops.c
  657. esp_err_t spi_flash_read_encrypted(size_t src, void *dstv, size_t size);
  658. esp_err_t IRAM_ATTR esp_flash_read_encrypted(esp_flash_t *chip, uint32_t address, void *out_buffer, uint32_t length)
  659. {
  660. /*
  661. * Since currently this feature is supported only by the hardware, there
  662. * is no way to support non-standard chips. We use the legacy
  663. * implementation and skip the chip and driver layers.
  664. */
  665. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  666. if (err != ESP_OK) return err;
  667. return spi_flash_read_encrypted(address, out_buffer, length);
  668. }
  669. // test only, non-public
  670. IRAM_ATTR esp_err_t esp_flash_get_io_mode(esp_flash_t* chip, bool* qe)
  671. {
  672. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  673. VERIFY_CHIP_OP(get_io_mode);
  674. esp_flash_io_mode_t io_mode;
  675. err = rom_spiflash_api_funcs->start(chip);
  676. if (err != ESP_OK) {
  677. return err;
  678. }
  679. err = chip->chip_drv->get_io_mode(chip, &io_mode);
  680. err = rom_spiflash_api_funcs->end(chip, err);
  681. if (err == ESP_OK) {
  682. *qe = (io_mode == SPI_FLASH_QOUT);
  683. }
  684. return err;
  685. }
  686. IRAM_ATTR esp_err_t esp_flash_set_io_mode(esp_flash_t* chip, bool qe)
  687. {
  688. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  689. VERIFY_CHIP_OP(set_io_mode);
  690. chip->read_mode = (qe? SPI_FLASH_QOUT: SPI_FLASH_SLOWRD);
  691. err = rom_spiflash_api_funcs->start(chip);
  692. if (err != ESP_OK) {
  693. return err;
  694. }
  695. err = chip->chip_drv->set_io_mode(chip);
  696. return rom_spiflash_api_funcs->end(chip, err);
  697. }
  698. #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  699. esp_err_t esp_flash_app_disable_protect(bool disable)
  700. {
  701. if (disable) {
  702. return esp_flash_app_disable_os_functions(esp_flash_default_chip);
  703. } else {
  704. return esp_flash_app_enable_os_functions(esp_flash_default_chip);
  705. }
  706. }
  707. #endif
  708. /*------------------------------------------------------------------------------
  709. Adapter layer to original api before IDF v4.0
  710. ------------------------------------------------------------------------------*/
  711. #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  712. /* Translate any ESP_ERR_FLASH_xxx error code (new API) to a generic ESP_ERR_xyz error code
  713. */
  714. static IRAM_ATTR esp_err_t spi_flash_translate_rc(esp_err_t err)
  715. {
  716. switch (err) {
  717. case ESP_OK:
  718. case ESP_ERR_INVALID_ARG:
  719. case ESP_ERR_NO_MEM:
  720. return err;
  721. case ESP_ERR_FLASH_NOT_INITIALISED:
  722. case ESP_ERR_FLASH_PROTECTED:
  723. return ESP_ERR_INVALID_STATE;
  724. case ESP_ERR_NOT_FOUND:
  725. case ESP_ERR_FLASH_UNSUPPORTED_HOST:
  726. case ESP_ERR_FLASH_UNSUPPORTED_CHIP:
  727. return ESP_ERR_NOT_SUPPORTED;
  728. case ESP_ERR_FLASH_NO_RESPONSE:
  729. return ESP_ERR_INVALID_RESPONSE;
  730. default:
  731. ESP_EARLY_LOGE(TAG, "unexpected spi flash error code: 0x%x", err);
  732. abort();
  733. }
  734. }
  735. esp_err_t IRAM_ATTR spi_flash_erase_range(uint32_t start_addr, uint32_t size)
  736. {
  737. esp_err_t err = esp_flash_erase_region(NULL, start_addr, size);
  738. return spi_flash_translate_rc(err);
  739. }
  740. esp_err_t IRAM_ATTR spi_flash_write(size_t dst, const void *srcv, size_t size)
  741. {
  742. esp_err_t err = esp_flash_write(NULL, srcv, dst, size);
  743. return spi_flash_translate_rc(err);
  744. }
  745. esp_err_t IRAM_ATTR spi_flash_read(size_t src, void *dstv, size_t size)
  746. {
  747. esp_err_t err = esp_flash_read(NULL, dstv, src, size);
  748. return spi_flash_translate_rc(err);
  749. }
  750. #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL