]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/Common/Misc.py
BaseTools: Support Structure PCD value assignment in DEC/DSC
[mirror_edk2.git] / BaseTools / Source / Python / Common / Misc.py
index 3be1f0f28b634c91f99cbd1108613168dc50cb2f..2bbd9945fc0ad702cd2272fbe88b068dceb124c1 100644 (file)
@@ -1,7 +1,7 @@
 ## @file\r
 # Common routines used by all tools\r
 #\r
-# Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.<BR>\r
+# Copyright (c) 2007 - 2017, 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
@@ -36,6 +36,7 @@ from CommonDataClass.DataClass import *
 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
 ## Regular expression used to find out place holders in string template\r
 gPlaceholderPattern = re.compile("\$\{([^$()\s]+)\}", re.MULTILINE | re.UNICODE)\r
@@ -67,14 +68,32 @@ def GetVariableOffset(mapfilepath, efifilepath, varnames):
     if (firstline.startswith("Archive member included ") and\r
         firstline.endswith(" file (symbol)")):\r
         return _parseForGCC(lines, efifilepath, varnames)\r
+    if firstline.startswith("# Path:"):\r
+        return _parseForXcode(lines, efifilepath, varnames)\r
     return _parseGeneral(lines, efifilepath, varnames)\r
 \r
+def _parseForXcode(lines, efifilepath, varnames):\r
+    status = 0\r
+    ret = []\r
+    for index, line in enumerate(lines):\r
+        line = line.strip()\r
+        if status == 0 and line == "# Symbols:":\r
+            status = 1\r
+            continue\r
+        if status == 1 and len(line) != 0:\r
+            for varname in varnames:\r
+                if varname in line:\r
+                    m = re.match('^([\da-fA-FxX]+)([\s\S]*)([_]*%s)$' % varname, line)\r
+                    if m != None:\r
+                        ret.append((varname, m.group(1)))\r
+    return ret\r
+\r
 def _parseForGCC(lines, efifilepath, varnames):\r
     """ Parse map file generated by GCC linker """\r
     status = 0\r
     sections = []\r
     varoffset = []\r
-    for line in lines:\r
+    for index, line in enumerate(lines):\r
         line = line.strip()\r
         # status machine transection\r
         if status == 0 and line == "Memory Configuration":\r
@@ -88,14 +107,23 @@ def _parseForGCC(lines, efifilepath, varnames):
             continue\r
 \r
         # status handler\r
-        if status == 2:\r
+        if status == 3:\r
             m = re.match('^([\w_\.]+) +([\da-fA-Fx]+) +([\da-fA-Fx]+)$', line)\r
             if m != None:\r
                 sections.append(m.groups(0))\r
             for varname in varnames:\r
-                m = re.match("^([\da-fA-Fx]+) +[_]*(%s)$" % varname, line)\r
+                Str = ''\r
+                m = re.match("^.data.(%s)" % varname, line)\r
                 if m != None:\r
-                    varoffset.append((varname, int(m.groups(0)[0], 16) , int(sections[-1][1], 16), sections[-1][0]))\r
+                    m = re.match(".data.(%s)$" % varname, line)\r
+                    if m != 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
 \r
     if not varoffset:\r
         return []\r
@@ -1444,6 +1472,100 @@ def AnalyzePcdExpression(Setting):
 \r
     return FieldList\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
+    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
@@ -1477,19 +1599,19 @@ def AnalyzeDscPcd(Setting, PcdType, DataType=''):
         Value = FieldList[0]\r
         Size = ''\r
         if len(FieldList) > 1:\r
-            Type = FieldList[1]\r
-            # Fix the PCD type when no DataType input\r
-            if Type == 'VOID*':\r
-                DataType = 'VOID*'\r
-            else:\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) > 2:\r
             Size = FieldList[2]\r
-        if DataType == 'VOID*':\r
-            IsValid = (len(FieldList) <= 3)\r
-        else:\r
+        if DataType == "":\r
             IsValid = (len(FieldList) <= 1)\r
-        return [Value, '', Size], IsValid, 0\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
         Value = FieldList[0]\r
         Size = Type = ''\r
@@ -1507,10 +1629,10 @@ def AnalyzeDscPcd(Setting, PcdType, DataType=''):
                     Size = str(len(Value.split(",")))\r
                 else:\r
                     Size = str(len(Value) -2 + 1 )\r
-        if DataType == 'VOID*':\r
-            IsValid = (len(FieldList) <= 3)\r
-        else:\r
+        if DataType == "":\r
             IsValid = (len(FieldList) <= 1)\r
+        else:\r
+            IsValid = (len(FieldList) <= 3)\r
         return [Value, Type, Size], IsValid, 0\r
     elif PcdType in (MODEL_PCD_DYNAMIC_VPD, MODEL_PCD_DYNAMIC_EX_VPD):\r
         VpdOffset = FieldList[0]\r
@@ -1523,10 +1645,11 @@ def AnalyzeDscPcd(Setting, PcdType, DataType=''):
                 Size = FieldList[1]\r
             if len(FieldList) > 2:\r
                 Value = FieldList[2]\r
-        if DataType == 'VOID*':\r
-            IsValid = (len(FieldList) <= 3)\r
+        if DataType == "":\r
+            IsValid = (len(FieldList) <= 1)\r
         else:\r
-            IsValid = (len(FieldList) <= 2)\r
+            IsValid = (len(FieldList) <= 3)\r
+\r
         return [VpdOffset, Size, Value], IsValid, 2\r
     elif PcdType in (MODEL_PCD_DYNAMIC_HII, MODEL_PCD_DYNAMIC_EX_HII):\r
         HiiString = FieldList[0]\r
@@ -1655,7 +1778,7 @@ def CheckPcdDatum(Type, Value):
             return False, "Invalid value [%s] of type [%s];"\\r
                           " must be a hexadecimal, decimal or octal in C language format." % (Value, Type)\r
     else:\r
-        return False, "Invalid type [%s]; must be one of VOID*, BOOLEAN, UINT8, UINT16, UINT32, UINT64." % (Type)\r
+        return True, "StructurePcd"\r
 \r
     return True, ""\r
 \r
@@ -1997,7 +2120,7 @@ class SkuClass():
             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
+                                      % (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
@@ -2007,7 +2130,7 @@ class SkuClass():
             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
+                                      % (each, " ".join(SkuIds.keys())))\r
         \r
     def __SkuUsageType(self): \r
         \r
@@ -2053,6 +2176,32 @@ def PackRegistryFormatGuid(Guid):
                 int(Guid[4][-2:], 16)\r
                 )\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
+\r
 ##\r
 #\r
 # This acts like the main() function for the script, unless it is 'import'ed into another\r