Merge branch 'feat/cmake_add_partition_flash_binary_function_v6.0' into 'release/v6.0'

feat(esp_partition): Add esp_partition_register_target Cmake function (v6.0)

See merge request espressif/esp-idf!51045
This commit is contained in:
Jiang Jiang Jian
2026-09-03 19:50:11 +08:00
15 changed files with 405 additions and 39 deletions

View File

@@ -5,9 +5,9 @@ if(NOT CONFIG_NVS_ENCRYPTION)
nvs_create_partition_image(nvs ../nvs_data.csv FLASH_IN_PROJECT)
else()
if(CONFIG_NVS_SEC_KEY_PROTECT_USING_FLASH_ENC)
esptool_py_flash_to_partition(flash "nvs_key" ${PROJECT_DIR}/main/encryption_keys.bin)
esptool_py_flash_to_partition(flash "nvs" ${PROJECT_DIR}/main/nvs_encrypted.bin)
esp_partition_register_target("nvs_key" ${PROJECT_DIR}/main/encryption_keys.bin FLASH_IN_PROJECT)
esp_partition_register_target("nvs" ${PROJECT_DIR}/main/nvs_encrypted.bin FLASH_IN_PROJECT)
else() # NVS Encryption using HMAC (CONFIG_NVS_SEC_KEY_PROTECT_USING_HMAC)
esptool_py_flash_to_partition(flash "nvs" ${PROJECT_DIR}/main/nvs_encrypted_hmac.bin)
esp_partition_register_target("nvs" ${PROJECT_DIR}/main/nvs_encrypted_hmac.bin FLASH_IN_PROJECT)
endif()
endif()

View File

@@ -18,7 +18,8 @@ and read back using `esp_partition_read`, verifying the read and written data ma
I (588) example: Written data: ESP-IDF Partition Operations Example (Read, Erase, Write)
I (588) example: Read data: ESP-IDF Partition Operations Example (Read, Erase, Write)
I (638) example: Erased data
I (638) example: Example end
I (693) example: Read data from custom partition: abcdef123456
I (693) example: Example end
```
# Others

View File

@@ -0,0 +1,39 @@
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Unlicense OR CC0-1.0
"""Create a 4kiB binary file with 'abcdef123456' at the start, rest filled with zeros."""
import argparse
import os
FILE_NAME = 'custom_partition.bin'
FILE_SIZE = 4 * 1024 # 4 kiB
HEADER = b'abcdef123456'
def main() -> None:
parser = argparse.ArgumentParser(description='Generate a custom partition binary file.')
parser.add_argument(
'output_path',
nargs='?',
default=None,
help='Path for the output binary file. Defaults to custom_partition.bin in the script directory.',
)
args = parser.parse_args()
if args.output_path:
output_path = args.output_path
else:
script_dir = os.path.dirname(os.path.abspath(__file__))
output_path = os.path.join(script_dir, FILE_NAME)
data = HEADER + b'\x00' * (FILE_SIZE - len(HEADER))
with open(output_path, 'wb') as f:
f.write(data)
print(f'Created {output_path} ({FILE_SIZE} bytes)')
if __name__ == '__main__':
main()

View File

@@ -1,3 +1,21 @@
idf_component_register(SRCS "main.c"
INCLUDE_DIRS "."
REQUIRES esp_partition)
set(partition "custom_partition")
set(image_file ${CMAKE_BINARY_DIR}/custom_partition.bin)
set(custom_partition_script_py ${CMAKE_CURRENT_SOURCE_DIR}/../create_custom_partition.py)
# Generate custom_partition.bin using the Python script (output to build directory)
add_custom_command(
OUTPUT ${image_file}
COMMAND ${PYTHON} ${custom_partition_script_py} ${image_file}
DEPENDS ${custom_partition_script_py}
COMMENT "Generating custom_partition.bin"
)
add_custom_target(generate_custom_partition ALL DEPENDS ${image_file})
# Flash the generated binary to the partition named "custom_partition"
# (build target depends on generate_custom_partition to ensure the binary is created first, and FLASH_IN_PROJECT
# makes it get flashed when calling `idf.py custom_partition-flash` or just `idf.py flash`)
esp_partition_register_target(${partition} "${image_file}" DEPENDS generate_custom_partition FLASH_IN_PROJECT)

View File

@@ -54,5 +54,15 @@ void app_main(void)
ESP_LOGI(TAG, "Erased data");
// Find the custom partition in the partition table
partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "custom_partition");
assert(partition != NULL);
memset(read_data, 0, sizeof(read_data));
// Read the data and check if it matches the expected content (first 12 bytes should be "abcdef123456", rest should be 0x00's)
ESP_ERROR_CHECK(esp_partition_read(partition, 0, read_data, sizeof(read_data)));
assert(memcmp("abcdef123456", read_data, 12) == 0);
ESP_LOGI(TAG, "Read data from custom partition: %s", read_data);
ESP_LOGI(TAG, "Example end");
}

View File

@@ -3,4 +3,5 @@
nvs, data, nvs, 0x9000, 0x6000,
phy_init, data, phy, 0xf000, 0x1000,
factory, app, factory, 0x10000, 1M,
storage, data, , , 0x40000,
storage, data, , , 0x3F000,
custom_partition, data, , , 0x1000,
1 # Name, Type, SubType, Offset, Size, Flags
3 nvs, data, nvs, 0x9000, 0x6000,
4 phy_init, data, phy, 0xf000, 0x1000,
5 factory, app, factory, 0x10000, 1M,
6 storage, data, , , 0x40000, storage, data, , , 0x3F000,
7 custom_partition, data, , , 0x1000,