2 The UEFI Library provides functions and macros that simplify the development of
3 UEFI Drivers and UEFI Applications. These functions and macros help manage EFI
4 events, build simple locks utilizing EFI Task Priority Levels (TPLs), install
5 EFI Driver Model related protocols, manage Unicode string tables for UEFI Drivers,
6 and print messages on the console output and standard error devices.
8 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
9 This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php.
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 #include "UefiLibInternal.h"
23 Empty constructor function that is required to resolve dependencies between
28 @param ImageHandle The firmware allocated handle for the EFI image.
29 @param SystemTable A pointer to the EFI System Table.
31 @retval EFI_SUCCESS The constructor executed correctly.
37 IN EFI_HANDLE ImageHandle
,
38 IN EFI_SYSTEM_TABLE
*SystemTable
45 Compare whether two names of languages are identical.
47 @param Language1 Name of language 1.
48 @param Language2 Name of language 2.
50 @retval TRUE Language 1 and language 2 are the same.
51 @retval FALSE Language 1 and language 2 are not the same.
55 CompareIso639LanguageCode (
56 IN CONST CHAR8
*Language1
,
57 IN CONST CHAR8
*Language2
63 Name1
= ReadUnaligned24 ((CONST UINT32
*) Language1
);
64 Name2
= ReadUnaligned24 ((CONST UINT32
*) Language2
);
66 return (BOOLEAN
) (Name1
== Name2
);
70 Retrieves a pointer to the system configuration table from the EFI System Table
71 based on a specified GUID.
73 This function searches the list of configuration tables stored in the EFI System Table
74 for a table with a GUID that matches TableGuid. If a match is found, then a pointer to
75 the configuration table is returned in Table., and EFI_SUCCESS is returned. If a matching GUID
76 is not found, then EFI_NOT_FOUND is returned.
77 If TableGuid is NULL, then ASSERT().
78 If Table is NULL, then ASSERT().
80 @param TableGuid The pointer to table's GUID type.
81 @param Table The pointer to the table associated with TableGuid in the EFI System Table.
83 @retval EFI_SUCCESS A configuration table matching TableGuid was found.
84 @retval EFI_NOT_FOUND A configuration table matching TableGuid could not be found.
89 EfiGetSystemConfigurationTable (
90 IN EFI_GUID
*TableGuid
,
94 EFI_SYSTEM_TABLE
*SystemTable
;
97 ASSERT (TableGuid
!= NULL
);
98 ASSERT (Table
!= NULL
);
102 for (Index
= 0; Index
< SystemTable
->NumberOfTableEntries
; Index
++) {
103 if (CompareGuid (TableGuid
, &(SystemTable
->ConfigurationTable
[Index
].VendorGuid
))) {
104 *Table
= SystemTable
->ConfigurationTable
[Index
].VendorTable
;
109 return EFI_NOT_FOUND
;
113 Creates and returns a notification event and registers that event with all the protocol
114 instances specified by ProtocolGuid.
116 This function causes the notification function to be executed for every protocol of type
117 ProtocolGuid instance that exists in the system when this function is invoked. If there are
118 no instances of ProtocolGuid in the handle database at the time this function is invoked,
119 then the notification function is still executed one time. In addition, every time a protocol
120 of type ProtocolGuid instance is installed or reinstalled, the notification function is also
121 executed. This function returns the notification event that was created.
122 If ProtocolGuid is NULL, then ASSERT().
123 If NotifyTpl is not a legal TPL value, then ASSERT().
124 If NotifyFunction is NULL, then ASSERT().
125 If Registration is NULL, then ASSERT().
128 @param ProtocolGuid Supplies GUID of the protocol upon whose installation the event is fired.
129 @param NotifyTpl Supplies the task priority level of the event notifications.
130 @param NotifyFunction Supplies the function to notify when the event is signaled.
131 @param NotifyContext The context parameter to pass to NotifyFunction.
132 @param Registration A pointer to a memory location to receive the registration value.
133 This value is passed to LocateHandle() to obtain new handles that
134 have been added that support the ProtocolGuid-specified protocol.
136 @return The notification event that was created.
141 EfiCreateProtocolNotifyEvent(
142 IN EFI_GUID
*ProtocolGuid
,
143 IN EFI_TPL NotifyTpl
,
144 IN EFI_EVENT_NOTIFY NotifyFunction
,
145 IN VOID
*NotifyContext
, OPTIONAL
146 OUT VOID
**Registration
152 ASSERT (ProtocolGuid
!= NULL
);
153 ASSERT (NotifyFunction
!= NULL
);
154 ASSERT (Registration
!= NULL
);
160 Status
= gBS
->CreateEvent (
167 ASSERT_EFI_ERROR (Status
);
170 // Register for protocol notifications on this event
173 Status
= gBS
->RegisterProtocolNotify (
179 ASSERT_EFI_ERROR (Status
);
182 // Kick the event so we will perform an initial pass of
183 // current installed drivers
186 gBS
->SignalEvent (Event
);
191 Creates a named event that can be signaled with EfiNamedEventSignal().
193 This function creates an event using NotifyTpl, NoifyFunction, and NotifyContext.
194 This event is signaled with EfiNamedEventSignal(). This provides the ability for one or more
195 listeners on the same event named by the GUID specified by Name.
196 If Name is NULL, then ASSERT().
197 If NotifyTpl is not a legal TPL value, then ASSERT().
198 If NotifyFunction is NULL, then ASSERT().
200 @param Name Supplies the GUID name of the event.
201 @param NotifyTpl Supplies the task priority level of the event notifications.
202 @param NotifyFunction Supplies the function to notify when the event is signaled.
203 @param NotifyContext The context parameter to pass to NotifyFunction.
204 @param Registration A pointer to a memory location to receive the registration value.
206 @retval EFI_SUCCESS A named event was created.
207 @retval EFI_OUT_OF_RESOURCES There are not enough resource to create the named event.
212 EfiNamedEventListen (
213 IN CONST EFI_GUID
*Name
,
214 IN EFI_TPL NotifyTpl
,
215 IN EFI_EVENT_NOTIFY NotifyFunction
,
216 IN CONST VOID
*NotifyContext
, OPTIONAL
217 OUT VOID
*Registration OPTIONAL
222 VOID
*RegistrationLocal
;
224 ASSERT (Name
!= NULL
);
225 ASSERT (NotifyFunction
!= NULL
);
226 ASSERT (NotifyTpl
<= TPL_HIGH_LEVEL
);
231 Status
= gBS
->CreateEvent (
235 (VOID
*) NotifyContext
,
238 ASSERT_EFI_ERROR (Status
);
241 // The Registration is not optional to RegisterProtocolNotify().
242 // To make it optional to EfiNamedEventListen(), may need to substitute with a local.
244 if (Registration
!= NULL
) {
245 RegistrationLocal
= Registration
;
247 RegistrationLocal
= &RegistrationLocal
;
251 // Register for an installation of protocol interface
254 Status
= gBS
->RegisterProtocolNotify (
259 ASSERT_EFI_ERROR (Status
);
265 Signals a named event created with EfiNamedEventListen().
267 This function signals the named event specified by Name. The named event must have been
268 created with EfiNamedEventListen().
269 If Name is NULL, then ASSERT().
271 @param Name Supplies the GUID name of the event.
273 @retval EFI_SUCCESS A named event was signaled.
274 @retval EFI_OUT_OF_RESOURCES There are not enough resource to signal the named event.
279 EfiNamedEventSignal (
280 IN CONST EFI_GUID
*Name
286 ASSERT(Name
!= NULL
);
289 Status
= gBS
->InstallProtocolInterface (
292 EFI_NATIVE_INTERFACE
,
295 ASSERT_EFI_ERROR (Status
);
297 Status
= gBS
->UninstallProtocolInterface (
302 ASSERT_EFI_ERROR (Status
);
308 Signals an event group by placing a new event in the group temporarily and
311 @param[in] EventGroup Supplies the unique identifier of the event
314 @retval EFI_SUCCESS The event group was signaled successfully.
315 @retval EFI_INVALID_PARAMETER EventGroup is NULL.
316 @return Error codes that report problems about event
317 creation or signaling.
321 EfiEventGroupSignal (
322 IN CONST EFI_GUID
*EventGroup
328 if (EventGroup
== NULL
) {
329 return EFI_INVALID_PARAMETER
;
332 Status
= gBS
->CreateEventEx (
335 InternalEmptyFunction
,
340 if (EFI_ERROR (Status
)) {
344 Status
= gBS
->SignalEvent (Event
);
345 gBS
->CloseEvent (Event
);
351 Returns the current TPL.
353 This function returns the current TPL. There is no EFI service to directly
354 retrieve the current TPL. Instead, the RaiseTPL() function is used to raise
355 the TPL to TPL_HIGH_LEVEL. This will return the current TPL. The TPL level
356 can then immediately be restored back to the current TPL level with a call
359 @return The current TPL.
370 Tpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
371 gBS
->RestoreTPL (Tpl
);
378 Initializes a basic mutual exclusion lock.
380 This function initializes a basic mutual exclusion lock to the released state
381 and returns the lock. Each lock provides mutual exclusion access at its task
382 priority level. Since there is no preemption or multiprocessor support in EFI,
383 acquiring the lock only consists of raising to the locks TPL.
384 If Lock is NULL, then ASSERT().
385 If Priority is not a valid TPL value, then ASSERT().
387 @param Lock A pointer to the lock data structure to initialize.
388 @param Priority EFI TPL is associated with the lock.
396 IN OUT EFI_LOCK
*Lock
,
400 ASSERT (Lock
!= NULL
);
401 ASSERT (Priority
<= TPL_HIGH_LEVEL
);
403 Lock
->Tpl
= Priority
;
404 Lock
->OwnerTpl
= TPL_APPLICATION
;
405 Lock
->Lock
= EfiLockReleased
;
410 Acquires ownership of a lock.
412 This function raises the system's current task priority level to the task
413 priority level of the mutual exclusion lock. Then, it places the lock in the
415 If Lock is NULL, then ASSERT().
416 If Lock is not initialized, then ASSERT().
417 If Lock is already in the acquired state, then ASSERT().
419 @param Lock A pointer to the lock to acquire.
428 ASSERT (Lock
!= NULL
);
429 ASSERT (Lock
->Lock
== EfiLockReleased
);
431 Lock
->OwnerTpl
= gBS
->RaiseTPL (Lock
->Tpl
);
432 Lock
->Lock
= EfiLockAcquired
;
436 Acquires ownership of a lock.
438 This function raises the system's current task priority level to the task priority
439 level of the mutual exclusion lock. Then, it attempts to place the lock in the acquired state.
440 If the lock is already in the acquired state, then EFI_ACCESS_DENIED is returned.
441 Otherwise, EFI_SUCCESS is returned.
442 If Lock is NULL, then ASSERT().
443 If Lock is not initialized, then ASSERT().
445 @param Lock A pointer to the lock to acquire.
447 @retval EFI_SUCCESS The lock was acquired.
448 @retval EFI_ACCESS_DENIED The lock could not be acquired because it is already owned.
453 EfiAcquireLockOrFail (
458 ASSERT (Lock
!= NULL
);
459 ASSERT (Lock
->Lock
!= EfiLockUninitialized
);
461 if (Lock
->Lock
== EfiLockAcquired
) {
463 // Lock is already owned, so bail out
465 return EFI_ACCESS_DENIED
;
468 Lock
->OwnerTpl
= gBS
->RaiseTPL (Lock
->Tpl
);
470 Lock
->Lock
= EfiLockAcquired
;
476 Releases ownership of a lock.
478 This function transitions a mutual exclusion lock from the acquired state to
479 the released state, and restores the system's task priority level to its
481 If Lock is NULL, then ASSERT().
482 If Lock is not initialized, then ASSERT().
483 If Lock is already in the released state, then ASSERT().
485 @param Lock A pointer to the lock to release.
496 ASSERT (Lock
!= NULL
);
497 ASSERT (Lock
->Lock
== EfiLockAcquired
);
499 Tpl
= Lock
->OwnerTpl
;
501 Lock
->Lock
= EfiLockReleased
;
503 gBS
->RestoreTPL (Tpl
);
507 Tests whether a controller handle is being managed by a specific driver.
509 This function tests whether the driver specified by DriverBindingHandle is
510 currently managing the controller specified by ControllerHandle. This test
511 is performed by evaluating if the the protocol specified by ProtocolGuid is
512 present on ControllerHandle and is was opened by DriverBindingHandle with an
513 attribute of EFI_OPEN_PROTOCOL_BY_DRIVER.
514 If ProtocolGuid is NULL, then ASSERT().
516 @param ControllerHandle A handle for a controller to test.
517 @param DriverBindingHandle Specifies the driver binding handle for the
519 @param ProtocolGuid Specifies the protocol that the driver specified
520 by DriverBindingHandle opens in its Start()
523 @retval EFI_SUCCESS ControllerHandle is managed by the driver
524 specified by DriverBindingHandle.
525 @retval EFI_UNSUPPORTED ControllerHandle is not managed by the driver
526 specified by DriverBindingHandle.
531 EfiTestManagedDevice (
532 IN CONST EFI_HANDLE ControllerHandle
,
533 IN CONST EFI_HANDLE DriverBindingHandle
,
534 IN CONST EFI_GUID
*ProtocolGuid
538 VOID
*ManagedInterface
;
540 ASSERT (ProtocolGuid
!= NULL
);
542 Status
= gBS
->OpenProtocol (
544 (EFI_GUID
*) ProtocolGuid
,
548 EFI_OPEN_PROTOCOL_BY_DRIVER
550 if (!EFI_ERROR (Status
)) {
553 (EFI_GUID
*) ProtocolGuid
,
557 return EFI_UNSUPPORTED
;
560 if (Status
!= EFI_ALREADY_STARTED
) {
561 return EFI_UNSUPPORTED
;
568 Tests whether a child handle is a child device of the controller.
570 This function tests whether ChildHandle is one of the children of
571 ControllerHandle. This test is performed by checking to see if the protocol
572 specified by ProtocolGuid is present on ControllerHandle and opened by
573 ChildHandle with an attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
574 If ProtocolGuid is NULL, then ASSERT().
576 @param ControllerHandle A handle for a (parent) controller to test.
577 @param ChildHandle A child handle to test.
578 @param ProtocolGuid Supplies the protocol that the child controller
579 opens on its parent controller.
581 @retval EFI_SUCCESS ChildHandle is a child of the ControllerHandle.
582 @retval EFI_UNSUPPORTED ChildHandle is not a child of the
589 IN CONST EFI_HANDLE ControllerHandle
,
590 IN CONST EFI_HANDLE ChildHandle
,
591 IN CONST EFI_GUID
*ProtocolGuid
595 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfoBuffer
;
599 ASSERT (ProtocolGuid
!= NULL
);
602 // Retrieve the list of agents that are consuming the specific protocol
603 // on ControllerHandle.
605 Status
= gBS
->OpenProtocolInformation (
607 (EFI_GUID
*) ProtocolGuid
,
611 if (EFI_ERROR (Status
)) {
612 return EFI_UNSUPPORTED
;
616 // Inspect if ChildHandle is one of the agents.
618 Status
= EFI_UNSUPPORTED
;
619 for (Index
= 0; Index
< EntryCount
; Index
++) {
620 if ((OpenInfoBuffer
[Index
].ControllerHandle
== ChildHandle
) &&
621 (OpenInfoBuffer
[Index
].Attributes
& EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
) != 0) {
622 Status
= EFI_SUCCESS
;
627 FreePool (OpenInfoBuffer
);
632 This function looks up a Unicode string in UnicodeStringTable.
634 If Language is a member of SupportedLanguages and a Unicode string is found in
635 UnicodeStringTable that matches the language code specified by Language, then it
636 is returned in UnicodeString.
638 @param Language A pointer to the ISO 639-2 language code for the
639 Unicode string to look up and return.
640 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes
641 that the Unicode string table supports. Language
642 must be a member of this set.
643 @param UnicodeStringTable A pointer to the table of Unicode strings.
644 @param UnicodeString A pointer to the Unicode string from UnicodeStringTable
645 that matches the language specified by Language.
647 @retval EFI_SUCCESS The Unicode string that matches the language
648 specified by Language was found
649 in the table of Unicode strings UnicodeStringTable,
650 and it was returned in UnicodeString.
651 @retval EFI_INVALID_PARAMETER Language is NULL.
652 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
653 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
654 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.
655 @retval EFI_UNSUPPORTED The language specified by Language is not a
656 member of SupportedLanguages.
657 @retval EFI_UNSUPPORTED The language specified by Language is not
658 supported by UnicodeStringTable.
663 LookupUnicodeString (
664 IN CONST CHAR8
*Language
,
665 IN CONST CHAR8
*SupportedLanguages
,
666 IN CONST EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
,
667 OUT CHAR16
**UnicodeString
671 // Make sure the parameters are valid
673 if (Language
== NULL
|| UnicodeString
== NULL
) {
674 return EFI_INVALID_PARAMETER
;
678 // If there are no supported languages, or the Unicode String Table is empty, then the
679 // Unicode String specified by Language is not supported by this Unicode String Table
681 if (SupportedLanguages
== NULL
|| UnicodeStringTable
== NULL
) {
682 return EFI_UNSUPPORTED
;
686 // Make sure Language is in the set of Supported Languages
688 while (*SupportedLanguages
!= 0) {
689 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
692 // Search the Unicode String Table for the matching Language specifier
694 while (UnicodeStringTable
->Language
!= NULL
) {
695 if (CompareIso639LanguageCode (Language
, UnicodeStringTable
->Language
)) {
698 // A matching string was found, so return it
700 *UnicodeString
= UnicodeStringTable
->UnicodeString
;
704 UnicodeStringTable
++;
707 return EFI_UNSUPPORTED
;
710 SupportedLanguages
+= 3;
713 return EFI_UNSUPPORTED
;
719 This function looks up a Unicode string in UnicodeStringTable.
721 If Language is a member of SupportedLanguages and a Unicode string is found in
722 UnicodeStringTable that matches the language code specified by Language, then
723 it is returned in UnicodeString.
725 @param Language A pointer to an ASCII string containing the ISO 639-2 or the
726 RFC 4646 language code for the Unicode string to look up and
727 return. If Iso639Language is TRUE, then this ASCII string is
728 not assumed to be Null-terminated, and only the first three
729 characters are used. If Iso639Language is FALSE, then this ASCII
730 string must be Null-terminated.
731 @param SupportedLanguages A pointer to a Null-terminated ASCII string that contains a
732 set of ISO 639-2 or RFC 4646 language codes that the Unicode
733 string table supports. Language must be a member of this set.
734 If Iso639Language is TRUE, then this string contains one or more
735 ISO 639-2 language codes with no separator characters. If Iso639Language
736 is FALSE, then is string contains one or more RFC 4646 language
737 codes separated by ';'.
738 @param UnicodeStringTable A pointer to the table of Unicode strings. Type EFI_UNICODE_STRING_TABLE
739 is defined in "Related Definitions".
740 @param UnicodeString A pointer to the Null-terminated Unicode string from UnicodeStringTable
741 that matches the language specified by Language.
742 @param Iso639Language Specifies the supported language code format. If it is TRUE, then
743 Language and SupportedLanguages follow ISO 639-2 language code format.
744 Otherwise, they follow RFC 4646 language code format.
747 @retval EFI_SUCCESS The Unicode string that matches the language specified by Language
748 was found in the table of Unicode strings UnicodeStringTable, and
749 it was returned in UnicodeString.
750 @retval EFI_INVALID_PARAMETER Language is NULL.
751 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
752 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
753 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.
754 @retval EFI_UNSUPPORTED The language specified by Language is not a member of SupportedLanguages.
755 @retval EFI_UNSUPPORTED The language specified by Language is not supported by UnicodeStringTable.
760 LookupUnicodeString2 (
761 IN CONST CHAR8
*Language
,
762 IN CONST CHAR8
*SupportedLanguages
,
763 IN CONST EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
,
764 OUT CHAR16
**UnicodeString
,
765 IN BOOLEAN Iso639Language
770 CHAR8
*LanguageString
;
773 // Make sure the parameters are valid
775 if (Language
== NULL
|| UnicodeString
== NULL
) {
776 return EFI_INVALID_PARAMETER
;
780 // If there are no supported languages, or the Unicode String Table is empty, then the
781 // Unicode String specified by Language is not supported by this Unicode String Table
783 if (SupportedLanguages
== NULL
|| UnicodeStringTable
== NULL
) {
784 return EFI_UNSUPPORTED
;
788 // Make sure Language is in the set of Supported Languages
791 while (*SupportedLanguages
!= 0) {
792 if (Iso639Language
) {
793 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
797 SupportedLanguages
+= 3;
799 for (Index
= 0; SupportedLanguages
[Index
] != 0 && SupportedLanguages
[Index
] != ';'; Index
++);
800 if ((AsciiStrnCmp(SupportedLanguages
, Language
, Index
) == 0) && (Language
[Index
] == 0)) {
804 SupportedLanguages
+= Index
;
805 for (; *SupportedLanguages
!= 0 && *SupportedLanguages
== ';'; SupportedLanguages
++);
810 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED
813 return EFI_UNSUPPORTED
;
817 // Search the Unicode String Table for the matching Language specifier
819 while (UnicodeStringTable
->Language
!= NULL
) {
820 LanguageString
= UnicodeStringTable
->Language
;
821 while (0 != *LanguageString
) {
822 for (Index
= 0 ;LanguageString
[Index
] != 0 && LanguageString
[Index
] != ';'; Index
++);
823 if (AsciiStrnCmp(LanguageString
, Language
, Index
) == 0) {
824 *UnicodeString
= UnicodeStringTable
->UnicodeString
;
827 LanguageString
+= Index
;
828 for (Index
= 0 ;LanguageString
[Index
] != 0 && LanguageString
[Index
] == ';'; Index
++);
830 UnicodeStringTable
++;
833 return EFI_UNSUPPORTED
;
838 This function adds a Unicode string to UnicodeStringTable.
840 If Language is a member of SupportedLanguages then UnicodeString is added to
841 UnicodeStringTable. New buffers are allocated for both Language and
842 UnicodeString. The contents of Language and UnicodeString are copied into
843 these new buffers. These buffers are automatically freed when
844 FreeUnicodeStringTable() is called.
846 @param Language A pointer to the ISO 639-2 language code for the Unicode
848 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes
849 that the Unicode string table supports.
850 Language must be a member of this set.
851 @param UnicodeStringTable A pointer to the table of Unicode strings.
852 @param UnicodeString A pointer to the Unicode string to add.
854 @retval EFI_SUCCESS The Unicode string that matches the language
855 specified by Language was found in the table of
856 Unicode strings UnicodeStringTable, and it was
857 returned in UnicodeString.
858 @retval EFI_INVALID_PARAMETER Language is NULL.
859 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
860 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.
861 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
862 @retval EFI_ALREADY_STARTED A Unicode string with language Language is
863 already present in UnicodeStringTable.
864 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another
865 Unicode string to UnicodeStringTable.
866 @retval EFI_UNSUPPORTED The language specified by Language is not a
867 member of SupportedLanguages.
873 IN CONST CHAR8
*Language
,
874 IN CONST CHAR8
*SupportedLanguages
,
875 IN EFI_UNICODE_STRING_TABLE
**UnicodeStringTable
,
876 IN CONST CHAR16
*UnicodeString
879 UINTN NumberOfEntries
;
880 EFI_UNICODE_STRING_TABLE
*OldUnicodeStringTable
;
881 EFI_UNICODE_STRING_TABLE
*NewUnicodeStringTable
;
882 UINTN UnicodeStringLength
;
885 // Make sure the parameter are valid
887 if (Language
== NULL
|| UnicodeString
== NULL
|| UnicodeStringTable
== NULL
) {
888 return EFI_INVALID_PARAMETER
;
892 // If there are no supported languages, then a Unicode String can not be added
894 if (SupportedLanguages
== NULL
) {
895 return EFI_UNSUPPORTED
;
899 // If the Unicode String is empty, then a Unicode String can not be added
901 if (UnicodeString
[0] == 0) {
902 return EFI_INVALID_PARAMETER
;
906 // Make sure Language is a member of SupportedLanguages
908 while (*SupportedLanguages
!= 0) {
909 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
912 // Determine the size of the Unicode String Table by looking for a NULL Language entry
915 if (*UnicodeStringTable
!= NULL
) {
916 OldUnicodeStringTable
= *UnicodeStringTable
;
917 while (OldUnicodeStringTable
->Language
!= NULL
) {
918 if (CompareIso639LanguageCode (Language
, OldUnicodeStringTable
->Language
)) {
919 return EFI_ALREADY_STARTED
;
922 OldUnicodeStringTable
++;
928 // Allocate space for a new Unicode String Table. It must hold the current number of
929 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table
932 NewUnicodeStringTable
= AllocatePool ((NumberOfEntries
+ 2) * sizeof (EFI_UNICODE_STRING_TABLE
));
933 if (NewUnicodeStringTable
== NULL
) {
934 return EFI_OUT_OF_RESOURCES
;
938 // If the current Unicode String Table contains any entries, then copy them to the
939 // newly allocated Unicode String Table.
941 if (*UnicodeStringTable
!= NULL
) {
943 NewUnicodeStringTable
,
945 NumberOfEntries
* sizeof (EFI_UNICODE_STRING_TABLE
)
950 // Allocate space for a copy of the Language specifier
952 NewUnicodeStringTable
[NumberOfEntries
].Language
= AllocateCopyPool (3, Language
);
953 if (NewUnicodeStringTable
[NumberOfEntries
].Language
== NULL
) {
954 FreePool (NewUnicodeStringTable
);
955 return EFI_OUT_OF_RESOURCES
;
959 // Compute the length of the Unicode String
961 for (UnicodeStringLength
= 0; UnicodeString
[UnicodeStringLength
] != 0; UnicodeStringLength
++)
965 // Allocate space for a copy of the Unicode String
967 NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
= AllocateCopyPool (
968 (UnicodeStringLength
+ 1) * sizeof (CHAR16
),
971 if (NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
== NULL
) {
972 FreePool (NewUnicodeStringTable
[NumberOfEntries
].Language
);
973 FreePool (NewUnicodeStringTable
);
974 return EFI_OUT_OF_RESOURCES
;
978 // Mark the end of the Unicode String Table
980 NewUnicodeStringTable
[NumberOfEntries
+ 1].Language
= NULL
;
981 NewUnicodeStringTable
[NumberOfEntries
+ 1].UnicodeString
= NULL
;
984 // Free the old Unicode String Table
986 if (*UnicodeStringTable
!= NULL
) {
987 FreePool (*UnicodeStringTable
);
991 // Point UnicodeStringTable at the newly allocated Unicode String Table
993 *UnicodeStringTable
= NewUnicodeStringTable
;
998 SupportedLanguages
+= 3;
1001 return EFI_UNSUPPORTED
;
1006 This function adds the Null-terminated Unicode string specified by UnicodeString
1007 to UnicodeStringTable.
1009 If Language is a member of SupportedLanguages then UnicodeString is added to
1010 UnicodeStringTable. New buffers are allocated for both Language and UnicodeString.
1011 The contents of Language and UnicodeString are copied into these new buffers.
1012 These buffers are automatically freed when EfiLibFreeUnicodeStringTable() is called.
1014 @param Language A pointer to an ASCII string containing the ISO 639-2 or
1015 the RFC 4646 language code for the Unicode string to add.
1016 If Iso639Language is TRUE, then this ASCII string is not
1017 assumed to be Null-terminated, and only the first three
1018 chacters are used. If Iso639Language is FALSE, then this
1019 ASCII string must be Null-terminated.
1020 @param SupportedLanguages A pointer to a Null-terminated ASCII string that contains
1021 a set of ISO 639-2 or RFC 4646 language codes that the Unicode
1022 string table supports. Language must be a member of this set.
1023 If Iso639Language is TRUE, then this string contains one or more
1024 ISO 639-2 language codes with no separator characters.
1025 If Iso639Language is FALSE, then is string contains one or more
1026 RFC 4646 language codes separated by ';'.
1027 @param UnicodeStringTable A pointer to the table of Unicode strings. Type EFI_UNICODE_STRING_TABLE
1028 is defined in "Related Definitions".
1029 @param UnicodeString A pointer to the Unicode string to add.
1030 @param Iso639Language Specifies the supported language code format. If it is TRUE,
1031 then Language and SupportedLanguages follow ISO 639-2 language code format.
1032 Otherwise, they follow RFC 4646 language code format.
1034 @retval EFI_SUCCESS The Unicode string that matches the language specified by
1035 Language was found in the table of Unicode strings UnicodeStringTable,
1036 and it was returned in UnicodeString.
1037 @retval EFI_INVALID_PARAMETER Language is NULL.
1038 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
1039 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.
1040 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
1041 @retval EFI_ALREADY_STARTED A Unicode string with language Language is already present in
1043 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another Unicode string UnicodeStringTable.
1044 @retval EFI_UNSUPPORTED The language specified by Language is not a member of SupportedLanguages.
1050 IN CONST CHAR8
*Language
,
1051 IN CONST CHAR8
*SupportedLanguages
,
1052 IN EFI_UNICODE_STRING_TABLE
**UnicodeStringTable
,
1053 IN CONST CHAR16
*UnicodeString
,
1054 IN BOOLEAN Iso639Language
1057 UINTN NumberOfEntries
;
1058 EFI_UNICODE_STRING_TABLE
*OldUnicodeStringTable
;
1059 EFI_UNICODE_STRING_TABLE
*NewUnicodeStringTable
;
1060 UINTN UnicodeStringLength
;
1063 CHAR8
*LanguageString
;
1066 // Make sure the parameter are valid
1068 if (Language
== NULL
|| UnicodeString
== NULL
|| UnicodeStringTable
== NULL
) {
1069 return EFI_INVALID_PARAMETER
;
1073 // If there are no supported languages, then a Unicode String can not be added
1075 if (SupportedLanguages
== NULL
) {
1076 return EFI_UNSUPPORTED
;
1080 // If the Unicode String is empty, then a Unicode String can not be added
1082 if (UnicodeString
[0] == 0) {
1083 return EFI_INVALID_PARAMETER
;
1087 // Make sure Language is a member of SupportedLanguages
1090 while (*SupportedLanguages
!= 0) {
1091 if (Iso639Language
) {
1092 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
1096 SupportedLanguages
+= 3;
1098 for (Index
= 0; SupportedLanguages
[Index
] != 0 && SupportedLanguages
[Index
] != ';'; Index
++);
1099 if (AsciiStrnCmp(SupportedLanguages
, Language
, Index
) == 0) {
1103 SupportedLanguages
+= Index
;
1104 for (; *SupportedLanguages
!= 0 && *SupportedLanguages
== ';'; SupportedLanguages
++);
1109 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED
1112 return EFI_UNSUPPORTED
;
1116 // Determine the size of the Unicode String Table by looking for a NULL Language entry
1118 NumberOfEntries
= 0;
1119 if (*UnicodeStringTable
!= NULL
) {
1120 OldUnicodeStringTable
= *UnicodeStringTable
;
1121 while (OldUnicodeStringTable
->Language
!= NULL
) {
1122 LanguageString
= OldUnicodeStringTable
->Language
;
1124 while (*LanguageString
!= 0) {
1125 for (Index
= 0; LanguageString
[Index
] != 0 && LanguageString
[Index
] != ';'; Index
++);
1127 if (AsciiStrnCmp (Language
, LanguageString
, Index
) == 0) {
1128 return EFI_ALREADY_STARTED
;
1130 LanguageString
+= Index
;
1131 for (; *LanguageString
!= 0 && *LanguageString
== ';'; LanguageString
++);
1133 OldUnicodeStringTable
++;
1139 // Allocate space for a new Unicode String Table. It must hold the current number of
1140 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table
1143 NewUnicodeStringTable
= AllocatePool ((NumberOfEntries
+ 2) * sizeof (EFI_UNICODE_STRING_TABLE
));
1144 if (NewUnicodeStringTable
== NULL
) {
1145 return EFI_OUT_OF_RESOURCES
;
1149 // If the current Unicode String Table contains any entries, then copy them to the
1150 // newly allocated Unicode String Table.
1152 if (*UnicodeStringTable
!= NULL
) {
1154 NewUnicodeStringTable
,
1155 *UnicodeStringTable
,
1156 NumberOfEntries
* sizeof (EFI_UNICODE_STRING_TABLE
)
1161 // Allocate space for a copy of the Language specifier
1163 NewUnicodeStringTable
[NumberOfEntries
].Language
= AllocateCopyPool (AsciiStrSize(Language
), Language
);
1164 if (NewUnicodeStringTable
[NumberOfEntries
].Language
== NULL
) {
1165 FreePool (NewUnicodeStringTable
);
1166 return EFI_OUT_OF_RESOURCES
;
1170 // Compute the length of the Unicode String
1172 for (UnicodeStringLength
= 0; UnicodeString
[UnicodeStringLength
] != 0; UnicodeStringLength
++);
1175 // Allocate space for a copy of the Unicode String
1177 NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
= AllocateCopyPool (StrSize (UnicodeString
), UnicodeString
);
1178 if (NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
== NULL
) {
1179 FreePool (NewUnicodeStringTable
[NumberOfEntries
].Language
);
1180 FreePool (NewUnicodeStringTable
);
1181 return EFI_OUT_OF_RESOURCES
;
1185 // Mark the end of the Unicode String Table
1187 NewUnicodeStringTable
[NumberOfEntries
+ 1].Language
= NULL
;
1188 NewUnicodeStringTable
[NumberOfEntries
+ 1].UnicodeString
= NULL
;
1191 // Free the old Unicode String Table
1193 if (*UnicodeStringTable
!= NULL
) {
1194 FreePool (*UnicodeStringTable
);
1198 // Point UnicodeStringTable at the newly allocated Unicode String Table
1200 *UnicodeStringTable
= NewUnicodeStringTable
;
1206 This function frees the table of Unicode strings in UnicodeStringTable.
1208 If UnicodeStringTable is NULL, then EFI_SUCCESS is returned.
1209 Otherwise, each language code, and each Unicode string in the Unicode string
1210 table are freed, and EFI_SUCCESS is returned.
1212 @param UnicodeStringTable A pointer to the table of Unicode strings.
1214 @retval EFI_SUCCESS The Unicode string table was freed.
1219 FreeUnicodeStringTable (
1220 IN EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
1226 // If the Unicode String Table is NULL, then it is already freed
1228 if (UnicodeStringTable
== NULL
) {
1233 // Loop through the Unicode String Table until we reach the end of table marker
1235 for (Index
= 0; UnicodeStringTable
[Index
].Language
!= NULL
; Index
++) {
1238 // Free the Language string from the Unicode String Table
1240 FreePool (UnicodeStringTable
[Index
].Language
);
1243 // Free the Unicode String from the Unicode String Table
1245 if (UnicodeStringTable
[Index
].UnicodeString
!= NULL
) {
1246 FreePool (UnicodeStringTable
[Index
].UnicodeString
);
1251 // Free the Unicode String Table itself
1253 FreePool (UnicodeStringTable
);
1258 #ifndef DISABLE_NEW_DEPRECATED_INTERFACES
1261 [ATTENTION] This function will be deprecated for security reason.
1263 Returns a pointer to an allocated buffer that contains the contents of a
1264 variable retrieved through the UEFI Runtime Service GetVariable(). The
1265 returned buffer is allocated using AllocatePool(). The caller is responsible
1266 for freeing this buffer with FreePool().
1268 If Name is NULL, then ASSERT().
1269 If Guid is NULL, then ASSERT().
1271 @param[in] Name The pointer to a Null-terminated Unicode string.
1272 @param[in] Guid The pointer to an EFI_GUID structure
1274 @retval NULL The variable could not be retrieved.
1275 @retval NULL There are not enough resources available for the variable contents.
1276 @retval Other A pointer to allocated buffer containing the variable contents.
1282 IN CONST CHAR16
*Name
,
1283 IN CONST EFI_GUID
*Guid
1290 ASSERT (Name
!= NULL
);
1291 ASSERT (Guid
!= NULL
);
1294 // Try to get the variable size.
1298 Status
= gRT
->GetVariable ((CHAR16
*) Name
, (EFI_GUID
*) Guid
, NULL
, &Size
, Value
);
1299 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
1304 // Allocate buffer to get the variable.
1306 Value
= AllocatePool (Size
);
1307 if (Value
== NULL
) {
1312 // Get the variable data.
1314 Status
= gRT
->GetVariable ((CHAR16
*) Name
, (EFI_GUID
*) Guid
, NULL
, &Size
, Value
);
1315 if (EFI_ERROR (Status
)) {
1324 [ATTENTION] This function will be deprecated for security reason.
1326 Returns a pointer to an allocated buffer that contains the contents of a
1327 variable retrieved through the UEFI Runtime Service GetVariable(). This
1328 function always uses the EFI_GLOBAL_VARIABLE GUID to retrieve variables.
1329 The returned buffer is allocated using AllocatePool(). The caller is
1330 responsible for freeing this buffer with FreePool().
1332 If Name is NULL, then ASSERT().
1334 @param[in] Name The pointer to a Null-terminated Unicode string.
1336 @retval NULL The variable could not be retrieved.
1337 @retval NULL There are not enough resources available for the variable contents.
1338 @retval Other A pointer to allocated buffer containing the variable contents.
1343 GetEfiGlobalVariable (
1344 IN CONST CHAR16
*Name
1347 return GetVariable (Name
, &gEfiGlobalVariableGuid
);
1352 Returns the status whether get the variable success. The function retrieves
1353 variable through the UEFI Runtime Service GetVariable(). The
1354 returned buffer is allocated using AllocatePool(). The caller is responsible
1355 for freeing this buffer with FreePool().
1357 If Name is NULL, then ASSERT().
1358 If Guid is NULL, then ASSERT().
1359 If Value is NULL, then ASSERT().
1361 @param[in] Name The pointer to a Null-terminated Unicode string.
1362 @param[in] Guid The pointer to an EFI_GUID structure
1363 @param[out] Value The buffer point saved the variable info.
1364 @param[out] Size The buffer size of the variable.
1366 @return EFI_OUT_OF_RESOURCES Allocate buffer failed.
1367 @return EFI_SUCCESS Find the specified variable.
1368 @return Others Errors Return errors from call to gRT->GetVariable.
1374 IN CONST CHAR16
*Name
,
1375 IN CONST EFI_GUID
*Guid
,
1377 OUT UINTN
*Size OPTIONAL
1383 ASSERT (Name
!= NULL
&& Guid
!= NULL
&& Value
!= NULL
);
1386 // Try to get the variable size.
1394 Status
= gRT
->GetVariable ((CHAR16
*) Name
, (EFI_GUID
*) Guid
, NULL
, &BufferSize
, *Value
);
1395 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
1400 // Allocate buffer to get the variable.
1402 *Value
= AllocatePool (BufferSize
);
1403 ASSERT (*Value
!= NULL
);
1404 if (*Value
== NULL
) {
1405 return EFI_OUT_OF_RESOURCES
;
1409 // Get the variable data.
1411 Status
= gRT
->GetVariable ((CHAR16
*) Name
, (EFI_GUID
*) Guid
, NULL
, &BufferSize
, *Value
);
1412 if (EFI_ERROR (Status
)) {
1425 Returns a pointer to an allocated buffer that contains the contents of a
1426 variable retrieved through the UEFI Runtime Service GetVariable(). This
1427 function always uses the EFI_GLOBAL_VARIABLE GUID to retrieve variables.
1428 The returned buffer is allocated using AllocatePool(). The caller is
1429 responsible for freeing this buffer with FreePool().
1431 If Name is NULL, then ASSERT().
1432 If Value is NULL, then ASSERT().
1434 @param[in] Name The pointer to a Null-terminated Unicode string.
1435 @param[out] Value The buffer point saved the variable info.
1436 @param[out] Size The buffer size of the variable.
1438 @return EFI_OUT_OF_RESOURCES Allocate buffer failed.
1439 @return EFI_SUCCESS Find the specified variable.
1440 @return Others Errors Return errors from call to gRT->GetVariable.
1445 GetEfiGlobalVariable2 (
1446 IN CONST CHAR16
*Name
,
1448 OUT UINTN
*Size OPTIONAL
1451 return GetVariable2 (Name
, &gEfiGlobalVariableGuid
, Value
, Size
);
1455 Returns a pointer to an allocated buffer that contains the best matching language
1456 from a set of supported languages.
1458 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1459 code types may not be mixed in a single call to this function. The language
1460 code returned is allocated using AllocatePool(). The caller is responsible for
1461 freeing the allocated buffer using FreePool(). This function supports a variable
1462 argument list that allows the caller to pass in a prioritized list of language
1463 codes to test against all the language codes in SupportedLanguages.
1465 If SupportedLanguages is NULL, then ASSERT().
1467 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1468 contains a set of language codes in the format
1469 specified by Iso639Language.
1470 @param[in] Iso639Language If TRUE, then all language codes are assumed to be
1471 in ISO 639-2 format. If FALSE, then all language
1472 codes are assumed to be in RFC 4646 language format
1473 @param[in] ... A variable argument list that contains pointers to
1474 Null-terminated ASCII strings that contain one or more
1475 language codes in the format specified by Iso639Language.
1476 The first language code from each of these language
1477 code lists is used to determine if it is an exact or
1478 close match to any of the language codes in
1479 SupportedLanguages. Close matches only apply to RFC 4646
1480 language codes, and the matching algorithm from RFC 4647
1481 is used to determine if a close match is present. If
1482 an exact or close match is found, then the matching
1483 language code from SupportedLanguages is returned. If
1484 no matches are found, then the next variable argument
1485 parameter is evaluated. The variable argument list
1486 is terminated by a NULL.
1488 @retval NULL The best matching language could not be found in SupportedLanguages.
1489 @retval NULL There are not enough resources available to return the best matching
1491 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1492 language in SupportedLanguages.
1498 IN CONST CHAR8
*SupportedLanguages
,
1499 IN BOOLEAN Iso639Language
,
1505 UINTN CompareLength
;
1506 UINTN LanguageLength
;
1507 CONST CHAR8
*Supported
;
1508 CHAR8
*BestLanguage
;
1510 ASSERT (SupportedLanguages
!= NULL
);
1512 VA_START (Args
, Iso639Language
);
1513 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
1515 // Default to ISO 639-2 mode
1518 LanguageLength
= MIN (3, AsciiStrLen (Language
));
1521 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1523 if (!Iso639Language
) {
1524 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
1528 // Trim back the length of Language used until it is empty
1530 while (LanguageLength
> 0) {
1532 // Loop through all language codes in SupportedLanguages
1534 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
1536 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1538 if (!Iso639Language
) {
1540 // Skip ';' characters in Supported
1542 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1544 // Determine the length of the next language code in Supported
1546 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
1548 // If Language is longer than the Supported, then skip to the next language
1550 if (LanguageLength
> CompareLength
) {
1555 // See if the first LanguageLength characters in Supported match Language
1557 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
1560 // Allocate, copy, and return the best matching language code from SupportedLanguages
1562 BestLanguage
= AllocateZeroPool (CompareLength
+ 1);
1563 if (BestLanguage
== NULL
) {
1566 return CopyMem (BestLanguage
, Supported
, CompareLength
);
1570 if (Iso639Language
) {
1572 // If ISO 639 mode, then each language can only be tested once
1577 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1579 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
1586 // No matches were found
1592 An empty function to pass error checking of CreateEventEx ().
1594 This empty function ensures that EVT_NOTIFY_SIGNAL_ALL is error
1595 checked correctly since it is now mapped into CreateEventEx() in UEFI 2.0.
1597 @param Event Event whose notification function is being invoked.
1598 @param Context The pointer to the notification function's context,
1599 which is implementation-dependent.
1604 InternalEmptyFunction (