Skip to content

Commit 02d61c1

Browse files
feat(esp_partition): Adds new esp_partition APIs
1 parent 82b1d5e commit 02d61c1

File tree

7 files changed

+207
-44
lines changed

7 files changed

+207
-44
lines changed

components/app_update/test_apps/test_app_update/main/test_switch_ota.c

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -101,24 +101,6 @@ static void copy_app_partition_with_offset(esp_ota_handle_t update_handle, const
101101
ESP_LOGI(TAG, "finish the copy process");
102102
}
103103

104-
#if defined(CONFIG_BOOTLOADER_FACTORY_RESET) || defined(CONFIG_BOOTLOADER_APP_TEST)
105-
/* @brief Copies partition from source partition to destination partition.
106-
*
107-
* Partitions can be of any types and subtypes.
108-
* @param[in] dst_partition - Destination partition
109-
* @param[in] src_partition - Source partition
110-
*/
111-
static void copy_partition(const esp_partition_t *dst_partition, const esp_partition_t *src_partition)
112-
{
113-
const void *partition_bin = NULL;
114-
esp_partition_mmap_handle_t data_map;
115-
TEST_ESP_OK(esp_partition_mmap(src_partition, 0, src_partition->size, ESP_PARTITION_MMAP_DATA, &partition_bin, &data_map));
116-
TEST_ESP_OK(esp_partition_erase_range(dst_partition, 0, dst_partition->size));
117-
TEST_ESP_OK(esp_partition_write(dst_partition, 0, (const void *)partition_bin, dst_partition->size));
118-
esp_partition_munmap(data_map);
119-
}
120-
#endif
121-
122104
/* @brief Get the next partition of OTA for the update.
123105
*
124106
* @return The next partition of OTA(OTA0-15).
@@ -530,7 +512,7 @@ static void test_flow5(void)
530512
ESP_LOGI(TAG, "Factory");
531513
TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_FACTORY, cur_app->subtype);
532514
set_output_pin(CONFIG_BOOTLOADER_NUM_PIN_APP_TEST);
533-
copy_partition(esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_TEST, NULL), cur_app);
515+
esp_partition_copy(esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_TEST, NULL), 0, cur_app, 0, cur_app->size);
534516
esp_restart();
535517
break;
536518
case 3:

components/esp_partition/host_test/partition_api_test/main/partition_api_test.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,48 @@ TEST(partition_api, test_partition_power_off_emulation)
722722
free(test_data_ptr);
723723
}
724724

725+
TEST(partition_api, test_partition_copy)
726+
{
727+
const esp_partition_t *factory_part = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_FACTORY, NULL);
728+
TEST_ASSERT_NOT_NULL(factory_part);
729+
730+
const esp_partition_t *ota0_part = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_0, NULL);
731+
TEST_ASSERT_NOT_NULL(ota0_part);
732+
733+
TEST_ESP_OK(esp_partition_copy(ota0_part, 0, factory_part, 0, factory_part->size));
734+
TEST_ESP_OK(esp_partition_copy(ota0_part, 0, factory_part, 0, SIZE_MAX));
735+
736+
TEST_ESP_ERR(ESP_ERR_INVALID_SIZE, esp_partition_copy(ota0_part, 0x1000000, factory_part, 0, SIZE_MAX));
737+
TEST_ESP_ERR(ESP_ERR_INVALID_SIZE, esp_partition_copy(ota0_part, 0, factory_part, 0x1000000, SIZE_MAX));
738+
739+
TEST_ESP_ERR(ESP_ERR_INVALID_SIZE, esp_partition_copy(ota0_part, 0, factory_part, 0, SIZE_MAX - 1));
740+
741+
TEST_ESP_ERR(ESP_ERR_INVALID_SIZE, esp_partition_copy(ota0_part, UINT32_MAX - 1, factory_part, 0, 0x10000));
742+
TEST_ESP_ERR(ESP_ERR_INVALID_SIZE, esp_partition_copy(ota0_part, 0, factory_part, UINT32_MAX - 1, 0x10000));
743+
744+
TEST_ESP_ERR(ESP_ERR_INVALID_SIZE, esp_partition_copy(ota0_part, UINT32_MAX - 1, factory_part, 0, SIZE_MAX));
745+
TEST_ESP_ERR(ESP_ERR_INVALID_SIZE, esp_partition_copy(ota0_part, 0, factory_part, UINT32_MAX - 1, SIZE_MAX));
746+
}
747+
748+
TEST(partition_api, test_partition_register_external)
749+
{
750+
esp_err_t error;
751+
const esp_partition_t *ota1_part = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_1, NULL);
752+
TEST_ASSERT_NULL(ota1_part);
753+
const esp_partition_t *storage_part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_UNDEFINED, NULL);
754+
error = esp_partition_register_external(NULL,
755+
storage_part->address + storage_part->size, // place this new partition after the storage (the last part in the table)
756+
1 * 1024 * 1024,
757+
"ota_1",
758+
ESP_PARTITION_TYPE_APP,
759+
ESP_PARTITION_SUBTYPE_APP_OTA_1,
760+
&ota1_part);
761+
TEST_ESP_OK(error);
762+
ota1_part = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_1, NULL);
763+
TEST_ASSERT_NOT_NULL(ota1_part);
764+
TEST_ESP_OK(esp_partition_deregister_external(ota1_part));
765+
}
766+
725767
TEST_GROUP_RUNNER(partition_api)
726768
{
727769
RUN_TEST_CASE(partition_api, test_partition_find_basic);
@@ -741,6 +783,8 @@ TEST_GROUP_RUNNER(partition_api)
741783
RUN_TEST_CASE(partition_api, test_partition_mmap_size_too_small);
742784
RUN_TEST_CASE(partition_api, test_partition_stats);
743785
RUN_TEST_CASE(partition_api, test_partition_power_off_emulation);
786+
RUN_TEST_CASE(partition_api, test_partition_copy);
787+
RUN_TEST_CASE(partition_api, test_partition_register_external);
744788
}
745789

746790
static void run_all_tests(void)

components/esp_partition/host_test/partition_api_test/partition_table.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
nvs, data, nvs, 0x9000, 0x6000,
44
phy_init, data, phy, 0xf000, 0x1000,
55
factory, app, factory, 0x10000, 1M,
6+
ota_0, app, ota_0, 0x120000, 1M,
67
storage, data, , , 0x40000,

components/esp_partition/include/esp_partition.h

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ typedef struct esp_flash_t esp_flash_t;
2929
* @brief Enumeration which specifies memory space requested in an mmap call
3030
*/
3131
typedef enum {
32-
ESP_PARTITION_MMAP_DATA, /**< map to data memory (Vaddr0), allows byte-aligned access, 4 MB total */
33-
ESP_PARTITION_MMAP_INST, /**< map to instruction memory (Vaddr1-3), allows only 4-byte-aligned access, 11 MB total */
32+
ESP_PARTITION_MMAP_DATA, /**< map to data memory (Vaddr0), allows byte-aligned access, (4 MB total - only for esp32) */
33+
ESP_PARTITION_MMAP_INST, /**< map to instruction memory (Vaddr1-3), allows only 4-byte-aligned access, (11 MB total - only for esp32) */
3434
} esp_partition_mmap_memory_t;
3535

