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