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