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