Change the GUID value of FVB to be that of FVB2 in PI 1.2 spec. this will force FVB...
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / Fv.py
CommitLineData
30fdf114
LG
1## @file\r
2# process FV generation\r
3#\r
4# Copyright (c) 2007, Intel Corporation\r
5#\r
6# All rights reserved. This program and the accompanying materials\r
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
18import os\r
19import shutil\r
20import subprocess\r
21import StringIO\r
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
29\r
30T_CHAR_LF = '\n'\r
31\r
32## generate FV\r
33#\r
34#\r
35class FV (FvClassObject):\r
36 ## The constructor\r
37 #\r
38 # @param self The object pointer\r
39 #\r
40 def __init__(self):\r
41 FvClassObject.__init__(self)\r
42 self.FvInfFile = None\r
43 self.FvAddressFile = None\r
44 self.BaseAddress = None\r
45 self.InfFileName = None\r
46 self.FvAddressFileName = None\r
fd171542 47 self.CapsuleName = None\r
30fdf114
LG
48\r
49 ## AddToBuffer()\r
50 #\r
51 # Generate Fv and add it to the Buffer\r
52 #\r
53 # @param self The object pointer\r
54 # @param Buffer The buffer generated FV data will be put\r
55 # @param BaseAddress base address of FV\r
56 # @param BlockSize block size of FV\r
57 # @param BlockNum How many blocks in FV\r
58 # @param ErasePolarity Flash erase polarity\r
59 # @param VtfDict VTF objects\r
60 # @param MacroDict macro value pair\r
61 # @retval string Generated FV file path\r
62 #\r
63 def AddToBuffer (self, Buffer, BaseAddress=None, BlockSize= None, BlockNum=None, ErasePloarity='1', VtfDict=None, MacroDict = {}) :\r
64\r
fd171542 65 if self.UiFvName.upper() + 'fv' in GenFds.ImageBinDict.keys():\r
66 return GenFds.ImageBinDict[self.UiFvName.upper() + 'fv']\r
67 \r
68 #\r
69 # Check whether FV in Capsule is in FD flash region.\r
70 # If yes, return error. Doesn't support FV in Capsule image is also in FD flash region.\r
71 #\r
72 if self.CapsuleName != None:\r
73 for FdName in GenFdsGlobalVariable.FdfParser.Profile.FdDict.keys():
74 FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict[FdName]
75 for RegionObj in FdObj.RegionList:\r
76 if RegionObj.RegionType == 'FV':\r
77 for RegionData in RegionObj.RegionDataList:\r
78 if RegionData.endswith(".fv"):\r
79 continue\r
80 elif RegionData.upper() + 'fv' in GenFds.ImageBinDict.keys():\r
81 continue\r
82 elif self.UiFvName.upper() == RegionData.upper():\r
83 GenFdsGlobalVariable.ErrorLogger("Capsule %s in FD region can't contain a FV %s in FD region." % (self.CapsuleName, self.UiFvName.upper()))\r
30fdf114 84\r
fd171542 85 GenFdsGlobalVariable.InfLogger( "\nGenerating %s FV" %self.UiFvName)\r
30fdf114
LG
86\r
87 self.__InitializeInf__(BaseAddress, BlockSize, BlockNum, ErasePloarity, VtfDict)\r
88 #\r
89 # First Process the Apriori section\r
90 #\r
91 MacroDict.update(self.DefineVarDict)\r
92\r
93 GenFdsGlobalVariable.VerboseLogger('First generate Apriori file !')\r
94 FfsFileList = []\r
95 for AprSection in self.AprioriSectionList:\r
96 FileName = AprSection.GenFfs (self.UiFvName, MacroDict)\r
97 FfsFileList.append(FileName)\r
98 # Add Apriori file name to Inf file\r
99 self.FvInfFile.writelines("EFI_FILE_NAME = " + \\r
100 FileName + \\r
101 T_CHAR_LF)\r
102\r
103 # Process Modules in FfsList\r
104 for FfsFile in self.FfsList :\r
105 FileName = FfsFile.GenFfs(MacroDict)\r
106 FfsFileList.append(FileName)\r
107 self.FvInfFile.writelines("EFI_FILE_NAME = " + \\r
108 FileName + \\r
109 T_CHAR_LF)\r
110\r
111 SaveFileOnChange(self.InfFileName, self.FvInfFile.getvalue(), False)\r
112 self.FvInfFile.close()\r
113 #\r
114 # Call GenFv tool\r
115 #\r
116 FvOutputFile = os.path.join(GenFdsGlobalVariable.FvDir, self.UiFvName)\r
117 FvOutputFile = FvOutputFile + '.Fv'\r
118 # BUGBUG: FvOutputFile could be specified from FDF file (FV section, CreateFile statement)\r
119 if self.CreateFileName != None:\r
120 FvOutputFile = self.CreateFileName\r
121\r
122 FvInfoFileName = os.path.join(GenFdsGlobalVariable.FfsDir, self.UiFvName + '.inf')\r
123 shutil.copy(GenFdsGlobalVariable.FvAddressFileName, FvInfoFileName)\r
124 GenFdsGlobalVariable.GenerateFirmwareVolume(\r
125 FvOutputFile,\r
126 [self.InfFileName],\r
127 AddressFile=FvInfoFileName,\r
128 FfsList=FfsFileList\r
129 )\r
130\r
131 #\r
132 # Write the Fv contents to Buffer\r
133 #\r
134 FvFileObj = open ( FvOutputFile,'r+b')\r
135\r
fd171542 136 GenFdsGlobalVariable.VerboseLogger( "\nGenerate %s FV Successfully" %self.UiFvName)\r
30fdf114
LG
137 GenFdsGlobalVariable.SharpCounter = 0\r
138\r
139 Buffer.write(FvFileObj.read())\r
140 FvFileObj.close()\r
fd171542 141 GenFds.ImageBinDict[self.UiFvName.upper() + 'fv'] = FvOutputFile\r
30fdf114
LG
142 return FvOutputFile\r
143\r
144 ## __InitializeInf__()\r
145 #\r
146 # Initilize the inf file to create FV\r
147 #\r
148 # @param self The object pointer\r
149 # @param BaseAddress base address of FV\r
150 # @param BlockSize block size of FV\r
151 # @param BlockNum How many blocks in FV\r
152 # @param ErasePolarity Flash erase polarity\r
153 # @param VtfDict VTF objects\r
154 #\r
155 def __InitializeInf__ (self, BaseAddress = None, BlockSize= None, BlockNum = None, ErasePloarity='1', VtfDict=None) :\r
156 #\r
157 # Create FV inf file\r
158 #\r
159 self.InfFileName = os.path.join(GenFdsGlobalVariable.FvDir,\r
160 self.UiFvName + '.inf')\r
161 self.FvInfFile = StringIO.StringIO()\r
162\r
163 #\r
164 # Add [Options]\r
165 #\r
166 self.FvInfFile.writelines("[options]" + T_CHAR_LF)\r
167 if BaseAddress != None :\r
168 self.FvInfFile.writelines("EFI_BASE_ADDRESS = " + \\r
169 BaseAddress + \\r
170 T_CHAR_LF)\r
171\r
172 if BlockSize != None:\r
173 self.FvInfFile.writelines("EFI_BLOCK_SIZE = " + \\r
174 '0x%X' %BlockSize + \\r
175 T_CHAR_LF)\r
176 if BlockNum != None:\r
177 self.FvInfFile.writelines("EFI_NUM_BLOCKS = " + \\r
178 ' 0x%X' %BlockNum + \\r
179 T_CHAR_LF)\r
180 else:\r
181 for BlockSize in self.BlockSizeList :\r
182 if BlockSize[0] != None:\r
183 self.FvInfFile.writelines("EFI_BLOCK_SIZE = " + \\r
184 '0x%X' %BlockSize[0] + \\r
185 T_CHAR_LF)\r
186\r
187 if BlockSize[1] != None:\r
188 self.FvInfFile.writelines("EFI_NUM_BLOCKS = " + \\r
189 ' 0x%X' %BlockSize[1] + \\r
190 T_CHAR_LF)\r
191\r
192 if self.BsBaseAddress != None:\r
193 self.FvInfFile.writelines('EFI_BOOT_DRIVER_BASE_ADDRESS = ' + \\r
194 '0x%X' %self.BsBaseAddress)\r
195 if self.RtBaseAddress != None:\r
196 self.FvInfFile.writelines('EFI_RUNTIME_DRIVER_BASE_ADDRESS = ' + \\r
197 '0x%X' %self.RtBaseAddress)\r
198 #\r
199 # Add attribute\r
200 #\r
201 self.FvInfFile.writelines("[attributes]" + T_CHAR_LF)\r
202\r
203 self.FvInfFile.writelines("EFI_ERASE_POLARITY = " + \\r
204 ' %s' %ErasePloarity + \\r
205 T_CHAR_LF)\r
206 if not (self.FvAttributeDict == None):\r
207 for FvAttribute in self.FvAttributeDict.keys() :\r
208 self.FvInfFile.writelines("EFI_" + \\r
209 FvAttribute + \\r
210 ' = ' + \\r
211 self.FvAttributeDict[FvAttribute] + \\r
212 T_CHAR_LF )\r
213 if self.FvAlignment != None:\r
214 self.FvInfFile.writelines("EFI_FVB2_ALIGNMENT_" + \\r
215 self.FvAlignment.strip() + \\r
216 " = TRUE" + \\r
217 T_CHAR_LF)\r
218 \r
219 if self.FvNameGuid != None:\r
220 self.FvInfFile.writelines("EFI_FVNAME_GUID" + \\r
221 " = %s" % self.FvNameGuid + \\r
222 T_CHAR_LF)\r
223 #\r
224 # Add [Files]\r
225 #\r
226\r
227 self.FvInfFile.writelines("[files]" + T_CHAR_LF)\r
228 if VtfDict != None and self.UiFvName in VtfDict.keys():\r
229 self.FvInfFile.writelines("EFI_FILE_NAME = " + \\r
230 VtfDict.get(self.UiFvName) + \\r
231 T_CHAR_LF)\r
232\r
233\r