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