]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/GenFds/GuidSection.py
BaseTools: Move FindExtendTool to GenFdsGlobalVariable.py
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / GuidSection.py
CommitLineData
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
19from __future__ import absolute_import\r
20from . import Section\r
30fdf114 21import subprocess\r
bfa65b61 22from .Ffs import Ffs\r
1be2ed90 23import Common.LongFilePathOs as os\r
bfa65b61 24from .GenFdsGlobalVariable import GenFdsGlobalVariable\r
89a69d4b 25from .GenFdsGlobalVariable import FindExtendTool\r
30fdf114
LG
26from CommonDataClass.FdfClass import GuidSectionClassObject\r
27from Common import ToolDefClassObject\r
28import sys\r
29from Common import EdkLogger\r
30from Common.BuildToolError import *\r
bfa65b61 31from .FvImageSection import FvImageSection\r
1be2ed90 32from Common.LongFilePathSupport import OpenLongFilePath as open\r
8bb63e37 33from Common.DataType import *\r
30fdf114
LG
34\r
35## generate GUIDed section\r
36#\r
37#\r
38class GuidSection(GuidSectionClassObject) :\r
39\r
40 ## The constructor\r
41 #\r
42 # @param self The object pointer\r
43 #\r
44 def __init__(self):\r
45 GuidSectionClassObject.__init__(self)\r
46\r
47 ## GenSection() method\r
48 #\r
49 # Generate GUIDed section\r
50 #\r
51 # @param self The object pointer\r
52 # @param OutputPath Where to place output file\r
53 # @param ModuleName Which module this section belongs to\r
54 # @param SecNum Index of section\r
55 # @param KeyStringList Filter for inputs of section generation\r
56 # @param FfsInf FfsInfStatement object that contains this section data\r
57 # @param Dict dictionary contains macro and its value\r
58 # @retval tuple (Generated file name, section alignment)\r
59 #\r
37de70b7 60 def GenSection(self, OutputPath, ModuleName, SecNum, KeyStringList, FfsInf=None, Dict={}, IsMakefile=False):\r
30fdf114
LG
61 #\r
62 # Generate all section\r
63 #\r
64 self.KeyStringList = KeyStringList\r
65 self.CurrentArchList = GenFdsGlobalVariable.ArchList\r
4231a819 66 if FfsInf is not None:\r
30fdf114
LG
67 self.Alignment = FfsInf.__ExtendMacro__(self.Alignment)\r
68 self.NameGuid = FfsInf.__ExtendMacro__(self.NameGuid)\r
69 self.SectionType = FfsInf.__ExtendMacro__(self.SectionType)\r
70 self.CurrentArchList = [FfsInf.CurrentArch]\r
71\r
47fea6af 72 SectFile = tuple()\r
52302d4d 73 SectAlign = []\r
30fdf114 74 Index = 0\r
52302d4d
LG
75 MaxAlign = None\r
76 if self.FvAddr != []:\r
77 FvAddrIsSet = True\r
78 else:\r
79 FvAddrIsSet = False\r
f7496d71 80\r
52302d4d
LG
81 if self.ProcessRequired in ("TRUE", "1"):\r
82 if self.FvAddr != []:\r
83 #no use FvAddr when the image is processed.\r
84 self.FvAddr = []\r
4231a819 85 if self.FvParentAddr is not None:\r
52302d4d
LG
86 #no use Parent Addr when the image is processed.\r
87 self.FvParentAddr = None\r
88\r
30fdf114
LG
89 for Sect in self.SectionList:\r
90 Index = Index + 1\r
47fea6af 91 SecIndex = '%s.%d' % (SecNum, Index)\r
52302d4d
LG
92 # set base address for inside FvImage\r
93 if isinstance(Sect, FvImageSection):\r
94 if self.FvAddr != []:\r
95 Sect.FvAddr = self.FvAddr.pop(0)\r
96 self.IncludeFvSection = True\r
97 elif isinstance(Sect, GuidSection):\r
98 Sect.FvAddr = self.FvAddr\r
99 Sect.FvParentAddr = self.FvParentAddr\r
37de70b7 100 ReturnSectList, align = Sect.GenSection(OutputPath, ModuleName, SecIndex, KeyStringList, FfsInf, Dict, IsMakefile=IsMakefile)\r
52302d4d
LG
101 if isinstance(Sect, GuidSection):\r
102 if Sect.IncludeFvSection:\r
103 self.IncludeFvSection = Sect.IncludeFvSection\r
104\r
4231a819
CJ
105 if align is not None:\r
106 if MaxAlign is None:\r
52302d4d
LG
107 MaxAlign = align\r
108 if GenFdsGlobalVariable.GetAlignment (align) > GenFdsGlobalVariable.GetAlignment (MaxAlign):\r
109 MaxAlign = align\r
30fdf114 110 if ReturnSectList != []:\r
4231a819 111 if align is None:\r
52302d4d 112 align = "1"\r
30fdf114
LG
113 for file in ReturnSectList:\r
114 SectFile += (file,)\r
52302d4d 115 SectAlign.append(align)\r
30fdf114 116\r
4231a819
CJ
117 if MaxAlign is not None:\r
118 if self.Alignment is None:\r
52302d4d
LG
119 self.Alignment = MaxAlign\r
120 else:\r
121 if GenFdsGlobalVariable.GetAlignment (MaxAlign) > GenFdsGlobalVariable.GetAlignment (self.Alignment):\r
122 self.Alignment = MaxAlign\r
30fdf114
LG
123\r
124 OutputFile = OutputPath + \\r
47fea6af 125 os.sep + \\r
30fdf114 126 ModuleName + \\r
8bb63e37 127 SUP_MODULE_SEC + \\r
47fea6af 128 SecNum + \\r
30fdf114
LG
129 Ffs.SectionSuffix['GUIDED']\r
130 OutputFile = os.path.normpath(OutputFile)\r
131\r
132 ExternalTool = None\r
52302d4d 133 ExternalOption = None\r
4231a819 134 if self.NameGuid is not None:\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