Sync EDKII BaseTools to BaseTools project r1903.
[efi/edk2/.git] / edk2 / BaseTools / Source / Python / GenFds / GuidSection.py
1 ## @file\r
2 # process GUIDed section generation\r
3 #\r
4 #  Copyright (c) 2007 - 2010, Intel Corporation\r
5 #\r
6 #  All rights reserved. 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
18 import Section\r
19 import subprocess\r
20 from Ffs import Ffs\r
21 import os\r
22 from GenFdsGlobalVariable import GenFdsGlobalVariable\r
23 from CommonDataClass.FdfClass import GuidSectionClassObject\r
24 from Common import ToolDefClassObject\r
25 import sys\r
26 from Common import EdkLogger\r
27 from Common.BuildToolError import *\r
28 from FvImageSection import FvImageSection\r
29 \r
30 ## generate GUIDed section\r
31 #\r
32 #\r
33 class GuidSection(GuidSectionClassObject) :\r
34 \r
35     ## The constructor\r
36     #\r
37     #   @param  self        The object pointer\r
38     #\r
39     def __init__(self):\r
40         GuidSectionClassObject.__init__(self)\r
41 \r
42     ## GenSection() method\r
43     #\r
44     #   Generate GUIDed section\r
45     #\r
46     #   @param  self        The object pointer\r
47     #   @param  OutputPath  Where to place output file\r
48     #   @param  ModuleName  Which module this section belongs to\r
49     #   @param  SecNum      Index of section\r
50     #   @param  KeyStringList  Filter for inputs of section generation\r
51     #   @param  FfsInf      FfsInfStatement object that contains this section data\r
52     #   @param  Dict        dictionary contains macro and its value\r
53     #   @retval tuple       (Generated file name, section alignment)\r
54     #\r
55     def GenSection(self, OutputPath, ModuleName, SecNum, KeyStringList, FfsInf = None, Dict = {}):\r
56         #\r
57         # Generate all section\r
58         #\r
59         self.KeyStringList = KeyStringList\r
60         self.CurrentArchList = GenFdsGlobalVariable.ArchList\r
61         if FfsInf != None:\r
62             self.Alignment = FfsInf.__ExtendMacro__(self.Alignment)\r
63             self.NameGuid = FfsInf.__ExtendMacro__(self.NameGuid)\r
64             self.SectionType = FfsInf.__ExtendMacro__(self.SectionType)\r
65             self.CurrentArchList = [FfsInf.CurrentArch]\r
66 \r
67         SectFile  = tuple()\r
68         SectAlign = []\r
69         Index = 0\r
70         MaxAlign = None\r
71         if self.FvAddr != []:\r
72             FvAddrIsSet = True\r
73         else:\r
74             FvAddrIsSet = False\r
75         \r
76         if self.ProcessRequired in ("TRUE", "1"):\r
77             if self.FvAddr != []:\r
78                 #no use FvAddr when the image is processed.\r
79                 self.FvAddr = []\r
80             if self.FvParentAddr != None:\r
81                 #no use Parent Addr when the image is processed.\r
82                 self.FvParentAddr = None\r
83 \r
84         for Sect in self.SectionList:\r
85             Index = Index + 1\r
86             SecIndex = '%s.%d' %(SecNum,Index)\r
87             # set base address for inside FvImage\r
88             if isinstance(Sect, FvImageSection):\r
89                 if self.FvAddr != []:\r
90                     Sect.FvAddr = self.FvAddr.pop(0)\r
91                 self.IncludeFvSection = True\r
92             elif isinstance(Sect, GuidSection):\r
93                 Sect.FvAddr = self.FvAddr\r
94                 Sect.FvParentAddr = self.FvParentAddr\r
95             ReturnSectList, align = Sect.GenSection(OutputPath, ModuleName, SecIndex, KeyStringList,FfsInf, Dict)\r
96             if isinstance(Sect, GuidSection):\r
97                 if Sect.IncludeFvSection:\r
98                     self.IncludeFvSection = Sect.IncludeFvSection\r
99 \r
100             if align != None:\r
101                 if MaxAlign == None:\r
102                     MaxAlign = align\r
103                 if GenFdsGlobalVariable.GetAlignment (align) > GenFdsGlobalVariable.GetAlignment (MaxAlign):\r
104                     MaxAlign = align\r
105             if ReturnSectList != []:\r
106                 if align == None:\r
107                     align = "1"\r
108                 for file in ReturnSectList:\r
109                     SectFile += (file,)\r
110                     SectAlign.append(align)\r
111 \r
112         if MaxAlign != None:\r
113             if self.Alignment == None:\r
114                 self.Alignment = MaxAlign\r
115             else:\r
116                 if GenFdsGlobalVariable.GetAlignment (MaxAlign) > GenFdsGlobalVariable.GetAlignment (self.Alignment):\r
117                     self.Alignment = MaxAlign\r
118 \r
119         OutputFile = OutputPath + \\r
120                      os.sep     + \\r
121                      ModuleName + \\r
122                      'SEC'      + \\r
123                      SecNum     + \\r
124                      Ffs.SectionSuffix['GUIDED']\r
125         OutputFile = os.path.normpath(OutputFile)\r
126 \r
127         ExternalTool = None\r
128         ExternalOption = None\r
129         if self.NameGuid != None:\r
130             ExternalTool, ExternalOption = self.__FindExtendTool__()\r
131 \r
132         #\r
133         # If not have GUID , call default\r
134         # GENCRC32 section\r
135         #\r
136         if self.NameGuid == None :\r
137             GenFdsGlobalVariable.VerboseLogger( "Use GenSection function Generate CRC32 Section")\r
138             GenFdsGlobalVariable.GenerateSection(OutputFile, SectFile, Section.Section.SectionType[self.SectionType], InputAlign=SectAlign)\r
139             OutputFileList = []\r
140             OutputFileList.append(OutputFile)\r
141             return OutputFileList, self.Alignment\r
142         #or GUID not in External Tool List\r
143         elif ExternalTool == None:\r
144             EdkLogger.error("GenFds", GENFDS_ERROR, "No tool found with GUID %s" % self.NameGuid)\r
145         else:\r
146             DummyFile = OutputFile+".dummy"\r
147             #\r
148             # Call GenSection with DUMMY section type.\r
149             #\r
150             GenFdsGlobalVariable.GenerateSection(DummyFile, SectFile, InputAlign=SectAlign)\r
151             #\r
152             # Use external tool process the Output\r
153             #\r
154             TempFile = OutputPath + \\r
155                        os.sep     + \\r
156                        ModuleName + \\r
157                        'SEC'      + \\r
158                        SecNum     + \\r
159                        '.tmp'\r
160             TempFile = os.path.normpath(TempFile)\r
161 \r
162             FirstCall = False\r
163             CmdOption = '-e'\r
164             if ExternalOption != None:\r
165                 CmdOption = CmdOption + ' ' + ExternalOption\r
166             if self.ProcessRequired not in ("TRUE", "1") and self.IncludeFvSection and not FvAddrIsSet and self.FvParentAddr != None:\r
167                 #FirstCall is only set for the encapsulated flash FV image without process required attribute.\r
168                 FirstCall = True\r
169             #\r
170             # Call external tool\r
171             #\r
172             ReturnValue = [1]\r
173             if FirstCall:\r
174                 #first try to call the guided tool with -z option and CmdOption for the no process required guided tool.\r
175                 GenFdsGlobalVariable.GuidTool(TempFile, [DummyFile], ExternalTool, '-z' + ' ' + CmdOption, ReturnValue)\r
176 \r
177             #\r
178             # when no call or first call failed, ReturnValue are not 1.\r
179             # Call the guided tool with CmdOption\r
180             #\r
181             if ReturnValue[0] != 0:\r
182                 FirstCall = False\r
183                 ReturnValue[0] = 0\r
184                 GenFdsGlobalVariable.GuidTool(TempFile, [DummyFile], ExternalTool, CmdOption)\r
185 \r
186             FileHandleIn = open(DummyFile,'rb')\r
187             FileHandleIn.seek(0,2)\r
188             InputFileSize = FileHandleIn.tell()\r
189             \r
190             FileHandleOut = open(TempFile,'rb')\r
191             FileHandleOut.seek(0,2)\r
192             TempFileSize = FileHandleOut.tell()\r
193 \r
194             Attribute = []\r
195             HeaderLength = None\r
196             if TempFileSize > InputFileSize and TempFileSize % 4 == 0:\r
197                 FileHandleIn.seek(0)\r
198                 BufferIn  = FileHandleIn.read()\r
199                 FileHandleOut.seek(0)\r
200                 BufferOut = FileHandleOut.read()\r
201                 if BufferIn == BufferOut[TempFileSize - InputFileSize:]:\r
202                     HeaderLength = str(TempFileSize - InputFileSize)\r
203             #auto sec guided attribute with process required\r
204             if HeaderLength == None:\r
205                 Attribute.append('PROCESSING_REQUIRED')\r
206 \r
207             FileHandleIn.close()\r
208             FileHandleOut.close()\r
209             \r
210             if FirstCall and 'PROCESSING_REQUIRED' in Attribute:\r
211                 # Guided data by -z option on first call is the process required data. Call the guided tool with the real option.\r
212                 GenFdsGlobalVariable.GuidTool(TempFile, [DummyFile], ExternalTool, CmdOption)\r
213             \r
214             #\r
215             # Call Gensection Add Section Header\r
216             #\r
217             if self.ProcessRequired in ("TRUE", "1"):\r
218                 if 'PROCESSING_REQUIRED' not in Attribute:\r
219                     Attribute.append('PROCESSING_REQUIRED')\r
220                 HeaderLength = None\r
221             if self.AuthStatusValid in ("TRUE", "1"):\r
222                 Attribute.append('AUTH_STATUS_VALID')\r
223             GenFdsGlobalVariable.GenerateSection(OutputFile, [TempFile], Section.Section.SectionType['GUIDED'],\r
224                                                  Guid=self.NameGuid, GuidAttr=Attribute, GuidHdrLen=HeaderLength)\r
225             OutputFileList = []\r
226             OutputFileList.append(OutputFile)\r
227             if 'PROCESSING_REQUIRED' in Attribute:\r
228                 # reset guided section alignment to none for the processed required guided data\r
229                 self.Alignment = None\r
230                 self.IncludeFvSection = False\r
231                 self.ProcessRequired = "TRUE"\r
232             return OutputFileList, self.Alignment\r
233 \r
234     ## __FindExtendTool()\r
235     #\r
236     #    Find location of tools to process section data\r
237     #\r
238     #   @param  self        The object pointer\r
239     #\r
240     def __FindExtendTool__(self):\r
241         # if user not specify filter, try to deduce it from global data.\r
242         if self.KeyStringList == None or self.KeyStringList == []:\r
243             Target = GenFdsGlobalVariable.TargetName\r
244             ToolChain = GenFdsGlobalVariable.ToolChainTag\r
245             ToolDb = ToolDefClassObject.ToolDefDict(GenFdsGlobalVariable.WorkSpaceDir).ToolsDefTxtDatabase\r
246             if ToolChain not in ToolDb['TOOL_CHAIN_TAG']:\r
247                 EdkLogger.error("GenFds", GENFDS_ERROR, "Can not find external tool because tool tag %s is not defined in tools_def.txt!" % ToolChain)\r
248             self.KeyStringList = [Target+'_'+ToolChain+'_'+self.CurrentArchList[0]]\r
249             for Arch in self.CurrentArchList:\r
250                 if Target+'_'+ToolChain+'_'+Arch not in self.KeyStringList:\r
251                     self.KeyStringList.append(Target+'_'+ToolChain+'_'+Arch)\r
252                     \r
253         ToolDefinition = ToolDefClassObject.ToolDefDict(GenFdsGlobalVariable.WorkSpaceDir).ToolsDefTxtDictionary\r
254         ToolPathTmp = None\r
255         for ToolDef in ToolDefinition.items():\r
256             if self.NameGuid == ToolDef[1]:\r
257                 KeyList = ToolDef[0].split('_')\r
258                 Key = KeyList[0] + \\r
259                       '_'        + \\r
260                       KeyList[1] + \\r
261                       '_'        + \\r
262                       KeyList[2]\r
263                 if Key in self.KeyStringList and KeyList[4] == 'GUID':\r
264 \r
265                     ToolPath = ToolDefinition.get( Key        + \\r
266                                                    '_'        + \\r
267                                                    KeyList[3] + \\r
268                                                    '_'        + \\r
269                                                    'PATH')\r
270 \r
271                     ToolOption = ToolDefinition.get( Key        + \\r
272                                                     '_'        + \\r
273                                                     KeyList[3] + \\r
274                                                     '_'        + \\r
275                                                     'FLAGS')\r
276                     if ToolPathTmp == None:\r
277                         ToolPathTmp = ToolPath\r
278                     else:\r
279                         if ToolPathTmp != ToolPath:\r
280                             EdkLogger.error("GenFds", GENFDS_ERROR, "Don't know which tool to use, %s or %s ?" % (ToolPathTmp, ToolPath))\r
281                             \r
282                     \r
283         return ToolPathTmp, ToolOption\r
284 \r
285 \r
286 \r