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.
16 // Include common header file for this module.
18 #include "UefiLibInternal.h"
21 Compare whether two names of languages are identical.
23 @param Language1 Name of language 1.
24 @param Language2 Name of language 2.
26 @retval TRUE Language 1 and language 2 are the same.
27 @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 @retval 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 Lock The task lock with priority level.
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 ProtocolGuid 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.
514 If Language is a member of SupportedLanguages and a Unicode
515 string is found in UnicodeStringTable that matches the
516 language code specified by Language, then it is returned in
519 @param Language A pointer to the ISO 639-2
520 language code for the Unicode
521 string to look up and return.
523 @param SupportedLanguages A pointer to the set of ISO
525 codes that the Unicode string
526 table supports. Language must
527 be a member of this set.
529 @param UnicodeStringTable A pointer to the table of
532 @param UnicodeString A pointer to the Unicode
533 string from UnicodeStringTable
534 that matches the language
535 specified by Language.
537 @retval EFI_SUCCESS The Unicode string that
538 matches the language specified
539 by Language was found in the
540 table of Unicoide strings
541 UnicodeStringTable, and it was
542 returned in UnicodeString.
544 @retval EFI_INVALID_PARAMETER Language is NULL.
546 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
547 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
549 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.
551 @retval EFI_UNSUPPORTED The language specified by
552 Language is not a member
553 ofSupportedLanguages.
555 @retval EFI_UNSUPPORTED The language specified by
556 Language is not supported by
562 LookupUnicodeString (
563 IN CONST CHAR8
*Language
,
564 IN CONST CHAR8
*SupportedLanguages
,
565 IN CONST EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
,
566 OUT CHAR16
**UnicodeString
570 // Make sure the parameters are valid
572 if (Language
== NULL
|| UnicodeString
== NULL
) {
573 return EFI_INVALID_PARAMETER
;
577 // If there are no supported languages, or the Unicode String Table is empty, then the
578 // Unicode String specified by Language is not supported by this Unicode String Table
580 if (SupportedLanguages
== NULL
|| UnicodeStringTable
== NULL
) {
581 return EFI_UNSUPPORTED
;
585 // Make sure Language is in the set of Supported Languages
587 while (*SupportedLanguages
!= 0) {
588 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
591 // Search the Unicode String Table for the matching Language specifier
593 while (UnicodeStringTable
->Language
!= NULL
) {
594 if (CompareIso639LanguageCode (Language
, UnicodeStringTable
->Language
)) {
597 // A matching string was found, so return it
599 *UnicodeString
= UnicodeStringTable
->UnicodeString
;
603 UnicodeStringTable
++;
606 return EFI_UNSUPPORTED
;
609 SupportedLanguages
+= 3;
612 return EFI_UNSUPPORTED
;
618 This function looks up a Unicode string in UnicodeStringTable.
619 If Language is a member of SupportedLanguages and a Unicode
620 string is found in UnicodeStringTable that matches the
621 language code specified by Language, then it is returned in
624 @param Language A pointer to the ISO 639-2 or
625 RFC 3066 language code for the
626 Unicode string to look up and
629 @param SupportedLanguages A pointer to the set of ISO
630 639-2 or RFC 3066 language
631 codes that the Unicode string
632 table supports. Language must
633 be a member of this set.
635 @param UnicodeStringTable A pointer to the table of
638 @param UnicodeString A pointer to the Unicode
639 string from UnicodeStringTable
640 that matches the language
641 specified by Language.
643 @param Iso639Language Specify the language code
644 format supported. If true,
645 then the format follow ISO
646 639-2. If false, then it
649 @retval EFI_SUCCESS The Unicode string that
650 matches the language specified
651 by Language was found in the
652 table of Unicoide strings
653 UnicodeStringTable, and it was
654 returned in UnicodeString.
656 @retval EFI_INVALID_PARAMETER Language is NULL.
658 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
660 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
662 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.
664 @retval EFI_UNSUPPORTED The language specified by
665 Language is not a member
666 ofSupportedLanguages.
668 @retval EFI_UNSUPPORTED The language specified by
669 Language is not supported by
675 LookupUnicodeString2 (
676 IN CONST CHAR8
*Language
,
677 IN CONST CHAR8
*SupportedLanguages
,
678 IN CONST EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
,
679 OUT CHAR16
**UnicodeString
,
680 IN BOOLEAN Iso639Language
685 CHAR8
*LanguageString
;
688 // Make sure the parameters are valid
690 if (Language
== NULL
|| UnicodeString
== NULL
) {
691 return EFI_INVALID_PARAMETER
;
695 // If there are no supported languages, or the Unicode String Table is empty, then the
696 // Unicode String specified by Language is not supported by this Unicode String Table
698 if (SupportedLanguages
== NULL
|| UnicodeStringTable
== NULL
) {
699 return EFI_UNSUPPORTED
;
703 // Make sure Language is in the set of Supported Languages
706 while (*SupportedLanguages
!= 0) {
707 if (Iso639Language
) {
708 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
712 SupportedLanguages
+= 3;
714 for (Index
= 0; SupportedLanguages
[Index
] != 0 && SupportedLanguages
[Index
] != ';'; Index
++);
715 if ((AsciiStrnCmp(SupportedLanguages
, Language
, Index
) == 0) && (Language
[Index
] == 0)) {
719 SupportedLanguages
+= Index
;
720 for (; *SupportedLanguages
!= 0 && *SupportedLanguages
== ';'; SupportedLanguages
++);
725 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED
728 return EFI_UNSUPPORTED
;
732 // Search the Unicode String Table for the matching Language specifier
734 while (UnicodeStringTable
->Language
!= NULL
) {
735 LanguageString
= UnicodeStringTable
->Language
;
736 while (0 != *LanguageString
) {
737 for (Index
= 0 ;LanguageString
[Index
] != 0 && LanguageString
[Index
] != ';'; Index
++);
738 if (AsciiStrnCmp(LanguageString
, Language
, Index
) == 0) {
739 *UnicodeString
= UnicodeStringTable
->UnicodeString
;
742 LanguageString
+= Index
;
743 for (Index
= 0 ;LanguageString
[Index
] != 0 && LanguageString
[Index
] == ';'; Index
++);
745 UnicodeStringTable
++;
748 return EFI_UNSUPPORTED
;
754 This function adds a Unicode string to UnicodeStringTable.
755 If Language is a member of SupportedLanguages then
756 UnicodeString is added to UnicodeStringTable. New buffers are
757 allocated for both Language and UnicodeString. The contents
758 of Language and UnicodeString are copied into these new
759 buffers. These buffers are automatically freed when
760 FreeUnicodeStringTable() is called.
762 @param Language A pointer to the ISO 639-2
763 language code for the Unicode
766 @param SupportedLanguages A pointer to the set of ISO
767 639-2 language codes that the
768 Unicode string table supports.
769 Language must be a member of
772 @param UnicodeStringTable A pointer to the table of
775 @param UnicodeString A pointer to the Unicode
778 @retval EFI_SUCCESS The Unicode string that
779 matches the language specified
780 by Language was found in the
781 table of Unicode strings
782 UnicodeStringTable, and it was
783 returned in UnicodeString.
785 @retval EFI_INVALID_PARAMETER Language is NULL.
787 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
789 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.
791 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
793 @retval EFI_ALREADY_STARTED A Unicode string with language
794 Language is already present in
797 @retval EFI_OUT_OF_RESOURCES There is not enough memory to
798 add another Unicode string to
801 @retval EFI_UNSUPPORTED The language specified by
802 Language is not a member of
809 IN CONST CHAR8
*Language
,
810 IN CONST CHAR8
*SupportedLanguages
,
811 IN EFI_UNICODE_STRING_TABLE
**UnicodeStringTable
,
812 IN CONST CHAR16
*UnicodeString
815 UINTN NumberOfEntries
;
816 EFI_UNICODE_STRING_TABLE
*OldUnicodeStringTable
;
817 EFI_UNICODE_STRING_TABLE
*NewUnicodeStringTable
;
818 UINTN UnicodeStringLength
;
821 // Make sure the parameter are valid
823 if (Language
== NULL
|| UnicodeString
== NULL
|| UnicodeStringTable
== NULL
) {
824 return EFI_INVALID_PARAMETER
;
828 // If there are no supported languages, then a Unicode String can not be added
830 if (SupportedLanguages
== NULL
) {
831 return EFI_UNSUPPORTED
;
835 // If the Unicode String is empty, then a Unicode String can not be added
837 if (UnicodeString
[0] == 0) {
838 return EFI_INVALID_PARAMETER
;
842 // Make sure Language is a member of SupportedLanguages
844 while (*SupportedLanguages
!= 0) {
845 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
848 // Determine the size of the Unicode String Table by looking for a NULL Language entry
851 if (*UnicodeStringTable
!= NULL
) {
852 OldUnicodeStringTable
= *UnicodeStringTable
;
853 while (OldUnicodeStringTable
->Language
!= NULL
) {
854 if (CompareIso639LanguageCode (Language
, OldUnicodeStringTable
->Language
)) {
855 return EFI_ALREADY_STARTED
;
858 OldUnicodeStringTable
++;
864 // Allocate space for a new Unicode String Table. It must hold the current number of
865 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table
868 NewUnicodeStringTable
= AllocatePool ((NumberOfEntries
+ 2) * sizeof (EFI_UNICODE_STRING_TABLE
));
869 if (NewUnicodeStringTable
== NULL
) {
870 return EFI_OUT_OF_RESOURCES
;
874 // If the current Unicode String Table contains any entries, then copy them to the
875 // newly allocated Unicode String Table.
877 if (*UnicodeStringTable
!= NULL
) {
879 NewUnicodeStringTable
,
881 NumberOfEntries
* sizeof (EFI_UNICODE_STRING_TABLE
)
886 // Allocate space for a copy of the Language specifier
888 NewUnicodeStringTable
[NumberOfEntries
].Language
= AllocateCopyPool (3, Language
);
889 if (NewUnicodeStringTable
[NumberOfEntries
].Language
== NULL
) {
890 gBS
->FreePool (NewUnicodeStringTable
);
891 return EFI_OUT_OF_RESOURCES
;
895 // Compute the length of the Unicode String
897 for (UnicodeStringLength
= 0; UnicodeString
[UnicodeStringLength
] != 0; UnicodeStringLength
++)
901 // Allocate space for a copy of the Unicode String
903 NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
= AllocateCopyPool (
904 (UnicodeStringLength
+ 1) * sizeof (CHAR16
),
907 if (NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
== NULL
) {
908 gBS
->FreePool (NewUnicodeStringTable
[NumberOfEntries
].Language
);
909 gBS
->FreePool (NewUnicodeStringTable
);
910 return EFI_OUT_OF_RESOURCES
;
914 // Mark the end of the Unicode String Table
916 NewUnicodeStringTable
[NumberOfEntries
+ 1].Language
= NULL
;
917 NewUnicodeStringTable
[NumberOfEntries
+ 1].UnicodeString
= NULL
;
920 // Free the old Unicode String Table
922 if (*UnicodeStringTable
!= NULL
) {
923 gBS
->FreePool (*UnicodeStringTable
);
927 // Point UnicodeStringTable at the newly allocated Unicode String Table
929 *UnicodeStringTable
= NewUnicodeStringTable
;
934 SupportedLanguages
+= 3;
937 return EFI_UNSUPPORTED
;
943 This function adds a Unicode string to UnicodeStringTable.
944 If Language is a member of SupportedLanguages then
945 UnicodeString is added to UnicodeStringTable. New buffers are
946 allocated for both Language and UnicodeString. The contents
947 of Language and UnicodeString are copied into these new
948 buffers. These buffers are automatically freed when
949 FreeUnicodeStringTable() is called.
951 @param Language A pointer to the ISO 639-2 or
952 RFC 3066 language code for the
953 Unicode string to add.
955 @param SupportedLanguages A pointer to the set of ISO
956 639-2 or RFC 3.66 language
957 codes that the Unicode string
958 table supports. Language must
959 be a member of this set.
961 @param UnicodeStringTable A pointer to the table of
964 @param UnicodeString A pointer to the Unicode
967 @param Iso639Language Specify the language code
968 format supported. If true,
969 then the format follow ISO
970 639-2. If false, then it
973 @retval EFI_SUCCESS The Unicode string that
974 matches the language specified
975 by Language was found in the
976 table of Unicode strings
977 UnicodeStringTable, and it was
978 returned in UnicodeString.
980 @retval EFI_INVALID_PARAMETER Language is NULL.
982 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
984 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.
986 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
988 @retval EFI_ALREADY_STARTED A Unicode string with language
989 Language is already present in
992 @retval EFI_OUT_OF_RESOURCES There is not enough memory to
993 add another Unicode string to
996 @retval EFI_UNSUPPORTED The language specified by
997 Language is not a member of
1004 IN CONST CHAR8
*Language
,
1005 IN CONST CHAR8
*SupportedLanguages
,
1006 IN EFI_UNICODE_STRING_TABLE
**UnicodeStringTable
,
1007 IN CONST CHAR16
*UnicodeString
,
1008 IN BOOLEAN Iso639Language
1011 UINTN NumberOfEntries
;
1012 EFI_UNICODE_STRING_TABLE
*OldUnicodeStringTable
;
1013 EFI_UNICODE_STRING_TABLE
*NewUnicodeStringTable
;
1014 UINTN UnicodeStringLength
;
1017 CHAR8
*LanguageString
;
1020 // Make sure the parameter are valid
1022 if (Language
== NULL
|| UnicodeString
== NULL
|| UnicodeStringTable
== NULL
) {
1023 return EFI_INVALID_PARAMETER
;
1027 // If there are no supported languages, then a Unicode String can not be added
1029 if (SupportedLanguages
== NULL
) {
1030 return EFI_UNSUPPORTED
;
1034 // If the Unicode String is empty, then a Unicode String can not be added
1036 if (UnicodeString
[0] == 0) {
1037 return EFI_INVALID_PARAMETER
;
1041 // Make sure Language is a member of SupportedLanguages
1044 while (*SupportedLanguages
!= 0) {
1045 if (Iso639Language
) {
1046 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
1050 SupportedLanguages
+= 3;
1052 for (Index
= 0; SupportedLanguages
[Index
] != 0 && SupportedLanguages
[Index
] != ';'; Index
++);
1053 if (AsciiStrnCmp(SupportedLanguages
, Language
, Index
) == 0) {
1057 SupportedLanguages
+= Index
;
1058 for (; *SupportedLanguages
!= 0 && *SupportedLanguages
== ';'; SupportedLanguages
++);
1063 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED
1066 return EFI_UNSUPPORTED
;
1070 // Determine the size of the Unicode String Table by looking for a NULL Language entry
1072 NumberOfEntries
= 0;
1073 if (*UnicodeStringTable
!= NULL
) {
1074 OldUnicodeStringTable
= *UnicodeStringTable
;
1075 while (OldUnicodeStringTable
->Language
!= NULL
) {
1076 LanguageString
= OldUnicodeStringTable
->Language
;
1078 while (*LanguageString
!= 0) {
1079 for (Index
= 0; LanguageString
[Index
] != 0 && LanguageString
[Index
] != ';'; Index
++);
1081 if (AsciiStrnCmp (Language
, LanguageString
, Index
) == 0) {
1082 return EFI_ALREADY_STARTED
;
1084 LanguageString
+= Index
;
1085 for (; *LanguageString
!= 0 && *LanguageString
== ';'; LanguageString
++);
1087 OldUnicodeStringTable
++;
1093 // Allocate space for a new Unicode String Table. It must hold the current number of
1094 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table
1097 NewUnicodeStringTable
= AllocatePool ((NumberOfEntries
+ 2) * sizeof (EFI_UNICODE_STRING_TABLE
));
1098 if (NewUnicodeStringTable
== NULL
) {
1099 return EFI_OUT_OF_RESOURCES
;
1103 // If the current Unicode String Table contains any entries, then copy them to the
1104 // newly allocated Unicode String Table.
1106 if (*UnicodeStringTable
!= NULL
) {
1108 NewUnicodeStringTable
,
1109 *UnicodeStringTable
,
1110 NumberOfEntries
* sizeof (EFI_UNICODE_STRING_TABLE
)
1115 // Allocate space for a copy of the Language specifier
1117 NewUnicodeStringTable
[NumberOfEntries
].Language
= AllocateCopyPool (AsciiStrSize(Language
), Language
);
1118 if (NewUnicodeStringTable
[NumberOfEntries
].Language
== NULL
) {
1119 gBS
->FreePool (NewUnicodeStringTable
);
1120 return EFI_OUT_OF_RESOURCES
;
1124 // Compute the length of the Unicode String
1126 for (UnicodeStringLength
= 0; UnicodeString
[UnicodeStringLength
] != 0; UnicodeStringLength
++);
1129 // Allocate space for a copy of the Unicode String
1131 NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
= AllocateCopyPool (StrSize (UnicodeString
), UnicodeString
);
1132 if (NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
== NULL
) {
1133 gBS
->FreePool (NewUnicodeStringTable
[NumberOfEntries
].Language
);
1134 gBS
->FreePool (NewUnicodeStringTable
);
1135 return EFI_OUT_OF_RESOURCES
;
1139 // Mark the end of the Unicode String Table
1141 NewUnicodeStringTable
[NumberOfEntries
+ 1].Language
= NULL
;
1142 NewUnicodeStringTable
[NumberOfEntries
+ 1].UnicodeString
= NULL
;
1145 // Free the old Unicode String Table
1147 if (*UnicodeStringTable
!= NULL
) {
1148 gBS
->FreePool (*UnicodeStringTable
);
1152 // Point UnicodeStringTable at the newly allocated Unicode String Table
1154 *UnicodeStringTable
= NewUnicodeStringTable
;
1160 This function frees the table of Unicode strings in UnicodeStringTable.
1161 If UnicodeStringTable is NULL, then EFI_SUCCESS is returned.
1162 Otherwise, each language code, and each Unicode string in the Unicode string
1163 table are freed, and EFI_SUCCESS is returned.
1165 @param UnicodeStringTable A pointer to the table of Unicode strings.
1167 @retval EFI_SUCCESS The Unicode string table was freed.
1172 FreeUnicodeStringTable (
1173 IN EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
1179 // If the Unicode String Table is NULL, then it is already freed
1181 if (UnicodeStringTable
== NULL
) {
1186 // Loop through the Unicode String Table until we reach the end of table marker
1188 for (Index
= 0; UnicodeStringTable
[Index
].Language
!= NULL
; Index
++) {
1191 // Free the Language string from the Unicode String Table
1193 gBS
->FreePool (UnicodeStringTable
[Index
].Language
);
1196 // Free the Unicode String from the Unicode String Table
1198 if (UnicodeStringTable
[Index
].UnicodeString
!= NULL
) {
1199 gBS
->FreePool (UnicodeStringTable
[Index
].UnicodeString
);
1204 // Free the Unicode String Table itself
1206 gBS
->FreePool (UnicodeStringTable
);