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