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