3 Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "EdkIIGlueUefi.h"
16 #include "Library/EdkIIGlueMemoryAllocationLib.h"
20 This function searches the list of configuration tables stored in the EFI System
21 Table for a table with a GUID that matches TableGuid. If a match is found,
22 then a pointer to the configuration table is returned in Table, and EFI_SUCCESS
23 is returned. If a matching GUID is not found, then EFI_NOT_FOUND is returned.
25 @param TableGuid Pointer to table's GUID type..
26 @param Table Pointer to the table associated with TableGuid in the EFI System Table.
28 @retval EFI_SUCCESS A configuration table matching TableGuid was found.
29 @retval EFI_NOT_FOUND A configuration table matching TableGuid could not be found.
34 EfiGetSystemConfigurationTable (
35 IN EFI_GUID
*TableGuid
,
39 EFI_SYSTEM_TABLE
*SystemTable
;
42 ASSERT (TableGuid
!= NULL
);
43 ASSERT (Table
!= NULL
);
47 for (Index
= 0; Index
< SystemTable
->NumberOfTableEntries
; Index
++) {
48 if (CompareGuid (TableGuid
, &(SystemTable
->ConfigurationTable
[Index
].VendorGuid
))) {
49 *Table
= SystemTable
->ConfigurationTable
[Index
].VendorTable
;
58 This function causes the notification function to be executed for every protocol
59 of type ProtocolGuid instance that exists in the system when this function is
60 invoked. In addition, every time a protocol of type ProtocolGuid instance is
61 installed or reinstalled, the notification function is also executed.
63 @param ProtocolGuid Supplies GUID of the protocol upon whose installation the event is fired.
64 @param NotifyTpl Supplies the task priority level of the event notifications.
65 @param NotifyFunction Supplies the function to notify when the event is signaled.
66 @param NotifyContext The context parameter to pass to NotifyFunction.
67 @param Registration A pointer to a memory location to receive the registration value.
69 @return The notification event that was created.
74 EfiCreateProtocolNotifyEvent(
75 IN EFI_GUID
*ProtocolGuid
,
77 IN EFI_EVENT_NOTIFY NotifyFunction
,
78 IN VOID
*NotifyContext
, OPTIONAL
79 OUT VOID
**Registration
89 Status
= gBS
->CreateEvent (
90 EFI_EVENT_NOTIFY_SIGNAL
,
96 ASSERT_EFI_ERROR (Status
);
99 // Register for protocol notifactions on this event
102 Status
= gBS
->RegisterProtocolNotify (
108 ASSERT_EFI_ERROR (Status
);
111 // Kick the event so we will perform an initial pass of
112 // current installed drivers
115 gBS
->SignalEvent (Event
);
120 This function creates an event using NotifyTpl, NoifyFunction, and NotifyContext.
121 This event is signaled with EfiNamedEventSignal(). This provide the ability for
122 one or more listeners on the same event named by the GUID specified by Name.
124 @param Name Supplies GUID name of the event.
125 @param NotifyTpl Supplies the task priority level of the event notifications.
126 @param NotifyFunction Supplies the function to notify when the event is signaled.
127 @param NotifyContext The context parameter to pass to NotifyFunction.
128 @param Registration A pointer to a memory location to receive the registration value.
130 @retval EFI_SUCCESS A named event was created.
131 @retval EFI_OUT_OF_RESOURCES There are not enough resource to create the named event.
136 EfiNamedEventListen (
137 IN CONST EFI_GUID
*Name
,
138 IN EFI_TPL NotifyTpl
,
139 IN EFI_EVENT_NOTIFY NotifyFunction
,
140 IN CONST VOID
*NotifyContext
, OPTIONAL
141 OUT VOID
*Registration OPTIONAL
146 VOID
*RegistrationLocal
;
151 Status
= gBS
->CreateEvent (
152 EFI_EVENT_NOTIFY_SIGNAL
,
155 (VOID
*) NotifyContext
,
158 ASSERT_EFI_ERROR (Status
);
161 // The Registration is not optional to RegisterProtocolNotify().
162 // To make it optional to EfiNamedEventListen(), may need to substitute with a local.
164 if (Registration
!= NULL
) {
165 RegistrationLocal
= Registration
;
167 RegistrationLocal
= &RegistrationLocal
;
171 // Register for an installation of protocol interface
174 Status
= gBS
->RegisterProtocolNotify (
179 ASSERT_EFI_ERROR (Status
);
185 This function signals the named event specified by Name. The named event must
186 have been created with EfiNamedEventListen().
188 @param Name Supplies GUID name of the event.
190 @retval EFI_SUCCESS A named event was signaled.
191 @retval EFI_OUT_OF_RESOURCES There are not enough resource to signal the named event.
196 EfiNamedEventSignal (
197 IN CONST EFI_GUID
*Name
204 Status
= gBS
->InstallProtocolInterface (
207 EFI_NATIVE_INTERFACE
,
210 ASSERT_EFI_ERROR (Status
);
212 Status
= gBS
->UninstallProtocolInterface (
217 ASSERT_EFI_ERROR (Status
);
223 Returns the current TPL.
225 This function returns the current TPL. There is no EFI service to directly
226 retrieve the current TPL. Instead, the RaiseTPL() function is used to raise
227 the TPL to TPL_HIGH_LEVEL. This will return the current TPL. The TPL level
228 can then immediately be restored back to the current TPL level with a call
233 @retvale EFI_TPL The current TPL.
244 Tpl
= gBS
->RaiseTPL (EFI_TPL_HIGH_LEVEL
);
245 gBS
->RestoreTPL (Tpl
);
252 This function initializes a basic mutual exclusion lock to the released state
253 and returns the lock. Each lock provides mutual exclusion access at its task
254 priority level. Since there is no preemption or multiprocessor support in EFI,
255 acquiring the lock only consists of raising to the locks TPL.
257 @param Lock A pointer to the lock data structure to initialize.
258 @param Priority EFI TPL associated with the lock.
265 GlueEfiInitializeLock (
266 IN OUT EFI_LOCK
*Lock
,
270 ASSERT (Lock
!= NULL
);
271 ASSERT (Priority
<= EFI_TPL_HIGH_LEVEL
);
273 Lock
->Tpl
= Priority
;
274 Lock
->OwnerTpl
= EFI_TPL_APPLICATION
;
275 Lock
->Lock
= EfiLockReleased
;
280 This function raises the system's current task priority level to the task
281 priority level of the mutual exclusion lock. Then, it places the lock in the
284 @param Priority The task priority level of the lock.
293 ASSERT (Lock
!= NULL
);
294 ASSERT (Lock
->Lock
== EfiLockReleased
);
296 Lock
->OwnerTpl
= gBS
->RaiseTPL (Lock
->Tpl
);
297 Lock
->Lock
= EfiLockAcquired
;
301 This function raises the system's current task priority level to the task
302 priority level of the mutual exclusion lock. Then, it attempts to place the
303 lock in the acquired state.
305 @param Lock A pointer to the lock to acquire.
307 @retval EFI_SUCCESS The lock was acquired.
308 @retval EFI_ACCESS_DENIED The lock could not be acquired because it is already owned.
313 GlueEfiAcquireLockOrFail (
318 ASSERT (Lock
!= NULL
);
319 ASSERT (Lock
->Lock
!= EfiLockUninitialized
);
321 if (Lock
->Lock
== EfiLockAcquired
) {
323 // Lock is already owned, so bail out
325 return EFI_ACCESS_DENIED
;
328 Lock
->OwnerTpl
= gBS
->RaiseTPL (Lock
->Tpl
);
330 Lock
->Lock
= EfiLockAcquired
;
336 This function transitions a mutual exclusion lock from the acquired state to
337 the released state, and restores the system's task priority level to its
340 @param Lock A pointer to the lock to release.
351 ASSERT (Lock
!= NULL
);
352 ASSERT (Lock
->Lock
== EfiLockAcquired
);
354 Tpl
= Lock
->OwnerTpl
;
356 Lock
->Lock
= EfiLockReleased
;
358 gBS
->RestoreTPL (Tpl
);
362 Tests whether a controller handle is being managed by a specific driver.
364 This function tests whether the driver specified by DriverBindingHandle is
365 currently managing the controller specified by ControllerHandle. This test
366 is performed by evaluating if the the protocol specified by ProtocolGuid is
367 present on ControllerHandle and is was opened by DriverBindingHandle with an
368 attribute of EFI_OPEN_PROTOCOL_BY_DRIVER.
369 If ProtocolGuid is NULL, then ASSERT().
371 @param ControllerHandle A handle for a controller to test.
372 @param DriverBindingHandle Specifies the driver binding handle for the
374 @param ProtocolGuid Specifies the protocol that the driver specified
375 by DriverBindingHandle opens in its Start()
378 @retval EFI_SUCCESS ControllerHandle is managed by the driver
379 specifed by DriverBindingHandle.
380 @retval EFI_UNSUPPORTED ControllerHandle is not managed by the driver
381 specifed by DriverBindingHandle.
386 EfiTestManagedDevice (
387 IN CONST EFI_HANDLE ControllerHandle
,
388 IN CONST EFI_HANDLE DriverBindingHandle
,
389 IN CONST EFI_GUID
*ProtocolGuid
393 VOID
*ManagedInterface
;
395 ASSERT (ProtocolGuid
!= NULL
);
397 Status
= gBS
->OpenProtocol (
399 (EFI_GUID
*) ProtocolGuid
,
403 EFI_OPEN_PROTOCOL_BY_DRIVER
405 if (!EFI_ERROR (Status
)) {
408 (EFI_GUID
*) ProtocolGuid
,
412 return EFI_UNSUPPORTED
;
415 if (Status
!= EFI_ALREADY_STARTED
) {
416 return EFI_UNSUPPORTED
;
423 Tests whether a child handle is a child device of the controller.
425 This function tests whether ChildHandle is one of the children of
426 ControllerHandle. This test is performed by checking to see if the protocol
427 specified by ProtocolGuid is present on ControllerHandle and opened by
428 ChildHandle with an attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
429 If ProtocolGuid is NULL, then ASSERT().
431 @param ControllerHandle A handle for a (parent) controller to test.
432 @param ChildHandle A child handle to test.
433 @param ConsumsedGuid Supplies the protocol that the child controller
434 opens on its parent controller.
436 @retval EFI_SUCCESS ChildHandle is a child of the ControllerHandle.
437 @retval EFI_UNSUPPORTED ChildHandle is not a child of the
444 IN CONST EFI_HANDLE ControllerHandle
,
445 IN CONST EFI_HANDLE ChildHandle
,
446 IN CONST EFI_GUID
*ProtocolGuid
450 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfoBuffer
;
454 ASSERT (ProtocolGuid
!= NULL
);
457 // Retrieve the list of agents that are consuming the specific protocol
458 // on ControllerHandle.
460 Status
= gBS
->OpenProtocolInformation (
462 (EFI_GUID
*) ProtocolGuid
,
466 if (EFI_ERROR (Status
)) {
467 return EFI_UNSUPPORTED
;
471 // Inspect if ChildHandle is one of the agents.
473 Status
= EFI_UNSUPPORTED
;
474 for (Index
= 0; Index
< EntryCount
; Index
++) {
475 if ((OpenInfoBuffer
[Index
].ControllerHandle
== ChildHandle
) &&
476 (OpenInfoBuffer
[Index
].Attributes
& EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
) != 0) {
477 Status
= EFI_SUCCESS
;
482 FreePool (OpenInfoBuffer
);
487 Tests whether a language code has format of ISO639-2.
489 @param Languages The language code to be tested.
491 @retval TRUE Language code format is ISO 639-2.
492 @retval FALSE Language code format is not ISO639-2.
497 IsIso639LanguageCode (
498 IN CONST CHAR8
*Languages
504 // Find out format of Languages
506 for (Index
= 0; Languages
[Index
] != 0 && Languages
[Index
] != ';' && Languages
[Index
] != '-'; Index
++);
507 if (Languages
[Index
] != 0) {
509 // RFC4646 language code
515 // No ';' and '-', it's either ISO639-2 code (list) or single RFC4646 code
519 // Single RFC4646 language code without country code, e.g. "en"
525 // Languages in format of ISO639-2
531 Compare the first language instance of two language codes, either could be a
532 single language code or a language code list. This function assume Language1
533 and Language2 has the same language code format, i.e. either ISO639-2 or RFC4646.
535 @param Language1 The first language code to be tested.
536 @param Language2 The second language code to be tested.
538 @retval TRUE Language code match.
539 @retval FALSE Language code mismatch.
544 CompareLanguageCode (
545 IN CONST CHAR8
*Language1
,
546 IN CONST CHAR8
*Language2
552 // Compare first two bytes of language tag
554 if ((Language1
[0] != Language2
[0]) || (Language1
[1] != Language2
[1])) {
558 if (IsIso639LanguageCode (Language1
)) {
560 // ISO639-2 language code, compare the third byte of language tag
562 return (BOOLEAN
) ((Language1
[2] == Language2
[2]) ? TRUE
: FALSE
);
566 // RFC4646 language code
568 for (Index
= 0; Language1
[Index
] != 0 && Language1
[Index
] != ';'; Index
++);
569 if ((AsciiStrnCmp (Language1
, Language2
, Index
) == 0) && (Language2
[Index
] == 0 || Language2
[Index
] == ';')) {
577 Step to next language code of a language code list.
579 @param Languages The language code list to traverse.
581 @return Pointer to next language code or NULL terminator if it's the last one.
587 NextSupportedLanguage (
588 IN CONST CHAR8
*Languages
593 if (IsIso639LanguageCode (Languages
)) {
595 // ISO639-2 language code
597 return (Languages
+ 3);
601 // Search in RFC4646 language code list
603 for (Index
= 0; Languages
[Index
] != 0 && Languages
[Index
] != ';'; Index
++);
604 if (Languages
[Index
] == ';') {
607 return (Languages
+ Index
);
611 This function looks up a Unicode string in UnicodeStringTable. If Language is
612 a member of SupportedLanguages and a Unicode string is found in UnicodeStringTable
613 that matches the language code specified by Language, then it is returned in
616 @param Language A pointer to the ISO 639-2 language code for the
617 Unicode string to look up and return.
618 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes
619 that the Unicode string table supports. Language
620 must be a member of this set.
621 @param UnicodeStringTable A pointer to the table of Unicode strings.
622 @param UnicodeString A pointer to the Unicode string from UnicodeStringTable
623 that matches the language specified by Language.
625 @retval EFI_SUCCESS The Unicode string that matches the language
626 specified by Language was found
627 in the table of Unicoide strings UnicodeStringTable,
628 and it was returned in UnicodeString.
629 @retval EFI_INVALID_PARAMETER Language is NULL.
630 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
631 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
632 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.
633 @retval EFI_UNSUPPORTED The language specified by Language is not a
634 member of SupportedLanguages.
635 @retval EFI_UNSUPPORTED The language specified by Language is not
636 supported by UnicodeStringTable.
641 LookupUnicodeString (
642 IN CONST CHAR8
*Language
,
643 IN CONST CHAR8
*SupportedLanguages
,
644 IN CONST EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
,
645 OUT CHAR16
**UnicodeString
649 // Make sure the parameters are valid
651 if (Language
== NULL
|| UnicodeString
== NULL
) {
652 return EFI_INVALID_PARAMETER
;
656 // If there are no supported languages, or the Unicode String Table is empty, then the
657 // Unicode String specified by Language is not supported by this Unicode String Table
659 if (SupportedLanguages
== NULL
|| UnicodeStringTable
== NULL
) {
660 return EFI_UNSUPPORTED
;
664 // Make sure Language is in the set of Supported Languages
666 while (*SupportedLanguages
!= 0) {
667 if (CompareLanguageCode (Language
, SupportedLanguages
)) {
670 // Search the Unicode String Table for the matching Language specifier
672 while (UnicodeStringTable
->Language
!= NULL
) {
673 if (CompareLanguageCode (Language
, UnicodeStringTable
->Language
)) {
676 // A matching string was found, so return it
678 *UnicodeString
= UnicodeStringTable
->UnicodeString
;
682 UnicodeStringTable
++;
685 return EFI_UNSUPPORTED
;
688 SupportedLanguages
= NextSupportedLanguage (SupportedLanguages
);
691 return EFI_UNSUPPORTED
;
695 This function adds a Unicode string to UnicodeStringTable.
696 If Language is a member of SupportedLanguages then UnicodeString is added to
697 UnicodeStringTable. New buffers are allocated for both Language and
698 UnicodeString. The contents of Language and UnicodeString are copied into
699 these new buffers. These buffers are automatically freed when
700 FreeUnicodeStringTable() is called.
702 @param Language A pointer to the ISO 639-2 language code for the Unicode
704 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes
705 that the Unicode string table supports.
706 Language must be a member of this set.
707 @param UnicodeStringTable A pointer to the table of Unicode strings.
708 @param UnicodeString A pointer to the Unicode string to add.
710 @retval EFI_SUCCESS The Unicode string that matches the language
711 specified by Language was found in the table of
712 Unicode strings UnicodeStringTable, and it was
713 returned in UnicodeString.
714 @retval EFI_INVALID_PARAMETER Language is NULL.
715 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
716 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.
717 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
718 @retval EFI_ALREADY_STARTED A Unicode string with language Language is
719 already present in UnicodeStringTable.
720 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another
721 Unicode string to UnicodeStringTable.
722 @retval EFI_UNSUPPORTED The language specified by Language is not a
723 member of SupportedLanguages.
729 IN CONST CHAR8
*Language
,
730 IN CONST CHAR8
*SupportedLanguages
,
731 IN EFI_UNICODE_STRING_TABLE
**UnicodeStringTable
,
732 IN CONST CHAR16
*UnicodeString
735 UINTN NumberOfEntries
;
736 EFI_UNICODE_STRING_TABLE
*OldUnicodeStringTable
;
737 EFI_UNICODE_STRING_TABLE
*NewUnicodeStringTable
;
738 UINTN UnicodeStringLength
;
741 // Make sure the parameter are valid
743 if (Language
== NULL
|| UnicodeString
== NULL
|| UnicodeStringTable
== NULL
) {
744 return EFI_INVALID_PARAMETER
;
748 // If there are no supported languages, then a Unicode String can not be added
750 if (SupportedLanguages
== NULL
) {
751 return EFI_UNSUPPORTED
;
755 // If the Unicode String is empty, then a Unicode String can not be added
757 if (UnicodeString
[0] == 0) {
758 return EFI_INVALID_PARAMETER
;
762 // Make sure Language is a member of SupportedLanguages
764 while (*SupportedLanguages
!= 0) {
765 if (CompareLanguageCode (Language
, SupportedLanguages
)) {
768 // Determine the size of the Unicode String Table by looking for a NULL Language entry
771 if (*UnicodeStringTable
!= NULL
) {
772 OldUnicodeStringTable
= *UnicodeStringTable
;
773 while (OldUnicodeStringTable
->Language
!= NULL
) {
774 if (CompareLanguageCode (Language
, OldUnicodeStringTable
->Language
)) {
775 return EFI_ALREADY_STARTED
;
778 OldUnicodeStringTable
++;
784 // Allocate space for a new Unicode String Table. It must hold the current number of
785 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table
788 NewUnicodeStringTable
= AllocatePool ((NumberOfEntries
+ 2) * sizeof (EFI_UNICODE_STRING_TABLE
));
789 if (NewUnicodeStringTable
== NULL
) {
790 return EFI_OUT_OF_RESOURCES
;
794 // If the current Unicode String Table contains any entries, then copy them to the
795 // newly allocated Unicode String Table.
797 if (*UnicodeStringTable
!= NULL
) {
799 NewUnicodeStringTable
,
801 NumberOfEntries
* sizeof (EFI_UNICODE_STRING_TABLE
)
806 // Allocate space for a copy of the Language specifier
808 NewUnicodeStringTable
[NumberOfEntries
].Language
= AllocateCopyPool (AsciiStrSize (Language
), Language
);
809 if (NewUnicodeStringTable
[NumberOfEntries
].Language
== NULL
) {
810 (gBS
->FreePool
) (NewUnicodeStringTable
);
811 return EFI_OUT_OF_RESOURCES
;
815 // Compute the length of the Unicode String
817 for (UnicodeStringLength
= 0; UnicodeString
[UnicodeStringLength
] != 0; UnicodeStringLength
++)
821 // Allocate space for a copy of the Unicode String
823 NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
= AllocateCopyPool (
824 (UnicodeStringLength
+ 1) * sizeof (CHAR16
),
827 if (NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
== NULL
) {
828 (gBS
->FreePool
) (NewUnicodeStringTable
[NumberOfEntries
].Language
);
829 (gBS
->FreePool
) (NewUnicodeStringTable
);
830 return EFI_OUT_OF_RESOURCES
;
834 // Mark the end of the Unicode String Table
836 NewUnicodeStringTable
[NumberOfEntries
+ 1].Language
= NULL
;
837 NewUnicodeStringTable
[NumberOfEntries
+ 1].UnicodeString
= NULL
;
840 // Free the old Unicode String Table
842 if (*UnicodeStringTable
!= NULL
) {
843 (gBS
->FreePool
) (*UnicodeStringTable
);
847 // Point UnicodeStringTable at the newly allocated Unicode String Table
849 *UnicodeStringTable
= NewUnicodeStringTable
;
854 SupportedLanguages
= NextSupportedLanguage (SupportedLanguages
);
857 return EFI_UNSUPPORTED
;
861 This function frees the table of Unicode strings in UnicodeStringTable.
862 If UnicodeStringTable is NULL, then EFI_SUCCESS is returned.
863 Otherwise, each language code, and each Unicode string in the Unicode string
864 table are freed, and EFI_SUCCESS is returned.
866 @param UnicodeStringTable A pointer to the table of Unicode strings.
868 @retval EFI_SUCCESS The Unicode string table was freed.
873 FreeUnicodeStringTable (
874 IN EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
880 // If the Unicode String Table is NULL, then it is already freed
882 if (UnicodeStringTable
== NULL
) {
887 // Loop through the Unicode String Table until we reach the end of table marker
889 for (Index
= 0; UnicodeStringTable
[Index
].Language
!= NULL
; Index
++) {
892 // Free the Language string from the Unicode String Table
894 (gBS
->FreePool
) (UnicodeStringTable
[Index
].Language
);
897 // Free the Unicode String from the Unicode String Table
899 if (UnicodeStringTable
[Index
].UnicodeString
!= NULL
) {
900 (gBS
->FreePool
) (UnicodeStringTable
[Index
].UnicodeString
);
905 // Free the Unicode String Table itself
907 (gBS
->FreePool
) (UnicodeStringTable
);