]> 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 1052988e7318a097f4caf8b7575df38558a29184..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 - 2012, 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
@@ -26,7 +28,9 @@
 #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
@@ -64,11 +68,10 @@ 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
@@ -192,12 +195,8 @@ DevicePathNodeLength (
   IN CONST VOID  *Node\r
   )\r
 {\r
-  UINTN Length;\r
-\r
   ASSERT (Node != NULL);\r
-  Length = ReadUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0]);\r
-  ASSERT (Length >= sizeof (EFI_DEVICE_PATH_PROTOCOL));\r
-  return Length;\r
+  return ReadUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0]);\r
 }\r
 \r
 /**\r
@@ -382,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
@@ -408,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
@@ -442,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
@@ -480,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
@@ -513,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
@@ -551,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
@@ -564,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
@@ -582,7 +580,7 @@ CreateDeviceNode (
   IN UINT16                          NodeLength\r
   )\r
 {\r
-  return mDevicePathUtilities->CreateDeviceNode (NodeType, NodeSubType, NodeLength);\r
+  return mDevicePathLibDevicePathUtilities->CreateDeviceNode (NodeType, NodeSubType, NodeLength);\r
 }\r
 \r
 /**\r
@@ -606,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
@@ -698,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