]> git.proxmox.com Git - mirror_edk2.git/commitdiff
DynamicTablesPkg: Fix IORT node length assignment
authorSami Mujawar <sami.mujawar@arm.com>
Tue, 9 Jul 2019 15:50:42 +0000 (16:50 +0100)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Sun, 29 Mar 2020 16:53:35 +0000 (16:53 +0000)
The VS2017 compiler reports 'warning C4267: 'return': conversion
from 'size_t' to 'UINT32', possible loss of data' for a number of
functions that compute the IORT node length. Similarly, it reports
warnings for IORT node length field assignments as the length
field is 16-bit wide.

This patch adds type casts at appropriate places and also implements
validations to ensure that the max width of the respective fields
is not exceeded.

This patch also fixes a typo in one of the local variable names.

Signed-off-by: Sami Mujawar <sami.mujawar@arm.com>
Reviewed-by: Alexei Fedorov <Alexei.Fedorov@arm.com>
DynamicTablesPkg/Library/Acpi/Arm/AcpiIortLibArm/IortGenerator.c

index f303e9f29cc700634d236c08505cab91b9d62cb8..bd71220ca19679792de2bb6d88eed8d1913a7600 100644 (file)
@@ -140,8 +140,8 @@ GetItsGroupNodeSize (
   /* Size of ITS Group Node +\r
      Size of ITS Identifier array\r
   */\r
-  return sizeof (EFI_ACPI_6_0_IO_REMAPPING_ITS_NODE) +\r
-           (Node->ItsIdCount * sizeof (UINT32));\r
+  return (UINT32)(sizeof (EFI_ACPI_6_0_IO_REMAPPING_ITS_NODE) +\r
+                    (Node->ItsIdCount * sizeof (UINT32)));\r
 }\r
 \r
 /** Returns the total size required for the ITS Group nodes and\r
@@ -160,7 +160,7 @@ GetItsGroupNodeSize (
     @retval Total size of the ITS Group Nodes.\r
 **/\r
 STATIC\r
