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