blob: 30c9db248750fd9b47aaa6fdcc9fc86e8c736171 [file] [log] [blame]
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -04001#!/usr/bin/env python
2# Script to check a bios image and report info on it.
3#
4# Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
5#
6# This file may be distributed under the terms of the GNU GPLv3 license.
7
8import sys
Kevin O'Connor5b8f8092009-09-20 19:47:45 -04009import layoutrom
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -040010
David Woodhousea82be562013-01-17 21:23:46 +000011def subst(data, offset, new):
12 return data[:offset] + new + data[offset + len(new):]
13
14def checksum(data, start, size, csum):
15 sumbyte = 0
16 while size:
17 sumbyte = sumbyte + ord(data[start + size - 1])
18 size = size - 1
19 sumbyte = (0x100 - sumbyte) & 0xff
20 return subst(data, start+csum, chr(sumbyte))
21
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -040022def main():
Kevin O'Connor5b8f8092009-09-20 19:47:45 -040023 # Get args
Gerd Hoffmann85f8fac2013-09-24 10:06:16 +020024 objinfo, finalsize, rawfile, outfile = sys.argv[1:]
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -040025
Kevin O'Connor5b8f8092009-09-20 19:47:45 -040026 # Read in symbols
27 objinfofile = open(objinfo, 'rb')
Kevin O'Connor1a4885e2010-09-15 21:28:31 -040028 symbols = layoutrom.parseObjDump(objinfofile, 'in')[1]
Kevin O'Connor5b8f8092009-09-20 19:47:45 -040029
30 # Read in raw file
31 f = open(rawfile, 'rb')
32 rawdata = f.read()
33 f.close()
34 datasize = len(rawdata)
Gerd Hoffmann85f8fac2013-09-24 10:06:16 +020035 finalsize = int(finalsize) * 1024
36 if finalsize == 0:
37 finalsize = 64*1024
38 if datasize > 64*1024:
39 finalsize = 128*1024
40 if datasize > 128*1024:
41 finalsize = 256*1024
42 if datasize > finalsize:
Johannes Krampf064fd062014-01-12 11:14:54 -050043 print("Error! ROM doesn't fit (%d > %d)" % (datasize, finalsize))
44 print(" You have to either increate the size (CONFIG_ROM_SIZE)")
45 print(" or turn off some features (such as hardware support not")
46 print(" needed) to make it fit. Trying a more recent gcc version")
47 print(" might work too.")
Gerd Hoffmann85f8fac2013-09-24 10:06:16 +020048 sys.exit(1)
Kevin O'Connor5b8f8092009-09-20 19:47:45 -040049
50 # Sanity checks
Kevin O'Connor1a4885e2010-09-15 21:28:31 -040051 start = symbols['code32flat_start'].offset
52 end = symbols['code32flat_end'].offset
Kevin O'Connor9ba1dea2010-05-01 09:50:13 -040053 expend = layoutrom.BUILD_BIOS_ADDR + layoutrom.BUILD_BIOS_SIZE
54 if end != expend:
Johannes Krampf064fd062014-01-12 11:14:54 -050055 print("Error! Code does not end at 0x%x (got 0x%x)" % (
56 expend, end))
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -040057 sys.exit(1)
Kevin O'Connor5b8f8092009-09-20 19:47:45 -040058 if datasize > finalsize:
Johannes Krampf064fd062014-01-12 11:14:54 -050059 print("Error! Code is too big (0x%x vs 0x%x)" % (
60 datasize, finalsize))
Kevin O'Connor5b8f8092009-09-20 19:47:45 -040061 sys.exit(1)
Kevin O'Connor9ba1dea2010-05-01 09:50:13 -040062 expdatasize = end - start
63 if datasize != expdatasize:
Johannes Krampf064fd062014-01-12 11:14:54 -050064 print("Error! Unknown extra data (0x%x vs 0x%x)" % (
65 datasize, expdatasize))
Kevin O'Connor871e0a02009-12-30 12:14:53 -050066 sys.exit(1)
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -040067
David Woodhousea82be562013-01-17 21:23:46 +000068 # Fix up CSM Compatibility16 table
69 if 'csm_compat_table' in symbols and 'entry_csm' in symbols:
70 # Field offsets within EFI_COMPATIBILITY16_TABLE
71 ENTRY_FIELD_OFS = 14 # Compatibility16CallOffset (UINT16)
72 SIZE_FIELD_OFS = 5 # TableLength (UINT8)
73 CSUM_FIELD_OFS = 4 # TableChecksum (UINT8)
74
75 tableofs = symbols['csm_compat_table'].offset - symbols['code32flat_start'].offset
76 entry_addr = symbols['entry_csm'].offset - layoutrom.BUILD_BIOS_ADDR
77 byte1 = chr(entry_addr & 0xff)
78 byte2 = chr(entry_addr >> 8)
79 rawdata = subst(rawdata, tableofs+ENTRY_FIELD_OFS, byte1+byte2)
80
81 tablesize = ord(rawdata[tableofs+SIZE_FIELD_OFS])
82 rawdata = checksum(rawdata, tableofs, tablesize, CSUM_FIELD_OFS)
83
Kevin O'Connor5b8f8092009-09-20 19:47:45 -040084 # Print statistics
Kevin O'Connor6afc6f82013-02-19 01:02:50 -050085 runtimesize = end - symbols['code32init_end'].offset
Johannes Krampf064fd062014-01-12 11:14:54 -050086 print("Total size: %d Fixed: %d Free: %d (used %.1f%% of %dKiB rom)" % (
Kevin O'Connor94dc9c42010-09-26 18:32:13 -040087 datasize, runtimesize, finalsize - datasize
Kevin O'Connor9ba1dea2010-05-01 09:50:13 -040088 , (datasize / float(finalsize)) * 100.0
Johannes Krampf9d7d0442014-01-12 11:19:22 -050089 , int(finalsize / 1024)))
Kevin O'Connor5b8f8092009-09-20 19:47:45 -040090
91 # Write final file
92 f = open(outfile, 'wb')
93 f.write(("\0" * (finalsize - datasize)) + rawdata)
94 f.close()
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -040095
96if __name__ == '__main__':
97 main()