Kevin O'Connor | 2fda7cb | 2008-07-05 20:41:53 -0400 | [diff] [blame] | 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 |
Kevin O'Connor | 5b8f809 | 2009-09-20 19:47:45 -0400 | [diff] [blame^] | 9 | import layoutrom |
Kevin O'Connor | 2fda7cb | 2008-07-05 20:41:53 -0400 | [diff] [blame] | 10 | |
| 11 | def main(): |
Kevin O'Connor | 5b8f809 | 2009-09-20 19:47:45 -0400 | [diff] [blame^] | 12 | # Get args |
| 13 | objinfo, rawfile, outfile = sys.argv[1:] |
Kevin O'Connor | 2fda7cb | 2008-07-05 20:41:53 -0400 | [diff] [blame] | 14 | |
Kevin O'Connor | 5b8f809 | 2009-09-20 19:47:45 -0400 | [diff] [blame^] | 15 | # 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'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 32 | c16e = syms['code16_end'] + 0xf0000 |
| 33 | f16e = syms['final_code16_end'] |
| 34 | if c16e != f16e: |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 35 | print "Error! 16bit code moved during linking (0x%x vs 0x%x)" % ( |
Kevin O'Connor | 711ddc6 | 2009-01-17 15:17:34 -0500 | [diff] [blame] | 36 | c16e, f16e) |
Kevin O'Connor | 2fda7cb | 2008-07-05 20:41:53 -0400 | [diff] [blame] | 37 | sys.exit(1) |
Kevin O'Connor | 5b8f809 | 2009-09-20 19:47:45 -0400 | [diff] [blame^] | 38 | if datasize > finalsize: |
| 39 | print "Error! Code is too big (0x%x vs 0x%x)" % ( |
| 40 | datasize, finalsize) |
| 41 | sys.exit(1) |
Kevin O'Connor | 2fda7cb | 2008-07-05 20:41:53 -0400 | [diff] [blame] | 42 | |
Kevin O'Connor | 5b8f809 | 2009-09-20 19:47:45 -0400 | [diff] [blame^] | 43 | # Print statistics |
Kevin O'Connor | b1a0d3a | 2009-05-23 17:49:44 -0400 | [diff] [blame] | 44 | sizefree = syms['freespace_end'] - syms['freespace_start'] |
| 45 | size16 = syms['code16_end'] - syms['code16_start'] |
Kevin O'Connor | 2fda7cb | 2008-07-05 20:41:53 -0400 | [diff] [blame] | 46 | size32 = syms['code32_end'] - syms['code32_start'] |
Kevin O'Connor | 4556a2c | 2008-07-12 14:30:11 -0400 | [diff] [blame] | 47 | totalc = size16+size32 |
Kevin O'Connor | 202024a | 2009-01-17 10:41:28 -0500 | [diff] [blame] | 48 | print "16bit size: %d" % size16 |
| 49 | print "32bit size: %d" % size32 |
Kevin O'Connor | 5b8f809 | 2009-09-20 19:47:45 -0400 | [diff] [blame^] | 50 | print "Total size: %d Free space: %d Percent used: %.1f%% (%dKiB rom)" % ( |
| 51 | totalc, sizefree + finalsize - datasize |
| 52 | , (totalc / float(finalsize)) * 100.0 |
| 53 | , finalsize / 1024) |
| 54 | |
| 55 | # Write final file |
| 56 | f = open(outfile, 'wb') |
| 57 | f.write(("\0" * (finalsize - datasize)) + rawdata) |
| 58 | f.close() |
Kevin O'Connor | 2fda7cb | 2008-07-05 20:41:53 -0400 | [diff] [blame] | 59 | |
| 60 | if __name__ == '__main__': |
| 61 | main() |