partition_linux.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. #include <limits.h>
  10. #include "esp_err.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /**
  15. * @file partition_linux.h
  16. *
  17. * @brief Private API functions used for Linux-target emulation of the Partition APIs (host-side testing)
  18. */
  19. /** @brief emulated sector size for the partition API on Linux */
  20. #define ESP_PARTITION_EMULATED_SECTOR_SIZE 0x1000
  21. /** @brief emulated whole flash size for the partition API on Linux */
  22. #define ESP_PARTITION_DEFAULT_EMULATED_FLASH_SIZE 0x400000 //4MB fixed
  23. /** @brief mode of fail after */
  24. #define ESP_PARTITION_FAIL_AFTER_MODE_ERASE 0x01
  25. #define ESP_PARTITION_FAIL_AFTER_MODE_WRITE 0x02
  26. #define ESP_PARTITION_FAIL_AFTER_MODE_BOTH 0x03
  27. /**
  28. * @brief Partition type to string conversion routine
  29. *
  30. * @param type Partition type, see esp_partition_type_t
  31. *
  32. * @return string equivalent of given partition type or "unknown" on mismatch
  33. */
  34. const char *esp_partition_type_to_str(const uint32_t type);
  35. /**
  36. * @brief Partition subtype to string conversion routine
  37. *
  38. * @param type Partition type, see esp_partition_type_t
  39. * @param subtype Partition subtype, see esp_partition_subtype_t
  40. *
  41. * @return string equivalent of given partition subtype or "unknown" on mismatch
  42. */
  43. const char *esp_partition_subtype_to_str(const uint32_t type, const uint32_t subtype);
  44. /**
  45. * @brief Creates memory emulation of SPI FLASH device (Linux host)
  46. *
  47. * The function creates a memory buffer to emulate SPI FLASH device and provides a pointer to its beginning, in order
  48. * to allow relevant Partition APIs run in host-emulated environment without any code change.
  49. *
  50. * The emulation buffer is actually a disk file mapped to the host memory, current version implements the following:
  51. * 1. create temporary file /tmp/idf-partition-XXXXXX (fixed size 4MB)
  52. * 2. mmap() whole file to the memory and set its contents to all 1s (SPI NOR flash default)
  53. * 3. upload build/partition_table/partition-table.bin (hard-wired path for now) to ESP_PARTITION_TABLE_OFFSET
  54. * (from the beginning of the memory buffer, ie to the same offset as in real SPI FLASH)
  55. * 4. [optional: iterate through the partitions uploaded and print esp_partition_info_t details for each]
  56. * 5. set part_desc_addr_start[out] parameter to the memory buffer starting address
  57. *
  58. * The pointer returned in part_desc_addr_start is then used as it was regular SPI FLASH address.
  59. *
  60. * NOTES:
  61. * 1. the temporary file generated is not deleted automatically - the cleanup happens during the next host system reset
  62. * 2. the mmapped() section remains active until esp_partition_file_unmmap() is called
  63. * 3. mmap() is called with MAP_SHARED so the emulated SPI FLASH can be shared among processes
  64. *
  65. * @param[out] part_desc_addr_start output pointer to receive memory SPI FLASH buffer address
  66. *
  67. * @return
  68. * - ESP_OK: Operation successful
  69. * - ESP_ERR_NOT_FINISHED: Failed to generate temporary file
  70. * - ESP_ERR_INVALID_SIZE (one of the following):
  71. * - Failed to resize temporary file to required value
  72. * - Failed to set filepointer in partition-table.bin
  73. * - ESP_ERR_NO_MEM: Failed to mmap() the temporary file into the memory
  74. * - ESP_ERR_NOT_FOUND: Couldn't open the partition_table.bin file
  75. * - ESP_ERR_INVALID_STATE: Failed to upload partition_table into the memory
  76. */
  77. esp_err_t esp_partition_file_mmap(const uint8_t **part_desc_addr_start);
  78. /**
  79. * @brief Releases the memory of emulated SPI FLASH device (Linux host)
  80. *
  81. * The function releases the memory block previously allocated by esp_partition_file_mmap().
  82. * The buffer is freed by calling munmap() with emulated_buffer, buffer_size
  83. *
  84. * @return
  85. * - ESP_OK: Operation successful
  86. * - ESP_ERR_NO_MEM: The memory buffer was not allocated
  87. * - ESP_ERR_INVALID_SIZE: The buffer size was 0
  88. * - ESP_ERR_INVALID_RESPONSE: Failed to munmap() the emulation file from memory
  89. */
  90. esp_err_t esp_partition_file_munmap(void);
  91. /**
  92. * Functions for host tests
  93. */
  94. /**
  95. * @brief Clears statistics gathered by emulated partition read/write/erase operations
  96. *
  97. */
  98. void esp_partition_clear_stats(void);
  99. /**
  100. * @brief Returns number of read operations called
  101. *
  102. * Function returns number of calls to the function esp_partition_read
  103. *
  104. * @return
  105. * - number of calls to esp_partition_read since recent esp_partition_clear_stats
  106. */
  107. size_t esp_partition_get_read_ops(void);
  108. /**
  109. * @brief Returns number of write operations called
  110. *
  111. * Function returns number of calls to the function esp_partition_write
  112. *
  113. * @return
  114. * - number of calls to esp_partition_write since recent esp_partition_clear_stats
  115. */
  116. size_t esp_partition_get_write_ops(void);
  117. /**
  118. * @brief Returns number of erase operations performed on behalf of calls to esp_partition_erase_range
  119. *
  120. * Function returns accumulated number of sectors erased on behalf of esp_partition_erase_range
  121. *
  122. * @return
  123. * - total number of emulated sector erase operations on behalf of esp_partition_erase_range since recent esp_partition_clear_stats
  124. */
  125. size_t esp_partition_get_erase_ops(void);
  126. /**
  127. * @brief Returns total number of bytes read on behalf of esp_partition_read
  128. *
  129. * Function returns number of bytes read by esp_partition_read
  130. *
  131. * @return
  132. * - total number of bytes read on behalf of esp_partition_read since recent esp_partition_clear_stats
  133. */
  134. size_t esp_partition_get_read_bytes(void);
  135. /**
  136. * @brief Returns total number of bytes written on behalf of esp_partition_write
  137. *
  138. * Function returns number of bytes written by esp_partition_write
  139. *
  140. * @return
  141. * - total number of bytes written on behalf of esp_partition_write since recent esp_partition_clear_stats
  142. */
  143. size_t esp_partition_get_write_bytes(void);
  144. /**
  145. * @brief Returns estimated total time spent on partition operations.
  146. *
  147. * Function returns estimated total time spent in esp_partition_read,
  148. * esp_partition_write and esp_partition_erase_range operations.
  149. *
  150. * @return
  151. * - estimated total time spent in read/write/erase operations in miliseconds
  152. */
  153. size_t esp_partition_get_total_time(void);
  154. /**
  155. * @brief Initializes emulation of lost power failure in write/erase operations
  156. *
  157. * Function initializes down counter emulating power off failure during write / erase operations.
  158. * Once this counter reaches 0, actual as well as all subsequent write / erase operations fail
  159. * Initial state of down counter is disabled.
  160. *
  161. * @param[in] count Number of remaining write / erase cycles before emulated failure. Call with SIZE_MAX to disable failure emulation.
  162. * @param[in] mode Controls whether remaining cycles are applied to erase, write or both operations
  163. *
  164. */
  165. void esp_partition_fail_after(size_t count, uint8_t mode);
  166. /**
  167. * @brief Returns count of erase operations performed on virtual emulated sector
  168. *
  169. * Function returns number of erase operations performed on virtual sector specified by the parameter sector.
  170. * The esp_parttion mapped address space is virtually split into sectors of the size ESP_PARTITION_EMULATED_SECTOR_SIZE.
  171. * Calls to the esp_partition_erase_range are impacting one or multiple virtual sectors, for each of them, the respective
  172. * count is incremented.
  173. *
  174. * @param[in] sector Virtual sector number to return erase count for
  175. *
  176. * @return
  177. * - count of erase operations performed on virtual emulated sector
  178. *
  179. */
  180. size_t esp_partition_get_sector_erase_count(size_t sector);
  181. typedef struct {
  182. char flash_file_name[PATH_MAX]; /*!< name of flash dump file, zero-terminated ASCII string */
  183. size_t flash_file_size; /*!< size of flash dump file in bytes */
  184. char partition_file_name[PATH_MAX]; /*!< name of file containing binary representation of partition table, zero-terminated ASCII string */
  185. bool remove_dump; /*!< flag is set to true if dump file has to be removed after esp_partition_file_munmap */
  186. } esp_partition_file_mmap_ctrl_t;
  187. /**
  188. * @brief Returns pointer to the structure controlling mapping of flash file
  189. *
  190. * Function returns pointer to structure used by esp_partition_file_mmap and esp_partition_file_munmap
  191. * Caller can change this structure members prior calls involving the above functions to
  192. * Specify existing flash file which will represent the content of flash memory after mapping
  193. * Specify size and partition file name used to create empty flash memory
  194. * Control whether the actual flash file will be deleted of kept after call to esp_partition_file_munmap
  195. *
  196. * @return
  197. * - pointer to flash file mapping control structure
  198. *
  199. */
  200. esp_partition_file_mmap_ctrl_t* esp_partition_get_file_mmap_ctrl_input(void);
  201. /**
  202. * @brief Returns pointer to the structure reflecting actual settings of flash file emulation
  203. *
  204. * Function returns pointer to structure containing:
  205. * flash file name representing emulated flash memory
  206. * size of file representing emulated flash memory
  207. * file name holding binary used to initialize partition table (if it was used)
  208. *
  209. * @return
  210. * - pointer to flash file mapping actuall values in control structure
  211. *
  212. */
  213. esp_partition_file_mmap_ctrl_t* esp_partition_get_file_mmap_ctrl_act(void);
  214. // private function in partition.c to unload partitions and free space allocated by them
  215. void unload_partitions(void);
  216. #ifdef __cplusplus
  217. }
  218. #endif