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
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
33 return (BOOLEAN
) (ReadUnaligned24 ((CONST UINT32
*) Language1
) == ReadUnaligned24 ((CONST UINT32
*) Language2
));
37 This function searches the list of configuration tables stored in the EFI System
38 Table for a table with a GUID that matches TableGuid. If a match is found,
39 then a pointer to the configuration table is returned in Table, and EFI_SUCCESS
40 is returned. If a matching GUID is not found, then EFI_NOT_FOUND is returned.
42 @param TableGuid Pointer to table's GUID type..
43 @param Table Pointer to the table associated with TableGuid in the EFI System Table.
45 @retval EFI_SUCCESS A configuration table matching TableGuid was found.
46 @retval EFI_NOT_FOUND A configuration table matching TableGuid could not be found.
51 EfiGetSystemConfigurationTable (
52 IN EFI_GUID
*TableGuid
,
56 EFI_SYSTEM_TABLE
*SystemTable
;
59 ASSERT (TableGuid
!= NULL
);
60 ASSERT (Table
!= NULL
);
64 for (Index
= 0; Index
< SystemTable
->NumberOfTableEntries
; Index
++) {
65 if (CompareGuid (TableGuid
, &(SystemTable
->ConfigurationTable
[Index
].VendorGuid
))) {
66 *Table
= SystemTable
->ConfigurationTable
[Index
].VendorTable
;
75 This function causes the notification function to be executed for every protocol
76 of type ProtocolGuid instance that exists in the system when this function is
77 invoked. In addition, every time a protocol of type ProtocolGuid instance is
78 installed or reinstalled, the notification function is also executed.
80 @param ProtocolGuid Supplies GUID of the protocol upon whose installation the event is fired.
81 @param NotifyTpl Supplies the task priority level of the event notifications.
82 @param NotifyFunction Supplies the function to notify when the event is signaled.
83 @param NotifyContext The context parameter to pass to NotifyFunction.
84 @param Registration A pointer to a memory location to receive the registration value.
86 @return The notification event that was created.
91 EfiCreateProtocolNotifyEvent(
92 IN EFI_GUID
*ProtocolGuid
,
94 IN EFI_EVENT_NOTIFY NotifyFunction
,
95 IN VOID
*NotifyContext
, OPTIONAL
96 OUT VOID
**Registration
106 Status
= gBS
->CreateEvent (
107 EFI_EVENT_NOTIFY_SIGNAL
,
113 ASSERT_EFI_ERROR (Status
);
116 // Register for protocol notifactions on this event
119 Status
= gBS
->RegisterProtocolNotify (
125 ASSERT_EFI_ERROR (Status
);
128 // Kick the event so we will perform an initial pass of
129 // current installed drivers
132 gBS
->SignalEvent (Event
);
137 This function creates an event using NotifyTpl, NoifyFunction, and NotifyContext.
138 This event is signaled with EfiNamedEventSignal(). This provide the ability for
139 one or more listeners on the same event named by the GUID specified by Name.
141 @param Name Supplies GUID name of the event.
142 @param NotifyTpl Supplies the task priority level of the event notifications.
143 @param NotifyFunction Supplies the function to notify when the event is signaled.
144 @param NotifyContext The context parameter to pass to NotifyFunction.
145 @param Registration A pointer to a memory location to receive the registration value.
147 @retval EFI_SUCCESS A named event was created.
148 @retval EFI_OUT_OF_RESOURCES There are not enough resource to create the named event.
153 EfiNamedEventListen (
154 IN CONST EFI_GUID
*Name
,
155 IN EFI_TPL NotifyTpl
,
156 IN EFI_EVENT_NOTIFY NotifyFunction
,
157 IN CONST VOID
*NotifyContext
, OPTIONAL
158 OUT VOID
*Registration OPTIONAL
163 VOID
*RegistrationLocal
;
168 Status
= gBS
->CreateEvent (
169 EFI_EVENT_NOTIFY_SIGNAL
,
172 (VOID
*) NotifyContext
,
175 ASSERT_EFI_ERROR (Status
);
178 // The Registration is not optional to RegisterProtocolNotify().
179 // To make it optional to EfiNamedEventListen(), may need to substitute with a local.
181 if (Registration
!= NULL
) {
182 RegistrationLocal
= Registration
;
184 RegistrationLocal
= &RegistrationLocal
;
188 // Register for an installation of protocol interface
191 Status
= gBS
->RegisterProtocolNotify (
196 ASSERT_EFI_ERROR (Status
);
202 This function signals the named event specified by Name. The named event must
203 have been created with EfiNamedEventListen().
205 @param Name Supplies GUID name of the event.
207 @retval EFI_SUCCESS A named event was signaled.
208 @retval EFI_OUT_OF_RESOURCES There are not enough resource to signal the named event.
213 EfiNamedEventSignal (
214 IN CONST EFI_GUID
*Name
221 Status
= gBS
->InstallProtocolInterface (
224 EFI_NATIVE_INTERFACE
,
227 ASSERT_EFI_ERROR (Status
);
229 Status
= gBS
->UninstallProtocolInterface (
234 ASSERT_EFI_ERROR (Status
);
241 This function initializes a basic mutual exclusion lock to the released state
242 and returns the lock. Each lock provides mutual exclusion access at its task
243 priority level. Since there is no preemption or multiprocessor support in EFI,
244 acquiring the lock only consists of raising to the locks TPL.
246 @param Lock A pointer to the lock data structure to initialize.
247 @param Priority EFI TPL associated with the lock.
255 IN OUT EFI_LOCK
*Lock
,
259 ASSERT (Lock
!= NULL
);
260 ASSERT (Priority
<= EFI_TPL_HIGH_LEVEL
);
262 Lock
->Tpl
= Priority
;
263 Lock
->OwnerTpl
= EFI_TPL_APPLICATION
;
264 Lock
->Lock
= EfiLockReleased
;
269 This function raises the system's current task priority level to the task
270 priority level of the mutual exclusion lock. Then, it places the lock in the
273 @param Priority The task priority level of the lock.
282 ASSERT (Lock
!= NULL
);
283 ASSERT (Lock
->Lock
== EfiLockReleased
);
285 Lock
->OwnerTpl
= gBS
->RaiseTPL (Lock
->Tpl
);
286 Lock
->Lock
= EfiLockAcquired
;
290 This function raises the system's current task priority level to the task
291 priority level of the mutual exclusion lock. Then, it attempts to place the
292 lock in the acquired state.
294 @param Lock A pointer to the lock to acquire.
296 @retval EFI_SUCCESS The lock was acquired.
297 @retval EFI_ACCESS_DENIED The lock could not be acquired because it is already owned.
302 EfiAcquireLockOrFail (
307 ASSERT (Lock
!= NULL
);
308 ASSERT (Lock
->Lock
!= EfiLockUninitialized
);
310 if (Lock
->Lock
== EfiLockAcquired
) {
312 // Lock is already owned, so bail out
314 return EFI_ACCESS_DENIED
;
317 Lock
->OwnerTpl
= gBS
->RaiseTPL (Lock
->Tpl
);
319 Lock
->Lock
= EfiLockAcquired
;
325 This function transitions a mutual exclusion lock from the acquired state to
326 the released state, and restores the system's task priority level to its
329 @param Lock A pointer to the lock to release.
340 ASSERT (Lock
!= NULL
);
341 ASSERT (Lock
->Lock
== EfiLockAcquired
);
343 Tpl
= Lock
->OwnerTpl
;
345 Lock
->Lock
= EfiLockReleased
;
347 gBS
->RestoreTPL (Tpl
);
351 Tests whether a controller is managed by a specific driver.
353 This function tests whether a specific driver manages ControllerHandle by
354 opening on DriverBindingHandle a protocol specified by ProtocolGuid with
355 attribute EFI_OPEN_PROTOCOL_BY_DRIVER. This library function is used to
356 implement the Component Name Protocol for EFI Drivers.
357 If ProtocolGuid is NULL, then ASSERT().
359 @param ControllerHandle A handle for a controller to test.
360 @param DriverBindingHandle Specifies the driver binding handle for the
362 @param ProtocolGuid Supplies GUID for the protocol opened by the
363 driver on the controller.
365 @retval EFI_SUCCESS ControllerHandle is managed by the specific
367 @retval EFI_UNSUPPORTED ControllerHandle is not managed by the specific
373 EfiTestManagedDevice (
374 IN CONST EFI_HANDLE ControllerHandle
,
375 IN CONST EFI_HANDLE DriverBindingHandle
,
376 IN CONST EFI_GUID
*ProtocolGuid
380 VOID
*ManagedInterface
;
382 ASSERT (ProtocolGuid
!= NULL
);
384 Status
= gBS
->OpenProtocol (
386 (EFI_GUID
*) ProtocolGuid
,
390 EFI_OPEN_PROTOCOL_BY_DRIVER
392 if (!EFI_ERROR (Status
)) {
395 (EFI_GUID
*) ProtocolGuid
,
399 return EFI_UNSUPPORTED
;
402 if (Status
!= EFI_ALREADY_STARTED
) {
403 return EFI_UNSUPPORTED
;
410 Tests whether a child handle is a children device of the controller.
412 This function tests whether ChildHandle is one of the children of
413 ControllerHandle which are consuming a protocol specified by ProtocolGuid
414 with the attribute bit EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER set. This
415 library function is used to implement the Component Name Protocol for EFI
417 If ProtocolGuid is NULL, then ASSERT().
419 @param ControllerHandle A handle for a (parent) controller to test.
420 @param ChildHandle A child handle to test.
421 @param ConsumsedGuid Supplies GUID for the protocol consumed by
422 children from controller.
424 @retval EFI_SUCCESS ChildHandle is a child of the ControllerHandle.
425 @retval EFI_UNSUPPORTED ChildHandle is not a child of the
432 IN CONST EFI_HANDLE ControllerHandle
,
433 IN CONST EFI_HANDLE ChildHandle
,
434 IN CONST EFI_GUID
*ProtocolGuid
438 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfoBuffer
;
442 ASSERT (ProtocolGuid
!= NULL
);
445 // Retrieve the list of agents that are consuming the specific protocol
446 // on ControllerHandle.
448 Status
= gBS
->OpenProtocolInformation (
450 (EFI_GUID
*) ProtocolGuid
,
454 if (EFI_ERROR (Status
)) {
455 return EFI_UNSUPPORTED
;
459 // Inspect if ChildHandle is one of the agents.
461 Status
= EFI_UNSUPPORTED
;
462 for (Index
= 0; Index
< EntryCount
; Index
++) {
463 if ((OpenInfoBuffer
[Index
].ControllerHandle
== ChildHandle
) &&
464 (OpenInfoBuffer
[Index
].Attributes
& EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
) != 0) {
465 Status
= EFI_SUCCESS
;
470 FreePool (OpenInfoBuffer
);
475 This function looks up a Unicode string in UnicodeStringTable. If Language is
476 a member of SupportedLanguages and a Unicode string is found in UnicodeStringTable
477 that matches the language code specified by Language, then it is returned in
480 @param Language A pointer to the ISO 639-2 language code for the
481 Unicode string to look up and return.
482 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes
483 that the Unicode string table supports. Language
484 must be a member of this set.
485 @param UnicodeStringTable A pointer to the table of Unicode strings.
486 @param UnicodeString A pointer to the Unicode string from UnicodeStringTable
487 that matches the language specified by Language.
489 @retval EFI_SUCCESS The Unicode string that matches the language
490 specified by Language was found
491 in the table of Unicoide strings UnicodeStringTable,
492 and it was returned in UnicodeString.
493 @retval EFI_INVALID_PARAMETER Language is NULL.
494 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
495 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
496 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.
497 @retval EFI_UNSUPPORTED The language specified by Language is not a
498 member of SupportedLanguages.
499 @retval EFI_UNSUPPORTED The language specified by Language is not
500 supported by UnicodeStringTable.
505 LookupUnicodeString (
506 IN CONST CHAR8
*Language
,
507 IN CONST CHAR8
*SupportedLanguages
,
508 IN CONST EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
,
509 OUT CHAR16
**UnicodeString
513 // Make sure the parameters are valid
515 if (Language
== NULL
|| UnicodeString
== NULL
) {
516 return EFI_INVALID_PARAMETER
;
520 // If there are no supported languages, or the Unicode String Table is empty, then the
521 // Unicode String specified by Language is not supported by this Unicode String Table
523 if (SupportedLanguages
== NULL
|| UnicodeStringTable
== NULL
) {
524 return EFI_UNSUPPORTED
;
528 // Make sure Language is in the set of Supported Languages
530 while (*SupportedLanguages
!= 0) {
531 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
534 // Search the Unicode String Table for the matching Language specifier
536 while (UnicodeStringTable
->Language
!= NULL
) {
537 if (CompareIso639LanguageCode (Language
, UnicodeStringTable
->Language
)) {
540 // A matching string was found, so return it
542 *UnicodeString
= UnicodeStringTable
->UnicodeString
;
546 UnicodeStringTable
++;
549 return EFI_UNSUPPORTED
;
552 SupportedLanguages
+= 3;
555 return EFI_UNSUPPORTED
;
559 This function adds a Unicode string to UnicodeStringTable.
560 If Language is a member of SupportedLanguages then UnicodeString is added to
561 UnicodeStringTable. New buffers are allocated for both Language and
562 UnicodeString. The contents of Language and UnicodeString are copied into
563 these new buffers. These buffers are automatically freed when
564 FreeUnicodeStringTable() is called.
566 @param Language A pointer to the ISO 639-2 language code for the Unicode
568 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes
569 that the Unicode string table supports.
570 Language must be a member of this set.
571 @param UnicodeStringTable A pointer to the table of Unicode strings.
572 @param UnicodeString A pointer to the Unicode string to add.
574 @retval EFI_SUCCESS The Unicode string that matches the language
575 specified by Language was found in the table of
576 Unicode strings UnicodeStringTable, and it was
577 returned in UnicodeString.
578 @retval EFI_INVALID_PARAMETER Language is NULL.
579 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
580 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.
581 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
582 @retval EFI_ALREADY_STARTED A Unicode string with language Language is
583 already present in UnicodeStringTable.
584 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another
585 Unicode string to UnicodeStringTable.
586 @retval EFI_UNSUPPORTED The language specified by Language is not a
587 member of SupportedLanguages.
593 IN CONST CHAR8
*Language
,
594 IN CONST CHAR8
*SupportedLanguages
,
595 IN EFI_UNICODE_STRING_TABLE
**UnicodeStringTable
,
596 IN CONST CHAR16
*UnicodeString
599 UINTN NumberOfEntries
;
600 EFI_UNICODE_STRING_TABLE
*OldUnicodeStringTable
;
601 EFI_UNICODE_STRING_TABLE
*NewUnicodeStringTable
;
602 UINTN UnicodeStringLength
;
605 // Make sure the parameter are valid
607 if (Language
== NULL
|| UnicodeString
== NULL
|| UnicodeStringTable
== NULL
) {
608 return EFI_INVALID_PARAMETER
;
612 // If there are no supported languages, then a Unicode String can not be added
614 if (SupportedLanguages
== NULL
) {
615 return EFI_UNSUPPORTED
;
619 // If the Unicode String is empty, then a Unicode String can not be added
621 if (UnicodeString
[0] == 0) {
622 return EFI_INVALID_PARAMETER
;
626 // Make sure Language is a member of SupportedLanguages
628 while (*SupportedLanguages
!= 0) {
629 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
632 // Determine the size of the Unicode String Table by looking for a NULL Language entry
635 if (*UnicodeStringTable
!= NULL
) {
636 OldUnicodeStringTable
= *UnicodeStringTable
;
637 while (OldUnicodeStringTable
->Language
!= NULL
) {
638 if (CompareIso639LanguageCode (Language
, OldUnicodeStringTable
->Language
)) {
639 return EFI_ALREADY_STARTED
;
642 OldUnicodeStringTable
++;
648 // Allocate space for a new Unicode String Table. It must hold the current number of
649 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table
652 NewUnicodeStringTable
= AllocatePool ((NumberOfEntries
+ 2) * sizeof (EFI_UNICODE_STRING_TABLE
));
653 if (NewUnicodeStringTable
== NULL
) {
654 return EFI_OUT_OF_RESOURCES
;
658 // If the current Unicode String Table contains any entries, then copy them to the
659 // newly allocated Unicode String Table.
661 if (*UnicodeStringTable
!= NULL
) {
663 NewUnicodeStringTable
,
665 NumberOfEntries
* sizeof (EFI_UNICODE_STRING_TABLE
)
670 // Allocate space for a copy of the Language specifier
672 NewUnicodeStringTable
[NumberOfEntries
].Language
= AllocateCopyPool (3, Language
);
673 if (NewUnicodeStringTable
[NumberOfEntries
].Language
== NULL
) {
674 gBS
->FreePool (NewUnicodeStringTable
);
675 return EFI_OUT_OF_RESOURCES
;
679 // Compute the length of the Unicode String
681 for (UnicodeStringLength
= 0; UnicodeString
[UnicodeStringLength
] != 0; UnicodeStringLength
++)
685 // Allocate space for a copy of the Unicode String
687 NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
= AllocateCopyPool (
688 (UnicodeStringLength
+ 1) * sizeof (CHAR16
),
691 if (NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
== NULL
) {
692 gBS
->FreePool (NewUnicodeStringTable
[NumberOfEntries
].Language
);
693 gBS
->FreePool (NewUnicodeStringTable
);
694 return EFI_OUT_OF_RESOURCES
;
698 // Mark the end of the Unicode String Table
700 NewUnicodeStringTable
[NumberOfEntries
+ 1].Language
= NULL
;
701 NewUnicodeStringTable
[NumberOfEntries
+ 1].UnicodeString
= NULL
;
704 // Free the old Unicode String Table
706 if (*UnicodeStringTable
!= NULL
) {
707 gBS
->FreePool (*UnicodeStringTable
);
711 // Point UnicodeStringTable at the newly allocated Unicode String Table
713 *UnicodeStringTable
= NewUnicodeStringTable
;
718 SupportedLanguages
+= 3;
721 return EFI_UNSUPPORTED
;
725 This function frees the table of Unicode strings in UnicodeStringTable.
726 If UnicodeStringTable is NULL, then EFI_SUCCESS is returned.
727 Otherwise, each language code, and each Unicode string in the Unicode string
728 table are freed, and EFI_SUCCESS is returned.
730 @param UnicodeStringTable A pointer to the table of Unicode strings.
732 @retval EFI_SUCCESS The Unicode string table was freed.
737 FreeUnicodeStringTable (
738 IN EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
744 // If the Unicode String Table is NULL, then it is already freed
746 if (UnicodeStringTable
== NULL
) {
751 // Loop through the Unicode String Table until we reach the end of table marker
753 for (Index
= 0; UnicodeStringTable
[Index
].Language
!= NULL
; Index
++) {
756 // Free the Language string from the Unicode String Table
758 gBS
->FreePool (UnicodeStringTable
[Index
].Language
);
761 // Free the Unicode String from the Unicode String Table
763 if (UnicodeStringTable
[Index
].UnicodeString
!= NULL
) {
764 gBS
->FreePool (UnicodeStringTable
[Index
].UnicodeString
);
769 // Free the Unicode String Table itself
771 gBS
->FreePool (UnicodeStringTable
);