]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/UefiDevicePathLibDevicePathProtocol/UefiDevicePathLib.c
Add new API IsDevicePathValid() to UefiDevicePathLib.
[mirror_edk2.git] / MdePkg / Library / UefiDevicePathLibDevicePathProtocol / UefiDevicePathLib.c
index 8275dd611a8a49d26f25d6f374a76f3888196c8b..1052988e7318a097f4caf8b7575df38558a29184 100644 (file)
@@ -2,7 +2,7 @@
   Library instance that implement UEFI Device Path Library class based on protocol\r
   gEfiDevicePathUtilitiesProtocolGuid.\r
 \r
-  Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2006 - 2012, 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
@@ -24,6 +24,7 @@
 #include <Library/MemoryAllocationLib.h>\r
 #include <Library/BaseMemoryLib.h>\r
 #include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/PcdLib.h>\r
 \r
 EFI_DEVICE_PATH_UTILITIES_PROTOCOL          *mDevicePathUtilities = NULL;\r
 \r
@@ -71,6 +72,61 @@ DevicePathLibConstructor (
   return Status;\r
 }\r
 \r
+/**\r
+  Determine whether a given device path is valid.\r
+  If DevicePath is NULL, then ASSERT().\r
+\r
+  @param  DevicePath  A pointer to a device path data structure.\r
+  @param  MaxSize     The maximum size of the device path data structure.\r
+\r
+  @retval TRUE        DevicePath is valid.\r
+  @retval FALSE       The length of any node node in the DevicePath is less\r
+                      than sizeof (EFI_DEVICE_PATH_PROTOCOL).\r
+  @retval FALSE       If MaxSize is not zero, the size of the DevicePath\r
+                      exceeds MaxSize.\r
+  @retval FALSE       If PcdMaximumDevicePathNodeCount is not zero, the node\r
+                      count of the DevicePath exceeds PcdMaximumDevicePathNodeCount.\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+IsDevicePathValid (\r
+  IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
+  IN       UINTN                    MaxSize\r
+  )\r
+{\r
+  UINTN Count;\r
+  UINTN Size;\r
+  UINTN NodeLength;\r
+\r
+  ASSERT (DevicePath != NULL);\r
+\r
+  for (Count = 0, Size = 0; !IsDevicePathEnd (DevicePath); DevicePath = NextDevicePathNode (DevicePath)) {\r
+    NodeLength = DevicePathNodeLength (DevicePath);\r
+    if (NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {\r
+      return FALSE;\r
+    }\r
+\r
+    if (MaxSize > 0) {\r
+      Size += NodeLength;\r
+      if (Size + END_DEVICE_PATH_LENGTH > MaxSize) {\r
+        return FALSE;\r
+      }\r
+    }\r
+\r
+    if (PcdGet32 (PcdMaximumDevicePathNodeCount) > 0) {\r
+      Count++;\r
+      if (Count >= PcdGet32 (PcdMaximumDevicePathNodeCount)) {\r
+        return FALSE;\r
+      }\r
+    }\r
+  }\r
+\r
+  //\r
+  // Only return TRUE when the End Device Path node is valid.\r
+  //\r
+  return (BOOLEAN) (DevicePathNodeLength (DevicePath) == END_DEVICE_PATH_LENGTH);\r
+}\r
+\r
 /**\r
   Returns the Type field of a device path node.\r
 \r
@@ -136,8 +192,12 @@ DevicePathNodeLength (
   IN CONST VOID  *Node\r
   )\r
 {\r
+  UINTN Length;\r
+\r
   ASSERT (Node != NULL);\r
-  return ReadUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0]);\r
+  Length = ReadUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0]);\r
+  ASSERT (Length >= sizeof (EFI_DEVICE_PATH_PROTOCOL));\r
+  return Length;\r
 }\r
 \r
 /**\r
@@ -256,7 +316,8 @@ IsDevicePathEndInstance (
   be used to set the contents of the Length field.\r
 \r
   If Node is NULL, then ASSERT().\r
-  If NodeLength >= 0x10000, then ASSERT().\r
+  If NodeLength >= SIZE_64KB, then ASSERT().\r
+  If NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL), then ASSERT().\r
 \r
   @param  Node      A pointer to a device path node data structure.\r
   @param  Length    The length, in bytes, of the device path node.\r
@@ -272,7 +333,7 @@ SetDevicePathNodeLength (
   )\r
 {\r
   ASSERT (Node != NULL);\r
-  ASSERT (Length < 0x10000);\r
+  ASSERT ((Length >= sizeof (EFI_DEVICE_PATH_PROTOCOL)) && (Length < SIZE_64KB));\r
   return WriteUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0], (UINT16)(Length));\r
 }\r
 \r
@@ -305,13 +366,14 @@ SetDevicePathEndNode (
 /**\r
   Returns the size of a device path in bytes.\r
 \r
-  This function returns the size, in bytes, of the device path data structure specified by\r
-  DevicePath including the end of device path node.  If DevicePath is NULL, then 0 is returned.\r
+  This function returns the size, in bytes, of the device path data structure \r
+  specified by DevicePath including the end of device path node.\r
+  If DevicePath is NULL or invalid, then 0 is returned.\r
 \r
-  @param  DevicePath                 A pointer to a device path data structure.\r
-  \r
-  @retval 0       If DevicePath is NULL.\r
-  @retval Others  The size of a device path in bytes.\r
+  @param  DevicePath  A pointer to a device path data structure.\r
+\r
+  @retval 0           If DevicePath is NULL or invalid.\r
+  @retval Others      The size of a device path in bytes.\r
 \r
 **/\r
 UINTN\r
