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