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