blob: 5c9600f13fb48ffe0599cf33b34869ac3879e8c5 [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')
17 symbols = layoutrom.parseObjDump(objinfofile)[1]
18 syms = {}
19 for name, (addr, section) in symbols.items():
20 syms[name] = addr
21
22 # Read in raw file
23 f = open(rawfile, 'rb')
24 rawdata = f.read()
25 f.close()
26 datasize = len(rawdata)
27 finalsize = 64*1024
28 if datasize > 64*1024:
29 finalsize = 128*1024
30
31 # Sanity checks
Kevin O'Connor2ceeec92009-12-19 11:03:40 -050032 c16e = syms['text16_end'] + 0xf0000
33 f16e = syms['final_text16_end']
Kevin O'Connor711ddc62009-01-17 15:17:34 -050034 if c16e != f16e:
Kevin O'Connor202024a2009-01-17 10:41:28 -050035 print "Error! 16bit code moved during linking (0x%x vs 0x%x)" % (
Kevin O'Connor711ddc62009-01-17 15:17:34 -050036 c16e, f16e)
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'Connor871e0a02009-12-30 12:14:53 -050042 actualdatasize = f16e - syms['code32flat_start']
43 if datasize != actualdatasize:
44 print "Error! Unknown extra data (0x%x vs 0x%x)" % (
45 datasize, actualdatasize)
46 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'Connorb1a0d3a2009-05-23 17:49:44 -040049 sizefree = syms['freespace_end'] - syms['freespace_start']
Kevin O'Connor2ceeec92009-12-19 11:03:40 -050050 size16 = syms['text16_end'] - syms['data16_start']
Kevin O'Connor871e0a02009-12-30 12:14:53 -050051 size32seg = syms['code32seg_end'] - syms['code32seg_start']
52 size32flat = syms['code32flat_end'] - syms['code32flat_start']
53 totalc = size16+size32seg+size32flat
54 print "16bit size: %d" % size16
55 print "32bit segmented size: %d" % size32seg
56 print "32bit flat size: %d" % size32flat
Kevin O'Connor5b8f8092009-09-20 19:47:45 -040057 print "Total size: %d Free space: %d Percent used: %.1f%% (%dKiB rom)" % (
58 totalc, sizefree + finalsize - datasize
59 , (totalc / float(finalsize)) * 100.0
60 , finalsize / 1024)
61
62 # Write final file
63 f = open(outfile, 'wb')
64 f.write(("\0" * (finalsize - datasize)) + rawdata)
65 f.close()
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -040066
67if __name__ == '__main__':
68 main()