]>
Commit | Line | Data |
---|---|---|
f03868bd | 1 | from __future__ import print_function |
0d6b9cc7 JK |
2 | # |
3 | # Option ROM signing utility | |
4 | # | |
5 | # Authors: | |
6 | # Jan Kiszka <jan.kiszka@siemens.com> | |
7 | # | |
8 | # This work is licensed under the terms of the GNU GPL, version 2 or later. | |
9 | # See the COPYING file in the top-level directory. | |
10 | ||
11 | import sys | |
12 | import struct | |
13 | ||
14 | if len(sys.argv) < 3: | |
15 | print('usage: signrom.py input output') | |
16 | sys.exit(1) | |
17 | ||
18 | fin = open(sys.argv[1], 'rb') | |
19 | fout = open(sys.argv[2], 'wb') | |
20 | ||
fd28938b | 21 | magic = fin.read(2) |
31d8f92e | 22 | if magic != b'\x55\xaa': |
fd28938b RJ |
23 | sys.exit("%s: option ROM does not begin with magic 55 aa" % sys.argv[1]) |
24 | ||
6f71b779 | 25 | size_byte = ord(fin.read(1)) |
0d6b9cc7 | 26 | fin.seek(0) |
7f256924 | 27 | data = fin.read() |
6f71b779 | 28 | |
7f256924 PB |
29 | size = size_byte * 512 |
30 | if len(data) > size: | |
31 | sys.stderr.write('error: ROM is too large (%d > %d)\n' % (len(data), size)) | |
32 | sys.exit(1) | |
33 | elif len(data) < size: | |
34 | # Add padding if necessary, rounding the whole input to a multiple of | |
35 | # 512 bytes according to the third byte of the input. | |
6f71b779 | 36 | # size-1 because a final byte is added below to store the checksum. |
31d8f92e | 37 | data = data.ljust(size-1, b'\0') |
6f71b779 | 38 | else: |
7f256924 PB |
39 | if ord(data[-1:]) != 0: |
40 | sys.stderr.write('WARNING: ROM includes nonzero checksum\n') | |
41 | data = data[:size-1] | |
6f71b779 | 42 | |
0d6b9cc7 JK |
43 | fout.write(data) |
44 | ||
45 | checksum = 0 | |
46 | for b in data: | |
47 | # catch Python 2 vs. 3 differences | |
48 | if isinstance(b, int): | |
49 | checksum += b | |
50 | else: | |
51 | checksum += ord(b) | |
52 | checksum = (256 - checksum) % 256 | |
53 | ||
54 | # Python 3 no longer allows chr(checksum) | |
55 | fout.write(struct.pack('B', checksum)) | |
56 | ||
57 | fin.close() | |
58 | fout.close() |