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