]> 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 dd7cd53197b0a4f07aec207cf2530e47250f354e..9ade1b0ce1a0e5842a7815af6dbfddbb92daf450 100644 (file)
@@ -5,11 +5,11 @@
   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 - 2008, Intel Corporation<BR>\r
-  All rights reserved. This program and the accompanying materials\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
-  http://opensource.org/licenses/bsd-license.php\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
 \r
 #include "UefiLibInternal.h"\r
 \r
+/**\r
+  Empty constructor function that is required to resolve dependencies between \r
+  libraries.\r
+  \r
+    ** DO NOT REMOVE **\r
+  \r
+  @param  ImageHandle   The firmware allocated handle for the EFI image.\r
+  @param  SystemTable   A pointer to the EFI System Table.\r
+  \r
+  @retval EFI_SUCCESS   The constructor executed correctly.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+UefiLibConstructor (\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable\r
+  )\r
+{\r
+  return EFI_SUCCESS;\r
+}\r
+\r
 /**\r
   Compare whether two names of languages are identical.\r
 \r
@@ -55,8 +77,8 @@ CompareIso639LanguageCode (
   If TableGuid is NULL, then ASSERT().\r
   If Table is NULL, then ASSERT().\r
 \r
-  @param  TableGuid       Pointer to table's GUID type..\r
-  @param  Table           Pointer to the table associated with TableGuid in the EFI System Table.\r
+  @param  TableGuid       The pointer to table's GUID type.\r
+  @param  Table           The pointer to the table associated with TableGuid in the EFI System Table.\r
 \r
   @retval EFI_SUCCESS     A configuration table matching TableGuid was found.\r
   @retval EFI_NOT_FOUND   A configuration table matching TableGuid could not be found.\r
@@ -175,7 +197,7 @@ EfiCreateProtocolNotifyEvent(
   If NotifyTpl is not a legal TPL value, then ASSERT().\r
   If NotifyFunction is NULL, then ASSERT().\r
 \r
-  @param  Name                  Supplies GUID name of the event.\r
+  @param  Name                  Supplies the GUID name of the event.\r
   @param  NotifyTpl             Supplies the task priority level of the event notifications.\r
   @param  NotifyFunction        Supplies the function to notify when the event is signaled.\r
   @param  NotifyContext         The context parameter to pass to NotifyFunction. \r
@@ -246,7 +268,7 @@ EfiNamedEventListen (
   created with EfiNamedEventListen().\r
   If Name is NULL, then ASSERT().\r
 \r
-  @param  Name                  Supplies GUID name of the event.\r
+  @param  Name                  Supplies the GUID name of the event.\r
 \r
   @retval EFI_SUCCESS           A named event was signaled.\r
   @retval EFI_OUT_OF_RESOURCES  There are not enough resource to signal the named event.\r
@@ -282,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
@@ -320,7 +403,7 @@ EfiGetCurrentTpl (
   If Priority is not a valid TPL value, then ASSERT().\r
 \r
   @param  Lock       A pointer to the lock data structure to initialize.\r
-  @param  Priority   EFI TPL associated with the lock.\r
+  @param  Priority   EFI TPL is associated with the lock.\r
 \r
   @return The lock.\r
 \r
@@ -805,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
@@ -886,7 +969,7 @@ AddUnicodeString (
       //\r
       NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (3, Language);\r
       if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) {\r
-        gBS->FreePool (NewUnicodeStringTable);\r
+        FreePool (NewUnicodeStringTable);\r
         return EFI_OUT_OF_RESOURCES;\r
       }\r
 \r
@@ -904,8 +987,8 @@ AddUnicodeString (
                                                               UnicodeString\r
                                                               );\r
       if (NewUnicodeStringTable[NumberOfEntries].UnicodeString == NULL) {\r
-        gBS->FreePool (NewUnicodeStringTable[NumberOfEntries].Language);\r
-        gBS->FreePool (NewUnicodeStringTable);\r
+        FreePool (NewUnicodeStringTable[NumberOfEntries].Language);\r
+        FreePool (NewUnicodeStringTable);\r
         return EFI_OUT_OF_RESOURCES;\r
       }\r
 \r
@@ -919,7 +1002,7 @@ AddUnicodeString (
       // Free the old Unicode String Table\r
       //\r
       if (*UnicodeStringTable != NULL) {\r
-        gBS->FreePool (*UnicodeStringTable);\r
+        FreePool (*UnicodeStringTable);\r
       }\r
 \r
       //\r
@@ -982,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
@@ -1097,7 +1180,7 @@ AddUnicodeString2 (
   //\r
   NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (AsciiStrSize(Language), Language);\r
   if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) {\r
-    gBS->FreePool (NewUnicodeStringTable);\r
+    FreePool (NewUnicodeStringTable);\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
@@ -1111,8 +1194,8 @@ AddUnicodeString2 (
   //\r
   NewUnicodeStringTable[NumberOfEntries].UnicodeString = AllocateCopyPool (StrSize (UnicodeString), UnicodeString);\r
   if (NewUnicodeStringTable[NumberOfEntries].UnicodeString == NULL) {\r
-    gBS->FreePool (NewUnicodeStringTable[NumberOfEntries].Language);\r
-    gBS->FreePool (NewUnicodeStringTable);\r
+    FreePool (NewUnicodeStringTable[NumberOfEntries].Language);\r
+    FreePool (NewUnicodeStringTable);\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
@@ -1126,7 +1209,7 @@ AddUnicodeString2 (
   // Free the old Unicode String Table\r
   //\r
   if (*UnicodeStringTable != NULL) {\r
-    gBS->FreePool (*UnicodeStringTable);\r
+    FreePool (*UnicodeStringTable);\r
   }\r
 \r
   //\r
@@ -1172,25 +1255,29 @@ FreeUnicodeStringTable (
     //\r
     // Free the Language string from the Unicode String Table\r
     //\r
-    gBS->FreePool (UnicodeStringTable[Index].Language);\r
+    FreePool (UnicodeStringTable[Index].Language);\r
 \r
     //\r
     // Free the Unicode String from the Unicode String Table\r
     //\r
     if (UnicodeStringTable[Index].UnicodeString != NULL) {\r
-      gBS->FreePool (UnicodeStringTable[Index].UnicodeString);\r
+      FreePool (UnicodeStringTable[Index].UnicodeString);\r
     }\r
   }\r
 \r
   //\r
   // Free the Unicode String Table itself\r
   //\r
-  gBS->FreePool (UnicodeStringTable);\r
+  FreePool (UnicodeStringTable);\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
+#ifndef DISABLE_NEW_DEPRECATED_INTERFACES\r
+\r
 /**\r
+  [ATTENTION] This function will be deprecated for security reason.\r
+\r
   Returns a pointer to an allocated buffer that contains the contents of a \r
   variable retrieved through the UEFI Runtime Service GetVariable().  The \r
   returned buffer is allocated using AllocatePool().  The caller is responsible\r
@@ -1199,8 +1286,8 @@ FreeUnicodeStringTable (
   If Name is NULL, then ASSERT().\r
   If Guid is NULL, then ASSERT().\r
 \r
-  @param[in]  Name  Pointer to a Null-terminated Unicode string.\r
-  @param[in]  Guid  Pointer to an EFI_GUID structure\r
+  @param[in]  Name  The pointer to a Null-terminated Unicode string.\r
+  @param[in]  Guid  The pointer to an EFI_GUID structure\r
 \r
   @retval NULL   The variable could not be retrieved.\r
   @retval NULL   There are not enough resources available for the variable contents.\r
@@ -1251,8 +1338,9 @@ GetVariable (
   return Value;\r
 }\r
 \r
-\r
 /**\r
+  [ATTENTION] This function will be deprecated for security reason.\r
+\r
   Returns a pointer to an allocated buffer that contains the contents of a \r
   variable retrieved through the UEFI Runtime Service GetVariable().  This \r
   function always uses the EFI_GLOBAL_VARIABLE GUID to retrieve variables.\r
@@ -1261,7 +1349,7 @@ GetVariable (
 \r
   If Name is NULL, then ASSERT().\r
 \r
-  @param[in]  Name  Pointer to a Null-terminated Unicode string.\r
+  @param[in]  Name  The pointer to a Null-terminated Unicode string.\r
 \r
   @retval NULL   The variable could not be retrieved.\r
   @retval NULL   There are not enough resources available for the variable contents.\r
@@ -1276,7 +1364,110 @@ GetEfiGlobalVariable (
 {\r
   return GetVariable (Name, &gEfiGlobalVariableGuid);\r
 }\r
+#endif\r
+\r
+/**\r
+  Returns the status whether get the variable success. The function retrieves \r
+  variable  through the UEFI Runtime Service GetVariable().  The \r
+  returned buffer is allocated using AllocatePool().  The caller is responsible\r
+  for freeing this buffer with FreePool().\r
+\r
+  If Name  is NULL, then ASSERT().\r
+  If Guid  is NULL, then ASSERT().\r
+  If Value is NULL, then ASSERT().\r
+\r
+  @param[in]  Name  The pointer to a Null-terminated Unicode string.\r
+  @param[in]  Guid  The pointer to an EFI_GUID structure\r
+  @param[out] Value The buffer point saved the variable info.\r
+  @param[out] Size  The buffer size of the variable.\r
+\r
+  @return EFI_OUT_OF_RESOURCES      Allocate buffer failed.\r
+  @return EFI_SUCCESS               Find the specified variable.\r
+  @return Others Errors             Return errors from call to gRT->GetVariable.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetVariable2 (\r
+  IN CONST CHAR16    *Name,\r
+  IN CONST EFI_GUID  *Guid,\r
+  OUT VOID           **Value,\r
+  OUT UINTN          *Size OPTIONAL\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       BufferSize;\r
 \r
+  ASSERT (Name != NULL && Guid != NULL && Value != NULL);\r
+\r
+  //\r
+  // Try to get the variable size.\r
+  //\r
+  BufferSize = 0;\r
+  *Value     = NULL;\r
+  if (Size != NULL) {\r
+    *Size  = 0;\r
+  }\r
+  \r
+  Status = gRT->GetVariable ((CHAR16 *) Name, (EFI_GUID *) Guid, NULL, &BufferSize, *Value);\r
+  if (Status != EFI_BUFFER_TOO_SMALL) {\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Allocate buffer to get the variable.\r
+  //\r
+  *Value = AllocatePool (BufferSize);\r
+  ASSERT (*Value != NULL);\r
+  if (*Value == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  //\r
+  // Get the variable data.\r
+  //\r
+  Status = gRT->GetVariable ((CHAR16 *) Name, (EFI_GUID *) Guid, NULL, &BufferSize, *Value);\r
+  if (EFI_ERROR (Status)) {\r
+    FreePool(*Value);\r
+    *Value = NULL;\r
+  }\r
+\r
+  if (Size != NULL) {\r
+    *Size = BufferSize;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Returns a pointer to an allocated buffer that contains the contents of a \r
+  variable retrieved through the UEFI Runtime Service GetVariable().  This \r
+  function always uses the EFI_GLOBAL_VARIABLE GUID to retrieve variables.\r
+  The returned buffer is allocated using AllocatePool().  The caller is \r
+  responsible for freeing this buffer with FreePool().\r
+\r
+  If Name is NULL, then ASSERT().\r
+  If Value is NULL, then ASSERT().\r
+\r
+  @param[in]  Name  The pointer to a Null-terminated Unicode string.\r
+  @param[out] Value The buffer point saved the variable info.\r
+  @param[out] Size  The buffer size of the variable.\r
+\r
+  @return EFI_OUT_OF_RESOURCES      Allocate buffer failed.\r
+  @return EFI_SUCCESS               Find the specified variable.\r
+  @return Others Errors             Return errors from call to gRT->GetVariable.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetEfiGlobalVariable2 (\r
+  IN CONST CHAR16    *Name,\r
+  OUT VOID           **Value,\r
+  OUT UINTN          *Size OPTIONAL\r
+  )\r
+{\r
+  return GetVariable2 (Name, &gEfiGlobalVariableGuid, Value, Size);\r
+}\r
 \r
 /**\r
   Returns a pointer to an allocated buffer that contains the best matching language \r
@@ -1323,7 +1514,7 @@ CHAR8 *
 EFIAPI\r
 GetBestLanguage (\r
   IN CONST CHAR8  *SupportedLanguages, \r
-  IN BOOLEAN      Iso639Language,\r
+  IN UINTN        Iso639Language,\r
   ...\r
   )\r
 {\r
@@ -1415,3 +1606,116 @@ GetBestLanguage (
   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
+    //\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