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