-UINT32\r
+UINT64\r
 GetSizeofItsGroupNodes (\r
   IN      CONST UINT32                         NodeStartOffset,\r
   IN      CONST CM_ARM_ITS_GROUP_NODE  *       NodeList,\r
@@ -168,7 +168,7 @@ GetSizeofItsGroupNodes (
   IN OUT        IORT_NODE_INDEXER     ** CONST NodeIndexer\r
   )\r
 {\r
-  UINT32  Size;\r
+  UINT64  Size;\r
 \r
   ASSERT (NodeList != NULL);\r
 \r
@@ -176,7 +176,7 @@ GetSizeofItsGroupNodes (
   while (NodeCount-- != 0) {\r
     (*NodeIndexer)->Token = NodeList->Token;\r
     (*NodeIndexer)->Object = (VOID*)NodeList;\r
-    (*NodeIndexer)->Offset = Size + NodeStartOffset;\r
+    (*NodeIndexer)->Offset = (UINT32)(Size + NodeStartOffset);\r
     DEBUG ((\r
       DEBUG_INFO,\r
       "IORT: Node Indexer = %p, Token = %p, Object = %p, Offset = 0x%x\n",\r
@@ -211,10 +211,10 @@ GetNamedComponentNodeSize (
      Size of ID mapping array +\r
      Size of ASCII string + 'padding to 32-bit word aligned'.\r
   */\r
-  return sizeof (EFI_ACPI_6_0_IO_REMAPPING_NAMED_COMP_NODE) +\r
-            (Node->IdMappingCount *\r
-             sizeof (EFI_ACPI_6_0_IO_REMAPPING_ID_TABLE)) +\r
-            ALIGN_VALUE (AsciiStrSize (Node->ObjectName), 4);\r
+  return (UINT32)(sizeof (EFI_ACPI_6_0_IO_REMAPPING_NAMED_COMP_NODE) +\r
+                    (Node->IdMappingCount *\r
+                     sizeof (EFI_ACPI_6_0_IO_REMAPPING_ID_TABLE)) +\r
+                    ALIGN_VALUE (AsciiStrSize (Node->ObjectName), 4));\r
 }\r
 \r
 /** Returns the total size required for the Named Component nodes and\r
@@ -233,7 +233,7 @@ GetNamedComponentNodeSize (
     @retval Total size of the Named Component nodes.\r
 **/\r
 STATIC\r
-UINT32\r
+UINT64\r
 GetSizeofNamedComponentNodes (\r
   IN      CONST UINT32                              NodeStartOffset,\r
   IN      CONST CM_ARM_NAMED_COMPONENT_NODE *       NodeList,\r
@@ -241,7 +241,7 @@ GetSizeofNamedComponentNodes (
   IN OUT        IORT_NODE_INDEXER          ** CONST NodeIndexer\r
   )\r
 {\r
-  UINT32  Size;\r
+  UINT64  Size;\r
 \r
   ASSERT (NodeList != NULL);\r
 \r
@@ -249,7 +249,7 @@ GetSizeofNamedComponentNodes (
   while (NodeCount-- != 0) {\r
     (*NodeIndexer)->Token = NodeList->Token;\r
     (*NodeIndexer)->Object = (VOID*)NodeList;\r
-    (*NodeIndexer)->Offset = Size + NodeStartOffset;\r
+    (*NodeIndexer)->Offset = (UINT32)(Size + NodeStartOffset);\r
     DEBUG ((\r
       DEBUG_INFO,\r
       "IORT: Node Indexer = %p, Token = %p, Object = %p, Offset = 0x%x\n",\r
@@ -284,9 +284,9 @@ GetRootComplexNodeSize (
   /* Size of Root Complex node +\r
      Size of ID mapping array\r
   */\r
-  return sizeof (EFI_ACPI_6_0_IO_REMAPPING_RC_NODE) +\r
-           (Node->IdMappingCount *\r
-            sizeof (EFI_ACPI_6_0_IO_REMAPPING_ID_TABLE));\r
+  return (UINT32)(sizeof (EFI_ACPI_6_0_IO_REMAPPING_RC_NODE) +\r
+                    (Node->IdMappingCount *\r
+                     sizeof (EFI_ACPI_6_0_IO_REMAPPING_ID_TABLE)));\r
 }\r
 \r
 /** Returns the total size required for the Root Complex nodes and\r
@@ -305,7 +305,7 @@ GetRootComplexNodeSize (
     @retval Total size of the Root Complex nodes.\r
 **/\r
 STATIC\r
-UINT32\r
+UINT64\r
 GetSizeofRootComplexNodes (\r
   IN      CONST UINT32                              NodeStartOffset,\r
   IN      CONST CM_ARM_ROOT_COMPLEX_NODE    *       NodeList,\r
@@ -313,7 +313,7 @@ GetSizeofRootComplexNodes (
   IN OUT        IORT_NODE_INDEXER          ** CONST NodeIndexer\r
   )\r
 {\r
-  UINT32  Size;\r
+  UINT64  Size;\r
 \r
   ASSERT (NodeList != NULL);\r
 \r
@@ -321,7 +321,7 @@ GetSizeofRootComplexNodes (
   while (NodeCount-- != 0) {\r
     (*NodeIndexer)->Token = NodeList->Token;\r
     (*NodeIndexer)->Object = (VOID*)NodeList;\r
-    (*NodeIndexer)->Offset = Size + NodeStartOffset;\r
+    (*NodeIndexer)->Offset = (UINT32)(Size + NodeStartOffset);\r
     DEBUG ((\r
       DEBUG_INFO,\r
       "IORT: Node Indexer = %p, Token = %p, Object = %p, Offset = 0x%x\n",\r
@@ -358,13 +358,13 @@ GetSmmuV1V2NodeSize (
      Size of context interrupt array +\r
      Size of PMU interrupt array\r
   */\r
-  return sizeof (EFI_ACPI_6_0_IO_REMAPPING_SMMU_NODE) +\r
-           (Node->IdMappingCount *\r
-            sizeof (EFI_ACPI_6_0_IO_REMAPPING_ID_TABLE)) +\r
-           (Node->ContextInterruptCount *\r
-            sizeof (EFI_ACPI_6_0_IO_REMAPPING_SMMU_INT)) +\r
-           (Node->PmuInterruptCount *\r
-            sizeof (EFI_ACPI_6_0_IO_REMAPPING_SMMU_INT));\r
+  return (UINT32)(sizeof (EFI_ACPI_6_0_IO_REMAPPING_SMMU_NODE) +\r
+                    (Node->IdMappingCount *\r
+                     sizeof (EFI_ACPI_6_0_IO_REMAPPING_ID_TABLE)) +\r
+                    (Node->ContextInterruptCount *\r
+                     sizeof (EFI_ACPI_6_0_IO_REMAPPING_SMMU_INT)) +\r
+                    (Node->PmuInterruptCount *\r
+                     sizeof (EFI_ACPI_6_0_IO_REMAPPING_SMMU_INT)));\r
 }\r
 \r
 /** Returns the total size required for the SMMUv1/SMMUv2 nodes and\r
@@ -383,7 +383,7 @@ GetSmmuV1V2NodeSize (
     @retval Total size of the SMMUv1/SMMUv2 nodes.\r
 **/\r
 STATIC\r
-UINT32\r
+UINT64\r
 GetSizeofSmmuV1V2Nodes (\r
   IN      CONST UINT32                              NodeStartOffset,\r
   IN      CONST CM_ARM_SMMUV1_SMMUV2_NODE   *       NodeList,\r
@@ -391,7 +391,7 @@ GetSizeofSmmuV1V2Nodes (
   IN OUT        IORT_NODE_INDEXER          ** CONST NodeIndexer\r
   )\r
 {\r
-  UINT32  Size;\r
+  UINT64  Size;\r
 \r
   ASSERT (NodeList != NULL);\r
 \r
@@ -399,7 +399,7 @@ GetSizeofSmmuV1V2Nodes (
   while (NodeCount-- != 0) {\r
     (*NodeIndexer)->Token = NodeList->Token;\r
     (*NodeIndexer)->Object = (VOID*)NodeList;\r
-    (*NodeIndexer)->Offset = Size + NodeStartOffset;\r
+    (*NodeIndexer)->Offset = (UINT32)(Size + NodeStartOffset);\r
     DEBUG ((\r
       DEBUG_INFO,\r
       "IORT: Node Indexer = %p, Token = %p, Object = %p, Offset = 0x%x\n",\r
@@ -433,9 +433,9 @@ GetSmmuV3NodeSize (
   /* Size of SMMU v1/SMMU v2 node +\r
      Size of ID mapping array\r
   */\r
-  return sizeof (EFI_ACPI_6_0_IO_REMAPPING_SMMU3_NODE) +\r
-           (Node->IdMappingCount *\r
-            sizeof (EFI_ACPI_6_0_IO_REMAPPING_ID_TABLE));\r
+  return (UINT32)(sizeof (EFI_ACPI_6_0_IO_REMAPPING_SMMU3_NODE) +\r
+                    (Node->IdMappingCount *\r
+                     sizeof (EFI_ACPI_6_0_IO_REMAPPING_ID_TABLE)));\r
 }\r
 \r
 /** Returns the total size required for the SMMUv3 nodes and\r
@@ -454,7 +454,7 @@ GetSmmuV3NodeSize (
     @retval Total size of the SMMUv3 nodes.\r
 **/\r
 STATIC\r
-UINT32\r
+UINT64\r
 GetSizeofSmmuV3Nodes (\r
   IN      CONST UINT32                       NodeStartOffset,\r
   IN      CONST CM_ARM_SMMUV3_NODE   *       NodeList,\r
@@ -462,7 +462,7 @@ GetSizeofSmmuV3Nodes (
   IN OUT        IORT_NODE_INDEXER   ** CONST NodeIndexer\r
   )\r
 {\r
-  UINT32  Size;\r
+  UINT64  Size;\r
 \r
   ASSERT (NodeList != NULL);\r
 \r
@@ -470,7 +470,7 @@ GetSizeofSmmuV3Nodes (
   while (NodeCount-- != 0) {\r
     (*NodeIndexer)->Token = NodeList->Token;\r
     (*NodeIndexer)->Object = (VOID*)NodeList;\r
-    (*NodeIndexer)->Offset = Size + NodeStartOffset;\r
+    (*NodeIndexer)->Offset = (UINT32)(Size + NodeStartOffset);\r
     DEBUG ((\r
       DEBUG_INFO,\r
       "IORT: Node Indexer = %p, Token = %p, Object = %p, Offset = 0x%x\n",\r
@@ -504,9 +504,9 @@ GetPmcgNodeSize (
   /* Size of PMCG node +\r
      Size of ID mapping array\r
   */\r
-  return sizeof (EFI_ACPI_6_0_IO_REMAPPING_PMCG_NODE) +\r
-           (Node->IdMappingCount *\r
-            sizeof (EFI_ACPI_6_0_IO_REMAPPING_ID_TABLE));\r
+  return (UINT32)(sizeof (EFI_ACPI_6_0_IO_REMAPPING_PMCG_NODE) +\r
+                    (Node->IdMappingCount *\r
+                     sizeof (EFI_ACPI_6_0_IO_REMAPPING_ID_TABLE)));\r
 }\r
 \r
 /** Returns the total size required for the PMCG nodes and\r
@@ -525,7 +525,7 @@ GetPmcgNodeSize (
     @retval Total size of the PMCG nodes.\r
 **/\r
 STATIC\r
-UINT32\r
+UINT64\r
 GetSizeofPmcgNodes (\r
   IN      CONST UINT32                     NodeStartOffset,\r
   IN      CONST CM_ARM_PMCG_NODE   *       NodeList,\r
@@ -533,7 +533,7 @@ GetSizeofPmcgNodes (
   IN OUT        IORT_NODE_INDEXER ** CONST NodeIndexer\r
   )\r
 {\r
-  UINT32  Size;\r
+  UINT64  Size;\r
 \r
   ASSERT (NodeList != NULL);\r
 \r
@@ -541,7 +541,7 @@ GetSizeofPmcgNodes (
   while (NodeCount-- != 0) {\r
     (*NodeIndexer)->Token = NodeList->Token;\r
     (*NodeIndexer)->Object = (VOID*)NodeList;\r
-    (*NodeIndexer)->Offset = Size + NodeStartOffset;\r
+    (*NodeIndexer)->Offset = (UINT32)(Size + NodeStartOffset);\r
     DEBUG ((\r
       DEBUG_INFO,\r
       "IORT: Node Indexer = %p, Token = %p, Object = %p, Offset = 0x%x\n",\r
@@ -735,6 +735,7 @@ AddItsGroupNodes (
   CM_ARM_ITS_IDENTIFIER               * ItsIdentifier;\r
   UINT32                                ItsIdentifierCount;\r
   UINT32                                IdIndex;\r
+  UINT64                                NodeLength;\r
 \r
   ASSERT (Iort != NULL);\r
 \r
@@ -742,9 +743,22 @@ AddItsGroupNodes (
                   NodesStartOffset);\r
 \r
   while (NodeCount-- != 0) {\r
+    NodeLength = GetItsGroupNodeSize (NodeList);\r
+    if (NodeLength > MAX_UINT16) {\r
+      Status = EFI_INVALID_PARAMETER;\r
+      DEBUG ((\r
+        DEBUG_ERROR,\r
+        "ERROR: IORT: ITS Id Array Node length 0x%lx > MAX_UINT16."\r
+        " Status = %r\n",\r
+        NodeLength,\r
+        Status\r
+        ));\r
+      return Status;\r
+    }\r
+\r
     // Populate the node header\r
     ItsGroupNode->Node.Type = EFI_ACPI_IORT_TYPE_ITS_GROUP;\r
-    ItsGroupNode->Node.Length = GetItsGroupNodeSize (NodeList);\r
+    ItsGroupNode->Node.Length = (UINT16)NodeLength;\r
     ItsGroupNode->Node.Revision = 0;\r
     ItsGroupNode->Node.Reserved = EFI_ACPI_RESERVED_DWORD;\r
     ItsGroupNode->Node.NumIdMappings = 0;\r
@@ -825,8 +839,9 @@ AddNamedComponentNodes (
   EFI_STATUS                                   Status;\r
   EFI_ACPI_6_0_IO_REMAPPING_NAMED_COMP_NODE  * NcNode;\r
   EFI_ACPI_6_0_IO_REMAPPING_ID_TABLE         * IdMapArray;\r
-  UINT32                                       ObjectNameLenght;\r
   CHAR8                                      * ObjectName;\r
+  UINTN                                        ObjectNameLength;\r
+  UINT64                                       NodeLength;\r
 \r
   ASSERT (Iort != NULL);\r
 \r
@@ -834,18 +849,30 @@ AddNamedComponentNodes (
             NodesStartOffset);\r
 \r
   while (NodeCount-- != 0) {\r
+    NodeLength = GetNamedComponentNodeSize (NodeList);\r
+    if (NodeLength > MAX_UINT16) {\r
+      Status = EFI_INVALID_PARAMETER;\r
+      DEBUG ((\r
+        DEBUG_ERROR,\r
+        "ERROR: IORT: Named Component Node length 0x%lx > MAX_UINT16."\r
+        " Status = %r\n",\r
+        NodeLength,\r
+        Status\r
+        ));\r
+      return Status;\r
+    }\r
+\r
     // Populate the node header\r
     NcNode->Node.Type = EFI_ACPI_IORT_TYPE_NAMED_COMP;\r
-    NcNode->Node.Length =\r
-      GetNamedComponentNodeSize (NodeList);\r
+    NcNode->Node.Length = (UINT16)NodeLength;\r
     NcNode->Node.Revision = 2;\r
     NcNode->Node.Reserved = EFI_ACPI_RESERVED_DWORD;\r
     NcNode->Node.NumIdMappings = NodeList->IdMappingCount;\r
 \r
-    ObjectNameLenght = AsciiStrLen (NodeList->ObjectName) + 1;\r
+    ObjectNameLength = AsciiStrLen (NodeList->ObjectName) + 1;\r
     NcNode->Node.IdReference =\r
-      sizeof (EFI_ACPI_6_0_IO_REMAPPING_NAMED_COMP_NODE) +\r
-      (ALIGN_VALUE (ObjectNameLenght, 4));\r
+      (UINT32)(sizeof (EFI_ACPI_6_0_IO_REMAPPING_NAMED_COMP_NODE) +\r
+        (ALIGN_VALUE (ObjectNameLength, 4)));\r
 \r
     // Named Component specific data\r
     NcNode->Flags = NodeList->Flags;\r
@@ -860,7 +887,7 @@ AddNamedComponentNodes (
       sizeof (EFI_ACPI_6_0_IO_REMAPPING_NAMED_COMP_NODE));\r
     Status = AsciiStrCpyS (\r
                ObjectName,\r
-               ObjectNameLenght,\r
+               ObjectNameLength,\r
                NodeList->ObjectName\r
                );\r
     if (EFI_ERROR (Status)) {\r
@@ -936,6 +963,7 @@ AddRootComplexNodes (
   EFI_STATUS                           Status;\r
   EFI_ACPI_6_0_IO_REMAPPING_RC_NODE  * RcNode;\r
   EFI_ACPI_6_0_IO_REMAPPING_ID_TABLE * IdMapArray;\r
+  UINT64                               NodeLength;\r
 \r
   ASSERT (Iort != NULL);\r
 \r
@@ -943,9 +971,22 @@ AddRootComplexNodes (
             NodesStartOffset);\r
 \r
   while (NodeCount-- != 0) {\r
+    NodeLength = GetRootComplexNodeSize (NodeList);\r
+    if (NodeLength > MAX_UINT16) {\r
+      Status = EFI_INVALID_PARAMETER;\r
+      DEBUG ((\r
+        DEBUG_ERROR,\r
+        "ERROR: IORT: Root Complex Node length 0x%lx > MAX_UINT16."\r
+        " Status = %r\n",\r
+        NodeLength,\r
+        Status\r
+        ));\r
+      return Status;\r
+    }\r
+\r
     // Populate the node header\r
     RcNode->Node.Type = EFI_ACPI_IORT_TYPE_ROOT_COMPLEX;\r
-    RcNode->Node.Length = GetRootComplexNodeSize (NodeList);\r
+    RcNode->Node.Length = (UINT16)NodeLength;\r
     RcNode->Node.Revision = 1;\r
     RcNode->Node.Reserved = EFI_ACPI_RESERVED_DWORD;\r
     RcNode->Node.NumIdMappings = NodeList->IdMappingCount;\r
@@ -1093,6 +1134,7 @@ AddSmmuV1V2Nodes (
 \r
   EFI_ACPI_6_0_IO_REMAPPING_SMMU_INT  * ContextInterruptArray;\r
   EFI_ACPI_6_0_IO_REMAPPING_SMMU_INT  * PmuInterruptArray;\r
+  UINT64                                NodeLength;\r
 \r
   ASSERT (Iort != NULL);\r
 \r
@@ -1100,9 +1142,21 @@ AddSmmuV1V2Nodes (
               NodesStartOffset);\r
 \r
   while (NodeCount-- != 0) {\r
+    NodeLength = GetSmmuV1V2NodeSize (NodeList);\r
+    if (NodeLength > MAX_UINT16) {\r
+      Status = EFI_INVALID_PARAMETER;\r
+      DEBUG ((\r
+        DEBUG_ERROR,\r
+        "ERROR: IORT: SMMU V1/V2 Node length 0x%lx > MAX_UINT16. Status = %r\n",\r
+        NodeLength,\r
+        Status\r
+        ));\r
+      return Status;\r
+    }\r
+\r
     // Populate the node header\r
     SmmuNode->Node.Type = EFI_ACPI_IORT_TYPE_SMMUv1v2;\r
-    SmmuNode->Node.Length = GetSmmuV1V2NodeSize (NodeList);\r
+    SmmuNode->Node.Length = (UINT16)NodeLength;\r
     SmmuNode->Node.Revision = 0;\r
     SmmuNode->Node.Reserved = EFI_ACPI_RESERVED_DWORD;\r
     SmmuNode->Node.NumIdMappings = NodeList->IdMappingCount;\r
@@ -1239,6 +1293,7 @@ AddSmmuV3Nodes (
   EFI_STATUS                             Status;\r
   EFI_ACPI_6_0_IO_REMAPPING_SMMU3_NODE * SmmuV3Node;\r
   EFI_ACPI_6_0_IO_REMAPPING_ID_TABLE   * IdMapArray;\r
+  UINT64                                 NodeLength;\r
 \r
   ASSERT (Iort != NULL);\r
 \r
@@ -1246,9 +1301,21 @@ AddSmmuV3Nodes (
                 NodesStartOffset);\r
 \r
   while (NodeCount-- != 0) {\r
+    NodeLength = GetSmmuV3NodeSize (NodeList);\r
+    if (NodeLength > MAX_UINT16) {\r
+      Status = EFI_INVALID_PARAMETER;\r
+      DEBUG ((\r
+        DEBUG_ERROR,\r
+        "ERROR: IORT: SMMU V3 Node length 0x%lx > MAX_UINT16. Status = %r\n",\r
+        NodeLength,\r
+        Status\r
+        ));\r
+      return Status;\r
+    }\r
+\r
     // Populate the node header\r
     SmmuV3Node->Node.Type = EFI_ACPI_IORT_TYPE_SMMUv3;\r
-    SmmuV3Node->Node.Length = GetSmmuV3NodeSize (NodeList);\r
+    SmmuV3Node->Node.Length = (UINT16)NodeLength;\r
     SmmuV3Node->Node.Revision = 2;\r
     SmmuV3Node->Node.Reserved = EFI_ACPI_RESERVED_DWORD;\r
     SmmuV3Node->Node.NumIdMappings = NodeList->IdMappingCount;\r
@@ -1344,6 +1411,7 @@ AddPmcgNodes (
   EFI_ACPI_6_0_IO_REMAPPING_PMCG_NODE  * PmcgNode;\r
   EFI_ACPI_6_0_IO_REMAPPING_ID_TABLE   * IdMapArray;\r
   ACPI_IORT_GENERATOR                  * Generator;\r
+  UINT64                                 NodeLength;\r
 \r
   ASSERT (Iort != NULL);\r
 \r
@@ -1352,9 +1420,21 @@ AddPmcgNodes (
               NodesStartOffset);\r
 \r
   while (NodeCount-- != 0) {\r
+    NodeLength = GetPmcgNodeSize (NodeList);\r
+    if (NodeLength > MAX_UINT16) {\r
+      Status = EFI_INVALID_PARAMETER;\r
+      DEBUG ((\r
+        DEBUG_ERROR,\r
+        "ERROR: IORT: PMCG Node length 0x%lx > MAX_UINT16. Status = %r\n",\r
+        NodeLength,\r
+        Status\r
+        ));\r
+      return Status;\r
+    }\r
+\r
     // Populate the node header\r
     PmcgNode->Node.Type = EFI_ACPI_IORT_TYPE_PMCG;\r
-    PmcgNode->Node.Length = GetPmcgNodeSize (NodeList);\r
+    PmcgNode->Node.Length = (UINT16)NodeLength;\r
     PmcgNode->Node.Revision = 1;\r
     PmcgNode->Node.Reserved = EFI_ACPI_RESERVED_DWORD;\r
     PmcgNode->Node.NumIdMappings = NodeList->IdMappingCount;\r
@@ -1448,9 +1528,11 @@ BuildIortTable (
   )\r
 {\r
   EFI_STATUS                             Status;\r
-  UINT32                                 TableSize;\r
-  UINT32                                 IortNodeCount;\r
 \r
+  UINT64                                 TableSize;\r
+  UINT64                                 NodeSize;\r
+\r
+  UINT32                                 IortNodeCount;\r
   UINT32                                 ItsGroupNodeCount;\r
   UINT32                                 NamedComponentNodeCount;\r
   UINT32                                 RootComplexNodeCount;\r
@@ -1638,81 +1720,141 @@ BuildIortTable (
 \r
   // ITS Group Nodes\r
   if (ItsGroupNodeCount > 0) {\r
-    ItsGroupOffset = TableSize;\r
+    ItsGroupOffset = (UINT32)TableSize;\r
     // Size of ITS Group node list.\r
-    TableSize += GetSizeofItsGroupNodes (\r
-                   ItsGroupOffset,\r
-                   ItsGroupNodeList,\r
-                   ItsGroupNodeCount,\r
-                   &NodeIndexer\r
-                   );\r
+    NodeSize = GetSizeofItsGroupNodes (\r
+                 ItsGroupOffset,\r
+                 ItsGroupNodeList,\r
+                 ItsGroupNodeCount,\r
+                 &NodeIndexer\r
+                 );\r
+    if (NodeSize > MAX_UINT32) {\r
+      Status = EFI_INVALID_PARAMETER;\r
+      DEBUG ((\r
+        DEBUG_ERROR,\r
+        "ERROR: IORT: Invalid Size of Group Nodes. Status = %r\n",\r
+        Status\r
+        ));\r
+      goto error_handler;\r
+    }\r
+    TableSize += NodeSize;\r
   }\r
 \r
   // Named Component Nodes\r
   if (NamedComponentNodeCount > 0) {\r
-    NamedComponentOffset = TableSize;\r
+    NamedComponentOffset = (UINT32)TableSize;\r
     // Size of Named Component node list.\r
-    TableSize += GetSizeofNamedComponentNodes (\r
-                   NamedComponentOffset,\r
-                   NamedComponentNodeList,\r
-                   NamedComponentNodeCount,\r
-                   &NodeIndexer\r
-                   );\r
+    NodeSize = GetSizeofNamedComponentNodes (\r
+                 NamedComponentOffset,\r
+                 NamedComponentNodeList,\r
+                 NamedComponentNodeCount,\r
+                 &NodeIndexer\r
+                 );\r
+    if (NodeSize > MAX_UINT32) {\r
+      Status = EFI_INVALID_PARAMETER;\r
+      DEBUG ((\r
+        DEBUG_ERROR,\r
+        "ERROR: IORT: Invalid Size of Named Component Nodes. Status = %r\n",\r
+        Status\r
+        ));\r
+      goto error_handler;\r
+    }\r
+    TableSize += NodeSize;\r
   }\r
 \r
   // Root Complex Nodes\r
   if (RootComplexNodeCount > 0) {\r
-    RootComplexOffset = TableSize;\r
+    RootComplexOffset = (UINT32)TableSize;\r
     // Size of Root Complex node list.\r
-    TableSize += GetSizeofRootComplexNodes (\r
-                   RootComplexOffset,\r
-                   RootComplexNodeList,\r
-                   RootComplexNodeCount,\r
-                   &NodeIndexer\r
-                   );\r
+    NodeSize = GetSizeofRootComplexNodes (\r
+                 RootComplexOffset,\r
+                 RootComplexNodeList,\r
+                 RootComplexNodeCount,\r
+                 &NodeIndexer\r
+                 );\r
+    if (NodeSize > MAX_UINT32) {\r
+      Status = EFI_INVALID_PARAMETER;\r
+      DEBUG ((\r
+        DEBUG_ERROR,\r
+        "ERROR: IORT: Invalid Size of Root Complex Nodes. Status = %r\n",\r
+        Status\r
+        ));\r
+      goto error_handler;\r
+    }\r
+    TableSize += NodeSize;\r
   }\r
 \r
   // SMMUv1/SMMUv2 Nodes\r
   if (SmmuV1V2NodeCount > 0) {\r
-    SmmuV1V2Offset = TableSize;\r
+    SmmuV1V2Offset = (UINT32)TableSize;\r
     // Size of SMMUv1/SMMUv2 node list.\r
-    TableSize += GetSizeofSmmuV1V2Nodes (\r
-                   SmmuV1V2Offset,\r
-                   SmmuV1V2NodeList,\r
-                   SmmuV1V2NodeCount,\r
-                   &NodeIndexer\r
-                   );\r
+    NodeSize = GetSizeofSmmuV1V2Nodes (\r
+                 SmmuV1V2Offset,\r
+                 SmmuV1V2NodeList,\r
+                 SmmuV1V2NodeCount,\r
+                 &NodeIndexer\r
+                 );\r
+    if (NodeSize > MAX_UINT32) {\r
+      Status = EFI_INVALID_PARAMETER;\r
+      DEBUG ((\r
+        DEBUG_ERROR,\r
+        "ERROR: IORT: Invalid Size of SMMUv1/v2 Nodes. Status = %r\n",\r
+        Status\r
+        ));\r
+      goto error_handler;\r
+    }\r
+    TableSize += NodeSize;\r
   }\r
 \r
   // SMMUv3 Nodes\r
   if (SmmuV3NodeCount > 0) {\r
-    SmmuV3Offset = TableSize;\r
+    SmmuV3Offset = (UINT32)TableSize;\r
     // Size of SMMUv3 node list.\r
-    TableSize += GetSizeofSmmuV3Nodes (\r
-                   SmmuV3Offset,\r
-                   SmmuV3NodeList,\r
-                   SmmuV3NodeCount,\r
-                   &NodeIndexer\r
-                   );\r
+    NodeSize = GetSizeofSmmuV3Nodes (\r
+                 SmmuV3Offset,\r
+                 SmmuV3NodeList,\r
+                 SmmuV3NodeCount,\r
+                 &NodeIndexer\r
+                 );\r
+    if (NodeSize > MAX_UINT32) {\r
+      Status = EFI_INVALID_PARAMETER;\r
+      DEBUG ((\r
+        DEBUG_ERROR,\r
+        "ERROR: IORT: Invalid Size of SMMUv3 Nodes. Status = %r\n",\r
+        Status\r
+        ));\r
+      goto error_handler;\r
+    }\r
+    TableSize += NodeSize;\r
   }\r
 \r
   // PMCG Nodes\r
   if (PmcgNodeCount > 0) {\r
-    PmcgOffset = TableSize;\r
+    PmcgOffset = (UINT32)TableSize;\r
     // Size of PMCG node list.\r
-    TableSize += GetSizeofPmcgNodes (\r
-                   PmcgOffset,\r
-                   PmcgNodeList,\r
-                   PmcgNodeCount,\r
-                   &NodeIndexer\r
-                   );\r
+    NodeSize = GetSizeofPmcgNodes (\r
+                 PmcgOffset,\r
+                 PmcgNodeList,\r
+                 PmcgNodeCount,\r
+                 &NodeIndexer\r
+                 );\r
+    if (NodeSize > MAX_UINT32) {\r
+      Status = EFI_INVALID_PARAMETER;\r
+      DEBUG ((\r
+        DEBUG_ERROR,\r
+        "ERROR: IORT: Invalid Size of PMCG Nodes. Status = %r\n",\r
+        Status\r
+        ));\r
+      goto error_handler;\r
+    }\r
+    TableSize += NodeSize;\r
   }\r
 \r
   DEBUG ((\r
     DEBUG_INFO,\r
     "INFO: IORT:\n" \\r
     " IortNodeCount = %d\n" \\r
-    " TableSize = %d\n",\r
+    " TableSize = 0x%lx\n",\r
     IortNodeCount,\r
     TableSize\r
     ));\r
@@ -1765,6 +1907,18 @@ BuildIortTable (
     PmcgOffset\r
     ));\r
 \r
+  if (TableSize > MAX_UINT32) {\r
+    Status = EFI_INVALID_PARAMETER;\r
+    DEBUG ((\r
+      DEBUG_ERROR,\r
+      "ERROR: IORT: IORT Table Size 0x%lx > MAX_UINT32," \\r
+      " Status = %r\n",\r
+      TableSize,\r
+      Status\r
+      ));\r
+    goto error_handler;\r
+  }\r
+\r
   // Allocate the Buffer for IORT table\r
   *Table = (EFI_ACPI_DESCRIPTION_HEADER*)AllocateZeroPool (TableSize);\r
   if (*Table == NULL) {\r
@@ -1783,7 +1937,7 @@ BuildIortTable (
 \r
   DEBUG ((\r
     DEBUG_INFO,\r
-    "IORT: Iort = 0x%p TableSize = 0x%x\n",\r
+    "IORT: Iort = 0x%p TableSize = 0x%lx\n",\r
     Iort,\r
     TableSize\r
     ));\r
@@ -1793,7 +1947,7 @@ BuildIortTable (
              This,\r
              &Iort->Header,\r
              AcpiTableInfo,\r
-             TableSize\r
+             (UINT32)TableSize\r
              );\r
   if (EFI_ERROR (Status)) {\r
     DEBUG ((\r