]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/UefiLib/UefiLib.c
MdePkg UefiLib: Use comparison logic to check UINTN parameter
[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
40070a18 8 Copyright (c) 2006 - 2018, 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
772fb7cb
LE
307/**\r
308 Signals an event group by placing a new event in the group temporarily and\r
309 signaling it.\r
310\r
311 @param[in] EventGroup Supplies the unique identifier of the event\r
312 group to signal.\r
313\r
314 @retval EFI_SUCCESS The event group was signaled successfully.\r
315 @retval EFI_INVALID_PARAMETER EventGroup is NULL.\r
316 @return Error codes that report problems about event\r
317 creation or signaling.\r
318**/\r
319EFI_STATUS\r
320EFIAPI\r
321EfiEventGroupSignal (\r
322 IN CONST EFI_GUID *EventGroup\r
323 )\r
324{\r
325 EFI_STATUS Status;\r
326 EFI_EVENT Event;\r
327\r
328 if (EventGroup == NULL) {\r
329 return EFI_INVALID_PARAMETER;\r
330 }\r
331\r
332 Status = gBS->CreateEventEx (\r
333 EVT_NOTIFY_SIGNAL,\r
334 TPL_CALLBACK,\r
1b197063 335 EfiEventEmptyFunction,\r
772fb7cb
LE
336 NULL,\r
337 EventGroup,\r
338 &Event\r
339 );\r
340 if (EFI_ERROR (Status)) {\r
341 return Status;\r
342 }\r
343\r
344 Status = gBS->SignalEvent (Event);\r
345 gBS->CloseEvent (Event);\r
346\r
347 return Status;\r
348}\r
349\r
1b197063
SZ
350/**\r
351 An empty function that can be used as NotifyFunction parameter of\r
352 CreateEvent() or CreateEventEx().\r
353\r
354 @param Event Event whose notification function is being invoked.\r
355 @param Context The pointer to the notification function's context,\r
356 which is implementation-dependent.\r
357\r
358**/\r
359VOID\r
360EFIAPI\r
361EfiEventEmptyFunction (\r
362 IN EFI_EVENT Event,\r
363 IN VOID *Context\r
364 )\r
365{\r
366}\r
367\r
cf8ae2f6 368/** \r
e386b444 369 Returns the current TPL.\r
370\r
cf8ae2f6 371 This function returns the current TPL. There is no EFI service to directly \r
372 retrieve the current TPL. Instead, the RaiseTPL() function is used to raise \r
373 the TPL to TPL_HIGH_LEVEL. This will return the current TPL. The TPL level \r
374 can then immediately be restored back to the current TPL level with a call \r
e386b444 375 to RestoreTPL().\r
376\r
cf8ae2f6 377 @return The current TPL.\r
e386b444 378\r
379**/\r
380EFI_TPL\r
381EFIAPI\r
382EfiGetCurrentTpl (\r
383 VOID\r
384 )\r
385{\r
386 EFI_TPL Tpl;\r
387\r
388 Tpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r
389 gBS->RestoreTPL (Tpl);\r
390\r
391 return Tpl;\r
392}\r
393\r
394\r
395/**\r
1d37ab9f 396 Initializes a basic mutual exclusion lock.\r
397\r
398 This function initializes a basic mutual exclusion lock to the released state \r
399 and returns the lock. Each lock provides mutual exclusion access at its task \r
e386b444 400 priority level. Since there is no preemption or multiprocessor support in EFI,\r
401 acquiring the lock only consists of raising to the locks TPL.\r
9edc73ad 402 If Lock is NULL, then ASSERT().\r
403 If Priority is not a valid TPL value, then ASSERT().\r
e386b444 404\r
405 @param Lock A pointer to the lock data structure to initialize.\r
58380e9c 406 @param Priority EFI TPL is associated with the lock.\r
e386b444 407\r
408 @return The lock.\r
409\r
410**/\r
411EFI_LOCK *\r
412EFIAPI\r
413EfiInitializeLock (\r
414 IN OUT EFI_LOCK *Lock,\r
415 IN EFI_TPL Priority\r
416 )\r
417{\r
418 ASSERT (Lock != NULL);\r
419 ASSERT (Priority <= TPL_HIGH_LEVEL);\r
420\r
421 Lock->Tpl = Priority;\r
422 Lock->OwnerTpl = TPL_APPLICATION;\r
423 Lock->Lock = EfiLockReleased ;\r
424 return Lock;\r
425}\r
426\r
427/**\r
1d37ab9f 428 Acquires ownership of a lock.\r
429\r
430 This function raises the system's current task priority level to the task \r
431 priority level of the mutual exclusion lock. Then, it places the lock in the \r
e386b444 432 acquired state.\r
9edc73ad 433 If Lock is NULL, then ASSERT().\r
434 If Lock is not initialized, then ASSERT().\r
435 If Lock is already in the acquired state, then ASSERT().\r
e386b444 436\r
1d37ab9f 437 @param Lock A pointer to the lock to acquire.\r
e386b444 438\r
439**/\r
440VOID\r
441EFIAPI\r
442EfiAcquireLock (\r
443 IN EFI_LOCK *Lock\r
444 )\r
445{\r
446 ASSERT (Lock != NULL);\r
447 ASSERT (Lock->Lock == EfiLockReleased);\r
448\r
449 Lock->OwnerTpl = gBS->RaiseTPL (Lock->Tpl);\r
450 Lock->Lock = EfiLockAcquired;\r
451}\r
452\r
453/**\r
cf8ae2f6 454 Acquires ownership of a lock.\r
1d37ab9f 455\r
cf8ae2f6 456 This function raises the system's current task priority level to the task priority\r
457 level of the mutual exclusion lock. Then, it attempts to place the lock in the acquired state.\r
458 If the lock is already in the acquired state, then EFI_ACCESS_DENIED is returned.\r
459 Otherwise, EFI_SUCCESS is returned.\r
1d37ab9f 460 If Lock is NULL, then ASSERT().\r
461 If Lock is not initialized, then ASSERT().\r
e386b444 462\r
463 @param Lock A pointer to the lock to acquire.\r
464\r
465 @retval EFI_SUCCESS The lock was acquired.\r
466 @retval EFI_ACCESS_DENIED The lock could not be acquired because it is already owned.\r
467\r
468**/\r
469EFI_STATUS\r
470EFIAPI\r
471EfiAcquireLockOrFail (\r
472 IN EFI_LOCK *Lock\r
473 )\r
474{\r
475\r
476 ASSERT (Lock != NULL);\r
477 ASSERT (Lock->Lock != EfiLockUninitialized);\r
478\r
479 if (Lock->Lock == EfiLockAcquired) {\r
480 //\r
481 // Lock is already owned, so bail out\r
482 //\r
483 return EFI_ACCESS_DENIED;\r
484 }\r
485\r
486 Lock->OwnerTpl = gBS->RaiseTPL (Lock->Tpl);\r
487\r
488 Lock->Lock = EfiLockAcquired;\r
489\r
490 return EFI_SUCCESS;\r
491}\r
492\r
493/**\r
1d37ab9f 494 Releases ownership of a lock.\r
495\r
496 This function transitions a mutual exclusion lock from the acquired state to \r
497 the released state, and restores the system's task priority level to its \r
e386b444 498 previous level.\r
1d37ab9f 499 If Lock is NULL, then ASSERT().\r
500 If Lock is not initialized, then ASSERT().\r
501 If Lock is already in the released state, then ASSERT().\r
e386b444 502\r
503 @param Lock A pointer to the lock to release.\r
504\r
505**/\r
506VOID\r
507EFIAPI\r
508EfiReleaseLock (\r
509 IN EFI_LOCK *Lock\r
510 )\r
511{\r
512 EFI_TPL Tpl;\r
513\r
514 ASSERT (Lock != NULL);\r
515 ASSERT (Lock->Lock == EfiLockAcquired);\r
516\r
517 Tpl = Lock->OwnerTpl;\r
518\r
519 Lock->Lock = EfiLockReleased;\r
520\r
521 gBS->RestoreTPL (Tpl);\r
522}\r
523\r
524/**\r
525 Tests whether a controller handle is being managed by a specific driver.\r
526\r
527 This function tests whether the driver specified by DriverBindingHandle is\r
528 currently managing the controller specified by ControllerHandle. This test\r
529 is performed by evaluating if the the protocol specified by ProtocolGuid is\r
530 present on ControllerHandle and is was opened by DriverBindingHandle with an\r
cf8ae2f6 531 attribute of EFI_OPEN_PROTOCOL_BY_DRIVER. \r
e386b444 532 If ProtocolGuid is NULL, then ASSERT().\r
533\r
534 @param ControllerHandle A handle for a controller to test.\r
535 @param DriverBindingHandle Specifies the driver binding handle for the\r
536 driver.\r
537 @param ProtocolGuid Specifies the protocol that the driver specified\r
538 by DriverBindingHandle opens in its Start()\r
539 function.\r
540\r
541 @retval EFI_SUCCESS ControllerHandle is managed by the driver\r
28d3e14f 542 specified by DriverBindingHandle.\r
e386b444 543 @retval EFI_UNSUPPORTED ControllerHandle is not managed by the driver\r
28d3e14f 544 specified by DriverBindingHandle.\r
e386b444 545\r
546**/\r
547EFI_STATUS\r
548EFIAPI\r
549EfiTestManagedDevice (\r
550 IN CONST EFI_HANDLE ControllerHandle,\r
551 IN CONST EFI_HANDLE DriverBindingHandle,\r
552 IN CONST EFI_GUID *ProtocolGuid\r
553 )\r
554{\r
555 EFI_STATUS Status;\r
556 VOID *ManagedInterface;\r
557\r
558 ASSERT (ProtocolGuid != NULL);\r
559\r
560 Status = gBS->OpenProtocol (\r
561 ControllerHandle,\r
562 (EFI_GUID *) ProtocolGuid,\r
563 &ManagedInterface,\r
564 DriverBindingHandle,\r
565 ControllerHandle,\r
566 EFI_OPEN_PROTOCOL_BY_DRIVER\r
567 );\r
568 if (!EFI_ERROR (Status)) {\r
569 gBS->CloseProtocol (\r
570 ControllerHandle,\r
571 (EFI_GUID *) ProtocolGuid,\r
572 DriverBindingHandle,\r
573 ControllerHandle\r
574 );\r
575 return EFI_UNSUPPORTED;\r
576 }\r
577\r
578 if (Status != EFI_ALREADY_STARTED) {\r
579 return EFI_UNSUPPORTED;\r
580 }\r
581\r
582 return EFI_SUCCESS;\r
583}\r
584\r
585/**\r
586 Tests whether a child handle is a child device of the controller.\r
587\r
588 This function tests whether ChildHandle is one of the children of\r
589 ControllerHandle. This test is performed by checking to see if the protocol\r
590 specified by ProtocolGuid is present on ControllerHandle and opened by\r
591 ChildHandle with an attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.\r
592 If ProtocolGuid is NULL, then ASSERT().\r
593\r
cf8ae2f6 594 @param ControllerHandle A handle for a (parent) controller to test. \r
e386b444 595 @param ChildHandle A child handle to test.\r
42eedea9 596 @param ProtocolGuid Supplies the protocol that the child controller\r
cf8ae2f6 597 opens on its parent controller. \r
e386b444 598\r
599 @retval EFI_SUCCESS ChildHandle is a child of the ControllerHandle.\r
600 @retval EFI_UNSUPPORTED ChildHandle is not a child of the\r
601 ControllerHandle.\r
602\r
603**/\r
604EFI_STATUS\r
605EFIAPI\r
606EfiTestChildHandle (\r
607 IN CONST EFI_HANDLE ControllerHandle,\r
608 IN CONST EFI_HANDLE ChildHandle,\r
609 IN CONST EFI_GUID *ProtocolGuid\r
610 )\r
611{\r
612 EFI_STATUS Status;\r
613 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfoBuffer;\r
614 UINTN EntryCount;\r
615 UINTN Index;\r
616\r
617 ASSERT (ProtocolGuid != NULL);\r
618\r
619 //\r
620 // Retrieve the list of agents that are consuming the specific protocol\r
621 // on ControllerHandle.\r
622 //\r
623 Status = gBS->OpenProtocolInformation (\r
624 ControllerHandle,\r
625 (EFI_GUID *) ProtocolGuid,\r
626 &OpenInfoBuffer,\r
627 &EntryCount\r
628 );\r
629 if (EFI_ERROR (Status)) {\r
630 return EFI_UNSUPPORTED;\r
631 }\r
632\r
633 //\r
634 // Inspect if ChildHandle is one of the agents.\r
635 //\r
636 Status = EFI_UNSUPPORTED;\r
637 for (Index = 0; Index < EntryCount; Index++) {\r
638 if ((OpenInfoBuffer[Index].ControllerHandle == ChildHandle) &&\r
639 (OpenInfoBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {\r
640 Status = EFI_SUCCESS;\r
641 break;\r
642 }\r
643 }\r
644\r
645 FreePool (OpenInfoBuffer);\r
646 return Status;\r
647}\r
648\r
649/**\r
dd51a993 650 This function looks up a Unicode string in UnicodeStringTable.\r
dd51a993 651\r
cf8ae2f6 652 If Language is a member of SupportedLanguages and a Unicode string is found in\r
1d37ab9f 653 UnicodeStringTable that matches the language code specified by Language, then it\r
654 is returned in UnicodeString.\r
655\r
656 @param Language A pointer to the ISO 639-2 language code for the \r
657 Unicode string to look up and return.\r
658 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes \r
659 that the Unicode string table supports. Language \r
660 must be a member of this set.\r
661 @param UnicodeStringTable A pointer to the table of Unicode strings.\r
662 @param UnicodeString A pointer to the Unicode string from UnicodeStringTable\r
663 that matches the language specified by Language.\r
664\r
665 @retval EFI_SUCCESS The Unicode string that matches the language \r
666 specified by Language was found\r
28d3e14f 667 in the table of Unicode strings UnicodeStringTable, \r
1d37ab9f 668 and it was returned in UnicodeString.\r
669 @retval EFI_INVALID_PARAMETER Language is NULL.\r
670 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.\r
671 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.\r
672 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.\r
673 @retval EFI_UNSUPPORTED The language specified by Language is not a \r
674 member of SupportedLanguages.\r
675 @retval EFI_UNSUPPORTED The language specified by Language is not \r
676 supported by UnicodeStringTable.\r
e386b444 677\r
678**/\r
679EFI_STATUS\r
680EFIAPI\r
681LookupUnicodeString (\r
682 IN CONST CHAR8 *Language,\r
683 IN CONST CHAR8 *SupportedLanguages,\r
684 IN CONST EFI_UNICODE_STRING_TABLE *UnicodeStringTable,\r
685 OUT CHAR16 **UnicodeString\r
686 )\r
687{\r
688 //\r
689 // Make sure the parameters are valid\r
690 //\r
691 if (Language == NULL || UnicodeString == NULL) {\r
692 return EFI_INVALID_PARAMETER;\r
693 }\r
694\r
695 //\r
696 // If there are no supported languages, or the Unicode String Table is empty, then the\r
697 // Unicode String specified by Language is not supported by this Unicode String Table\r
698 //\r
699 if (SupportedLanguages == NULL || UnicodeStringTable == NULL) {\r
700 return EFI_UNSUPPORTED;\r
701 }\r
702\r
703 //\r
704 // Make sure Language is in the set of Supported Languages\r
705 //\r
706 while (*SupportedLanguages != 0) {\r
707 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
708\r
709 //\r
710 // Search the Unicode String Table for the matching Language specifier\r
711 //\r
712 while (UnicodeStringTable->Language != NULL) {\r
713 if (CompareIso639LanguageCode (Language, UnicodeStringTable->Language)) {\r
714\r
715 //\r
716 // A matching string was found, so return it\r
717 //\r
718 *UnicodeString = UnicodeStringTable->UnicodeString;\r
719 return EFI_SUCCESS;\r
720 }\r
721\r
722 UnicodeStringTable++;\r
723 }\r
724\r
725 return EFI_UNSUPPORTED;\r
726 }\r
727\r
728 SupportedLanguages += 3;\r
729 }\r
730\r
731 return EFI_UNSUPPORTED;\r
732}\r
733\r
dd51a993 734\r
735\r
e386b444 736/**\r
dd51a993 737 This function looks up a Unicode string in UnicodeStringTable.\r
cf8ae2f6 738\r
739 If Language is a member of SupportedLanguages and a Unicode string is found in\r
740 UnicodeStringTable that matches the language code specified by Language, then\r
741 it is returned in UnicodeString.\r
742\r
743 @param Language A pointer to an ASCII string containing the ISO 639-2 or the\r
744 RFC 4646 language code for the Unicode string to look up and\r
745 return. If Iso639Language is TRUE, then this ASCII string is\r
746 not assumed to be Null-terminated, and only the first three\r
28d3e14f 747 characters are used. If Iso639Language is FALSE, then this ASCII\r
cf8ae2f6 748 string must be Null-terminated. \r
749 @param SupportedLanguages A pointer to a Null-terminated ASCII string that contains a\r
750 set of ISO 639-2 or RFC 4646 language codes that the Unicode\r
751 string table supports. Language must be a member of this set.\r
752 If Iso639Language is TRUE, then this string contains one or more\r
753 ISO 639-2 language codes with no separator characters. If Iso639Language\r
754 is FALSE, then is string contains one or more RFC 4646 language\r
755 codes separated by ';'.\r
756 @param UnicodeStringTable A pointer to the table of Unicode strings. Type EFI_UNICODE_STRING_TABLE\r
757 is defined in "Related Definitions".\r
758 @param UnicodeString A pointer to the Null-terminated Unicode string from UnicodeStringTable\r
759 that matches the language specified by Language.\r
760 @param Iso639Language Specifies the supported language code format. If it is TRUE, then\r
761 Language and SupportedLanguages follow ISO 639-2 language code format.\r
762 Otherwise, they follow RFC 4646 language code format.\r
763\r
764\r
765 @retval EFI_SUCCESS The Unicode string that matches the language specified by Language\r
766 was found in the table of Unicode strings UnicodeStringTable, and\r
767 it was returned in UnicodeString.\r
1d37ab9f 768 @retval EFI_INVALID_PARAMETER Language is NULL. \r
769 @retval EFI_INVALID_PARAMETER UnicodeString is NULL. \r
770 @retval EFI_UNSUPPORTED SupportedLanguages is NULL. \r
cf8ae2f6 771 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL. \r
772 @retval EFI_UNSUPPORTED The language specified by Language is not a member of SupportedLanguages. \r
773 @retval EFI_UNSUPPORTED The language specified by Language is not supported by UnicodeStringTable.\r
dd51a993 774\r
775**/\r
776EFI_STATUS\r
777EFIAPI\r
778LookupUnicodeString2 (\r
779 IN CONST CHAR8 *Language,\r
780 IN CONST CHAR8 *SupportedLanguages,\r
781 IN CONST EFI_UNICODE_STRING_TABLE *UnicodeStringTable,\r
782 OUT CHAR16 **UnicodeString,\r
783 IN BOOLEAN Iso639Language\r
784 )\r
785{\r
786 BOOLEAN Found;\r
787 UINTN Index;\r
788 CHAR8 *LanguageString;\r
789\r
790 //\r
791 // Make sure the parameters are valid\r
792 //\r
793 if (Language == NULL || UnicodeString == NULL) {\r
794 return EFI_INVALID_PARAMETER;\r
795 }\r
796\r
797 //\r
798 // If there are no supported languages, or the Unicode String Table is empty, then the\r
799 // Unicode String specified by Language is not supported by this Unicode String Table\r
800 //\r
801 if (SupportedLanguages == NULL || UnicodeStringTable == NULL) {\r
802 return EFI_UNSUPPORTED;\r
803 }\r
804\r
805 //\r
806 // Make sure Language is in the set of Supported Languages\r
807 //\r
808 Found = FALSE;\r
809 while (*SupportedLanguages != 0) {\r
810 if (Iso639Language) {\r
811 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
812 Found = TRUE;\r
813 break;\r
814 }\r
815 SupportedLanguages += 3;\r
816 } else {\r
817 for (Index = 0; SupportedLanguages[Index] != 0 && SupportedLanguages[Index] != ';'; Index++);\r
634aa59d 818 if ((AsciiStrnCmp(SupportedLanguages, Language, Index) == 0) && (Language[Index] == 0)) {\r
dd51a993 819 Found = TRUE;\r
820 break;\r
821 }\r
822 SupportedLanguages += Index;\r
823 for (; *SupportedLanguages != 0 && *SupportedLanguages == ';'; SupportedLanguages++);\r
824 }\r
825 }\r
826\r
827 //\r
828 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED\r
829 //\r
830 if (!Found) {\r
831 return EFI_UNSUPPORTED;\r
832 }\r
833\r
834 //\r
835 // Search the Unicode String Table for the matching Language specifier\r
836 //\r
837 while (UnicodeStringTable->Language != NULL) {\r
838 LanguageString = UnicodeStringTable->Language;\r
839 while (0 != *LanguageString) {\r
840 for (Index = 0 ;LanguageString[Index] != 0 && LanguageString[Index] != ';'; Index++);\r
841 if (AsciiStrnCmp(LanguageString, Language, Index) == 0) {\r
842 *UnicodeString = UnicodeStringTable->UnicodeString;\r
843 return EFI_SUCCESS;\r
844 }\r
845 LanguageString += Index;\r
846 for (Index = 0 ;LanguageString[Index] != 0 && LanguageString[Index] == ';'; Index++);\r
847 }\r
848 UnicodeStringTable++;\r
849 }\r
850\r
851 return EFI_UNSUPPORTED;\r
852}\r
853\r
854\r
855/**\r
e386b444 856 This function adds a Unicode string to UnicodeStringTable.\r
1d37ab9f 857\r
858 If Language is a member of SupportedLanguages then UnicodeString is added to \r
859 UnicodeStringTable. New buffers are allocated for both Language and \r
860 UnicodeString. The contents of Language and UnicodeString are copied into \r
861 these new buffers. These buffers are automatically freed when \r
e386b444 862 FreeUnicodeStringTable() is called.\r
863\r
1d37ab9f 864 @param Language A pointer to the ISO 639-2 language code for the Unicode \r
e386b444 865 string to add.\r
1d37ab9f 866 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes\r
867 that the Unicode string table supports.\r
868 Language must be a member of this set.\r
869 @param UnicodeStringTable A pointer to the table of Unicode strings.\r
870 @param UnicodeString A pointer to the Unicode string to add.\r
871\r
872 @retval EFI_SUCCESS The Unicode string that matches the language \r
873 specified by Language was found in the table of \r
874 Unicode strings UnicodeStringTable, and it was \r
e386b444 875 returned in UnicodeString.\r
876 @retval EFI_INVALID_PARAMETER Language is NULL.\r
877 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.\r
878 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.\r
879 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.\r
1d37ab9f 880 @retval EFI_ALREADY_STARTED A Unicode string with language Language is \r
881 already present in UnicodeStringTable.\r
882 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another \r
883 Unicode string to UnicodeStringTable.\r
884 @retval EFI_UNSUPPORTED The language specified by Language is not a \r
885 member of SupportedLanguages.\r
e386b444 886\r
887**/\r
888EFI_STATUS\r
889EFIAPI\r
890AddUnicodeString (\r
5b9626e8
MH
891 IN CONST CHAR8 *Language,\r
892 IN CONST CHAR8 *SupportedLanguages,\r
893 IN OUT EFI_UNICODE_STRING_TABLE **UnicodeStringTable,\r
894 IN CONST CHAR16 *UnicodeString\r
e386b444 895 )\r
896{\r
897 UINTN NumberOfEntries;\r
898 EFI_UNICODE_STRING_TABLE *OldUnicodeStringTable;\r
899 EFI_UNICODE_STRING_TABLE *NewUnicodeStringTable;\r
900 UINTN UnicodeStringLength;\r
901\r
902 //\r
903 // Make sure the parameter are valid\r
904 //\r
905 if (Language == NULL || UnicodeString == NULL || UnicodeStringTable == NULL) {\r
906 return EFI_INVALID_PARAMETER;\r
907 }\r
908\r
909 //\r
910 // If there are no supported languages, then a Unicode String can not be added\r
911 //\r
912 if (SupportedLanguages == NULL) {\r
913 return EFI_UNSUPPORTED;\r
914 }\r
915\r
916 //\r
917 // If the Unicode String is empty, then a Unicode String can not be added\r
918 //\r
919 if (UnicodeString[0] == 0) {\r
920 return EFI_INVALID_PARAMETER;\r
921 }\r
922\r
923 //\r
924 // Make sure Language is a member of SupportedLanguages\r
925 //\r
926 while (*SupportedLanguages != 0) {\r
927 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
928\r
929 //\r
930 // Determine the size of the Unicode String Table by looking for a NULL Language entry\r
931 //\r
932 NumberOfEntries = 0;\r
933 if (*UnicodeStringTable != NULL) {\r
934 OldUnicodeStringTable = *UnicodeStringTable;\r
935 while (OldUnicodeStringTable->Language != NULL) {\r
936 if (CompareIso639LanguageCode (Language, OldUnicodeStringTable->Language)) {\r
937 return EFI_ALREADY_STARTED;\r
938 }\r
939\r
940 OldUnicodeStringTable++;\r
941 NumberOfEntries++;\r
942 }\r
943 }\r
944\r
945 //\r
946 // Allocate space for a new Unicode String Table. It must hold the current number of\r
947 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table\r
948 // marker\r
949 //\r
950 NewUnicodeStringTable = AllocatePool ((NumberOfEntries + 2) * sizeof (EFI_UNICODE_STRING_TABLE));\r
951 if (NewUnicodeStringTable == NULL) {\r
952 return EFI_OUT_OF_RESOURCES;\r
953 }\r
954\r
955 //\r
956 // If the current Unicode String Table contains any entries, then copy them to the\r
957 // newly allocated Unicode String Table.\r
958 //\r
959 if (*UnicodeStringTable != NULL) {\r
960 CopyMem (\r
961 NewUnicodeStringTable,\r
962 *UnicodeStringTable,\r
963 NumberOfEntries * sizeof (EFI_UNICODE_STRING_TABLE)\r
964 );\r
965 }\r
966\r
967 //\r
968 // Allocate space for a copy of the Language specifier\r
969 //\r
970 NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (3, Language);\r
971 if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) {\r
6389e32b 972 FreePool (NewUnicodeStringTable);\r
e386b444 973 return EFI_OUT_OF_RESOURCES;\r
974 }\r
975\r
976 //\r
977 // Compute the length of the Unicode String\r
978 //\r
979 for (UnicodeStringLength = 0; UnicodeString[UnicodeStringLength] != 0; UnicodeStringLength++)\r
980 ;\r
981\r
982 //\r
983 // Allocate space for a copy of the Unicode String\r
984 //\r
985 NewUnicodeStringTable[NumberOfEntries].UnicodeString = AllocateCopyPool (\r
986 (UnicodeStringLength + 1) * sizeof (CHAR16),\r
987 UnicodeString\r
988 );\r
989 if (NewUnicodeStringTable[NumberOfEntries].UnicodeString == NULL) {\r
6389e32b
LG
990 FreePool (NewUnicodeStringTable[NumberOfEntries].Language);\r
991 FreePool (NewUnicodeStringTable);\r
e386b444 992 return EFI_OUT_OF_RESOURCES;\r
993 }\r
994\r
995 //\r
996 // Mark the end of the Unicode String Table\r
997 //\r
998 NewUnicodeStringTable[NumberOfEntries + 1].Language = NULL;\r
999 NewUnicodeStringTable[NumberOfEntries + 1].UnicodeString = NULL;\r
1000\r
1001 //\r
1002 // Free the old Unicode String Table\r
1003 //\r
1004 if (*UnicodeStringTable != NULL) {\r
6389e32b 1005 FreePool (*UnicodeStringTable);\r
e386b444 1006 }\r
1007\r
1008 //\r
1009 // Point UnicodeStringTable at the newly allocated Unicode String Table\r
1010 //\r
1011 *UnicodeStringTable = NewUnicodeStringTable;\r
1012\r
1013 return EFI_SUCCESS;\r
1014 }\r
1015\r
1016 SupportedLanguages += 3;\r
1017 }\r
1018\r
1019 return EFI_UNSUPPORTED;\r
1020}\r
1021\r
dd51a993 1022\r
1023/**\r
cf8ae2f6 1024 This function adds the Null-terminated Unicode string specified by UnicodeString\r
1025 to UnicodeStringTable.\r
1026\r
1027 If Language is a member of SupportedLanguages then UnicodeString is added to\r
1028 UnicodeStringTable. New buffers are allocated for both Language and UnicodeString.\r
1029 The contents of Language and UnicodeString are copied into these new buffers.\r
1030 These buffers are automatically freed when EfiLibFreeUnicodeStringTable() is called.\r
1031\r
1032 @param Language A pointer to an ASCII string containing the ISO 639-2 or\r
1033 the RFC 4646 language code for the Unicode string to add.\r
1034 If Iso639Language is TRUE, then this ASCII string is not\r
1035 assumed to be Null-terminated, and only the first three\r
1036 chacters are used. If Iso639Language is FALSE, then this\r
1037 ASCII string must be Null-terminated.\r
1038 @param SupportedLanguages A pointer to a Null-terminated ASCII string that contains\r
1039 a set of ISO 639-2 or RFC 4646 language codes that the Unicode\r
1040 string table supports. Language must be a member of this set.\r
1041 If Iso639Language is TRUE, then this string contains one or more\r
1042 ISO 639-2 language codes with no separator characters.\r
1043 If Iso639Language is FALSE, then is string contains one or more\r
1044 RFC 4646 language codes separated by ';'.\r
1045 @param UnicodeStringTable A pointer to the table of Unicode strings. Type EFI_UNICODE_STRING_TABLE\r
1046 is defined in "Related Definitions".\r
1047 @param UnicodeString A pointer to the Unicode string to add. \r
1048 @param Iso639Language Specifies the supported language code format. If it is TRUE,\r
1049 then Language and SupportedLanguages follow ISO 639-2 language code format.\r
1050 Otherwise, they follow RFC 4646 language code format.\r
1051\r
1052 @retval EFI_SUCCESS The Unicode string that matches the language specified by\r
1053 Language was found in the table of Unicode strings UnicodeStringTable,\r
1054 and it was returned in UnicodeString. \r
1055 @retval EFI_INVALID_PARAMETER Language is NULL. \r
1056 @retval EFI_INVALID_PARAMETER UnicodeString is NULL. \r
1057 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string. \r
1058 @retval EFI_UNSUPPORTED SupportedLanguages is NULL. \r
1059 @retval EFI_ALREADY_STARTED A Unicode string with language Language is already present in\r
1060 UnicodeStringTable. \r
1061 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another Unicode string UnicodeStringTable. \r
1062 @retval EFI_UNSUPPORTED The language specified by Language is not a member of SupportedLanguages.\r
dd51a993 1063\r
1064**/\r
1065EFI_STATUS\r
1066EFIAPI\r
1067AddUnicodeString2 (\r
5b9626e8
MH
1068 IN CONST CHAR8 *Language,\r
1069 IN CONST CHAR8 *SupportedLanguages,\r
1070 IN OUT EFI_UNICODE_STRING_TABLE **UnicodeStringTable,\r
1071 IN CONST CHAR16 *UnicodeString,\r
1072 IN BOOLEAN Iso639Language\r
dd51a993 1073 )\r
1074{\r
1075 UINTN NumberOfEntries;\r
1076 EFI_UNICODE_STRING_TABLE *OldUnicodeStringTable;\r
1077 EFI_UNICODE_STRING_TABLE *NewUnicodeStringTable;\r
1078 UINTN UnicodeStringLength;\r
1079 BOOLEAN Found;\r
1080 UINTN Index;\r
1081 CHAR8 *LanguageString;\r
1082\r
1083 //\r
1084 // Make sure the parameter are valid\r
1085 //\r
1086 if (Language == NULL || UnicodeString == NULL || UnicodeStringTable == NULL) {\r
1087 return EFI_INVALID_PARAMETER;\r
1088 }\r
1089\r
1090 //\r
1091 // If there are no supported languages, then a Unicode String can not be added\r
1092 //\r
1093 if (SupportedLanguages == NULL) {\r
1094 return EFI_UNSUPPORTED;\r
1095 }\r
1096\r
1097 //\r
1098 // If the Unicode String is empty, then a Unicode String can not be added\r
1099 //\r
1100 if (UnicodeString[0] == 0) {\r
1101 return EFI_INVALID_PARAMETER;\r
1102 }\r
1103\r
1104 //\r
1105 // Make sure Language is a member of SupportedLanguages\r
1106 //\r
1107 Found = FALSE;\r
1108 while (*SupportedLanguages != 0) {\r
1109 if (Iso639Language) {\r
1110 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
1111 Found = TRUE;\r
1112 break;\r
1113 }\r
1114 SupportedLanguages += 3;\r
1115 } else {\r
1116 for (Index = 0; SupportedLanguages[Index] != 0 && SupportedLanguages[Index] != ';'; Index++);\r
1117 if (AsciiStrnCmp(SupportedLanguages, Language, Index) == 0) {\r
1118 Found = TRUE;\r
1119 break;\r
1120 }\r
1121 SupportedLanguages += Index;\r
1122 for (; *SupportedLanguages != 0 && *SupportedLanguages == ';'; SupportedLanguages++);\r
1123 }\r
1124 }\r
1125\r
1126 //\r
1127 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED\r
1128 //\r
1129 if (!Found) {\r
1130 return EFI_UNSUPPORTED;\r
1131 }\r
1132\r
1133 //\r
1134 // Determine the size of the Unicode String Table by looking for a NULL Language entry\r
1135 //\r
1136 NumberOfEntries = 0;\r
1137 if (*UnicodeStringTable != NULL) {\r
1138 OldUnicodeStringTable = *UnicodeStringTable;\r
1139 while (OldUnicodeStringTable->Language != NULL) {\r
1140 LanguageString = OldUnicodeStringTable->Language;\r
1141\r
42eedea9 1142 while (*LanguageString != 0) {\r
dd51a993 1143 for (Index = 0; LanguageString[Index] != 0 && LanguageString[Index] != ';'; Index++);\r
1144\r
1145 if (AsciiStrnCmp (Language, LanguageString, Index) == 0) { \r
1146 return EFI_ALREADY_STARTED;\r
1147 }\r
1148 LanguageString += Index;\r
1149 for (; *LanguageString != 0 && *LanguageString == ';'; LanguageString++);\r
1150 }\r
1151 OldUnicodeStringTable++;\r
1152 NumberOfEntries++;\r
1153 }\r
1154 }\r
1155\r
1156 //\r
1157 // Allocate space for a new Unicode String Table. It must hold the current number of\r
1158 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table\r
1159 // marker\r
1160 //\r
1161 NewUnicodeStringTable = AllocatePool ((NumberOfEntries + 2) * sizeof (EFI_UNICODE_STRING_TABLE));\r
1162 if (NewUnicodeStringTable == NULL) {\r
1163 return EFI_OUT_OF_RESOURCES;\r
1164 }\r
1165\r
1166 //\r
1167 // If the current Unicode String Table contains any entries, then copy them to the\r
1168 // newly allocated Unicode String Table.\r
1169 //\r
1170 if (*UnicodeStringTable != NULL) {\r
1171 CopyMem (\r
1172 NewUnicodeStringTable,\r
1173 *UnicodeStringTable,\r
1174 NumberOfEntries * sizeof (EFI_UNICODE_STRING_TABLE)\r
1175 );\r
1176 }\r
1177\r
1178 //\r
1179 // Allocate space for a copy of the Language specifier\r
1180 //\r
1181 NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (AsciiStrSize(Language), Language);\r
1182 if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) {\r
6389e32b 1183 FreePool (NewUnicodeStringTable);\r
dd51a993 1184 return EFI_OUT_OF_RESOURCES;\r
1185 }\r
1186\r
1187 //\r
1188 // Compute the length of the Unicode String\r
1189 //\r
1190 for (UnicodeStringLength = 0; UnicodeString[UnicodeStringLength] != 0; UnicodeStringLength++);\r
1191\r
1192 //\r
1193 // Allocate space for a copy of the Unicode String\r
1194 //\r
1195 NewUnicodeStringTable[NumberOfEntries].UnicodeString = AllocateCopyPool (StrSize (UnicodeString), UnicodeString);\r
1196 if (NewUnicodeStringTable[NumberOfEntries].UnicodeString == NULL) {\r
6389e32b
LG
1197 FreePool (NewUnicodeStringTable[NumberOfEntries].Language);\r
1198 FreePool (NewUnicodeStringTable);\r
dd51a993 1199 return EFI_OUT_OF_RESOURCES;\r
1200 }\r
1201\r
1202 //\r
1203 // Mark the end of the Unicode String Table\r
1204 //\r
1205 NewUnicodeStringTable[NumberOfEntries + 1].Language = NULL;\r
1206 NewUnicodeStringTable[NumberOfEntries + 1].UnicodeString = NULL;\r
1207\r
1208 //\r
1209 // Free the old Unicode String Table\r
1210 //\r
1211 if (*UnicodeStringTable != NULL) {\r
6389e32b 1212 FreePool (*UnicodeStringTable);\r
dd51a993 1213 }\r
1214\r
1215 //\r
1216 // Point UnicodeStringTable at the newly allocated Unicode String Table\r
1217 //\r
1218 *UnicodeStringTable = NewUnicodeStringTable;\r
1219\r
1220 return EFI_SUCCESS;\r
1221}\r
1222\r
e386b444 1223/**\r
1224 This function frees the table of Unicode strings in UnicodeStringTable.\r
1d37ab9f 1225\r
e386b444 1226 If UnicodeStringTable is NULL, then EFI_SUCCESS is returned.\r
dd51a993 1227 Otherwise, each language code, and each Unicode string in the Unicode string \r
e386b444 1228 table are freed, and EFI_SUCCESS is returned.\r
1229\r
1230 @param UnicodeStringTable A pointer to the table of Unicode strings.\r
1231\r
1232 @retval EFI_SUCCESS The Unicode string table was freed.\r
1233\r
1234**/\r
1235EFI_STATUS\r
1236EFIAPI\r
1237FreeUnicodeStringTable (\r
1238 IN EFI_UNICODE_STRING_TABLE *UnicodeStringTable\r
1239 )\r
1240{\r
1241 UINTN Index;\r
1242\r
1243 //\r
1244 // If the Unicode String Table is NULL, then it is already freed\r
1245 //\r
1246 if (UnicodeStringTable == NULL) {\r
1247 return EFI_SUCCESS;\r
1248 }\r
1249\r
1250 //\r
1251 // Loop through the Unicode String Table until we reach the end of table marker\r
1252 //\r
1253 for (Index = 0; UnicodeStringTable[Index].Language != NULL; Index++) {\r
1254\r
1255 //\r
1256 // Free the Language string from the Unicode String Table\r
1257 //\r
6389e32b 1258 FreePool (UnicodeStringTable[Index].Language);\r
e386b444 1259\r
1260 //\r
1261 // Free the Unicode String from the Unicode String Table\r
1262 //\r
1263 if (UnicodeStringTable[Index].UnicodeString != NULL) {\r
6389e32b 1264 FreePool (UnicodeStringTable[Index].UnicodeString);\r
e386b444 1265 }\r
1266 }\r
1267\r
1268 //\r
1269 // Free the Unicode String Table itself\r
1270 //\r
6389e32b 1271 FreePool (UnicodeStringTable);\r
e386b444 1272\r
1273 return EFI_SUCCESS;\r
1274}\r
6d28c497 1275\r
bf4a3dbd
ED
1276#ifndef DISABLE_NEW_DEPRECATED_INTERFACES\r
1277\r
6d28c497 1278/**\r
bf4a3dbd
ED
1279 [ATTENTION] This function will be deprecated for security reason.\r
1280\r
28d3e14f 1281 Returns a pointer to an allocated buffer that contains the contents of a \r
1282 variable retrieved through the UEFI Runtime Service GetVariable(). The \r
1283 returned buffer is allocated using AllocatePool(). The caller is responsible\r
1284 for freeing this buffer with FreePool().\r
6d28c497 1285\r
28d3e14f 1286 If Name is NULL, then ASSERT().\r
1287 If Guid is NULL, then ASSERT().\r
6d28c497 1288\r
2fc59a00 1289 @param[in] Name The pointer to a Null-terminated Unicode string.\r
1290 @param[in] Guid The pointer to an EFI_GUID structure\r
6d28c497 1291\r
28d3e14f 1292 @retval NULL The variable could not be retrieved.\r
1293 @retval NULL There are not enough resources available for the variable contents.\r
1294 @retval Other A pointer to allocated buffer containing the variable contents.\r
6d28c497 1295\r
1296**/\r
1297VOID *\r
1298EFIAPI\r
1299GetVariable (\r
35db1186 1300 IN CONST CHAR16 *Name,\r
1301 IN CONST EFI_GUID *Guid\r
1302 )\r
6d28c497 1303{\r
1304 EFI_STATUS Status;\r
1305 UINTN Size;\r
1306 VOID *Value;\r
1307\r
1308 ASSERT (Name != NULL);\r
1309 ASSERT (Guid != NULL);\r
1310\r
1311 //\r
1312 // Try to get the variable size.\r
1313 //\r
1314 Value = NULL;\r
1315 Size = 0;\r
1316 Status = gRT->GetVariable ((CHAR16 *) Name, (EFI_GUID *) Guid, NULL, &Size, Value);\r
1317 if (Status != EFI_BUFFER_TOO_SMALL) {\r
1318 return NULL;\r
1319 }\r
1320\r
1321 //\r
1322 // Allocate buffer to get the variable.\r
1323 //\r
1324 Value = AllocatePool (Size);\r
1325 if (Value == NULL) {\r
1326 return NULL;\r
1327 }\r
1328\r
1329 //\r
1330 // Get the variable data.\r
1331 //\r
1332 Status = gRT->GetVariable ((CHAR16 *) Name, (EFI_GUID *) Guid, NULL, &Size, Value);\r
1333 if (EFI_ERROR (Status)) {\r
1334 FreePool(Value);\r
1335 return NULL;\r
1336 }\r
1337\r
1338 return Value;\r
1339}\r
1340\r
6d28c497 1341/**\r
bf4a3dbd
ED
1342 [ATTENTION] This function will be deprecated for security reason.\r
1343\r
6d28c497 1344 Returns a pointer to an allocated buffer that contains the contents of a \r
1345 variable retrieved through the UEFI Runtime Service GetVariable(). This \r
1346 function always uses the EFI_GLOBAL_VARIABLE GUID to retrieve variables.\r
1347 The returned buffer is allocated using AllocatePool(). The caller is \r
1348 responsible for freeing this buffer with FreePool().\r
1349\r
1350 If Name is NULL, then ASSERT().\r
1351\r
2fc59a00 1352 @param[in] Name The pointer to a Null-terminated Unicode string.\r
6d28c497 1353\r
1354 @retval NULL The variable could not be retrieved.\r
1355 @retval NULL There are not enough resources available for the variable contents.\r
1356 @retval Other A pointer to allocated buffer containing the variable contents.\r
1357\r
1358**/\r
1359VOID *\r
1360EFIAPI\r
1361GetEfiGlobalVariable (\r
1362 IN CONST CHAR16 *Name\r
1363 )\r
1364{\r
1365 return GetVariable (Name, &gEfiGlobalVariableGuid);\r
1366}\r
bf4a3dbd
ED
1367#endif\r
1368\r
1369/**\r
1370 Returns the status whether get the variable success. The function retrieves \r
1371 variable through the UEFI Runtime Service GetVariable(). The \r
1372 returned buffer is allocated using AllocatePool(). The caller is responsible\r
1373 for freeing this buffer with FreePool().\r
1374\r
1375 If Name is NULL, then ASSERT().\r
1376 If Guid is NULL, then ASSERT().\r
1377 If Value is NULL, then ASSERT().\r
1378\r
1379 @param[in] Name The pointer to a Null-terminated Unicode string.\r
1380 @param[in] Guid The pointer to an EFI_GUID structure\r
1381 @param[out] Value The buffer point saved the variable info.\r
1382 @param[out] Size The buffer size of the variable.\r
1383\r
1384 @return EFI_OUT_OF_RESOURCES Allocate buffer failed.\r
1385 @return EFI_SUCCESS Find the specified variable.\r
1386 @return Others Errors Return errors from call to gRT->GetVariable.\r
1387\r
1388**/\r
1389EFI_STATUS\r
1390EFIAPI\r
1391GetVariable2 (\r
1392 IN CONST CHAR16 *Name,\r
1393 IN CONST EFI_GUID *Guid,\r
1394 OUT VOID **Value,\r
1395 OUT UINTN *Size OPTIONAL\r
1396 )\r
1397{\r
1398 EFI_STATUS Status;\r
1399 UINTN BufferSize;\r
1400\r
1401 ASSERT (Name != NULL && Guid != NULL && Value != NULL);\r
1402\r
1403 //\r
1404 // Try to get the variable size.\r
1405 //\r
1406 BufferSize = 0;\r
1407 *Value = NULL;\r
1408 if (Size != NULL) {\r
1409 *Size = 0;\r
1410 }\r
1411 \r
1412 Status = gRT->GetVariable ((CHAR16 *) Name, (EFI_GUID *) Guid, NULL, &BufferSize, *Value);\r
1413 if (Status != EFI_BUFFER_TOO_SMALL) {\r
1414 return Status;\r
1415 }\r
1416\r
1417 //\r
1418 // Allocate buffer to get the variable.\r
1419 //\r
1420 *Value = AllocatePool (BufferSize);\r
1421 ASSERT (*Value != NULL);\r
1422 if (*Value == NULL) {\r
1423 return EFI_OUT_OF_RESOURCES;\r
1424 }\r
1425\r
1426 //\r
1427 // Get the variable data.\r
1428 //\r
1429 Status = gRT->GetVariable ((CHAR16 *) Name, (EFI_GUID *) Guid, NULL, &BufferSize, *Value);\r
1430 if (EFI_ERROR (Status)) {\r
1431 FreePool(*Value);\r
1432 *Value = NULL;\r
1433 }\r
1434\r
1435 if (Size != NULL) {\r
1436 *Size = BufferSize;\r
1437 }\r
1438\r
1439 return Status;\r
1440}\r
6d28c497 1441\r
bf4a3dbd
ED
1442/**\r
1443 Returns a pointer to an allocated buffer that contains the contents of a \r
1444 variable retrieved through the UEFI Runtime Service GetVariable(). This \r
1445 function always uses the EFI_GLOBAL_VARIABLE GUID to retrieve variables.\r
1446 The returned buffer is allocated using AllocatePool(). The caller is \r
1447 responsible for freeing this buffer with FreePool().\r
1448\r
1449 If Name is NULL, then ASSERT().\r
1450 If Value is NULL, then ASSERT().\r
1451\r
1452 @param[in] Name The pointer to a Null-terminated Unicode string.\r
1453 @param[out] Value The buffer point saved the variable info.\r
1454 @param[out] Size The buffer size of the variable.\r
1455\r
1456 @return EFI_OUT_OF_RESOURCES Allocate buffer failed.\r
1457 @return EFI_SUCCESS Find the specified variable.\r
1458 @return Others Errors Return errors from call to gRT->GetVariable.\r
1459\r
1460**/\r
1461EFI_STATUS\r
1462EFIAPI\r
1463GetEfiGlobalVariable2 (\r
1464 IN CONST CHAR16 *Name,\r
1465 OUT VOID **Value,\r
1466 OUT UINTN *Size OPTIONAL\r
1467 )\r
1468{\r
1469 return GetVariable2 (Name, &gEfiGlobalVariableGuid, Value, Size);\r
1470}\r
6d28c497 1471\r
1472/**\r
1473 Returns a pointer to an allocated buffer that contains the best matching language \r
1474 from a set of supported languages. \r
1475 \r
1476 This function supports both ISO 639-2 and RFC 4646 language codes, but language \r
1477 code types may not be mixed in a single call to this function. The language \r
1478 code returned is allocated using AllocatePool(). The caller is responsible for \r
1479 freeing the allocated buffer using FreePool(). This function supports a variable\r
1480 argument list that allows the caller to pass in a prioritized list of language \r
1481 codes to test against all the language codes in SupportedLanguages. \r
1482\r
1483 If SupportedLanguages is NULL, then ASSERT().\r
1484\r
1485 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that\r
1486 contains a set of language codes in the format \r
1487 specified by Iso639Language.\r
3d7c6cfb
LG
1488 @param[in] Iso639Language If not zero, then all language codes are assumed to be\r
1489 in ISO 639-2 format. If zero, then all language\r
6d28c497 1490 codes are assumed to be in RFC 4646 language format\r
1491 @param[in] ... A variable argument list that contains pointers to \r
1492 Null-terminated ASCII strings that contain one or more\r
1493 language codes in the format specified by Iso639Language.\r
1494 The first language code from each of these language\r
1495 code lists is used to determine if it is an exact or\r
1496 close match to any of the language codes in \r
1497 SupportedLanguages. Close matches only apply to RFC 4646\r
1498 language codes, and the matching algorithm from RFC 4647\r
1499 is used to determine if a close match is present. If \r
1500 an exact or close match is found, then the matching\r
1501 language code from SupportedLanguages is returned. If\r
1502 no matches are found, then the next variable argument\r
1503 parameter is evaluated. The variable argument list \r
1504 is terminated by a NULL.\r
1505\r
1506 @retval NULL The best matching language could not be found in SupportedLanguages.\r
1507 @retval NULL There are not enough resources available to return the best matching \r
1508 language.\r
1509 @retval Other A pointer to a Null-terminated ASCII string that is the best matching \r
1510 language in SupportedLanguages.\r
1511\r
1512**/\r
1513CHAR8 *\r
1514EFIAPI\r
1515GetBestLanguage (\r
1516 IN CONST CHAR8 *SupportedLanguages, \r
d2aafe1e 1517 IN UINTN Iso639Language,\r
6d28c497 1518 ...\r
1519 )\r
1520{\r
1521 VA_LIST Args;\r
1522 CHAR8 *Language;\r
1523 UINTN CompareLength;\r
1524 UINTN LanguageLength;\r
1525 CONST CHAR8 *Supported;\r
1526 CHAR8 *BestLanguage;\r
1527\r
1528 ASSERT (SupportedLanguages != NULL);\r
1529\r
1530 VA_START (Args, Iso639Language);\r
1531 while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {\r
1532 //\r
1533 // Default to ISO 639-2 mode\r
1534 //\r
1535 CompareLength = 3;\r
1536 LanguageLength = MIN (3, AsciiStrLen (Language));\r
1537\r
1538 //\r
1539 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language\r
1540 //\r
3d7c6cfb 1541 if (Iso639Language == 0) {\r
6d28c497 1542 for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);\r
1543 }\r
1544\r
1545 //\r
1546 // Trim back the length of Language used until it is empty\r
1547 //\r
1548 while (LanguageLength > 0) {\r
1549 //\r
1550 // Loop through all language codes in SupportedLanguages\r
1551 //\r
1552 for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {\r
1553 //\r
1554 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages\r
1555 //\r
3d7c6cfb 1556 if (Iso639Language == 0) {\r
6d28c497 1557 //\r
1558 // Skip ';' characters in Supported\r
1559 //\r
1560 for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
1561 //\r
1562 // Determine the length of the next language code in Supported\r
1563 //\r
1564 for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);\r
1565 //\r
1566 // If Language is longer than the Supported, then skip to the next language\r
1567 //\r
1568 if (LanguageLength > CompareLength) {\r
1569 continue;\r
1570 }\r
1571 }\r
1572 //\r
1573 // See if the first LanguageLength characters in Supported match Language\r
1574 //\r
1575 if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {\r
1576 VA_END (Args);\r
1577 //\r
1578 // Allocate, copy, and return the best matching language code from SupportedLanguages\r
1579 //\r
1580 BestLanguage = AllocateZeroPool (CompareLength + 1);\r
1581 if (BestLanguage == NULL) {\r
1582 return NULL;\r
1583 }\r
1584 return CopyMem (BestLanguage, Supported, CompareLength);\r
1585 }\r
1586 }\r
1587\r
3d7c6cfb 1588 if (Iso639Language != 0) {\r
6d28c497 1589 //\r
1590 // If ISO 639 mode, then each language can only be tested once\r
1591 //\r
1592 LanguageLength = 0;\r
1593 } else {\r
1594 //\r
1595 // If RFC 4646 mode, then trim Language from the right to the next '-' character \r
1596 //\r
1597 for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);\r
1598 }\r
1599 }\r
1600 }\r
1601 VA_END (Args);\r
1602\r
1603 //\r
1604 // No matches were found \r
1605 //\r
1606 return NULL;\r
1607}\r
40070a18
MK
1608\r
1609/**\r
1610 Returns an array of protocol instance that matches the given protocol.\r
1611\r
1612 @param[in] Protocol Provides the protocol to search for.\r
1613 @param[out] NoProtocols The number of protocols returned in Buffer.\r
1614 @param[out] Buffer A pointer to the buffer to return the requested\r
1615 array of protocol instances that match Protocol.\r
1616 The returned buffer is allocated using\r
1617 EFI_BOOT_SERVICES.AllocatePool(). The caller is\r
1618 responsible for freeing this buffer with\r
1619 EFI_BOOT_SERVICES.FreePool().\r
1620\r
1621 @retval EFI_SUCCESS The array of protocols was returned in Buffer,\r
1622 and the number of protocols in Buffer was\r
1623 returned in NoProtocols.\r
1624 @retval EFI_NOT_FOUND No protocols found.\r
1625 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the\r
1626 matching results.\r
1627 @retval EFI_INVALID_PARAMETER Protocol is NULL.\r
1628 @retval EFI_INVALID_PARAMETER NoProtocols is NULL.\r
1629 @retval EFI_INVALID_PARAMETER Buffer is NULL.\r
1630\r
1631**/\r
1632EFI_STATUS\r
1633EFIAPI\r
1634EfiLocateProtocolBuffer (\r
1635 IN EFI_GUID *Protocol,\r
1636 OUT UINTN *NoProtocols,\r
1637 OUT VOID ***Buffer\r
1638 )\r
1639{\r
1640 EFI_STATUS Status;\r
1641 UINTN NoHandles;\r
1642 EFI_HANDLE *HandleBuffer;\r
1643 UINTN Index;\r
1644\r
1645 //\r
1646 // Check input parameters\r
1647 //\r
1648 if (Protocol == NULL || NoProtocols == NULL || Buffer == NULL) {\r
1649 return EFI_INVALID_PARAMETER;\r
1650 }\r
1651\r
1652 //\r
1653 // Initialze output parameters\r
1654 //\r
1655 *NoProtocols = 0;\r
1656 *Buffer = NULL;\r
1657\r
1658 //\r
1659 // Retrieve the array of handles that support Protocol\r
1660 //\r
1661 Status = gBS->LocateHandleBuffer (\r
1662 ByProtocol,\r
1663 Protocol,\r
1664 NULL,\r
1665 &NoHandles,\r
1666 &HandleBuffer\r
1667 );\r
1668 if (EFI_ERROR (Status)) {\r
1669 return Status;\r
1670 }\r
1671\r
1672 //\r
1673 // Allocate array of protocol instances\r
1674 //\r
1675 Status = gBS->AllocatePool (\r
1676 EfiBootServicesData,\r
1677 NoHandles * sizeof (VOID *),\r
1678 (VOID **)Buffer\r
1679 );\r
1680 if (EFI_ERROR (Status)) {\r
fe507283
SZ
1681 //\r
1682 // Free the handle buffer\r
1683 //\r
1684 gBS->FreePool (HandleBuffer);\r
40070a18
MK
1685 return EFI_OUT_OF_RESOURCES;\r
1686 }\r
1687 ZeroMem (*Buffer, NoHandles * sizeof (VOID *));\r
1688\r
1689 //\r
1690 // Lookup Protocol on each handle in HandleBuffer to fill in the array of\r
1691 // protocol instances. Handle case where protocol instance was present when\r
1692 // LocateHandleBuffer() was called, but is not present when HandleProtocol()\r
1693 // is called.\r
1694 //\r
1695 for (Index = 0, *NoProtocols = 0; Index < NoHandles; Index++) {\r
1696 Status = gBS->HandleProtocol (\r
1697 HandleBuffer[Index],\r
1698 Protocol,\r
1699 &((*Buffer)[*NoProtocols])\r
1700 );\r
1701 if (!EFI_ERROR (Status)) {\r
1702 (*NoProtocols)++;\r
1703 }\r
1704 }\r
1705\r
1706 //\r
1707 // Free the handle buffer\r
1708 //\r
1709 gBS->FreePool (HandleBuffer);\r
1710\r
1711 //\r
1712 // Make sure at least one protocol instance was found\r
1713 //\r
1714 if (*NoProtocols == 0) {\r
1715 gBS->FreePool (*Buffer);\r
1716 *Buffer = NULL;\r
1717 return EFI_NOT_FOUND;\r
1718 }\r
1719\r
1720 return EFI_SUCCESS;\r
1721}\r