]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/GenFds/EfiSection.py
BaseTools: Remove equality operator with None
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / EfiSection.py
1 ## @file
2 # process rule section generation
3 #
4 # Copyright (c) 2007 - 2017, 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 from struct import *
19 import Section
20 from GenFdsGlobalVariable import GenFdsGlobalVariable
21 import subprocess
22 from Ffs import Ffs
23 import Common.LongFilePathOs as os
24 from CommonDataClass.FdfClass import EfiSectionClassObject
25 from Common import EdkLogger
26 from Common.BuildToolError import *
27 from Common.Misc import PeImageClass
28 from Common.LongFilePathSupport import OpenLongFilePath as open
29 from Common.LongFilePathSupport import CopyLongFilePath
30
31 ## generate rule section
32 #
33 #
34 class EfiSection (EfiSectionClassObject):
35
36 ## The constructor
37 #
38 # @param self The object pointer
39 #
40 def __init__(self):
41 EfiSectionClassObject.__init__(self)
42
43 ## GenSection() method
44 #
45 # Generate rule section
46 #
47 # @param self The object pointer
48 # @param OutputPath Where to place output file
49 # @param ModuleName Which module this section belongs to
50 # @param SecNum Index of section
51 # @param KeyStringList Filter for inputs of section generation
52 # @param FfsInf FfsInfStatement object that contains this section data
53 # @param Dict dictionary contains macro and its value
54 # @retval tuple (Generated file name list, section alignment)
55 #
56 def GenSection(self, OutputPath, ModuleName, SecNum, KeyStringList, FfsInf = None, Dict = {}, IsMakefile = False) :
57
58 if self.FileName is not None and self.FileName.startswith('PCD('):
59 self.FileName = GenFdsGlobalVariable.GetPcdValue(self.FileName)
60 """Prepare the parameter of GenSection"""
61 if FfsInf is not None :
62 InfFileName = FfsInf.InfFileName
63 SectionType = FfsInf.__ExtendMacro__(self.SectionType)
64 Filename = FfsInf.__ExtendMacro__(self.FileName)
65 BuildNum = FfsInf.__ExtendMacro__(self.BuildNum)
66 StringData = FfsInf.__ExtendMacro__(self.StringData)
67 NoStrip = True
68 if FfsInf.ModuleType in ('SEC', 'PEI_CORE', 'PEIM') and SectionType in ('TE', 'PE32'):
69 if FfsInf.KeepReloc is not None:
70 NoStrip = FfsInf.KeepReloc
71 elif FfsInf.KeepRelocFromRule is not None:
72 NoStrip = FfsInf.KeepRelocFromRule
73 elif self.KeepReloc is not None:
74 NoStrip = self.KeepReloc
75 elif FfsInf.ShadowFromInfFile is not None:
76 NoStrip = FfsInf.ShadowFromInfFile
77 else:
78 EdkLogger.error("GenFds", GENFDS_ERROR, "Module %s apply rule for None!" %ModuleName)
79
80 """If the file name was pointed out, add it in FileList"""
81 FileList = []
82 if Filename is not None:
83 Filename = GenFdsGlobalVariable.MacroExtend(Filename, Dict)
84 # check if the path is absolute or relative
85 if os.path.isabs(Filename):
86 Filename = os.path.normpath(Filename)
87 else:
88 Filename = os.path.normpath(os.path.join(FfsInf.EfiOutputPath, Filename))
89
90 if not self.Optional:
91 FileList.append(Filename)
92 elif os.path.exists(Filename):
93 FileList.append(Filename)
94 elif '.depex' in FfsInf.FinalTargetSuffixMap or FfsInf.Depex:
95 if IsMakefile:
96 FileList.append(Filename)
97 else:
98 FileList, IsSect = Section.Section.GetFileList(FfsInf, self.FileType, self.FileExtension, Dict, IsMakefile=IsMakefile)
99 if IsSect :
100 return FileList, self.Alignment
101
102 Index = 0
103 Align = self.Alignment
104
105 """ If Section type is 'VERSION'"""
106 OutputFileList = []
107 if SectionType == 'VERSION':
108
109 InfOverrideVerString = False
110 if FfsInf.Version is not None:
111 #StringData = FfsInf.Version
112 BuildNum = FfsInf.Version
113 InfOverrideVerString = True
114
115 if InfOverrideVerString:
116 #VerTuple = ('-n', '"' + StringData + '"')
117 if BuildNum is not None and BuildNum != '':
118 BuildNumTuple = ('-j', BuildNum)
119 else:
120 BuildNumTuple = tuple()
121
122 Num = SecNum
123 OutputFile = os.path.join( OutputPath, ModuleName + 'SEC' + str(Num) + Ffs.SectionSuffix.get(SectionType))
124 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_VERSION',
125 #Ui=StringData,
126 Ver=BuildNum,
127 IsMakefile=IsMakefile)
128 OutputFileList.append(OutputFile)
129
130 elif FileList != []:
131 for File in FileList:
132 Index = Index + 1
133 Num = '%s.%d' %(SecNum , Index)
134 OutputFile = os.path.join(OutputPath, ModuleName + 'SEC' + Num + Ffs.SectionSuffix.get(SectionType))
135 f = open(File, 'r')
136 VerString = f.read()
137 f.close()
138 BuildNum = VerString
139 if BuildNum is not None and BuildNum != '':
140 BuildNumTuple = ('-j', BuildNum)
141 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_VERSION',
142 #Ui=VerString,
143 Ver=BuildNum,
144 IsMakefile=IsMakefile)
145 OutputFileList.append(OutputFile)
146
147 else:
148 BuildNum = StringData
149 if BuildNum is not None and BuildNum != '':
150 BuildNumTuple = ('-j', BuildNum)
151 else:
152 BuildNumTuple = tuple()
153 BuildNumString = ' ' + ' '.join(BuildNumTuple)
154
155 #if VerString == '' and
156 if BuildNumString == '':
157 if self.Optional == True :
158 GenFdsGlobalVariable.VerboseLogger( "Optional Section don't exist!")
159 return [], None
160 else:
161 EdkLogger.error("GenFds", GENFDS_ERROR, "File: %s miss Version Section value" %InfFileName)
162 Num = SecNum
163 OutputFile = os.path.join( OutputPath, ModuleName + 'SEC' + str(Num) + Ffs.SectionSuffix.get(SectionType))
164 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_VERSION',
165 #Ui=VerString,
166 Ver=BuildNum,
167 IsMakefile=IsMakefile)
168 OutputFileList.append(OutputFile)
169
170 #
171 # If Section Type is 'UI'
172 #
173 elif SectionType == 'UI':
174
175 InfOverrideUiString = False
176 if FfsInf.Ui is not None:
177 StringData = FfsInf.Ui
178 InfOverrideUiString = True
179
180 if InfOverrideUiString:
181 Num = SecNum
182 OutputFile = os.path.join( OutputPath, ModuleName + 'SEC' + str(Num) + Ffs.SectionSuffix.get(SectionType))
183 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_USER_INTERFACE',
184 Ui=StringData, IsMakefile=IsMakefile)
185 OutputFileList.append(OutputFile)
186
187 elif FileList != []:
188 for File in FileList:
189 Index = Index + 1
190 Num = '%s.%d' %(SecNum , Index)
191 OutputFile = os.path.join(OutputPath, ModuleName + 'SEC' + Num + Ffs.SectionSuffix.get(SectionType))
192 f = open(File, 'r')
193 UiString = f.read()
194 f.close()
195 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_USER_INTERFACE',
196 Ui=UiString, IsMakefile=IsMakefile)
197 OutputFileList.append(OutputFile)
198 else:
199 if StringData is not None and len(StringData) > 0:
200 UiTuple = ('-n', '"' + StringData + '"')
201 else:
202 UiTuple = tuple()
203
204 if self.Optional == True :
205 GenFdsGlobalVariable.VerboseLogger( "Optional Section don't exist!")
206 return '', None
207 else:
208 EdkLogger.error("GenFds", GENFDS_ERROR, "File: %s miss UI Section value" %InfFileName)
209
210 Num = SecNum
211 OutputFile = os.path.join( OutputPath, ModuleName + 'SEC' + str(Num) + Ffs.SectionSuffix.get(SectionType))
212 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_USER_INTERFACE',
213 Ui=StringData, IsMakefile=IsMakefile)
214 OutputFileList.append(OutputFile)
215
216
217 else:
218 """If File List is empty"""
219 if FileList == [] :
220 if self.Optional == True:
221 GenFdsGlobalVariable.VerboseLogger("Optional Section don't exist!")
222 return [], None
223 else:
224 EdkLogger.error("GenFds", GENFDS_ERROR, "Output file for %s section could not be found for %s" % (SectionType, InfFileName))
225
226 else:
227 """Convert the File to Section file one by one """
228 for File in FileList:
229 """ Copy Map file to FFS output path """
230 Index = Index + 1
231 Num = '%s.%d' %(SecNum , Index)
232 OutputFile = os.path.join( OutputPath, ModuleName + 'SEC' + Num + Ffs.SectionSuffix.get(SectionType))
233 File = GenFdsGlobalVariable.MacroExtend(File, Dict)
234
235 #Get PE Section alignment when align is set to AUTO
236 if self.Alignment == 'Auto' and (SectionType == 'PE32' or SectionType == 'TE'):
237 ImageObj = PeImageClass (File)
238 if ImageObj.SectionAlignment < 0x400:
239 Align = str (ImageObj.SectionAlignment)
240 elif ImageObj.SectionAlignment < 0x100000:
241 Align = str (ImageObj.SectionAlignment / 0x400) + 'K'
242 else:
243 Align = str (ImageObj.SectionAlignment / 0x100000) + 'M'
244
245 if File[(len(File)-4):] == '.efi':
246 MapFile = File.replace('.efi', '.map')
247 CopyMapFile = os.path.join(OutputPath, ModuleName + '.map')
248 if IsMakefile:
249 if GenFdsGlobalVariable.CopyList == []:
250 GenFdsGlobalVariable.CopyList = [(MapFile, CopyMapFile)]
251 else:
252 GenFdsGlobalVariable.CopyList.append((MapFile, CopyMapFile))
253 else:
254 if os.path.exists(MapFile):
255 if not os.path.exists(CopyMapFile) or \
256 (os.path.getmtime(MapFile) > os.path.getmtime(CopyMapFile)):
257 CopyLongFilePath(MapFile, CopyMapFile)
258
259 if not NoStrip:
260 FileBeforeStrip = os.path.join(OutputPath, ModuleName + '.efi')
261 if IsMakefile:
262 if GenFdsGlobalVariable.CopyList == []:
263 GenFdsGlobalVariable.CopyList = [(File, FileBeforeStrip)]
264 else:
265 GenFdsGlobalVariable.CopyList.append((File, FileBeforeStrip))
266 else:
267 if not os.path.exists(FileBeforeStrip) or \
268 (os.path.getmtime(File) > os.path.getmtime(FileBeforeStrip)):
269 CopyLongFilePath(File, FileBeforeStrip)
270 StrippedFile = os.path.join(OutputPath, ModuleName + '.stripped')
271 GenFdsGlobalVariable.GenerateFirmwareImage(
272 StrippedFile,
273 [File],
274 Strip=True,
275 IsMakefile = IsMakefile
276 )
277 File = StrippedFile
278
279 """For TE Section call GenFw to generate TE image"""
280
281 if SectionType == 'TE':
282 TeFile = os.path.join( OutputPath, ModuleName + 'Te.raw')
283 GenFdsGlobalVariable.GenerateFirmwareImage(
284 TeFile,
285 [File],
286 Type='te',
287 IsMakefile = IsMakefile
288 )
289 File = TeFile
290
291 """Call GenSection"""
292 GenFdsGlobalVariable.GenerateSection(OutputFile,
293 [File],
294 Section.Section.SectionType.get (SectionType),
295 IsMakefile=IsMakefile
296 )
297 OutputFileList.append(OutputFile)
298
299 return OutputFileList, Align