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