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