partition_linux.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <assert.h>
  8. #include <string.h>
  9. #if __has_include(<bsd/string.h>)
  10. // for strlcpy
  11. #include <bsd/string.h>
  12. #endif
  13. #include <sys/mman.h>
  14. #include <sys/stat.h>
  15. #include <fcntl.h>
  16. #include <unistd.h>
  17. #include <limits.h>
  18. #include <errno.h>
  19. #include "sdkconfig.h"
  20. #include "esp_partition.h"
  21. #include "esp_flash_partitions.h"
  22. #include "esp_private/partition_linux.h"
  23. #include "esp_log.h"
  24. #include "spi_flash_mmap.h"
  25. static const char *TAG = "linux_spiflash";
  26. static void *s_spiflash_mem_file_buf = NULL;
  27. static int s_spiflash_mem_file_fd = -1;
  28. static const esp_partition_mmap_handle_t s_default_partition_mmap_handle = 0;
  29. // input control structure, always contains what was specified by caller
  30. static esp_partition_file_mmap_ctrl_t s_esp_partition_file_mmap_ctrl_input = {0};
  31. // actual control structure, contains what is actually used by the esp_partition
  32. static esp_partition_file_mmap_ctrl_t s_esp_partition_file_mmap_ctrl_act = {0};
  33. #ifdef CONFIG_ESP_PARTITION_ENABLE_STATS
  34. // variables holding stats and controlling power-off emulation
  35. static size_t s_esp_partition_stat_read_ops = 0;
  36. static size_t s_esp_partition_stat_write_ops = 0;
  37. static size_t s_esp_partition_stat_read_bytes = 0;
  38. static size_t s_esp_partition_stat_write_bytes = 0;
  39. static size_t s_esp_partition_stat_erase_ops = 0;
  40. static size_t s_esp_partition_stat_total_time = 0;
  41. static size_t s_esp_partition_emulated_power_off_counter = SIZE_MAX;
  42. static uint8_t s_esp_partition_emulated_power_off_mode = 0;
  43. // tracking erase count individually for each emulated sector
  44. static size_t *s_esp_partition_stat_sector_erase_count = NULL;
  45. // forward declaration of hooks
  46. static void esp_partition_hook_read(const void *srcAddr, const size_t size);
  47. static bool esp_partition_hook_write(const void *dstAddr, size_t *size);
  48. static bool esp_partition_hook_erase(const void *dstAddr, size_t *size);
  49. // redirect hooks to functions
  50. #define ESP_PARTITION_HOOK_READ(srcAddr, size) esp_partition_hook_read(srcAddr, size)
  51. #define ESP_PARTITION_HOOK_WRITE(dstAddr, size) esp_partition_hook_write(dstAddr, size)
  52. #define ESP_PARTITION_HOOK_ERASE(dstAddr, size) esp_partition_hook_erase(dstAddr, size)
  53. #else
  54. // redirect hooks to "do nothing code"
  55. #define ESP_PARTITION_HOOK_READ(srcAddr, size)
  56. #define ESP_PARTITION_HOOK_WRITE(dstAddr, size) true
  57. #define ESP_PARTITION_HOOK_ERASE(dstAddr, size) true
  58. #endif
  59. const char *esp_partition_type_to_str(const uint32_t type)
  60. {
  61. switch (type) {
  62. case PART_TYPE_APP: return "app";
  63. case PART_TYPE_DATA: return "data";
  64. default: return "unknown";
  65. }
  66. }
  67. const char *esp_partition_subtype_to_str(const uint32_t type, const uint32_t subtype)
  68. {
  69. switch (type) {
  70. case PART_TYPE_APP:
  71. switch (subtype) {
  72. case PART_SUBTYPE_FACTORY: return "factory";
  73. case PART_SUBTYPE_OTA_FLAG: return "ota_flag";
  74. case PART_SUBTYPE_OTA_MASK: return "ota_mask";
  75. case PART_SUBTYPE_TEST: return "test";
  76. default: return "unknown";
  77. }
  78. case PART_TYPE_DATA:
  79. switch (subtype) {
  80. case PART_SUBTYPE_DATA_OTA: return "data_ota";
  81. case PART_SUBTYPE_DATA_RF: return "data_rf";
  82. case PART_SUBTYPE_DATA_WIFI: return "data_wifi";
  83. case PART_SUBTYPE_DATA_NVS_KEYS: return "nvs_keys";
  84. case PART_SUBTYPE_DATA_EFUSE_EM: return "efuse_em";
  85. default: return "unknown";
  86. }
  87. default: return "unknown";
  88. }
  89. }
  90. esp_err_t esp_partition_file_mmap(const uint8_t **part_desc_addr_start)
  91. {
  92. // temporary file is used only if control structure doesn't specify file name.
  93. bool open_existing_file = false;
  94. if (strlen(s_esp_partition_file_mmap_ctrl_input.flash_file_name) > 0) {
  95. // Open existing file. If size or partition table file were specified, raise errors
  96. if (s_esp_partition_file_mmap_ctrl_input.flash_file_size > 0) {
  97. ESP_LOGE(TAG, "Flash emulation file size: %u was specified while together with the file name: %s (illegal). Use file size = 0",
  98. s_esp_partition_file_mmap_ctrl_input.flash_file_size,
  99. s_esp_partition_file_mmap_ctrl_input.flash_file_name);
  100. return ESP_ERR_INVALID_ARG;
  101. }
  102. if (strlen(s_esp_partition_file_mmap_ctrl_input.partition_file_name) > 0) {
  103. ESP_LOGE(TAG, "Partition file name: %s was specified together with the flash emulation file name: %s (illegal). Use empty partition file name",
  104. s_esp_partition_file_mmap_ctrl_input.partition_file_name,
  105. s_esp_partition_file_mmap_ctrl_input.flash_file_name);
  106. return ESP_ERR_INVALID_ARG;
  107. }
  108. // copy flash file name to actual control struct
  109. strlcpy(s_esp_partition_file_mmap_ctrl_act.flash_file_name, s_esp_partition_file_mmap_ctrl_input.flash_file_name, sizeof(s_esp_partition_file_mmap_ctrl_act.flash_file_name));
  110. open_existing_file = true;
  111. } else {
  112. // Open temporary file. If size was specified, also partition table has to be specified, otherwise raise error.
  113. // If none of size, partition table were specified, defaults are used.
  114. // Name of temporary file is available in s_esp_partition_file_mmap_ctrl.flash_file_name
  115. bool has_partfile = (strlen(s_esp_partition_file_mmap_ctrl_input.partition_file_name) > 0);
  116. bool has_len = (s_esp_partition_file_mmap_ctrl_input.flash_file_size > 0);
  117. // conflicting input
  118. if (has_partfile != has_len) {
  119. ESP_LOGE(TAG, "Invalid combination of Partition file name: %s flash file size: %u was specified. Use either both parameters or none.",
  120. s_esp_partition_file_mmap_ctrl_input.partition_file_name,
  121. s_esp_partition_file_mmap_ctrl_input.flash_file_size);
  122. return ESP_ERR_INVALID_ARG;
  123. }
  124. // check if partition file is present, if not, use default
  125. if (!has_partfile) {
  126. strlcpy(s_esp_partition_file_mmap_ctrl_act.partition_file_name, BUILD_DIR "/partition_table/partition-table.bin", sizeof(s_esp_partition_file_mmap_ctrl_act.partition_file_name));
  127. } else {
  128. strlcpy(s_esp_partition_file_mmap_ctrl_act.partition_file_name, s_esp_partition_file_mmap_ctrl_input.partition_file_name, sizeof(s_esp_partition_file_mmap_ctrl_act.partition_file_name));
  129. }
  130. // check if flash size is present, if not set to default
  131. if (!has_len) {
  132. s_esp_partition_file_mmap_ctrl_act.flash_file_size = ESP_PARTITION_DEFAULT_EMULATED_FLASH_SIZE;
  133. } else {
  134. s_esp_partition_file_mmap_ctrl_act.flash_file_size = s_esp_partition_file_mmap_ctrl_input.flash_file_size;
  135. }
  136. // specify pattern file name for temporary flash file
  137. strlcpy(s_esp_partition_file_mmap_ctrl_act.flash_file_name, "/tmp/idf-partition-XXXXXX", sizeof(s_esp_partition_file_mmap_ctrl_act.flash_file_name));
  138. }
  139. esp_err_t ret = ESP_OK;
  140. if (open_existing_file) {
  141. s_spiflash_mem_file_fd = open(s_esp_partition_file_mmap_ctrl_act.flash_file_name, O_RDWR);
  142. if (s_spiflash_mem_file_fd == -1) {
  143. ESP_LOGE(TAG, "Failed to open SPI FLASH emulation file %s: %s", s_esp_partition_file_mmap_ctrl_act.flash_file_name, strerror(errno));
  144. return ESP_ERR_NOT_FOUND;
  145. }
  146. do {
  147. // seek to the end
  148. off_t size = lseek(s_spiflash_mem_file_fd, 0L, SEEK_END);
  149. if (size < 0) {
  150. ESP_LOGE(TAG, "Failed to seek in SPI FLASH emulation file %s: %s", s_esp_partition_file_mmap_ctrl_act.flash_file_name, strerror(errno));
  151. ret = ESP_ERR_NOT_FINISHED;
  152. break;
  153. }
  154. s_esp_partition_file_mmap_ctrl_act.flash_file_size = size;
  155. // seek to beginning
  156. size = lseek(s_spiflash_mem_file_fd, 0L, SEEK_SET);
  157. if (size < 0) {
  158. ESP_LOGE(TAG, "Failed to seek in SPI FLASH emulation file %s: %s", s_esp_partition_file_mmap_ctrl_act.flash_file_name, strerror(errno));
  159. ret = ESP_ERR_NOT_FINISHED;
  160. break;
  161. }
  162. //create memory-mapping for the flash holder file
  163. if ((s_spiflash_mem_file_buf = mmap(NULL, s_esp_partition_file_mmap_ctrl_act.flash_file_size, PROT_READ | PROT_WRITE, MAP_SHARED, s_spiflash_mem_file_fd, 0)) == MAP_FAILED) {
  164. ESP_LOGE(TAG, "Failed to mmap() SPI FLASH memory emulation file %s: %s", s_esp_partition_file_mmap_ctrl_act.flash_file_name, strerror(errno));
  165. ret = ESP_ERR_NOT_FINISHED;
  166. break;
  167. }
  168. } while (false);
  169. } else {
  170. //create temporary file to hold complete SPIFLASH size
  171. s_spiflash_mem_file_fd = mkstemp(s_esp_partition_file_mmap_ctrl_act.flash_file_name);
  172. if (s_spiflash_mem_file_fd == -1) {
  173. ESP_LOGE(TAG, "Failed to create SPI FLASH emulation file %s: %s", s_esp_partition_file_mmap_ctrl_act.flash_file_name, strerror(errno));
  174. return ESP_ERR_NOT_FINISHED;
  175. }
  176. do {
  177. // resize file
  178. if (ftruncate(s_spiflash_mem_file_fd, s_esp_partition_file_mmap_ctrl_act.flash_file_size) != 0) {
  179. ESP_LOGE(TAG, "Failed to set size of SPI FLASH memory emulation file %s: %s", s_esp_partition_file_mmap_ctrl_act.flash_file_name, strerror(errno));
  180. ret = ESP_ERR_INVALID_SIZE;
  181. break;
  182. }
  183. ESP_LOGV(TAG, "SPIFLASH memory emulation file created: %s (size: %d B)", s_esp_partition_file_mmap_ctrl_act.flash_file_name, s_esp_partition_file_mmap_ctrl_act.flash_file_size);
  184. // create memory-mapping for the flash holder file
  185. if ((s_spiflash_mem_file_buf = mmap(NULL, s_esp_partition_file_mmap_ctrl_act.flash_file_size, PROT_READ | PROT_WRITE, MAP_SHARED, s_spiflash_mem_file_fd, 0)) == MAP_FAILED) {
  186. ESP_LOGE(TAG, "Failed to mmap() SPI FLASH memory emulation file %s: %s", s_esp_partition_file_mmap_ctrl_act.flash_file_name, strerror(errno));
  187. ret = ESP_ERR_NO_MEM;
  188. break;
  189. }
  190. // initialize whole range with bit-1 (NOR FLASH default)
  191. memset(s_spiflash_mem_file_buf, 0xFF, s_esp_partition_file_mmap_ctrl_act.flash_file_size);
  192. // upload partition table to the mmap file at real offset as in SPIFLASH
  193. FILE *f_partition_table = fopen(s_esp_partition_file_mmap_ctrl_act.partition_file_name, "r+");
  194. if (f_partition_table == NULL) {
  195. ESP_LOGE(TAG, "Failed to open partition table file %s: %s", s_esp_partition_file_mmap_ctrl_act.partition_file_name, strerror(errno));
  196. ret = ESP_ERR_NOT_FOUND;
  197. break;
  198. }
  199. if (fseek(f_partition_table, 0L, SEEK_END) != 0) {
  200. ESP_LOGE(TAG, "Failed to seek in partition table file %s: %s", s_esp_partition_file_mmap_ctrl_act.partition_file_name, strerror(errno));
  201. ret = ESP_ERR_INVALID_SIZE;
  202. break;
  203. }
  204. int partition_table_file_size = ftell(f_partition_table);
  205. ESP_LOGV(TAG, "Using partition table file %s (size: %d B):", s_esp_partition_file_mmap_ctrl_act.partition_file_name, partition_table_file_size);
  206. // check whether partition table fits into the memory mapped file
  207. if (partition_table_file_size + ESP_PARTITION_TABLE_OFFSET > s_esp_partition_file_mmap_ctrl_act.flash_file_size) {
  208. ESP_LOGE(TAG, "Flash file: %s (size: %d B) cannot hold partition table requiring %d B",
  209. s_esp_partition_file_mmap_ctrl_act.flash_file_name,
  210. s_esp_partition_file_mmap_ctrl_act.flash_file_size,
  211. partition_table_file_size + ESP_PARTITION_TABLE_OFFSET);
  212. ret = ESP_ERR_INVALID_SIZE;
  213. break;
  214. }
  215. //copy partition table from the file to emulated SPIFLASH memory space
  216. if (fseek(f_partition_table, 0L, SEEK_SET) != 0) {
  217. ESP_LOGE(TAG, "Failed to seek in partition table file %s: %s", s_esp_partition_file_mmap_ctrl_act.partition_file_name, strerror(errno));
  218. ret = ESP_ERR_INVALID_SIZE;
  219. break;
  220. }
  221. uint8_t *part_table_in_spiflash = s_spiflash_mem_file_buf + ESP_PARTITION_TABLE_OFFSET;
  222. size_t res = fread(part_table_in_spiflash, 1, partition_table_file_size, f_partition_table);
  223. fclose(f_partition_table);
  224. if (res != partition_table_file_size) {
  225. ESP_LOGE(TAG, "Failed to read partition table file %s", s_esp_partition_file_mmap_ctrl_act.partition_file_name);
  226. ret = ESP_ERR_INVALID_STATE;
  227. break;
  228. }
  229. } while (false);
  230. }
  231. if (ret != ESP_OK) {
  232. if (close(s_spiflash_mem_file_fd)) {
  233. ESP_LOGE(TAG, "Failed to close() SPIFLASH memory emulation file: %s", strerror(errno));
  234. }
  235. s_spiflash_mem_file_fd = -1;
  236. return ret;
  237. }
  238. #ifdef CONFIG_LOG_DEFAULT_LEVEL_VERBOSE
  239. uint8_t *part_ptr = s_spiflash_mem_file_buf + ESP_PARTITION_TABLE_OFFSET;
  240. ESP_LOGV(TAG, "");
  241. ESP_LOGV(TAG, "Partition table sucessfully imported, partitions found:");
  242. while (true) {
  243. esp_partition_info_t *p_part_item = (esp_partition_info_t *)part_ptr;
  244. if (p_part_item->magic != ESP_PARTITION_MAGIC ) {
  245. break;
  246. }
  247. ESP_LOGV(TAG, " --------------");
  248. ESP_LOGV(TAG, " label: %s", p_part_item->label);
  249. ESP_LOGV(TAG, " type: %s", esp_partition_type_to_str(p_part_item->type));
  250. ESP_LOGV(TAG, " subtype: %s", esp_partition_subtype_to_str(p_part_item->type, p_part_item->subtype));
  251. ESP_LOGV(TAG, " offset: 0x%08X", p_part_item->pos.offset);
  252. ESP_LOGV(TAG, " size: %d", p_part_item->pos.size);
  253. ESP_LOGV(TAG, " flags: %d", p_part_item->flags);
  254. part_ptr += sizeof(esp_partition_info_t);
  255. }
  256. ESP_LOGV(TAG, "");
  257. #endif
  258. #ifdef CONFIG_ESP_PARTITION_ENABLE_STATS
  259. free(s_esp_partition_stat_sector_erase_count);
  260. s_esp_partition_stat_sector_erase_count = malloc(sizeof(size_t) * s_esp_partition_file_mmap_ctrl_act.flash_file_size / ESP_PARTITION_EMULATED_SECTOR_SIZE);
  261. #endif
  262. //return mmapped file starting address
  263. *part_desc_addr_start = s_spiflash_mem_file_buf;
  264. // clear input control structure
  265. memset(&s_esp_partition_file_mmap_ctrl_input, 0, sizeof(s_esp_partition_file_mmap_ctrl_input));
  266. return ESP_OK;
  267. }
  268. esp_err_t esp_partition_file_munmap(void)
  269. {
  270. if (s_spiflash_mem_file_buf == NULL) {
  271. return ESP_ERR_NO_MEM;
  272. }
  273. if (s_esp_partition_file_mmap_ctrl_act.flash_file_size == 0) {
  274. return ESP_ERR_INVALID_SIZE;
  275. }
  276. if (s_spiflash_mem_file_fd == -1) {
  277. return ESP_ERR_NOT_FOUND;
  278. }
  279. unload_partitions();
  280. #ifdef CONFIG_ESP_PARTITION_ENABLE_STATS
  281. free(s_esp_partition_stat_sector_erase_count);
  282. s_esp_partition_stat_sector_erase_count = NULL;
  283. #endif
  284. // unmap the flash emulation memory file
  285. if (munmap(s_spiflash_mem_file_buf, s_esp_partition_file_mmap_ctrl_act.flash_file_size) != 0) {
  286. ESP_LOGE(TAG, "Failed to munmap() SPIFLASH memory emulation file %s: %s", s_esp_partition_file_mmap_ctrl_act.flash_file_name, strerror(errno));
  287. return ESP_ERR_INVALID_RESPONSE;
  288. }
  289. // close memory mapped file
  290. if (close(s_spiflash_mem_file_fd)) {
  291. ESP_LOGE(TAG, "Failed to close() SPIFLASH memory emulation file %s: %s", s_esp_partition_file_mmap_ctrl_act.flash_file_name, strerror(errno));
  292. return ESP_ERR_INVALID_RESPONSE;
  293. }
  294. if (s_esp_partition_file_mmap_ctrl_input.remove_dump) {
  295. // delete spi flash file
  296. if (remove(s_esp_partition_file_mmap_ctrl_act.flash_file_name) != 0) {
  297. ESP_LOGE(TAG, "Failed to remove() SPI FLASH memory emulation file %s: %s", s_esp_partition_file_mmap_ctrl_act.flash_file_name, strerror(errno));
  298. return ESP_ERR_INVALID_RESPONSE;
  299. }
  300. }
  301. // cleanup
  302. memset(&s_esp_partition_file_mmap_ctrl_act, 0, sizeof(s_esp_partition_file_mmap_ctrl_act));
  303. s_spiflash_mem_file_buf = NULL;
  304. s_spiflash_mem_file_fd = -1;
  305. return ESP_OK;
  306. }
  307. esp_err_t esp_partition_write(const esp_partition_t *partition, size_t dst_offset, const void *src, size_t size)
  308. {
  309. assert(partition != NULL && s_spiflash_mem_file_buf != NULL);
  310. if (partition->encrypted) {
  311. return ESP_ERR_NOT_SUPPORTED;
  312. }
  313. if (dst_offset > partition->size) {
  314. return ESP_ERR_INVALID_ARG;
  315. }
  316. if (dst_offset + size > partition->size) {
  317. return ESP_ERR_INVALID_SIZE;
  318. }
  319. void *dst_addr = s_spiflash_mem_file_buf + partition->address + dst_offset;
  320. ESP_LOGV(TAG, "esp_partition_write(): partition=%s dst_offset=%zu src=%p size=%zu (real dst address: %p)", partition->label, dst_offset, src, size, dst_addr);
  321. // local size, can be modified by the write hook in case of simulated power-off
  322. size_t new_size = size;
  323. esp_err_t ret = ESP_OK;
  324. // hook gathers statistics and can emulate power-off
  325. // in case of power - off it decreases new_size to the number of bytes written
  326. // before power event occured
  327. if (!ESP_PARTITION_HOOK_WRITE(dst_addr, &new_size)) {
  328. ret = ESP_ERR_FLASH_OP_FAIL;
  329. }
  330. for (size_t x = 0; x < new_size; x++) {
  331. // Check if address to be written was erased first
  332. if((~((uint8_t *)dst_addr)[x] & ((uint8_t *)src)[x]) != 0) {
  333. ESP_LOGW(TAG, "invalid flash operation detected");
  334. ret = ESP_ERR_FLASH_OP_FAIL;
  335. break;
  336. }
  337. // AND with destination byte (to emulate real NOR FLASH behavior)
  338. ((uint8_t *)dst_addr)[x] &= ((uint8_t *)src)[x];
  339. }
  340. return ret;
  341. }
  342. esp_err_t esp_partition_read(const esp_partition_t *partition, size_t src_offset, void *dst, size_t size)
  343. {
  344. assert(partition != NULL && s_spiflash_mem_file_buf != NULL);
  345. if (partition->encrypted) {
  346. return ESP_ERR_NOT_SUPPORTED;
  347. }
  348. if (src_offset > partition->size) {
  349. return ESP_ERR_INVALID_ARG;
  350. }
  351. if (src_offset + size > partition->size) {
  352. return ESP_ERR_INVALID_SIZE;
  353. }
  354. void *src_addr = s_spiflash_mem_file_buf + partition->address + src_offset;
  355. ESP_LOGV(TAG, "esp_partition_read(): partition=%s src_offset=%zu dst=%p size=%zu (real src address: %p)", partition->label, src_offset, dst, size, src_addr);
  356. memcpy(dst, src_addr, size);
  357. ESP_PARTITION_HOOK_READ(src_addr, size); // statistics
  358. return ESP_OK;
  359. }
  360. esp_err_t esp_partition_read_raw(const esp_partition_t *partition, size_t src_offset, void *dst, size_t size)
  361. {
  362. ESP_LOGV(TAG, "esp_partition_read_raw(): calling esp_partition_read()");
  363. return esp_partition_read(partition, src_offset, dst, size);
  364. }
  365. esp_err_t esp_partition_write_raw(const esp_partition_t *partition, size_t dst_offset, const void *src, size_t size)
  366. {
  367. ESP_LOGV(TAG, "esp_partition_write_raw(): calling esp_partition_write()");
  368. return esp_partition_write(partition, dst_offset, src, size);
  369. }
  370. esp_err_t esp_partition_erase_range(const esp_partition_t *partition, size_t offset, size_t size)
  371. {
  372. assert(partition != NULL);
  373. if (offset > partition->size || offset % partition->erase_size != 0) {
  374. return ESP_ERR_INVALID_ARG;
  375. }
  376. if (offset + size > partition->size || size % partition->erase_size != 0) {
  377. return ESP_ERR_INVALID_SIZE;
  378. }
  379. void *target_addr = s_spiflash_mem_file_buf + partition->address + offset;
  380. ESP_LOGV(TAG, "esp_partition_erase_range(): partition=%s offset=%zu size=%zu (real target address: %p)", partition->label, offset, size, target_addr);
  381. // local size to be potentially updated by the hook in case of power-off event
  382. size_t new_size = size;
  383. // hook gathers statistics and can emulate power-off
  384. esp_err_t ret = ESP_OK;
  385. if(!ESP_PARTITION_HOOK_ERASE(target_addr, &new_size)) {
  386. ret = ESP_ERR_FLASH_OP_FAIL;
  387. }
  388. //set all bits to 1 (NOR FLASH default)
  389. memset(target_addr, 0xFF, new_size);
  390. return ret;
  391. }
  392. /*
  393. * Exposes direct pointer to the memory mapped file created by esp_partition_file_mmap
  394. * No address alignment is performed
  395. * Default handle is always returned
  396. * Returns:
  397. * ESP_ERR_INVALID_ARG - offset exceeds size of partition
  398. * ESP_ERR_INVALID_SIZE - address range defined by offset + size is beyond the size of partition
  399. * ESP_ERR_NOT_SUPPORTED - flash_chip of partition is not NULL
  400. * ESP_OK - calculated out parameters hold pointer to the requested memory area and default handle respectively
  401. */
  402. esp_err_t esp_partition_mmap(const esp_partition_t *partition, size_t offset, size_t size,
  403. esp_partition_mmap_memory_t memory,
  404. const void **out_ptr, esp_partition_mmap_handle_t *out_handle)
  405. {
  406. ESP_LOGV(TAG, "esp_partition_mmap(): partition=%s offset=%zu size=%zu", partition->label, offset, size);
  407. assert(partition != NULL);
  408. if (offset > partition->size) {
  409. return ESP_ERR_INVALID_ARG;
  410. }
  411. if (offset + size > partition->size) {
  412. return ESP_ERR_INVALID_SIZE;
  413. }
  414. if (partition->flash_chip != NULL) {
  415. return ESP_ERR_NOT_SUPPORTED;
  416. }
  417. // required starting address in flash aka offset from the flash beginning
  418. size_t req_flash_addr = (size_t)(partition->address) + offset;
  419. esp_err_t rc = ESP_OK;
  420. // check if memory mapped file is already present, if not, map it now
  421. if (s_spiflash_mem_file_buf == NULL) {
  422. uint8_t *part_desc_addr_start = NULL;
  423. rc = esp_partition_file_mmap((const uint8_t **) &part_desc_addr_start);
  424. }
  425. // adjust memory mapped pointer to the required offset
  426. if (rc == ESP_OK) {
  427. *out_ptr = (void *) (s_spiflash_mem_file_buf + req_flash_addr);
  428. *out_handle = s_default_partition_mmap_handle;
  429. } else {
  430. *out_ptr = NULL;
  431. *out_handle = 0;
  432. }
  433. return rc;
  434. }
  435. // Intentionally does nothing.
  436. void esp_partition_munmap(esp_partition_mmap_handle_t handle __attribute__((unused)))
  437. {
  438. }
  439. esp_partition_file_mmap_ctrl_t *esp_partition_get_file_mmap_ctrl_input(void)
  440. {
  441. return &s_esp_partition_file_mmap_ctrl_input;
  442. }
  443. esp_partition_file_mmap_ctrl_t *esp_partition_get_file_mmap_ctrl_act(void)
  444. {
  445. return &s_esp_partition_file_mmap_ctrl_act;
  446. }
  447. #ifdef CONFIG_ESP_PARTITION_ENABLE_STATS
  448. // timing data for ESP8266, 160MHz CPU frequency, 80MHz flash requency
  449. // all values in microseconds
  450. // values are for block sizes starting at 4 bytes and going up to 4096 bytes
  451. static size_t s_esp_partition_stat_read_times[] = {7, 5, 6, 7, 11, 18, 32, 60, 118, 231, 459};
  452. static size_t s_esp_partition_stat_write_times[] = {19, 23, 35, 57, 106, 205, 417, 814, 1622, 3200, 6367};
  453. static size_t s_esp_partition_stat_block_erase_time = 37142;
  454. static size_t esp_partition_stat_time_interpolate(uint32_t bytes, size_t *lut)
  455. {
  456. const int lut_size = sizeof(s_esp_partition_stat_read_times) / sizeof(s_esp_partition_stat_read_times[0]);
  457. int lz = __builtin_clz(bytes / 4);
  458. int log_size = 32 - lz;
  459. size_t x2 = 1 << (log_size + 2);
  460. size_t upper_index = (log_size < lut_size - 1) ? log_size : lut_size - 1;
  461. size_t y2 = lut[upper_index];
  462. size_t x1 = 1 << (log_size + 1);
  463. size_t y1 = lut[log_size - 1];
  464. return (bytes - x1) * (y2 - y1) / (x2 - x1) + y1;
  465. }
  466. // Registers read access statistics of emulated SPI FLASH device (Linux host)
  467. // Function increases nmuber of read operations, accumulates number of read bytes
  468. // and accumulates emulated read operation time (size dependent)
  469. static void esp_partition_hook_read(const void *srcAddr, const size_t size)
  470. {
  471. ESP_LOGV(TAG, "esp_partition_hook_read()");
  472. // stats
  473. ++s_esp_partition_stat_read_ops;
  474. s_esp_partition_stat_read_bytes += size;
  475. s_esp_partition_stat_total_time += esp_partition_stat_time_interpolate((uint32_t) size, s_esp_partition_stat_read_times);
  476. }
  477. // Registers write access statistics of emulated SPI FLASH device (Linux host)
  478. // If enabled by the esp_partition_fail_after, function emulates power-off event during write operations by
  479. // decrementing the s_esp_partition_emulated_power_off_counter for each 4 bytes written
  480. // If zero threshold is reached, false is returned. In this case the size parameter contains number of successfully written bytes
  481. // Else the function increases nmuber of write operations, accumulates number
  482. // of bytes written and accumulates emulated write operation time (size dependent) and returns true.
  483. static bool esp_partition_hook_write(const void *dstAddr, size_t *size)
  484. {
  485. ESP_LOGV(TAG, "%s", __FUNCTION__);
  486. bool ret_val = true;
  487. // one power down cycle per 4 bytes written
  488. size_t write_cycles = *size / 4;
  489. // check whether power off simulation is active for write
  490. if (s_esp_partition_emulated_power_off_counter != SIZE_MAX &&
  491. ESP_PARTITION_FAIL_AFTER_MODE_WRITE) {
  492. // check if power down happens during this call
  493. if (s_esp_partition_emulated_power_off_counter > write_cycles) {
  494. // OK
  495. s_esp_partition_emulated_power_off_counter -= write_cycles;
  496. } else {
  497. // failure in this call
  498. // update number of bytes written to the in/out parameter
  499. *size = s_esp_partition_emulated_power_off_counter * 4;
  500. // disable power on cycles for further calls
  501. s_esp_partition_emulated_power_off_counter = SIZE_MAX;
  502. // final result value will be false
  503. ret_val = false;
  504. }
  505. }
  506. if(ret_val) {
  507. // stats
  508. ++s_esp_partition_stat_write_ops;
  509. s_esp_partition_stat_write_bytes += write_cycles * 4;
  510. s_esp_partition_stat_total_time += esp_partition_stat_time_interpolate((uint32_t) (*size), s_esp_partition_stat_write_times);
  511. }
  512. return ret_val;
  513. }
  514. // Registers erase access statistics of emulated SPI FLASH device (Linux host)
  515. // If enabled by 'esp_partition_fail_after' parameter, the function emulates a power-off event during erase
  516. // operation by decrementing the s_esp_partition_emulated_power_off_counterpower for each erased virtual sector.
  517. // If zero threshold is reached, false is returned. In out parameter size is updated with number of bytes erased until power-off
  518. // Else, for statistics purpose, the impacted virtual sectors are identified based on
  519. // ESP_PARTITION_EMULATED_SECTOR_SIZE and their respective counts of erase operations are incremented
  520. // Total number of erase operations is increased by the number of impacted virtual sectors
  521. static bool esp_partition_hook_erase(const void *dstAddr, size_t *size)
  522. {
  523. ESP_LOGV(TAG, "%s", __FUNCTION__);
  524. if (*size == 0) {
  525. return true;
  526. }
  527. // cycle over virtual sectors
  528. ptrdiff_t offset = dstAddr - s_spiflash_mem_file_buf;
  529. size_t first_sector_idx = offset / ESP_PARTITION_EMULATED_SECTOR_SIZE;
  530. size_t last_sector_idx = (offset + *size - 1) / ESP_PARTITION_EMULATED_SECTOR_SIZE;
  531. size_t sector_count = 1 + last_sector_idx - first_sector_idx;
  532. bool ret_val = true;
  533. // check whether power off simulation is active for erase
  534. if (s_esp_partition_emulated_power_off_counter != SIZE_MAX &&
  535. ESP_PARTITION_FAIL_AFTER_MODE_ERASE) {
  536. // check if power down happens during this call
  537. if (s_esp_partition_emulated_power_off_counter > sector_count) {
  538. // OK
  539. s_esp_partition_emulated_power_off_counter -= sector_count;
  540. } else {
  541. // failure in this call - reduce sector_count to the number of remaining power on cycles
  542. sector_count = s_esp_partition_emulated_power_off_counter;
  543. // disable power on cycles for further calls
  544. s_esp_partition_emulated_power_off_counter = SIZE_MAX;
  545. // update number of bytes to be really erased before power-off event
  546. *size = sector_count * ESP_PARTITION_EMULATED_SECTOR_SIZE;
  547. // final result value will be false
  548. ret_val = false;
  549. }
  550. }
  551. // update statistcs for all sectors until power down cycle
  552. for (size_t sector_index = first_sector_idx; sector_index < first_sector_idx + sector_count; sector_index++) {
  553. ++s_esp_partition_stat_erase_ops;
  554. s_esp_partition_stat_sector_erase_count[sector_index]++;
  555. s_esp_partition_stat_total_time += s_esp_partition_stat_block_erase_time;
  556. }
  557. return ret_val;
  558. }
  559. void esp_partition_clear_stats(void)
  560. {
  561. s_esp_partition_stat_read_bytes = 0;
  562. s_esp_partition_stat_write_bytes = 0;
  563. s_esp_partition_stat_erase_ops = 0;
  564. s_esp_partition_stat_read_ops = 0;
  565. s_esp_partition_stat_write_ops = 0;
  566. s_esp_partition_stat_total_time = 0;
  567. memset(s_esp_partition_stat_sector_erase_count, 0, sizeof(size_t) * s_esp_partition_file_mmap_ctrl_act.flash_file_size / ESP_PARTITION_EMULATED_SECTOR_SIZE);
  568. }
  569. size_t esp_partition_get_read_ops(void)
  570. {
  571. return s_esp_partition_stat_read_ops;
  572. }
  573. size_t esp_partition_get_write_ops(void)
  574. {
  575. return s_esp_partition_stat_write_ops;
  576. }
  577. size_t esp_partition_get_erase_ops(void)
  578. {
  579. return s_esp_partition_stat_erase_ops;
  580. }
  581. size_t esp_partition_get_read_bytes(void)
  582. {
  583. return s_esp_partition_stat_read_bytes;
  584. }
  585. size_t esp_partition_get_write_bytes(void)
  586. {
  587. return s_esp_partition_stat_write_bytes;
  588. }
  589. size_t esp_partition_get_total_time(void)
  590. {
  591. return s_esp_partition_stat_total_time;
  592. }
  593. void esp_partition_fail_after(size_t count, uint8_t mode)
  594. {
  595. s_esp_partition_emulated_power_off_counter = count;
  596. s_esp_partition_emulated_power_off_mode = mode;
  597. }
  598. size_t esp_partition_get_sector_erase_count(size_t sector)
  599. {
  600. return s_esp_partition_stat_sector_erase_count[sector];
  601. }
  602. #endif