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