]>
Commit | Line | Data |
---|---|---|
a64b9449 CC |
1 | ## @file\r |
2 | # This file is used to define the functions to operate bios binary file.\r | |
3 | #\r | |
4 | # Copyright (c) 2021-, Intel Corporation. All rights reserved.<BR>\r | |
5 | # SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
6 | ##\r | |
7 | from core.FMMTParser import *\r | |
8 | from core.FvHandler import *\r | |
9 | from utils.FvLayoutPrint import *\r | |
10 | from utils.FmmtLogger import FmmtLogger as logger\r | |
11 | \r | |
12 | global Fv_count\r | |
13 | Fv_count = 0\r | |
14 | \r | |
15 | # The ROOT_TYPE can be 'ROOT_TREE', 'ROOT_FV_TREE', 'ROOT_FFS_TREE', 'ROOT_SECTION_TREE'\r | |
16 | def ViewFile(inputfile: str, ROOT_TYPE: str, layoutfile: str=None, outputfile: str=None) -> None:\r | |
17 | if not os.path.exists(inputfile):\r | |
18 | logger.error("Invalid inputfile, can not open {}.".format(inputfile))\r | |
19 | raise Exception("Process Failed: Invalid inputfile!")\r | |
20 | # 1. Data Prepare\r | |
21 | with open(inputfile, "rb") as f:\r | |
22 | whole_data = f.read()\r | |
23 | FmmtParser = FMMTParser(inputfile, ROOT_TYPE)\r | |
24 | # 2. DataTree Create\r | |
25 | logger.debug('Parsing inputfile data......')\r | |
26 | FmmtParser.ParserFromRoot(FmmtParser.WholeFvTree, whole_data)\r | |
27 | logger.debug('Done!')\r | |
28 | # 3. Log Output\r | |
29 | InfoDict = FmmtParser.WholeFvTree.ExportTree()\r | |
30 | logger.debug('BinaryTree created, start parsing BinaryTree data......')\r | |
31 | FmmtParser.WholeFvTree.parserTree(InfoDict, FmmtParser.BinaryInfo)\r | |
32 | logger.debug('Done!')\r | |
33 | GetFormatter("").LogPrint(FmmtParser.BinaryInfo)\r | |
34 | if layoutfile:\r | |
35 | if os.path.splitext(layoutfile)[1]:\r | |
36 | layoutfilename = layoutfile\r | |
37 | layoutfileformat = os.path.splitext(layoutfile)[1][1:].lower()\r | |
38 | else:\r | |
39 | layoutfilename = "Layout_{}{}".format(os.path.basename(inputfile),".{}".format(layoutfile.lower()))\r | |
40 | layoutfileformat = layoutfile.lower()\r | |
41 | GetFormatter(layoutfileformat).dump(InfoDict, FmmtParser.BinaryInfo, layoutfilename)\r | |
42 | # 4. Data Encapsulation\r | |
43 | if outputfile:\r | |
44 | logger.debug('Start encapsulating data......')\r | |
45 | FmmtParser.Encapsulation(FmmtParser.WholeFvTree, False)\r | |
46 | with open(outputfile, "wb") as f:\r | |
47 | f.write(FmmtParser.FinalData)\r | |
48 | logger.debug('Encapsulated data is saved in {}.'.format(outputfile))\r | |
49 | \r | |
50 | def DeleteFfs(inputfile: str, TargetFfs_name: str, outputfile: str, Fv_name: str=None) -> None:\r | |
51 | if not os.path.exists(inputfile):\r | |
52 | logger.error("Invalid inputfile, can not open {}.".format(inputfile))\r | |
53 | raise Exception("Process Failed: Invalid inputfile!")\r | |
54 | # 1. Data Prepare\r | |
55 | with open(inputfile, "rb") as f:\r | |
56 | whole_data = f.read()\r | |
57 | FmmtParser = FMMTParser(inputfile, ROOT_TREE)\r | |
58 | # 2. DataTree Create\r | |
59 | logger.debug('Parsing inputfile data......')\r | |
60 | FmmtParser.ParserFromRoot(FmmtParser.WholeFvTree, whole_data)\r | |
61 | logger.debug('Done!')\r | |
62 | # 3. Data Modify\r | |
63 | FmmtParser.WholeFvTree.FindNode(TargetFfs_name, FmmtParser.WholeFvTree.Findlist)\r | |
64 | # Choose the Specfic DeleteFfs with Fv info\r | |
65 | if Fv_name:\r | |
66 | for item in FmmtParser.WholeFvTree.Findlist:\r | |
67 | if item.Parent.key != Fv_name and item.Parent.Data.Name != Fv_name:\r | |
68 | FmmtParser.WholeFvTree.Findlist.remove(item)\r | |
69 | Status = False\r | |
70 | if FmmtParser.WholeFvTree.Findlist != []:\r | |
71 | for Delete_Ffs in FmmtParser.WholeFvTree.Findlist:\r | |
72 | FfsMod = FvHandler(None, Delete_Ffs)\r | |
73 | Status = FfsMod.DeleteFfs()\r | |
74 | else:\r | |
75 | logger.error('Target Ffs not found!!!')\r | |
76 | # 4. Data Encapsulation\r | |
77 | if Status:\r | |
78 | logger.debug('Start encapsulating data......')\r | |
79 | FmmtParser.Encapsulation(FmmtParser.WholeFvTree, False)\r | |
80 | with open(outputfile, "wb") as f:\r | |
81 | f.write(FmmtParser.FinalData)\r | |
82 | logger.debug('Encapsulated data is saved in {}.'.format(outputfile))\r | |
83 | \r | |
84 | def AddNewFfs(inputfile: str, Fv_name: str, newffsfile: str, outputfile: str) -> None:\r | |
85 | if not os.path.exists(inputfile):\r | |
86 | logger.error("Invalid inputfile, can not open {}.".format(inputfile))\r | |
87 | raise Exception("Process Failed: Invalid inputfile!")\r | |
88 | if not os.path.exists(newffsfile):\r | |
89 | logger.error("Invalid ffsfile, can not open {}.".format(newffsfile))\r | |
90 | raise Exception("Process Failed: Invalid ffs file!")\r | |
91 | # 1. Data Prepare\r | |
92 | with open(inputfile, "rb") as f:\r | |
93 | whole_data = f.read()\r | |
94 | FmmtParser = FMMTParser(inputfile, ROOT_TREE)\r | |
95 | # 2. DataTree Create\r | |
96 | logger.debug('Parsing inputfile data......')\r | |
97 | FmmtParser.ParserFromRoot(FmmtParser.WholeFvTree, whole_data)\r | |
98 | logger.debug('Done!')\r | |
99 | # Get Target Fv and Target Ffs_Pad\r | |
100 | FmmtParser.WholeFvTree.FindNode(Fv_name, FmmtParser.WholeFvTree.Findlist)\r | |
101 | # Create new ffs Tree\r | |
102 | with open(newffsfile, "rb") as f:\r | |
103 | new_ffs_data = f.read()\r | |
104 | NewFmmtParser = FMMTParser(newffsfile, ROOT_FFS_TREE)\r | |
105 | Status = False\r | |
106 | # 3. Data Modify\r | |
107 | if FmmtParser.WholeFvTree.Findlist:\r | |
108 | for TargetFv in FmmtParser.WholeFvTree.Findlist:\r | |
109 | TargetFfsPad = TargetFv.Child[-1]\r | |
110 | logger.debug('Parsing newffsfile data......')\r | |
111 | if TargetFfsPad.type == FFS_FREE_SPACE:\r | |
112 | NewFmmtParser.ParserFromRoot(NewFmmtParser.WholeFvTree, new_ffs_data, TargetFfsPad.Data.HOffset)\r | |
113 | else:\r | |
114 | NewFmmtParser.ParserFromRoot(NewFmmtParser.WholeFvTree, new_ffs_data, TargetFfsPad.Data.HOffset+TargetFfsPad.Data.Size)\r | |
115 | logger.debug('Done!')\r | |
116 | FfsMod = FvHandler(NewFmmtParser.WholeFvTree.Child[0], TargetFfsPad)\r | |
117 | Status = FfsMod.AddFfs()\r | |
118 | else:\r | |
119 | logger.error('Target Fv not found!!!')\r | |
120 | # 4. Data Encapsulation\r | |
121 | if Status:\r | |
122 | logger.debug('Start encapsulating data......')\r | |
123 | FmmtParser.Encapsulation(FmmtParser.WholeFvTree, False)\r | |
124 | with open(outputfile, "wb") as f:\r | |
125 | f.write(FmmtParser.FinalData)\r | |
126 | logger.debug('Encapsulated data is saved in {}.'.format(outputfile))\r | |
127 | \r | |
128 | def ReplaceFfs(inputfile: str, Ffs_name: str, newffsfile: str, outputfile: str, Fv_name: str=None) -> None:\r | |
129 | if not os.path.exists(inputfile):\r | |
130 | logger.error("Invalid inputfile, can not open {}.".format(inputfile))\r | |
131 | raise Exception("Process Failed: Invalid inputfile!")\r | |
132 | # 1. Data Prepare\r | |
133 | with open(inputfile, "rb") as f:\r | |
134 | whole_data = f.read()\r | |
135 | FmmtParser = FMMTParser(inputfile, ROOT_TREE)\r | |
136 | # 2. DataTree Create\r | |
137 | logger.debug('Parsing inputfile data......')\r | |
138 | FmmtParser.ParserFromRoot(FmmtParser.WholeFvTree, whole_data)\r | |
139 | logger.debug('Done!')\r | |
140 | with open(newffsfile, "rb") as f:\r | |
141 | new_ffs_data = f.read()\r | |
142 | newFmmtParser = FMMTParser(newffsfile, FV_TREE)\r | |
143 | logger.debug('Parsing newffsfile data......')\r | |
144 | newFmmtParser.ParserFromRoot(newFmmtParser.WholeFvTree, new_ffs_data)\r | |
145 | logger.debug('Done!')\r | |
146 | Status = False\r | |
147 | # 3. Data Modify\r | |
148 | new_ffs = newFmmtParser.WholeFvTree.Child[0]\r | |
149 | new_ffs.Data.PadData = GetPadSize(new_ffs.Data.Size, FFS_COMMON_ALIGNMENT) * b'\xff'\r | |
150 | FmmtParser.WholeFvTree.FindNode(Ffs_name, FmmtParser.WholeFvTree.Findlist)\r | |
151 | if Fv_name:\r | |
152 | for item in FmmtParser.WholeFvTree.Findlist:\r | |
153 | if item.Parent.key != Fv_name and item.Parent.Data.Name != Fv_name:\r | |
154 | FmmtParser.WholeFvTree.Findlist.remove(item)\r | |
155 | if FmmtParser.WholeFvTree.Findlist != []:\r | |
156 | for TargetFfs in FmmtParser.WholeFvTree.Findlist:\r | |
157 | FfsMod = FvHandler(newFmmtParser.WholeFvTree.Child[0], TargetFfs)\r | |
158 | Status = FfsMod.ReplaceFfs()\r | |
159 | else:\r | |
160 | logger.error('Target Ffs not found!!!')\r | |
161 | # 4. Data Encapsulation\r | |
162 | if Status:\r | |
163 | logger.debug('Start encapsulating data......')\r | |
164 | FmmtParser.Encapsulation(FmmtParser.WholeFvTree, False)\r | |
165 | with open(outputfile, "wb") as f:\r | |
166 | f.write(FmmtParser.FinalData)\r | |
167 | logger.debug('Encapsulated data is saved in {}.'.format(outputfile))\r | |
168 | \r | |
169 | def ExtractFfs(inputfile: str, Ffs_name: str, outputfile: str, Fv_name: str=None) -> None:\r | |
170 | if not os.path.exists(inputfile):\r | |
171 | logger.error("Invalid inputfile, can not open {}.".format(inputfile))\r | |
172 | raise Exception("Process Failed: Invalid inputfile!")\r | |
173 | # 1. Data Prepare\r | |
174 | with open(inputfile, "rb") as f:\r | |
175 | whole_data = f.read()\r | |
176 | FmmtParser = FMMTParser(inputfile, ROOT_TREE)\r | |
177 | # 2. DataTree Create\r | |
178 | logger.debug('Parsing inputfile data......')\r | |
179 | FmmtParser.ParserFromRoot(FmmtParser.WholeFvTree, whole_data)\r | |
180 | logger.debug('Done!')\r | |
181 | FmmtParser.WholeFvTree.FindNode(Ffs_name, FmmtParser.WholeFvTree.Findlist)\r | |
182 | if Fv_name:\r | |
183 | for item in FmmtParser.WholeFvTree.Findlist:\r | |
184 | if item.Parent.key != Fv_name and item.Parent.Data.Name != Fv_name:\r | |
185 | FmmtParser.WholeFvTree.Findlist.remove(item)\r | |
186 | if FmmtParser.WholeFvTree.Findlist != []:\r | |
187 | TargetNode = FmmtParser.WholeFvTree.Findlist[0]\r | |
188 | TargetFv = TargetNode.Parent\r | |
189 | if TargetFv.Data.Header.Attributes & EFI_FVB2_ERASE_POLARITY:\r | |
190 | TargetNode.Data.Header.State = c_uint8(\r | |
191 | ~TargetNode.Data.Header.State)\r | |
192 | FinalData = struct2stream(TargetNode.Data.Header) + TargetNode.Data.Data\r | |
193 | with open(outputfile, "wb") as f:\r | |
194 | f.write(FinalData)\r | |
195 | logger.debug('Extract ffs data is saved in {}.'.format(outputfile))\r | |
196 | else:\r | |
197 | logger.error('Target Ffs not found!!!')\r |