tools: Add support for COMMAND_START_APP.
authorTilman Sauerbeck <tilman@code-monkey.de>
Sun, 7 Jul 2019 12:21:37 +0000 (14:21 +0200)
committerTilman Sauerbeck <tilman@code-monkey.de>
Sun, 5 Jan 2020 19:38:11 +0000 (20:38 +0100)
May be combined with the write or verify commands, but it can also
be used on its own.

tools/update-firmware

index 76002935f8f6d030f001f1d295015e82acd3261c..32381dc47393ac39b32d4819d5f0ddbb54214485 100755 (executable)
@@ -121,6 +121,10 @@ class LoadChunkCommand(Command):
     def __init__(self):
         super(LoadChunkCommand, self).__init__(0x1b329768, 'LOAD_CHUNK')
 
+class StartAppCommand(Command):
+    def __init__(self):
+        super(StartAppCommand, self).__init__(0xd27df1bf, 'START_APP')
+
 class Application:
     SECTOR_SIZE = 1024
     MAX_NUM_CHUNKS = 256
@@ -136,6 +140,7 @@ class Application:
 
         self._do_write = args.write
         self._do_verify = args.verify
+        self._do_start = args.start
 
         self._filename = args.filename
         self._offset = args.offset
@@ -143,20 +148,25 @@ class Application:
     def run(self):
         chunks = []
 
-        with open(self._filename, 'rb') as f:
-            while True:
-                chunk = f.read(Application.SECTOR_SIZE)
+        if self._filename:
+            with open(self._filename, 'rb') as f:
+                while True:
+                    chunk = f.read(Application.SECTOR_SIZE)
 
-                if len(chunk) == 0:
-                    break
+                    if len(chunk) == 0:
+                        break
 
-                chunks.append(chunk)
+                    chunks.append(chunk)
 
-        if len(chunks) > Application.MAX_NUM_CHUNKS:
-            sys.stderr.write('File too large.\n')
-            return 3
+            if len(chunks) > Application.MAX_NUM_CHUNKS:
+                sys.stderr.write('File too large.\n')
+                return 3
 
-        sector0 = self._offset // Application.SECTOR_SIZE
+        # Defaulting to zero seems too dangerous:
+        if self._offset is None:
+            sector0 = None
+        else:
+            sector0 = self._offset // Application.SECTOR_SIZE
 
         try:
             return self._run(sector0, chunks)
@@ -192,6 +202,9 @@ class Application:
 
                     num_verify_errors += 1
 
+        if self._do_start and num_verify_errors == 0:
+            self._start_app()
+
         if num_verify_errors == 0:
             return 0
         else:
@@ -255,6 +268,15 @@ class Application:
 
         return False
 
+    def _start_app(self):
+        self._log(0, 'Starting application... ')
+
+        self._prepare_command(StartAppCommand())
+        self._serial.flush()
+        self._read_and_handle_error()
+
+        self._log(0, 'OK\n')
+
     def _prepare_command(self, command):
         self._last_command = command
 
@@ -297,18 +319,19 @@ if __name__ == '__main__':
     parser = argparse.ArgumentParser(description='Flash GPS watch firmware')
 
     parser.add_argument('-d', '--device', type=str, required=True)
-    parser.add_argument('-o', '--offset', type=offset, required=True)
+    parser.add_argument('-o', '--offset', type=offset, default=None)
     parser.add_argument('-n', '--dry-run', action='store_true')
     parser.add_argument('-l', '--log-level', type=int, default=0)
     parser.add_argument('-w', '--write', action='store_true')
     parser.add_argument('-v', '--verify', action='store_true')
+    parser.add_argument('-s', '--start', action='store_true')
     parser.add_argument('filename', type=str, nargs='?', default=None)
 
     args = parser.parse_args()
 
-    if not args.write and not args.verify:
+    if not args.write and not args.verify and not args.start:
         parser.error('one or more of the following arguments are required: ' +
-                     '-w/--write, -v/--verify')
+                     '-w/--write, -v/--verify, -s/--start')
 
     if args.filename is None:
         if args.write:
@@ -316,4 +339,8 @@ if __name__ == '__main__':
         elif args.verify:
             parser.error('argument -v/--verify: expected one argument')
 
+    if args.offset is None:
+        if args.write or args.verify:
+            parser.error('the following arguments are required: -o/--offset')
+
     sys.exit(Application(args).run())