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 - 2010, 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 Returns the current TPL.
310 This function returns the current TPL. There is no EFI service to directly
311 retrieve the current TPL. Instead, the RaiseTPL() function is used to raise
312 the TPL to TPL_HIGH_LEVEL. This will return the current TPL. The TPL level
313 can then immediately be restored back to the current TPL level with a call
316 @return The current TPL.
327 Tpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
328 gBS
->RestoreTPL (Tpl
);
335 Initializes a basic mutual exclusion lock.
337 This function initializes a basic mutual exclusion lock to the released state
338 and returns the lock. Each lock provides mutual exclusion access at its task
339 priority level. Since there is no preemption or multiprocessor support in EFI,
340 acquiring the lock only consists of raising to the locks TPL.
341 If Lock is NULL, then ASSERT().
342 If Priority is not a valid TPL value, then ASSERT().
344 @param Lock A pointer to the lock data structure to initialize.
345 @param Priority EFI TPL is associated with the lock.
353 IN OUT EFI_LOCK
*Lock
,
357 ASSERT (Lock
!= NULL
);
358 ASSERT (Priority
<= TPL_HIGH_LEVEL
);
360 Lock
->Tpl
= Priority
;
361 Lock
->OwnerTpl
= TPL_APPLICATION
;
362 Lock
->Lock
= EfiLockReleased
;
367 Acquires ownership of a lock.
369 This function raises the system's current task priority level to the task
370 priority level of the mutual exclusion lock. Then, it places the lock in the
372 If Lock is NULL, then ASSERT().
373 If Lock is not initialized, then ASSERT().
374 If Lock is already in the acquired state, then ASSERT().
376 @param Lock A pointer to the lock to acquire.
385 ASSERT (Lock
!= NULL
);
386 ASSERT (Lock
->Lock
== EfiLockReleased
);
388 Lock
->OwnerTpl
= gBS
->RaiseTPL (Lock
->Tpl
);
389 Lock
->Lock
= EfiLockAcquired
;
393 Acquires ownership of a lock.
395 This function raises the system's current task priority level to the task priority
396 level of the mutual exclusion lock. Then, it attempts to place the lock in the acquired state.
397 If the lock is already in the acquired state, then EFI_ACCESS_DENIED is returned.
398 Otherwise, EFI_SUCCESS is returned.
399 If Lock is NULL, then ASSERT().
400 If Lock is not initialized, then ASSERT().
402 @param Lock A pointer to the lock to acquire.
404 @retval EFI_SUCCESS The lock was acquired.
405 @retval EFI_ACCESS_DENIED The lock could not be acquired because it is already owned.
410 EfiAcquireLockOrFail (
415 ASSERT (Lock
!= NULL
);
416 ASSERT (Lock
->Lock
!= EfiLockUninitialized
);
418 if (Lock
->Lock
== EfiLockAcquired
) {
420 // Lock is already owned, so bail out
422 return EFI_ACCESS_DENIED
;
425 Lock
->OwnerTpl
= gBS
->RaiseTPL (Lock
->Tpl
);
427 Lock
->Lock
= EfiLockAcquired
;
433 Releases ownership of a lock.
435 This function transitions a mutual exclusion lock from the acquired state to
436 the released state, and restores the system's task priority level to its
438 If Lock is NULL, then ASSERT().
439 If Lock is not initialized, then ASSERT().
440 If Lock is already in the released state, then ASSERT().
442 @param Lock A pointer to the lock to release.
453 ASSERT (Lock
!= NULL
);
454 ASSERT (Lock
->Lock
== EfiLockAcquired
);
456 Tpl
= Lock
->OwnerTpl
;
458 Lock
->Lock
= EfiLockReleased
;
460 gBS
->RestoreTPL (Tpl
);
464 Tests whether a controller handle is being managed by a specific driver.
466 This function tests whether the driver specified by DriverBindingHandle is
467 currently managing the controller specified by ControllerHandle. This test
468 is performed by evaluating if the the protocol specified by ProtocolGuid is
469 present on ControllerHandle and is was opened by DriverBindingHandle with an
470 attribute of EFI_OPEN_PROTOCOL_BY_DRIVER.
471 If ProtocolGuid is NULL, then ASSERT().
473 @param ControllerHandle A handle for a controller to test.
474 @param DriverBindingHandle Specifies the driver binding handle for the
476 @param ProtocolGuid Specifies the protocol that the driver specified
477 by DriverBindingHandle opens in its Start()
480 @retval EFI_SUCCESS ControllerHandle is managed by the driver
481 specified by DriverBindingHandle.
482 @retval EFI_UNSUPPORTED ControllerHandle is not managed by the driver
483 specified by DriverBindingHandle.
488 EfiTestManagedDevice (
489 IN CONST EFI_HANDLE ControllerHandle
,
490 IN CONST EFI_HANDLE DriverBindingHandle
,
491 IN CONST EFI_GUID
*ProtocolGuid
495 VOID
*ManagedInterface
;
497 ASSERT (ProtocolGuid
!= NULL
);
499 Status
= gBS
->OpenProtocol (
501 (EFI_GUID
*) ProtocolGuid
,
505 EFI_OPEN_PROTOCOL_BY_DRIVER
507 if (!EFI_ERROR (Status
)) {
510 (EFI_GUID
*) ProtocolGuid
,
514 return EFI_UNSUPPORTED
;
517 if (Status
!= EFI_ALREADY_STARTED
) {
518 return EFI_UNSUPPORTED
;
525 Tests whether a child handle is a child device of the controller.
527 This function tests whether ChildHandle is one of the children of
528 ControllerHandle. This test is performed by checking to see if the protocol
529 specified by ProtocolGuid is present on ControllerHandle and opened by
530 ChildHandle with an attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
531 If ProtocolGuid is NULL, then ASSERT().
533 @param ControllerHandle A handle for a (parent) controller to test.
534 @param ChildHandle A child handle to test.
535 @param ProtocolGuid Supplies the protocol that the child controller
536 opens on its parent controller.
538 @retval EFI_SUCCESS ChildHandle is a child of the ControllerHandle.
539 @retval EFI_UNSUPPORTED ChildHandle is not a child of the
546 IN CONST EFI_HANDLE ControllerHandle
,
547 IN CONST EFI_HANDLE ChildHandle
,
548 IN CONST EFI_GUID
*ProtocolGuid
552 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfoBuffer
;
556 ASSERT (ProtocolGuid
!= NULL
);
559 // Retrieve the list of agents that are consuming the specific protocol
560 // on ControllerHandle.
562 Status
= gBS
->OpenProtocolInformation (
564 (EFI_GUID
*) ProtocolGuid
,
568 if (EFI_ERROR (Status
)) {
569 return EFI_UNSUPPORTED
;
573 // Inspect if ChildHandle is one of the agents.
575 Status
= EFI_UNSUPPORTED
;
576 for (Index
= 0; Index
< EntryCount
; Index
++) {
577 if ((OpenInfoBuffer
[Index
].ControllerHandle
== ChildHandle
) &&
578 (OpenInfoBuffer
[Index
].Attributes
& EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
) != 0) {
579 Status
= EFI_SUCCESS
;
584 FreePool (OpenInfoBuffer
);
589 This function looks up a Unicode string in UnicodeStringTable.
591 If Language is a member of SupportedLanguages and a Unicode string is found in
592 UnicodeStringTable that matches the language code specified by Language, then it
593 is returned in UnicodeString.
595 @param Language A pointer to the ISO 639-2 language code for the
596 Unicode string to look up and return.
597 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes
598 that the Unicode string table supports. Language
599 must be a member of this set.
600 @param UnicodeStringTable A pointer to the table of Unicode strings.
601 @param UnicodeString A pointer to the Unicode string from UnicodeStringTable
602 that matches the language specified by Language.
604 @retval EFI_SUCCESS The Unicode string that matches the language
605 specified by Language was found
606 in the table of Unicode strings UnicodeStringTable,
607 and it was returned in UnicodeString.
608 @retval EFI_INVALID_PARAMETER Language is NULL.
609 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
610 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
611 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.
612 @retval EFI_UNSUPPORTED The language specified by Language is not a
613 member of SupportedLanguages.
614 @retval EFI_UNSUPPORTED The language specified by Language is not
615 supported by UnicodeStringTable.
620 LookupUnicodeString (
621 IN CONST CHAR8
*Language
,
622 IN CONST CHAR8
*SupportedLanguages
,
623 IN CONST EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
,
624 OUT CHAR16
**UnicodeString
628 // Make sure the parameters are valid
630 if (Language
== NULL
|| UnicodeString
== NULL
) {
631 return EFI_INVALID_PARAMETER
;
635 // If there are no supported languages, or the Unicode String Table is empty, then the
636 // Unicode String specified by Language is not supported by this Unicode String Table
638 if (SupportedLanguages
== NULL
|| UnicodeStringTable
== NULL
) {
639 return EFI_UNSUPPORTED
;
643 // Make sure Language is in the set of Supported Languages
645 while (*SupportedLanguages
!= 0) {
646 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
649 // Search the Unicode String Table for the matching Language specifier
651 while (UnicodeStringTable
->Language
!= NULL
) {
652 if (CompareIso639LanguageCode (Language
, UnicodeStringTable
->Language
)) {
655 // A matching string was found, so return it
657 *UnicodeString
= UnicodeStringTable
->UnicodeString
;
661 UnicodeStringTable
++;
664 return EFI_UNSUPPORTED
;
667 SupportedLanguages
+= 3;
670 return EFI_UNSUPPORTED
;
676 This function looks up a Unicode string in UnicodeStringTable.
678 If Language is a member of SupportedLanguages and a Unicode string is found in
679 UnicodeStringTable that matches the language code specified by Language, then
680 it is returned in UnicodeString.
682 @param Language A pointer to an ASCII string containing the ISO 639-2 or the
683 RFC 4646 language code for the Unicode string to look up and
684 return. If Iso639Language is TRUE, then this ASCII string is
685 not assumed to be Null-terminated, and only the first three
686 characters are used. If Iso639Language is FALSE, then this ASCII
687 string must be Null-terminated.
688 @param SupportedLanguages A pointer to a Null-terminated ASCII string that contains a
689 set of ISO 639-2 or RFC 4646 language codes that the Unicode
690 string table supports. Language must be a member of this set.
691 If Iso639Language is TRUE, then this string contains one or more
692 ISO 639-2 language codes with no separator characters. If Iso639Language
693 is FALSE, then is string contains one or more RFC 4646 language
694 codes separated by ';'.
695 @param UnicodeStringTable A pointer to the table of Unicode strings. Type EFI_UNICODE_STRING_TABLE
696 is defined in "Related Definitions".
697 @param UnicodeString A pointer to the Null-terminated Unicode string from UnicodeStringTable
698 that matches the language specified by Language.
699 @param Iso639Language Specifies the supported language code format. If it is TRUE, then
700 Language and SupportedLanguages follow ISO 639-2 language code format.
701 Otherwise, they follow RFC 4646 language code format.
704 @retval EFI_SUCCESS The Unicode string that matches the language specified by Language
705 was found in the table of Unicode strings UnicodeStringTable, and
706 it was returned in UnicodeString.
707 @retval EFI_INVALID_PARAMETER Language is NULL.
708 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
709 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
710 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.
711 @retval EFI_UNSUPPORTED The language specified by Language is not a member of SupportedLanguages.
712 @retval EFI_UNSUPPORTED The language specified by Language is not supported by UnicodeStringTable.
717 LookupUnicodeString2 (
718 IN CONST CHAR8
*Language
,
719 IN CONST CHAR8
*SupportedLanguages
,
720 IN CONST EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
,
721 OUT CHAR16
**UnicodeString
,
722 IN BOOLEAN Iso639Language
727 CHAR8
*LanguageString
;
730 // Make sure the parameters are valid
732 if (Language
== NULL
|| UnicodeString
== NULL
) {
733 return EFI_INVALID_PARAMETER
;
737 // If there are no supported languages, or the Unicode String Table is empty, then the
738 // Unicode String specified by Language is not supported by this Unicode String Table
740 if (SupportedLanguages
== NULL
|| UnicodeStringTable
== NULL
) {
741 return EFI_UNSUPPORTED
;
745 // Make sure Language is in the set of Supported Languages
748 while (*SupportedLanguages
!= 0) {
749 if (Iso639Language
) {
750 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
754 SupportedLanguages
+= 3;
756 for (Index
= 0; SupportedLanguages
[Index
] != 0 && SupportedLanguages
[Index
] != ';'; Index
++);
757 if ((AsciiStrnCmp(SupportedLanguages
, Language
, Index
) == 0) && (Language
[Index
] == 0)) {
761 SupportedLanguages
+= Index
;
762 for (; *SupportedLanguages
!= 0 && *SupportedLanguages
== ';'; SupportedLanguages
++);
767 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED
770 return EFI_UNSUPPORTED
;
774 // Search the Unicode String Table for the matching Language specifier
776 while (UnicodeStringTable
->Language
!= NULL
) {
777 LanguageString
= UnicodeStringTable
->Language
;
778 while (0 != *LanguageString
) {
779 for (Index
= 0 ;LanguageString
[Index
] != 0 && LanguageString
[Index
] != ';'; Index
++);
780 if (AsciiStrnCmp(LanguageString
, Language
, Index
) == 0) {
781 *UnicodeString
= UnicodeStringTable
->UnicodeString
;
784 LanguageString
+= Index
;
785 for (Index
= 0 ;LanguageString
[Index
] != 0 && LanguageString
[Index
] == ';'; Index
++);
787 UnicodeStringTable
++;
790 return EFI_UNSUPPORTED
;
795 This function adds a Unicode string to UnicodeStringTable.
797 If Language is a member of SupportedLanguages then UnicodeString is added to
798 UnicodeStringTable. New buffers are allocated for both Language and
799 UnicodeString. The contents of Language and UnicodeString are copied into
800 these new buffers. These buffers are automatically freed when
801 FreeUnicodeStringTable() is called.
803 @param Language A pointer to the ISO 639-2 language code for the Unicode
805 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes
806 that the Unicode string table supports.
807 Language must be a member of this set.
808 @param UnicodeStringTable A pointer to the table of Unicode strings.
809 @param UnicodeString A pointer to the Unicode string to add.
811 @retval EFI_SUCCESS The Unicode string that matches the language
812 specified by Language was found in the table of
813 Unicode strings UnicodeStringTable, and it was
814 returned in UnicodeString.
815 @retval EFI_INVALID_PARAMETER Language is NULL.
816 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
817 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.
818 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
819 @retval EFI_ALREADY_STARTED A Unicode string with language Language is
820 already present in UnicodeStringTable.
821 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another
822 Unicode string to UnicodeStringTable.
823 @retval EFI_UNSUPPORTED The language specified by Language is not a
824 member of SupportedLanguages.
830 IN CONST CHAR8
*Language
,
831 IN CONST CHAR8
*SupportedLanguages
,
832 IN EFI_UNICODE_STRING_TABLE
**UnicodeStringTable
,
833 IN CONST CHAR16
*UnicodeString
836 UINTN NumberOfEntries
;
837 EFI_UNICODE_STRING_TABLE
*OldUnicodeStringTable
;
838 EFI_UNICODE_STRING_TABLE
*NewUnicodeStringTable
;
839 UINTN UnicodeStringLength
;
842 // Make sure the parameter are valid
844 if (Language
== NULL
|| UnicodeString
== NULL
|| UnicodeStringTable
== NULL
) {
845 return EFI_INVALID_PARAMETER
;
849 // If there are no supported languages, then a Unicode String can not be added
851 if (SupportedLanguages
== NULL
) {
852 return EFI_UNSUPPORTED
;
856 // If the Unicode String is empty, then a Unicode String can not be added
858 if (UnicodeString
[0] == 0) {
859 return EFI_INVALID_PARAMETER
;
863 // Make sure Language is a member of SupportedLanguages
865 while (*SupportedLanguages
!= 0) {
866 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
869 // Determine the size of the Unicode String Table by looking for a NULL Language entry
872 if (*UnicodeStringTable
!= NULL
) {
873 OldUnicodeStringTable
= *UnicodeStringTable
;
874 while (OldUnicodeStringTable
->Language
!= NULL
) {
875 if (CompareIso639LanguageCode (Language
, OldUnicodeStringTable
->Language
)) {
876 return EFI_ALREADY_STARTED
;
879 OldUnicodeStringTable
++;
885 // Allocate space for a new Unicode String Table. It must hold the current number of
886 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table
889 NewUnicodeStringTable
= AllocatePool ((NumberOfEntries
+ 2) * sizeof (EFI_UNICODE_STRING_TABLE
));
890 if (NewUnicodeStringTable
== NULL
) {
891 return EFI_OUT_OF_RESOURCES
;
895 // If the current Unicode String Table contains any entries, then copy them to the
896 // newly allocated Unicode String Table.
898 if (*UnicodeStringTable
!= NULL
) {
900 NewUnicodeStringTable
,
902 NumberOfEntries
* sizeof (EFI_UNICODE_STRING_TABLE
)
907 // Allocate space for a copy of the Language specifier
909 NewUnicodeStringTable
[NumberOfEntries
].Language
= AllocateCopyPool (3, Language
);
910 if (NewUnicodeStringTable
[NumberOfEntries
].Language
== NULL
) {
911 FreePool (NewUnicodeStringTable
);
912 return EFI_OUT_OF_RESOURCES
;
916 // Compute the length of the Unicode String
918 for (UnicodeStringLength
= 0; UnicodeString
[UnicodeStringLength
] != 0; UnicodeStringLength
++)
922 // Allocate space for a copy of the Unicode String
924 NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
= AllocateCopyPool (
925 (UnicodeStringLength
+ 1) * sizeof (CHAR16
),
928 if (NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
== NULL
) {
929 FreePool (NewUnicodeStringTable
[NumberOfEntries
].Language
);
930 FreePool (NewUnicodeStringTable
);
931 return EFI_OUT_OF_RESOURCES
;
935 // Mark the end of the Unicode String Table
937 NewUnicodeStringTable
[NumberOfEntries
+ 1].Language
= NULL
;
938 NewUnicodeStringTable
[NumberOfEntries
+ 1].UnicodeString
= NULL
;
941 // Free the old Unicode String Table
943 if (*UnicodeStringTable
!= NULL
) {
944 FreePool (*UnicodeStringTable
);
948 // Point UnicodeStringTable at the newly allocated Unicode String Table
950 *UnicodeStringTable
= NewUnicodeStringTable
;
955 SupportedLanguages
+= 3;
958 return EFI_UNSUPPORTED
;
963 This function adds the Null-terminated Unicode string specified by UnicodeString
964 to UnicodeStringTable.
966 If Language is a member of SupportedLanguages then UnicodeString is added to
967 UnicodeStringTable. New buffers are allocated for both Language and UnicodeString.
968 The contents of Language and UnicodeString are copied into these new buffers.
969 These buffers are automatically freed when EfiLibFreeUnicodeStringTable() is called.
971 @param Language A pointer to an ASCII string containing the ISO 639-2 or
972 the RFC 4646 language code for the Unicode string to add.
973 If Iso639Language is TRUE, then this ASCII string is not
974 assumed to be Null-terminated, and only the first three
975 chacters are used. If Iso639Language is FALSE, then this
976 ASCII string must be Null-terminated.
977 @param SupportedLanguages A pointer to a Null-terminated ASCII string that contains
978 a set of ISO 639-2 or RFC 4646 language codes that the Unicode
979 string table supports. Language must be a member of this set.
980 If Iso639Language is TRUE, then this string contains one or more
981 ISO 639-2 language codes with no separator characters.
982 If Iso639Language is FALSE, then is string contains one or more
983 RFC 4646 language codes separated by ';'.
984 @param UnicodeStringTable A pointer to the table of Unicode strings. Type EFI_UNICODE_STRING_TABLE
985 is defined in "Related Definitions".
986 @param UnicodeString A pointer to the Unicode string to add.
987 @param Iso639Language Specifies the supported language code format. If it is TRUE,
988 then Language and SupportedLanguages follow ISO 639-2 language code format.
989 Otherwise, they follow RFC 4646 language code format.
991 @retval EFI_SUCCESS The Unicode string that matches the language specified by
992 Language was found in the table of Unicode strings UnicodeStringTable,
993 and it was returned in UnicodeString.
994 @retval EFI_INVALID_PARAMETER Language is NULL.
995 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
996 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.
997 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
998 @retval EFI_ALREADY_STARTED A Unicode string with language Language is already present in
1000 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another Unicode string UnicodeStringTable.
1001 @retval EFI_UNSUPPORTED The language specified by Language is not a member of SupportedLanguages.
1007 IN CONST CHAR8
*Language
,
1008 IN CONST CHAR8
*SupportedLanguages
,
1009 IN EFI_UNICODE_STRING_TABLE
**UnicodeStringTable
,
1010 IN CONST CHAR16
*UnicodeString
,
1011 IN BOOLEAN Iso639Language
1014 UINTN NumberOfEntries
;
1015 EFI_UNICODE_STRING_TABLE
*OldUnicodeStringTable
;
1016 EFI_UNICODE_STRING_TABLE
*NewUnicodeStringTable
;
1017 UINTN UnicodeStringLength
;
1020 CHAR8
*LanguageString
;
1023 // Make sure the parameter are valid
1025 if (Language
== NULL
|| UnicodeString
== NULL
|| UnicodeStringTable
== NULL
) {
1026 return EFI_INVALID_PARAMETER
;
1030 // If there are no supported languages, then a Unicode String can not be added
1032 if (SupportedLanguages
== NULL
) {
1033 return EFI_UNSUPPORTED
;
1037 // If the Unicode String is empty, then a Unicode String can not be added
1039 if (UnicodeString
[0] == 0) {
1040 return EFI_INVALID_PARAMETER
;
1044 // Make sure Language is a member of SupportedLanguages
1047 while (*SupportedLanguages
!= 0) {
1048 if (Iso639Language
) {
1049 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
1053 SupportedLanguages
+= 3;
1055 for (Index
= 0; SupportedLanguages
[Index
] != 0 && SupportedLanguages
[Index
] != ';'; Index
++);
1056 if (AsciiStrnCmp(SupportedLanguages
, Language
, Index
) == 0) {
1060 SupportedLanguages
+= Index
;
1061 for (; *SupportedLanguages
!= 0 && *SupportedLanguages
== ';'; SupportedLanguages
++);
1066 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED
1069 return EFI_UNSUPPORTED
;
1073 // Determine the size of the Unicode String Table by looking for a NULL Language entry
1075 NumberOfEntries
= 0;
1076 if (*UnicodeStringTable
!= NULL
) {
1077 OldUnicodeStringTable
= *UnicodeStringTable
;
1078 while (OldUnicodeStringTable
->Language
!= NULL
) {
1079 LanguageString
= OldUnicodeStringTable
->Language
;
1081 while (*LanguageString
!= 0) {
1082 for (Index
= 0; LanguageString
[Index
] != 0 && LanguageString
[Index
] != ';'; Index
++);
1084 if (AsciiStrnCmp (Language
, LanguageString
, Index
) == 0) {
1085 return EFI_ALREADY_STARTED
;
1087 LanguageString
+= Index
;
1088 for (; *LanguageString
!= 0 && *LanguageString
== ';'; LanguageString
++);
1090 OldUnicodeStringTable
++;
1096 // Allocate space for a new Unicode String Table. It must hold the current number of
1097 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table
1100 NewUnicodeStringTable
= AllocatePool ((NumberOfEntries
+ 2) * sizeof (EFI_UNICODE_STRING_TABLE
));
1101 if (NewUnicodeStringTable
== NULL
) {
1102 return EFI_OUT_OF_RESOURCES
;
1106 // If the current Unicode String Table contains any entries, then copy them to the
1107 // newly allocated Unicode String Table.
1109 if (*UnicodeStringTable
!= NULL
) {
1111 NewUnicodeStringTable
,
1112 *UnicodeStringTable
,
1113 NumberOfEntries
* sizeof (EFI_UNICODE_STRING_TABLE
)
1118 // Allocate space for a copy of the Language specifier
1120 NewUnicodeStringTable
[NumberOfEntries
].Language
= AllocateCopyPool (AsciiStrSize(Language
), Language
);
1121 if (NewUnicodeStringTable
[NumberOfEntries
].Language
== NULL
) {
1122 FreePool (NewUnicodeStringTable
);
1123 return EFI_OUT_OF_RESOURCES
;
1127 // Compute the length of the Unicode String
1129 for (UnicodeStringLength
= 0; UnicodeString
[UnicodeStringLength
] != 0; UnicodeStringLength
++);
1132 // Allocate space for a copy of the Unicode String
1134 NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
= AllocateCopyPool (StrSize (UnicodeString
), UnicodeString
);
1135 if (NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
== NULL
) {
1136 FreePool (NewUnicodeStringTable
[NumberOfEntries
].Language
);
1137 FreePool (NewUnicodeStringTable
);
1138 return EFI_OUT_OF_RESOURCES
;
1142 // Mark the end of the Unicode String Table
1144 NewUnicodeStringTable
[NumberOfEntries
+ 1].Language
= NULL
;
1145 NewUnicodeStringTable
[NumberOfEntries
+ 1].UnicodeString
= NULL
;
1148 // Free the old Unicode String Table
1150 if (*UnicodeStringTable
!= NULL
) {
1151 FreePool (*UnicodeStringTable
);
1155 // Point UnicodeStringTable at the newly allocated Unicode String Table
1157 *UnicodeStringTable
= NewUnicodeStringTable
;
1163 This function frees the table of Unicode strings in UnicodeStringTable.
1165 If UnicodeStringTable is NULL, then EFI_SUCCESS is returned.
1166 Otherwise, each language code, and each Unicode string in the Unicode string
1167 table are freed, and EFI_SUCCESS is returned.
1169 @param UnicodeStringTable A pointer to the table of Unicode strings.
1171 @retval EFI_SUCCESS The Unicode string table was freed.
1176 FreeUnicodeStringTable (
1177 IN EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
1183 // If the Unicode String Table is NULL, then it is already freed
1185 if (UnicodeStringTable
== NULL
) {
1190 // Loop through the Unicode String Table until we reach the end of table marker
1192 for (Index
= 0; UnicodeStringTable
[Index
].Language
!= NULL
; Index
++) {
1195 // Free the Language string from the Unicode String Table
1197 FreePool (UnicodeStringTable
[Index
].Language
);
1200 // Free the Unicode String from the Unicode String Table
1202 if (UnicodeStringTable
[Index
].UnicodeString
!= NULL
) {
1203 FreePool (UnicodeStringTable
[Index
].UnicodeString
);
1208 // Free the Unicode String Table itself
1210 FreePool (UnicodeStringTable
);
1216 Returns a pointer to an allocated buffer that contains the contents of a
1217 variable retrieved through the UEFI Runtime Service GetVariable(). The
1218 returned buffer is allocated using AllocatePool(). The caller is responsible
1219 for freeing this buffer with FreePool().
1221 If Name is NULL, then ASSERT().
1222 If Guid is NULL, then ASSERT().
1224 @param[in] Name The pointer to a Null-terminated Unicode string.
1225 @param[in] Guid The pointer to an EFI_GUID structure
1227 @retval NULL The variable could not be retrieved.
1228 @retval NULL There are not enough resources available for the variable contents.
1229 @retval Other A pointer to allocated buffer containing the variable contents.
1235 IN CONST CHAR16
*Name
,
1236 IN CONST EFI_GUID
*Guid
1243 ASSERT (Name
!= NULL
);
1244 ASSERT (Guid
!= NULL
);
1247 // Try to get the variable size.
1251 Status
= gRT
->GetVariable ((CHAR16
*) Name
, (EFI_GUID
*) Guid
, NULL
, &Size
, Value
);
1252 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
1257 // Allocate buffer to get the variable.
1259 Value
= AllocatePool (Size
);
1260 if (Value
== NULL
) {
1265 // Get the variable data.
1267 Status
= gRT
->GetVariable ((CHAR16
*) Name
, (EFI_GUID
*) Guid
, NULL
, &Size
, Value
);
1268 if (EFI_ERROR (Status
)) {
1278 Returns a pointer to an allocated buffer that contains the contents of a
1279 variable retrieved through the UEFI Runtime Service GetVariable(). This
1280 function always uses the EFI_GLOBAL_VARIABLE GUID to retrieve variables.
1281 The returned buffer is allocated using AllocatePool(). The caller is
1282 responsible for freeing this buffer with FreePool().
1284 If Name is NULL, then ASSERT().
1286 @param[in] Name The pointer to a Null-terminated Unicode string.
1288 @retval NULL The variable could not be retrieved.
1289 @retval NULL There are not enough resources available for the variable contents.
1290 @retval Other A pointer to allocated buffer containing the variable contents.
1295 GetEfiGlobalVariable (
1296 IN CONST CHAR16
*Name
1299 return GetVariable (Name
, &gEfiGlobalVariableGuid
);
1304 Returns a pointer to an allocated buffer that contains the best matching language
1305 from a set of supported languages.
1307 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1308 code types may not be mixed in a single call to this function. The language
1309 code returned is allocated using AllocatePool(). The caller is responsible for
1310 freeing the allocated buffer using FreePool(). This function supports a variable
1311 argument list that allows the caller to pass in a prioritized list of language
1312 codes to test against all the language codes in SupportedLanguages.
1314 If SupportedLanguages is NULL, then ASSERT().
1316 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1317 contains a set of language codes in the format
1318 specified by Iso639Language.
1319 @param[in] Iso639Language If TRUE, then all language codes are assumed to be
1320 in ISO 639-2 format. If FALSE, then all language
1321 codes are assumed to be in RFC 4646 language format
1322 @param[in] ... A variable argument list that contains pointers to
1323 Null-terminated ASCII strings that contain one or more
1324 language codes in the format specified by Iso639Language.
1325 The first language code from each of these language
1326 code lists is used to determine if it is an exact or
1327 close match to any of the language codes in
1328 SupportedLanguages. Close matches only apply to RFC 4646
1329 language codes, and the matching algorithm from RFC 4647
1330 is used to determine if a close match is present. If
1331 an exact or close match is found, then the matching
1332 language code from SupportedLanguages is returned. If
1333 no matches are found, then the next variable argument
1334 parameter is evaluated. The variable argument list
1335 is terminated by a NULL.
1337 @retval NULL The best matching language could not be found in SupportedLanguages.
1338 @retval NULL There are not enough resources available to return the best matching
1340 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1341 language in SupportedLanguages.
1347 IN CONST CHAR8
*SupportedLanguages
,
1348 IN BOOLEAN Iso639Language
,
1354 UINTN CompareLength
;
1355 UINTN LanguageLength
;
1356 CONST CHAR8
*Supported
;
1357 CHAR8
*BestLanguage
;
1359 ASSERT (SupportedLanguages
!= NULL
);
1361 VA_START (Args
, Iso639Language
);
1362 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
1364 // Default to ISO 639-2 mode
1367 LanguageLength
= MIN (3, AsciiStrLen (Language
));
1370 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1372 if (!Iso639Language
) {
1373 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
1377 // Trim back the length of Language used until it is empty
1379 while (LanguageLength
> 0) {
1381 // Loop through all language codes in SupportedLanguages
1383 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
1385 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1387 if (!Iso639Language
) {
1389 // Skip ';' characters in Supported
1391 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1393 // Determine the length of the next language code in Supported
1395 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
1397 // If Language is longer than the Supported, then skip to the next language
1399 if (LanguageLength
> CompareLength
) {
1404 // See if the first LanguageLength characters in Supported match Language
1406 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
1409 // Allocate, copy, and return the best matching language code from SupportedLanguages
1411 BestLanguage
= AllocateZeroPool (CompareLength
+ 1);
1412 if (BestLanguage
== NULL
) {
1415 return CopyMem (BestLanguage
, Supported
, CompareLength
);
1419 if (Iso639Language
) {
1421 // If ISO 639 mode, then each language can only be tested once
1426 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1428 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
1435 // No matches were found