2 # Option ROM signing utility
7 # This work is licensed under the terms of the GNU GPL, version 2 or later.
8 # See the COPYING file in the top-level directory.
14 print('usage: signrom.py input output')
17 fin = open(sys.argv[1], 'rb')
18 fout = open(sys.argv[2], 'wb')
21 if magic != '\x55\xaa':
22 sys.exit("%s: option ROM does not begin with magic 55 aa" % sys.argv[1])
24 size_byte = ord(fin.read(1))
28 size = size_byte * 512
30 sys.stderr.write('error: ROM is too large (%d > %d)\n' % (len(data), size))
32 elif len(data) < size:
33 # Add padding if necessary, rounding the whole input to a multiple of
34 # 512 bytes according to the third byte of the input.
35 # size-1 because a final byte is added below to store the checksum.
36 data = data.ljust(size-1, '\0')
38 if ord(data[-1:]) != 0:
39 sys.stderr.write('WARNING: ROM includes nonzero checksum\n')
46 # catch Python 2 vs. 3 differences
47 if isinstance(b, int):
51 checksum = (256 - checksum) % 256
53 # Python 3 no longer allows chr(checksum)
54 fout.write(struct.pack('B', checksum))