1. Add PeiDxeDebugLibReportStatusCode.inf and UefiLibFramework/UefiLib.inf
[mirror_edk2.git] / IntelFrameworkPkg / Library / UefiLibFramework / 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 Module Name: UefiLib.c
14
15 **/
16
17 #include "UefiLibFramework.h"
18
19 /**
20 Compare whether two names of languages are identical.
21
22 @param Language1 Name of language 1.
23 @param Language2 Name of language 2.
24
25 @retval TRUE Language 1 and language 2 are the same.
26 @retval FALSE Language 1 and language 2 are not the same.
27
28 **/
29 STATIC
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 @retvale 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 Priority The task priority level of the lock.
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 ConsumsedGuid 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. If Language is
514 a member of SupportedLanguages and a Unicode string is found in UnicodeStringTable
515 that matches the language code specified by Language, then it is returned in
516 UnicodeString.
517
518 @param Language A pointer to the ISO 639-2 language code for the
519 Unicode string to look up and return.
520 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes
521 that the Unicode string table supports. Language
522 must be a member of this set.
523 @param UnicodeStringTable A pointer to the table of Unicode strings.
524 @param UnicodeString A pointer to the Unicode string from UnicodeStringTable
525 that matches the language specified by Language.
526
527 @retval EFI_SUCCESS The Unicode string that matches the language
528 specified by Language was found
529 in the table of Unicoide strings UnicodeStringTable,
530 and it was returned in UnicodeString.
531 @retval EFI_INVALID_PARAMETER Language is NULL.
532 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
533 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
534 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.
535 @retval EFI_UNSUPPORTED The language specified by Language is not a
536 member of SupportedLanguages.
537 @retval EFI_UNSUPPORTED The language specified by Language is not
538 supported by UnicodeStringTable.
539
540 **/
541 EFI_STATUS
542 EFIAPI
543 LookupUnicodeString (
544 IN CONST CHAR8 *Language,
545 IN CONST CHAR8 *SupportedLanguages,
546 IN CONST EFI_UNICODE_STRING_TABLE *UnicodeStringTable,
547 OUT CHAR16 **UnicodeString
548 )
549 {
550 //
551 // Make sure the parameters are valid
552 //
553 if (Language == NULL || UnicodeString == NULL) {
554 return EFI_INVALID_PARAMETER;
555 }
556
557 //
558 // If there are no supported languages, or the Unicode String Table is empty, then the
559 // Unicode String specified by Language is not supported by this Unicode String Table
560 //
561 if (SupportedLanguages == NULL || UnicodeStringTable == NULL) {
562 return EFI_UNSUPPORTED;
563 }
564
565 //
566 // Make sure Language is in the set of Supported Languages
567 //
568 while (*SupportedLanguages != 0) {
569 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {
570
571 //
572 // Search the Unicode String Table for the matching Language specifier
573 //
574 while (UnicodeStringTable->Language != NULL) {
575 if (CompareIso639LanguageCode (Language, UnicodeStringTable->Language)) {
576
577 //
578 // A matching string was found, so return it
579 //
580 *UnicodeString = UnicodeStringTable->UnicodeString;
581 return EFI_SUCCESS;
582 }
583
584 UnicodeStringTable++;
585 }
586
587 return EFI_UNSUPPORTED;
588 }
589
590 SupportedLanguages += 3;
591 }
592
593 return EFI_UNSUPPORTED;
594 }
595
596 /**
597 This function adds a Unicode string to UnicodeStringTable.
598 If Language is a member of SupportedLanguages then UnicodeString is added to
599 UnicodeStringTable. New buffers are allocated for both Language and
600 UnicodeString. The contents of Language and UnicodeString are copied into
601 these new buffers. These buffers are automatically freed when
602 FreeUnicodeStringTable() is called.
603
604 @param Language A pointer to the ISO 639-2 language code for the Unicode
605 string to add.
606 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes
607 that the Unicode string table supports.
608 Language must be a member of this set.
609 @param UnicodeStringTable A pointer to the table of Unicode strings.
610 @param UnicodeString A pointer to the Unicode string to add.
611
612 @retval EFI_SUCCESS The Unicode string that matches the language
613 specified by Language was found in the table of
614 Unicode strings UnicodeStringTable, and it was
615 returned in UnicodeString.
616 @retval EFI_INVALID_PARAMETER Language is NULL.
617 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
618 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.
619 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
620 @retval EFI_ALREADY_STARTED A Unicode string with language Language is
621 already present in UnicodeStringTable.
622 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another
623 Unicode string to UnicodeStringTable.
624 @retval EFI_UNSUPPORTED The language specified by Language is not a
625 member of SupportedLanguages.
626
627 **/
628 EFI_STATUS
629 EFIAPI
630 AddUnicodeString (
631 IN CONST CHAR8 *Language,
632 IN CONST CHAR8 *SupportedLanguages,
633 IN EFI_UNICODE_STRING_TABLE **UnicodeStringTable,
634 IN CONST CHAR16 *UnicodeString
635 )
636 {
637 UINTN NumberOfEntries;
638 EFI_UNICODE_STRING_TABLE *OldUnicodeStringTable;
639 EFI_UNICODE_STRING_TABLE *NewUnicodeStringTable;
640 UINTN UnicodeStringLength;
641
642 //
643 // Make sure the parameter are valid
644 //
645 if (Language == NULL || UnicodeString == NULL || UnicodeStringTable == NULL) {
646 return EFI_INVALID_PARAMETER;
647 }
648
649 //
650 // If there are no supported languages, then a Unicode String can not be added
651 //
652 if (SupportedLanguages == NULL) {
653 return EFI_UNSUPPORTED;
654 }
655
656 //
657 // If the Unicode String is empty, then a Unicode String can not be added
658 //
659 if (UnicodeString[0] == 0) {
660 return EFI_INVALID_PARAMETER;
661 }
662
663 //
664 // Make sure Language is a member of SupportedLanguages
665 //
666 while (*SupportedLanguages != 0) {
667 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {
668
669 //
670 // Determine the size of the Unicode String Table by looking for a NULL Language entry
671 //
672 NumberOfEntries = 0;
673 if (*UnicodeStringTable != NULL) {
674 OldUnicodeStringTable = *UnicodeStringTable;
675 while (OldUnicodeStringTable->Language != NULL) {
676 if (CompareIso639LanguageCode (Language, OldUnicodeStringTable->Language)) {
677 return EFI_ALREADY_STARTED;
678 }
679
680 OldUnicodeStringTable++;
681 NumberOfEntries++;
682 }
683 }
684
685 //
686 // Allocate space for a new Unicode String Table. It must hold the current number of
687 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table
688 // marker
689 //
690 NewUnicodeStringTable = AllocatePool ((NumberOfEntries + 2) * sizeof (EFI_UNICODE_STRING_TABLE));
691 if (NewUnicodeStringTable == NULL) {
692 return EFI_OUT_OF_RESOURCES;
693 }
694
695 //
696 // If the current Unicode String Table contains any entries, then copy them to the
697 // newly allocated Unicode String Table.
698 //
699 if (*UnicodeStringTable != NULL) {
700 CopyMem (
701 NewUnicodeStringTable,
702 *UnicodeStringTable,
703 NumberOfEntries * sizeof (EFI_UNICODE_STRING_TABLE)
704 );
705 }
706
707 //
708 // Allocate space for a copy of the Language specifier
709 //
710 NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (3, Language);
711 if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) {
712 gBS->FreePool (NewUnicodeStringTable);
713 return EFI_OUT_OF_RESOURCES;
714 }
715
716 //
717 // Compute the length of the Unicode String
718 //
719 for (UnicodeStringLength = 0; UnicodeString[UnicodeStringLength] != 0; UnicodeStringLength++)
720 ;
721
722 //
723 // Allocate space for a copy of the Unicode String
724 //
725 NewUnicodeStringTable[NumberOfEntries].UnicodeString = AllocateCopyPool (
726 (UnicodeStringLength + 1) * sizeof (CHAR16),
727 UnicodeString
728 );
729 if (NewUnicodeStringTable[NumberOfEntries].UnicodeString == NULL) {
730 gBS->FreePool (NewUnicodeStringTable[NumberOfEntries].Language);
731 gBS->FreePool (NewUnicodeStringTable);
732 return EFI_OUT_OF_RESOURCES;
733 }
734
735 //
736 // Mark the end of the Unicode String Table
737 //
738 NewUnicodeStringTable[NumberOfEntries + 1].Language = NULL;
739 NewUnicodeStringTable[NumberOfEntries + 1].UnicodeString = NULL;
740
741 //
742 // Free the old Unicode String Table
743 //
744 if (*UnicodeStringTable != NULL) {
745 gBS->FreePool (*UnicodeStringTable);
746 }
747
748 //
749 // Point UnicodeStringTable at the newly allocated Unicode String Table
750 //
751 *UnicodeStringTable = NewUnicodeStringTable;
752
753 return EFI_SUCCESS;
754 }
755
756 SupportedLanguages += 3;
757 }
758
759 return EFI_UNSUPPORTED;
760 }
761
762 /**
763 This function frees the table of Unicode strings in UnicodeStringTable.
764 If UnicodeStringTable is NULL, then EFI_SUCCESS is returned.
765 Otherwise, each language code, and each Unicode string in the Unicode string
766 table are freed, and EFI_SUCCESS is returned.
767
768 @param UnicodeStringTable A pointer to the table of Unicode strings.
769
770 @retval EFI_SUCCESS The Unicode string table was freed.
771
772 **/
773 EFI_STATUS
774 EFIAPI
775 FreeUnicodeStringTable (
776 IN EFI_UNICODE_STRING_TABLE *UnicodeStringTable
777 )
778 {
779 UINTN Index;
780
781 //
782 // If the Unicode String Table is NULL, then it is already freed
783 //
784 if (UnicodeStringTable == NULL) {
785 return EFI_SUCCESS;
786 }
787
788 //
789 // Loop through the Unicode String Table until we reach the end of table marker
790 //
791 for (Index = 0; UnicodeStringTable[Index].Language != NULL; Index++) {
792
793 //
794 // Free the Language string from the Unicode String Table
795 //
796 gBS->FreePool (UnicodeStringTable[Index].Language);
797
798 //
799 // Free the Unicode String from the Unicode String Table
800 //
801 if (UnicodeStringTable[Index].UnicodeString != NULL) {
802 gBS->FreePool (UnicodeStringTable[Index].UnicodeString);
803 }
804 }
805
806 //
807 // Free the Unicode String Table itself
808 //
809 gBS->FreePool (UnicodeStringTable);
810
811 return EFI_SUCCESS;
812 }
813