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