]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/GenFds/Region.py
Sync EDKII BaseTools to BaseTools project r2093.
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / Region.py
1 ## @file
2 # process FD Region generation
3 #
4 # Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
5 #
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
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 from struct import *
19 from GenFdsGlobalVariable import GenFdsGlobalVariable
20 import StringIO
21 from CommonDataClass.FdfClass import RegionClassObject
22 import os
23 from stat import *
24 from Common import EdkLogger
25 from Common.BuildToolError import *
26
27 ## generate Region
28 #
29 #
30 class Region(RegionClassObject):
31
32 ## The constructor
33 #
34 # @param self The object pointer
35 #
36 def __init__(self):
37 RegionClassObject.__init__(self)
38
39
40 ## AddToBuffer()
41 #
42 # Add region data to the Buffer
43 #
44 # @param self The object pointer
45 # @param Buffer The buffer generated region data will be put
46 # @param BaseAddress base address of region
47 # @param BlockSize block size of region
48 # @param BlockNum How many blocks in region
49 # @param ErasePolarity Flash erase polarity
50 # @param VtfDict VTF objects
51 # @param MacroDict macro value pair
52 # @retval string Generated FV file path
53 #
54
55 def AddToBuffer(self, Buffer, BaseAddress, BlockSizeList, ErasePolarity, ImageBinDict, vtfDict = None, MacroDict = {}):
56 Size = self.Size
57 GenFdsGlobalVariable.InfLogger('\nGenerate Region at Offset 0x%X' % self.Offset)
58 GenFdsGlobalVariable.InfLogger(" Region Size = 0x%X" %Size)
59 GenFdsGlobalVariable.SharpCounter = 0
60
61 if self.RegionType == 'FV':
62 #
63 # Get Fv from FvDict
64 #
65 self.FvAddress = int(BaseAddress, 16) + self.Offset
66 FvBaseAddress = '0x%X' %self.FvAddress
67 FvOffset = 0
68 for RegionData in self.RegionDataList:
69 FileName = None
70 if RegionData.endswith(".fv"):
71 RegionData = GenFdsGlobalVariable.MacroExtend(RegionData, MacroDict)
72 GenFdsGlobalVariable.InfLogger(' Region FV File Name = .fv : %s'%RegionData)
73 if RegionData[1] != ':' :
74 RegionData = os.path.join (GenFdsGlobalVariable.WorkSpaceDir, RegionData)
75 if not os.path.exists(RegionData):
76 EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=RegionData)
77
78 FileName = RegionData
79 elif RegionData.upper() + 'fv' in ImageBinDict.keys():
80 GenFdsGlobalVariable.InfLogger(' Region Name = FV')
81 FileName = ImageBinDict[RegionData.upper() + 'fv']
82 else:
83 #
84 # Generate FvImage.
85 #
86 FvObj = None
87 if RegionData.upper() in GenFdsGlobalVariable.FdfParser.Profile.FvDict.keys():
88 FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict.get(RegionData.upper())
89
90 if FvObj != None :
91 GenFdsGlobalVariable.InfLogger(' Region Name = FV')
92 #
93 # Call GenFv tool
94 #
95 self.BlockInfoOfRegion(BlockSizeList, FvObj)
96 self.FvAddress = self.FvAddress + FvOffset
97 FvAlignValue = self.GetFvAlignValue(FvObj.FvAlignment)
98 if self.FvAddress % FvAlignValue != 0:
99 EdkLogger.error("GenFds", GENFDS_ERROR,
100 "FV (%s) is NOT %s Aligned!" % (FvObj.UiFvName, FvObj.FvAlignment))
101 FvBuffer = StringIO.StringIO('')
102 FvBaseAddress = '0x%X' %self.FvAddress
103 BlockSize = None
104 BlockNum = None
105 FvObj.AddToBuffer(FvBuffer, FvBaseAddress, BlockSize, BlockNum, ErasePolarity, vtfDict)
106 if FvBuffer.len > Size:
107 FvBuffer.close()
108 EdkLogger.error("GenFds", GENFDS_ERROR,
109 "Size of FV (%s) is larger than Region Size 0x%X specified." % (RegionData, Size))
110 #
111 # Put the generated image into FD buffer.
112 #
113 Buffer.write(FvBuffer.getvalue())
114 FvBuffer.close()
115 FvOffset = FvOffset + FvBuffer.len
116 Size = Size - FvBuffer.len
117 continue
118 else:
119 EdkLogger.error("GenFds", GENFDS_ERROR, "FV (%s) is NOT described in FDF file!" % (RegionData))
120 #
121 # Add the exist Fv image into FD buffer
122 #
123 if FileName != None:
124 FileLength = os.stat(FileName)[ST_SIZE]
125 if FileLength > Size:
126 EdkLogger.error("GenFds", GENFDS_ERROR,
127 "Size of FV File (%s) is larger than Region Size 0x%X specified." \
128 % (RegionData, Size))
129 BinFile = open (FileName, 'r+b')
130 Buffer.write(BinFile.read())
131 BinFile.close()
132 Size = Size - FileLength
133 #
134 # Pad the left buffer
135 #
136 if Size > 0:
137 if (ErasePolarity == '1') :
138 PadData = 0xFF
139 else :
140 PadData = 0
141 for i in range(0, Size):
142 Buffer.write(pack('B', PadData))
143
144 if self.RegionType == 'CAPSULE':
145 #
146 # Get Capsule from Capsule Dict
147 #
148 for RegionData in self.RegionDataList:
149 if RegionData.endswith(".cap"):
150 RegionData = GenFdsGlobalVariable.MacroExtend(RegionData, MacroDict)
151 GenFdsGlobalVariable.InfLogger(' Region CAPSULE Image Name = .cap : %s'%RegionData)
152 if RegionData[1] != ':' :
153 RegionData = os.path.join (GenFdsGlobalVariable.WorkSpaceDir, RegionData)
154 if not os.path.exists(RegionData):
155 EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=RegionData)
156
157 FileName = RegionData
158 elif RegionData.upper() + 'cap' in ImageBinDict.keys():
159 GenFdsGlobalVariable.InfLogger(' Region Name = CAPSULE')
160 FileName = ImageBinDict[RegionData.upper() + 'cap']
161 else:
162 #
163 # Generate Capsule image and Put it into FD buffer
164 #
165 CapsuleObj = None
166 if RegionData.upper() in GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict.keys():
167 CapsuleObj = GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict[RegionData.upper()]
168
169 if CapsuleObj != None :
170 CapsuleObj.CapsuleName = RegionData.upper()
171 GenFdsGlobalVariable.InfLogger(' Region Name = CAPSULE')
172 #
173 # Call GenFv tool to generate Capsule Image
174 #
175 FileName = CapsuleObj.GenCapsule()
176 CapsuleObj.CapsuleName = None
177 else:
178 EdkLogger.error("GenFds", GENFDS_ERROR, "Capsule (%s) is NOT described in FDF file!" % (RegionData))
179
180 #
181 # Add the capsule image into FD buffer
182 #
183 FileLength = os.stat(FileName)[ST_SIZE]
184 if FileLength > Size:
185 EdkLogger.error("GenFds", GENFDS_ERROR,
186 "Size 0x%X of Capsule File (%s) is larger than Region Size 0x%X specified." \
187 % (FileLength, RegionData, Size))
188 BinFile = open (FileName, 'r+b')
189 Buffer.write(BinFile.read())
190 BinFile.close()
191 Size = Size - FileLength
192 #
193 # Pad the left buffer
194 #
195 if Size > 0:
196 if (ErasePolarity == '1') :
197 PadData = 0xFF
198 else :
199 PadData = 0
200 for i in range(0, Size):
201 Buffer.write(pack('B', PadData))
202
203 if self.RegionType == 'FILE':
204 for RegionData in self.RegionDataList:
205 RegionData = GenFdsGlobalVariable.MacroExtend(RegionData, MacroDict)
206 if RegionData[1] != ':' :
207 RegionData = os.path.join (GenFdsGlobalVariable.WorkSpaceDir, RegionData)
208 if not os.path.exists(RegionData):
209 EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=RegionData)
210 #
211 # Add the file image into FD buffer
212 #
213 FileLength = os.stat(RegionData)[ST_SIZE]
214 if FileLength > Size:
215 EdkLogger.error("GenFds", GENFDS_ERROR,
216 "Size of File (%s) is larger than Region Size 0x%X specified." \
217 % (RegionData, Size))
218 GenFdsGlobalVariable.InfLogger(' Region File Name = %s'%RegionData)
219 BinFile = open (RegionData, 'r+b')
220 Buffer.write(BinFile.read())
221 BinFile.close()
222 Size = Size - FileLength
223 #
224 # Pad the left buffer
225 #
226 if Size > 0:
227 if (ErasePolarity == '1') :
228 PadData = 0xFF
229 else :
230 PadData = 0
231 for i in range(0, Size):
232 Buffer.write(pack('B', PadData))
233
234 if self.RegionType == 'DATA' :
235 GenFdsGlobalVariable.InfLogger(' Region Name = DATA')
236 DataSize = 0
237 for RegionData in self.RegionDataList:
238 Data = RegionData.split(',')
239 DataSize = DataSize + len(Data)
240 if DataSize > Size:
241 EdkLogger.error("GenFds", GENFDS_ERROR, "Size of DATA is larger than Region Size ")
242 else:
243 for item in Data :
244 Buffer.write(pack('B', int(item, 16)))
245 Size = Size - DataSize
246 #
247 # Pad the left buffer
248 #
249 if Size > 0:
250 if (ErasePolarity == '1') :
251 PadData = 0xFF
252 else :
253 PadData = 0
254 for i in range(0, Size):
255 Buffer.write(pack('B', PadData))
256
257 if self.RegionType == None:
258 GenFdsGlobalVariable.InfLogger(' Region Name = None')
259 if (ErasePolarity == '1') :
260 PadData = 0xFF
261 else :
262 PadData = 0
263 for i in range(0, Size):
264 Buffer.write(pack('B', PadData))
265
266 def GetFvAlignValue(self, Str):
267 AlignValue = 1
268 Granu = 1
269 Str = Str.strip().upper()
270 if Str.endswith('K'):
271 Granu = 1024
272 Str = Str[:-1]
273 elif Str.endswith('M'):
274 Granu = 1024*1024
275 Str = Str[:-1]
276 elif Str.endswith('G'):
277 Granu = 1024*1024*1024
278 Str = Str[:-1]
279 else:
280 pass
281
282 AlignValue = int(Str)*Granu
283 return AlignValue
284
285 ## BlockSizeOfRegion()
286 #
287 # @param BlockSizeList List of block information
288 # @param FvObj The object for FV
289 #
290 def BlockInfoOfRegion(self, BlockSizeList, FvObj):
291 Start = 0
292 End = 0
293 RemindingSize = self.Size
294 ExpectedList = []
295 for (BlockSize, BlockNum, pcd) in BlockSizeList:
296 End = Start + BlockSize * BlockNum
297 # region not started yet
298 if self.Offset >= End:
299 Start = End
300 continue
301 # region located in current blocks
302 else:
303 # region ended within current blocks
304 if self.Offset + self.Size <= End:
305 ExpectedList.append((BlockSize, (RemindingSize + BlockSize - 1)/BlockSize))
306 break
307 # region not ended yet
308 else:
309 # region not started in middle of current blocks
310 if self.Offset <= Start:
311 UsedBlockNum = BlockNum
312 # region started in middle of current blocks
313 else:
314 UsedBlockNum = (End - self.Offset)/BlockSize
315 Start = End
316 ExpectedList.append((BlockSize, UsedBlockNum))
317 RemindingSize -= BlockSize * UsedBlockNum
318
319 if FvObj.BlockSizeList == []:
320 FvObj.BlockSizeList = ExpectedList
321 else:
322 # first check whether FvObj.BlockSizeList items have only "BlockSize" or "NumBlocks",
323 # if so, use ExpectedList
324 for Item in FvObj.BlockSizeList:
325 if Item[0] == None or Item[1] == None:
326 FvObj.BlockSizeList = ExpectedList
327 break
328 # make sure region size is no smaller than the summed block size in FV
329 Sum = 0
330 for Item in FvObj.BlockSizeList:
331 Sum += Item[0] * Item[1]
332 if self.Size < Sum:
333 EdkLogger.error("GenFds", GENFDS_ERROR, "Total Size of FV %s 0x%x is larger than Region Size 0x%x "
334 %(FvObj.UiFvName, Sum, self.Size))
335 # check whether the BlockStatements in FV section is appropriate
336 ExpectedListData = ''
337 for Item in ExpectedList:
338 ExpectedListData += "BlockSize = 0x%x\n\tNumBlocks = 0x%x\n\t"%Item
339 Index = 0
340 for Item in FvObj.BlockSizeList:
341 if Item[0] != ExpectedList[Index][0]:
342 EdkLogger.error("GenFds", GENFDS_ERROR, "BlockStatements of FV %s are not align with FD's, suggested FV BlockStatement"
343 %FvObj.UiFvName, ExtraData = ExpectedListData)
344 elif Item[1] != ExpectedList[Index][1]:
345 if (Item[1] < ExpectedList[Index][1]) and (Index == len(FvObj.BlockSizeList) - 1):
346 break;
347 else:
348 EdkLogger.error("GenFds", GENFDS_ERROR, "BlockStatements of FV %s are not align with FD's, suggested FV BlockStatement"
349 %FvObj.UiFvName, ExtraData = ExpectedListData)
350 else:
351 Index += 1
352
353
354