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