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>
		
			
				
	
	
		
			88 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			88 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| ## @file
 | |
| # This file is used to define the interface of Bios Parser.
 | |
| #
 | |
| # Copyright (c) 2021-, Intel Corporation. All rights reserved.<BR>
 | |
| # SPDX-License-Identifier: BSD-2-Clause-Patent
 | |
| ##
 | |
| from FirmwareStorageFormat.Common import *
 | |
| from core.BinaryFactoryProduct import ParserEntry
 | |
| from core.BiosTreeNode import *
 | |
| from core.BiosTree import *
 | |
| from core.GuidTools import *
 | |
| from utils.FmmtLogger import FmmtLogger as logger
 | |
| 
 | |
| class FMMTParser:
 | |
|     def __init__(self, name: str, TYPE: str) -> None:
 | |
|         self.WholeFvTree = BIOSTREE(name)
 | |
|         self.WholeFvTree.type = TYPE
 | |
|         self.FinalData = b''
 | |
|         self.BinaryInfo = []
 | |
| 
 | |
|     ## Parser the nodes in WholeTree.
 | |
|     def ParserFromRoot(self, WholeFvTree=None, whole_data: bytes=b'', Reloffset: int=0) -> None:
 | |
|         if WholeFvTree.type == ROOT_TREE or WholeFvTree.type == ROOT_FV_TREE:
 | |
|             ParserEntry().DataParser(self.WholeFvTree, whole_data, Reloffset)
 | |
|         else:
 | |
|             ParserEntry().DataParser(WholeFvTree, whole_data, Reloffset)
 | |
|         for Child in WholeFvTree.Child:
 | |
|             self.ParserFromRoot(Child, "")
 | |
| 
 | |
|     ## Encapuslation all the data in tree into self.FinalData
 | |
|     def Encapsulation(self, rootTree, CompressStatus: bool) -> None:
 | |
|         # If current node is Root node, skip it.
 | |
|         if rootTree.type == ROOT_TREE or rootTree.type == ROOT_FV_TREE or rootTree.type == ROOT_FFS_TREE or rootTree.type == ROOT_SECTION_TREE:
 | |
|             logger.debug('Encapsulated successfully!')
 | |
|         # If current node do not have Header, just add Data.
 | |
|         elif rootTree.type == BINARY_DATA or rootTree.type == FFS_FREE_SPACE:
 | |
|             self.FinalData += rootTree.Data.Data
 | |
|             rootTree.Child = []
 | |
|         # If current node do not have Child and ExtHeader, just add its Header and Data.
 | |
|         elif rootTree.type == DATA_FV_TREE or rootTree.type == FFS_PAD:
 | |
|             self.FinalData += struct2stream(rootTree.Data.Header) + rootTree.Data.Data + rootTree.Data.PadData
 | |
|             if rootTree.isFinalChild():
 | |
|                 ParTree = rootTree.Parent
 | |
|                 if ParTree.type != 'ROOT':
 | |
|                     self.FinalData += ParTree.Data.PadData
 | |
|             rootTree.Child = []
 | |
|         # If current node is not Section node and may have Child and ExtHeader, add its Header,ExtHeader. If do not have Child, add its Data.
 | |
|         elif rootTree.type == FV_TREE or rootTree.type == FFS_TREE or rootTree.type == SEC_FV_TREE:
 | |
|             if rootTree.HasChild():
 | |
|                 self.FinalData += struct2stream(rootTree.Data.Header)
 | |
|             else:
 | |
|                 self.FinalData += struct2stream(rootTree.Data.Header) + rootTree.Data.Data + rootTree.Data.PadData
 | |
|                 if rootTree.isFinalChild():
 | |
|                     ParTree = rootTree.Parent
 | |
|                     if ParTree.type != 'ROOT':
 | |
|                         self.FinalData += ParTree.Data.PadData
 | |
|         # If current node is Section, need to consider its ExtHeader, Child and Compressed Status.
 | |
|         elif rootTree.type == SECTION_TREE:
 | |
|             # Not compressed section
 | |
|             if rootTree.Data.OriData == b'' or (rootTree.Data.OriData != b'' and CompressStatus):
 | |
|                 if rootTree.HasChild():
 | |
|                     if rootTree.Data.ExtHeader:
 | |
|                         self.FinalData += struct2stream(rootTree.Data.Header) + struct2stream(rootTree.Data.ExtHeader)
 | |
|                     else:
 | |
|                         self.FinalData += struct2stream(rootTree.Data.Header)
 | |
|                 else:
 | |
|                     Data = rootTree.Data.Data
 | |
|                     if rootTree.Data.ExtHeader:
 | |
|                         self.FinalData += struct2stream(rootTree.Data.Header) + struct2stream(rootTree.Data.ExtHeader) + Data + rootTree.Data.PadData
 | |
|                     else:
 | |
|                         self.FinalData += struct2stream(rootTree.Data.Header) + Data + rootTree.Data.PadData
 | |
|                     if rootTree.isFinalChild():
 | |
|                         ParTree = rootTree.Parent
 | |
|                         self.FinalData += ParTree.Data.PadData
 | |
|             # If compressed section
 | |
|             else:
 | |
|                 Data = rootTree.Data.OriData
 | |
|                 rootTree.Child = []
 | |
|                 if rootTree.Data.ExtHeader:
 | |
|                     self.FinalData += struct2stream(rootTree.Data.Header) + struct2stream(rootTree.Data.ExtHeader) + Data + rootTree.Data.PadData
 | |
|                 else:
 | |
|                     self.FinalData += struct2stream(rootTree.Data.Header) + Data + rootTree.Data.PadData
 | |
|                 if rootTree.isFinalChild():
 | |
|                     ParTree = rootTree.Parent
 | |
|                     self.FinalData += ParTree.Data.PadData
 | |
|         for Child in rootTree.Child:
 | |
|             self.Encapsulation(Child, CompressStatus)
 |