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