esp_flash_api.c 30 KB

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