]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/UefiLib/UefiLib.c
Code Scrub for MdePkg.
[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
51 @param TableGuid Pointer to table's GUID type..
52 @param Table Pointer to the table associated with TableGuid in the EFI System Table.
53
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.
56
57 **/
58 EFI_STATUS
59 EFIAPI
60 EfiGetSystemConfigurationTable (
61 IN EFI_GUID *TableGuid,
62 OUT VOID **Table
63 )
64 {
65 EFI_SYSTEM_TABLE *SystemTable;
66 UINTN Index;
67
68 ASSERT (TableGuid != NULL);
69 ASSERT (Table != NULL);
70
71 SystemTable = gST;
72 *Table = NULL;
73 for (Index = 0; Index < SystemTable->NumberOfTableEntries; Index++) {
74 if (CompareGuid (TableGuid, &(SystemTable->ConfigurationTable[Index].VendorGuid))) {
75 *Table = SystemTable->ConfigurationTable[Index].VendorTable;
76 return EFI_SUCCESS;
77 }
78 }
79
80 return EFI_NOT_FOUND;
81 }
82
83 /**
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.
88
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.
94
95 @return The notification event that was created.
96
97 **/
98 EFI_EVENT
99 EFIAPI
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
106 )
107 {
108 EFI_STATUS Status;
109 EFI_EVENT Event;
110
111 //
112 // Create the event
113 //
114
115 Status = gBS->CreateEvent (
116 EVT_NOTIFY_SIGNAL,
117 NotifyTpl,
118 NotifyFunction,
119 NotifyContext,
120 &Event
121 );
122 ASSERT_EFI_ERROR (Status);
123
124 //
125 // Register for protocol notifactions on this event
126 //
127
128 Status = gBS->RegisterProtocolNotify (
129 ProtocolGuid,
130 Event,
131 Registration
132 );
133
134 ASSERT_EFI_ERROR (Status);
135
136 //
137 // Kick the event so we will perform an initial pass of
138 // current installed drivers
139 //
140
141 gBS->SignalEvent (Event);
142 return Event;
143 }
144
145 /**
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.
149
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.
155
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.
158
159 **/
160 EFI_STATUS
161 EFIAPI
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
168 )
169 {
170 EFI_STATUS Status;
171 EFI_EVENT Event;
172 VOID *RegistrationLocal;
173
174 //
175 // Create event
176 //
177 Status = gBS->CreateEvent (
178 EVT_NOTIFY_SIGNAL,
179 NotifyTpl,
180 NotifyFunction,
181 (VOID *) NotifyContext,
182 &Event
183 );
184 ASSERT_EFI_ERROR (Status);
185
186 //
187 // The Registration is not optional to RegisterProtocolNotify().
188 // To make it optional to EfiNamedEventListen(), may need to substitute with a local.
189 //
190 if (Registration != NULL) {
191 RegistrationLocal = Registration;
192 } else {
193 RegistrationLocal = &RegistrationLocal;
194 }
195
196 //
197 // Register for an installation of protocol interface
198 //
199
200 Status = gBS->RegisterProtocolNotify (
201 (EFI_GUID *) Name,
202 Event,
203 RegistrationLocal
204 );
205 ASSERT_EFI_ERROR (Status);
206
207 return EFI_SUCCESS;
208 }
209
210 /**
211 This function signals the named event specified by Name. The named event must
212 have been created with EfiNamedEventListen().
213
214 @param Name Supplies GUID name of the event.
215
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.
218
219 **/
220 EFI_STATUS
221 EFIAPI
222 EfiNamedEventSignal (
223 IN CONST EFI_GUID *Name
224 )
225 {
226 EFI_STATUS Status;
227 EFI_HANDLE Handle;
228
229 Handle = NULL;
230 Status = gBS->InstallProtocolInterface (
231 &Handle,
232 (EFI_GUID *) Name,
233 EFI_NATIVE_INTERFACE,
234 NULL
235 );
236 ASSERT_EFI_ERROR (Status);
237
238 Status = gBS->UninstallProtocolInterface (
239 Handle,
240 (EFI_GUID *) Name,
241 NULL
242 );
243 ASSERT_EFI_ERROR (Status);
244
245 return EFI_SUCCESS;
246 }
247
248 /**
249 Returns the current TPL.
250
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
255 to RestoreTPL().
256
257 @param VOID
258
259 @retval EFI_TPL The current TPL.
260
261 **/
262 EFI_TPL
263 EFIAPI
264 EfiGetCurrentTpl (
265 VOID
266 )
267 {
268 EFI_TPL Tpl;
269
270 Tpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
271 gBS->RestoreTPL (Tpl);
272
273 return Tpl;
274 }
275
276
277 /**
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.
282
283 @param Lock A pointer to the lock data structure to initialize.
284 @param Priority EFI TPL associated with the lock.
285
286 @return The lock.
287
288 **/
289 EFI_LOCK *
290 EFIAPI
291 EfiInitializeLock (
292 IN OUT EFI_LOCK *Lock,
293 IN EFI_TPL Priority
294 )
295 {
296 ASSERT (Lock != NULL);
297 ASSERT (Priority <= TPL_HIGH_LEVEL);
298
299 Lock->Tpl = Priority;
300 Lock->OwnerTpl = TPL_APPLICATION;
301 Lock->Lock = EfiLockReleased ;
302 return Lock;
303 }
304
305 /**
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
308 acquired state.
309
310 @param Lock The task lock with priority level.
311
312 **/
313 VOID
314 EFIAPI
315 EfiAcquireLock (
316 IN EFI_LOCK *Lock
317 )
318 {
319 ASSERT (Lock != NULL);
320 ASSERT (Lock->Lock == EfiLockReleased);
321
322 Lock->OwnerTpl = gBS->RaiseTPL (Lock->Tpl);
323 Lock->Lock = EfiLockAcquired;
324 }
325
326 /**
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.
330
331 @param Lock A pointer to the lock to acquire.
332
333 @retval EFI_SUCCESS The lock was acquired.
334 @retval EFI_ACCESS_DENIED The lock could not be acquired because it is already owned.
335
336 **/
337 EFI_STATUS
338 EFIAPI
339 EfiAcquireLockOrFail (
340 IN EFI_LOCK *Lock
341 )
342 {
343
344 ASSERT (Lock != NULL);
345 ASSERT (Lock->Lock != EfiLockUninitialized);
346
347 if (Lock->Lock == EfiLockAcquired) {
348 //
349 // Lock is already owned, so bail out
350 //
351 return EFI_ACCESS_DENIED;
352 }
353
354 Lock->OwnerTpl = gBS->RaiseTPL (Lock->Tpl);
355
356 Lock->Lock = EfiLockAcquired;
357
358 return EFI_SUCCESS;
359 }
360
361 /**
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
364 previous level.
365
366 @param Lock A pointer to the lock to release.
367
368 **/
369 VOID
370 EFIAPI
371 EfiReleaseLock (
372 IN EFI_LOCK *Lock
373 )
374 {
375 EFI_TPL Tpl;
376
377 ASSERT (Lock != NULL);
378 ASSERT (Lock->Lock == EfiLockAcquired);
379
380 Tpl = Lock->OwnerTpl;
381
382 Lock->Lock = EfiLockReleased;
383
384 gBS->RestoreTPL (Tpl);
385 }
386
387 /**
388 Tests whether a controller handle is being managed by a specific driver.
389
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().
396
397 @param ControllerHandle A handle for a controller to test.
398 @param DriverBindingHandle Specifies the driver binding handle for the
399 driver.
400 @param ProtocolGuid Specifies the protocol that the driver specified
401 by DriverBindingHandle opens in its Start()
402 function.
403
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.
408
409 **/
410 EFI_STATUS
411 EFIAPI
412 EfiTestManagedDevice (
413 IN CONST EFI_HANDLE ControllerHandle,
414 IN CONST EFI_HANDLE DriverBindingHandle,
415 IN CONST EFI_GUID *ProtocolGuid
416 )
417 {
418 EFI_STATUS Status;
419 VOID *ManagedInterface;
420
421 ASSERT (ProtocolGuid != NULL);
422
423 Status = gBS->OpenProtocol (
424 ControllerHandle,
425 (EFI_GUID *) ProtocolGuid,
426 &ManagedInterface,
427 DriverBindingHandle,
428 ControllerHandle,
429 EFI_OPEN_PROTOCOL_BY_DRIVER
430 );
431 if (!EFI_ERROR (Status)) {
432 gBS->CloseProtocol (
433 ControllerHandle,
434 (EFI_GUID *) ProtocolGuid,
435 DriverBindingHandle,
436 ControllerHandle
437 );
438 return EFI_UNSUPPORTED;
439 }
440
441 if (Status != EFI_ALREADY_STARTED) {
442 return EFI_UNSUPPORTED;
443 }
444
445 return EFI_SUCCESS;
446 }
447
448 /**
449 Tests whether a child handle is a child device of the controller.
450
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().
456
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.
461
462 @retval EFI_SUCCESS ChildHandle is a child of the ControllerHandle.
463 @retval EFI_UNSUPPORTED ChildHandle is not a child of the
464 ControllerHandle.
465
466 **/
467 EFI_STATUS
468 EFIAPI
469 EfiTestChildHandle (
470 IN CONST EFI_HANDLE ControllerHandle,
471 IN CONST EFI_HANDLE ChildHandle,
472 IN CONST EFI_GUID *ProtocolGuid
473 )
474 {
475 EFI_STATUS Status;
476 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfoBuffer;
477 UINTN EntryCount;
478 UINTN Index;
479
480 ASSERT (ProtocolGuid != NULL);
481
482 //
483 // Retrieve the list of agents that are consuming the specific protocol
484 // on ControllerHandle.
485 //
486 Status = gBS->OpenProtocolInformation (
487 ControllerHandle,
488 (EFI_GUID *) ProtocolGuid,
489 &OpenInfoBuffer,
490 &EntryCount
491 );
492 if (EFI_ERROR (Status)) {
493 return EFI_UNSUPPORTED;
494 }
495
496 //
497 // Inspect if ChildHandle is one of the agents.
498 //
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;
504 break;
505 }
506 }
507
508 FreePool (OpenInfoBuffer);
509 return Status;
510 }
511
512 /**
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
517 UnicodeString.
518
519 @param Language A pointer to the ISO 639-2
520 language code for the Unicode
521 string to look up and return.
522
523 @param SupportedLanguages A pointer to the set of ISO
524 639-2language
525 codes that the Unicode string
526 table supports. Language must
527 be a member of this set.
528
529 @param UnicodeStringTable A pointer to the table of
530 Unicode strings.
531
532 @param UnicodeString A pointer to the Unicode
533 string from UnicodeStringTable
534 that matches the language
535 specified by Language.
536
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.
543
544 @retval EFI_INVALID_PARAMETER Language is NULL.
545
546 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
547 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
548
549 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.
550
551 @retval EFI_UNSUPPORTED The language specified by
552 Language is not a member
553 ofSupportedLanguages.
554
555 @retval EFI_UNSUPPORTED The language specified by
556 Language is not supported by
557 UnicodeStringTable.
558
559 **/
560 EFI_STATUS
561 EFIAPI
562 LookupUnicodeString (
563 IN CONST CHAR8 *Language,
564 IN CONST CHAR8 *SupportedLanguages,
565 IN CONST EFI_UNICODE_STRING_TABLE *UnicodeStringTable,
566 OUT CHAR16 **UnicodeString
567 )
568 {
569 //
570 // Make sure the parameters are valid
571 //
572 if (Language == NULL || UnicodeString == NULL) {
573 return EFI_INVALID_PARAMETER;
574 }
575
576 //
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
579 //
580 if (SupportedLanguages == NULL || UnicodeStringTable == NULL) {
581 return EFI_UNSUPPORTED;
582 }
583
584 //
585 // Make sure Language is in the set of Supported Languages
586 //
587 while (*SupportedLanguages != 0) {
588 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {
589
590 //
591 // Search the Unicode String Table for the matching Language specifier
592 //
593 while (UnicodeStringTable->Language != NULL) {
594 if (CompareIso639LanguageCode (Language, UnicodeStringTable->Language)) {
595
596 //
597 // A matching string was found, so return it
598 //
599 *UnicodeString = UnicodeStringTable->UnicodeString;
600 return EFI_SUCCESS;
601 }
602
603 UnicodeStringTable++;
604 }
605
606 return EFI_UNSUPPORTED;
607 }
608
609 SupportedLanguages += 3;
610 }
611
612 return EFI_UNSUPPORTED;
613 }
614
615
616
617 /**
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
622 UnicodeString.
623
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
627 return.
628
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.
634
635 @param UnicodeStringTable A pointer to the table of
636 Unicode strings.
637
638 @param UnicodeString A pointer to the Unicode
639 string from UnicodeStringTable
640 that matches the language
641 specified by Language.
642
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
647 follows RFC3066.
648
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.
655
656 @retval EFI_INVALID_PARAMETER Language is NULL.
657
658 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
659
660 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
661
662 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.
663
664 @retval EFI_UNSUPPORTED The language specified by
665 Language is not a member
666 ofSupportedLanguages.
667
668 @retval EFI_UNSUPPORTED The language specified by
669 Language is not supported by
670 UnicodeStringTable.
671
672 **/
673 EFI_STATUS
674 EFIAPI
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
681 )
682 {
683 BOOLEAN Found;
684 UINTN Index;
685 CHAR8 *LanguageString;
686
687 //
688 // Make sure the parameters are valid
689 //
690 if (Language == NULL || UnicodeString == NULL) {
691 return EFI_INVALID_PARAMETER;
692 }
693
694 //
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
697 //
698 if (SupportedLanguages == NULL || UnicodeStringTable == NULL) {
699 return EFI_UNSUPPORTED;
700 }
701
702 //
703 // Make sure Language is in the set of Supported Languages
704 //
705 Found = FALSE;
706 while (*SupportedLanguages != 0) {
707 if (Iso639Language) {
708 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {
709 Found = TRUE;
710 break;
711 }
712 SupportedLanguages += 3;
713 } else {
714 for (Index = 0; SupportedLanguages[Index] != 0 && SupportedLanguages[Index] != ';'; Index++);
715 if ((AsciiStrnCmp(SupportedLanguages, Language, Index) == 0) && (Language[Index] == 0)) {
716 Found = TRUE;
717 break;
718 }
719 SupportedLanguages += Index;
720 for (; *SupportedLanguages != 0 && *SupportedLanguages == ';'; SupportedLanguages++);
721 }
722 }
723
724 //
725 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED
726 //
727 if (!Found) {
728 return EFI_UNSUPPORTED;
729 }
730
731 //
732 // Search the Unicode String Table for the matching Language specifier
733 //
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;
740 return EFI_SUCCESS;
741 }
742 LanguageString += Index;
743 for (Index = 0 ;LanguageString[Index] != 0 && LanguageString[Index] == ';'; Index++);
744 }
745 UnicodeStringTable++;
746 }
747
748 return EFI_UNSUPPORTED;
749 }
750
751
752 /**
753
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.
761
762 @param Language A pointer to the ISO 639-2
763 language code for the Unicode
764 string to add.
765
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
770 this set.
771
772 @param UnicodeStringTable A pointer to the table of
773 Unicode strings.
774
775 @param UnicodeString A pointer to the Unicode
776 string to add.
777
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.
784
785 @retval EFI_INVALID_PARAMETER Language is NULL.
786
787 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
788
789 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.
790
791 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
792
793 @retval EFI_ALREADY_STARTED A Unicode string with language
794 Language is already present in
795 UnicodeStringTable.
796
797 @retval EFI_OUT_OF_RESOURCES There is not enough memory to
798 add another Unicode string to
799 UnicodeStringTable.
800
801 @retval EFI_UNSUPPORTED The language specified by
802 Language is not a member of
803 SupportedLanguages.
804
805 **/
806 EFI_STATUS
807 EFIAPI
808 AddUnicodeString (
809 IN CONST CHAR8 *Language,
810 IN CONST CHAR8 *SupportedLanguages,
811 IN EFI_UNICODE_STRING_TABLE **UnicodeStringTable,
812 IN CONST CHAR16 *UnicodeString
813 )
814 {
815 UINTN NumberOfEntries;
816 EFI_UNICODE_STRING_TABLE *OldUnicodeStringTable;
817 EFI_UNICODE_STRING_TABLE *NewUnicodeStringTable;
818 UINTN UnicodeStringLength;
819
820 //
821 // Make sure the parameter are valid
822 //
823 if (Language == NULL || UnicodeString == NULL || UnicodeStringTable == NULL) {
824 return EFI_INVALID_PARAMETER;
825 }
826
827 //
828 // If there are no supported languages, then a Unicode String can not be added
829 //
830 if (SupportedLanguages == NULL) {
831 return EFI_UNSUPPORTED;
832 }
833
834 //
835 // If the Unicode String is empty, then a Unicode String can not be added
836 //
837 if (UnicodeString[0] == 0) {
838 return EFI_INVALID_PARAMETER;
839 }
840
841 //
842 // Make sure Language is a member of SupportedLanguages
843 //
844 while (*SupportedLanguages != 0) {
845 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {
846
847 //
848 // Determine the size of the Unicode String Table by looking for a NULL Language entry
849 //
850 NumberOfEntries = 0;
851 if (*UnicodeStringTable != NULL) {
852 OldUnicodeStringTable = *UnicodeStringTable;
853 while (OldUnicodeStringTable->Language != NULL) {
854 if (CompareIso639LanguageCode (Language, OldUnicodeStringTable->Language)) {
855 return EFI_ALREADY_STARTED;
856 }
857
858 OldUnicodeStringTable++;
859 NumberOfEntries++;
860 }
861 }
862
863 //
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
866 // marker
867 //
868 NewUnicodeStringTable = AllocatePool ((NumberOfEntries + 2) * sizeof (EFI_UNICODE_STRING_TABLE));
869 if (NewUnicodeStringTable == NULL) {
870 return EFI_OUT_OF_RESOURCES;
871 }
872
873 //
874 // If the current Unicode String Table contains any entries, then copy them to the
875 // newly allocated Unicode String Table.
876 //
877 if (*UnicodeStringTable != NULL) {
878 CopyMem (
879 NewUnicodeStringTable,
880 *UnicodeStringTable,
881 NumberOfEntries * sizeof (EFI_UNICODE_STRING_TABLE)
882 );
883 }
884
885 //
886 // Allocate space for a copy of the Language specifier
887 //
888 NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (3, Language);
889 if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) {
890 gBS->FreePool (NewUnicodeStringTable);
891 return EFI_OUT_OF_RESOURCES;
892 }
893
894 //
895 // Compute the length of the Unicode String
896 //
897 for (UnicodeStringLength = 0; UnicodeString[UnicodeStringLength] != 0; UnicodeStringLength++)
898 ;
899
900 //
901 // Allocate space for a copy of the Unicode String
902 //
903 NewUnicodeStringTable[NumberOfEntries].UnicodeString = AllocateCopyPool (
904 (UnicodeStringLength + 1) * sizeof (CHAR16),
905 UnicodeString
906 );
907 if (NewUnicodeStringTable[NumberOfEntries].UnicodeString == NULL) {
908 gBS->FreePool (NewUnicodeStringTable[NumberOfEntries].Language);
909 gBS->FreePool (NewUnicodeStringTable);
910 return EFI_OUT_OF_RESOURCES;
911 }
912
913 //
914 // Mark the end of the Unicode String Table
915 //
916 NewUnicodeStringTable[NumberOfEntries + 1].Language = NULL;
917 NewUnicodeStringTable[NumberOfEntries + 1].UnicodeString = NULL;
918
919 //
920 // Free the old Unicode String Table
921 //
922 if (*UnicodeStringTable != NULL) {
923 gBS->FreePool (*UnicodeStringTable);
924 }
925
926 //
927 // Point UnicodeStringTable at the newly allocated Unicode String Table
928 //
929 *UnicodeStringTable = NewUnicodeStringTable;
930
931 return EFI_SUCCESS;
932 }
933
934 SupportedLanguages += 3;
935 }
936
937 return EFI_UNSUPPORTED;
938 }
939
940
941 /**
942
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.
950
951 @param Language A pointer to the ISO 639-2 or
952 RFC 3066 language code for the
953 Unicode string to add.
954
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.
960
961 @param UnicodeStringTable A pointer to the table of
962 Unicode strings.
963
964 @param UnicodeString A pointer to the Unicode
965 string to add.
966
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
971 follows RFC3066.
972
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.
979
980 @retval EFI_INVALID_PARAMETER Language is NULL.
981
982 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
983
984 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.
985
986 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
987
988 @retval EFI_ALREADY_STARTED A Unicode string with language
989 Language is already present in
990 UnicodeStringTable.
991
992 @retval EFI_OUT_OF_RESOURCES There is not enough memory to
993 add another Unicode string to
994 UnicodeStringTable.
995
996 @retval EFI_UNSUPPORTED The language specified by
997 Language is not a member of
998 SupportedLanguages.
999
1000 **/
1001 EFI_STATUS
1002 EFIAPI
1003 AddUnicodeString2 (
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
1009 )
1010 {
1011 UINTN NumberOfEntries;
1012 EFI_UNICODE_STRING_TABLE *OldUnicodeStringTable;
1013 EFI_UNICODE_STRING_TABLE *NewUnicodeStringTable;
1014 UINTN UnicodeStringLength;
1015 BOOLEAN Found;
1016 UINTN Index;
1017 CHAR8 *LanguageString;
1018
1019 //
1020 // Make sure the parameter are valid
1021 //
1022 if (Language == NULL || UnicodeString == NULL || UnicodeStringTable == NULL) {
1023 return EFI_INVALID_PARAMETER;
1024 }
1025
1026 //
1027 // If there are no supported languages, then a Unicode String can not be added
1028 //
1029 if (SupportedLanguages == NULL) {
1030 return EFI_UNSUPPORTED;
1031 }
1032
1033 //
1034 // If the Unicode String is empty, then a Unicode String can not be added
1035 //
1036 if (UnicodeString[0] == 0) {
1037 return EFI_INVALID_PARAMETER;
1038 }
1039
1040 //
1041 // Make sure Language is a member of SupportedLanguages
1042 //
1043 Found = FALSE;
1044 while (*SupportedLanguages != 0) {
1045 if (Iso639Language) {
1046 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {
1047 Found = TRUE;
1048 break;
1049 }
1050 SupportedLanguages += 3;
1051 } else {
1052 for (Index = 0; SupportedLanguages[Index] != 0 && SupportedLanguages[Index] != ';'; Index++);
1053 if (AsciiStrnCmp(SupportedLanguages, Language, Index) == 0) {
1054 Found = TRUE;
1055 break;
1056 }
1057 SupportedLanguages += Index;
1058 for (; *SupportedLanguages != 0 && *SupportedLanguages == ';'; SupportedLanguages++);
1059 }
1060 }
1061
1062 //
1063 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED
1064 //
1065 if (!Found) {
1066 return EFI_UNSUPPORTED;
1067 }
1068
1069 //
1070 // Determine the size of the Unicode String Table by looking for a NULL Language entry
1071 //
1072 NumberOfEntries = 0;
1073 if (*UnicodeStringTable != NULL) {
1074 OldUnicodeStringTable = *UnicodeStringTable;
1075 while (OldUnicodeStringTable->Language != NULL) {
1076 LanguageString = OldUnicodeStringTable->Language;
1077
1078 while (*LanguageString != 0) {
1079 for (Index = 0; LanguageString[Index] != 0 && LanguageString[Index] != ';'; Index++);
1080
1081 if (AsciiStrnCmp (Language, LanguageString, Index) == 0) {
1082 return EFI_ALREADY_STARTED;
1083 }
1084 LanguageString += Index;
1085 for (; *LanguageString != 0 && *LanguageString == ';'; LanguageString++);
1086 }
1087 OldUnicodeStringTable++;
1088 NumberOfEntries++;
1089 }
1090 }
1091
1092 //
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
1095 // marker
1096 //
1097 NewUnicodeStringTable = AllocatePool ((NumberOfEntries + 2) * sizeof (EFI_UNICODE_STRING_TABLE));
1098 if (NewUnicodeStringTable == NULL) {
1099 return EFI_OUT_OF_RESOURCES;
1100 }
1101
1102 //
1103 // If the current Unicode String Table contains any entries, then copy them to the
1104 // newly allocated Unicode String Table.
1105 //
1106 if (*UnicodeStringTable != NULL) {
1107 CopyMem (
1108 NewUnicodeStringTable,
1109 *UnicodeStringTable,
1110 NumberOfEntries * sizeof (EFI_UNICODE_STRING_TABLE)
1111 );
1112 }
1113
1114 //
1115 // Allocate space for a copy of the Language specifier
1116 //
1117 NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (AsciiStrSize(Language), Language);
1118 if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) {
1119 gBS->FreePool (NewUnicodeStringTable);
1120 return EFI_OUT_OF_RESOURCES;
1121 }
1122
1123 //
1124 // Compute the length of the Unicode String
1125 //
1126 for (UnicodeStringLength = 0; UnicodeString[UnicodeStringLength] != 0; UnicodeStringLength++);
1127
1128 //
1129 // Allocate space for a copy of the Unicode String
1130 //
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;
1136 }
1137
1138 //
1139 // Mark the end of the Unicode String Table
1140 //
1141 NewUnicodeStringTable[NumberOfEntries + 1].Language = NULL;
1142 NewUnicodeStringTable[NumberOfEntries + 1].UnicodeString = NULL;
1143
1144 //
1145 // Free the old Unicode String Table
1146 //
1147 if (*UnicodeStringTable != NULL) {
1148 gBS->FreePool (*UnicodeStringTable);
1149 }
1150
1151 //
1152 // Point UnicodeStringTable at the newly allocated Unicode String Table
1153 //
1154 *UnicodeStringTable = NewUnicodeStringTable;
1155
1156 return EFI_SUCCESS;
1157 }
1158
1159 /**
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.
1164
1165 @param UnicodeStringTable A pointer to the table of Unicode strings.
1166
1167 @retval EFI_SUCCESS The Unicode string table was freed.
1168
1169 **/
1170 EFI_STATUS
1171 EFIAPI
1172 FreeUnicodeStringTable (
1173 IN EFI_UNICODE_STRING_TABLE *UnicodeStringTable
1174 )
1175 {
1176 UINTN Index;
1177
1178 //
1179 // If the Unicode String Table is NULL, then it is already freed
1180 //
1181 if (UnicodeStringTable == NULL) {
1182 return EFI_SUCCESS;
1183 }
1184
1185 //
1186 // Loop through the Unicode String Table until we reach the end of table marker
1187 //
1188 for (Index = 0; UnicodeStringTable[Index].Language != NULL; Index++) {
1189
1190 //
1191 // Free the Language string from the Unicode String Table
1192 //
1193 gBS->FreePool (UnicodeStringTable[Index].Language);
1194
1195 //
1196 // Free the Unicode String from the Unicode String Table
1197 //
1198 if (UnicodeStringTable[Index].UnicodeString != NULL) {
1199 gBS->FreePool (UnicodeStringTable[Index].UnicodeString);
1200 }
1201 }
1202
1203 //
1204 // Free the Unicode String Table itself
1205 //
1206 gBS->FreePool (UnicodeStringTable);
1207
1208 return EFI_SUCCESS;
1209 }
1210
1211