2 # process rule section generation
4 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
6 # SPDX-License-Identifier: BSD-2-Clause-Patent
12 from __future__
import absolute_import
15 from .GenFdsGlobalVariable
import GenFdsGlobalVariable
17 from .Ffs
import SectionSuffix
18 import Common
.LongFilePathOs
as os
19 from CommonDataClass
.FdfClass
import EfiSectionClassObject
20 from Common
import EdkLogger
21 from Common
.BuildToolError
import *
22 from Common
.Misc
import PeImageClass
23 from Common
.LongFilePathSupport
import OpenLongFilePath
as open
24 from Common
.LongFilePathSupport
import CopyLongFilePath
25 from Common
.DataType
import *
27 ## generate rule section
30 class EfiSection (EfiSectionClassObject
):
34 # @param self The object pointer
37 EfiSectionClassObject
.__init
__(self
)
39 ## GenSection() method
41 # Generate rule section
43 # @param self The object pointer
44 # @param OutputPath Where to place output file
45 # @param ModuleName Which module this section belongs to
46 # @param SecNum Index of section
47 # @param KeyStringList Filter for inputs of section generation
48 # @param FfsInf FfsInfStatement object that contains this section data
49 # @param Dict dictionary contains macro and its value
50 # @retval tuple (Generated file name list, section alignment)
52 def GenSection(self
, OutputPath
, ModuleName
, SecNum
, KeyStringList
, FfsInf
= None, Dict
= {}, IsMakefile
= False) :
54 if self
.FileName
is not None and self
.FileName
.startswith('PCD('):
55 self
.FileName
= GenFdsGlobalVariable
.GetPcdValue(self
.FileName
)
56 """Prepare the parameter of GenSection"""
57 if FfsInf
is not None :
58 InfFileName
= FfsInf
.InfFileName
59 SectionType
= FfsInf
.__ExtendMacro
__(self
.SectionType
)
60 Filename
= FfsInf
.__ExtendMacro
__(self
.FileName
)
61 BuildNum
= FfsInf
.__ExtendMacro
__(self
.BuildNum
)
62 StringData
= FfsInf
.__ExtendMacro
__(self
.StringData
)
63 ModuleNameStr
= FfsInf
.__ExtendMacro
__('$(MODULE_NAME)')
65 if FfsInf
.ModuleType
in (SUP_MODULE_SEC
, SUP_MODULE_PEI_CORE
, SUP_MODULE_PEIM
, SUP_MODULE_MM_CORE_STANDALONE
) and SectionType
in (BINARY_FILE_TYPE_TE
, BINARY_FILE_TYPE_PE32
):
66 if FfsInf
.KeepReloc
is not None:
67 NoStrip
= FfsInf
.KeepReloc
68 elif FfsInf
.KeepRelocFromRule
is not None:
69 NoStrip
= FfsInf
.KeepRelocFromRule
70 elif self
.KeepReloc
is not None:
71 NoStrip
= self
.KeepReloc
72 elif FfsInf
.ShadowFromInfFile
is not None:
73 NoStrip
= FfsInf
.ShadowFromInfFile
75 EdkLogger
.error("GenFds", GENFDS_ERROR
, "Module %s apply rule for None!" %ModuleName
)
77 """If the file name was pointed out, add it in FileList"""
79 if Filename
is not None:
80 Filename
= GenFdsGlobalVariable
.MacroExtend(Filename
, Dict
)
81 # check if the path is absolute or relative
82 if os
.path
.isabs(Filename
):
83 Filename
= os
.path
.normpath(Filename
)
85 Filename
= os
.path
.normpath(os
.path
.join(FfsInf
.EfiOutputPath
, Filename
))
88 FileList
.append(Filename
)
89 elif os
.path
.exists(Filename
):
90 FileList
.append(Filename
)
92 SuffixMap
= FfsInf
.GetFinalTargetSuffixMap()
93 if '.depex' in SuffixMap
:
94 FileList
.append(Filename
)
96 FileList
, IsSect
= Section
.Section
.GetFileList(FfsInf
, self
.FileType
, self
.FileExtension
, Dict
, IsMakefile
=IsMakefile
, SectionType
=SectionType
)
98 return FileList
, self
.Alignment
101 Align
= self
.Alignment
103 """ If Section type is 'VERSION'"""
105 if SectionType
== 'VERSION':
107 InfOverrideVerString
= False
108 if FfsInf
.Version
is not None:
109 #StringData = FfsInf.Version
110 BuildNum
= FfsInf
.Version
111 InfOverrideVerString
= True
113 if InfOverrideVerString
:
114 #VerTuple = ('-n', '"' + StringData + '"')
115 if BuildNum
is not None and BuildNum
!= '':
116 BuildNumTuple
= ('-j', BuildNum
)
118 BuildNumTuple
= tuple()
121 OutputFile
= os
.path
.join( OutputPath
, ModuleName
+ SUP_MODULE_SEC
+ str(Num
) + SectionSuffix
.get(SectionType
))
122 GenFdsGlobalVariable
.GenerateSection(OutputFile
, [], 'EFI_SECTION_VERSION',
125 IsMakefile
=IsMakefile
)
126 OutputFileList
.append(OutputFile
)
129 for File
in FileList
:
131 Num
= '%s.%d' %(SecNum
, Index
)
132 OutputFile
= os
.path
.join(OutputPath
, ModuleName
+ SUP_MODULE_SEC
+ Num
+ SectionSuffix
.get(SectionType
))
137 if BuildNum
is not None and BuildNum
!= '':
138 BuildNumTuple
= ('-j', BuildNum
)
139 GenFdsGlobalVariable
.GenerateSection(OutputFile
, [], 'EFI_SECTION_VERSION',
142 IsMakefile
=IsMakefile
)
143 OutputFileList
.append(OutputFile
)
146 BuildNum
= StringData
147 if BuildNum
is not None and BuildNum
!= '':
148 BuildNumTuple
= ('-j', BuildNum
)
150 BuildNumTuple
= tuple()
151 BuildNumString
= ' ' + ' '.join(BuildNumTuple
)
153 #if VerString == '' and
154 if BuildNumString
== '':
155 if self
.Optional
== True :
156 GenFdsGlobalVariable
.VerboseLogger( "Optional Section don't exist!")
159 EdkLogger
.error("GenFds", GENFDS_ERROR
, "File: %s miss Version Section value" %InfFileName
)
161 OutputFile
= os
.path
.join( OutputPath
, ModuleName
+ SUP_MODULE_SEC
+ str(Num
) + SectionSuffix
.get(SectionType
))
162 GenFdsGlobalVariable
.GenerateSection(OutputFile
, [], 'EFI_SECTION_VERSION',
165 IsMakefile
=IsMakefile
)
166 OutputFileList
.append(OutputFile
)
169 # If Section Type is BINARY_FILE_TYPE_UI
171 elif SectionType
== BINARY_FILE_TYPE_UI
:
173 InfOverrideUiString
= False
174 if FfsInf
.Ui
is not None:
175 StringData
= FfsInf
.Ui
176 InfOverrideUiString
= True
178 if InfOverrideUiString
:
180 if IsMakefile
and StringData
== ModuleNameStr
:
181 StringData
= "$(MODULE_NAME)"
182 OutputFile
= os
.path
.join( OutputPath
, ModuleName
+ SUP_MODULE_SEC
+ str(Num
) + SectionSuffix
.get(SectionType
))
183 GenFdsGlobalVariable
.GenerateSection(OutputFile
, [], 'EFI_SECTION_USER_INTERFACE',
184 Ui
=StringData
, IsMakefile
=IsMakefile
)
185 OutputFileList
.append(OutputFile
)
188 for File
in FileList
:
190 Num
= '%s.%d' %(SecNum
, Index
)
191 OutputFile
= os
.path
.join(OutputPath
, ModuleName
+ SUP_MODULE_SEC
+ Num
+ SectionSuffix
.get(SectionType
))
195 if IsMakefile
and UiString
== ModuleNameStr
:
196 UiString
= "$(MODULE_NAME)"
197 GenFdsGlobalVariable
.GenerateSection(OutputFile
, [], 'EFI_SECTION_USER_INTERFACE',
198 Ui
=UiString
, IsMakefile
=IsMakefile
)
199 OutputFileList
.append(OutputFile
)
201 if StringData
is not None and len(StringData
) > 0:
202 UiTuple
= ('-n', '"' + StringData
+ '"')
206 if self
.Optional
== True :
207 GenFdsGlobalVariable
.VerboseLogger( "Optional Section don't exist!")
210 EdkLogger
.error("GenFds", GENFDS_ERROR
, "File: %s miss UI Section value" %InfFileName
)
213 if IsMakefile
and StringData
== ModuleNameStr
:
214 StringData
= "$(MODULE_NAME)"
215 OutputFile
= os
.path
.join( OutputPath
, ModuleName
+ SUP_MODULE_SEC
+ str(Num
) + SectionSuffix
.get(SectionType
))
216 GenFdsGlobalVariable
.GenerateSection(OutputFile
, [], 'EFI_SECTION_USER_INTERFACE',
217 Ui
=StringData
, IsMakefile
=IsMakefile
)
218 OutputFileList
.append(OutputFile
)
221 # If Section Type is BINARY_FILE_TYPE_RAW
223 elif SectionType
== BINARY_FILE_TYPE_RAW
:
224 """If File List is empty"""
226 if self
.Optional
== True:
227 GenFdsGlobalVariable
.VerboseLogger("Optional Section don't exist!")
230 EdkLogger
.error("GenFds", GENFDS_ERROR
, "Output file for %s section could not be found for %s" % (SectionType
, InfFileName
))
232 elif len(FileList
) > 1:
233 EdkLogger
.error("GenFds", GENFDS_ERROR
,
234 "Files suffixed with %s are not allowed to have more than one file in %s[Binaries] section" % (
235 self
.FileExtension
, InfFileName
))
237 for File
in FileList
:
238 File
= GenFdsGlobalVariable
.MacroExtend(File
, Dict
)
239 OutputFileList
.append(File
)
242 """If File List is empty"""
244 if self
.Optional
== True:
245 GenFdsGlobalVariable
.VerboseLogger("Optional Section don't exist!")
248 EdkLogger
.error("GenFds", GENFDS_ERROR
, "Output file for %s section could not be found for %s" % (SectionType
, InfFileName
))
251 """Convert the File to Section file one by one """
252 for File
in FileList
:
253 """ Copy Map file to FFS output path """
255 Num
= '%s.%d' %(SecNum
, Index
)
256 OutputFile
= os
.path
.join( OutputPath
, ModuleName
+ SUP_MODULE_SEC
+ Num
+ SectionSuffix
.get(SectionType
))
257 File
= GenFdsGlobalVariable
.MacroExtend(File
, Dict
)
259 #Get PE Section alignment when align is set to AUTO
260 if self
.Alignment
== 'Auto' and (SectionType
== BINARY_FILE_TYPE_PE32
or SectionType
== BINARY_FILE_TYPE_TE
):
261 ImageObj
= PeImageClass (File
)
262 if ImageObj
.SectionAlignment
< 0x400:
263 Align
= str (ImageObj
.SectionAlignment
)
264 elif ImageObj
.SectionAlignment
< 0x100000:
265 Align
= str (ImageObj
.SectionAlignment
// 0x400) + 'K'
267 Align
= str (ImageObj
.SectionAlignment
// 0x100000) + 'M'
269 if File
[(len(File
)-4):] == '.efi':
270 MapFile
= File
.replace('.efi', '.map')
271 CopyMapFile
= os
.path
.join(OutputPath
, ModuleName
+ '.map')
273 if GenFdsGlobalVariable
.CopyList
== []:
274 GenFdsGlobalVariable
.CopyList
= [(MapFile
, CopyMapFile
)]
276 GenFdsGlobalVariable
.CopyList
.append((MapFile
, CopyMapFile
))
278 if os
.path
.exists(MapFile
):
279 if not os
.path
.exists(CopyMapFile
) or \
280 (os
.path
.getmtime(MapFile
) > os
.path
.getmtime(CopyMapFile
)):
281 CopyLongFilePath(MapFile
, CopyMapFile
)
284 FileBeforeStrip
= os
.path
.join(OutputPath
, ModuleName
+ '.efi')
286 if GenFdsGlobalVariable
.CopyList
== []:
287 GenFdsGlobalVariable
.CopyList
= [(File
, FileBeforeStrip
)]
289 GenFdsGlobalVariable
.CopyList
.append((File
, FileBeforeStrip
))
291 if not os
.path
.exists(FileBeforeStrip
) or \
292 (os
.path
.getmtime(File
) > os
.path
.getmtime(FileBeforeStrip
)):
293 CopyLongFilePath(File
, FileBeforeStrip
)
294 StrippedFile
= os
.path
.join(OutputPath
, ModuleName
+ '.stripped')
295 GenFdsGlobalVariable
.GenerateFirmwareImage(
299 IsMakefile
= IsMakefile
303 """For TE Section call GenFw to generate TE image"""
305 if SectionType
== BINARY_FILE_TYPE_TE
:
306 TeFile
= os
.path
.join( OutputPath
, ModuleName
+ 'Te.raw')
307 GenFdsGlobalVariable
.GenerateFirmwareImage(
311 IsMakefile
= IsMakefile
315 """Call GenSection"""
316 GenFdsGlobalVariable
.GenerateSection(OutputFile
,
318 Section
.Section
.SectionType
.get (SectionType
),
319 IsMakefile
=IsMakefile
321 OutputFileList
.append(OutputFile
)
323 return OutputFileList
, Align