]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/UefiDevicePathLibDevicePathProtocol/UefiDevicePathLib.c
Do not assert when the device path node length is invalid.
[mirror_edk2.git] / MdePkg / Library / UefiDevicePathLibDevicePathProtocol / UefiDevicePathLib.c
index 8275dd611a8a49d26f25d6f374a76f3888196c8b..5cc4b35fe492e3ed9fddc40799a109add92684b7 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 - 2014, 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
@@ -17,6 +17,8 @@
 #include <Uefi.h>\r
 \r
 #include <Protocol/DevicePathUtilities.h>\r
+#include <Protocol/DevicePathToText.h>\r
+#include <Protocol/DevicePathFromText.h>\r
 \r
 #include <Library/DevicePathLib.h>\r
 #include <Library/DebugLib.h>\r
 #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
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_DEVICE_PATH_UTILITIES_PROTOCOL *mDevicePathLibDevicePathUtilities = NULL;\r
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_DEVICE_PATH_TO_TEXT_PROTOCOL   *mDevicePathLibDevicePathToText    = NULL;\r
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL *mDevicePathLibDevicePathFromText  = NULL;\r
 \r
 //\r
 // Template for an end-of-device path node.\r
@@ -63,14 +68,68 @@ DevicePathLibConstructor (
   Status = gBS->LocateProtocol (\r
                   &gEfiDevicePathUtilitiesProtocolGuid,\r
                   NULL,\r
-                  (VOID**) &mDevicePathUtilities\r
+                  (VOID**) &mDevicePathLibDevicePathUtilities\r
                   );\r
   ASSERT_EFI_ERROR (Status);\r
-  ASSERT (mDevicePathUtilities != NULL);\r
-\r
+  ASSERT (mDevicePathLibDevicePathUtilities != NULL);\r
   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
@@ -256,7 +315,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 +332,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 +365,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
@@ -320,7 +381,7 @@ GetDevicePathSize (
   IN CONST EFI_DEVICE_PATH_PROTOCOL  *DevicePath\r
   )\r
 {\r
-  return mDevicePathUtilities->GetDevicePathSize (DevicePath);\r
+  return mDevicePathLibDevicePathUtilities->GetDevicePathSize (DevicePath);\r
 }\r
 \r
 /**\r
@@ -336,7 +397,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
@@ -346,7 +407,7 @@ DuplicateDevicePath (
   IN CONST EFI_DEVICE_PATH_PROTOCOL  *DevicePath\r
   )\r
 {\r
-  return mDevicePathUtilities->DuplicateDevicePath (DevicePath);\r
+  return mDevicePathLibDevicePathUtilities->DuplicateDevicePath (DevicePath);\r
 }\r
 \r
 /**\r
@@ -367,6 +428,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
@@ -379,7 +441,7 @@ AppendDevicePath (
   IN CONST EFI_DEVICE_PATH_PROTOCOL  *SecondDevicePath  OPTIONAL\r
   )\r
 {\r
-  return mDevicePathUtilities->AppendDevicePath (FirstDevicePath, SecondDevicePath);\r
+  return mDevicePathLibDevicePathUtilities->AppendDevicePath (FirstDevicePath, SecondDevicePath);\r
 }\r
 \r
 /**\r
@@ -417,7 +479,7 @@ AppendDevicePathNode (
   IN CONST EFI_DEVICE_PATH_PROTOCOL  *DevicePathNode  OPTIONAL\r
   )\r
 {\r
-  return mDevicePathUtilities->AppendDeviceNode (DevicePath, DevicePathNode);\r
+  return mDevicePathLibDevicePathUtilities->AppendDeviceNode (DevicePath, DevicePathNode);\r
 }\r
 \r
 /**\r
@@ -431,6 +493,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
@@ -449,7 +512,7 @@ AppendDevicePathInstance (
   IN CONST EFI_DEVICE_PATH_PROTOCOL  *DevicePathInstance OPTIONAL\r
   )\r
 {\r
-  return mDevicePathUtilities->AppendDevicePathInstance (DevicePath, DevicePathInstance);\r
+  return mDevicePathLibDevicePathUtilities->AppendDevicePathInstance (DevicePath, DevicePathInstance);\r
 }\r
 \r
 /**\r
@@ -487,7 +550,7 @@ GetNextDevicePathInstance (
   )\r
 {\r
   ASSERT (Size != NULL);\r
-  return mDevicePathUtilities->GetNextDevicePathInstance (DevicePath, Size);\r
+  return mDevicePathLibDevicePathUtilities->GetNextDevicePathInstance (DevicePath, Size);\r
 }\r
 \r
 /**\r
@@ -500,8 +563,7 @@ GetNextDevicePathInstance (
   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
-  of the caller to\r
-  free the memory allocated.\r
+  of the caller to free the memory allocated.\r
 \r
   @param  NodeType                   The device node type for the new device node.\r
   @param  NodeSubType                The device node sub-type for the new device node.\r
@@ -518,21 +580,22 @@ CreateDeviceNode (
   IN UINT16                          NodeLength\r
   )\r
 {\r
-  return mDevicePathUtilities->CreateDeviceNode (NodeType, NodeSubType, NodeLength);\r
+  return mDevicePathLibDevicePathUtilities->CreateDeviceNode (NodeType, NodeSubType, NodeLength);\r
 }\r
 \r
 /**\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
@@ -541,7 +604,7 @@ IsDevicePathMultiInstance (
   IN CONST EFI_DEVICE_PATH_PROTOCOL  *DevicePath\r
   )\r
 {\r
-  return mDevicePathUtilities->IsDevicePathMultiInstance (DevicePath);\r
+  return mDevicePathLibDevicePathUtilities->IsDevicePathMultiInstance (DevicePath);\r
 }\r
 \r
 /**\r
@@ -633,3 +696,151 @@ FileDevicePath (
 \r
   return DevicePath;\r
 }\r
+\r
+/**\r
+  Locate and return the protocol instance identified by the ProtocolGuid.\r
+\r
+  @param ProtocolGuid     The GUID of the protocol.\r
+\r
+  @return A pointer to the protocol instance or NULL when absent.\r
+**/\r
+VOID *\r
+UefiDevicePathLibLocateProtocol (\r
+  EFI_GUID                         *ProtocolGuid\r
+  )\r
+{\r
+  EFI_STATUS Status;\r
+  VOID       *Protocol;\r
+  Status = gBS->LocateProtocol (\r
+                  ProtocolGuid,\r
+                  NULL,\r
+                  (VOID**) &Protocol\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return NULL;\r
+  } else {\r
+    return Protocol;\r
+  }\r
+}\r
+\r
+/**\r
+  Converts a device node to its string representation.\r
+\r
+  @param DeviceNode        A Pointer to the device node to be converted.\r
+  @param DisplayOnly       If DisplayOnly is TRUE, then the shorter text representation\r
+                           of the display node is used, where applicable. If DisplayOnly\r
+                           is FALSE, then the longer text representation of the display node\r
+                           is used.\r
+  @param AllowShortcuts    If AllowShortcuts is TRUE, then the shortcut forms of text\r
+                           representation for a device node can be used, where applicable.\r
+\r
+  @return A pointer to the allocated text representation of the device node or NULL if DeviceNode\r
+          is NULL or there was insufficient memory.\r
+\r
+**/\r
+CHAR16 *\r
+EFIAPI\r
+ConvertDeviceNodeToText (\r
+  IN CONST EFI_DEVICE_PATH_PROTOCOL  *DeviceNode,\r
+  IN BOOLEAN                         DisplayOnly,\r
+  IN BOOLEAN                         AllowShortcuts\r
+  )\r
+{\r
+  if (mDevicePathLibDevicePathToText == NULL) {\r
+    mDevicePathLibDevicePathToText = UefiDevicePathLibLocateProtocol (&gEfiDevicePathToTextProtocolGuid);\r
+  }\r
+  if (mDevicePathLibDevicePathToText != NULL) {\r
+    return mDevicePathLibDevicePathToText->ConvertDeviceNodeToText (DeviceNode, DisplayOnly, AllowShortcuts);\r
+  } else {\r
+    return NULL;\r
+  }\r
+}\r
+\r
+/**\r
+  Converts a device path to its text representation.\r
+\r
+  @param DevicePath      A Pointer to the device to be converted.\r
+  @param DisplayOnly     If DisplayOnly is TRUE, then the shorter text representation\r
+                         of the display node is used, where applicable. If DisplayOnly\r
+                         is FALSE, then the longer text representation of the display node\r
+                         is used.\r
+  @param AllowShortcuts  If AllowShortcuts is TRUE, then the shortcut forms of text\r
+                         representation for a device node can be used, where applicable.\r
+\r
+  @return A pointer to the allocated text representation of the device path or\r
+          NULL if DeviceNode is NULL or there was insufficient memory.\r
+\r
+**/\r
+CHAR16 *\r
+EFIAPI\r
+ConvertDevicePathToText (\r
+  IN CONST EFI_DEVICE_PATH_PROTOCOL   *DevicePath,\r
+  IN BOOLEAN                          DisplayOnly,\r
+  IN BOOLEAN                          AllowShortcuts\r
+  )\r
+{\r
+  if (mDevicePathLibDevicePathToText == NULL) {\r
+    mDevicePathLibDevicePathToText = UefiDevicePathLibLocateProtocol (&gEfiDevicePathToTextProtocolGuid);\r
+  }\r
+  if (mDevicePathLibDevicePathToText != NULL) {\r
+    return mDevicePathLibDevicePathToText->ConvertDevicePathToText (DevicePath, DisplayOnly, AllowShortcuts);\r
+  } else {\r
+    return NULL;\r
+  }\r
+}\r
+\r
+/**\r
+  Convert text to the binary representation of a device node.\r
+\r
+  @param TextDeviceNode  TextDeviceNode points to the text representation of a device\r
+                         node. Conversion starts with the first character and continues\r
+                         until the first non-device node character.\r
+\r
+  @return A pointer to the EFI device node or NULL if TextDeviceNode is NULL or there was\r
+          insufficient memory or text unsupported.\r
+\r
+**/\r
+EFI_DEVICE_PATH_PROTOCOL *\r
+EFIAPI\r
+ConvertTextToDeviceNode (\r
+  IN CONST CHAR16 *TextDeviceNode\r
+  )\r
+{\r
+  if (mDevicePathLibDevicePathFromText == NULL) {\r
+    mDevicePathLibDevicePathFromText = UefiDevicePathLibLocateProtocol (&gEfiDevicePathFromTextProtocolGuid);\r
+  }\r
+  if (mDevicePathLibDevicePathFromText != NULL) {\r
+    return mDevicePathLibDevicePathFromText->ConvertTextToDeviceNode (TextDeviceNode);\r
+  } else {\r
+    return NULL;\r
+  }\r
+}\r
+\r
+/**\r
+  Convert text to the binary representation of a device path.\r
+\r
+\r
+  @param TextDevicePath  TextDevicePath points to the text representation of a device\r
+                         path. Conversion starts with the first character and continues\r
+                         until the first non-device node character.\r
+\r
+  @return A pointer to the allocated device path or NULL if TextDeviceNode is NULL or\r
+          there was insufficient memory.\r
+\r
+**/\r
+EFI_DEVICE_PATH_PROTOCOL *\r
+EFIAPI\r
+ConvertTextToDevicePath (\r
+  IN CONST CHAR16 *TextDevicePath\r
+  )\r
+{\r
+  if (mDevicePathLibDevicePathFromText == NULL) {\r
+    mDevicePathLibDevicePathFromText = UefiDevicePathLibLocateProtocol (&gEfiDevicePathFromTextProtocolGuid);\r
+  }\r
+  if (mDevicePathLibDevicePathFromText != NULL) {\r
+    return mDevicePathLibDevicePathFromText->ConvertTextToDevicePath (TextDevicePath);\r
+  } else {\r
+    return NULL;\r
+  }\r
+}\r
+\r