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