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