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