2 Mde UEFI library functions.
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.
13 Module Name: UefiLib.c
17 #include "UefiLibFramework.h"
20 Compare whether two names of languages are identical.
22 @param Language1 Name of language 1.
23 @param Language2 Name of language 2.
25 @retval TRUE Language 1 and language 2 are the same.
26 @retval FALSE Language 1 and language 2 are not the same.
31 CompareIso639LanguageCode (
32 IN CONST CHAR8
*Language1
,
33 IN CONST CHAR8
*Language2
39 Name1
= ReadUnaligned24 ((CONST UINT32
*) Language1
);
40 Name2
= ReadUnaligned24 ((CONST UINT32
*) Language2
);
42 return (BOOLEAN
) (Name1
== Name2
);
46 This function searches the list of configuration tables stored in the EFI System
47 Table for a table with a GUID that matches TableGuid. If a match is found,
48 then a pointer to the configuration table is returned in Table, and EFI_SUCCESS
49 is returned. If a matching GUID is not found, then EFI_NOT_FOUND is returned.
51 @param TableGuid Pointer to table's GUID type..
52 @param Table Pointer to the table associated with TableGuid in the EFI System Table.
54 @retval EFI_SUCCESS A configuration table matching TableGuid was found.
55 @retval EFI_NOT_FOUND A configuration table matching TableGuid could not be found.
60 EfiGetSystemConfigurationTable (
61 IN EFI_GUID
*TableGuid
,
65 EFI_SYSTEM_TABLE
*SystemTable
;
68 ASSERT (TableGuid
!= NULL
);
69 ASSERT (Table
!= NULL
);
73 for (Index
= 0; Index
< SystemTable
->NumberOfTableEntries
; Index
++) {
74 if (CompareGuid (TableGuid
, &(SystemTable
->ConfigurationTable
[Index
].VendorGuid
))) {
75 *Table
= SystemTable
->ConfigurationTable
[Index
].VendorTable
;
84 This function causes the notification function to be executed for every protocol
85 of type ProtocolGuid instance that exists in the system when this function is
86 invoked. In addition, every time a protocol of type ProtocolGuid instance is
87 installed or reinstalled, the notification function is also executed.
89 @param ProtocolGuid Supplies GUID of the protocol upon whose installation the event is fired.
90 @param NotifyTpl Supplies the task priority level of the event notifications.
91 @param NotifyFunction Supplies the function to notify when the event is signaled.
92 @param NotifyContext The context parameter to pass to NotifyFunction.
93 @param Registration A pointer to a memory location to receive the registration value.
95 @return The notification event that was created.
100 EfiCreateProtocolNotifyEvent(
101 IN EFI_GUID
*ProtocolGuid
,
102 IN EFI_TPL NotifyTpl
,
103 IN EFI_EVENT_NOTIFY NotifyFunction
,
104 IN VOID
*NotifyContext
, OPTIONAL
105 OUT VOID
**Registration
115 Status
= gBS
->CreateEvent (
122 ASSERT_EFI_ERROR (Status
);
125 // Register for protocol notifactions on this event
128 Status
= gBS
->RegisterProtocolNotify (
134 ASSERT_EFI_ERROR (Status
);
137 // Kick the event so we will perform an initial pass of
138 // current installed drivers
141 gBS
->SignalEvent (Event
);
146 This function creates an event using NotifyTpl, NoifyFunction, and NotifyContext.
147 This event is signaled with EfiNamedEventSignal(). This provide the ability for
148 one or more listeners on the same event named by the GUID specified by Name.
150 @param Name Supplies GUID name of the event.
151 @param NotifyTpl Supplies the task priority level of the event notifications.
152 @param NotifyFunction Supplies the function to notify when the event is signaled.
153 @param NotifyContext The context parameter to pass to NotifyFunction.
154 @param Registration A pointer to a memory location to receive the registration value.
156 @retval EFI_SUCCESS A named event was created.
157 @retval EFI_OUT_OF_RESOURCES There are not enough resource to create the named event.
162 EfiNamedEventListen (
163 IN CONST EFI_GUID
*Name
,
164 IN EFI_TPL NotifyTpl
,
165 IN EFI_EVENT_NOTIFY NotifyFunction
,
166 IN CONST VOID
*NotifyContext
, OPTIONAL
167 OUT VOID
*Registration OPTIONAL
172 VOID
*RegistrationLocal
;
177 Status
= gBS
->CreateEvent (
181 (VOID
*) NotifyContext
,
184 ASSERT_EFI_ERROR (Status
);
187 // The Registration is not optional to RegisterProtocolNotify().
188 // To make it optional to EfiNamedEventListen(), may need to substitute with a local.
190 if (Registration
!= NULL
) {
191 RegistrationLocal
= Registration
;
193 RegistrationLocal
= &RegistrationLocal
;
197 // Register for an installation of protocol interface
200 Status
= gBS
->RegisterProtocolNotify (
205 ASSERT_EFI_ERROR (Status
);
211 This function signals the named event specified by Name. The named event must
212 have been created with EfiNamedEventListen().
214 @param Name Supplies GUID name of the event.
216 @retval EFI_SUCCESS A named event was signaled.
217 @retval EFI_OUT_OF_RESOURCES There are not enough resource to signal the named event.
222 EfiNamedEventSignal (
223 IN CONST EFI_GUID
*Name
230 Status
= gBS
->InstallProtocolInterface (
233 EFI_NATIVE_INTERFACE
,
236 ASSERT_EFI_ERROR (Status
);
238 Status
= gBS
->UninstallProtocolInterface (
243 ASSERT_EFI_ERROR (Status
);
249 Returns the current TPL.
251 This function returns the current TPL. There is no EFI service to directly
252 retrieve the current TPL. Instead, the RaiseTPL() function is used to raise
253 the TPL to TPL_HIGH_LEVEL. This will return the current TPL. The TPL level
254 can then immediately be restored back to the current TPL level with a call
259 @retvale EFI_TPL The current TPL.
270 Tpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
271 gBS
->RestoreTPL (Tpl
);
278 This function initializes a basic mutual exclusion lock to the released state
279 and returns the lock. Each lock provides mutual exclusion access at its task
280 priority level. Since there is no preemption or multiprocessor support in EFI,
281 acquiring the lock only consists of raising to the locks TPL.
283 @param Lock A pointer to the lock data structure to initialize.
284 @param Priority EFI TPL associated with the lock.
292 IN OUT EFI_LOCK
*Lock
,
296 ASSERT (Lock
!= NULL
);
297 ASSERT (Priority
<= TPL_HIGH_LEVEL
);
299 Lock
->Tpl
= Priority
;
300 Lock
->OwnerTpl
= TPL_APPLICATION
;
301 Lock
->Lock
= EfiLockReleased
;
306 This function raises the system's current task priority level to the task
307 priority level of the mutual exclusion lock. Then, it places the lock in the
310 @param Priority The task priority level of the lock.
319 ASSERT (Lock
!= NULL
);
320 ASSERT (Lock
->Lock
== EfiLockReleased
);
322 Lock
->OwnerTpl
= gBS
->RaiseTPL (Lock
->Tpl
);
323 Lock
->Lock
= EfiLockAcquired
;
327 This function raises the system's current task priority level to the task
328 priority level of the mutual exclusion lock. Then, it attempts to place the
329 lock in the acquired state.
331 @param Lock A pointer to the lock to acquire.
333 @retval EFI_SUCCESS The lock was acquired.
334 @retval EFI_ACCESS_DENIED The lock could not be acquired because it is already owned.
339 EfiAcquireLockOrFail (
344 ASSERT (Lock
!= NULL
);
345 ASSERT (Lock
->Lock
!= EfiLockUninitialized
);
347 if (Lock
->Lock
== EfiLockAcquired
) {
349 // Lock is already owned, so bail out
351 return EFI_ACCESS_DENIED
;
354 Lock
->OwnerTpl
= gBS
->RaiseTPL (Lock
->Tpl
);
356 Lock
->Lock
= EfiLockAcquired
;
362 This function transitions a mutual exclusion lock from the acquired state to
363 the released state, and restores the system's task priority level to its
366 @param Lock A pointer to the lock to release.
377 ASSERT (Lock
!= NULL
);
378 ASSERT (Lock
->Lock
== EfiLockAcquired
);
380 Tpl
= Lock
->OwnerTpl
;
382 Lock
->Lock
= EfiLockReleased
;
384 gBS
->RestoreTPL (Tpl
);
388 Tests whether a controller handle is being managed by a specific driver.
390 This function tests whether the driver specified by DriverBindingHandle is
391 currently managing the controller specified by ControllerHandle. This test
392 is performed by evaluating if the the protocol specified by ProtocolGuid is
393 present on ControllerHandle and is was opened by DriverBindingHandle with an
394 attribute of EFI_OPEN_PROTOCOL_BY_DRIVER.
395 If ProtocolGuid is NULL, then ASSERT().
397 @param ControllerHandle A handle for a controller to test.
398 @param DriverBindingHandle Specifies the driver binding handle for the
400 @param ProtocolGuid Specifies the protocol that the driver specified
401 by DriverBindingHandle opens in its Start()
404 @retval EFI_SUCCESS ControllerHandle is managed by the driver
405 specifed by DriverBindingHandle.
406 @retval EFI_UNSUPPORTED ControllerHandle is not managed by the driver
407 specifed by DriverBindingHandle.
412 EfiTestManagedDevice (
413 IN CONST EFI_HANDLE ControllerHandle
,
414 IN CONST EFI_HANDLE DriverBindingHandle
,
415 IN CONST EFI_GUID
*ProtocolGuid
419 VOID
*ManagedInterface
;
421 ASSERT (ProtocolGuid
!= NULL
);
423 Status
= gBS
->OpenProtocol (
425 (EFI_GUID
*) ProtocolGuid
,
429 EFI_OPEN_PROTOCOL_BY_DRIVER
431 if (!EFI_ERROR (Status
)) {
434 (EFI_GUID
*) ProtocolGuid
,
438 return EFI_UNSUPPORTED
;
441 if (Status
!= EFI_ALREADY_STARTED
) {
442 return EFI_UNSUPPORTED
;
449 Tests whether a child handle is a child device of the controller.
451 This function tests whether ChildHandle is one of the children of
452 ControllerHandle. This test is performed by checking to see if the protocol
453 specified by ProtocolGuid is present on ControllerHandle and opened by
454 ChildHandle with an attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
455 If ProtocolGuid is NULL, then ASSERT().
457 @param ControllerHandle A handle for a (parent) controller to test.
458 @param ChildHandle A child handle to test.
459 @param ConsumsedGuid Supplies the protocol that the child controller
460 opens on its parent controller.
462 @retval EFI_SUCCESS ChildHandle is a child of the ControllerHandle.
463 @retval EFI_UNSUPPORTED ChildHandle is not a child of the
470 IN CONST EFI_HANDLE ControllerHandle
,
471 IN CONST EFI_HANDLE ChildHandle
,
472 IN CONST EFI_GUID
*ProtocolGuid
476 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfoBuffer
;
480 ASSERT (ProtocolGuid
!= NULL
);
483 // Retrieve the list of agents that are consuming the specific protocol
484 // on ControllerHandle.
486 Status
= gBS
->OpenProtocolInformation (
488 (EFI_GUID
*) ProtocolGuid
,
492 if (EFI_ERROR (Status
)) {
493 return EFI_UNSUPPORTED
;
497 // Inspect if ChildHandle is one of the agents.
499 Status
= EFI_UNSUPPORTED
;
500 for (Index
= 0; Index
< EntryCount
; Index
++) {
501 if ((OpenInfoBuffer
[Index
].ControllerHandle
== ChildHandle
) &&
502 (OpenInfoBuffer
[Index
].Attributes
& EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
) != 0) {
503 Status
= EFI_SUCCESS
;
508 FreePool (OpenInfoBuffer
);
513 This function looks up a Unicode string in UnicodeStringTable. If Language is
514 a member of SupportedLanguages and a Unicode string is found in UnicodeStringTable
515 that matches the language code specified by Language, then it is returned in
518 @param Language A pointer to the ISO 639-2 language code for the
519 Unicode string to look up and return.
520 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes
521 that the Unicode string table supports. Language
522 must be a member of this set.
523 @param UnicodeStringTable A pointer to the table of Unicode strings.
524 @param UnicodeString A pointer to the Unicode string from UnicodeStringTable
525 that matches the language specified by Language.
527 @retval EFI_SUCCESS The Unicode string that matches the language
528 specified by Language was found
529 in the table of Unicoide strings UnicodeStringTable,
530 and it was returned in UnicodeString.
531 @retval EFI_INVALID_PARAMETER Language is NULL.
532 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
533 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
534 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.
535 @retval EFI_UNSUPPORTED The language specified by Language is not a
536 member of SupportedLanguages.
537 @retval EFI_UNSUPPORTED The language specified by Language is not
538 supported by UnicodeStringTable.
543 LookupUnicodeString (
544 IN CONST CHAR8
*Language
,
545 IN CONST CHAR8
*SupportedLanguages
,
546 IN CONST EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
,
547 OUT CHAR16
**UnicodeString
551 // Make sure the parameters are valid
553 if (Language
== NULL
|| UnicodeString
== NULL
) {
554 return EFI_INVALID_PARAMETER
;
558 // If there are no supported languages, or the Unicode String Table is empty, then the
559 // Unicode String specified by Language is not supported by this Unicode String Table
561 if (SupportedLanguages
== NULL
|| UnicodeStringTable
== NULL
) {
562 return EFI_UNSUPPORTED
;
566 // Make sure Language is in the set of Supported Languages
568 while (*SupportedLanguages
!= 0) {
569 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
572 // Search the Unicode String Table for the matching Language specifier
574 while (UnicodeStringTable
->Language
!= NULL
) {
575 if (CompareIso639LanguageCode (Language
, UnicodeStringTable
->Language
)) {
578 // A matching string was found, so return it
580 *UnicodeString
= UnicodeStringTable
->UnicodeString
;
584 UnicodeStringTable
++;
587 return EFI_UNSUPPORTED
;
590 SupportedLanguages
+= 3;
593 return EFI_UNSUPPORTED
;
597 This function adds a Unicode string to UnicodeStringTable.
598 If Language is a member of SupportedLanguages then UnicodeString is added to
599 UnicodeStringTable. New buffers are allocated for both Language and
600 UnicodeString. The contents of Language and UnicodeString are copied into
601 these new buffers. These buffers are automatically freed when
602 FreeUnicodeStringTable() is called.
604 @param Language A pointer to the ISO 639-2 language code for the Unicode
606 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes
607 that the Unicode string table supports.
608 Language must be a member of this set.
609 @param UnicodeStringTable A pointer to the table of Unicode strings.
610 @param UnicodeString A pointer to the Unicode string to add.
612 @retval EFI_SUCCESS The Unicode string that matches the language
613 specified by Language was found in the table of
614 Unicode strings UnicodeStringTable, and it was
615 returned in UnicodeString.
616 @retval EFI_INVALID_PARAMETER Language is NULL.
617 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
618 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.
619 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
620 @retval EFI_ALREADY_STARTED A Unicode string with language Language is
621 already present in UnicodeStringTable.
622 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another
623 Unicode string to UnicodeStringTable.
624 @retval EFI_UNSUPPORTED The language specified by Language is not a
625 member of SupportedLanguages.
631 IN CONST CHAR8
*Language
,
632 IN CONST CHAR8
*SupportedLanguages
,
633 IN EFI_UNICODE_STRING_TABLE
**UnicodeStringTable
,
634 IN CONST CHAR16
*UnicodeString
637 UINTN NumberOfEntries
;
638 EFI_UNICODE_STRING_TABLE
*OldUnicodeStringTable
;
639 EFI_UNICODE_STRING_TABLE
*NewUnicodeStringTable
;
640 UINTN UnicodeStringLength
;
643 // Make sure the parameter are valid
645 if (Language
== NULL
|| UnicodeString
== NULL
|| UnicodeStringTable
== NULL
) {
646 return EFI_INVALID_PARAMETER
;
650 // If there are no supported languages, then a Unicode String can not be added
652 if (SupportedLanguages
== NULL
) {
653 return EFI_UNSUPPORTED
;
657 // If the Unicode String is empty, then a Unicode String can not be added
659 if (UnicodeString
[0] == 0) {
660 return EFI_INVALID_PARAMETER
;
664 // Make sure Language is a member of SupportedLanguages
666 while (*SupportedLanguages
!= 0) {
667 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
670 // Determine the size of the Unicode String Table by looking for a NULL Language entry
673 if (*UnicodeStringTable
!= NULL
) {
674 OldUnicodeStringTable
= *UnicodeStringTable
;
675 while (OldUnicodeStringTable
->Language
!= NULL
) {
676 if (CompareIso639LanguageCode (Language
, OldUnicodeStringTable
->Language
)) {
677 return EFI_ALREADY_STARTED
;
680 OldUnicodeStringTable
++;
686 // Allocate space for a new Unicode String Table. It must hold the current number of
687 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table
690 NewUnicodeStringTable
= AllocatePool ((NumberOfEntries
+ 2) * sizeof (EFI_UNICODE_STRING_TABLE
));
691 if (NewUnicodeStringTable
== NULL
) {
692 return EFI_OUT_OF_RESOURCES
;
696 // If the current Unicode String Table contains any entries, then copy them to the
697 // newly allocated Unicode String Table.
699 if (*UnicodeStringTable
!= NULL
) {
701 NewUnicodeStringTable
,
703 NumberOfEntries
* sizeof (EFI_UNICODE_STRING_TABLE
)
708 // Allocate space for a copy of the Language specifier
710 NewUnicodeStringTable
[NumberOfEntries
].Language
= AllocateCopyPool (3, Language
);
711 if (NewUnicodeStringTable
[NumberOfEntries
].Language
== NULL
) {
712 gBS
->FreePool (NewUnicodeStringTable
);
713 return EFI_OUT_OF_RESOURCES
;
717 // Compute the length of the Unicode String
719 for (UnicodeStringLength
= 0; UnicodeString
[UnicodeStringLength
] != 0; UnicodeStringLength
++)
723 // Allocate space for a copy of the Unicode String
725 NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
= AllocateCopyPool (
726 (UnicodeStringLength
+ 1) * sizeof (CHAR16
),
729 if (NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
== NULL
) {
730 gBS
->FreePool (NewUnicodeStringTable
[NumberOfEntries
].Language
);
731 gBS
->FreePool (NewUnicodeStringTable
);
732 return EFI_OUT_OF_RESOURCES
;
736 // Mark the end of the Unicode String Table
738 NewUnicodeStringTable
[NumberOfEntries
+ 1].Language
= NULL
;
739 NewUnicodeStringTable
[NumberOfEntries
+ 1].UnicodeString
= NULL
;
742 // Free the old Unicode String Table
744 if (*UnicodeStringTable
!= NULL
) {
745 gBS
->FreePool (*UnicodeStringTable
);
749 // Point UnicodeStringTable at the newly allocated Unicode String Table
751 *UnicodeStringTable
= NewUnicodeStringTable
;
756 SupportedLanguages
+= 3;
759 return EFI_UNSUPPORTED
;
763 This function frees the table of Unicode strings in UnicodeStringTable.
764 If UnicodeStringTable is NULL, then EFI_SUCCESS is returned.
765 Otherwise, each language code, and each Unicode string in the Unicode string
766 table are freed, and EFI_SUCCESS is returned.
768 @param UnicodeStringTable A pointer to the table of Unicode strings.
770 @retval EFI_SUCCESS The Unicode string table was freed.
775 FreeUnicodeStringTable (
776 IN EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
782 // If the Unicode String Table is NULL, then it is already freed
784 if (UnicodeStringTable
== NULL
) {
789 // Loop through the Unicode String Table until we reach the end of table marker
791 for (Index
= 0; UnicodeStringTable
[Index
].Language
!= NULL
; Index
++) {
794 // Free the Language string from the Unicode String Table
796 gBS
->FreePool (UnicodeStringTable
[Index
].Language
);
799 // Free the Unicode String from the Unicode String Table
801 if (UnicodeStringTable
[Index
].UnicodeString
!= NULL
) {
802 gBS
->FreePool (UnicodeStringTable
[Index
].UnicodeString
);
807 // Free the Unicode String Table itself
809 gBS
->FreePool (UnicodeStringTable
);