Add README.
[gps-watch.git] / src / bootloader / bootloader.rs
index cf4bca2926638a34be6be5572d9a3a241970f32c..d4a686b5ea83a290f16f4518f251c71a5f3fc03f 100644 (file)
@@ -46,6 +46,7 @@ enum Error {
     UnknownCommand = 1,
     InvalidArgument,
     ChecksumMismatch,
+    PermissionDenied,
 }
 
 extern {
@@ -121,7 +122,7 @@ impl Bootloader {
         }
     }
 
-    pub fn run(&mut self) {
+    pub fn run(&mut self) -> bool {
         if self.command_bytes < 4 {
             if let Some(b) = try_read_u8() {
                 self.command >>= 8;
@@ -131,10 +132,14 @@ impl Bootloader {
             }
         }
 
-        if self.command_bytes == 4 {
+        if self.command_bytes != 4 {
+            true
+        } else {
             self.command_bytes = 0;
 
             self.process_command();
+
+            self.command != COMMAND_START_APP
         }
     }
 
@@ -187,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);