]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkPkg/Library/FrameworkUefiLib/UefiLib.c
64f7cde18492e8f66085f81e6424a435f5732cee
[mirror_edk2.git] / IntelFrameworkPkg / Library / FrameworkUefiLib / UefiLib.c
1 /** @file
2 Implementation of UefiLib
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 #include "FrameworkUefiLib.h"
16
17 /**
18 Compare whether two names of languages are identical.
19
20 @param Language1 Name of language 1.
21 @param Language2 Name of language 2.
22
23 @retval TRUE Language 1 and language 2 are the same.
24 @retval FALSE Language 1 and language 2 are not the same.
25
26 **/
27 STATIC
28 BOOLEAN
29 CompareIso639LanguageCode (
30 IN CONST CHAR8 *Language1,
31 IN CONST CHAR8 *Language2
32 )
33 {
34 UINT32 Name1;
35 UINT32 Name2;
36
37 Name1 = ReadUnaligned24 ((CONST UINT32 *) Language1);
38 Name2 = ReadUnaligned24 ((CONST UINT32 *) Language2);
39
40 return (BOOLEAN) (Name1 == Name2);
41 }
42
43 /**
44 This function searches the list of configuration tables stored in the EFI System
45 Table for a table with a GUID that matches TableGuid. If a match is found,
46 then a pointer to the configuration table is returned in Table, and EFI_SUCCESS
47 is returned. If a matching GUID is not found, then EFI_NOT_FOUND is returned.
48
49 @param TableGuid Pointer to table's GUID type..
50 @param Table Pointer to the table associated with TableGuid in the EFI System Table.
51
52 @retval EFI_SUCCESS A configuration table matching TableGuid was found.
53 @retval EFI_NOT_FOUND A configuration table matching TableGuid could not be found.
54
55 **/
56 EFI_STATUS
57 EFIAPI
58 EfiGetSystemConfigurationTable (
59 IN EFI_GUID *TableGuid,
60 OUT VOID **Table
61 )
62 {
63 EFI_SYSTEM_TABLE *SystemTable;
64 UINTN Index;
65
66 ASSERT (TableGuid != NULL);
67 ASSERT (Table != NULL);
68
69 SystemTable = gST;
70 *Table = NULL;
71 for (Index = 0; Index < SystemTable->NumberOfTableEntries; Index++) {
72 if (CompareGuid (TableGuid, &(SystemTable->ConfigurationTable[Index].VendorGuid))) {
73 *Table = SystemTable->ConfigurationTable[Index].VendorTable;
74 return EFI_SUCCESS;
75 }
76 }
77
78 return EFI_NOT_FOUND;
79 }
80
81 /**
82 This function causes the notification function to be executed for every protocol
83 of type ProtocolGuid instance that exists in the system when this function is
84 invoked. In addition, every time a protocol of type ProtocolGuid instance is
85 installed or reinstalled, the notification function is also executed.
86
87 @param ProtocolGuid Supplies GUID of the protocol upon whose installation the event is fired.
88 @param NotifyTpl Supplies the task priority level of the event notifications.
89 @param NotifyFunction Supplies the function to notify when the event is signaled.
90 @param NotifyContext The context parameter to pass to NotifyFunction.
91 @param Registration A pointer to a memory location to receive the registration value.
92
93 @return The notification event that was created.
94
95 **/
96 EFI_EVENT
97 EFIAPI
98 EfiCreateProtocolNotifyEvent(
99 IN EFI_GUID *ProtocolGuid,
100 IN EFI_TPL NotifyTpl,
101 IN EFI_EVENT_NOTIFY NotifyFunction,
102 IN VOID *NotifyContext, OPTIONAL
103 OUT VOID **Registration
104 )
105 {
106 EFI_STATUS Status;
107 EFI_EVENT Event;
108
109 //
110 // Create the event
111 //
112
113 Status = gBS->CreateEvent (
114 EVT_NOTIFY_SIGNAL,
115 NotifyTpl,
116 NotifyFunction,
117 NotifyContext,
118 &Event
119 );
120 ASSERT_EFI_ERROR (Status);
121
122 //
123 // Register for protocol notifactions on this event
124 //
125
126 Status = gBS->RegisterProtocolNotify (
127 ProtocolGuid,
128 Event,
129 Registration
130 );
131
132 ASSERT_EFI_ERROR (Status);
133
134 //
135 // Kick the event so we will perform an initial pass of
136 // current installed drivers
137 //
138
139 gBS->SignalEvent (Event);
140 return Event;
141 }
142
143 /**
144 This function creates an event using NotifyTpl, NoifyFunction, and NotifyContext.
145 This event is signaled with EfiNamedEventSignal(). This provide the ability for
146 one or more listeners on the same event named by the GUID specified by Name.
147
148 @param Name Supplies GUID name of the event.
149 @param NotifyTpl Supplies the task priority level of the event notifications.
150 @param NotifyFunction Supplies the function to notify when the event is signaled.
151 @param NotifyContext The context parameter to pass to NotifyFunction.
152 @param Registration A pointer to a memory location to receive the registration value.
153
154 @retval EFI_SUCCESS A named event was created.
155 @retval EFI_OUT_OF_RESOURCES There are not enough resource to create the named event.
156
157 **/
158 EFI_STATUS
159 EFIAPI
160 EfiNamedEventListen (
161 IN CONST EFI_GUID *Name,
162 IN EFI_TPL NotifyTpl,
163 IN EFI_EVENT_NOTIFY NotifyFunction,
164 IN CONST VOID *NotifyContext, OPTIONAL
165 OUT VOID *Registration OPTIONAL
166 )
167 {
168 EFI_STATUS Status;
169 EFI_EVENT Event;
170 VOID *RegistrationLocal;
171
172 //
173 // Create event
174 //
175 Status = gBS->CreateEvent (
176 EVT_NOTIFY_SIGNAL,
177 NotifyTpl,
178 NotifyFunction,
179 (VOID *) NotifyContext,
180 &Event
181 );
182 ASSERT_EFI_ERROR (Status);
183
184 //
185 // The Registration is not optional to RegisterProtocolNotify().
186 // To make it optional to EfiNamedEventListen(), may need to substitute with a local.
187 //
188 if (Registration != NULL) {
189 RegistrationLocal = Registration;
190 } else {
191 RegistrationLocal = &RegistrationLocal;
192 }
193
194 //
195 // Register for an installation of protocol interface
196 //
197
198 Status = gBS->RegisterProtocolNotify (
199 (EFI_GUID *) Name,
200 Event,
201 RegistrationLocal
202 );
203 ASSERT_EFI_ERROR (Status);
204
205 return EFI_SUCCESS;
206 }
207
208 /**
209 This function signals the named event specified by Name. The named event must
210 have been created with EfiNamedEventListen().
211
212 @param Name Supplies GUID name of the event.
213
214 @retval EFI_SUCCESS A named event was signaled.
215 @retval EFI_OUT_OF_RESOURCES There are not enough resource to signal the named event.
216
217 **/
218 EFI_STATUS
219 EFIAPI
220 EfiNamedEventSignal (
221 IN CONST EFI_GUID *Name
222 )
223 {
224 EFI_STATUS Status;
225 EFI_HANDLE Handle;
226
227 Handle = NULL;
228 Status = gBS->InstallProtocolInterface (
229 &Handle,
230 (EFI_GUID *) Name,
231 EFI_NATIVE_INTERFACE,
232 NULL
233 );
234 ASSERT_EFI_ERROR (Status);
235
236 Status = gBS->UninstallProtocolInterface (
237 Handle,
238 (EFI_GUID *) Name,
239 NULL
240 );
241 ASSERT_EFI_ERROR (Status);
242
243 return EFI_SUCCESS;
244 }
245
246 /**
247 Returns the current TPL.
248
249 This function returns the current TPL. There is no EFI service to directly
250 retrieve the current TPL. Instead, the RaiseTPL() function is used to raise
251 the TPL to TPL_HIGH_LEVEL. This will return the current TPL. The TPL level
252 can then immediately be restored back to the current TPL level with a call
253 to RestoreTPL().
254
255 @param VOID
256
257 @retvale EFI_TPL The current TPL.
258
259 **/
260 EFI_TPL
261 EFIAPI
262 EfiGetCurrentTpl (
263 VOID
264 )
265 {
266 EFI_TPL Tpl;
267
268 Tpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
269 gBS->RestoreTPL (Tpl);
270
271 return Tpl;
272 }
273
274
275 /**
276 This function initializes a basic mutual exclusion lock to the released state
277 and returns the lock. Each lock provides mutual exclusion access at its task
278 priority level. Since there is no preemption or multiprocessor support in EFI,
279 acquiring the lock only consists of raising to the locks TPL.
280
281 @param Lock A pointer to the lock data structure to initialize.
282 @param Priority EFI TPL associated with the lock.
283
284 @return The lock.
285
286 **/
287 EFI_LOCK *
288 EFIAPI
289 EfiInitializeLock (
290 IN OUT EFI_LOCK *Lock,
291 IN EFI_TPL Priority
292 )
293 {
294 ASSERT (Lock != NULL);
295 ASSERT (Priority <= TPL_HIGH_LEVEL);
296
297 Lock->Tpl = Priority;
298 Lock->OwnerTpl = TPL_APPLICATION;
299 Lock->Lock = EfiLockReleased ;
300 return Lock;
301 }
302
303 /**
304 This function raises the system's current task priority level to the task
305 priority level of the mutual exclusion lock. Then, it places the lock in the
306 acquired state.
307
308 @param Priority The task priority level of the lock.
309
310 **/
311 VOID
312 EFIAPI
313 EfiAcquireLock (
314 IN EFI_LOCK *Lock
315 )
316 {
317 ASSERT (Lock != NULL);
318 ASSERT (Lock->Lock == EfiLockReleased);
319
320 Lock->OwnerTpl = gBS->RaiseTPL (Lock->Tpl);
321 Lock->Lock = EfiLockAcquired;
322 }
323
324 /**
325 This function raises the system's current task priority level to the task
326 priority level of the mutual exclusion lock. Then, it attempts to place the
327 lock in the acquired state.
328
329 @param Lock A pointer to the lock to acquire.
330
331 @retval EFI_SUCCESS The lock was acquired.
332 @retval EFI_ACCESS_DENIED The lock could not be acquired because it is already owned.
333
334 **/
335 EFI_STATUS
336 EFIAPI
337 EfiAcquireLockOrFail (
338 IN EFI_LOCK *Lock
339 )
340 {
341
342 ASSERT (Lock != NULL);
343 ASSERT (Lock->Lock != EfiLockUninitialized);
344
345 if (Lock->Lock == EfiLockAcquired) {
346 //
347 // Lock is already owned, so bail out
348 //
349 return EFI_ACCESS_DENIED;
350 }
351
352 Lock->OwnerTpl = gBS->RaiseTPL (Lock->Tpl);
353
354 Lock->Lock = EfiLockAcquired;
355
356 return EFI_SUCCESS;
357 }
358
359 /**
360 This function transitions a mutual exclusion lock from the acquired state to
361 the released state, and restores the system's task priority level to its
362 previous level.
363
364 @param Lock A pointer to the lock to release.
365
366 **/
367 VOID
368 EFIAPI
369 EfiReleaseLock (
370 IN EFI_LOCK *Lock
371 )
372 {
373 EFI_TPL Tpl;
374
375 ASSERT (Lock != NULL);
376 ASSERT (Lock->Lock == EfiLockAcquired);
377
378 Tpl = Lock->OwnerTpl;
379
380 Lock->Lock = EfiLockReleased;
381
382 gBS->RestoreTPL (Tpl);
383 }
384
385 /**
386 Tests whether a controller handle is being managed by a specific driver.
387
388 This function tests whether the driver specified by DriverBindingHandle is
389 currently managing the controller specified by ControllerHandle. This test
390 is performed by evaluating if the the protocol specified by ProtocolGuid is
391 present on ControllerHandle and is was opened by DriverBindingHandle with an
392 attribute of EFI_OPEN_PROTOCOL_BY_DRIVER.
393 If ProtocolGuid is NULL, then ASSERT().
394
395 @param ControllerHandle A handle for a controller to test.
396 @param DriverBindingHandle Specifies the driver binding handle for the
397 driver.
398 @param ProtocolGuid Specifies the protocol that the driver specified
399 by DriverBindingHandle opens in its Start()
400 function.
401
402 @retval EFI_SUCCESS ControllerHandle is managed by the driver
403 specifed by DriverBindingHandle.
404 @retval EFI_UNSUPPORTED ControllerHandle is not managed by the driver
405 specifed by DriverBindingHandle.
406
407 **/
408 EFI_STATUS
409 EFIAPI
410 EfiTestManagedDevice (
411 IN CONST EFI_HANDLE ControllerHandle,
412 IN CONST EFI_HANDLE DriverBindingHandle,
413 IN CONST EFI_GUID *ProtocolGuid
414 )
415 {
416 EFI_STATUS Status;
417 VOID *ManagedInterface;
418
419 ASSERT (ProtocolGuid != NULL);
420
421 Status = gBS->OpenProtocol (
422 ControllerHandle,
423 (EFI_GUID *) ProtocolGuid,
424 &ManagedInterface,
425 DriverBindingHandle,
426 ControllerHandle,
427 EFI_OPEN_PROTOCOL_BY_DRIVER
428 );
429 if (!EFI_ERROR (Status)) {
430 gBS->CloseProtocol (
431 ControllerHandle,
432 (EFI_GUID *) ProtocolGuid,
433 DriverBindingHandle,
434 ControllerHandle
435 );
436 return EFI_UNSUPPORTED;
437 }
438
439 if (Status != EFI_ALREADY_STARTED) {
440 return EFI_UNSUPPORTED;
441 }
442
443 return EFI_SUCCESS;
444 }
445
446 /**
447 Tests whether a child handle is a child device of the controller.
448
449 This function tests whether ChildHandle is one of the children of
450 ControllerHandle. This test is performed by checking to see if the protocol
451 specified by ProtocolGuid is present on ControllerHandle and opened by
452 ChildHandle with an attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
453 If ProtocolGuid is NULL, then ASSERT().
454
455 @param ControllerHandle A handle for a (parent) controller to test.
456 @param ChildHandle A child handle to test.
457 @param ConsumsedGuid Supplies the protocol that the child controller
458 opens on its parent controller.
459
460 @retval EFI_SUCCESS ChildHandle is a child of the ControllerHandle.
461 @retval EFI_UNSUPPORTED ChildHandle is not a child of the
462 ControllerHandle.
463
464 **/
465 EFI_STATUS
466 EFIAPI
467 EfiTestChildHandle (
468 IN CONST EFI_HANDLE ControllerHandle,
469 IN CONST EFI_HANDLE ChildHandle,
470 IN CONST EFI_GUID *ProtocolGuid
471 )
472 {
473 EFI_STATUS Status;
474 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfoBuffer;
475 UINTN EntryCount;
476 UINTN Index;
477
478 ASSERT (ProtocolGuid != NULL);
479
480 //
481 // Retrieve the list of agents that are consuming the specific protocol
482 // on ControllerHandle.
483 //
484 Status = gBS->OpenProtocolInformation (
485 ControllerHandle,
486 (EFI_GUID *) ProtocolGuid,
487 &OpenInfoBuffer,
488 &EntryCount
489 );
490 if (EFI_ERROR (Status)) {
491 return EFI_UNSUPPORTED;
492 }
493
494 //
495 // Inspect if ChildHandle is one of the agents.
496 //
497 Status = EFI_UNSUPPORTED;
498 for (Index = 0; Index < EntryCount; Index++) {
499 if ((OpenInfoBuffer[Index].ControllerHandle == ChildHandle) &&
500 (OpenInfoBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {
501 Status = EFI_SUCCESS;
502 break;
503 }
504 }
505
506 FreePool (OpenInfoBuffer);
507 return Status;
508 }
509
510 /**
511 This function looks up a Unicode string in UnicodeStringTable. If Language is
512 a member of SupportedLanguages and a Unicode string is found in UnicodeStringTable
513 that matches the language code specified by Language, then it is returned in
514 UnicodeString.
515
516 @param Language A pointer to the ISO 639-2 language code for the
517 Unicode string to look up and return.
518 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes
519 that the Unicode string table supports. Language
520 must be a member of this set.
521 @param UnicodeStringTable A pointer to the table of Unicode strings.
522 @param UnicodeString A pointer to the Unicode string from UnicodeStringTable
523 that matches the language specified by Language.
524
525 @retval EFI_SUCCESS The Unicode string that matches the language
526 specified by Language was found
527 in the table of Unicoide strings UnicodeStringTable,
528 and it was returned in UnicodeString.
529 @retval EFI_INVALID_PARAMETER Language is NULL.
530 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
531 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
532 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.
533 @retval EFI_UNSUPPORTED The language specified by Language is not a
534 member of SupportedLanguages.
535 @retval EFI_UNSUPPORTED The language specified by Language is not
536 supported by UnicodeStringTable.
537
538 **/
539 EFI_STATUS
540 EFIAPI
541 LookupUnicodeString (
542 IN CONST CHAR8 *Language,
543 IN CONST CHAR8 *SupportedLanguages,
544 IN CONST EFI_UNICODE_STRING_TABLE *UnicodeStringTable,
545 OUT CHAR16 **UnicodeString
546 )
547 {
548 //
549 // Make sure the parameters are valid
550 //
551 if (Language == NULL || UnicodeString == NULL) {
552 return EFI_INVALID_PARAMETER;
553 }
554
555 //
556 // If there are no supported languages, or the Unicode String Table is empty, then the
557 // Unicode String specified by Language is not supported by this Unicode String Table
558 //
559 if (SupportedLanguages == NULL || UnicodeStringTable == NULL) {
560 return EFI_UNSUPPORTED;
561 }
562
563 //
564 // Make sure Language is in the set of Supported Languages
565 //
566 while (*SupportedLanguages != 0) {
567 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {
568
569 //
570 // Search the Unicode String Table for the matching Language specifier
571 //
572 while (UnicodeStringTable->Language != NULL) {
573 if (CompareIso639LanguageCode (Language, UnicodeStringTable->Language)) {
574
575 //
576 // A matching string was found, so return it
577 //
578 *UnicodeString = UnicodeStringTable->UnicodeString;
579 return EFI_SUCCESS;
580 }
581
582 UnicodeStringTable++;
583 }
584
585 return EFI_UNSUPPORTED;
586 }
587
588 SupportedLanguages += 3;
589 }
590
591 return EFI_UNSUPPORTED;
592 }
593
594 /**
595 This function adds a Unicode string to UnicodeStringTable.
596 If Language is a member of SupportedLanguages then UnicodeString is added to
597 UnicodeStringTable. New buffers are allocated for both Language and
598 UnicodeString. The contents of Language and UnicodeString are copied into
599 these new buffers. These buffers are automatically freed when
600 FreeUnicodeStringTable() is called.
601
602 @param Language A pointer to the ISO 639-2 language code for the Unicode
603 string to add.
604 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes
605 that the Unicode string table supports.
606 Language must be a member of this set.
607 @param UnicodeStringTable A pointer to the table of Unicode strings.
608 @param UnicodeString A pointer to the Unicode string to add.
609
610 @retval EFI_SUCCESS The Unicode string that matches the language
611 specified by Language was found in the table of
612 Unicode strings UnicodeStringTable, and it was
613 returned in UnicodeString.
614 @retval EFI_INVALID_PARAMETER Language is NULL.
615 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.
616 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.
617 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.
618 @retval EFI_ALREADY_STARTED A Unicode string with language Language is
619 already present in UnicodeStringTable.
620 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another
621 Unicode string to UnicodeStringTable.
622 @retval EFI_UNSUPPORTED The language specified by Language is not a
623 member of SupportedLanguages.
624
625 **/
626 EFI_STATUS
627 EFIAPI
628 AddUnicodeString (
629 IN CONST CHAR8 *Language,
630 IN CONST CHAR8 *SupportedLanguages,
631 IN EFI_UNICODE_STRING_TABLE **UnicodeStringTable,
632 IN CONST CHAR16 *UnicodeString
633 )
634 {
635 UINTN NumberOfEntries;
636 EFI_UNICODE_STRING_TABLE *OldUnicodeStringTable;
637 EFI_UNICODE_STRING_TABLE *NewUnicodeStringTable;
638 UINTN UnicodeStringLength;
639
640 //
641 // Make sure the parameter are valid
642 //
643 if (Language == NULL || UnicodeString == NULL || UnicodeStringTable == NULL) {
644 return EFI_INVALID_PARAMETER;
645 }
646
647 //
648 // If there are no supported languages, then a Unicode String can not be added
649 //
650 if (SupportedLanguages == NULL) {
651 return EFI_UNSUPPORTED;
652 }
653
654 //
655 // If the Unicode String is empty, then a Unicode String can not be added
656 //
657 if (UnicodeString[0] == 0) {
658 return EFI_INVALID_PARAMETER;
659 }
660
661 //
662 // Make sure Language is a member of SupportedLanguages
663 //
664 while (*SupportedLanguages != 0) {
665 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {
666
667 //
668 // Determine the size of the Unicode String Table by looking for a NULL Language entry
669 //
670 NumberOfEntries = 0;
671 if (*UnicodeStringTable != NULL) {
672 OldUnicodeStringTable = *UnicodeStringTable;
673 while (OldUnicodeStringTable->Language != NULL) {
674 if (CompareIso639LanguageCode (Language, OldUnicodeStringTable->Language)) {
675 return EFI_ALREADY_STARTED;
676 }
677
678 OldUnicodeStringTable++;
679 NumberOfEntries++;
680 }
681 }
682
683 //
684 // Allocate space for a new Unicode String Table. It must hold the current number of
685 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table
686 // marker
687 //
688 NewUnicodeStringTable = AllocatePool ((NumberOfEntries + 2) * sizeof (EFI_UNICODE_STRING_TABLE));
689 if (NewUnicodeStringTable == NULL) {
690 return EFI_OUT_OF_RESOURCES;
691 }
692
693 //
694 // If the current Unicode String Table contains any entries, then copy them to the
695 // newly allocated Unicode String Table.
696 //
697 if (*UnicodeStringTable != NULL) {
698 CopyMem (
699 NewUnicodeStringTable,
700 *UnicodeStringTable,
701 NumberOfEntries * sizeof (EFI_UNICODE_STRING_TABLE)
702 );
703 }
704
705 //
706 // Allocate space for a copy of the Language specifier
707 //
708 NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (3, Language);
709 if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) {
710 gBS->FreePool (NewUnicodeStringTable);
711 return EFI_OUT_OF_RESOURCES;
712 }
713
714 //
715 // Compute the length of the Unicode String
716 //
717 for (UnicodeStringLength = 0; UnicodeString[UnicodeStringLength] != 0; UnicodeStringLength++)
718 ;
719
720 //
721 // Allocate space for a copy of the Unicode String
722 //
723 NewUnicodeStringTable[NumberOfEntries].UnicodeString = AllocateCopyPool (
724 (UnicodeStringLength + 1) * sizeof (CHAR16),
725 UnicodeString
726 );
727 if (NewUnicodeStringTable[NumberOfEntries].UnicodeString == NULL) {
728 gBS->FreePool (NewUnicodeStringTable[NumberOfEntries].Language);
729 gBS->FreePool (NewUnicodeStringTable);
730 return EFI_OUT_OF_RESOURCES;
731 }
732
733 //
734 // Mark the end of the Unicode String Table
735 //
736 NewUnicodeStringTable[NumberOfEntries + 1].Language = NULL;
737 NewUnicodeStringTable[NumberOfEntries + 1].UnicodeString = NULL;
738
739 //
740 // Free the old Unicode String Table
741 //
742 if (*UnicodeStringTable != NULL) {
743 gBS->FreePool (*UnicodeStringTable);
744 }
745
746 //
747 // Point UnicodeStringTable at the newly allocated Unicode String Table
748 //
749 *UnicodeStringTable = NewUnicodeStringTable;
750
751 return EFI_SUCCESS;
752 }
753
754 SupportedLanguages += 3;
755 }
756
757 return EFI_UNSUPPORTED;
758 }
759
760 /**
761 This function frees the table of Unicode strings in UnicodeStringTable.
762 If UnicodeStringTable is NULL, then EFI_SUCCESS is returned.
763 Otherwise, each language code, and each Unicode string in the Unicode string
764 table are freed, and EFI_SUCCESS is returned.
765
766 @param UnicodeStringTable A pointer to the table of Unicode strings.
767
768 @retval EFI_SUCCESS The Unicode string table was freed.
769
770 **/
771 EFI_STATUS
772 EFIAPI
773 FreeUnicodeStringTable (
774 IN EFI_UNICODE_STRING_TABLE *UnicodeStringTable
775 )
776 {
777 UINTN Index;
778
779 //
780 // If the Unicode String Table is NULL, then it is already freed
781 //
782 if (UnicodeStringTable == NULL) {
783 return EFI_SUCCESS;
784 }
785
786 //
787 // Loop through the Unicode String Table until we reach the end of table marker
788 //
789 for (Index = 0; UnicodeStringTable[Index].Language != NULL; Index++) {
790
791 //
792 // Free the Language string from the Unicode String Table
793 //
794 gBS->FreePool (UnicodeStringTable[Index].Language);
795
796 //
797 // Free the Unicode String from the Unicode String Table
798 //
799 if (UnicodeStringTable[Index].UnicodeString != NULL) {
800 gBS->FreePool (UnicodeStringTable[Index].UnicodeString);
801 }
802 }
803
804 //
805 // Free the Unicode String Table itself
806 //
807 gBS->FreePool (UnicodeStringTable);
808
809 return EFI_SUCCESS;
810 }
811