3636
/**
@@ -50,6 +50,8 @@ typedef uint32_t esp_partition_mmap_handle_t;
5050
typedef enum {
5151
ESP_PARTITION_TYPE_APP = 0x00, //!< Application partition type
5252
ESP_PARTITION_TYPE_DATA = 0x01, //!< Data partition type
53+
ESP_PARTITION_TYPE_BOOTLOADER = 0x02, //!< Bootloader partition type
54+
ESP_PARTITION_TYPE_PARTITION_TABLE = 0x03, //!< Partition table type
5355

5456
ESP_PARTITION_TYPE_ANY = 0xff, //!< Used to search for partitions with any type
5557
} esp_partition_type_t;
@@ -429,7 +431,7 @@ bool esp_partition_check_identity(const esp_partition_t* partition_1, const esp_
429431
* This API allows designating certain areas of external flash chips (identified by the esp_flash_t structure)
430432
* as partitions. This allows using them with components which access SPI flash through the esp_partition API.
431433
*
432-
* @param flash_chip Pointer to the structure identifying the flash chip
434+
* @param flash_chip Pointer to the structure identifying the flash chip. If NULL then the internal flash chip is used (esp_flash_default_chip).
433435
* @param offset Address in bytes, where the partition starts
434436
* @param size Size of the partition in bytes
435437
* @param label Partition name
@@ -472,6 +474,35 @@ void esp_partition_unload_all(void);
472474
*/
473475
uint32_t esp_partition_get_main_flash_sector_size(void);
474476

