]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Hand/Locate.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Hand / Locate.c
index 55211e2637e819f51d5479d3702c34264b60d7c9..be17f4cbc3ade2b39ef277c8848fc489208b2826 100644 (file)
@@ -1,18 +1,13 @@
 /** @file\r
   Locate handle functions\r
 \r
-Copyright (c) 2006 - 2008, Intel Corporation. <BR>\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
-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
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
-#include <DxeMain.h>\r
+#include "DxeMain.h"\r
+#include "Handle.h"\r
 \r
 //\r
 // ProtocolRequest - Last LocateHandle request ID\r
@@ -45,7 +40,7 @@ IHANDLE *
                                  protocol.\r
 \r
   @return An pointer to IHANDLE if the next Position is not the end of the list.\r
-          Otherwise,NULL_HANDLE is returned.\r
+          Otherwise,NULL is returned.\r
 \r
 **/\r
 IHANDLE *\r
@@ -63,7 +58,7 @@ CoreGetNextLocateAllHandles (
                                  protocol.\r
 \r
   @return An pointer to IHANDLE if the next Position is not the end of the list.\r
-          Otherwise,NULL_HANDLE is returned.\r
+          Otherwise,NULL is returned.\r
 \r
 **/\r
 IHANDLE *\r
@@ -80,7 +75,7 @@ CoreGetNextLocateByRegisterNotify (
                                  protocol.\r
 \r
   @return An pointer to IHANDLE if the next Position is not the end of the list.\r
-          Otherwise,NULL_HANDLE is returned.\r
+          Otherwise,NULL is returned.\r
 \r
 **/\r
 IHANDLE *\r
@@ -128,7 +123,7 @@ CoreLocateHandle (
   VOID                *Interface;\r
 \r
   if (BufferSize == NULL) {\r
-    Status = EFI_INVALID_PARAMETER;\r
+    return EFI_INVALID_PARAMETER;\r
   }\r
 \r
   if ((*BufferSize > 0) && (Buffer == NULL)) {\r
@@ -199,6 +194,7 @@ CoreLocateHandle (
     return Status;\r
   }\r
 \r
+  ASSERT (GetNext != NULL);\r
   //\r
   // Enumerate out the matching handles\r
   //\r
@@ -245,6 +241,7 @@ CoreLocateHandle (
       // If this is a search by register notify and a handle was\r
       // returned, update the register notification position\r
       //\r
+      ASSERT (SearchKey != NULL);\r
       ProtNotify = SearchKey;\r
       ProtNotify->Position = ProtNotify->Position->ForwardLink;\r
     }\r
@@ -264,7 +261,7 @@ CoreLocateHandle (
                                  protocol.\r
 \r
   @return An pointer to IHANDLE if the next Position is not the end of the list.\r
-          Otherwise,NULL_HANDLE is returned.\r
+          Otherwise,NULL is returned.\r
 \r
 **/\r
 IHANDLE *\r
@@ -283,7 +280,7 @@ CoreGetNextLocateAllHandles (
   //\r
   // If not at the end of the list, get the handle\r
   //\r
-  Handle      = NULL_HANDLE;\r
+  Handle      = NULL;\r
   *Interface  = NULL;\r
   if (Position->Position != &gHandleList) {\r
     Handle = CR (Position->Position, IHANDLE, AllHandles, EFI_HANDLE_SIGNATURE);\r
@@ -303,7 +300,7 @@ CoreGetNextLocateAllHandles (
                                  protocol.\r
 \r
   @return An pointer to IHANDLE if the next Position is not the end of the list.\r
-          Otherwise,NULL_HANDLE is returned.\r
+          Otherwise,NULL is returned.\r
 \r
 **/\r
 IHANDLE *\r
@@ -317,7 +314,7 @@ CoreGetNextLocateByRegisterNotify (
   PROTOCOL_INTERFACE  *Prot;\r
   LIST_ENTRY          *Link;\r
 \r
-  Handle      = NULL_HANDLE;\r
+  Handle      = NULL;\r
   *Interface  = NULL;\r
   ProtNotify = Position->SearchKey;\r
 \r
@@ -334,7 +331,7 @@ CoreGetNextLocateByRegisterNotify (
     Link = ProtNotify->Position->ForwardLink;\r
     if (Link != &ProtNotify->Protocol->Protocols) {\r
       Prot = CR (Link, PROTOCOL_INTERFACE, ByProtocol, PROTOCOL_INTERFACE_SIGNATURE);\r
-      Handle = (IHANDLE *) Prot->Handle;\r
+      Handle = Prot->Handle;\r
       *Interface = Prot->Interface;\r
     }\r
   }\r
@@ -351,7 +348,7 @@ CoreGetNextLocateByRegisterNotify (
                                  protocol.\r
 \r
   @return An pointer to IHANDLE if the next Position is not the end of the list.\r
-          Otherwise,NULL_HANDLE is returned.\r
+          Otherwise,NULL is returned.\r
 \r
 **/\r
 IHANDLE *\r
@@ -364,7 +361,7 @@ CoreGetNextLocateByProtocol (
   LIST_ENTRY          *Link;\r
   PROTOCOL_INTERFACE  *Prot;\r
 \r
-  Handle      = NULL_HANDLE;\r
+  Handle      = NULL;\r
   *Interface  = NULL;\r
   for (; ;) {\r
     //\r
@@ -377,7 +374,7 @@ CoreGetNextLocateByProtocol (
     // If not at the end, return the handle\r
     //\r
     if (Link == &Position->ProtEntry->Protocols) {\r
-      Handle = NULL_HANDLE;\r
+      Handle = NULL;\r
       break;\r
     }\r
 \r
@@ -385,7 +382,7 @@ CoreGetNextLocateByProtocol (
     // Get the handle\r
     //\r
     Prot = CR(Link, PROTOCOL_INTERFACE, ByProtocol, PROTOCOL_INTERFACE_SIGNATURE);\r
-    Handle = (IHANDLE *) Prot->Handle;\r
+    Handle = Prot->Handle;\r
     *Interface = Prot->Interface;\r
 \r
     //\r
@@ -403,18 +400,19 @@ CoreGetNextLocateByProtocol (
 \r
 \r
 /**\r
-  Locates the handle to a device on the device path that best matches the specified protocol.\r
+  Locates the handle to a device on the device path that supports the specified protocol.\r
 \r
-  @param  Protocol               The protocol to search for.\r
-  @param  DevicePath             On input, a pointer to a pointer to the device\r
-                                 path. On output, the device path pointer is\r
-                                 modified to point to the remaining part of the\r
-                                 devicepath.\r
-  @param  Device                 A pointer to the returned device handle.\r
+  @param  Protocol              Specifies the protocol to search for.\r
+  @param  DevicePath            On input, a pointer to a pointer to the device path. On output, the device\r
+                                path pointer is modified to point to the remaining part of the device\r
+                                path.\r
+  @param  Device                A pointer to the returned device handle.\r
 \r
-  @retval EFI_SUCCESS            The resulting handle was returned.\r
-  @retval EFI_NOT_FOUND          No handles matched the search.\r
-  @retval EFI_INVALID_PARAMETER  One of the parameters has an invalid value.\r
+  @retval EFI_SUCCESS           The resulting handle was returned.\r
+  @retval EFI_NOT_FOUND         No handles match the search.\r
+  @retval EFI_INVALID_PARAMETER Protocol is NULL.\r
+  @retval EFI_INVALID_PARAMETER DevicePath is NULL.\r
+  @retval EFI_INVALID_PARAMETER A handle matched the search and Device is NULL.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -433,6 +431,7 @@ CoreLocateDevicePath (
   EFI_STATUS                  Status;\r
   EFI_HANDLE                  *Handles;\r
   EFI_HANDLE                  Handle;\r
+  EFI_HANDLE                  BestDevice;\r
   EFI_DEVICE_PATH_PROTOCOL    *SourcePath;\r
   EFI_DEVICE_PATH_PROTOCOL    *TmpDevicePath;\r
 \r
@@ -444,22 +443,23 @@ CoreLocateDevicePath (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  if (Device == NULL) {\r
-    return  EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  *Device = NULL_HANDLE;\r
+  Handles = NULL;\r
+  BestDevice = NULL;\r
   SourcePath = *DevicePath;\r
-  SourceSize = CoreDevicePathSize (SourcePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL);\r
-\r
-  //\r
-  // The source path can only have 1 instance\r
-  //\r
-  if (CoreIsDevicePathMultiInstance (SourcePath)) {\r
-    DEBUG((DEBUG_ERROR, "LocateDevicePath: Device path has too many instances\n"));\r
-    return EFI_INVALID_PARAMETER;\r
+  TmpDevicePath = SourcePath;\r
+  while (!IsDevicePathEnd (TmpDevicePath)) {\r
+    if (IsDevicePathEndInstance (TmpDevicePath)) {\r
+      //\r
+      // If DevicePath is a multi-instance device path,\r
+      // the function will operate on the first instance\r
+      //\r
+      break;\r
+    }\r
+    TmpDevicePath = NextDevicePathNode (TmpDevicePath);\r
   }\r
 \r
+  SourceSize = (UINTN) TmpDevicePath - (UINTN) SourcePath;\r
+\r
   //\r
   // Get a list of all handles that support the requested protocol\r
   //\r
@@ -482,11 +482,12 @@ CoreLocateDevicePath (
     //\r
     // Check if DevicePath is first part of SourcePath\r
     //\r
-    Size = CoreDevicePathSize (TmpDevicePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL);\r
-    if ((Size <= SourceSize) && CompareMem (SourcePath, TmpDevicePath, Size) == 0) {\r
+    Size = GetDevicePathSize (TmpDevicePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL);\r
+    ASSERT (Size >= 0);\r
+    if ((Size <= SourceSize) && CompareMem (SourcePath, TmpDevicePath, (UINTN) Size) == 0) {\r
       //\r
       // If the size is equal to the best match, then we\r
-      // have a duplice device path for 2 different device\r
+      // have a duplicate device path for 2 different device\r
       // handles\r
       //\r
       ASSERT (Size != BestMatch);\r
@@ -496,7 +497,7 @@ CoreLocateDevicePath (
       //\r
       if (Size > BestMatch) {\r
         BestMatch = Size;\r
-        *Device = Handle;\r
+        BestDevice = Handle;\r
       }\r
     }\r
   }\r
@@ -511,6 +512,11 @@ CoreLocateDevicePath (
     return EFI_NOT_FOUND;\r
   }\r
 \r
+  if (Device == NULL) {\r
+    return  EFI_INVALID_PARAMETER;\r
+  }\r
+  *Device = BestDevice;\r
+\r
   //\r
   // Return the remaining part of the device path\r
   //\r
@@ -519,10 +525,10 @@ CoreLocateDevicePath (
 }\r
 \r
 \r
- /**\r
+/**\r
   Return the first Protocol Interface that matches the Protocol GUID. If\r
-  Registration is pasased in return a Protocol Instance that was just add\r
-  to the system. If Retistration is NULL return the first Protocol Interface\r
+  Registration is passed in, return a Protocol Instance that was just add\r
+  to the system. If Registration is NULL return the first Protocol Interface\r
   you find.\r
 \r
   @param  Protocol               The protocol to search for\r
@@ -548,14 +554,10 @@ CoreLocateProtocol (
   PROTOCOL_NOTIFY         *ProtNotify;\r
   IHANDLE                 *Handle;\r
 \r
-  if (Interface == NULL) {\r
+  if ((Interface == NULL) || (Protocol == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  if (Protocol == NULL) {\r
-    return EFI_NOT_FOUND;\r
-  }\r
-\r
   *Interface = NULL;\r
   Status = EFI_SUCCESS;\r
 \r
@@ -569,7 +571,10 @@ CoreLocateProtocol (
   //\r
   // Lock the protocol database\r
   //\r
-  CoreAcquireProtocolLock ();\r
+  Status = CoreAcquireLockOrFail (&gProtocolDatabaseLock);\r
+  if (EFI_ERROR (Status)) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
 \r
   mEfiLocateHandleRequest += 1;\r
 \r
@@ -625,7 +630,7 @@ Done:
   @retval EFI_NOT_FOUND          No handles match the search.\r
   @retval EFI_OUT_OF_RESOURCES   There is not enough pool memory to store the\r
                                  matching results.\r
-  @retval EFI_INVALID_PARAMETER  One or more paramters are not valid.\r
+  @retval EFI_INVALID_PARAMETER  One or more parameters are not valid.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -672,7 +677,7 @@ CoreLocateHandleBuffer (
     return Status;\r
   }\r
 \r
-  *Buffer = CoreAllocateBootServicesPool (BufferSize);\r
+  *Buffer = AllocatePool (BufferSize);\r
   if (*Buffer == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r