]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/GenFds/EfiSection.py
BaseTools: Replace BSD License with BSD+Patent License
[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
37de70b7 52 def GenSection(self, OutputPath, ModuleName, SecNum, KeyStringList, FfsInf = None, Dict = {}, 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
4231a819 79 if Filename is not None:\r
30fdf114 80 Filename = GenFdsGlobalVariable.MacroExtend(Filename, Dict)\r
f51461c8
LG
81 # check if the path is absolute or relative\r
82 if os.path.isabs(Filename):\r
83 Filename = os.path.normpath(Filename)\r
84 else:\r
85 Filename = os.path.normpath(os.path.join(FfsInf.EfiOutputPath, Filename))\r
52302d4d 86\r
30fdf114
LG
87 if not self.Optional:\r
88 FileList.append(Filename)\r
89 elif os.path.exists(Filename):\r
90 FileList.append(Filename)\r
a146c532
FY
91 elif IsMakefile:\r
92 SuffixMap = FfsInf.GetFinalTargetSuffixMap()\r
93 if '.depex' in SuffixMap:\r
cf245466 94 FileList.append(Filename)\r
30fdf114 95 else:\r
cf245466 96 FileList, IsSect = Section.Section.GetFileList(FfsInf, self.FileType, self.FileExtension, Dict, IsMakefile=IsMakefile)\r
30fdf114
LG
97 if IsSect :\r
98 return FileList, self.Alignment\r
99\r
100 Index = 0\r
547a6507 101 Align = self.Alignment\r
30fdf114
LG
102\r
103 """ If Section type is 'VERSION'"""\r
104 OutputFileList = []\r
105 if SectionType == 'VERSION':\r
106\r
107 InfOverrideVerString = False\r
4231a819 108 if FfsInf.Version is not None:\r
30fdf114
LG
109 #StringData = FfsInf.Version\r
110 BuildNum = FfsInf.Version\r
111 InfOverrideVerString = True\r
112\r
113 if InfOverrideVerString:\r
114 #VerTuple = ('-n', '"' + StringData + '"')\r
4231a819 115 if BuildNum is not None and BuildNum != '':\r
30fdf114
LG
116 BuildNumTuple = ('-j', BuildNum)\r
117 else:\r
118 BuildNumTuple = tuple()\r
119\r
120 Num = SecNum\r
9e47e6f9 121 OutputFile = os.path.join( OutputPath, ModuleName + SUP_MODULE_SEC + str(Num) + SectionSuffix.get(SectionType))\r
30fdf114 122 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_VERSION',\r
37de70b7
YZ
123 #Ui=StringData,\r
124 Ver=BuildNum,\r
125 IsMakefile=IsMakefile)\r
30fdf114
LG
126 OutputFileList.append(OutputFile)\r
127\r
128 elif FileList != []:\r
129 for File in FileList:\r
130 Index = Index + 1\r
ccaa7754 131 Num = '%s.%d' %(SecNum, Index)\r
9e47e6f9 132 OutputFile = os.path.join(OutputPath, ModuleName + SUP_MODULE_SEC + Num + SectionSuffix.get(SectionType))\r
30fdf114
LG
133 f = open(File, 'r')\r
134 VerString = f.read()\r
135 f.close()\r
30fdf114 136 BuildNum = VerString\r
4231a819 137 if BuildNum is not None and BuildNum != '':\r
30fdf114
LG
138 BuildNumTuple = ('-j', BuildNum)\r
139 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_VERSION',\r
37de70b7
YZ
140 #Ui=VerString,\r
141 Ver=BuildNum,\r
142 IsMakefile=IsMakefile)\r
30fdf114
LG
143 OutputFileList.append(OutputFile)\r
144\r
145 else:\r
30fdf114 146 BuildNum = StringData\r
4231a819 147 if BuildNum is not None and BuildNum != '':\r
30fdf114
LG
148 BuildNumTuple = ('-j', BuildNum)\r
149 else:\r
150 BuildNumTuple = tuple()\r
151 BuildNumString = ' ' + ' '.join(BuildNumTuple)\r
152\r
f7496d71 153 #if VerString == '' and\r
30fdf114
LG
154 if BuildNumString == '':\r
155 if self.Optional == True :\r
156 GenFdsGlobalVariable.VerboseLogger( "Optional Section don't exist!")\r
157 return [], None\r
158 else:\r
159 EdkLogger.error("GenFds", GENFDS_ERROR, "File: %s miss Version Section value" %InfFileName)\r
160 Num = SecNum\r
9e47e6f9 161 OutputFile = os.path.join( OutputPath, ModuleName + SUP_MODULE_SEC + str(Num) + SectionSuffix.get(SectionType))\r
30fdf114 162 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_VERSION',\r
37de70b7
YZ
163 #Ui=VerString,\r
164 Ver=BuildNum,\r
165 IsMakefile=IsMakefile)\r
30fdf114
LG
166 OutputFileList.append(OutputFile)\r
167\r
168 #\r
91fa33ee 169 # If Section Type is BINARY_FILE_TYPE_UI\r
30fdf114 170 #\r
91fa33ee 171 elif SectionType == BINARY_FILE_TYPE_UI:\r
30fdf114
LG
172\r
173 InfOverrideUiString = False\r
4231a819 174 if FfsInf.Ui is not None:\r
30fdf114
LG
175 StringData = FfsInf.Ui\r
176 InfOverrideUiString = True\r
177\r
178 if InfOverrideUiString:\r
179 Num = SecNum\r
a146c532
FY
180 if IsMakefile and StringData == ModuleNameStr:\r
181 StringData = "$(MODULE_NAME)"\r
9e47e6f9 182 OutputFile = os.path.join( OutputPath, ModuleName + SUP_MODULE_SEC + str(Num) + SectionSuffix.get(SectionType))\r
30fdf114 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
ccaa7754 190 Num = '%s.%d' %(SecNum, Index)\r
9e47e6f9 191 OutputFile = os.path.join(OutputPath, ModuleName + SUP_MODULE_SEC + Num + SectionSuffix.get(SectionType))\r
30fdf114
LG
192 f = open(File, 'r')\r
193 UiString = f.read()\r
194 f.close()\r
a146c532
FY
195 if IsMakefile and UiString == ModuleNameStr:\r
196 UiString = "$(MODULE_NAME)"\r
30fdf114 197 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_USER_INTERFACE',\r
37de70b7 198 Ui=UiString, IsMakefile=IsMakefile)\r
30fdf114
LG
199 OutputFileList.append(OutputFile)\r
200 else:\r
4231a819 201 if StringData is not None and len(StringData) > 0:\r
30fdf114
LG
202 UiTuple = ('-n', '"' + StringData + '"')\r
203 else:\r
204 UiTuple = tuple()\r
205\r
206 if self.Optional == True :\r
207 GenFdsGlobalVariable.VerboseLogger( "Optional Section don't exist!")\r
208 return '', None\r
209 else:\r
210 EdkLogger.error("GenFds", GENFDS_ERROR, "File: %s miss UI Section value" %InfFileName)\r
211\r
212 Num = SecNum\r
a146c532
FY
213 if IsMakefile and StringData == ModuleNameStr:\r
214 StringData = "$(MODULE_NAME)"\r
9e47e6f9 215 OutputFile = os.path.join( OutputPath, ModuleName + SUP_MODULE_SEC + str(Num) + SectionSuffix.get(SectionType))\r
30fdf114 216 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_USER_INTERFACE',\r
37de70b7 217 Ui=StringData, IsMakefile=IsMakefile)\r
30fdf114
LG
218 OutputFileList.append(OutputFile)\r
219\r
220\r
221 else:\r
222 """If File List is empty"""\r
223 if FileList == [] :\r
224 if self.Optional == True:\r
97fa0ee9
YL
225 GenFdsGlobalVariable.VerboseLogger("Optional Section don't exist!")\r
226 return [], None\r
30fdf114 227 else:\r
97fa0ee9 228 EdkLogger.error("GenFds", GENFDS_ERROR, "Output file for %s section could not be found for %s" % (SectionType, InfFileName))\r
30fdf114
LG
229\r
230 else:\r
231 """Convert the File to Section file one by one """\r
232 for File in FileList:\r
233 """ Copy Map file to FFS output path """\r
234 Index = Index + 1\r
ccaa7754 235 Num = '%s.%d' %(SecNum, Index)\r
9e47e6f9 236 OutputFile = os.path.join( OutputPath, ModuleName + SUP_MODULE_SEC + Num + SectionSuffix.get(SectionType))\r
30fdf114 237 File = GenFdsGlobalVariable.MacroExtend(File, Dict)\r
f7496d71 238\r
52302d4d 239 #Get PE Section alignment when align is set to AUTO\r
91fa33ee 240 if self.Alignment == 'Auto' and (SectionType == BINARY_FILE_TYPE_PE32 or SectionType == BINARY_FILE_TYPE_TE):\r
52302d4d
LG
241 ImageObj = PeImageClass (File)\r
242 if ImageObj.SectionAlignment < 0x400:\r
547a6507 243 Align = str (ImageObj.SectionAlignment)\r
e921f58d 244 elif ImageObj.SectionAlignment < 0x100000:\r
b3e94a06 245 Align = str (ImageObj.SectionAlignment // 0x400) + 'K'\r
e921f58d 246 else:\r
b3e94a06 247 Align = str (ImageObj.SectionAlignment // 0x100000) + 'M'\r
52302d4d 248\r
30fdf114
LG
249 if File[(len(File)-4):] == '.efi':\r
250 MapFile = File.replace('.efi', '.map')\r
37de70b7
YZ
251 CopyMapFile = os.path.join(OutputPath, ModuleName + '.map')\r
252 if IsMakefile:\r
253 if GenFdsGlobalVariable.CopyList == []:\r
254 GenFdsGlobalVariable.CopyList = [(MapFile, CopyMapFile)]\r
255 else:\r
256 GenFdsGlobalVariable.CopyList.append((MapFile, CopyMapFile))\r
257 else:\r
258 if os.path.exists(MapFile):\r
259 if not os.path.exists(CopyMapFile) or \\r
260 (os.path.getmtime(MapFile) > os.path.getmtime(CopyMapFile)):\r
261 CopyLongFilePath(MapFile, CopyMapFile)\r
30fdf114
LG
262\r
263 if not NoStrip:\r
264 FileBeforeStrip = os.path.join(OutputPath, ModuleName + '.efi')\r
37de70b7
YZ
265 if IsMakefile:\r
266 if GenFdsGlobalVariable.CopyList == []:\r
267 GenFdsGlobalVariable.CopyList = [(File, FileBeforeStrip)]\r
268 else:\r
269 GenFdsGlobalVariable.CopyList.append((File, FileBeforeStrip))\r
270 else:\r
271 if not os.path.exists(FileBeforeStrip) or \\r
272 (os.path.getmtime(File) > os.path.getmtime(FileBeforeStrip)):\r
273 CopyLongFilePath(File, FileBeforeStrip)\r
30fdf114
LG
274 StrippedFile = os.path.join(OutputPath, ModuleName + '.stripped')\r
275 GenFdsGlobalVariable.GenerateFirmwareImage(\r
37de70b7
YZ
276 StrippedFile,\r
277 [File],\r
278 Strip=True,\r
279 IsMakefile = IsMakefile\r
280 )\r
30fdf114 281 File = StrippedFile\r
f7496d71 282\r
30fdf114
LG
283 """For TE Section call GenFw to generate TE image"""\r
284\r
91fa33ee 285 if SectionType == BINARY_FILE_TYPE_TE:\r
30fdf114
LG
286 TeFile = os.path.join( OutputPath, ModuleName + 'Te.raw')\r
287 GenFdsGlobalVariable.GenerateFirmwareImage(\r
37de70b7
YZ
288 TeFile,\r
289 [File],\r
290 Type='te',\r
291 IsMakefile = IsMakefile\r
292 )\r
30fdf114
LG
293 File = TeFile\r
294\r
295 """Call GenSection"""\r
296 GenFdsGlobalVariable.GenerateSection(OutputFile,\r
37de70b7
YZ
297 [File],\r
298 Section.Section.SectionType.get (SectionType),\r
299 IsMakefile=IsMakefile\r
300 )\r
30fdf114
LG
301 OutputFileList.append(OutputFile)\r
302\r
547a6507 303 return OutputFileList, Align\r