@@ -336,7 +398,7 @@ GetDevicePathSize (
   \r
   @param  DevicePath                 A pointer to a device path data structure.\r
 \r
-  @retval NULL    If DevicePath is NULL.\r
+  @retval NULL    If DevicePath is NULL or invalid.\r
   @retval Others  A pointer to the duplicated device path.\r
   \r
 **/\r
@@ -367,6 +429,7 @@ DuplicateDevicePath (
   @param  SecondDevicePath           A pointer to a device path data structure.\r
   \r
   @retval NULL      If there is not enough memory for the newly allocated buffer.\r
+  @retval NULL      If FirstDevicePath or SecondDevicePath is invalid.\r
   @retval Others    A pointer to the new device path if success.\r
                     Or a copy an end-of-device-path if both FirstDevicePath and \r
                     SecondDevicePath are NULL.\r
@@ -431,6 +494,7 @@ AppendDevicePathNode (
   path instance and a new end-of-device-path-instance node is inserted between. \r
   If DevicePath is NULL, then a copy if DevicePathInstance is returned.\r
   If DevicePathInstance is NULL, then NULL is returned.\r
+  If DevicePath or DevicePathInstance is invalid, then NULL is returned.\r
   If there is not enough memory to allocate space for the new device path, then \r
   NULL is returned.   \r
   The memory is allocated from EFI boot services memory. It is the responsibility \r
@@ -524,15 +588,16 @@ CreateDeviceNode (
 /**\r
   Determines if a device path is single or multi-instance.\r
 \r
-  This function returns TRUE if the device path specified by DevicePath is \r
+  This function returns TRUE if the device path specified by DevicePath is\r
   multi-instance.\r
-  Otherwise, FALSE is returned.  If DevicePath is NULL, then FALSE is returned.\r
+  Otherwise, FALSE is returned.\r
+  If DevicePath is NULL or invalid, then FALSE is returned.\r
 \r
   @param  DevicePath                 A pointer to a device path data structure.\r
 \r
   @retval  TRUE                      DevicePath is multi-instance.\r
-  @retval  FALSE                     DevicePath is not multi-instance or DevicePath \r
-                                     is NULL.\r
+  @retval  FALSE                     DevicePath is not multi-instance, or DevicePath \r
+                                     is NULL or invalid.\r
 \r
 **/\r
 BOOLEAN\r