## @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
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
+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
\r
return FieldList\r
\r
+def ParseDevPathValue (Value):\r
+ DevPathList = [ "Path","HardwarePath","Pci","PcCard","MemoryMapped","VenHw","Ctrl","BMC","AcpiPath","Acpi","PciRoot",\r
+ "PcieRoot","Floppy","Keyboard","Serial","ParallelPort","AcpiEx","AcpiExp","AcpiAdr","Msg","Ata","Scsi",\r
+ "Fibre","FibreEx","I1394","USB","I2O","Infiniband","VenMsg","VenPcAnsi","VenVt100","VenVt100Plus",\r
+ "VenUtf8","UartFlowCtrl","SAS","SasEx","NVMe","UFS","SD","eMMC","DebugPort","MAC","IPv4","IPv6","Uart",\r
+ "UsbClass","UsbAudio","UsbCDCControl","UsbHID","UsbImage","UsbPrinter","UsbMassStorage","UsbHub",\r
+ "UsbCDCData","UsbSmartCard","UsbVideo","UsbDiagnostic","UsbWireless","UsbDeviceFirmwareUpdate",\r
+ "UsbIrdaBridge","UsbTestAndMeasurement","UsbWwid","Unit","iSCSI","Vlan","Uri","Bluetooth","Wi-Fi",\r
+ "MediaPath","HD","CDROM","VenMedia","Media","Fv","FvFile","Offset","RamDisk","VirtualDisk","VirtualCD",\r
+ "PersistentVirtualDisk","PersistentVirtualCD","BbsPath","BBS","Sata" ]\r
+ if '\\' in Value:\r
+ Value.replace('\\', '/').replace(' ', '')\r
+ for Item in Value.split('/'):\r
+ Key = Item.strip().split('(')[0]\r
+ if Key not in DevPathList:\r
+ pass\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, 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 type(Value) == type(0):\r
+ return Value, (Value.bit_length() + 7) / 8\r
+ if type(Value) <> type(''):\r
+ raise BadExpression('Type %s is %s' %(Value, type(Value)))\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 BadExpression('Value (%s) Size larger than %d' %(Value, Size))\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 BadExpression('Value (%s) Size larger than %d' %(Value, Size))\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 BadExpression('Value (%s) Size larger than %d' %(Value, Size))\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 BadExpression('Value (%s) Size larger than %d' % (Value, Size))\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 = ['%02x' % int(Item, 16) for Item in (Value[0] + Value[1][:-1]).split(',')]\r
+ if len(Value[0]) != 8:\r
+ Value[0] = '%08X' % int(Value[0], 16)\r
+ if len(Value[1]) != 4:\r
+ Value[1] = '%04X' % int(Value[1], 16)\r
+ if len(Value[2]) != 4:\r
+ Value[2] = '%04X' % int(Value[2], 16)\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
+ try:\r
+ Value = "'" + uuid.UUID(Value).get_bytes_le() + "'"\r
+ except ValueError, Message:\r
+ raise BadExpression('%s' % Message)\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
+ 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
+ List = list(Value[1:-1])\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.split('"')[1]\r
+ return ParseDevPathValue(Value)\r
+ if Value.lower().startswith('0x'):\r
+ Value = int(Value, 16)\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
if Type == "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
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 1:\r
)\r
\r
def BuildOptionPcdValueFormat(TokenSpaceGuidCName, TokenCName, PcdDatumType, Value):\r
- if PcdDatumType == 'VOID*':\r
- if Value.startswith('L'):\r
+ if PcdDatumType not in [TAB_UINT8, TAB_UINT16, TAB_UINT32, TAB_UINT64,'BOOLEAN']:\r
+ if Value.startswith('L') or Value.startswith('"'):\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
+ Value = Value\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