partition_linux.c 30 KB

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