]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.c
Spec checked
[mirror_edk2.git] / MdePkg / Library / UefiDevicePathLib / UefiDevicePathLib.c
index 31a0462f079beaffd47e9a8ed8fac8572b9da1e9..15b16a237adabb3323da54f06ff95955abd4a79f 100644 (file)
@@ -8,7 +8,7 @@
   environment varibles. Multi-instance device paths should never be placed\r
   on a Handle.\r
 \r
-  Copyright (c) 2006, Intel Corporation                                                         \r
+  Copyright (c) 2006 - 2008, Intel Corporation                                                         \r
   All rights reserved. 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
   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
-  Module Name:  UefiDevicePathLib.c\r
+**/\r
+\r
+\r
+#include <Uefi.h>\r
+\r
+#include <Protocol/DevicePath.h>\r
+\r
+#include <Library/DevicePathLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/BaseLib.h>\r
+\r
+//\r
+// Template for an end-of-device path node.\r
+//\r
+GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_DEVICE_PATH_PROTOCOL  mUefiDevicePathLibEndDevicePath = {\r
+  END_DEVICE_PATH_TYPE,\r
+  END_ENTIRE_DEVICE_PATH_SUBTYPE,\r
+  {\r
+    END_DEVICE_PATH_LENGTH,\r
+    0\r
+  }\r
+};\r
+\r
+/**\r
+  Returns the Type field of a device path node.\r
+\r
+  Returns the Type field of the device path node specified by Node.\r
+\r
+  If Node is NULL, then ASSERT().\r
+\r
+  @param  Node      A pointer to a device path node data structure.\r
+\r
+  @return The Type field of the device path node specified by Node.\r
 \r
 **/\r
