]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/UefiLib/UefiLib.c
MdePkg UefiLib: Fix in EfiLocateProtocolBuffer()
[mirror_edk2.git] / MdePkg / Library / UefiLib / UefiLib.c
index ba3acc1af228e55a16b85c657fa58a4c2b27d6ef..9ade1b0ce1a0e5842a7815af6dbfddbb92daf450 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
-  Copyright (c) 2006 - 2012, 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
@@ -304,6 +304,67 @@ EfiNamedEventSignal (
   return Status;\r
 }\r
 \r
+/**\r
+  Signals an event group by placing a new event in the group temporarily and\r
+  signaling it.\r
+\r
+  @param[in] EventGroup          Supplies the unique identifier of the event\r
+                                 group to signal.\r
+\r
+  @retval EFI_SUCCESS            The event group was signaled successfully.\r
+  @retval EFI_INVALID_PARAMETER  EventGroup is NULL.\r
+  @return                        Error codes that report problems about event\r
+                                 creation or signaling.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+EfiEventGroupSignal (\r
+  IN CONST EFI_GUID *EventGroup\r
+  )\r
+{\r
+  EFI_STATUS Status;\r
+  EFI_EVENT  Event;\r
+\r
+  if (EventGroup == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Status = gBS->CreateEventEx (\r
+                  EVT_NOTIFY_SIGNAL,\r
+                  TPL_CALLBACK,\r
+                  EfiEventEmptyFunction,\r
+                  NULL,\r
+                  EventGroup,\r
+                  &Event\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  Status = gBS->SignalEvent (Event);\r
+  gBS->CloseEvent (Event);\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  An empty function that can be used as NotifyFunction parameter of\r
+  CreateEvent() or CreateEventEx().\r
+\r
+  @param Event              Event whose notification function is being invoked.\r
+  @param Context            The pointer to the notification function's context,\r
+                            which is implementation-dependent.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+EfiEventEmptyFunction (\r
+  IN EFI_EVENT              Event,\r
+  IN VOID                   *Context\r
+  )\r
+{\r
+}\r
+\r
 /** \r
   Returns the current TPL.\r
 \r
@@ -827,10 +888,10 @@ LookupUnicodeString2 (
 EFI_STATUS\r
 EFIAPI\r
 AddUnicodeString (\r
-  IN CONST CHAR8               *Language,\r
-  IN CONST CHAR8               *SupportedLanguages,\r
-  IN EFI_UNICODE_STRING_TABLE  **UnicodeStringTable,\r
-  IN CONST CHAR16              *UnicodeString\r
+  IN     CONST CHAR8               *Language,\r
+  IN     CONST CHAR8               *SupportedLanguages,\r
+  IN OUT EFI_UNICODE_STRING_TABLE  **UnicodeStringTable,\r
+  IN     CONST CHAR16              *UnicodeString\r
   )\r
 {\r
   UINTN                     NumberOfEntries;\r
@@ -1004,11 +1065,11 @@ AddUnicodeString (
 EFI_STATUS\r
 EFIAPI\r
 AddUnicodeString2 (\r
-  IN CONST CHAR8               *Language,\r
-  IN CONST CHAR8               *SupportedLanguages,\r
-  IN EFI_UNICODE_STRING_TABLE  **UnicodeStringTable,\r
-  IN CONST CHAR16              *UnicodeString,\r
-  IN BOOLEAN                   Iso639Language\r
+  IN     CONST CHAR8               *Language,\r
+  IN     CONST CHAR8               *SupportedLanguages,\r
+  IN OUT EFI_UNICODE_STRING_TABLE  **UnicodeStringTable,\r
+  IN     CONST CHAR16              *UnicodeString,\r
+  IN     BOOLEAN                   Iso639Language\r
   )\r
 {\r
   UINTN                     NumberOfEntries;\r
@@ -1453,7 +1514,7 @@ CHAR8 *
 EFIAPI\r
 GetBestLanguage (\r
   IN CONST CHAR8  *SupportedLanguages, \r
-  IN BOOLEAN      Iso639Language,\r
+  IN UINTN        Iso639Language,\r
   ...\r
   )\r
 {\r
@@ -1546,21 +1607,115 @@ GetBestLanguage (
 }\r
 \r
 /**\r
-  An empty function to pass error checking of CreateEventEx ().\r
-\r
-  This empty function ensures that EVT_NOTIFY_SIGNAL_ALL is error\r
-  checked correctly since it is now mapped into CreateEventEx() in UEFI 2.0.\r
-\r
-  @param  Event                 Event whose notification function is being invoked.\r
-  @param  Context               The pointer to the notification function's context,\r
-                                which is implementation-dependent.\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
-VOID\r
+EFI_STATUS\r
 EFIAPI\r
-InternalEmptyFunction (\r
-  IN EFI_EVENT                Event,\r
-  IN VOID                     *Context\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
+    //\r
+    // Free the handle buffer\r
+    //\r
+    gBS->FreePool (HandleBuffer);\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