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