esp_flash_api.c 30 KB

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