]> git.proxmox.com Git - mirror_qemu.git/blame - scripts/signrom.py
Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into staging
[mirror_qemu.git] / scripts / signrom.py
CommitLineData
b38c2458
PB
1#!/usr/bin/env python3
2
0d6b9cc7
JK
3#
4# Option ROM signing utility
5#
6# Authors:
7# Jan Kiszka <jan.kiszka@siemens.com>
8#
9# This work is licensed under the terms of the GNU GPL, version 2 or later.
10# See the COPYING file in the top-level directory.
11
12import sys
13import struct
14
15if len(sys.argv) < 3:
16 print('usage: signrom.py input output')
17 sys.exit(1)
18
19fin = open(sys.argv[1], 'rb')
20fout = open(sys.argv[2], 'wb')
21
fd28938b 22magic = fin.read(2)
31d8f92e 23if magic != b'\x55\xaa':
fd28938b
RJ
24 sys.exit("%s: option ROM does not begin with magic 55 aa" % sys.argv[1])
25
6f71b779 26size_byte = ord(fin.read(1))
0d6b9cc7 27fin.seek(0)
7f256924 28data = fin.read()
6f71b779 29
7f256924
PB
30size = size_byte * 512
31if len(data) > size:
32 sys.stderr.write('error: ROM is too large (%d > %d)\n' % (len(data), size))
33 sys.exit(1)
34elif len(data) < size:
35 # Add padding if necessary, rounding the whole input to a multiple of
36 # 512 bytes according to the third byte of the input.
6f71b779 37 # size-1 because a final byte is added below to store the checksum.
31d8f92e 38 data = data.ljust(size-1, b'\0')
6f71b779 39else:
7f256924
PB
40 if ord(data[-1:]) != 0:
41 sys.stderr.write('WARNING: ROM includes nonzero checksum\n')
42 data = data[:size-1]
6f71b779 43
0d6b9cc7
JK
44fout.write(data)
45
46checksum = 0
47for b in data:
b38c2458
PB
48 checksum = (checksum - b) & 255
49
0d6b9cc7
JK
50fout.write(struct.pack('B', checksum))
51
52fin.close()
53fout.close()