esp_flash_partitions.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. /* Pre-partition table fixed flash offsets */
  37. #define ESP_BOOTLOADER_DIGEST_OFFSET 0x0
  38. #define ESP_BOOTLOADER_OFFSET 0x1000 /* Offset of bootloader image. Has matching value in bootloader KConfig.projbuild file. */
  39. #define ESP_PARTITION_TABLE_OFFSET CONFIG_PARTITION_TABLE_OFFSET /* Offset of partition table. Backwards-compatible name.*/
  40. #define ESP_PARTITION_TABLE_MAX_LEN 0xC00 /* Maximum length of partition table data */
  41. #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 */
  42. /// OTA_DATA states for checking operability of the app.
  43. typedef enum {
  44. ESP_OTA_IMG_NEW = 0x0U, /*!< Monitor the first boot. In bootloader this state is changed to ESP_OTA_IMG_PENDING_VERIFY. */
  45. 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. */
  46. ESP_OTA_IMG_VALID = 0x2U, /*!< App was confirmed as workable. App can boot and work without limits. */
  47. ESP_OTA_IMG_INVALID = 0x3U, /*!< App was confirmed as non-workable. This app will not selected to boot at all. */
  48. 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. */
  49. ESP_OTA_IMG_UNDEFINED = 0xFFFFFFFFU, /*!< Undefined. App can boot and work without limits. */
  50. } esp_ota_img_states_t;
  51. /* OTA selection structure (two copies in the OTA data partition.)
  52. Size of 32 bytes is friendly to flash encryption */
  53. typedef struct {
  54. uint32_t ota_seq;
  55. uint8_t seq_label[20];
  56. uint32_t ota_state;
  57. uint32_t crc; /* CRC32 of ota_seq field only */
  58. } esp_ota_select_entry_t;
  59. typedef struct {
  60. uint32_t offset;
  61. uint32_t size;
  62. } esp_partition_pos_t;
  63. /* Structure which describes the layout of partition table entry.
  64. * See docs/partition_tables.rst for more information about individual fields.
  65. */
  66. typedef struct {
  67. uint16_t magic;
  68. uint8_t type;
  69. uint8_t subtype;
  70. esp_partition_pos_t pos;
  71. uint8_t label[16];
  72. uint32_t flags;
  73. } esp_partition_info_t;
  74. /* @brief Verify the partition table
  75. *
  76. * @param partition_table Pointer to at least ESP_PARTITION_TABLE_MAX_ENTRIES of potential partition table data. (ESP_PARTITION_TABLE_MAX_LEN bytes.)
  77. * @param log_errors Log errors if the partition table is invalid.
  78. * @param num_partitions If result is ESP_OK, num_partitions is updated with total number of partitions (not including terminating entry).
  79. *
  80. * @return ESP_OK on success, ESP_ERR_INVALID_STATE if partition table is not valid.
  81. */
  82. esp_err_t esp_partition_table_verify(const esp_partition_info_t *partition_table, bool log_errors, int *num_partitions);
  83. /**
  84. * Check whether the region on the main flash is safe to write.
  85. *
  86. * @param addr Start address of the region
  87. * @param size Size of the region
  88. *
  89. * @return true if the region is safe to write, otherwise false.
  90. */
  91. bool esp_partition_main_flash_region_safe(size_t addr, size_t size);
  92. #ifdef __cplusplus
  93. }
  94. #endif