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 - 2017, 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 Compare whether two names of languages are identical.
25 @param Language1 Name of language 1.
26 @param Language2 Name of language 2.
28 @retval TRUE Language 1 and language 2 are the same.
29 @retval FALSE Language 1 and language 2 are not the same.
33 CompareIso639LanguageCode (
34 IN CONST CHAR8
*Language1
,
35 IN CONST CHAR8
*Language2
41 Name1
= ReadUnaligned24 ((CONST UINT32
*) Language1
);
42 Name2
= ReadUnaligned24 ((CONST UINT32
*) Language2
);
44 return (BOOLEAN
) (Name1
== Name2
);
48 Retrieves a pointer to the system configuration table from the EFI System Table
49 based on a specified GUID.
51 This function searches the list of configuration tables stored in the EFI System Table
52 for a table with a GUID that matches TableGuid. If a match is found, then a pointer to
53 the configuration table is returned in Table., and EFI_SUCCESS is returned. If a matching GUID
54 is not found, then EFI_NOT_FOUND is returned.
55 If TableGuid is NULL, then ASSERT().
56 If Table is NULL, then ASSERT().
58 @param TableGuid Pointer to table's GUID type..
59 @param Table Pointer to the table associated with TableGuid in the EFI System Table.
61 @retval EFI_SUCCESS A configuration table matching TableGuid was found.
62 @retval EFI_NOT_FOUND A configuration table matching TableGuid could not be found.
67 EfiGetSystemConfigurationTable (
68 IN EFI_GUID
*TableGuid
,
72 EFI_SYSTEM_TABLE
*SystemTable
;
75 ASSERT (TableGuid
!= NULL
);
76 ASSERT (Table
!= NULL
);
80 for (Index
= 0; Index
< SystemTable
->NumberOfTableEntries
; Index
++) {
81 if (CompareGuid (TableGuid
, &(SystemTable
->ConfigurationTable
[Index
].VendorGuid
))) {
82 *Table
= SystemTable
->ConfigurationTable
[Index
].VendorTable
;
91 Creates and returns a notification event and registers that event with all the protocol
92 instances specified by ProtocolGuid.
94 This function causes the notification function to be executed for every protocol of type
95 ProtocolGuid instance that exists in the system when this function is invoked. If there are
96 no instances of ProtocolGuid in the handle database at the time this function is invoked,
97 then the notification function is still executed one time. In addition, every time a protocol
98 of type ProtocolGuid instance is installed or reinstalled, the notification function is also
99 executed. This function returns the notification event that was created.
100 If ProtocolGuid is NULL, then ASSERT().
101 If NotifyTpl is not a legal TPL value, then ASSERT().
102 If NotifyFunction is NULL, then ASSERT().
103 If Registration is NULL, then ASSERT().
106 @param ProtocolGuid Supplies GUID of the protocol upon whose installation the event is fired.
107 @param NotifyTpl Supplies the task priority level of the event notifications.
108 @param NotifyFunction Supplies the function to notify when the event is signaled.
109 @param NotifyContext The context parameter to pass to NotifyFunction.
110 @param Registration A pointer to a memory location to receive the registration value.
111 This value is passed to LocateHandle() to obtain new handles that
112 have been added that support the ProtocolGuid-specified protocol.
114 @return The notification event that was created.
119 EfiCreateProtocolNotifyEvent(
120 IN EFI_GUID
*ProtocolGuid
,
121 IN EFI_TPL NotifyTpl
,
122 IN EFI_EVENT_NOTIFY NotifyFunction
,
123 IN VOID
*NotifyContext
, OPTIONAL
124 OUT VOID
**Registration
130 ASSERT (ProtocolGuid
!= NULL
);
131 ASSERT (NotifyFunction
!= NULL
);
132 ASSERT (Registration
!= NULL
);
138 Status
= gBS
->CreateEvent (
145 ASSERT_EFI_ERROR (Status
);
148 // Register for protocol notifications on this event
151 Status
= gBS
->RegisterProtocolNotify (
157 ASSERT_EFI_ERROR (Status
);
160 // Kick the event so we will perform an initial pass of
161 // current installed drivers
164 gBS
->SignalEvent (Event
);
169 Creates a named event that can be signaled with EfiNamedEventSignal().
171 This function creates an event using NotifyTpl, NoifyFunction, and NotifyContext.
172 This event is signaled with EfiNamedEventSignal(). This provides the ability for one or more
173 listeners on the same event named by the GUID specified by Name.
174 If Name is NULL, then ASSERT().
175 If NotifyTpl is not a legal TPL value, then ASSERT().
176 If NotifyFunction is NULL, then ASSERT().
178 @param Name Supplies GUID name of the event.
179 @param NotifyTpl Supplies the task priority level of the event notifications.
180 @param NotifyFunction Supplies the function to notify when the event is signaled.
181 @param NotifyContext The context parameter to pass to NotifyFunction.
182 @param Registration A pointer to a memory location to receive the registration value.
184 @retval EFI_SUCCESS A named event was created.
185 @retval EFI_OUT_OF_RESOURCES There are not enough resource to create the named event.
190 EfiNamedEventListen (
191 IN CONST EFI_GUID
*Name
,
192 IN EFI_TPL NotifyTpl
,
193 IN EFI_EVENT_NOTIFY NotifyFunction
,
194 IN CONST VOID
*NotifyContext
, OPTIONAL
195 OUT VOID
*Registration OPTIONAL
200 VOID
*RegistrationLocal
;
202 ASSERT (Name
!= NULL
);
203 ASSERT (NotifyFunction
!= NULL
);
204 ASSERT (NotifyTpl
<= TPL_HIGH_LEVEL
);
209 Status
= gBS
->CreateEvent (
213 (VOID
*) NotifyContext
,
216 ASSERT_EFI_ERROR (Status
);
219 // The Registration is not optional to RegisterProtocolNotify().
220 // To make it optional to EfiNamedEventListen(), may need to substitute with a local.
222 if (Registration
!= NULL
) {
223 RegistrationLocal
= Registration
;
225 RegistrationLocal
= &RegistrationLocal
;
229 // Register for an installation of protocol interface
232 Status
= gBS
->RegisterProtocolNotify (
237 ASSERT_EFI_ERROR (Status
);
243 Signals a named event created with EfiNamedEventListen().
245 This function signals the named event specified by Name. The named event must have been
246 created with EfiNamedEventListen().
247 If Name is NULL, then ASSERT().
249 @param Name Supplies GUID name of the event.
251 @retval EFI_SUCCESS A named event was signaled.
252 @retval EFI_OUT_OF_RESOURCES There are not enough resource to signal the named event.
257 EfiNamedEventSignal (
258 IN CONST EFI_GUID
*Name
264 ASSERT(Name
!= NULL
);
267 Status
= gBS
->InstallProtocolInterface (
270 EFI_NATIVE_INTERFACE
,
273 ASSERT_EFI_ERROR (Status
);
275 Status
= gBS
->UninstallProtocolInterface (
280 ASSERT_EFI_ERROR (Status
);
286 Signals an event group by placing a new event in the group temporarily and
289 @param[in] EventGroup Supplies the unique identifier of the event
292 @retval EFI_SUCCESS The event group was signaled successfully.
293 @retval EFI_INVALID_PARAMETER EventGroup is NULL.
294 @return Error codes that report problems about event
295 creation or signaling.
299 EfiEventGroupSignal (
300 IN CONST EFI_GUID
*EventGroup
306 if (EventGroup
== NULL
) {
307 return EFI_INVALID_PARAMETER
;
310 Status
= gBS
->CreateEventEx (
313 EfiEventEmptyFunction
,
318 if (EFI_ERROR (Status
)) {
322 Status
= gBS
->SignalEvent (Event
);
323 gBS
->CloseEvent (Event
);
329 An empty function that can be used as NotifyFunction parameter of
330 CreateEvent() or CreateEventEx().
332 @param Event Event whose notification function is being invoked.
333 @param Context The pointer to the notification function's context,
334 which is implementation-dependent.
339 EfiEventEmptyFunction (
347 Returns the current TPL.
349 This function returns the current TPL. There is no EFI service to directly
350 retrieve the current TPL. Instead, the RaiseTPL() function is used to raise
351 the TPL to TPL_HIGH_LEVEL. This will return the current TPL. The TPL level
352 can then immediately be restored back to the current TPL level with a call
355 @return The current TPL.
366 Tpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
367 gBS
->RestoreTPL (Tpl
);
374 Initializes a basic mutual exclusion lock.
376 This function initializes a basic mutual exclusion lock to the released state
377 and returns the lock. Each lock provides mutual exclusion access at its task
378 priority level. Since there is no preemption or multiprocessor support in EFI,
379 acquiring the lock only consists of raising to the locks TPL.
380 If Lock is NULL, then ASSERT().
381 If Priority is not a valid TPL value, then ASSERT().
383 @param Lock A pointer to the lock data structure to initialize.
384 @param Priority EFI TPL associated with the lock.
392 IN OUT EFI_LOCK
*Lock
,
396 ASSERT (Lock
!= NULL
);
397 ASSERT (Priority
<= TPL_HIGH_LEVEL
);
399 Lock
->Tpl
= Priority
;
400 Lock
->OwnerTpl
= TPL_APPLICATION
;
401 Lock
->Lock
= EfiLockReleased
;
406 Acquires ownership of a lock.
408 This function raises the system's current task priority level to the task
409 priority level of the mutual exclusion lock. Then, it places the lock in the
411 If Lock is NULL, then ASSERT().
412 If Lock is not initialized, then ASSERT().
413 If Lock is already in the acquired state, then ASSERT().
415 @param Lock A pointer to the lock to acquire.
424 ASSERT (Lock
!= NULL
);
425 ASSERT (Lock
->Lock
== EfiLockReleased
);
427 Lock
->OwnerTpl
= gBS
->RaiseTPL (Lock
->Tpl
);
428 Lock
->Lock
= EfiLockAcquired
;
432 Acquires ownership of a lock.
434 This function raises the system's current task priority level to the task priority
435 level of the mutual exclusion lock. Then, it attempts to place the lock in the acquired state.
436 If the lock is already in the acquired state, then EFI_ACCESS_DENIED is returned.
437 Otherwise, EFI_SUCCESS is returned.
438 If Lock is NULL, then ASSERT().
439 If Lock is not initialized, then ASSERT().
441 @param Lock A pointer to the lock to acquire.
443 @retval EFI_SUCCESS The lock was acquired.
444 @retval EFI_ACCESS_DENIED The lock could not be acquired because it is already owned.
449 EfiAcquireLockOrFail (
454 ASSERT (Lock
!= NULL
);
455 ASSERT (Lock
->Lock
!= EfiLockUninitialized
);
457 if (Lock
->Lock
== EfiLockAcquired
) {
459 // Lock is already owned, so bail out
461 return EFI_ACCESS_DENIED
;
464 Lock
->OwnerTpl
= gBS
->RaiseTPL (Lock
->Tpl
);
466 Lock
->Lock
= EfiLockAcquired
;
472 Releases ownership of a lock.
474 This function transitions a mutual exclusion lock from the acquired state to
475 the released state, and restores the system's task priority level to its
477 If Lock is NULL, then ASSERT().
478 If Lock is not initialized, then ASSERT().
479 If Lock is already in the released state, then ASSERT().
481 @param Lock A pointer to the lock to release.
492 ASSERT (Lock
!= NULL
);
493 ASSERT (Lock
->Lock
== EfiLockAcquired
);
495 Tpl
= Lock
->OwnerTpl
;
497 Lock
->Lock
= EfiLockReleased
;
499 gBS
->RestoreTPL (Tpl
);
503 Tests whether a controller handle is being managed by a specific driver.
505 This function tests whether the driver specified by DriverBindingHandle is
506 currently managing the controller specified by ControllerHandle. This test
507 is performed by evaluating if the the protocol specified by ProtocolGuid is
508 present on ControllerHandle and is was opened by DriverBindingHandle with an
509 attribute of EFI_OPEN_PROTOCOL_BY_DRIVER.
510 If ProtocolGuid is NULL, then ASSERT().
512 @param ControllerHandle A handle for a controller to test.
513 @param DriverBindingHandle Specifies the driver binding handle for the
515 @param ProtocolGuid Specifies the protocol that the driver specified
516 by DriverBindingHandle opens in its Start()
519 @retval EFI_SUCCESS ControllerHandle is managed by the driver
520 specified by DriverBindingHandle.
521 @retval EFI_UNSUPPORTED ControllerHandle is not managed by the driver
522 specified by DriverBindingHandle.
527 EfiTestManagedDevice (
528 IN CONST EFI_HANDLE ControllerHandle
,
529 IN CONST EFI_HANDLE DriverBindingHandle
,
530 IN CONST EFI_GUID
*ProtocolGuid
534 VOID
*ManagedInterface
;
536 ASSERT (ProtocolGuid
!= NULL
);
538 Status
= gBS
->OpenProtocol (
540 (EFI_GUID
*) ProtocolGuid
,
544 EFI_OPEN_PROTOCOL_BY_DRIVER
546 if (!EFI_ERROR (Status
)) {
549 (EFI_GUID
*) ProtocolGuid
,
553 return EFI_UNSUPPORTED
;
556 if (Status
!= EFI_ALREADY_STARTED
) {
557 return EFI_UNSUPPORTED
;
564 Tests whether a child handle is a child device of the controller.
566 This function tests whether ChildHandle is one of the children of
567 ControllerHandle. This test is performed by checking to see if the protocol
568 specified by ProtocolGuid is present on ControllerHandle and opened by
569 ChildHandle with an attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
570 If ProtocolGuid is NULL, then ASSERT().
572 @param ControllerHandle A handle for a (parent) controller to test.
573 @param ChildHandle A child handle to test.
574 @param ProtocolGuid Supplies the protocol that the child controller
575 opens on its parent controller.
577 @retval EFI_SUCCESS ChildHandle is a child of the ControllerHandle.
578 @retval EFI_UNSUPPORTED ChildHandle is not a child of the
585 IN CONST EFI_HANDLE ControllerHandle
,
586 IN CONST EFI_HANDLE ChildHandle
,
587 IN CONST EFI_GUID
*ProtocolGuid
591 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfoBuffer
;
595 ASSERT (ProtocolGuid
!= NULL
);
598 // Retrieve the list of agents that are consuming the specific protocol
599 // on ControllerHandle.
601 Status
= gBS
->OpenProtocolInformation (
603 (EFI_GUID
*) ProtocolGuid
,
607 if (EFI_ERROR (Status
)) {
608 return EFI_UNSUPPORTED
;
612 // Inspect if ChildHandle is one of the agents.
614 Status
= EFI_UNSUPPORTED
;
615 for (Index
= 0; Index
< EntryCount
; Index
++) {
616 if ((OpenInfoBuffer
[Index
].ControllerHandle
== ChildHandle
) &&
617 (OpenInfoBuffer
[Index
].Attributes
& EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
) != 0) {
618 Status
= EFI_SUCCESS
;
623 FreePool (OpenInfoBuffer
);
628 This function looks up a Unicode string in UnicodeStringTable.
630 If Language is a member of SupportedLanguages and a Unicode string is found in
631 UnicodeStringTable that matches the language code specified by Language, then it
632 is returned in UnicodeString.
634 @param Language A pointer to the ISO 639-2 language code for the
635 Unicode string to look up and return.
636 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes
637 that the Unicode string table supports. Language
638 must be a member of this set.
639 @param UnicodeStringTable A pointer to the table of Unicode strings.
640 @param UnicodeString A pointer to the Unicode string from UnicodeStringTable
641 that matches the language specified by Language.
643 @retval EFI_SUCCESS The Unicode string that matches the language
644 specified by Language was found
645 in the table of Unicode strings UnicodeStringTable,
646 and it was returned in UnicodeString.
647 @retval EFI_INVALID_PARAMETER Language is NULL.
648 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
649 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
650 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.
651 @retval EFI_UNSUPPORTED The language specified by Language is not a
652 member of SupportedLanguages.
653 @retval EFI_UNSUPPORTED The language specified by Language is not
654 supported by UnicodeStringTable.
659 LookupUnicodeString (
660 IN CONST CHAR8
*Language
,
661 IN CONST CHAR8
*SupportedLanguages
,
662 IN CONST EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
,
663 OUT CHAR16
**UnicodeString
667 // Make sure the parameters are valid
669 if (Language
== NULL
|| UnicodeString
== NULL
) {
670 return EFI_INVALID_PARAMETER
;
674 // If there are no supported languages, or the Unicode String Table is empty, then the
675 // Unicode String specified by Language is not supported by this Unicode String Table
677 if (SupportedLanguages
== NULL
|| UnicodeStringTable
== NULL
) {
678 return EFI_UNSUPPORTED
;
682 // Make sure Language is in the set of Supported Languages
684 while (*SupportedLanguages
!= 0) {
685 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
688 // Search the Unicode String Table for the matching Language specifier
690 while (UnicodeStringTable
->Language
!= NULL
) {
691 if (CompareIso639LanguageCode (Language
, UnicodeStringTable
->Language
)) {
694 // A matching string was found, so return it
696 *UnicodeString
= UnicodeStringTable
->UnicodeString
;
700 UnicodeStringTable
++;
703 return EFI_UNSUPPORTED
;
706 SupportedLanguages
+= 3;
709 return EFI_UNSUPPORTED
;
715 This function looks up a Unicode string in UnicodeStringTable.
717 If Language is a member of SupportedLanguages and a Unicode string is found in
718 UnicodeStringTable that matches the language code specified by Language, then
719 it is returned in UnicodeString.
721 @param Language A pointer to an ASCII string containing the ISO 639-2 or the
722 RFC 4646 language code for the Unicode string to look up and
723 return. If Iso639Language is TRUE, then this ASCII string is
724 not assumed to be Null-terminated, and only the first three
725 characters are used. If Iso639Language is FALSE, then this ASCII
726 string must be Null-terminated.
727 @param SupportedLanguages A pointer to a Null-terminated ASCII string that contains a
728 set of ISO 639-2 or RFC 4646 language codes that the Unicode
729 string table supports. Language must be a member of this set.
730 If Iso639Language is TRUE, then this string contains one or more
731 ISO 639-2 language codes with no separator characters. If Iso639Language
732 is FALSE, then is string contains one or more RFC 4646 language
733 codes separated by ';'.
734 @param UnicodeStringTable A pointer to the table of Unicode strings. Type EFI_UNICODE_STRING_TABLE
735 is defined in "Related Definitions".
736 @param UnicodeString A pointer to the Null-terminated Unicode string from UnicodeStringTable
737 that matches the language specified by Language.
738 @param Iso639Language Specifies the supported language code format. If it is TRUE, then
739 Language and SupportedLanguages follow ISO 639-2 language code format.
740 Otherwise, they follow RFC 4646 language code format.
743 @retval EFI_SUCCESS The Unicode string that matches the language specified by Language
744 was found in the table of Unicode strings UnicodeStringTable, and
745 it was returned in UnicodeString.
746 @retval EFI_INVALID_PARAMETER Language is NULL.
747 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
748 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
749 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.
750 @retval EFI_UNSUPPORTED The language specified by Language is not a member of SupportedLanguages.
751 @retval EFI_UNSUPPORTED The language specified by Language is not supported by UnicodeStringTable.
756 LookupUnicodeString2 (
757 IN CONST CHAR8
*Language
,
758 IN CONST CHAR8
*SupportedLanguages
,
759 IN CONST EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
,
760 OUT CHAR16
**UnicodeString
,
761 IN BOOLEAN Iso639Language
766 CHAR8
*LanguageString
;
769 // Make sure the parameters are valid
771 if (Language
== NULL
|| UnicodeString
== NULL
) {
772 return EFI_INVALID_PARAMETER
;
776 // If there are no supported languages, or the Unicode String Table is empty, then the
777 // Unicode String specified by Language is not supported by this Unicode String Table
779 if (SupportedLanguages
== NULL
|| UnicodeStringTable
== NULL
) {
780 return EFI_UNSUPPORTED
;
784 // Make sure Language is in the set of Supported Languages
787 while (*SupportedLanguages
!= 0) {
788 if (Iso639Language
) {
789 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
793 SupportedLanguages
+= 3;
795 for (Index
= 0; SupportedLanguages
[Index
] != 0 && SupportedLanguages
[Index
] != ';'; Index
++);
796 if ((AsciiStrnCmp(SupportedLanguages
, Language
, Index
) == 0) && (Language
[Index
] == 0)) {
800 SupportedLanguages
+= Index
;
801 for (; *SupportedLanguages
!= 0 && *SupportedLanguages
== ';'; SupportedLanguages
++);
806 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED
809 return EFI_UNSUPPORTED
;
813 // Search the Unicode String Table for the matching Language specifier
815 while (UnicodeStringTable
->Language
!= NULL
) {
816 LanguageString
= UnicodeStringTable
->Language
;
817 while (0 != *LanguageString
) {
818 for (Index
= 0 ;LanguageString
[Index
] != 0 && LanguageString
[Index
] != ';'; Index
++);
819 if (AsciiStrnCmp(LanguageString
, Language
, Index
) == 0) {
820 *UnicodeString
= UnicodeStringTable
->UnicodeString
;
823 LanguageString
+= Index
;
824 for (Index
= 0 ;LanguageString
[Index
] != 0 && LanguageString
[Index
] == ';'; Index
++);
826 UnicodeStringTable
++;
829 return EFI_UNSUPPORTED
;
834 This function adds a Unicode string to UnicodeStringTable.
836 If Language is a member of SupportedLanguages then UnicodeString is added to
837 UnicodeStringTable. New buffers are allocated for both Language and
838 UnicodeString. The contents of Language and UnicodeString are copied into
839 these new buffers. These buffers are automatically freed when
840 FreeUnicodeStringTable() is called.
842 @param Language A pointer to the ISO 639-2 language code for the Unicode
844 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes
845 that the Unicode string table supports.
846 Language must be a member of this set.
847 @param UnicodeStringTable A pointer to the table of Unicode strings.
848 @param UnicodeString A pointer to the Unicode string to add.
850 @retval EFI_SUCCESS The Unicode string that matches the language
851 specified by Language was found in the table of
852 Unicode strings UnicodeStringTable, and it was
853 returned in UnicodeString.
854 @retval EFI_INVALID_PARAMETER Language is NULL.
855 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
856 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.
857 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
858 @retval EFI_ALREADY_STARTED A Unicode string with language Language is
859 already present in UnicodeStringTable.
860 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another
861 Unicode string to UnicodeStringTable.
862 @retval EFI_UNSUPPORTED The language specified by Language is not a
863 member of SupportedLanguages.
869 IN CONST CHAR8
*Language
,
870 IN CONST CHAR8
*SupportedLanguages
,
871 IN EFI_UNICODE_STRING_TABLE
**UnicodeStringTable
,
872 IN CONST CHAR16
*UnicodeString
875 UINTN NumberOfEntries
;
876 EFI_UNICODE_STRING_TABLE
*OldUnicodeStringTable
;
877 EFI_UNICODE_STRING_TABLE
*NewUnicodeStringTable
;
878 UINTN UnicodeStringLength
;
881 // Make sure the parameter are valid
883 if (Language
== NULL
|| UnicodeString
== NULL
|| UnicodeStringTable
== NULL
) {
884 return EFI_INVALID_PARAMETER
;
888 // If there are no supported languages, then a Unicode String can not be added
890 if (SupportedLanguages
== NULL
) {
891 return EFI_UNSUPPORTED
;
895 // If the Unicode String is empty, then a Unicode String can not be added
897 if (UnicodeString
[0] == 0) {
898 return EFI_INVALID_PARAMETER
;
902 // Make sure Language is a member of SupportedLanguages
904 while (*SupportedLanguages
!= 0) {
905 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
908 // Determine the size of the Unicode String Table by looking for a NULL Language entry
911 if (*UnicodeStringTable
!= NULL
) {
912 OldUnicodeStringTable
= *UnicodeStringTable
;
913 while (OldUnicodeStringTable
->Language
!= NULL
) {
914 if (CompareIso639LanguageCode (Language
, OldUnicodeStringTable
->Language
)) {
915 return EFI_ALREADY_STARTED
;
918 OldUnicodeStringTable
++;
924 // Allocate space for a new Unicode String Table. It must hold the current number of
925 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table
928 NewUnicodeStringTable
= AllocatePool ((NumberOfEntries
+ 2) * sizeof (EFI_UNICODE_STRING_TABLE
));
929 if (NewUnicodeStringTable
== NULL
) {
930 return EFI_OUT_OF_RESOURCES
;
934 // If the current Unicode String Table contains any entries, then copy them to the
935 // newly allocated Unicode String Table.
937 if (*UnicodeStringTable
!= NULL
) {
939 NewUnicodeStringTable
,
941 NumberOfEntries
* sizeof (EFI_UNICODE_STRING_TABLE
)
946 // Allocate space for a copy of the Language specifier
948 NewUnicodeStringTable
[NumberOfEntries
].Language
= AllocateCopyPool (3, Language
);
949 if (NewUnicodeStringTable
[NumberOfEntries
].Language
== NULL
) {
950 gBS
->FreePool (NewUnicodeStringTable
);
951 return EFI_OUT_OF_RESOURCES
;
955 // Compute the length of the Unicode String
957 for (UnicodeStringLength
= 0; UnicodeString
[UnicodeStringLength
] != 0; UnicodeStringLength
++)
961 // Allocate space for a copy of the Unicode String
963 NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
= AllocateCopyPool (
964 (UnicodeStringLength
+ 1) * sizeof (CHAR16
),
967 if (NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
== NULL
) {
968 gBS
->FreePool (NewUnicodeStringTable
[NumberOfEntries
].Language
);
969 gBS
->FreePool (NewUnicodeStringTable
);
970 return EFI_OUT_OF_RESOURCES
;
974 // Mark the end of the Unicode String Table
976 NewUnicodeStringTable
[NumberOfEntries
+ 1].Language
= NULL
;
977 NewUnicodeStringTable
[NumberOfEntries
+ 1].UnicodeString
= NULL
;
980 // Free the old Unicode String Table
982 if (*UnicodeStringTable
!= NULL
) {
983 gBS
->FreePool (*UnicodeStringTable
);
987 // Point UnicodeStringTable at the newly allocated Unicode String Table
989 *UnicodeStringTable
= NewUnicodeStringTable
;
994 SupportedLanguages
+= 3;
997 return EFI_UNSUPPORTED
;
1002 This function adds the Null-terminated Unicode string specified by UnicodeString
1003 to UnicodeStringTable.
1005 If Language is a member of SupportedLanguages then UnicodeString is added to
1006 UnicodeStringTable. New buffers are allocated for both Language and UnicodeString.
1007 The contents of Language and UnicodeString are copied into these new buffers.
1008 These buffers are automatically freed when EfiLibFreeUnicodeStringTable() is called.
1010 @param Language A pointer to an ASCII string containing the ISO 639-2 or
1011 the RFC 4646 language code for the Unicode string to add.
1012 If Iso639Language is TRUE, then this ASCII string is not
1013 assumed to be Null-terminated, and only the first three
1014 chacters are used. If Iso639Language is FALSE, then this
1015 ASCII string must be Null-terminated.
1016 @param SupportedLanguages A pointer to a Null-terminated ASCII string that contains
1017 a set of ISO 639-2 or RFC 4646 language codes that the Unicode
1018 string table supports. Language must be a member of this set.
1019 If Iso639Language is TRUE, then this string contains one or more
1020 ISO 639-2 language codes with no separator characters.
1021 If Iso639Language is FALSE, then is string contains one or more
1022 RFC 4646 language codes separated by ';'.
1023 @param UnicodeStringTable A pointer to the table of Unicode strings. Type EFI_UNICODE_STRING_TABLE
1024 is defined in "Related Definitions".
1025 @param UnicodeString A pointer to the Unicode string to add.
1026 @param Iso639Language Specifies the supported language code format. If it is TRUE,
1027 then Language and SupportedLanguages follow ISO 639-2 language code format.
1028 Otherwise, they follow RFC 4646 language code format.
1030 @retval EFI_SUCCESS The Unicode string that matches the language specified by
1031 Language was found in the table of Unicode strings UnicodeStringTable,
1032 and it was returned in UnicodeString.
1033 @retval EFI_INVALID_PARAMETER Language is NULL.
1034 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
1035 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.
1036 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
1037 @retval EFI_ALREADY_STARTED A Unicode string with language Language is already present in
1039 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another Unicode string UnicodeStringTable.
1040 @retval EFI_UNSUPPORTED The language specified by Language is not a member of SupportedLanguages.
1046 IN CONST CHAR8
*Language
,
1047 IN CONST CHAR8
*SupportedLanguages
,
1048 IN EFI_UNICODE_STRING_TABLE
**UnicodeStringTable
,
1049 IN CONST CHAR16
*UnicodeString
,
1050 IN BOOLEAN Iso639Language
1053 UINTN NumberOfEntries
;
1054 EFI_UNICODE_STRING_TABLE
*OldUnicodeStringTable
;
1055 EFI_UNICODE_STRING_TABLE
*NewUnicodeStringTable
;
1056 UINTN UnicodeStringLength
;
1059 CHAR8
*LanguageString
;
1062 // Make sure the parameter are valid
1064 if (Language
== NULL
|| UnicodeString
== NULL
|| UnicodeStringTable
== NULL
) {
1065 return EFI_INVALID_PARAMETER
;
1069 // If there are no supported languages, then a Unicode String can not be added
1071 if (SupportedLanguages
== NULL
) {
1072 return EFI_UNSUPPORTED
;
1076 // If the Unicode String is empty, then a Unicode String can not be added
1078 if (UnicodeString
[0] == 0) {
1079 return EFI_INVALID_PARAMETER
;
1083 // Make sure Language is a member of SupportedLanguages
1086 while (*SupportedLanguages
!= 0) {
1087 if (Iso639Language
) {
1088 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
1092 SupportedLanguages
+= 3;
1094 for (Index
= 0; SupportedLanguages
[Index
] != 0 && SupportedLanguages
[Index
] != ';'; Index
++);
1095 if (AsciiStrnCmp(SupportedLanguages
, Language
, Index
) == 0) {
1099 SupportedLanguages
+= Index
;
1100 for (; *SupportedLanguages
!= 0 && *SupportedLanguages
== ';'; SupportedLanguages
++);
1105 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED
1108 return EFI_UNSUPPORTED
;
1112 // Determine the size of the Unicode String Table by looking for a NULL Language entry
1114 NumberOfEntries
= 0;
1115 if (*UnicodeStringTable
!= NULL
) {
1116 OldUnicodeStringTable
= *UnicodeStringTable
;
1117 while (OldUnicodeStringTable
->Language
!= NULL
) {
1118 LanguageString
= OldUnicodeStringTable
->Language
;
1120 while (*LanguageString
!= 0) {
1121 for (Index
= 0; LanguageString
[Index
] != 0 && LanguageString
[Index
] != ';'; Index
++);
1123 if (AsciiStrnCmp (Language
, LanguageString
, Index
) == 0) {
1124 return EFI_ALREADY_STARTED
;
1126 LanguageString
+= Index
;
1127 for (; *LanguageString
!= 0 && *LanguageString
== ';'; LanguageString
++);
1129 OldUnicodeStringTable
++;
1135 // Allocate space for a new Unicode String Table. It must hold the current number of
1136 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table
1139 NewUnicodeStringTable
= AllocatePool ((NumberOfEntries
+ 2) * sizeof (EFI_UNICODE_STRING_TABLE
));
1140 if (NewUnicodeStringTable
== NULL
) {
1141 return EFI_OUT_OF_RESOURCES
;
1145 // If the current Unicode String Table contains any entries, then copy them to the
1146 // newly allocated Unicode String Table.
1148 if (*UnicodeStringTable
!= NULL
) {
1150 NewUnicodeStringTable
,
1151 *UnicodeStringTable
,
1152 NumberOfEntries
* sizeof (EFI_UNICODE_STRING_TABLE
)
1157 // Allocate space for a copy of the Language specifier
1159 NewUnicodeStringTable
[NumberOfEntries
].Language
= AllocateCopyPool (AsciiStrSize(Language
), Language
);
1160 if (NewUnicodeStringTable
[NumberOfEntries
].Language
== NULL
) {
1161 gBS
->FreePool (NewUnicodeStringTable
);
1162 return EFI_OUT_OF_RESOURCES
;
1166 // Compute the length of the Unicode String
1168 for (UnicodeStringLength
= 0; UnicodeString
[UnicodeStringLength
] != 0; UnicodeStringLength
++);
1171 // Allocate space for a copy of the Unicode String
1173 NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
= AllocateCopyPool (StrSize (UnicodeString
), UnicodeString
);
1174 if (NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
== NULL
) {
1175 gBS
->FreePool (NewUnicodeStringTable
[NumberOfEntries
].Language
);
1176 gBS
->FreePool (NewUnicodeStringTable
);
1177 return EFI_OUT_OF_RESOURCES
;
1181 // Mark the end of the Unicode String Table
1183 NewUnicodeStringTable
[NumberOfEntries
+ 1].Language
= NULL
;
1184 NewUnicodeStringTable
[NumberOfEntries
+ 1].UnicodeString
= NULL
;
1187 // Free the old Unicode String Table
1189 if (*UnicodeStringTable
!= NULL
) {
1190 gBS
->FreePool (*UnicodeStringTable
);
1194 // Point UnicodeStringTable at the newly allocated Unicode String Table
1196 *UnicodeStringTable
= NewUnicodeStringTable
;
1202 This function frees the table of Unicode strings in UnicodeStringTable.
1204 If UnicodeStringTable is NULL, then EFI_SUCCESS is returned.
1205 Otherwise, each language code, and each Unicode string in the Unicode string
1206 table are freed, and EFI_SUCCESS is returned.
1208 @param UnicodeStringTable A pointer to the table of Unicode strings.
1210 @retval EFI_SUCCESS The Unicode string table was freed.
1215 FreeUnicodeStringTable (
1216 IN EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
1222 // If the Unicode String Table is NULL, then it is already freed
1224 if (UnicodeStringTable
== NULL
) {
1229 // Loop through the Unicode String Table until we reach the end of table marker
1231 for (Index
= 0; UnicodeStringTable
[Index
].Language
!= NULL
; Index
++) {
1234 // Free the Language string from the Unicode String Table
1236 gBS
->FreePool (UnicodeStringTable
[Index
].Language
);
1239 // Free the Unicode String from the Unicode String Table
1241 if (UnicodeStringTable
[Index
].UnicodeString
!= NULL
) {
1242 gBS
->FreePool (UnicodeStringTable
[Index
].UnicodeString
);
1247 // Free the Unicode String Table itself
1249 gBS
->FreePool (UnicodeStringTable
);
1255 Returns a pointer to an allocated buffer that contains the contents of a
1256 variable retrieved through the UEFI Runtime Service GetVariable(). The
1257 returned buffer is allocated using AllocatePool(). The caller is responsible
1258 for freeing this buffer with FreePool().
1260 If Name is NULL, then ASSERT().
1261 If Guid is NULL, then ASSERT().
1263 @param[in] Name Pointer to a Null-terminated Unicode string.
1264 @param[in] Guid Pointer to an EFI_GUID structure
1266 @retval NULL The variable could not be retrieved.
1267 @retval NULL There are not enough resources available for the variable contents.
1268 @retval Other A pointer to allocated buffer containing the variable contents.
1274 IN CONST CHAR16
*Name
,
1275 IN CONST EFI_GUID
*Guid
1282 ASSERT (Name
!= NULL
);
1283 ASSERT (Guid
!= NULL
);
1286 // Try to get the variable size.
1290 Status
= gRT
->GetVariable ((CHAR16
*) Name
, (EFI_GUID
*) Guid
, NULL
, &Size
, Value
);
1291 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
1296 // Allocate buffer to get the variable.
1298 Value
= AllocatePool (Size
);
1299 if (Value
== NULL
) {
1304 // Get the variable data.
1306 Status
= gRT
->GetVariable ((CHAR16
*) Name
, (EFI_GUID
*) Guid
, NULL
, &Size
, Value
);
1307 if (EFI_ERROR (Status
)) {
1317 Returns a pointer to an allocated buffer that contains the contents of a
1318 variable retrieved through the UEFI Runtime Service GetVariable(). This
1319 function always uses the EFI_GLOBAL_VARIABLE GUID to retrieve variables.
1320 The returned buffer is allocated using AllocatePool(). The caller is
1321 responsible for freeing this buffer with FreePool().
1323 If Name is NULL, then ASSERT().
1325 @param[in] Name Pointer to a Null-terminated Unicode string.
1327 @retval NULL The variable could not be retrieved.
1328 @retval NULL There are not enough resources available for the variable contents.
1329 @retval Other A pointer to allocated buffer containing the variable contents.
1334 GetEfiGlobalVariable (
1335 IN CONST CHAR16
*Name
1338 return GetVariable (Name
, &gEfiGlobalVariableGuid
);
1343 Returns a pointer to an allocated buffer that contains the best matching language
1344 from a set of supported languages.
1346 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1347 code types may not be mixed in a single call to this function. The language
1348 code returned is allocated using AllocatePool(). The caller is responsible for
1349 freeing the allocated buffer using FreePool(). This function supports a variable
1350 argument list that allows the caller to pass in a prioritized list of language
1351 codes to test against all the language codes in SupportedLanguages.
1353 If SupportedLanguages is NULL, then ASSERT().
1355 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1356 contains a set of language codes in the format
1357 specified by Iso639Language.
1358 @param[in] Iso639Language If TRUE, then all language codes are assumed to be
1359 in ISO 639-2 format. If FALSE, then all language
1360 codes are assumed to be in RFC 4646 language format
1361 @param[in] ... A variable argument list that contains pointers to
1362 Null-terminated ASCII strings that contain one or more
1363 language codes in the format specified by Iso639Language.
1364 The first language code from each of these language
1365 code lists is used to determine if it is an exact or
1366 close match to any of the language codes in
1367 SupportedLanguages. Close matches only apply to RFC 4646
1368 language codes, and the matching algorithm from RFC 4647
1369 is used to determine if a close match is present. If
1370 an exact or close match is found, then the matching
1371 language code from SupportedLanguages is returned. If
1372 no matches are found, then the next variable argument
1373 parameter is evaluated. The variable argument list
1374 is terminated by a NULL.
1376 @retval NULL The best matching language could not be found in SupportedLanguages.
1377 @retval NULL There are not enough resources available to return the best matching
1379 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1380 language in SupportedLanguages.
1386 IN CONST CHAR8
*SupportedLanguages
,
1387 IN BOOLEAN Iso639Language
,
1393 UINTN CompareLength
;
1394 UINTN LanguageLength
;
1395 CONST CHAR8
*Supported
;
1396 CHAR8
*BestLanguage
;
1398 ASSERT (SupportedLanguages
!= NULL
);
1400 VA_START (Args
, Iso639Language
);
1401 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
1403 // Default to ISO 639-2 mode
1406 LanguageLength
= MIN (3, AsciiStrLen (Language
));
1409 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1411 if (!Iso639Language
) {
1412 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
1416 // Trim back the length of Language used until it is empty
1418 while (LanguageLength
> 0) {
1420 // Loop through all language codes in SupportedLanguages
1422 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
1424 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1426 if (!Iso639Language
) {
1428 // Skip ';' characters in Supported
1430 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1432 // Determine the length of the next language code in Supported
1434 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
1436 // If Language is longer than the Supported, then skip to the next language
1438 if (LanguageLength
> CompareLength
) {
1443 // See if the first LanguageLength characters in Supported match Language
1445 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
1448 // Allocate, copy, and return the best matching language code from SupportedLanguages
1450 BestLanguage
= AllocateZeroPool (CompareLength
+ 1);
1451 if (BestLanguage
== NULL
) {
1454 return CopyMem (BestLanguage
, Supported
, CompareLength
);
1458 if (Iso639Language
) {
1460 // If ISO 639 mode, then each language can only be tested once
1465 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1467 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
1474 // No matches were found