bootloader: Refuse to erase the second flash sector.
authorTilman Sauerbeck <tilman@code-monkey.de>
Sun, 7 Jul 2019 20:05:03 +0000 (22:05 +0200)
committerTilman Sauerbeck <tilman@code-monkey.de>
Sun, 5 Jan 2020 19:38:11 +0000 (20:38 +0100)
The second flash sector (address 0x400) stores the flash configuration
field -- resetting the MCU with 0xff stored in there bricks the device.

src/bootloader/bootloader.rs

index 311b1a60e6a2ed491474eecf0d986c10d58d54a3..d4a686b5ea83a290f16f4518f251c71a5f3fc03f 100644 (file)
@@ -46,6 +46,7 @@ enum Error {
     UnknownCommand = 1,
     InvalidArgument,
     ChecksumMismatch,
+    PermissionDenied,
 }
 
 extern {
@@ -191,6 +192,11 @@ impl Bootloader {
 
         if sector > 0xff {
             Err(Error::InvalidArgument)
+        } else if sector == 0x01 {
+            // The second sector contains the flash configuration field,
+            // and keeping it in the erased state has the potential
+            // to brick the device.
+            Err(Error::PermissionDenied)
         } else {
             flash::erase(sector);