2 # Script to check a bios image and report info on it.
4 # Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
6 # This file may be distributed under the terms of the GNU GPLv3 license.
9 import layoutrom, buildrom
11 from python23compat import as_bytes
13 def subst(data, offset, new):
14 return data[:offset] + new + data[offset + len(new):]
16 def checksum(data, start, size, csum):
17 sumbyte = buildrom.checksum(data[start:start+size])
18 return subst(data, start+csum, sumbyte)
22 objinfo, finalsize, rawfile, outfile = sys.argv[1:]
25 objinfofile = open(objinfo, 'r')
26 symbols = layoutrom.parseObjDump(objinfofile, 'in')[1]
29 f = open(rawfile, 'rb')
32 datasize = len(rawdata)
33 finalsize = int(finalsize) * 1024
36 if datasize > 64*1024:
38 if datasize > 128*1024:
40 if datasize > finalsize:
41 print("Error! ROM doesn't fit (%d > %d)" % (datasize, finalsize))
42 print(" You have to either increase the size (CONFIG_ROM_SIZE)")
43 print(" or turn off some features (such as hardware support not")
44 print(" needed) to make it fit. Trying a more recent gcc version")
45 print(" might work too.")
49 start = symbols['code32flat_start'].offset
50 end = symbols['code32flat_end'].offset
51 expend = layoutrom.BUILD_BIOS_ADDR + layoutrom.BUILD_BIOS_SIZE
53 print("Error! Code does not end at 0x%x (got 0x%x)" % (
56 if datasize > finalsize:
57 print("Error! Code is too big (0x%x vs 0x%x)" % (
60 expdatasize = end - start
61 if datasize != expdatasize:
62 print("Error! Unknown extra data (0x%x vs 0x%x)" % (
63 datasize, expdatasize))
66 # Fix up CSM Compatibility16 table
67 if 'csm_compat_table' in symbols and 'entry_csm' in symbols:
68 # Field offsets within EFI_COMPATIBILITY16_TABLE
69 ENTRY_FIELD_OFS = 14 # Compatibility16CallOffset (UINT16)
70 SIZE_FIELD_OFS = 5 # TableLength (UINT8)
71 CSUM_FIELD_OFS = 4 # TableChecksum (UINT8)
73 tableofs = symbols['csm_compat_table'].offset - symbols['code32flat_start'].offset
74 entry_addr = symbols['entry_csm'].offset - layoutrom.BUILD_BIOS_ADDR
75 entry_addr = struct.pack('<H', entry_addr)
76 rawdata = subst(rawdata, tableofs+ENTRY_FIELD_OFS, entry_addr)
78 tsfield = tableofs+SIZE_FIELD_OFS
79 tablesize = ord(rawdata[tsfield:tsfield+1])
80 rawdata = checksum(rawdata, tableofs, tablesize, CSUM_FIELD_OFS)
83 runtimesize = end - symbols['code32init_end'].offset
84 print("Total size: %d Fixed: %d Free: %d (used %.1f%% of %dKiB rom)" % (
85 datasize, runtimesize, finalsize - datasize
86 , (datasize / float(finalsize)) * 100.0
87 , int(finalsize / 1024)))
90 f = open(outfile, 'wb')
91 f.write((as_bytes("\0") * (finalsize - datasize)) + rawdata)
94 if __name__ == '__main__':