2 Mde UEFI library functions.
4 Copyright (c) 2006, 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 This function looks up a Unicode string in UnicodeStringTable. If Language is
352 a member of SupportedLanguages and a Unicode string is found in UnicodeStringTable
353 that matches the language code specified by Language, then it is returned in
356 @param Language A pointer to the ISO 639-2 language code for the
357 Unicode string to look up and return.
358 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes
359 that the Unicode string table supports. Language
360 must be a member of this set.
361 @param UnicodeStringTable A pointer to the table of Unicode strings.
362 @param UnicodeString A pointer to the Unicode string from UnicodeStringTable
363 that matches the language specified by Language.
365 @retval EFI_SUCCESS The Unicode string that matches the language
366 specified by Language was found
367 in the table of Unicoide strings UnicodeStringTable,
368 and it was returned in UnicodeString.
369 @retval EFI_INVALID_PARAMETER Language is NULL.
370 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
371 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
372 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.
373 @retval EFI_UNSUPPORTED The language specified by Language is not a
374 member of SupportedLanguages.
375 @retval EFI_UNSUPPORTED The language specified by Language is not
376 supported by UnicodeStringTable.
381 LookupUnicodeString (
382 IN CONST CHAR8
*Language
,
383 IN CONST CHAR8
*SupportedLanguages
,
384 IN CONST EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
,
385 OUT CHAR16
**UnicodeString
389 // Make sure the parameters are valid
391 if (Language
== NULL
|| UnicodeString
== NULL
) {
392 return EFI_INVALID_PARAMETER
;
396 // If there are no supported languages, or the Unicode String Table is empty, then the
397 // Unicode String specified by Language is not supported by this Unicode String Table
399 if (SupportedLanguages
== NULL
|| UnicodeStringTable
== NULL
) {
400 return EFI_UNSUPPORTED
;
404 // Make sure Language is in the set of Supported Languages
406 while (*SupportedLanguages
!= 0) {
407 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
410 // Search the Unicode String Table for the matching Language specifier
412 while (UnicodeStringTable
->Language
!= NULL
) {
413 if (CompareIso639LanguageCode (Language
, UnicodeStringTable
->Language
)) {
416 // A matching string was found, so return it
418 *UnicodeString
= UnicodeStringTable
->UnicodeString
;
422 UnicodeStringTable
++;
425 return EFI_UNSUPPORTED
;
428 SupportedLanguages
+= 3;
431 return EFI_UNSUPPORTED
;
435 This function adds a Unicode string to UnicodeStringTable.
436 If Language is a member of SupportedLanguages then UnicodeString is added to
437 UnicodeStringTable. New buffers are allocated for both Language and
438 UnicodeString. The contents of Language and UnicodeString are copied into
439 these new buffers. These buffers are automatically freed when
440 FreeUnicodeStringTable() is called.
442 @param Language A pointer to the ISO 639-2 language code for the Unicode
444 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes
445 that the Unicode string table supports.
446 Language must be a member of this set.
447 @param UnicodeStringTable A pointer to the table of Unicode strings.
448 @param UnicodeString A pointer to the Unicode string to add.
450 @retval EFI_SUCCESS The Unicode string that matches the language
451 specified by Language was found in the table of
452 Unicode strings UnicodeStringTable, and it was
453 returned in UnicodeString.
454 @retval EFI_INVALID_PARAMETER Language is NULL.
455 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
456 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.
457 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
458 @retval EFI_ALREADY_STARTED A Unicode string with language Language is
459 already present in UnicodeStringTable.
460 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another
461 Unicode string to UnicodeStringTable.
462 @retval EFI_UNSUPPORTED The language specified by Language is not a
463 member of SupportedLanguages.
469 IN CONST CHAR8
*Language
,
470 IN CONST CHAR8
*SupportedLanguages
,
471 IN EFI_UNICODE_STRING_TABLE
**UnicodeStringTable
,
472 IN CONST CHAR16
*UnicodeString
475 UINTN NumberOfEntries
;
476 EFI_UNICODE_STRING_TABLE
*OldUnicodeStringTable
;
477 EFI_UNICODE_STRING_TABLE
*NewUnicodeStringTable
;
478 UINTN UnicodeStringLength
;
481 // Make sure the parameter are valid
483 if (Language
== NULL
|| UnicodeString
== NULL
|| UnicodeStringTable
== NULL
) {
484 return EFI_INVALID_PARAMETER
;
488 // If there are no supported languages, then a Unicode String can not be added
490 if (SupportedLanguages
== NULL
) {
491 return EFI_UNSUPPORTED
;
495 // If the Unicode String is empty, then a Unicode String can not be added
497 if (UnicodeString
[0] == 0) {
498 return EFI_INVALID_PARAMETER
;
502 // Make sure Language is a member of SupportedLanguages
504 while (*SupportedLanguages
!= 0) {
505 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
508 // Determine the size of the Unicode String Table by looking for a NULL Language entry
511 if (*UnicodeStringTable
!= NULL
) {
512 OldUnicodeStringTable
= *UnicodeStringTable
;
513 while (OldUnicodeStringTable
->Language
!= NULL
) {
514 if (CompareIso639LanguageCode (Language
, OldUnicodeStringTable
->Language
)) {
515 return EFI_ALREADY_STARTED
;
518 OldUnicodeStringTable
++;
524 // Allocate space for a new Unicode String Table. It must hold the current number of
525 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table
528 NewUnicodeStringTable
= AllocatePool ((NumberOfEntries
+ 2) * sizeof (EFI_UNICODE_STRING_TABLE
));
529 if (NewUnicodeStringTable
== NULL
) {
530 return EFI_OUT_OF_RESOURCES
;
534 // If the current Unicode String Table contains any entries, then copy them to the
535 // newly allocated Unicode String Table.
537 if (*UnicodeStringTable
!= NULL
) {
539 NewUnicodeStringTable
,
541 NumberOfEntries
* sizeof (EFI_UNICODE_STRING_TABLE
)
546 // Allocate space for a copy of the Language specifier
548 NewUnicodeStringTable
[NumberOfEntries
].Language
= AllocateCopyPool (3, Language
);
549 if (NewUnicodeStringTable
[NumberOfEntries
].Language
== NULL
) {
550 gBS
->FreePool (NewUnicodeStringTable
);
551 return EFI_OUT_OF_RESOURCES
;
555 // Compute the length of the Unicode String
557 for (UnicodeStringLength
= 0; UnicodeString
[UnicodeStringLength
] != 0; UnicodeStringLength
++)
561 // Allocate space for a copy of the Unicode String
563 NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
= AllocateCopyPool (
564 (UnicodeStringLength
+ 1) * sizeof (CHAR16
),
567 if (NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
== NULL
) {
568 gBS
->FreePool (NewUnicodeStringTable
[NumberOfEntries
].Language
);
569 gBS
->FreePool (NewUnicodeStringTable
);
570 return EFI_OUT_OF_RESOURCES
;
574 // Mark the end of the Unicode String Table
576 NewUnicodeStringTable
[NumberOfEntries
+ 1].Language
= NULL
;
577 NewUnicodeStringTable
[NumberOfEntries
+ 1].UnicodeString
= NULL
;
580 // Free the old Unicode String Table
582 if (*UnicodeStringTable
!= NULL
) {
583 gBS
->FreePool (*UnicodeStringTable
);
587 // Point UnicodeStringTable at the newly allocated Unicode String Table
589 *UnicodeStringTable
= NewUnicodeStringTable
;
594 SupportedLanguages
+= 3;
597 return EFI_UNSUPPORTED
;
601 This function frees the table of Unicode strings in UnicodeStringTable.
602 If UnicodeStringTable is NULL, then EFI_SUCCESS is returned.
603 Otherwise, each language code, and each Unicode string in the Unicode string
604 table are freed, and EFI_SUCCESS is returned.
606 @param UnicodeStringTable A pointer to the table of Unicode strings.
608 @retval EFI_SUCCESS The Unicode string table was freed.
613 FreeUnicodeStringTable (
614 IN EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
620 // If the Unicode String Table is NULL, then it is already freed
622 if (UnicodeStringTable
== NULL
) {
627 // Loop through the Unicode String Table until we reach the end of table marker
629 for (Index
= 0; UnicodeStringTable
[Index
].Language
!= NULL
; Index
++) {
632 // Free the Language string from the Unicode String Table
634 gBS
->FreePool (UnicodeStringTable
[Index
].Language
);
637 // Free the Unicode String from the Unicode String Table
639 if (UnicodeStringTable
[Index
].UnicodeString
!= NULL
) {
640 gBS
->FreePool (UnicodeStringTable
[Index
].UnicodeString
);
645 // Free the Unicode String Table itself
647 gBS
->FreePool (UnicodeStringTable
);