blob: 9be5dab1cfb12ccbc70081fe171531b858e0d478 [file] [log] [blame]
Paolo Bonzinib38c2452020-02-04 17:00:28 +01001#!/usr/bin/env python3
2
Eduardo Habkostf03868b2018-06-08 09:29:43 -03003from __future__ import print_function
Jan Kiszka0d6b9cc2012-01-27 19:44:53 +01004#
5# Option ROM signing utility
6#
7# Authors:
8# Jan Kiszka <jan.kiszka@siemens.com>
9#
10# This work is licensed under the terms of the GNU GPL, version 2 or later.
11# See the COPYING file in the top-level directory.
12
13import sys
14import struct
15
16if len(sys.argv) < 3:
17 print('usage: signrom.py input output')
18 sys.exit(1)
19
20fin = open(sys.argv[1], 'rb')
21fout = open(sys.argv[2], 'wb')
22
Richard W.M. Jonesfd289382016-05-11 22:06:46 +010023magic = fin.read(2)
Daniel P. Berrange31d8f922018-01-16 13:42:12 +000024if magic != b'\x55\xaa':
Richard W.M. Jonesfd289382016-05-11 22:06:46 +010025 sys.exit("%s: option ROM does not begin with magic 55 aa" % sys.argv[1])
26
Richard W.M. Jones6f71b772016-05-11 22:06:45 +010027size_byte = ord(fin.read(1))
Jan Kiszka0d6b9cc2012-01-27 19:44:53 +010028fin.seek(0)
Paolo Bonzini7f256922016-08-05 10:51:37 +020029data = fin.read()
Richard W.M. Jones6f71b772016-05-11 22:06:45 +010030
Paolo Bonzini7f256922016-08-05 10:51:37 +020031size = size_byte * 512
32if len(data) > size:
33 sys.stderr.write('error: ROM is too large (%d > %d)\n' % (len(data), size))
34 sys.exit(1)
35elif len(data) < size:
36 # Add padding if necessary, rounding the whole input to a multiple of
37 # 512 bytes according to the third byte of the input.
Richard W.M. Jones6f71b772016-05-11 22:06:45 +010038 # size-1 because a final byte is added below to store the checksum.
Daniel P. Berrange31d8f922018-01-16 13:42:12 +000039 data = data.ljust(size-1, b'\0')
Richard W.M. Jones6f71b772016-05-11 22:06:45 +010040else:
Paolo Bonzini7f256922016-08-05 10:51:37 +020041 if ord(data[-1:]) != 0:
42 sys.stderr.write('WARNING: ROM includes nonzero checksum\n')
43 data = data[:size-1]
Richard W.M. Jones6f71b772016-05-11 22:06:45 +010044
Jan Kiszka0d6b9cc2012-01-27 19:44:53 +010045fout.write(data)
46
47checksum = 0
48for b in data:
Paolo Bonzinib38c2452020-02-04 17:00:28 +010049 checksum = (checksum - b) & 255
Jan Kiszka0d6b9cc2012-01-27 19:44:53 +010050
Jan Kiszka0d6b9cc2012-01-27 19:44:53 +010051fout.write(struct.pack('B', checksum))
52
53fin.close()
54fout.close()