]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/GenFds/Fv.py
Check In tool source code based on Build tool project revision r1655.
[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
48 ## AddToBuffer()
49 #
50 # Generate Fv and add it to the Buffer
51 #
52 # @param self The object pointer
53 # @param Buffer The buffer generated FV data will be put
54 # @param BaseAddress base address of FV
55 # @param BlockSize block size of FV
56 # @param BlockNum How many blocks in FV
57 # @param ErasePolarity Flash erase polarity
58 # @param VtfDict VTF objects
59 # @param MacroDict macro value pair
60 # @retval string Generated FV file path
61 #
62 def AddToBuffer (self, Buffer, BaseAddress=None, BlockSize= None, BlockNum=None, ErasePloarity='1', VtfDict=None, MacroDict = {}) :
63
64 if self.UiFvName.upper() in GenFds.FvBinDict.keys():
65 return GenFds.FvBinDict[self.UiFvName.upper()]
66
67 GenFdsGlobalVariable.InfLogger( "\nGenerating %s FV ..." %self.UiFvName)
68
69 self.__InitializeInf__(BaseAddress, BlockSize, BlockNum, ErasePloarity, VtfDict)
70 #
71 # First Process the Apriori section
72 #
73 MacroDict.update(self.DefineVarDict)
74
75 GenFdsGlobalVariable.VerboseLogger('First generate Apriori file !')
76 FfsFileList = []
77 for AprSection in self.AprioriSectionList:
78 FileName = AprSection.GenFfs (self.UiFvName, MacroDict)
79 FfsFileList.append(FileName)
80 # Add Apriori file name to Inf file
81 self.FvInfFile.writelines("EFI_FILE_NAME = " + \
82 FileName + \
83 T_CHAR_LF)
84
85 # Process Modules in FfsList
86 for FfsFile in self.FfsList :
87 FileName = FfsFile.GenFfs(MacroDict)
88 FfsFileList.append(FileName)
89 self.FvInfFile.writelines("EFI_FILE_NAME = " + \
90 FileName + \
91 T_CHAR_LF)
92
93 SaveFileOnChange(self.InfFileName, self.FvInfFile.getvalue(), False)
94 self.FvInfFile.close()
95 #
96 # Call GenFv tool
97 #
98 FvOutputFile = os.path.join(GenFdsGlobalVariable.FvDir, self.UiFvName)
99 FvOutputFile = FvOutputFile + '.Fv'
100 # BUGBUG: FvOutputFile could be specified from FDF file (FV section, CreateFile statement)
101 if self.CreateFileName != None:
102 FvOutputFile = self.CreateFileName
103
104 FvInfoFileName = os.path.join(GenFdsGlobalVariable.FfsDir, self.UiFvName + '.inf')
105 shutil.copy(GenFdsGlobalVariable.FvAddressFileName, FvInfoFileName)
106 GenFdsGlobalVariable.GenerateFirmwareVolume(
107 FvOutputFile,
108 [self.InfFileName],
109 AddressFile=FvInfoFileName,
110 FfsList=FfsFileList
111 )
112
113 #
114 # Write the Fv contents to Buffer
115 #
116 FvFileObj = open ( FvOutputFile,'r+b')
117
118 GenFdsGlobalVariable.InfLogger( "\nGenerate %s FV Successfully" %self.UiFvName)
119 GenFdsGlobalVariable.SharpCounter = 0
120
121 Buffer.write(FvFileObj.read())
122 FvFileObj.close()
123 GenFds.FvBinDict[self.UiFvName.upper()] = FvOutputFile
124 return FvOutputFile
125
126 ## __InitializeInf__()
127 #
128 # Initilize the inf file to create FV
129 #
130 # @param self The object pointer
131 # @param BaseAddress base address of FV
132 # @param BlockSize block size of FV
133 # @param BlockNum How many blocks in FV
134 # @param ErasePolarity Flash erase polarity
135 # @param VtfDict VTF objects
136 #
137 def __InitializeInf__ (self, BaseAddress = None, BlockSize= None, BlockNum = None, ErasePloarity='1', VtfDict=None) :
138 #
139 # Create FV inf file
140 #
141 self.InfFileName = os.path.join(GenFdsGlobalVariable.FvDir,
142 self.UiFvName + '.inf')
143 self.FvInfFile = StringIO.StringIO()
144
145 #
146 # Add [Options]
147 #
148 self.FvInfFile.writelines("[options]" + T_CHAR_LF)
149 if BaseAddress != None :
150 self.FvInfFile.writelines("EFI_BASE_ADDRESS = " + \
151 BaseAddress + \
152 T_CHAR_LF)
153
154 if BlockSize != None:
155 self.FvInfFile.writelines("EFI_BLOCK_SIZE = " + \
156 '0x%X' %BlockSize + \
157 T_CHAR_LF)
158 if BlockNum != None:
159 self.FvInfFile.writelines("EFI_NUM_BLOCKS = " + \
160 ' 0x%X' %BlockNum + \
161 T_CHAR_LF)
162 else:
163 for BlockSize in self.BlockSizeList :
164 if BlockSize[0] != None:
165 self.FvInfFile.writelines("EFI_BLOCK_SIZE = " + \
166 '0x%X' %BlockSize[0] + \
167 T_CHAR_LF)
168
169 if BlockSize[1] != None:
170 self.FvInfFile.writelines("EFI_NUM_BLOCKS = " + \
171 ' 0x%X' %BlockSize[1] + \
172 T_CHAR_LF)
173
174 if self.BsBaseAddress != None:
175 self.FvInfFile.writelines('EFI_BOOT_DRIVER_BASE_ADDRESS = ' + \
176 '0x%X' %self.BsBaseAddress)
177 if self.RtBaseAddress != None:
178 self.FvInfFile.writelines('EFI_RUNTIME_DRIVER_BASE_ADDRESS = ' + \
179 '0x%X' %self.RtBaseAddress)
180 #
181 # Add attribute
182 #
183 self.FvInfFile.writelines("[attributes]" + T_CHAR_LF)
184
185 self.FvInfFile.writelines("EFI_ERASE_POLARITY = " + \
186 ' %s' %ErasePloarity + \
187 T_CHAR_LF)
188 if not (self.FvAttributeDict == None):
189 for FvAttribute in self.FvAttributeDict.keys() :
190 self.FvInfFile.writelines("EFI_" + \
191 FvAttribute + \
192 ' = ' + \
193 self.FvAttributeDict[FvAttribute] + \
194 T_CHAR_LF )
195 if self.FvAlignment != None:
196 self.FvInfFile.writelines("EFI_FVB2_ALIGNMENT_" + \
197 self.FvAlignment.strip() + \
198 " = TRUE" + \
199 T_CHAR_LF)
200
201 if self.FvNameGuid != None:
202 self.FvInfFile.writelines("EFI_FVNAME_GUID" + \
203 " = %s" % self.FvNameGuid + \
204 T_CHAR_LF)
205 #
206 # Add [Files]
207 #
208
209 self.FvInfFile.writelines("[files]" + T_CHAR_LF)
210 if VtfDict != None and self.UiFvName in VtfDict.keys():
211 self.FvInfFile.writelines("EFI_FILE_NAME = " + \
212 VtfDict.get(self.UiFvName) + \
213 T_CHAR_LF)
214
215