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') |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 17 | symbols = layoutrom.parseObjDump(objinfofile, 'in')[1] |
Kevin O'Connor | 5b8f809 | 2009-09-20 19:47:45 -0400 | [diff] [blame] | 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 |
Kevin O'Connor | a899945 | 2010-09-25 12:48:43 -0400 | [diff] [blame] | 27 | if datasize > 128*1024: |
| 28 | finalsize = 256*1024 |
Kevin O'Connor | 5b8f809 | 2009-09-20 19:47:45 -0400 | [diff] [blame] | 29 | |
| 30 | # Sanity checks |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 31 | start = symbols['code32flat_start'].offset |
| 32 | end = symbols['code32flat_end'].offset |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 33 | 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'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 | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 42 | expdatasize = end - start |
| 43 | if datasize != expdatasize: |
Kevin O'Connor | 871e0a0 | 2009-12-30 12:14:53 -0500 | [diff] [blame] | 44 | print "Error! Unknown extra data (0x%x vs 0x%x)" % ( |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 45 | datasize, expdatasize) |
Kevin O'Connor | 871e0a0 | 2009-12-30 12:14:53 -0500 | [diff] [blame] | 46 | sys.exit(1) |
Kevin O'Connor | 2fda7cb | 2008-07-05 20:41:53 -0400 | [diff] [blame] | 47 | |
Kevin O'Connor | 5b8f809 | 2009-09-20 19:47:45 -0400 | [diff] [blame] | 48 | # Print statistics |
Kevin O'Connor | 94dc9c4 | 2010-09-26 18:32:13 -0400 | [diff] [blame] | 49 | runtimesize = datasize |
| 50 | if '_reloc_abs_start' in symbols: |
| 51 | runtimesize = end - symbols['code32init_end'].offset |
| 52 | print "Total size: %d Fixed: %d Free: %d (used %.1f%% of %dKiB rom)" % ( |
| 53 | datasize, runtimesize, finalsize - datasize |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 54 | , (datasize / float(finalsize)) * 100.0 |
Kevin O'Connor | 5b8f809 | 2009-09-20 19:47:45 -0400 | [diff] [blame] | 55 | , finalsize / 1024) |
| 56 | |
| 57 | # Write final file |
| 58 | f = open(outfile, 'wb') |
| 59 | f.write(("\0" * (finalsize - datasize)) + rawdata) |
| 60 | f.close() |
Kevin O'Connor | 2fda7cb | 2008-07-05 20:41:53 -0400 | [diff] [blame] | 61 | |
| 62 | if __name__ == '__main__': |
| 63 | main() |