blob: 6309528e308bb46b99a5f1576af74127f26ca9f1 [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
11def main():
Kevin O'Connor5b8f8092009-09-20 19:47:45 -040012 # Get args
13 objinfo, rawfile, outfile = sys.argv[1:]
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -040014
Kevin O'Connor5b8f8092009-09-20 19:47:45 -040015 # Read in symbols
16 objinfofile = open(objinfo, 'rb')
Kevin O'Connor1a4885e2010-09-15 21:28:31 -040017 symbols = layoutrom.parseObjDump(objinfofile, 'in')[1]
Kevin O'Connor5b8f8092009-09-20 19:47:45 -040018
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
Kevin O'Connora8999452010-09-25 12:48:43 -040027 if datasize > 128*1024:
28 finalsize = 256*1024
Kevin O'Connor5b8f8092009-09-20 19:47:45 -040029
30 # Sanity checks
Kevin O'Connor1a4885e2010-09-15 21:28:31 -040031 start = symbols['code32flat_start'].offset
32 end = symbols['code32flat_end'].offset
Kevin O'Connor9ba1dea2010-05-01 09:50:13 -040033 expend = layoutrom.BUILD_BIOS_ADDR + layoutrom.BUILD_BIOS_SIZE
34 if end != expend:
35 print "Error! Code does not end at 0x%x (got 0x%x)" % (
36 expend, end)
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -040037 sys.exit(1)
Kevin O'Connor5b8f8092009-09-20 19:47:45 -040038 if datasize > finalsize:
39 print "Error! Code is too big (0x%x vs 0x%x)" % (
40 datasize, finalsize)
41 sys.exit(1)
Kevin O'Connor9ba1dea2010-05-01 09:50:13 -040042 expdatasize = end - start
43 if datasize != expdatasize:
Kevin O'Connor871e0a02009-12-30 12:14:53 -050044 print "Error! Unknown extra data (0x%x vs 0x%x)" % (
Kevin O'Connor9ba1dea2010-05-01 09:50:13 -040045 datasize, expdatasize)
Kevin O'Connor871e0a02009-12-30 12:14:53 -050046 sys.exit(1)
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -040047
Kevin O'Connor5b8f8092009-09-20 19:47:45 -040048 # Print statistics
Kevin O'Connor5b8f8092009-09-20 19:47:45 -040049 print "Total size: %d Free space: %d Percent used: %.1f%% (%dKiB rom)" % (
Kevin O'Connor9ba1dea2010-05-01 09:50:13 -040050 datasize, finalsize - datasize
51 , (datasize / float(finalsize)) * 100.0
Kevin O'Connor5b8f8092009-09-20 19:47:45 -040052 , finalsize / 1024)
53
54 # Write final file
55 f = open(outfile, 'wb')
56 f.write(("\0" * (finalsize - datasize)) + rawdata)
57 f.close()
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -040058
59if __name__ == '__main__':
60 main()