]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/GenFds/Region.py
2 # process FD Region generation
4 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
6 # SPDX-License-Identifier: BSD-2-Clause-Patent
12 from __future__
import absolute_import
14 from .GenFdsGlobalVariable
import GenFdsGlobalVariable
15 from io
import BytesIO
17 import Common
.LongFilePathOs
as os
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
32 # @param self The object pointer
35 self
.Offset
= None # The begin position of the Region
36 self
.Size
= None # The Size of the Region
40 self
.RegionType
= None
41 self
.RegionDataList
= []
45 # Add padding bytes to the Buffer
47 # @param Buffer The buffer the generated region data will be put
49 # @param ErasePolarity Flash erase polarity
50 # @param Size Number of padding bytes requested
53 def PadBuffer(self
, Buffer
, ErasePolarity
, Size
):
55 if (ErasePolarity
== '1') :
56 PadByte
= pack('B', 0xFF)
58 PadByte
= pack('B', 0)
59 for i
in range(0, Size
):
64 # Add region data to the Buffer
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
76 def AddToBuffer(self
, Buffer
, BaseAddress
, BlockSizeList
, ErasePolarity
, ImageBinDict
, MacroDict
={}, Flag
=False):
79 GenFdsGlobalVariable
.InfLogger('\nGenerate Region at Offset 0x%X' % self
.Offset
)
80 GenFdsGlobalVariable
.InfLogger(" Region Size = 0x%X" % Size
)
81 GenFdsGlobalVariable
.SharpCounter
= 0
82 if Flag
and (self
.RegionType
!= BINARY_FILE_TYPE_FV
):
85 if self
.RegionType
== BINARY_FILE_TYPE_FV
:
89 self
.FvAddress
= int(BaseAddress
, 16) + self
.Offset
90 FvBaseAddress
= '0x%X' % self
.FvAddress
92 for RegionData
in self
.RegionDataList
:
94 if RegionData
.endswith(".fv"):
95 RegionData
= GenFdsGlobalVariable
.MacroExtend(RegionData
, MacroDict
)
97 GenFdsGlobalVariable
.InfLogger(' Region FV File Name = .fv : %s' % RegionData
)
98 if RegionData
[1] != ':' :
99 RegionData
= mws
.join (GenFdsGlobalVariable
.WorkSpaceDir
, RegionData
)
100 if not os
.path
.exists(RegionData
):
101 EdkLogger
.error("GenFds", FILE_NOT_FOUND
, ExtraData
=RegionData
)
103 FileName
= RegionData
104 elif RegionData
.upper() + 'fv' in ImageBinDict
:
106 GenFdsGlobalVariable
.InfLogger(' Region Name = FV')
107 FileName
= ImageBinDict
[RegionData
.upper() + 'fv']
113 if RegionData
.upper() in GenFdsGlobalVariable
.FdfParser
.Profile
.FvDict
:
114 FvObj
= GenFdsGlobalVariable
.FdfParser
.Profile
.FvDict
[RegionData
.upper()]
116 if FvObj
is not None :
118 GenFdsGlobalVariable
.InfLogger(' Region Name = FV')
122 self
.BlockInfoOfRegion(BlockSizeList
, FvObj
)
123 self
.FvAddress
= self
.FvAddress
+ FvOffset
124 FvAlignValue
= GenFdsGlobalVariable
.GetAlignment(FvObj
.FvAlignment
)
125 if self
.FvAddress
% FvAlignValue
!= 0:
126 EdkLogger
.error("GenFds", GENFDS_ERROR
,
127 "FV (%s) is NOT %s Aligned!" % (FvObj
.UiFvName
, FvObj
.FvAlignment
))
129 FvBaseAddress
= '0x%X' % self
.FvAddress
132 FvObj
.AddToBuffer(FvBuffer
, FvBaseAddress
, BlockSize
, BlockNum
, ErasePolarity
, Flag
=Flag
)
136 FvBufferLen
= len(FvBuffer
.getvalue())
137 if FvBufferLen
> Size
:
139 EdkLogger
.error("GenFds", GENFDS_ERROR
,
140 "Size of FV (%s) is larger than Region Size 0x%X specified." % (RegionData
, Size
))
142 # Put the generated image into FD buffer.
144 Buffer
.write(FvBuffer
.getvalue())
146 FvOffset
= FvOffset
+ FvBufferLen
147 Size
= Size
- FvBufferLen
150 EdkLogger
.error("GenFds", GENFDS_ERROR
, "FV (%s) is NOT described in FDF file!" % (RegionData
))
152 # Add the exist Fv image into FD buffer
155 if FileName
is not None:
156 FileLength
= os
.stat(FileName
)[ST_SIZE
]
157 if FileLength
> Size
:
158 EdkLogger
.error("GenFds", GENFDS_ERROR
,
159 "Size of FV File (%s) is larger than Region Size 0x%X specified." \
160 % (RegionData
, Size
))
161 BinFile
= open(FileName
, 'rb')
162 Buffer
.write(BinFile
.read())
164 Size
= Size
- FileLength
166 # Pad the left buffer
169 self
.PadBuffer(Buffer
, ErasePolarity
, Size
)
171 if self
.RegionType
== 'CAPSULE':
173 # Get Capsule from Capsule Dict
175 for RegionData
in self
.RegionDataList
:
176 if RegionData
.endswith(".cap"):
177 RegionData
= GenFdsGlobalVariable
.MacroExtend(RegionData
, MacroDict
)
178 GenFdsGlobalVariable
.InfLogger(' Region CAPSULE Image Name = .cap : %s' % RegionData
)
179 if RegionData
[1] != ':' :
180 RegionData
= mws
.join (GenFdsGlobalVariable
.WorkSpaceDir
, RegionData
)
181 if not os
.path
.exists(RegionData
):
182 EdkLogger
.error("GenFds", FILE_NOT_FOUND
, ExtraData
=RegionData
)
184 FileName
= RegionData
185 elif RegionData
.upper() + 'cap' in ImageBinDict
:
186 GenFdsGlobalVariable
.InfLogger(' Region Name = CAPSULE')
187 FileName
= ImageBinDict
[RegionData
.upper() + 'cap']
190 # Generate Capsule image and Put it into FD buffer
193 if RegionData
.upper() in GenFdsGlobalVariable
.FdfParser
.Profile
.CapsuleDict
:
194 CapsuleObj
= GenFdsGlobalVariable
.FdfParser
.Profile
.CapsuleDict
[RegionData
.upper()]
196 if CapsuleObj
is not None :
197 CapsuleObj
.CapsuleName
= RegionData
.upper()
198 GenFdsGlobalVariable
.InfLogger(' Region Name = CAPSULE')
200 # Call GenFv tool to generate Capsule Image
202 FileName
= CapsuleObj
.GenCapsule()
203 CapsuleObj
.CapsuleName
= None
205 EdkLogger
.error("GenFds", GENFDS_ERROR
, "Capsule (%s) is NOT described in FDF file!" % (RegionData
))
208 # Add the capsule image into FD buffer
210 FileLength
= os
.stat(FileName
)[ST_SIZE
]
211 if FileLength
> Size
:
212 EdkLogger
.error("GenFds", GENFDS_ERROR
,
213 "Size 0x%X of Capsule File (%s) is larger than Region Size 0x%X specified." \
214 % (FileLength
, RegionData
, Size
))
215 BinFile
= open(FileName
, 'rb')
216 Buffer
.write(BinFile
.read())
218 Size
= Size
- FileLength
220 # Pad the left buffer
222 self
.PadBuffer(Buffer
, ErasePolarity
, Size
)
224 if self
.RegionType
in ('FILE', 'INF'):
225 for RegionData
in self
.RegionDataList
:
226 if self
.RegionType
== 'INF':
227 RegionData
.__InfParse
__(None)
228 if len(RegionData
.BinFileList
) != 1:
229 EdkLogger
.error('GenFds', GENFDS_ERROR
, 'INF in FD region can only contain one binary: %s' % RegionData
)
230 File
= RegionData
.BinFileList
[0]
231 RegionData
= RegionData
.PatchEfiFile(File
.Path
, File
.Type
)
233 RegionData
= GenFdsGlobalVariable
.MacroExtend(RegionData
, MacroDict
)
234 if RegionData
[1] != ':' :
235 RegionData
= mws
.join (GenFdsGlobalVariable
.WorkSpaceDir
, RegionData
)
236 if not os
.path
.exists(RegionData
):
237 EdkLogger
.error("GenFds", FILE_NOT_FOUND
, ExtraData
=RegionData
)
239 # Add the file image into FD buffer
241 FileLength
= os
.stat(RegionData
)[ST_SIZE
]
242 if FileLength
> Size
:
243 EdkLogger
.error("GenFds", GENFDS_ERROR
,
244 "Size of File (%s) is larger than Region Size 0x%X specified." \
245 % (RegionData
, Size
))
246 GenFdsGlobalVariable
.InfLogger(' Region File Name = %s' % RegionData
)
247 BinFile
= open(RegionData
, 'rb')
248 Buffer
.write(BinFile
.read())
250 Size
= Size
- FileLength
252 # Pad the left buffer
254 self
.PadBuffer(Buffer
, ErasePolarity
, Size
)
256 if self
.RegionType
== 'DATA' :
257 GenFdsGlobalVariable
.InfLogger(' Region Name = DATA')
259 for RegionData
in self
.RegionDataList
:
260 Data
= RegionData
.split(',')
261 DataSize
= DataSize
+ len(Data
)
263 EdkLogger
.error("GenFds", GENFDS_ERROR
, "Size of DATA is larger than Region Size ")
266 Buffer
.write(pack('B', int(item
, 16)))
267 Size
= Size
- DataSize
269 # Pad the left buffer
271 self
.PadBuffer(Buffer
, ErasePolarity
, Size
)
273 if self
.RegionType
is None:
274 GenFdsGlobalVariable
.InfLogger(' Region Name = None')
275 self
.PadBuffer(Buffer
, ErasePolarity
, Size
)
277 ## BlockSizeOfRegion()
279 # @param BlockSizeList List of block information
280 # @param FvObj The object for FV
282 def BlockInfoOfRegion(self
, BlockSizeList
, FvObj
):
285 RemindingSize
= self
.Size
287 for (BlockSize
, BlockNum
, pcd
) in BlockSizeList
:
288 End
= Start
+ BlockSize
* BlockNum
289 # region not started yet
290 if self
.Offset
>= End
:
293 # region located in current blocks
295 # region ended within current blocks
296 if self
.Offset
+ self
.Size
<= End
:
297 ExpectedList
.append((BlockSize
, (RemindingSize
+ BlockSize
- 1) // BlockSize
))
299 # region not ended yet
301 # region not started in middle of current blocks
302 if self
.Offset
<= Start
:
303 UsedBlockNum
= BlockNum
304 # region started in middle of current blocks
306 UsedBlockNum
= (End
- self
.Offset
) // BlockSize
308 ExpectedList
.append((BlockSize
, UsedBlockNum
))
309 RemindingSize
-= BlockSize
* UsedBlockNum
311 if FvObj
.BlockSizeList
== []:
312 FvObj
.BlockSizeList
= ExpectedList
314 # first check whether FvObj.BlockSizeList items have only "BlockSize" or "NumBlocks",
315 # if so, use ExpectedList
316 for Item
in FvObj
.BlockSizeList
:
317 if Item
[0] is None or Item
[1] is None:
318 FvObj
.BlockSizeList
= ExpectedList
320 # make sure region size is no smaller than the summed block size in FV
322 for Item
in FvObj
.BlockSizeList
:
323 Sum
+= Item
[0] * Item
[1]
325 EdkLogger
.error("GenFds", GENFDS_ERROR
, "Total Size of FV %s 0x%x is larger than Region Size 0x%x "
326 % (FvObj
.UiFvName
, Sum
, self
.Size
))
327 # check whether the BlockStatements in FV section is appropriate
328 ExpectedListData
= ''
329 for Item
in ExpectedList
:
330 ExpectedListData
+= "BlockSize = 0x%x\n\tNumBlocks = 0x%x\n\t" % Item
332 for Item
in FvObj
.BlockSizeList
:
333 if Item
[0] != ExpectedList
[Index
][0]:
334 EdkLogger
.error("GenFds", GENFDS_ERROR
, "BlockStatements of FV %s are not align with FD's, suggested FV BlockStatement"
335 % FvObj
.UiFvName
, ExtraData
=ExpectedListData
)
336 elif Item
[1] != ExpectedList
[Index
][1]:
337 if (Item
[1] < ExpectedList
[Index
][1]) and (Index
== len(FvObj
.BlockSizeList
) - 1):
340 EdkLogger
.error("GenFds", GENFDS_ERROR
, "BlockStatements of FV %s are not align with FD's, suggested FV BlockStatement"
341 % FvObj
.UiFvName
, ExtraData
=ExpectedListData
)