]>
Commit | Line | Data |
---|---|---|
1 | ## @file\r | |
2 | # process FV generation\r | |
3 | #\r | |
4 | # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r | |
5 | #\r | |
6 | # SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
7 | #\r | |
8 | \r | |
9 | ##\r | |
10 | # Import Modules\r | |
11 | #\r | |
12 | from __future__ import absolute_import\r | |
13 | import Common.LongFilePathOs as os\r | |
14 | import subprocess\r | |
15 | from io import BytesIO\r | |
16 | from struct import *\r | |
17 | from . import FfsFileStatement\r | |
18 | from .GenFdsGlobalVariable import GenFdsGlobalVariable\r | |
19 | from Common.Misc import SaveFileOnChange, PackGUID\r | |
20 | from Common.LongFilePathSupport import CopyLongFilePath\r | |
21 | from Common.LongFilePathSupport import OpenLongFilePath as open\r | |
22 | from Common.DataType import *\r | |
23 | \r | |
24 | FV_UI_EXT_ENTY_GUID = 'A67DF1FA-8DE8-4E98-AF09-4BDF2EFFBC7C'\r | |
25 | \r | |
26 | ## generate FV\r | |
27 | #\r | |
28 | #\r | |
29 | class FV (object):\r | |
30 | ## The constructor\r | |
31 | #\r | |
32 | # @param self The object pointer\r | |
33 | #\r | |
34 | def __init__(self, Name=None):\r | |
35 | self.UiFvName = Name\r | |
36 | self.CreateFileName = None\r | |
37 | self.BlockSizeList = []\r | |
38 | self.DefineVarDict = {}\r | |
39 | self.SetVarDict = {}\r | |
40 | self.FvAlignment = None\r | |
41 | self.FvAttributeDict = {}\r | |
42 | self.FvNameGuid = None\r | |
43 | self.FvNameString = None\r | |
44 | self.AprioriSectionList = []\r | |
45 | self.FfsList = []\r | |
46 | self.BsBaseAddress = None\r | |
47 | self.RtBaseAddress = None\r | |
48 | self.FvInfFile = None\r | |
49 | self.FvAddressFile = None\r | |
50 | self.BaseAddress = None\r | |
51 | self.InfFileName = None\r | |
52 | self.FvAddressFileName = None\r | |
53 | self.CapsuleName = None\r | |
54 | self.FvBaseAddress = None\r | |
55 | self.FvForceRebase = None\r | |
56 | self.FvRegionInFD = None\r | |
57 | self.UsedSizeEnable = False\r | |
58 | self.FvExtEntryTypeValue = []\r | |
59 | self.FvExtEntryType = []\r | |
60 | self.FvExtEntryData = []\r | |
61 | ## AddToBuffer()\r | |
62 | #\r | |
63 | # Generate Fv and add it to the Buffer\r | |
64 | #\r | |
65 | # @param self The object pointer\r | |
66 | # @param Buffer The buffer generated FV data will be put\r | |
67 | # @param BaseAddress base address of FV\r | |
68 | # @param BlockSize block size of FV\r | |
69 | # @param BlockNum How many blocks in FV\r | |
70 | # @param ErasePolarity Flash erase polarity\r | |
71 | # @param MacroDict macro value pair\r | |
72 | # @retval string Generated FV file path\r | |
73 | #\r | |
74 | def AddToBuffer (self, Buffer, BaseAddress=None, BlockSize= None, BlockNum=None, ErasePloarity='1', MacroDict = {}, Flag=False):\r | |
75 | if BaseAddress is None and self.UiFvName.upper() + 'fv' in GenFdsGlobalVariable.ImageBinDict:\r | |
76 | return GenFdsGlobalVariable.ImageBinDict[self.UiFvName.upper() + 'fv']\r | |
77 | \r | |
78 | #\r | |
79 | # Check whether FV in Capsule is in FD flash region.\r | |
80 | # If yes, return error. Doesn't support FV in Capsule image is also in FD flash region.\r | |
81 | #\r | |
82 | if self.CapsuleName is not None:\r | |
83 | for FdObj in GenFdsGlobalVariable.FdfParser.Profile.FdDict.values():\r | |
84 | for RegionObj in FdObj.RegionList:\r | |
85 | if RegionObj.RegionType == BINARY_FILE_TYPE_FV:\r | |
86 | for RegionData in RegionObj.RegionDataList:\r | |
87 | if RegionData.endswith(".fv"):\r | |
88 | continue\r | |
89 | elif RegionData.upper() + 'fv' in GenFdsGlobalVariable.ImageBinDict:\r | |
90 | continue\r | |
91 | elif self.UiFvName.upper() == RegionData.upper():\r | |
92 | GenFdsGlobalVariable.ErrorLogger("Capsule %s in FD region can't contain a FV %s in FD region." % (self.CapsuleName, self.UiFvName.upper()))\r | |
93 | if not Flag:\r | |
94 | GenFdsGlobalVariable.InfLogger( "\nGenerating %s FV" %self.UiFvName)\r | |
95 | GenFdsGlobalVariable.LargeFileInFvFlags.append(False)\r | |
96 | FFSGuid = None\r | |
97 | \r | |
98 | if self.FvBaseAddress is not None:\r | |
99 | BaseAddress = self.FvBaseAddress\r | |
100 | if not Flag:\r | |
101 | self._InitializeInf(BaseAddress, BlockSize, BlockNum, ErasePloarity)\r | |
102 | #\r | |
103 | # First Process the Apriori section\r | |
104 | #\r | |
105 | MacroDict.update(self.DefineVarDict)\r | |
106 | \r | |
107 | GenFdsGlobalVariable.VerboseLogger('First generate Apriori file !')\r | |
108 | FfsFileList = []\r | |
109 | for AprSection in self.AprioriSectionList:\r | |
110 | FileName = AprSection.GenFfs (self.UiFvName, MacroDict, IsMakefile=Flag)\r | |
111 | FfsFileList.append(FileName)\r | |
112 | # Add Apriori file name to Inf file\r | |
113 | if not Flag:\r | |
114 | self.FvInfFile.append("EFI_FILE_NAME = " + \\r | |
115 | FileName + \\r | |
116 | TAB_LINE_BREAK)\r | |
117 | \r | |
118 | # Process Modules in FfsList\r | |
119 | for FfsFile in self.FfsList:\r | |
120 | if Flag:\r | |
121 | if isinstance(FfsFile, FfsFileStatement.FileStatement):\r | |
122 | continue\r | |
123 | if GenFdsGlobalVariable.EnableGenfdsMultiThread and GenFdsGlobalVariable.ModuleFile and GenFdsGlobalVariable.ModuleFile.Path.find(os.path.normpath(FfsFile.InfFileName)) == -1:\r | |
124 | continue\r | |
125 | FileName = FfsFile.GenFfs(MacroDict, FvParentAddr=BaseAddress, IsMakefile=Flag, FvName=self.UiFvName)\r | |
126 | FfsFileList.append(FileName)\r | |
127 | if not Flag:\r | |
128 | self.FvInfFile.append("EFI_FILE_NAME = " + \\r | |
129 | FileName + \\r | |
130 | TAB_LINE_BREAK)\r | |
131 | if not Flag:\r | |
132 | FvInfFile = ''.join(self.FvInfFile)\r | |
133 | SaveFileOnChange(self.InfFileName, FvInfFile, False)\r | |
134 | #\r | |
135 | # Call GenFv tool\r | |
136 | #\r | |
137 | FvOutputFile = os.path.join(GenFdsGlobalVariable.FvDir, self.UiFvName)\r | |
138 | FvOutputFile = FvOutputFile + '.Fv'\r | |
139 | # BUGBUG: FvOutputFile could be specified from FDF file (FV section, CreateFile statement)\r | |
140 | if self.CreateFileName is not None:\r | |
141 | FvOutputFile = self.CreateFileName\r | |
142 | \r | |
143 | if Flag:\r | |
144 | GenFdsGlobalVariable.ImageBinDict[self.UiFvName.upper() + 'fv'] = FvOutputFile\r | |
145 | return FvOutputFile\r | |
146 | \r | |
147 | FvInfoFileName = os.path.join(GenFdsGlobalVariable.FfsDir, self.UiFvName + '.inf')\r | |
148 | if not Flag:\r | |
149 | CopyLongFilePath(GenFdsGlobalVariable.FvAddressFileName, FvInfoFileName)\r | |
150 | OrigFvInfo = None\r | |
151 | if os.path.exists (FvInfoFileName):\r | |
152 | OrigFvInfo = open(FvInfoFileName, 'r').read()\r | |
153 | if GenFdsGlobalVariable.LargeFileInFvFlags[-1]:\r | |
154 | FFSGuid = GenFdsGlobalVariable.EFI_FIRMWARE_FILE_SYSTEM3_GUID\r | |
155 | GenFdsGlobalVariable.GenerateFirmwareVolume(\r | |
156 | FvOutputFile,\r | |
157 | [self.InfFileName],\r | |
158 | AddressFile=FvInfoFileName,\r | |
159 | FfsList=FfsFileList,\r | |
160 | ForceRebase=self.FvForceRebase,\r | |
161 | FileSystemGuid=FFSGuid\r | |
162 | )\r | |
163 | \r | |
164 | NewFvInfo = None\r | |
165 | if os.path.exists (FvInfoFileName):\r | |
166 | NewFvInfo = open(FvInfoFileName, 'r').read()\r | |
167 | if NewFvInfo is not None and NewFvInfo != OrigFvInfo:\r | |
168 | FvChildAddr = []\r | |
169 | AddFileObj = open(FvInfoFileName, 'r')\r | |
170 | AddrStrings = AddFileObj.readlines()\r | |
171 | AddrKeyFound = False\r | |
172 | for AddrString in AddrStrings:\r | |
173 | if AddrKeyFound:\r | |
174 | #get base address for the inside FvImage\r | |
175 | FvChildAddr.append (AddrString)\r | |
176 | elif AddrString.find ("[FV_BASE_ADDRESS]") != -1:\r | |
177 | AddrKeyFound = True\r | |
178 | AddFileObj.close()\r | |
179 | \r | |
180 | if FvChildAddr != []:\r | |
181 | # Update Ffs again\r | |
182 | for FfsFile in self.FfsList:\r | |
183 | FileName = FfsFile.GenFfs(MacroDict, FvChildAddr, BaseAddress, IsMakefile=Flag, FvName=self.UiFvName)\r | |
184 | \r | |
185 | if GenFdsGlobalVariable.LargeFileInFvFlags[-1]:\r | |
186 | FFSGuid = GenFdsGlobalVariable.EFI_FIRMWARE_FILE_SYSTEM3_GUID;\r | |
187 | #Update GenFv again\r | |
188 | GenFdsGlobalVariable.GenerateFirmwareVolume(\r | |
189 | FvOutputFile,\r | |
190 | [self.InfFileName],\r | |
191 | AddressFile=FvInfoFileName,\r | |
192 | FfsList=FfsFileList,\r | |
193 | ForceRebase=self.FvForceRebase,\r | |
194 | FileSystemGuid=FFSGuid\r | |
195 | )\r | |
196 | \r | |
197 | #\r | |
198 | # Write the Fv contents to Buffer\r | |
199 | #\r | |
200 | if os.path.isfile(FvOutputFile) and os.path.getsize(FvOutputFile) >= 0x48:\r | |
201 | FvFileObj = open(FvOutputFile, 'rb')\r | |
202 | # PI FvHeader is 0x48 byte\r | |
203 | FvHeaderBuffer = FvFileObj.read(0x48)\r | |
204 | Signature = FvHeaderBuffer[0x28:0x32]\r | |
205 | if Signature and Signature.startswith(b'_FVH'):\r | |
206 | GenFdsGlobalVariable.VerboseLogger("\nGenerate %s FV Successfully" % self.UiFvName)\r | |
207 | GenFdsGlobalVariable.SharpCounter = 0\r | |
208 | \r | |
209 | FvFileObj.seek(0)\r | |
210 | Buffer.write(FvFileObj.read())\r | |
211 | # FV alignment position.\r | |
212 | FvAlignmentValue = 1 << (ord(FvHeaderBuffer[0x2E:0x2F]) & 0x1F)\r | |
213 | if FvAlignmentValue >= 0x400:\r | |
214 | if FvAlignmentValue >= 0x100000:\r | |
215 | if FvAlignmentValue >= 0x1000000:\r | |
216 | #The max alignment supported by FFS is 16M.\r | |
217 | self.FvAlignment = "16M"\r | |
218 | else:\r | |
219 | self.FvAlignment = str(FvAlignmentValue // 0x100000) + "M"\r | |
220 | else:\r | |
221 | self.FvAlignment = str(FvAlignmentValue // 0x400) + "K"\r | |
222 | else:\r | |
223 | # FvAlignmentValue is less than 1K\r | |
224 | self.FvAlignment = str (FvAlignmentValue)\r | |
225 | FvFileObj.close()\r | |
226 | GenFdsGlobalVariable.ImageBinDict[self.UiFvName.upper() + 'fv'] = FvOutputFile\r | |
227 | GenFdsGlobalVariable.LargeFileInFvFlags.pop()\r | |
228 | else:\r | |
229 | GenFdsGlobalVariable.ErrorLogger("Invalid FV file %s." % self.UiFvName)\r | |
230 | else:\r | |
231 | GenFdsGlobalVariable.ErrorLogger("Failed to generate %s FV file." %self.UiFvName)\r | |
232 | return FvOutputFile\r | |
233 | \r | |
234 | ## _GetBlockSize()\r | |
235 | #\r | |
236 | # Calculate FV's block size\r | |
237 | # Inherit block size from FD if no block size specified in FV\r | |
238 | #\r | |
239 | def _GetBlockSize(self):\r | |
240 | if self.BlockSizeList:\r | |
241 | return True\r | |
242 | \r | |
243 | for FdObj in GenFdsGlobalVariable.FdfParser.Profile.FdDict.values():\r | |
244 | for RegionObj in FdObj.RegionList:\r | |
245 | if RegionObj.RegionType != BINARY_FILE_TYPE_FV:\r | |
246 | continue\r | |
247 | for RegionData in RegionObj.RegionDataList:\r | |
248 | #\r | |
249 | # Found the FD and region that contain this FV\r | |
250 | #\r | |
251 | if self.UiFvName.upper() == RegionData.upper():\r | |
252 | RegionObj.BlockInfoOfRegion(FdObj.BlockSizeList, self)\r | |
253 | if self.BlockSizeList:\r | |
254 | return True\r | |
255 | return False\r | |
256 | \r | |
257 | ## _InitializeInf()\r | |
258 | #\r | |
259 | # Initialize the inf file to create FV\r | |
260 | #\r | |
261 | # @param self The object pointer\r | |
262 | # @param BaseAddress base address of FV\r | |
263 | # @param BlockSize block size of FV\r | |
264 | # @param BlockNum How many blocks in FV\r | |
265 | # @param ErasePolarity Flash erase polarity\r | |
266 | #\r | |
267 | def _InitializeInf (self, BaseAddress = None, BlockSize= None, BlockNum = None, ErasePloarity='1'):\r | |
268 | #\r | |
269 | # Create FV inf file\r | |
270 | #\r | |
271 | self.InfFileName = os.path.join(GenFdsGlobalVariable.FvDir,\r | |
272 | self.UiFvName + '.inf')\r | |
273 | self.FvInfFile = []\r | |
274 | \r | |
275 | #\r | |
276 | # Add [Options]\r | |
277 | #\r | |
278 | self.FvInfFile.append("[options]" + TAB_LINE_BREAK)\r | |
279 | if BaseAddress is not None:\r | |
280 | self.FvInfFile.append("EFI_BASE_ADDRESS = " + \\r | |
281 | BaseAddress + \\r | |
282 | TAB_LINE_BREAK)\r | |
283 | \r | |
284 | if BlockSize is not None:\r | |
285 | self.FvInfFile.append("EFI_BLOCK_SIZE = " + \\r | |
286 | '0x%X' %BlockSize + \\r | |
287 | TAB_LINE_BREAK)\r | |
288 | if BlockNum is not None:\r | |
289 | self.FvInfFile.append("EFI_NUM_BLOCKS = " + \\r | |
290 | ' 0x%X' %BlockNum + \\r | |
291 | TAB_LINE_BREAK)\r | |
292 | else:\r | |
293 | if self.BlockSizeList == []:\r | |
294 | if not self._GetBlockSize():\r | |
295 | #set default block size is 1\r | |
296 | self.FvInfFile.append("EFI_BLOCK_SIZE = 0x1" + TAB_LINE_BREAK)\r | |
297 | \r | |
298 | for BlockSize in self.BlockSizeList:\r | |
299 | if BlockSize[0] is not None:\r | |
300 | self.FvInfFile.append("EFI_BLOCK_SIZE = " + \\r | |
301 | '0x%X' %BlockSize[0] + \\r | |
302 | TAB_LINE_BREAK)\r | |
303 | \r | |
304 | if BlockSize[1] is not None:\r | |
305 | self.FvInfFile.append("EFI_NUM_BLOCKS = " + \\r | |
306 | ' 0x%X' %BlockSize[1] + \\r | |
307 | TAB_LINE_BREAK)\r | |
308 | \r | |
309 | if self.BsBaseAddress is not None:\r | |
310 | self.FvInfFile.append('EFI_BOOT_DRIVER_BASE_ADDRESS = ' + \\r | |
311 | '0x%X' %self.BsBaseAddress)\r | |
312 | if self.RtBaseAddress is not None:\r | |
313 | self.FvInfFile.append('EFI_RUNTIME_DRIVER_BASE_ADDRESS = ' + \\r | |
314 | '0x%X' %self.RtBaseAddress)\r | |
315 | #\r | |
316 | # Add attribute\r | |
317 | #\r | |
318 | self.FvInfFile.append("[attributes]" + TAB_LINE_BREAK)\r | |
319 | \r | |
320 | self.FvInfFile.append("EFI_ERASE_POLARITY = " + \\r | |
321 | ' %s' %ErasePloarity + \\r | |
322 | TAB_LINE_BREAK)\r | |
323 | if not (self.FvAttributeDict is None):\r | |
324 | for FvAttribute in self.FvAttributeDict.keys():\r | |
325 | if FvAttribute == "FvUsedSizeEnable":\r | |
326 | if self.FvAttributeDict[FvAttribute].upper() in ('TRUE', '1'):\r | |
327 | self.UsedSizeEnable = True\r | |
328 | continue\r | |
329 | self.FvInfFile.append("EFI_" + \\r | |
330 | FvAttribute + \\r | |
331 | ' = ' + \\r | |
332 | self.FvAttributeDict[FvAttribute] + \\r | |
333 | TAB_LINE_BREAK )\r | |
334 | if self.FvAlignment is not None:\r | |
335 | self.FvInfFile.append("EFI_FVB2_ALIGNMENT_" + \\r | |
336 | self.FvAlignment.strip() + \\r | |
337 | " = TRUE" + \\r | |
338 | TAB_LINE_BREAK)\r | |
339 | \r | |
340 | #\r | |
341 | # Generate FV extension header file\r | |
342 | #\r | |
343 | if not self.FvNameGuid:\r | |
344 | if len(self.FvExtEntryType) > 0 or self.UsedSizeEnable:\r | |
345 | GenFdsGlobalVariable.ErrorLogger("FV Extension Header Entries declared for %s with no FvNameGuid declaration." % (self.UiFvName))\r | |
346 | else:\r | |
347 | TotalSize = 16 + 4\r | |
348 | Buffer = bytearray()\r | |
349 | if self.UsedSizeEnable:\r | |
350 | TotalSize += (4 + 4)\r | |
351 | ## define EFI_FV_EXT_TYPE_USED_SIZE_TYPE 0x03\r | |
352 | #typedef struct\r | |
353 | # {\r | |
354 | # EFI_FIRMWARE_VOLUME_EXT_ENTRY Hdr;\r | |
355 | # UINT32 UsedSize;\r | |
356 | # } EFI_FIRMWARE_VOLUME_EXT_ENTRY_USED_SIZE_TYPE;\r | |
357 | Buffer += pack('HHL', 8, 3, 0)\r | |
358 | \r | |
359 | if self.FvNameString == 'TRUE':\r | |
360 | #\r | |
361 | # Create EXT entry for FV UI name\r | |
362 | # This GUID is used: A67DF1FA-8DE8-4E98-AF09-4BDF2EFFBC7C\r | |
363 | #\r | |
364 | FvUiLen = len(self.UiFvName)\r | |
365 | TotalSize += (FvUiLen + 16 + 4)\r | |
366 | Guid = FV_UI_EXT_ENTY_GUID.split('-')\r | |
367 | #\r | |
368 | # Layout:\r | |
369 | # EFI_FIRMWARE_VOLUME_EXT_ENTRY: size 4\r | |
370 | # GUID: size 16\r | |
371 | # FV UI name\r | |
372 | #\r | |
373 | Buffer += (pack('HH', (FvUiLen + 16 + 4), 0x0002)\r | |
374 | + PackGUID(Guid)\r | |
375 | + self.UiFvName.encode('utf-8'))\r | |
376 | \r | |
377 | for Index in range (0, len(self.FvExtEntryType)):\r | |
378 | if self.FvExtEntryType[Index] == 'FILE':\r | |
379 | # check if the path is absolute or relative\r | |
380 | if os.path.isabs(self.FvExtEntryData[Index]):\r | |
381 | FileFullPath = os.path.normpath(self.FvExtEntryData[Index])\r | |
382 | else:\r | |
383 | FileFullPath = os.path.normpath(os.path.join(GenFdsGlobalVariable.WorkSpaceDir, self.FvExtEntryData[Index]))\r | |
384 | # check if the file path exists or not\r | |
385 | if not os.path.isfile(FileFullPath):\r | |
386 | GenFdsGlobalVariable.ErrorLogger("Error opening FV Extension Header Entry file %s." % (self.FvExtEntryData[Index]))\r | |
387 | FvExtFile = open (FileFullPath, 'rb')\r | |
388 | FvExtFile.seek(0, 2)\r | |
389 | Size = FvExtFile.tell()\r | |
390 | if Size >= 0x10000:\r | |
391 | GenFdsGlobalVariable.ErrorLogger("The size of FV Extension Header Entry file %s exceeds 0x10000." % (self.FvExtEntryData[Index]))\r | |
392 | TotalSize += (Size + 4)\r | |
393 | FvExtFile.seek(0)\r | |
394 | Buffer += pack('HH', (Size + 4), int(self.FvExtEntryTypeValue[Index], 16))\r | |
395 | Buffer += FvExtFile.read()\r | |
396 | FvExtFile.close()\r | |
397 | if self.FvExtEntryType[Index] == 'DATA':\r | |
398 | ByteList = self.FvExtEntryData[Index].split(',')\r | |
399 | Size = len (ByteList)\r | |
400 | if Size >= 0x10000:\r | |
401 | GenFdsGlobalVariable.ErrorLogger("The size of FV Extension Header Entry data %s exceeds 0x10000." % (self.FvExtEntryData[Index]))\r | |
402 | TotalSize += (Size + 4)\r | |
403 | Buffer += pack('HH', (Size + 4), int(self.FvExtEntryTypeValue[Index], 16))\r | |
404 | for Index1 in range (0, Size):\r | |
405 | Buffer += pack('B', int(ByteList[Index1], 16))\r | |
406 | \r | |
407 | Guid = self.FvNameGuid.split('-')\r | |
408 | Buffer = PackGUID(Guid) + pack('=L', TotalSize) + Buffer\r | |
409 | \r | |
410 | #\r | |
411 | # Generate FV extension header file if the total size is not zero\r | |
412 | #\r | |
413 | if TotalSize > 0:\r | |
414 | FvExtHeaderFileName = os.path.join(GenFdsGlobalVariable.FvDir, self.UiFvName + '.ext')\r | |
415 | FvExtHeaderFile = BytesIO()\r | |
416 | FvExtHeaderFile.write(Buffer)\r | |
417 | Changed = SaveFileOnChange(FvExtHeaderFileName, FvExtHeaderFile.getvalue(), True)\r | |
418 | FvExtHeaderFile.close()\r | |
419 | if Changed:\r | |
420 | if os.path.exists (self.InfFileName):\r | |
421 | os.remove (self.InfFileName)\r | |
422 | self.FvInfFile.append("EFI_FV_EXT_HEADER_FILE_NAME = " + \\r | |
423 | FvExtHeaderFileName + \\r | |
424 | TAB_LINE_BREAK)\r | |
425 | \r | |
426 | #\r | |
427 | # Add [Files]\r | |
428 | #\r | |
429 | self.FvInfFile.append("[files]" + TAB_LINE_BREAK)\r |