From ccde8c7d2f02abcb25dc17a053f5db36b95e03cc Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 30 Sep 2019 16:11:09 +0200 Subject: [PATCH] spi_flash: load partition table before adding an external partition esp_partition_register_external did not call load_partitions, so if it was called before any call to esp_partition_find, then the main partition table would never be loaded. Introduce new function, ensure_partitions_loaded, and call it both from esp_partition_find and esp_partition_register_external. Closes https://github.com/espressif/esp-idf/issues/4116 --- components/spi_flash/partition.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/components/spi_flash/partition.c b/components/spi_flash/partition.c index 14de32c9f..4457d6336 100644 --- a/components/spi_flash/partition.c +++ b/components/spi_flash/partition.c @@ -55,27 +55,38 @@ typedef struct esp_partition_iterator_opaque_ { 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(void); +static esp_err_t ensure_partitions_loaded(void); +static const char* TAG = "partition"; static SLIST_HEAD(partition_list_head_, partition_list_item_) s_partition_list = SLIST_HEAD_INITIALIZER(s_partition_list); static _lock_t s_partition_list_lock; -esp_partition_iterator_t esp_partition_find(esp_partition_type_t type, - esp_partition_subtype_t subtype, const char* label) +static esp_err_t ensure_partitions_loaded(void) { + esp_err_t err = ESP_OK; if (SLIST_EMPTY(&s_partition_list)) { // only lock if list is empty (and check again after acquiring lock) _lock_acquire(&s_partition_list_lock); - esp_err_t err = ESP_OK; if (SLIST_EMPTY(&s_partition_list)) { + ESP_LOGD(TAG, "Loading the partition table"); err = load_partitions(); + if (err != ESP_OK) { + ESP_LOGE(TAG, "load_partitions returned 0x%x", err); + } } _lock_release(&s_partition_list_lock); - if (err != ESP_OK) { - return NULL; - } + } + return err; +} + +esp_partition_iterator_t esp_partition_find(esp_partition_type_t type, + esp_partition_subtype_t subtype, const char* label) +{ + if (ensure_partitions_loaded() != ESP_OK) { + return NULL; } // create an iterator pointing to the start of the list // (next item will be the first one) @@ -233,6 +244,11 @@ esp_err_t esp_partition_register_external(esp_flash_t* flash_chip, size_t offset return ESP_ERR_INVALID_SIZE; } + esp_err_t err = ensure_partitions_loaded(); + if (err != ESP_OK) { + return err; + } + partition_list_item_t* item = (partition_list_item_t*) calloc(sizeof(partition_list_item_t), 1); if (item == NULL) { return ESP_ERR_NO_MEM;