Guest User

Untitled

a guest
Nov 19th, 2019
110
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. """fwf.py - Firmware File collector:
  2.  
  3. Runs on Python 2.7.x with following module(s) installed:
  4. pip install uefi_firmware
  5. https://github.com/theopolis/uefi-firmware-parser
  6.  
  7. Copyright (C) 2016-2019 Phoenix Technologies Ltd. All Rights Reserved. """
  8.  
  9. import struct
  10. import collections
  11. import uefi_firmware
  12.  
  13. def sguid(b, big=False):
  14. '''RFC4122 binary GUID as string.'''
  15. if b is None or len(b) != 16:
  16. return ""
  17. a, b, c, d = struct.unpack("%sIHH8s" % (">" if big else "<"), b)
  18. d = ''.join('%02X' % ord(c) for c in d)
  19. return "%08X-%04X-%04X-%s-%s" % (a, b, c, d[:4], d[4:])
  20.  
  21. #EFI_SECTION_TYPES = {
  22. # 0x01: ("Compression", "compressed", None),
  23. # 0x02: ("Guid Defined", "guid", None),
  24. # 0x03: ("Disposable", "disposable", None),
  25. # 0x10: ("PE32 image", "pe", "PE32"),
  26. # 0x11: ("PE32+ PIC image", "pic.pe", "PIC"),
  27. # 0x12: ("Terse executable (TE)", "te", "TE"),
  28. # 0x13: ("DXE dependency expression", "dxe.depex", "DXE_DEPEX"),
  29. # # Added from previous code (not in Phoenix spec
  30. # 0x14: ("Version section", "version", "VERSION"),
  31. # 0x15: ("User interface name", "ui", "UI"),
  32. #
  33. # 0x16: ("IA-32 16-bit image", "ia32.16bit", "COMPAT16"),
  34. # 0x17: ("Firmware volume image", "fv", "FV_IMAGE"),
  35. # # See FdfParser.py in EDKII's GenFds
  36. # 0x18: ("Free-form GUID", "freeform.guid", "SUBTYPE_GUID"),
  37. # 0x19: ("Raw", "raw", "RAW"),
  38. # 0x1b: ("PEI dependency expression", "pie.depex", "PEI_DEPEX"),
  39. # 0x1c: ("SMM dependency expression", "smm.depex", "SMM_DEPEX")
  40. executable_sction_types = {0x10, 0x11, 0x12}
  41.  
  42. complain_duplicate_file = False
  43. found_files = collections.OrderedDict()
  44.  
  45. raw_files = []
  46.  
  47. def FwFiles(FirmwareImagePath, search_guid=''):
  48. """Collect firmware-files with their file-section-ids
  49.  
  50. Input:
  51. 1. bios file path
  52. 2. search_guid
  53. Output: raw_files[]
  54. """
  55.  
  56. if search_guid:
  57. search_guid = search_guid.lower()
  58.  
  59. def traverse_MultiVolumeContainer(self):
  60. for volume in self.volumes:
  61. volume.traverse()
  62.  
  63. def traverse_MultiObject(self):
  64. for obj in self.objects:
  65. obj.traverse()
  66.  
  67. def traverse_FirmwareVolume(self):
  68. if self.valid_header and len(self.data):
  69. for _ffs in self.firmware_filesystems:
  70. _ffs.traverse()
  71.  
  72. def traverse_FirmwareFileSystem(self):
  73. for firmware_file in self.files:
  74. firmware_file.traverse()
  75.  
  76. def traverse_FirmwareFile(self):
  77. global raw_files
  78. if search_guid:
  79. if self.guid_label == search_guid:
  80. raw_files += [(self.guid_label, self.data)]
  81. else:
  82. raw_files += [(self.guid_label, self.data)]
  83. for section in self.sections:
  84. section.traverse()
  85.  
  86. def traverse_FirmwareFileSystemSection(self):
  87. if self.parsed_object:
  88. self.parsed_object.traverse()
  89.  
  90. def traverse_GuidDefinedSection(self):
  91. for section in self.subsections:
  92. section.traverse()
  93.  
  94. def traverse_RawObject(self):
  95. return
  96.  
  97. def traverse_FirmwareCapsule(self):
  98. if self.valid_header and len(self.data):
  99. if self.capsule_body:
  100. self.capsule_body.traverse()
  101.  
  102. def traverse_CompressedSection(self):
  103. for object in self.subsections:
  104. pass #object.traverse()
  105.  
  106. def traverse_FlashDescriptor(self):
  107. for region in self.regions:
  108. region.traverse()
  109.  
  110. def traverse_FlashRegion(self):
  111. for section in self.sections:
  112. section.traverse()
  113.  
  114. def traverse_MeContainer(self):
  115. for partition in self.partitions:
  116. partition.traverse()
  117.  
  118. def traverse_MeModule(self):
  119. pass
  120.  
  121. def traverse_MeVariableModule(self):
  122. pass
  123.  
  124. def traverse_MeManifestHeader(self):
  125. for module in self.modules:
  126. module.traverse()
  127. for module in self.variable_modules:
  128. module.traverse()
  129. if self.huffman_llut is not None:
  130. self.huffman_llut.traverse()
  131.  
  132. def traverse_PartitionEntry(self):
  133. if self.manifest:
  134. self.manifest.traverse()
  135.  
  136. def traverse_CPDManifestHeader(self):
  137. for entry in self.modules:
  138. entry.traverse()
  139.  
  140. def traverse_CPDEntry(self):
  141. pass
  142.  
  143. def traverse_MeLLUT(self):
  144. pass
  145.  
  146. # monkey-patching
  147. uefi_firmware.MultiVolumeContainer.traverse = traverse_MultiVolumeContainer
  148. uefi_firmware.MultiObject.traverse = traverse_MultiObject
  149. uefi_firmware.uefi.FirmwareVolume.traverse = traverse_FirmwareVolume
  150. uefi_firmware.uefi.FirmwareFileSystem.traverse = traverse_FirmwareFileSystem
  151. uefi_firmware.uefi.FirmwareFile.traverse = traverse_FirmwareFile
  152. uefi_firmware.uefi.FirmwareFileSystemSection.traverse = traverse_FirmwareFileSystemSection
  153. uefi_firmware.uefi.GuidDefinedSection.traverse = traverse_GuidDefinedSection
  154. uefi_firmware.uefi.RawObject.traverse = traverse_RawObject
  155. uefi_firmware.uefi.FirmwareCapsule.traverse = traverse_FirmwareCapsule
  156. uefi_firmware.uefi.CompressedSection.traverse = traverse_CompressedSection
  157. uefi_firmware.flash.FlashDescriptor.traverse = traverse_FlashDescriptor
  158. uefi_firmware.flash.FlashRegion.traverse = traverse_FlashRegion
  159. uefi_firmware.me.MeContainer.traverse = traverse_MeContainer
  160. uefi_firmware.me.MeManifestHeader.traverse = traverse_MeManifestHeader
  161. uefi_firmware.me.MeModule.traverse = traverse_MeModule
  162. uefi_firmware.me.MeVariableModule.traverse = traverse_MeVariableModule
  163. uefi_firmware.me.MeLLUT.traverse = traverse_MeLLUT
  164. uefi_firmware.me.PartitionEntry.traverse = traverse_PartitionEntry
  165. uefi_firmware.me.CPDManifestHeader.traverse = traverse_CPDManifestHeader
  166. uefi_firmware.me.CPDEntry.traverse = traverse_CPDEntry
  167.  
  168. with open(FirmwareImagePath, 'rb') as fh:
  169. file_content = fh.read()
  170.  
  171. if not len(file_content):
  172. raise Exception("Invalid file size: %d" % len(file_content))
  173.  
  174. parser = uefi_firmware.AutoParser(file_content)
  175.  
  176. if parser.type() != 'unknown':
  177. firmware = parser.parse()
  178. firmware.traverse()
  179.  
  180. return raw_files
  181.  
  182. def int32(data):
  183. ret = 0
  184. for i, v in enumerate(data[::-1]):
  185. ret *= 0x100
  186. ret += ord(v)
  187. if i == 3:
  188. break
  189. return ret
  190.  
  191. def mcu_header(blob):
  192. inc = 0
  193. count = 1
  194. while inc < len(blob):
  195. blobx = blob[inc:]
  196. print('#%d - %8.8X : rev%X (@%8X)' % (
  197. count, int32(blobx[3*4:4*4]), int32(blobx[1*4:2*4]), int32(blobx[2*4:3*4])
  198. ))
  199. inc += int32(blobx[8*4:9*4])
  200. count += 1
  201.  
  202.  
  203. def mcu_header_v(blob):
  204. inc = 0
  205. count = 1
  206. hd = [
  207. 'Header Version',
  208. 'Patch ID',
  209. 'DATE',
  210. 'CPUID',
  211. 'Checksum',
  212. 'Loader Version',
  213. 'Processor Flags',
  214. 'Data Size',
  215. 'Total Size'
  216. ]
  217. while inc < len(blob):
  218. print('\n[MCU %d]' % count)
  219. for i, n in enumerate(hd):
  220. print('%-16s : %8X' % (n, int32(blob[4*i:4*(i+1)])))
  221. count += 1
  222. inc += int32(blob[8*4: 9*4])
  223.  
  224.  
  225. if __name__ == '__main__':
  226. bios = "CML_RVP_X64.bin"
  227. fwfdb0 = FwFiles(bios, '197DB236-F856-4924-90F8-CDF12FB875F3')
  228. for fn, data in fwfdb0:
  229. print('Write: %s' % fn)
  230. with open(fn, 'wb') as fout:
  231. fout.write(data)
  232. mcu_header(data)
RAW Paste Data