Embedding file with esp-idf

I’ve got a problem getting a root cert to embed properly in my project. I’ve followed, to the best that I can tell, the instructions in (link removed because I’m a new user) but I’m getting a failure:

rhettg ➜ ~/Documents/PlatformIO/Projects/YakasaurDemo (main) $ pio --version
PlatformIO Core, version 6.1.15
rhettg ➜ ~/Documents/PlatformIO/Projects/YakasaurDemo (main) $ pio run
Processing esp32-s3-devkitc-1 (platform: espressif32; board: esp32-s3-devkitc-1; framework: espidf)
-----------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32-s3-devkitc-1.html
PLATFORM: Espressif 32 (6.6.0) > Espressif ESP32-S3-DevKitC-1-N8 (8 MB QD, No PSRAM)
HARDWARE: ESP32S3 240MHz, 320KB RAM, 8MB Flash
DEBUG: Current (esp-builtin) On-board (esp-builtin) External (cmsis-dap, esp-bridge, esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES: 
 - framework-espidf @ 3.50201.0 (5.2.1) 
 - tool-cmake @ 3.16.4 
 - tool-esptoolpy @ 1.40501.0 (4.5.1) 
 - tool-ninja @ 1.9.0 
 - tool-riscv32-esp-elf-gdb @ 12.1.0+20221002 
 - tool-xtensa-esp-elf-gdb @ 12.1.0+20221002 
 - toolchain-esp32ulp @ 1.23500.220830 (2.35.0) 
 - toolchain-riscv32-esp @ 13.2.0+20230928 
 - toolchain-xtensa-esp32s3 @ 12.2.0+20230208
Reading CMake configuration...
Warning! Flash memory size mismatch detected. Expected 8MB, found 2MB!
Please select a proper value in your `sdkconfig.defaults` or via the `menuconfig` target!
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 0 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Linking .pio/build/esp32-s3-devkitc-1/bootloader.elf
Linking .pio/build/esp32-s3-devkitc-1/firmware.elf
/Users/rhettg/.platformio/packages/toolchain-xtensa-esp32s3/bin/../lib/gcc/xtensa-esp32s3-elf/12.2.0/../../../../xtensa-esp32s3-elf/bin/ld: .pio/build/esp32-s3-devkitc-1/src/main.o:(.literal.send_image+0x2c): undefined reference to `_binary_isrg_root_pem_start'
collect2: error: ld returned 1 exit status
*** [.pio/build/esp32-s3-devkitc-1/firmware.elf] Error 1
========================================================= [FAILED] Took 8.07 seconds =========================================================

My platform.ini:

[env:esp32-s3-devkitc-1]
platform = espressif32
board = esp32-s3-devkitc-1
framework = espidf
monitor_speed = 115200
monitor_filters = direct
board_build.embed_txtfiles =
  src/isrg_root.pem

According to the docs:

In case with ESP-IDF, these files also need to be specified in CMakeLists.txt using the target_add_binary_data function.

The link to the example project doesn’t work, but I found Espressif’s documentation (but I can’t link it) which indicates how to use target_add_binary_data. So I did that:

cmake_minimum_required(VERSION 3.16.0)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(YakasaurDemo)

target_add_binary_data(YakasaurDemo.elf "src/isrg_root.pem" TEXT)

The platformio docs also mention:

With the ESP-IDF framework symbol names should not contain path to the files, for example _binary_private_pem_key_start instead of _binary_src_private_pem_key_start.

So I’m referencing them like this in main.c:

extern const char isrg_root_pem_start[] asm("_binary_isrg_root_pem_start");
extern const char isrg_root_pem_end[]   asm("_binary_isrg_root_pem_end");

Full project is here on GitHub.

What am I missing? Any help would be very appreciated.

I’ve finally got it to build by finding a working example in the platformio repository: platform-espressif32/examples/espidf-coap-server/src/CMakeLists.txt at 022e6044b468d0db65d502f5a16a25f446e94f5e · platformio/platform-espressif32 · GitHub

This suggests that using target_add_binary_data as documented is probably no longer correct.

But this works:

FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.c)

idf_component_register(SRCS ${app_sources}
                    INCLUDE_DIRS "."
                    EMBED_TXTFILES isrg_root.pem)
1 Like