]>
Commit | Line | Data |
---|---|---|
30fdf114 LG |
1 | ## @file\r |
2 | # process GUIDed section generation\r | |
3 | #\r | |
213ae077 | 4 | # Copyright (c) 2007 - 2016, 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 | |
18 | import Section\r | |
19 | import subprocess\r | |
20 | from Ffs import Ffs\r | |
1be2ed90 | 21 | import Common.LongFilePathOs as os\r |
30fdf114 LG |
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 | |
52302d4d | 28 | from FvImageSection import FvImageSection\r |
1be2ed90 | 29 | from Common.LongFilePathSupport import OpenLongFilePath as open\r |
91ae2988 | 30 | from GenFds import FindExtendTool\r |
30fdf114 LG |
31 | \r |
32 | ## generate GUIDed section\r | |
33 | #\r | |
34 | #\r | |
35 | class GuidSection(GuidSectionClassObject) :\r | |
36 | \r | |
37 | ## The constructor\r | |
38 | #\r | |
39 | # @param self The object pointer\r | |
40 | #\r | |
41 | def __init__(self):\r | |
42 | GuidSectionClassObject.__init__(self)\r | |
43 | \r | |
44 | ## GenSection() method\r | |
45 | #\r | |
46 | # Generate GUIDed 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, section alignment)\r | |
56 | #\r | |
47fea6af | 57 | def GenSection(self, OutputPath, ModuleName, SecNum, KeyStringList, FfsInf=None, Dict={}):\r |
30fdf114 LG |
58 | #\r |
59 | # Generate all section\r | |
60 | #\r | |
61 | self.KeyStringList = KeyStringList\r | |
62 | self.CurrentArchList = GenFdsGlobalVariable.ArchList\r | |
63 | if FfsInf != None:\r | |
64 | self.Alignment = FfsInf.__ExtendMacro__(self.Alignment)\r | |
65 | self.NameGuid = FfsInf.__ExtendMacro__(self.NameGuid)\r | |
66 | self.SectionType = FfsInf.__ExtendMacro__(self.SectionType)\r | |
67 | self.CurrentArchList = [FfsInf.CurrentArch]\r | |
68 | \r | |
47fea6af | 69 | SectFile = tuple()\r |
52302d4d | 70 | SectAlign = []\r |
30fdf114 | 71 | Index = 0\r |
52302d4d LG |
72 | MaxAlign = None\r |
73 | if self.FvAddr != []:\r | |
74 | FvAddrIsSet = True\r | |
75 | else:\r | |
76 | FvAddrIsSet = False\r | |
77 | \r | |
78 | if self.ProcessRequired in ("TRUE", "1"):\r | |
79 | if self.FvAddr != []:\r | |
80 | #no use FvAddr when the image is processed.\r | |
81 | self.FvAddr = []\r | |
82 | if self.FvParentAddr != None:\r | |
83 | #no use Parent Addr when the image is processed.\r | |
84 | self.FvParentAddr = None\r | |
85 | \r | |
30fdf114 LG |
86 | for Sect in self.SectionList:\r |
87 | Index = Index + 1\r | |
47fea6af | 88 | SecIndex = '%s.%d' % (SecNum, Index)\r |
52302d4d LG |
89 | # set base address for inside FvImage\r |
90 | if isinstance(Sect, FvImageSection):\r | |
91 | if self.FvAddr != []:\r | |
92 | Sect.FvAddr = self.FvAddr.pop(0)\r | |
93 | self.IncludeFvSection = True\r | |
94 | elif isinstance(Sect, GuidSection):\r | |
95 | Sect.FvAddr = self.FvAddr\r | |
96 | Sect.FvParentAddr = self.FvParentAddr\r | |
47fea6af | 97 | ReturnSectList, align = Sect.GenSection(OutputPath, ModuleName, SecIndex, KeyStringList, FfsInf, Dict)\r |
52302d4d LG |
98 | if isinstance(Sect, GuidSection):\r |
99 | if Sect.IncludeFvSection:\r | |
100 | self.IncludeFvSection = Sect.IncludeFvSection\r | |
101 | \r | |
102 | if align != None:\r | |
103 | if MaxAlign == None:\r | |
104 | MaxAlign = align\r | |
105 | if GenFdsGlobalVariable.GetAlignment (align) > GenFdsGlobalVariable.GetAlignment (MaxAlign):\r | |
106 | MaxAlign = align\r | |
30fdf114 | 107 | if ReturnSectList != []:\r |
52302d4d LG |
108 | if align == None:\r |
109 | align = "1"\r | |
30fdf114 LG |
110 | for file in ReturnSectList:\r |
111 | SectFile += (file,)\r | |
52302d4d | 112 | SectAlign.append(align)\r |
30fdf114 | 113 | \r |
52302d4d LG |
114 | if MaxAlign != None:\r |
115 | if self.Alignment == None:\r | |
116 | self.Alignment = MaxAlign\r | |
117 | else:\r | |
118 | if GenFdsGlobalVariable.GetAlignment (MaxAlign) > GenFdsGlobalVariable.GetAlignment (self.Alignment):\r | |
119 | self.Alignment = MaxAlign\r | |
30fdf114 LG |
120 | \r |
121 | OutputFile = OutputPath + \\r | |
47fea6af | 122 | os.sep + \\r |
30fdf114 | 123 | ModuleName + \\r |
47fea6af YZ |
124 | 'SEC' + \\r |
125 | SecNum + \\r | |
30fdf114 LG |
126 | Ffs.SectionSuffix['GUIDED']\r |
127 | OutputFile = os.path.normpath(OutputFile)\r | |
128 | \r | |
129 | ExternalTool = None\r | |
52302d4d | 130 | ExternalOption = None\r |
30fdf114 | 131 | if self.NameGuid != None:\r |
91ae2988 | 132 | ExternalTool, ExternalOption = FindExtendTool(self.KeyStringList, self.CurrentArchList, self.NameGuid)\r |
52302d4d | 133 | \r |
30fdf114 LG |
134 | #\r |
135 | # If not have GUID , call default\r | |
136 | # GENCRC32 section\r | |
137 | #\r | |
138 | if self.NameGuid == None :\r | |
47fea6af | 139 | GenFdsGlobalVariable.VerboseLogger("Use GenSection function Generate CRC32 Section")\r |
52302d4d | 140 | GenFdsGlobalVariable.GenerateSection(OutputFile, SectFile, Section.Section.SectionType[self.SectionType], InputAlign=SectAlign)\r |
30fdf114 LG |
141 | OutputFileList = []\r |
142 | OutputFileList.append(OutputFile)\r | |
143 | return OutputFileList, self.Alignment\r | |
144 | #or GUID not in External Tool List\r | |
145 | elif ExternalTool == None:\r | |
146 | EdkLogger.error("GenFds", GENFDS_ERROR, "No tool found with GUID %s" % self.NameGuid)\r | |
147 | else:\r | |
47fea6af | 148 | DummyFile = OutputFile + ".dummy"\r |
30fdf114 LG |
149 | #\r |
150 | # Call GenSection with DUMMY section type.\r | |
151 | #\r | |
52302d4d | 152 | GenFdsGlobalVariable.GenerateSection(DummyFile, SectFile, InputAlign=SectAlign)\r |
30fdf114 LG |
153 | #\r |
154 | # Use external tool process the Output\r | |
155 | #\r | |
30fdf114 | 156 | TempFile = OutputPath + \\r |
47fea6af | 157 | os.sep + \\r |
30fdf114 | 158 | ModuleName + \\r |
47fea6af YZ |
159 | 'SEC' + \\r |
160 | SecNum + \\r | |
30fdf114 LG |
161 | '.tmp'\r |
162 | TempFile = os.path.normpath(TempFile)\r | |
edafa0bb YL |
163 | #\r |
164 | # Remove temp file if its time stamp is older than dummy file\r | |
165 | # Just in case the external tool fails at this time but succeeded before\r | |
166 | # Error should be reported if the external tool does not generate a new output based on new input\r | |
167 | #\r | |
168 | if os.path.exists(TempFile) and os.path.exists(DummyFile) and os.path.getmtime(TempFile) < os.path.getmtime(DummyFile):\r | |
169 | os.remove(TempFile)\r | |
30fdf114 | 170 | \r |
52302d4d LG |
171 | FirstCall = False\r |
172 | CmdOption = '-e'\r | |
173 | if ExternalOption != None:\r | |
174 | CmdOption = CmdOption + ' ' + ExternalOption\r | |
175 | if self.ProcessRequired not in ("TRUE", "1") and self.IncludeFvSection and not FvAddrIsSet and self.FvParentAddr != None:\r | |
176 | #FirstCall is only set for the encapsulated flash FV image without process required attribute.\r | |
177 | FirstCall = True\r | |
30fdf114 LG |
178 | #\r |
179 | # Call external tool\r | |
180 | #\r | |
52302d4d LG |
181 | ReturnValue = [1]\r |
182 | if FirstCall:\r | |
183 | #first try to call the guided tool with -z option and CmdOption for the no process required guided tool.\r | |
184 | GenFdsGlobalVariable.GuidTool(TempFile, [DummyFile], ExternalTool, '-z' + ' ' + CmdOption, ReturnValue)\r | |
30fdf114 LG |
185 | \r |
186 | #\r | |
52302d4d LG |
187 | # when no call or first call failed, ReturnValue are not 1.\r |
188 | # Call the guided tool with CmdOption\r | |
30fdf114 | 189 | #\r |
52302d4d LG |
190 | if ReturnValue[0] != 0:\r |
191 | FirstCall = False\r | |
192 | ReturnValue[0] = 0\r | |
193 | GenFdsGlobalVariable.GuidTool(TempFile, [DummyFile], ExternalTool, CmdOption)\r | |
edafa0bb YL |
194 | #\r |
195 | # There is external tool which does not follow standard rule which return nonzero if tool fails\r | |
196 | # The output file has to be checked\r | |
197 | #\r | |
198 | if not os.path.exists(TempFile):\r | |
199 | EdkLogger.error("GenFds", COMMAND_FAILURE, 'Fail to call %s, no output file was generated' % ExternalTool)\r | |
52302d4d | 200 | \r |
47fea6af YZ |
201 | FileHandleIn = open(DummyFile, 'rb')\r |
202 | FileHandleIn.seek(0, 2)\r | |
52302d4d | 203 | InputFileSize = FileHandleIn.tell()\r |
47fea6af YZ |
204 | \r |
205 | FileHandleOut = open(TempFile, 'rb')\r | |
206 | FileHandleOut.seek(0, 2)\r | |
52302d4d LG |
207 | TempFileSize = FileHandleOut.tell()\r |
208 | \r | |
209 | Attribute = []\r | |
210 | HeaderLength = None\r | |
25918452 LG |
211 | if self.ExtraHeaderSize != -1:\r |
212 | HeaderLength = str(self.ExtraHeaderSize)\r | |
213 | \r | |
214 | if self.ProcessRequired == "NONE" and HeaderLength == None:\r | |
97fa0ee9 | 215 | if TempFileSize > InputFileSize:\r |
14c48571 | 216 | FileHandleIn.seek(0)\r |
47fea6af | 217 | BufferIn = FileHandleIn.read()\r |
14c48571 | 218 | FileHandleOut.seek(0)\r |
219 | BufferOut = FileHandleOut.read()\r | |
220 | if BufferIn == BufferOut[TempFileSize - InputFileSize:]:\r | |
221 | HeaderLength = str(TempFileSize - InputFileSize)\r | |
222 | #auto sec guided attribute with process required\r | |
223 | if HeaderLength == None:\r | |
224 | Attribute.append('PROCESSING_REQUIRED')\r | |
52302d4d LG |
225 | \r |
226 | FileHandleIn.close()\r | |
227 | FileHandleOut.close()\r | |
47fea6af | 228 | \r |
52302d4d LG |
229 | if FirstCall and 'PROCESSING_REQUIRED' in Attribute:\r |
230 | # Guided data by -z option on first call is the process required data. Call the guided tool with the real option.\r | |
231 | GenFdsGlobalVariable.GuidTool(TempFile, [DummyFile], ExternalTool, CmdOption)\r | |
47fea6af | 232 | \r |
52302d4d LG |
233 | #\r |
234 | # Call Gensection Add Section Header\r | |
235 | #\r | |
236 | if self.ProcessRequired in ("TRUE", "1"):\r | |
237 | if 'PROCESSING_REQUIRED' not in Attribute:\r | |
238 | Attribute.append('PROCESSING_REQUIRED')\r | |
47fea6af | 239 | \r |
52302d4d LG |
240 | if self.AuthStatusValid in ("TRUE", "1"):\r |
241 | Attribute.append('AUTH_STATUS_VALID')\r | |
30fdf114 | 242 | GenFdsGlobalVariable.GenerateSection(OutputFile, [TempFile], Section.Section.SectionType['GUIDED'],\r |
52302d4d | 243 | Guid=self.NameGuid, GuidAttr=Attribute, GuidHdrLen=HeaderLength)\r |
30fdf114 LG |
244 | OutputFileList = []\r |
245 | OutputFileList.append(OutputFile)\r | |
52302d4d LG |
246 | if 'PROCESSING_REQUIRED' in Attribute:\r |
247 | # reset guided section alignment to none for the processed required guided data\r | |
248 | self.Alignment = None\r | |
249 | self.IncludeFvSection = False\r | |
250 | self.ProcessRequired = "TRUE"\r | |
30fdf114 LG |
251 | return OutputFileList, self.Alignment\r |
252 | \r | |
30fdf114 LG |
253 | \r |
254 | \r |