Skip to content

Commit

Permalink
fix a few Python3 migration oversights in bootloader and intelhex
Browse files Browse the repository at this point in the history
  • Loading branch information
atlas0fd00m committed Aug 11, 2022
1 parent 08aceea commit 3278733
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.9.5
1.9.6
7 changes: 4 additions & 3 deletions firmware/bootloader_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
ser = int(sys.argv[1])
else:
try:
ser = int(file(".serial", 'rb').read(), 16) + 1
except IOError:
serbytes = open(".serial", 'rb').read()
ser = int(serbytes, 16) + 1
except (IOError, ValueError):
ser = 0

print(("[--- new serial number: %.4x ---]" % ser), file=sys.stderr)

if WRITEBACK:
file(".serial", 'wb').write("%.13x" % ser)
open(".serial", 'wb').write(b"%.13x" % ser)

sertxt = ''
sertmp = "%.13x" % ser
Expand Down
22 changes: 11 additions & 11 deletions rflib/intelhex.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,13 +507,13 @@ def write_hex_file(self, f, write_start_addr=True):
# timeit shows that using hexstr.translate(table)
# is faster than hexstr.upper():
# 0.452ms vs. 0.652ms (translate vs. upper)
table = ''.join(correctbytes(i).upper() for i in range(256))
table = b''.join(correctbytes(i).upper() for i in range(256))

# start address record if any
if self.start_addr and write_start_addr:
keys = list(self.start_addr.keys())
keys.sort()
bin = array('B', '\0'*9)
bin = array('B', b'\0'*9)
if keys == ['CS','IP']:
# Start Segment Address Record
bin[0] = 4 # reclen
Expand All @@ -527,7 +527,7 @@ def write_hex_file(self, f, write_start_addr=True):
bin[6] = (ip >> 8) & 0x0FF
bin[7] = ip & 0x0FF
bin[8] = (-sum(bin)) & 0x0FF # chksum
fwrite(':' + hexlify(bin.tostring()).translate(table) + '\n')
fwrite(b':' + hexlify(bin.tostring()).translate(table) + b'\n')
elif keys == ['EIP']:
# Start Linear Address Record
bin[0] = 4 # reclen
Expand All @@ -540,7 +540,7 @@ def write_hex_file(self, f, write_start_addr=True):
bin[6] = (eip >> 8) & 0x0FF
bin[7] = eip & 0x0FF
bin[8] = (-sum(bin)) & 0x0FF # chksum
fwrite(':' + hexlify(bin.tostring()).translate(table) + '\n')
fwrite(b':' + hexlify(bin.tostring()).translate(table) + b'\n')
else:
if fclose:
fclose()
Expand All @@ -565,7 +565,7 @@ def write_hex_file(self, f, write_start_addr=True):

while cur_addr <= maxaddr:
if need_offset_record:
bin = array('B', '\0'*7)
bin = array('B', b'\0'*7)
bin[0] = 2 # reclen
bin[1] = 0 # offset msb
bin[2] = 0 # offset lsb
Expand All @@ -575,7 +575,7 @@ def write_hex_file(self, f, write_start_addr=True):
bin[4] = bytes[0] # msb of high_ofs
bin[5] = bytes[1] # lsb of high_ofs
bin[6] = (-sum(bin)) & 0x0FF # chksum
fwrite(':' + hexlify(bin.tostring()).translate(table) + '\n')
fwrite(b':' + hexlify(bin.tostring()).translate(table) + b'\n')

while True:
# produce one record
Expand All @@ -597,7 +597,7 @@ def write_hex_file(self, f, write_start_addr=True):
else:
chain_len = 1 # real chain_len

bin = array('B', '\0'*(5+chain_len))
bin = array('B', b'\0'*(5+chain_len))
bytes = divmod(low_addr, 256)
bin[1] = bytes[0] # msb of low_addr
bin[2] = bytes[1] # lsb of low_addr
Expand All @@ -611,7 +611,7 @@ def write_hex_file(self, f, write_start_addr=True):
bin = bin[:5+i]
bin[0] = chain_len
bin[4+chain_len] = (-sum(bin)) & 0x0FF # chksum
fwrite(':' + hexlify(bin.tostring()).translate(table) + '\n')
fwrite(':' + hexlify(bin.tostring()).translate(table).decode('latin1') + '\n')

# adjust cur_addr/cur_ix
cur_ix += chain_len
Expand Down Expand Up @@ -647,7 +647,7 @@ def gets(self, addr, length):
"""Get string of bytes from given address. If any entries are blank
from addr through addr+length, a NotEnoughDataError exception will
be raised. Padding is not used."""
a = array('B', '\0'*length)
a = array('B', b'\0'*length)
try:
for i in range(length):
a[i] = self._buf[addr+i]
Expand All @@ -659,7 +659,7 @@ def puts(self, addr, s):
"""Put string of bytes at given address. Will overwrite any previous
entries.
"""
a = array('B', s)
a = array('B', bytes(s, 'latin1'))
for i in range(len(s)):
self._buf[addr+i] = a[i]

Expand Down Expand Up @@ -943,7 +943,7 @@ def _from_bytes(bytes):
# calculate checksum
s = (-sum(bytes)) & 0x0FF
bin = array('B', bytes + [s])
return ':' + hexlify(bin.tostring()).upper()
return b':' + hexlify(bin.tostring()).upper()
_from_bytes = staticmethod(_from_bytes)

def data(offset, bytes):
Expand Down
2 changes: 1 addition & 1 deletion rflib/rflib_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
RFLIB_VERSION=606
RFLIB_VERSION=624

0 comments on commit 3278733

Please sign in to comment.