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 | |
David Woodhouse | a82be56 | 2013-01-17 21:23:46 +0000 | [diff] [blame] | 11 | def subst(data, offset, new): |
| 12 | return data[:offset] + new + data[offset + len(new):] |
| 13 | |
| 14 | def checksum(data, start, size, csum): |
| 15 | sumbyte = 0 |
| 16 | while size: |
| 17 | sumbyte = sumbyte + ord(data[start + size - 1]) |
| 18 | size = size - 1 |
| 19 | sumbyte = (0x100 - sumbyte) & 0xff |
| 20 | return subst(data, start+csum, chr(sumbyte)) |
| 21 | |
Kevin O'Connor | 2fda7cb | 2008-07-05 20:41:53 -0400 | [diff] [blame] | 22 | def main(): |
Kevin O'Connor | 5b8f809 | 2009-09-20 19:47:45 -0400 | [diff] [blame] | 23 | # Get args |
| 24 | objinfo, rawfile, outfile = sys.argv[1:] |
Kevin O'Connor | 2fda7cb | 2008-07-05 20:41:53 -0400 | [diff] [blame] | 25 | |
Kevin O'Connor | 5b8f809 | 2009-09-20 19:47:45 -0400 | [diff] [blame] | 26 | # Read in symbols |
| 27 | objinfofile = open(objinfo, 'rb') |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 28 | symbols = layoutrom.parseObjDump(objinfofile, 'in')[1] |
Kevin O'Connor | 5b8f809 | 2009-09-20 19:47:45 -0400 | [diff] [blame] | 29 | |
| 30 | # Read in raw file |
| 31 | f = open(rawfile, 'rb') |
| 32 | rawdata = f.read() |
| 33 | f.close() |
| 34 | datasize = len(rawdata) |
| 35 | finalsize = 64*1024 |
| 36 | if datasize > 64*1024: |
| 37 | finalsize = 128*1024 |
Kevin O'Connor | a899945 | 2010-09-25 12:48:43 -0400 | [diff] [blame] | 38 | if datasize > 128*1024: |
| 39 | finalsize = 256*1024 |
Kevin O'Connor | 5b8f809 | 2009-09-20 19:47:45 -0400 | [diff] [blame] | 40 | |
| 41 | # Sanity checks |
Kevin O'Connor | 1a4885e | 2010-09-15 21:28:31 -0400 | [diff] [blame] | 42 | start = symbols['code32flat_start'].offset |
| 43 | end = symbols['code32flat_end'].offset |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 44 | expend = layoutrom.BUILD_BIOS_ADDR + layoutrom.BUILD_BIOS_SIZE |
| 45 | if end != expend: |
| 46 | print "Error! Code does not end at 0x%x (got 0x%x)" % ( |
| 47 | expend, end) |
Kevin O'Connor | 2fda7cb | 2008-07-05 20:41:53 -0400 | [diff] [blame] | 48 | sys.exit(1) |
Kevin O'Connor | 5b8f809 | 2009-09-20 19:47:45 -0400 | [diff] [blame] | 49 | if datasize > finalsize: |
| 50 | print "Error! Code is too big (0x%x vs 0x%x)" % ( |
| 51 | datasize, finalsize) |
| 52 | sys.exit(1) |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 53 | expdatasize = end - start |
| 54 | if datasize != expdatasize: |
Kevin O'Connor | 871e0a0 | 2009-12-30 12:14:53 -0500 | [diff] [blame] | 55 | print "Error! Unknown extra data (0x%x vs 0x%x)" % ( |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 56 | datasize, expdatasize) |
Kevin O'Connor | 871e0a0 | 2009-12-30 12:14:53 -0500 | [diff] [blame] | 57 | sys.exit(1) |
Kevin O'Connor | 2fda7cb | 2008-07-05 20:41:53 -0400 | [diff] [blame] | 58 | |
David Woodhouse | a82be56 | 2013-01-17 21:23:46 +0000 | [diff] [blame] | 59 | # Fix up CSM Compatibility16 table |
| 60 | if 'csm_compat_table' in symbols and 'entry_csm' in symbols: |
| 61 | # Field offsets within EFI_COMPATIBILITY16_TABLE |
| 62 | ENTRY_FIELD_OFS = 14 # Compatibility16CallOffset (UINT16) |
| 63 | SIZE_FIELD_OFS = 5 # TableLength (UINT8) |
| 64 | CSUM_FIELD_OFS = 4 # TableChecksum (UINT8) |
| 65 | |
| 66 | tableofs = symbols['csm_compat_table'].offset - symbols['code32flat_start'].offset |
| 67 | entry_addr = symbols['entry_csm'].offset - layoutrom.BUILD_BIOS_ADDR |
| 68 | byte1 = chr(entry_addr & 0xff) |
| 69 | byte2 = chr(entry_addr >> 8) |
| 70 | rawdata = subst(rawdata, tableofs+ENTRY_FIELD_OFS, byte1+byte2) |
| 71 | |
| 72 | tablesize = ord(rawdata[tableofs+SIZE_FIELD_OFS]) |
| 73 | rawdata = checksum(rawdata, tableofs, tablesize, CSUM_FIELD_OFS) |
| 74 | |
Kevin O'Connor | 5b8f809 | 2009-09-20 19:47:45 -0400 | [diff] [blame] | 75 | # Print statistics |
Kevin O'Connor | 6afc6f8 | 2013-02-19 01:02:50 -0500 | [diff] [blame] | 76 | runtimesize = end - symbols['code32init_end'].offset |
Kevin O'Connor | 94dc9c4 | 2010-09-26 18:32:13 -0400 | [diff] [blame] | 77 | print "Total size: %d Fixed: %d Free: %d (used %.1f%% of %dKiB rom)" % ( |
| 78 | datasize, runtimesize, finalsize - datasize |
Kevin O'Connor | 9ba1dea | 2010-05-01 09:50:13 -0400 | [diff] [blame] | 79 | , (datasize / float(finalsize)) * 100.0 |
Kevin O'Connor | 5b8f809 | 2009-09-20 19:47:45 -0400 | [diff] [blame] | 80 | , finalsize / 1024) |
| 81 | |
| 82 | # Write final file |
| 83 | f = open(outfile, 'wb') |
| 84 | f.write(("\0" * (finalsize - datasize)) + rawdata) |
| 85 | f.close() |
Kevin O'Connor | 2fda7cb | 2008-07-05 20:41:53 -0400 | [diff] [blame] | 86 | |
| 87 | if __name__ == '__main__': |
| 88 | main() |