]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.c
Rename the global variable to avoid possible symbol conflict.
[mirror_edk2.git] / MdePkg / Library / UefiDevicePathLib / UefiDevicePathLib.c
index ef25fc983f8194ad5d788c4235b99cff0e118871..2d266c70b76fc7f97cec6fbadbdf0ff54904ae25 100644 (file)
 #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 size of a device path in bytes.\r
 \r
@@ -65,7 +77,7 @@ GetDevicePathSize (
   //\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) + EfiDevicePathNodeLength (DevicePath);\r
 }\r
 \r
 /**\r
@@ -87,7 +99,6 @@ DuplicateDevicePath (
   IN CONST EFI_DEVICE_PATH_PROTOCOL  *DevicePath\r
   )\r
 {\r
-  EFI_DEVICE_PATH_PROTOCOL  *NewDevicePath;\r
   UINTN                     Size;\r
 \r
   //\r
@@ -101,9 +112,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
@@ -114,7 +124,8 @@ 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
@@ -142,7 +153,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
@@ -155,7 +166,7 @@ AppendDevicePath (
   //\r
   Size1         = GetDevicePathSize (FirstDevicePath);\r
   Size2         = GetDevicePathSize (SecondDevicePath);\r
-  Size          = Size1 + Size2 - sizeof (EFI_DEVICE_PATH_PROTOCOL);\r
+  Size          = Size1 + Size2 - EFI_END_DEVICE_PATH_LENGTH;\r
 \r
   NewDevicePath = AllocatePool (Size);\r
 \r
@@ -165,7 +176,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 - EFI_END_DEVICE_PATH_LENGTH));\r
     CopyMem (DevicePath2, SecondDevicePath, Size2);\r
   }\r
 \r
@@ -178,8 +189,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
@@ -202,15 +216,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 + EFI_END_DEVICE_PATH_LENGTH);\r
   if (TempDevicePath == NULL) {\r
     return NULL;\r
   }\r
@@ -382,7 +396,7 @@ GetNextDevicePathInstance (
   @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
+  @return A pointer to the new create device path.\r
 \r
 **/\r
 EFI_DEVICE_PATH_PROTOCOL *\r
@@ -402,7 +416,7 @@ 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
@@ -494,7 +508,7 @@ DevicePathFromHandle (
                                      may be NULL.\r
   @param  FileName                   A pointer to a Null-terminated Unicode string.\r
 \r
-  @return The allocated device path.\r
+  @return A pointer to the new created file device path.\r
 \r
 **/\r
 EFI_DEVICE_PATH_PROTOCOL *\r