esp_flash_partitions.h 4.6 KB

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