## @file
# This file is used to define common parsing related functions used in parsing
#
# Copyright (c) 2008 - 2010, Intel Corporation. All rights reserved.<BR>
# This program and the accompanying materials
# are licensed and made available under the terms and conditions of the BSD License
# which accompanies this distribution. The full text of the license may be found at
#
# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#
##
# Import Modules
#
from CommonDataClass.DataClass import *
import EotGlobalData
## PreProcess() method
#
# Pre process a file
#
# 1. Remove all comments
# 2. Merge multiple lines code to one line
#
# @param Filename: Name of the file to be parsed
# @param MergeMultipleLines: Switch for if merge multiple lines
# @param LineNo: Default line no
#
# @return Lines: The file contents after remvoing comments
#
Lines = []
ReservedLine = ''
# Remove comment block
ReservedLine = ''
continue
# Remove comments at tail and remove spaces again
if Line == '':
continue
# Add multiple lines to one line
ReservedLine = ''
continue
continue
return Lines
## AddToGlobalMacro() method
#
# Add a macro to EotGlobalData.gMACRO
#
# @param Name: Name of the macro
# @param Value: Value of the macro
#
## AddToSelfMacro() method
#
# Parse a line of macro definition and add it to a macro set
#
# @param SelfMacro: The self macro set
# @param Line: The line of a macro definition
#
# @return Name: Name of macro
# @return Value: Value of macro
#
## GetIncludeListOfFile() method
#
# Get the include path list for a source file
#
# 1. Find the source file belongs to which INF file
# 2. Find the inf's package
# 3. Return the include path list of the package
#
# @param WorkSpace: WORKSPACE path
# @param Filepath: File path
# @param Db: Eot database
#
# @return IncludeList: A list of include directories
#
IncludeList = []
SqlCommand = """
select Value1 from Inf where Model = %s and BelongsToFile in(
select distinct B.BelongsToFile from File as A left join Inf as B
where A.ID = B.BelongsToFile and B.Model = %s and (A.Path || '%s' || B.Value1) = '%s')""" \
SqlCommand = """select Value1 from Dec where BelongsToFile =
(select ID from File where FullPath = '%s') and Model = %s""" \
for NewRecord in NewRecordSet:
if IncludePath not in IncludeList:
return IncludeList
## GetTableList() method
#
# Search table file and find all small tables
#
# @param FileModelList: Model code for the file list
# @param Table: Table to insert records
# @param Db: Eot database
#
# @return TableList: A list of tables
#
TableList = []
return TableList
## GetAllIncludeDir() method
#
# Find all Include directories
#
# @param Db: Eot database
#
# @return IncludeList: A list of include directories
#
IncludeList = []
SqlCommand = """select distinct Value1 from Inf where Model = %s order by Value1""" % MODEL_EFI_INCLUDE
return IncludeList
## GetAllIncludeFiles() method
#
# Find all Include files
#
# @param Db: Eot database
#
# @return IncludeFileList: A list of include files
#
IncludeFileList = []
for Dir in IncludeList:
return IncludeFileList
## GetAllSourceFiles() method
#
# Find all source files
#
# @param Db: Eot database
#
# @return SourceFileList: A list of source files
#
SourceFileList = []
SqlCommand = """select distinct Value1 from Inf where Model = %s order by Value1""" % MODEL_EFI_SOURCE_FILE
return SourceFileList
## GetAllFiles() method
#
# Find all files, both source files and include files
#
# @param Db: Eot database
#
# @return FileList: A list of files
#
FileList = []
for Item in IncludeFileList:
for Item in SourceFileList:
return FileList
## ParseConditionalStatement() method
#
# Parse conditional statement
#
# @param Line: One line to be parsed
# @param Macros: A set of all macro
# @param StatusSet: A set of all status
#
# @retval True: Find keyword of conditional statement
# @retval False: Not find keyword of conditional statement
#
return True
return True
return True
return True
return True
return True
return True
return False
## ParseConditionalStatement() method
#
# Parse conditional statement with Macros
#
# @param Line: One line to be parsed
# @param Macros: A set of macros
#
# @return Line: New line after replacing macros
#
return False
## GetConditionalStatementStatus() method
#
# 1. Assume the latest status as True
# 2. Pop the top status of status set, previous status
# 3. Compare the latest one and the previous one and get new status
#
# @param StatusSet: A set of all status
#
# @return Status: The final status
#
return Status
## SearchBelongsToFunction() method
#
# Search all functions belong to the file
#
# @param BelongsToFile: File id
# @param StartLine: Start line of search scope
# @param EndLine: End line of search scope
#
# @return: The found function
#
SqlCommand = """select ID, Name from Function where BelongsToFile = %s and StartLine <= %s and EndLine >= %s""" %(BelongsToFile, StartLine, EndLine)
if RecordSet != []:
else:
return -1, ''
## SearchPpiCallFunction() method
#
# Search all used PPI calling function 'PeiServicesReInstallPpi' and 'PeiServicesInstallPpi'
# Store the result to database
#
# @param Identifier: Table id
# @param SourceFileID: Source file id
# @param SourceFileFullPath: Source file full path
# @param ItemMode: Mode of the item
#
SqlCommand = """select Value, Name, BelongsToFile, StartLine, EndLine from %s
where (Name like '%%%s%%' and Model = %s)""" \
Index = 0
for Variable in VariableList:
# Get index of the variable
# Get variable name
# Get variable value
SqlCommand = """select Value from %s where (Name like '%%%s%%') and Model = %s""" \
if NewRecordSet:
Db.Insert(-1, '', '', SourceFileID, SourceFileFullPath, ItemName, ItemType, ItemMode, NewVariableValue[1:], GuidMacro, GuidValue, BelongsToFunction, 0)
continue
else:
EotGlobalData.gOP_UN_MATCHED.write('%s, %s, %s, %s, %s, %s\n' % (ItemType, ItemMode, SourceFileID, SourceFileFullPath, StartLine, NewParameter))
SqlCommand = """select Value, Name, BelongsToFile, StartLine, EndLine from %s
where (Value like '%%%s%%' and Model = %s)""" \
SqlCommand = """select Value, Name, BelongsToFile, StartLine, EndLine from %s
where (Name like '%%%s%%' and Model = %s)""" \
if Record == []:
continue
Index = 0
Variable = Record[0].replace('PeiServicesInstallPpi', '').replace('(', '').replace(')', '').replace('&', '').strip()
# Get index of the variable
# Get variable name
# Get variable value
SqlCommand = """select Value from %s where (Name like '%%%s%%') and Model = %s""" \
if NewRecordSet:
Db.Insert(-1, '', '', SourceFileID, SourceFileFullPath, ItemName, ItemType, ItemMode, NewVariableValue[1:], GuidMacro, GuidValue, BelongsToFunction, 0)
continue
else:
EotGlobalData.gOP_UN_MATCHED.write('%s, %s, %s, %s, %s, %s\n' % (ItemType, ItemMode, SourceFileID, SourceFileFullPath, StartLine, NewParameter))
## SearchPpis() method
#
# Search all used PPI calling function
# Store the result to database
#
# @param SqlCommand: SQL command statement
# @param Table: Table id
# @param SourceFileID: Source file id
# @param SourceFileFullPath: Source file full path
# @param ItemMode: Mode of the item
# @param PpiMode: Mode of PPI
#
# Get BelongsToFunction
# Default is Not Found
# For Consumed Ppi
if ItemMode == 'Consumed':
Db.Insert(-1, '', '', SourceFileID, SourceFileFullPath, ItemName, ItemType, ItemMode, Parameter, GuidMacro, GuidValue, BelongsToFunction, 0)
else:
EotGlobalData.gOP_UN_MATCHED.write('%s, %s, %s, %s, %s, %s\n' % (ItemType, ItemMode, SourceFileID, SourceFileFullPath, StartLine, Parameter))
continue
# Direct Parameter.Guid
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)
for NewRecord in NewRecordSet:
Db.Insert(-1, '', '', SourceFileID, SourceFileFullPath, ItemName, ItemType, ItemMode, GuidName, GuidMacro, GuidValue, BelongsToFunction, 0)
# Defined Parameter
if not IsFound:
Db.Insert(-1, '', '', SourceFileID, SourceFileFullPath, ItemName, ItemType, ItemMode, GuidName, GuidMacro, GuidValue, BelongsToFunction, 0)
# A list Parameter
if not IsFound:
try:
SqlCommand = """select Value from %s where Name = '%s' and Model = %s""" % (Table, Parameter, MODEL_IDENTIFIER_VARIABLE)
for NewRecord in NewRecordSet:
Db.Insert(-1, '', '', SourceFileID, SourceFileFullPath, ItemName, ItemType, ItemMode, GuidName, GuidMacro, GuidValue, BelongsToFunction, 0)
except Exception:
pass
# A External Parameter
if not IsFound:
SqlCommand = """select File.ID from Inf, File
where BelongsToFile = (select BelongsToFile from Inf where Value1 = '%s')
and Inf.Model = %s and Inf.Value1 = File.FullPath and File.Model = %s""" % (SourceFileFullPath, MODEL_EFI_SOURCE_FILE, MODEL_FILE_C)
for NewRecord in NewRecordSet:
SqlCommand = """select Value from %s where Name = '%s' and Modifier = 'EFI_PEI_PPI_DESCRIPTOR' and Model = %s""" % (Table, Parameter, MODEL_IDENTIFIER_VARIABLE)
if PpiSet != []:
if GuidName != '':
Db.Insert(-1, '', '', SourceFileID, SourceFileFullPath, ItemName, ItemType, ItemMode, GuidName, GuidMacro, GuidValue, BelongsToFunction, 0)
break
if not IsFound:
EotGlobalData.gOP_UN_MATCHED.write('%s, %s, %s, %s, %s, %s\n' % (ItemType, ItemMode, SourceFileID, SourceFileFullPath, StartLine, Parameter))
## SearchProtocols() method
#
# Search all used PROTOCOL calling function
# Store the result to database
#
# @param SqlCommand: SQL command statement
# @param Table: Table id
# @param SourceFileID: Source file id
# @param SourceFileFullPath: Source file full path
# @param ItemMode: Mode of the item
# @param ProtocolMode: Mode of PROTOCOL
#
Parameter = ''
# Get BelongsToFunction
# Default is Not Found
if Parameter.startswith('g') or Parameter.endswith('Guid') or Parameter == 'ShellEnvProtocol' or Parameter == 'ShellInterfaceProtocol':
Db.Insert(-1, '', '', SourceFileID, SourceFileFullPath, ItemName, ItemType, ItemMode, GuidName, GuidMacro, GuidValue, BelongsToFunction, 0)
if ProtocolMode == 2:
Db.Insert(-1, '', '', SourceFileID, SourceFileFullPath, ItemName, ItemType, ItemMode, GuidName, GuidMacro, GuidValue, BelongsToFunction, 0)
else:
Db.Insert(-1, '', '', SourceFileID, SourceFileFullPath, ItemName, ItemType, ItemMode, GuidName, GuidMacro, GuidValue, BelongsToFunction, 0)
if not IsFound:
if BelongsToFunction in EotGlobalData.gProducedProtocolLibrary or BelongsToFunction in EotGlobalData.gConsumedProtocolLibrary:
EotGlobalData.gOP_UN_MATCHED_IN_LIBRARY_CALLING.write('%s, %s, %s, %s, %s, %s, %s\n' % (ItemType, ItemMode, SourceFileID, SourceFileFullPath, StartLine, Parameter, BelongsToFunction))
else:
EotGlobalData.gOP_UN_MATCHED.write('%s, %s, %s, %s, %s, %s\n' % (ItemType, ItemMode, SourceFileID, SourceFileFullPath, StartLine, Parameter))
## SearchFunctionCalling() method
#
# Store the result to database
#
# @param SqlCommand: SQL command statement
# @param Table: Table id
# @param SourceFileID: Source file id
# @param SourceFileFullPath: Source file full path
# @param ItemType: Type of the item, PPI or PROTOCOL
# @param ItemMode: Mode of item
#
LibraryList = sdict()
for Library in LibraryList:
SqlCommand = """select Value, StartLine from %s
where Name like '%%%s%%' and Model = %s""" \
if Index == -1:
for Parameter in ParameterList:
else:
for Parameter in Parameters:
if Parameter.startswith('g') or Parameter.endswith('Guid') or Parameter == 'ShellEnvProtocol' or Parameter == 'ShellInterfaceProtocol':
Db.Insert(-1, '', '', SourceFileID, SourceFileFullPath, ItemName, ItemType, ItemMode, GuidName, GuidMacro, GuidValue, BelongsToFunction, 0)
if not IsFound:
EotGlobalData.gOP_UN_MATCHED.write('%s, %s, %s, %s, %s, %s\n' % (ItemType, ItemMode, SourceFileID, SourceFileFullPath, StartLine, Parameter))
## FindProtocols() method
#
# Find defined protocols
#
# @param SqlCommand: SQL command statement
# @param Table: Table id
# @param SourceFileID: Source file id
# @param SourceFileFullPath: Source file full path
# @param ItemName: String of protocol definition
# @param ItemType: Type of the item, PPI or PROTOCOL
# @param ItemMode: Mode of item
#
#def FindProtocols(Db, SqlCommand, Table, SourceFileID, SourceFileFullPath, ItemName, ItemType, ItemMode, GuidName, GuidMacro, GuidValue):
# BelongsToFunction = ''
# RecordSet = Db.Exec(SqlCommand)
# for Record in RecordSet:
# IsFound = True
# Parameter = GetProtocolParameter(Record[0])
## GetProtocolParameter() method
#
# Parse string of protocol and find parameters
#
# @param Parameter: Parameter to be parsed
# @param Index: The index of the parameter
#
# @return: call common GetParameter
#
## GetPpiParameter() method
#
# Parse string of ppi and find parameters
#
# @param Parameter: Parameter to be parsed
# @param Index: The index of the parameter
#
# @return: call common GetParameter
#
## GetParameter() method
#
# Get a parameter by index
#
# @param Parameter: Parameter to be parsed
# @param Index: The index of the parameter
#
# @return Parameter: The found parameter
#
return Parameter
return ''
## GetParameterName() method
#
# Get a parameter name
#
# @param Parameter: Parameter to be parsed
#
# @return: The name of parameter
#
else:
## FindKeyValue() method
#
# Find key value of a variable
#
# @param Db: Database to be searched
# @param Table: Table to be searched
# @param Key: The keyword
#
# @return Value: The value of the the keyword
#
SqlCommand = """select Value from %s where Name = '%s' and (Model = %s or Model = %s)""" % (Table, Key, MODEL_IDENTIFIER_VARIABLE, MODEL_IDENTIFIER_ASSIGNMENT_EXPRESSION)
Value = ''
if Value != '':
return Value
else:
return Key
## ParseMapFile() method
#
# Parse map files to get a dict of 'ModuleName' : {FunName : FunAddress}
#
# @param Files: A list of map files
#
# @return AllMaps: An object of all map files
#
AllMaps = {}
CurrentModule = ''
CurrentMaps = {}
# skip empty line
if Line == '':
continue
CurrentMaps = {}
continue
else:
Name = ''
Address = ''
else:
continue
return AllMaps
## ConvertGuid
#
# Convert a GUID to a GUID with all upper letters
#
# @param guid: The GUID to be converted
#
# @param newGuid: The GUID with all upper letters.
#
newGuid = ''
for i in guid:
else:
return newGuid
## ConvertGuid2() method
#
# Convert a GUID to a GUID with new string instead of old string
#
# @param guid: The GUID to be converted
# @param old: Old string to be replaced
# @param new: New string to replace the old one
#
# @param newGuid: The GUID after replacement
#
return newGuid
##
#
# This acts like the main() function for the script, unless it is 'import'ed into another
# script.
#
if __name__ == '__main__':
pass