]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - IntelFrameworkPkg/Library/FrameworkUefiLib/UefiLib.c
MdePkg/UefiLib: Add EfiLocateProtocolBuffer()
[mirror_edk2.git] / IntelFrameworkPkg / Library / FrameworkUefiLib / UefiLib.c
... / ...
CommitLineData
1/** @file\r
2 The UEFI Library provides functions and macros that simplify the development of \r
3 UEFI Drivers and UEFI Applications. These functions and macros help manage EFI \r
4 events, build simple locks utilizing EFI Task Priority Levels (TPLs), install \r
5 EFI Driver Model related protocols, manage Unicode string tables for UEFI Drivers, \r
6 and print messages on the console output and standard error devices.\r
7\r
8 Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
9 This program and the accompanying materials\r
10 are licensed and made available under the terms and conditions of the BSD License\r
11 which accompanies this distribution. The full text of the license may be found at\r
12 http://opensource.org/licenses/bsd-license.php\r
13\r
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
16\r
17**/\r
18\r
19\r
20#include "UefiLibInternal.h"\r
21\r
22/**\r
23 Compare whether two names of languages are identical.\r
24\r
25 @param Language1 Name of language 1.\r
26 @param Language2 Name of language 2.\r
27\r
28 @retval TRUE Language 1 and language 2 are the same.\r
29 @retval FALSE Language 1 and language 2 are not the same.\r
30\r
31**/\r
32BOOLEAN\r
33CompareIso639LanguageCode (\r
34 IN CONST CHAR8 *Language1,\r
35 IN CONST CHAR8 *Language2\r
36 )\r
37{\r
38 UINT32 Name1;\r
39 UINT32 Name2;\r
40\r
41 Name1 = ReadUnaligned24 ((CONST UINT32 *) Language1);\r
42 Name2 = ReadUnaligned24 ((CONST UINT32 *) Language2);\r
43\r
44 return (BOOLEAN) (Name1 == Name2);\r
45}\r
46\r
47/**\r
48 Retrieves a pointer to the system configuration table from the EFI System Table\r
49 based on a specified GUID.\r
50 \r
51 This function searches the list of configuration tables stored in the EFI System Table\r
52 for a table with a GUID that matches TableGuid. If a match is found, then a pointer to\r
53 the configuration table is returned in Table., and EFI_SUCCESS is returned. If a matching GUID\r
54 is not found, then EFI_NOT_FOUND is returned.\r
55 If TableGuid is NULL, then ASSERT().\r
56 If Table is NULL, then ASSERT().\r
57\r
58 @param TableGuid Pointer to table's GUID type..\r
59 @param Table Pointer to the table associated with TableGuid in the EFI System Table.\r
60\r
61 @retval EFI_SUCCESS A configuration table matching TableGuid was found.\r
62 @retval EFI_NOT_FOUND A configuration table matching TableGuid could not be found.\r
63\r
64**/\r
65EFI_STATUS\r
66EFIAPI\r
67EfiGetSystemConfigurationTable (\r
68 IN EFI_GUID *TableGuid,\r
69 OUT VOID **Table\r
70 )\r
71{\r
72 EFI_SYSTEM_TABLE *SystemTable;\r
73 UINTN Index;\r
74\r
75 ASSERT (TableGuid != NULL);\r
76 ASSERT (Table != NULL);\r
77\r
78 SystemTable = gST;\r
79 *Table = NULL;\r
80 for (Index = 0; Index < SystemTable->NumberOfTableEntries; Index++) {\r
81 if (CompareGuid (TableGuid, &(SystemTable->ConfigurationTable[Index].VendorGuid))) {\r
82 *Table = SystemTable->ConfigurationTable[Index].VendorTable;\r
83 return EFI_SUCCESS;\r
84 }\r
85 }\r
86\r
87 return EFI_NOT_FOUND;\r
88}\r
89\r
90/**\r
91 Creates and returns a notification event and registers that event with all the protocol\r
92 instances specified by ProtocolGuid.\r
93\r
94 This function causes the notification function to be executed for every protocol of type\r
95 ProtocolGuid instance that exists in the system when this function is invoked. If there are\r
96 no instances of ProtocolGuid in the handle database at the time this function is invoked,\r
97 then the notification function is still executed one time. In addition, every time a protocol\r
98 of type ProtocolGuid instance is installed or reinstalled, the notification function is also\r
99 executed. This function returns the notification event that was created. \r
100 If ProtocolGuid is NULL, then ASSERT().\r
101 If NotifyTpl is not a legal TPL value, then ASSERT().\r
102 If NotifyFunction is NULL, then ASSERT().\r
103 If Registration is NULL, then ASSERT().\r
104\r
105\r
106 @param ProtocolGuid Supplies GUID of the protocol upon whose installation the event is fired.\r
107 @param NotifyTpl Supplies the task priority level of the event notifications.\r
108 @param NotifyFunction Supplies the function to notify when the event is signaled.\r
109 @param NotifyContext The context parameter to pass to NotifyFunction.\r
110 @param Registration A pointer to a memory location to receive the registration value.\r
111 This value is passed to LocateHandle() to obtain new handles that\r
112 have been added that support the ProtocolGuid-specified protocol. \r
113\r
114 @return The notification event that was created.\r
115\r
116**/\r
117EFI_EVENT\r
118EFIAPI\r
119EfiCreateProtocolNotifyEvent(\r
120 IN EFI_GUID *ProtocolGuid,\r
121 IN EFI_TPL NotifyTpl,\r
122 IN EFI_EVENT_NOTIFY NotifyFunction,\r
123 IN VOID *NotifyContext, OPTIONAL\r
124 OUT VOID **Registration\r
125 )\r
126{\r
127 EFI_STATUS Status;\r
128 EFI_EVENT Event;\r
129\r
130 ASSERT (ProtocolGuid != NULL);\r
131 ASSERT (NotifyFunction != NULL);\r
132 ASSERT (Registration != NULL);\r
133\r
134 //\r
135 // Create the event\r
136 //\r
137\r
138 Status = gBS->CreateEvent (\r
139 EVT_NOTIFY_SIGNAL,\r
140 NotifyTpl,\r
141 NotifyFunction,\r
142 NotifyContext,\r
143 &Event\r
144 );\r
145 ASSERT_EFI_ERROR (Status);\r
146\r
147 //\r
148 // Register for protocol notifications on this event\r
149 //\r
150\r
151 Status = gBS->RegisterProtocolNotify (\r
152 ProtocolGuid,\r
153 Event,\r
154 Registration\r
155 );\r
156\r
157 ASSERT_EFI_ERROR (Status);\r
158\r
159 //\r
160 // Kick the event so we will perform an initial pass of\r
161 // current installed drivers\r
162 //\r
163\r
164 gBS->SignalEvent (Event);\r
165 return Event;\r
166}\r
167\r
168/**\r
169 Creates a named event that can be signaled with EfiNamedEventSignal().\r
170\r
171 This function creates an event using NotifyTpl, NoifyFunction, and NotifyContext.\r
172 This event is signaled with EfiNamedEventSignal(). This provides the ability for one or more\r
173 listeners on the same event named by the GUID specified by Name. \r
174 If Name is NULL, then ASSERT().\r
175 If NotifyTpl is not a legal TPL value, then ASSERT().\r
176 If NotifyFunction is NULL, then ASSERT().\r
177\r
178 @param Name Supplies GUID name of the event.\r
179 @param NotifyTpl Supplies the task priority level of the event notifications.\r
180 @param NotifyFunction Supplies the function to notify when the event is signaled.\r
181 @param NotifyContext The context parameter to pass to NotifyFunction. \r
182 @param Registration A pointer to a memory location to receive the registration value.\r
183\r
184 @retval EFI_SUCCESS A named event was created.\r
185 @retval EFI_OUT_OF_RESOURCES There are not enough resource to create the named event.\r
186\r
187**/\r
188EFI_STATUS\r
189EFIAPI\r
190EfiNamedEventListen (\r
191 IN CONST EFI_GUID *Name,\r
192 IN EFI_TPL NotifyTpl,\r
193 IN EFI_EVENT_NOTIFY NotifyFunction,\r
194 IN CONST VOID *NotifyContext, OPTIONAL\r
195 OUT VOID *Registration OPTIONAL\r
196 )\r
197{\r
198 EFI_STATUS Status;\r
199 EFI_EVENT Event;\r
200 VOID *RegistrationLocal;\r
201\r
202 ASSERT (Name != NULL);\r
203 ASSERT (NotifyFunction != NULL);\r
204 ASSERT (NotifyTpl <= TPL_HIGH_LEVEL);\r
205 \r
206 //\r
207 // Create event\r
208 //\r
209 Status = gBS->CreateEvent (\r
210 EVT_NOTIFY_SIGNAL,\r
211 NotifyTpl,\r
212 NotifyFunction,\r
213 (VOID *) NotifyContext,\r
214 &Event\r
215 );\r
216 ASSERT_EFI_ERROR (Status);\r
217\r
218 //\r
219 // The Registration is not optional to RegisterProtocolNotify().\r
220 // To make it optional to EfiNamedEventListen(), may need to substitute with a local.\r
221 //\r
222 if (Registration != NULL) {\r
223 RegistrationLocal = Registration;\r
224 } else {\r
225 RegistrationLocal = &RegistrationLocal;\r
226 }\r
227\r
228 //\r
229 // Register for an installation of protocol interface\r
230 //\r
231\r
232 Status = gBS->RegisterProtocolNotify (\r
233 (EFI_GUID *) Name,\r
234 Event,\r
235 RegistrationLocal\r
236 );\r
237 ASSERT_EFI_ERROR (Status);\r
238\r
239 return Status;\r
240}\r
241\r
242/**\r
243 Signals a named event created with EfiNamedEventListen().\r
244\r
245 This function signals the named event specified by Name. The named event must have been\r
246 created with EfiNamedEventListen().\r
247 If Name is NULL, then ASSERT().\r
248\r
249 @param Name Supplies GUID name of the event.\r
250\r
251 @retval EFI_SUCCESS A named event was signaled.\r
252 @retval EFI_OUT_OF_RESOURCES There are not enough resource to signal the named event.\r
253\r
254**/\r
255EFI_STATUS\r
256EFIAPI\r
257EfiNamedEventSignal (\r
258 IN CONST EFI_GUID *Name\r
259 )\r
260{\r
261 EFI_STATUS Status;\r
262 EFI_HANDLE Handle;\r
263\r
264 ASSERT(Name != NULL);\r
265\r
266 Handle = NULL;\r
267 Status = gBS->InstallProtocolInterface (\r
268 &Handle,\r
269 (EFI_GUID *) Name,\r
270 EFI_NATIVE_INTERFACE,\r
271 NULL\r
272 );\r
273 ASSERT_EFI_ERROR (Status);\r
274\r
275 Status = gBS->UninstallProtocolInterface (\r
276 Handle,\r
277 (EFI_GUID *) Name,\r
278 NULL\r
279 );\r
280 ASSERT_EFI_ERROR (Status);\r
281\r
282 return Status;\r
283}\r
284\r
285/**\r
286 Signals an event group by placing a new event in the group temporarily and\r
287 signaling it.\r
288\r
289 @param[in] EventGroup Supplies the unique identifier of the event\r
290 group to signal.\r
291\r
292 @retval EFI_SUCCESS The event group was signaled successfully.\r
293 @retval EFI_INVALID_PARAMETER EventGroup is NULL.\r
294 @return Error codes that report problems about event\r
295 creation or signaling.\r
296**/\r
297EFI_STATUS\r
298EFIAPI\r
299EfiEventGroupSignal (\r
300 IN CONST EFI_GUID *EventGroup\r
301 )\r
302{\r
303 EFI_STATUS Status;\r
304 EFI_EVENT Event;\r
305\r
306 if (EventGroup == NULL) {\r
307 return EFI_INVALID_PARAMETER;\r
308 }\r
309\r
310 Status = gBS->CreateEventEx (\r
311 EVT_NOTIFY_SIGNAL,\r
312 TPL_CALLBACK,\r
313 EfiEventEmptyFunction,\r
314 NULL,\r
315 EventGroup,\r
316 &Event\r
317 );\r
318 if (EFI_ERROR (Status)) {\r
319 return Status;\r
320 }\r
321\r
322 Status = gBS->SignalEvent (Event);\r
323 gBS->CloseEvent (Event);\r
324\r
325 return Status;\r
326}\r
327\r
328/**\r
329 An empty function that can be used as NotifyFunction parameter of\r
330 CreateEvent() or CreateEventEx().\r
331\r
332 @param Event Event whose notification function is being invoked.\r
333 @param Context The pointer to the notification function's context,\r
334 which is implementation-dependent.\r
335\r
336**/\r
337VOID\r
338EFIAPI\r
339EfiEventEmptyFunction (\r
340 IN EFI_EVENT Event,\r
341 IN VOID *Context\r
342 )\r
343{\r
344}\r
345\r
346/** \r
347 Returns the current TPL.\r
348\r
349 This function returns the current TPL. There is no EFI service to directly \r
350 retrieve the current TPL. Instead, the RaiseTPL() function is used to raise \r
351 the TPL to TPL_HIGH_LEVEL. This will return the current TPL. The TPL level \r
352 can then immediately be restored back to the current TPL level with a call \r
353 to RestoreTPL().\r
354\r
355 @return The current TPL.\r
356\r
357**/\r
358EFI_TPL\r
359EFIAPI\r
360EfiGetCurrentTpl (\r
361 VOID\r
362 )\r
363{\r
364 EFI_TPL Tpl;\r
365\r
366 Tpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r
367 gBS->RestoreTPL (Tpl);\r
368\r
369 return Tpl;\r
370}\r
371\r
372\r
373/**\r
374 Initializes a basic mutual exclusion lock.\r
375\r
376 This function initializes a basic mutual exclusion lock to the released state \r
377 and returns the lock. Each lock provides mutual exclusion access at its task \r
378 priority level. Since there is no preemption or multiprocessor support in EFI,\r
379 acquiring the lock only consists of raising to the locks TPL.\r
380 If Lock is NULL, then ASSERT().\r
381 If Priority is not a valid TPL value, then ASSERT().\r
382\r
383 @param Lock A pointer to the lock data structure to initialize.\r
384 @param Priority EFI TPL associated with the lock.\r
385\r
386 @return The lock.\r
387\r
388**/\r
389EFI_LOCK *\r
390EFIAPI\r
391EfiInitializeLock (\r
392 IN OUT EFI_LOCK *Lock,\r
393 IN EFI_TPL Priority\r
394 )\r
395{\r
396 ASSERT (Lock != NULL);\r
397 ASSERT (Priority <= TPL_HIGH_LEVEL);\r
398\r
399 Lock->Tpl = Priority;\r
400 Lock->OwnerTpl = TPL_APPLICATION;\r
401 Lock->Lock = EfiLockReleased ;\r
402 return Lock;\r
403}\r
404\r
405/**\r
406 Acquires ownership of a lock.\r
407\r
408 This function raises the system's current task priority level to the task \r
409 priority level of the mutual exclusion lock. Then, it places the lock in the \r
410 acquired state.\r
411 If Lock is NULL, then ASSERT().\r
412 If Lock is not initialized, then ASSERT().\r
413 If Lock is already in the acquired state, then ASSERT().\r
414\r
415 @param Lock A pointer to the lock to acquire.\r
416\r
417**/\r
418VOID\r
419EFIAPI\r
420EfiAcquireLock (\r
421 IN EFI_LOCK *Lock\r
422 )\r
423{\r
424 ASSERT (Lock != NULL);\r
425 ASSERT (Lock->Lock == EfiLockReleased);\r
426\r
427 Lock->OwnerTpl = gBS->RaiseTPL (Lock->Tpl);\r
428 Lock->Lock = EfiLockAcquired;\r
429}\r
430\r
431/**\r
432 Acquires ownership of a lock.\r
433\r
434 This function raises the system's current task priority level to the task priority\r
435 level of the mutual exclusion lock. Then, it attempts to place the lock in the acquired state.\r
436 If the lock is already in the acquired state, then EFI_ACCESS_DENIED is returned.\r
437 Otherwise, EFI_SUCCESS is returned.\r
438 If Lock is NULL, then ASSERT().\r
439 If Lock is not initialized, then ASSERT().\r
440\r
441 @param Lock A pointer to the lock to acquire.\r
442\r
443 @retval EFI_SUCCESS The lock was acquired.\r
444 @retval EFI_ACCESS_DENIED The lock could not be acquired because it is already owned.\r
445\r
446**/\r
447EFI_STATUS\r
448EFIAPI\r
449EfiAcquireLockOrFail (\r
450 IN EFI_LOCK *Lock\r
451 )\r
452{\r
453\r
454 ASSERT (Lock != NULL);\r
455 ASSERT (Lock->Lock != EfiLockUninitialized);\r
456\r
457 if (Lock->Lock == EfiLockAcquired) {\r
458 //\r
459 // Lock is already owned, so bail out\r
460 //\r
461 return EFI_ACCESS_DENIED;\r
462 }\r
463\r
464 Lock->OwnerTpl = gBS->RaiseTPL (Lock->Tpl);\r
465\r
466 Lock->Lock = EfiLockAcquired;\r
467\r
468 return EFI_SUCCESS;\r
469}\r
470\r
471/**\r
472 Releases ownership of a lock.\r
473\r
474 This function transitions a mutual exclusion lock from the acquired state to \r
475 the released state, and restores the system's task priority level to its \r
476 previous level.\r
477 If Lock is NULL, then ASSERT().\r
478 If Lock is not initialized, then ASSERT().\r
479 If Lock is already in the released state, then ASSERT().\r
480\r
481 @param Lock A pointer to the lock to release.\r
482\r
483**/\r
484VOID\r
485EFIAPI\r
486EfiReleaseLock (\r
487 IN EFI_LOCK *Lock\r
488 )\r
489{\r
490 EFI_TPL Tpl;\r
491\r
492 ASSERT (Lock != NULL);\r
493 ASSERT (Lock->Lock == EfiLockAcquired);\r
494\r
495 Tpl = Lock->OwnerTpl;\r
496\r
497 Lock->Lock = EfiLockReleased;\r
498\r
499 gBS->RestoreTPL (Tpl);\r
500}\r
501\r
502/**\r
503 Tests whether a controller handle is being managed by a specific driver.\r
504\r
505 This function tests whether the driver specified by DriverBindingHandle is\r
506 currently managing the controller specified by ControllerHandle. This test\r
507 is performed by evaluating if the the protocol specified by ProtocolGuid is\r
508 present on ControllerHandle and is was opened by DriverBindingHandle with an\r
509 attribute of EFI_OPEN_PROTOCOL_BY_DRIVER. \r
510 If ProtocolGuid is NULL, then ASSERT().\r
511\r
512 @param ControllerHandle A handle for a controller to test.\r
513 @param DriverBindingHandle Specifies the driver binding handle for the\r
514 driver.\r
515 @param ProtocolGuid Specifies the protocol that the driver specified\r
516 by DriverBindingHandle opens in its Start()\r
517 function.\r
518\r
519 @retval EFI_SUCCESS ControllerHandle is managed by the driver\r
520 specified by DriverBindingHandle.\r
521 @retval EFI_UNSUPPORTED ControllerHandle is not managed by the driver\r
522 specified by DriverBindingHandle.\r
523\r
524**/\r
525EFI_STATUS\r
526EFIAPI\r
527EfiTestManagedDevice (\r
528 IN CONST EFI_HANDLE ControllerHandle,\r
529 IN CONST EFI_HANDLE DriverBindingHandle,\r
530 IN CONST EFI_GUID *ProtocolGuid\r
531 )\r
532{\r
533 EFI_STATUS Status;\r
534 VOID *ManagedInterface;\r
535\r
536 ASSERT (ProtocolGuid != NULL);\r
537\r
538 Status = gBS->OpenProtocol (\r
539 ControllerHandle,\r
540 (EFI_GUID *) ProtocolGuid,\r
541 &ManagedInterface,\r
542 DriverBindingHandle,\r
543 ControllerHandle,\r
544 EFI_OPEN_PROTOCOL_BY_DRIVER\r
545 );\r
546 if (!EFI_ERROR (Status)) {\r
547 gBS->CloseProtocol (\r
548 ControllerHandle,\r
549 (EFI_GUID *) ProtocolGuid,\r
550 DriverBindingHandle,\r
551 ControllerHandle\r
552 );\r
553 return EFI_UNSUPPORTED;\r
554 }\r
555\r
556 if (Status != EFI_ALREADY_STARTED) {\r
557 return EFI_UNSUPPORTED;\r
558 }\r
559\r
560 return EFI_SUCCESS;\r
561}\r
562\r
563/**\r
564 Tests whether a child handle is a child device of the controller.\r
565\r
566 This function tests whether ChildHandle is one of the children of\r
567 ControllerHandle. This test is performed by checking to see if the protocol\r
568 specified by ProtocolGuid is present on ControllerHandle and opened by\r
569 ChildHandle with an attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.\r
570 If ProtocolGuid is NULL, then ASSERT().\r
571\r
572 @param ControllerHandle A handle for a (parent) controller to test. \r
573 @param ChildHandle A child handle to test.\r
574 @param ProtocolGuid Supplies the protocol that the child controller\r
575 opens on its parent controller. \r
576\r
577 @retval EFI_SUCCESS ChildHandle is a child of the ControllerHandle.\r
578 @retval EFI_UNSUPPORTED ChildHandle is not a child of the\r
579 ControllerHandle.\r
580\r
581**/\r
582EFI_STATUS\r
583EFIAPI\r
584EfiTestChildHandle (\r
585 IN CONST EFI_HANDLE ControllerHandle,\r
586 IN CONST EFI_HANDLE ChildHandle,\r
587 IN CONST EFI_GUID *ProtocolGuid\r
588 )\r
589{\r
590 EFI_STATUS Status;\r
591 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfoBuffer;\r
592 UINTN EntryCount;\r
593 UINTN Index;\r
594\r
595 ASSERT (ProtocolGuid != NULL);\r
596\r
597 //\r
598 // Retrieve the list of agents that are consuming the specific protocol\r
599 // on ControllerHandle.\r
600 //\r
601 Status = gBS->OpenProtocolInformation (\r
602 ControllerHandle,\r
603 (EFI_GUID *) ProtocolGuid,\r
604 &OpenInfoBuffer,\r
605 &EntryCount\r
606 );\r
607 if (EFI_ERROR (Status)) {\r
608 return EFI_UNSUPPORTED;\r
609 }\r
610\r
611 //\r
612 // Inspect if ChildHandle is one of the agents.\r
613 //\r
614 Status = EFI_UNSUPPORTED;\r
615 for (Index = 0; Index < EntryCount; Index++) {\r
616 if ((OpenInfoBuffer[Index].ControllerHandle == ChildHandle) &&\r
617 (OpenInfoBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {\r
618 Status = EFI_SUCCESS;\r
619 break;\r
620 }\r
621 }\r
622\r
623 FreePool (OpenInfoBuffer);\r
624 return Status;\r
625}\r
626\r
627/**\r
628 This function looks up a Unicode string in UnicodeStringTable.\r
629\r
630 If Language is a member of SupportedLanguages and a Unicode string is found in\r
631 UnicodeStringTable that matches the language code specified by Language, then it\r
632 is returned in UnicodeString.\r
633\r
634 @param Language A pointer to the ISO 639-2 language code for the \r
635 Unicode string to look up and return.\r
636 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes \r
637 that the Unicode string table supports. Language \r
638 must be a member of this set.\r
639 @param UnicodeStringTable A pointer to the table of Unicode strings.\r
640 @param UnicodeString A pointer to the Unicode string from UnicodeStringTable\r
641 that matches the language specified by Language.\r
642\r
643 @retval EFI_SUCCESS The Unicode string that matches the language \r
644 specified by Language was found\r
645 in the table of Unicode strings UnicodeStringTable, \r
646 and it was returned in UnicodeString.\r
647 @retval EFI_INVALID_PARAMETER Language is NULL.\r
648 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.\r
649 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.\r
650 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.\r
651 @retval EFI_UNSUPPORTED The language specified by Language is not a \r
652 member of SupportedLanguages.\r
653 @retval EFI_UNSUPPORTED The language specified by Language is not \r
654 supported by UnicodeStringTable.\r
655\r
656**/\r
657EFI_STATUS\r
658EFIAPI\r
659LookupUnicodeString (\r
660 IN CONST CHAR8 *Language,\r
661 IN CONST CHAR8 *SupportedLanguages,\r
662 IN CONST EFI_UNICODE_STRING_TABLE *UnicodeStringTable,\r
663 OUT CHAR16 **UnicodeString\r
664 )\r
665{\r
666 //\r
667 // Make sure the parameters are valid\r
668 //\r
669 if (Language == NULL || UnicodeString == NULL) {\r
670 return EFI_INVALID_PARAMETER;\r
671 }\r
672\r
673 //\r
674 // If there are no supported languages, or the Unicode String Table is empty, then the\r
675 // Unicode String specified by Language is not supported by this Unicode String Table\r
676 //\r
677 if (SupportedLanguages == NULL || UnicodeStringTable == NULL) {\r
678 return EFI_UNSUPPORTED;\r
679 }\r
680\r
681 //\r
682 // Make sure Language is in the set of Supported Languages\r
683 //\r
684 while (*SupportedLanguages != 0) {\r
685 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
686\r
687 //\r
688 // Search the Unicode String Table for the matching Language specifier\r
689 //\r
690 while (UnicodeStringTable->Language != NULL) {\r
691 if (CompareIso639LanguageCode (Language, UnicodeStringTable->Language)) {\r
692\r
693 //\r
694 // A matching string was found, so return it\r
695 //\r
696 *UnicodeString = UnicodeStringTable->UnicodeString;\r
697 return EFI_SUCCESS;\r
698 }\r
699\r
700 UnicodeStringTable++;\r
701 }\r
702\r
703 return EFI_UNSUPPORTED;\r
704 }\r
705\r
706 SupportedLanguages += 3;\r
707 }\r
708\r
709 return EFI_UNSUPPORTED;\r
710}\r
711\r
712\r
713\r
714/**\r
715 This function looks up a Unicode string in UnicodeStringTable.\r
716\r
717 If Language is a member of SupportedLanguages and a Unicode string is found in\r
718 UnicodeStringTable that matches the language code specified by Language, then\r
719 it is returned in UnicodeString.\r
720\r
721 @param Language A pointer to an ASCII string containing the ISO 639-2 or the\r
722 RFC 4646 language code for the Unicode string to look up and\r
723 return. If Iso639Language is TRUE, then this ASCII string is\r
724 not assumed to be Null-terminated, and only the first three\r
725 characters are used. If Iso639Language is FALSE, then this ASCII\r
726 string must be Null-terminated. \r
727 @param SupportedLanguages A pointer to a Null-terminated ASCII string that contains a\r
728 set of ISO 639-2 or RFC 4646 language codes that the Unicode\r
729 string table supports. Language must be a member of this set.\r
730 If Iso639Language is TRUE, then this string contains one or more\r
731 ISO 639-2 language codes with no separator characters. If Iso639Language\r
732 is FALSE, then is string contains one or more RFC 4646 language\r
733 codes separated by ';'.\r
734 @param UnicodeStringTable A pointer to the table of Unicode strings. Type EFI_UNICODE_STRING_TABLE\r
735 is defined in "Related Definitions".\r
736 @param UnicodeString A pointer to the Null-terminated Unicode string from UnicodeStringTable\r
737 that matches the language specified by Language.\r
738 @param Iso639Language Specifies the supported language code format. If it is TRUE, then\r
739 Language and SupportedLanguages follow ISO 639-2 language code format.\r
740 Otherwise, they follow RFC 4646 language code format.\r
741\r
742\r
743 @retval EFI_SUCCESS The Unicode string that matches the language specified by Language\r
744 was found in the table of Unicode strings UnicodeStringTable, and\r
745 it was returned in UnicodeString.\r
746 @retval EFI_INVALID_PARAMETER Language is NULL. \r
747 @retval EFI_INVALID_PARAMETER UnicodeString is NULL. \r
748 @retval EFI_UNSUPPORTED SupportedLanguages is NULL. \r
749 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL. \r
750 @retval EFI_UNSUPPORTED The language specified by Language is not a member of SupportedLanguages. \r
751 @retval EFI_UNSUPPORTED The language specified by Language is not supported by UnicodeStringTable.\r
752\r
753**/\r
754EFI_STATUS\r
755EFIAPI\r
756LookupUnicodeString2 (\r
757 IN CONST CHAR8 *Language,\r
758 IN CONST CHAR8 *SupportedLanguages,\r
759 IN CONST EFI_UNICODE_STRING_TABLE *UnicodeStringTable,\r
760 OUT CHAR16 **UnicodeString,\r
761 IN BOOLEAN Iso639Language\r
762 )\r
763{\r
764 BOOLEAN Found;\r
765 UINTN Index;\r
766 CHAR8 *LanguageString;\r
767\r
768 //\r
769 // Make sure the parameters are valid\r
770 //\r
771 if (Language == NULL || UnicodeString == NULL) {\r
772 return EFI_INVALID_PARAMETER;\r
773 }\r
774\r
775 //\r
776 // If there are no supported languages, or the Unicode String Table is empty, then the\r
777 // Unicode String specified by Language is not supported by this Unicode String Table\r
778 //\r
779 if (SupportedLanguages == NULL || UnicodeStringTable == NULL) {\r
780 return EFI_UNSUPPORTED;\r
781 }\r
782\r
783 //\r
784 // Make sure Language is in the set of Supported Languages\r
785 //\r
786 Found = FALSE;\r
787 while (*SupportedLanguages != 0) {\r
788 if (Iso639Language) {\r
789 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
790 Found = TRUE;\r
791 break;\r
792 }\r
793 SupportedLanguages += 3;\r
794 } else {\r
795 for (Index = 0; SupportedLanguages[Index] != 0 && SupportedLanguages[Index] != ';'; Index++);\r
796 if ((AsciiStrnCmp(SupportedLanguages, Language, Index) == 0) && (Language[Index] == 0)) {\r
797 Found = TRUE;\r
798 break;\r
799 }\r
800 SupportedLanguages += Index;\r
801 for (; *SupportedLanguages != 0 && *SupportedLanguages == ';'; SupportedLanguages++);\r
802 }\r
803 }\r
804\r
805 //\r
806 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED\r
807 //\r
808 if (!Found) {\r
809 return EFI_UNSUPPORTED;\r
810 }\r
811\r
812 //\r
813 // Search the Unicode String Table for the matching Language specifier\r
814 //\r
815 while (UnicodeStringTable->Language != NULL) {\r
816 LanguageString = UnicodeStringTable->Language;\r
817 while (0 != *LanguageString) {\r
818 for (Index = 0 ;LanguageString[Index] != 0 && LanguageString[Index] != ';'; Index++);\r
819 if (AsciiStrnCmp(LanguageString, Language, Index) == 0) {\r
820 *UnicodeString = UnicodeStringTable->UnicodeString;\r
821 return EFI_SUCCESS;\r
822 }\r
823 LanguageString += Index;\r
824 for (Index = 0 ;LanguageString[Index] != 0 && LanguageString[Index] == ';'; Index++);\r
825 }\r
826 UnicodeStringTable++;\r
827 }\r
828\r
829 return EFI_UNSUPPORTED;\r
830}\r
831\r
832\r
833/**\r
834 This function adds a Unicode string to UnicodeStringTable.\r
835\r
836 If Language is a member of SupportedLanguages then UnicodeString is added to \r
837 UnicodeStringTable. New buffers are allocated for both Language and \r
838 UnicodeString. The contents of Language and UnicodeString are copied into \r
839 these new buffers. These buffers are automatically freed when \r
840 FreeUnicodeStringTable() is called.\r
841\r
842 @param Language A pointer to the ISO 639-2 language code for the Unicode \r
843 string to add.\r
844 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes\r
845 that the Unicode string table supports.\r
846 Language must be a member of this set.\r
847 @param UnicodeStringTable A pointer to the table of Unicode strings.\r
848 @param UnicodeString A pointer to the Unicode string to add.\r
849\r
850 @retval EFI_SUCCESS The Unicode string that matches the language \r
851 specified by Language was found in the table of \r
852 Unicode strings UnicodeStringTable, and it was \r
853 returned in UnicodeString.\r
854 @retval EFI_INVALID_PARAMETER Language is NULL.\r
855 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.\r
856 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.\r
857 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.\r
858 @retval EFI_ALREADY_STARTED A Unicode string with language Language is \r
859 already present in UnicodeStringTable.\r
860 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another \r
861 Unicode string to UnicodeStringTable.\r
862 @retval EFI_UNSUPPORTED The language specified by Language is not a \r
863 member of SupportedLanguages.\r
864\r
865**/\r
866EFI_STATUS\r
867EFIAPI\r
868AddUnicodeString (\r
869 IN CONST CHAR8 *Language,\r
870 IN CONST CHAR8 *SupportedLanguages,\r
871 IN EFI_UNICODE_STRING_TABLE **UnicodeStringTable,\r
872 IN CONST CHAR16 *UnicodeString\r
873 )\r
874{\r
875 UINTN NumberOfEntries;\r
876 EFI_UNICODE_STRING_TABLE *OldUnicodeStringTable;\r
877 EFI_UNICODE_STRING_TABLE *NewUnicodeStringTable;\r
878 UINTN UnicodeStringLength;\r
879\r
880 //\r
881 // Make sure the parameter are valid\r
882 //\r
883 if (Language == NULL || UnicodeString == NULL || UnicodeStringTable == NULL) {\r
884 return EFI_INVALID_PARAMETER;\r
885 }\r
886\r
887 //\r
888 // If there are no supported languages, then a Unicode String can not be added\r
889 //\r
890 if (SupportedLanguages == NULL) {\r
891 return EFI_UNSUPPORTED;\r
892 }\r
893\r
894 //\r
895 // If the Unicode String is empty, then a Unicode String can not be added\r
896 //\r
897 if (UnicodeString[0] == 0) {\r
898 return EFI_INVALID_PARAMETER;\r
899 }\r
900\r
901 //\r
902 // Make sure Language is a member of SupportedLanguages\r
903 //\r
904 while (*SupportedLanguages != 0) {\r
905 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
906\r
907 //\r
908 // Determine the size of the Unicode String Table by looking for a NULL Language entry\r
909 //\r
910 NumberOfEntries = 0;\r
911 if (*UnicodeStringTable != NULL) {\r
912 OldUnicodeStringTable = *UnicodeStringTable;\r
913 while (OldUnicodeStringTable->Language != NULL) {\r
914 if (CompareIso639LanguageCode (Language, OldUnicodeStringTable->Language)) {\r
915 return EFI_ALREADY_STARTED;\r
916 }\r
917\r
918 OldUnicodeStringTable++;\r
919 NumberOfEntries++;\r
920 }\r
921 }\r
922\r
923 //\r
924 // Allocate space for a new Unicode String Table. It must hold the current number of\r
925 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table\r
926 // marker\r
927 //\r
928 NewUnicodeStringTable = AllocatePool ((NumberOfEntries + 2) * sizeof (EFI_UNICODE_STRING_TABLE));\r
929 if (NewUnicodeStringTable == NULL) {\r
930 return EFI_OUT_OF_RESOURCES;\r
931 }\r
932\r
933 //\r
934 // If the current Unicode String Table contains any entries, then copy them to the\r
935 // newly allocated Unicode String Table.\r
936 //\r
937 if (*UnicodeStringTable != NULL) {\r
938 CopyMem (\r
939 NewUnicodeStringTable,\r
940 *UnicodeStringTable,\r
941 NumberOfEntries * sizeof (EFI_UNICODE_STRING_TABLE)\r
942 );\r
943 }\r
944\r
945 //\r
946 // Allocate space for a copy of the Language specifier\r
947 //\r
948 NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (3, Language);\r
949 if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) {\r
950 gBS->FreePool (NewUnicodeStringTable);\r
951 return EFI_OUT_OF_RESOURCES;\r
952 }\r
953\r
954 //\r
955 // Compute the length of the Unicode String\r
956 //\r
957 for (UnicodeStringLength = 0; UnicodeString[UnicodeStringLength] != 0; UnicodeStringLength++)\r
958 ;\r
959\r
960 //\r
961 // Allocate space for a copy of the Unicode String\r
962 //\r
963 NewUnicodeStringTable[NumberOfEntries].UnicodeString = AllocateCopyPool (\r
964 (UnicodeStringLength + 1) * sizeof (CHAR16),\r
965 UnicodeString\r
966 );\r
967 if (NewUnicodeStringTable[NumberOfEntries].UnicodeString == NULL) {\r
968 gBS->FreePool (NewUnicodeStringTable[NumberOfEntries].Language);\r
969 gBS->FreePool (NewUnicodeStringTable);\r
970 return EFI_OUT_OF_RESOURCES;\r
971 }\r
972\r
973 //\r
974 // Mark the end of the Unicode String Table\r
975 //\r
976 NewUnicodeStringTable[NumberOfEntries + 1].Language = NULL;\r
977 NewUnicodeStringTable[NumberOfEntries + 1].UnicodeString = NULL;\r
978\r
979 //\r
980 // Free the old Unicode String Table\r
981 //\r
982 if (*UnicodeStringTable != NULL) {\r
983 gBS->FreePool (*UnicodeStringTable);\r
984 }\r
985\r
986 //\r
987 // Point UnicodeStringTable at the newly allocated Unicode String Table\r
988 //\r
989 *UnicodeStringTable = NewUnicodeStringTable;\r
990\r
991 return EFI_SUCCESS;\r
992 }\r
993\r
994 SupportedLanguages += 3;\r
995 }\r
996\r
997 return EFI_UNSUPPORTED;\r
998}\r
999\r
1000\r
1001/**\r
1002 This function adds the Null-terminated Unicode string specified by UnicodeString\r
1003 to UnicodeStringTable.\r
1004\r
1005 If Language is a member of SupportedLanguages then UnicodeString is added to\r
1006 UnicodeStringTable. New buffers are allocated for both Language and UnicodeString.\r
1007 The contents of Language and UnicodeString are copied into these new buffers.\r
1008 These buffers are automatically freed when EfiLibFreeUnicodeStringTable() is called.\r
1009\r
1010 @param Language A pointer to an ASCII string containing the ISO 639-2 or\r
1011 the RFC 4646 language code for the Unicode string to add.\r
1012 If Iso639Language is TRUE, then this ASCII string is not\r
1013 assumed to be Null-terminated, and only the first three\r
1014 chacters are used. If Iso639Language is FALSE, then this\r
1015 ASCII string must be Null-terminated.\r
1016 @param SupportedLanguages A pointer to a Null-terminated ASCII string that contains\r
1017 a set of ISO 639-2 or RFC 4646 language codes that the Unicode\r
1018 string table supports. Language must be a member of this set.\r
1019 If Iso639Language is TRUE, then this string contains one or more\r
1020 ISO 639-2 language codes with no separator characters.\r
1021 If Iso639Language is FALSE, then is string contains one or more\r
1022 RFC 4646 language codes separated by ';'.\r
1023 @param UnicodeStringTable A pointer to the table of Unicode strings. Type EFI_UNICODE_STRING_TABLE\r
1024 is defined in "Related Definitions".\r
1025 @param UnicodeString A pointer to the Unicode string to add. \r
1026 @param Iso639Language Specifies the supported language code format. If it is TRUE,\r
1027 then Language and SupportedLanguages follow ISO 639-2 language code format.\r
1028 Otherwise, they follow RFC 4646 language code format.\r
1029\r
1030 @retval EFI_SUCCESS The Unicode string that matches the language specified by\r
1031 Language was found in the table of Unicode strings UnicodeStringTable,\r
1032 and it was returned in UnicodeString. \r
1033 @retval EFI_INVALID_PARAMETER Language is NULL. \r
1034 @retval EFI_INVALID_PARAMETER UnicodeString is NULL. \r
1035 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string. \r
1036 @retval EFI_UNSUPPORTED SupportedLanguages is NULL. \r
1037 @retval EFI_ALREADY_STARTED A Unicode string with language Language is already present in\r
1038 UnicodeStringTable. \r
1039 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another Unicode string UnicodeStringTable. \r
1040 @retval EFI_UNSUPPORTED The language specified by Language is not a member of SupportedLanguages.\r
1041\r
1042**/\r
1043EFI_STATUS\r
1044EFIAPI\r
1045AddUnicodeString2 (\r
1046 IN CONST CHAR8 *Language,\r
1047 IN CONST CHAR8 *SupportedLanguages,\r
1048 IN EFI_UNICODE_STRING_TABLE **UnicodeStringTable,\r
1049 IN CONST CHAR16 *UnicodeString,\r
1050 IN BOOLEAN Iso639Language\r
1051 )\r
1052{\r
1053 UINTN NumberOfEntries;\r
1054 EFI_UNICODE_STRING_TABLE *OldUnicodeStringTable;\r
1055 EFI_UNICODE_STRING_TABLE *NewUnicodeStringTable;\r
1056 UINTN UnicodeStringLength;\r
1057 BOOLEAN Found;\r
1058 UINTN Index;\r
1059 CHAR8 *LanguageString;\r
1060\r
1061 //\r
1062 // Make sure the parameter are valid\r
1063 //\r
1064 if (Language == NULL || UnicodeString == NULL || UnicodeStringTable == NULL) {\r
1065 return EFI_INVALID_PARAMETER;\r
1066 }\r
1067\r
1068 //\r
1069 // If there are no supported languages, then a Unicode String can not be added\r
1070 //\r
1071 if (SupportedLanguages == NULL) {\r
1072 return EFI_UNSUPPORTED;\r
1073 }\r
1074\r
1075 //\r
1076 // If the Unicode String is empty, then a Unicode String can not be added\r
1077 //\r
1078 if (UnicodeString[0] == 0) {\r
1079 return EFI_INVALID_PARAMETER;\r
1080 }\r
1081\r
1082 //\r
1083 // Make sure Language is a member of SupportedLanguages\r
1084 //\r
1085 Found = FALSE;\r
1086 while (*SupportedLanguages != 0) {\r
1087 if (Iso639Language) {\r
1088 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
1089 Found = TRUE;\r
1090 break;\r
1091 }\r
1092 SupportedLanguages += 3;\r
1093 } else {\r
1094 for (Index = 0; SupportedLanguages[Index] != 0 && SupportedLanguages[Index] != ';'; Index++);\r
1095 if (AsciiStrnCmp(SupportedLanguages, Language, Index) == 0) {\r
1096 Found = TRUE;\r
1097 break;\r
1098 }\r
1099 SupportedLanguages += Index;\r
1100 for (; *SupportedLanguages != 0 && *SupportedLanguages == ';'; SupportedLanguages++);\r
1101 }\r
1102 }\r
1103\r
1104 //\r
1105 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED\r
1106 //\r
1107 if (!Found) {\r
1108 return EFI_UNSUPPORTED;\r
1109 }\r
1110\r
1111 //\r
1112 // Determine the size of the Unicode String Table by looking for a NULL Language entry\r
1113 //\r
1114 NumberOfEntries = 0;\r
1115 if (*UnicodeStringTable != NULL) {\r
1116 OldUnicodeStringTable = *UnicodeStringTable;\r
1117 while (OldUnicodeStringTable->Language != NULL) {\r
1118 LanguageString = OldUnicodeStringTable->Language;\r
1119\r
1120 while (*LanguageString != 0) {\r
1121 for (Index = 0; LanguageString[Index] != 0 && LanguageString[Index] != ';'; Index++);\r
1122\r
1123 if (AsciiStrnCmp (Language, LanguageString, Index) == 0) { \r
1124 return EFI_ALREADY_STARTED;\r
1125 }\r
1126 LanguageString += Index;\r
1127 for (; *LanguageString != 0 && *LanguageString == ';'; LanguageString++);\r
1128 }\r
1129 OldUnicodeStringTable++;\r
1130 NumberOfEntries++;\r
1131 }\r
1132 }\r
1133\r
1134 //\r
1135 // Allocate space for a new Unicode String Table. It must hold the current number of\r
1136 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table\r
1137 // marker\r
1138 //\r
1139 NewUnicodeStringTable = AllocatePool ((NumberOfEntries + 2) * sizeof (EFI_UNICODE_STRING_TABLE));\r
1140 if (NewUnicodeStringTable == NULL) {\r
1141 return EFI_OUT_OF_RESOURCES;\r
1142 }\r
1143\r
1144 //\r
1145 // If the current Unicode String Table contains any entries, then copy them to the\r
1146 // newly allocated Unicode String Table.\r
1147 //\r
1148 if (*UnicodeStringTable != NULL) {\r
1149 CopyMem (\r
1150 NewUnicodeStringTable,\r
1151 *UnicodeStringTable,\r
1152 NumberOfEntries * sizeof (EFI_UNICODE_STRING_TABLE)\r
1153 );\r
1154 }\r
1155\r
1156 //\r
1157 // Allocate space for a copy of the Language specifier\r
1158 //\r
1159 NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (AsciiStrSize(Language), Language);\r
1160 if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) {\r
1161 gBS->FreePool (NewUnicodeStringTable);\r
1162 return EFI_OUT_OF_RESOURCES;\r
1163 }\r
1164\r
1165 //\r
1166 // Compute the length of the Unicode String\r
1167 //\r
1168 for (UnicodeStringLength = 0; UnicodeString[UnicodeStringLength] != 0; UnicodeStringLength++);\r
1169\r
1170 //\r
1171 // Allocate space for a copy of the Unicode String\r
1172 //\r
1173 NewUnicodeStringTable[NumberOfEntries].UnicodeString = AllocateCopyPool (StrSize (UnicodeString), UnicodeString);\r
1174 if (NewUnicodeStringTable[NumberOfEntries].UnicodeString == NULL) {\r
1175 gBS->FreePool (NewUnicodeStringTable[NumberOfEntries].Language);\r
1176 gBS->FreePool (NewUnicodeStringTable);\r
1177 return EFI_OUT_OF_RESOURCES;\r
1178 }\r
1179\r
1180 //\r
1181 // Mark the end of the Unicode String Table\r
1182 //\r
1183 NewUnicodeStringTable[NumberOfEntries + 1].Language = NULL;\r
1184 NewUnicodeStringTable[NumberOfEntries + 1].UnicodeString = NULL;\r
1185\r
1186 //\r
1187 // Free the old Unicode String Table\r
1188 //\r
1189 if (*UnicodeStringTable != NULL) {\r
1190 gBS->FreePool (*UnicodeStringTable);\r
1191 }\r
1192\r
1193 //\r
1194 // Point UnicodeStringTable at the newly allocated Unicode String Table\r
1195 //\r
1196 *UnicodeStringTable = NewUnicodeStringTable;\r
1197\r
1198 return EFI_SUCCESS;\r
1199}\r
1200\r
1201/**\r
1202 This function frees the table of Unicode strings in UnicodeStringTable.\r
1203\r
1204 If UnicodeStringTable is NULL, then EFI_SUCCESS is returned.\r
1205 Otherwise, each language code, and each Unicode string in the Unicode string \r
1206 table are freed, and EFI_SUCCESS is returned.\r
1207\r
1208 @param UnicodeStringTable A pointer to the table of Unicode strings.\r
1209\r
1210 @retval EFI_SUCCESS The Unicode string table was freed.\r
1211\r
1212**/\r
1213EFI_STATUS\r
1214EFIAPI\r
1215FreeUnicodeStringTable (\r
1216 IN EFI_UNICODE_STRING_TABLE *UnicodeStringTable\r
1217 )\r
1218{\r
1219 UINTN Index;\r
1220\r
1221 //\r
1222 // If the Unicode String Table is NULL, then it is already freed\r
1223 //\r
1224 if (UnicodeStringTable == NULL) {\r
1225 return EFI_SUCCESS;\r
1226 }\r
1227\r
1228 //\r
1229 // Loop through the Unicode String Table until we reach the end of table marker\r
1230 //\r
1231 for (Index = 0; UnicodeStringTable[Index].Language != NULL; Index++) {\r
1232\r
1233 //\r
1234 // Free the Language string from the Unicode String Table\r
1235 //\r
1236 gBS->FreePool (UnicodeStringTable[Index].Language);\r
1237\r
1238 //\r
1239 // Free the Unicode String from the Unicode String Table\r
1240 //\r
1241 if (UnicodeStringTable[Index].UnicodeString != NULL) {\r
1242 gBS->FreePool (UnicodeStringTable[Index].UnicodeString);\r
1243 }\r
1244 }\r
1245\r
1246 //\r
1247 // Free the Unicode String Table itself\r
1248 //\r
1249 gBS->FreePool (UnicodeStringTable);\r
1250\r
1251 return EFI_SUCCESS;\r
1252}\r
1253\r
1254/**\r
1255 Returns a pointer to an allocated buffer that contains the contents of a \r
1256 variable retrieved through the UEFI Runtime Service GetVariable(). The \r
1257 returned buffer is allocated using AllocatePool(). The caller is responsible\r
1258 for freeing this buffer with FreePool().\r
1259\r
1260 If Name is NULL, then ASSERT().\r
1261 If Guid is NULL, then ASSERT().\r
1262\r
1263 @param[in] Name Pointer to a Null-terminated Unicode string.\r
1264 @param[in] Guid Pointer to an EFI_GUID structure\r
1265\r
1266 @retval NULL The variable could not be retrieved.\r
1267 @retval NULL There are not enough resources available for the variable contents.\r
1268 @retval Other A pointer to allocated buffer containing the variable contents.\r
1269\r
1270**/\r
1271VOID *\r
1272EFIAPI\r
1273GetVariable (\r
1274 IN CONST CHAR16 *Name,\r
1275 IN CONST EFI_GUID *Guid\r
1276 )\r
1277{\r
1278 EFI_STATUS Status;\r
1279 UINTN Size;\r
1280 VOID *Value;\r
1281\r
1282 ASSERT (Name != NULL);\r
1283 ASSERT (Guid != NULL);\r
1284\r
1285 //\r
1286 // Try to get the variable size.\r
1287 //\r
1288 Value = NULL;\r
1289 Size = 0;\r
1290 Status = gRT->GetVariable ((CHAR16 *) Name, (EFI_GUID *) Guid, NULL, &Size, Value);\r
1291 if (Status != EFI_BUFFER_TOO_SMALL) {\r
1292 return NULL;\r
1293 }\r
1294\r
1295 //\r
1296 // Allocate buffer to get the variable.\r
1297 //\r
1298 Value = AllocatePool (Size);\r
1299 if (Value == NULL) {\r
1300 return NULL;\r
1301 }\r
1302\r
1303 //\r
1304 // Get the variable data.\r
1305 //\r
1306 Status = gRT->GetVariable ((CHAR16 *) Name, (EFI_GUID *) Guid, NULL, &Size, Value);\r
1307 if (EFI_ERROR (Status)) {\r
1308 FreePool(Value);\r
1309 return NULL;\r
1310 }\r
1311\r
1312 return Value;\r
1313}\r
1314\r
1315\r
1316/**\r
1317 Returns a pointer to an allocated buffer that contains the contents of a \r
1318 variable retrieved through the UEFI Runtime Service GetVariable(). This \r
1319 function always uses the EFI_GLOBAL_VARIABLE GUID to retrieve variables.\r
1320 The returned buffer is allocated using AllocatePool(). The caller is \r
1321 responsible for freeing this buffer with FreePool().\r
1322\r
1323 If Name is NULL, then ASSERT().\r
1324\r
1325 @param[in] Name Pointer to a Null-terminated Unicode string.\r
1326\r
1327 @retval NULL The variable could not be retrieved.\r
1328 @retval NULL There are not enough resources available for the variable contents.\r
1329 @retval Other A pointer to allocated buffer containing the variable contents.\r
1330\r
1331**/\r
1332VOID *\r
1333EFIAPI\r
1334GetEfiGlobalVariable (\r
1335 IN CONST CHAR16 *Name\r
1336 )\r
1337{\r
1338 return GetVariable (Name, &gEfiGlobalVariableGuid);\r
1339}\r
1340\r
1341\r
1342/**\r
1343 Returns a pointer to an allocated buffer that contains the best matching language \r
1344 from a set of supported languages. \r
1345 \r
1346 This function supports both ISO 639-2 and RFC 4646 language codes, but language \r
1347 code types may not be mixed in a single call to this function. The language \r
1348 code returned is allocated using AllocatePool(). The caller is responsible for \r
1349 freeing the allocated buffer using FreePool(). This function supports a variable\r
1350 argument list that allows the caller to pass in a prioritized list of language \r
1351 codes to test against all the language codes in SupportedLanguages. \r
1352\r
1353 If SupportedLanguages is NULL, then ASSERT().\r
1354\r
1355 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that\r
1356 contains a set of language codes in the format \r
1357 specified by Iso639Language.\r
1358 @param[in] Iso639Language If TRUE, then all language codes are assumed to be\r
1359 in ISO 639-2 format. If FALSE, then all language\r
1360 codes are assumed to be in RFC 4646 language format\r
1361 @param[in] ... A variable argument list that contains pointers to \r
1362 Null-terminated ASCII strings that contain one or more\r
1363 language codes in the format specified by Iso639Language.\r
1364 The first language code from each of these language\r
1365 code lists is used to determine if it is an exact or\r
1366 close match to any of the language codes in \r
1367 SupportedLanguages. Close matches only apply to RFC 4646\r
1368 language codes, and the matching algorithm from RFC 4647\r
1369 is used to determine if a close match is present. If \r
1370 an exact or close match is found, then the matching\r
1371 language code from SupportedLanguages is returned. If\r
1372 no matches are found, then the next variable argument\r
1373 parameter is evaluated. The variable argument list \r
1374 is terminated by a NULL.\r
1375\r
1376 @retval NULL The best matching language could not be found in SupportedLanguages.\r
1377 @retval NULL There are not enough resources available to return the best matching \r
1378 language.\r
1379 @retval Other A pointer to a Null-terminated ASCII string that is the best matching \r
1380 language in SupportedLanguages.\r
1381\r
1382**/\r
1383CHAR8 *\r
1384EFIAPI\r
1385GetBestLanguage (\r
1386 IN CONST CHAR8 *SupportedLanguages, \r
1387 IN BOOLEAN Iso639Language,\r
1388 ...\r
1389 )\r
1390{\r
1391 VA_LIST Args;\r
1392 CHAR8 *Language;\r
1393 UINTN CompareLength;\r
1394 UINTN LanguageLength;\r
1395 CONST CHAR8 *Supported;\r
1396 CHAR8 *BestLanguage;\r
1397\r
1398 ASSERT (SupportedLanguages != NULL);\r
1399\r
1400 VA_START (Args, Iso639Language);\r
1401 while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {\r
1402 //\r
1403 // Default to ISO 639-2 mode\r
1404 //\r
1405 CompareLength = 3;\r
1406 LanguageLength = MIN (3, AsciiStrLen (Language));\r
1407\r
1408 //\r
1409 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language\r
1410 //\r
1411 if (!Iso639Language) {\r
1412 for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);\r
1413 }\r
1414\r
1415 //\r
1416 // Trim back the length of Language used until it is empty\r
1417 //\r
1418 while (LanguageLength > 0) {\r
1419 //\r
1420 // Loop through all language codes in SupportedLanguages\r
1421 //\r
1422 for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {\r
1423 //\r
1424 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages\r
1425 //\r
1426 if (!Iso639Language) {\r
1427 //\r
1428 // Skip ';' characters in Supported\r
1429 //\r
1430 for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
1431 //\r
1432 // Determine the length of the next language code in Supported\r
1433 //\r
1434 for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);\r
1435 //\r
1436 // If Language is longer than the Supported, then skip to the next language\r
1437 //\r
1438 if (LanguageLength > CompareLength) {\r
1439 continue;\r
1440 }\r
1441 }\r
1442 //\r
1443 // See if the first LanguageLength characters in Supported match Language\r
1444 //\r
1445 if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {\r
1446 VA_END (Args);\r
1447 //\r
1448 // Allocate, copy, and return the best matching language code from SupportedLanguages\r
1449 //\r
1450 BestLanguage = AllocateZeroPool (CompareLength + 1);\r
1451 if (BestLanguage == NULL) {\r
1452 return NULL;\r
1453 }\r
1454 return CopyMem (BestLanguage, Supported, CompareLength);\r
1455 }\r
1456 }\r
1457\r
1458 if (Iso639Language) {\r
1459 //\r
1460 // If ISO 639 mode, then each language can only be tested once\r
1461 //\r
1462 LanguageLength = 0;\r
1463 } else {\r
1464 //\r
1465 // If RFC 4646 mode, then trim Language from the right to the next '-' character \r
1466 //\r
1467 for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);\r
1468 }\r
1469 }\r
1470 }\r
1471 VA_END (Args);\r
1472\r
1473 //\r
1474 // No matches were found \r
1475 //\r
1476 return NULL;\r
1477}\r