+UINT8\r
+DevicePathType (\r
+  IN CONST VOID  *Node\r
+  )\r
+{\r
+  ASSERT (Node != NULL);\r
+  return ((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Type;\r
+}\r
+\r
+/**\r
+  Returns the SubType field of a device path node.\r
+\r
+  Returns the SubType field of the device path node specified by Node.\r
+\r
+  If Node is NULL, then ASSERT().\r
+\r
+  @param  Node      A pointer to a device path node data structure.\r
+\r
+  @return The SubType field of the device path node specified by Node.\r
+\r
+**/\r
+UINT8\r
+DevicePathSubType (\r
+  IN CONST VOID  *Node\r
+  )\r
+{\r
+  ASSERT (Node != NULL);\r
+  return ((EFI_DEVICE_PATH_PROTOCOL *)(Node))->SubType;\r
+}\r
+\r
+/**\r
+  Returns the 16-bit Length field of a device path node.\r
+\r
+  Returns the 16-bit Length field of the device path node specified by Node.  \r
+  Node is not required to be aligned on a 16-bit boundary, so it is recommended\r
+  that a function such as ReadUnaligned16() be used to extract the contents of \r
+  the Length field.\r
+\r
+  If Node is NULL, then ASSERT().\r
+\r
+  @param  Node      A pointer to a device path node data structure.\r
+\r
+  @return The 16-bit Length field of the device path node specified by Node.\r
+\r
+**/\r
+UINTN\r
+DevicePathNodeLength (\r
+  IN CONST VOID  *Node\r
+  )\r
+{\r
+  ASSERT (Node != NULL);\r
+  return ReadUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0]);\r
+}\r
+\r
+/**\r
+  Returns a pointer to the next node in a device path.\r
+\r
+  Returns a pointer to the device path node that follows the device path node specified by Node.\r
+\r
+  If Node is NULL, then ASSERT().\r
+\r
+  @param  Node      A pointer to a device path node data structure.\r
+\r
+  @return a pointer to the device path node that follows the device path node specified by Node.\r
+\r
+**/\r
+EFI_DEVICE_PATH_PROTOCOL *\r
+NextDevicePathNode (\r
+  IN CONST VOID  *Node\r
+  )\r
+{\r
+  ASSERT (Node != NULL);\r
+  return (EFI_DEVICE_PATH_PROTOCOL *)((UINT8 *)(Node) + DevicePathNodeLength(Node));\r
+}\r
+\r
+/**\r
+  Determines if a device path node is an end node of a device path.\r
+  This includes nodes that are the end of a device path instance and nodes that are the end of an entire device path.\r
+\r
+  Determines if the device path node specified by Node is an end node of a device path.  \r
+  This includes nodes that are the end of a device path instance and nodes that are the \r
+  end of an entire device path.  If Node represents an end node of a device path, \r
+  then TRUE is returned.  Otherwise, FALSE is returned.\r
+\r
+  If Node is NULL, then ASSERT().\r
+\r
+  @param  Node      A pointer to a device path node data structure.\r
+\r
+  @retval TRUE      The device path node specified by Node is an end node of a device path.\r
+  @retval FALSE     The device path node specified by Node is not an end node of a device path.\r
+  \r
+**/\r
+BOOLEAN\r
+IsDevicePathEndType (\r
+  IN CONST VOID  *Node\r
+  )\r
+{\r
+  ASSERT (Node != NULL);\r
+  return (BOOLEAN) ((DevicePathType (Node) & 0x7f) == END_DEVICE_PATH_TYPE);\r
+}\r
+\r
+/**\r
+  Determines if a device path node is an end node of an entire device path.\r
+\r
+  Determines if a device path node specified by Node is an end node of an entire device path.\r
+  If Node represents the end of an entire device path, then TRUE is returned.  Otherwise, FALSE is returned.\r
+\r
+  If Node is NULL, then ASSERT().\r
+\r
+  @param  Node      A pointer to a device path node data structure.\r
+\r
+  @retval TRUE      The device path node specified by Node is the end of an entire device path.\r
+  @retval FALSE     The device path node specified by Node is not the end of an entire device path.\r
+\r
+**/\r
+BOOLEAN\r
+IsDevicePathEnd (\r
+  IN CONST VOID  *Node\r
+  )\r
+{\r
+  ASSERT (Node != NULL);\r
+  return (BOOLEAN) (IsDevicePathEndType (Node) && DevicePathSubType(Node) == END_ENTIRE_DEVICE_PATH_SUBTYPE);\r
+}\r
+\r
+/**\r
+  Determines if a device path node is an end node of a device path instance.\r
+\r
+  Determines if a device path node specified by Node is an end node of a device path instance.\r
+  If Node represents the end of a device path instance, then TRUE is returned.  Otherwise, FALSE is returned.\r
+\r
+  If Node is NULL, then ASSERT().\r
+\r
+  @param  Node      A pointer to a device path node data structure.\r
+\r
+  @retval TRUE      The device path node specified by Node is the end of a device path instance.\r
+  @retval FALSE     The device path node specified by Node is not the end of a device path instance.\r
+\r
+**/\r
+BOOLEAN\r
+IsDevicePathEndInstance (\r
+  IN CONST VOID  *Node\r
+  )\r
+{\r
+  ASSERT (Node != NULL);\r
+  return (BOOLEAN) (IsDevicePathEndType (Node) && DevicePathSubType(Node) == END_INSTANCE_DEVICE_PATH_SUBTYPE);\r
+}\r
+\r
+/**\r
+  Sets the length, in bytes, of a device path node.\r
+\r
+  Sets the length of the device path node specified by Node to the value specified \r
+  by NodeLength.  NodeLength is returned.  Node is not required to be aligned on \r
+  a 16-bit boundary, so it is recommended that a function such as WriteUnaligned16()\r
+  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
+\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
+\r
+  @return Length\r
+\r
+**/\r
+UINT16\r
+SetDevicePathNodeLength (\r
+  IN OUT VOID  *Node,\r
+  IN UINTN     Length\r
+  )\r
+{\r
+  ASSERT (Node != NULL);\r
+  ASSERT (Length < 0x10000);\r
+  return WriteUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0], (UINT16)(Length));\r
+}\r
+\r
+/**\r
+  Fills in all the fields of a device path node that is the end of an entire device path.\r
+\r
+  Fills in all the fields of a device path node specified by Node so Node represents \r
+  the end of an entire device path.  The Type field of Node is set to \r
+  END_DEVICE_PATH_TYPE, the SubType field of Node is set to \r
+  END_ENTIRE_DEVICE_PATH_SUBTYPE, and the Length field of Node is set to \r
+  END_DEVICE_PATH_LENGTH.  Node is not required to be aligned on a 16-bit boundary, \r
+  so it is recommended that a function such as WriteUnaligned16() be used to set \r
+  the contents of the Length field. \r
+\r
+  If Node is NULL, then ASSERT(). \r
+\r
+  @param  Node      A pointer to a device path node data structure.\r
+\r
+**/\r
+VOID\r
+SetDevicePathEndNode (\r
+  OUT VOID  *Node\r
+  )\r
+{\r
+  ASSERT (Node != NULL);\r
+  CopyMem (Node, &mUefiDevicePathLibEndDevicePath, sizeof (mUefiDevicePathLibEndDevicePath));\r
+}\r
 \r
 /**\r
   Returns the size of a device path in bytes.\r
   DevicePath including the end of device path node.  If DevicePath is NULL, then 0 is returned.\r
 \r
   @param  DevicePath                 A pointer to a device path data structure.\r
-\r
-  @return The size of a device path in bytes.\r
+  \r
+  @retval 0       If DevicePath is NULL.\r
+  @retval Others  The size of a device path in bytes.\r
 \r
 **/\r
 UINTN\r
