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 - 2018, Intel Corporation. All rights reserved.<BR>
9 SPDX-License-Identifier: BSD-2-Clause-Patent
14 #include "UefiLibInternal.h"
17 Empty constructor function that is required to resolve dependencies between
22 @param ImageHandle The firmware allocated handle for the EFI image.
23 @param SystemTable A pointer to the EFI System Table.
25 @retval EFI_SUCCESS The constructor executed correctly.
31 IN EFI_HANDLE ImageHandle
,
32 IN EFI_SYSTEM_TABLE
*SystemTable
39 Compare whether two names of languages are identical.
41 @param Language1 Name of language 1.
42 @param Language2 Name of language 2.
44 @retval TRUE Language 1 and language 2 are the same.
45 @retval FALSE Language 1 and language 2 are not the same.
49 CompareIso639LanguageCode (
50 IN CONST CHAR8
*Language1
,
51 IN CONST CHAR8
*Language2
57 Name1
= ReadUnaligned24 ((CONST UINT32
*) Language1
);
58 Name2
= ReadUnaligned24 ((CONST UINT32
*) Language2
);
60 return (BOOLEAN
) (Name1
== Name2
);
64 Retrieves a pointer to the system configuration table from the EFI System Table
65 based on a specified GUID.
67 This function searches the list of configuration tables stored in the EFI System Table
68 for a table with a GUID that matches TableGuid. If a match is found, then a pointer to
69 the configuration table is returned in Table., and EFI_SUCCESS is returned. If a matching GUID
70 is not found, then EFI_NOT_FOUND is returned.
71 If TableGuid is NULL, then ASSERT().
72 If Table is NULL, then ASSERT().
74 @param TableGuid The pointer to table's GUID type.
75 @param Table The pointer to the table associated with TableGuid in the EFI System Table.
77 @retval EFI_SUCCESS A configuration table matching TableGuid was found.
78 @retval EFI_NOT_FOUND A configuration table matching TableGuid could not be found.
83 EfiGetSystemConfigurationTable (
84 IN EFI_GUID
*TableGuid
,
88 EFI_SYSTEM_TABLE
*SystemTable
;
91 ASSERT (TableGuid
!= NULL
);
92 ASSERT (Table
!= NULL
);
96 for (Index
= 0; Index
< SystemTable
->NumberOfTableEntries
; Index
++) {
97 if (CompareGuid (TableGuid
, &(SystemTable
->ConfigurationTable
[Index
].VendorGuid
))) {
98 *Table
= SystemTable
->ConfigurationTable
[Index
].VendorTable
;
103 return EFI_NOT_FOUND
;
107 Creates and returns a notification event and registers that event with all the protocol
108 instances specified by ProtocolGuid.
110 This function causes the notification function to be executed for every protocol of type
111 ProtocolGuid instance that exists in the system when this function is invoked. If there are
112 no instances of ProtocolGuid in the handle database at the time this function is invoked,
113 then the notification function is still executed one time. In addition, every time a protocol
114 of type ProtocolGuid instance is installed or reinstalled, the notification function is also
115 executed. This function returns the notification event that was created.
116 If ProtocolGuid is NULL, then ASSERT().
117 If NotifyTpl is not a legal TPL value, then ASSERT().
118 If NotifyFunction is NULL, then ASSERT().
119 If Registration is NULL, then ASSERT().
122 @param ProtocolGuid Supplies GUID of the protocol upon whose installation the event is fired.
123 @param NotifyTpl Supplies the task priority level of the event notifications.
124 @param NotifyFunction Supplies the function to notify when the event is signaled.
125 @param NotifyContext The context parameter to pass to NotifyFunction.
126 @param Registration A pointer to a memory location to receive the registration value.
127 This value is passed to LocateHandle() to obtain new handles that
128 have been added that support the ProtocolGuid-specified protocol.
130 @return The notification event that was created.
135 EfiCreateProtocolNotifyEvent(
136 IN EFI_GUID
*ProtocolGuid
,
137 IN EFI_TPL NotifyTpl
,
138 IN EFI_EVENT_NOTIFY NotifyFunction
,
139 IN VOID
*NotifyContext
, OPTIONAL
140 OUT VOID
**Registration
146 ASSERT (ProtocolGuid
!= NULL
);
147 ASSERT (NotifyFunction
!= NULL
);
148 ASSERT (Registration
!= NULL
);
154 Status
= gBS
->CreateEvent (
161 ASSERT_EFI_ERROR (Status
);
164 // Register for protocol notifications on this event
167 Status
= gBS
->RegisterProtocolNotify (
173 ASSERT_EFI_ERROR (Status
);
176 // Kick the event so we will perform an initial pass of
177 // current installed drivers
180 gBS
->SignalEvent (Event
);
185 Creates a named event that can be signaled with EfiNamedEventSignal().
187 This function creates an event using NotifyTpl, NoifyFunction, and NotifyContext.
188 This event is signaled with EfiNamedEventSignal(). This provides the ability for one or more
189 listeners on the same event named by the GUID specified by Name.
190 If Name is NULL, then ASSERT().
191 If NotifyTpl is not a legal TPL value, then ASSERT().
192 If NotifyFunction is NULL, then ASSERT().
194 @param Name Supplies the GUID name of the event.
195 @param NotifyTpl Supplies the task priority level of the event notifications.
196 @param NotifyFunction Supplies the function to notify when the event is signaled.
197 @param NotifyContext The context parameter to pass to NotifyFunction.
198 @param Registration A pointer to a memory location to receive the registration value.
200 @retval EFI_SUCCESS A named event was created.
201 @retval EFI_OUT_OF_RESOURCES There are not enough resource to create the named event.
206 EfiNamedEventListen (
207 IN CONST EFI_GUID
*Name
,
208 IN EFI_TPL NotifyTpl
,
209 IN EFI_EVENT_NOTIFY NotifyFunction
,
210 IN CONST VOID
*NotifyContext
, OPTIONAL
211 OUT VOID
*Registration OPTIONAL
216 VOID
*RegistrationLocal
;
218 ASSERT (Name
!= NULL
);
219 ASSERT (NotifyFunction
!= NULL
);
220 ASSERT (NotifyTpl
<= TPL_HIGH_LEVEL
);
225 Status
= gBS
->CreateEvent (
229 (VOID
*) NotifyContext
,
232 ASSERT_EFI_ERROR (Status
);
235 // The Registration is not optional to RegisterProtocolNotify().
236 // To make it optional to EfiNamedEventListen(), may need to substitute with a local.
238 if (Registration
!= NULL
) {
239 RegistrationLocal
= Registration
;
241 RegistrationLocal
= &RegistrationLocal
;
245 // Register for an installation of protocol interface
248 Status
= gBS
->RegisterProtocolNotify (
253 ASSERT_EFI_ERROR (Status
);
259 Signals a named event created with EfiNamedEventListen().
261 This function signals the named event specified by Name. The named event must have been
262 created with EfiNamedEventListen().
263 If Name is NULL, then ASSERT().
265 @param Name Supplies the GUID name of the event.
267 @retval EFI_SUCCESS A named event was signaled.
268 @retval EFI_OUT_OF_RESOURCES There are not enough resource to signal the named event.
273 EfiNamedEventSignal (
274 IN CONST EFI_GUID
*Name
280 ASSERT(Name
!= NULL
);
283 Status
= gBS
->InstallProtocolInterface (
286 EFI_NATIVE_INTERFACE
,
289 ASSERT_EFI_ERROR (Status
);
291 Status
= gBS
->UninstallProtocolInterface (
296 ASSERT_EFI_ERROR (Status
);
302 Signals an event group by placing a new event in the group temporarily and
305 @param[in] EventGroup Supplies the unique identifier of the event
308 @retval EFI_SUCCESS The event group was signaled successfully.
309 @retval EFI_INVALID_PARAMETER EventGroup is NULL.
310 @return Error codes that report problems about event
311 creation or signaling.
315 EfiEventGroupSignal (
316 IN CONST EFI_GUID
*EventGroup
322 if (EventGroup
== NULL
) {
323 return EFI_INVALID_PARAMETER
;
326 Status
= gBS
->CreateEventEx (
329 EfiEventEmptyFunction
,
334 if (EFI_ERROR (Status
)) {
338 Status
= gBS
->SignalEvent (Event
);
339 gBS
->CloseEvent (Event
);
345 An empty function that can be used as NotifyFunction parameter of
346 CreateEvent() or CreateEventEx().
348 @param Event Event whose notification function is being invoked.
349 @param Context The pointer to the notification function's context,
350 which is implementation-dependent.
355 EfiEventEmptyFunction (
363 Returns the current TPL.
365 This function returns the current TPL. There is no EFI service to directly
366 retrieve the current TPL. Instead, the RaiseTPL() function is used to raise
367 the TPL to TPL_HIGH_LEVEL. This will return the current TPL. The TPL level
368 can then immediately be restored back to the current TPL level with a call
371 @return The current TPL.
382 Tpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
383 gBS
->RestoreTPL (Tpl
);
390 Initializes a basic mutual exclusion lock.
392 This function initializes a basic mutual exclusion lock to the released state
393 and returns the lock. Each lock provides mutual exclusion access at its task
394 priority level. Since there is no preemption or multiprocessor support in EFI,
395 acquiring the lock only consists of raising to the locks TPL.
396 If Lock is NULL, then ASSERT().
397 If Priority is not a valid TPL value, then ASSERT().
399 @param Lock A pointer to the lock data structure to initialize.
400 @param Priority EFI TPL is associated with the lock.
408 IN OUT EFI_LOCK
*Lock
,
412 ASSERT (Lock
!= NULL
);
413 ASSERT (Priority
<= TPL_HIGH_LEVEL
);
415 Lock
->Tpl
= Priority
;
416 Lock
->OwnerTpl
= TPL_APPLICATION
;
417 Lock
->Lock
= EfiLockReleased
;
422 Acquires ownership of a lock.
424 This function raises the system's current task priority level to the task
425 priority level of the mutual exclusion lock. Then, it places the lock in the
427 If Lock is NULL, then ASSERT().
428 If Lock is not initialized, then ASSERT().
429 If Lock is already in the acquired state, then ASSERT().
431 @param Lock A pointer to the lock to acquire.
440 ASSERT (Lock
!= NULL
);
441 ASSERT (Lock
->Lock
== EfiLockReleased
);
443 Lock
->OwnerTpl
= gBS
->RaiseTPL (Lock
->Tpl
);
444 Lock
->Lock
= EfiLockAcquired
;
448 Acquires ownership of a lock.
450 This function raises the system's current task priority level to the task priority
451 level of the mutual exclusion lock. Then, it attempts to place the lock in the acquired state.
452 If the lock is already in the acquired state, then EFI_ACCESS_DENIED is returned.
453 Otherwise, EFI_SUCCESS is returned.
454 If Lock is NULL, then ASSERT().
455 If Lock is not initialized, then ASSERT().
457 @param Lock A pointer to the lock to acquire.
459 @retval EFI_SUCCESS The lock was acquired.
460 @retval EFI_ACCESS_DENIED The lock could not be acquired because it is already owned.
465 EfiAcquireLockOrFail (
470 ASSERT (Lock
!= NULL
);
471 ASSERT (Lock
->Lock
!= EfiLockUninitialized
);
473 if (Lock
->Lock
== EfiLockAcquired
) {
475 // Lock is already owned, so bail out
477 return EFI_ACCESS_DENIED
;
480 Lock
->OwnerTpl
= gBS
->RaiseTPL (Lock
->Tpl
);
482 Lock
->Lock
= EfiLockAcquired
;
488 Releases ownership of a lock.
490 This function transitions a mutual exclusion lock from the acquired state to
491 the released state, and restores the system's task priority level to its
493 If Lock is NULL, then ASSERT().
494 If Lock is not initialized, then ASSERT().
495 If Lock is already in the released state, then ASSERT().
497 @param Lock A pointer to the lock to release.
508 ASSERT (Lock
!= NULL
);
509 ASSERT (Lock
->Lock
== EfiLockAcquired
);
511 Tpl
= Lock
->OwnerTpl
;
513 Lock
->Lock
= EfiLockReleased
;
515 gBS
->RestoreTPL (Tpl
);
519 Tests whether a controller handle is being managed by a specific driver.
521 This function tests whether the driver specified by DriverBindingHandle is
522 currently managing the controller specified by ControllerHandle. This test
523 is performed by evaluating if the the protocol specified by ProtocolGuid is
524 present on ControllerHandle and is was opened by DriverBindingHandle with an
525 attribute of EFI_OPEN_PROTOCOL_BY_DRIVER.
526 If ProtocolGuid is NULL, then ASSERT().
528 @param ControllerHandle A handle for a controller to test.
529 @param DriverBindingHandle Specifies the driver binding handle for the
531 @param ProtocolGuid Specifies the protocol that the driver specified
532 by DriverBindingHandle opens in its Start()
535 @retval EFI_SUCCESS ControllerHandle is managed by the driver
536 specified by DriverBindingHandle.
537 @retval EFI_UNSUPPORTED ControllerHandle is not managed by the driver
538 specified by DriverBindingHandle.
543 EfiTestManagedDevice (
544 IN CONST EFI_HANDLE ControllerHandle
,
545 IN CONST EFI_HANDLE DriverBindingHandle
,
546 IN CONST EFI_GUID
*ProtocolGuid
550 VOID
*ManagedInterface
;
552 ASSERT (ProtocolGuid
!= NULL
);
554 Status
= gBS
->OpenProtocol (
556 (EFI_GUID
*) ProtocolGuid
,
560 EFI_OPEN_PROTOCOL_BY_DRIVER
562 if (!EFI_ERROR (Status
)) {
565 (EFI_GUID
*) ProtocolGuid
,
569 return EFI_UNSUPPORTED
;
572 if (Status
!= EFI_ALREADY_STARTED
) {
573 return EFI_UNSUPPORTED
;
580 Tests whether a child handle is a child device of the controller.
582 This function tests whether ChildHandle is one of the children of
583 ControllerHandle. This test is performed by checking to see if the protocol
584 specified by ProtocolGuid is present on ControllerHandle and opened by
585 ChildHandle with an attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
586 If ProtocolGuid is NULL, then ASSERT().
588 @param ControllerHandle A handle for a (parent) controller to test.
589 @param ChildHandle A child handle to test.
590 @param ProtocolGuid Supplies the protocol that the child controller
591 opens on its parent controller.
593 @retval EFI_SUCCESS ChildHandle is a child of the ControllerHandle.
594 @retval EFI_UNSUPPORTED ChildHandle is not a child of the
601 IN CONST EFI_HANDLE ControllerHandle
,
602 IN CONST EFI_HANDLE ChildHandle
,
603 IN CONST EFI_GUID
*ProtocolGuid
607 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfoBuffer
;
611 ASSERT (ProtocolGuid
!= NULL
);
614 // Retrieve the list of agents that are consuming the specific protocol
615 // on ControllerHandle.
617 Status
= gBS
->OpenProtocolInformation (
619 (EFI_GUID
*) ProtocolGuid
,
623 if (EFI_ERROR (Status
)) {
624 return EFI_UNSUPPORTED
;
628 // Inspect if ChildHandle is one of the agents.
630 Status
= EFI_UNSUPPORTED
;
631 for (Index
= 0; Index
< EntryCount
; Index
++) {
632 if ((OpenInfoBuffer
[Index
].ControllerHandle
== ChildHandle
) &&
633 (OpenInfoBuffer
[Index
].Attributes
& EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
) != 0) {
634 Status
= EFI_SUCCESS
;
639 FreePool (OpenInfoBuffer
);
644 This function looks up a Unicode string in UnicodeStringTable.
646 If Language is a member of SupportedLanguages and a Unicode string is found in
647 UnicodeStringTable that matches the language code specified by Language, then it
648 is returned in UnicodeString.
650 @param Language A pointer to the ISO 639-2 language code for the
651 Unicode string to look up and return.
652 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes
653 that the Unicode string table supports. Language
654 must be a member of this set.
655 @param UnicodeStringTable A pointer to the table of Unicode strings.
656 @param UnicodeString A pointer to the Unicode string from UnicodeStringTable
657 that matches the language specified by Language.
659 @retval EFI_SUCCESS The Unicode string that matches the language
660 specified by Language was found
661 in the table of Unicode strings UnicodeStringTable,
662 and it was returned in UnicodeString.
663 @retval EFI_INVALID_PARAMETER Language is NULL.
664 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
665 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
666 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.
667 @retval EFI_UNSUPPORTED The language specified by Language is not a
668 member of SupportedLanguages.
669 @retval EFI_UNSUPPORTED The language specified by Language is not
670 supported by UnicodeStringTable.
675 LookupUnicodeString (
676 IN CONST CHAR8
*Language
,
677 IN CONST CHAR8
*SupportedLanguages
,
678 IN CONST EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
,
679 OUT CHAR16
**UnicodeString
683 // Make sure the parameters are valid
685 if (Language
== NULL
|| UnicodeString
== NULL
) {
686 return EFI_INVALID_PARAMETER
;
690 // If there are no supported languages, or the Unicode String Table is empty, then the
691 // Unicode String specified by Language is not supported by this Unicode String Table
693 if (SupportedLanguages
== NULL
|| UnicodeStringTable
== NULL
) {
694 return EFI_UNSUPPORTED
;
698 // Make sure Language is in the set of Supported Languages
700 while (*SupportedLanguages
!= 0) {
701 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
704 // Search the Unicode String Table for the matching Language specifier
706 while (UnicodeStringTable
->Language
!= NULL
) {
707 if (CompareIso639LanguageCode (Language
, UnicodeStringTable
->Language
)) {
710 // A matching string was found, so return it
712 *UnicodeString
= UnicodeStringTable
->UnicodeString
;
716 UnicodeStringTable
++;
719 return EFI_UNSUPPORTED
;
722 SupportedLanguages
+= 3;
725 return EFI_UNSUPPORTED
;
731 This function looks up a Unicode string in UnicodeStringTable.
733 If Language is a member of SupportedLanguages and a Unicode string is found in
734 UnicodeStringTable that matches the language code specified by Language, then
735 it is returned in UnicodeString.
737 @param Language A pointer to an ASCII string containing the ISO 639-2 or the
738 RFC 4646 language code for the Unicode string to look up and
739 return. If Iso639Language is TRUE, then this ASCII string is
740 not assumed to be Null-terminated, and only the first three
741 characters are used. If Iso639Language is FALSE, then this ASCII
742 string must be Null-terminated.
743 @param SupportedLanguages A pointer to a Null-terminated ASCII string that contains a
744 set of ISO 639-2 or RFC 4646 language codes that the Unicode
745 string table supports. Language must be a member of this set.
746 If Iso639Language is TRUE, then this string contains one or more
747 ISO 639-2 language codes with no separator characters. If Iso639Language
748 is FALSE, then is string contains one or more RFC 4646 language
749 codes separated by ';'.
750 @param UnicodeStringTable A pointer to the table of Unicode strings. Type EFI_UNICODE_STRING_TABLE
751 is defined in "Related Definitions".
752 @param UnicodeString A pointer to the Null-terminated Unicode string from UnicodeStringTable
753 that matches the language specified by Language.
754 @param Iso639Language Specifies the supported language code format. If it is TRUE, then
755 Language and SupportedLanguages follow ISO 639-2 language code format.
756 Otherwise, they follow RFC 4646 language code format.
759 @retval EFI_SUCCESS The Unicode string that matches the language specified by Language
760 was found in the table of Unicode strings UnicodeStringTable, and
761 it was returned in UnicodeString.
762 @retval EFI_INVALID_PARAMETER Language is NULL.
763 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
764 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
765 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.
766 @retval EFI_UNSUPPORTED The language specified by Language is not a member of SupportedLanguages.
767 @retval EFI_UNSUPPORTED The language specified by Language is not supported by UnicodeStringTable.
772 LookupUnicodeString2 (
773 IN CONST CHAR8
*Language
,
774 IN CONST CHAR8
*SupportedLanguages
,
775 IN CONST EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
,
776 OUT CHAR16
**UnicodeString
,
777 IN BOOLEAN Iso639Language
782 CHAR8
*LanguageString
;
785 // Make sure the parameters are valid
787 if (Language
== NULL
|| UnicodeString
== NULL
) {
788 return EFI_INVALID_PARAMETER
;
792 // If there are no supported languages, or the Unicode String Table is empty, then the
793 // Unicode String specified by Language is not supported by this Unicode String Table
795 if (SupportedLanguages
== NULL
|| UnicodeStringTable
== NULL
) {
796 return EFI_UNSUPPORTED
;
800 // Make sure Language is in the set of Supported Languages
803 while (*SupportedLanguages
!= 0) {
804 if (Iso639Language
) {
805 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
809 SupportedLanguages
+= 3;
811 for (Index
= 0; SupportedLanguages
[Index
] != 0 && SupportedLanguages
[Index
] != ';'; Index
++);
812 if ((AsciiStrnCmp(SupportedLanguages
, Language
, Index
) == 0) && (Language
[Index
] == 0)) {
816 SupportedLanguages
+= Index
;
817 for (; *SupportedLanguages
!= 0 && *SupportedLanguages
== ';'; SupportedLanguages
++);
822 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED
825 return EFI_UNSUPPORTED
;
829 // Search the Unicode String Table for the matching Language specifier
831 while (UnicodeStringTable
->Language
!= NULL
) {
832 LanguageString
= UnicodeStringTable
->Language
;
833 while (0 != *LanguageString
) {
834 for (Index
= 0 ;LanguageString
[Index
] != 0 && LanguageString
[Index
] != ';'; Index
++);
835 if (AsciiStrnCmp(LanguageString
, Language
, Index
) == 0) {
836 *UnicodeString
= UnicodeStringTable
->UnicodeString
;
839 LanguageString
+= Index
;
840 for (Index
= 0 ;LanguageString
[Index
] != 0 && LanguageString
[Index
] == ';'; Index
++);
842 UnicodeStringTable
++;
845 return EFI_UNSUPPORTED
;
850 This function adds a Unicode string to UnicodeStringTable.
852 If Language is a member of SupportedLanguages then UnicodeString is added to
853 UnicodeStringTable. New buffers are allocated for both Language and
854 UnicodeString. The contents of Language and UnicodeString are copied into
855 these new buffers. These buffers are automatically freed when
856 FreeUnicodeStringTable() is called.
858 @param Language A pointer to the ISO 639-2 language code for the Unicode
860 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes
861 that the Unicode string table supports.
862 Language must be a member of this set.
863 @param UnicodeStringTable A pointer to the table of Unicode strings.
864 @param UnicodeString A pointer to the Unicode string to add.
866 @retval EFI_SUCCESS The Unicode string that matches the language
867 specified by Language was found in the table of
868 Unicode strings UnicodeStringTable, and it was
869 returned in UnicodeString.
870 @retval EFI_INVALID_PARAMETER Language is NULL.
871 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
872 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.
873 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
874 @retval EFI_ALREADY_STARTED A Unicode string with language Language is
875 already present in UnicodeStringTable.
876 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another
877 Unicode string to UnicodeStringTable.
878 @retval EFI_UNSUPPORTED The language specified by Language is not a
879 member of SupportedLanguages.
885 IN CONST CHAR8
*Language
,
886 IN CONST CHAR8
*SupportedLanguages
,
887 IN OUT EFI_UNICODE_STRING_TABLE
**UnicodeStringTable
,
888 IN CONST CHAR16
*UnicodeString
891 UINTN NumberOfEntries
;
892 EFI_UNICODE_STRING_TABLE
*OldUnicodeStringTable
;
893 EFI_UNICODE_STRING_TABLE
*NewUnicodeStringTable
;
894 UINTN UnicodeStringLength
;
897 // Make sure the parameter are valid
899 if (Language
== NULL
|| UnicodeString
== NULL
|| UnicodeStringTable
== NULL
) {
900 return EFI_INVALID_PARAMETER
;
904 // If there are no supported languages, then a Unicode String can not be added
906 if (SupportedLanguages
== NULL
) {
907 return EFI_UNSUPPORTED
;
911 // If the Unicode String is empty, then a Unicode String can not be added
913 if (UnicodeString
[0] == 0) {
914 return EFI_INVALID_PARAMETER
;
918 // Make sure Language is a member of SupportedLanguages
920 while (*SupportedLanguages
!= 0) {
921 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
924 // Determine the size of the Unicode String Table by looking for a NULL Language entry
927 if (*UnicodeStringTable
!= NULL
) {
928 OldUnicodeStringTable
= *UnicodeStringTable
;
929 while (OldUnicodeStringTable
->Language
!= NULL
) {
930 if (CompareIso639LanguageCode (Language
, OldUnicodeStringTable
->Language
)) {
931 return EFI_ALREADY_STARTED
;
934 OldUnicodeStringTable
++;
940 // Allocate space for a new Unicode String Table. It must hold the current number of
941 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table
944 NewUnicodeStringTable
= AllocatePool ((NumberOfEntries
+ 2) * sizeof (EFI_UNICODE_STRING_TABLE
));
945 if (NewUnicodeStringTable
== NULL
) {
946 return EFI_OUT_OF_RESOURCES
;
950 // If the current Unicode String Table contains any entries, then copy them to the
951 // newly allocated Unicode String Table.
953 if (*UnicodeStringTable
!= NULL
) {
955 NewUnicodeStringTable
,
957 NumberOfEntries
* sizeof (EFI_UNICODE_STRING_TABLE
)
962 // Allocate space for a copy of the Language specifier
964 NewUnicodeStringTable
[NumberOfEntries
].Language
= AllocateCopyPool (3, Language
);
965 if (NewUnicodeStringTable
[NumberOfEntries
].Language
== NULL
) {
966 FreePool (NewUnicodeStringTable
);
967 return EFI_OUT_OF_RESOURCES
;
971 // Compute the length of the Unicode String
973 for (UnicodeStringLength
= 0; UnicodeString
[UnicodeStringLength
] != 0; UnicodeStringLength
++)
977 // Allocate space for a copy of the Unicode String
979 NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
= AllocateCopyPool (
980 (UnicodeStringLength
+ 1) * sizeof (CHAR16
),
983 if (NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
== NULL
) {
984 FreePool (NewUnicodeStringTable
[NumberOfEntries
].Language
);
985 FreePool (NewUnicodeStringTable
);
986 return EFI_OUT_OF_RESOURCES
;
990 // Mark the end of the Unicode String Table
992 NewUnicodeStringTable
[NumberOfEntries
+ 1].Language
= NULL
;
993 NewUnicodeStringTable
[NumberOfEntries
+ 1].UnicodeString
= NULL
;
996 // Free the old Unicode String Table
998 if (*UnicodeStringTable
!= NULL
) {
999 FreePool (*UnicodeStringTable
);
1003 // Point UnicodeStringTable at the newly allocated Unicode String Table
1005 *UnicodeStringTable
= NewUnicodeStringTable
;
1010 SupportedLanguages
+= 3;
1013 return EFI_UNSUPPORTED
;
1018 This function adds the Null-terminated Unicode string specified by UnicodeString
1019 to UnicodeStringTable.
1021 If Language is a member of SupportedLanguages then UnicodeString is added to
1022 UnicodeStringTable. New buffers are allocated for both Language and UnicodeString.
1023 The contents of Language and UnicodeString are copied into these new buffers.
1024 These buffers are automatically freed when EfiLibFreeUnicodeStringTable() is called.
1026 @param Language A pointer to an ASCII string containing the ISO 639-2 or
1027 the RFC 4646 language code for the Unicode string to add.
1028 If Iso639Language is TRUE, then this ASCII string is not
1029 assumed to be Null-terminated, and only the first three
1030 chacters are used. If Iso639Language is FALSE, then this
1031 ASCII string must be Null-terminated.
1032 @param SupportedLanguages A pointer to a Null-terminated ASCII string that contains
1033 a set of ISO 639-2 or RFC 4646 language codes that the Unicode
1034 string table supports. Language must be a member of this set.
1035 If Iso639Language is TRUE, then this string contains one or more
1036 ISO 639-2 language codes with no separator characters.
1037 If Iso639Language is FALSE, then is string contains one or more
1038 RFC 4646 language codes separated by ';'.
1039 @param UnicodeStringTable A pointer to the table of Unicode strings. Type EFI_UNICODE_STRING_TABLE
1040 is defined in "Related Definitions".
1041 @param UnicodeString A pointer to the Unicode string to add.
1042 @param Iso639Language Specifies the supported language code format. If it is TRUE,
1043 then Language and SupportedLanguages follow ISO 639-2 language code format.
1044 Otherwise, they follow RFC 4646 language code format.
1046 @retval EFI_SUCCESS The Unicode string that matches the language specified by
1047 Language was found in the table of Unicode strings UnicodeStringTable,
1048 and it was returned in UnicodeString.
1049 @retval EFI_INVALID_PARAMETER Language is NULL.
1050 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
1051 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.
1052 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
1053 @retval EFI_ALREADY_STARTED A Unicode string with language Language is already present in
1055 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another Unicode string UnicodeStringTable.
1056 @retval EFI_UNSUPPORTED The language specified by Language is not a member of SupportedLanguages.
1062 IN CONST CHAR8
*Language
,
1063 IN CONST CHAR8
*SupportedLanguages
,
1064 IN OUT EFI_UNICODE_STRING_TABLE
**UnicodeStringTable
,
1065 IN CONST CHAR16
*UnicodeString
,
1066 IN BOOLEAN Iso639Language
1069 UINTN NumberOfEntries
;
1070 EFI_UNICODE_STRING_TABLE
*OldUnicodeStringTable
;
1071 EFI_UNICODE_STRING_TABLE
*NewUnicodeStringTable
;
1072 UINTN UnicodeStringLength
;
1075 CHAR8
*LanguageString
;
1078 // Make sure the parameter are valid
1080 if (Language
== NULL
|| UnicodeString
== NULL
|| UnicodeStringTable
== NULL
) {
1081 return EFI_INVALID_PARAMETER
;
1085 // If there are no supported languages, then a Unicode String can not be added
1087 if (SupportedLanguages
== NULL
) {
1088 return EFI_UNSUPPORTED
;
1092 // If the Unicode String is empty, then a Unicode String can not be added
1094 if (UnicodeString
[0] == 0) {
1095 return EFI_INVALID_PARAMETER
;
1099 // Make sure Language is a member of SupportedLanguages
1102 while (*SupportedLanguages
!= 0) {
1103 if (Iso639Language
) {
1104 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
1108 SupportedLanguages
+= 3;
1110 for (Index
= 0; SupportedLanguages
[Index
] != 0 && SupportedLanguages
[Index
] != ';'; Index
++);
1111 if (AsciiStrnCmp(SupportedLanguages
, Language
, Index
) == 0) {
1115 SupportedLanguages
+= Index
;
1116 for (; *SupportedLanguages
!= 0 && *SupportedLanguages
== ';'; SupportedLanguages
++);
1121 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED
1124 return EFI_UNSUPPORTED
;
1128 // Determine the size of the Unicode String Table by looking for a NULL Language entry
1130 NumberOfEntries
= 0;
1131 if (*UnicodeStringTable
!= NULL
) {
1132 OldUnicodeStringTable
= *UnicodeStringTable
;
1133 while (OldUnicodeStringTable
->Language
!= NULL
) {
1134 LanguageString
= OldUnicodeStringTable
->Language
;
1136 while (*LanguageString
!= 0) {
1137 for (Index
= 0; LanguageString
[Index
] != 0 && LanguageString
[Index
] != ';'; Index
++);
1139 if (AsciiStrnCmp (Language
, LanguageString
, Index
) == 0) {
1140 return EFI_ALREADY_STARTED
;
1142 LanguageString
+= Index
;
1143 for (; *LanguageString
!= 0 && *LanguageString
== ';'; LanguageString
++);
1145 OldUnicodeStringTable
++;
1151 // Allocate space for a new Unicode String Table. It must hold the current number of
1152 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table
1155 NewUnicodeStringTable
= AllocatePool ((NumberOfEntries
+ 2) * sizeof (EFI_UNICODE_STRING_TABLE
));
1156 if (NewUnicodeStringTable
== NULL
) {
1157 return EFI_OUT_OF_RESOURCES
;
1161 // If the current Unicode String Table contains any entries, then copy them to the
1162 // newly allocated Unicode String Table.
1164 if (*UnicodeStringTable
!= NULL
) {
1166 NewUnicodeStringTable
,
1167 *UnicodeStringTable
,
1168 NumberOfEntries
* sizeof (EFI_UNICODE_STRING_TABLE
)
1173 // Allocate space for a copy of the Language specifier
1175 NewUnicodeStringTable
[NumberOfEntries
].Language
= AllocateCopyPool (AsciiStrSize(Language
), Language
);
1176 if (NewUnicodeStringTable
[NumberOfEntries
].Language
== NULL
) {
1177 FreePool (NewUnicodeStringTable
);
1178 return EFI_OUT_OF_RESOURCES
;
1182 // Compute the length of the Unicode String
1184 for (UnicodeStringLength
= 0; UnicodeString
[UnicodeStringLength
] != 0; UnicodeStringLength
++);
1187 // Allocate space for a copy of the Unicode String
1189 NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
= AllocateCopyPool (StrSize (UnicodeString
), UnicodeString
);
1190 if (NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
== NULL
) {
1191 FreePool (NewUnicodeStringTable
[NumberOfEntries
].Language
);
1192 FreePool (NewUnicodeStringTable
);
1193 return EFI_OUT_OF_RESOURCES
;
1197 // Mark the end of the Unicode String Table
1199 NewUnicodeStringTable
[NumberOfEntries
+ 1].Language
= NULL
;
1200 NewUnicodeStringTable
[NumberOfEntries
+ 1].UnicodeString
= NULL
;
1203 // Free the old Unicode String Table
1205 if (*UnicodeStringTable
!= NULL
) {
1206 FreePool (*UnicodeStringTable
);
1210 // Point UnicodeStringTable at the newly allocated Unicode String Table
1212 *UnicodeStringTable
= NewUnicodeStringTable
;
1218 This function frees the table of Unicode strings in UnicodeStringTable.
1220 If UnicodeStringTable is NULL, then EFI_SUCCESS is returned.
1221 Otherwise, each language code, and each Unicode string in the Unicode string
1222 table are freed, and EFI_SUCCESS is returned.
1224 @param UnicodeStringTable A pointer to the table of Unicode strings.
1226 @retval EFI_SUCCESS The Unicode string table was freed.
1231 FreeUnicodeStringTable (
1232 IN EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
1238 // If the Unicode String Table is NULL, then it is already freed
1240 if (UnicodeStringTable
== NULL
) {
1245 // Loop through the Unicode String Table until we reach the end of table marker
1247 for (Index
= 0; UnicodeStringTable
[Index
].Language
!= NULL
; Index
++) {
1250 // Free the Language string from the Unicode String Table
1252 FreePool (UnicodeStringTable
[Index
].Language
);
1255 // Free the Unicode String from the Unicode String Table
1257 if (UnicodeStringTable
[Index
].UnicodeString
!= NULL
) {
1258 FreePool (UnicodeStringTable
[Index
].UnicodeString
);
1263 // Free the Unicode String Table itself
1265 FreePool (UnicodeStringTable
);
1270 #ifndef DISABLE_NEW_DEPRECATED_INTERFACES
1273 [ATTENTION] This function will be deprecated for security reason.
1275 Returns a pointer to an allocated buffer that contains the contents of a
1276 variable retrieved through the UEFI Runtime Service GetVariable(). The
1277 returned buffer is allocated using AllocatePool(). The caller is responsible
1278 for freeing this buffer with FreePool().
1280 If Name is NULL, then ASSERT().
1281 If Guid is NULL, then ASSERT().
1283 @param[in] Name The pointer to a Null-terminated Unicode string.
1284 @param[in] Guid The pointer to an EFI_GUID structure
1286 @retval NULL The variable could not be retrieved.
1287 @retval NULL There are not enough resources available for the variable contents.
1288 @retval Other A pointer to allocated buffer containing the variable contents.
1294 IN CONST CHAR16
*Name
,
1295 IN CONST EFI_GUID
*Guid
1302 ASSERT (Name
!= NULL
);
1303 ASSERT (Guid
!= NULL
);
1306 // Try to get the variable size.
1310 Status
= gRT
->GetVariable ((CHAR16
*) Name
, (EFI_GUID
*) Guid
, NULL
, &Size
, Value
);
1311 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
1316 // Allocate buffer to get the variable.
1318 Value
= AllocatePool (Size
);
1319 if (Value
== NULL
) {
1324 // Get the variable data.
1326 Status
= gRT
->GetVariable ((CHAR16
*) Name
, (EFI_GUID
*) Guid
, NULL
, &Size
, Value
);
1327 if (EFI_ERROR (Status
)) {
1336 [ATTENTION] This function will be deprecated for security reason.
1338 Returns a pointer to an allocated buffer that contains the contents of a
1339 variable retrieved through the UEFI Runtime Service GetVariable(). This
1340 function always uses the EFI_GLOBAL_VARIABLE GUID to retrieve variables.
1341 The returned buffer is allocated using AllocatePool(). The caller is
1342 responsible for freeing this buffer with FreePool().
1344 If Name is NULL, then ASSERT().
1346 @param[in] Name The pointer to a Null-terminated Unicode string.
1348 @retval NULL The variable could not be retrieved.
1349 @retval NULL There are not enough resources available for the variable contents.
1350 @retval Other A pointer to allocated buffer containing the variable contents.
1355 GetEfiGlobalVariable (
1356 IN CONST CHAR16
*Name
1359 return GetVariable (Name
, &gEfiGlobalVariableGuid
);
1364 Returns the status whether get the variable success. The function retrieves
1365 variable through the UEFI Runtime Service GetVariable(). The
1366 returned buffer is allocated using AllocatePool(). The caller is responsible
1367 for freeing this buffer with FreePool().
1369 If Name is NULL, then ASSERT().
1370 If Guid is NULL, then ASSERT().
1371 If Value is NULL, then ASSERT().
1373 @param[in] Name The pointer to a Null-terminated Unicode string.
1374 @param[in] Guid The pointer to an EFI_GUID structure
1375 @param[out] Value The buffer point saved the variable info.
1376 @param[out] Size The buffer size of the variable.
1378 @return EFI_OUT_OF_RESOURCES Allocate buffer failed.
1379 @return EFI_SUCCESS Find the specified variable.
1380 @return Others Errors Return errors from call to gRT->GetVariable.
1386 IN CONST CHAR16
*Name
,
1387 IN CONST EFI_GUID
*Guid
,
1389 OUT UINTN
*Size OPTIONAL
1395 ASSERT (Name
!= NULL
&& Guid
!= NULL
&& Value
!= NULL
);
1398 // Try to get the variable size.
1406 Status
= gRT
->GetVariable ((CHAR16
*) Name
, (EFI_GUID
*) Guid
, NULL
, &BufferSize
, *Value
);
1407 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
1412 // Allocate buffer to get the variable.
1414 *Value
= AllocatePool (BufferSize
);
1415 ASSERT (*Value
!= NULL
);
1416 if (*Value
== NULL
) {
1417 return EFI_OUT_OF_RESOURCES
;
1421 // Get the variable data.
1423 Status
= gRT
->GetVariable ((CHAR16
*) Name
, (EFI_GUID
*) Guid
, NULL
, &BufferSize
, *Value
);
1424 if (EFI_ERROR (Status
)) {
1436 /** Return the attributes of the variable.
1438 Returns the status whether get the variable success. The function retrieves
1439 variable through the UEFI Runtime Service GetVariable(). The
1440 returned buffer is allocated using AllocatePool(). The caller is responsible
1441 for freeing this buffer with FreePool(). The attributes are returned if
1442 the caller provides a valid Attribute parameter.
1444 If Name is NULL, then ASSERT().
1445 If Guid is NULL, then ASSERT().
1446 If Value is NULL, then ASSERT().
1448 @param[in] Name The pointer to a Null-terminated Unicode string.
1449 @param[in] Guid The pointer to an EFI_GUID structure
1450 @param[out] Value The buffer point saved the variable info.
1451 @param[out] Size The buffer size of the variable.
1452 @param[out] Attr The pointer to the variable attributes as found in var store
1454 @retval EFI_OUT_OF_RESOURCES Allocate buffer failed.
1455 @retval EFI_SUCCESS Find the specified variable.
1456 @retval Others Errors Return errors from call to gRT->GetVariable.
1462 IN CONST CHAR16
*Name
,
1463 IN CONST EFI_GUID
*Guid
,
1465 OUT UINTN
*Size OPTIONAL
,
1466 OUT UINT32
*Attr OPTIONAL
1472 ASSERT(Name
!= NULL
&& Guid
!= NULL
&& Value
!= NULL
);
1475 // Try to get the variable size.
1487 Status
= gRT
->GetVariable((CHAR16
*)Name
, (EFI_GUID
*)Guid
, Attr
, &BufferSize
, *Value
);
1488 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
1493 // Allocate buffer to get the variable.
1495 *Value
= AllocatePool(BufferSize
);
1496 ASSERT(*Value
!= NULL
);
1497 if (*Value
== NULL
) {
1498 return EFI_OUT_OF_RESOURCES
;
1502 // Get the variable data.
1504 Status
= gRT
->GetVariable((CHAR16
*)Name
, (EFI_GUID
*)Guid
, Attr
, &BufferSize
, *Value
);
1505 if (EFI_ERROR(Status
)) {
1518 Returns a pointer to an allocated buffer that contains the contents of a
1519 variable retrieved through the UEFI Runtime Service GetVariable(). This
1520 function always uses the EFI_GLOBAL_VARIABLE GUID to retrieve variables.
1521 The returned buffer is allocated using AllocatePool(). The caller is
1522 responsible for freeing this buffer with FreePool().
1524 If Name is NULL, then ASSERT().
1525 If Value is NULL, then ASSERT().
1527 @param[in] Name The pointer to a Null-terminated Unicode string.
1528 @param[out] Value The buffer point saved the variable info.
1529 @param[out] Size The buffer size of the variable.
1531 @return EFI_OUT_OF_RESOURCES Allocate buffer failed.
1532 @return EFI_SUCCESS Find the specified variable.
1533 @return Others Errors Return errors from call to gRT->GetVariable.
1538 GetEfiGlobalVariable2 (
1539 IN CONST CHAR16
*Name
,
1541 OUT UINTN
*Size OPTIONAL
1544 return GetVariable2 (Name
, &gEfiGlobalVariableGuid
, Value
, Size
);
1548 Returns a pointer to an allocated buffer that contains the best matching language
1549 from a set of supported languages.
1551 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1552 code types may not be mixed in a single call to this function. The language
1553 code returned is allocated using AllocatePool(). The caller is responsible for
1554 freeing the allocated buffer using FreePool(). This function supports a variable
1555 argument list that allows the caller to pass in a prioritized list of language
1556 codes to test against all the language codes in SupportedLanguages.
1558 If SupportedLanguages is NULL, then ASSERT().
1560 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1561 contains a set of language codes in the format
1562 specified by Iso639Language.
1563 @param[in] Iso639Language If not zero, then all language codes are assumed to be
1564 in ISO 639-2 format. If zero, then all language
1565 codes are assumed to be in RFC 4646 language format
1566 @param[in] ... A variable argument list that contains pointers to
1567 Null-terminated ASCII strings that contain one or more
1568 language codes in the format specified by Iso639Language.
1569 The first language code from each of these language
1570 code lists is used to determine if it is an exact or
1571 close match to any of the language codes in
1572 SupportedLanguages. Close matches only apply to RFC 4646
1573 language codes, and the matching algorithm from RFC 4647
1574 is used to determine if a close match is present. If
1575 an exact or close match is found, then the matching
1576 language code from SupportedLanguages is returned. If
1577 no matches are found, then the next variable argument
1578 parameter is evaluated. The variable argument list
1579 is terminated by a NULL.
1581 @retval NULL The best matching language could not be found in SupportedLanguages.
1582 @retval NULL There are not enough resources available to return the best matching
1584 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1585 language in SupportedLanguages.
1591 IN CONST CHAR8
*SupportedLanguages
,
1592 IN UINTN Iso639Language
,
1598 UINTN CompareLength
;
1599 UINTN LanguageLength
;
1600 CONST CHAR8
*Supported
;
1601 CHAR8
*BestLanguage
;
1603 ASSERT (SupportedLanguages
!= NULL
);
1605 VA_START (Args
, Iso639Language
);
1606 while ((Language
= VA_ARG (Args
, CHAR8
*)) != NULL
) {
1608 // Default to ISO 639-2 mode
1611 LanguageLength
= MIN (3, AsciiStrLen (Language
));
1614 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1616 if (Iso639Language
== 0) {
1617 for (LanguageLength
= 0; Language
[LanguageLength
] != 0 && Language
[LanguageLength
] != ';'; LanguageLength
++);
1621 // Trim back the length of Language used until it is empty
1623 while (LanguageLength
> 0) {
1625 // Loop through all language codes in SupportedLanguages
1627 for (Supported
= SupportedLanguages
; *Supported
!= '\0'; Supported
+= CompareLength
) {
1629 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1631 if (Iso639Language
== 0) {
1633 // Skip ';' characters in Supported
1635 for (; *Supported
!= '\0' && *Supported
== ';'; Supported
++);
1637 // Determine the length of the next language code in Supported
1639 for (CompareLength
= 0; Supported
[CompareLength
] != 0 && Supported
[CompareLength
] != ';'; CompareLength
++);
1641 // If Language is longer than the Supported, then skip to the next language
1643 if (LanguageLength
> CompareLength
) {
1648 // See if the first LanguageLength characters in Supported match Language
1650 if (AsciiStrnCmp (Supported
, Language
, LanguageLength
) == 0) {
1653 // Allocate, copy, and return the best matching language code from SupportedLanguages
1655 BestLanguage
= AllocateZeroPool (CompareLength
+ 1);
1656 if (BestLanguage
== NULL
) {
1659 return CopyMem (BestLanguage
, Supported
, CompareLength
);
1663 if (Iso639Language
!= 0) {
1665 // If ISO 639 mode, then each language can only be tested once
1670 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1672 for (LanguageLength
--; LanguageLength
> 0 && Language
[LanguageLength
] != '-'; LanguageLength
--);
1679 // No matches were found
1685 Returns an array of protocol instance that matches the given protocol.
1687 @param[in] Protocol Provides the protocol to search for.
1688 @param[out] NoProtocols The number of protocols returned in Buffer.
1689 @param[out] Buffer A pointer to the buffer to return the requested
1690 array of protocol instances that match Protocol.
1691 The returned buffer is allocated using
1692 EFI_BOOT_SERVICES.AllocatePool(). The caller is
1693 responsible for freeing this buffer with
1694 EFI_BOOT_SERVICES.FreePool().
1696 @retval EFI_SUCCESS The array of protocols was returned in Buffer,
1697 and the number of protocols in Buffer was
1698 returned in NoProtocols.
1699 @retval EFI_NOT_FOUND No protocols found.
1700 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the
1702 @retval EFI_INVALID_PARAMETER Protocol is NULL.
1703 @retval EFI_INVALID_PARAMETER NoProtocols is NULL.
1704 @retval EFI_INVALID_PARAMETER Buffer is NULL.
1709 EfiLocateProtocolBuffer (
1710 IN EFI_GUID
*Protocol
,
1711 OUT UINTN
*NoProtocols
,
1717 EFI_HANDLE
*HandleBuffer
;
1721 // Check input parameters
1723 if (Protocol
== NULL
|| NoProtocols
== NULL
|| Buffer
== NULL
) {
1724 return EFI_INVALID_PARAMETER
;
1728 // Initialze output parameters
1734 // Retrieve the array of handles that support Protocol
1736 Status
= gBS
->LocateHandleBuffer (
1743 if (EFI_ERROR (Status
)) {
1748 // Allocate array of protocol instances
1750 Status
= gBS
->AllocatePool (
1751 EfiBootServicesData
,
1752 NoHandles
* sizeof (VOID
*),
1755 if (EFI_ERROR (Status
)) {
1757 // Free the handle buffer
1759 gBS
->FreePool (HandleBuffer
);
1760 return EFI_OUT_OF_RESOURCES
;
1762 ZeroMem (*Buffer
, NoHandles
* sizeof (VOID
*));
1765 // Lookup Protocol on each handle in HandleBuffer to fill in the array of
1766 // protocol instances. Handle case where protocol instance was present when
1767 // LocateHandleBuffer() was called, but is not present when HandleProtocol()
1770 for (Index
= 0, *NoProtocols
= 0; Index
< NoHandles
; Index
++) {
1771 Status
= gBS
->HandleProtocol (
1772 HandleBuffer
[Index
],
1774 &((*Buffer
)[*NoProtocols
])
1776 if (!EFI_ERROR (Status
)) {
1782 // Free the handle buffer
1784 gBS
->FreePool (HandleBuffer
);
1787 // Make sure at least one protocol instance was found
1789 if (*NoProtocols
== 0) {
1790 gBS
->FreePool (*Buffer
);
1792 return EFI_NOT_FOUND
;
1799 Open or create a file or directory, possibly creating the chain of
1800 directories leading up to the directory.
1802 EfiOpenFileByDevicePath() first locates EFI_SIMPLE_FILE_SYSTEM_PROTOCOL on
1803 FilePath, and opens the root directory of that filesystem with
1804 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL.OpenVolume().
1806 On the remaining device path, the longest initial sequence of
1807 FILEPATH_DEVICE_PATH nodes is node-wise traversed with
1808 EFI_FILE_PROTOCOL.Open().
1810 (As a consequence, if OpenMode includes EFI_FILE_MODE_CREATE, and Attributes
1811 includes EFI_FILE_DIRECTORY, and each FILEPATH_DEVICE_PATH specifies a single
1812 pathname component, then EfiOpenFileByDevicePath() ensures that the specified
1813 series of subdirectories exist on return.)
1815 The EFI_FILE_PROTOCOL identified by the last FILEPATH_DEVICE_PATH node is
1816 output to the caller; intermediate EFI_FILE_PROTOCOL instances are closed. If
1817 there are no FILEPATH_DEVICE_PATH nodes past the node that identifies the
1818 filesystem, then the EFI_FILE_PROTOCOL of the root directory of the
1819 filesystem is output to the caller. If a device path node that is different
1820 from FILEPATH_DEVICE_PATH is encountered relative to the filesystem, the
1821 traversal is stopped with an error, and a NULL EFI_FILE_PROTOCOL is output.
1823 @param[in,out] FilePath On input, the device path to the file or directory
1824 to open or create. The caller is responsible for
1825 ensuring that the device path pointed-to by FilePath
1826 is well-formed. On output, FilePath points one past
1827 the last node in the original device path that has
1828 been successfully processed. FilePath is set on
1829 output even if EfiOpenFileByDevicePath() returns an
1832 @param[out] File On error, File is set to NULL. On success, File is
1833 set to the EFI_FILE_PROTOCOL of the root directory
1834 of the filesystem, if there are no
1835 FILEPATH_DEVICE_PATH nodes in FilePath; otherwise,
1836 File is set to the EFI_FILE_PROTOCOL identified by
1837 the last node in FilePath.
1839 @param[in] OpenMode The OpenMode parameter to pass to
1840 EFI_FILE_PROTOCOL.Open().
1842 @param[in] Attributes The Attributes parameter to pass to
1843 EFI_FILE_PROTOCOL.Open().
1845 @retval EFI_SUCCESS The file or directory has been opened or
1848 @retval EFI_INVALID_PARAMETER FilePath is NULL; or File is NULL; or FilePath
1849 contains a device path node, past the node
1851 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL, that is not a
1852 FILEPATH_DEVICE_PATH node.
1854 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
1856 @return Error codes propagated from the
1857 LocateDevicePath() and OpenProtocol() boot
1858 services, and from the
1859 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL.OpenVolume()
1860 and EFI_FILE_PROTOCOL.Open() member functions.
1864 EfiOpenFileByDevicePath (
1865 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
1866 OUT EFI_FILE_PROTOCOL
**File
,
1868 IN UINT64 Attributes
1872 EFI_HANDLE FileSystemHandle
;
1873 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*FileSystem
;
1874 EFI_FILE_PROTOCOL
*LastFile
;
1875 FILEPATH_DEVICE_PATH
*FilePathNode
;
1876 CHAR16
*AlignedPathName
;
1878 EFI_FILE_PROTOCOL
*NextFile
;
1881 return EFI_INVALID_PARAMETER
;
1885 if (FilePath
== NULL
) {
1886 return EFI_INVALID_PARAMETER
;
1890 // Look up the filesystem.
1892 Status
= gBS
->LocateDevicePath (
1893 &gEfiSimpleFileSystemProtocolGuid
,
1897 if (EFI_ERROR (Status
)) {
1900 Status
= gBS
->OpenProtocol (
1902 &gEfiSimpleFileSystemProtocolGuid
,
1903 (VOID
**)&FileSystem
,
1906 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1908 if (EFI_ERROR (Status
)) {
1913 // Open the root directory of the filesystem. After this operation succeeds,
1914 // we have to release LastFile on error.
1916 Status
= FileSystem
->OpenVolume (FileSystem
, &LastFile
);
1917 if (EFI_ERROR (Status
)) {
1922 // Traverse the device path nodes relative to the filesystem.
1924 while (!IsDevicePathEnd (*FilePath
)) {
1925 if (DevicePathType (*FilePath
) != MEDIA_DEVICE_PATH
||
1926 DevicePathSubType (*FilePath
) != MEDIA_FILEPATH_DP
) {
1927 Status
= EFI_INVALID_PARAMETER
;
1930 FilePathNode
= (FILEPATH_DEVICE_PATH
*)*FilePath
;
1933 // FilePathNode->PathName may be unaligned, and the UEFI specification
1934 // requires pointers that are passed to protocol member functions to be
1935 // aligned. Create an aligned copy of the pathname if necessary.
1937 if ((UINTN
)FilePathNode
->PathName
% sizeof *FilePathNode
->PathName
== 0) {
1938 AlignedPathName
= NULL
;
1939 PathName
= FilePathNode
->PathName
;
1941 AlignedPathName
= AllocateCopyPool (
1942 (DevicePathNodeLength (FilePathNode
) -
1943 SIZE_OF_FILEPATH_DEVICE_PATH
),
1944 FilePathNode
->PathName
1946 if (AlignedPathName
== NULL
) {
1947 Status
= EFI_OUT_OF_RESOURCES
;
1950 PathName
= AlignedPathName
;
1954 // Open or create the file corresponding to the next pathname fragment.
1956 Status
= LastFile
->Open (
1965 // Release any AlignedPathName on both error and success paths; PathName is
1966 // no longer needed.
1968 if (AlignedPathName
!= NULL
) {
1969 FreePool (AlignedPathName
);
1971 if (EFI_ERROR (Status
)) {
1976 // Advance to the next device path node.
1978 LastFile
->Close (LastFile
);
1979 LastFile
= NextFile
;
1980 *FilePath
= NextDevicePathNode (FilePathNode
);
1987 LastFile
->Close (LastFile
);
1990 // We are on the error path; we must have set an error Status for returning
1993 ASSERT (EFI_ERROR (Status
));