Repository: esp-rs/espflash (language: rust)

Issue:
SHA-256 comparison failed: [...] attempting to boot anyway...
When the app image header is updated with new flash configuration values, output like the following can be seen in the bootloader messages:

```text
SHA-256 comparison failed:
Calculated: bf6e7aa7738b60d6b15de2bea35979ca40a8a94224f3c78ef00f19a96becf599
Expected: c279089098b4d648c53538bb1dfba7caa16140817d4be0ee4394fe6967e6813d
Attempting to boot anyway...
```

While the application still executes as expected, we will need to address this. This is not an issue unless you plan to use Secure Boot, which we do not presently support anyways. I have a local branch in-progress but it still needs some work.


Discussion / hints:
I did some investigations for this issue using an `esp-idf` example with `esptool` and `espflash`, here are my initial findings:
If we use: 
```
esptool.py -p /dev/ttyACM0 -b 460800 --before default_reset --after hard_reset --chip esp32c3  write_flash --flash_mode dio --flash_size 2MB --flash_freq 80m 0x0 build/bootloader/bootloader.bin 0x8000 build/partition_table/partition-table.bin 0x10000 build/blink.bin

espflash monitor
```
Or the espflash equivalent:
```
espflash flash -M --port /dev/ttyACM0 -b 460800 --chip esp32c3 --flash-mode dio --flash-size 2mb --flash-freq 80mhz --bootloader build/bootloader/bootloader.bin --partition-table build/partition_table/partition-table.bin build/blink.elf
```
We don't get the comparison failed message. Note that we are using `--flash-mode dio --flash-size 2mb --flash-freq 80mhz ` in both commands.

From `esptool` docs:
![image](https://github.com/esp-rs/espflash/assets/12926049/60fda3a6-87ad-4889-8466-88e01b3de5a1)

In `espflash`, we are not using the same defaults [FlashSettings](https://github.com/esp-rs/espflash/blob/31817fa0112e23911aa0ccac840bf1f6b890a744/espflash/src/flasher/mod.rs#L261-L265), for example, for C3, we use 40MHz as default flash freq (while `esptool` default is 80MHz). If you try to use this freq in `esptool`:
```
esptool.py -p /dev/ttyACM0 -b 460800 --before default_reset --after hard_reset --chip esp32c3  write_flash --flash_mode dio --flash_size 2MB --flash_freq 40m 0x0 build/bootloader/bootloader.bin 0x8000 build/partition_table/partition-table.bin 0x10000 build/blink.bin
```
We get the following warning:
```
Warning: Image file at 0x0 is protected with a hash checksum, so not changing the flash frequency setting. Use the --flash_frequency=keep option instead of --flash_frequency=40m in order to remove this warning, or use the --dont-append-digest option for the elf2image command in order to generate an image file without a hash checksum
```

Possible workarounds:
- Print a warning to the user when he is not using the defaults `FlashSettings`. Possibly also update default values to avoid the issue if we dont modify any `FlashSettings` param.
- Create a `--dont-append-digest` option for `save-image` arg and print a similar warning as `esptool`.

We can do one or both workarounds, any input or suggestions on how to proceed @esp-rs/espressif ?
related info maybe -> general description from [esp-idf](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/app_image_format.html)

in esp-idf-sys we provide a macro to override the app_description via a weak link into the app binary [here](https://github.com/esp-rs/esp-idf-sys/blob/master/src/app_desc.rs), but on default its filled by the esp-idf build system
I _think_ I have something .... will create a PR soonish

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6a97ef2e..ccdb5dbf 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 - Fixed `defmt` parsing when data is read in parts (#503)
 - Use partition table instead of hard-coded values for the location of partitions (#516)
 - Fixed a missed `flush` call that may be causing communication errors (#521)
+- Fix "SHA-256 comparison failed: [...] attempting to boot anyway..." (#567)
 
 ### Changed
 - Created `FlashData`, `FlashDataBuilder` and `FlashSettings` structs to reduce number of input arguments in some functions (#512, #566)
diff --git a/espflash/src/image_format/idf_bootloader.rs b/espflash/src/image_format/idf_bootloader.rs
index 4d671844..3f654f54 100644
--- a/espflash/src/image_format/idf_bootloader.rs
+++ b/espflash/src/image_format/idf_bootloader.rs
@@ -73,6 +73,13 @@ impl<'a> IdfBootloaderFormat<'a> {
             bytes_of(&header).iter().copied(),
         );
 
+        // re-calculate hash of the bootloader - needed since we modified the header
+        let bootloader_len = bootloader.len();
+        let mut hasher = Sha256::new();
+        hasher.update(&bootloader[..bootloader_len - 32]);
+        let hash = hasher.finalize();
+        bootloader.to_mut()[bootloader_len - 32..].copy_from_slice(&hash);
+
         // write the header of the app
         // use the same settings as the bootloader
         // just update the entry point


Test patch: