2 Implementation of UefiLib
4 Copyright (c) 2006 - 2007, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "FrameworkUefiLib.h"
18 Compare whether two names of languages are identical.
20 @param Language1 Name of language 1.
21 @param Language2 Name of language 2.
23 @retval TRUE Language 1 and language 2 are the same.
24 @retval FALSE Language 1 and language 2 are not the same.
29 CompareIso639LanguageCode (
30 IN CONST CHAR8
*Language1
,
31 IN CONST CHAR8
*Language2
37 Name1
= ReadUnaligned24 ((CONST UINT32
*) Language1
);
38 Name2
= ReadUnaligned24 ((CONST UINT32
*) Language2
);
40 return (BOOLEAN
) (Name1
== Name2
);
44 This function searches the list of configuration tables stored in the EFI System
45 Table for a table with a GUID that matches TableGuid. If a match is found,
46 then a pointer to the configuration table is returned in Table, and EFI_SUCCESS
47 is returned. If a matching GUID is not found, then EFI_NOT_FOUND is returned.
49 @param TableGuid Pointer to table's GUID type..
50 @param Table Pointer to the table associated with TableGuid in the EFI System Table.
52 @retval EFI_SUCCESS A configuration table matching TableGuid was found.
53 @retval EFI_NOT_FOUND A configuration table matching TableGuid could not be found.
58 EfiGetSystemConfigurationTable (
59 IN EFI_GUID
*TableGuid
,
63 EFI_SYSTEM_TABLE
*SystemTable
;
66 ASSERT (TableGuid
!= NULL
);
67 ASSERT (Table
!= NULL
);
71 for (Index
= 0; Index
< SystemTable
->NumberOfTableEntries
; Index
++) {
72 if (CompareGuid (TableGuid
, &(SystemTable
->ConfigurationTable
[Index
].VendorGuid
))) {
73 *Table
= SystemTable
->ConfigurationTable
[Index
].VendorTable
;
82 This function causes the notification function to be executed for every protocol
83 of type ProtocolGuid instance that exists in the system when this function is
84 invoked. In addition, every time a protocol of type ProtocolGuid instance is
85 installed or reinstalled, the notification function is also executed.
87 @param ProtocolGuid Supplies GUID of the protocol upon whose installation the event is fired.
88 @param NotifyTpl Supplies the task priority level of the event notifications.
89 @param NotifyFunction Supplies the function to notify when the event is signaled.
90 @param NotifyContext The context parameter to pass to NotifyFunction.
91 @param Registration A pointer to a memory location to receive the registration value.
93 @return The notification event that was created.
98 EfiCreateProtocolNotifyEvent(
99 IN EFI_GUID
*ProtocolGuid
,
100 IN EFI_TPL NotifyTpl
,
101 IN EFI_EVENT_NOTIFY NotifyFunction
,
102 IN VOID
*NotifyContext
, OPTIONAL
103 OUT VOID
**Registration
113 Status
= gBS
->CreateEvent (
120 ASSERT_EFI_ERROR (Status
);
123 // Register for protocol notifactions on this event
126 Status
= gBS
->RegisterProtocolNotify (
132 ASSERT_EFI_ERROR (Status
);
135 // Kick the event so we will perform an initial pass of
136 // current installed drivers
139 gBS
->SignalEvent (Event
);
144 This function creates an event using NotifyTpl, NoifyFunction, and NotifyContext.
145 This event is signaled with EfiNamedEventSignal(). This provide the ability for
146 one or more listeners on the same event named by the GUID specified by Name.
148 @param Name Supplies GUID name of the event.
149 @param NotifyTpl Supplies the task priority level of the event notifications.
150 @param NotifyFunction Supplies the function to notify when the event is signaled.
151 @param NotifyContext The context parameter to pass to NotifyFunction.
152 @param Registration A pointer to a memory location to receive the registration value.
154 @retval EFI_SUCCESS A named event was created.
155 @retval EFI_OUT_OF_RESOURCES There are not enough resource to create the named event.
160 EfiNamedEventListen (
161 IN CONST EFI_GUID
*Name
,
162 IN EFI_TPL NotifyTpl
,
163 IN EFI_EVENT_NOTIFY NotifyFunction
,
164 IN CONST VOID
*NotifyContext
, OPTIONAL
165 OUT VOID
*Registration OPTIONAL
170 VOID
*RegistrationLocal
;
175 Status
= gBS
->CreateEvent (
179 (VOID
*) NotifyContext
,
182 ASSERT_EFI_ERROR (Status
);
185 // The Registration is not optional to RegisterProtocolNotify().
186 // To make it optional to EfiNamedEventListen(), may need to substitute with a local.
188 if (Registration
!= NULL
) {
189 RegistrationLocal
= Registration
;
191 RegistrationLocal
= &RegistrationLocal
;
195 // Register for an installation of protocol interface
198 Status
= gBS
->RegisterProtocolNotify (
203 ASSERT_EFI_ERROR (Status
);
209 This function signals the named event specified by Name. The named event must
210 have been created with EfiNamedEventListen().
212 @param Name Supplies GUID name of the event.
214 @retval EFI_SUCCESS A named event was signaled.
215 @retval EFI_OUT_OF_RESOURCES There are not enough resource to signal the named event.
220 EfiNamedEventSignal (
221 IN CONST EFI_GUID
*Name
228 Status
= gBS
->InstallProtocolInterface (
231 EFI_NATIVE_INTERFACE
,
234 ASSERT_EFI_ERROR (Status
);
236 Status
= gBS
->UninstallProtocolInterface (
241 ASSERT_EFI_ERROR (Status
);
247 Returns the current TPL.
249 This function returns the current TPL. There is no EFI service to directly
250 retrieve the current TPL. Instead, the RaiseTPL() function is used to raise
251 the TPL to TPL_HIGH_LEVEL. This will return the current TPL. The TPL level
252 can then immediately be restored back to the current TPL level with a call
255 @return The current TPL.
266 Tpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
267 gBS
->RestoreTPL (Tpl
);
274 This function initializes a basic mutual exclusion lock to the released state
275 and returns the lock. Each lock provides mutual exclusion access at its task
276 priority level. Since there is no preemption or multiprocessor support in EFI,
277 acquiring the lock only consists of raising to the locks TPL.
279 @param Lock A pointer to the lock data structure to initialize.
280 @param Priority EFI TPL associated with the lock.
288 IN OUT EFI_LOCK
*Lock
,
292 ASSERT (Lock
!= NULL
);
293 ASSERT (Priority
<= TPL_HIGH_LEVEL
);
295 Lock
->Tpl
= Priority
;
296 Lock
->OwnerTpl
= TPL_APPLICATION
;
297 Lock
->Lock
= EfiLockReleased
;
302 This function raises the system's current task priority level to the task
303 priority level of the mutual exclusion lock. Then, it places the lock in the
306 @param Lock Point to EFI_LOCK instance
315 ASSERT (Lock
!= NULL
);
316 ASSERT (Lock
->Lock
== EfiLockReleased
);
318 Lock
->OwnerTpl
= gBS
->RaiseTPL (Lock
->Tpl
);
319 Lock
->Lock
= EfiLockAcquired
;
323 This function raises the system's current task priority level to the task
324 priority level of the mutual exclusion lock. Then, it attempts to place the
325 lock in the acquired state.
327 @param Lock A pointer to the lock to acquire.
329 @retval EFI_SUCCESS The lock was acquired.
330 @retval EFI_ACCESS_DENIED The lock could not be acquired because it is already owned.
335 EfiAcquireLockOrFail (
340 ASSERT (Lock
!= NULL
);
341 ASSERT (Lock
->Lock
!= EfiLockUninitialized
);
343 if (Lock
->Lock
== EfiLockAcquired
) {
345 // Lock is already owned, so bail out
347 return EFI_ACCESS_DENIED
;
350 Lock
->OwnerTpl
= gBS
->RaiseTPL (Lock
->Tpl
);
352 Lock
->Lock
= EfiLockAcquired
;
358 This function transitions a mutual exclusion lock from the acquired state to
359 the released state, and restores the system's task priority level to its
362 @param Lock A pointer to the lock to release.
373 ASSERT (Lock
!= NULL
);
374 ASSERT (Lock
->Lock
== EfiLockAcquired
);
376 Tpl
= Lock
->OwnerTpl
;
378 Lock
->Lock
= EfiLockReleased
;
380 gBS
->RestoreTPL (Tpl
);
384 Tests whether a controller handle is being managed by a specific driver.
386 This function tests whether the driver specified by DriverBindingHandle is
387 currently managing the controller specified by ControllerHandle. This test
388 is performed by evaluating if the the protocol specified by ProtocolGuid is
389 present on ControllerHandle and is was opened by DriverBindingHandle with an
390 attribute of EFI_OPEN_PROTOCOL_BY_DRIVER.
391 If ProtocolGuid is NULL, then ASSERT().
393 @param ControllerHandle A handle for a controller to test.
394 @param DriverBindingHandle Specifies the driver binding handle for the
396 @param ProtocolGuid Specifies the protocol that the driver specified
397 by DriverBindingHandle opens in its Start()
400 @retval EFI_SUCCESS ControllerHandle is managed by the driver
401 specifed by DriverBindingHandle.
402 @retval EFI_UNSUPPORTED ControllerHandle is not managed by the driver
403 specifed by DriverBindingHandle.
408 EfiTestManagedDevice (
409 IN CONST EFI_HANDLE ControllerHandle
,
410 IN CONST EFI_HANDLE DriverBindingHandle
,
411 IN CONST EFI_GUID
*ProtocolGuid
415 VOID
*ManagedInterface
;
417 ASSERT (ProtocolGuid
!= NULL
);
419 Status
= gBS
->OpenProtocol (
421 (EFI_GUID
*) ProtocolGuid
,
425 EFI_OPEN_PROTOCOL_BY_DRIVER
427 if (!EFI_ERROR (Status
)) {
430 (EFI_GUID
*) ProtocolGuid
,
434 return EFI_UNSUPPORTED
;
437 if (Status
!= EFI_ALREADY_STARTED
) {
438 return EFI_UNSUPPORTED
;
445 Tests whether a child handle is a child device of the controller.
447 This function tests whether ChildHandle is one of the children of
448 ControllerHandle. This test is performed by checking to see if the protocol
449 specified by ProtocolGuid is present on ControllerHandle and opened by
450 ChildHandle with an attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
451 If ProtocolGuid is NULL, then ASSERT().
453 @param ControllerHandle A handle for a (parent) controller to test.
454 @param ChildHandle A child handle to test.
455 @param ProtocolGuid Supplies the protocol that the child controller
456 opens on its parent controller.
458 @retval EFI_SUCCESS ChildHandle is a child of the ControllerHandle.
459 @retval EFI_UNSUPPORTED ChildHandle is not a child of the
466 IN CONST EFI_HANDLE ControllerHandle
,
467 IN CONST EFI_HANDLE ChildHandle
,
468 IN CONST EFI_GUID
*ProtocolGuid
472 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfoBuffer
;
476 ASSERT (ProtocolGuid
!= NULL
);
479 // Retrieve the list of agents that are consuming the specific protocol
480 // on ControllerHandle.
482 Status
= gBS
->OpenProtocolInformation (
484 (EFI_GUID
*) ProtocolGuid
,
488 if (EFI_ERROR (Status
)) {
489 return EFI_UNSUPPORTED
;
493 // Inspect if ChildHandle is one of the agents.
495 Status
= EFI_UNSUPPORTED
;
496 for (Index
= 0; Index
< EntryCount
; Index
++) {
497 if ((OpenInfoBuffer
[Index
].ControllerHandle
== ChildHandle
) &&
498 (OpenInfoBuffer
[Index
].Attributes
& EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
) != 0) {
499 Status
= EFI_SUCCESS
;
504 FreePool (OpenInfoBuffer
);
509 This function looks up a Unicode string in UnicodeStringTable. If Language is
510 a member of SupportedLanguages and a Unicode string is found in UnicodeStringTable
511 that matches the language code specified by Language, then it is returned in
514 @param Language A pointer to the ISO 639-2 language code for the
515 Unicode string to look up and return.
516 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes
517 that the Unicode string table supports. Language
518 must be a member of this set.
519 @param UnicodeStringTable A pointer to the table of Unicode strings.
520 @param UnicodeString A pointer to the Unicode string from UnicodeStringTable
521 that matches the language specified by Language.
523 @retval EFI_SUCCESS The Unicode string that matches the language
524 specified by Language was found
525 in the table of Unicoide strings UnicodeStringTable,
526 and it was returned in UnicodeString.
527 @retval EFI_INVALID_PARAMETER Language is NULL.
528 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
529 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
530 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.
531 @retval EFI_UNSUPPORTED The language specified by Language is not a
532 member of SupportedLanguages.
533 @retval EFI_UNSUPPORTED The language specified by Language is not
534 supported by UnicodeStringTable.
539 LookupUnicodeString (
540 IN CONST CHAR8
*Language
,
541 IN CONST CHAR8
*SupportedLanguages
,
542 IN CONST EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
,
543 OUT CHAR16
**UnicodeString
547 // Make sure the parameters are valid
549 if (Language
== NULL
|| UnicodeString
== NULL
) {
550 return EFI_INVALID_PARAMETER
;
554 // If there are no supported languages, or the Unicode String Table is empty, then the
555 // Unicode String specified by Language is not supported by this Unicode String Table
557 if (SupportedLanguages
== NULL
|| UnicodeStringTable
== NULL
) {
558 return EFI_UNSUPPORTED
;
562 // Make sure Language is in the set of Supported Languages
564 while (*SupportedLanguages
!= 0) {
565 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
568 // Search the Unicode String Table for the matching Language specifier
570 while (UnicodeStringTable
->Language
!= NULL
) {
571 if (CompareIso639LanguageCode (Language
, UnicodeStringTable
->Language
)) {
574 // A matching string was found, so return it
576 *UnicodeString
= UnicodeStringTable
->UnicodeString
;
580 UnicodeStringTable
++;
583 return EFI_UNSUPPORTED
;
586 SupportedLanguages
+= 3;
589 return EFI_UNSUPPORTED
;
593 This function adds a Unicode string to UnicodeStringTable.
594 If Language is a member of SupportedLanguages then UnicodeString is added to
595 UnicodeStringTable. New buffers are allocated for both Language and
596 UnicodeString. The contents of Language and UnicodeString are copied into
597 these new buffers. These buffers are automatically freed when
598 FreeUnicodeStringTable() is called.
600 @param Language A pointer to the ISO 639-2 language code for the Unicode
602 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes
603 that the Unicode string table supports.
604 Language must be a member of this set.
605 @param UnicodeStringTable A pointer to the table of Unicode strings.
606 @param UnicodeString A pointer to the Unicode string to add.
608 @retval EFI_SUCCESS The Unicode string that matches the language
609 specified by Language was found in the table of
610 Unicode strings UnicodeStringTable, and it was
611 returned in UnicodeString.
612 @retval EFI_INVALID_PARAMETER Language is NULL.
613 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
614 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.
615 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
616 @retval EFI_ALREADY_STARTED A Unicode string with language Language is
617 already present in UnicodeStringTable.
618 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another
619 Unicode string to UnicodeStringTable.
620 @retval EFI_UNSUPPORTED The language specified by Language is not a
621 member of SupportedLanguages.
627 IN CONST CHAR8
*Language
,
628 IN CONST CHAR8
*SupportedLanguages
,
629 IN EFI_UNICODE_STRING_TABLE
**UnicodeStringTable
,
630 IN CONST CHAR16
*UnicodeString
633 UINTN NumberOfEntries
;
634 EFI_UNICODE_STRING_TABLE
*OldUnicodeStringTable
;
635 EFI_UNICODE_STRING_TABLE
*NewUnicodeStringTable
;
636 UINTN UnicodeStringLength
;
639 // Make sure the parameter are valid
641 if (Language
== NULL
|| UnicodeString
== NULL
|| UnicodeStringTable
== NULL
) {
642 return EFI_INVALID_PARAMETER
;
646 // If there are no supported languages, then a Unicode String can not be added
648 if (SupportedLanguages
== NULL
) {
649 return EFI_UNSUPPORTED
;
653 // If the Unicode String is empty, then a Unicode String can not be added
655 if (UnicodeString
[0] == 0) {
656 return EFI_INVALID_PARAMETER
;
660 // Make sure Language is a member of SupportedLanguages
662 while (*SupportedLanguages
!= 0) {
663 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
666 // Determine the size of the Unicode String Table by looking for a NULL Language entry
669 if (*UnicodeStringTable
!= NULL
) {
670 OldUnicodeStringTable
= *UnicodeStringTable
;
671 while (OldUnicodeStringTable
->Language
!= NULL
) {
672 if (CompareIso639LanguageCode (Language
, OldUnicodeStringTable
->Language
)) {
673 return EFI_ALREADY_STARTED
;
676 OldUnicodeStringTable
++;
682 // Allocate space for a new Unicode String Table. It must hold the current number of
683 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table
686 NewUnicodeStringTable
= AllocatePool ((NumberOfEntries
+ 2) * sizeof (EFI_UNICODE_STRING_TABLE
));
687 if (NewUnicodeStringTable
== NULL
) {
688 return EFI_OUT_OF_RESOURCES
;
692 // If the current Unicode String Table contains any entries, then copy them to the
693 // newly allocated Unicode String Table.
695 if (*UnicodeStringTable
!= NULL
) {
697 NewUnicodeStringTable
,
699 NumberOfEntries
* sizeof (EFI_UNICODE_STRING_TABLE
)
704 // Allocate space for a copy of the Language specifier
706 NewUnicodeStringTable
[NumberOfEntries
].Language
= AllocateCopyPool (3, Language
);
707 if (NewUnicodeStringTable
[NumberOfEntries
].Language
== NULL
) {
708 gBS
->FreePool (NewUnicodeStringTable
);
709 return EFI_OUT_OF_RESOURCES
;
713 // Compute the length of the Unicode String
715 for (UnicodeStringLength
= 0; UnicodeString
[UnicodeStringLength
] != 0; UnicodeStringLength
++)
719 // Allocate space for a copy of the Unicode String
721 NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
= AllocateCopyPool (
722 (UnicodeStringLength
+ 1) * sizeof (CHAR16
),
725 if (NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
== NULL
) {
726 gBS
->FreePool (NewUnicodeStringTable
[NumberOfEntries
].Language
);
727 gBS
->FreePool (NewUnicodeStringTable
);
728 return EFI_OUT_OF_RESOURCES
;
732 // Mark the end of the Unicode String Table
734 NewUnicodeStringTable
[NumberOfEntries
+ 1].Language
= NULL
;
735 NewUnicodeStringTable
[NumberOfEntries
+ 1].UnicodeString
= NULL
;
738 // Free the old Unicode String Table
740 if (*UnicodeStringTable
!= NULL
) {
741 gBS
->FreePool (*UnicodeStringTable
);
745 // Point UnicodeStringTable at the newly allocated Unicode String Table
747 *UnicodeStringTable
= NewUnicodeStringTable
;
752 SupportedLanguages
+= 3;
755 return EFI_UNSUPPORTED
;
759 This function frees the table of Unicode strings in UnicodeStringTable.
760 If UnicodeStringTable is NULL, then EFI_SUCCESS is returned.
761 Otherwise, each language code, and each Unicode string in the Unicode string
762 table are freed, and EFI_SUCCESS is returned.
764 @param UnicodeStringTable A pointer to the table of Unicode strings.
766 @retval EFI_SUCCESS The Unicode string table was freed.
771 FreeUnicodeStringTable (
772 IN EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
778 // If the Unicode String Table is NULL, then it is already freed
780 if (UnicodeStringTable
== NULL
) {
785 // Loop through the Unicode String Table until we reach the end of table marker
787 for (Index
= 0; UnicodeStringTable
[Index
].Language
!= NULL
; Index
++) {
790 // Free the Language string from the Unicode String Table
792 gBS
->FreePool (UnicodeStringTable
[Index
].Language
);
795 // Free the Unicode String from the Unicode String Table
797 if (UnicodeStringTable
[Index
].UnicodeString
!= NULL
) {
798 gBS
->FreePool (UnicodeStringTable
[Index
].UnicodeString
);
803 // Free the Unicode String Table itself
805 gBS
->FreePool (UnicodeStringTable
);