]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/GenFds/EfiSection.py
BaseTools:File open failed for VPD MapFile
[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
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
1ccc4d89 18from __future__ import absolute_import\r
52302d4d 19from struct import *\r
bfa65b61
GL
20from . import Section\r
21from .GenFdsGlobalVariable import GenFdsGlobalVariable\r
30fdf114 22import subprocess\r
9e47e6f9 23from .Ffs import SectionSuffix\r
1be2ed90 24import Common.LongFilePathOs as os\r
30fdf114 25from CommonDataClass.FdfClass import EfiSectionClassObject\r
30fdf114
LG
26from Common import EdkLogger\r
27from Common.BuildToolError import *\r
52302d4d 28from Common.Misc import PeImageClass\r
1be2ed90
HC
29from Common.LongFilePathSupport import OpenLongFilePath as open\r
30from Common.LongFilePathSupport import CopyLongFilePath\r
8bb63e37 31from Common.DataType import *\r
30fdf114
LG
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
37de70b7 58 def GenSection(self, OutputPath, ModuleName, SecNum, KeyStringList, FfsInf = None, Dict = {}, IsMakefile = False) :\r
f7496d71 59\r
4231a819 60 if self.FileName is not None and self.FileName.startswith('PCD('):\r
30fdf114
LG
61 self.FileName = GenFdsGlobalVariable.GetPcdValue(self.FileName)\r
62 """Prepare the parameter of GenSection"""\r
4231a819 63 if FfsInf is not None :\r
30fdf114
LG
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
a146c532 69 ModuleNameStr = FfsInf.__ExtendMacro__('$(MODULE_NAME)')\r
30fdf114 70 NoStrip = True\r
8ef653aa 71 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 72 if FfsInf.KeepReloc is not None:\r
30fdf114 73 NoStrip = FfsInf.KeepReloc\r
4231a819 74 elif FfsInf.KeepRelocFromRule is not None:\r
30fdf114 75 NoStrip = FfsInf.KeepRelocFromRule\r
4231a819 76 elif self.KeepReloc is not None:\r
30fdf114 77 NoStrip = self.KeepReloc\r
4231a819 78 elif FfsInf.ShadowFromInfFile is not None:\r
30fdf114
LG
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
4231a819 85 if Filename is not None:\r
30fdf114 86 Filename = GenFdsGlobalVariable.MacroExtend(Filename, Dict)\r
f51461c8
LG
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
52302d4d 92\r
30fdf114
LG
93 if not self.Optional:\r
94 FileList.append(Filename)\r
95 elif os.path.exists(Filename):\r
96 FileList.append(Filename)\r
a146c532
FY
97 elif IsMakefile:\r
98 SuffixMap = FfsInf.GetFinalTargetSuffixMap()\r
99 if '.depex' in SuffixMap:\r
cf245466 100 FileList.append(Filename)\r
30fdf114 101 else:\r
cf245466 102 FileList, IsSect = Section.Section.GetFileList(FfsInf, self.FileType, self.FileExtension, Dict, IsMakefile=IsMakefile)\r
30fdf114
LG
103 if IsSect :\r
104 return FileList, self.Alignment\r
105\r
106 Index = 0\r
547a6507 107 Align = self.Alignment\r
30fdf114
LG
108\r
109 """ If Section type is 'VERSION'"""\r
110 OutputFileList = []\r
111 if SectionType == 'VERSION':\r
112\r
113 InfOverrideVerString = False\r
4231a819 114 if FfsInf.Version is not None:\r
30fdf114
LG
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
4231a819 121 if BuildNum is not None and BuildNum != '':\r
30fdf114
LG
122 BuildNumTuple = ('-j', BuildNum)\r
123 else:\r
124 BuildNumTuple = tuple()\r
125\r
126 Num = SecNum\r
9e47e6f9 127 OutputFile = os.path.join( OutputPath, ModuleName + SUP_MODULE_SEC + str(Num) + SectionSuffix.get(SectionType))\r
30fdf114 128 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_VERSION',\r
37de70b7
YZ
129 #Ui=StringData,\r
130 Ver=BuildNum,\r
131 IsMakefile=IsMakefile)\r
30fdf114
LG
132 OutputFileList.append(OutputFile)\r
133\r
134 elif FileList != []:\r
135 for File in FileList:\r
136 Index = Index + 1\r
ccaa7754 137 Num = '%s.%d' %(SecNum, Index)\r
9e47e6f9 138 OutputFile = os.path.join(OutputPath, ModuleName + SUP_MODULE_SEC + Num + SectionSuffix.get(SectionType))\r
30fdf114
LG
139 f = open(File, 'r')\r
140 VerString = f.read()\r
141 f.close()\r
30fdf114 142 BuildNum = VerString\r
4231a819 143 if BuildNum is not None and BuildNum != '':\r
30fdf114
LG
144 BuildNumTuple = ('-j', BuildNum)\r
145 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_VERSION',\r
37de70b7
YZ
146 #Ui=VerString,\r
147 Ver=BuildNum,\r
148 IsMakefile=IsMakefile)\r
30fdf114
LG
149 OutputFileList.append(OutputFile)\r
150\r
151 else:\r
30fdf114 152 BuildNum = StringData\r
4231a819 153 if BuildNum is not None and BuildNum != '':\r
30fdf114
LG
154 BuildNumTuple = ('-j', BuildNum)\r
155 else:\r
156 BuildNumTuple = tuple()\r
157 BuildNumString = ' ' + ' '.join(BuildNumTuple)\r
158\r
f7496d71 159 #if VerString == '' and\r
30fdf114
LG
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
9e47e6f9 167 OutputFile = os.path.join( OutputPath, ModuleName + SUP_MODULE_SEC + str(Num) + SectionSuffix.get(SectionType))\r
30fdf114 168 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_VERSION',\r
37de70b7
YZ
169 #Ui=VerString,\r
170 Ver=BuildNum,\r
171 IsMakefile=IsMakefile)\r
30fdf114
LG
172 OutputFileList.append(OutputFile)\r
173\r
174 #\r
91fa33ee 175 # If Section Type is BINARY_FILE_TYPE_UI\r
30fdf114 176 #\r
91fa33ee 177 elif SectionType == BINARY_FILE_TYPE_UI:\r
30fdf114
LG
178\r
179 InfOverrideUiString = False\r
4231a819 180 if FfsInf.Ui is not None:\r
30fdf114
LG
181 StringData = FfsInf.Ui\r
182 InfOverrideUiString = True\r
183\r
184 if InfOverrideUiString:\r
185 Num = SecNum\r
a146c532
FY
186 if IsMakefile and StringData == ModuleNameStr:\r
187 StringData = "$(MODULE_NAME)"\r
9e47e6f9 188 OutputFile = os.path.join( OutputPath, ModuleName + SUP_MODULE_SEC + str(Num) + SectionSuffix.get(SectionType))\r
30fdf114 189 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_USER_INTERFACE',\r
37de70b7 190 Ui=StringData, IsMakefile=IsMakefile)\r
30fdf114
LG
191 OutputFileList.append(OutputFile)\r
192\r
193 elif FileList != []:\r
194 for File in FileList:\r
195 Index = Index + 1\r
ccaa7754 196 Num = '%s.%d' %(SecNum, Index)\r
9e47e6f9 197 OutputFile = os.path.join(OutputPath, ModuleName + SUP_MODULE_SEC + Num + SectionSuffix.get(SectionType))\r
30fdf114
LG
198 f = open(File, 'r')\r
199 UiString = f.read()\r
200 f.close()\r
a146c532
FY
201 if IsMakefile and UiString == ModuleNameStr:\r
202 UiString = "$(MODULE_NAME)"\r
30fdf114 203 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_USER_INTERFACE',\r
37de70b7 204 Ui=UiString, IsMakefile=IsMakefile)\r
30fdf114
LG
205 OutputFileList.append(OutputFile)\r
206 else:\r
4231a819 207 if StringData is not None and len(StringData) > 0:\r
30fdf114
LG
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
a146c532
FY
219 if IsMakefile and StringData == ModuleNameStr:\r
220 StringData = "$(MODULE_NAME)"\r
9e47e6f9 221 OutputFile = os.path.join( OutputPath, ModuleName + SUP_MODULE_SEC + str(Num) + SectionSuffix.get(SectionType))\r
30fdf114 222 GenFdsGlobalVariable.GenerateSection(OutputFile, [], 'EFI_SECTION_USER_INTERFACE',\r
37de70b7 223 Ui=StringData, IsMakefile=IsMakefile)\r
30fdf114
LG
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
97fa0ee9
YL
231 GenFdsGlobalVariable.VerboseLogger("Optional Section don't exist!")\r
232 return [], None\r
30fdf114 233 else:\r
97fa0ee9 234 EdkLogger.error("GenFds", GENFDS_ERROR, "Output file for %s section could not be found for %s" % (SectionType, InfFileName))\r
30fdf114
LG
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
ccaa7754 241 Num = '%s.%d' %(SecNum, Index)\r
9e47e6f9 242 OutputFile = os.path.join( OutputPath, ModuleName + SUP_MODULE_SEC + Num + SectionSuffix.get(SectionType))\r
30fdf114 243 File = GenFdsGlobalVariable.MacroExtend(File, Dict)\r
f7496d71 244\r
52302d4d 245 #Get PE Section alignment when align is set to AUTO\r
91fa33ee 246 if self.Alignment == 'Auto' and (SectionType == BINARY_FILE_TYPE_PE32 or SectionType == BINARY_FILE_TYPE_TE):\r
52302d4d
LG
247 ImageObj = PeImageClass (File)\r
248 if ImageObj.SectionAlignment < 0x400:\r
547a6507 249 Align = str (ImageObj.SectionAlignment)\r
e921f58d 250 elif ImageObj.SectionAlignment < 0x100000:\r
1ccc4d89 251 Align = str (ImageObj.SectionAlignment / 0x400) + 'K'\r
e921f58d 252 else:\r
1ccc4d89 253 Align = str (ImageObj.SectionAlignment / 0x100000) + 'M'\r
52302d4d 254\r
30fdf114
LG
255 if File[(len(File)-4):] == '.efi':\r
256 MapFile = File.replace('.efi', '.map')\r
37de70b7
YZ
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
30fdf114
LG
268\r
269 if not NoStrip:\r
270 FileBeforeStrip = os.path.join(OutputPath, ModuleName + '.efi')\r
37de70b7
YZ
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
30fdf114
LG
280 StrippedFile = os.path.join(OutputPath, ModuleName + '.stripped')\r
281 GenFdsGlobalVariable.GenerateFirmwareImage(\r
37de70b7
YZ
282 StrippedFile,\r
283 [File],\r
284 Strip=True,\r
285 IsMakefile = IsMakefile\r
286 )\r
30fdf114 287 File = StrippedFile\r
f7496d71 288\r
30fdf114
LG
289 """For TE Section call GenFw to generate TE image"""\r
290\r
91fa33ee 291 if SectionType == BINARY_FILE_TYPE_TE:\r
30fdf114
LG
292 TeFile = os.path.join( OutputPath, ModuleName + 'Te.raw')\r
293 GenFdsGlobalVariable.GenerateFirmwareImage(\r
37de70b7
YZ
294 TeFile,\r
295 [File],\r
296 Type='te',\r
297 IsMakefile = IsMakefile\r
298 )\r
30fdf114
LG
299 File = TeFile\r
300\r
301 """Call GenSection"""\r
302 GenFdsGlobalVariable.GenerateSection(OutputFile,\r
37de70b7
YZ
303 [File],\r
304 Section.Section.SectionType.get (SectionType),\r
305 IsMakefile=IsMakefile\r
306 )\r
30fdf114
LG
307 OutputFileList.append(OutputFile)\r
308\r
547a6507 309 return OutputFileList, Align\r