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.
50 If TableGuid is NULL, then ASSERT().
51 If Table is NULL, then ASSERT().
53 @param TableGuid Pointer to table's GUID type..
54 @param Table Pointer to the table associated with TableGuid in the EFI System Table.
56 @retval EFI_SUCCESS A configuration table matching TableGuid was found.
57 @retval EFI_NOT_FOUND A configuration table matching TableGuid could not be found.
62 EfiGetSystemConfigurationTable (
63 IN EFI_GUID
*TableGuid
,
67 EFI_SYSTEM_TABLE
*SystemTable
;
70 ASSERT (TableGuid
!= NULL
);
71 ASSERT (Table
!= NULL
);
75 for (Index
= 0; Index
< SystemTable
->NumberOfTableEntries
; Index
++) {
76 if (CompareGuid (TableGuid
, &(SystemTable
->ConfigurationTable
[Index
].VendorGuid
))) {
77 *Table
= SystemTable
->ConfigurationTable
[Index
].VendorTable
;
86 This function causes the notification function to be executed for every protocol
87 of type ProtocolGuid instance that exists in the system when this function is
88 invoked. In addition, every time a protocol of type ProtocolGuid instance is
89 installed or reinstalled, the notification function is also executed.
91 @param ProtocolGuid Supplies GUID of the protocol upon whose installation the event is fired.
92 @param NotifyTpl Supplies the task priority level of the event notifications.
93 @param NotifyFunction Supplies the function to notify when the event is signaled.
94 @param NotifyContext The context parameter to pass to NotifyFunction.
95 @param Registration A pointer to a memory location to receive the registration value.
97 @return The notification event that was created.
102 EfiCreateProtocolNotifyEvent(
103 IN EFI_GUID
*ProtocolGuid
,
104 IN EFI_TPL NotifyTpl
,
105 IN EFI_EVENT_NOTIFY NotifyFunction
,
106 IN VOID
*NotifyContext
, OPTIONAL
107 OUT VOID
**Registration
117 Status
= gBS
->CreateEvent (
124 ASSERT_EFI_ERROR (Status
);
127 // Register for protocol notifactions on this event
130 Status
= gBS
->RegisterProtocolNotify (
136 ASSERT_EFI_ERROR (Status
);
139 // Kick the event so we will perform an initial pass of
140 // current installed drivers
143 gBS
->SignalEvent (Event
);
148 This function creates an event using NotifyTpl, NoifyFunction, and NotifyContext.
149 This event is signaled with EfiNamedEventSignal(). This provide the ability for
150 one or more listeners on the same event named by the GUID specified by Name.
151 If Name is NULL, then ASSERT().
152 If NotifyTpl is not a legal TPL value, then ASSERT().
153 If NotifyFunction is NULL, then ASSERT().
155 @param Name Supplies GUID name of the event.
156 @param NotifyTpl Supplies the task priority level of the event notifications.
157 @param NotifyFunction Supplies the function to notify when the event is signaled.
158 @param NotifyContext The context parameter to pass to NotifyFunction.
159 @param Registration A pointer to a memory location to receive the registration value.
161 @retval EFI_SUCCESS A named event was created.
162 @retval EFI_OUT_OF_RESOURCES There are not enough resource to create the named event.
167 EfiNamedEventListen (
168 IN CONST EFI_GUID
*Name
,
169 IN EFI_TPL NotifyTpl
,
170 IN EFI_EVENT_NOTIFY NotifyFunction
,
171 IN CONST VOID
*NotifyContext
, OPTIONAL
172 OUT VOID
*Registration OPTIONAL
177 VOID
*RegistrationLocal
;
179 ASSERT (Name
!= NULL
);
180 ASSERT (NotifyFunction
!= NULL
);
181 ASSERT (NotifyTpl
<= TPL_HIGH_LEVEL
);
186 Status
= gBS
->CreateEvent (
190 (VOID
*) NotifyContext
,
193 ASSERT_EFI_ERROR (Status
);
196 // The Registration is not optional to RegisterProtocolNotify().
197 // To make it optional to EfiNamedEventListen(), may need to substitute with a local.
199 if (Registration
!= NULL
) {
200 RegistrationLocal
= Registration
;
202 RegistrationLocal
= &RegistrationLocal
;
206 // Register for an installation of protocol interface
209 Status
= gBS
->RegisterProtocolNotify (
214 ASSERT_EFI_ERROR (Status
);
220 This function signals the named event specified by Name. The named event must
221 have been created with EfiNamedEventListen().
223 @param Name Supplies GUID name of the event.
225 @retval EFI_SUCCESS A named event was signaled.
226 @retval EFI_OUT_OF_RESOURCES There are not enough resource to signal the named event.
231 EfiNamedEventSignal (
232 IN CONST EFI_GUID
*Name
239 Status
= gBS
->InstallProtocolInterface (
242 EFI_NATIVE_INTERFACE
,
245 ASSERT_EFI_ERROR (Status
);
247 Status
= gBS
->UninstallProtocolInterface (
252 ASSERT_EFI_ERROR (Status
);
258 Returns the current TPL.
260 This function returns the current TPL. There is no EFI service to directly
261 retrieve the current TPL. Instead, the RaiseTPL() function is used to raise
262 the TPL to TPL_HIGH_LEVEL. This will return the current TPL. The TPL level
263 can then immediately be restored back to the current TPL level with a call
268 @retval EFI_TPL The current TPL.
279 Tpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
280 gBS
->RestoreTPL (Tpl
);
287 This function initializes a basic mutual exclusion lock to the released state
288 and returns the lock. Each lock provides mutual exclusion access at its task
289 priority level. Since there is no preemption or multiprocessor support in EFI,
290 acquiring the lock only consists of raising to the locks TPL.
291 If Lock is NULL, then ASSERT().
292 If Priority is not a valid TPL value, then ASSERT().
294 @param Lock A pointer to the lock data structure to initialize.
295 @param Priority EFI TPL associated with the lock.
303 IN OUT EFI_LOCK
*Lock
,
307 ASSERT (Lock
!= NULL
);
308 ASSERT (Priority
<= TPL_HIGH_LEVEL
);
310 Lock
->Tpl
= Priority
;
311 Lock
->OwnerTpl
= TPL_APPLICATION
;
312 Lock
->Lock
= EfiLockReleased
;
317 This function raises the system's current task priority level to the task
318 priority level of the mutual exclusion lock. Then, it places the lock in the
320 If Lock is NULL, then ASSERT().
321 If Lock is not initialized, then ASSERT().
322 If Lock is already in the acquired state, then ASSERT().
324 @param Lock The task lock with priority level.
333 ASSERT (Lock
!= NULL
);
334 ASSERT (Lock
->Lock
== EfiLockReleased
);
336 Lock
->OwnerTpl
= gBS
->RaiseTPL (Lock
->Tpl
);
337 Lock
->Lock
= EfiLockAcquired
;
341 This function raises the system's current task priority level to the task
342 priority level of the mutual exclusion lock. Then, it attempts to place the
343 lock in the acquired state.
345 @param Lock A pointer to the lock to acquire.
347 @retval EFI_SUCCESS The lock was acquired.
348 @retval EFI_ACCESS_DENIED The lock could not be acquired because it is already owned.
353 EfiAcquireLockOrFail (
358 ASSERT (Lock
!= NULL
);
359 ASSERT (Lock
->Lock
!= EfiLockUninitialized
);
361 if (Lock
->Lock
== EfiLockAcquired
) {
363 // Lock is already owned, so bail out
365 return EFI_ACCESS_DENIED
;
368 Lock
->OwnerTpl
= gBS
->RaiseTPL (Lock
->Tpl
);
370 Lock
->Lock
= EfiLockAcquired
;
376 This function transitions a mutual exclusion lock from the acquired state to
377 the released state, and restores the system's task priority level to its
380 @param Lock A pointer to the lock to release.
391 ASSERT (Lock
!= NULL
);
392 ASSERT (Lock
->Lock
== EfiLockAcquired
);
394 Tpl
= Lock
->OwnerTpl
;
396 Lock
->Lock
= EfiLockReleased
;
398 gBS
->RestoreTPL (Tpl
);
402 Tests whether a controller handle is being managed by a specific driver.
404 This function tests whether the driver specified by DriverBindingHandle is
405 currently managing the controller specified by ControllerHandle. This test
406 is performed by evaluating if the the protocol specified by ProtocolGuid is
407 present on ControllerHandle and is was opened by DriverBindingHandle with an
408 attribute of EFI_OPEN_PROTOCOL_BY_DRIVER.
409 If ProtocolGuid is NULL, then ASSERT().
411 @param ControllerHandle A handle for a controller to test.
412 @param DriverBindingHandle Specifies the driver binding handle for the
414 @param ProtocolGuid Specifies the protocol that the driver specified
415 by DriverBindingHandle opens in its Start()
418 @retval EFI_SUCCESS ControllerHandle is managed by the driver
419 specifed by DriverBindingHandle.
420 @retval EFI_UNSUPPORTED ControllerHandle is not managed by the driver
421 specifed by DriverBindingHandle.
426 EfiTestManagedDevice (
427 IN CONST EFI_HANDLE ControllerHandle
,
428 IN CONST EFI_HANDLE DriverBindingHandle
,
429 IN CONST EFI_GUID
*ProtocolGuid
433 VOID
*ManagedInterface
;
435 ASSERT (ProtocolGuid
!= NULL
);
437 Status
= gBS
->OpenProtocol (
439 (EFI_GUID
*) ProtocolGuid
,
443 EFI_OPEN_PROTOCOL_BY_DRIVER
445 if (!EFI_ERROR (Status
)) {
448 (EFI_GUID
*) ProtocolGuid
,
452 return EFI_UNSUPPORTED
;
455 if (Status
!= EFI_ALREADY_STARTED
) {
456 return EFI_UNSUPPORTED
;
463 Tests whether a child handle is a child device of the controller.
465 This function tests whether ChildHandle is one of the children of
466 ControllerHandle. This test is performed by checking to see if the protocol
467 specified by ProtocolGuid is present on ControllerHandle and opened by
468 ChildHandle with an attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
469 If ProtocolGuid is NULL, then ASSERT().
471 @param ControllerHandle A handle for a (parent) controller to test.
472 @param ChildHandle A child handle to test.
473 @param ProtocolGuid Supplies the protocol that the child controller
474 opens on its parent controller.
476 @retval EFI_SUCCESS ChildHandle is a child of the ControllerHandle.
477 @retval EFI_UNSUPPORTED ChildHandle is not a child of the
484 IN CONST EFI_HANDLE ControllerHandle
,
485 IN CONST EFI_HANDLE ChildHandle
,
486 IN CONST EFI_GUID
*ProtocolGuid
490 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfoBuffer
;
494 ASSERT (ProtocolGuid
!= NULL
);
497 // Retrieve the list of agents that are consuming the specific protocol
498 // on ControllerHandle.
500 Status
= gBS
->OpenProtocolInformation (
502 (EFI_GUID
*) ProtocolGuid
,
506 if (EFI_ERROR (Status
)) {
507 return EFI_UNSUPPORTED
;
511 // Inspect if ChildHandle is one of the agents.
513 Status
= EFI_UNSUPPORTED
;
514 for (Index
= 0; Index
< EntryCount
; Index
++) {
515 if ((OpenInfoBuffer
[Index
].ControllerHandle
== ChildHandle
) &&
516 (OpenInfoBuffer
[Index
].Attributes
& EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
) != 0) {
517 Status
= EFI_SUCCESS
;
522 FreePool (OpenInfoBuffer
);
527 This function looks up a Unicode string in UnicodeStringTable.
528 If Language is a member of SupportedLanguages and a Unicode
529 string is found in UnicodeStringTable that matches the
530 language code specified by Language, then it is returned in
533 @param Language A pointer to the ISO 639-2
534 language code for the Unicode
535 string to look up and return.
537 @param SupportedLanguages A pointer to the set of ISO
539 codes that the Unicode string
540 table supports. Language must
541 be a member of this set.
543 @param UnicodeStringTable A pointer to the table of
546 @param UnicodeString A pointer to the Unicode
547 string from UnicodeStringTable
548 that matches the language
549 specified by Language.
551 @retval EFI_SUCCESS The Unicode string that
552 matches the language specified
553 by Language was found in the
554 table of Unicoide strings
555 UnicodeStringTable, and it was
556 returned in UnicodeString.
558 @retval EFI_INVALID_PARAMETER Language is NULL.
560 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
561 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
563 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.
565 @retval EFI_UNSUPPORTED The language specified by
566 Language is not a member
567 ofSupportedLanguages.
569 @retval EFI_UNSUPPORTED The language specified by
570 Language is not supported by
576 LookupUnicodeString (
577 IN CONST CHAR8
*Language
,
578 IN CONST CHAR8
*SupportedLanguages
,
579 IN CONST EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
,
580 OUT CHAR16
**UnicodeString
584 // Make sure the parameters are valid
586 if (Language
== NULL
|| UnicodeString
== NULL
) {
587 return EFI_INVALID_PARAMETER
;
591 // If there are no supported languages, or the Unicode String Table is empty, then the
592 // Unicode String specified by Language is not supported by this Unicode String Table
594 if (SupportedLanguages
== NULL
|| UnicodeStringTable
== NULL
) {
595 return EFI_UNSUPPORTED
;
599 // Make sure Language is in the set of Supported Languages
601 while (*SupportedLanguages
!= 0) {
602 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
605 // Search the Unicode String Table for the matching Language specifier
607 while (UnicodeStringTable
->Language
!= NULL
) {
608 if (CompareIso639LanguageCode (Language
, UnicodeStringTable
->Language
)) {
611 // A matching string was found, so return it
613 *UnicodeString
= UnicodeStringTable
->UnicodeString
;
617 UnicodeStringTable
++;
620 return EFI_UNSUPPORTED
;
623 SupportedLanguages
+= 3;
626 return EFI_UNSUPPORTED
;
632 This function looks up a Unicode string in UnicodeStringTable.
633 If Language is a member of SupportedLanguages and a Unicode
634 string is found in UnicodeStringTable that matches the
635 language code specified by Language, then it is returned in
638 @param Language A pointer to the ISO 639-2 or
639 RFC 3066 language code for the
640 Unicode string to look up and
643 @param SupportedLanguages A pointer to the set of ISO
644 639-2 or RFC 3066 language
645 codes that the Unicode string
646 table supports. Language must
647 be a member of this set.
649 @param UnicodeStringTable A pointer to the table of
652 @param UnicodeString A pointer to the Unicode
653 string from UnicodeStringTable
654 that matches the language
655 specified by Language.
657 @param Iso639Language Specify the language code
658 format supported. If true,
659 then the format follow ISO
660 639-2. If false, then it
663 @retval EFI_SUCCESS The Unicode string that
664 matches the language specified
665 by Language was found in the
666 table of Unicoide strings
667 UnicodeStringTable, and it was
668 returned in UnicodeString.
670 @retval EFI_INVALID_PARAMETER Language is NULL.
672 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
674 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
676 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.
678 @retval EFI_UNSUPPORTED The language specified by
679 Language is not a member
680 ofSupportedLanguages.
682 @retval EFI_UNSUPPORTED The language specified by
683 Language is not supported by
690 LookupUnicodeString2 (
691 IN CONST CHAR8
*Language
,
692 IN CONST CHAR8
*SupportedLanguages
,
693 IN CONST EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
,
694 OUT CHAR16
**UnicodeString
,
695 IN BOOLEAN Iso639Language
700 CHAR8
*LanguageString
;
703 // Make sure the parameters are valid
705 if (Language
== NULL
|| UnicodeString
== NULL
) {
706 return EFI_INVALID_PARAMETER
;
710 // If there are no supported languages, or the Unicode String Table is empty, then the
711 // Unicode String specified by Language is not supported by this Unicode String Table
713 if (SupportedLanguages
== NULL
|| UnicodeStringTable
== NULL
) {
714 return EFI_UNSUPPORTED
;
718 // Make sure Language is in the set of Supported Languages
721 while (*SupportedLanguages
!= 0) {
722 if (Iso639Language
) {
723 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
727 SupportedLanguages
+= 3;
729 for (Index
= 0; SupportedLanguages
[Index
] != 0 && SupportedLanguages
[Index
] != ';'; Index
++);
730 if ((AsciiStrnCmp(SupportedLanguages
, Language
, Index
) == 0) && (Language
[Index
] == 0)) {
734 SupportedLanguages
+= Index
;
735 for (; *SupportedLanguages
!= 0 && *SupportedLanguages
== ';'; SupportedLanguages
++);
740 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED
743 return EFI_UNSUPPORTED
;
747 // Search the Unicode String Table for the matching Language specifier
749 while (UnicodeStringTable
->Language
!= NULL
) {
750 LanguageString
= UnicodeStringTable
->Language
;
751 while (0 != *LanguageString
) {
752 for (Index
= 0 ;LanguageString
[Index
] != 0 && LanguageString
[Index
] != ';'; Index
++);
753 if (AsciiStrnCmp(LanguageString
, Language
, Index
) == 0) {
754 *UnicodeString
= UnicodeStringTable
->UnicodeString
;
757 LanguageString
+= Index
;
758 for (Index
= 0 ;LanguageString
[Index
] != 0 && LanguageString
[Index
] == ';'; Index
++);
760 UnicodeStringTable
++;
763 return EFI_UNSUPPORTED
;
769 This function adds a Unicode string to UnicodeStringTable.
770 If Language is a member of SupportedLanguages then
771 UnicodeString is added to UnicodeStringTable. New buffers are
772 allocated for both Language and UnicodeString. The contents
773 of Language and UnicodeString are copied into these new
774 buffers. These buffers are automatically freed when
775 FreeUnicodeStringTable() is called.
777 @param Language A pointer to the ISO 639-2
778 language code for the Unicode
781 @param SupportedLanguages A pointer to the set of ISO
782 639-2 language codes that the
783 Unicode string table supports.
784 Language must be a member of
787 @param UnicodeStringTable A pointer to the table of
790 @param UnicodeString A pointer to the Unicode
793 @retval EFI_SUCCESS The Unicode string that
794 matches the language specified
795 by Language was found in the
796 table of Unicode strings
797 UnicodeStringTable, and it was
798 returned in UnicodeString.
800 @retval EFI_INVALID_PARAMETER Language is NULL.
802 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
804 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.
806 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
808 @retval EFI_ALREADY_STARTED A Unicode string with language
809 Language is already present in
812 @retval EFI_OUT_OF_RESOURCES There is not enough memory to
813 add another Unicode string to
816 @retval EFI_UNSUPPORTED The language specified by
817 Language is not a member of
824 IN CONST CHAR8
*Language
,
825 IN CONST CHAR8
*SupportedLanguages
,
826 IN EFI_UNICODE_STRING_TABLE
**UnicodeStringTable
,
827 IN CONST CHAR16
*UnicodeString
830 UINTN NumberOfEntries
;
831 EFI_UNICODE_STRING_TABLE
*OldUnicodeStringTable
;
832 EFI_UNICODE_STRING_TABLE
*NewUnicodeStringTable
;
833 UINTN UnicodeStringLength
;
836 // Make sure the parameter are valid
838 if (Language
== NULL
|| UnicodeString
== NULL
|| UnicodeStringTable
== NULL
) {
839 return EFI_INVALID_PARAMETER
;
843 // If there are no supported languages, then a Unicode String can not be added
845 if (SupportedLanguages
== NULL
) {
846 return EFI_UNSUPPORTED
;
850 // If the Unicode String is empty, then a Unicode String can not be added
852 if (UnicodeString
[0] == 0) {
853 return EFI_INVALID_PARAMETER
;
857 // Make sure Language is a member of SupportedLanguages
859 while (*SupportedLanguages
!= 0) {
860 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
863 // Determine the size of the Unicode String Table by looking for a NULL Language entry
866 if (*UnicodeStringTable
!= NULL
) {
867 OldUnicodeStringTable
= *UnicodeStringTable
;
868 while (OldUnicodeStringTable
->Language
!= NULL
) {
869 if (CompareIso639LanguageCode (Language
, OldUnicodeStringTable
->Language
)) {
870 return EFI_ALREADY_STARTED
;
873 OldUnicodeStringTable
++;
879 // Allocate space for a new Unicode String Table. It must hold the current number of
880 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table
883 NewUnicodeStringTable
= AllocatePool ((NumberOfEntries
+ 2) * sizeof (EFI_UNICODE_STRING_TABLE
));
884 if (NewUnicodeStringTable
== NULL
) {
885 return EFI_OUT_OF_RESOURCES
;
889 // If the current Unicode String Table contains any entries, then copy them to the
890 // newly allocated Unicode String Table.
892 if (*UnicodeStringTable
!= NULL
) {
894 NewUnicodeStringTable
,
896 NumberOfEntries
* sizeof (EFI_UNICODE_STRING_TABLE
)
901 // Allocate space for a copy of the Language specifier
903 NewUnicodeStringTable
[NumberOfEntries
].Language
= AllocateCopyPool (3, Language
);
904 if (NewUnicodeStringTable
[NumberOfEntries
].Language
== NULL
) {
905 gBS
->FreePool (NewUnicodeStringTable
);
906 return EFI_OUT_OF_RESOURCES
;
910 // Compute the length of the Unicode String
912 for (UnicodeStringLength
= 0; UnicodeString
[UnicodeStringLength
] != 0; UnicodeStringLength
++)
916 // Allocate space for a copy of the Unicode String
918 NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
= AllocateCopyPool (
919 (UnicodeStringLength
+ 1) * sizeof (CHAR16
),
922 if (NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
== NULL
) {
923 gBS
->FreePool (NewUnicodeStringTable
[NumberOfEntries
].Language
);
924 gBS
->FreePool (NewUnicodeStringTable
);
925 return EFI_OUT_OF_RESOURCES
;
929 // Mark the end of the Unicode String Table
931 NewUnicodeStringTable
[NumberOfEntries
+ 1].Language
= NULL
;
932 NewUnicodeStringTable
[NumberOfEntries
+ 1].UnicodeString
= NULL
;
935 // Free the old Unicode String Table
937 if (*UnicodeStringTable
!= NULL
) {
938 gBS
->FreePool (*UnicodeStringTable
);
942 // Point UnicodeStringTable at the newly allocated Unicode String Table
944 *UnicodeStringTable
= NewUnicodeStringTable
;
949 SupportedLanguages
+= 3;
952 return EFI_UNSUPPORTED
;
958 This function adds a Unicode string to UnicodeStringTable.
959 If Language is a member of SupportedLanguages then
960 UnicodeString is added to UnicodeStringTable. New buffers are
961 allocated for both Language and UnicodeString. The contents
962 of Language and UnicodeString are copied into these new
963 buffers. These buffers are automatically freed when
964 FreeUnicodeStringTable() is called.
966 @param Language A pointer to the ISO 639-2 or
967 RFC 3066 language code for the
968 Unicode string to add.
970 @param SupportedLanguages A pointer to the set of ISO
971 639-2 or RFC 3.66 language
972 codes that the Unicode string
973 table supports. Language must
974 be a member of this set.
976 @param UnicodeStringTable A pointer to the table of
979 @param UnicodeString A pointer to the Unicode
982 @param Iso639Language Specify the language code
983 format supported. If true,
984 then the format follow ISO
985 639-2. If false, then it
988 @retval EFI_SUCCESS The Unicode string that
989 matches the language specified
990 by Language was found in the
991 table of Unicode strings
992 UnicodeStringTable, and it was
993 returned in UnicodeString.
995 @retval EFI_INVALID_PARAMETER Language is NULL.
997 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
999 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.
1001 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
1003 @retval EFI_ALREADY_STARTED A Unicode string with language
1004 Language is already present in
1007 @retval EFI_OUT_OF_RESOURCES There is not enough memory to
1008 add another Unicode string to
1011 @retval EFI_UNSUPPORTED The language specified by
1012 Language is not a member of
1019 IN CONST CHAR8
*Language
,
1020 IN CONST CHAR8
*SupportedLanguages
,
1021 IN EFI_UNICODE_STRING_TABLE
**UnicodeStringTable
,
1022 IN CONST CHAR16
*UnicodeString
,
1023 IN BOOLEAN Iso639Language
1026 UINTN NumberOfEntries
;
1027 EFI_UNICODE_STRING_TABLE
*OldUnicodeStringTable
;
1028 EFI_UNICODE_STRING_TABLE
*NewUnicodeStringTable
;
1029 UINTN UnicodeStringLength
;
1032 CHAR8
*LanguageString
;
1035 // Make sure the parameter are valid
1037 if (Language
== NULL
|| UnicodeString
== NULL
|| UnicodeStringTable
== NULL
) {
1038 return EFI_INVALID_PARAMETER
;
1042 // If there are no supported languages, then a Unicode String can not be added
1044 if (SupportedLanguages
== NULL
) {
1045 return EFI_UNSUPPORTED
;
1049 // If the Unicode String is empty, then a Unicode String can not be added
1051 if (UnicodeString
[0] == 0) {
1052 return EFI_INVALID_PARAMETER
;
1056 // Make sure Language is a member of SupportedLanguages
1059 while (*SupportedLanguages
!= 0) {
1060 if (Iso639Language
) {
1061 if (CompareIso639LanguageCode (Language
, SupportedLanguages
)) {
1065 SupportedLanguages
+= 3;
1067 for (Index
= 0; SupportedLanguages
[Index
] != 0 && SupportedLanguages
[Index
] != ';'; Index
++);
1068 if (AsciiStrnCmp(SupportedLanguages
, Language
, Index
) == 0) {
1072 SupportedLanguages
+= Index
;
1073 for (; *SupportedLanguages
!= 0 && *SupportedLanguages
== ';'; SupportedLanguages
++);
1078 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED
1081 return EFI_UNSUPPORTED
;
1085 // Determine the size of the Unicode String Table by looking for a NULL Language entry
1087 NumberOfEntries
= 0;
1088 if (*UnicodeStringTable
!= NULL
) {
1089 OldUnicodeStringTable
= *UnicodeStringTable
;
1090 while (OldUnicodeStringTable
->Language
!= NULL
) {
1091 LanguageString
= OldUnicodeStringTable
->Language
;
1093 while (*LanguageString
!= 0) {
1094 for (Index
= 0; LanguageString
[Index
] != 0 && LanguageString
[Index
] != ';'; Index
++);
1096 if (AsciiStrnCmp (Language
, LanguageString
, Index
) == 0) {
1097 return EFI_ALREADY_STARTED
;
1099 LanguageString
+= Index
;
1100 for (; *LanguageString
!= 0 && *LanguageString
== ';'; LanguageString
++);
1102 OldUnicodeStringTable
++;
1108 // Allocate space for a new Unicode String Table. It must hold the current number of
1109 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table
1112 NewUnicodeStringTable
= AllocatePool ((NumberOfEntries
+ 2) * sizeof (EFI_UNICODE_STRING_TABLE
));
1113 if (NewUnicodeStringTable
== NULL
) {
1114 return EFI_OUT_OF_RESOURCES
;
1118 // If the current Unicode String Table contains any entries, then copy them to the
1119 // newly allocated Unicode String Table.
1121 if (*UnicodeStringTable
!= NULL
) {
1123 NewUnicodeStringTable
,
1124 *UnicodeStringTable
,
1125 NumberOfEntries
* sizeof (EFI_UNICODE_STRING_TABLE
)
1130 // Allocate space for a copy of the Language specifier
1132 NewUnicodeStringTable
[NumberOfEntries
].Language
= AllocateCopyPool (AsciiStrSize(Language
), Language
);
1133 if (NewUnicodeStringTable
[NumberOfEntries
].Language
== NULL
) {
1134 gBS
->FreePool (NewUnicodeStringTable
);
1135 return EFI_OUT_OF_RESOURCES
;
1139 // Compute the length of the Unicode String
1141 for (UnicodeStringLength
= 0; UnicodeString
[UnicodeStringLength
] != 0; UnicodeStringLength
++);
1144 // Allocate space for a copy of the Unicode String
1146 NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
= AllocateCopyPool (StrSize (UnicodeString
), UnicodeString
);
1147 if (NewUnicodeStringTable
[NumberOfEntries
].UnicodeString
== NULL
) {
1148 gBS
->FreePool (NewUnicodeStringTable
[NumberOfEntries
].Language
);
1149 gBS
->FreePool (NewUnicodeStringTable
);
1150 return EFI_OUT_OF_RESOURCES
;
1154 // Mark the end of the Unicode String Table
1156 NewUnicodeStringTable
[NumberOfEntries
+ 1].Language
= NULL
;
1157 NewUnicodeStringTable
[NumberOfEntries
+ 1].UnicodeString
= NULL
;
1160 // Free the old Unicode String Table
1162 if (*UnicodeStringTable
!= NULL
) {
1163 gBS
->FreePool (*UnicodeStringTable
);
1167 // Point UnicodeStringTable at the newly allocated Unicode String Table
1169 *UnicodeStringTable
= NewUnicodeStringTable
;
1175 This function frees the table of Unicode strings in UnicodeStringTable.
1176 If UnicodeStringTable is NULL, then EFI_SUCCESS is returned.
1177 Otherwise, each language code, and each Unicode string in the Unicode string
1178 table are freed, and EFI_SUCCESS is returned.
1180 @param UnicodeStringTable A pointer to the table of Unicode strings.
1182 @retval EFI_SUCCESS The Unicode string table was freed.
1187 FreeUnicodeStringTable (
1188 IN EFI_UNICODE_STRING_TABLE
*UnicodeStringTable
1194 // If the Unicode String Table is NULL, then it is already freed
1196 if (UnicodeStringTable
== NULL
) {
1201 // Loop through the Unicode String Table until we reach the end of table marker
1203 for (Index
= 0; UnicodeStringTable
[Index
].Language
!= NULL
; Index
++) {
1206 // Free the Language string from the Unicode String Table
1208 gBS
->FreePool (UnicodeStringTable
[Index
].Language
);
1211 // Free the Unicode String from the Unicode String Table
1213 if (UnicodeStringTable
[Index
].UnicodeString
!= NULL
) {
1214 gBS
->FreePool (UnicodeStringTable
[Index
].UnicodeString
);
1219 // Free the Unicode String Table itself
1221 gBS
->FreePool (UnicodeStringTable
);