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