477+
/**
478+
* @brief Copy data from a source partition at a specific offset to a destination partition at a specific offset.
479+
*
480+
* The destination offset must be aligned to the flash sector size (SPI_FLASH_SEC_SIZE = 0x1000).
481+
* If "size" is SIZE_MAX, the entire destination partition (from dest_offset onward) will be erased,
482+
* and the function will copy all of the source partition starting from src_offset into the destination.
483+
* The function ensures that the destination partition is erased on sector boundaries (erase size is aligned up SPI_FLASH_SEC_SIZE).
484+
*
485+
* This function does the following:
486+
* - erases the destination partition from dest_offset to the specified size (or the whole partition if "size" == SIZE_MAX),
487+
* - maps data from the source partition in chunks,
488+
* - writes the source data into the destination partition in corresponding chunks.
489+
*
490+
* @param dest_part Pointer to a destination partition.
491+
* @param dest_offset Offset in the destination partition where the data should be written (must be aligned to SPI_FLASH_SEC_SIZE = 0x1000).
492+
* @param src_part Pointer to a source partition (must be located on internal flash).
493+
* @param src_offset Offset in the source partition where the data should be read from.
494+
* @param size Number of bytes to copy from the source partition to the destination partition. If "size" is SIZE_MAX,
495+
* the function copies from src_offset to the end of the source partition and erases
496+
* the entire destination partition (from dest_offset onward).
497+
*
498+
* @return ESP_OK, if the source partition was copied successfully to the destination partition;
499+
* ESP_ERR_INVALID_ARG, if src_part or dest_part are incorrect, or if dest_offset is not sector aligned;
500+
* ESP_ERR_INVALID_SIZE, if the copy would go out of bounds of the source or destination partition;
501+
* ESP_ERR_NOT_ALLOWED, if the destination partition is read-only;
502+
* or one of the error codes from the lower-level flash driver.
503+
*/
504+
esp_err_t esp_partition_copy(const esp_partition_t* dest_part, uint32_t dest_offset, const esp_partition_t* src_part, uint32_t src_offset, size_t size);
505+
475506
#ifdef __cplusplus
476507
}
477508
#endif

components/esp_partition/partition.c

Lines changed: 114 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <string.h>
1010
#include <stdio.h>
1111
#include <sys/lock.h>
12+
#include <sys/param.h>
1213

1314
/* interim to enable test_wl_host and test_fatfs_on_host compilation (both use IDF_TARGET_ESP32)
1415
* should go back to #include "sys/queue.h" once the tests are switched to CMake
@@ -24,11 +25,11 @@
2425
#include "esp_flash_partitions.h"
2526
#include "esp_attr.h"
2627
#include "esp_partition.h"
27-
#if !CONFIG_IDF_TARGET_LINUX
2828
#include "esp_flash.h"
29+
#if !CONFIG_IDF_TARGET_LINUX
2930
#include "esp_flash_encrypt.h"
30-
#include "spi_flash_mmap.h"
3131
#endif
32+
#include "spi_flash_mmap.h"
3233
#include "esp_log.h"
3334
#include "esp_rom_md5.h"
3435
#include "bootloader_util.h"
@@ -49,6 +50,8 @@
4950
#define INVARIANTS
5051
#endif
5152

53+
#define ALIGN_UP(num, align) (((num) + ((align) - 1)) & ~((align) - 1))
54+
5255
typedef struct partition_list_item_ {
5356
esp_partition_t info;
5457
bool user_registered;
@@ -68,6 +71,31 @@ static _lock_t s_partition_list_lock;
6871

6972
static const char *TAG = "partition";
7073

74+
static bool is_partition_encrypted(bool encryption_config, esp_partition_type_t type, esp_partition_subtype_t subtype)
75+
{
76+
#if CONFIG_IDF_TARGET_LINUX
77+
(void) type;
78+
(void) subtype;
79+
(void) encryption_config;
80+
return false;
81+
#else
82+
bool ret_encrypted = encryption_config;
83+
if (!esp_flash_encryption_enabled()) {
84+
/* If flash encryption is not turned on, no partitions should be treated as encrypted */
85+
ret_encrypted = false;
86+
} else if (type == ESP_PARTITION_TYPE_APP
87+
|| (type == ESP_PARTITION_TYPE_BOOTLOADER)
88+
|| (type == ESP_PARTITION_TYPE_PARTITION_TABLE)
89+
|| (type == ESP_PARTITION_TYPE_DATA && subtype == ESP_PARTITION_SUBTYPE_DATA_OTA)
90+
|| (type == ESP_PARTITION_TYPE_DATA && subtype == ESP_PARTITION_SUBTYPE_DATA_NVS_KEYS)) {
91+
/* If encryption is turned on, all app partitions and OTA data
92+
are always encrypted */
93+
ret_encrypted = true;
94+
}
95+
return ret_encrypted;
96+
#endif
97+
}
98+
7199
// Create linked list of partition_list_item_t structures.
72100
// This function is called only once, with s_partition_list_lock taken.
73101
static esp_err_t load_partitions(void)
@@ -151,25 +179,10 @@ static esp_err_t load_partitions(void)
151179
#endif
152180
item->info.type = entry.type;
153181
item->info.subtype = entry.subtype;
154-
item->info.encrypted = entry.flags & PART_FLAG_ENCRYPTED;
182+
item->info.encrypted = is_partition_encrypted(entry.flags & PART_FLAG_ENCRYPTED, entry.type, entry.subtype);
155183
item->info.readonly = entry.flags & PART_FLAG_READONLY;
156184
item->user_registered = false;
157185

