]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.c
Code have been checked with spec
[mirror_edk2.git] / MdePkg / Library / UefiDevicePathLib / UefiDevicePathLib.c
index c6661fe70da2885412d0d793be0d21d724d94981..8bdb598f88f106f9bdf2289edaa981dde63137f3 100644 (file)
@@ -34,7 +34,7 @@
 //\r
 // Template for an end-of-device path node.\r
 //\r
-EFI_DEVICE_PATH_PROTOCOL  mEndDevicePath = {\r
+GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_DEVICE_PATH_PROTOCOL  mUefiDevicePathLibEndDevicePath = {\r
   END_DEVICE_PATH_TYPE,\r
   END_ENTIRE_DEVICE_PATH_SUBTYPE,\r
   {\r
@@ -50,8 +50,9 @@ EFI_DEVICE_PATH_PROTOCOL  mEndDevicePath = {
   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
@@ -70,14 +71,14 @@ 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) + EfiDevicePathNodeLength (DevicePath);\r
+  return ((UINTN) DevicePath - (UINTN) Start) + DevicePathNodeLength (DevicePath);\r
 }\r
 \r
 /**\r
@@ -87,11 +88,14 @@ GetDevicePathSize (
   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
@@ -132,8 +136,10 @@ DuplicateDevicePath (
 \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
@@ -153,7 +159,7 @@ AppendDevicePath (
   // If there's only 1 path, just duplicate it.\r
   //\r
   if (FirstDevicePath == NULL) {\r
-    return DuplicateDevicePath ((SecondDevicePath != NULL) ? SecondDevicePath : &mEndDevicePath);\r
+    return DuplicateDevicePath ((SecondDevicePath != NULL) ? SecondDevicePath : &mUefiDevicePathLibEndDevicePath);\r
   }\r
 \r
   if (SecondDevicePath == NULL) {\r
@@ -166,7 +172,7 @@ AppendDevicePath (
   //\r
   Size1         = GetDevicePathSize (FirstDevicePath);\r
   Size2         = GetDevicePathSize (SecondDevicePath);\r
-  Size          = Size1 + Size2 - EFI_END_DEVICE_PATH_LENGTH;\r
+  Size          = Size1 + Size2 - END_DEVICE_PATH_LENGTH;\r
 \r
   NewDevicePath = AllocatePool (Size);\r
 \r
@@ -176,7 +182,7 @@ AppendDevicePath (
     // Over write FirstDevicePath EndNode and do the copy\r
     //\r
     DevicePath2 = (EFI_DEVICE_PATH_PROTOCOL *) ((CHAR8 *) NewDevicePath +\r
-                  (Size1 - EFI_END_DEVICE_PATH_LENGTH));\r
+                  (Size1 - END_DEVICE_PATH_LENGTH));\r
     CopyMem (DevicePath2, SecondDevicePath, Size2);\r
   }\r
 \r
@@ -201,7 +207,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
@@ -217,14 +227,14 @@ AppendDevicePathNode (
   UINTN                     NodeLength;\r
 \r
   if (DevicePathNode == NULL) {\r
-    return DuplicateDevicePath ((DevicePath != NULL) ? DevicePath : &mEndDevicePath);\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 + EFI_END_DEVICE_PATH_LENGTH);\r
+  TempDevicePath = AllocatePool (NodeLength + END_DEVICE_PATH_LENGTH);\r
   if (TempDevicePath == NULL) {\r
     return NULL;\r
   }\r
@@ -381,8 +391,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
@@ -396,7 +405,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 A pointer to the new create device path.\r
+  @return The new device path.\r
 \r
 **/\r
 EFI_DEVICE_PATH_PROTOCOL *\r
@@ -451,12 +460,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
@@ -502,13 +511,17 @@ 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
   @param  FileName                   A pointer to a Null-terminated Unicode string.\r
 \r
-  @return A pointer to the new created file device path.\r
+  @return The allocated device path.\r
 \r
 **/\r
 EFI_DEVICE_PATH_PROTOCOL *\r
@@ -518,15 +531,16 @@ 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  *FileDevicePath;\r
 \r
   DevicePath = NULL;\r
 \r
-  Size = StrSize (FileName);\r
-  FileDevicePath = AllocatePool (Size + SIZE_OF_FILEPATH_DEVICE_PATH + EFI_END_DEVICE_PATH_LENGTH);\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