]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdePkg/UefiLib: Add EfiLocateProtocolBuffer()
authorMichael D Kinney <michael.d.kinney@intel.com>
Tue, 2 Jan 2018 18:40:07 +0000 (10:40 -0800)
committerMichael D Kinney <michael.d.kinney@intel.com>
Sun, 11 Feb 2018 23:10:03 +0000 (15:10 -0800)
https://bugzilla.tianocore.org/show_bug.cgi?id=838

Add new API to the UefiLib that locates and returns
an array of protocols instances that match a given
protocol.

Cc: Sean Brogan <sean.brogan@microsoft.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Reviewed-by: Star Zeng <star.zeng@intel.com>
Reviewed-by: Bret Barkelew <Bret.Barkelew@microsoft.com>
MdePkg/Include/Library/UefiLib.h
MdePkg/Library/UefiLib/UefiLib.c

index 0b14792a0a7fa98ec3604ba102b46972e3b8310f..54bc2cc5a334d3d3b097e27312dfb3c44d3e2280 100644 (file)
@@ -12,7 +12,7 @@
   of size reduction when compiler optimization is disabled. If MDEPKG_NDEBUG is\r
   defined, then debug and assert related macros wrapped by it are the NULL implementations.\r
 \r
   of size reduction when compiler optimization is disabled. If MDEPKG_NDEBUG is\r
   defined, then debug and assert related macros wrapped by it are the NULL implementations.\r
 \r
-Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials are licensed and made available under \r
 the terms and conditions of the BSD License that accompanies this distribution.  \r
 The full text of the license may be found at\r
 This program and the accompanying materials are licensed and made available under \r
 the terms and conditions of the BSD License that accompanies this distribution.  \r
 The full text of the license may be found at\r
@@ -1490,4 +1490,34 @@ CatSPrint (
   ...\r
   );\r
 \r
   ...\r
   );\r
 \r
+/**\r
+  Returns an array of protocol instance that matches the given protocol.\r
+\r
+  @param[in]  Protocol      Provides the protocol to search for.\r
+  @param[out] NoProtocols   The number of protocols returned in Buffer.\r
+  @param[out] Buffer        A pointer to the buffer to return the requested\r
+                            array of protocol instances that match Protocol.\r
+                            The returned buffer is allocated using\r
+                            EFI_BOOT_SERVICES.AllocatePool().  The caller is\r
+                            responsible for freeing this buffer with\r
+                            EFI_BOOT_SERVICES.FreePool().\r
+\r
+  @retval EFI_SUCCESS            The array of protocols was returned in Buffer,\r
+                                 and the number of protocols in Buffer was\r
+                                 returned in NoProtocols.\r
+  @retval EFI_NOT_FOUND          No protocols found.\r
+  @retval EFI_OUT_OF_RESOURCES   There is not enough pool memory to store the\r
+                                 matching results.\r
+  @retval EFI_INVALID_PARAMETER  Protocol is NULL.\r
+  @retval EFI_INVALID_PARAMETER  NoProtocols is NULL.\r
+  @retval EFI_INVALID_PARAMETER  Buffer is NULL.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+EfiLocateProtocolBuffer (\r
+  IN  EFI_GUID  *Protocol,\r
+  OUT UINTN     *NoProtocols,\r
+  OUT VOID      ***Buffer\r
+  );\r
 #endif\r
 #endif\r
index a7eee01240c1cfe1203d19f14a1e96767843a238..f1a3f1c7af0502159a69dda6416e785c72795fb8 100644 (file)
@@ -5,7 +5,7 @@
   EFI Driver Model related protocols, manage Unicode string tables for UEFI Drivers, \r
   and print messages on the console output and standard error devices.\r
 \r
   EFI Driver Model related protocols, manage Unicode string tables for UEFI Drivers, \r
   and print messages on the console output and standard error devices.\r
 \r
-  Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2006 - 2018, 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
   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
@@ -1605,3 +1605,113 @@ GetBestLanguage (
   //\r
   return NULL;\r
 }\r
   //\r
   return NULL;\r
 }\r
+\r
+/**\r
+  Returns an array of protocol instance that matches the given protocol.\r
+\r
+  @param[in]  Protocol      Provides the protocol to search for.\r
+  @param[out] NoProtocols   The number of protocols returned in Buffer.\r
+  @param[out] Buffer        A pointer to the buffer to return the requested\r
+                            array of protocol instances that match Protocol.\r
+                            The returned buffer is allocated using\r
+                            EFI_BOOT_SERVICES.AllocatePool().  The caller is\r
+                            responsible for freeing this buffer with\r
+                            EFI_BOOT_SERVICES.FreePool().\r
+\r
+  @retval EFI_SUCCESS            The array of protocols was returned in Buffer,\r
+                                 and the number of protocols in Buffer was\r
+                                 returned in NoProtocols.\r
+  @retval EFI_NOT_FOUND          No protocols found.\r
+  @retval EFI_OUT_OF_RESOURCES   There is not enough pool memory to store the\r
+                                 matching results.\r
+  @retval EFI_INVALID_PARAMETER  Protocol is NULL.\r
+  @retval EFI_INVALID_PARAMETER  NoProtocols is NULL.\r
+  @retval EFI_INVALID_PARAMETER  Buffer is NULL.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+EfiLocateProtocolBuffer (\r
+  IN  EFI_GUID  *Protocol,\r
+  OUT UINTN     *NoProtocols,\r
+  OUT VOID      ***Buffer\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       NoHandles;\r
+  EFI_HANDLE  *HandleBuffer;\r
+  UINTN       Index;\r
+\r
+  //\r
+  // Check input parameters\r
+  //\r
+  if (Protocol == NULL || NoProtocols == NULL || Buffer == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // Initialze output parameters\r
+  //\r
+  *NoProtocols = 0;\r
+  *Buffer = NULL;\r
+\r
+  //\r
+  // Retrieve the array of handles that support Protocol\r
+  //\r
+  Status = gBS->LocateHandleBuffer (\r
+                  ByProtocol,\r
+                  Protocol,\r
+                  NULL,\r
+                  &NoHandles,\r
+                  &HandleBuffer\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Allocate array of protocol instances\r
+  //\r
+  Status = gBS->AllocatePool (\r
+                  EfiBootServicesData,\r
+                  NoHandles * sizeof (VOID *),\r
+                  (VOID **)Buffer\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  ZeroMem (*Buffer, NoHandles * sizeof (VOID *));\r
+\r
+  //\r
+  // Lookup Protocol on each handle in HandleBuffer to fill in the array of\r
+  // protocol instances.  Handle case where protocol instance was present when\r
+  // LocateHandleBuffer() was called, but is not present when HandleProtocol()\r
+  // is called.\r
+  //\r
+  for (Index = 0, *NoProtocols = 0; Index < NoHandles; Index++) {\r
+    Status = gBS->HandleProtocol (\r
+                    HandleBuffer[Index],\r
+                    Protocol,\r
+                    &((*Buffer)[*NoProtocols])\r
+                    );\r
+    if (!EFI_ERROR (Status)) {\r
+      (*NoProtocols)++;\r
+    }\r
+  }\r
+\r
+  //\r
+  // Free the handle buffer\r
+  //\r
+  gBS->FreePool (HandleBuffer);\r
+\r
+  //\r
+  // Make sure at least one protocol instance was found\r
+  //\r
+  if (*NoProtocols == 0) {\r
+    gBS->FreePool (*Buffer);\r
+    *Buffer = NULL;\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r