]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/GenFds/Region.py
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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 # SPDX-License-Identifier: BSD-2-Clause-Patent
7 #
8
9 ##
10 # Import Modules
11 #
12 from __future__ import absolute_import
13 from struct import *
14 from .GenFdsGlobalVariable import GenFdsGlobalVariable
15 from io import BytesIO
16 import string
17 import Common.LongFilePathOs as os
18 from stat import *
19 from Common import EdkLogger
20 from Common.BuildToolError import *
21 from Common.LongFilePathSupport import OpenLongFilePath as open
22 from Common.MultipleWorkspace import MultipleWorkspace as mws
23 from Common.DataType import BINARY_FILE_TYPE_FV
24
25 ## generate Region
26 #
27 #
28 class Region(object):
29
30 ## The constructor
31 #
32 # @param self The object pointer
33 #
34 def __init__(self):
35 self.Offset = None # The begin position of the Region
36 self.Size = None # The Size of the Region
37 self.PcdOffset = None
38 self.PcdSize = None
39 self.SetVarDict = {}
40 self.RegionType = None
41 self.RegionDataList = []
42
43 ## PadBuffer()
44 #
45 # Add padding bytes to the Buffer
46 #
47 # @param Buffer The buffer the generated region data will be put
48 # in
49 # @param ErasePolarity Flash erase polarity
50 # @param Size Number of padding bytes requested
51 #
52
53 def PadBuffer(self, Buffer, ErasePolarity, Size):
54 if Size > 0:
55 if (ErasePolarity == '1') :
56 PadByte = pack('B', 0xFF)
57 else:
58 PadByte = pack('B', 0)
59 for i in range(0, Size):
60 Buffer.write(PadByte)
61
62 ## AddToBuffer()
63 #
64 # Add region data to the Buffer
65 #
66 # @param self The object pointer
67 # @param Buffer The buffer generated region data will be put
68 # @param BaseAddress base address of region
69 # @param BlockSize block size of region
70 # @param BlockNum How many blocks in region
71 # @param ErasePolarity Flash erase polarity
72 # @param MacroDict macro value pair
73 # @retval string Generated FV file path
74 #
75
76 def AddToBuffer(self, Buffer, BaseAddress, BlockSizeList, ErasePolarity, ImageBinDict, MacroDict=None, Flag=False):
77 Size = self.Size
78 if MacroDict is None:
79 MacroDict = {}
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 = GenFdsGlobalVariable.GetAlignment(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, 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 ## BlockSizeOfRegion()
280 #
281 # @param BlockSizeList List of block information
282 # @param FvObj The object for FV
283 #
284 def BlockInfoOfRegion(self, BlockSizeList, FvObj):
285 Start = 0
286 End = 0
287 RemindingSize = self.Size
288 ExpectedList = []
289 for (BlockSize, BlockNum, pcd) in BlockSizeList:
290 End = Start + BlockSize * BlockNum
291 # region not started yet
292 if self.Offset >= End:
293 Start = End
294 continue
295 # region located in current blocks
296 else:
297 # region ended within current blocks
298 if self.Offset + self.Size <= End:
299 ExpectedList.append((BlockSize, (RemindingSize + BlockSize - 1) // BlockSize))
300 break
301 # region not ended yet
302 else:
303 # region not started in middle of current blocks
304 if self.Offset <= Start:
305 UsedBlockNum = BlockNum
306 # region started in middle of current blocks
307 else:
308 UsedBlockNum = (End - self.Offset) // BlockSize
309 Start = End
310 ExpectedList.append((BlockSize, UsedBlockNum))
311 RemindingSize -= BlockSize * UsedBlockNum
312
313 if FvObj.BlockSizeList == []:
314 FvObj.BlockSizeList = ExpectedList
315 else:
316 # first check whether FvObj.BlockSizeList items have only "BlockSize" or "NumBlocks",
317 # if so, use ExpectedList
318 for Item in FvObj.BlockSizeList:
319 if Item[0] is None or Item[1] is None:
320 FvObj.BlockSizeList = ExpectedList
321 break
322 # make sure region size is no smaller than the summed block size in FV
323 Sum = 0
324 for Item in FvObj.BlockSizeList:
325 Sum += Item[0] * Item[1]
326 if self.Size < Sum:
327 EdkLogger.error("GenFds", GENFDS_ERROR, "Total Size of FV %s 0x%x is larger than Region Size 0x%x "
328 % (FvObj.UiFvName, Sum, self.Size))
329 # check whether the BlockStatements in FV section is appropriate
330 ExpectedListData = ''
331 for Item in ExpectedList:
332 ExpectedListData += "BlockSize = 0x%x\n\tNumBlocks = 0x%x\n\t" % Item
333 Index = 0
334 for Item in FvObj.BlockSizeList:
335 if Item[0] != ExpectedList[Index][0]:
336 EdkLogger.error("GenFds", GENFDS_ERROR, "BlockStatements of FV %s are not align with FD's, suggested FV BlockStatement"
337 % FvObj.UiFvName, ExtraData=ExpectedListData)
338 elif Item[1] != ExpectedList[Index][1]:
339 if (Item[1] < ExpectedList[Index][1]) and (Index == len(FvObj.BlockSizeList) - 1):
340 break;
341 else:
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 else:
345 Index += 1
346
347
348