]> defiant.homedns.org Git - pyshared.git/commitdiff
bootloader.py: Fix handling of EOF record
authorErik Andresen <erik@vontaene.de>
Tue, 26 Mar 2019 21:28:06 +0000 (22:28 +0100)
committerErik Andresen <erik@vontaene.de>
Tue, 26 Mar 2019 21:28:06 +0000 (22:28 +0100)
bootloader.py

index a23828710b81a6a9c650ac28d7533a40c8bbc735..a077d38db510f687759670608f86662d1b6f87b3 100755 (executable)
@@ -22,7 +22,7 @@ class bootloader:
                self.boot_addr = 0x0
 
                if not self.identify():
-                       raise "Bootloader not running"
+                       raise Exception("Bootloader not running")
                self.pagesize = self.get_pagesize()
        
        def read_mem(self, addr, num):
@@ -36,12 +36,12 @@ class bootloader:
                self.run_cmd(CMD_ERASE_ALL)
 
        def __compare_memarea(self, addr, data):
-                       mem_cmp = self.read_mem(addr, self.pagesize)
-                       if mem_cmp != data:
-                               print "Expected:", data.encode("hex")
-                               print "Got:     ", mem_cmp.encode("hex")
-                               raise("Compare mismatch at 0x%x" % addr)
-                       return
+               mem_cmp = self.read_mem(addr, self.pagesize)
+               if mem_cmp != data:
+                       print "Expected:", data.encode("hex")
+                       print "Got:     ", mem_cmp.encode("hex")
+                       raise Exception("Compare mismatch at 0x%x" % addr)
+               return
        
        def __program_memarea(self, addr, data):
                self.run_cmd(CMD_WRITE, addr, self.pagesize, data)
@@ -62,7 +62,7 @@ class bootloader:
                count=0
                for line in f:
                        if line[0] != ':':
-                               raise("Bad line start character")
+                               raise Exception("Bad line start character")
                        hex = line[1:].replace("\r\n", "")
                        data = hex.decode("hex")
                        num = ord(data[0])
@@ -70,14 +70,14 @@ class bootloader:
                        for c in data:
                                chksum+=ord(c)
                        if chksum % 256 != 0:
-                               raise("Checksum error")
+                               raise Exception("Checksum error")
                        addr, typ, data, chksum = struct.unpack(">HB%ssB" % num, data[1:])
 
                        if typ == 0: # Data Record
                                count+=len(data)
                                if next_addr is not None:
                                        if next_addr != addr:
-                                               raise "Gap in file"
+                                               raise Exception("Gap in file")
                                buf_addr = addr-len(buf)
                                buf+=data
                                if len(buf) >= self.pagesize:
@@ -91,17 +91,19 @@ class bootloader:
                        elif typ == 3: # Start Segment Address Record
                                self.boot_addr = int(data.encode("hex"), 16)
                        elif typ == 1: # End of File Record
-                               print "Addr (rest) 0x%x" % buf_addr
-                               diff = self.pagesize-len(buf)
-                               buf+=chr(0xff)*diff # fill with 0xff
-                               handle(buf_addr, buf[:self.pagesize])
-                               if lFirstRow: # was first
-                                       buf_addr = lFirstRow[0]
-                                       buf = lFirstRow[1]
-                                       print "Addr (First) 0x%x" % buf_addr
-                                       handle(buf_addr, buf)
+                               if len(buf) > 0:
+                                       # Send unhandled data
+                                       print "Addr (rest) 0x%x" % buf_addr
+                                       diff = self.pagesize-len(buf)
+                                       buf+=chr(0xff)*diff # fill with 0xff
+                                       handle(buf_addr, buf[:self.pagesize])
+                                       if lFirstRow: # was first
+                                               buf_addr = lFirstRow[0]
+                                               buf = lFirstRow[1]
+                                               print "Addr (First) 0x%x" % buf_addr
+                                               handle(buf_addr, buf)
                        else:
-                               raise("Unknown type %d" % typ)
+                               raise Exception("Unknown type %d" % typ)
                        
                        next_addr = addr+num
                print "Byte count:", count
@@ -161,7 +163,7 @@ class bootloader:
                s = self.read(1)
                i = ord(s[0])
                if i not in [64, 128]:
-                       raise
+                       raise Exception("Unsupported pagesize")
                return i