]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/GenFds/EfiSection.py
BaseTools: Set section alignment as zero if its type is Auto
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / EfiSection.py
CommitLineData
30fdf114
LG
1## @file\r
2# process rule section generation\r
3#\r
a146c532 4# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
30fdf114 5#\r
2e351cbe 6# SPDX-License-Identifier: BSD-2-Clause-Patent\r
30fdf114
LG
7#\r
8\r
9##\r
10# Import Modules\r
11#\r
1ccc4d89 12from __future__ import absolute_import\r
52302d4d 13from struct import *\r
bfa65b61
GL
14from . import Section\r
15from .GenFdsGlobalVariable import GenFdsGlobalVariable\r
30fdf114 16import subprocess\r
9e47e6f9 17from .Ffs import SectionSuffix\r
1be2ed90 18import Common.LongFilePathOs as os\r
30fdf114 19from CommonDataClass.FdfClass import EfiSectionClassObject\r
30fdf114
LG
20from Common import EdkLogger\r
21from Common.BuildToolError import *\r
52302d4d 22from Common.Misc import PeImageClass\r
1be2ed90
HC
23from Common.LongFilePathSupport import OpenLongFilePath as open\r
24from Common.LongFilePathSupport import CopyLongFilePath\r
8bb63e37 25from Common.DataType import *\r
30fdf114
LG
26\r
27## generate rule section\r
28#\r
29#\r
30class EfiSection (EfiSectionClassObject):\r
31\r
32 ## The constructor\r
33 #\r
34 # @param self The object pointer\r
35 #\r
36 def __init__(self):\r
37 EfiSectionClassObject.__init__(self)\r
38\r
39 ## GenSection() method\r
40 #\r
41 # Generate rule section\r
42 #\r
43 # @param self The object pointer\r
44 # @param OutputPath Where to place output file\r
45 # @param ModuleName Which module this section belongs to\r
46 # @param SecNum Index of section\r
47 # @param KeyStringList Filter for inputs of section generation\r
48 # @param FfsInf FfsInfStatement object that contains this section data\r
49 # @param Dict dictionary contains macro and its value\r
50 # @retval tuple (Generated file name list, section alignment)\r
51 #\r
e32f7bc9 52 def GenSection(self, OutputPath, ModuleName, SecNum, KeyStringList, FfsInf = None, Dict = None, IsMakefile = False) :\r
f7496d71 53\r
4231a819 54 if self.FileName is not None and self.FileName.startswith('PCD('):\r
30fdf114
LG
55 self.FileName = GenFdsGlobalVariable.GetPcdValue(self.FileName)\r
56 """Prepare the parameter of GenSection"""\r
4231a819 57 if FfsInf is not None :\r
30fdf114
LG
58 InfFileName = FfsInf.InfFileName\r
59 SectionType = FfsInf.__ExtendMacro__(self.SectionType)\r
60 Filename = FfsInf.__ExtendMacro__(self.FileName)\r
61 BuildNum = FfsInf.__ExtendMacro__(self.BuildNum)\r
62 StringData = FfsInf.__ExtendMacro__(self.StringData)\r
a146c532 63 ModuleNameStr = FfsInf.__ExtendMacro__('$(MODULE_NAME)')\r
30fdf114 64 NoStrip = True\r
8ef653aa 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):\r
4231a819 66 if FfsInf.KeepReloc is not None:\r
30fdf114 67 NoStrip = FfsInf.KeepReloc\r
4231a819 68 elif FfsInf.KeepRelocFromRule is not None:\r
30fdf114 69 NoStrip = FfsInf.KeepRelocFromRule\r
4231a819 70 elif self.KeepReloc is not None:\r
30fdf114 71 NoStrip = self.KeepReloc\r
4231a819 72 elif FfsInf.ShadowFromInfFile is not None:\r
30fdf114
LG
73 NoStrip = FfsInf.ShadowFromInfFile\r
74 else:\r
75 EdkLogger.error("GenFds", GENFDS_ERROR, "Module %s apply rule for None!" %ModuleName)\r
76\r
77 """If the file name was pointed out, add it in FileList"""\r
78 FileList = []\r
e32f7bc9
FZ
79 if Dict is None:\r
80 Dict = {}\r
4231a819 81 if Filename is not None:\r
30fdf114 82 Filename = GenFdsGlobalVariable.MacroExtend(Filename, Dict)\r
f51461c8
LG
83 # check if the path is absolute or relative\r
84 if os.path.isabs(Filename):\r
85 Filename = os.path.normpath(Filename)\r
86 else:\r
87 Filename = os.path.normpath(os.path.join(FfsInf.EfiOutputPath, Filename))\r
52302d4d 88\r
30fdf114
LG
89 if not self.Optional:\r
90 FileList.append(Filename)\r
91 elif os.path.exists(Filename):\r
92 FileList.append(Filename)\r
a146c532
FY
93 elif IsMakefile:\r
94 SuffixMap = FfsInf.GetFinalTargetSuffixMap()\r
95 if '.depex' in SuffixMap:\r
cf245466 96 FileList.append(Filename)\r
30fdf114 97 else:\r
04797875 98 FileList, IsSect = Section.Section.GetFileList(FfsInf, self.FileType, self.FileExtension, Dict, IsMakefile=IsMakefile, SectionType=SectionType)\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
9e47e6f9 123 OutputFile = os.path.join( OutputPath, ModuleName + SUP_MODULE_SEC + str(Num) + SectionSuffix.get(SectionType))\r
30fdf114 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
ccaa7754 133 Num = '%s.%d' %(SecNum, Index)\r
9e47e6f9 134 OutputFile = os.path.join(OutputPath, ModuleName + SUP_MODULE_SEC + Num + SectionSuffix.get(SectionType))\r
30fdf114
LG
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
f7496d71 155 #if VerString == '' and\r
30fdf114
LG
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
9e47e6f9 163 OutputFile = os.path.join( OutputPath, ModuleName + SUP_MODULE_SEC + str(Num) + SectionSuffix.get(SectionType))\r
30fdf114 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
91fa33ee 171 # If Section Type is BINARY_FILE_TYPE_UI\r
30fdf114 172 #\r
91fa33ee 173 elif SectionType == BINARY_FILE_TYPE_UI:\r
30fdf114
LG
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
a146c532
FY
182 if IsMakefile and StringData == ModuleNameStr:\r
183 StringData = "$(MODULE_NAME)"\r
9e47e6f9 184 OutputFile = os.path.join( OutputPath, ModuleName + SUP_MODULE_SEC + str(Num) + SectionSuffix.get(SectionType))\r
30fdf114 185 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_USER_INTERFACE',\r
37de70b7 186 Ui=StringData, IsMakefile=IsMakefile)\r
30fdf114
LG
187 OutputFileList.append(OutputFile)\r
188\r
189 elif FileList != []:\r
190 for File in FileList:\r
191 Index = Index + 1\r
ccaa7754 192 Num = '%s.%d' %(SecNum, Index)\r
9e47e6f9 193 OutputFile = os.path.join(OutputPath, ModuleName + SUP_MODULE_SEC + Num + SectionSuffix.get(SectionType))\r
30fdf114
LG
194 f = open(File, 'r')\r
195 UiString = f.read()\r
196 f.close()\r
a146c532
FY
197 if IsMakefile and UiString == ModuleNameStr:\r
198 UiString = "$(MODULE_NAME)"\r
30fdf114 199 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_USER_INTERFACE',\r
37de70b7 200 Ui=UiString, IsMakefile=IsMakefile)\r
30fdf114
LG
201 OutputFileList.append(OutputFile)\r
202 else:\r
4231a819 203 if StringData is not None and len(StringData) > 0:\r
30fdf114
LG
204 UiTuple = ('-n', '"' + StringData + '"')\r
205 else:\r
206 UiTuple = tuple()\r
207\r
208 if self.Optional == True :\r
209 GenFdsGlobalVariable.VerboseLogger( "Optional Section don't exist!")\r
210 return '', None\r
211 else:\r
212 EdkLogger.error("GenFds", GENFDS_ERROR, "File: %s miss UI Section value" %InfFileName)\r
213\r
214 Num = SecNum\r
a146c532
FY
215 if IsMakefile and StringData == ModuleNameStr:\r
216 StringData = "$(MODULE_NAME)"\r
9e47e6f9 217 OutputFile = os.path.join( OutputPath, ModuleName + SUP_MODULE_SEC + str(Num) + SectionSuffix.get(SectionType))\r
30fdf114 218 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_USER_INTERFACE',\r
37de70b7 219 Ui=StringData, IsMakefile=IsMakefile)\r
30fdf114
LG
220 OutputFileList.append(OutputFile)\r
221\r
04797875
FZ
222 #\r
223 # If Section Type is BINARY_FILE_TYPE_RAW\r
224 #\r
225 elif SectionType == BINARY_FILE_TYPE_RAW:\r
226 """If File List is empty"""\r
227 if FileList == []:\r
228 if self.Optional == True:\r
229 GenFdsGlobalVariable.VerboseLogger("Optional Section don't exist!")\r
230 return [], None\r
231 else:\r
232 EdkLogger.error("GenFds", GENFDS_ERROR, "Output file for %s section could not be found for %s" % (SectionType, InfFileName))\r
233\r
234 elif len(FileList) > 1:\r
235 EdkLogger.error("GenFds", GENFDS_ERROR,\r
236 "Files suffixed with %s are not allowed to have more than one file in %s[Binaries] section" % (\r
237 self.FileExtension, InfFileName))\r
238 else:\r
239 for File in FileList:\r
240 File = GenFdsGlobalVariable.MacroExtend(File, Dict)\r
241 OutputFileList.append(File)\r
30fdf114
LG
242\r
243 else:\r
244 """If File List is empty"""\r
245 if FileList == [] :\r
246 if self.Optional == True:\r
97fa0ee9
YL
247 GenFdsGlobalVariable.VerboseLogger("Optional Section don't exist!")\r
248 return [], None\r
30fdf114 249 else:\r
97fa0ee9 250 EdkLogger.error("GenFds", GENFDS_ERROR, "Output file for %s section could not be found for %s" % (SectionType, InfFileName))\r
30fdf114
LG
251\r
252 else:\r
253 """Convert the File to Section file one by one """\r
254 for File in FileList:\r
255 """ Copy Map file to FFS output path """\r
256 Index = Index + 1\r
ccaa7754 257 Num = '%s.%d' %(SecNum, Index)\r
9e47e6f9 258 OutputFile = os.path.join( OutputPath, ModuleName + SUP_MODULE_SEC + Num + SectionSuffix.get(SectionType))\r
30fdf114 259 File = GenFdsGlobalVariable.MacroExtend(File, Dict)\r
f7496d71 260\r
52302d4d 261 #Get PE Section alignment when align is set to AUTO\r
91fa33ee 262 if self.Alignment == 'Auto' and (SectionType == BINARY_FILE_TYPE_PE32 or SectionType == BINARY_FILE_TYPE_TE):\r
d8be0107 263 Align = "0"\r
d0da48f1 264 if File[(len(File)-4):] == '.efi' and FfsInf.InfModule.BaseName == os.path.basename(File)[:-4]:\r
30fdf114 265 MapFile = File.replace('.efi', '.map')\r
37de70b7
YZ
266 CopyMapFile = os.path.join(OutputPath, ModuleName + '.map')\r
267 if IsMakefile:\r
268 if GenFdsGlobalVariable.CopyList == []:\r
269 GenFdsGlobalVariable.CopyList = [(MapFile, CopyMapFile)]\r
270 else:\r
271 GenFdsGlobalVariable.CopyList.append((MapFile, CopyMapFile))\r
272 else:\r
273 if os.path.exists(MapFile):\r
274 if not os.path.exists(CopyMapFile) or \\r
275 (os.path.getmtime(MapFile) > os.path.getmtime(CopyMapFile)):\r
276 CopyLongFilePath(MapFile, CopyMapFile)\r
30fdf114
LG
277\r
278 if not NoStrip:\r
279 FileBeforeStrip = os.path.join(OutputPath, ModuleName + '.efi')\r
37de70b7
YZ
280 if IsMakefile:\r
281 if GenFdsGlobalVariable.CopyList == []:\r
282 GenFdsGlobalVariable.CopyList = [(File, FileBeforeStrip)]\r
283 else:\r
284 GenFdsGlobalVariable.CopyList.append((File, FileBeforeStrip))\r
285 else:\r
286 if not os.path.exists(FileBeforeStrip) or \\r
287 (os.path.getmtime(File) > os.path.getmtime(FileBeforeStrip)):\r
288 CopyLongFilePath(File, FileBeforeStrip)\r
30fdf114
LG
289 StrippedFile = os.path.join(OutputPath, ModuleName + '.stripped')\r
290 GenFdsGlobalVariable.GenerateFirmwareImage(\r
37de70b7
YZ
291 StrippedFile,\r
292 [File],\r
293 Strip=True,\r
294 IsMakefile = IsMakefile\r
295 )\r
30fdf114 296 File = StrippedFile\r
f7496d71 297\r
30fdf114
LG
298 """For TE Section call GenFw to generate TE image"""\r
299\r
91fa33ee 300 if SectionType == BINARY_FILE_TYPE_TE:\r
30fdf114
LG
301 TeFile = os.path.join( OutputPath, ModuleName + 'Te.raw')\r
302 GenFdsGlobalVariable.GenerateFirmwareImage(\r
37de70b7
YZ
303 TeFile,\r
304 [File],\r
305 Type='te',\r
306 IsMakefile = IsMakefile\r
307 )\r
30fdf114
LG
308 File = TeFile\r
309\r
310 """Call GenSection"""\r
311 GenFdsGlobalVariable.GenerateSection(OutputFile,\r
37de70b7
YZ
312 [File],\r
313 Section.Section.SectionType.get (SectionType),\r
314 IsMakefile=IsMakefile\r
315 )\r
30fdf114
LG
316 OutputFileList.append(OutputFile)\r
317\r
547a6507 318 return OutputFileList, Align\r