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