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.
28 CompareIso639LanguageCode (
29 IN CONST CHAR8
*Language1
,
30 IN CONST CHAR8
*Language2
36 Name1
= ReadUnaligned24 ((CONST UINT32
*) Language1
);
37 Name2
= ReadUnaligned24 ((CONST UINT32
*) Language2
);
39 return (BOOLEAN
) (Name1
== Name2
);
43 This function searches the list of configuration tables stored in the EFI System
44 Table for a table with a GUID that matches TableGuid. If a match is found,
45 then a pointer to the configuration table is returned in Table, and EFI_SUCCESS
46 is returned. If a matching GUID is not found, then EFI_NOT_FOUND is returned.
48 @param TableGuid Pointer to table's GUID type..
49 @param Table Pointer to the table associated with TableGuid in the EFI System Table.
51 @retval EFI_SUCCESS A configuration table matching TableGuid was found.
52 @retval EFI_NOT_FOUND A configuration table matching TableGuid could not be found.
57 EfiGetSystemConfigurationTable (
58 IN EFI_GUID
*TableGuid
,
62 EFI_SYSTEM_TABLE
*SystemTable
;
65 ASSERT (TableGuid
!= NULL
);
66 ASSERT (Table
!= NULL
);
70 for (Index
= 0; Index
< SystemTable
->NumberOfTableEntries
; Index
++) {
71 if (CompareGuid (TableGuid
, &(SystemTable
->ConfigurationTable
[Index
].VendorGuid
))) {
72 *Table
= SystemTable
->ConfigurationTable
[Index
].VendorTable
;
81 This function causes the notification function to be executed for every protocol
82 of type ProtocolGuid instance that exists in the system when this function is
83 invoked. In addition, every time a protocol of type ProtocolGuid instance is
84 installed or reinstalled, the notification function is also executed.
86 @param ProtocolGuid Supplies GUID of the protocol upon whose installation the event is fired.
87 @param NotifyTpl Supplies the task priority level of the event notifications.
88 @param NotifyFunction Supplies the function to notify when the event is signaled.
89 @param NotifyContext The context parameter to pass to NotifyFunction.
90 @param Registration A pointer to a memory location to receive the registration value.
92 @return The notification event that was created.
97 EfiCreateProtocolNotifyEvent(
98 IN EFI_GUID
*ProtocolGuid
,
100 IN EFI_EVENT_NOTIFY NotifyFunction
,
101 IN VOID
*NotifyContext
, OPTIONAL
102 OUT VOID
**Registration
112 Status
= gBS
->CreateEvent (
119 ASSERT_EFI_ERROR (Status
);
122 // Register for protocol notifactions on this event
125 Status
= gBS
->RegisterProtocolNotify (
131 ASSERT_EFI_ERROR (Status
);
134 // Kick the event so we will perform an initial pass of
135 // current installed drivers
138 gBS
->SignalEvent (Event
);
143 This function creates an event using NotifyTpl, NoifyFunction, and NotifyContext.
144 This event is signaled with EfiNamedEventSignal(). This provide the ability for
145 one or more listeners on the same event named by the GUID specified by Name.
147 @param Name Supplies GUID name of the event.
148 @param NotifyTpl Supplies the task priority level of the event notifications.
149 @param NotifyFunction Supplies the function to notify when the event is signaled.
150 @param NotifyContext The context parameter to pass to NotifyFunction.
151 @param Registration A pointer to a memory location to receive the registration value.
153 @retval EFI_SUCCESS A named event was created.
154 @retval EFI_OUT_OF_RESOURCES There are not enough resource to create the named event.
159 EfiNamedEventListen (
160 IN CONST EFI_GUID
*Name
,
161 IN EFI_TPL NotifyTpl
,
162 IN EFI_EVENT_NOTIFY NotifyFunction
,
163 IN CONST VOID
*NotifyContext
, OPTIONAL
164 OUT VOID
*Registration OPTIONAL
169 VOID
*RegistrationLocal
;
174 Status
= gBS
->CreateEvent (
178 (VOID
*) NotifyContext
,
181 ASSERT_EFI_ERROR (Status
);
184 // The Registration is not optional to RegisterProtocolNotify().
185 // To make it optional to EfiNamedEventListen(), may need to substitute with a local.
187 if (Registration
!= NULL
) {
188 RegistrationLocal
= Registration
;
190 RegistrationLocal
= &RegistrationLocal
;
194 // Register for an installation of protocol interface
197 Status
= gBS
->RegisterProtocolNotify (
202 ASSERT_EFI_ERROR (Status
);
208 This function signals the named event specified by Name. The named event must
209 have been created with EfiNamedEventListen().
211 @param Name Supplies GUID name of the event.
213 @retval EFI_SUCCESS A named event was signaled.
214 @retval EFI_OUT_OF_RESOURCES There are not enough resource to signal the named event.
219 EfiNamedEventSignal (
220 IN CONST EFI_GUID
*Name
227 Status
= gBS
->InstallProtocolInterface (
230 EFI_NATIVE_INTERFACE
,
233 ASSERT_EFI_ERROR (Status
);
235 Status
= gBS
->UninstallProtocolInterface (
240 ASSERT_EFI_ERROR (Status
);
246 Returns the current TPL.
248 This function returns the current TPL. There is no EFI service to directly
249 retrieve the current TPL. Instead, the RaiseTPL() function is used to raise
250 the TPL to TPL_HIGH_LEVEL. This will return the current TPL. The TPL level
251 can then immediately be restored back to the current TPL level with a call
254 @return The current TPL.
265 Tpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
266 gBS
->RestoreTPL (Tpl
);
273 This function initializes a basic mutual exclusion lock to the released state
274 and returns the lock. Each lock provides mutual exclusion access at its task
275 priority level. Since there is no preemption or multiprocessor support in EFI,
276 acquiring the lock only consists of raising to the locks TPL.
278 @param Lock A pointer to the lock data structure to initialize.
279 @param Priority EFI TPL associated with the lock.
287 IN OUT EFI_LOCK
*Lock
,
291 ASSERT (Lock
!= NULL
);
292 ASSERT (Priority
<= TPL_HIGH_LEVEL
);
294 Lock
->Tpl
= Priority
;
295 Lock
->OwnerTpl
= TPL_APPLICATION
;
296 Lock
->Lock
= EfiLockReleased
;
301 This function raises the system's current task priority level to the task
302 priority level of the mutual exclusion lock. Then, it places the lock in the
305 @param Lock Point to EFI_LOCK instance
314 ASSERT (Lock
!= NULL
);
315 ASSERT (Lock
->Lock
== EfiLockReleased
);
317 Lock
->OwnerTpl
= gBS
->RaiseTPL (Lock
->Tpl
);
318 Lock
->Lock
= EfiLockAcquired
;
322 This function raises the system's current task priority level to the task
323 priority level of the mutual exclusion lock. Then, it attempts to place the
324 lock in the acquired state.
326 @param Lock A pointer to the lock to acquire.
328 @retval EFI_SUCCESS The lock was acquired.
329 @retval EFI_ACCESS_DENIED The lock could not be acquired because it is already owned.
334 EfiAcquireLockOrFail (
339 ASSERT (Lock
!= NULL
);
340 ASSERT (Lock
->Lock
!= EfiLockUninitialized
);
342 if (Lock
->Lock
== EfiLockAcquired
) {
344 // Lock is already owned, so bail out
346 return EFI_ACCESS_DENIED
;
349 Lock
->OwnerTpl
= gBS
->RaiseTPL (Lock
->Tpl
);
351 Lock
->Lock
= EfiLockAcquired
;
357 This function transitions a mutual exclusion lock from the acquired state to
358 the released state, and restores the system's task priority level to its
361 @param Lock A pointer to the lock to release.
372 ASSERT (Lock
!= NULL
);
373 ASSERT (Lock
->Lock
== EfiLockAcquired
);
375 Tpl
= Lock
->OwnerTpl
;
377 Lock
->Lock
= EfiLockReleased
;
379 gBS
->RestoreTPL (Tpl
);
383 Tests whether a controller handle is being managed by a specific driver.
385 This function tests whether the driver specified by DriverBindingHandle is
386 currently managing the controller specified by ControllerHandle. This test
387 is performed by evaluating if the the protocol specified by ProtocolGuid is
388 present on ControllerHandle and is was opened by DriverBindingHandle with an
389 attribute of EFI_OPEN_PROTOCOL_BY_DRIVER.
390 If ProtocolGuid is NULL, then ASSERT().
392 @param ControllerHandle A handle for a controller to test.
393 @param DriverBindingHandle Specifies the driver binding handle for the
395 @param ProtocolGuid Specifies the protocol that the driver specified
396 by DriverBindingHandle opens in its Start()
399 @retval EFI_SUCCESS ControllerHandle is managed by the driver
400 specifed by DriverBindingHandle.
401 @retval EFI_UNSUPPORTED ControllerHandle is not managed by the driver
402 specifed by DriverBindingHandle.
407 EfiTestManagedDevice (
408 IN CONST EFI_HANDLE ControllerHandle
,
409 IN CONST EFI_HANDLE DriverBindingHandle
,
410 IN CONST EFI_GUID
*ProtocolGuid
414 VOID
*ManagedInterface
;
416 ASSERT (ProtocolGuid
!= NULL
);
418 Status
= gBS
->OpenProtocol (
420 (EFI_GUID
*) ProtocolGuid
,
424 EFI_OPEN_PROTOCOL_BY_DRIVER
426 if (!EFI_ERROR (Status
)) {
429 (EFI_GUID
*) ProtocolGuid
,
433 return EFI_UNSUPPORTED
;
436 if (Status
!= EFI_ALREADY_STARTED
) {
437 return EFI_UNSUPPORTED
;
444 Tests whether a child handle is a child device of the controller.
446 This function tests whether ChildHandle is one of the children of
447 ControllerHandle. This test is performed by checking to see if the protocol
448 specified by ProtocolGuid is present on ControllerHandle and opened by
449 ChildHandle with an attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
450 If ProtocolGuid is NULL, then ASSERT().
452 @param ControllerHandle A handle for a (parent) controller to test.
453 @param ChildHandle A child handle to test.
454 @param ProtocolGuid Supplies the protocol that the child controller
455 opens on its parent controller.
457 @retval EFI_SUCCESS ChildHandle is a child of the ControllerHandle.
458 @retval EFI_UNSUPPORTED ChildHandle is not a child of the
465 IN CONST EFI_HANDLE ControllerHandle
,
466 IN CONST EFI_HANDLE ChildHandle
,
467 IN CONST EFI_GUID
*ProtocolGuid
471 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfoBuffer
;
475 ASSERT (ProtocolGuid
!= NULL
);
478 // Retrieve the list of agents that are consuming the specific protocol
479 // on ControllerHandle.
481 Status
= gBS
->OpenProtocolInformation (
483 (EFI_GUID
*) ProtocolGuid
,
487 if (EFI_ERROR (Status
)) {
488 return EFI_UNSUPPORTED
;
492 // Inspect if ChildHandle is one of the agents.
494 Status
= EFI_UNSUPPORTED
;
495 for (Index
= 0; Index
< EntryCount
; Index
++) {
496 if ((OpenInfoBuffer
[Index
].ControllerHandle
== ChildHandle
) &&
497 (OpenInfoBuffer
[Index
].Attributes
& EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
) != 0) {
498 Status
= EFI_SUCCESS
;
503 FreePool (OpenInfoBuffer
);
508 This function looks up a Unicode string in UnicodeStringTable. If Language is
509 a member of SupportedLanguages and a Unicode string is found in UnicodeStringTable
510 that matches the language code specified by Language, then it is returned in
513 @param Language A pointer to the ISO 639-2 language code for the
514 Unicode string to look up and return.
515 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes
516 that the Unicode string table supports. Language
517 must be a member of this set.
518 @param UnicodeStringTable A pointer to the table of Unicode strings.
519 @param UnicodeString A pointer to the Unicode string from UnicodeStringTable
520 that matches the language specified by Language.
522 @retval EFI_SUCCESS The Unicode string that matches the language
523 specified by Language was found
524 in the table of Unicoide strings UnicodeStringTable,
525 and it was returned in UnicodeString.
526 @retval EFI_INVALID_PARAMETER Language is NULL.
527 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
528 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
529 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.
530 @retval EFI_UNSUPPORTED The language specified by Language is not a
531 member of SupportedLanguages.
532 @retval EFI_UNSUPPORTED The language specified by Language is not
533 supported by UnicodeStringTable.
538 LookupUnicodeString (
539 IN CONST CHAR8
*Language
,
540 IN CONST CHAR8
*SupportedLanguages
,
541 IN CONST EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
,
542 OUT CHAR16
**UnicodeString
546 // Make sure the parameters are valid
548 if (Language
== NULL
|| UnicodeString
== NULL
) {
549 return EFI_INVALID_PARAMETER
;
553 // If there are no supported languages, or the Unicode String Table is empty, then the
554 // Unicode String specified by Language is not supported by this Unicode String Table
556 if (SupportedLanguages
== NULL
|| UnicodeStringTable
== NULL
) {
557 return EFI_UNSUPPORTED
;
561 // Make sure Language is in the set of Supported Languages
563 while (*SupportedLanguages
!= 0) {
564 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
567 // Search the Unicode String Table for the matching Language specifier
569 while (UnicodeStringTable
->Language
!= NULL
) {
570 if (CompareIso639LanguageCode (Language
, UnicodeStringTable
->Language
)) {
573 // A matching string was found, so return it
575 *UnicodeString
= UnicodeStringTable
->UnicodeString
;
579 UnicodeStringTable
++;
582 return EFI_UNSUPPORTED
;
585 SupportedLanguages
+= 3;
588 return EFI_UNSUPPORTED
;
592 This function adds a Unicode string to UnicodeStringTable.
593 If Language is a member of SupportedLanguages then UnicodeString is added to
594 UnicodeStringTable. New buffers are allocated for both Language and
595 UnicodeString. The contents of Language and UnicodeString are copied into
596 these new buffers. These buffers are automatically freed when
597 FreeUnicodeStringTable() is called.
599 @param Language A pointer to the ISO 639-2 language code for the Unicode
601 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes
602 that the Unicode string table supports.
603 Language must be a member of this set.
604 @param UnicodeStringTable A pointer to the table of Unicode strings.
605 @param UnicodeString A pointer to the Unicode string to add.
607 @retval EFI_SUCCESS The Unicode string that matches the language
608 specified by Language was found in the table of
609 Unicode strings UnicodeStringTable, and it was
610 returned in UnicodeString.
611 @retval EFI_INVALID_PARAMETER Language is NULL.
612 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
613 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.
614 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
615 @retval EFI_ALREADY_STARTED A Unicode string with language Language is
616 already present in UnicodeStringTable.
617 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another
618 Unicode string to UnicodeStringTable.
619 @retval EFI_UNSUPPORTED The language specified by Language is not a
620 member of SupportedLanguages.
626 IN CONST CHAR8
*Language
,
627 IN CONST CHAR8
*SupportedLanguages
,
628 IN EFI_UNICODE_STRING_TABLE
**UnicodeStringTable
,
629 IN CONST CHAR16
*UnicodeString
632 UINTN NumberOfEntries
;
633 EFI_UNICODE_STRING_TABLE
*OldUnicodeStringTable
;
634 EFI_UNICODE_STRING_TABLE
*NewUnicodeStringTable
;
635 UINTN UnicodeStringLength
;
638 // Make sure the parameter are valid
640 if (Language
== NULL
|| UnicodeString
== NULL
|| UnicodeStringTable
== NULL
) {
641 return EFI_INVALID_PARAMETER
;
645 // If there are no supported languages, then a Unicode String can not be added
647 if (SupportedLanguages
== NULL
) {
648 return EFI_UNSUPPORTED
;
652 // If the Unicode String is empty, then a Unicode String can not be added
654 if (UnicodeString
[0] == 0) {
655 return EFI_INVALID_PARAMETER
;
659 // Make sure Language is a member of SupportedLanguages
661 while (*SupportedLanguages
!= 0) {
662 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
665 // Determine the size of the Unicode String Table by looking for a NULL Language entry
668 if (*UnicodeStringTable
!= NULL
) {
669 OldUnicodeStringTable
= *UnicodeStringTable
;
670 while (OldUnicodeStringTable
->Language
!= NULL
) {
671 if (CompareIso639LanguageCode (Language
, OldUnicodeStringTable
->Language
)) {
672 return EFI_ALREADY_STARTED
;
675 OldUnicodeStringTable
++;
681 // Allocate space for a new Unicode String Table. It must hold the current number of
682 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table
685 NewUnicodeStringTable
= AllocatePool ((NumberOfEntries
+ 2) * sizeof (EFI_UNICODE_STRING_TABLE
));
686 if (NewUnicodeStringTable
== NULL
) {
687 return EFI_OUT_OF_RESOURCES
;
691 // If the current Unicode String Table contains any entries, then copy them to the
692 // newly allocated Unicode String Table.
694 if (*UnicodeStringTable
!= NULL
) {
696 NewUnicodeStringTable
,
698 NumberOfEntries
* sizeof (EFI_UNICODE_STRING_TABLE
)
703 // Allocate space for a copy of the Language specifier
705 NewUnicodeStringTable
[NumberOfEntries
].Language
= AllocateCopyPool (3, Language
);
706 if (NewUnicodeStringTable
[NumberOfEntries
].Language
== NULL
) {
707 gBS
->FreePool (NewUnicodeStringTable
);
708 return EFI_OUT_OF_RESOURCES
;
712 // Compute the length of the Unicode String
714 for (UnicodeStringLength
= 0; UnicodeString
[UnicodeStringLength
] != 0; UnicodeStringLength
++)
718 // Allocate space for a copy of the Unicode String
720 NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
= AllocateCopyPool (
721 (UnicodeStringLength
+ 1) * sizeof (CHAR16
),
724 if (NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
== NULL
) {
725 gBS
->FreePool (NewUnicodeStringTable
[NumberOfEntries
].Language
);
726 gBS
->FreePool (NewUnicodeStringTable
);
727 return EFI_OUT_OF_RESOURCES
;
731 // Mark the end of the Unicode String Table
733 NewUnicodeStringTable
[NumberOfEntries
+ 1].Language
= NULL
;
734 NewUnicodeStringTable
[NumberOfEntries
+ 1].UnicodeString
= NULL
;
737 // Free the old Unicode String Table
739 if (*UnicodeStringTable
!= NULL
) {
740 gBS
->FreePool (*UnicodeStringTable
);
744 // Point UnicodeStringTable at the newly allocated Unicode String Table
746 *UnicodeStringTable
= NewUnicodeStringTable
;
751 SupportedLanguages
+= 3;
754 return EFI_UNSUPPORTED
;
758 This function frees the table of Unicode strings in UnicodeStringTable.
759 If UnicodeStringTable is NULL, then EFI_SUCCESS is returned.
760 Otherwise, each language code, and each Unicode string in the Unicode string
761 table are freed, and EFI_SUCCESS is returned.
763 @param UnicodeStringTable A pointer to the table of Unicode strings.
765 @retval EFI_SUCCESS The Unicode string table was freed.
770 FreeUnicodeStringTable (
771 IN EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
777 // If the Unicode String Table is NULL, then it is already freed
779 if (UnicodeStringTable
== NULL
) {
784 // Loop through the Unicode String Table until we reach the end of table marker
786 for (Index
= 0; UnicodeStringTable
[Index
].Language
!= NULL
; Index
++) {
789 // Free the Language string from the Unicode String Table
791 gBS
->FreePool (UnicodeStringTable
[Index
].Language
);
794 // Free the Unicode String from the Unicode String Table
796 if (UnicodeStringTable
[Index
].UnicodeString
!= NULL
) {
797 gBS
->FreePool (UnicodeStringTable
[Index
].UnicodeString
);
802 // Free the Unicode String Table itself
804 gBS
->FreePool (UnicodeStringTable
);