## @file\r
# Common routines used by all tools\r
#\r
-# Copyright (c) 2007 - 2017, Intel Corporation. All rights reserved.<BR>\r
+# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
# This program and the accompanying materials\r
# are licensed and made available under the terms and conditions of the BSD License\r
# which accompanies this distribution. The full text of the license may be found at\r
##\r
# Import Modules\r
#\r
+from __future__ import absolute_import\r
import Common.LongFilePathOs as os\r
import sys\r
import string\r
-import thread\r
import threading\r
import time\r
import re\r
-import cPickle\r
+import pickle\r
import array\r
import shutil\r
+from random import sample\r
from struct import pack\r
from UserDict import IterableUserDict\r
from UserList import UserList\r
\r
from Common import EdkLogger as EdkLogger\r
from Common import GlobalData as GlobalData\r
-from DataType import *\r
-from BuildToolError import *\r
+from .DataType import *\r
+from .BuildToolError import *\r
from CommonDataClass.DataClass import *\r
-from Parsing import GetSplitValueList\r
+from .Parsing import GetSplitValueList\r
from Common.LongFilePathSupport import OpenLongFilePath as open\r
from Common.MultipleWorkspace import MultipleWorkspace as mws\r
import uuid\r
-\r
+from CommonDataClass.Exceptions import BadExpression\r
+from Common.caching import cached_property\r
+import subprocess\r
## Regular expression used to find out place holders in string template\r
gPlaceholderPattern = re.compile("\$\{([^$()\s]+)\}", re.MULTILINE | re.UNICODE)\r
\r
+## regular expressions for map file processing\r
+startPatternGeneral = re.compile("^Start[' ']+Length[' ']+Name[' ']+Class")\r
+addressPatternGeneral = re.compile("^Address[' ']+Publics by Value[' ']+Rva\+Base")\r
+valuePatternGcc = re.compile('^([\w_\.]+) +([\da-fA-Fx]+) +([\da-fA-Fx]+)$')\r
+pcdPatternGcc = re.compile('^([\da-fA-Fx]+) +([\da-fA-Fx]+)')\r
+secReGeneral = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\da-fA-F]+)[Hh]? +([.\w\$]+) +(\w+)', re.UNICODE)\r
+\r
+StructPattern = re.compile(r'[_a-zA-Z][0-9A-Za-z_]*$')\r
+\r
## Dictionary used to store file time stamp for quick re-access\r
gFileTimeStampCache = {} # {file path : file time stamp}\r
\r
## Dictionary used to store dependencies of files\r
gDependencyDatabase = {} # arch : {file path : [dependent files list]}\r
\r
+#\r
+# If a module is built more than once with different PCDs or library classes\r
+# a temporary INF file with same content is created, the temporary file is removed\r
+# when build exits.\r
+#\r
+_TempInfs = []\r
+\r
def GetVariableOffset(mapfilepath, efifilepath, varnames):\r
- """ Parse map file to get variable offset in current EFI file \r
+ """ Parse map file to get variable offset in current EFI file\r
@param mapfilepath Map file absolution path\r
@param efifilepath: EFI binary file full path\r
@param varnames iteratable container whose elements are variable names to be searched\r
- \r
+\r
@return List whos elements are tuple with variable name and raw offset\r
"""\r
lines = []\r
f.close()\r
except:\r
return None\r
- \r
+\r
if len(lines) == 0: return None\r
firstline = lines[0].strip()\r
if (firstline.startswith("Archive member included ") and\r
def _parseForXcode(lines, efifilepath, varnames):\r
status = 0\r
ret = []\r
- for index, line in enumerate(lines):\r
+ for line in lines:\r
line = line.strip()\r
if status == 0 and line == "# Symbols:":\r
status = 1\r
if status == 1 and len(line) != 0:\r
for varname in varnames:\r
if varname in line:\r
+ # cannot pregenerate this RegEx since it uses varname from varnames.\r
m = re.match('^([\da-fA-FxX]+)([\s\S]*)([_]*%s)$' % varname, line)\r
- if m != None:\r
+ if m is not None:\r
ret.append((varname, m.group(1)))\r
return ret\r
\r
\r
# status handler\r
if status == 3:\r
- m = re.match('^([\w_\.]+) +([\da-fA-Fx]+) +([\da-fA-Fx]+)$', line)\r
- if m != None:\r
+ m = valuePatternGcc.match(line)\r
+ if m is not None:\r
sections.append(m.groups(0))\r
for varname in varnames:\r
Str = ''\r
m = re.match("^.data.(%s)" % varname, line)\r
- if m != None:\r
+ if m is not None:\r
m = re.match(".data.(%s)$" % varname, line)\r
- if m != None:\r
+ if m is not None:\r
Str = lines[index + 1]\r
else:\r
Str = line[len(".data.%s" % varname):]\r
if Str:\r
- m = re.match('^([\da-fA-Fx]+) +([\da-fA-Fx]+)', Str.strip())\r
- if m != None:\r
- varoffset.append((varname, int(m.groups(0)[0], 16) , int(sections[-1][1], 16), sections[-1][0]))\r
+ m = pcdPatternGcc.match(Str.strip())\r
+ if m is not None:\r
+ varoffset.append((varname, int(m.groups(0)[0], 16), int(sections[-1][1], 16), sections[-1][0]))\r
\r
if not varoffset:\r
return []\r
# get section information from efi file\r
efisecs = PeImageClass(efifilepath).SectionHeaderList\r
- if efisecs == None or len(efisecs) == 0:\r
+ if efisecs is None or len(efisecs) == 0:\r
return []\r
#redirection\r
redirection = 0\r
status = 0 #0 - beginning of file; 1 - PE section definition; 2 - symbol table\r
secs = [] # key = section name\r
varoffset = []\r
- secRe = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\da-fA-F]+)[Hh]? +([.\w\$]+) +(\w+)', re.UNICODE)\r
symRe = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\.:\\\\\w\?@\$]+) +([\da-fA-F]+)', re.UNICODE)\r
\r
for line in lines:\r
line = line.strip()\r
- if re.match("^Start[' ']+Length[' ']+Name[' ']+Class", line):\r
+ if startPatternGeneral.match(line):\r
status = 1\r
continue\r
- if re.match("^Address[' ']+Publics by Value[' ']+Rva\+Base", line):\r
+ if addressPatternGeneral.match(line):\r
status = 2\r
continue\r
- if re.match("^entry point at", line):\r
+ if line.startswith("entry point at"):\r
status = 3\r
- continue \r
+ continue\r
if status == 1 and len(line) != 0:\r
- m = secRe.match(line)\r
- assert m != None, "Fail to parse the section in map file , line is %s" % line\r
+ m = secReGeneral.match(line)\r
+ assert m is not None, "Fail to parse the section in map file , line is %s" % line\r
sec_no, sec_start, sec_length, sec_name, sec_class = m.groups(0)\r
secs.append([int(sec_no, 16), int(sec_start, 16), int(sec_length, 16), sec_name, sec_class])\r
if status == 2 and len(line) != 0:\r
for varname in varnames:\r
m = symRe.match(line)\r
- assert m != None, "Fail to parse the symbol in map file, line is %s" % line\r
+ assert m is not None, "Fail to parse the symbol in map file, line is %s" % line\r
sec_no, sym_offset, sym_name, vir_addr = m.groups(0)\r
sec_no = int(sec_no, 16)\r
sym_offset = int(sym_offset, 16)\r
vir_addr = int(vir_addr, 16)\r
+ # cannot pregenerate this RegEx since it uses varname from varnames.\r
m2 = re.match('^[_]*(%s)' % varname, sym_name)\r
- if m2 != None:\r
+ if m2 is not None:\r
# fond a binary pcd entry in map file\r
for sec in secs:\r
if sec[0] == sec_no and (sym_offset >= sec[1] and sym_offset < sec[1] + sec[2]):\r
\r
# get section information from efi file\r
efisecs = PeImageClass(efifilepath).SectionHeaderList\r
- if efisecs == None or len(efisecs) == 0:\r
+ if efisecs is None or len(efisecs) == 0:\r
return []\r
\r
ret = []\r
#\r
# A temporary INF is copied to database path which must have write permission\r
# The temporary will be removed at the end of build\r
- # In case of name conflict, the file name is \r
+ # In case of name conflict, the file name is\r
# FILE_GUIDBaseName (0D1B936F-68F3-4589-AFCC-FB8B7AEBC836module.inf)\r
#\r
TempFullPath = os.path.join(DbDir,\r
#\r
# To build same module more than once, the module path with FILE_GUID overridden has\r
# the file name FILE_GUIDmodule.inf, but the relative path (self.MetaFile.File) is the real path\r
- # in DSC which is used as relative path by C files and other files in INF. \r
+ # in DSC which is used as relative path by C files and other files in INF.\r
# A trick was used: all module paths are PathClass instances, after the initialization\r
# of PathClass, the PathClass.Path is overridden by the temporary INF path.\r
#\r
# If file exists, compare contents\r
#\r
if os.path.exists(TempFullPath):\r
- with open(str(Path), 'rb') as f1: Src = f1.read()\r
- with open(TempFullPath, 'rb') as f2: Dst = f2.read()\r
- if Src == Dst:\r
- return RtPath\r
- GlobalData.gTempInfs.append(TempFullPath)\r
+ with open(str(Path), 'rb') as f1, open(TempFullPath, 'rb') as f2:\r
+ if f1.read() == f2.read():\r
+ return RtPath\r
+ _TempInfs.append(TempFullPath)\r
shutil.copy2(str(Path), TempFullPath)\r
return RtPath\r
\r
-## Remove temporary created INFs whose paths were saved in gTempInfs\r
+## Remove temporary created INFs whose paths were saved in _TempInfs\r
#\r
def ClearDuplicatedInf():\r
- for File in GlobalData.gTempInfs:\r
+ while _TempInfs:\r
+ File = _TempInfs.pop()\r
if os.path.exists(File):\r
os.remove(File)\r
\r
-## callback routine for processing variable option\r
-#\r
-# This function can be used to process variable number of option values. The\r
-# typical usage of it is specify architecure list on command line.\r
-# (e.g. <tool> -a IA32 X64 IPF)\r
-#\r
-# @param Option Standard callback function parameter\r
-# @param OptionString Standard callback function parameter\r
-# @param Value Standard callback function parameter\r
-# @param Parser Standard callback function parameter\r
-#\r
-# @retval\r
-#\r
-def ProcessVariableArgument(Option, OptionString, Value, Parser):\r
- assert Value is None\r
- Value = []\r
- RawArgs = Parser.rargs\r
- while RawArgs:\r
- Arg = RawArgs[0]\r
- if (Arg[:2] == "--" and len(Arg) > 2) or \\r
- (Arg[:1] == "-" and len(Arg) > 1 and Arg[1] != "-"):\r
- break\r
- Value.append(Arg)\r
- del RawArgs[0]\r
- setattr(Parser.values, Option.dest, Value)\r
-\r
## Convert GUID string in xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx style to C structure style\r
#\r
# @param Guid The GUID string\r
# @retval string The GUID value in xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx format\r
#\r
def GuidStructureStringToGuidString(GuidValue):\r
+ if not GlobalData.gGuidCFormatPattern.match(GuidValue):\r
+ return ''\r
guidValueString = GuidValue.lower().replace("{", "").replace("}", "").replace(" ", "").replace(";", "")\r
guidValueList = guidValueString.split(",")\r
if len(guidValueList) != 11:\r
# @param Directory The directory name\r
#\r
def CreateDirectory(Directory):\r
- if Directory == None or Directory.strip() == "":\r
+ if Directory is None or Directory.strip() == "":\r
return True\r
try:\r
if not os.access(Directory, os.F_OK):\r
# @param Directory The directory name\r
#\r
def RemoveDirectory(Directory, Recursively=False):\r
- if Directory == None or Directory.strip() == "" or not os.path.exists(Directory):\r
+ if Directory is None or Directory.strip() == "" or not os.path.exists(Directory):\r
return\r
if Recursively:\r
CurrentDirectory = os.getcwd()\r
os.chdir(CurrentDirectory)\r
os.rmdir(Directory)\r
\r
-## Check if given file is changed or not\r
-#\r
-# This method is used to check if a file is changed or not between two build\r
-# actions. It makes use a cache to store files timestamp.\r
-#\r
-# @param File The path of file\r
-#\r
-# @retval True If the given file is changed, doesn't exist, or can't be\r
-# found in timestamp cache\r
-# @retval False If the given file is changed\r
-#\r
-def IsChanged(File):\r
- if not os.path.exists(File):\r
- return True\r
-\r
- FileState = os.stat(File)\r
- TimeStamp = FileState[-2]\r
-\r
- if File in gFileTimeStampCache and TimeStamp == gFileTimeStampCache[File]:\r
- FileChanged = False\r
- else:\r
- FileChanged = True\r
- gFileTimeStampCache[File] = TimeStamp\r
-\r
- return FileChanged\r
-\r
## Store content in file\r
#\r
# This method is used to save file only when its content is changed. This is\r
EdkLogger.error(None, PERMISSION_FAILURE, "Do not have write permission on directory %s" % DirName)\r
\r
try:\r
- if GlobalData.gIsWindows:\r
- try:\r
- from PyUtility import SaveFileToDisk\r
- if not SaveFileToDisk(File, Content):\r
- EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData=File)\r
- except:\r
- Fd = open(File, "wb")\r
- Fd.write(Content)\r
- Fd.close()\r
- else:\r
- Fd = open(File, "wb")\r
- Fd.write(Content)\r
- Fd.close()\r
- except IOError, X:\r
+ Fd = open(File, "wb")\r
+ Fd.write(Content)\r
+ Fd.close()\r
+ except IOError as X:\r
EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData='IOError %s' % X)\r
\r
return True\r
Fd = None\r
try:\r
Fd = open(File, 'wb')\r
- cPickle.dump(Data, Fd, cPickle.HIGHEST_PROTOCOL)\r
+ pickle.dump(Data, Fd, pickle.HIGHEST_PROTOCOL)\r
except:\r
EdkLogger.error("", FILE_OPEN_FAILURE, ExtraData=File, RaiseError=False)\r
finally:\r
- if Fd != None:\r
+ if Fd is not None:\r
Fd.close()\r
\r
## Restore a Python object from a file\r
Fd = None\r
try:\r
Fd = open(File, 'rb')\r
- Data = cPickle.load(Fd)\r
- except Exception, e:\r
+ Data = pickle.load(Fd)\r
+ except Exception as e:\r
EdkLogger.verbose("Failed to load [%s]\n\t%s" % (File, str(e)))\r
Data = None\r
finally:\r
- if Fd != None:\r
+ if Fd is not None:\r
Fd.close()\r
return Data\r
\r
return os.path.join(self._Root, self._UPPER_CACHE_[UpperPath])\r
return None\r
\r
-## Get all files of a directory\r
-#\r
-# @param Root: Root dir\r
-# @param SkipList : The files need be skipped\r
-#\r
-# @retval A list of all files\r
-#\r
-def GetFiles(Root, SkipList=None, FullPath=True):\r
- OriPath = Root\r
- FileList = []\r
- for Root, Dirs, Files in os.walk(Root):\r
- if SkipList:\r
- for Item in SkipList:\r
- if Item in Dirs:\r
- Dirs.remove(Item)\r
-\r
- for File in Files:\r
- File = os.path.normpath(os.path.join(Root, File))\r
- if not FullPath:\r
- File = File[len(OriPath) + 1:]\r
- FileList.append(File)\r
-\r
- return FileList\r
-\r
-## Check if gvien file exists or not\r
-#\r
-# @param File File name or path to be checked\r
-# @param Dir The directory the file is relative to\r
-#\r
-# @retval True if file exists\r
-# @retval False if file doesn't exists\r
-#\r
-def ValidFile(File, Ext=None):\r
- if Ext != None:\r
- Dummy, FileExt = os.path.splitext(File)\r
- if FileExt.lower() != Ext.lower():\r
- return False\r
- if not os.path.exists(File):\r
- return False\r
- return True\r
-\r
def RealPath(File, Dir='', OverrideDir=''):\r
NewFile = os.path.normpath(os.path.join(Dir, File))\r
NewFile = GlobalData.gAllFiles[NewFile]\r
\r
return None, None\r
\r
-## Check if gvien file exists or not\r
-#\r
-#\r
-def ValidFile2(AllFiles, File, Ext=None, Workspace='', EfiSource='', EdkSource='', Dir='.', OverrideDir=''):\r
- NewFile = File\r
- if Ext != None:\r
- Dummy, FileExt = os.path.splitext(File)\r
- if FileExt.lower() != Ext.lower():\r
- return False, File\r
-\r
- # Replace the Edk macros\r
- if OverrideDir != '' and OverrideDir != None:\r
- if OverrideDir.find('$(EFI_SOURCE)') > -1:\r
- OverrideDir = OverrideDir.replace('$(EFI_SOURCE)', EfiSource)\r
- if OverrideDir.find('$(EDK_SOURCE)') > -1:\r
- OverrideDir = OverrideDir.replace('$(EDK_SOURCE)', EdkSource)\r
-\r
- # Replace the default dir to current dir\r
- if Dir == '.':\r
- Dir = os.getcwd()\r
- Dir = Dir[len(Workspace) + 1:]\r
-\r
- # First check if File has Edk definition itself\r
- if File.find('$(EFI_SOURCE)') > -1 or File.find('$(EDK_SOURCE)') > -1:\r
- NewFile = File.replace('$(EFI_SOURCE)', EfiSource)\r
- NewFile = NewFile.replace('$(EDK_SOURCE)', EdkSource)\r
- NewFile = AllFiles[os.path.normpath(NewFile)]\r
- if NewFile != None:\r
- return True, NewFile\r
-\r
- # Second check the path with override value\r
- if OverrideDir != '' and OverrideDir != None:\r
- NewFile = AllFiles[os.path.normpath(os.path.join(OverrideDir, File))]\r
- if NewFile != None:\r
- return True, NewFile\r
-\r
- # Last check the path with normal definitions\r
- File = os.path.join(Dir, File)\r
- NewFile = AllFiles[os.path.normpath(File)]\r
- if NewFile != None:\r
- return True, NewFile\r
-\r
- return False, File\r
-\r
-## Check if gvien file exists or not\r
-#\r
-#\r
-def ValidFile3(AllFiles, File, Workspace='', EfiSource='', EdkSource='', Dir='.', OverrideDir=''):\r
- # Replace the Edk macros\r
- if OverrideDir != '' and OverrideDir != None:\r
- if OverrideDir.find('$(EFI_SOURCE)') > -1:\r
- OverrideDir = OverrideDir.replace('$(EFI_SOURCE)', EfiSource)\r
- if OverrideDir.find('$(EDK_SOURCE)') > -1:\r
- OverrideDir = OverrideDir.replace('$(EDK_SOURCE)', EdkSource)\r
-\r
- # Replace the default dir to current dir\r
- # Dir is current module dir related to workspace\r
- if Dir == '.':\r
- Dir = os.getcwd()\r
- Dir = Dir[len(Workspace) + 1:]\r
-\r
- NewFile = File\r
- RelaPath = AllFiles[os.path.normpath(Dir)]\r
- NewRelaPath = RelaPath\r
-\r
- while(True):\r
- # First check if File has Edk definition itself\r
- if File.find('$(EFI_SOURCE)') > -1 or File.find('$(EDK_SOURCE)') > -1:\r
- File = File.replace('$(EFI_SOURCE)', EfiSource)\r
- File = File.replace('$(EDK_SOURCE)', EdkSource)\r
- NewFile = AllFiles[os.path.normpath(File)]\r
- if NewFile != None:\r
- NewRelaPath = os.path.dirname(NewFile)\r
- File = os.path.basename(NewFile)\r
- #NewRelaPath = NewFile[:len(NewFile) - len(File.replace("..\\", '').replace("../", '')) - 1]\r
- break\r
-\r
- # Second check the path with override value\r
- if OverrideDir != '' and OverrideDir != None:\r
- NewFile = AllFiles[os.path.normpath(os.path.join(OverrideDir, File))]\r
- if NewFile != None:\r
- #NewRelaPath = os.path.dirname(NewFile)\r
- NewRelaPath = NewFile[:len(NewFile) - len(File.replace("..\\", '').replace("../", '')) - 1]\r
- break\r
-\r
- # Last check the path with normal definitions\r
- NewFile = AllFiles[os.path.normpath(os.path.join(Dir, File))]\r
- if NewFile != None:\r
- break\r
-\r
- # No file found\r
- break\r
-\r
- return NewRelaPath, RelaPath, File\r
-\r
-\r
-def GetRelPath(Path1, Path2):\r
- FileName = os.path.basename(Path2)\r
- L1 = os.path.normpath(Path1).split(os.path.normpath('/'))\r
- L2 = os.path.normpath(Path2).split(os.path.normpath('/'))\r
- for Index in range(0, len(L1)):\r
- if L1[Index] != L2[Index]:\r
- FileName = '../' * (len(L1) - Index)\r
- for Index2 in range(Index, len(L2)):\r
- FileName = os.path.join(FileName, L2[Index2])\r
- break\r
- return os.path.normpath(FileName)\r
-\r
-\r
## Get GUID value from given packages\r
#\r
# @param CName The CName of the GUID\r
GuidKeys = P.Guids.keys()\r
if Inffile and P._PrivateGuids:\r
if not Inffile.startswith(P.MetaFile.Dir):\r
- GuidKeys = (dict.fromkeys(x for x in P.Guids if x not in P._PrivateGuids)).keys()\r
+ GuidKeys = [x for x in P.Guids if x not in P._PrivateGuids]\r
if CName in GuidKeys:\r
return P.Guids[CName]\r
return None\r
ProtocolKeys = P.Protocols.keys()\r
if Inffile and P._PrivateProtocols:\r
if not Inffile.startswith(P.MetaFile.Dir):\r
- ProtocolKeys = (dict.fromkeys(x for x in P.Protocols if x not in P._PrivateProtocols)).keys()\r
+ ProtocolKeys = [x for x in P.Protocols if x not in P._PrivateProtocols]\r
if CName in ProtocolKeys:\r
return P.Protocols[CName]\r
return None\r
PpiKeys = P.Ppis.keys()\r
if Inffile and P._PrivatePpis:\r
if not Inffile.startswith(P.MetaFile.Dir):\r
- PpiKeys = (dict.fromkeys(x for x in P.Ppis if x not in P._PrivatePpis)).keys()\r
+ PpiKeys = [x for x in P.Ppis if x not in P._PrivatePpis]\r
if CName in PpiKeys:\r
return P.Ppis[CName]\r
return None\r
def Append(self, AppendString, Dictionary=None):\r
if Dictionary:\r
SectionList = self._Parse(AppendString)\r
- self.String += "".join([S.Instantiate(Dictionary) for S in SectionList])\r
+ self.String += "".join(S.Instantiate(Dictionary) for S in SectionList)\r
else:\r
self.String += AppendString\r
\r
# @retval str The string replaced with placeholder values\r
#\r
def Replace(self, Dictionary=None):\r
- return "".join([S.Instantiate(Dictionary) for S in self._TemplateSectionList])\r
+ return "".join(S.Instantiate(Dictionary) for S in self._TemplateSectionList)\r
\r
## Progress indicator class\r
#\r
self.CodaMessage = CloseMessage\r
self.ProgressChar = ProgressChar\r
self.Interval = Interval\r
- if Progressor._StopFlag == None:\r
+ if Progressor._StopFlag is None:\r
Progressor._StopFlag = threading.Event()\r
\r
## Start to print progress charater\r
# @param OpenMessage The string printed before progress charaters\r
#\r
def Start(self, OpenMessage=None):\r
- if OpenMessage != None:\r
+ if OpenMessage is not None:\r
self.PromptMessage = OpenMessage\r
Progressor._StopFlag.clear()\r
- if Progressor._ProgressThread == None:\r
+ if Progressor._ProgressThread is None:\r
Progressor._ProgressThread = threading.Thread(target=self._ProgressThreadEntry)\r
Progressor._ProgressThread.setDaemon(False)\r
Progressor._ProgressThread.start()\r
#\r
def Stop(self, CloseMessage=None):\r
OriginalCodaMessage = self.CodaMessage\r
- if CloseMessage != None:\r
+ if CloseMessage is not None:\r
self.CodaMessage = CloseMessage\r
self.Abort()\r
self.CodaMessage = OriginalCodaMessage\r
## Abort the progress display\r
@staticmethod\r
def Abort():\r
- if Progressor._StopFlag != None:\r
+ if Progressor._StopFlag is not None:\r
Progressor._StopFlag.set()\r
- if Progressor._ProgressThread != None:\r
+ if Progressor._ProgressThread is not None:\r
Progressor._ProgressThread.join()\r
Progressor._ProgressThread = None\r
\r
return key, value\r
\r
def update(self, dict=None, **kwargs):\r
- if dict != None:\r
+ if dict is not None:\r
for k, v in dict.items():\r
self[k] = v\r
if len(kwargs):\r
_ListType = type([])\r
_TupleType = type(())\r
_Wildcard = 'COMMON'\r
- _ValidWildcardList = ['COMMON', 'DEFAULT', 'ALL', '*', 'PLATFORM']\r
+ _ValidWildcardList = ['COMMON', 'DEFAULT', 'ALL', TAB_STAR, 'PLATFORM']\r
\r
def __init__(self, _Single_=False, _Level_=2):\r
self._Level_ = _Level_\r
if self._Level_ > 1:\r
RestKeys = [self._Wildcard for i in range(0, self._Level_ - 1)]\r
\r
- if FirstKey == None or str(FirstKey).upper() in self._ValidWildcardList:\r
+ if FirstKey is None or str(FirstKey).upper() in self._ValidWildcardList:\r
FirstKey = self._Wildcard\r
\r
if self._Single_:\r
if FirstKey == self._Wildcard:\r
if FirstKey in self.data:\r
Value = self.data[FirstKey][RestKeys]\r
- if Value == None:\r
+ if Value is None:\r
for Key in self.data:\r
Value = self.data[Key][RestKeys]\r
- if Value != None: break\r
+ if Value is not None: break\r
else:\r
if FirstKey in self.data:\r
Value = self.data[FirstKey][RestKeys]\r
- if Value == None and self._Wildcard in self.data:\r
+ if Value is None and self._Wildcard in self.data:\r
#print "Value=None"\r
Value = self.data[self._Wildcard][RestKeys]\r
else:\r
if FirstKey == self._Wildcard:\r
if FirstKey in self.data:\r
Value = self.data[FirstKey]\r
- if Value == None:\r
+ if Value is None:\r
for Key in self.data:\r
Value = self.data[Key]\r
- if Value != None: break\r
+ if Value is not None: break\r
else:\r
if FirstKey in self.data:\r
Value = self.data[FirstKey]\r
keys |= self.data[Key].GetKeys(KeyIndex - 1)\r
return keys\r
\r
-## Boolean chain list\r
-#\r
-class Blist(UserList):\r
- def __init__(self, initlist=None):\r
- UserList.__init__(self, initlist)\r
- def __setitem__(self, i, item):\r
- if item not in [True, False]:\r
- if item == 0:\r
- item = False\r
- else:\r
- item = True\r
- self.data[i] = item\r
- def _GetResult(self):\r
- Value = True\r
- for item in self.data:\r
- Value &= item\r
- return Value\r
- Result = property(_GetResult)\r
-\r
-def ParseConsoleLog(Filename):\r
- Opr = open(os.path.normpath(Filename), 'r')\r
- Opw = open(os.path.normpath(Filename + '.New'), 'w+')\r
- for Line in Opr.readlines():\r
- if Line.find('.efi') > -1:\r
- Line = Line[Line.rfind(' ') : Line.rfind('.efi')].strip()\r
- Opw.write('%s\n' % Line)\r
-\r
- Opr.close()\r
- Opw.close()\r
+def IsFieldValueAnArray (Value):\r
+ Value = Value.strip()\r
+ if Value.startswith(TAB_GUID) and Value.endswith(')'):\r
+ return True\r
+ if Value.startswith('L"') and Value.endswith('"') and len(list(Value[2:-1])) > 1:\r
+ return True\r
+ if Value[0] == '"' and Value[-1] == '"' and len(list(Value[1:-1])) > 1:\r
+ return True\r
+ if Value[0] == '{' and Value[-1] == '}':\r
+ return True\r
+ if Value.startswith("L'") and Value.endswith("'") and len(list(Value[2:-1])) > 1:\r
+ return True\r
+ if Value[0] == "'" and Value[-1] == "'" and len(list(Value[1:-1])) > 1:\r
+ return True\r
+ return False\r
\r
def AnalyzePcdExpression(Setting):\r
- Setting = Setting.strip()\r
- # There might be escaped quote in a string: \", \\\"\r
- Data = Setting.replace('\\\\', '//').replace('\\\"', '\\\'')\r
+ RanStr = ''.join(sample(string.ascii_letters + string.digits, 8))\r
+ Setting = Setting.replace('\\\\', RanStr).strip()\r
+ # There might be escaped quote in a string: \", \\\" , \', \\\'\r
+ Data = Setting\r
# There might be '|' in string and in ( ... | ... ), replace it with '-'\r
NewStr = ''\r
- InStr = False\r
+ InSingleQuoteStr = False\r
+ InDoubleQuoteStr = False\r
Pair = 0\r
- for ch in Data:\r
- if ch == '"':\r
- InStr = not InStr\r
- elif ch == '(' and not InStr:\r
+ for Index, ch in enumerate(Data):\r
+ if ch == '"' and not InSingleQuoteStr:\r
+ if Data[Index - 1] != '\\':\r
+ InDoubleQuoteStr = not InDoubleQuoteStr\r
+ elif ch == "'" and not InDoubleQuoteStr:\r
+ if Data[Index - 1] != '\\':\r
+ InSingleQuoteStr = not InSingleQuoteStr\r
+ elif ch == '(' and not (InSingleQuoteStr or InDoubleQuoteStr):\r
Pair += 1\r
- elif ch == ')' and not InStr:\r
+ elif ch == ')' and not (InSingleQuoteStr or InDoubleQuoteStr):\r
Pair -= 1\r
\r
- if (Pair > 0 or InStr) and ch == TAB_VALUE_SPLIT:\r
+ if (Pair > 0 or InSingleQuoteStr or InDoubleQuoteStr) and ch == TAB_VALUE_SPLIT:\r
NewStr += '-'\r
else:\r
NewStr += ch\r
break\r
FieldList.append(Setting[StartPos:Pos].strip())\r
StartPos = Pos + 1\r
-\r
+ for i, ch in enumerate(FieldList):\r
+ if RanStr in ch:\r
+ FieldList[i] = ch.replace(RanStr,'\\\\')\r
return FieldList\r
\r
+def ParseDevPathValue (Value):\r
+ if '\\' in Value:\r
+ Value.replace('\\', '/').replace(' ', '')\r
+\r
+ Cmd = 'DevicePath ' + '"' + Value + '"'\r
+ try:\r
+ p = subprocess.Popen(Cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\r
+ out, err = p.communicate()\r
+ except Exception as X:\r
+ raise BadExpression("DevicePath: %s" % (str(X)) )\r
+ finally:\r
+ subprocess._cleanup()\r
+ p.stdout.close()\r
+ p.stderr.close()\r
+ if err:\r
+ raise BadExpression("DevicePath: %s" % str(err))\r
+ Size = len(out.split())\r
+ out = ','.join(out.split())\r
+ return '{' + out + '}', Size\r
+\r
def ParseFieldValue (Value):\r
- if type(Value) == type(0):\r
- return Value, (Value.bit_length() + 7) / 8\r
- if type(Value) <> type(''):\r
- raise ValueError\r
- Value = Value.strip()\r
- if Value.startswith('UINT8') and Value.endswith(')'):\r
- Value, Size = ParseFieldValue(Value.split('(', 1)[1][:-1])\r
- if Size > 1:\r
- raise ValueError\r
+ if isinstance(Value, type(0)):\r
+ return Value, (Value.bit_length() + 7) / 8\r
+ if not isinstance(Value, type('')):\r
+ raise BadExpression('Type %s is %s' %(Value, type(Value)))\r
+ Value = Value.strip()\r
+ if Value.startswith(TAB_UINT8) and Value.endswith(')'):\r
+ Value, Size = ParseFieldValue(Value.split('(', 1)[1][:-1])\r
+ if Size > 1:\r
+ raise BadExpression('Value (%s) Size larger than %d' %(Value, Size))\r
+ return Value, 1\r
+ if Value.startswith(TAB_UINT16) and Value.endswith(')'):\r
+ Value, Size = ParseFieldValue(Value.split('(', 1)[1][:-1])\r
+ if Size > 2:\r
+ raise BadExpression('Value (%s) Size larger than %d' %(Value, Size))\r
+ return Value, 2\r
+ if Value.startswith(TAB_UINT32) and Value.endswith(')'):\r
+ Value, Size = ParseFieldValue(Value.split('(', 1)[1][:-1])\r
+ if Size > 4:\r
+ raise BadExpression('Value (%s) Size larger than %d' %(Value, Size))\r
+ return Value, 4\r
+ if Value.startswith(TAB_UINT64) and Value.endswith(')'):\r
+ Value, Size = ParseFieldValue(Value.split('(', 1)[1][:-1])\r
+ if Size > 8:\r
+ raise BadExpression('Value (%s) Size larger than %d' % (Value, Size))\r
+ return Value, 8\r
+ if Value.startswith(TAB_GUID) and Value.endswith(')'):\r
+ Value = Value.split('(', 1)[1][:-1].strip()\r
+ if Value[0] == '{' and Value[-1] == '}':\r
+ TmpValue = GuidStructureStringToGuidString(Value)\r
+ if not TmpValue:\r
+ raise BadExpression("Invalid GUID value string %s" % Value)\r
+ Value = TmpValue\r
+ if Value[0] == '"' and Value[-1] == '"':\r
+ Value = Value[1:-1]\r
+ try:\r
+ Value = "'" + uuid.UUID(Value).get_bytes_le() + "'"\r
+ except ValueError as Message:\r
+ raise BadExpression(Message)\r
+ Value, Size = ParseFieldValue(Value)\r
+ return Value, 16\r
+ if Value.startswith('L"') and Value.endswith('"'):\r
+ # Unicode String\r
+ # translate escape character\r
+ Value = Value[1:]\r
+ try:\r
+ Value = eval(Value)\r
+ except:\r
+ Value = Value[1:-1]\r
+ List = list(Value)\r
+ List.reverse()\r
+ Value = 0\r
+ for Char in List:\r
+ Value = (Value << 16) | ord(Char)\r
+ return Value, (len(List) + 1) * 2\r
+ if Value.startswith('"') and Value.endswith('"'):\r
+ # ASCII String\r
+ # translate escape character\r
+ try:\r
+ Value = eval(Value)\r
+ except:\r
+ Value = Value[1:-1]\r
+ List = list(Value)\r
+ List.reverse()\r
+ Value = 0\r
+ for Char in List:\r
+ Value = (Value << 8) | ord(Char)\r
+ return Value, len(List) + 1\r
+ if Value.startswith("L'") and Value.endswith("'"):\r
+ # Unicode Character Constant\r
+ # translate escape character\r
+ Value = Value[1:]\r
+ try:\r
+ Value = eval(Value)\r
+ except:\r
+ Value = Value[1:-1]\r
+ List = list(Value)\r
+ if len(List) == 0:\r
+ raise BadExpression('Length %s is %s' % (Value, len(List)))\r
+ List.reverse()\r
+ Value = 0\r
+ for Char in List:\r
+ Value = (Value << 16) | ord(Char)\r
+ return Value, len(List) * 2\r
+ if Value.startswith("'") and Value.endswith("'"):\r
+ # Character constant\r
+ # translate escape character\r
+ try:\r
+ Value = eval(Value)\r
+ except:\r
+ Value = Value[1:-1]\r
+ List = list(Value)\r
+ if len(List) == 0:\r
+ raise BadExpression('Length %s is %s' % (Value, len(List)))\r
+ List.reverse()\r
+ Value = 0\r
+ for Char in List:\r
+ Value = (Value << 8) | ord(Char)\r
+ return Value, len(List)\r
+ if Value.startswith('{') and Value.endswith('}'):\r
+ # Byte array\r
+ Value = Value[1:-1]\r
+ List = [Item.strip() for Item in Value.split(',')]\r
+ List.reverse()\r
+ Value = 0\r
+ RetSize = 0\r
+ for Item in List:\r
+ ItemValue, Size = ParseFieldValue(Item)\r
+ RetSize += Size\r
+ for I in range(Size):\r
+ Value = (Value << 8) | ((ItemValue >> 8 * I) & 0xff)\r
+ return Value, RetSize\r
+ if Value.startswith('DEVICE_PATH(') and Value.endswith(')'):\r
+ Value = Value.replace("DEVICE_PATH(", '').rstrip(')')\r
+ Value = Value.strip().strip('"')\r
+ return ParseDevPathValue(Value)\r
+ if Value.lower().startswith('0x'):\r
+ try:\r
+ Value = int(Value, 16)\r
+ except:\r
+ raise BadExpression("invalid hex value: %s" % Value)\r
+ if Value == 0:\r
+ return 0, 1\r
+ return Value, (Value.bit_length() + 7) / 8\r
+ if Value[0].isdigit():\r
+ Value = int(Value, 10)\r
+ if Value == 0:\r
+ return 0, 1\r
+ return Value, (Value.bit_length() + 7) / 8\r
+ if Value.lower() == 'true':\r
+ return 1, 1\r
+ if Value.lower() == 'false':\r
+ return 0, 1\r
return Value, 1\r
- if Value.startswith('UINT16') and Value.endswith(')'):\r
- Value, Size = ParseFieldValue(Value.split('(', 1)[1][:-1])\r
- if Size > 2:\r
- raise ValueError\r
- return Value, 2\r
- if Value.startswith('UINT32') and Value.endswith(')'):\r
- Value, Size = ParseFieldValue(Value.split('(', 1)[1][:-1])\r
- if Size > 4:\r
- raise ValueError\r
- return Value, 4\r
- if Value.startswith('UINT64') and Value.endswith(')'):\r
- Value, Size = ParseFieldValue(Value.split('(', 1)[1][:-1])\r
- if Size > 8:\r
- raise ValueError\r
- return Value, 8\r
- if Value.startswith('GUID') and Value.endswith(')'):\r
- Value = Value.split('(', 1)[1][:-1].strip()\r
- if Value[0] == '{' and Value[-1] == '}':\r
- Value = Value[1:-1].strip()\r
- Value = Value.split('{', 1)\r
- Value = [Item.strip()[2:] for Item in (Value[0] + Value[1][:-1]).split(',')]\r
- Value = '-'.join(Value[0:3]) + '-' + ''.join(Value[3:5]) + '-' + ''.join(Value[5:11])\r
- if Value[0] == '"' and Value[-1] == '"':\r
- Value = Value[1:-1]\r
- Value = "'" + uuid.UUID(Value).get_bytes_le() + "'"\r
- Value, Size = ParseFieldValue(Value)\r
- return Value, 16\r
- if Value.startswith('L"') and Value.endswith('"'):\r
- # Unicode String\r
- List = list(Value[2:-1])\r
- List.reverse()\r
- Value = 0\r
- for Char in List:\r
- Value = (Value << 16) | ord(Char)\r
- return Value, (len(List) + 1) * 2\r
- if Value.startswith('"') and Value.endswith('"'):\r
- # ASCII String\r
- List = list(Value[1:-1])\r
- List.reverse()\r
- Value = 0\r
- for Char in List:\r
- Value = (Value << 8) | ord(Char)\r
- return Value, len(List) + 1\r
- if Value.startswith("L'") and Value.endswith("'"):\r
- # Unicode Character Constant\r
- List = list(Value[2:-1])\r
- List.reverse()\r
- Value = 0\r
- for Char in List:\r
- Value = (Value << 16) | ord(Char)\r
- return Value, len(List) * 2\r
- if Value.startswith("'") and Value.endswith("'"):\r
- # Character constant\r
- List = list(Value[1:-1])\r
- List.reverse()\r
- Value = 0\r
- for Char in List:\r
- Value = (Value << 8) | ord(Char)\r
- return Value, len(List)\r
- if Value.startswith('{') and Value.endswith('}'):\r
- # Byte array\r
- Value = Value[1:-1]\r
- List = [Item.strip() for Item in Value.split(',')]\r
- List.reverse()\r
- Value = 0\r
- for Item in List:\r
- ItemValue, Size = ParseFieldValue(Item)\r
- if Size > 1:\r
- raise ValueError\r
- Value = (Value << 8) | ItemValue\r
- return Value, len(List)\r
- if Value.lower().startswith('0x'):\r
- Value = int(Value, 16)\r
- return Value, (Value.bit_length() + 7) / 8\r
- if Value[0].isdigit():\r
- Value = int(Value, 10)\r
- return Value, (Value.bit_length() + 7) / 8\r
- if Value.lower() == 'true':\r
- return 1, 1\r
- if Value.lower() == 'false':\r
- return 0, 1\r
- return Value, 1\r
\r
## AnalyzeDscPcd\r
#\r
# Analyze DSC PCD value, since there is no data type info in DSC\r
-# This fuction is used to match functions (AnalyzePcdData, AnalyzeHiiPcdData, AnalyzeVpdPcdData) used for retrieving PCD value from database\r
+# This function is used to match functions (AnalyzePcdData) used for retrieving PCD value from database\r
# 1. Feature flag: TokenSpace.PcdCName|PcdValue\r
-# 2. Fix and Patch:TokenSpace.PcdCName|PcdValue[|MaxSize]\r
+# 2. Fix and Patch:TokenSpace.PcdCName|PcdValue[|VOID*[|MaxSize]]\r
# 3. Dynamic default:\r
# TokenSpace.PcdCName|PcdValue[|VOID*[|MaxSize]]\r
# TokenSpace.PcdCName|PcdValue\r
# TokenSpace.PcdCName|VpdOffset[|VpdValue]\r
# TokenSpace.PcdCName|VpdOffset[|MaxSize[|VpdValue]]\r
# 5. Dynamic HII:\r
-# TokenSpace.PcdCName|HiiString|VaiableGuid|VariableOffset[|HiiValue]\r
+# TokenSpace.PcdCName|HiiString|VariableGuid|VariableOffset[|HiiValue]\r
# PCD value needs to be located in such kind of string, and the PCD value might be an expression in which\r
# there might have "|" operator, also in string value.\r
#\r
FieldList = AnalyzePcdExpression(Setting)\r
\r
IsValid = True\r
- if PcdType in (MODEL_PCD_FIXED_AT_BUILD, MODEL_PCD_PATCHABLE_IN_MODULE, MODEL_PCD_FEATURE_FLAG):\r
+ if PcdType in (MODEL_PCD_FIXED_AT_BUILD, MODEL_PCD_PATCHABLE_IN_MODULE, MODEL_PCD_DYNAMIC_DEFAULT, MODEL_PCD_DYNAMIC_EX_DEFAULT):\r
Value = FieldList[0]\r
Size = ''\r
- if len(FieldList) > 1:\r
- if FieldList[1].upper().startswith("0X") or FieldList[1].isdigit():\r
- Size = FieldList[1]\r
- else:\r
- DataType = FieldList[1]\r
-\r
+ if len(FieldList) > 1 and FieldList[1]:\r
+ DataType = FieldList[1]\r
+ if FieldList[1] != TAB_VOID and StructPattern.match(FieldList[1]) is None:\r
+ IsValid = False\r
if len(FieldList) > 2:\r
Size = FieldList[2]\r
- if DataType == "":\r
- IsValid = (len(FieldList) <= 1)\r
- else:\r
- IsValid = (len(FieldList) <= 3)\r
-# Value, Size = ParseFieldValue(Value)\r
- return [str(Value), '', str(Size)], IsValid, 0\r
- elif PcdType in (MODEL_PCD_DYNAMIC_DEFAULT, MODEL_PCD_DYNAMIC_EX_DEFAULT):\r
+ if IsValid:\r
+ if DataType == "":\r
+ IsValid = (len(FieldList) <= 1)\r
+ else:\r
+ IsValid = (len(FieldList) <= 3)\r
+\r
+ if Size:\r
+ try:\r
+ int(Size, 16) if Size.upper().startswith("0X") else int(Size)\r
+ except:\r
+ IsValid = False\r
+ Size = -1\r
+ return [str(Value), DataType, str(Size)], IsValid, 0\r
+ elif PcdType == MODEL_PCD_FEATURE_FLAG:\r
Value = FieldList[0]\r
- Size = Type = ''\r
- if len(FieldList) > 1:\r
- Type = FieldList[1]\r
- else:\r
- Type = DataType\r
- if len(FieldList) > 2:\r
- Size = FieldList[2]\r
- else:\r
- if Type == 'VOID*':\r
- if Value.startswith("L"):\r
- Size = str((len(Value)- 3 + 1) * 2)\r
- elif Value.startswith("{"):\r
- Size = str(len(Value.split(",")))\r
- else:\r
- Size = str(len(Value) -2 + 1 )\r
- if DataType == "":\r
- IsValid = (len(FieldList) <= 1)\r
- else:\r
- IsValid = (len(FieldList) <= 3)\r
- return [Value, Type, Size], IsValid, 0\r
+ Size = ''\r
+ IsValid = (len(FieldList) <= 1)\r
+ return [Value, DataType, str(Size)], IsValid, 0\r
elif PcdType in (MODEL_PCD_DYNAMIC_VPD, MODEL_PCD_DYNAMIC_EX_VPD):\r
VpdOffset = FieldList[0]\r
Value = Size = ''\r
- if not DataType == 'VOID*':\r
+ if not DataType == TAB_VOID:\r
if len(FieldList) > 1:\r
Value = FieldList[1]\r
else:\r
IsValid = (len(FieldList) <= 1)\r
else:\r
IsValid = (len(FieldList) <= 3)\r
-\r
- return [VpdOffset, Size, Value], IsValid, 2\r
+ if Size:\r
+ try:\r
+ int(Size, 16) if Size.upper().startswith("0X") else int(Size)\r
+ except:\r
+ IsValid = False\r
+ Size = -1\r
+ return [VpdOffset, str(Size), Value], IsValid, 2\r
elif PcdType in (MODEL_PCD_DYNAMIC_HII, MODEL_PCD_DYNAMIC_EX_HII):\r
+ IsValid = (3 <= len(FieldList) <= 5)\r
HiiString = FieldList[0]\r
Guid = Offset = Value = Attribute = ''\r
if len(FieldList) > 1:\r
Offset = FieldList[2]\r
if len(FieldList) > 3:\r
Value = FieldList[3]\r
+ if not Value:\r
+ IsValid = False\r
if len(FieldList) > 4:\r
Attribute = FieldList[4]\r
- IsValid = (3 <= len(FieldList) <= 5)\r
return [HiiString, Guid, Offset, Value, Attribute], IsValid, 3\r
return [], False, 0\r
\r
# Used to avoid split issue while the value string contain "|" character\r
#\r
# @param[in] Setting: A String contain value/datum type/token number information;\r
-# \r
-# @retval ValueList: A List contain value, datum type and toke number. \r
+#\r
+# @retval ValueList: A List contain value, datum type and toke number.\r
#\r
def AnalyzePcdData(Setting):\r
ValueList = ['', '', '']\r
\r
ValueRe = re.compile(r'^\s*L?\".*\|.*\"')\r
PtrValue = ValueRe.findall(Setting)\r
- \r
+\r
ValueUpdateFlag = False\r
- \r
+\r
if len(PtrValue) >= 1:\r
Setting = re.sub(ValueRe, '', Setting)\r
ValueUpdateFlag = True\r
\r
TokenList = Setting.split(TAB_VALUE_SPLIT)\r
ValueList[0:len(TokenList)] = TokenList\r
- \r
+\r
if ValueUpdateFlag:\r
ValueList[0] = PtrValue[0]\r
- \r
- return ValueList \r
- \r
-## AnalyzeHiiPcdData\r
-#\r
-# Analyze the pcd Value, variable name, variable Guid and variable offset.\r
-# Used to avoid split issue while the value string contain "|" character\r
-#\r
-# @param[in] Setting: A String contain VariableName, VariableGuid, VariableOffset, DefaultValue information;\r
-# \r
-# @retval ValueList: A List contaian VariableName, VariableGuid, VariableOffset, DefaultValue. \r
-#\r
-def AnalyzeHiiPcdData(Setting):\r
- ValueList = ['', '', '', '']\r
-\r
- TokenList = GetSplitValueList(Setting)\r
- ValueList[0:len(TokenList)] = TokenList\r
\r
return ValueList\r
\r
-## AnalyzeVpdPcdData\r
-#\r
-# Analyze the vpd pcd VpdOffset, MaxDatumSize and InitialValue.\r
-# Used to avoid split issue while the value string contain "|" character\r
-#\r
-# @param[in] Setting: A String contain VpdOffset/MaxDatumSize/InitialValue information;\r
-# \r
-# @retval ValueList: A List contain VpdOffset, MaxDatumSize and InitialValue. \r
-#\r
-def AnalyzeVpdPcdData(Setting):\r
- ValueList = ['', '', '']\r
-\r
- ValueRe = re.compile(r'\s*L?\".*\|.*\"\s*$')\r
- PtrValue = ValueRe.findall(Setting)\r
- \r
- ValueUpdateFlag = False\r
- \r
- if len(PtrValue) >= 1:\r
- Setting = re.sub(ValueRe, '', Setting)\r
- ValueUpdateFlag = True\r
-\r
- TokenList = Setting.split(TAB_VALUE_SPLIT)\r
- ValueList[0:len(TokenList)] = TokenList\r
- \r
- if ValueUpdateFlag:\r
- ValueList[2] = PtrValue[0]\r
- \r
- return ValueList \r
-\r
## check format of PCD value against its the datum type\r
#\r
# For PCD value setting\r
#\r
def CheckPcdDatum(Type, Value):\r
- if Type == "VOID*":\r
+ if Type == TAB_VOID:\r
ValueRe = re.compile(r'\s*L?\".*\"\s*$')\r
if not (((Value.startswith('L"') or Value.startswith('"')) and Value.endswith('"'))\r
- or (Value.startswith('{') and Value.endswith('}'))\r
+ or (Value.startswith('{') and Value.endswith('}')) or (Value.startswith("L'") or Value.startswith("'") and Value.endswith("'"))\r
):\r
return False, "Invalid value [%s] of type [%s]; must be in the form of {...} for array"\\r
- ", or \"...\" for string, or L\"...\" for unicode string" % (Value, Type)\r
+ ", \"...\" or \'...\' for string, L\"...\" or L\'...\' for unicode string" % (Value, Type)\r
elif ValueRe.match(Value):\r
# Check the chars in UnicodeString or CString is printable\r
if Value.startswith("L"):\r
Printset.add(TAB_PRINTCHAR_BS)\r
Printset.add(TAB_PRINTCHAR_NUL)\r
if not set(Value).issubset(Printset):\r
- PrintList = list(Printset)\r
- PrintList.sort()\r
+ PrintList = sorted(Printset)\r
return False, "Invalid PCD string value of type [%s]; must be printable chars %s." % (Type, PrintList)\r
elif Type == 'BOOLEAN':\r
if Value not in ['TRUE', 'True', 'true', '0x1', '0x01', '1', 'FALSE', 'False', 'false', '0x0', '0x00', '0']:\r
return False, "Invalid value [%s] of type [%s]; must be one of TRUE, True, true, 0x1, 0x01, 1"\\r
", FALSE, False, false, 0x0, 0x00, 0" % (Value, Type)\r
elif Type in [TAB_UINT8, TAB_UINT16, TAB_UINT32, TAB_UINT64]:\r
+ if Value and int(Value, 0) < 0:\r
+ return False, "PCD can't be set to negative value[%s] for datum type [%s]" % (Value, Type)\r
try:\r
Value = long(Value, 0)\r
+ if Value > MAX_VAL_TYPE[Type]:\r
+ return False, "Too large PCD value[%s] for datum type [%s]" % (Value, Type)\r
except:\r
return False, "Invalid value [%s] of type [%s];"\\r
" must be a hexadecimal, decimal or octal in C language format." % (Value, Type)\r
\r
Value = eval(Value) # translate escape character\r
NewValue = '{'\r
- for Index in range(0,len(Value)):\r
+ for Index in range(0, len(Value)):\r
if Unicode:\r
NewValue = NewValue + str(ord(Value[Index]) % 0x10000) + ','\r
else:\r
self.ToolCode = ToolCode\r
self.ToolChainFamily = ToolChainFamily\r
\r
- self._Key = None\r
-\r
## Convert the object of this class to a string\r
#\r
# Convert member Path of the class to a string\r
# @retval True The two PathClass are the same\r
#\r
def __eq__(self, Other):\r
- if type(Other) == type(self):\r
+ if isinstance(Other, type(self)):\r
return self.Path == Other.Path\r
else:\r
return self.Path == str(Other)\r
# @retval -1 The first PathClass is less than the second PathClass\r
# @retval 1 The first PathClass is Bigger than the second PathClass\r
def __cmp__(self, Other):\r
- if type(Other) == type(self):\r
+ if isinstance(Other, type(self)):\r
OtherKey = Other.Path\r
else:\r
OtherKey = str(Other)\r
- \r
+\r
SelfKey = self.Path\r
if SelfKey == OtherKey:\r
return 0\r
def __hash__(self):\r
return hash(self.Path)\r
\r
- def _GetFileKey(self):\r
- if self._Key == None:\r
- self._Key = self.Path.upper() # + self.ToolChainFamily + self.TagName + self.ToolCode + self.Target\r
- return self._Key\r
+ @cached_property\r
+ def Key(self):\r
+ return self.Path.upper()\r
\r
- def _GetTimeStamp(self):\r
+ @property\r
+ def TimeStamp(self):\r
return os.stat(self.Path)[8]\r
\r
def Validate(self, Type='', CaseSensitive=True):\r
self.Path = os.path.join(RealRoot, RealFile)\r
return ErrorCode, ErrorInfo\r
\r
- Key = property(_GetFileKey)\r
- TimeStamp = property(_GetTimeStamp)\r
-\r
## Parse PE image to get the required PE informaion.\r
#\r
class PeImageClass():\r
def _ByteListToStr(self, ByteList):\r
String = ''\r
for index in range(len(ByteList)):\r
- if ByteList[index] == 0: \r
+ if ByteList[index] == 0:\r
break\r
String += chr(ByteList[index])\r
return String\r
Value = (Value << 8) | int(ByteList[index])\r
return Value\r
\r
+class DefaultStore():\r
+ def __init__(self, DefaultStores ):\r
+\r
+ self.DefaultStores = DefaultStores\r
+ def DefaultStoreID(self, DefaultStoreName):\r
+ for key, value in self.DefaultStores.items():\r
+ if value == DefaultStoreName:\r
+ return key\r
+ return None\r
+ def GetDefaultDefault(self):\r
+ if not self.DefaultStores or "0" in self.DefaultStores:\r
+ return "0", TAB_DEFAULT_STORES_DEFAULT\r
+ else:\r
+ minvalue = min(int(value_str) for value_str in self.DefaultStores)\r
+ return (str(minvalue), self.DefaultStores[str(minvalue)])\r
+ def GetMin(self, DefaultSIdList):\r
+ if not DefaultSIdList:\r
+ return TAB_DEFAULT_STORES_DEFAULT\r
+ storeidset = {storeid for storeid, storename in self.DefaultStores.values() if storename in DefaultSIdList}\r
+ if not storeidset:\r
+ return ""\r
+ minid = min(storeidset )\r
+ for sid, name in self.DefaultStores.values():\r
+ if sid == minid:\r
+ return name\r
\r
class SkuClass():\r
- \r
DEFAULT = 0\r
SINGLE = 1\r
MULTIPLE =2\r
- \r
- def __init__(self,SkuIdentifier='', SkuIds={}):\r
- \r
+\r
+ def __init__(self,SkuIdentifier='', SkuIds=None):\r
+ if SkuIds is None:\r
+ SkuIds = {}\r
+\r
+ for SkuName in SkuIds:\r
+ SkuId = SkuIds[SkuName][0]\r
+ skuid_num = int(SkuId, 16) if SkuId.upper().startswith("0X") else int(SkuId)\r
+ if skuid_num > 0xFFFFFFFFFFFFFFFF:\r
+ EdkLogger.error("build", PARAMETER_INVALID,\r
+ ExtraData = "SKU-ID [%s] value %s exceeds the max value of UINT64"\r
+ % (SkuName, SkuId))\r
+\r
self.AvailableSkuIds = sdict()\r
self.SkuIdSet = []\r
self.SkuIdNumberSet = []\r
+ self.SkuData = SkuIds\r
+ self._SkuInherit = {}\r
+ self._SkuIdentifier = SkuIdentifier\r
if SkuIdentifier == '' or SkuIdentifier is None:\r
self.SkuIdSet = ['DEFAULT']\r
self.SkuIdNumberSet = ['0U']\r
elif SkuIdentifier == 'ALL':\r
self.SkuIdSet = SkuIds.keys()\r
- self.SkuIdNumberSet = [num.strip() + 'U' for num in SkuIds.values()]\r
+ self.SkuIdNumberSet = [num[0].strip() + 'U' for num in SkuIds.values()]\r
else:\r
- r = SkuIdentifier.split('|') \r
- self.SkuIdSet=[r[k].strip() for k in range(len(r))] \r
+ r = SkuIdentifier.split('|')\r
+ self.SkuIdSet=[(r[k].strip()).upper() for k in range(len(r))]\r
k = None\r
- try: \r
- self.SkuIdNumberSet = [SkuIds[k].strip() + 'U' for k in self.SkuIdSet] \r
+ try:\r
+ self.SkuIdNumberSet = [SkuIds[k][0].strip() + 'U' for k in self.SkuIdSet]\r
except Exception:\r
EdkLogger.error("build", PARAMETER_INVALID,\r
ExtraData = "SKU-ID [%s] is not supported by the platform. [Valid SKU-ID: %s]"\r
% (k, " | ".join(SkuIds.keys())))\r
- if len(self.SkuIdSet) == 2 and 'DEFAULT' in self.SkuIdSet and SkuIdentifier != 'ALL':\r
- self.SkuIdSet.remove('DEFAULT')\r
- self.SkuIdNumberSet.remove('0U')\r
for each in self.SkuIdSet:\r
if each in SkuIds:\r
- self.AvailableSkuIds[each] = SkuIds[each]\r
+ self.AvailableSkuIds[each] = SkuIds[each][0]\r
else:\r
EdkLogger.error("build", PARAMETER_INVALID,\r
ExtraData="SKU-ID [%s] is not supported by the platform. [Valid SKU-ID: %s]"\r
% (each, " | ".join(SkuIds.keys())))\r
- \r
- def __SkuUsageType(self): \r
- \r
+ if self.SkuUsageType != SkuClass.SINGLE:\r
+ self.AvailableSkuIds.update({'DEFAULT':0, 'COMMON':0})\r
+ if self.SkuIdSet:\r
+ GlobalData.gSkuids = (self.SkuIdSet)\r
+ if 'COMMON' in GlobalData.gSkuids:\r
+ GlobalData.gSkuids.remove('COMMON')\r
+ if self.SkuUsageType == self.SINGLE:\r
+ if len(GlobalData.gSkuids) != 1:\r
+ if 'DEFAULT' in GlobalData.gSkuids:\r
+ GlobalData.gSkuids.remove('DEFAULT')\r
+ if GlobalData.gSkuids:\r
+ GlobalData.gSkuids.sort()\r
+\r
+ def GetNextSkuId(self, skuname):\r
+ if not self._SkuInherit:\r
+ self._SkuInherit = {}\r
+ for item in self.SkuData.values():\r
+ self._SkuInherit[item[1]]=item[2] if item[2] else "DEFAULT"\r
+ return self._SkuInherit.get(skuname, "DEFAULT")\r
+\r
+ def GetSkuChain(self, sku):\r
+ if sku == "DEFAULT":\r
+ return ["DEFAULT"]\r
+ skulist = [sku]\r
+ nextsku = sku\r
+ while True:\r
+ nextsku = self.GetNextSkuId(nextsku)\r
+ skulist.append(nextsku)\r
+ if nextsku == "DEFAULT":\r
+ break\r
+ skulist.reverse()\r
+ return skulist\r
+ def SkuOverrideOrder(self):\r
+ skuorderset = []\r
+ for skuname in self.SkuIdSet:\r
+ skuorderset.append(self.GetSkuChain(skuname))\r
+\r
+ skuorder = []\r
+ for index in range(max(len(item) for item in skuorderset)):\r
+ for subset in skuorderset:\r
+ if index > len(subset)-1:\r
+ continue\r
+ if subset[index] in skuorder:\r
+ continue\r
+ skuorder.append(subset[index])\r
+\r
+ return skuorder\r
+\r
+ @property\r
+ def SkuUsageType(self):\r
+ if self._SkuIdentifier.upper() == "ALL":\r
+ return SkuClass.MULTIPLE\r
+\r
if len(self.SkuIdSet) == 1:\r
if self.SkuIdSet[0] == 'DEFAULT':\r
return SkuClass.DEFAULT\r
- else:\r
- return SkuClass.SINGLE\r
- else:\r
- return SkuClass.MULTIPLE\r
-\r
- def __GetAvailableSkuIds(self):\r
+ return SkuClass.SINGLE\r
+ if len(self.SkuIdSet) == 2 and 'DEFAULT' in self.SkuIdSet:\r
+ return SkuClass.SINGLE\r
+ return SkuClass.MULTIPLE\r
+\r
+ def DumpSkuIdArrary(self):\r
+ if self.SkuUsageType == SkuClass.SINGLE:\r
+ return "{0x0}"\r
+ ArrayStrList = []\r
+ for skuname in self.AvailableSkuIds:\r
+ if skuname == "COMMON":\r
+ continue\r
+ while skuname != "DEFAULT":\r
+ ArrayStrList.append(hex(int(self.AvailableSkuIds[skuname])))\r
+ skuname = self.GetNextSkuId(skuname)\r
+ ArrayStrList.append("0x0")\r
+ return "{{{myList}}}".format(myList=",".join(ArrayStrList))\r
+\r
+ @property\r
+ def AvailableSkuIdSet(self):\r
return self.AvailableSkuIds\r
- \r
- def __GetSystemSkuID(self):\r
- if self.__SkuUsageType() == SkuClass.SINGLE:\r
- return self.SkuIdSet[0]\r
+\r
+ @property\r
+ def SystemSkuId(self):\r
+ if self.SkuUsageType == SkuClass.SINGLE:\r
+ if len(self.SkuIdSet) == 1:\r
+ return self.SkuIdSet[0]\r
+ else:\r
+ return self.SkuIdSet[0] if self.SkuIdSet[0] != 'DEFAULT' else self.SkuIdSet[1]\r
else:\r
return 'DEFAULT'\r
- def __GetAvailableSkuIdNumber(self):\r
- return self.SkuIdNumberSet\r
- SystemSkuId = property(__GetSystemSkuID)\r
- AvailableSkuIdSet = property(__GetAvailableSkuIds)\r
- SkuUsageType = property(__SkuUsageType)\r
- AvailableSkuIdNumSet = property(__GetAvailableSkuIdNumber)\r
\r
#\r
# Pack a registry format GUID\r
#\r
def PackRegistryFormatGuid(Guid):\r
- Guid = Guid.split('-')\r
- return pack('=LHHBBBBBBBB',\r
- int(Guid[0], 16),\r
- int(Guid[1], 16),\r
- int(Guid[2], 16),\r
- int(Guid[3][-4:-2], 16),\r
- int(Guid[3][-2:], 16),\r
- int(Guid[4][-12:-10], 16),\r
- int(Guid[4][-10:-8], 16),\r
- int(Guid[4][-8:-6], 16),\r
- int(Guid[4][-6:-4], 16),\r
- int(Guid[4][-4:-2], 16),\r
- int(Guid[4][-2:], 16)\r
- )\r
+ return PackGUID(Guid.split('-'))\r
\r
-def BuildOptionPcdValueFormat(TokenSpaceGuidCName, TokenCName, PcdDatumType, Value):\r
- if PcdDatumType == 'VOID*':\r
- if Value.startswith('L'):\r
- if not Value[1]:\r
- EdkLogger.error("build", FORMAT_INVALID, 'For Void* type PCD, when specify the Value in the command line, please use the following format: "string", L"string", H"{...}"')\r
- Value = Value[0] + '"' + Value[1:] + '"'\r
- elif Value.startswith('H'):\r
- if not Value[1]:\r
- EdkLogger.error("build", FORMAT_INVALID, 'For Void* type PCD, when specify the Value in the command line, please use the following format: "string", L"string", H"{...}"')\r
- Value = Value[1:]\r
- else:\r
- if not Value[0]:\r
- EdkLogger.error("build", FORMAT_INVALID, 'For Void* type PCD, when specify the Value in the command line, please use the following format: "string", L"string", H"{...}"')\r
- Value = '"' + Value + '"'\r
-\r
- IsValid, Cause = CheckPcdDatum(PcdDatumType, Value)\r
- if not IsValid:\r
- EdkLogger.error("build", FORMAT_INVALID, Cause, ExtraData="%s.%s" % (TokenSpaceGuidCName, TokenCName))\r
- if PcdDatumType == 'BOOLEAN':\r
- Value = Value.upper()\r
- if Value == 'TRUE' or Value == '1':\r
- Value = '1'\r
- elif Value == 'FALSE' or Value == '0':\r
- Value = '0'\r
- return Value\r
## Get the integer value from string like "14U" or integer like 2\r
#\r
# @param Input The object that may be either a integer value or a string\r
else:\r
return int(String)\r
\r
+#\r
+# Pack a GUID (registry format) list into a buffer and return it\r
+#\r
+def PackGUID(Guid):\r
+ return pack(PACK_PATTERN_GUID,\r
+ int(Guid[0], 16),\r
+ int(Guid[1], 16),\r
+ int(Guid[2], 16),\r
+ int(Guid[3][-4:-2], 16),\r
+ int(Guid[3][-2:], 16),\r
+ int(Guid[4][-12:-10], 16),\r
+ int(Guid[4][-10:-8], 16),\r
+ int(Guid[4][-8:-6], 16),\r
+ int(Guid[4][-6:-4], 16),\r
+ int(Guid[4][-4:-2], 16),\r
+ int(Guid[4][-2:], 16)\r
+ )\r
+\r
+#\r
+# Pack a GUID (byte) list into a buffer and return it\r
+#\r
+def PackByteFormatGUID(Guid):\r
+ return pack(PACK_PATTERN_GUID,\r
+ Guid[0],\r
+ Guid[1],\r
+ Guid[2],\r
+ Guid[3],\r
+ Guid[4],\r
+ Guid[5],\r
+ Guid[6],\r
+ Guid[7],\r
+ Guid[8],\r
+ Guid[9],\r
+ Guid[10],\r
+ )\r
+\r
##\r
#\r
# This acts like the main() function for the script, unless it is 'import'ed into another\r