Closed Kampi closed 1 year ago
@Kampi
espsecure.py sign_data --version 2 --keyfile secure_boot_signing_key_1.pem --output 3.0.3-modified.bin 3.0.3.bin
espsecure.py v4.5.1
Padding data contents by 1680 bytes so signature sector aligns at sector boundary
Above log shows that the original firmware binary was not generated with secure padding enabled and hence the signing utility had to add padding bytes. Ideally, for secure boot v2 case, the unsigned firmware must be 64K aligned in size, please see documentation details here.
Most likely, this could be an issue with PlatformIO build system where it is not considering the --secure-pad-v2
argument to elf2image
utility. You might want to check this by reporting an issue at: https://github.com/platformio/platform-espressif32
Even if we ignore the secure padding part, then also ideally the verification part should have succeeded. But, I suspect an issue with secure OTA without secure boot (CONFIG_SECURE_SIGNED_ON_UPDATE_NO_SECURE_BOOT
) and the padding (as described above) use-case here. Please try following fix once, please share verbose enabled logs if you still run into any issues:
--- a/components/bootloader_support/src/esp_image_format.c
+++ b/components/bootloader_support/src/esp_image_format.c
@@ -864,7 +864,7 @@ static esp_err_t verify_secure_boot_signature(bootloader_sha256_handle_t sha_han
bootloader_munmap(simple_hash);
}
-#if CONFIG_SECURE_BOOT_V2_ENABLED
+#if CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME || CONFIG_SECURE_SIGNED_APPS_ECDSA_V2_SCHEME
// End of the image needs to be padded all the way to a 4KB boundary, after the simple hash
// (for apps they are usually already padded due to --secure-pad-v2, only a problem if this option was not used.)
uint32_t padded_end = ALIGN_UP(end, FLASH_SECTOR_SIZE);
Hi @mahavirj.
thank you. I have changed Default log verbosity (Verbose)
and apply the change to esp_image_format
...
bootloader_munmap(simple_hash);
}
#if CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME || CONFIG_SECURE_SIGNED_APPS_ECDSA_V2_SCHEME
// End of the image needs to be padded all the way to a 4KB boundary, after the simple hash
// (for apps they are usually already padded due to --secure-pad-v2, only a problem if this option was not used.)
uint32_t padded_end = ALIGN_UP(end, FLASH_SECTOR_SIZE);
if (padded_end > end) {
const void *padding = bootloader_mmap(end, padded_end - end);
bootloader_sha256_data(sha_handle, padding, padded_end - end);
bootloader_munmap(padding);
end = padded_end;
}
#endif
...
After that, I compiled both versions again and the update was executed successfully.
Is this the final solution or only a rough fix? Should I apply the padding after the build process manually by èxecuting?
esptool.py --chip ESP32-C3 elf2image firmware.elf --secure-pad-v2
Is this the final solution or only a rough fix?
Mostly that's the fix as I see it, but we will check internally for other use-cases and then update here. So, it's not final version yet.
Should I apply the padding after the build process manually by èxecuting?
Probably this part https://github.com/platformio/platform-espressif32/blob/07820cf6f46d2b0fb564d7c20358dd4c2e1fe255/builder/main.py#L271 needs the relevant update for --secure-pad-v2
. You may note down original elf2image
command then add the argument post build process, that should do it.
Hi @mahavirj,
should I create a pull request with this fix or will you do it complete on your side?
No. I have created an internal MR, this issue will be auto-closed when the fix reaches to GH.
Answers checklist.
General issue report
I´m using an ESP32-C3 with ESP-IDF 5.0.2 in PlatformIO:
I have two firmware versions that only differ in version number and I´m using the esp-idf OTA library.
After the build, each image is signed with the same key:
espsecure
confirms that both firmware versions are valid:But the OTA process fails with the following error:
The strange part is:
Is there any difference in the requirements for the OTA backend when using Arduino or esp-idf? What is a possible reason for this issue?
The
sdkconfig
for the esp-idf based release build looks like this