]>
Commit | Line | Data |
---|---|---|
30fdf114 LG |
1 | ## @file\r |
2 | # process GUIDed section generation\r | |
3 | #\r | |
a146c532 | 4 | # Copyright (c) 2007 - 2018, 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 | import Section\r | |
19 | import subprocess\r | |
20 | from Ffs import Ffs\r | |
1be2ed90 | 21 | import Common.LongFilePathOs as os\r |
30fdf114 LG |
22 | from GenFdsGlobalVariable import GenFdsGlobalVariable\r |
23 | from CommonDataClass.FdfClass import GuidSectionClassObject\r | |
24 | from Common import ToolDefClassObject\r | |
25 | import sys\r | |
26 | from Common import EdkLogger\r | |
27 | from Common.BuildToolError import *\r | |
52302d4d | 28 | from FvImageSection import FvImageSection\r |
1be2ed90 | 29 | from Common.LongFilePathSupport import OpenLongFilePath as open\r |
91ae2988 | 30 | from GenFds import FindExtendTool\r |
8bb63e37 | 31 | from Common.DataType import *\r |
30fdf114 LG |
32 | \r |
33 | ## generate GUIDed section\r | |
34 | #\r | |
35 | #\r | |
36 | class GuidSection(GuidSectionClassObject) :\r | |
37 | \r | |
38 | ## The constructor\r | |
39 | #\r | |
40 | # @param self The object pointer\r | |
41 | #\r | |
42 | def __init__(self):\r | |
43 | GuidSectionClassObject.__init__(self)\r | |
44 | \r | |
45 | ## GenSection() method\r | |
46 | #\r | |
47 | # Generate GUIDed section\r | |
48 | #\r | |
49 | # @param self The object pointer\r | |
50 | # @param OutputPath Where to place output file\r | |
51 | # @param ModuleName Which module this section belongs to\r | |
52 | # @param SecNum Index of section\r | |
53 | # @param KeyStringList Filter for inputs of section generation\r | |
54 | # @param FfsInf FfsInfStatement object that contains this section data\r | |
55 | # @param Dict dictionary contains macro and its value\r | |
56 | # @retval tuple (Generated file name, section alignment)\r | |
57 | #\r | |
37de70b7 | 58 | def GenSection(self, OutputPath, ModuleName, SecNum, KeyStringList, FfsInf=None, Dict={}, IsMakefile=False):\r |
30fdf114 LG |
59 | #\r |
60 | # Generate all section\r | |
61 | #\r | |
62 | self.KeyStringList = KeyStringList\r | |
63 | self.CurrentArchList = GenFdsGlobalVariable.ArchList\r | |
4231a819 | 64 | if FfsInf is not None:\r |
30fdf114 LG |
65 | self.Alignment = FfsInf.__ExtendMacro__(self.Alignment)\r |
66 | self.NameGuid = FfsInf.__ExtendMacro__(self.NameGuid)\r | |
67 | self.SectionType = FfsInf.__ExtendMacro__(self.SectionType)\r | |
68 | self.CurrentArchList = [FfsInf.CurrentArch]\r | |
69 | \r | |
47fea6af | 70 | SectFile = tuple()\r |
52302d4d | 71 | SectAlign = []\r |
30fdf114 | 72 | Index = 0\r |
52302d4d LG |
73 | MaxAlign = None\r |
74 | if self.FvAddr != []:\r | |
75 | FvAddrIsSet = True\r | |
76 | else:\r | |
77 | FvAddrIsSet = False\r | |
78 | \r | |
79 | if self.ProcessRequired in ("TRUE", "1"):\r | |
80 | if self.FvAddr != []:\r | |
81 | #no use FvAddr when the image is processed.\r | |
82 | self.FvAddr = []\r | |
4231a819 | 83 | if self.FvParentAddr is not None:\r |
52302d4d LG |
84 | #no use Parent Addr when the image is processed.\r |
85 | self.FvParentAddr = None\r | |
86 | \r | |
30fdf114 LG |
87 | for Sect in self.SectionList:\r |
88 | Index = Index + 1\r | |
47fea6af | 89 | SecIndex = '%s.%d' % (SecNum, Index)\r |
52302d4d LG |
90 | # set base address for inside FvImage\r |
91 | if isinstance(Sect, FvImageSection):\r | |
92 | if self.FvAddr != []:\r | |
93 | Sect.FvAddr = self.FvAddr.pop(0)\r | |
94 | self.IncludeFvSection = True\r | |
95 | elif isinstance(Sect, GuidSection):\r | |
96 | Sect.FvAddr = self.FvAddr\r | |
97 | Sect.FvParentAddr = self.FvParentAddr\r | |
37de70b7 | 98 | ReturnSectList, align = Sect.GenSection(OutputPath, ModuleName, SecIndex, KeyStringList, FfsInf, Dict, IsMakefile=IsMakefile)\r |
52302d4d LG |
99 | if isinstance(Sect, GuidSection):\r |
100 | if Sect.IncludeFvSection:\r | |
101 | self.IncludeFvSection = Sect.IncludeFvSection\r | |
102 | \r | |
4231a819 CJ |
103 | if align is not None:\r |
104 | if MaxAlign is None:\r | |
52302d4d LG |
105 | MaxAlign = align\r |
106 | if GenFdsGlobalVariable.GetAlignment (align) > GenFdsGlobalVariable.GetAlignment (MaxAlign):\r | |
107 | MaxAlign = align\r | |
30fdf114 | 108 | if ReturnSectList != []:\r |
4231a819 | 109 | if align is None:\r |
52302d4d | 110 | align = "1"\r |
30fdf114 LG |
111 | for file in ReturnSectList:\r |
112 | SectFile += (file,)\r | |
52302d4d | 113 | SectAlign.append(align)\r |
30fdf114 | 114 | \r |
4231a819 CJ |
115 | if MaxAlign is not None:\r |
116 | if self.Alignment is None:\r | |
52302d4d LG |
117 | self.Alignment = MaxAlign\r |
118 | else:\r | |
119 | if GenFdsGlobalVariable.GetAlignment (MaxAlign) > GenFdsGlobalVariable.GetAlignment (self.Alignment):\r | |
120 | self.Alignment = MaxAlign\r | |
30fdf114 LG |
121 | \r |
122 | OutputFile = OutputPath + \\r | |
47fea6af | 123 | os.sep + \\r |
30fdf114 | 124 | ModuleName + \\r |
8bb63e37 | 125 | SUP_MODULE_SEC + \\r |
47fea6af | 126 | SecNum + \\r |
30fdf114 LG |
127 | Ffs.SectionSuffix['GUIDED']\r |
128 | OutputFile = os.path.normpath(OutputFile)\r | |
129 | \r | |
130 | ExternalTool = None\r | |
52302d4d | 131 | ExternalOption = None\r |
4231a819 | 132 | if self.NameGuid is not None:\r |
91ae2988 | 133 | ExternalTool, ExternalOption = FindExtendTool(self.KeyStringList, self.CurrentArchList, self.NameGuid)\r |
52302d4d | 134 | \r |
30fdf114 LG |
135 | #\r |
136 | # If not have GUID , call default\r | |
137 | # GENCRC32 section\r | |
138 | #\r | |
4231a819 | 139 | if self.NameGuid is None :\r |
47fea6af | 140 | GenFdsGlobalVariable.VerboseLogger("Use GenSection function Generate CRC32 Section")\r |
37de70b7 | 141 | GenFdsGlobalVariable.GenerateSection(OutputFile, SectFile, Section.Section.SectionType[self.SectionType], InputAlign=SectAlign, IsMakefile=IsMakefile)\r |
30fdf114 LG |
142 | OutputFileList = []\r |
143 | OutputFileList.append(OutputFile)\r | |
144 | return OutputFileList, self.Alignment\r | |
145 | #or GUID not in External Tool List\r | |
4231a819 | 146 | elif ExternalTool is None:\r |
30fdf114 LG |
147 | EdkLogger.error("GenFds", GENFDS_ERROR, "No tool found with GUID %s" % self.NameGuid)\r |
148 | else:\r | |
47fea6af | 149 | DummyFile = OutputFile + ".dummy"\r |
30fdf114 LG |
150 | #\r |
151 | # Call GenSection with DUMMY section type.\r | |
152 | #\r | |
37de70b7 | 153 | GenFdsGlobalVariable.GenerateSection(DummyFile, SectFile, InputAlign=SectAlign, IsMakefile=IsMakefile)\r |
30fdf114 LG |
154 | #\r |
155 | # Use external tool process the Output\r | |
156 | #\r | |
30fdf114 | 157 | TempFile = OutputPath + \\r |
47fea6af | 158 | os.sep + \\r |
30fdf114 | 159 | ModuleName + \\r |
8bb63e37 | 160 | SUP_MODULE_SEC + \\r |
47fea6af | 161 | SecNum + \\r |
30fdf114 LG |
162 | '.tmp'\r |
163 | TempFile = os.path.normpath(TempFile)\r | |
edafa0bb YL |
164 | #\r |
165 | # Remove temp file if its time stamp is older than dummy file\r | |
166 | # Just in case the external tool fails at this time but succeeded before\r | |
167 | # Error should be reported if the external tool does not generate a new output based on new input\r | |
168 | #\r | |
169 | if os.path.exists(TempFile) and os.path.exists(DummyFile) and os.path.getmtime(TempFile) < os.path.getmtime(DummyFile):\r | |
170 | os.remove(TempFile)\r | |
30fdf114 | 171 | \r |
52302d4d LG |
172 | FirstCall = False\r |
173 | CmdOption = '-e'\r | |
4231a819 | 174 | if ExternalOption is not None:\r |
52302d4d | 175 | CmdOption = CmdOption + ' ' + ExternalOption\r |
37de70b7 | 176 | if not GenFdsGlobalVariable.EnableGenfdsMultiThread:\r |
4231a819 | 177 | if self.ProcessRequired not in ("TRUE", "1") and self.IncludeFvSection and not FvAddrIsSet and self.FvParentAddr is not None:\r |
37de70b7 YZ |
178 | #FirstCall is only set for the encapsulated flash FV image without process required attribute.\r |
179 | FirstCall = True\r | |
180 | #\r | |
181 | # Call external tool\r | |
182 | #\r | |
183 | ReturnValue = [1]\r | |
184 | if FirstCall:\r | |
185 | #first try to call the guided tool with -z option and CmdOption for the no process required guided tool.\r | |
186 | GenFdsGlobalVariable.GuidTool(TempFile, [DummyFile], ExternalTool, '-z' + ' ' + CmdOption, ReturnValue)\r | |
30fdf114 | 187 | \r |
37de70b7 YZ |
188 | #\r |
189 | # when no call or first call failed, ReturnValue are not 1.\r | |
190 | # Call the guided tool with CmdOption\r | |
191 | #\r | |
192 | if ReturnValue[0] != 0:\r | |
193 | FirstCall = False\r | |
194 | ReturnValue[0] = 0\r | |
195 | GenFdsGlobalVariable.GuidTool(TempFile, [DummyFile], ExternalTool, CmdOption)\r | |
196 | #\r | |
197 | # There is external tool which does not follow standard rule which return nonzero if tool fails\r | |
198 | # The output file has to be checked\r | |
199 | #\r | |
200 | \r | |
201 | if not os.path.exists(TempFile) :\r | |
202 | EdkLogger.error("GenFds", COMMAND_FAILURE, 'Fail to call %s, no output file was generated' % ExternalTool)\r | |
203 | \r | |
204 | FileHandleIn = open(DummyFile, 'rb')\r | |
205 | FileHandleIn.seek(0, 2)\r | |
206 | InputFileSize = FileHandleIn.tell()\r | |
207 | \r | |
208 | FileHandleOut = open(TempFile, 'rb')\r | |
209 | FileHandleOut.seek(0, 2)\r | |
210 | TempFileSize = FileHandleOut.tell()\r | |
211 | \r | |
212 | Attribute = []\r | |
213 | HeaderLength = None\r | |
214 | if self.ExtraHeaderSize != -1:\r | |
215 | HeaderLength = str(self.ExtraHeaderSize)\r | |
216 | \r | |
4231a819 | 217 | if self.ProcessRequired == "NONE" and HeaderLength is None:\r |
37de70b7 YZ |
218 | if TempFileSize > InputFileSize:\r |
219 | FileHandleIn.seek(0)\r | |
220 | BufferIn = FileHandleIn.read()\r | |
221 | FileHandleOut.seek(0)\r | |
222 | BufferOut = FileHandleOut.read()\r | |
223 | if BufferIn == BufferOut[TempFileSize - InputFileSize:]:\r | |
224 | HeaderLength = str(TempFileSize - InputFileSize)\r | |
225 | #auto sec guided attribute with process required\r | |
4231a819 | 226 | if HeaderLength is None:\r |
37de70b7 YZ |
227 | Attribute.append('PROCESSING_REQUIRED')\r |
228 | \r | |
229 | FileHandleIn.close()\r | |
230 | FileHandleOut.close()\r | |
231 | \r | |
232 | if FirstCall and 'PROCESSING_REQUIRED' in Attribute:\r | |
233 | # Guided data by -z option on first call is the process required data. Call the guided tool with the real option.\r | |
234 | GenFdsGlobalVariable.GuidTool(TempFile, [DummyFile], ExternalTool, CmdOption)\r | |
235 | \r | |
236 | #\r | |
237 | # Call Gensection Add Section Header\r | |
238 | #\r | |
239 | if self.ProcessRequired in ("TRUE", "1"):\r | |
240 | if 'PROCESSING_REQUIRED' not in Attribute:\r | |
241 | Attribute.append('PROCESSING_REQUIRED')\r | |
242 | \r | |
243 | if self.AuthStatusValid in ("TRUE", "1"):\r | |
244 | Attribute.append('AUTH_STATUS_VALID')\r | |
245 | GenFdsGlobalVariable.GenerateSection(OutputFile, [TempFile], Section.Section.SectionType['GUIDED'],\r | |
246 | Guid=self.NameGuid, GuidAttr=Attribute, GuidHdrLen=HeaderLength)\r | |
247 | \r | |
248 | else:\r | |
249 | #add input file for GenSec get PROCESSING_REQUIRED\r | |
250 | GenFdsGlobalVariable.GuidTool(TempFile, [DummyFile], ExternalTool, CmdOption, IsMakefile=IsMakefile)\r | |
251 | Attribute = []\r | |
252 | HeaderLength = None\r | |
253 | if self.ExtraHeaderSize != -1:\r | |
254 | HeaderLength = str(self.ExtraHeaderSize)\r | |
255 | if self.AuthStatusValid in ("TRUE", "1"):\r | |
256 | Attribute.append('AUTH_STATUS_VALID')\r | |
4231a819 | 257 | if self.ProcessRequired == "NONE" and HeaderLength is None:\r |
37de70b7 YZ |
258 | GenFdsGlobalVariable.GenerateSection(OutputFile, [TempFile], Section.Section.SectionType['GUIDED'],\r |
259 | Guid=self.NameGuid, GuidAttr=Attribute,\r | |
260 | GuidHdrLen=HeaderLength, DummyFile=DummyFile, IsMakefile=IsMakefile)\r | |
261 | else:\r | |
262 | if self.ProcessRequired in ("TRUE", "1"):\r | |
263 | if 'PROCESSING_REQUIRED' not in Attribute:\r | |
264 | Attribute.append('PROCESSING_REQUIRED')\r | |
265 | GenFdsGlobalVariable.GenerateSection(OutputFile, [TempFile], Section.Section.SectionType['GUIDED'],\r | |
266 | Guid=self.NameGuid, GuidAttr=Attribute,\r | |
267 | GuidHdrLen=HeaderLength, IsMakefile=IsMakefile)\r | |
47fea6af | 268 | \r |
30fdf114 LG |
269 | OutputFileList = []\r |
270 | OutputFileList.append(OutputFile)\r | |
52302d4d LG |
271 | if 'PROCESSING_REQUIRED' in Attribute:\r |
272 | # reset guided section alignment to none for the processed required guided data\r | |
273 | self.Alignment = None\r | |
274 | self.IncludeFvSection = False\r | |
275 | self.ProcessRequired = "TRUE"\r | |
a146c532 FY |
276 | if IsMakefile and self.Alignment.strip() == '0':\r |
277 | self.Alignment = '1'\r | |
30fdf114 LG |
278 | return OutputFileList, self.Alignment\r |
279 | \r | |
30fdf114 LG |
280 | \r |
281 | \r |