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