summaryrefslogtreecommitdiffstats
path: root/BaseTools/Source/Python/FMMT/core
diff options
context:
space:
mode:
Diffstat (limited to 'BaseTools/Source/Python/FMMT/core')
-rw-r--r--BaseTools/Source/Python/FMMT/core/FMMTOperation.py26
-rw-r--r--BaseTools/Source/Python/FMMT/core/FvHandler.py28
2 files changed, 53 insertions, 1 deletions
diff --git a/BaseTools/Source/Python/FMMT/core/FMMTOperation.py b/BaseTools/Source/Python/FMMT/core/FMMTOperation.py
index 4e58c91b5c..a86f8dda9a 100644
--- a/BaseTools/Source/Python/FMMT/core/FMMTOperation.py
+++ b/BaseTools/Source/Python/FMMT/core/FMMTOperation.py
@@ -204,3 +204,29 @@ def ExtractFfs(inputfile: str, Ffs_name: str, outputfile: str, Fv_name: str=None
logger.debug('Extract ffs data is saved in {}.'.format(outputfile))
else:
logger.error('Target Ffs/Fv not found!!!')
+
+def ShrinkFv(inputfile: str, outputfile: str) -> None:
+ if not os.path.exists(inputfile):
+ logger.error("Invalid inputfile, can not open {}.".format(inputfile))
+ raise Exception("Process Failed: Invalid inputfile!")
+ # 1. Data Prepare
+ with open(inputfile, "rb") as f:
+ whole_data = f.read()
+ FmmtParser = FMMTParser(inputfile, ROOT_TREE)
+ # 2. DataTree Create
+ logger.debug('Parsing inputfile data......')
+ FmmtParser.ParserFromRoot(FmmtParser.WholeFvTree, whole_data)
+ logger.debug('Done!')
+ TargetFv = FmmtParser.WholeFvTree.Child[0]
+ if TargetFv:
+ FvMod = FvHandler(TargetFv)
+ Status = FvMod.ShrinkFv()
+ else:
+ logger.error('Target Fv not found!!!')
+ # 4. Data Encapsulation
+ if Status:
+ logger.debug('Start encapsulating data......')
+ FmmtParser.Encapsulation(FmmtParser.WholeFvTree, False)
+ with open(outputfile, "wb") as f:
+ f.write(FmmtParser.FinalData)
+ logger.debug('Encapsulated data is saved in {}.'.format(outputfile))
diff --git a/BaseTools/Source/Python/FMMT/core/FvHandler.py b/BaseTools/Source/Python/FMMT/core/FvHandler.py
index e8b8480098..ff3d637623 100644
--- a/BaseTools/Source/Python/FMMT/core/FvHandler.py
+++ b/BaseTools/Source/Python/FMMT/core/FvHandler.py
@@ -145,7 +145,7 @@ def ModifyFvSystemGuid(TargetFv) -> None:
TargetFv.Data.Data += struct2stream(item.Data.Header)+ item.Data.Data + item.Data.PadData
class FvHandler:
- def __init__(self, NewFfs, TargetFfs) -> None:
+ def __init__(self, NewFfs, TargetFfs=None) -> None:
self.NewFfs = NewFfs
self.TargetFfs = TargetFfs
self.Status = False
@@ -638,3 +638,29 @@ class FvHandler:
self.Status = True
logger.debug('Done!')
return self.Status
+
+ def ShrinkFv(self) -> bool:
+ TargetFv = self.NewFfs
+ TargetFv.Data.Data = b''
+ if not TargetFv.Data.Free_Space:
+ self.Status = True
+ else:
+ BlockSize = TargetFv.Data.Header.BlockMap[0].Length
+ New_Free_Space = TargetFv.Data.Free_Space%BlockSize
+ Removed_Space = TargetFv.Data.Free_Space - New_Free_Space
+ TargetFv.Child[-1].Data.Data = b'\xff' * New_Free_Space
+ TargetFv.Data.Size -= Removed_Space
+ TargetFv.Data.Header.Fvlength = TargetFv.Data.Size
+ ModifyFvSystemGuid(TargetFv)
+ for item in TargetFv.Child:
+ if item.type == FFS_FREE_SPACE:
+ TargetFv.Data.Data += item.Data.Data + item.Data.PadData
+ else:
+ TargetFv.Data.Data += struct2stream(item.Data.Header)+ item.Data.Data + item.Data.PadData
+ TargetFv.Data.ModFvExt()
+ TargetFv.Data.ModFvSize()
+ TargetFv.Data.ModExtHeaderData()
+ ModifyFvExtData(TargetFv)
+ TargetFv.Data.ModCheckSum()
+ self.Status = True
+ return self.Status