feat(esp_partition): Add esp_partition_flash_binary() CMake function

Add a new CMake function esp_partition_flash_binary() that provides a
unified API for registering partition data binaries to be flashed. It
replaces the direct esptool_py_flash_target calls scattered across
components (spiffs, fatfs, nvs_flash) with a single function that:

- Resolves partition offset from the partition table automatically
- Determines encryption requirements (auto-detect or ALWAYS_PLAINTEXT)
- Creates per-partition flash targets (e.g. idf.py <partition>-flash)
- Optionally includes the binary in `idf.py flash` via FLASH_IN_PROJECT

On the linux target, the function registers binaries for pre-loading
into the emulated flash. A build-time manifest (linux_flash_data.txt)
is generated via file(GENERATE), and partition_linux.c reads it at
runtime to copy each binary into the memory-mapped flash buffer at
the correct offset.

The partition_ops example is updated to use the new function and
includes a custom_partition with pre-built data to demonstrate the
full workflow, including on the linux target.
This commit is contained in:
Adam Múdry
2026-04-10 15:22:50 +02:00
parent 443d4705b7
commit 749c446a7e
14 changed files with 368 additions and 36 deletions
@@ -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)
@@ -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");
}