--- /dev/null
+/** @file\r
+ Device Path services. The thing to remember is device paths are built out of\r
+ nodes. The device path is terminated by an end node that is length\r
+ sizeof(EFI_DEVICE_PATH_PROTOCOL). That would be why there is sizeof(EFI_DEVICE_PATH_PROTOCOL)\r
+ all over this file.\r
+\r
+ The only place where multi-instance device paths are supported is in\r
+ environment varibles. Multi-instance device paths should never be placed\r
+ on a Handle.\r
+\r
+ Copyright (c) 2013, 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
+ http://opensource.org/licenses/bsd-license.php. \r
+\r
+ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
+ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
+\r
+**/\r
+\r
+\r
+#include "UefiDevicePathLib.h"\r
+\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
+ The constructor function caches the pointer to DevicePathUtilites protocol,\r
+ DevicePathToText protocol and DevicePathFromText protocol.\r
+ \r
+ The constructor function locates these three protocols from protocol database.\r
+ It will caches the pointer to local protocol instance if that operation fails\r
+ and it will always return EFI_SUCCESS. \r
+\r
+ @param ImageHandle The firmware allocated handle for the EFI image.\r
+ @param SystemTable A pointer to the EFI System Table.\r
+ \r
+ @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+UefiDevicePathLibOptionalDevicePathProtocolConstructor (\r
+ IN EFI_HANDLE ImageHandle,\r
+ IN EFI_SYSTEM_TABLE *SystemTable\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+\r
+ Status = gBS->LocateProtocol (\r
+ &gEfiDevicePathUtilitiesProtocolGuid,\r
+ NULL,\r
+ (VOID**) &mDevicePathLibDevicePathUtilities\r
+ );\r
+ ASSERT_EFI_ERROR (Status);\r
+ ASSERT (mDevicePathLibDevicePathUtilities != NULL);\r
+ return Status;\r
+}\r
+\r
+/**\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 \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 or invalid.\r
+ @retval Others The size of a device path in bytes.\r
+\r
+**/\r
+UINTN\r
+EFIAPI\r
+GetDevicePathSize (\r
+ IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
+ )\r
+{\r
+ if (mDevicePathLibDevicePathUtilities != NULL) {\r
+ return mDevicePathLibDevicePathUtilities->GetDevicePathSize (DevicePath);\r
+ } else {\r
+ return UefiDevicePathLibGetDevicePathSize (DevicePath);\r
+ }\r
+}\r
+\r
+/**\r
+ Creates a new copy of an existing device path.\r
+\r
+ This function allocates space for a new copy of the device path specified by DevicePath. \r
+ If DevicePath is NULL, then NULL is returned. If the memory is successfully \r
+ allocated, then the contents of DevicePath are copied to the newly allocated \r
+ buffer, and a pointer to that buffer is returned. Otherwise, NULL is returned. \r
+ The memory for the new device path is allocated from EFI boot services memory. \r
+ It is the responsibility of the caller to free the memory allocated. \r
+ \r
+ @param DevicePath A pointer to a device path data structure.\r
+\r
+ @retval NULL DevicePath is NULL or invalid.\r
+ @retval Others A pointer to the duplicated device path.\r
+ \r
+**/\r
+EFI_DEVICE_PATH_PROTOCOL *\r
+EFIAPI\r
+DuplicateDevicePath (\r
+ IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
+ )\r
+{\r
+ if (mDevicePathLibDevicePathUtilities != NULL) {\r
+ return mDevicePathLibDevicePathUtilities->DuplicateDevicePath (DevicePath);\r
+ } else {\r
+ return UefiDevicePathLibDuplicateDevicePath (DevicePath);\r
+ }\r
+}\r
+\r
+/**\r
+ Creates a new device path by appending a second device path to a first device path.\r
+\r
+ This function creates a new device path by appending a copy of SecondDevicePath \r
+ to a copy of FirstDevicePath in a newly allocated buffer. Only the end-of-device-path \r
+ device node from SecondDevicePath is retained. The newly created device path is \r
+ returned. If FirstDevicePath is NULL, then it is ignored, and a duplicate of \r
+ SecondDevicePath is returned. If SecondDevicePath is NULL, then it is ignored, \r
+ and a duplicate of FirstDevicePath is returned. If both FirstDevicePath and \r
+ SecondDevicePath are NULL, then a copy of an end-of-device-path is returned. \r
+ \r
+ If there is not enough memory for the newly allocated buffer, then NULL is returned.\r
+ The memory for the new device path is allocated from EFI boot services memory. \r
+ It is the responsibility of the caller to free the memory allocated.\r
+\r
+ @param FirstDevicePath A pointer to a device path data structure.\r
+ @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 SecondDevicePath are NULL.\r
+\r
+**/\r
+EFI_DEVICE_PATH_PROTOCOL *\r
+EFIAPI\r
+AppendDevicePath (\r
+ IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath, OPTIONAL\r
+ IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath OPTIONAL\r
+ )\r
+{\r
+ if (mDevicePathLibDevicePathUtilities != NULL) {\r
+ return mDevicePathLibDevicePathUtilities->AppendDevicePath (FirstDevicePath, SecondDevicePath);\r
+ } else {\r
+ return UefiDevicePathLibAppendDevicePath (FirstDevicePath, SecondDevicePath);\r
+ }\r
+}\r
+\r
+/**\r
+ Creates a new path by appending the device node to the device path.\r
+\r
+ This function creates a new device path by appending a copy of the device node \r
+ specified by DevicePathNode to a copy of the device path specified by DevicePath \r
+ in an allocated buffer. The end-of-device-path device node is moved after the \r
+ end of the appended device node.\r
+ If DevicePathNode is NULL then a copy of DevicePath is returned.\r
+ If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device \r
+ path device node is returned.\r
+ If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path \r
+ device node 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
+ of the caller to free the memory allocated.\r
+\r
+ @param DevicePath A pointer to a device path data structure.\r
+ @param DevicePathNode A pointer to a single device path node.\r
+\r
+ @retval NULL If there is not enough memory for the new device path.\r
+ @retval Others A pointer to the new device path if success.\r
+ A copy of DevicePathNode followed by an end-of-device-path node \r
+ if both FirstDevicePath and SecondDevicePath are NULL.\r
+ A copy of an end-of-device-path node if both FirstDevicePath \r
+ and SecondDevicePath are NULL.\r
+\r
+**/\r
+EFI_DEVICE_PATH_PROTOCOL *\r
+EFIAPI\r
+AppendDevicePathNode (\r
+ IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL\r
+ IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathNode OPTIONAL\r
+ )\r
+{\r
+ if (mDevicePathLibDevicePathUtilities != NULL) {\r
+ return mDevicePathLibDevicePathUtilities->AppendDeviceNode (DevicePath, DevicePathNode);\r
+ } else {\r
+ return UefiDevicePathLibAppendDevicePathNode (DevicePath, DevicePathNode);\r
+ }\r
+}\r
+\r
+/**\r
+ Creates a new device path by appending the specified device path instance to the specified device\r
+ path.\r
+ \r
+ This function creates a new device path by appending a copy of the device path \r
+ instance specified by DevicePathInstance to a copy of the device path specified \r
+ by DevicePath in a allocated buffer.\r
+ The end-of-device-path device node is moved after the end of the appended device \r
+ 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
+ of the caller to free the memory allocated.\r
+ \r
+ @param DevicePath A pointer to a device path data structure.\r
+ @param DevicePathInstance A pointer to a device path instance.\r
+\r
+ @return A pointer to the new device path.\r
+\r
+**/\r
+EFI_DEVICE_PATH_PROTOCOL *\r
+EFIAPI\r
+AppendDevicePathInstance (\r
+ IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL\r
+ IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathInstance OPTIONAL\r
+ )\r
+{\r
+ if (mDevicePathLibDevicePathUtilities != NULL) {\r
+ return mDevicePathLibDevicePathUtilities->AppendDevicePathInstance (DevicePath, DevicePathInstance);\r
+ } else {\r
+ return UefiDevicePathLibAppendDevicePathInstance (DevicePath, DevicePathInstance);\r
+ }\r
+}\r
+\r
+/**\r
+ Creates a copy of the current device path instance and returns a pointer to the next device path\r
+ instance.\r
+\r
+ This function creates a copy of the current device path instance. It also updates \r
+ DevicePath to point to the next device path instance in the device path (or NULL \r
+ if no more) and updates Size to hold the size of the device path instance copy.\r
+ If DevicePath is NULL, then NULL is returned.\r
+ If DevicePath points to a invalid device path, 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
+ of the caller to free the memory allocated.\r
+ If Size is NULL, then ASSERT().\r
+ \r
+ @param DevicePath On input, this holds the pointer to the current \r
+ device path instance. On output, this holds \r
+ the pointer to the next device path instance \r
+ or NULL if there are no more device path\r
+ instances in the device path pointer to a \r
+ device path data structure.\r
+ @param Size On output, this holds the size of the device \r
+ path instance, in bytes or zero, if DevicePath \r
+ is NULL.\r
+\r
+ @return A pointer to the current device path instance.\r
+\r
+**/\r
+EFI_DEVICE_PATH_PROTOCOL *\r
+EFIAPI\r
+GetNextDevicePathInstance (\r
+ IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath,\r
+ OUT UINTN *Size\r
+ )\r
+{\r
+ if (mDevicePathLibDevicePathUtilities != NULL) {\r
+ return mDevicePathLibDevicePathUtilities->GetNextDevicePathInstance (DevicePath, Size);\r
+ } else {\r
+ return UefiDevicePathLibGetNextDevicePathInstance (DevicePath, Size);\r
+ }\r
+}\r
+\r
+/**\r
+ Creates a device node.\r
+\r
+ This function creates a new device node in a newly allocated buffer of size \r
+ NodeLength and initializes the device path node header with NodeType and NodeSubType. \r
+ The new device path node is returned.\r
+ If NodeLength is smaller than a device path header, 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
+ 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
+ @param NodeLength The length of the new device node.\r
+\r
+ @return The new device path.\r
+\r
+**/\r
+EFI_DEVICE_PATH_PROTOCOL *\r
+EFIAPI\r
+CreateDeviceNode (\r
+ IN UINT8 NodeType,\r
+ IN UINT8 NodeSubType,\r
+ IN UINT16 NodeLength\r
+ )\r
+{\r
+ if (mDevicePathLibDevicePathUtilities != NULL) {\r
+ return mDevicePathLibDevicePathUtilities->CreateDeviceNode (NodeType, NodeSubType, NodeLength);\r
+ } else {\r
+ return UefiDevicePathLibCreateDeviceNode (NodeType, NodeSubType, NodeLength);\r
+ }\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
+ multi-instance.\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 or invalid.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+IsDevicePathMultiInstance (\r
+ IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
+ )\r
+{\r
+ if (mDevicePathLibDevicePathUtilities != NULL) {\r
+ return mDevicePathLibDevicePathUtilities->IsDevicePathMultiInstance (DevicePath);\r
+ } else {\r
+ return UefiDevicePathLibIsDevicePathMultiInstance (DevicePath);\r
+ }\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
+ }\r
+\r
+ return UefiDevicePathLibConvertDeviceNodeToText (DeviceNode, DisplayOnly, AllowShortcuts);\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
+ }\r
+\r
+ return UefiDevicePathLibConvertDevicePathToText (DevicePath, DisplayOnly, AllowShortcuts);\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
+ }\r
+\r
+ return UefiDevicePathLibConvertTextToDeviceNode (TextDeviceNode);\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
+ }\r
+\r
+ return UefiDevicePathLibConvertTextToDevicePath (TextDevicePath);\r
+}\r
+\r