jacobrosenthal / hf2-rs

Microsoft HF2 HID Flashing Format for UF2 Bootloaders
49 stars 14 forks source link

Flashing my feather_m0 fails with ContentsDifferent #28

Closed TDHolmes closed 3 years ago

TDHolmes commented 3 years ago

I recently jumped back into an embedded project after not touching it for a few months and once I tried flashing it once, it no longer exited the boot loader. I thought I should probably update cargo-hf2, which looked to be a good idea because I was pretty out of date (Replaced packagecargo-hf2 v0.1.1withcargo-hf2 v0.3.0(executablecargo-hf2)).

Before, flashing would "succeed" without complaint, but after the update, it now fails with reason ContentsDifferent:

$ RUST_BACKTRACE=1 cargo hf2 --release
    Finished release [optimized + debuginfo] target(s) in 0.03s
    Searching for a connected device with known vid/pid pair.
    Trying  Ok(Some("Adafruit Industries")) Ok(Some("Feather M0"))
    Flashing "/Users/tholmes/projects/rust/oven-temp-rs/target/thumbv6m-none-eabi/release/oven-temp-rs"
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: ContentsDifferent', /Users/tholmes/.cargo/registry/src/github.com-1ecc6299db9ec823/cargo-hf2-0.3.0/src/main.rs:118:47
stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::result::unwrap_failed
   3: cargo_hf2::main
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

How can I debug what's going wrong here?

My boot loader info is:

UF2 Bootloader v3.10.0 SFHWRO
Model: Feather M0
Board-ID: SAMD21G18A-Feather-v0

Thanks for an awesome tool!

TDHolmes commented 3 years ago

This is happening on latest stable & nightly

rustc 1.47.0 (18bf6b4f0 2020-10-07)
 rustc 1.49.0-nightly (38d911dfc 2020-10-09)
jacobrosenthal commented 3 years ago

gah. Sorry. Some variant of this keeps popping up

First off. Does the flashed image work?

add RUST_LOG=debug for a ton of logging

You can go in and print the verify and binary and see which bytes wont conform.

I feel like this is always around the binary gathering path and have touched that several times in the past releases id look there https://github.com/jacobrosenthal/hf2-rs/blob/master/hf2/src/utils/mod.rs#L42

I can try to help more in a day or two as well

TDHolmes commented 3 years ago

That's the weird thing, both on old cargo hf2 and latest, it doesn't seem to leave the boot loader after flashing. This was working a few months back.

TDHolmes commented 3 years ago

I think I might have tried updating bootloaders a while back and haven't tried since... I'm wondering if I did something stupid. If I use uf2conv-rs to convert a binary to uf2 format and manually drag, I can make it so I'm no longer stuck in the bootloader on reset, but it seems bricked and non-functional.

jacobrosenthal commented 3 years ago

Nah more likely I changed that code path. Assuming you can still drop a uf2 on the mass storage drive the device is fine

On Sat, Oct 10, 2020, 8:26 PM Tyler Holmes notifications@github.com wrote:

I think I might have tried updating bootloaders a while back and haven't tried since... I'm wondering if I did something stupid. If I use uf2conv-rs to convert a binary to uf2 format and manually drag, I can make it so I'm no longer stuck in the bootloader on reset, but it seems bricked and non-functional.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/jacobrosenthal/hf2-rs/issues/28#issuecomment-706643913, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADPI5D2WAE2JLBVC3QVZZTSKEQVVANCNFSM4SLLPP6A .

twitchyliquid64 commented 3 years ago

I just hit this today - no idea why it started happening. My board is only a week old, and I never played with the bootloader.

twitchyliquid64 commented 3 years ago

Debug logs: https://gist.github.com/twitchyliquid64/0f787ddfa33cef65f42439ae7621286f

twitchyliquid64 commented 3 years ago

Flashing is successful using bossac:

[xxx@xxx]:~/projects/wio-terminal> cargo build --release --example usb_serial_display
   Compiling wio-terminal v0.1.0 (/home/xxx/projects/wio-terminal)
    Finished release [optimized] target(s) in 1.12s
[xxx@xxx]:~/projects/wio-terminal> arm-none-eabi-objcopy -O binary \
                target/thumbv7em-none-eabihf/release/examples/usb_serial_display usb.bin
[xxx@xxx]:~/projects/wio-terminal> bossac -p ttyACM0 -e -w -v --offset=0x4000 usb.bin 
Erase flash

Done in 2.943 seconds
Write 21956 bytes to flash (43 pages)
[==============================] 100% (43/43 pages)
Done in 0.130 seconds
Verify 21956 bytes of flash
[==============================] 100% (43/43 pages)
Verify successful
Done in 0.465 seconds

I recently updated rust from 1.44 to 1.47.0. Could this be a regression?

twitchyliquid64 commented 3 years ago

Confirmed that this works with rust 1.44.0:

[xxx@xxx]:~/projects/wio-terminal> rustup toolchain install 1.44.0
[xxx@xxx]:~/projects/wio-terminal> rustup default 1.44.0-x86_64-unknown-linux-gnu
info: using existing install for '1.44.0-x86_64-unknown-linux-gnu'
info: default toolchain set to '1.44.0-x86_64-unknown-linux-gnu'

  1.44.0-x86_64-unknown-linux-gnu unchanged - rustc 1.44.0 (49cae5576 2020-06-01)

[xxx@xxx]:~/projects/wio-terminal> rustup target add thumbv7em-none-eabihf
info: downloading component 'rust-std' for 'thumbv7em-none-eabihf'
info: installing component 'rust-std' for 'thumbv7em-none-eabihf'
info: Defaulting to 500.0 MiB unpack ram

[xxx@xxx]:~/projects/wio-terminal> cargo hf2 --release --example=usb_serial_display --vid 0x2886 --pid 0x002d
    Finished release [optimized] target(s) in 0.04s
    Trying  Ok(Some("Seeed Studio")) Ok(Some("Wio Terminal"))
    Flashing "/home/xxx/projects/wio-terminal/target/thumbv7em-none-eabihf/release/examples/usb_serial_display"
    Finished in 0.765s

But not stable (1.47.0):

[xxx@xxx]:~/projects/wio-terminal> rustup default stable
info: using existing install for 'stable-x86_64-unknown-linux-gnu'
info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'

  stable-x86_64-unknown-linux-gnu unchanged - rustc 1.47.0 (18bf6b4f0 2020-10-07)

[xxx@xxx]:~/projects/wio-terminal> cargo hf2 --release --example=usb_serial_display --vid 0x2886 --pid 0x002d
    Finished release [optimized] target(s) in 0.08s
    Trying  Ok(Some("Seeed Studio")) Ok(Some("Wio Terminal"))
    Flashing "/home/xxx/projects/wio-terminal/target/thumbv7em-none-eabihf/release/examples/usb_serial_display"
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: ContentsDifferent', /home/xxx/.cargo/registry/src/github.com-1ecc6299db9ec823/cargo-hf2-0.3.0/src/main.rs:118:47
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
jacobrosenthal commented 3 years ago

Hell. can you diff the RUST_LOG=debug logs for both maybe?

jacobrosenthal commented 3 years ago

You could also post the elfs of both builds, and we can look at the binary sections of both. I still think its a difference in the extracted binaries between the two

jacobrosenthal commented 3 years ago

Also Im presuming youre doing both the cargo build and the cargo install on the specific rust version? If not trying pull apart those two would be good data, install hf2 on the old version, but switch back to rust stable when you cargo build --and vice versa.

twitchyliquid64 commented 3 years ago

I'll try and get you the ELF and a log diff sometime today or tomorrow.

The interesting thing was that simply installing Rust version 1.47 (without re-installing cargo-hf2) was enough to trigger the issue, which makes me guess that maybe a slight difference in the ELF format is triggering a bug?

TDHolmes commented 3 years ago

Confirmed this is fixed for me too Thanks @twitchyliquid64 & @jacobrosenthal!