]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/GenFds/EfiSection.py
BaseTools: fix --genfds-multi-thread generate makefile issue
[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 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 ModuleNameStr = FfsInf.__ExtendMacro__('$(MODULE_NAME)')
68 NoStrip = True
69 if FfsInf.ModuleType in ('SEC', 'PEI_CORE', 'PEIM') and SectionType in ('TE', 'PE32'):
70 if FfsInf.KeepReloc is not None:
71 NoStrip = FfsInf.KeepReloc
72 elif FfsInf.KeepRelocFromRule is not None:
73 NoStrip = FfsInf.KeepRelocFromRule
74 elif self.KeepReloc is not None:
75 NoStrip = self.KeepReloc
76 elif FfsInf.ShadowFromInfFile is not None:
77 NoStrip = FfsInf.ShadowFromInfFile
78 else:
79 EdkLogger.error("GenFds", GENFDS_ERROR, "Module %s apply rule for None!" %ModuleName)
80
81 """If the file name was pointed out, add it in FileList"""
82 FileList = []
83 if Filename is not None:
84 Filename = GenFdsGlobalVariable.MacroExtend(Filename, Dict)
85 # check if the path is absolute or relative
86 if os.path.isabs(Filename):
87 Filename = os.path.normpath(Filename)
88 else:
89 Filename = os.path.normpath(os.path.join(FfsInf.EfiOutputPath, Filename))
90
91 if not self.Optional:
92 FileList.append(Filename)
93 elif os.path.exists(Filename):
94 FileList.append(Filename)
95 elif IsMakefile:
96 SuffixMap = FfsInf.GetFinalTargetSuffixMap()
97 if '.depex' in SuffixMap:
98 FileList.append(Filename)
99 else:
100 FileList, IsSect = Section.Section.GetFileList(FfsInf, self.FileType, self.FileExtension, Dict, IsMakefile=IsMakefile)
101 if IsSect :
102 return FileList, self.Alignment
103
104 Index = 0
105 Align = self.Alignment
106
107 """ If Section type is 'VERSION'"""
108 OutputFileList = []
109 if SectionType == 'VERSION':
110
111 InfOverrideVerString = False
112 if FfsInf.Version is not None:
113 #StringData = FfsInf.Version
114 BuildNum = FfsInf.Version
115 InfOverrideVerString = True
116
117 if InfOverrideVerString:
118 #VerTuple = ('-n', '"' + StringData + '"')
119 if BuildNum is not None and BuildNum != '':
120 BuildNumTuple = ('-j', BuildNum)
121 else:
122 BuildNumTuple = tuple()
123
124 Num = SecNum
125 OutputFile = os.path.join( OutputPath, ModuleName + 'SEC' + str(Num) + Ffs.SectionSuffix.get(SectionType))
126 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_VERSION',
127 #Ui=StringData,
128 Ver=BuildNum,
129 IsMakefile=IsMakefile)
130 OutputFileList.append(OutputFile)
131
132 elif FileList != []:
133 for File in FileList:
134 Index = Index + 1
135 Num = '%s.%d' %(SecNum , Index)
136 OutputFile = os.path.join(OutputPath, ModuleName + 'SEC' + Num + Ffs.SectionSuffix.get(SectionType))
137 f = open(File, 'r')
138 VerString = f.read()
139 f.close()
140 BuildNum = VerString
141 if BuildNum is not None and BuildNum != '':
142 BuildNumTuple = ('-j', BuildNum)
143 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_VERSION',
144 #Ui=VerString,
145 Ver=BuildNum,
146 IsMakefile=IsMakefile)
147 OutputFileList.append(OutputFile)
148
149 else:
150 BuildNum = StringData
151 if BuildNum is not None and BuildNum != '':
152 BuildNumTuple = ('-j', BuildNum)
153 else:
154 BuildNumTuple = tuple()
155 BuildNumString = ' ' + ' '.join(BuildNumTuple)
156
157 #if VerString == '' and
158 if BuildNumString == '':
159 if self.Optional == True :
160 GenFdsGlobalVariable.VerboseLogger( "Optional Section don't exist!")
161 return [], None
162 else:
163 EdkLogger.error("GenFds", GENFDS_ERROR, "File: %s miss Version Section value" %InfFileName)
164 Num = SecNum
165 OutputFile = os.path.join( OutputPath, ModuleName + 'SEC' + str(Num) + Ffs.SectionSuffix.get(SectionType))
166 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_VERSION',
167 #Ui=VerString,
168 Ver=BuildNum,
169 IsMakefile=IsMakefile)
170 OutputFileList.append(OutputFile)
171
172 #
173 # If Section Type is 'UI'
174 #
175 elif SectionType == 'UI':
176
177 InfOverrideUiString = False
178 if FfsInf.Ui is not None:
179 StringData = FfsInf.Ui
180 InfOverrideUiString = True
181
182 if InfOverrideUiString:
183 Num = SecNum
184 if IsMakefile and StringData == ModuleNameStr:
185 StringData = "$(MODULE_NAME)"
186 OutputFile = os.path.join( OutputPath, ModuleName + 'SEC' + str(Num) + Ffs.SectionSuffix.get(SectionType))
187 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_USER_INTERFACE',
188 Ui=StringData, IsMakefile=IsMakefile)
189 OutputFileList.append(OutputFile)
190
191 elif FileList != []:
192 for File in FileList:
193 Index = Index + 1
194 Num = '%s.%d' %(SecNum , Index)
195 OutputFile = os.path.join(OutputPath, ModuleName + 'SEC' + Num + Ffs.SectionSuffix.get(SectionType))
196 f = open(File, 'r')
197 UiString = f.read()
198 f.close()
199 if IsMakefile and UiString == ModuleNameStr:
200 UiString = "$(MODULE_NAME)"
201 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_USER_INTERFACE',
202 Ui=UiString, IsMakefile=IsMakefile)
203 OutputFileList.append(OutputFile)
204 else:
205 if StringData is not None and len(StringData) > 0:
206 UiTuple = ('-n', '"' + StringData + '"')
207 else:
208 UiTuple = tuple()
209
210 if self.Optional == True :
211 GenFdsGlobalVariable.VerboseLogger( "Optional Section don't exist!")
212 return '', None
213 else:
214 EdkLogger.error("GenFds", GENFDS_ERROR, "File: %s miss UI Section value" %InfFileName)
215
216 Num = SecNum
217 if IsMakefile and StringData == ModuleNameStr:
218 StringData = "$(MODULE_NAME)"
219 OutputFile = os.path.join( OutputPath, ModuleName + 'SEC' + str(Num) + Ffs.SectionSuffix.get(SectionType))
220 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_USER_INTERFACE',
221 Ui=StringData, IsMakefile=IsMakefile)
222 OutputFileList.append(OutputFile)
223
224
225 else:
226 """If File List is empty"""
227 if FileList == [] :
228 if self.Optional == True:
229 GenFdsGlobalVariable.VerboseLogger("Optional Section don't exist!")
230 return [], None
231 else:
232 EdkLogger.error("GenFds", GENFDS_ERROR, "Output file for %s section could not be found for %s" % (SectionType, InfFileName))
233
234 else:
235 """Convert the File to Section file one by one """
236 for File in FileList:
237 """ Copy Map file to FFS output path """
238 Index = Index + 1
239 Num = '%s.%d' %(SecNum , Index)
240 OutputFile = os.path.join( OutputPath, ModuleName + 'SEC' + Num + Ffs.SectionSuffix.get(SectionType))
241 File = GenFdsGlobalVariable.MacroExtend(File, Dict)
242
243 #Get PE Section alignment when align is set to AUTO
244 if self.Alignment == 'Auto' and (SectionType == 'PE32' or SectionType == 'TE'):
245 ImageObj = PeImageClass (File)
246 if ImageObj.SectionAlignment < 0x400:
247 Align = str (ImageObj.SectionAlignment)
248 elif ImageObj.SectionAlignment < 0x100000:
249 Align = str (ImageObj.SectionAlignment / 0x400) + 'K'
250 else:
251 Align = str (ImageObj.SectionAlignment / 0x100000) + 'M'
252
253 if File[(len(File)-4):] == '.efi':
254 MapFile = File.replace('.efi', '.map')
255 CopyMapFile = os.path.join(OutputPath, ModuleName + '.map')
256 if IsMakefile:
257 if GenFdsGlobalVariable.CopyList == []:
258 GenFdsGlobalVariable.CopyList = [(MapFile, CopyMapFile)]
259 else:
260 GenFdsGlobalVariable.CopyList.append((MapFile, CopyMapFile))
261 else:
262 if os.path.exists(MapFile):
263 if not os.path.exists(CopyMapFile) or \
264 (os.path.getmtime(MapFile) > os.path.getmtime(CopyMapFile)):
265 CopyLongFilePath(MapFile, CopyMapFile)
266
267 if not NoStrip:
268 FileBeforeStrip = os.path.join(OutputPath, ModuleName + '.efi')
269 if IsMakefile:
270 if GenFdsGlobalVariable.CopyList == []:
271 GenFdsGlobalVariable.CopyList = [(File, FileBeforeStrip)]
272 else:
273 GenFdsGlobalVariable.CopyList.append((File, FileBeforeStrip))
274 else:
275 if not os.path.exists(FileBeforeStrip) or \
276 (os.path.getmtime(File) > os.path.getmtime(FileBeforeStrip)):
277 CopyLongFilePath(File, FileBeforeStrip)
278 StrippedFile = os.path.join(OutputPath, ModuleName + '.stripped')
279 GenFdsGlobalVariable.GenerateFirmwareImage(
280 StrippedFile,
281 [File],
282 Strip=True,
283 IsMakefile = IsMakefile
284 )
285 File = StrippedFile
286
287 """For TE Section call GenFw to generate TE image"""
288
289 if SectionType == 'TE':
290 TeFile = os.path.join( OutputPath, ModuleName + 'Te.raw')
291 GenFdsGlobalVariable.GenerateFirmwareImage(
292 TeFile,
293 [File],
294 Type='te',
295 IsMakefile = IsMakefile
296 )
297 File = TeFile
298
299 """Call GenSection"""
300 GenFdsGlobalVariable.GenerateSection(OutputFile,
301 [File],
302 Section.Section.SectionType.get (SectionType),
303 IsMakefile=IsMakefile
304 )
305 OutputFileList.append(OutputFile)
306
307 return OutputFileList, Align