Failsafe upgrades with STM32L476 Dfu
On the STM32L476, we have been using the Dfu program that comes loaded in system memory.
We've been using this ROM code succesfully as a way to load firmware on to the MCU flash over USB, with the documented Dfu file format + DfuSeDemo + DfuSe sample code, etc. So this has been helpful to get up and running with a loader solution over USB.
We're now planning to do field firmware upgrades on the MCU, and I'm running into what seems like a limitation of the Dfu ROM code: If we're in the middle of a Dfu transfer, and something breaks (USB gets disconnected, power glitch, PC crashes, etc), a partially-loaded image will be present on the MCU. Rebooting the MCU after that will essentially brick it, as I have no way to assert the Boot0 pin to go back into the DFU when this device is deployed in the field.
Has anyone run into this and created a 'failsafe' recovery flow for a situation like this? What I'd like is for the MCU to automatically go back into the Dfu after a failure of this type so the upgrade can be restarted, not try to run a corrupted image endlessly. Are there any recommended recovery methods or workarounds? I have read AN2606, AN3156 but I don't see a clear path forward.
The best options I have come up with so far are:
1) Create a 2nd stage loader and package that up with my application code, and have the Dfu always load my 2nd loader+app code. I'm not sure if I could get this approach to not have the same 'failsafe' vulnerability of the ROM Dfu.
2) Stop using the ST ROM Dfu altogether, and write my own bootloader with some failsafe features. Partition the flash to always have this bootloader resident, and only update the application code in the field - not the bootloader. This bootloader would always run first before my upgrade-able application code and always check the application code image integrity before booting it.
Both of these are non-trivial efforts so if anyone can think of anything simpler I would really appreciate it.