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 <bob.c.feng@intel.com> Cc: Liming Gao <gaoliming@byosoft.com.cn> Signed-off-by: Yuwei Chen <yuwei.chen@intel.com> Reviewed-by: Bob Feng <bob.c.feng@intel.com> Acked-by: Liming Gao <gaoliming@byosoft.com.cn>
		
			
				
	
	
		
			111 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			111 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
## @file
 | 
						|
# This file is used to define the Section Header C Struct.
 | 
						|
#
 | 
						|
# Copyright (c) 2021-, Intel Corporation. All rights reserved.<BR>
 | 
						|
# 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
 |