158-
#if CONFIG_IDF_TARGET_LINUX
159-
item->info.encrypted = false;
160-
#else
161-
if (!esp_flash_encryption_enabled()) {
162-
/* If flash encryption is not turned on, no partitions should be treated as encrypted */
163-
item->info.encrypted = false;
164-
} else if (entry.type == ESP_PARTITION_TYPE_APP
165-
|| (entry.type == ESP_PARTITION_TYPE_DATA && entry.subtype == ESP_PARTITION_SUBTYPE_DATA_OTA)
166-
|| (entry.type == ESP_PARTITION_TYPE_DATA && entry.subtype == ESP_PARTITION_SUBTYPE_DATA_NVS_KEYS)) {
167-
/* If encryption is turned on, all app partitions and OTA data
168-
are always encrypted */
169-
item->info.encrypted = true;
170-
}
171-
#endif
172-
173186
#if CONFIG_NVS_COMPATIBLE_PRE_V4_3_ENCRYPTION_FLAG
174187
if (entry.type == ESP_PARTITION_TYPE_DATA &&
175188
entry.subtype == ESP_PARTITION_SUBTYPE_DATA_NVS &&
@@ -392,10 +405,10 @@ esp_err_t esp_partition_register_external(esp_flash_t *flash_chip, size_t offset
392405
*out_partition = NULL;
393406
}
394407

395-
#if CONFIG_IDF_TARGET_LINUX
396-
return ESP_ERR_NOT_SUPPORTED;
397-
398-
#else
408+
#if !CONFIG_IDF_TARGET_LINUX
409+
if (flash_chip == NULL) {
410+
flash_chip = esp_flash_default_chip;
411+
}
399412
if (offset + size > flash_chip->size) {
400413
return ESP_ERR_INVALID_SIZE;
401414
}
@@ -415,7 +428,14 @@ esp_err_t esp_partition_register_external(esp_flash_t *flash_chip, size_t offset
415428
item->info.size = size;
416429
item->info.type = type;
417430
item->info.subtype = subtype;
431+
#if CONFIG_IDF_TARGET_LINUX
432+
item->info.erase_size = ESP_PARTITION_EMULATED_SECTOR_SIZE;
418433
item->info.encrypted = false;
434+
#else
435+
item->info.erase_size = SPI_FLASH_SEC_SIZE;
436+
item->info.encrypted = (flash_chip == esp_flash_default_chip) ? is_partition_encrypted(false, type, subtype) : false;
437+
#endif // CONFIG_IDF_TARGET_LINUX
438+
item->info.readonly = false;
419439
item->user_registered = true;
420440
strlcpy(item->info.label, label, sizeof(item->info.label));
421441

@@ -466,3 +486,75 @@ esp_err_t esp_partition_deregister_external(const esp_partition_t *partition)
466486
_lock_release(&s_partition_list_lock);
467487
return result;
468488
}
489+
490+
esp_err_t esp_partition_copy(const esp_partition_t* dest_part, uint32_t dest_offset, const esp_partition_t* src_part, uint32_t src_offset, size_t size)
491+
{
492+
if (src_part == NULL || dest_part == NULL || src_part == dest_part) {
493+
return ESP_ERR_INVALID_ARG;
494+
}
495+
496+
if (src_offset > src_part->size || dest_offset > dest_part->size) {
497+
return ESP_ERR_INVALID_SIZE;
498+
}
499+
500+
// Check if the source partition is on external flash and return error
501+
#if !CONFIG_IDF_TARGET_LINUX
502+
if (src_part->flash_chip != esp_flash_default_chip) {
503+
ESP_LOGE(TAG, "Source partition is on external flash. Operation not supported.");
504+
return ESP_ERR_NOT_SUPPORTED;
505+
}
506+
#endif
507+
508+
size_t dest_erase_size = size;
509+
if (size == SIZE_MAX) {
510+
size = src_part->size - src_offset;
511+
dest_erase_size = dest_part->size - dest_offset; // Erase the whole destination partition
512+
}
513+
514+
uint32_t src_end_offset;
515+
uint32_t dest_end_offset;
516+
if ((__builtin_add_overflow(src_offset, size, &src_end_offset) || (src_end_offset > src_part->size))
517+
|| (__builtin_add_overflow(dest_offset, size, &dest_end_offset) || (dest_end_offset > dest_part->size))) { // with overflow checks
518+
return ESP_ERR_INVALID_SIZE;
519+
}
520+
521+
esp_err_t error = esp_partition_erase_range(dest_part, dest_offset, ALIGN_UP(dest_erase_size, SPI_FLASH_SEC_SIZE));
522+
if (error) {
523+
ESP_LOGE(TAG, "Erasing destination partition range failed (err=0x%x)", error);
524+
return error;
525+
}
526+
527+
uint32_t src_current_offset = src_offset;
528+
uint32_t dest_current_offset = dest_offset;
529+
size_t remaining_size = size;
530+
/* Read the portion that fits in the free MMU pages */
531+
uint32_t mmu_free_pages_count = spi_flash_mmap_get_free_pages(SPI_FLASH_MMAP_DATA);
532+
int attempts_for_mmap = 0;
533+
while (remaining_size > 0) {
534+
uint32_t chunk_size = MIN(remaining_size, mmu_free_pages_count * SPI_FLASH_MMU_PAGE_SIZE);
535+
esp_partition_mmap_handle_t src_part_map;
536+
const void *src_data = NULL;
537+
error = esp_partition_mmap(src_part, src_current_offset, chunk_size, ESP_PARTITION_MMAP_DATA, &src_data, &src_part_map);
538+
if (error == ESP_OK) {
539+
attempts_for_mmap = 0;
540+
error = esp_partition_write(dest_part, dest_current_offset, src_data, chunk_size);
541+
if (error != ESP_OK) {
542+
ESP_LOGE(TAG, "Writing to destination partition failed (err=0x%x)", error);
543+
esp_partition_munmap(src_part_map);
544+
break;
545+
}
546+
esp_partition_munmap(src_part_map);
547+
} else {
548+
mmu_free_pages_count = spi_flash_mmap_get_free_pages(SPI_FLASH_MMAP_DATA);
549+
chunk_size = 0;
550+
if (++attempts_for_mmap >= 3) {
551+
ESP_LOGE(TAG, "Failed to mmap source partition after a few attempts, mmu_free_pages = %" PRIu32 " (err=0x%x)", mmu_free_pages_count, error);
552+
break;
553+
}
554+
}
555+
src_current_offset += chunk_size;
556+
dest_current_offset += chunk_size;
557+
remaining_size -= chunk_size;
558+
}
559+
return error;
560+
}

components/spi_flash/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ idf_build_get_property(target IDF_TARGET)
22
if(${target} STREQUAL "linux")
33
idf_component_register(SRCS "linux/spi_flash_linux.c"
44
"linux/cache_utils.c"
5+
"linux/flash_mmap.c"
56
INCLUDE_DIRS include
67
PRIV_INCLUDE_DIRS include/spi_flash)
78
return()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include "spi_flash_mmap.h"
7+
8+
uint32_t spi_flash_mmap_get_free_pages(spi_flash_mmap_memory_t memory)
9+
{
10+
(void) memory;
11+
return 10;
12+
}

0 commit comments

Comments
 (0)