How to embed binary files into esp32 firmware

Hello

I am trying to compile the esp32 ssl example with Platformio:

The problem seems to be that platformIO is not recognising the needed component.mk with its option:
COMPONENT_EMBED_TXTFILES := server_root_cert.pem
Documented here: Build System - ESP32 - — ESP-IDF Programming Guide latest documentation

there is no obvious way to me to configure or bypass these configuration to the underlaying sdk.

Can anybody help me with this problem?

Anyone found an answer to this ?

http://docs.platformio.org/en/latest/platforms/espressif32.html#embedding-binary-data

2 Likes

Quick question about that link. Its says ’ You can set a macro (define) COMPONENT_EMBED_FILES using build_flags"

Yet the example given is: Notice here they are using “COMPONENT_EMBED_TXTFILES”

[env:myenv]
platform = espressif32
board = …
build_flags =
-DCOMPONENT_EMBED_TXTFILES=src/private.pem.key:src/certificate.pem.crt:src/aws-root-ca.pem

Later on that page they say again " The names are generated from the full name of the file, as given in COMPONENT_EMBED_FILES ." So where did they get “COMPONENT_EMBED_TXTFILES” at ?

You are right! Thanks! I’ve just fixed typos

Anyone did resolve this issue?
What is the supported way to embed binary not text files?
Is the key COMPONENT_EMBED_FILES supported by platformio? (I can see only the key COMPONENT_EMBED_TXTFILES mentioned in the pio documentation)

Yes COMPONENT_EMBED_TXTFILES works for binary data, just as stated in the documentation.

some binary or text data

Thank you maxgerhardt for your reply.
As a conclusion for my analysis: [COMPONENT_EMBED_TXTFILES] yes does support binary, but as well it adds “\0” at the end of the string once stored on ESP32. That illegal character was breaking the java scripts that I was uploading to ESP32.
The solution was to copy “string length -1” to avoid including the “\0” at the end of the file once it is returned to the user from ESP32.
example:
“extern const uint8_t code_js_start asm(”_binary_src_code_js_start");
extern const uint8_t code_js_end asm(“_binary_src_code_js_end”);
netconn_write(conn, code_js_start, code_js_end - code_js_start -1, NETCONN_NOCOPY); "

Hope that could be helpful to the community having same problem.

1 Like

Thank you all for the information. I intend to use the COMPONENT_EMBED_TXTFILES option in my current ESP32 project however, I believe there is a problem. It appears that during compilation, the ‘\0’ null char is not only added to the embedded file, the source file is also modified. The best way to replicate this error is to embed a Javascript file. On the ESP32 device, the file will work correctly (assuming you specify the length -1) however if the source file is now opened, a NULL Char has been appended to the end and it is broken. Attempting to set the source file as Read Only causes the compiler to have a meltdown! Although this is not a ‘Critical’ problem, it causes no end of drama while developing.

Please file an issue here Issues · platformio/platform-espressif32 · GitHub

hello ,

I’ve also tried without success to embed binary file into my webserver .

This is my platformio.ini :

[env:esp32doit-devkit-v1]
platform = espressif32
board = esp32dev
framework = espidf
monitor_speed = 115200
debug_tool = esp-prog
build_flags =
-DCOMPONENT_EMBED_TXTFILES=src/logo.png

You can find attache also printscreen of the source with the error

please advise what I’m doing wrong ??
Moran.

The file of the object which is injected into the binary is generated from the path and filename. Since you wrote src/logo.png it will create the two symbols _binary_src_logo_png_start and _binary_src_logo_png_end. In your code you however reference a different name, thus you get the error.

Either move the src/logo.png one folder higher and use -DCOMPONENT_EMBED_TXTFILES=logo.png or use the correct object name in your asm symbol declaration.

1 Like

As an alternative, you can try free utility from Segger, named bin2c: What is Bin2c

It creates C source file with null-terminated array of unsigned char containing data of the binary file which is specified on command line as an argument: bin2c binary_file.

The only manual correction I had to do was to change the array name because my binary file contained dots as part of its name, and they got carried over to the array name.

After that, you can include this C file in yours, and voila.

I have an issue with this using platform = espressif32@1.11.0

As a conclusion for my analysis: [ COMPONENT_EMBED_TXTFILES ] yes does support binary, but as well it adds “\0” at the end of the string once stored on ESP32. That illegal character was breaking the java scripts that I was uploading to ESP32. The solution was to copy “string length -1” to avoid including the “\0” at the end of the file once it is returned to the user from ESP32

Every time I now build or upload my program it will add another “\0” at the end of the file…

I have to go through the embedded files manually and delete the extra \0\0\0\0\0\0\ depending on how many builds i do.

Is this a known issue.

.

According to the issue Multiple Nulls Appended to Files with COMPONENT_EMBED_TXTFILES · Issue #245 · platformio/platform-espressif32 · GitHub

that was fixed. Is it still not working in the latest espressif32 release? If it is fixed, you would either need to upgrade your platform version or a backport of the fix is needed.

(which is commit Update support for embedding files // Issue #161, Resolves #220 · platformio/platform-espressif32@671234e · GitHub, aka since version 1.11.1)

thanks. your a life saver… well since my project was hard coded to 1.11.0 i presume i never got the fix.

I have a look at 1.11.1. cheers for the speedy response

im staying clear of 1.12.0 for now as I need to get my head around the make files

Once i ran the 1.11.1 version after clearing the extra ‘\0’ out the files everything is fixed and building correctly.

1 Like

Are the docs still up to date concerning this?
Doc Link

In that link it says “See full example with embedding Amazon AWS certificates:” Yet that example is outdated and does not work either.

When I download the example and compile it, it does so just fine.

C:\Users\Max\temp\espidf-aws-iot>pio run
Processing esp32dev (platform: espressif32; framework: espidf; board: esp32dev)
------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32dev.html
PLATFORM: Espressif 32 (3.1.0) > Espressif ESP32 Dev Module
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (esp-prog) External (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.40200.210118 (4.2.0)
 - tool-cmake 3.16.4
 - tool-esptoolpy 1.30000.201119 (3.0.0)
 - tool-idf 1.0.1
 - tool-mconf 1.4060000.20190628 (406.0.0)
 - tool-ninja 1.9.0
 - toolchain-esp32ulp 1.22851.191205 (2.28.51)
 - toolchain-xtensa32 2.80400.210114 (8.4.0)
Reading CMake configuration...
..
Building .pio\build\esp32dev\firmware.bin
Retrieving maximum program size .pio\build\esp32dev\firmware.elf
Checking size .pio\build\esp32dev\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]  10.0% (used 32780 bytes from 327680 bytes)
Flash: [=======   ]  72.9% (used 764488 bytes from 1048576 bytes)
esptool.py v3.0
============================================= [SUCCESS] Took 58.84 seconds =============================================

What’s the error on your side? Full log?

If you take a look at CMakeLists.txt file for a current project, and that one you will notice they are significantly different. Using Esp32 v3.2.0 & PlatformIO v2.3.2.
If you try to add target_add_binary_data(${COMPONENT_TARGET} “src/myfile.txt” TEXT)
you will get an error.

I.E. A current project looks like this:

> # This file was automatically generated for projects
> # without default 'CMakeLists.txt' file.
> 
> FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*)
> 
> idf_component_register(SRCS ${app_sources})

And that projects CMakeLists.txt file.

set(COMPONENT_SRCS "subscribe_publish_sample.c")
set(COMPONENT_ADD_INCLUDEDIRS ".")


register_component()

if(CONFIG_EXAMPLE_EMBEDDED_CERTS)
target_add_binary_data(${COMPONENT_TARGET} "certs/aws-root-ca.pem" TEXT)
target_add_binary_data(${COMPONENT_TARGET} "certs/certificate.pem.crt" TEXT)
target_add_binary_data(${COMPONENT_TARGET} "certs/private.pem.key" TEXT)
endif()

Regardless, if you follow this Link

It says, you can use board_build.embed_txtfiles = your file.
When I enter this in using my file it gives an error. Not at work so cannot copy it now. Basically I just want to be sure that the docs are still correct and things have not changed. That example does not do it this way, and I want to be able to.