From a64b944942d828fe98e4843929662aad7f47bcca Mon Sep 17 00:00:00 2001 From: "Chen, Christine" Date: Thu, 28 Apr 2022 20:49:37 +0800 Subject: BaseTools: Add FMMT Python Tool The FMMT python tool is used for firmware files operation, which has the Fv/FFs-based 'View'&'Add'&'Delete'&'Replace' operation function: 1.Parse a FD(Firmware Device) / FV(Firmware Volume) / FFS(Firmware Files) 2.Add a new FFS into a FV file (both included in a FD file or not) 3.Replace an FFS in a FV file with a new FFS file 4.Delete an FFS in a FV file (both included in a FD file or not) 5.Extract the FFS from a FV file (both included in a FD file or not) This version of FMMT Python tool does not support PEIM rebase feature, this feature will be added in future update. Currently the FMMT C tool is saved in edk2-staging repo, but its quality and coding style can't meet the Edk2 quality, which is hard to maintain (Hard/Duplicate Code; Regression bugs; Restrict usage). The new Python version keeps same functions with origin C version. It has higher quality and better coding style, and it is much easier to extend new functions and to maintain. REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1847 RFC Link: https://edk2.groups.io/g/devel/message/82877 Staging Link: https://github.com/tianocore/edk2-staging/tree/PyFMMT Cc: Bob Feng Cc: Liming Gao Signed-off-by: Yuwei Chen Reviewed-by: Bob Feng Acked-by: Liming Gao --- .../Source/Python/FirmwareStorageFormat/Common.py | 85 ++++++++++++++++ .../Python/FirmwareStorageFormat/FfsFileHeader.py | 66 ++++++++++++ .../Python/FirmwareStorageFormat/FvHeader.py | 112 +++++++++++++++++++++ .../Python/FirmwareStorageFormat/SectionHeader.py | 110 ++++++++++++++++++++ .../Python/FirmwareStorageFormat/__init__.py | 6 ++ 5 files changed, 379 insertions(+) create mode 100644 BaseTools/Source/Python/FirmwareStorageFormat/Common.py create mode 100644 BaseTools/Source/Python/FirmwareStorageFormat/FfsFileHeader.py create mode 100644 BaseTools/Source/Python/FirmwareStorageFormat/FvHeader.py create mode 100644 BaseTools/Source/Python/FirmwareStorageFormat/SectionHeader.py create mode 100644 BaseTools/Source/Python/FirmwareStorageFormat/__init__.py (limited to 'BaseTools/Source/Python/FirmwareStorageFormat') diff --git a/BaseTools/Source/Python/FirmwareStorageFormat/Common.py b/BaseTools/Source/Python/FirmwareStorageFormat/Common.py new file mode 100644 index 0000000000..5082268a00 --- /dev/null +++ b/BaseTools/Source/Python/FirmwareStorageFormat/Common.py @@ -0,0 +1,85 @@ +## @file +# This file is used to define the common C struct and functions. +# +# Copyright (c) 2021-, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +## +from ctypes import * +import uuid + +# ZeroGuid = uuid.UUID('{00000000-0000-0000-0000-000000000000}') +# EFI_FIRMWARE_FILE_SYSTEM2_GUID = uuid.UUID('{8C8CE578-8A3D-4f1c-9935-896185C32DD3}') +# EFI_FIRMWARE_FILE_SYSTEM3_GUID = uuid.UUID('{5473C07A-3DCB-4dca-BD6F-1E9689E7349A}') +# EFI_FFS_VOLUME_TOP_FILE_GUID = uuid.UUID('{1BA0062E-C779-4582-8566-336AE8F78F09}') + +EFI_FIRMWARE_FILE_SYSTEM2_GUID = uuid.UUID("8c8ce578-8a3d-4f1c-9935-896185c32dd3") +EFI_FIRMWARE_FILE_SYSTEM2_GUID_BYTE = b'x\xe5\x8c\x8c=\x8a\x1cO\x995\x89a\x85\xc3-\xd3' +# EFI_FIRMWARE_FILE_SYSTEM2_GUID_BYTE = EFI_FIRMWARE_FILE_SYSTEM2_GUID.bytes +EFI_FIRMWARE_FILE_SYSTEM3_GUID = uuid.UUID("5473C07A-3DCB-4dca-BD6F-1E9689E7349A") +# EFI_FIRMWARE_FILE_SYSTEM3_GUID_BYTE = b'x\xe5\x8c\x8c=\x8a\x1cO\x995\x89a\x85\xc3-\xd3' +EFI_FIRMWARE_FILE_SYSTEM3_GUID_BYTE = b'z\xc0sT\xcb=\xcaM\xbdo\x1e\x96\x89\xe74\x9a' +EFI_SYSTEM_NVDATA_FV_GUID = uuid.UUID("fff12b8d-7696-4c8b-a985-2747075b4f50") +EFI_SYSTEM_NVDATA_FV_GUID_BYTE = b"\x8d+\xf1\xff\x96v\x8bL\xa9\x85'G\x07[OP" +EFI_FFS_VOLUME_TOP_FILE_GUID = uuid.UUID("1ba0062e-c779-4582-8566-336ae8f78f09") +EFI_FFS_VOLUME_TOP_FILE_GUID_BYTE = b'.\x06\xa0\x1by\xc7\x82E\x85f3j\xe8\xf7\x8f\t' +ZEROVECTOR_BYTE = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' +PADVECTOR = uuid.UUID("ffffffff-ffff-ffff-ffff-ffffffffffff") +FVH_SIGNATURE = b'_FVH' + +#Alignment +SECTION_COMMON_ALIGNMENT = 4 +FFS_COMMON_ALIGNMENT = 8 + +class GUID(Structure): + _pack_ = 1 + _fields_ = [ + ('Guid1', c_uint32), + ('Guid2', c_uint16), + ('Guid3', c_uint16), + ('Guid4', ARRAY(c_uint8, 8)), + ] + + def from_list(self, listformat: list) -> None: + self.Guid1 = listformat[0] + self.Guid2 = listformat[1] + self.Guid3 = listformat[2] + for i in range(8): + self.Guid4[i] = listformat[i+3] + + def __cmp__(self, otherguid) -> bool: + if not isinstance(otherguid, GUID): + return 'Input is not the GUID instance!' + rt = False + if self.Guid1 == otherguid.Guid1 and self.Guid2 == otherguid.Guid2 and self.Guid3 == otherguid.Guid3: + rt = True + for i in range(8): + rt = rt & (self.Guid4[i] == otherguid.Guid4[i]) + return rt + +def ModifyGuidFormat(target_guid: str) -> GUID: + target_guid = target_guid.replace('-', '') + target_list = [] + start = [0,8,12,16,18,20,22,24,26,28,30] + end = [8,12,16,18,20,22,24,26,28,30,32] + num = len(start) + for pos in range(num): + new_value = int(target_guid[start[pos]:end[pos]], 16) + target_list.append(new_value) + new_format = GUID() + new_format.from_list(target_list) + return new_format + + +# Get data from ctypes to bytes. +def struct2stream(s) -> bytes: + length = sizeof(s) + p = cast(pointer(s), POINTER(c_char * length)) + return p.contents.raw + + + +def GetPadSize(Size: int, alignment: int) -> int: + if Size % alignment == 0: + return 0 + Pad_Size = alignment - Size % alignment + return Pad_Size diff --git a/BaseTools/Source/Python/FirmwareStorageFormat/FfsFileHeader.py b/BaseTools/Source/Python/FirmwareStorageFormat/FfsFileHeader.py new file mode 100644 index 0000000000..e9c619d224 --- /dev/null +++ b/BaseTools/Source/Python/FirmwareStorageFormat/FfsFileHeader.py @@ -0,0 +1,66 @@ +## @file +# This file is used to define the Ffs Header C Struct. +# +# Copyright (c) 2021-, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +## +from struct import * +from ctypes import * +from FirmwareStorageFormat.Common import * + +EFI_FFS_FILE_HEADER_LEN = 24 +EFI_FFS_FILE_HEADER2_LEN = 32 + +class CHECK_SUM(Structure): + _pack_ = 1 + _fields_ = [ + ('Header', c_uint8), + ('File', c_uint8), + ] + +class EFI_FFS_INTEGRITY_CHECK(Union): + _pack_ = 1 + _fields_ = [ + ('Checksum', CHECK_SUM), + ('Checksum16', c_uint16), + ] + + +class EFI_FFS_FILE_HEADER(Structure): + _pack_ = 1 + _fields_ = [ + ('Name', GUID), + ('IntegrityCheck', EFI_FFS_INTEGRITY_CHECK), + ('Type', c_uint8), + ('Attributes', c_uint8), + ('Size', ARRAY(c_uint8, 3)), + ('State', c_uint8), + ] + + @property + def FFS_FILE_SIZE(self) -> int: + return self.Size[0] | self.Size[1] << 8 | self.Size[2] << 16 + + @property + def HeaderLength(self) -> int: + return 24 + +class EFI_FFS_FILE_HEADER2(Structure): + _pack_ = 1 + _fields_ = [ + ('Name', GUID), + ('IntegrityCheck', EFI_FFS_INTEGRITY_CHECK), + ('Type', c_uint8), + ('Attributes', c_uint8), + ('Size', ARRAY(c_uint8, 3)), + ('State', c_uint8), + ('ExtendedSize', c_uint64), + ] + + @property + def FFS_FILE_SIZE(self) -> int: + return self.ExtendedSize + + @property + def HeaderLength(self) -> int: + return 32 diff --git a/BaseTools/Source/Python/FirmwareStorageFormat/FvHeader.py b/BaseTools/Source/Python/FirmwareStorageFormat/FvHeader.py new file mode 100644 index 0000000000..078beda9e5 --- /dev/null +++ b/BaseTools/Source/Python/FirmwareStorageFormat/FvHeader.py @@ -0,0 +1,112 @@ +## @file +# This file is used to define the FV Header C Struct. +# +# Copyright (c) 2021-, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +## +from ast import Str +from struct import * +from ctypes import * +from FirmwareStorageFormat.Common import * + +class EFI_FV_BLOCK_MAP_ENTRY(Structure): + _pack_ = 1 + _fields_ = [ + ('NumBlocks', c_uint32), + ('Length', c_uint32), + ] + + +class EFI_FIRMWARE_VOLUME_HEADER(Structure): + _fields_ = [ + ('ZeroVector', ARRAY(c_uint8, 16)), + ('FileSystemGuid', GUID), + ('FvLength', c_uint64), + ('Signature', c_uint32), + ('Attributes', c_uint32), + ('HeaderLength', c_uint16), + ('Checksum', c_uint16), + ('ExtHeaderOffset', c_uint16), + ('Reserved', c_uint8), + ('Revision', c_uint8), + ('BlockMap', ARRAY(EFI_FV_BLOCK_MAP_ENTRY, 1)), + ] + +def Refine_FV_Header(nums): + class EFI_FIRMWARE_VOLUME_HEADER(Structure): + _fields_ = [ + ('ZeroVector', ARRAY(c_uint8, 16)), + ('FileSystemGuid', GUID), + ('FvLength', c_uint64), + ('Signature', c_uint32), + ('Attributes', c_uint32), + ('HeaderLength', c_uint16), + ('Checksum', c_uint16), + ('ExtHeaderOffset', c_uint16), + ('Reserved', c_uint8), + ('Revision', c_uint8), + ('BlockMap', ARRAY(EFI_FV_BLOCK_MAP_ENTRY, nums)), + ] + return EFI_FIRMWARE_VOLUME_HEADER + +class EFI_FIRMWARE_VOLUME_EXT_HEADER(Structure): + _fields_ = [ + ('FvName', GUID), + ('ExtHeaderSize', c_uint32) + ] + +class EFI_FIRMWARE_VOLUME_EXT_ENTRY(Structure): + _fields_ = [ + ('ExtEntrySize', c_uint16), + ('ExtEntryType', c_uint16) + ] + +class EFI_FIRMWARE_VOLUME_EXT_ENTRY_OEM_TYPE_0(Structure): + _fields_ = [ + ('Hdr', EFI_FIRMWARE_VOLUME_EXT_ENTRY), + ('TypeMask', c_uint32) + ] + +class EFI_FIRMWARE_VOLUME_EXT_ENTRY_OEM_TYPE(Structure): + _fields_ = [ + ('Hdr', EFI_FIRMWARE_VOLUME_EXT_ENTRY), + ('TypeMask', c_uint32), + ('Types', ARRAY(GUID, 1)) + ] + +def Refine_FV_EXT_ENTRY_OEM_TYPE_Header(nums: int) -> EFI_FIRMWARE_VOLUME_EXT_ENTRY_OEM_TYPE: + class EFI_FIRMWARE_VOLUME_EXT_ENTRY_OEM_TYPE(Structure): + _fields_ = [ + ('Hdr', EFI_FIRMWARE_VOLUME_EXT_ENTRY), + ('TypeMask', c_uint32), + ('Types', ARRAY(GUID, nums)) + ] + return EFI_FIRMWARE_VOLUME_EXT_ENTRY_OEM_TYPE(Structure) + +class EFI_FIRMWARE_VOLUME_EXT_ENTRY_GUID_TYPE_0(Structure): + _fields_ = [ + ('Hdr', EFI_FIRMWARE_VOLUME_EXT_ENTRY), + ('FormatType', GUID) + ] + +class EFI_FIRMWARE_VOLUME_EXT_ENTRY_GUID_TYPE(Structure): + _fields_ = [ + ('Hdr', EFI_FIRMWARE_VOLUME_EXT_ENTRY), + ('FormatType', GUID), + ('Data', ARRAY(c_uint8, 1)) + ] + +def Refine_FV_EXT_ENTRY_GUID_TYPE_Header(nums: int) -> EFI_FIRMWARE_VOLUME_EXT_ENTRY_GUID_TYPE: + class EFI_FIRMWARE_VOLUME_EXT_ENTRY_GUID_TYPE(Structure): + _fields_ = [ + ('Hdr', EFI_FIRMWARE_VOLUME_EXT_ENTRY), + ('FormatType', GUID), + ('Data', ARRAY(c_uint8, nums)) + ] + return EFI_FIRMWARE_VOLUME_EXT_ENTRY_GUID_TYPE(Structure) + +class EFI_FIRMWARE_VOLUME_EXT_ENTRY_USED_SIZE_TYPE(Structure): + _fields_ = [ + ('Hdr', EFI_FIRMWARE_VOLUME_EXT_ENTRY), + ('UsedSize', c_uint32) + ] diff --git a/BaseTools/Source/Python/FirmwareStorageFormat/SectionHeader.py b/BaseTools/Source/Python/FirmwareStorageFormat/SectionHeader.py new file mode 100644 index 0000000000..ee6a63679d --- /dev/null +++ b/BaseTools/Source/Python/FirmwareStorageFormat/SectionHeader.py @@ -0,0 +1,110 @@ +## @file +# This file is used to define the Section Header C Struct. +# +# Copyright (c) 2021-, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +## +from struct import * +from ctypes import * +from FirmwareStorageFormat.Common import * + +EFI_COMMON_SECTION_HEADER_LEN = 4 +EFI_COMMON_SECTION_HEADER2_LEN = 8 + +class EFI_COMMON_SECTION_HEADER(Structure): + _pack_ = 1 + _fields_ = [ + ('Size', ARRAY(c_uint8, 3)), + ('Type', c_uint8), + ] + + @property + def SECTION_SIZE(self) -> int: + return self.Size[0] | self.Size[1] << 8 | self.Size[2] << 16 + + def Common_Header_Size(self) -> int: + return 4 + +class EFI_COMMON_SECTION_HEADER2(Structure): + _pack_ = 1 + _fields_ = [ + ('Size', ARRAY(c_uint8, 3)), + ('Type', c_uint8), + ('ExtendedSize', c_uint32), + ] + + @property + def SECTION_SIZE(self) -> int: + return self.ExtendedSize + + def Common_Header_Size(self) -> int: + return 8 + +class EFI_COMPRESSION_SECTION(Structure): + _pack_ = 1 + _fields_ = [ + ('UncompressedLength', c_uint32), + ('CompressionType', c_uint8), + ] + + def ExtHeaderSize(self) -> int: + return 5 + +class EFI_FREEFORM_SUBTYPE_GUID_SECTION(Structure): + _pack_ = 1 + _fields_ = [ + ('SubTypeGuid', GUID), + ] + + def ExtHeaderSize(self) -> int: + return 16 + +class EFI_GUID_DEFINED_SECTION(Structure): + _pack_ = 1 + _fields_ = [ + ('SectionDefinitionGuid', GUID), + ('DataOffset', c_uint16), + ('Attributes', c_uint16), + ] + + def ExtHeaderSize(self) -> int: + return 20 + +def Get_USER_INTERFACE_Header(nums: int): + class EFI_SECTION_USER_INTERFACE(Structure): + _pack_ = 1 + _fields_ = [ + ('FileNameString', ARRAY(c_uint16, nums)), + ] + + def ExtHeaderSize(self) -> int: + return 2 * nums + + def GetUiString(self) -> str: + UiString = '' + for i in range(nums): + if self.FileNameString[i]: + UiString += chr(self.FileNameString[i]) + return UiString + + return EFI_SECTION_USER_INTERFACE + +def Get_VERSION_Header(nums: int): + class EFI_SECTION_VERSION(Structure): + _pack_ = 1 + _fields_ = [ + ('BuildNumber', c_uint16), + ('VersionString', ARRAY(c_uint16, nums)), + ] + + def ExtHeaderSize(self) -> int: + return 2 * (nums+1) + + def GetVersionString(self) -> str: + VersionString = '' + for i in range(nums): + if self.VersionString[i]: + VersionString += chr(self.VersionString[i]) + return VersionString + + return EFI_SECTION_VERSION diff --git a/BaseTools/Source/Python/FirmwareStorageFormat/__init__.py b/BaseTools/Source/Python/FirmwareStorageFormat/__init__.py new file mode 100644 index 0000000000..335653c6cc --- /dev/null +++ b/BaseTools/Source/Python/FirmwareStorageFormat/__init__.py @@ -0,0 +1,6 @@ +## @file +# This file is used to define the Firmware Storage Format. +# +# Copyright (c) 2021-, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +## \ No newline at end of file -- cgit v1.2.3