Updated EZ‐TUNE (markdown)
[inav.wiki.git] / Firmware-recovery-with-dfu‐util.md
blob32a60ac9ed992067e462baf829bb0eebafd064d7
1 # Firmware Recovery
3 ## Overview
5 It is possible to recover firmware from a flight controller using the open source [dfu-util](https://sourceforge.net/projects/dfu-util/) application. This is available via the package manager for most Linux distros / FreeBSD and from the quoted URI for other platforms.
7 Note that you will need to either set device permission or run `dfu-util` as root on POSIX platforms. On Linux systems with `systemd`, the following `udev` rule may help:
9 ```
10 SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="df11", MODE="0660", TAG+="uaccess"
11 ```
13 Note: It is also necessary to set the FC into `dfu` (aka bootloader) mode before running `dfu-util`.
15 ## Hex and Bin files
17 The INAV build process typically generates `hex` files (these are encoded ASCII text), whereas `dfu-util` requires binary (`bin`) files. The `gcc` tool `objcopy` can convert between `hex` and `bin` if required.
19 ```
20 # hex to bin
21 objcopy -I ihex file.hex -O binary file.bin
22 # bin to hex
23 objcopy -I binary file.bin -O ihex file.hex
24 ```
26 Note: You can build `bin` files directly from source `ninja TARGET.bin` (vice `ninja TARGET` for a `hex` file).
28 ### Recovering flash
30 In order to recover FC flash, you need to know the firmware `bin` file size. There is no penalty for specifying to large a size (within the flash capacity), so using the known flash size or the extant INAV firmware `bin` size (with some overhead) will work.
32 ### Recovery Example
34 To recover the factory firmware from a F722. The physical flash size is 512KB and the INAV 8 firmware for this (out of tree) device is `499479` bytes:
35 ```
36 $ ls -l /tmp/inav_8.0.0_WARPF7.bin
37 -rw-r--r-- 1 jrh jrh 499479 Nov 17 19:48 /tmp/inav_8.0.0_WARPF7.bin
38 ```
39 So I'll use 512000 bytes in the example, as that should be safe; replace `512000` with the size your device requires: 
40 ```
42 # First, but the device in DFU Mode (button, cli command)
43 # then
44 $ dfu-util  -a 0 -s 0x08000000:512000 -U /tmp/oldflash.bin
45 dfu-util 0.11
47 Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc.
48 Copyright 2010-2021 Tormod Volden and Stefan Schmidt
49 This program is Free Software and has ABSOLUTELY NO WARRANTY
50 Please report bugs to http://sourceforge.net/p/dfu-util/tickets/
52 Opening DFU capable USB device...
53 Device ID 0483:df11
54 Device DFU version 011a
55 Claiming USB DFU Interface...
56 Setting Alternate Interface #0 ...
57 Determining device status...
58 DFU state(2) = dfuIDLE, status(0) = No error condition is present
59 DFU mode device DFU version 011a
60 Device returned transfer size 2048
61 DfuSe interface name: "Internal Flash  "
62 Upload    [=========================] 100%       512000 bytes
63 Upload done.
64 ```
66 ### Reinstall the recovered firmware
68 Having got the firmware `/tmp/oldflash.bin`, it is possible to reflash this recovered firmware.
70 ```
72 # First, but the device in DFU Mode (button, cli command)
73 # then
74 $ dfu-util -d 0483:df11 --alt 0 -s 0x08000000:force:leave -D /tmp/oldflash.bin
75 dfu-util 0.11
77 Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc.
78 Copyright 2010-2021 Tormod Volden and Stefan Schmidt
79 This program is Free Software and has ABSOLUTELY NO WARRANTY
80 Please report bugs to http://sourceforge.net/p/dfu-util/tickets/
82 dfu-util: Warning: Invalid DFU suffix signature
83 dfu-util: A valid DFU suffix will be required in a future dfu-util release
84 Opening DFU capable USB device...
85 Device ID 0483:df11
86 Device DFU version 011a
87 Claiming USB DFU Interface...
88 Setting Alternate Interface #0 ...
89 Determining device status...
90 DFU state(2) = dfuIDLE, status(0) = No error condition is present
91 DFU mode device DFU version 011a
92 Device returned transfer size 2048
93 DfuSe interface name: "Internal Flash  "
94 Downloading element to address = 0x08000000, size = 512000
95 Erase       [=========================] 100%       512000 bytes
96 Erase    done.
97 Download    [=========================] 100%       512000 bytes
98 Download done.
99 File downloaded successfully
100 Submitting leave request...
101 Transitioning to dfuMANIFEST state
104 When the FC has rebooted, let's see (you can just as easily use `INAV Configurator` or `mwp` or other tool), here I use mwp's `cliterm` as it's quick and easy:
107 $ cliterm
108 2024-11-17T19:58:47+0000 Registered serial device: /dev/ttyACM0 [0483:5740], Vendor: INAV, Model: STM32_Virtual_ComPort_in_FS_Mode, Serial: 2065345E4847, Driver: cdc_acm
109 opening  /dev/ttyACM0 ...
111 Entering CLI Mode, type 'exit' to return, or 'help'
113 # version
114 # INAV/WARPF7 8.0.0 Nov 16 2024 / 18:35:05 (7c0fe517)
115 # GCC-14.1.0
118 Looks like that worked.