Advertisement
Guest User

Untitled

a guest
Mar 26th, 2020
688
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.98 KB | None | 0 0
  1. # python2
  2. import sys
  3. import struct
  4.  
  5. def hexdump(src, length=16, sep='.', start=0):
  6.   # based on https://gist.github.com/7h3rAm/5603718
  7.   FILTER = ''.join([(len(repr(chr(x))) == 3) and chr(x) or sep for x in range(256)])
  8.   lines = []
  9.   for c in range(0, len(src), length):
  10.     chars = src[c:c+length]
  11.     hexstr = ' '.join(["%02X" % ord(x) for x in chars]) if type(chars) is str else ' '.join(['{:02X}'.format(x) for x in chars])
  12.     if len(hexstr) > 24:
  13.       hexstr = "%s %s" % (hexstr[:24], hexstr[24:])
  14.     printable = ''.join(["%s" % ((ord(x) <= 127 and FILTER[ord(x)]) or sep) for x in chars]) if type(chars) is str else ''.join(['{}'.format((x <= 127 and FILTER[x]) or sep) for x in chars])
  15.     lines.append("%08X:  %-*s  |%s|" % (start, length*3, hexstr, printable))
  16.     start += length
  17.   return '\n'.join(lines)
  18.  
  19.  
  20. def parse_patch(paddress, pdata, pcodeaddress):
  21.   ret = ""
  22.   address = struct.unpack('<I', paddress)[0]
  23.   if address >= 0x60000-4:
  24.     ret += "-------    --------    ";
  25.   else:
  26.     ret += "0x{:05X}    {:08X}    ".format(address, struct.unpack('>I', pdata)[0])
  27.   codeaddress = struct.unpack('<I', pcodeaddress)[0]
  28.   if codeaddress < 0x2000000 or codeaddress >= 0x2010000-2:
  29.     ret += "---------";
  30.   else:
  31.     ret += "0x{:07X}".format(codeaddress)
  32.   return ret
  33.  
  34.  
  35. def parse_hdmi_patch(paddress, pselect):
  36.     ret = ""
  37.     address = struct.unpack('<' + 'I'*25, paddress)[::-1]
  38.     select = struct.unpack('<I', pselect)[0]
  39.     if select == 0:
  40.         print "non selected!"
  41.     j = 0
  42.     for i in xrange(0, 25):
  43.         if select & (1 << i):
  44.             print "{:02}                         0x{:07X}".format(i+1, address[j])
  45.             j += 1
  46.     print ""
  47.  
  48.  
  49. def main(argc, argv):
  50.   with open(sys.argv[1], 'rb') as f:
  51.       patch = f.read()
  52.       print "Firmware Version: {}.{}       Patch Version: {}.{}".format(struct.unpack('<H', patch[0:2])[0], struct.unpack('<H', patch[2:4])[0], struct.unpack('<H', patch[4:6])[0], struct.unpack('<H', patch[6:8])[0])
  53.       print ""
  54.       print "                  Offset     Data/Instr  Code Offset"
  55.       print "Patch 0:          " + parse_patch(patch[0x08:0x0C], patch[0x18:0x1C], patch[0x28:0x2C])
  56.       print "Patch 1:          " + parse_patch(patch[0x0C:0x10], patch[0x1C:0x20], patch[0x2C:0x30])
  57.       print "Patch 2:          " + parse_patch(patch[0x10:0x14], patch[0x20:0x24], patch[0x30:0x34])
  58.       print "Patch 3:          " + parse_patch(patch[0x14:0x18], patch[0x24:0x28], patch[0x34:0x38])
  59.       print ""
  60.       print "Code:"
  61.       print hexdump("\x00"*8 + patch[0x38:0x3C0], 16, '.', 0x200F070)
  62.       print ""
  63.       print ""
  64.       print "HDMI State Function        Code Offset"      
  65.       parse_hdmi_patch(patch[0xF54:0xFB8], patch[0xFB8:0xFBC])
  66.       print ""
  67.       print "HDMI Code:"
  68.       print hexdump(patch[0x3C0:0xF54], 16, '.', 0x200F400)
  69.       print ""
  70.       print "HDMI Checksum: 0x{:04X}".format(struct.unpack('<H', patch[0xFBE:0xFC0])[0])
  71.  
  72. if __name__ == '__main__':
  73.   main(len(sys.argv), sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement