2 # process FV generation
4 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
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
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.
18 from __future__
import absolute_import
19 import Common
.LongFilePathOs
as os
21 from io
import BytesIO
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 *
30 FV_UI_EXT_ENTY_GUID
= 'A67DF1FA-8DE8-4E98-AF09-4BDF2EFFBC7C'
38 # @param self The object pointer
40 def __init__(self
, Name
=None):
42 self
.CreateFileName
= None
43 self
.BlockSizeList
= []
44 self
.DefineVarDict
= {}
46 self
.FvAlignment
= None
47 self
.FvAttributeDict
= {}
48 self
.FvNameGuid
= None
49 self
.FvNameString
= None
50 self
.AprioriSectionList
= []
52 self
.BsBaseAddress
= None
53 self
.RtBaseAddress
= 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
= []
69 # Generate Fv and add it to the Buffer
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 VtfDict VTF objects
78 # @param MacroDict macro value pair
79 # @retval string Generated FV file path
81 def AddToBuffer (self
, Buffer
, BaseAddress
=None, BlockSize
= None, BlockNum
=None, ErasePloarity
='1', VtfDict
=None, MacroDict
= {}, Flag
=False):
82 if BaseAddress
is None and self
.UiFvName
.upper() + 'fv' in GenFdsGlobalVariable
.ImageBinDict
:
83 return GenFdsGlobalVariable
.ImageBinDict
[self
.UiFvName
.upper() + 'fv']
86 # Check whether FV in Capsule is in FD flash region.
87 # If yes, return error. Doesn't support FV in Capsule image is also in FD flash region.
89 if self
.CapsuleName
is not None:
90 for FdObj
in GenFdsGlobalVariable
.FdfParser
.Profile
.FdDict
.values():
91 for RegionObj
in FdObj
.RegionList
:
92 if RegionObj
.RegionType
== BINARY_FILE_TYPE_FV
:
93 for RegionData
in RegionObj
.RegionDataList
:
94 if RegionData
.endswith(".fv"):
96 elif RegionData
.upper() + 'fv' in GenFdsGlobalVariable
.ImageBinDict
:
98 elif self
.UiFvName
.upper() == RegionData
.upper():
99 GenFdsGlobalVariable
.ErrorLogger("Capsule %s in FD region can't contain a FV %s in FD region." % (self
.CapsuleName
, self
.UiFvName
.upper()))
101 GenFdsGlobalVariable
.InfLogger( "\nGenerating %s FV" %self
.UiFvName
)
102 GenFdsGlobalVariable
.LargeFileInFvFlags
.append(False)
105 if self
.FvBaseAddress
is not None:
106 BaseAddress
= self
.FvBaseAddress
108 self
._InitializeInf
(BaseAddress
, BlockSize
, BlockNum
, ErasePloarity
, VtfDict
)
110 # First Process the Apriori section
112 MacroDict
.update(self
.DefineVarDict
)
114 GenFdsGlobalVariable
.VerboseLogger('First generate Apriori file !')
116 for AprSection
in self
.AprioriSectionList
:
117 FileName
= AprSection
.GenFfs (self
.UiFvName
, MacroDict
, IsMakefile
=Flag
)
118 FfsFileList
.append(FileName
)
119 # Add Apriori file name to Inf file
121 self
.FvInfFile
.writelines("EFI_FILE_NAME = " + \
125 # Process Modules in FfsList
126 for FfsFile
in self
.FfsList
:
128 if isinstance(FfsFile
, FfsFileStatement
.FileStatement
):
130 if GenFdsGlobalVariable
.EnableGenfdsMultiThread
and GenFdsGlobalVariable
.ModuleFile
and GenFdsGlobalVariable
.ModuleFile
.Path
.find(os
.path
.normpath(FfsFile
.InfFileName
)) == -1:
132 FileName
= FfsFile
.GenFfs(MacroDict
, FvParentAddr
=BaseAddress
, IsMakefile
=Flag
, FvName
=self
.UiFvName
)
133 FfsFileList
.append(FileName
)
135 self
.FvInfFile
.writelines("EFI_FILE_NAME = " + \
139 SaveFileOnChange(self
.InfFileName
, self
.FvInfFile
.getvalue(), False)
140 self
.FvInfFile
.close()
144 FvOutputFile
= os
.path
.join(GenFdsGlobalVariable
.FvDir
, self
.UiFvName
)
145 FvOutputFile
= FvOutputFile
+ '.Fv'
146 # BUGBUG: FvOutputFile could be specified from FDF file (FV section, CreateFile statement)
147 if self
.CreateFileName
is not None:
148 FvOutputFile
= self
.CreateFileName
151 GenFdsGlobalVariable
.ImageBinDict
[self
.UiFvName
.upper() + 'fv'] = FvOutputFile
154 FvInfoFileName
= os
.path
.join(GenFdsGlobalVariable
.FfsDir
, self
.UiFvName
+ '.inf')
156 CopyLongFilePath(GenFdsGlobalVariable
.FvAddressFileName
, FvInfoFileName
)
158 if os
.path
.exists (FvInfoFileName
):
159 OrigFvInfo
= open(FvInfoFileName
, 'r').read()
160 if GenFdsGlobalVariable
.LargeFileInFvFlags
[-1]:
161 FFSGuid
= GenFdsGlobalVariable
.EFI_FIRMWARE_FILE_SYSTEM3_GUID
162 GenFdsGlobalVariable
.GenerateFirmwareVolume(
165 AddressFile
=FvInfoFileName
,
167 ForceRebase
=self
.FvForceRebase
,
168 FileSystemGuid
=FFSGuid
172 if os
.path
.exists (FvInfoFileName
):
173 NewFvInfo
= open(FvInfoFileName
, 'r').read()
174 if NewFvInfo
is not None and NewFvInfo
!= OrigFvInfo
:
176 AddFileObj
= open(FvInfoFileName
, 'r')
177 AddrStrings
= AddFileObj
.readlines()
179 for AddrString
in AddrStrings
:
181 #get base address for the inside FvImage
182 FvChildAddr
.append (AddrString
)
183 elif AddrString
.find ("[FV_BASE_ADDRESS]") != -1:
187 if FvChildAddr
!= []:
189 for FfsFile
in self
.FfsList
:
190 FileName
= FfsFile
.GenFfs(MacroDict
, FvChildAddr
, BaseAddress
, IsMakefile
=Flag
, FvName
=self
.UiFvName
)
192 if GenFdsGlobalVariable
.LargeFileInFvFlags
[-1]:
193 FFSGuid
= GenFdsGlobalVariable
.EFI_FIRMWARE_FILE_SYSTEM3_GUID
;
195 GenFdsGlobalVariable
.GenerateFirmwareVolume(
198 AddressFile
=FvInfoFileName
,
200 ForceRebase
=self
.FvForceRebase
,
201 FileSystemGuid
=FFSGuid
205 # Write the Fv contents to Buffer
207 if os
.path
.isfile(FvOutputFile
) and os
.path
.getsize(FvOutputFile
) >= 0x48:
208 FvFileObj
= open(FvOutputFile
, 'rb')
209 # PI FvHeader is 0x48 byte
210 FvHeaderBuffer
= FvFileObj
.read(0x48)
211 Signature
= FvHeaderBuffer
[0x28:0x32]
212 if Signature
and Signature
.startswith('_FVH'):
213 GenFdsGlobalVariable
.VerboseLogger("\nGenerate %s FV Successfully" % self
.UiFvName
)
214 GenFdsGlobalVariable
.SharpCounter
= 0
217 Buffer
.write(FvFileObj
.read())
218 # FV alignment position.
219 FvAlignmentValue
= 1 << (ord(FvHeaderBuffer
[0x2E]) & 0x1F)
220 if FvAlignmentValue
>= 0x400:
221 if FvAlignmentValue
>= 0x100000:
222 if FvAlignmentValue
>= 0x1000000:
223 #The max alignment supported by FFS is 16M.
224 self
.FvAlignment
= "16M"
226 self
.FvAlignment
= str(FvAlignmentValue
/ 0x100000) + "M"
228 self
.FvAlignment
= str(FvAlignmentValue
/ 0x400) + "K"
230 # FvAlignmentValue is less than 1K
231 self
.FvAlignment
= str (FvAlignmentValue
)
233 GenFdsGlobalVariable
.ImageBinDict
[self
.UiFvName
.upper() + 'fv'] = FvOutputFile
234 GenFdsGlobalVariable
.LargeFileInFvFlags
.pop()
236 GenFdsGlobalVariable
.ErrorLogger("Invalid FV file %s." % self
.UiFvName
)
238 GenFdsGlobalVariable
.ErrorLogger("Failed to generate %s FV file." %self
.UiFvName
)
243 # Calculate FV's block size
244 # Inherit block size from FD if no block size specified in FV
246 def _GetBlockSize(self
):
247 if self
.BlockSizeList
:
250 for FdObj
in GenFdsGlobalVariable
.FdfParser
.Profile
.FdDict
.values():
251 for RegionObj
in FdObj
.RegionList
:
252 if RegionObj
.RegionType
!= BINARY_FILE_TYPE_FV
:
254 for RegionData
in RegionObj
.RegionDataList
:
256 # Found the FD and region that contain this FV
258 if self
.UiFvName
.upper() == RegionData
.upper():
259 RegionObj
.BlockInfoOfRegion(FdObj
.BlockSizeList
, self
)
260 if self
.BlockSizeList
:
266 # Initilize the inf file to create FV
268 # @param self The object pointer
269 # @param BaseAddress base address of FV
270 # @param BlockSize block size of FV
271 # @param BlockNum How many blocks in FV
272 # @param ErasePolarity Flash erase polarity
273 # @param VtfDict VTF objects
275 def _InitializeInf (self
, BaseAddress
= None, BlockSize
= None, BlockNum
= None, ErasePloarity
='1', VtfDict
=None):
279 self
.InfFileName
= os
.path
.join(GenFdsGlobalVariable
.FvDir
,
280 self
.UiFvName
+ '.inf')
281 self
.FvInfFile
= BytesIO()
286 self
.FvInfFile
.writelines("[options]" + TAB_LINE_BREAK
)
287 if BaseAddress
is not None:
288 self
.FvInfFile
.writelines("EFI_BASE_ADDRESS = " + \
292 if BlockSize
is not None:
293 self
.FvInfFile
.writelines("EFI_BLOCK_SIZE = " + \
294 '0x%X' %BlockSize
+ \
296 if BlockNum
is not None:
297 self
.FvInfFile
.writelines("EFI_NUM_BLOCKS = " + \
298 ' 0x%X' %BlockNum
+ \
301 if self
.BlockSizeList
== []:
302 if not self
._GetBlockSize
():
303 #set default block size is 1
304 self
.FvInfFile
.writelines("EFI_BLOCK_SIZE = 0x1" + TAB_LINE_BREAK
)
306 for BlockSize
in self
.BlockSizeList
:
307 if BlockSize
[0] is not None:
308 self
.FvInfFile
.writelines("EFI_BLOCK_SIZE = " + \
309 '0x%X' %BlockSize
[0] + \
312 if BlockSize
[1] is not None:
313 self
.FvInfFile
.writelines("EFI_NUM_BLOCKS = " + \
314 ' 0x%X' %BlockSize
[1] + \
317 if self
.BsBaseAddress
is not None:
318 self
.FvInfFile
.writelines('EFI_BOOT_DRIVER_BASE_ADDRESS = ' + \
319 '0x%X' %self
.BsBaseAddress
)
320 if self
.RtBaseAddress
is not None:
321 self
.FvInfFile
.writelines('EFI_RUNTIME_DRIVER_BASE_ADDRESS = ' + \
322 '0x%X' %self
.RtBaseAddress
)
326 self
.FvInfFile
.writelines("[attributes]" + TAB_LINE_BREAK
)
328 self
.FvInfFile
.writelines("EFI_ERASE_POLARITY = " + \
329 ' %s' %ErasePloarity
+ \
331 if not (self
.FvAttributeDict
is None):
332 for FvAttribute
in self
.FvAttributeDict
.keys():
333 if FvAttribute
== "FvUsedSizeEnable":
334 if self
.FvAttributeDict
[FvAttribute
].upper() in ('TRUE', '1'):
335 self
.UsedSizeEnable
= True
337 self
.FvInfFile
.writelines("EFI_" + \
340 self
.FvAttributeDict
[FvAttribute
] + \
342 if self
.FvAlignment
is not None:
343 self
.FvInfFile
.writelines("EFI_FVB2_ALIGNMENT_" + \
344 self
.FvAlignment
.strip() + \
349 # Generate FV extension header file
351 if not self
.FvNameGuid
:
352 if len(self
.FvExtEntryType
) > 0 or self
.UsedSizeEnable
:
353 GenFdsGlobalVariable
.ErrorLogger("FV Extension Header Entries declared for %s with no FvNameGuid declaration." % (self
.UiFvName
))
357 if self
.UsedSizeEnable
:
359 ## define EFI_FV_EXT_TYPE_USED_SIZE_TYPE 0x03
362 # EFI_FIRMWARE_VOLUME_EXT_ENTRY Hdr;
364 # } EFI_FIRMWARE_VOLUME_EXT_ENTRY_USED_SIZE_TYPE;
365 Buffer
+= pack('HHL', 8, 3, 0)
367 if self
.FvNameString
== 'TRUE':
369 # Create EXT entry for FV UI name
370 # This GUID is used: A67DF1FA-8DE8-4E98-AF09-4BDF2EFFBC7C
372 FvUiLen
= len(self
.UiFvName
)
373 TotalSize
+= (FvUiLen
+ 16 + 4)
374 Guid
= FV_UI_EXT_ENTY_GUID
.split('-')
377 # EFI_FIRMWARE_VOLUME_EXT_ENTRY: size 4
381 Buffer
+= (pack('HH', (FvUiLen
+ 16 + 4), 0x0002)
385 for Index
in range (0, len(self
.FvExtEntryType
)):
386 if self
.FvExtEntryType
[Index
] == 'FILE':
387 # check if the path is absolute or relative
388 if os
.path
.isabs(self
.FvExtEntryData
[Index
]):
389 FileFullPath
= os
.path
.normpath(self
.FvExtEntryData
[Index
])
391 FileFullPath
= os
.path
.normpath(os
.path
.join(GenFdsGlobalVariable
.WorkSpaceDir
, self
.FvExtEntryData
[Index
]))
392 # check if the file path exists or not
393 if not os
.path
.isfile(FileFullPath
):
394 GenFdsGlobalVariable
.ErrorLogger("Error opening FV Extension Header Entry file %s." % (self
.FvExtEntryData
[Index
]))
395 FvExtFile
= open (FileFullPath
, 'rb')
397 Size
= FvExtFile
.tell()
399 GenFdsGlobalVariable
.ErrorLogger("The size of FV Extension Header Entry file %s exceeds 0x10000." % (self
.FvExtEntryData
[Index
]))
400 TotalSize
+= (Size
+ 4)
402 Buffer
+= pack('HH', (Size
+ 4), int(self
.FvExtEntryTypeValue
[Index
], 16))
403 Buffer
+= FvExtFile
.read()
405 if self
.FvExtEntryType
[Index
] == 'DATA':
406 ByteList
= self
.FvExtEntryData
[Index
].split(',')
407 Size
= len (ByteList
)
409 GenFdsGlobalVariable
.ErrorLogger("The size of FV Extension Header Entry data %s exceeds 0x10000." % (self
.FvExtEntryData
[Index
]))
410 TotalSize
+= (Size
+ 4)
411 Buffer
+= pack('HH', (Size
+ 4), int(self
.FvExtEntryTypeValue
[Index
], 16))
412 for Index1
in range (0, Size
):
413 Buffer
+= pack('B', int(ByteList
[Index1
], 16))
415 Guid
= self
.FvNameGuid
.split('-')
416 Buffer
= PackGUID(Guid
) + pack('=L', TotalSize
) + Buffer
419 # Generate FV extension header file if the total size is not zero
422 FvExtHeaderFileName
= os
.path
.join(GenFdsGlobalVariable
.FvDir
, self
.UiFvName
+ '.ext')
423 FvExtHeaderFile
= BytesIO()
424 FvExtHeaderFile
.write(Buffer
)
425 Changed
= SaveFileOnChange(FvExtHeaderFileName
, FvExtHeaderFile
.getvalue(), True)
426 FvExtHeaderFile
.close()
428 if os
.path
.exists (self
.InfFileName
):
429 os
.remove (self
.InfFileName
)
430 self
.FvInfFile
.writelines("EFI_FV_EXT_HEADER_FILE_NAME = " + \
431 FvExtHeaderFileName
+ \
437 self
.FvInfFile
.writelines("[files]" + TAB_LINE_BREAK
)
438 if VtfDict
and self
.UiFvName
in VtfDict
:
439 self
.FvInfFile
.writelines("EFI_FILE_NAME = " + \
440 VtfDict
[self
.UiFvName
] + \