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