@@ -48,28 +283,31 @@ GetDevicePathSize (
   // Search for the end of the device path structure\r
   //\r
   Start = DevicePath;\r
-  while (!EfiIsDevicePathEnd (DevicePath)) {\r
-    DevicePath = EfiNextDevicePathNode (DevicePath);\r
+  while (!IsDevicePathEnd (DevicePath)) {\r
+    DevicePath = NextDevicePathNode (DevicePath);\r
   }\r
 \r
   //\r
   // Compute the size and add back in the size of the end device path structure\r
   //\r
-  return ((UINTN) DevicePath - (UINTN) Start) + sizeof (EFI_DEVICE_PATH_PROTOCOL);\r
+  return ((UINTN) DevicePath - (UINTN) Start) + DevicePathNodeLength (DevicePath);\r
 }\r
 \r
 /**\r
-  Creates a new device path by appending a second device path to a first device path.\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.  If\r
   DevicePath is NULL, then NULL is returned.  If the memory is successfully allocated, then the\r
   contents of DevicePath are copied to the newly allocated buffer, and a pointer to that buffer\r
   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
-  @return A pointer to the duplicated device path.\r
-\r
+  @retval NULL    If DevicePath is NULL.\r
+  @retval Others  A pointer to the duplicated device path.\r
+  \r
 **/\r
 EFI_DEVICE_PATH_PROTOCOL *\r
 EFIAPI\r
@@ -77,7 +315,6 @@ DuplicateDevicePath (
   IN CONST EFI_DEVICE_PATH_PROTOCOL  *DevicePath\r
   )\r
 {\r
-  EFI_DEVICE_PATH_PROTOCOL  *NewDevicePath;\r
   UINTN                     Size;\r
 \r
   //\r
@@ -91,9 +328,8 @@ DuplicateDevicePath (
   //\r
   // Allocate space for duplicate device path\r
   //\r
-  NewDevicePath = AllocateCopyPool (Size, DevicePath);\r
 \r
-  return NewDevicePath;\r
+  return AllocateCopyPool (Size, DevicePath);\r
 }\r
 \r
 /**\r
@@ -104,15 +340,18 @@ DuplicateDevicePath (
   SecondDevicePath is retained. The newly created device path is returned.  \r
   If FirstDevicePath is NULL, then it is ignored, and a duplicate of SecondDevicePath is returned.  \r
   If SecondDevicePath is NULL, then it is ignored, and a duplicate of FirstDevicePath is returned.  \r
-  If both FirstDevicePath and SecondDevicePath are NULL, then NULL is returned.  \r
+  If both FirstDevicePath and SecondDevicePath are NULL, then a copy of an end-of-device-path is\r
+  returned.  \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. It is the\r
   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
-  @return A pointer to the new device path.\r
+  \r
+  @retval NULL      If there is not enough memory for the newly allocated buffer.\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
@@ -132,7 +371,7 @@ AppendDevicePath (
   // If there's only 1 path, just duplicate it.\r
   //\r
   if (FirstDevicePath == NULL) {\r
-    return DuplicateDevicePath (SecondDevicePath);\r
+    return DuplicateDevicePath ((SecondDevicePath != NULL) ? SecondDevicePath : &mUefiDevicePathLibEndDevicePath);\r
   }\r
 \r
   if (SecondDevicePath == NULL) {\r
@@ -145,7 +384,7 @@ AppendDevicePath (
   //\r
   Size1         = GetDevicePathSize (FirstDevicePath);\r
   Size2         = GetDevicePathSize (SecondDevicePath);\r
-  Size          = Size1 + Size2 - sizeof (EFI_DEVICE_PATH_PROTOCOL);\r
+  Size          = Size1 + Size2 - END_DEVICE_PATH_LENGTH;\r
 \r
   NewDevicePath = AllocatePool (Size);\r
 \r
@@ -155,7 +394,7 @@ AppendDevicePath (
     // Over write FirstDevicePath EndNode and do the copy\r
     //\r
     DevicePath2 = (EFI_DEVICE_PATH_PROTOCOL *) ((CHAR8 *) NewDevicePath +\r
-                  (Size1 - sizeof (EFI_DEVICE_PATH_PROTOCOL)));\r
+                  (Size1 - END_DEVICE_PATH_LENGTH));\r
     CopyMem (DevicePath2, SecondDevicePath, Size2);\r
   }\r
 \r
@@ -168,8 +407,11 @@ AppendDevicePath (
   This function creates a new device path by appending a copy of the device node specified by\r
   DevicePathNode to a copy of the device path specified by DevicePath in an allocated buffer.\r
   The end-of-device-path device node is moved after the end of the appended device node.\r
-  If DevicePath is NULL, then NULL is returned.\r
-  If DevicePathNode is NULL, then NULL is returned.\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 path device\r
+  node is returned.\r
+  If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path device node\r
+  is returned.\r
   If there is not enough memory to allocate space for the new device path, then NULL is returned.  \r
   The memory is allocated from EFI boot services memory. It is the responsibility of the caller to\r
   free the memory allocated.\r
@@ -177,7 +419,11 @@ AppendDevicePath (
   @param  DevicePath                 A pointer to a device path data structure.\r
   @param  DevicePathNode             A pointer to a single device path node.\r
 \r
-  @return A pointer to the new device path.\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 and SecondDevicePath are NULL.\r
 \r
 **/\r
 EFI_DEVICE_PATH_PROTOCOL *\r
@@ -192,15 +438,15 @@ AppendDevicePathNode (
   EFI_DEVICE_PATH_PROTOCOL  *NewDevicePath;\r
   UINTN                     NodeLength;\r
 \r
-  if (DevicePath == NULL || DevicePathNode == NULL) {\r
-    return NULL;\r
+  if (DevicePathNode == NULL) {\r
+    return DuplicateDevicePath ((DevicePath != NULL) ? DevicePath : &mUefiDevicePathLibEndDevicePath);\r
   }\r
   //\r
   // Build a Node that has a terminator on it\r
   //\r
   NodeLength = DevicePathNodeLength (DevicePathNode);\r
 \r
-  TempDevicePath = AllocatePool (NodeLength + sizeof (EFI_DEVICE_PATH_PROTOCOL));\r
+  TempDevicePath = AllocatePool (NodeLength + END_DEVICE_PATH_LENGTH);\r
   if (TempDevicePath == NULL) {\r
     return NULL;\r
   }\r
@@ -357,8 +603,7 @@ GetNextDevicePathInstance (
 }\r
 \r
 /**\r
-  Creates a copy of the current device path instance and returns a pointer to the next device path\r
-  instance.\r
+  Creates a device node.\r
 \r
   This function creates a new device node in a newly allocated buffer of size NodeLength and\r
   initializes the device path node header with NodeType and NodeSubType.  The new device path node\r
@@ -392,10 +637,11 @@ CreateDeviceNode (
     return NULL;\r
   }\r
  \r
-  DevicePath = AllocatePool (NodeLength);\r
+  DevicePath = AllocateZeroPool (NodeLength);\r
   if (DevicePath != NULL) {\r
      DevicePath->Type    = NodeType;\r
      DevicePath->SubType = NodeSubType;\r
+     SetDevicePathNodeLength (DevicePath, NodeLength);\r
   }\r
 \r
   return DevicePath;\r
@@ -426,12 +672,12 @@ IsDevicePathMultiInstance (
   }\r
 \r
   Node = DevicePath;\r
-  while (!EfiIsDevicePathEnd (Node)) {\r
-    if (EfiIsDevicePathEndInstance (Node)) {\r
+  while (!IsDevicePathEnd (Node)) {\r
+    if (IsDevicePathEndInstance (Node)) {\r
       return TRUE;\r
     }\r
 \r
-    Node = EfiNextDevicePathNode (Node);\r
+    Node = NextDevicePathNode (Node);\r
   }\r
 \r
   return FALSE;\r
@@ -477,7 +723,11 @@ DevicePathFromHandle (
   handle Device.  The allocated device path is returned.  If Device is NULL or Device is a handle\r
   that does not support the device path protocol, then a device path containing a single device\r
   path node for the file specified by FileName is allocated and returned.\r
+  The memory for the new device path is allocated from EFI boot services memory. It is the responsibility\r
+  of the caller to free the memory allocated.\r
+  \r
   If FileName is NULL, then ASSERT().\r
+  If FileName is not aligned on a 16-bit boundary, then ASSERT().\r
 \r
   @param  Device                     A pointer to a device handle.  This parameter is optional and\r
                                      may be NULL.\r
@@ -493,27 +743,30 @@ FileDevicePath (
   IN CONST CHAR16                    *FileName\r
   )\r
 {\r
-  UINT                    Size;\r
+  UINT16                    Size;\r
   FILEPATH_DEVICE_PATH      *FilePath;\r
   EFI_DEVICE_PATH_PROTOCOL  *DevicePath;\r
-  EFI_DEVICE_PATH_PROTOCOL  *FileDevicePathNode;\r
+  EFI_DEVICE_PATH_PROTOCOL  *FileDevicePath;\r
 \r
   DevicePath = NULL;\r
 \r
-  Size = StrSize (FileName);\r
-  FileDevicePathNode = CreateDeviceNode (\r
-                         MEDIA_DEVICE_PATH,\r
-                         MEDIA_FILEPATH_DP,\r
-                         (UINT16) (Size + SIZE_OF_FILEPATH_DEVICE_PATH)\r
-                         );\r
-  if (FileDevicePathNode != NULL) {\r
-    FilePath = (FILEPATH_DEVICE_PATH *) FileDevicePathNode;\r
+  Size = (UINT16) StrSize (FileName);\r
+  \r
+  FileDevicePath = AllocatePool (Size + SIZE_OF_FILEPATH_DEVICE_PATH + END_DEVICE_PATH_LENGTH);\r
+  if (FileDevicePath != NULL) {\r
+    FilePath = (FILEPATH_DEVICE_PATH *) FileDevicePath;\r
+    FilePath->Header.Type    = MEDIA_DEVICE_PATH;\r
+    FilePath->Header.SubType = MEDIA_FILEPATH_DP;\r
     CopyMem (&FilePath->PathName, FileName, Size);\r
+    SetDevicePathNodeLength (&FilePath->Header, Size + SIZE_OF_FILEPATH_DEVICE_PATH);\r
+    SetDevicePathEndNode (NextDevicePathNode (&FilePath->Header));\r
+\r
     if (Device != NULL) {\r
       DevicePath = DevicePathFromHandle (Device);\r
     }\r
-    DevicePath = AppendDevicePathNode (DevicePath, FileDevicePathNode);\r
-    FreePool (FileDevicePathNode);\r
+\r
+    DevicePath = AppendDevicePath (DevicePath, FileDevicePath);\r
+    FreePool (FileDevicePath);\r
   }\r
 \r
   return DevicePath;\r