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 size_byte = ord(fin.read(1))
25 # If the caller left the size field blank then we will fill it in,
26 # also rounding the whole input to a multiple of 512 bytes.
28 # +1 because we need a byte to store the checksum.
30 # Round up to next multiple of 512.
34 sys.exit("%s: option ROM size too large" % sys.argv[1])
35 # size-1 because a final byte is added below to store the checksum.
36 data = data.ljust(size-1, '\0')
37 data = data[:2] + chr(size/512) + data[3:]
39 # Otherwise the input file specifies the size so use it.
40 # -1 because we overwrite the last byte of the file with the checksum.
41 size = size_byte * 512 - 1
48 # catch Python 2 vs. 3 differences
49 if isinstance(b, int):
53 checksum = (256 - checksum) % 256
55 # Python 3 no longer allows chr(checksum)
56 fout.write(struct.pack('B', checksum))