partition_linux.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 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 <sys/mman.h>
  10. #include <sys/stat.h>
  11. #include <unistd.h>
  12. #include <limits.h>
  13. #include <errno.h>
  14. #include "sdkconfig.h"
  15. #include "esp_partition.h"
  16. #include "esp_flash_partitions.h"
  17. #include "esp_private/partition_linux.h"
  18. #include "esp_log.h"
  19. static const char *TAG = "linux_spiflash";
  20. static void *s_spiflash_mem_file_buf = NULL;
  21. static const esp_partition_mmap_handle_t s_default_partition_mmap_handle = 0;
  22. #ifdef CONFIG_ESP_PARTITION_ENABLE_STATS
  23. // variables holding stats and controlling wear emulation
  24. size_t s_esp_partition_stat_read_ops = 0;
  25. size_t s_esp_partition_stat_write_ops = 0;
  26. size_t s_esp_partition_stat_read_bytes = 0;
  27. size_t s_esp_partition_stat_write_bytes = 0;
  28. size_t s_esp_partition_stat_erase_ops = 0;
  29. size_t s_esp_partition_stat_total_time = 0;
  30. size_t s_esp_partition_emulated_flash_life = SIZE_MAX;
  31. // tracking erase count individually for each emulated sector
  32. size_t s_esp_partition_stat_sector_erase_count[ESP_PARTITION_EMULATED_FLASH_SIZE / ESP_PARTITION_EMULATED_SECTOR_SIZE] = {0};
  33. // forward declaration of hooks
  34. static void esp_partition_hook_read(const void *srcAddr, const size_t size);
  35. static bool esp_partition_hook_write(const void *dstAddr, const size_t size);
  36. static bool esp_partition_hook_erase(const void *dstAddr, const size_t size);
  37. // redirect hooks to functions
  38. #define ESP_PARTITION_HOOK_READ(srcAddr, size) esp_partition_hook_read(srcAddr, size)
  39. #define ESP_PARTITION_HOOK_WRITE(dstAddr, size) esp_partition_hook_write(dstAddr, size)
  40. #define ESP_PARTITION_HOOK_ERASE(dstAddr, size) esp_partition_hook_erase(dstAddr, size)
  41. #else
  42. // redirect hooks to "do nothing code"
  43. #define ESP_PARTITION_HOOK_READ(srcAddr, size)
  44. #define ESP_PARTITION_HOOK_WRITE(dstAddr, size) true
  45. #define ESP_PARTITION_HOOK_ERASE(dstAddr, size) true
  46. #endif
  47. const char *esp_partition_type_to_str(const uint32_t type)
  48. {
  49. switch (type) {
  50. case PART_TYPE_APP: return "app";
  51. case PART_TYPE_DATA: return "data";
  52. default: return "unknown";
  53. }
  54. }
  55. const char *esp_partition_subtype_to_str(const uint32_t type, const uint32_t subtype)
  56. {
  57. switch (type) {
  58. case PART_TYPE_APP:
  59. switch (subtype) {
  60. case PART_SUBTYPE_FACTORY: return "factory";
  61. case PART_SUBTYPE_OTA_FLAG: return "ota_flag";
  62. case PART_SUBTYPE_OTA_MASK: return "ota_mask";
  63. case PART_SUBTYPE_TEST: return "test";
  64. default: return "unknown";
  65. }
  66. case PART_TYPE_DATA:
  67. switch (subtype) {
  68. case PART_SUBTYPE_DATA_OTA: return "data_ota";
  69. case PART_SUBTYPE_DATA_RF: return "data_rf";
  70. case PART_SUBTYPE_DATA_WIFI: return "data_wifi";
  71. case PART_SUBTYPE_DATA_NVS_KEYS: return "nvs_keys";
  72. case PART_SUBTYPE_DATA_EFUSE_EM: return "efuse_em";
  73. default: return "unknown";
  74. }
  75. default: return "unknown";
  76. }
  77. }
  78. esp_err_t esp_partition_file_mmap(const uint8_t **part_desc_addr_start)
  79. {
  80. //create temporary file to hold complete SPIFLASH size
  81. char temp_spiflash_mem_file_name[PATH_MAX] = {"/tmp/idf-partition-XXXXXX"};
  82. int spiflash_mem_file_fd = mkstemp(temp_spiflash_mem_file_name);
  83. if (spiflash_mem_file_fd == -1) {
  84. ESP_LOGE(TAG, "Failed to create SPI FLASH emulation file %s: %s", temp_spiflash_mem_file_name, strerror(errno));
  85. return ESP_ERR_NOT_FINISHED;
  86. }
  87. if (ftruncate(spiflash_mem_file_fd, ESP_PARTITION_EMULATED_FLASH_SIZE) != 0) {
  88. ESP_LOGE(TAG, "Failed to set size of SPI FLASH memory emulation file %s: %s", temp_spiflash_mem_file_name, strerror(errno));
  89. return ESP_ERR_INVALID_SIZE;
  90. }
  91. ESP_LOGV(TAG, "SPIFLASH memory emulation file created: %s (size: %d B)", temp_spiflash_mem_file_name, ESP_PARTITION_EMULATED_FLASH_SIZE);
  92. //create memory-mapping for the partitions holder file
  93. if ((s_spiflash_mem_file_buf = mmap(NULL, ESP_PARTITION_EMULATED_FLASH_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, spiflash_mem_file_fd, 0)) == MAP_FAILED) {
  94. ESP_LOGE(TAG, "Failed to mmap() SPI FLASH memory emulation file: %s", strerror(errno));
  95. return ESP_ERR_NO_MEM;
  96. }
  97. //initialize whole range with bit-1 (NOR FLASH default)
  98. memset(s_spiflash_mem_file_buf, 0xFF, ESP_PARTITION_EMULATED_FLASH_SIZE);
  99. //upload partition table to the mmap file at real offset as in SPIFLASH
  100. const char *partition_table_file_name = "build/partition_table/partition-table.bin";
  101. FILE *f_partition_table = fopen(partition_table_file_name, "r+");
  102. if (f_partition_table == NULL) {
  103. ESP_LOGE(TAG, "Failed to open partition table file %s: %s", partition_table_file_name, strerror(errno));
  104. return ESP_ERR_NOT_FOUND;
  105. }
  106. if (fseek(f_partition_table, 0L, SEEK_END) != 0) {
  107. ESP_LOGE(TAG, "Failed to seek in partition table file %s: %s", partition_table_file_name, strerror(errno));
  108. return ESP_ERR_INVALID_SIZE;
  109. }
  110. int partition_table_file_size = ftell(f_partition_table);
  111. ESP_LOGV(TAG, "Using partition table file %s (size: %d B):", partition_table_file_name, partition_table_file_size);
  112. uint8_t *part_table_in_spiflash = s_spiflash_mem_file_buf + ESP_PARTITION_TABLE_OFFSET;
  113. //copy partition table from the file to emulated SPIFLASH memory space
  114. if (fseek(f_partition_table, 0L, SEEK_SET) != 0) {
  115. ESP_LOGE(TAG, "Failed to seek in partition table file %s: %s", partition_table_file_name, strerror(errno));
  116. return ESP_ERR_INVALID_SIZE;
  117. }
  118. size_t res = fread(part_table_in_spiflash, 1, partition_table_file_size, f_partition_table);
  119. fclose(f_partition_table);
  120. if (res != partition_table_file_size) {
  121. ESP_LOGE(TAG, "Failed to read partition table file %s", partition_table_file_name);
  122. return ESP_ERR_INVALID_STATE;
  123. }
  124. #ifdef CONFIG_LOG_DEFAULT_LEVEL_VERBOSE
  125. uint8_t *part_ptr = part_table_in_spiflash;
  126. uint8_t *part_end_ptr = part_table_in_spiflash + partition_table_file_size;
  127. ESP_LOGV(TAG, "");
  128. ESP_LOGV(TAG, "Partition table sucessfully imported, partitions found:");
  129. while (part_ptr < part_end_ptr) {
  130. esp_partition_info_t *p_part_item = (esp_partition_info_t *)part_ptr;
  131. if (p_part_item->magic != ESP_PARTITION_MAGIC ) {
  132. break;
  133. }
  134. ESP_LOGV(TAG, " --------------");
  135. ESP_LOGV(TAG, " label: %s", p_part_item->label);
  136. ESP_LOGV(TAG, " type: %s", esp_partition_type_to_str(p_part_item->type));
  137. ESP_LOGV(TAG, " subtype: %s", esp_partition_subtype_to_str(p_part_item->type, p_part_item->subtype));
  138. ESP_LOGV(TAG, " offset: 0x%08X", p_part_item->pos.offset);
  139. ESP_LOGV(TAG, " size: %d", p_part_item->pos.size);
  140. ESP_LOGV(TAG, " flags: %d", p_part_item->flags);
  141. part_ptr += sizeof(esp_partition_info_t);
  142. }
  143. ESP_LOGV(TAG, "");
  144. #endif
  145. //return mmapped file starting address
  146. *part_desc_addr_start = s_spiflash_mem_file_buf;
  147. return ESP_OK;
  148. }
  149. esp_err_t esp_partition_file_munmap(void)
  150. {
  151. if (s_spiflash_mem_file_buf == NULL) {
  152. return ESP_ERR_NO_MEM;
  153. }
  154. if (ESP_PARTITION_EMULATED_FLASH_SIZE == 0) {
  155. return ESP_ERR_INVALID_SIZE;
  156. }
  157. if (munmap(s_spiflash_mem_file_buf, ESP_PARTITION_EMULATED_FLASH_SIZE) != 0) {
  158. ESP_LOGE(TAG, "Failed to munmap() SPI FLASH memory emulation file: %s", strerror(errno));
  159. return ESP_ERR_INVALID_RESPONSE;
  160. }
  161. s_spiflash_mem_file_buf = NULL;
  162. return ESP_OK;
  163. }
  164. esp_err_t esp_partition_write(const esp_partition_t *partition, size_t dst_offset, const void *src, size_t size)
  165. {
  166. assert(partition != NULL);
  167. if (partition->encrypted) {
  168. return ESP_ERR_NOT_SUPPORTED;
  169. }
  170. if (dst_offset > partition->size) {
  171. return ESP_ERR_INVALID_ARG;
  172. }
  173. if (dst_offset + size > partition->size) {
  174. return ESP_ERR_INVALID_SIZE;
  175. }
  176. uint8_t *write_buf = malloc(size);
  177. if (write_buf == NULL) {
  178. return ESP_ERR_NO_MEM;
  179. }
  180. void *dst_addr = s_spiflash_mem_file_buf + partition->address + dst_offset;
  181. 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);
  182. // hook gathers statistics and can emulate limited number of write cycles
  183. if (!ESP_PARTITION_HOOK_WRITE(dst_addr, size)) {
  184. return ESP_FAIL;
  185. }
  186. //read the contents first, AND with the write buffer (to emulate real NOR FLASH behavior)
  187. memcpy(write_buf, dst_addr, size);
  188. for (size_t x = 0; x < size; x++) {
  189. write_buf[x] &= ((uint8_t *)src)[x];
  190. }
  191. memcpy(dst_addr, write_buf, size);
  192. free(write_buf);
  193. return ESP_OK;
  194. }
  195. esp_err_t esp_partition_read(const esp_partition_t *partition, size_t src_offset, void *dst, size_t size)
  196. {
  197. assert(partition != NULL);
  198. if (partition->encrypted) {
  199. return ESP_ERR_NOT_SUPPORTED;
  200. }
  201. if (src_offset > partition->size) {
  202. return ESP_ERR_INVALID_ARG;
  203. }
  204. if (src_offset + size > partition->size) {
  205. return ESP_ERR_INVALID_SIZE;
  206. }
  207. void *src_addr = s_spiflash_mem_file_buf + partition->address + src_offset;
  208. 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);
  209. memcpy(dst, src_addr, size);
  210. ESP_PARTITION_HOOK_READ(src_addr, size); // statistics
  211. return ESP_OK;
  212. }
  213. esp_err_t esp_partition_read_raw(const esp_partition_t *partition, size_t src_offset, void *dst, size_t size)
  214. {
  215. ESP_LOGV(TAG, "esp_partition_read_raw(): calling esp_partition_read()");
  216. return esp_partition_read(partition, src_offset, dst, size);
  217. }
  218. esp_err_t esp_partition_write_raw(const esp_partition_t *partition, size_t dst_offset, const void *src, size_t size)
  219. {
  220. ESP_LOGV(TAG, "esp_partition_write_raw(): calling esp_partition_write()");
  221. return esp_partition_write(partition, dst_offset, src, size);
  222. }
  223. esp_err_t esp_partition_erase_range(const esp_partition_t *partition, size_t offset, size_t size)
  224. {
  225. assert(partition != NULL);
  226. if (offset > partition->size || offset % partition->erase_size != 0) {
  227. return ESP_ERR_INVALID_ARG;
  228. }
  229. if (offset + size > partition->size || size % partition->erase_size != 0) {
  230. return ESP_ERR_INVALID_SIZE;
  231. }
  232. void *target_addr = s_spiflash_mem_file_buf + partition->address + offset;
  233. ESP_LOGV(TAG, "esp_partition_erase_range(): partition=%s offset=%zu size=%zu (real target address: %p)", partition->label, offset, size, target_addr);
  234. // hook gathers statistics and can emulate limited number of write/erase cycles
  235. if (!ESP_PARTITION_HOOK_ERASE(target_addr, size)) {
  236. return ESP_FAIL;
  237. }
  238. //set all bits to 1 (NOR FLASH default)
  239. memset(target_addr, 0xFF, size);
  240. return ESP_OK;
  241. }
  242. /*
  243. * Exposes direct pointer to the memory mapped file created by esp_partition_file_mmap
  244. * No address alignment is performed
  245. * Default handle is always returned
  246. * Returns:
  247. * ESP_ERR_INVALID_ARG - offset exceeds size of partition
  248. * ESP_ERR_INVALID_SIZE - address range defined by offset + size is beyond the size of partition
  249. * ESP_ERR_NOT_SUPPORTED - flash_chip of partition is not NULL
  250. * ESP_OK - calculated out parameters hold pointer to the requested memory area and default handle respectively
  251. */
  252. esp_err_t esp_partition_mmap(const esp_partition_t *partition, size_t offset, size_t size,
  253. esp_partition_mmap_memory_t memory,
  254. const void **out_ptr, esp_partition_mmap_handle_t *out_handle)
  255. {
  256. ESP_LOGV(TAG, "esp_partition_mmap(): partition=%s offset=%zu size=%zu", partition->label, offset, size);
  257. assert(partition != NULL);
  258. if (offset > partition->size) {
  259. return ESP_ERR_INVALID_ARG;
  260. }
  261. if (offset + size > partition->size) {
  262. return ESP_ERR_INVALID_SIZE;
  263. }
  264. if (partition->flash_chip != NULL) {
  265. return ESP_ERR_NOT_SUPPORTED;
  266. }
  267. // required starting address in flash aka offset from the flash beginning
  268. size_t req_flash_addr = (size_t)(partition->address) + offset;
  269. esp_err_t rc = ESP_OK;
  270. // check if memory mapped file is already present, if not, map it now
  271. if (s_spiflash_mem_file_buf == NULL) {
  272. ESP_LOGE(TAG, "esp_partition_mmap(): in esp_partition_file_mmap");
  273. uint8_t *part_desc_addr_start = NULL;
  274. rc = esp_partition_file_mmap((const uint8_t **) &part_desc_addr_start);
  275. }
  276. // adjust memory mapped pointer to the required offset
  277. if (rc == ESP_OK) {
  278. *out_ptr = (void *) (s_spiflash_mem_file_buf + req_flash_addr);
  279. *out_handle = s_default_partition_mmap_handle;
  280. } else {
  281. *out_ptr = (void *) NULL;
  282. *out_handle = 0;
  283. }
  284. return rc;
  285. }
  286. // Intentionally does nothing.
  287. void esp_partition_munmap(esp_partition_mmap_handle_t handle)
  288. {
  289. ;
  290. }
  291. #ifdef CONFIG_ESP_PARTITION_ENABLE_STATS
  292. // timing data for ESP8266, 160MHz CPU frequency, 80MHz flash requency
  293. // all values in microseconds
  294. // values are for block sizes starting at 4 bytes and going up to 4096 bytes
  295. static size_t s_esp_partition_stat_read_times[] = {7, 5, 6, 7, 11, 18, 32, 60, 118, 231, 459};
  296. static size_t s_esp_partition_stat_write_times[] = {19, 23, 35, 57, 106, 205, 417, 814, 1622, 3200, 6367};
  297. static size_t s_esp_partition_stat_block_erase_time = 37142;
  298. static size_t esp_partition_stat_time_interpolate(uint32_t bytes, size_t *lut)
  299. {
  300. const int lut_size = sizeof(s_esp_partition_stat_read_times) / sizeof(s_esp_partition_stat_read_times[0]);
  301. int lz = __builtin_clz(bytes / 4);
  302. int log_size = 32 - lz;
  303. size_t x2 = 1 << (log_size + 2);
  304. size_t upper_index = (log_size < lut_size - 1) ? log_size : lut_size - 1;
  305. size_t y2 = lut[upper_index];
  306. size_t x1 = 1 << (log_size + 1);
  307. size_t y1 = lut[log_size - 1];
  308. return (bytes - x1) * (y2 - y1) / (x2 - x1) + y1;
  309. }
  310. // Registers read access statistics of emulated SPI FLASH device (Linux host)
  311. // Ffunction increases nmuber of read operations, accumulates number of read bytes
  312. // and accumulates emulated read operation time (size dependent)
  313. static void esp_partition_hook_read(const void *srcAddr, const size_t size)
  314. {
  315. ESP_LOGV(TAG, "esp_partition_hook_read()");
  316. // stats
  317. ++s_esp_partition_stat_read_ops;
  318. s_esp_partition_stat_read_bytes += size;
  319. s_esp_partition_stat_total_time += esp_partition_stat_time_interpolate((uint32_t) size, s_esp_partition_stat_read_times);
  320. }
  321. // Registers write access statistics of emulated SPI FLASH device (Linux host)
  322. // If enabled by the esp_partition_fail_after, function emulates physical limitation of write/erase operations by
  323. // decrementing the s_esp_partition_emulated_life for each 4 bytes written
  324. // If zero threshold is reached, false is returned.
  325. // Else the function increases nmuber of write operations, accumulates number
  326. // of bytes written and accumulates emulated write operation time (size dependent) and returns true.
  327. static bool esp_partition_hook_write(const void *dstAddr, const size_t size)
  328. {
  329. ESP_LOGV(TAG, "esp_partition_hook_write()");
  330. // wear emulation
  331. for (size_t i = 0; i < size / 4; ++i) {
  332. if (s_esp_partition_emulated_flash_life != SIZE_MAX && s_esp_partition_emulated_flash_life-- == 0) {
  333. return false;
  334. }
  335. }
  336. // stats
  337. ++s_esp_partition_stat_write_ops;
  338. s_esp_partition_stat_write_bytes += size;
  339. s_esp_partition_stat_total_time += esp_partition_stat_time_interpolate((uint32_t) size, s_esp_partition_stat_write_times);
  340. return true;
  341. }
  342. // Registers erase access statistics of emulated SPI FLASH device (Linux host)
  343. // If enabled by the esp_partition_fail_after, function emulates physical limitation of write/erase operations by
  344. // decrementing the s_esp_partition_emulated_life for each erased virtual sector.
  345. // If zero threshold is reached, false is returned.
  346. // Else, for statistics purpose, the impacted virtual sectors are identified based on
  347. // ESP_PARTITION_EMULATED_SECTOR_SIZE and their respective counts of erase operations are incremented
  348. // Total number of erase operations is increased by the number of impacted virtual sectors
  349. static bool esp_partition_hook_erase(const void *dstAddr, const size_t size)
  350. {
  351. ESP_LOGV(TAG, "esp_partition_hook_erase()");
  352. if (size == 0) {
  353. return true;
  354. }
  355. // cycle over virtual sectors
  356. ptrdiff_t offset = dstAddr - s_spiflash_mem_file_buf;
  357. size_t first_sector_idx = offset / ESP_PARTITION_EMULATED_SECTOR_SIZE;
  358. size_t last_sector_idx = (offset + size - 1) / ESP_PARTITION_EMULATED_SECTOR_SIZE;
  359. size_t sector_count = 1 + last_sector_idx - first_sector_idx;
  360. for (size_t sector_index = first_sector_idx; sector_index < first_sector_idx + sector_count; sector_index++) {
  361. // wear emulation
  362. if (s_esp_partition_emulated_flash_life != SIZE_MAX && s_esp_partition_emulated_flash_life-- == 0) {
  363. return false;
  364. }
  365. // stats
  366. ++s_esp_partition_stat_erase_ops;
  367. s_esp_partition_stat_sector_erase_count[sector_index]++;
  368. s_esp_partition_stat_total_time += s_esp_partition_stat_block_erase_time;
  369. }
  370. return true;
  371. }
  372. void esp_partition_clear_stats(void)
  373. {
  374. s_esp_partition_stat_read_bytes = 0;
  375. s_esp_partition_stat_write_bytes = 0;
  376. s_esp_partition_stat_erase_ops = 0;
  377. s_esp_partition_stat_read_ops = 0;
  378. s_esp_partition_stat_write_ops = 0;
  379. s_esp_partition_stat_total_time = 0;
  380. memset(s_esp_partition_stat_sector_erase_count, 0, sizeof(s_esp_partition_stat_sector_erase_count));
  381. }
  382. size_t esp_partition_get_read_ops(void)
  383. {
  384. return s_esp_partition_stat_read_ops;
  385. }
  386. size_t esp_partition_get_write_ops(void)
  387. {
  388. return s_esp_partition_stat_write_ops;
  389. }
  390. size_t esp_partition_get_erase_ops(void)
  391. {
  392. return s_esp_partition_stat_erase_ops;
  393. }
  394. size_t esp_partition_get_read_bytes(void)
  395. {
  396. return s_esp_partition_stat_read_bytes;
  397. }
  398. size_t esp_partition_get_write_bytes(void)
  399. {
  400. return s_esp_partition_stat_write_bytes;
  401. }
  402. size_t esp_partition_get_total_time(void)
  403. {
  404. return s_esp_partition_stat_total_time;
  405. }
  406. void esp_partition_fail_after(size_t count)
  407. {
  408. s_esp_partition_emulated_flash_life = count;
  409. }
  410. size_t esp_partition_get_sector_erase_count(size_t sector)
  411. {
  412. return s_esp_partition_stat_sector_erase_count[sector];
  413. }
  414. #endif