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