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