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