esp_flash_partitions.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include "esp_err.h"
  8. #include "esp_types.h"
  9. #include "sdkconfig.h"
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. #define ESP_PARTITION_MAGIC 0x50AA
  14. #define ESP_PARTITION_MAGIC_MD5 0xEBEB
  15. #define PART_TYPE_APP 0x00
  16. #define PART_SUBTYPE_FACTORY 0x00
  17. #define PART_SUBTYPE_OTA_FLAG 0x10
  18. #define PART_SUBTYPE_OTA_MASK 0x0f
  19. #define PART_SUBTYPE_TEST 0x20
  20. #define PART_TYPE_DATA 0x01
  21. #define PART_SUBTYPE_DATA_OTA 0x00
  22. #define PART_SUBTYPE_DATA_RF 0x01
  23. #define PART_SUBTYPE_DATA_WIFI 0x02
  24. #define PART_SUBTYPE_DATA_NVS_KEYS 0x04
  25. #define PART_SUBTYPE_DATA_EFUSE_EM 0x05
  26. #define PART_TYPE_END 0xff
  27. #define PART_SUBTYPE_END 0xff
  28. #define PART_FLAG_ENCRYPTED (1<<0)
  29. /* The md5sum value is found this many bytes after the ESP_PARTITION_MAGIC_MD5 offset */
  30. #define ESP_PARTITION_MD5_OFFSET 16
  31. /* Pre-partition table fixed flash offsets */
  32. #define ESP_BOOTLOADER_DIGEST_OFFSET 0x0
  33. #define ESP_BOOTLOADER_OFFSET CONFIG_BOOTLOADER_OFFSET_IN_FLASH /* Offset of bootloader image. Has matching value in bootloader KConfig.projbuild file. */
  34. #define ESP_PARTITION_TABLE_OFFSET CONFIG_PARTITION_TABLE_OFFSET /* Offset of partition table. Backwards-compatible name.*/
  35. #define ESP_PARTITION_TABLE_MAX_LEN 0xC00 /* Maximum length of partition table data */
  36. #define ESP_PARTITION_TABLE_MAX_ENTRIES (ESP_PARTITION_TABLE_MAX_LEN / sizeof(esp_partition_info_t)) /* Maximum length of partition table data, including terminating entry */
  37. /// OTA_DATA states for checking operability of the app.
  38. typedef enum {
  39. ESP_OTA_IMG_NEW = 0x0U, /*!< Monitor the first boot. In bootloader this state is changed to ESP_OTA_IMG_PENDING_VERIFY. */
  40. ESP_OTA_IMG_PENDING_VERIFY = 0x1U, /*!< First boot for this app was. If while the second boot this state is then it will be changed to ABORTED. */
  41. ESP_OTA_IMG_VALID = 0x2U, /*!< App was confirmed as workable. App can boot and work without limits. */
  42. ESP_OTA_IMG_INVALID = 0x3U, /*!< App was confirmed as non-workable. This app will not selected to boot at all. */
  43. ESP_OTA_IMG_ABORTED = 0x4U, /*!< App could not confirm the workable or non-workable. In bootloader IMG_PENDING_VERIFY state will be changed to IMG_ABORTED. This app will not selected to boot at all. */
  44. ESP_OTA_IMG_UNDEFINED = 0xFFFFFFFFU, /*!< Undefined. App can boot and work without limits. */
  45. } esp_ota_img_states_t;
  46. /* OTA selection structure (two copies in the OTA data partition.)
  47. Size of 32 bytes is friendly to flash encryption */
  48. typedef struct {
  49. uint32_t ota_seq;
  50. uint8_t seq_label[20];
  51. uint32_t ota_state;
  52. uint32_t crc; /* CRC32 of ota_seq field only */
  53. } esp_ota_select_entry_t;
  54. typedef struct {
  55. uint32_t offset;
  56. uint32_t size;
  57. } esp_partition_pos_t;
  58. /* Structure which describes the layout of partition table entry.
  59. * See docs/partition_tables.rst for more information about individual fields.
  60. */
  61. typedef struct {
  62. uint16_t magic;
  63. uint8_t type;
  64. uint8_t subtype;
  65. esp_partition_pos_t pos;
  66. uint8_t label[16];
  67. uint32_t flags;
  68. } esp_partition_info_t;
  69. /* @brief Verify the partition table
  70. *
  71. * @param partition_table Pointer to at least ESP_PARTITION_TABLE_MAX_ENTRIES of potential partition table data. (ESP_PARTITION_TABLE_MAX_LEN bytes.)
  72. * @param log_errors Log errors if the partition table is invalid.
  73. * @param num_partitions If result is ESP_OK, num_partitions is updated with total number of partitions (not including terminating entry).
  74. *
  75. * @return ESP_OK on success, ESP_ERR_INVALID_STATE if partition table is not valid.
  76. */
  77. esp_err_t esp_partition_table_verify(const esp_partition_info_t *partition_table, bool log_errors, int *num_partitions);
  78. /**
  79. * Check whether the region on the main flash is safe to write.
  80. *
  81. * @param addr Start address of the region
  82. * @param size Size of the region
  83. *
  84. * @return true if the region is safe to write, otherwise false.
  85. */
  86. bool esp_partition_main_flash_region_safe(size_t addr, size_t size);
  87. #ifdef __cplusplus
  88. }
  89. #endif