X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdePkg%2FLibrary%2FUefiLib%2FUefiLib.c;h=9ade1b0ce1a0e5842a7815af6dbfddbb92daf450;hp=e1e9f756f567f0bc1f823693036370ed15762053;hb=fe507283afe245dae5ffdc0f926d9e2abb355250;hpb=2fc59a003ed9104f9feebe0e418f2a04a50f3284 diff --git a/MdePkg/Library/UefiLib/UefiLib.c b/MdePkg/Library/UefiLib/UefiLib.c index e1e9f756f5..9ade1b0ce1 100644 --- a/MdePkg/Library/UefiLib/UefiLib.c +++ b/MdePkg/Library/UefiLib/UefiLib.c @@ -5,7 +5,7 @@ EFI Driver Model related protocols, manage Unicode string tables for UEFI Drivers, and print messages on the console output and standard error devices. - Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.
+ Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -19,6 +19,28 @@ #include "UefiLibInternal.h" +/** + Empty constructor function that is required to resolve dependencies between + libraries. + + ** DO NOT REMOVE ** + + @param ImageHandle The firmware allocated handle for the EFI image. + @param SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The constructor executed correctly. + +**/ +EFI_STATUS +EFIAPI +UefiLibConstructor ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + return EFI_SUCCESS; +} + /** Compare whether two names of languages are identical. @@ -55,7 +77,7 @@ CompareIso639LanguageCode ( If TableGuid is NULL, then ASSERT(). If Table is NULL, then ASSERT(). - @param TableGuid The pointer to table's GUID type.. + @param TableGuid The pointer to table's GUID type. @param Table The pointer to the table associated with TableGuid in the EFI System Table. @retval EFI_SUCCESS A configuration table matching TableGuid was found. @@ -175,7 +197,7 @@ EfiCreateProtocolNotifyEvent( If NotifyTpl is not a legal TPL value, then ASSERT(). If NotifyFunction is NULL, then ASSERT(). - @param Name Supplies GUID name of the event. + @param Name Supplies the GUID name of the event. @param NotifyTpl Supplies the task priority level of the event notifications. @param NotifyFunction Supplies the function to notify when the event is signaled. @param NotifyContext The context parameter to pass to NotifyFunction. @@ -246,7 +268,7 @@ EfiNamedEventListen ( created with EfiNamedEventListen(). If Name is NULL, then ASSERT(). - @param Name Supplies GUID name of the event. + @param Name Supplies the GUID name of the event. @retval EFI_SUCCESS A named event was signaled. @retval EFI_OUT_OF_RESOURCES There are not enough resource to signal the named event. @@ -282,6 +304,67 @@ EfiNamedEventSignal ( return Status; } +/** + Signals an event group by placing a new event in the group temporarily and + signaling it. + + @param[in] EventGroup Supplies the unique identifier of the event + group to signal. + + @retval EFI_SUCCESS The event group was signaled successfully. + @retval EFI_INVALID_PARAMETER EventGroup is NULL. + @return Error codes that report problems about event + creation or signaling. +**/ +EFI_STATUS +EFIAPI +EfiEventGroupSignal ( + IN CONST EFI_GUID *EventGroup + ) +{ + EFI_STATUS Status; + EFI_EVENT Event; + + if (EventGroup == NULL) { + return EFI_INVALID_PARAMETER; + } + + Status = gBS->CreateEventEx ( + EVT_NOTIFY_SIGNAL, + TPL_CALLBACK, + EfiEventEmptyFunction, + NULL, + EventGroup, + &Event + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = gBS->SignalEvent (Event); + gBS->CloseEvent (Event); + + return Status; +} + +/** + An empty function that can be used as NotifyFunction parameter of + CreateEvent() or CreateEventEx(). + + @param Event Event whose notification function is being invoked. + @param Context The pointer to the notification function's context, + which is implementation-dependent. + +**/ +VOID +EFIAPI +EfiEventEmptyFunction ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ +} + /** Returns the current TPL. @@ -320,7 +403,7 @@ EfiGetCurrentTpl ( If Priority is not a valid TPL value, then ASSERT(). @param Lock A pointer to the lock data structure to initialize. - @param Priority EFI TPL associated with the lock. + @param Priority EFI TPL is associated with the lock. @return The lock. @@ -805,10 +888,10 @@ LookupUnicodeString2 ( EFI_STATUS EFIAPI AddUnicodeString ( - IN CONST CHAR8 *Language, - IN CONST CHAR8 *SupportedLanguages, - IN EFI_UNICODE_STRING_TABLE **UnicodeStringTable, - IN CONST CHAR16 *UnicodeString + IN CONST CHAR8 *Language, + IN CONST CHAR8 *SupportedLanguages, + IN OUT EFI_UNICODE_STRING_TABLE **UnicodeStringTable, + IN CONST CHAR16 *UnicodeString ) { UINTN NumberOfEntries; @@ -886,7 +969,7 @@ AddUnicodeString ( // NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (3, Language); if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) { - gBS->FreePool (NewUnicodeStringTable); + FreePool (NewUnicodeStringTable); return EFI_OUT_OF_RESOURCES; } @@ -904,8 +987,8 @@ AddUnicodeString ( UnicodeString ); if (NewUnicodeStringTable[NumberOfEntries].UnicodeString == NULL) { - gBS->FreePool (NewUnicodeStringTable[NumberOfEntries].Language); - gBS->FreePool (NewUnicodeStringTable); + FreePool (NewUnicodeStringTable[NumberOfEntries].Language); + FreePool (NewUnicodeStringTable); return EFI_OUT_OF_RESOURCES; } @@ -919,7 +1002,7 @@ AddUnicodeString ( // Free the old Unicode String Table // if (*UnicodeStringTable != NULL) { - gBS->FreePool (*UnicodeStringTable); + FreePool (*UnicodeStringTable); } // @@ -982,11 +1065,11 @@ AddUnicodeString ( EFI_STATUS EFIAPI AddUnicodeString2 ( - IN CONST CHAR8 *Language, - IN CONST CHAR8 *SupportedLanguages, - IN EFI_UNICODE_STRING_TABLE **UnicodeStringTable, - IN CONST CHAR16 *UnicodeString, - IN BOOLEAN Iso639Language + IN CONST CHAR8 *Language, + IN CONST CHAR8 *SupportedLanguages, + IN OUT EFI_UNICODE_STRING_TABLE **UnicodeStringTable, + IN CONST CHAR16 *UnicodeString, + IN BOOLEAN Iso639Language ) { UINTN NumberOfEntries; @@ -1097,7 +1180,7 @@ AddUnicodeString2 ( // NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (AsciiStrSize(Language), Language); if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) { - gBS->FreePool (NewUnicodeStringTable); + FreePool (NewUnicodeStringTable); return EFI_OUT_OF_RESOURCES; } @@ -1111,8 +1194,8 @@ AddUnicodeString2 ( // NewUnicodeStringTable[NumberOfEntries].UnicodeString = AllocateCopyPool (StrSize (UnicodeString), UnicodeString); if (NewUnicodeStringTable[NumberOfEntries].UnicodeString == NULL) { - gBS->FreePool (NewUnicodeStringTable[NumberOfEntries].Language); - gBS->FreePool (NewUnicodeStringTable); + FreePool (NewUnicodeStringTable[NumberOfEntries].Language); + FreePool (NewUnicodeStringTable); return EFI_OUT_OF_RESOURCES; } @@ -1126,7 +1209,7 @@ AddUnicodeString2 ( // Free the old Unicode String Table // if (*UnicodeStringTable != NULL) { - gBS->FreePool (*UnicodeStringTable); + FreePool (*UnicodeStringTable); } // @@ -1172,25 +1255,29 @@ FreeUnicodeStringTable ( // // Free the Language string from the Unicode String Table // - gBS->FreePool (UnicodeStringTable[Index].Language); + FreePool (UnicodeStringTable[Index].Language); // // Free the Unicode String from the Unicode String Table // if (UnicodeStringTable[Index].UnicodeString != NULL) { - gBS->FreePool (UnicodeStringTable[Index].UnicodeString); + FreePool (UnicodeStringTable[Index].UnicodeString); } } // // Free the Unicode String Table itself // - gBS->FreePool (UnicodeStringTable); + FreePool (UnicodeStringTable); return EFI_SUCCESS; } +#ifndef DISABLE_NEW_DEPRECATED_INTERFACES + /** + [ATTENTION] This function will be deprecated for security reason. + Returns a pointer to an allocated buffer that contains the contents of a variable retrieved through the UEFI Runtime Service GetVariable(). The returned buffer is allocated using AllocatePool(). The caller is responsible @@ -1251,8 +1338,9 @@ GetVariable ( return Value; } - /** + [ATTENTION] This function will be deprecated for security reason. + Returns a pointer to an allocated buffer that contains the contents of a variable retrieved through the UEFI Runtime Service GetVariable(). This function always uses the EFI_GLOBAL_VARIABLE GUID to retrieve variables. @@ -1276,7 +1364,110 @@ GetEfiGlobalVariable ( { return GetVariable (Name, &gEfiGlobalVariableGuid); } +#endif + +/** + Returns the status whether get the variable success. The function retrieves + variable through the UEFI Runtime Service GetVariable(). The + returned buffer is allocated using AllocatePool(). The caller is responsible + for freeing this buffer with FreePool(). + + If Name is NULL, then ASSERT(). + If Guid is NULL, then ASSERT(). + If Value is NULL, then ASSERT(). + + @param[in] Name The pointer to a Null-terminated Unicode string. + @param[in] Guid The pointer to an EFI_GUID structure + @param[out] Value The buffer point saved the variable info. + @param[out] Size The buffer size of the variable. + + @return EFI_OUT_OF_RESOURCES Allocate buffer failed. + @return EFI_SUCCESS Find the specified variable. + @return Others Errors Return errors from call to gRT->GetVariable. + +**/ +EFI_STATUS +EFIAPI +GetVariable2 ( + IN CONST CHAR16 *Name, + IN CONST EFI_GUID *Guid, + OUT VOID **Value, + OUT UINTN *Size OPTIONAL + ) +{ + EFI_STATUS Status; + UINTN BufferSize; + + ASSERT (Name != NULL && Guid != NULL && Value != NULL); + + // + // Try to get the variable size. + // + BufferSize = 0; + *Value = NULL; + if (Size != NULL) { + *Size = 0; + } + + Status = gRT->GetVariable ((CHAR16 *) Name, (EFI_GUID *) Guid, NULL, &BufferSize, *Value); + if (Status != EFI_BUFFER_TOO_SMALL) { + return Status; + } + + // + // Allocate buffer to get the variable. + // + *Value = AllocatePool (BufferSize); + ASSERT (*Value != NULL); + if (*Value == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Get the variable data. + // + Status = gRT->GetVariable ((CHAR16 *) Name, (EFI_GUID *) Guid, NULL, &BufferSize, *Value); + if (EFI_ERROR (Status)) { + FreePool(*Value); + *Value = NULL; + } + + if (Size != NULL) { + *Size = BufferSize; + } + + return Status; +} +/** + Returns a pointer to an allocated buffer that contains the contents of a + variable retrieved through the UEFI Runtime Service GetVariable(). This + function always uses the EFI_GLOBAL_VARIABLE GUID to retrieve variables. + The returned buffer is allocated using AllocatePool(). The caller is + responsible for freeing this buffer with FreePool(). + + If Name is NULL, then ASSERT(). + If Value is NULL, then ASSERT(). + + @param[in] Name The pointer to a Null-terminated Unicode string. + @param[out] Value The buffer point saved the variable info. + @param[out] Size The buffer size of the variable. + + @return EFI_OUT_OF_RESOURCES Allocate buffer failed. + @return EFI_SUCCESS Find the specified variable. + @return Others Errors Return errors from call to gRT->GetVariable. + +**/ +EFI_STATUS +EFIAPI +GetEfiGlobalVariable2 ( + IN CONST CHAR16 *Name, + OUT VOID **Value, + OUT UINTN *Size OPTIONAL + ) +{ + return GetVariable2 (Name, &gEfiGlobalVariableGuid, Value, Size); +} /** Returns a pointer to an allocated buffer that contains the best matching language @@ -1323,7 +1514,7 @@ CHAR8 * EFIAPI GetBestLanguage ( IN CONST CHAR8 *SupportedLanguages, - IN BOOLEAN Iso639Language, + IN UINTN Iso639Language, ... ) { @@ -1415,3 +1606,116 @@ GetBestLanguage ( return NULL; } +/** + Returns an array of protocol instance that matches the given protocol. + + @param[in] Protocol Provides the protocol to search for. + @param[out] NoProtocols The number of protocols returned in Buffer. + @param[out] Buffer A pointer to the buffer to return the requested + array of protocol instances that match Protocol. + The returned buffer is allocated using + EFI_BOOT_SERVICES.AllocatePool(). The caller is + responsible for freeing this buffer with + EFI_BOOT_SERVICES.FreePool(). + + @retval EFI_SUCCESS The array of protocols was returned in Buffer, + and the number of protocols in Buffer was + returned in NoProtocols. + @retval EFI_NOT_FOUND No protocols found. + @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the + matching results. + @retval EFI_INVALID_PARAMETER Protocol is NULL. + @retval EFI_INVALID_PARAMETER NoProtocols is NULL. + @retval EFI_INVALID_PARAMETER Buffer is NULL. + +**/ +EFI_STATUS +EFIAPI +EfiLocateProtocolBuffer ( + IN EFI_GUID *Protocol, + OUT UINTN *NoProtocols, + OUT VOID ***Buffer + ) +{ + EFI_STATUS Status; + UINTN NoHandles; + EFI_HANDLE *HandleBuffer; + UINTN Index; + + // + // Check input parameters + // + if (Protocol == NULL || NoProtocols == NULL || Buffer == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Initialze output parameters + // + *NoProtocols = 0; + *Buffer = NULL; + + // + // Retrieve the array of handles that support Protocol + // + Status = gBS->LocateHandleBuffer ( + ByProtocol, + Protocol, + NULL, + &NoHandles, + &HandleBuffer + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Allocate array of protocol instances + // + Status = gBS->AllocatePool ( + EfiBootServicesData, + NoHandles * sizeof (VOID *), + (VOID **)Buffer + ); + if (EFI_ERROR (Status)) { + // + // Free the handle buffer + // + gBS->FreePool (HandleBuffer); + return EFI_OUT_OF_RESOURCES; + } + ZeroMem (*Buffer, NoHandles * sizeof (VOID *)); + + // + // Lookup Protocol on each handle in HandleBuffer to fill in the array of + // protocol instances. Handle case where protocol instance was present when + // LocateHandleBuffer() was called, but is not present when HandleProtocol() + // is called. + // + for (Index = 0, *NoProtocols = 0; Index < NoHandles; Index++) { + Status = gBS->HandleProtocol ( + HandleBuffer[Index], + Protocol, + &((*Buffer)[*NoProtocols]) + ); + if (!EFI_ERROR (Status)) { + (*NoProtocols)++; + } + } + + // + // Free the handle buffer + // + gBS->FreePool (HandleBuffer); + + // + // Make sure at least one protocol instance was found + // + if (*NoProtocols == 0) { + gBS->FreePool (*Buffer); + *Buffer = NULL; + return EFI_NOT_FOUND; + } + + return EFI_SUCCESS; +}