From f92e1c45ae42ee44267035d2f9ee0ddc5f4491a5 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Wed, 5 May 2021 15:11:29 +0200 Subject: [PATCH] esp_partition: add test, docs for ESP_PARTITION_TYPE_ANY --- components/partition_table/test/test_partition.c | 9 +++++++++ components/spi_flash/include/esp_partition.h | 8 ++++++-- components/spi_flash/partition.c | 5 +++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/components/partition_table/test/test_partition.c b/components/partition_table/test/test_partition.c index 15088728bb..0abc1c82ad 100644 --- a/components/partition_table/test/test_partition.c +++ b/components/partition_table/test/test_partition.c @@ -28,6 +28,15 @@ TEST_CASE("Can read partition table", "[partition]") } esp_partition_iterator_release(it); TEST_ASSERT_EQUAL(5, count); + + it = esp_partition_find(ESP_PARTITION_TYPE_ANY, ESP_PARTITION_SUBTYPE_ANY, NULL); + TEST_ASSERT_NOT_NULL(it); + count = 0; + for (; it != NULL; it = esp_partition_next(it)) { + ++count; + } + esp_partition_iterator_release(it); + TEST_ASSERT_EQUAL(8, count); } TEST_CASE("Can write, read, mmap partition", "[partition][ignore]") diff --git a/components/spi_flash/include/esp_partition.h b/components/spi_flash/include/esp_partition.h index fc68d5941a..e59babb451 100644 --- a/components/spi_flash/include/esp_partition.h +++ b/components/spi_flash/include/esp_partition.h @@ -125,7 +125,9 @@ typedef struct { /** * @brief Find partition based on one or more parameters * - * @param type Partition type, one of esp_partition_type_t values or an 8-bit unsigned integer + * @param type Partition type, one of esp_partition_type_t values or an 8-bit unsigned integer. + * To find all partitions, no matter the type, use ESP_PARTITION_TYPE_ANY, and set + * subtype argument to ESP_PARTITION_SUBTYPE_ANY. * @param subtype Partition subtype, one of esp_partition_subtype_t values or an 8-bit unsigned integer. * To find all partitions of given type, use ESP_PARTITION_SUBTYPE_ANY. * @param label (optional) Partition label. Set this value if looking @@ -141,7 +143,9 @@ esp_partition_iterator_t esp_partition_find(esp_partition_type_t type, esp_parti /** * @brief Find first partition based on one or more parameters * - * @param type Partition type, one of esp_partition_type_t values or an 8-bit unsigned integer + * @param type Partition type, one of esp_partition_type_t values or an 8-bit unsigned integer. + * To find all partitions, no matter the type, use ESP_PARTITION_TYPE_ANY, and set + * subtype argument to ESP_PARTITION_SUBTYPE_ANY. * @param subtype Partition subtype, one of esp_partition_subtype_t values or an 8-bit unsigned integer * To find all partitions of given type, use ESP_PARTITION_SUBTYPE_ANY. * @param label (optional) Partition label. Set this value if looking diff --git a/components/spi_flash/partition.c b/components/spi_flash/partition.c index 22272af125..47b938f4ea 100644 --- a/components/spi_flash/partition.c +++ b/components/spi_flash/partition.c @@ -88,6 +88,11 @@ esp_partition_iterator_t esp_partition_find(esp_partition_type_t type, if (ensure_partitions_loaded() != ESP_OK) { return NULL; } + // Searching for a specific subtype without specifying the type doesn't make + // sense, and is likely a usage error. + if (type == ESP_PARTITION_TYPE_ANY && subtype != ESP_PARTITION_SUBTYPE_ANY) { + return NULL; + } // 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, subtype, label);