Avoid using chr() as this produces unicode strings on python3. Make sure to only use ord() on slices as the python3 bytearray type returns an integer on a non-slice array access.
Signed-off-by: Kevin O'Connor kevin@koconnor.net --- scripts/buildrom.py | 18 +++++++++++------- scripts/checkrom.py | 20 ++++++++------------ 2 files changed, 19 insertions(+), 19 deletions(-)
diff --git a/scripts/buildrom.py b/scripts/buildrom.py index 8ff60e2..0499049 100755 --- a/scripts/buildrom.py +++ b/scripts/buildrom.py @@ -5,7 +5,7 @@ # # This file may be distributed under the terms of the GNU GPLv3 license.
-import sys +import sys, struct
from python23compat import as_bytes
@@ -14,8 +14,11 @@ def alignpos(pos, alignbytes): return (pos + mask) & ~mask
def checksum(data): - ords = map(ord, data) - return sum(ords) + if (sys.version_info > (3, 0)): + cksum = sum(data) + else: + cksum = sum(map(ord, data)) + return struct.pack('<B', (0x100 - cksum) & 0xff)
def main(): inname = sys.argv[1] @@ -34,14 +37,15 @@ def main(): # Check if a pci header is present pcidata = ord(data[24:25]) + (ord(data[25:26]) << 8) if pcidata != 0: - data = data[:pcidata + 16] + chr(int(count/512)) + chr(0) + data[pcidata + 18:] + blocks = struct.pack('<H', int(count/512)) + data = data[:pcidata + 16] + blocks + data[pcidata + 18:]
# Fill in size field; clear checksum field - data = data[:2] + chr(int(count/512)) + data[3:6] + as_bytes("\0") + data[7:] + blocks = struct.pack('<B', int(count/512)) + data = data[:2] + blocks + data[3:6] + as_bytes("\0") + data[7:]
# Checksum rom - newsum = (256 - checksum(data)) & 0xff - data = data[:6] + chr(newsum) + data[7:] + data = data[:6] + checksum(data) + data[7:]
# Write new rom f = open(outname, 'wb') diff --git a/scripts/checkrom.py b/scripts/checkrom.py index 83d4671..377277d 100755 --- a/scripts/checkrom.py +++ b/scripts/checkrom.py @@ -5,8 +5,8 @@ # # This file may be distributed under the terms of the GNU GPLv3 license.
-import sys -import layoutrom +import sys, struct +import layoutrom, buildrom
from python23compat import as_bytes
@@ -14,12 +14,8 @@ def subst(data, offset, new): return data[:offset] + new + data[offset + len(new):]
def checksum(data, start, size, csum): - sumbyte = 0 - while size: - sumbyte = sumbyte + ord(data[start + size - 1]) - size = size - 1 - sumbyte = (0x100 - sumbyte) & 0xff - return subst(data, start+csum, chr(sumbyte)) + sumbyte = buildrom.checksum(data[start:start+size]) + return subst(data, start+csum, sumbyte)
def main(): # Get args @@ -76,11 +72,11 @@ def main():
tableofs = symbols['csm_compat_table'].offset - symbols['code32flat_start'].offset entry_addr = symbols['entry_csm'].offset - layoutrom.BUILD_BIOS_ADDR - byte1 = chr(entry_addr & 0xff) - byte2 = chr(entry_addr >> 8) - rawdata = subst(rawdata, tableofs+ENTRY_FIELD_OFS, byte1+byte2) + entry_addr = struct.pack('<H', entry_addr) + rawdata = subst(rawdata, tableofs+ENTRY_FIELD_OFS, entry_addr)
- tablesize = ord(rawdata[tableofs+SIZE_FIELD_OFS]) + tsfield = tableofs+SIZE_FIELD_OFS + tablesize = ord(rawdata[tsfield:tsfield+1]) rawdata = checksum(rawdata, tableofs, tablesize, CSUM_FIELD_OFS)
# Print statistics