Files
esp-idf/components/esp_partition/CMakeLists.txt
T
Adam Múdry 749c446a7e 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.
2026-04-10 15:22:50 +02:00

82 lines
2.9 KiB
CMake

idf_build_get_property(esp_tee_build ESP_TEE_BUILD)
set(reqs esp_blockdev)
# bootloader build simplified version
if(BOOTLOADER_BUILD)
set(srcs "partition_bootloader.c")
list(APPEND reqs spi_flash)
set(priv_reqs "bootloader_support" "efuse")
idf_component_register(SRCS "${srcs}"
INCLUDE_DIRS "include"
REQUIRES ${reqs}
PRIV_REQUIRES ${priv_reqs})
# esp-tee build simplified version
elseif(esp_tee_build)
set(srcs "partition_tee.c")
list(APPEND reqs spi_flash)
set(priv_reqs "tee_flash_mgr" "efuse")
idf_component_register(SRCS "${srcs}"
INCLUDE_DIRS "include"
REQUIRES ${reqs}
PRIV_REQUIRES ${priv_reqs})
# regular, OS build
else()
set(srcs "partition.c")
set(priv_reqs esp_system spi_flash partition_table efuse)
set(reqs esp_blockdev)
set(private_include_dirs)
idf_build_get_property(build_dir BUILD_DIR)
idf_build_get_property(target IDF_TARGET)
if(${target} STREQUAL "linux")
list(APPEND srcs "partition_linux.c")
# Steal some include directories from bootloader_support components:
idf_component_get_property(bootloader_support_dir bootloader_support COMPONENT_DIR)
set(private_include_dirs ${bootloader_support_dir}/include)
else()
list(APPEND priv_reqs bootloader_support app_update)
list(APPEND srcs "partition_target.c")
endif()
idf_component_register(SRCS "${srcs}"
INCLUDE_DIRS "include"
PRIV_INCLUDE_DIRS ${private_include_dirs}
REQUIRES ${reqs}
PRIV_REQUIRES ${priv_reqs})
if(${target} STREQUAL "linux")
# set BUILD_DIR because partition_linux.c uses a file created in the build directory
target_compile_definitions(${COMPONENT_LIB} PRIVATE "BUILD_DIR=\"${build_dir}\"")
target_link_libraries(${COMPONENT_LIB} PRIVATE dl)
# Create the linux_flash_data target that accumulates partition data entries
# registered by esp_partition_register_target(). The target may already exist if
# a project_include.cmake ran before this CMakeLists.txt.
if(NOT TARGET linux_flash_data)
add_custom_target(linux_flash_data)
endif()
# Generate a manifest file listing all partition data binaries to pre-load
# into the emulated flash. Each line has format: "<hex_offset> <absolute_path>".
# Generator expressions are evaluated after all CMakeLists.txt processing completes,
# so all entries accumulated by esp_partition_register_target() will be included.
file(GENERATE
OUTPUT "${build_dir}/linux_flash_data.txt"
CONTENT "$<JOIN:$<TARGET_PROPERTY:linux_flash_data,FLASH_DATA_ENTRIES>,\n>\n")
endif()
if(CMAKE_C_COMPILER_ID MATCHES "GNU")
# These flags are GCC specific
set_property(SOURCE ${cache_srcs} APPEND_STRING PROPERTY COMPILE_FLAGS
" -fno-inline-small-functions -fno-inline-functions-called-once")
endif()
endif()