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