partition API: separate type and subtype into two enums

This commit is contained in:
Ivan Grokhotkov 2016-10-27 00:48:10 +08:00
parent 9f0f05d520
commit c581229e1d
2 changed files with 62 additions and 70 deletions

View file

@ -26,51 +26,52 @@ extern "C" {
#endif
typedef enum {
ESP_PARTITION_APP_MASK = 0x0000,
ESP_PARTITION_APP_FACTORY = ESP_PARTITION_APP_MASK | 0x00,
ESP_PARTITION_APP_OTA_MIN = ESP_PARTITION_APP_MASK | 0x10,
ESP_PARTITION_APP_OTA_0 = ESP_PARTITION_APP_OTA_MIN + 0,
ESP_PARTITION_APP_OTA_1 = ESP_PARTITION_APP_OTA_MIN + 1,
ESP_PARTITION_APP_OTA_2 = ESP_PARTITION_APP_OTA_MIN + 2,
ESP_PARTITION_APP_OTA_3 = ESP_PARTITION_APP_OTA_MIN + 3,
ESP_PARTITION_APP_OTA_4 = ESP_PARTITION_APP_OTA_MIN + 4,
ESP_PARTITION_APP_OTA_5 = ESP_PARTITION_APP_OTA_MIN + 5,
ESP_PARTITION_APP_OTA_6 = ESP_PARTITION_APP_OTA_MIN + 6,
ESP_PARTITION_APP_OTA_7 = ESP_PARTITION_APP_OTA_MIN + 7,
ESP_PARTITION_APP_OTA_8 = ESP_PARTITION_APP_OTA_MIN + 8,
ESP_PARTITION_APP_OTA_9 = ESP_PARTITION_APP_OTA_MIN + 9,
ESP_PARTITION_APP_OTA_10 = ESP_PARTITION_APP_OTA_MIN + 10,
ESP_PARTITION_APP_OTA_11 = ESP_PARTITION_APP_OTA_MIN + 11,
ESP_PARTITION_APP_OTA_12 = ESP_PARTITION_APP_OTA_MIN + 12,
ESP_PARTITION_APP_OTA_13 = ESP_PARTITION_APP_OTA_MIN + 13,
ESP_PARTITION_APP_OTA_14 = ESP_PARTITION_APP_OTA_MIN + 14,
ESP_PARTITION_APP_OTA_15 = ESP_PARTITION_APP_OTA_MIN + 15,
ESP_PARTITION_APP_OTA_MAX = ESP_PARTITION_APP_MASK | 0x1f,
ESP_PARTITION_APP_TEST = ESP_PARTITION_APP_MASK | 0x20,
ESP_PARTITION_APP_ANY = ESP_PARTITION_APP_MASK | 0xff,
ESP_PARTITION_DATA_MASK = 0x0100,
ESP_PARTITION_DATA_OTA = ESP_PARTITION_DATA_MASK | 0x00,
ESP_PARTITION_DATA_RF = ESP_PARTITION_DATA_MASK | 0x01,
ESP_PARTITION_DATA_WIFI = ESP_PARTITION_DATA_MASK | 0x02,
ESP_PARTITION_DATA_ANY = ESP_PARTITION_DATA_MASK | 0xff,
ESP_PARTITION_FILESYSTEM_MASK = 0x0200,
ESP_PARTITION_FILESYSTEM_ESPHTTPD = 0x0200,
ESP_PARTITION_FILESYSTEM_FAT = 0x0201,
ESP_PARTITION_FILESYSTEM_SPIFFS = 0x0202,
ESP_PARTITION_FILESYSTEM_ANY = 0x20ff,
ESP_PARTITION_END = 0xffff
ESP_PARTITION_TYPE_APP = 0x00,
ESP_PARTITION_TYPE_DATA = 0x01,
ESP_PARTITION_TYPE_FILESYSTEM = 0x02,
} esp_partition_type_t;
#define ESP_PARTITION_APP_OTA(i) ((esp_partition_type_t)(ESP_PARTITION_APP_OTA_MIN + ((i) & 0xf)))
typedef enum {
ESP_PARTITION_SUBTYPE_APP_FACTORY = 0x00,
ESP_PARTITION_SUBTYPE_APP_OTA_MIN = 0x10,
ESP_PARTITION_SUBTYPE_APP_OTA_0 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 0,
ESP_PARTITION_SUBTYPE_APP_OTA_1 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 1,
ESP_PARTITION_SUBTYPE_APP_OTA_2 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 2,
ESP_PARTITION_SUBTYPE_APP_OTA_3 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 3,
ESP_PARTITION_SUBTYPE_APP_OTA_4 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 4,
ESP_PARTITION_SUBTYPE_APP_OTA_5 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 5,
ESP_PARTITION_SUBTYPE_APP_OTA_6 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 6,
ESP_PARTITION_SUBTYPE_APP_OTA_7 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 7,
ESP_PARTITION_SUBTYPE_APP_OTA_8 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 8,
ESP_PARTITION_SUBTYPE_APP_OTA_9 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 9,
ESP_PARTITION_SUBTYPE_APP_OTA_10 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 10,
ESP_PARTITION_SUBTYPE_APP_OTA_11 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 11,
ESP_PARTITION_SUBTYPE_APP_OTA_12 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 12,
ESP_PARTITION_SUBTYPE_APP_OTA_13 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 13,
ESP_PARTITION_SUBTYPE_APP_OTA_14 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 14,
ESP_PARTITION_SUBTYPE_APP_OTA_15 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 15,
ESP_PARTITION_SUBTYPE_APP_OTA_MAX = 15,
ESP_PARTITION_SUBTYPE_APP_TEST = 0x20,
ESP_PARTITION_SUBTYPE_DATA_OTA = 0x00,
ESP_PARTITION_SUBTYPE_DATA_RF = 0x01,
ESP_PARTITION_SUBTYPE_DATA_NVS = 0x02,
ESP_PARTITION_SUBTYPE_FILESYSTEM_ESPHTTPD = 0x00,
ESP_PARTITION_SUBTYPE_FILESYSTEM_FAT = 0x01,
ESP_PARTITION_SUBTYPE_FILESYSTEM_SPIFFS = 0x02,
ESP_PARTITION_SUBTYPE_ANY = 0xff,
} esp_partition_subtype_t;
#define ESP_PARTITION_SUBTYPE_OTA(i) ((esp_partition_subtype_t)(ESP_PARTITION_SUBTYPE_APP_OTA_MIN + ((i) & 0xf)))
typedef struct esp_partition_iterator_opaque_* esp_partition_iterator_t;
typedef struct {
esp_partition_type_t type;
esp_partition_subtype_t subtype;
uint32_t address;
uint32_t size;
char label[17];
@ -81,9 +82,9 @@ typedef struct {
* @brief Find partition based on one or more parameters
*
* @param type Partition type, one of esp_partition_type_t values
* To find all app partitions or all filesystem partitions,
* use ESP_PARTITION_APP_ANY or ESP_PARTITION_FILESYSTEM_ANY,
* respectively.
* @param subtype Partition subtype, of esp_partition_subtype_t values.
* To find all partitions of given type, use
* ESP_PARTITION_SUBTYPE_ANY.
* @param label (optional) Partition label. Set this value if looking
* for partition with a specific name. Pass NULL otherwise.
*
@ -92,22 +93,22 @@ typedef struct {
* Iterator obtained through this function has to be released
* using esp_partition_iterator_release when not used any more.
*/
esp_partition_iterator_t esp_partition_find(esp_partition_type_t type, const char* label);
esp_partition_iterator_t esp_partition_find(esp_partition_type_t type, esp_partition_subtype_t subtype, const char* label);
/**
* @brief Find first partition based on one or more parameters
*
* @param type Partition type, one of esp_partition_type_t values
* To find all app partitions or all filesystem partitions,
* use ESP_PARTITION_APP_ANY or ESP_PARTITION_FILESYSTEM_ANY,
* respectively.
* @param subtype Partition subtype, of esp_partition_subtype_t values.
* To find all partitions of given type, use
* ESP_PARTITION_SUBTYPE_ANY.
* @param label (optional) Partition label. Set this value if looking
* for partition with a specific name. Pass NULL otherwise.
*
* @return pointer to esp_partition_t structure, or NULL if no parition is found.
* This pointer is valid for the lifetime of the application.
*/
const esp_partition_t* esp_partition_find_first(esp_partition_type_t type, const char* label);
const esp_partition_t* esp_partition_find_first(esp_partition_type_t type, esp_partition_subtype_t subtype, const char* label);
/**
* @brief Get esp_partition_t structure for given partition

View file

@ -39,13 +39,14 @@ typedef struct partition_list_item_ {
typedef struct esp_partition_iterator_opaque_ {
esp_partition_type_t type; // requested type
esp_partition_subtype_t subtype; // requested subtype
const char* label; // requested label (can be NULL)
partition_list_item_t* next_item; // next item to iterate to
esp_partition_t* info; // pointer to info (it is redundant, but makes code more readable)
} esp_partition_iterator_opaque_t;
static esp_partition_iterator_opaque_t* iterator_create(esp_partition_type_t type, const char* label);
static esp_partition_iterator_opaque_t* iterator_create(esp_partition_type_t type, esp_partition_subtype_t subtype, const char* label);
static esp_err_t load_partitions();
@ -54,18 +55,8 @@ static SLIST_HEAD(partition_list_head_, partition_list_item_) s_partition_list =
static _lock_t s_partition_list_lock;
static uint32_t get_major_type(esp_partition_type_t type)
{
return (type >> 8) & 0xff;
}
static uint32_t get_minor_type(esp_partition_type_t type)
{
return type & 0xff;
}
esp_partition_iterator_t esp_partition_find(esp_partition_type_t type,
const char* label)
esp_partition_subtype_t subtype, const char* label)
{
if (SLIST_EMPTY(&s_partition_list)) {
// only lock if list is empty (and check again after acquiring lock)
@ -81,7 +72,7 @@ esp_partition_iterator_t esp_partition_find(esp_partition_type_t type,
}
// create an iterator pointing to the start of the list
// (next item will be the first one)
esp_partition_iterator_t it = iterator_create(type, label);
esp_partition_iterator_t it = iterator_create(type, subtype, label);
// advance iterator to the next item which matches constraints
it = esp_partition_next(it);
// if nothing found, it == NULL and iterator has been released
@ -95,17 +86,13 @@ esp_partition_iterator_t esp_partition_next(esp_partition_iterator_t it)
if (it->next_item == NULL) {
return NULL;
}
uint32_t requested_major_type = get_major_type(it->type);
uint32_t requested_minor_type = get_minor_type(it->type);
_lock_acquire(&s_partition_list_lock);
for (; it->next_item != NULL; it->next_item = SLIST_NEXT(it->next_item, next)) {
esp_partition_t* p = &it->next_item->info;
uint32_t it_major_type = get_major_type(p->type);
uint32_t it_minor_type = get_minor_type(p->type);
if (requested_major_type != it_major_type) {
if (it->type != p->type) {
continue;
}
if (requested_minor_type != 0xff && requested_minor_type != it_minor_type) {
if (it->subtype != 0xff && it->subtype != p->subtype) {
continue;
}
if (it->label != NULL && strcmp(it->label, p->label) != 0) {
@ -124,9 +111,10 @@ esp_partition_iterator_t esp_partition_next(esp_partition_iterator_t it)
return it;
}
const esp_partition_t* esp_partition_find_first(esp_partition_type_t type, const char* label)
const esp_partition_t* esp_partition_find_first(esp_partition_type_t type,
esp_partition_subtype_t subtype, const char* label)
{
esp_partition_iterator_t it = esp_partition_find(type, label);
esp_partition_iterator_t it = esp_partition_find(type, subtype, label);
if (it == NULL) {
return NULL;
}
@ -135,11 +123,13 @@ const esp_partition_t* esp_partition_find_first(esp_partition_type_t type, const
return res;
}
static esp_partition_iterator_opaque_t* iterator_create(esp_partition_type_t type, const char* label)
static esp_partition_iterator_opaque_t* iterator_create(esp_partition_type_t type,
esp_partition_subtype_t subtype, const char* label)
{
esp_partition_iterator_opaque_t* it =
(esp_partition_iterator_opaque_t*) malloc(sizeof(esp_partition_iterator_opaque_t));
it->type = type;
it->subtype = subtype;
it->label = label;
it->next_item = SLIST_FIRST(&s_partition_list);
it->info = NULL;
@ -172,7 +162,8 @@ static esp_err_t load_partitions()
partition_list_item_t* item = (partition_list_item_t*) malloc(sizeof(partition_list_item_t));
item->info.address = it->pos.offset;
item->info.size = it->pos.size;
item->info.type = (it->type << 8) | it->subtype;
item->info.type = it->type;
item->info.subtype = it->subtype;
item->info.encrypted = false;
// it->label may not be zero-terminated
strncpy(item->info.label, (const char*) it->label, sizeof(it->label));
@ -201,7 +192,7 @@ const esp_partition_t* esp_partition_get(esp_partition_iterator_t iterator)
}
esp_err_t esp_partition_read(const esp_partition_t* partition,
size_t src_offset, uint8_t* dst, size_t size)
size_t src_offset, void* dst, size_t size)
{
assert(partition != NULL);
if (src_offset > partition->size) {
@ -214,7 +205,7 @@ esp_err_t esp_partition_read(const esp_partition_t* partition,
}
esp_err_t esp_partition_write(const esp_partition_t* partition,
size_t dst_offset, const uint8_t* src, size_t size)
size_t dst_offset, const void* src, size_t size)
{
assert(partition != NULL);
if (dst_offset > partition->size) {