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