Advertisement
Guest User

.NetRolller 3D

a guest
Mar 17th, 2010
600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.62 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import sys, struct, codecs
  4.  
  5. sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
  6.  
  7. #STRING_TABLE = 0x10b60
  8. STRING_TABLE = 0x6F80
  9. #STRING_TABLE=0x10DD0
  10. storage_map = {}
  11.  
  12. FORMS = [
  13. #   ('Exit',    0xcac0),
  14. #   ('Boot',    0xcb70),
  15. #   ('Power',   0xcca0),
  16. #   ('Security',0xd590),
  17. #   ('Advanced',0xd7e0),
  18. #   ('Main',    0x10950),
  19. #   ('OEM',     0x10cc0)
  20.     ('Setup Engine', 0x18b98)
  21. ]
  22.  
  23. def fguid(s):
  24.     a, b, c, d = struct.unpack("<IHH8s", s)
  25.     ds = ''.join('%02x'%ord(c) for c in d)
  26.     return "%08x-%04x-%04x-%s-%s"%(a,b,c,ds[:4], ds[4:])
  27.  
  28. def hexdump(s):
  29.     return ' '.join('%02x'%ord(c) for c in s)
  30.  
  31. class HiiPack(object):
  32.     def __init__(self, data, offset):
  33.         hdr = data[offset:offset+6]
  34.         self.length, self.type = struct.unpack("<IH", hdr)
  35.         self.data = data[offset+6:offset+self.length]
  36.  
  37. class StringTable(HiiPack):
  38.     def __init__(self, data, offset):
  39.         HiiPack.__init__(self, data, offset)
  40.         self.strings = []
  41.         hdr = self.data[:16]
  42.         lnoff, plnoff, count, attributes = struct.unpack("<IIII", hdr)
  43.         print "lnoff: %x, plnoff: %x, attr: %x, count: %x"%(lnoff, plnoff, attributes, count)
  44.         offsets = struct.unpack("<%dI"%count, self.data[16:16+count*4])
  45.         self.name = self._getstring(lnoff)
  46.         self.printablename = self._getstring(plnoff)
  47.         for i in range(count):
  48.             self.strings.append(self._getstring(offsets[i]))
  49.     def _getstring(self, off):
  50.         return self.data[off-6:].decode('utf-16le').split('\0')[0]
  51.     def __getitem__(self, a):
  52.         return self.strings.__getitem__(a)
  53.     def showinfo(self, ts=''):
  54.         print ts+"String table:"
  55.         print ts+" Language: %s (%s)"%(self.name, self.printablename)
  56.         print ts+" String count: %d"%len(self.strings)
  57.         for i in range(0, len(self.strings)-1):
  58.             print ts+"[0x%04x] %s"%(i, self.strings[i])
  59.  
  60. class FormOp(object):
  61.     EFI_IFR_FORM_OP            = 0x01
  62.     EFI_IFR_SUBTITLE_OP        = 0x02
  63.     EFI_IFR_TEXT_OP            = 0x03
  64.     EFI_IFR_GRAPHIC_OP         = 0x04
  65.     EFI_IFR_ONE_OF_OP          = 0x05
  66.     EFI_IFR_CHECKBOX_OP        = 0x06
  67.     EFI_IFR_NUMERIC_OP         = 0x07
  68.     EFI_IFR_PASSWORD_OP        = 0x08
  69.     EFI_IFR_ONE_OF_OPTION_OP   = 0x09 # ONEOF OPTION field
  70.     EFI_IFR_SUPPRESS_IF_OP     = 0x0A
  71.     EFI_IFR_END_FORM_OP        = 0x0B
  72.     EFI_IFR_HIDDEN_OP          = 0x0C
  73.     EFI_IFR_END_FORM_SET_OP    = 0x0D
  74.     EFI_IFR_FORM_SET_OP        = 0x0E
  75.     EFI_IFR_REF_OP             = 0x0F
  76.     EFI_IFR_END_ONE_OF_OP      = 0x10
  77.     EFI_IFR_END_OP             = EFI_IFR_END_ONE_OF_OP
  78.     EFI_IFR_INCONSISTENT_IF_OP = 0x11
  79.     EFI_IFR_EQ_ID_VAL_OP       = 0x12
  80.     EFI_IFR_EQ_ID_ID_OP        = 0x13
  81.     EFI_IFR_EQ_ID_LIST_OP      = 0x14
  82.     EFI_IFR_AND_OP             = 0x15
  83.     EFI_IFR_OR_OP              = 0x16
  84.     EFI_IFR_NOT_OP             = 0x17
  85.     EFI_IFR_END_IF_OP          = 0x18 # for endif of
  86.                                     # inconsistentif,
  87.                                     # suppressif, grayoutif
  88.     EFI_IFR_GRAYOUT_IF_OP      = 0x19
  89.     EFI_IFR_DATE_OP            = 0x1A
  90.     EFI_IFR_TIME_OP            = 0x1B
  91.     EFI_IFR_STRING_OP          = 0x1C
  92.     EFI_IFR_LABEL_OP           = 0x1D
  93.     EFI_IFR_SAVE_DEFAULTS_OP   = 0x1E
  94.     EFI_IFR_RESTORE_DEFAULTS_OP= 0x1F
  95.     EFI_IFR_BANNER_OP          = 0x20
  96.     EFI_IFR_INVENTORY_OP       = 0x21
  97.     EFI_IFR_EQ_VAR_VAL_OP      = 0x22
  98.     EFI_IFR_ORDERED_LIST_OP    = 0x23
  99.     EFI_IFR_VARSTORE_OP        = 0x24
  100.     EFI_IFR_VARSTORE_SELECT_OP = 0x25
  101.     EFI_IFR_VARSTORE_SELECT_PAIR_OP  = 0x26
  102.     EFI_IFR_VARSTORE_DEVICE_OP = 0x27
  103.     EFI_IFR_LAST_OPCODE        = EFI_IFR_VARSTORE_SELECT_PAIR_OP
  104.     EFI_IFR_OEM_OP             = 0xFE
  105.     EFI_IFR_NV_ACCESS_COMMAND  = 0xFF
  106.    
  107.     INDENTS = {
  108.         EFI_IFR_FORM_OP            : 1,
  109.         EFI_IFR_SUBTITLE_OP        : 0,
  110.         EFI_IFR_TEXT_OP            : 0,
  111.         EFI_IFR_GRAPHIC_OP         : 0,
  112.         EFI_IFR_ONE_OF_OP          : 1,
  113.         EFI_IFR_CHECKBOX_OP        : 0,
  114.         EFI_IFR_NUMERIC_OP         : 0,
  115.         EFI_IFR_PASSWORD_OP        : 0,
  116.         EFI_IFR_ONE_OF_OPTION_OP   : 0,
  117.         EFI_IFR_SUPPRESS_IF_OP     : 1,
  118.         EFI_IFR_END_FORM_OP        : -1,
  119.         EFI_IFR_HIDDEN_OP          : 0,
  120.         EFI_IFR_END_FORM_SET_OP    : -1,
  121.         EFI_IFR_FORM_SET_OP        : 1,
  122.         EFI_IFR_REF_OP             : 0,
  123.         EFI_IFR_END_OP             : -1,
  124.         EFI_IFR_INCONSISTENT_IF_OP : 0,
  125.         EFI_IFR_EQ_ID_VAL_OP       : 0,
  126.         EFI_IFR_EQ_ID_ID_OP        : 0,
  127.         EFI_IFR_EQ_ID_LIST_OP      : 0,
  128.         EFI_IFR_AND_OP             : 0,
  129.         EFI_IFR_OR_OP              : 0,
  130.         EFI_IFR_NOT_OP             : 0,
  131.         EFI_IFR_END_IF_OP          : -1,
  132.         EFI_IFR_GRAYOUT_IF_OP      : 1,
  133.         EFI_IFR_DATE_OP            : 0,
  134.         EFI_IFR_TIME_OP            : 0,
  135.         EFI_IFR_STRING_OP          : 0,
  136.         EFI_IFR_LABEL_OP           : 0,
  137.         EFI_IFR_SAVE_DEFAULTS_OP   : 0,
  138.         EFI_IFR_RESTORE_DEFAULTS_OP: 0,
  139.         EFI_IFR_BANNER_OP          : 0,
  140.         EFI_IFR_INVENTORY_OP       : 0,
  141.         EFI_IFR_EQ_VAR_VAL_OP      : 0,
  142.         EFI_IFR_ORDERED_LIST_OP    : 0,
  143.         EFI_IFR_VARSTORE_OP        : 0,
  144.         EFI_IFR_VARSTORE_SELECT_OP : 0,
  145.         EFI_IFR_VARSTORE_SELECT_PAIR_OP : 0,
  146.         EFI_IFR_VARSTORE_DEVICE_OP : 0,
  147.         EFI_IFR_LAST_OPCODE        : 0,
  148.         EFI_IFR_OEM_OP             : 0,
  149.         EFI_IFR_NV_ACCESS_COMMAND  : 0,
  150.     }
  151.    
  152.     def __init__(self, data):
  153.         self.opcode, self.length = struct.unpack("<BB", data[:2])
  154.         self.payload = data[2:self.length]
  155.         self.indent = self.INDENTS[self.opcode]
  156.        
  157.     def showinfo(self, s, ts=''):
  158.         if self.opcode == self.EFI_IFR_FORM_OP:
  159.             id, title = struct.unpack("<HH", self.payload)
  160.             print ts+"Form ID:0x%04x Name:'%s' (0x%04x)"%(id, s[title], title)
  161.         elif self.opcode == self.EFI_IFR_SUBTITLE_OP:
  162.             print ts+"Subtitle: '%s'"%s[struct.unpack("<H", self.payload)[0]]
  163.         elif self.opcode == self.EFI_IFR_TEXT_OP:
  164.             if len(self.payload) != 9:
  165.                 print ts+"BROKEN TEXT OP %r"%self.payload
  166.             else:
  167.                 hid, tid, t2id, flags, key=struct.unpack("<HHHBH", self.payload)
  168.                 print ts+"Text: '%s','%s' Flags:0x%x Key:0x%x"%(s[tid],s[t2id],flags,key)
  169.                 if s[hid] and s[hid] != ' ':
  170.                     print ts+"\Help text: '%s'"%s[hid]
  171.         elif self.opcode == self.EFI_IFR_FORM_SET_OP:
  172.             guid, fsid, hid, cb, cls, subcls, nvsize = struct.unpack("<16sHHQHHH", self.payload)
  173.             print ts+"Form Set '%s' Class %d-%d NvSize 0x%x Callback 0x%x"%(s[fsid],cls, subcls, nvsize, cb)
  174.             print ts+"\GUID: %s"%fguid(guid)
  175.             if s[hid] and s[hid] != ' ':
  176.                 print ts+"\Help text: '%s'"%s[hid]
  177.         elif self.opcode == self.EFI_IFR_END_FORM_SET_OP:
  178.             print ts+"End Form Set"
  179.         elif self.opcode == self.EFI_IFR_END_FORM_OP:
  180.             print ts+"End Form"
  181.         elif self.opcode == self.EFI_IFR_GRAYOUT_IF_OP:
  182.             print ts+"Grayout If"
  183.         elif self.opcode == self.EFI_IFR_SUPPRESS_IF_OP:
  184.             print ts+"Suppress If"
  185.         elif self.opcode == self.EFI_IFR_END_IF_OP:
  186.             print ts+"End If",hexdump(self.payload)
  187.         elif self.opcode == self.EFI_IFR_EQ_ID_VAL_OP:
  188.             qid, width, val = struct.unpack("<HBH", self.payload)
  189.             print ts+"EQ [0x%x<%d>] == 0x%x"%(qid, width, val)
  190.         elif self.opcode == self.EFI_IFR_EQ_ID_ID_OP:
  191.             qid, width, qid2, width2, val = struct.unpack("<HBHB", self.payload)
  192.             print ts+"EQ [0x%x<%d>] == [0x%x.%d]"%(qid, width, qid2, width2, val)
  193.         elif self.opcode == self.EFI_IFR_EQ_ID_LIST_OP:
  194.             qid, width, length = struct.unpack("<HBH", self.payload[:5])
  195.             l = struct.unpack("<%dH"%length, self.payload[5:])
  196.             print ts+"LIST [0x%x<%d>] in (%s)"%(qid, width, ','.join(["0x%x"%i for i in l]))
  197.         elif self.opcode == self.EFI_IFR_AND_OP:
  198.             print ts+"AND"
  199.         elif self.opcode == self.EFI_IFR_OR_OP:
  200.             print ts+"OR"
  201.         elif self.opcode == self.EFI_IFR_NOT_OP:
  202.             print ts+"NOT"
  203.         elif self.opcode == self.EFI_IFR_ONE_OF_OP:
  204.             qid, width, pid, hid = struct.unpack("<HBHH", self.payload)
  205.             storage_map[qid] = s[pid]
  206.             print ts+"One Of [0x%x<%d>] '%s'"%(qid, width, s[pid])
  207.             if s[hid] and s[hid] != ' ':
  208.                 print ts+"\Help text: '%s'"%s[hid]
  209.         elif self.opcode == self.EFI_IFR_ONE_OF_OPTION_OP:
  210.             oid, value, flags, key = struct.unpack("<HHBH", self.payload)
  211.             print ts+"Option '%s' = 0x%x Flags 0x%x Key 0x%x"%(s[oid], value, flags, key)
  212.         elif self.opcode == self.EFI_IFR_END_ONE_OF_OP:
  213.             print ts+"End One Of"
  214.         elif self.opcode == self.EFI_IFR_LABEL_OP:
  215.             lid = struct.unpack("<H", self.payload)[0]
  216.             print ts+"Label ID: 0x%x"%lid
  217.         elif self.opcode == self.EFI_IFR_REF_OP:
  218.             fid, pid, hid, flags, key = struct.unpack("<HHHBH", self.payload)
  219.             print ts+"Reference: '%s' Form ID 0x%x Flags 0x%x Key 0x%x"%(s[pid], fid, flags, key)
  220.             if s[hid] and s[hid] != ' ':
  221.                 print ts+"\Help text: '%s'"%s[hid]
  222.         elif self.opcode in (self.EFI_IFR_TIME_OP, self.EFI_IFR_DATE_OP, self.EFI_IFR_NUMERIC_OP):
  223.             qid, width, pid, hid, flags, key, min, max, step, default = struct.unpack("<HBHHBHHHHH", self.payload)
  224.             t = {self.EFI_IFR_TIME_OP:'Time', self.EFI_IFR_DATE_OP:'Date', self.EFI_IFR_NUMERIC_OP:'Numeric'}[self.opcode]
  225.             print ts+"%s: '%s' [0x%x<%d>] %d-%d Step %d Default %d Flags 0x%x"%(t, s[pid], qid, width, min, max, step, default, flags)
  226.             if s[hid] and s[hid] != ' ':
  227.                 print ts+"\Help text: '%s'"%s[hid]
  228.         elif self.opcode == self.EFI_IFR_PASSWORD_OP:
  229.             qid, width, pid, hid, flags, key, mins, maxs, encoding = struct.unpack("<HBHHBHBBH", self.payload)
  230.             storage_map[qid] = s[pid]
  231.             print ts+"Password: '%s' [0x%x<%d>] Flags 0x%x Key 0x%x Size %d-%d Encoding %d"%(s[pid], qid, width, flags, key, mins, maxs, encoding)
  232.             if s[hid] and s[hid] != ' ':
  233.                 print ts+"\Help text: '%s'"%s[hid]
  234.         else:
  235.             print ts+"Opcode 0x%x (%d)"%(self.opcode, self.length),hexdump(self.payload)
  236.  
  237. class Form(HiiPack):
  238.     def __init__(self, data, offset):
  239.         HiiPack.__init__(self, data, offset)
  240.         data = self.data
  241.         self.opcodes = []
  242.         while len(data):
  243.             op = FormOp(data)
  244.             data = data[op.length:]
  245.             self.opcodes.append(op)
  246.         print len(self.opcodes)
  247.            
  248.     def showinfo(self, stringtable, ts=''):
  249.         ind = 0
  250.         in_if = False
  251.         fstk = []
  252.         for op in self.opcodes:
  253.             if op.opcode == op.EFI_IFR_FORM_OP:
  254.                 fstk.append(ind)
  255.             if op.indent < 0:
  256.                 ind += op.indent
  257.             ots = ts+' '*ind
  258.             if in_if and op.opcode in (op.EFI_IFR_SUPPRESS_IF_OP, op.EFI_IFR_GRAYOUT_IF_OP):
  259.                 ots = ts+' '*(ind-1)+'+'
  260.             try:
  261.                 op.showinfo(stringtable, ots)
  262.             except:
  263.                 print ts+"ERROR DECODING OPCODE 0x%x LEN 0x%x"%(op.opcode, op.length)
  264.             finally:
  265.                 pass
  266.             if (not in_if or op.opcode not in (op.EFI_IFR_SUPPRESS_IF_OP, op.EFI_IFR_GRAYOUT_IF_OP) ) and op.indent > 0:
  267.                 ind += op.indent
  268.             if op.opcode in (op.EFI_IFR_SUPPRESS_IF_OP, op.EFI_IFR_GRAYOUT_IF_OP):
  269.                 in_if = True
  270.             elif op.opcode == op.EFI_IFR_END_IF_OP:
  271.                 in_if = False
  272.             if op.opcode == op.EFI_IFR_END_FORM_OP:
  273.                 xind = fstk.pop()
  274.                 if xind != ind:
  275.                     print "WARNING: Indentation mismatch"
  276.                     ind = xind
  277.  
  278. pe = open(sys.argv[1], "rb").read()
  279.  
  280. strings = StringTable(pe, STRING_TABLE)
  281. strings.showinfo()
  282.  
  283. for fn, off in FORMS:
  284.     print
  285.     print "Reading form '%s'"%fn
  286.     f = Form(pe, off)
  287.     f.showinfo(strings, ' ')
  288.    
  289. print "Storage map:"
  290. for k in sorted(storage_map.keys()):
  291.     print " 0x%x: %s"%(k,storage_map[k])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement