esp32

Is it possible to read my ESP32 OTA partition application image size at runtime


I am developing (because I couldn't find an IDF example app) an application where my ROOT_MESH will download and update itself with an OTA image from an HTTPS server (from working example), and then proceed to read the current working OTA partition and send it in fragments of 1024 bytes across the WIFI mesh network using esp_mesh_send.

Going well so far but have run into a snag finding out how large the app in the current partition is when I am reading it using esp_partition_read. I expected the size would be returned in the esp_ota_get_partition_description but it is not. I don't want to send the entire OTA partition when my application is much less.

I added a work around test detecting when I read a block (1024 bytes) entirely consisting of 0xff and assuming this is empty flash. My concern is I would like to use the SHA256 of the esp_app_desc_t as part of my detection the image has changed at the MESH_NODE and I am assuming this won't match if I have added a bunch of 0xff bytes on the end.

I have tested adding a bunch of 0xffs to my current .bin file and then OTA updating it to the ROOT_NODE using https OTA, and doesn't seem to mind them. I also found I could change the contents of the bin file at the start in the description block changing the App name, version and date, and the OTA update still loaded and ran fine. This really surprised me as it demonstrates there is no form of file integrity check (eg checksum or CRC) in the bin file itself. I am not sure what the sha256 in the application description does? Maybe this will become a problem if I enable "Require signed app images" later.

I would really like to know the original image size, is this possible to obtain at runtime?


Solution

  • For anyone following in future, you can use esp_image_get_metadata(): https://github.com/espressif/esp-idf/blob/6673376297b921d438790d195014b860eaf8bb17/components/bootloader_support/include/esp_image_format.h#L115-L127

    The function: esp_err_t err = esp_image_get_metadata(&partition, &metadata); returns the image_len in the metadata, for the partition passed to load the program from