2 # process FV generation
4 # Copyright (c) 2007 - 2014, 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 import Common
.LongFilePathOs
as os
25 from GenFdsGlobalVariable
import GenFdsGlobalVariable
26 from GenFds
import GenFds
27 from CommonDataClass
.FdfClass
import FvClassObject
28 from Common
.Misc
import SaveFileOnChange
29 from Common
.LongFilePathSupport
import CopyLongFilePath
30 from Common
.LongFilePathSupport
import OpenLongFilePath
as open
33 FV_UI_EXT_ENTY_GUID
= 'A67DF1FA-8DE8-4E98-AF09-4BDF2EFFBC7C'
38 class FV (FvClassObject
):
41 # @param self The object pointer
44 FvClassObject
.__init
__(self
)
46 self
.FvAddressFile
= None
47 self
.BaseAddress
= None
48 self
.InfFileName
= None
49 self
.FvAddressFileName
= None
50 self
.CapsuleName
= None
51 self
.FvBaseAddress
= None
52 self
.FvForceRebase
= None
56 # Generate Fv and add it to the Buffer
58 # @param self The object pointer
59 # @param Buffer The buffer generated FV data will be put
60 # @param BaseAddress base address of FV
61 # @param BlockSize block size of FV
62 # @param BlockNum How many blocks in FV
63 # @param ErasePolarity Flash erase polarity
64 # @param VtfDict VTF objects
65 # @param MacroDict macro value pair
66 # @retval string Generated FV file path
68 def AddToBuffer (self
, Buffer
, BaseAddress
=None, BlockSize
= None, BlockNum
=None, ErasePloarity
='1', VtfDict
=None, MacroDict
= {}) :
70 if BaseAddress
== None and self
.UiFvName
.upper() + 'fv' in GenFds
.ImageBinDict
.keys():
71 return GenFds
.ImageBinDict
[self
.UiFvName
.upper() + 'fv']
74 # Check whether FV in Capsule is in FD flash region.
75 # If yes, return error. Doesn't support FV in Capsule image is also in FD flash region.
77 if self
.CapsuleName
!= None:
78 for FdName
in GenFdsGlobalVariable
.FdfParser
.Profile
.FdDict
.keys():
79 FdObj
= GenFdsGlobalVariable
.FdfParser
.Profile
.FdDict
[FdName
]
80 for RegionObj
in FdObj
.RegionList
:
81 if RegionObj
.RegionType
== 'FV':
82 for RegionData
in RegionObj
.RegionDataList
:
83 if RegionData
.endswith(".fv"):
85 elif RegionData
.upper() + 'fv' in GenFds
.ImageBinDict
.keys():
87 elif self
.UiFvName
.upper() == RegionData
.upper():
88 GenFdsGlobalVariable
.ErrorLogger("Capsule %s in FD region can't contain a FV %s in FD region." % (self
.CapsuleName
, self
.UiFvName
.upper()))
90 GenFdsGlobalVariable
.InfLogger( "\nGenerating %s FV" %self
.UiFvName
)
91 GenFdsGlobalVariable
.LargeFileInFvFlags
.append(False)
94 if self
.FvBaseAddress
!= None:
95 BaseAddress
= self
.FvBaseAddress
97 self
.__InitializeInf
__(BaseAddress
, BlockSize
, BlockNum
, ErasePloarity
, VtfDict
)
99 # First Process the Apriori section
101 MacroDict
.update(self
.DefineVarDict
)
103 GenFdsGlobalVariable
.VerboseLogger('First generate Apriori file !')
105 for AprSection
in self
.AprioriSectionList
:
106 FileName
= AprSection
.GenFfs (self
.UiFvName
, MacroDict
)
107 FfsFileList
.append(FileName
)
108 # Add Apriori file name to Inf file
109 self
.FvInfFile
.writelines("EFI_FILE_NAME = " + \
113 # Process Modules in FfsList
114 for FfsFile
in self
.FfsList
:
115 FileName
= FfsFile
.GenFfs(MacroDict
, FvParentAddr
=BaseAddress
)
116 FfsFileList
.append(FileName
)
117 self
.FvInfFile
.writelines("EFI_FILE_NAME = " + \
121 SaveFileOnChange(self
.InfFileName
, self
.FvInfFile
.getvalue(), False)
122 self
.FvInfFile
.close()
126 FvOutputFile
= os
.path
.join(GenFdsGlobalVariable
.FvDir
, self
.UiFvName
)
127 FvOutputFile
= FvOutputFile
+ '.Fv'
128 # BUGBUG: FvOutputFile could be specified from FDF file (FV section, CreateFile statement)
129 if self
.CreateFileName
!= None:
130 FvOutputFile
= self
.CreateFileName
132 FvInfoFileName
= os
.path
.join(GenFdsGlobalVariable
.FfsDir
, self
.UiFvName
+ '.inf')
133 CopyLongFilePath(GenFdsGlobalVariable
.FvAddressFileName
, FvInfoFileName
)
135 if os
.path
.exists (FvInfoFileName
):
136 OrigFvInfo
= open(FvInfoFileName
, 'r').read()
137 if GenFdsGlobalVariable
.LargeFileInFvFlags
[-1]:
138 FFSGuid
= GenFdsGlobalVariable
.EFI_FIRMWARE_FILE_SYSTEM3_GUID
;
139 GenFdsGlobalVariable
.GenerateFirmwareVolume(
142 AddressFile
=FvInfoFileName
,
144 ForceRebase
=self
.FvForceRebase
,
145 FileSystemGuid
=FFSGuid
149 if os
.path
.exists (FvInfoFileName
):
150 NewFvInfo
= open(FvInfoFileName
, 'r').read()
151 if NewFvInfo
!= None and NewFvInfo
!= OrigFvInfo
:
153 AddFileObj
= open(FvInfoFileName
, 'r')
154 AddrStrings
= AddFileObj
.readlines()
156 for AddrString
in AddrStrings
:
158 #get base address for the inside FvImage
159 FvChildAddr
.append (AddrString
)
160 elif AddrString
.find ("[FV_BASE_ADDRESS]") != -1:
164 if FvChildAddr
!= []:
166 for FfsFile
in self
.FfsList
:
167 FileName
= FfsFile
.GenFfs(MacroDict
, FvChildAddr
, BaseAddress
)
169 if GenFdsGlobalVariable
.LargeFileInFvFlags
[-1]:
170 FFSGuid
= GenFdsGlobalVariable
.EFI_FIRMWARE_FILE_SYSTEM3_GUID
;
172 GenFdsGlobalVariable
.GenerateFirmwareVolume(
175 AddressFile
=FvInfoFileName
,
177 ForceRebase
=self
.FvForceRebase
,
178 FileSystemGuid
=FFSGuid
182 # Write the Fv contents to Buffer
184 FvFileObj
= open ( FvOutputFile
,'r+b')
186 GenFdsGlobalVariable
.VerboseLogger( "\nGenerate %s FV Successfully" %self
.UiFvName
)
187 GenFdsGlobalVariable
.SharpCounter
= 0
189 Buffer
.write(FvFileObj
.read())
191 # PI FvHeader is 0x48 byte
192 FvHeaderBuffer
= FvFileObj
.read(0x48)
193 # FV alignment position.
194 FvAlignmentValue
= 1 << (ord (FvHeaderBuffer
[0x2E]) & 0x1F)
195 # FvAlignmentValue is larger than or equal to 1K
196 if FvAlignmentValue
>= 0x400:
197 if FvAlignmentValue
>= 0x10000:
198 #The max alignment supported by FFS is 64K.
199 self
.FvAlignment
= "64K"
201 self
.FvAlignment
= str (FvAlignmentValue
/ 0x400) + "K"
203 # FvAlignmentValue is less than 1K
204 self
.FvAlignment
= str (FvAlignmentValue
)
206 GenFds
.ImageBinDict
[self
.UiFvName
.upper() + 'fv'] = FvOutputFile
207 GenFdsGlobalVariable
.LargeFileInFvFlags
.pop()
212 # Calculate FV's block size
213 # Inherit block size from FD if no block size specified in FV
215 def _GetBlockSize(self
):
216 if self
.BlockSizeList
:
219 for FdName
in GenFdsGlobalVariable
.FdfParser
.Profile
.FdDict
.keys():
220 FdObj
= GenFdsGlobalVariable
.FdfParser
.Profile
.FdDict
[FdName
]
221 for RegionObj
in FdObj
.RegionList
:
222 if RegionObj
.RegionType
!= 'FV':
224 for RegionData
in RegionObj
.RegionDataList
:
226 # Found the FD and region that contain this FV
228 if self
.UiFvName
.upper() == RegionData
.upper():
229 RegionObj
.BlockInfoOfRegion(FdObj
.BlockSizeList
, self
)
230 if self
.BlockSizeList
:
234 ## __InitializeInf__()
236 # Initilize the inf file to create FV
238 # @param self The object pointer
239 # @param BaseAddress base address of FV
240 # @param BlockSize block size of FV
241 # @param BlockNum How many blocks in FV
242 # @param ErasePolarity Flash erase polarity
243 # @param VtfDict VTF objects
245 def __InitializeInf__ (self
, BaseAddress
= None, BlockSize
= None, BlockNum
= None, ErasePloarity
='1', VtfDict
=None) :
249 self
.InfFileName
= os
.path
.join(GenFdsGlobalVariable
.FvDir
,
250 self
.UiFvName
+ '.inf')
251 self
.FvInfFile
= StringIO
.StringIO()
256 self
.FvInfFile
.writelines("[options]" + T_CHAR_LF
)
257 if BaseAddress
!= None :
258 self
.FvInfFile
.writelines("EFI_BASE_ADDRESS = " + \
262 if BlockSize
!= None:
263 self
.FvInfFile
.writelines("EFI_BLOCK_SIZE = " + \
264 '0x%X' %BlockSize
+ \
267 self
.FvInfFile
.writelines("EFI_NUM_BLOCKS = " + \
268 ' 0x%X' %BlockNum
+ \
271 if self
.BlockSizeList
== []:
272 if not self
._GetBlockSize
():
273 #set default block size is 1
274 self
.FvInfFile
.writelines("EFI_BLOCK_SIZE = 0x1" + T_CHAR_LF
)
276 for BlockSize
in self
.BlockSizeList
:
277 if BlockSize
[0] != None:
278 self
.FvInfFile
.writelines("EFI_BLOCK_SIZE = " + \
279 '0x%X' %BlockSize
[0] + \
282 if BlockSize
[1] != None:
283 self
.FvInfFile
.writelines("EFI_NUM_BLOCKS = " + \
284 ' 0x%X' %BlockSize
[1] + \
287 if self
.BsBaseAddress
!= None:
288 self
.FvInfFile
.writelines('EFI_BOOT_DRIVER_BASE_ADDRESS = ' + \
289 '0x%X' %self
.BsBaseAddress
)
290 if self
.RtBaseAddress
!= None:
291 self
.FvInfFile
.writelines('EFI_RUNTIME_DRIVER_BASE_ADDRESS = ' + \
292 '0x%X' %self
.RtBaseAddress
)
296 self
.FvInfFile
.writelines("[attributes]" + T_CHAR_LF
)
298 self
.FvInfFile
.writelines("EFI_ERASE_POLARITY = " + \
299 ' %s' %ErasePloarity
+ \
301 if not (self
.FvAttributeDict
== None):
302 for FvAttribute
in self
.FvAttributeDict
.keys() :
303 self
.FvInfFile
.writelines("EFI_" + \
306 self
.FvAttributeDict
[FvAttribute
] + \
308 if self
.FvAlignment
!= None:
309 self
.FvInfFile
.writelines("EFI_FVB2_ALIGNMENT_" + \
310 self
.FvAlignment
.strip() + \
315 # Generate FV extension header file
317 if self
.FvNameGuid
== None or self
.FvNameGuid
== '':
318 if len(self
.FvExtEntryType
) > 0:
319 GenFdsGlobalVariable
.ErrorLogger("FV Extension Header Entries declared for %s with no FvNameGuid declaration." % (self
.UiFvName
))
321 if self
.FvNameGuid
<> None and self
.FvNameGuid
<> '':
324 if self
.FvNameString
== 'TRUE':
326 # Create EXT entry for FV UI name
327 # This GUID is used: A67DF1FA-8DE8-4E98-AF09-4BDF2EFFBC7C
329 FvUiLen
= len(self
.UiFvName
)
330 TotalSize
+= (FvUiLen
+ 16 + 4)
331 Guid
= FV_UI_EXT_ENTY_GUID
.split('-')
334 # EFI_FIRMWARE_VOLUME_EXT_ENTRY : size 4
338 Buffer
+= (pack('HH', (FvUiLen
+ 16 + 4), 0x0002)
339 + pack('=LHHBBBBBBBB', int(Guid
[0], 16), int(Guid
[1], 16), int(Guid
[2], 16),
340 int(Guid
[3][-4:-2], 16), int(Guid
[3][-2:], 16), int(Guid
[4][-12:-10], 16),
341 int(Guid
[4][-10:-8], 16), int(Guid
[4][-8:-6], 16), int(Guid
[4][-6:-4], 16),
342 int(Guid
[4][-4:-2], 16), int(Guid
[4][-2:], 16))
345 for Index
in range (0, len(self
.FvExtEntryType
)):
346 if self
.FvExtEntryType
[Index
] == 'FILE':
347 # check if the path is absolute or relative
348 if os
.path
.isabs(self
.FvExtEntryData
[Index
]):
349 FileFullPath
= os
.path
.normpath(self
.FvExtEntryData
[Index
])
351 FileFullPath
= os
.path
.normpath(os
.path
.join(GenFdsGlobalVariable
.WorkSpaceDir
, self
.FvExtEntryData
[Index
]))
352 # check if the file path exists or not
353 if not os
.path
.isfile(FileFullPath
):
354 GenFdsGlobalVariable
.ErrorLogger("Error opening FV Extension Header Entry file %s." % (self
.FvExtEntryData
[Index
]))
355 FvExtFile
= open (FileFullPath
,'rb')
357 Size
= FvExtFile
.tell()
359 GenFdsGlobalVariable
.ErrorLogger("The size of FV Extension Header Entry file %s exceeds 0x10000." % (self
.FvExtEntryData
[Index
]))
360 TotalSize
+= (Size
+ 4)
362 Buffer
+= pack('HH', (Size
+ 4), int(self
.FvExtEntryTypeValue
[Index
], 16))
363 Buffer
+= FvExtFile
.read()
365 if self
.FvExtEntryType
[Index
] == 'DATA':
366 ByteList
= self
.FvExtEntryData
[Index
].split(',')
367 Size
= len (ByteList
)
369 GenFdsGlobalVariable
.ErrorLogger("The size of FV Extension Header Entry data %s exceeds 0x10000." % (self
.FvExtEntryData
[Index
]))
370 TotalSize
+= (Size
+ 4)
371 Buffer
+= pack('HH', (Size
+ 4), int(self
.FvExtEntryTypeValue
[Index
], 16))
372 for Index1
in range (0, Size
):
373 Buffer
+= pack('B', int(ByteList
[Index1
], 16))
375 Guid
= self
.FvNameGuid
.split('-')
376 Buffer
= pack('=LHHBBBBBBBBL',
380 int(Guid
[3][-4:-2], 16),
381 int(Guid
[3][-2:], 16),
382 int(Guid
[4][-12:-10], 16),
383 int(Guid
[4][-10:-8], 16),
384 int(Guid
[4][-8:-6], 16),
385 int(Guid
[4][-6:-4], 16),
386 int(Guid
[4][-4:-2], 16),
387 int(Guid
[4][-2:], 16),
392 # Generate FV extension header file if the total size is not zero
395 FvExtHeaderFileName
= os
.path
.join(GenFdsGlobalVariable
.FvDir
, self
.UiFvName
+ '.ext')
396 FvExtHeaderFile
= StringIO
.StringIO()
397 FvExtHeaderFile
.write(Buffer
)
398 Changed
= SaveFileOnChange(FvExtHeaderFileName
, FvExtHeaderFile
.getvalue(), True)
399 FvExtHeaderFile
.close()
401 if os
.path
.exists (self
.InfFileName
):
402 os
.remove (self
.InfFileName
)
403 self
.FvInfFile
.writelines("EFI_FV_EXT_HEADER_FILE_NAME = " + \
404 FvExtHeaderFileName
+ \
411 self
.FvInfFile
.writelines("[files]" + T_CHAR_LF
)
412 if VtfDict
!= None and self
.UiFvName
in VtfDict
.keys():
413 self
.FvInfFile
.writelines("EFI_FILE_NAME = " + \
414 VtfDict
.get(self
.UiFvName
) + \