]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/UefiLib/UefiLib.c
e3fff09de3953bd60da396a2757f7c6ab5937f48
[mirror_edk2.git] / MdePkg / Library / UefiLib / UefiLib.c
1 /** @file
2 Mde UEFI library functions.
3
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
9
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.
12
13 **/
14
15 //
16 // Include common header file for this module.
17 //
18 #include "UefiLibInternal.h"
19
20 /**
21 Compare whether two names of languages are identical.
22
23 @param Language1 Name of language 1.
24 @param Language2 Name of language 2.
25
26 @retval TRUE Language 1 and language 2 are the same.
27 @retval FALSE Language 1 and language 2 are not the same.
28
29 **/
30 BOOLEAN
31 CompareIso639LanguageCode (
32 IN CONST CHAR8 *Language1,
33 IN CONST CHAR8 *Language2
34 )
35 {
36 UINT32 Name1;
37 UINT32 Name2;
38
39 Name1 = ReadUnaligned24 ((CONST UINT32 *) Language1);
40 Name2 = ReadUnaligned24 ((CONST UINT32 *) Language2);
41
42 return (BOOLEAN) (Name1 == Name2);
43 }
44
45 /**
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().
52
53 @param TableGuid Pointer to table's GUID type..
54 @param Table Pointer to the table associated with TableGuid in the EFI System Table.
55
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.
58
59 **/
60 EFI_STATUS
61 EFIAPI
62 EfiGetSystemConfigurationTable (
63 IN EFI_GUID *TableGuid,
64 OUT VOID **Table
65 )
66 {
67 EFI_SYSTEM_TABLE *SystemTable;
68 UINTN Index;
69
70 ASSERT (TableGuid != NULL);
71 ASSERT (Table != NULL);
72
73 SystemTable = gST;
74 *Table = NULL;
75 for (Index = 0; Index < SystemTable->NumberOfTableEntries; Index++) {
76 if (CompareGuid (TableGuid, &(SystemTable->ConfigurationTable[Index].VendorGuid))) {
77 *Table = SystemTable->ConfigurationTable[Index].VendorTable;
78 return EFI_SUCCESS;
79 }
80 }
81
82 return EFI_NOT_FOUND;
83 }
84
85 /**
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.
90
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.
96
97 @return The notification event that was created.
98
99 **/
100 EFI_EVENT
101 EFIAPI
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
108 )
109 {
110 EFI_STATUS Status;
111 EFI_EVENT Event;
112
113 //
114 // Create the event
115 //
116
117 Status = gBS->CreateEvent (
118 EVT_NOTIFY_SIGNAL,
119 NotifyTpl,
120 NotifyFunction,
121 NotifyContext,
122 &Event
123 );
124 ASSERT_EFI_ERROR (Status);
125
126 //
127 // Register for protocol notifactions on this event
128 //
129
130 Status = gBS->RegisterProtocolNotify (
131 ProtocolGuid,
132 Event,
133 Registration
134 );
135
136 ASSERT_EFI_ERROR (Status);
137
138 //
139 // Kick the event so we will perform an initial pass of
140 // current installed drivers
141 //
142
143 gBS->SignalEvent (Event);
144 return Event;
145 }
146
147 /**
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().
154
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.
160
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.
163
164 **/
165 EFI_STATUS
166 EFIAPI
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
173 )
174 {
175 EFI_STATUS Status;
176 EFI_EVENT Event;
177 VOID *RegistrationLocal;
178
179 ASSERT (Name != NULL);
180 ASSERT (NotifyFunction != NULL);
181 ASSERT (NotifyTpl <= TPL_HIGH_LEVEL);
182
183 //
184 // Create event
185 //
186 Status = gBS->CreateEvent (
187 EVT_NOTIFY_SIGNAL,
188 NotifyTpl,
189 NotifyFunction,
190 (VOID *) NotifyContext,
191 &Event
192 );
193 ASSERT_EFI_ERROR (Status);
194
195 //
196 // The Registration is not optional to RegisterProtocolNotify().
197 // To make it optional to EfiNamedEventListen(), may need to substitute with a local.
198 //
199 if (Registration != NULL) {
200 RegistrationLocal = Registration;
201 } else {
202 RegistrationLocal = &RegistrationLocal;
203 }
204
205 //
206 // Register for an installation of protocol interface
207 //
208
209 Status = gBS->RegisterProtocolNotify (
210 (EFI_GUID *) Name,
211 Event,
212 RegistrationLocal
213 );
214 ASSERT_EFI_ERROR (Status);
215
216 return Status;
217 }
218
219 /**
220 This function signals the named event specified by Name. The named event must
221 have been created with EfiNamedEventListen().
222
223 @param Name Supplies GUID name of the event.
224
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.
227
228 **/
229 EFI_STATUS
230 EFIAPI
231 EfiNamedEventSignal (
232 IN CONST EFI_GUID *Name
233 )
234 {
235 EFI_STATUS Status;
236 EFI_HANDLE Handle;
237
238 Handle = NULL;
239 Status = gBS->InstallProtocolInterface (
240 &Handle,
241 (EFI_GUID *) Name,
242 EFI_NATIVE_INTERFACE,
243 NULL
244 );
245 ASSERT_EFI_ERROR (Status);
246
247 Status = gBS->UninstallProtocolInterface (
248 Handle,
249 (EFI_GUID *) Name,
250 NULL
251 );
252 ASSERT_EFI_ERROR (Status);
253
254 return Status;
255 }
256
257 /**
258 Returns the current TPL.
259
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
264 to RestoreTPL().
265
266 @param VOID
267
268 @retval EFI_TPL The current TPL.
269
270 **/
271 EFI_TPL
272 EFIAPI
273 EfiGetCurrentTpl (
274 VOID
275 )
276 {
277 EFI_TPL Tpl;
278
279 Tpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
280 gBS->RestoreTPL (Tpl);
281
282 return Tpl;
283 }
284
285
286 /**
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().
293
294 @param Lock A pointer to the lock data structure to initialize.
295 @param Priority EFI TPL associated with the lock.
296
297 @return The lock.
298
299 **/
300 EFI_LOCK *
301 EFIAPI
302 EfiInitializeLock (
303 IN OUT EFI_LOCK *Lock,
304 IN EFI_TPL Priority
305 )
306 {
307 ASSERT (Lock != NULL);
308 ASSERT (Priority <= TPL_HIGH_LEVEL);
309
310 Lock->Tpl = Priority;
311 Lock->OwnerTpl = TPL_APPLICATION;
312 Lock->Lock = EfiLockReleased ;
313 return Lock;
314 }
315
316 /**
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
319 acquired state.
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().
323
324 @param Lock The task lock with priority level.
325
326 **/
327 VOID
328 EFIAPI
329 EfiAcquireLock (
330 IN EFI_LOCK *Lock
331 )
332 {
333 ASSERT (Lock != NULL);
334 ASSERT (Lock->Lock == EfiLockReleased);
335
336 Lock->OwnerTpl = gBS->RaiseTPL (Lock->Tpl);
337 Lock->Lock = EfiLockAcquired;
338 }
339
340 /**
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.
344
345 @param Lock A pointer to the lock to acquire.
346
347 @retval EFI_SUCCESS The lock was acquired.
348 @retval EFI_ACCESS_DENIED The lock could not be acquired because it is already owned.
349
350 **/
351 EFI_STATUS
352 EFIAPI
353 EfiAcquireLockOrFail (
354 IN EFI_LOCK *Lock
355 )
356 {
357
358 ASSERT (Lock != NULL);
359 ASSERT (Lock->Lock != EfiLockUninitialized);
360
361 if (Lock->Lock == EfiLockAcquired) {
362 //
363 // Lock is already owned, so bail out
364 //
365 return EFI_ACCESS_DENIED;
366 }
367
368 Lock->OwnerTpl = gBS->RaiseTPL (Lock->Tpl);
369
370 Lock->Lock = EfiLockAcquired;
371
372 return EFI_SUCCESS;
373 }
374
375 /**
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
378 previous level.
379
380 @param Lock A pointer to the lock to release.
381
382 **/
383 VOID
384 EFIAPI
385 EfiReleaseLock (
386 IN EFI_LOCK *Lock
387 )
388 {
389 EFI_TPL Tpl;
390
391 ASSERT (Lock != NULL);
392 ASSERT (Lock->Lock == EfiLockAcquired);
393
394 Tpl = Lock->OwnerTpl;
395
396 Lock->Lock = EfiLockReleased;
397
398 gBS->RestoreTPL (Tpl);
399 }
400
401 /**
402 Tests whether a controller handle is being managed by a specific driver.
403
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().
410
411 @param ControllerHandle A handle for a controller to test.
412 @param DriverBindingHandle Specifies the driver binding handle for the
413 driver.
414 @param ProtocolGuid Specifies the protocol that the driver specified
415 by DriverBindingHandle opens in its Start()
416 function.
417
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.
422
423 **/
424 EFI_STATUS
425 EFIAPI
426 EfiTestManagedDevice (
427 IN CONST EFI_HANDLE ControllerHandle,
428 IN CONST EFI_HANDLE DriverBindingHandle,
429 IN CONST EFI_GUID *ProtocolGuid
430 )
431 {
432 EFI_STATUS Status;
433 VOID *ManagedInterface;
434
435 ASSERT (ProtocolGuid != NULL);
436
437 Status = gBS->OpenProtocol (
438 ControllerHandle,
439 (EFI_GUID *) ProtocolGuid,
440 &ManagedInterface,
441 DriverBindingHandle,
442 ControllerHandle,
443 EFI_OPEN_PROTOCOL_BY_DRIVER
444 );
445 if (!EFI_ERROR (Status)) {
446 gBS->CloseProtocol (
447 ControllerHandle,
448 (EFI_GUID *) ProtocolGuid,
449 DriverBindingHandle,
450 ControllerHandle
451 );
452 return EFI_UNSUPPORTED;
453 }
454
455 if (Status != EFI_ALREADY_STARTED) {
456 return EFI_UNSUPPORTED;
457 }
458
459 return EFI_SUCCESS;
460 }
461
462 /**
463 Tests whether a child handle is a child device of the controller.
464
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().
470
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.
475
476 @retval EFI_SUCCESS ChildHandle is a child of the ControllerHandle.
477 @retval EFI_UNSUPPORTED ChildHandle is not a child of the
478 ControllerHandle.
479
480 **/
481 EFI_STATUS
482 EFIAPI
483 EfiTestChildHandle (
484 IN CONST EFI_HANDLE ControllerHandle,
485 IN CONST EFI_HANDLE ChildHandle,
486 IN CONST EFI_GUID *ProtocolGuid
487 )
488 {
489 EFI_STATUS Status;
490 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfoBuffer;
491 UINTN EntryCount;
492 UINTN Index;
493
494 ASSERT (ProtocolGuid != NULL);
495
496 //
497 // Retrieve the list of agents that are consuming the specific protocol
498 // on ControllerHandle.
499 //
500 Status = gBS->OpenProtocolInformation (
501 ControllerHandle,
502 (EFI_GUID *) ProtocolGuid,
503 &OpenInfoBuffer,
504 &EntryCount
505 );
506 if (EFI_ERROR (Status)) {
507 return EFI_UNSUPPORTED;
508 }
509
510 //
511 // Inspect if ChildHandle is one of the agents.
512 //
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;
518 break;
519 }
520 }
521
522 FreePool (OpenInfoBuffer);
523 return Status;
524 }
525
526 /**
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
531 UnicodeString.
532
533 @param Language A pointer to the ISO 639-2
534 language code for the Unicode
535 string to look up and return.
536
537 @param SupportedLanguages A pointer to the set of ISO
538 639-2language
539 codes that the Unicode string
540 table supports. Language must
541 be a member of this set.
542
543 @param UnicodeStringTable A pointer to the table of
544 Unicode strings.
545
546 @param UnicodeString A pointer to the Unicode
547 string from UnicodeStringTable
548 that matches the language
549 specified by Language.
550
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.
557
558 @retval EFI_INVALID_PARAMETER Language is NULL.
559
560 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
561 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
562
563 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.
564
565 @retval EFI_UNSUPPORTED The language specified by
566 Language is not a member
567 ofSupportedLanguages.
568
569 @retval EFI_UNSUPPORTED The language specified by
570 Language is not supported by
571 UnicodeStringTable.
572
573 **/
574 EFI_STATUS
575 EFIAPI
576 LookupUnicodeString (
577 IN CONST CHAR8 *Language,
578 IN CONST CHAR8 *SupportedLanguages,
579 IN CONST EFI_UNICODE_STRING_TABLE *UnicodeStringTable,
580 OUT CHAR16 **UnicodeString
581 )
582 {
583 //
584 // Make sure the parameters are valid
585 //
586 if (Language == NULL || UnicodeString == NULL) {
587 return EFI_INVALID_PARAMETER;
588 }
589
590 //
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
593 //
594 if (SupportedLanguages == NULL || UnicodeStringTable == NULL) {
595 return EFI_UNSUPPORTED;
596 }
597
598 //
599 // Make sure Language is in the set of Supported Languages
600 //
601 while (*SupportedLanguages != 0) {
602 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {
603
604 //
605 // Search the Unicode String Table for the matching Language specifier
606 //
607 while (UnicodeStringTable->Language != NULL) {
608 if (CompareIso639LanguageCode (Language, UnicodeStringTable->Language)) {
609
610 //
611 // A matching string was found, so return it
612 //
613 *UnicodeString = UnicodeStringTable->UnicodeString;
614 return EFI_SUCCESS;
615 }
616
617 UnicodeStringTable++;
618 }
619
620 return EFI_UNSUPPORTED;
621 }
622
623 SupportedLanguages += 3;
624 }
625
626 return EFI_UNSUPPORTED;
627 }
628
629
630
631 /**
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
636 UnicodeString.
637
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
641 return.
642
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.
648
649 @param UnicodeStringTable A pointer to the table of
650 Unicode strings.
651
652 @param UnicodeString A pointer to the Unicode
653 string from UnicodeStringTable
654 that matches the language
655 specified by Language.
656
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
661 follows RFC3066.
662
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.
669
670 @retval EFI_INVALID_PARAMETER Language is NULL.
671
672 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
673
674 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
675
676 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.
677
678 @retval EFI_UNSUPPORTED The language specified by
679 Language is not a member
680 ofSupportedLanguages.
681
682 @retval EFI_UNSUPPORTED The language specified by
683 Language is not supported by
684 UnicodeStringTable.
685
686 **/
687 EFI_STATUS
688
689 EFIAPI
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
696 )
697 {
698 BOOLEAN Found;
699 UINTN Index;
700 CHAR8 *LanguageString;
701
702 //
703 // Make sure the parameters are valid
704 //
705 if (Language == NULL || UnicodeString == NULL) {
706 return EFI_INVALID_PARAMETER;
707 }
708
709 //
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
712 //
713 if (SupportedLanguages == NULL || UnicodeStringTable == NULL) {
714 return EFI_UNSUPPORTED;
715 }
716
717 //
718 // Make sure Language is in the set of Supported Languages
719 //
720 Found = FALSE;
721 while (*SupportedLanguages != 0) {
722 if (Iso639Language) {
723 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {
724 Found = TRUE;
725 break;
726 }
727 SupportedLanguages += 3;
728 } else {
729 for (Index = 0; SupportedLanguages[Index] != 0 && SupportedLanguages[Index] != ';'; Index++);
730 if ((AsciiStrnCmp(SupportedLanguages, Language, Index) == 0) && (Language[Index] == 0)) {
731 Found = TRUE;
732 break;
733 }
734 SupportedLanguages += Index;
735 for (; *SupportedLanguages != 0 && *SupportedLanguages == ';'; SupportedLanguages++);
736 }
737 }
738
739 //
740 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED
741 //
742 if (!Found) {
743 return EFI_UNSUPPORTED;
744 }
745
746 //
747 // Search the Unicode String Table for the matching Language specifier
748 //
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;
755 return EFI_SUCCESS;
756 }
757 LanguageString += Index;
758 for (Index = 0 ;LanguageString[Index] != 0 && LanguageString[Index] == ';'; Index++);
759 }
760 UnicodeStringTable++;
761 }
762
763 return EFI_UNSUPPORTED;
764 }
765
766
767 /**
768
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.
776
777 @param Language A pointer to the ISO 639-2
778 language code for the Unicode
779 string to add.
780
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
785 this set.
786
787 @param UnicodeStringTable A pointer to the table of
788 Unicode strings.
789
790 @param UnicodeString A pointer to the Unicode
791 string to add.
792
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.
799
800 @retval EFI_INVALID_PARAMETER Language is NULL.
801
802 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
803
804 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.
805
806 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
807
808 @retval EFI_ALREADY_STARTED A Unicode string with language
809 Language is already present in
810 UnicodeStringTable.
811
812 @retval EFI_OUT_OF_RESOURCES There is not enough memory to
813 add another Unicode string to
814 UnicodeStringTable.
815
816 @retval EFI_UNSUPPORTED The language specified by
817 Language is not a member of
818 SupportedLanguages.
819
820 **/
821 EFI_STATUS
822 EFIAPI
823 AddUnicodeString (
824 IN CONST CHAR8 *Language,
825 IN CONST CHAR8 *SupportedLanguages,
826 IN EFI_UNICODE_STRING_TABLE **UnicodeStringTable,
827 IN CONST CHAR16 *UnicodeString
828 )
829 {
830 UINTN NumberOfEntries;
831 EFI_UNICODE_STRING_TABLE *OldUnicodeStringTable;
832 EFI_UNICODE_STRING_TABLE *NewUnicodeStringTable;
833 UINTN UnicodeStringLength;
834
835 //
836 // Make sure the parameter are valid
837 //
838 if (Language == NULL || UnicodeString == NULL || UnicodeStringTable == NULL) {
839 return EFI_INVALID_PARAMETER;
840 }
841
842 //
843 // If there are no supported languages, then a Unicode String can not be added
844 //
845 if (SupportedLanguages == NULL) {
846 return EFI_UNSUPPORTED;
847 }
848
849 //
850 // If the Unicode String is empty, then a Unicode String can not be added
851 //
852 if (UnicodeString[0] == 0) {
853 return EFI_INVALID_PARAMETER;
854 }
855
856 //
857 // Make sure Language is a member of SupportedLanguages
858 //
859 while (*SupportedLanguages != 0) {
860 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {
861
862 //
863 // Determine the size of the Unicode String Table by looking for a NULL Language entry
864 //
865 NumberOfEntries = 0;
866 if (*UnicodeStringTable != NULL) {
867 OldUnicodeStringTable = *UnicodeStringTable;
868 while (OldUnicodeStringTable->Language != NULL) {
869 if (CompareIso639LanguageCode (Language, OldUnicodeStringTable->Language)) {
870 return EFI_ALREADY_STARTED;
871 }
872
873 OldUnicodeStringTable++;
874 NumberOfEntries++;
875 }
876 }
877
878 //
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
881 // marker
882 //
883 NewUnicodeStringTable = AllocatePool ((NumberOfEntries + 2) * sizeof (EFI_UNICODE_STRING_TABLE));
884 if (NewUnicodeStringTable == NULL) {
885 return EFI_OUT_OF_RESOURCES;
886 }
887
888 //
889 // If the current Unicode String Table contains any entries, then copy them to the
890 // newly allocated Unicode String Table.
891 //
892 if (*UnicodeStringTable != NULL) {
893 CopyMem (
894 NewUnicodeStringTable,
895 *UnicodeStringTable,
896 NumberOfEntries * sizeof (EFI_UNICODE_STRING_TABLE)
897 );
898 }
899
900 //
901 // Allocate space for a copy of the Language specifier
902 //
903 NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (3, Language);
904 if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) {
905 gBS->FreePool (NewUnicodeStringTable);
906 return EFI_OUT_OF_RESOURCES;
907 }
908
909 //
910 // Compute the length of the Unicode String
911 //
912 for (UnicodeStringLength = 0; UnicodeString[UnicodeStringLength] != 0; UnicodeStringLength++)
913 ;
914
915 //
916 // Allocate space for a copy of the Unicode String
917 //
918 NewUnicodeStringTable[NumberOfEntries].UnicodeString = AllocateCopyPool (
919 (UnicodeStringLength + 1) * sizeof (CHAR16),
920 UnicodeString
921 );
922 if (NewUnicodeStringTable[NumberOfEntries].UnicodeString == NULL) {
923 gBS->FreePool (NewUnicodeStringTable[NumberOfEntries].Language);
924 gBS->FreePool (NewUnicodeStringTable);
925 return EFI_OUT_OF_RESOURCES;
926 }
927
928 //
929 // Mark the end of the Unicode String Table
930 //
931 NewUnicodeStringTable[NumberOfEntries + 1].Language = NULL;
932 NewUnicodeStringTable[NumberOfEntries + 1].UnicodeString = NULL;
933
934 //
935 // Free the old Unicode String Table
936 //
937 if (*UnicodeStringTable != NULL) {
938 gBS->FreePool (*UnicodeStringTable);
939 }
940
941 //
942 // Point UnicodeStringTable at the newly allocated Unicode String Table
943 //
944 *UnicodeStringTable = NewUnicodeStringTable;
945
946 return EFI_SUCCESS;
947 }
948
949 SupportedLanguages += 3;
950 }
951
952 return EFI_UNSUPPORTED;
953 }
954
955
956 /**
957
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.
965
966 @param Language A pointer to the ISO 639-2 or
967 RFC 3066 language code for the
968 Unicode string to add.
969
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.
975
976 @param UnicodeStringTable A pointer to the table of
977 Unicode strings.
978
979 @param UnicodeString A pointer to the Unicode
980 string to add.
981
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
986 follows RFC3066.
987
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.
994
995 @retval EFI_INVALID_PARAMETER Language is NULL.
996
997 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
998
999 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.
1000
1001 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
1002
1003 @retval EFI_ALREADY_STARTED A Unicode string with language
1004 Language is already present in
1005 UnicodeStringTable.
1006
1007 @retval EFI_OUT_OF_RESOURCES There is not enough memory to
1008 add another Unicode string to
1009 UnicodeStringTable.
1010
1011 @retval EFI_UNSUPPORTED The language specified by
1012 Language is not a member of
1013 SupportedLanguages.
1014
1015 **/
1016 EFI_STATUS
1017 EFIAPI
1018 AddUnicodeString2 (
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
1024 )
1025 {
1026 UINTN NumberOfEntries;
1027 EFI_UNICODE_STRING_TABLE *OldUnicodeStringTable;
1028 EFI_UNICODE_STRING_TABLE *NewUnicodeStringTable;
1029 UINTN UnicodeStringLength;
1030 BOOLEAN Found;
1031 UINTN Index;
1032 CHAR8 *LanguageString;
1033
1034 //
1035 // Make sure the parameter are valid
1036 //
1037 if (Language == NULL || UnicodeString == NULL || UnicodeStringTable == NULL) {
1038 return EFI_INVALID_PARAMETER;
1039 }
1040
1041 //
1042 // If there are no supported languages, then a Unicode String can not be added
1043 //
1044 if (SupportedLanguages == NULL) {
1045 return EFI_UNSUPPORTED;
1046 }
1047
1048 //
1049 // If the Unicode String is empty, then a Unicode String can not be added
1050 //
1051 if (UnicodeString[0] == 0) {
1052 return EFI_INVALID_PARAMETER;
1053 }
1054
1055 //
1056 // Make sure Language is a member of SupportedLanguages
1057 //
1058 Found = FALSE;
1059 while (*SupportedLanguages != 0) {
1060 if (Iso639Language) {
1061 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {
1062 Found = TRUE;
1063 break;
1064 }
1065 SupportedLanguages += 3;
1066 } else {
1067 for (Index = 0; SupportedLanguages[Index] != 0 && SupportedLanguages[Index] != ';'; Index++);
1068 if (AsciiStrnCmp(SupportedLanguages, Language, Index) == 0) {
1069 Found = TRUE;
1070 break;
1071 }
1072 SupportedLanguages += Index;
1073 for (; *SupportedLanguages != 0 && *SupportedLanguages == ';'; SupportedLanguages++);
1074 }
1075 }
1076
1077 //
1078 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED
1079 //
1080 if (!Found) {
1081 return EFI_UNSUPPORTED;
1082 }
1083
1084 //
1085 // Determine the size of the Unicode String Table by looking for a NULL Language entry
1086 //
1087 NumberOfEntries = 0;
1088 if (*UnicodeStringTable != NULL) {
1089 OldUnicodeStringTable = *UnicodeStringTable;
1090 while (OldUnicodeStringTable->Language != NULL) {
1091 LanguageString = OldUnicodeStringTable->Language;
1092
1093 while (*LanguageString != 0) {
1094 for (Index = 0; LanguageString[Index] != 0 && LanguageString[Index] != ';'; Index++);
1095
1096 if (AsciiStrnCmp (Language, LanguageString, Index) == 0) {
1097 return EFI_ALREADY_STARTED;
1098 }
1099 LanguageString += Index;
1100 for (; *LanguageString != 0 && *LanguageString == ';'; LanguageString++);
1101 }
1102 OldUnicodeStringTable++;
1103 NumberOfEntries++;
1104 }
1105 }
1106
1107 //
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
1110 // marker
1111 //
1112 NewUnicodeStringTable = AllocatePool ((NumberOfEntries + 2) * sizeof (EFI_UNICODE_STRING_TABLE));
1113 if (NewUnicodeStringTable == NULL) {
1114 return EFI_OUT_OF_RESOURCES;
1115 }
1116
1117 //
1118 // If the current Unicode String Table contains any entries, then copy them to the
1119 // newly allocated Unicode String Table.
1120 //
1121 if (*UnicodeStringTable != NULL) {
1122 CopyMem (
1123 NewUnicodeStringTable,
1124 *UnicodeStringTable,
1125 NumberOfEntries * sizeof (EFI_UNICODE_STRING_TABLE)
1126 );
1127 }
1128
1129 //
1130 // Allocate space for a copy of the Language specifier
1131 //
1132 NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (AsciiStrSize(Language), Language);
1133 if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) {
1134 gBS->FreePool (NewUnicodeStringTable);
1135 return EFI_OUT_OF_RESOURCES;
1136 }
1137
1138 //
1139 // Compute the length of the Unicode String
1140 //
1141 for (UnicodeStringLength = 0; UnicodeString[UnicodeStringLength] != 0; UnicodeStringLength++);
1142
1143 //
1144 // Allocate space for a copy of the Unicode String
1145 //
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;
1151 }
1152
1153 //
1154 // Mark the end of the Unicode String Table
1155 //
1156 NewUnicodeStringTable[NumberOfEntries + 1].Language = NULL;
1157 NewUnicodeStringTable[NumberOfEntries + 1].UnicodeString = NULL;
1158
1159 //
1160 // Free the old Unicode String Table
1161 //
1162 if (*UnicodeStringTable != NULL) {
1163 gBS->FreePool (*UnicodeStringTable);
1164 }
1165
1166 //
1167 // Point UnicodeStringTable at the newly allocated Unicode String Table
1168 //
1169 *UnicodeStringTable = NewUnicodeStringTable;
1170
1171 return EFI_SUCCESS;
1172 }
1173
1174 /**
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.
1179
1180 @param UnicodeStringTable A pointer to the table of Unicode strings.
1181
1182 @retval EFI_SUCCESS The Unicode string table was freed.
1183
1184 **/
1185 EFI_STATUS
1186 EFIAPI
1187 FreeUnicodeStringTable (
1188 IN EFI_UNICODE_STRING_TABLE *UnicodeStringTable
1189 )
1190 {
1191 UINTN Index;
1192
1193 //
1194 // If the Unicode String Table is NULL, then it is already freed
1195 //
1196 if (UnicodeStringTable == NULL) {
1197 return EFI_SUCCESS;
1198 }
1199
1200 //
1201 // Loop through the Unicode String Table until we reach the end of table marker
1202 //
1203 for (Index = 0; UnicodeStringTable[Index].Language != NULL; Index++) {
1204
1205 //
1206 // Free the Language string from the Unicode String Table
1207 //
1208 gBS->FreePool (UnicodeStringTable[Index].Language);
1209
1210 //
1211 // Free the Unicode String from the Unicode String Table
1212 //
1213 if (UnicodeStringTable[Index].UnicodeString != NULL) {
1214 gBS->FreePool (UnicodeStringTable[Index].UnicodeString);
1215 }
1216 }
1217
1218 //
1219 // Free the Unicode String Table itself
1220 //
1221 gBS->FreePool (UnicodeStringTable);
1222
1223 return EFI_SUCCESS;
1224 }
1225
1226