Modify tools/layoutrom.py to use classes instead of tuples.
[seabios.git] / tools / checkrom.py
1 #!/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
8 import sys
9 import layoutrom
10
11 def main():
12     # Get args
13     objinfo, rawfile, outfile = sys.argv[1:]
14
15     # Read in symbols
16     objinfofile = open(objinfo, 'rb')
17     symbols = layoutrom.parseObjDump(objinfofile, 'in')[1]
18
19     # Read in raw file
20     f = open(rawfile, 'rb')
21     rawdata = f.read()
22     f.close()
23     datasize = len(rawdata)
24     finalsize = 64*1024
25     if datasize > 64*1024:
26         finalsize = 128*1024
27
28     # Sanity checks
29     start = symbols['code32flat_start'].offset
30     end = symbols['code32flat_end'].offset
31     expend = layoutrom.BUILD_BIOS_ADDR + layoutrom.BUILD_BIOS_SIZE
32     if end != expend:
33         print "Error!  Code does not end at 0x%x (got 0x%x)" % (
34             expend, end)
35         sys.exit(1)
36     if datasize > finalsize:
37         print "Error!  Code is too big (0x%x vs 0x%x)" % (
38             datasize, finalsize)
39         sys.exit(1)
40     expdatasize = end - start
41     if datasize != expdatasize:
42         print "Error!  Unknown extra data (0x%x vs 0x%x)" % (
43             datasize, expdatasize)
44         sys.exit(1)
45
46     # Print statistics
47     print "Total size: %d  Free space: %d  Percent used: %.1f%% (%dKiB rom)" % (
48         datasize, finalsize - datasize
49         , (datasize / float(finalsize)) * 100.0
50         , finalsize / 1024)
51
52     # Write final file
53     f = open(outfile, 'wb')
54     f.write(("\0" * (finalsize - datasize)) + rawdata)
55     f.close()
56
57 if __name__ == '__main__':
58     main()