]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/GenFds/EfiSection.py
BaseTools: Remove equality operator with None
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / EfiSection.py
CommitLineData
30fdf114
LG
1## @file\r
2# process rule section generation\r
3#\r
e921f58d 4# Copyright (c) 2007 - 2017, Intel Corporation. All rights reserved.<BR>\r
30fdf114 5#\r
40d841f6 6# This program and the accompanying materials\r
30fdf114
LG
7# are licensed and made available under the terms and conditions of the BSD License\r
8# which accompanies this distribution. The full text of the license may be found at\r
9# http://opensource.org/licenses/bsd-license.php\r
10#\r
11# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13#\r
14\r
15##\r
16# Import Modules\r
17#\r
52302d4d 18from struct import *\r
30fdf114
LG
19import Section\r
20from GenFdsGlobalVariable import GenFdsGlobalVariable\r
21import subprocess\r
22from Ffs import Ffs\r
1be2ed90 23import Common.LongFilePathOs as os\r
30fdf114 24from CommonDataClass.FdfClass import EfiSectionClassObject\r
30fdf114
LG
25from Common import EdkLogger\r
26from Common.BuildToolError import *\r
52302d4d 27from Common.Misc import PeImageClass\r
1be2ed90
HC
28from Common.LongFilePathSupport import OpenLongFilePath as open\r
29from Common.LongFilePathSupport import CopyLongFilePath\r
30fdf114
LG
30\r
31## generate rule section\r
32#\r
33#\r
34class EfiSection (EfiSectionClassObject):\r
35\r
36 ## The constructor\r
37 #\r
38 # @param self The object pointer\r
39 #\r
40 def __init__(self):\r
41 EfiSectionClassObject.__init__(self)\r
42\r
43 ## GenSection() method\r
44 #\r
45 # Generate rule section\r
46 #\r
47 # @param self The object pointer\r
48 # @param OutputPath Where to place output file\r
49 # @param ModuleName Which module this section belongs to\r
50 # @param SecNum Index of section\r
51 # @param KeyStringList Filter for inputs of section generation\r
52 # @param FfsInf FfsInfStatement object that contains this section data\r
53 # @param Dict dictionary contains macro and its value\r
54 # @retval tuple (Generated file name list, section alignment)\r
55 #\r
37de70b7 56 def GenSection(self, OutputPath, ModuleName, SecNum, KeyStringList, FfsInf = None, Dict = {}, IsMakefile = False) :\r
30fdf114 57 \r
4231a819 58 if self.FileName is not None and self.FileName.startswith('PCD('):\r
30fdf114
LG
59 self.FileName = GenFdsGlobalVariable.GetPcdValue(self.FileName)\r
60 """Prepare the parameter of GenSection"""\r
4231a819 61 if FfsInf is not None :\r
30fdf114
LG
62 InfFileName = FfsInf.InfFileName\r
63 SectionType = FfsInf.__ExtendMacro__(self.SectionType)\r
64 Filename = FfsInf.__ExtendMacro__(self.FileName)\r
65 BuildNum = FfsInf.__ExtendMacro__(self.BuildNum)\r
66 StringData = FfsInf.__ExtendMacro__(self.StringData)\r
67 NoStrip = True\r
68 if FfsInf.ModuleType in ('SEC', 'PEI_CORE', 'PEIM') and SectionType in ('TE', 'PE32'):\r
4231a819 69 if FfsInf.KeepReloc is not None:\r
30fdf114 70 NoStrip = FfsInf.KeepReloc\r
4231a819 71 elif FfsInf.KeepRelocFromRule is not None:\r
30fdf114 72 NoStrip = FfsInf.KeepRelocFromRule\r
4231a819 73 elif self.KeepReloc is not None:\r
30fdf114 74 NoStrip = self.KeepReloc\r
4231a819 75 elif FfsInf.ShadowFromInfFile is not None:\r
30fdf114
LG
76 NoStrip = FfsInf.ShadowFromInfFile\r
77 else:\r
78 EdkLogger.error("GenFds", GENFDS_ERROR, "Module %s apply rule for None!" %ModuleName)\r
79\r
80 """If the file name was pointed out, add it in FileList"""\r
81 FileList = []\r
4231a819 82 if Filename is not None:\r
30fdf114 83 Filename = GenFdsGlobalVariable.MacroExtend(Filename, Dict)\r
f51461c8
LG
84 # check if the path is absolute or relative\r
85 if os.path.isabs(Filename):\r
86 Filename = os.path.normpath(Filename)\r
87 else:\r
88 Filename = os.path.normpath(os.path.join(FfsInf.EfiOutputPath, Filename))\r
52302d4d 89\r
30fdf114
LG
90 if not self.Optional:\r
91 FileList.append(Filename)\r
92 elif os.path.exists(Filename):\r
93 FileList.append(Filename)\r
37de70b7 94 elif '.depex' in FfsInf.FinalTargetSuffixMap or FfsInf.Depex:\r
cf245466
YF
95 if IsMakefile:\r
96 FileList.append(Filename)\r
30fdf114 97 else:\r
cf245466 98 FileList, IsSect = Section.Section.GetFileList(FfsInf, self.FileType, self.FileExtension, Dict, IsMakefile=IsMakefile)\r
30fdf114
LG
99 if IsSect :\r
100 return FileList, self.Alignment\r
101\r
102 Index = 0\r
547a6507 103 Align = self.Alignment\r
30fdf114
LG
104\r
105 """ If Section type is 'VERSION'"""\r
106 OutputFileList = []\r
107 if SectionType == 'VERSION':\r
108\r
109 InfOverrideVerString = False\r
4231a819 110 if FfsInf.Version is not None:\r
30fdf114
LG
111 #StringData = FfsInf.Version\r
112 BuildNum = FfsInf.Version\r
113 InfOverrideVerString = True\r
114\r
115 if InfOverrideVerString:\r
116 #VerTuple = ('-n', '"' + StringData + '"')\r
4231a819 117 if BuildNum is not None and BuildNum != '':\r
30fdf114
LG
118 BuildNumTuple = ('-j', BuildNum)\r
119 else:\r
120 BuildNumTuple = tuple()\r
121\r
122 Num = SecNum\r
123 OutputFile = os.path.join( OutputPath, ModuleName + 'SEC' + str(Num) + Ffs.SectionSuffix.get(SectionType))\r
124 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_VERSION',\r
37de70b7
YZ
125 #Ui=StringData,\r
126 Ver=BuildNum,\r
127 IsMakefile=IsMakefile)\r
30fdf114
LG
128 OutputFileList.append(OutputFile)\r
129\r
130 elif FileList != []:\r
131 for File in FileList:\r
132 Index = Index + 1\r
133 Num = '%s.%d' %(SecNum , Index)\r
134 OutputFile = os.path.join(OutputPath, ModuleName + 'SEC' + Num + Ffs.SectionSuffix.get(SectionType))\r
135 f = open(File, 'r')\r
136 VerString = f.read()\r
137 f.close()\r
30fdf114 138 BuildNum = VerString\r
4231a819 139 if BuildNum is not None and BuildNum != '':\r
30fdf114
LG
140 BuildNumTuple = ('-j', BuildNum)\r
141 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_VERSION',\r
37de70b7
YZ
142 #Ui=VerString,\r
143 Ver=BuildNum,\r
144 IsMakefile=IsMakefile)\r
30fdf114
LG
145 OutputFileList.append(OutputFile)\r
146\r
147 else:\r
30fdf114 148 BuildNum = StringData\r
4231a819 149 if BuildNum is not None and BuildNum != '':\r
30fdf114
LG
150 BuildNumTuple = ('-j', BuildNum)\r
151 else:\r
152 BuildNumTuple = tuple()\r
153 BuildNumString = ' ' + ' '.join(BuildNumTuple)\r
154\r
155 #if VerString == '' and \r
156 if BuildNumString == '':\r
157 if self.Optional == True :\r
158 GenFdsGlobalVariable.VerboseLogger( "Optional Section don't exist!")\r
159 return [], None\r
160 else:\r
161 EdkLogger.error("GenFds", GENFDS_ERROR, "File: %s miss Version Section value" %InfFileName)\r
162 Num = SecNum\r
163 OutputFile = os.path.join( OutputPath, ModuleName + 'SEC' + str(Num) + Ffs.SectionSuffix.get(SectionType))\r
164 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_VERSION',\r
37de70b7
YZ
165 #Ui=VerString,\r
166 Ver=BuildNum,\r
167 IsMakefile=IsMakefile)\r
30fdf114
LG
168 OutputFileList.append(OutputFile)\r
169\r
170 #\r
171 # If Section Type is 'UI'\r
172 #\r
173 elif SectionType == 'UI':\r
174\r
175 InfOverrideUiString = False\r
4231a819 176 if FfsInf.Ui is not None:\r
30fdf114
LG
177 StringData = FfsInf.Ui\r
178 InfOverrideUiString = True\r
179\r
180 if InfOverrideUiString:\r
181 Num = SecNum\r
182 OutputFile = os.path.join( OutputPath, ModuleName + 'SEC' + str(Num) + Ffs.SectionSuffix.get(SectionType))\r
183 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_USER_INTERFACE',\r
37de70b7 184 Ui=StringData, IsMakefile=IsMakefile)\r
30fdf114
LG
185 OutputFileList.append(OutputFile)\r
186\r
187 elif FileList != []:\r
188 for File in FileList:\r
189 Index = Index + 1\r
190 Num = '%s.%d' %(SecNum , Index)\r
191 OutputFile = os.path.join(OutputPath, ModuleName + 'SEC' + Num + Ffs.SectionSuffix.get(SectionType))\r
192 f = open(File, 'r')\r
193 UiString = f.read()\r
194 f.close()\r
195 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_USER_INTERFACE',\r
37de70b7 196 Ui=UiString, IsMakefile=IsMakefile)\r
30fdf114
LG
197 OutputFileList.append(OutputFile)\r
198 else:\r
4231a819 199 if StringData is not None and len(StringData) > 0:\r
30fdf114
LG
200 UiTuple = ('-n', '"' + StringData + '"')\r
201 else:\r
202 UiTuple = tuple()\r
203\r
204 if self.Optional == True :\r
205 GenFdsGlobalVariable.VerboseLogger( "Optional Section don't exist!")\r
206 return '', None\r
207 else:\r
208 EdkLogger.error("GenFds", GENFDS_ERROR, "File: %s miss UI Section value" %InfFileName)\r
209\r
210 Num = SecNum\r
211 OutputFile = os.path.join( OutputPath, ModuleName + 'SEC' + str(Num) + Ffs.SectionSuffix.get(SectionType))\r
212 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_USER_INTERFACE',\r
37de70b7 213 Ui=StringData, IsMakefile=IsMakefile)\r
30fdf114
LG
214 OutputFileList.append(OutputFile)\r
215\r
216\r
217 else:\r
218 """If File List is empty"""\r
219 if FileList == [] :\r
220 if self.Optional == True:\r
97fa0ee9
YL
221 GenFdsGlobalVariable.VerboseLogger("Optional Section don't exist!")\r
222 return [], None\r
30fdf114 223 else:\r
97fa0ee9 224 EdkLogger.error("GenFds", GENFDS_ERROR, "Output file for %s section could not be found for %s" % (SectionType, InfFileName))\r
30fdf114
LG
225\r
226 else:\r
227 """Convert the File to Section file one by one """\r
228 for File in FileList:\r
229 """ Copy Map file to FFS output path """\r
230 Index = Index + 1\r
231 Num = '%s.%d' %(SecNum , Index)\r
232 OutputFile = os.path.join( OutputPath, ModuleName + 'SEC' + Num + Ffs.SectionSuffix.get(SectionType))\r
233 File = GenFdsGlobalVariable.MacroExtend(File, Dict)\r
52302d4d
LG
234 \r
235 #Get PE Section alignment when align is set to AUTO\r
236 if self.Alignment == 'Auto' and (SectionType == 'PE32' or SectionType == 'TE'):\r
237 ImageObj = PeImageClass (File)\r
238 if ImageObj.SectionAlignment < 0x400:\r
547a6507 239 Align = str (ImageObj.SectionAlignment)\r
e921f58d 240 elif ImageObj.SectionAlignment < 0x100000:\r
547a6507 241 Align = str (ImageObj.SectionAlignment / 0x400) + 'K'\r
e921f58d
YZ
242 else:\r
243 Align = str (ImageObj.SectionAlignment / 0x100000) + 'M'\r
52302d4d 244\r
30fdf114
LG
245 if File[(len(File)-4):] == '.efi':\r
246 MapFile = File.replace('.efi', '.map')\r
37de70b7
YZ
247 CopyMapFile = os.path.join(OutputPath, ModuleName + '.map')\r
248 if IsMakefile:\r
249 if GenFdsGlobalVariable.CopyList == []:\r
250 GenFdsGlobalVariable.CopyList = [(MapFile, CopyMapFile)]\r
251 else:\r
252 GenFdsGlobalVariable.CopyList.append((MapFile, CopyMapFile))\r
253 else:\r
254 if os.path.exists(MapFile):\r
255 if not os.path.exists(CopyMapFile) or \\r
256 (os.path.getmtime(MapFile) > os.path.getmtime(CopyMapFile)):\r
257 CopyLongFilePath(MapFile, CopyMapFile)\r
30fdf114
LG
258\r
259 if not NoStrip:\r
260 FileBeforeStrip = os.path.join(OutputPath, ModuleName + '.efi')\r
37de70b7
YZ
261 if IsMakefile:\r
262 if GenFdsGlobalVariable.CopyList == []:\r
263 GenFdsGlobalVariable.CopyList = [(File, FileBeforeStrip)]\r
264 else:\r
265 GenFdsGlobalVariable.CopyList.append((File, FileBeforeStrip))\r
266 else:\r
267 if not os.path.exists(FileBeforeStrip) or \\r
268 (os.path.getmtime(File) > os.path.getmtime(FileBeforeStrip)):\r
269 CopyLongFilePath(File, FileBeforeStrip)\r
30fdf114
LG
270 StrippedFile = os.path.join(OutputPath, ModuleName + '.stripped')\r
271 GenFdsGlobalVariable.GenerateFirmwareImage(\r
37de70b7
YZ
272 StrippedFile,\r
273 [File],\r
274 Strip=True,\r
275 IsMakefile = IsMakefile\r
276 )\r
30fdf114 277 File = StrippedFile\r
52302d4d 278 \r
30fdf114
LG
279 """For TE Section call GenFw to generate TE image"""\r
280\r
281 if SectionType == 'TE':\r
282 TeFile = os.path.join( OutputPath, ModuleName + 'Te.raw')\r
283 GenFdsGlobalVariable.GenerateFirmwareImage(\r
37de70b7
YZ
284 TeFile,\r
285 [File],\r
286 Type='te',\r
287 IsMakefile = IsMakefile\r
288 )\r
30fdf114
LG
289 File = TeFile\r
290\r
291 """Call GenSection"""\r
292 GenFdsGlobalVariable.GenerateSection(OutputFile,\r
37de70b7
YZ
293 [File],\r
294 Section.Section.SectionType.get (SectionType),\r
295 IsMakefile=IsMakefile\r
296 )\r
30fdf114
LG
297 OutputFileList.append(OutputFile)\r
298\r
547a6507 299 return OutputFileList, Align\r