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