]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/UefiLib/UefiLib.c
MdePkg/UefiLib: Add a new API GetVariable3
[mirror_edk2.git] / MdePkg / Library / UefiLib / UefiLib.c
CommitLineData
e386b444 1/** @file\r
9095d37b
LG
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
5ad97f35 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
9095d37b 23 Empty constructor function that is required to resolve dependencies between\r
5bd2d2cb 24 libraries.\r
9095d37b 25\r
5bd2d2cb 26 ** DO NOT REMOVE **\r
9095d37b 27\r
5bd2d2cb 28 @param ImageHandle The firmware allocated handle for the EFI image.\r
29 @param SystemTable A pointer to the EFI System Table.\r
9095d37b 30\r
5bd2d2cb 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
9095d37b 72\r
1d37ab9f 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
9095d37b 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
9095d37b 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
9095d37b 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
9095d37b 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
9095d37b 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
9095d37b 368/**\r
e386b444 369 Returns the current TPL.\r
370\r
9095d37b
LG
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
9095d37b
LG
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
9095d37b
LG
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
9095d37b
LG
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
9095d37b 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
9095d37b 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
9095d37b 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
9095d37b 656 @param Language A pointer to the ISO 639-2 language code for the\r
1d37ab9f 657 Unicode string to look up and return.\r
9095d37b
LG
658 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes\r
659 that the Unicode string table supports. Language\r
1d37ab9f 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
9095d37b 665 @retval EFI_SUCCESS The Unicode string that matches the language\r
1d37ab9f 666 specified by Language was found\r
9095d37b 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
9095d37b 673 @retval EFI_UNSUPPORTED The language specified by Language is not a\r
1d37ab9f 674 member of SupportedLanguages.\r
9095d37b 675 @retval EFI_UNSUPPORTED The language specified by Language is not\r
1d37ab9f 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
9095d37b 748 string must be Null-terminated.\r
cf8ae2f6 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
9095d37b
LG
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
771 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.\r
772 @retval EFI_UNSUPPORTED The language specified by Language is not a member of SupportedLanguages.\r
cf8ae2f6 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
9095d37b
LG
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
9095d37b 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
9095d37b
LG
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
9095d37b 880 @retval EFI_ALREADY_STARTED A Unicode string with language Language is\r
1d37ab9f 881 already present in UnicodeStringTable.\r
9095d37b 882 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another\r
1d37ab9f 883 Unicode string to UnicodeStringTable.\r
9095d37b 884 @retval EFI_UNSUPPORTED The language specified by Language is not a\r
1d37ab9f 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
9095d37b 1047 @param UnicodeString A pointer to the Unicode string to add.\r
cf8ae2f6 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
9095d37b
LG
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
cf8ae2f6 1059 @retval EFI_ALREADY_STARTED A Unicode string with language Language is already present in\r
9095d37b
LG
1060 UnicodeStringTable.\r
1061 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another Unicode string UnicodeStringTable.\r
cf8ae2f6 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
9095d37b 1145 if (AsciiStrnCmp (Language, LanguageString, Index) == 0) {\r
dd51a993 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
9095d37b 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
9095d37b
LG
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
28d3e14f 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
9095d37b
LG
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
6d28c497 1346 function always uses the EFI_GLOBAL_VARIABLE GUID to retrieve variables.\r
9095d37b 1347 The returned buffer is allocated using AllocatePool(). The caller is\r
6d28c497 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
9095d37b
LG
1370 Returns the status whether get the variable success. The function retrieves\r
1371 variable through the UEFI Runtime Service GetVariable(). The\r
bf4a3dbd
ED
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
9095d37b 1411\r
bf4a3dbd
ED
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
37bf6787
BB
1439 return Status;\r
1440}\r
1441\r
1442/** Return the attributes of the variable.\r
1443\r
1444 Returns the status whether get the variable success. The function retrieves\r
1445 variable through the UEFI Runtime Service GetVariable(). The\r
1446 returned buffer is allocated using AllocatePool(). The caller is responsible\r
1447 for freeing this buffer with FreePool(). The attributes are returned if\r
1448 the caller provides a valid Attribute parameter.\r
1449\r
1450 If Name is NULL, then ASSERT().\r
1451 If Guid is NULL, then ASSERT().\r
1452 If Value is NULL, then ASSERT().\r
1453\r
1454 @param[in] Name The pointer to a Null-terminated Unicode string.\r
1455 @param[in] Guid The pointer to an EFI_GUID structure\r
1456 @param[out] Value The buffer point saved the variable info.\r
1457 @param[out] Size The buffer size of the variable.\r
1458 @param[out] Attr The pointer to the variable attributes as found in var store\r
1459\r
1460 @retval EFI_OUT_OF_RESOURCES Allocate buffer failed.\r
1461 @retval EFI_SUCCESS Find the specified variable.\r
1462 @retval Others Errors Return errors from call to gRT->GetVariable.\r
1463\r
1464**/\r
1465EFI_STATUS\r
1466EFIAPI\r
1467GetVariable3(\r
1468 IN CONST CHAR16 *Name,\r
1469 IN CONST EFI_GUID *Guid,\r
1470 OUT VOID **Value,\r
1471 OUT UINTN *Size OPTIONAL,\r
1472 OUT UINT32 *Attr OPTIONAL\r
1473 )\r
1474{\r
1475 EFI_STATUS Status;\r
1476 UINTN BufferSize;\r
1477\r
1478 ASSERT(Name != NULL && Guid != NULL && Value != NULL);\r
1479\r
1480 //\r
1481 // Try to get the variable size.\r
1482 //\r
1483 BufferSize = 0;\r
1484 *Value = NULL;\r
1485 if (Size != NULL) {\r
1486 *Size = 0;\r
1487 }\r
1488\r
1489 if (Attr != NULL) {\r
1490 *Attr = 0;\r
1491 }\r
1492\r
1493 Status = gRT->GetVariable((CHAR16 *)Name, (EFI_GUID *)Guid, Attr, &BufferSize, *Value);\r
1494 if (Status != EFI_BUFFER_TOO_SMALL) {\r
1495 return Status;\r
1496 }\r
1497\r
1498 //\r
1499 // Allocate buffer to get the variable.\r
1500 //\r
1501 *Value = AllocatePool(BufferSize);\r
1502 ASSERT(*Value != NULL);\r
1503 if (*Value == NULL) {\r
1504 return EFI_OUT_OF_RESOURCES;\r
1505 }\r
1506\r
1507 //\r
1508 // Get the variable data.\r
1509 //\r
1510 Status = gRT->GetVariable((CHAR16 *)Name, (EFI_GUID *)Guid, Attr, &BufferSize, *Value);\r
1511 if (EFI_ERROR(Status)) {\r
1512 FreePool(*Value);\r
1513 *Value = NULL;\r
1514 }\r
1515\r
1516 if (Size != NULL) {\r
1517 *Size = BufferSize;\r
1518 }\r
1519\r
bf4a3dbd
ED
1520 return Status;\r
1521}\r
6d28c497 1522\r
bf4a3dbd 1523/**\r
9095d37b
LG
1524 Returns a pointer to an allocated buffer that contains the contents of a\r
1525 variable retrieved through the UEFI Runtime Service GetVariable(). This\r
bf4a3dbd 1526 function always uses the EFI_GLOBAL_VARIABLE GUID to retrieve variables.\r
9095d37b 1527 The returned buffer is allocated using AllocatePool(). The caller is\r
bf4a3dbd
ED
1528 responsible for freeing this buffer with FreePool().\r
1529\r
1530 If Name is NULL, then ASSERT().\r
1531 If Value is NULL, then ASSERT().\r
1532\r
1533 @param[in] Name The pointer to a Null-terminated Unicode string.\r
1534 @param[out] Value The buffer point saved the variable info.\r
1535 @param[out] Size The buffer size of the variable.\r
1536\r
1537 @return EFI_OUT_OF_RESOURCES Allocate buffer failed.\r
1538 @return EFI_SUCCESS Find the specified variable.\r
1539 @return Others Errors Return errors from call to gRT->GetVariable.\r
1540\r
1541**/\r
1542EFI_STATUS\r
1543EFIAPI\r
1544GetEfiGlobalVariable2 (\r
1545 IN CONST CHAR16 *Name,\r
1546 OUT VOID **Value,\r
1547 OUT UINTN *Size OPTIONAL\r
1548 )\r
1549{\r
1550 return GetVariable2 (Name, &gEfiGlobalVariableGuid, Value, Size);\r
1551}\r
6d28c497 1552\r
1553/**\r
9095d37b
LG
1554 Returns a pointer to an allocated buffer that contains the best matching language\r
1555 from a set of supported languages.\r
1556\r
1557 This function supports both ISO 639-2 and RFC 4646 language codes, but language\r
1558 code types may not be mixed in a single call to this function. The language\r
1559 code returned is allocated using AllocatePool(). The caller is responsible for\r
6d28c497 1560 freeing the allocated buffer using FreePool(). This function supports a variable\r
9095d37b
LG
1561 argument list that allows the caller to pass in a prioritized list of language\r
1562 codes to test against all the language codes in SupportedLanguages.\r
6d28c497 1563\r
1564 If SupportedLanguages is NULL, then ASSERT().\r
1565\r
1566 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that\r
9095d37b 1567 contains a set of language codes in the format\r
6d28c497 1568 specified by Iso639Language.\r
3d7c6cfb
LG
1569 @param[in] Iso639Language If not zero, then all language codes are assumed to be\r
1570 in ISO 639-2 format. If zero, then all language\r
6d28c497 1571 codes are assumed to be in RFC 4646 language format\r
9095d37b 1572 @param[in] ... A variable argument list that contains pointers to\r
6d28c497 1573 Null-terminated ASCII strings that contain one or more\r
1574 language codes in the format specified by Iso639Language.\r
1575 The first language code from each of these language\r
1576 code lists is used to determine if it is an exact or\r
9095d37b 1577 close match to any of the language codes in\r
6d28c497 1578 SupportedLanguages. Close matches only apply to RFC 4646\r
1579 language codes, and the matching algorithm from RFC 4647\r
9095d37b 1580 is used to determine if a close match is present. If\r
6d28c497 1581 an exact or close match is found, then the matching\r
1582 language code from SupportedLanguages is returned. If\r
1583 no matches are found, then the next variable argument\r
9095d37b 1584 parameter is evaluated. The variable argument list\r
6d28c497 1585 is terminated by a NULL.\r
1586\r
1587 @retval NULL The best matching language could not be found in SupportedLanguages.\r
9095d37b 1588 @retval NULL There are not enough resources available to return the best matching\r
6d28c497 1589 language.\r
9095d37b 1590 @retval Other A pointer to a Null-terminated ASCII string that is the best matching\r
6d28c497 1591 language in SupportedLanguages.\r
1592\r
1593**/\r
1594CHAR8 *\r
1595EFIAPI\r
1596GetBestLanguage (\r
9095d37b 1597 IN CONST CHAR8 *SupportedLanguages,\r
d2aafe1e 1598 IN UINTN Iso639Language,\r
6d28c497 1599 ...\r
1600 )\r
1601{\r
1602 VA_LIST Args;\r
1603 CHAR8 *Language;\r
1604 UINTN CompareLength;\r
1605 UINTN LanguageLength;\r
1606 CONST CHAR8 *Supported;\r
1607 CHAR8 *BestLanguage;\r
1608\r
1609 ASSERT (SupportedLanguages != NULL);\r
1610\r
1611 VA_START (Args, Iso639Language);\r
1612 while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {\r
1613 //\r
1614 // Default to ISO 639-2 mode\r
1615 //\r
1616 CompareLength = 3;\r
1617 LanguageLength = MIN (3, AsciiStrLen (Language));\r
1618\r
1619 //\r
1620 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language\r
1621 //\r
3d7c6cfb 1622 if (Iso639Language == 0) {\r
6d28c497 1623 for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);\r
1624 }\r
1625\r
1626 //\r
1627 // Trim back the length of Language used until it is empty\r
1628 //\r
1629 while (LanguageLength > 0) {\r
1630 //\r
1631 // Loop through all language codes in SupportedLanguages\r
1632 //\r
1633 for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {\r
1634 //\r
1635 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages\r
1636 //\r
3d7c6cfb 1637 if (Iso639Language == 0) {\r
6d28c497 1638 //\r
1639 // Skip ';' characters in Supported\r
1640 //\r
1641 for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
1642 //\r
1643 // Determine the length of the next language code in Supported\r
1644 //\r
1645 for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);\r
1646 //\r
1647 // If Language is longer than the Supported, then skip to the next language\r
1648 //\r
1649 if (LanguageLength > CompareLength) {\r
1650 continue;\r
1651 }\r
1652 }\r
1653 //\r
1654 // See if the first LanguageLength characters in Supported match Language\r
1655 //\r
1656 if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {\r
1657 VA_END (Args);\r
1658 //\r
1659 // Allocate, copy, and return the best matching language code from SupportedLanguages\r
1660 //\r
1661 BestLanguage = AllocateZeroPool (CompareLength + 1);\r
1662 if (BestLanguage == NULL) {\r
1663 return NULL;\r
1664 }\r
1665 return CopyMem (BestLanguage, Supported, CompareLength);\r
1666 }\r
1667 }\r
1668\r
3d7c6cfb 1669 if (Iso639Language != 0) {\r
6d28c497 1670 //\r
1671 // If ISO 639 mode, then each language can only be tested once\r
1672 //\r
1673 LanguageLength = 0;\r
1674 } else {\r
1675 //\r
9095d37b 1676 // If RFC 4646 mode, then trim Language from the right to the next '-' character\r
6d28c497 1677 //\r
1678 for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);\r
1679 }\r
1680 }\r
1681 }\r
1682 VA_END (Args);\r
1683\r
1684 //\r
9095d37b 1685 // No matches were found\r
6d28c497 1686 //\r
1687 return NULL;\r
1688}\r
40070a18
MK
1689\r
1690/**\r
1691 Returns an array of protocol instance that matches the given protocol.\r
1692\r
1693 @param[in] Protocol Provides the protocol to search for.\r
1694 @param[out] NoProtocols The number of protocols returned in Buffer.\r
1695 @param[out] Buffer A pointer to the buffer to return the requested\r
1696 array of protocol instances that match Protocol.\r
1697 The returned buffer is allocated using\r
1698 EFI_BOOT_SERVICES.AllocatePool(). The caller is\r
1699 responsible for freeing this buffer with\r
1700 EFI_BOOT_SERVICES.FreePool().\r
1701\r
1702 @retval EFI_SUCCESS The array of protocols was returned in Buffer,\r
1703 and the number of protocols in Buffer was\r
1704 returned in NoProtocols.\r
1705 @retval EFI_NOT_FOUND No protocols found.\r
1706 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the\r
1707 matching results.\r
1708 @retval EFI_INVALID_PARAMETER Protocol is NULL.\r
1709 @retval EFI_INVALID_PARAMETER NoProtocols is NULL.\r
1710 @retval EFI_INVALID_PARAMETER Buffer is NULL.\r
1711\r
1712**/\r
1713EFI_STATUS\r
1714EFIAPI\r
1715EfiLocateProtocolBuffer (\r
1716 IN EFI_GUID *Protocol,\r
1717 OUT UINTN *NoProtocols,\r
1718 OUT VOID ***Buffer\r
1719 )\r
1720{\r
1721 EFI_STATUS Status;\r
1722 UINTN NoHandles;\r
1723 EFI_HANDLE *HandleBuffer;\r
1724 UINTN Index;\r
1725\r
1726 //\r
1727 // Check input parameters\r
1728 //\r
1729 if (Protocol == NULL || NoProtocols == NULL || Buffer == NULL) {\r
1730 return EFI_INVALID_PARAMETER;\r
1731 }\r
1732\r
1733 //\r
1734 // Initialze output parameters\r
1735 //\r
1736 *NoProtocols = 0;\r
1737 *Buffer = NULL;\r
1738\r
1739 //\r
1740 // Retrieve the array of handles that support Protocol\r
1741 //\r
1742 Status = gBS->LocateHandleBuffer (\r
1743 ByProtocol,\r
1744 Protocol,\r
1745 NULL,\r
1746 &NoHandles,\r
1747 &HandleBuffer\r
1748 );\r
1749 if (EFI_ERROR (Status)) {\r
1750 return Status;\r
1751 }\r
1752\r
1753 //\r
1754 // Allocate array of protocol instances\r
1755 //\r
1756 Status = gBS->AllocatePool (\r
1757 EfiBootServicesData,\r
1758 NoHandles * sizeof (VOID *),\r
1759 (VOID **)Buffer\r
1760 );\r
1761 if (EFI_ERROR (Status)) {\r
fe507283
SZ
1762 //\r
1763 // Free the handle buffer\r
1764 //\r
1765 gBS->FreePool (HandleBuffer);\r
40070a18
MK
1766 return EFI_OUT_OF_RESOURCES;\r
1767 }\r
1768 ZeroMem (*Buffer, NoHandles * sizeof (VOID *));\r
1769\r
1770 //\r
1771 // Lookup Protocol on each handle in HandleBuffer to fill in the array of\r
1772 // protocol instances. Handle case where protocol instance was present when\r
1773 // LocateHandleBuffer() was called, but is not present when HandleProtocol()\r
1774 // is called.\r
1775 //\r
1776 for (Index = 0, *NoProtocols = 0; Index < NoHandles; Index++) {\r
1777 Status = gBS->HandleProtocol (\r
1778 HandleBuffer[Index],\r
1779 Protocol,\r
1780 &((*Buffer)[*NoProtocols])\r
1781 );\r
1782 if (!EFI_ERROR (Status)) {\r
1783 (*NoProtocols)++;\r
1784 }\r
1785 }\r
1786\r
1787 //\r
1788 // Free the handle buffer\r
1789 //\r
1790 gBS->FreePool (HandleBuffer);\r
1791\r
1792 //\r
1793 // Make sure at least one protocol instance was found\r
1794 //\r
1795 if (*NoProtocols == 0) {\r
1796 gBS->FreePool (*Buffer);\r
1797 *Buffer = NULL;\r
1798 return EFI_NOT_FOUND;\r
1799 }\r
1800\r
1801 return EFI_SUCCESS;\r
1802}\r
768b6111
LE
1803\r
1804/**\r
1805 Open or create a file or directory, possibly creating the chain of\r
1806 directories leading up to the directory.\r
1807\r
1808 EfiOpenFileByDevicePath() first locates EFI_SIMPLE_FILE_SYSTEM_PROTOCOL on\r
1809 FilePath, and opens the root directory of that filesystem with\r
1810 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL.OpenVolume().\r
1811\r
1812 On the remaining device path, the longest initial sequence of\r
1813 FILEPATH_DEVICE_PATH nodes is node-wise traversed with\r
5dbc768f 1814 EFI_FILE_PROTOCOL.Open().\r
768b6111
LE
1815\r
1816 (As a consequence, if OpenMode includes EFI_FILE_MODE_CREATE, and Attributes\r
1817 includes EFI_FILE_DIRECTORY, and each FILEPATH_DEVICE_PATH specifies a single\r
1818 pathname component, then EfiOpenFileByDevicePath() ensures that the specified\r
1819 series of subdirectories exist on return.)\r
1820\r
1821 The EFI_FILE_PROTOCOL identified by the last FILEPATH_DEVICE_PATH node is\r
1822 output to the caller; intermediate EFI_FILE_PROTOCOL instances are closed. If\r
1823 there are no FILEPATH_DEVICE_PATH nodes past the node that identifies the\r
1824 filesystem, then the EFI_FILE_PROTOCOL of the root directory of the\r
1825 filesystem is output to the caller. If a device path node that is different\r
1826 from FILEPATH_DEVICE_PATH is encountered relative to the filesystem, the\r
1827 traversal is stopped with an error, and a NULL EFI_FILE_PROTOCOL is output.\r
1828\r
1829 @param[in,out] FilePath On input, the device path to the file or directory\r
1830 to open or create. The caller is responsible for\r
1831 ensuring that the device path pointed-to by FilePath\r
1832 is well-formed. On output, FilePath points one past\r
1833 the last node in the original device path that has\r
1834 been successfully processed. FilePath is set on\r
1835 output even if EfiOpenFileByDevicePath() returns an\r
1836 error.\r
1837\r
1838 @param[out] File On error, File is set to NULL. On success, File is\r
1839 set to the EFI_FILE_PROTOCOL of the root directory\r
1840 of the filesystem, if there are no\r
1841 FILEPATH_DEVICE_PATH nodes in FilePath; otherwise,\r
1842 File is set to the EFI_FILE_PROTOCOL identified by\r
1843 the last node in FilePath.\r
1844\r
1845 @param[in] OpenMode The OpenMode parameter to pass to\r
5dbc768f 1846 EFI_FILE_PROTOCOL.Open().\r
768b6111
LE
1847\r
1848 @param[in] Attributes The Attributes parameter to pass to\r
5dbc768f 1849 EFI_FILE_PROTOCOL.Open().\r
768b6111
LE
1850\r
1851 @retval EFI_SUCCESS The file or directory has been opened or\r
1852 created.\r
1853\r
1854 @retval EFI_INVALID_PARAMETER FilePath is NULL; or File is NULL; or FilePath\r
1855 contains a device path node, past the node\r
1856 that identifies\r
1857 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL, that is not a\r
1858 FILEPATH_DEVICE_PATH node.\r
1859\r
1860 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.\r
1861\r
1862 @return Error codes propagated from the\r
1863 LocateDevicePath() and OpenProtocol() boot\r
1864 services, and from the\r
1865 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL.OpenVolume()\r
1866 and EFI_FILE_PROTOCOL.Open() member functions.\r
1867**/\r
1868EFI_STATUS\r
1869EFIAPI\r
1870EfiOpenFileByDevicePath (\r
1871 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath,\r
1872 OUT EFI_FILE_PROTOCOL **File,\r
1873 IN UINT64 OpenMode,\r
1874 IN UINT64 Attributes\r
1875 )\r
1876{\r
1877 EFI_STATUS Status;\r
1878 EFI_HANDLE FileSystemHandle;\r
1879 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *FileSystem;\r
1880 EFI_FILE_PROTOCOL *LastFile;\r
1881 FILEPATH_DEVICE_PATH *FilePathNode;\r
1882 CHAR16 *AlignedPathName;\r
1883 CHAR16 *PathName;\r
1884 EFI_FILE_PROTOCOL *NextFile;\r
1885\r
1886 if (File == NULL) {\r
1887 return EFI_INVALID_PARAMETER;\r
1888 }\r
1889 *File = NULL;\r
1890\r
1891 if (FilePath == NULL) {\r
1892 return EFI_INVALID_PARAMETER;\r
1893 }\r
1894\r
1895 //\r
1896 // Look up the filesystem.\r
1897 //\r
1898 Status = gBS->LocateDevicePath (\r
1899 &gEfiSimpleFileSystemProtocolGuid,\r
1900 FilePath,\r
1901 &FileSystemHandle\r
1902 );\r
1903 if (EFI_ERROR (Status)) {\r
1904 return Status;\r
1905 }\r
1906 Status = gBS->OpenProtocol (\r
1907 FileSystemHandle,\r
1908 &gEfiSimpleFileSystemProtocolGuid,\r
1909 (VOID **)&FileSystem,\r
1910 gImageHandle,\r
1911 NULL,\r
1912 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
1913 );\r
1914 if (EFI_ERROR (Status)) {\r
1915 return Status;\r
1916 }\r
1917\r
1918 //\r
1919 // Open the root directory of the filesystem. After this operation succeeds,\r
1920 // we have to release LastFile on error.\r
1921 //\r
1922 Status = FileSystem->OpenVolume (FileSystem, &LastFile);\r
1923 if (EFI_ERROR (Status)) {\r
1924 return Status;\r
1925 }\r
1926\r
1927 //\r
1928 // Traverse the device path nodes relative to the filesystem.\r
1929 //\r
1930 while (!IsDevicePathEnd (*FilePath)) {\r
1931 if (DevicePathType (*FilePath) != MEDIA_DEVICE_PATH ||\r
1932 DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP) {\r
1933 Status = EFI_INVALID_PARAMETER;\r
1934 goto CloseLastFile;\r
1935 }\r
1936 FilePathNode = (FILEPATH_DEVICE_PATH *)*FilePath;\r
1937\r
1938 //\r
1939 // FilePathNode->PathName may be unaligned, and the UEFI specification\r
1940 // requires pointers that are passed to protocol member functions to be\r
1941 // aligned. Create an aligned copy of the pathname if necessary.\r
1942 //\r
1943 if ((UINTN)FilePathNode->PathName % sizeof *FilePathNode->PathName == 0) {\r
1944 AlignedPathName = NULL;\r
1945 PathName = FilePathNode->PathName;\r
1946 } else {\r
1947 AlignedPathName = AllocateCopyPool (\r
1948 (DevicePathNodeLength (FilePathNode) -\r
1949 SIZE_OF_FILEPATH_DEVICE_PATH),\r
1950 FilePathNode->PathName\r
1951 );\r
1952 if (AlignedPathName == NULL) {\r
1953 Status = EFI_OUT_OF_RESOURCES;\r
1954 goto CloseLastFile;\r
1955 }\r
1956 PathName = AlignedPathName;\r
1957 }\r
1958\r
1959 //\r
5dbc768f 1960 // Open or create the file corresponding to the next pathname fragment.\r
768b6111
LE
1961 //\r
1962 Status = LastFile->Open (\r
1963 LastFile,\r
1964 &NextFile,\r
1965 PathName,\r
5dbc768f
LE
1966 OpenMode,\r
1967 Attributes\r
768b6111
LE
1968 );\r
1969\r
768b6111
LE
1970 //\r
1971 // Release any AlignedPathName on both error and success paths; PathName is\r
1972 // no longer needed.\r
1973 //\r
1974 if (AlignedPathName != NULL) {\r
1975 FreePool (AlignedPathName);\r
1976 }\r
1977 if (EFI_ERROR (Status)) {\r
1978 goto CloseLastFile;\r
1979 }\r
1980\r
1981 //\r
1982 // Advance to the next device path node.\r
1983 //\r
1984 LastFile->Close (LastFile);\r
1985 LastFile = NextFile;\r
1986 *FilePath = NextDevicePathNode (FilePathNode);\r
1987 }\r
1988\r
1989 *File = LastFile;\r
1990 return EFI_SUCCESS;\r
1991\r
1992CloseLastFile:\r
1993 LastFile->Close (LastFile);\r
1994\r
1995 //\r
1996 // We are on the error path; we must have set an error Status for returning\r
1997 // to the caller.\r
1998 //\r
1999 ASSERT (EFI_ERROR (Status));\r
2000 return Status;\r
2001}\r