2 # This file is used to define common parsing related functions used in parsing
3 # Inf/Dsc/Makefile process
5 # Copyright (c) 2008 - 2010, Intel Corporation
6 # All rights reserved. This program and the accompanying materials
7 # are licensed and made available under the terms and conditions of the BSD License
8 # which accompanies this distribution. The full text of the license may be found at
9 # http://opensource.org/licenses/bsd-license.php
11 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 import Common
.EdkLogger
as EdkLogger
20 from Common
.DataType
import *
21 from CommonDataClass
.DataClass
import *
22 from Common
.String
import CleanString
, GetSplitValueList
, ReplaceMacro
24 from Common
.Misc
import sdict
26 ## PreProcess() method
30 # 1. Remove all comments
31 # 2. Merge multiple lines code to one line
33 # @param Filename: Name of the file to be parsed
34 # @param MergeMultipleLines: Switch for if merge multiple lines
35 # @param LineNo: Default line no
37 # @return Lines: The file contents after remvoing comments
39 def PreProcess(Filename
, MergeMultipleLines
= True, LineNo
= -1):
41 Filename
= os
.path
.normpath(Filename
)
42 if not os
.path
.isfile(Filename
):
43 EdkLogger
.error("Eot", EdkLogger
.FILE_NOT_FOUND
, ExtraData
=Filename
)
45 IsFindBlockComment
= False
46 IsFindBlockCode
= False
48 ReservedLineLength
= 0
49 for Line
in open(Filename
, 'r'):
51 # Remove comment block
52 if Line
.find(TAB_COMMENT_R8_START
) > -1:
53 ReservedLine
= GetSplitValueList(Line
, TAB_COMMENT_R8_START
, 1)[0]
54 IsFindBlockComment
= True
55 if Line
.find(TAB_COMMENT_R8_END
) > -1:
56 Line
= ReservedLine
+ GetSplitValueList(Line
, TAB_COMMENT_R8_END
, 1)[1]
58 IsFindBlockComment
= False
59 if IsFindBlockComment
:
63 # Remove comments at tail and remove spaces again
64 Line
= CleanString(Line
)
69 if MergeMultipleLines
:
70 # Add multiple lines to one line
71 if IsFindBlockCode
and Line
[-1] != TAB_SLASH
:
72 ReservedLine
= (ReservedLine
+ TAB_SPACE_SPLIT
+ Line
).strip()
73 Lines
.append(ReservedLine
)
74 for Index
in (0, ReservedLineLength
):
77 ReservedLineLength
= 0
78 IsFindBlockCode
= False
80 if Line
[-1] == TAB_SLASH
:
81 ReservedLine
= ReservedLine
+ TAB_SPACE_SPLIT
+ Line
[0:-1].strip()
82 ReservedLineLength
= ReservedLineLength
+ 1
83 IsFindBlockCode
= True
90 ## AddToGlobalMacro() method
92 # Add a macro to EotGlobalData.gMACRO
94 # @param Name: Name of the macro
95 # @param Value: Value of the macro
97 def AddToGlobalMacro(Name
, Value
):
98 Value
= ReplaceMacro(Value
, EotGlobalData
.gMACRO
, True)
99 EotGlobalData
.gMACRO
[Name
] = Value
101 ## AddToSelfMacro() method
103 # Parse a line of macro definition and add it to a macro set
105 # @param SelfMacro: The self macro set
106 # @param Line: The line of a macro definition
108 # @return Name: Name of macro
109 # @return Value: Value of macro
111 def AddToSelfMacro(SelfMacro
, Line
):
113 List
= GetSplitValueList(Line
, TAB_EQUAL_SPLIT
, 1)
117 Value
= ReplaceMacro(Value
, EotGlobalData
.gMACRO
, True)
118 Value
= ReplaceMacro(Value
, SelfMacro
, True)
119 SelfMacro
[Name
] = Value
123 ## GetIncludeListOfFile() method
125 # Get the include path list for a source file
127 # 1. Find the source file belongs to which INF file
128 # 2. Find the inf's package
129 # 3. Return the include path list of the package
131 # @param WorkSpace: WORKSPACE path
132 # @param Filepath: File path
133 # @param Db: Eot database
135 # @return IncludeList: A list of include directories
137 def GetIncludeListOfFile(WorkSpace
, Filepath
, Db
):
139 Filepath
= os
.path
.normpath(Filepath
)
141 select Value1 from Inf where Model = %s and BelongsToFile in(
142 select distinct B.BelongsToFile from File as A left join Inf as B
143 where A.ID = B.BelongsToFile and B.Model = %s and (A.Path || '%s' || B.Value1) = '%s')""" \
144 % (MODEL_META_DATA_PACKAGE
, MODEL_EFI_SOURCE_FILE
, '\\', Filepath
)
145 RecordSet
= Db
.TblFile
.Exec(SqlCommand
)
146 for Record
in RecordSet
:
147 DecFullPath
= os
.path
.normpath(os
.path
.join(WorkSpace
, Record
[0]))
148 (DecPath
, DecName
) = os
.path
.split(DecFullPath
)
149 SqlCommand
= """select Value1 from Dec where BelongsToFile =
150 (select ID from File where FullPath = '%s') and Model = %s""" \
151 % (DecFullPath
, MODEL_EFI_INCLUDE
)
152 NewRecordSet
= Db
.TblDec
.Exec(SqlCommand
)
153 for NewRecord
in NewRecordSet
:
154 IncludePath
= os
.path
.normpath(os
.path
.join(DecPath
, NewRecord
[0]))
155 if IncludePath
not in IncludeList
:
156 IncludeList
.append(IncludePath
)
160 ## GetTableList() method
162 # Search table file and find all small tables
164 # @param FileModelList: Model code for the file list
165 # @param Table: Table to insert records
166 # @param Db: Eot database
168 # @return TableList: A list of tables
170 def GetTableList(FileModelList
, Table
, Db
):
172 SqlCommand
= """select ID, FullPath from File where Model in %s""" % str(FileModelList
)
173 RecordSet
= Db
.TblFile
.Exec(SqlCommand
)
174 for Record
in RecordSet
:
175 TableName
= Table
+ str(Record
[0])
176 TableList
.append([TableName
, Record
[1]])
180 ## GetAllIncludeDir() method
182 # Find all Include directories
184 # @param Db: Eot database
186 # @return IncludeList: A list of include directories
188 def GetAllIncludeDirs(Db
):
190 SqlCommand
= """select distinct Value1 from Inf where Model = %s order by Value1""" % MODEL_EFI_INCLUDE
191 RecordSet
= Db
.TblInf
.Exec(SqlCommand
)
193 for Record
in RecordSet
:
194 IncludeList
.append(Record
[0])
198 ## GetAllIncludeFiles() method
200 # Find all Include files
202 # @param Db: Eot database
204 # @return IncludeFileList: A list of include files
206 def GetAllIncludeFiles(Db
):
207 IncludeList
= GetAllIncludeDirs(Db
)
210 for Dir
in IncludeList
:
211 if os
.path
.isdir(Dir
):
212 SubDir
= os
.listdir(Dir
)
214 if os
.path
.isfile(Item
):
215 IncludeFileList
.append(Item
)
217 return IncludeFileList
219 ## GetAllSourceFiles() method
221 # Find all source files
223 # @param Db: Eot database
225 # @return SourceFileList: A list of source files
227 def GetAllSourceFiles(Db
):
229 SqlCommand
= """select distinct Value1 from Inf where Model = %s order by Value1""" % MODEL_EFI_SOURCE_FILE
230 RecordSet
= Db
.TblInf
.Exec(SqlCommand
)
232 for Record
in RecordSet
:
233 SourceFileList
.append(Record
[0])
235 return SourceFileList
237 ## GetAllFiles() method
239 # Find all files, both source files and include files
241 # @param Db: Eot database
243 # @return FileList: A list of files
247 IncludeFileList
= GetAllIncludeFiles(Db
)
248 SourceFileList
= GetAllSourceFiles(Db
)
249 for Item
in IncludeFileList
:
250 if os
.path
.isfile(Item
) and Item
not in FileList
:
251 FileList
.append(Item
)
252 for Item
in SourceFileList
:
253 if os
.path
.isfile(Item
) and Item
not in FileList
:
254 FileList
.append(Item
)
258 ## ParseConditionalStatement() method
260 # Parse conditional statement
262 # @param Line: One line to be parsed
263 # @param Macros: A set of all macro
264 # @param StatusSet: A set of all status
266 # @retval True: Find keyword of conditional statement
267 # @retval False: Not find keyword of conditional statement
269 def ParseConditionalStatement(Line
, Macros
, StatusSet
):
270 NewLine
= Line
.upper()
271 if NewLine
.find(TAB_IF_EXIST
.upper()) > -1:
272 IfLine
= Line
[NewLine
.find(TAB_IF_EXIST
) + len(TAB_IF_EXIST
) + 1:].strip()
273 IfLine
= ReplaceMacro(IfLine
, EotGlobalData
.gMACRO
, True)
274 IfLine
= ReplaceMacro(IfLine
, Macros
, True)
275 IfLine
= IfLine
.replace("\"", '')
276 IfLine
= IfLine
.replace("(", '')
277 IfLine
= IfLine
.replace(")", '')
278 Status
= os
.path
.exists(os
.path
.normpath(IfLine
))
279 StatusSet
.append([Status
])
281 if NewLine
.find(TAB_IF_DEF
.upper()) > -1:
282 IfLine
= Line
[NewLine
.find(TAB_IF_DEF
) + len(TAB_IF_DEF
) + 1:].strip()
284 if IfLine
in Macros
or IfLine
in EotGlobalData
.gMACRO
:
286 StatusSet
.append([Status
])
288 if NewLine
.find(TAB_IF_N_DEF
.upper()) > -1:
289 IfLine
= Line
[NewLine
.find(TAB_IF_N_DEF
) + len(TAB_IF_N_DEF
) + 1:].strip()
291 if IfLine
not in Macros
and IfLine
not in EotGlobalData
.gMACRO
:
293 StatusSet
.append([Status
])
295 if NewLine
.find(TAB_IF
.upper()) > -1:
296 IfLine
= Line
[NewLine
.find(TAB_IF
) + len(TAB_IF
) + 1:].strip()
297 Status
= ParseConditionalStatementMacros(IfLine
, Macros
)
298 StatusSet
.append([Status
])
300 if NewLine
.find(TAB_ELSE_IF
.upper()) > -1:
301 IfLine
= Line
[NewLine
.find(TAB_ELSE_IF
) + len(TAB_ELSE_IF
) + 1:].strip()
302 Status
= ParseConditionalStatementMacros(IfLine
, Macros
)
303 StatusSet
[-1].append(Status
)
305 if NewLine
.find(TAB_ELSE
.upper()) > -1:
307 for Item
in StatusSet
[-1]:
308 Status
= Status
or Item
309 StatusSet
[-1].append(not Status
)
311 if NewLine
.find(TAB_END_IF
.upper()) > -1:
317 ## ParseConditionalStatement() method
319 # Parse conditional statement with Macros
321 # @param Line: One line to be parsed
322 # @param Macros: A set of macros
324 # @return Line: New line after replacing macros
326 def ParseConditionalStatementMacros(Line
, Macros
):
327 if Line
.upper().find('DEFINED(') > -1 or Line
.upper().find('EXIST') > -1:
329 Line
= ReplaceMacro(Line
, EotGlobalData
.gMACRO
, True)
330 Line
= ReplaceMacro(Line
, Macros
, True)
331 Line
= Line
.replace("&&", "and")
332 Line
= Line
.replace("||", "or")
335 ## GetConditionalStatementStatus() method
337 # 1. Assume the latest status as True
338 # 2. Pop the top status of status set, previous status
339 # 3. Compare the latest one and the previous one and get new status
341 # @param StatusSet: A set of all status
343 # @return Status: The final status
345 def GetConditionalStatementStatus(StatusSet
):
347 for Item
in StatusSet
:
348 Status
= Status
and Item
[-1]
352 ## SearchBelongsToFunction() method
354 # Search all functions belong to the file
356 # @param BelongsToFile: File id
357 # @param StartLine: Start line of search scope
358 # @param EndLine: End line of search scope
360 # @return: The found function
362 def SearchBelongsToFunction(BelongsToFile
, StartLine
, EndLine
):
363 SqlCommand
= """select ID, Name from Function where BelongsToFile = %s and StartLine <= %s and EndLine >= %s""" %(BelongsToFile
, StartLine
, EndLine
)
364 RecordSet
= EotGlobalData
.gDb
.TblFunction
.Exec(SqlCommand
)
366 return RecordSet
[0][0], RecordSet
[0][1]
370 ## SearchPpiCallFunction() method
372 # Search all used PPI calling function 'PeiServicesReInstallPpi' and 'PeiServicesInstallPpi'
373 # Store the result to database
375 # @param Identifier: Table id
376 # @param SourceFileID: Source file id
377 # @param SourceFileFullPath: Source file full path
378 # @param ItemMode: Mode of the item
380 def SearchPpiCallFunction(Identifier
, SourceFileID
, SourceFileFullPath
, ItemMode
):
381 ItemName
, ItemType
, GuidName
, GuidMacro
, GuidValue
= '', 'Ppi', '', '', ''
382 SqlCommand
= """select Value, Name, BelongsToFile, StartLine, EndLine from %s
383 where (Name like '%%%s%%' and Model = %s)""" \
384 % (Identifier
, 'PeiServicesReInstallPpi', MODEL_IDENTIFIER_FUNCTION_CALLING
)
385 BelongsToFunctionID
, BelongsToFunction
= -1, ''
386 Db
= EotGlobalData
.gDb
.TblReport
387 RecordSet
= Db
.Exec(SqlCommand
)
388 for Record
in RecordSet
:
390 BelongsToFile
, StartLine
, EndLine
= Record
[2], Record
[3], Record
[4]
391 BelongsToFunctionID
, BelongsToFunction
= SearchBelongsToFunction(BelongsToFile
, StartLine
, EndLine
)
392 VariableList
= Record
[0].split(',')
393 for Variable
in VariableList
:
394 Variable
= Variable
.strip()
395 # Get index of the variable
396 if Variable
.find('[') > -1:
397 Index
= int(Variable
[Variable
.find('[') + 1 : Variable
.find(']')])
398 Variable
= Variable
[:Variable
.find('[')]
400 if Variable
.startswith('&'):
401 Variable
= Variable
[1:]
403 SqlCommand
= """select Value from %s where (Name like '%%%s%%') and Model = %s""" \
404 % (Identifier
, Variable
, MODEL_IDENTIFIER_VARIABLE
)
405 NewRecordSet
= Db
.Exec(SqlCommand
)
407 NewRecord
= NewRecordSet
[0][0]
408 VariableValueList
= NewRecord
.split('},')
409 if len(VariableValueList
) > Index
:
410 VariableValue
= VariableValueList
[Index
]
411 NewVariableValueList
= VariableValue
.split(',')
412 if len(NewVariableValueList
) > 1:
413 NewVariableValue
= NewVariableValueList
[1].strip()
414 if NewVariableValue
.startswith('&'):
415 Db
.Insert(-1, '', '', SourceFileID
, SourceFileFullPath
, ItemName
, ItemType
, ItemMode
, NewVariableValue
[1:], GuidMacro
, GuidValue
, BelongsToFunction
, 0)
418 EotGlobalData
.gOP_UN_MATCHED
.write('%s, %s, %s, %s, %s, %s\n' % (ItemType
, ItemMode
, SourceFileID
, SourceFileFullPath
, StartLine
, NewParameter
))
420 ItemName
, ItemType
, GuidName
, GuidMacro
, GuidValue
= '', 'Ppi', '', '', ''
421 SqlCommand
= """select Value, Name, BelongsToFile, StartLine, EndLine from %s
422 where (Value like '%%%s%%' and Model = %s)""" \
423 % (Identifier
, 'PeiServicesInstallPpi', MODEL_IDENTIFIER_ASSIGNMENT_EXPRESSION
)
424 BelongsToFunctionID
, BelongsToFunction
= -1, ''
425 Db
= EotGlobalData
.gDb
.TblReport
426 RecordSet
= Db
.Exec(SqlCommand
)
428 SqlCommand
= """select Value, Name, BelongsToFile, StartLine, EndLine from %s
429 where (Name like '%%%s%%' and Model = %s)""" \
430 % (Identifier
, 'PeiServicesInstallPpi', MODEL_IDENTIFIER_FUNCTION_CALLING
)
431 Db
= EotGlobalData
.gDb
.TblReport
432 RecordSet2
= Db
.Exec(SqlCommand
)
434 for Record
in RecordSet
+ RecordSet2
:
438 BelongsToFile
, StartLine
, EndLine
= Record
[2], Record
[3], Record
[4]
439 BelongsToFunctionID
, BelongsToFunction
= SearchBelongsToFunction(BelongsToFile
, StartLine
, EndLine
)
440 Variable
= Record
[0].replace('PeiServicesInstallPpi', '').replace('(', '').replace(')', '').replace('&', '').strip()
441 Variable
= Variable
[Variable
.find(',') + 1:].strip()
442 # Get index of the variable
443 if Variable
.find('[') > -1:
444 Index
= int(Variable
[Variable
.find('[') + 1 : Variable
.find(']')])
445 Variable
= Variable
[:Variable
.find('[')]
447 if Variable
.startswith('&'):
448 Variable
= Variable
[1:]
450 SqlCommand
= """select Value from %s where (Name like '%%%s%%') and Model = %s""" \
451 % (Identifier
, Variable
, MODEL_IDENTIFIER_VARIABLE
)
452 NewRecordSet
= Db
.Exec(SqlCommand
)
454 NewRecord
= NewRecordSet
[0][0]
455 VariableValueList
= NewRecord
.split('},')
456 if len(VariableValueList
) > Index
:
457 VariableValue
= VariableValueList
[Index
]
458 NewVariableValueList
= VariableValue
.split(',')
459 if len(NewVariableValueList
) > 1:
460 NewVariableValue
= NewVariableValueList
[1].strip()
461 if NewVariableValue
.startswith('&'):
462 Db
.Insert(-1, '', '', SourceFileID
, SourceFileFullPath
, ItemName
, ItemType
, ItemMode
, NewVariableValue
[1:], GuidMacro
, GuidValue
, BelongsToFunction
, 0)
465 EotGlobalData
.gOP_UN_MATCHED
.write('%s, %s, %s, %s, %s, %s\n' % (ItemType
, ItemMode
, SourceFileID
, SourceFileFullPath
, StartLine
, NewParameter
))
467 ## SearchPpis() method
469 # Search all used PPI calling function
470 # Store the result to database
472 # @param SqlCommand: SQL command statement
473 # @param Table: Table id
474 # @param SourceFileID: Source file id
475 # @param SourceFileFullPath: Source file full path
476 # @param ItemMode: Mode of the item
477 # @param PpiMode: Mode of PPI
479 def SearchPpi(SqlCommand
, Table
, SourceFileID
, SourceFileFullPath
, ItemMode
, PpiMode
= 1):
480 ItemName
, ItemType
, GuidName
, GuidMacro
, GuidValue
= '', 'Ppi', '', '', ''
481 BelongsToFunctionID
, BelongsToFunction
= -1, ''
482 Db
= EotGlobalData
.gDb
.TblReport
483 RecordSet
= Db
.Exec(SqlCommand
)
484 for Record
in RecordSet
:
485 Parameter
= GetPpiParameter(Record
[0], PpiMode
)
486 BelongsToFile
, StartLine
, EndLine
= Record
[2], Record
[3], Record
[4]
487 # Get BelongsToFunction
488 BelongsToFunctionID
, BelongsToFunction
= SearchBelongsToFunction(BelongsToFile
, StartLine
, EndLine
)
490 # Default is Not Found
494 if ItemMode
== 'Consumed':
495 if Parameter
.startswith('g'):
496 Db
.Insert(-1, '', '', SourceFileID
, SourceFileFullPath
, ItemName
, ItemType
, ItemMode
, Parameter
, GuidMacro
, GuidValue
, BelongsToFunction
, 0)
498 EotGlobalData
.gOP_UN_MATCHED
.write('%s, %s, %s, %s, %s, %s\n' % (ItemType
, ItemMode
, SourceFileID
, SourceFileFullPath
, StartLine
, Parameter
))
501 # Direct Parameter.Guid
502 SqlCommand
= """select Value from %s where (Name like '%%%s.Guid%%' or Name like '%%%s->Guid%%') and Model = %s""" % (Table
, Parameter
, Parameter
, MODEL_IDENTIFIER_ASSIGNMENT_EXPRESSION
)
503 NewRecordSet
= Db
.Exec(SqlCommand
)
504 for NewRecord
in NewRecordSet
:
505 GuidName
= GetParameterName(NewRecord
[0])
506 Db
.Insert(-1, '', '', SourceFileID
, SourceFileFullPath
, ItemName
, ItemType
, ItemMode
, GuidName
, GuidMacro
, GuidValue
, BelongsToFunction
, 0)
512 if Key
.rfind(' ') > -1:
513 Key
= Key
[Key
.rfind(' ') : ].strip().replace('&', '')
514 Value
= FindKeyValue(EotGlobalData
.gDb
.TblFile
, Table
, Key
)
515 List
= GetSplitValueList(Value
.replace('\n', ''), TAB_COMMA_SPLIT
)
517 GuidName
= GetParameterName(List
[1])
518 Db
.Insert(-1, '', '', SourceFileID
, SourceFileFullPath
, ItemName
, ItemType
, ItemMode
, GuidName
, GuidMacro
, GuidValue
, BelongsToFunction
, 0)
523 Start
= Parameter
.find('[')
524 End
= Parameter
.find(']')
525 if Start
> -1 and End
> -1 and Start
< End
:
527 Index
= int(Parameter
[Start
+ 1 : End
])
528 Parameter
= Parameter
[0 : Start
]
529 SqlCommand
= """select Value from %s where Name = '%s' and Model = %s""" % (Table
, Parameter
, MODEL_IDENTIFIER_VARIABLE
)
530 NewRecordSet
= Db
.Exec(SqlCommand
)
531 for NewRecord
in NewRecordSet
:
532 NewParameter
= GetSplitValueList(NewRecord
[0], '}')[Index
]
533 GuidName
= GetPpiParameter(NewParameter
[NewParameter
.find('{') : ])
534 Db
.Insert(-1, '', '', SourceFileID
, SourceFileFullPath
, ItemName
, ItemType
, ItemMode
, GuidName
, GuidMacro
, GuidValue
, BelongsToFunction
, 0)
539 # A External Parameter
541 SqlCommand
= """select File.ID from Inf, File
542 where BelongsToFile = (select BelongsToFile from Inf where Value1 = '%s')
543 and Inf.Model = %s and Inf.Value1 = File.FullPath and File.Model = %s""" % (SourceFileFullPath
, MODEL_EFI_SOURCE_FILE
, MODEL_FILE_C
)
544 NewRecordSet
= Db
.Exec(SqlCommand
)
545 for NewRecord
in NewRecordSet
:
546 Table
= 'Identifier' + str(NewRecord
[0])
547 SqlCommand
= """select Value from %s where Name = '%s' and Modifier = 'EFI_PEI_PPI_DESCRIPTOR' and Model = %s""" % (Table
, Parameter
, MODEL_IDENTIFIER_VARIABLE
)
548 PpiSet
= Db
.Exec(SqlCommand
)
550 GuidName
= GetPpiParameter(PpiSet
[0][0])
552 Db
.Insert(-1, '', '', SourceFileID
, SourceFileFullPath
, ItemName
, ItemType
, ItemMode
, GuidName
, GuidMacro
, GuidValue
, BelongsToFunction
, 0)
557 EotGlobalData
.gOP_UN_MATCHED
.write('%s, %s, %s, %s, %s, %s\n' % (ItemType
, ItemMode
, SourceFileID
, SourceFileFullPath
, StartLine
, Parameter
))
559 ## SearchProtocols() method
561 # Search all used PROTOCOL calling function
562 # Store the result to database
564 # @param SqlCommand: SQL command statement
565 # @param Table: Table id
566 # @param SourceFileID: Source file id
567 # @param SourceFileFullPath: Source file full path
568 # @param ItemMode: Mode of the item
569 # @param ProtocolMode: Mode of PROTOCOL
571 def SearchProtocols(SqlCommand
, Table
, SourceFileID
, SourceFileFullPath
, ItemMode
, ProtocolMode
):
572 ItemName
, ItemType
, GuidName
, GuidMacro
, GuidValue
= '', 'Protocol', '', '', ''
573 BelongsToFunctionID
, BelongsToFunction
= -1, ''
574 Db
= EotGlobalData
.gDb
.TblReport
575 RecordSet
= Db
.Exec(SqlCommand
)
576 for Record
in RecordSet
:
578 BelongsToFile
, StartLine
, EndLine
= Record
[2], Record
[3], Record
[4]
579 # Get BelongsToFunction
580 BelongsToFunctionID
, BelongsToFunction
= SearchBelongsToFunction(BelongsToFile
, StartLine
, EndLine
)
582 # Default is Not Found
585 if ProtocolMode
== 0 or ProtocolMode
== 1:
586 Parameter
= GetProtocolParameter(Record
[0], ProtocolMode
)
587 if Parameter
.startswith('g') or Parameter
.endswith('Guid') or Parameter
== 'ShellEnvProtocol' or Parameter
== 'ShellInterfaceProtocol':
588 GuidName
= GetParameterName(Parameter
)
589 Db
.Insert(-1, '', '', SourceFileID
, SourceFileFullPath
, ItemName
, ItemType
, ItemMode
, GuidName
, GuidMacro
, GuidValue
, BelongsToFunction
, 0)
592 if ProtocolMode
== 2:
593 Protocols
= GetSplitValueList(Record
[0], TAB_COMMA_SPLIT
)
594 for Protocol
in Protocols
:
595 if Protocol
.startswith('&') and Protocol
.endswith('Guid'):
596 GuidName
= GetParameterName(Protocol
)
597 Db
.Insert(-1, '', '', SourceFileID
, SourceFileFullPath
, ItemName
, ItemType
, ItemMode
, GuidName
, GuidMacro
, GuidValue
, BelongsToFunction
, 0)
600 NewValue
= FindKeyValue(EotGlobalData
.gDb
.TblFile
, Table
, Protocol
)
601 if Protocol
!= NewValue
and NewValue
.endswith('Guid'):
602 GuidName
= GetParameterName(NewValue
)
603 Db
.Insert(-1, '', '', SourceFileID
, SourceFileFullPath
, ItemName
, ItemType
, ItemMode
, GuidName
, GuidMacro
, GuidValue
, BelongsToFunction
, 0)
607 if BelongsToFunction
in EotGlobalData
.gProducedProtocolLibrary
or BelongsToFunction
in EotGlobalData
.gConsumedProtocolLibrary
:
608 EotGlobalData
.gOP_UN_MATCHED_IN_LIBRARY_CALLING
.write('%s, %s, %s, %s, %s, %s, %s\n' % (ItemType
, ItemMode
, SourceFileID
, SourceFileFullPath
, StartLine
, Parameter
, BelongsToFunction
))
610 EotGlobalData
.gOP_UN_MATCHED
.write('%s, %s, %s, %s, %s, %s\n' % (ItemType
, ItemMode
, SourceFileID
, SourceFileFullPath
, StartLine
, Parameter
))
612 ## SearchFunctionCalling() method
614 # Search all used PPI/PROTOCOL calling function by library
615 # Store the result to database
617 # @param SqlCommand: SQL command statement
618 # @param Table: Table id
619 # @param SourceFileID: Source file id
620 # @param SourceFileFullPath: Source file full path
621 # @param ItemType: Type of the item, PPI or PROTOCOL
622 # @param ItemMode: Mode of item
624 def SearchFunctionCalling(Table
, SourceFileID
, SourceFileFullPath
, ItemType
, ItemMode
):
625 LibraryList
= sdict()
626 Db
= EotGlobalData
.gDb
.TblReport
627 Parameters
, ItemName
, GuidName
, GuidMacro
, GuidValue
, BelongsToFunction
= [], '', '', '', '', ''
628 if ItemType
== 'Protocol' and ItemMode
== 'Produced':
629 LibraryList
= EotGlobalData
.gProducedProtocolLibrary
630 elif ItemType
== 'Protocol' and ItemMode
== 'Consumed':
631 LibraryList
= EotGlobalData
.gConsumedProtocolLibrary
632 elif ItemType
== 'Protocol' and ItemMode
== 'Callback':
633 LibraryList
= EotGlobalData
.gCallbackProtocolLibrary
634 elif ItemType
== 'Ppi' and ItemMode
== 'Produced':
635 LibraryList
= EotGlobalData
.gProducedPpiLibrary
636 elif ItemType
== 'Ppi' and ItemMode
== 'Consumed':
637 LibraryList
= EotGlobalData
.gConsumedPpiLibrary
639 for Library
in LibraryList
:
640 Index
= LibraryList
[Library
]
641 SqlCommand
= """select Value, StartLine from %s
642 where Name like '%%%s%%' and Model = %s""" \
643 % (Table
, Library
, MODEL_IDENTIFIER_FUNCTION_CALLING
)
644 RecordSet
= Db
.Exec(SqlCommand
)
645 for Record
in RecordSet
:
648 ParameterList
= GetSplitValueList(Record
[0], TAB_COMMA_SPLIT
)
649 for Parameter
in ParameterList
:
650 Parameters
.append(GetParameterName(Parameter
))
652 Parameters
= [GetProtocolParameter(Record
[0], Index
)]
653 StartLine
= Record
[1]
654 for Parameter
in Parameters
:
655 if Parameter
.startswith('g') or Parameter
.endswith('Guid') or Parameter
== 'ShellEnvProtocol' or Parameter
== 'ShellInterfaceProtocol':
656 GuidName
= GetParameterName(Parameter
)
657 Db
.Insert(-1, '', '', SourceFileID
, SourceFileFullPath
, ItemName
, ItemType
, ItemMode
, GuidName
, GuidMacro
, GuidValue
, BelongsToFunction
, 0)
661 EotGlobalData
.gOP_UN_MATCHED
.write('%s, %s, %s, %s, %s, %s\n' % (ItemType
, ItemMode
, SourceFileID
, SourceFileFullPath
, StartLine
, Parameter
))
663 ## FindProtocols() method
665 # Find defined protocols
667 # @param SqlCommand: SQL command statement
668 # @param Table: Table id
669 # @param SourceFileID: Source file id
670 # @param SourceFileFullPath: Source file full path
671 # @param ItemName: String of protocol definition
672 # @param ItemType: Type of the item, PPI or PROTOCOL
673 # @param ItemMode: Mode of item
675 #def FindProtocols(Db, SqlCommand, Table, SourceFileID, SourceFileFullPath, ItemName, ItemType, ItemMode, GuidName, GuidMacro, GuidValue):
676 # BelongsToFunction = ''
677 # RecordSet = Db.Exec(SqlCommand)
678 # for Record in RecordSet:
680 # Parameter = GetProtocolParameter(Record[0])
682 ## GetProtocolParameter() method
684 # Parse string of protocol and find parameters
686 # @param Parameter: Parameter to be parsed
687 # @param Index: The index of the parameter
689 # @return: call common GetParameter
691 def GetProtocolParameter(Parameter
, Index
= 1):
692 return GetParameter(Parameter
, Index
)
694 ## GetPpiParameter() method
696 # Parse string of ppi and find parameters
698 # @param Parameter: Parameter to be parsed
699 # @param Index: The index of the parameter
701 # @return: call common GetParameter
703 def GetPpiParameter(Parameter
, Index
= 1):
704 return GetParameter(Parameter
, Index
)
706 ## GetParameter() method
708 # Get a parameter by index
710 # @param Parameter: Parameter to be parsed
711 # @param Index: The index of the parameter
713 # @return Parameter: The found parameter
715 def GetParameter(Parameter
, Index
= 1):
716 ParameterList
= GetSplitValueList(Parameter
, TAB_COMMA_SPLIT
)
717 if len(ParameterList
) > Index
:
718 Parameter
= GetParameterName(ParameterList
[Index
])
724 ## GetParameterName() method
726 # Get a parameter name
728 # @param Parameter: Parameter to be parsed
730 # @return: The name of parameter
732 def GetParameterName(Parameter
):
733 if type(Parameter
) == type('') and Parameter
.startswith('&'):
734 return Parameter
[1:].replace('{', '').replace('}', '').replace('\r', '').replace('\n', '').strip()
736 return Parameter
.strip()
738 ## FindKeyValue() method
740 # Find key value of a variable
742 # @param Db: Database to be searched
743 # @param Table: Table to be searched
744 # @param Key: The keyword
746 # @return Value: The value of the the keyword
748 def FindKeyValue(Db
, Table
, Key
):
749 SqlCommand
= """select Value from %s where Name = '%s' and (Model = %s or Model = %s)""" % (Table
, Key
, MODEL_IDENTIFIER_VARIABLE
, MODEL_IDENTIFIER_ASSIGNMENT_EXPRESSION
)
750 RecordSet
= Db
.Exec(SqlCommand
)
752 for Record
in RecordSet
:
753 if Record
[0] != 'NULL':
754 Value
= FindKeyValue(Db
, Table
, GetParameterName(Record
[0]))
761 ## ParseMapFile() method
763 # Parse map files to get a dict of 'ModuleName' : {FunName : FunAddress}
765 # @param Files: A list of map files
767 # @return AllMaps: An object of all map files
769 def ParseMapFile(Files
):
774 Content
= open(File
, 'r').readlines()
776 Line
= CleanString(Line
)
781 if Line
.find('(') > -1 and Line
.find(')') > -1:
782 if CurrentModule
!= '' and CurrentMaps
!= {}:
783 AllMaps
[CurrentModule
] = CurrentMaps
784 CurrentModule
= Line
[:Line
.find('(')]
792 if List
[1] == 'F' or List
[1] == 'FS':
796 CurrentMaps
[Name
] = Address
803 # Convert a GUID to a GUID with all upper letters
805 # @param guid: The GUID to be converted
807 # @param newGuid: The GUID with all upper letters.
809 def ConvertGuid(guid
):
810 numList
= ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
812 if guid
.startswith('g'):
815 if i
.upper() == i
and i
not in numList
:
816 newGuid
= newGuid
+ ('_' + i
)
818 newGuid
= newGuid
+ i
.upper()
819 if newGuid
.startswith('_'):
820 newGuid
= newGuid
[1:]
821 if newGuid
.endswith('_'):
822 newGuid
= newGuid
[:-1]
826 ## ConvertGuid2() method
828 # Convert a GUID to a GUID with new string instead of old string
830 # @param guid: The GUID to be converted
831 # @param old: Old string to be replaced
832 # @param new: New string to replace the old one
834 # @param newGuid: The GUID after replacement
836 def ConvertGuid2(guid
, old
, new
):
837 newGuid
= ConvertGuid(guid
)
838 newGuid
= newGuid
.replace(old
, new
)
844 # This acts like the main() function for the script, unless it is 'import'ed into another
847 if __name__
== '__main__':