partition_linux.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 "esp_err.h"
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /**
  14. * @file partition_linux.h
  15. *
  16. * @brief Private API functions used for Linux-target emulation of the Partition APIs (host-side testing)
  17. */
  18. /** @brief emulated sector size for the partition API on Linux */
  19. #define ESP_PARTITION_EMULATED_SECTOR_SIZE 0x1000
  20. /** @brief emulated whole flash size for the partition API on Linux */
  21. #define ESP_PARTITION_EMULATED_FLASH_SIZE 0x400000 //4MB fixed
  22. /**
  23. * @brief Partition type to string conversion routine
  24. *
  25. * @param type Partition type, see esp_partition_type_t
  26. *
  27. * @return string equivalent of given partition type or "unknown" on mismatch
  28. */
  29. const char *esp_partition_type_to_str(const uint32_t type);
  30. /**
  31. * @brief Partition subtype to string conversion routine
  32. *
  33. * @param type Partition type, see esp_partition_type_t
  34. * @param subtype Partition subtype, see esp_partition_subtype_t
  35. *
  36. * @return string equivalent of given partition subtype or "unknown" on mismatch
  37. */
  38. const char *esp_partition_subtype_to_str(const uint32_t type, const uint32_t subtype);
  39. /**
  40. * @brief Creates memory emulation of SPI FLASH device (Linux host)
  41. *
  42. * The function creates a memory buffer to emulate SPI FLASH device and provides a pointer to its beginning, in order
  43. * to allow relevant Partition APIs run in host-emulated environment without any code change.
  44. *
  45. * The emulation buffer is actually a disk file mapped to the host memory, current version implements the following:
  46. * 1. create temporary file /tmp/idf-partition-XXXXXX (fixed size 4MB)
  47. * 2. mmap() whole file to the memory and set its contents to all 1s (SPI NOR flash default)
  48. * 3. upload build/partition_table/partition-table.bin (hard-wired path for now) to ESP_PARTITION_TABLE_OFFSET
  49. * (from the beginning of the memory buffer, ie to the same offset as in real SPI FLASH)
  50. * 4. [optional: iterate through the partitions uploaded and print esp_partition_info_t details for each]
  51. * 5. set part_desc_addr_start[out] parameter to the memory buffer starting address
  52. *
  53. * The pointer returned in part_desc_addr_start is then used as it was regular SPI FLASH address.
  54. *
  55. * NOTES:
  56. * 1. the temporary file generated is not deleted automatically - the cleanup happens during the next host system reset
  57. * 2. the mmapped() section remains active until esp_partition_file_unmmap() is called
  58. * 3. mmap() is called with MAP_SHARED so the emulated SPI FLASH can be shared among processes
  59. *
  60. * @param[out] part_desc_addr_start output pointer to receive memory SPI FLASH buffer address
  61. *
  62. * @return
  63. * - ESP_OK: Operation successful
  64. * - ESP_ERR_NOT_FINISHED: Failed to generate temporary file
  65. * - ESP_ERR_INVALID_SIZE (one of the following):
  66. * - Failed to resize temporary file to required value
  67. * - Failed to set filepointer in partition-table.bin
  68. * - ESP_ERR_NO_MEM: Failed to mmap() the temporary file into the memory
  69. * - ESP_ERR_NOT_FOUND: Couldn't open the partition_table.bin file
  70. * - ESP_ERR_INVALID_STATE: Failed to upload partition_table into the memory
  71. */
  72. esp_err_t esp_partition_file_mmap(const uint8_t **part_desc_addr_start);
  73. /**
  74. * @brief Releases the memory of emulated SPI FLASH device (Linux host)
  75. *
  76. * The function releases the memory block previously allocated by esp_partition_file_mmap().
  77. * The buffer is freed by calling munmap() with emulated_buffer, buffer_size
  78. *
  79. * @return
  80. * - ESP_OK: Operation successful
  81. * - ESP_ERR_NO_MEM: The memory buffer was not allocated
  82. * - ESP_ERR_INVALID_SIZE: The buffer size was 0
  83. * - ESP_ERR_INVALID_RESPONSE: Failed to munmap() the emulation file from memory
  84. */
  85. esp_err_t esp_partition_file_munmap(void);
  86. /**
  87. * Functions for host tests
  88. */
  89. /**
  90. * @brief Clears statistics gathered by emulated partition read/write/erase operations
  91. *
  92. */
  93. void esp_partition_clear_stats(void);
  94. /**
  95. * @brief Returns number of read operations called
  96. *
  97. * Function returns number of calls to the function esp_partition_read
  98. *
  99. * @return
  100. * - number of calls to esp_partition_read since recent esp_partition_clear_stats
  101. */
  102. size_t esp_partition_get_read_ops(void);
  103. /**
  104. * @brief Returns number of write operations called
  105. *
  106. * Function returns number of calls to the function esp_partition_write
  107. *
  108. * @return
  109. * - number of calls to esp_partition_write since recent esp_partition_clear_stats
  110. */
  111. size_t esp_partition_get_write_ops(void);
  112. /**
  113. * @brief Returns number of erase operations performed on behalf of calls to esp_partition_erase_range
  114. *
  115. * Function returns accumulated number of sectors erased on behalf of esp_partition_erase_range
  116. *
  117. * @return
  118. * - total number of emulated sector erase operations on behalf of esp_partition_erase_range since recent esp_partition_clear_stats
  119. */
  120. size_t esp_partition_get_erase_ops(void);
  121. /**
  122. * @brief Returns total number of bytes read on behalf of esp_partition_read
  123. *
  124. * Function returns number of bytes read by esp_partition_read
  125. *
  126. * @return
  127. * - total number of bytes read on behalf of esp_partition_read since recent esp_partition_clear_stats
  128. */
  129. size_t esp_partition_get_read_bytes(void);
  130. /**
  131. * @brief Returns total number of bytes written on behalf of esp_partition_write
  132. *
  133. * Function returns number of bytes written by esp_partition_write
  134. *
  135. * @return
  136. * - total number of bytes written on behalf of esp_partition_write since recent esp_partition_clear_stats
  137. */
  138. size_t esp_partition_get_write_bytes(void);
  139. /**
  140. * @brief Returns estimated total time spent on partition operations.
  141. *
  142. * Function returns estimated total time spent in esp_partition_read,
  143. * esp_partition_write and esp_partition_erase_range operations.
  144. *
  145. * @return
  146. * - estimated total time spent in read/write/erase operations in miliseconds
  147. */
  148. size_t esp_partition_get_total_time(void);
  149. /**
  150. * @brief Initializes emulation of failure caused by wear on behalf of write/erase operations
  151. *
  152. * Function initializes down counter emulating remaining write / erase cycles.
  153. * Once this counter reaches 0, emulation of all subsequent write / erase operations fails
  154. * Initial state of down counter is disabled.
  155. *
  156. * @param[in] count Number of remaining write / erase cycles before failure. Call with SIZE_MAX to disable simulation of flash wear.
  157. *
  158. */
  159. void esp_partition_fail_after(size_t count);
  160. /**
  161. * @brief Returns count of erase operations performed on virtual emulated sector
  162. *
  163. * Function returns number of erase operatinos performed on virtual sector specified by the parameter sector.
  164. * The esp_parttion mapped address space is virtually split into sectors of the size ESP_PARTITION_EMULATED_SECTOR_SIZE.
  165. * Calls to the esp_partition_erase_range are impacting one or multiple virtual sectors, for each of them, the respective
  166. * count is incremented.
  167. *
  168. * @param[in] sector Virtual sector number to return erase count for
  169. *
  170. * @return
  171. * - count of erase operations performed on virtual emulated sector
  172. *
  173. */
  174. size_t esp_partition_get_sector_erase_count(size_t sector);
  175. #ifdef __cplusplus
  176. }
  177. #endif