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