]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/UefiLib/UefiLib.c
MdePkg: Replace BSD License with BSD+Patent License
[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
9344f092 9 SPDX-License-Identifier: BSD-2-Clause-Patent\r
e386b444 10\r
e386b444 11**/\r
12\r
1efcc4ae 13\r
f734a10a 14#include "UefiLibInternal.h"\r
e386b444 15\r
5bd2d2cb 16/**\r
9095d37b 17 Empty constructor function that is required to resolve dependencies between\r
5bd2d2cb 18 libraries.\r
9095d37b 19\r
5bd2d2cb 20 ** DO NOT REMOVE **\r
9095d37b 21\r
5bd2d2cb 22 @param ImageHandle The firmware allocated handle for the EFI image.\r
23 @param SystemTable A pointer to the EFI System Table.\r
9095d37b 24\r
5bd2d2cb 25 @retval EFI_SUCCESS The constructor executed correctly.\r
26\r
27**/\r
28EFI_STATUS\r
29EFIAPI\r
30UefiLibConstructor (\r
31 IN EFI_HANDLE ImageHandle,\r
32 IN EFI_SYSTEM_TABLE *SystemTable\r
33 )\r
34{\r
35 return EFI_SUCCESS;\r
36}\r
37\r
e386b444 38/**\r
39 Compare whether two names of languages are identical.\r
40\r
41 @param Language1 Name of language 1.\r
42 @param Language2 Name of language 2.\r
43\r
44 @retval TRUE Language 1 and language 2 are the same.\r
45 @retval FALSE Language 1 and language 2 are not the same.\r
46\r
47**/\r
e386b444 48BOOLEAN\r
49CompareIso639LanguageCode (\r
50 IN CONST CHAR8 *Language1,\r
51 IN CONST CHAR8 *Language2\r
52 )\r
53{\r
54 UINT32 Name1;\r
55 UINT32 Name2;\r
56\r
57 Name1 = ReadUnaligned24 ((CONST UINT32 *) Language1);\r
58 Name2 = ReadUnaligned24 ((CONST UINT32 *) Language2);\r
59\r
60 return (BOOLEAN) (Name1 == Name2);\r
61}\r
62\r
63/**\r
1d37ab9f 64 Retrieves a pointer to the system configuration table from the EFI System Table\r
65 based on a specified GUID.\r
9095d37b 66\r
1d37ab9f 67 This function searches the list of configuration tables stored in the EFI System Table\r
68 for a table with a GUID that matches TableGuid. If a match is found, then a pointer to\r
69 the configuration table is returned in Table., and EFI_SUCCESS is returned. If a matching GUID\r
70 is not found, then EFI_NOT_FOUND is returned.\r
9edc73ad 71 If TableGuid is NULL, then ASSERT().\r
72 If Table is NULL, then ASSERT().\r
e386b444 73\r
58380e9c 74 @param TableGuid The pointer to table's GUID type.\r
2fc59a00 75 @param Table The pointer to the table associated with TableGuid in the EFI System Table.\r
e386b444 76\r
77 @retval EFI_SUCCESS A configuration table matching TableGuid was found.\r
78 @retval EFI_NOT_FOUND A configuration table matching TableGuid could not be found.\r
79\r
80**/\r
81EFI_STATUS\r
82EFIAPI\r
83EfiGetSystemConfigurationTable (\r
84 IN EFI_GUID *TableGuid,\r
85 OUT VOID **Table\r
86 )\r
87{\r
88 EFI_SYSTEM_TABLE *SystemTable;\r
89 UINTN Index;\r
90\r
91 ASSERT (TableGuid != NULL);\r
92 ASSERT (Table != NULL);\r
93\r
94 SystemTable = gST;\r
95 *Table = NULL;\r
96 for (Index = 0; Index < SystemTable->NumberOfTableEntries; Index++) {\r
97 if (CompareGuid (TableGuid, &(SystemTable->ConfigurationTable[Index].VendorGuid))) {\r
98 *Table = SystemTable->ConfigurationTable[Index].VendorTable;\r
99 return EFI_SUCCESS;\r
100 }\r
101 }\r
102\r
103 return EFI_NOT_FOUND;\r
104}\r
105\r
106/**\r
1d37ab9f 107 Creates and returns a notification event and registers that event with all the protocol\r
108 instances specified by ProtocolGuid.\r
109\r
110 This function causes the notification function to be executed for every protocol of type\r
f0a8eeb2 111 ProtocolGuid instance that exists in the system when this function is invoked. If there are\r
112 no instances of ProtocolGuid in the handle database at the time this function is invoked,\r
113 then the notification function is still executed one time. In addition, every time a protocol\r
114 of type ProtocolGuid instance is installed or reinstalled, the notification function is also\r
9095d37b 115 executed. This function returns the notification event that was created.\r
1d37ab9f 116 If ProtocolGuid is NULL, then ASSERT().\r
117 If NotifyTpl is not a legal TPL value, then ASSERT().\r
118 If NotifyFunction is NULL, then ASSERT().\r
119 If Registration is NULL, then ASSERT().\r
e386b444 120\r
f0a8eeb2 121\r
e386b444 122 @param ProtocolGuid Supplies GUID of the protocol upon whose installation the event is fired.\r
123 @param NotifyTpl Supplies the task priority level of the event notifications.\r
124 @param NotifyFunction Supplies the function to notify when the event is signaled.\r
125 @param NotifyContext The context parameter to pass to NotifyFunction.\r
126 @param Registration A pointer to a memory location to receive the registration value.\r
1d37ab9f 127 This value is passed to LocateHandle() to obtain new handles that\r
9095d37b 128 have been added that support the ProtocolGuid-specified protocol.\r
e386b444 129\r
130 @return The notification event that was created.\r
131\r
132**/\r
133EFI_EVENT\r
134EFIAPI\r
135EfiCreateProtocolNotifyEvent(\r
136 IN EFI_GUID *ProtocolGuid,\r
137 IN EFI_TPL NotifyTpl,\r
138 IN EFI_EVENT_NOTIFY NotifyFunction,\r
139 IN VOID *NotifyContext, OPTIONAL\r
140 OUT VOID **Registration\r
141 )\r
142{\r
143 EFI_STATUS Status;\r
144 EFI_EVENT Event;\r
145\r
1d37ab9f 146 ASSERT (ProtocolGuid != NULL);\r
147 ASSERT (NotifyFunction != NULL);\r
148 ASSERT (Registration != NULL);\r
149\r
e386b444 150 //\r
151 // Create the event\r
152 //\r
153\r
154 Status = gBS->CreateEvent (\r
155 EVT_NOTIFY_SIGNAL,\r
156 NotifyTpl,\r
157 NotifyFunction,\r
158 NotifyContext,\r
159 &Event\r
160 );\r
161 ASSERT_EFI_ERROR (Status);\r
162\r
163 //\r
6d28c497 164 // Register for protocol notifications on this event\r
e386b444 165 //\r
166\r
167 Status = gBS->RegisterProtocolNotify (\r
168 ProtocolGuid,\r
169 Event,\r
170 Registration\r
171 );\r
172\r
173 ASSERT_EFI_ERROR (Status);\r
174\r
175 //\r
176 // Kick the event so we will perform an initial pass of\r
177 // current installed drivers\r
178 //\r
179\r
180 gBS->SignalEvent (Event);\r
181 return Event;\r
182}\r
183\r
184/**\r
1d37ab9f 185 Creates a named event that can be signaled with EfiNamedEventSignal().\r
186\r
e386b444 187 This function creates an event using NotifyTpl, NoifyFunction, and NotifyContext.\r
1d37ab9f 188 This event is signaled with EfiNamedEventSignal(). This provides the ability for one or more\r
9095d37b 189 listeners on the same event named by the GUID specified by Name.\r
9edc73ad 190 If Name is NULL, then ASSERT().\r
191 If NotifyTpl is not a legal TPL value, then ASSERT().\r
192 If NotifyFunction is NULL, then ASSERT().\r
e386b444 193\r
58380e9c 194 @param Name Supplies the GUID name of the event.\r
e386b444 195 @param NotifyTpl Supplies the task priority level of the event notifications.\r
196 @param NotifyFunction Supplies the function to notify when the event is signaled.\r
9095d37b 197 @param NotifyContext The context parameter to pass to NotifyFunction.\r
e386b444 198 @param Registration A pointer to a memory location to receive the registration value.\r
199\r
200 @retval EFI_SUCCESS A named event was created.\r
201 @retval EFI_OUT_OF_RESOURCES There are not enough resource to create the named event.\r
202\r
203**/\r
204EFI_STATUS\r
205EFIAPI\r
206EfiNamedEventListen (\r
207 IN CONST EFI_GUID *Name,\r
208 IN EFI_TPL NotifyTpl,\r
209 IN EFI_EVENT_NOTIFY NotifyFunction,\r
210 IN CONST VOID *NotifyContext, OPTIONAL\r
211 OUT VOID *Registration OPTIONAL\r
212 )\r
213{\r
214 EFI_STATUS Status;\r
215 EFI_EVENT Event;\r
216 VOID *RegistrationLocal;\r
217\r
9edc73ad 218 ASSERT (Name != NULL);\r
219 ASSERT (NotifyFunction != NULL);\r
220 ASSERT (NotifyTpl <= TPL_HIGH_LEVEL);\r
9095d37b 221\r
e386b444 222 //\r
223 // Create event\r
224 //\r
225 Status = gBS->CreateEvent (\r
226 EVT_NOTIFY_SIGNAL,\r
227 NotifyTpl,\r
228 NotifyFunction,\r
229 (VOID *) NotifyContext,\r
230 &Event\r
231 );\r
232 ASSERT_EFI_ERROR (Status);\r
233\r
234 //\r
235 // The Registration is not optional to RegisterProtocolNotify().\r
236 // To make it optional to EfiNamedEventListen(), may need to substitute with a local.\r
237 //\r
238 if (Registration != NULL) {\r
239 RegistrationLocal = Registration;\r
240 } else {\r
241 RegistrationLocal = &RegistrationLocal;\r
242 }\r
243\r
244 //\r
245 // Register for an installation of protocol interface\r
246 //\r
247\r
248 Status = gBS->RegisterProtocolNotify (\r
249 (EFI_GUID *) Name,\r
250 Event,\r
251 RegistrationLocal\r
252 );\r
253 ASSERT_EFI_ERROR (Status);\r
254\r
9edc73ad 255 return Status;\r
e386b444 256}\r
257\r
258/**\r
1d37ab9f 259 Signals a named event created with EfiNamedEventListen().\r
260\r
261 This function signals the named event specified by Name. The named event must have been\r
262 created with EfiNamedEventListen().\r
263 If Name is NULL, then ASSERT().\r
e386b444 264\r
58380e9c 265 @param Name Supplies the GUID name of the event.\r
e386b444 266\r
267 @retval EFI_SUCCESS A named event was signaled.\r
268 @retval EFI_OUT_OF_RESOURCES There are not enough resource to signal the named event.\r
269\r
270**/\r
271EFI_STATUS\r
272EFIAPI\r
273EfiNamedEventSignal (\r
274 IN CONST EFI_GUID *Name\r
275 )\r
276{\r
277 EFI_STATUS Status;\r
278 EFI_HANDLE Handle;\r
279\r
1d37ab9f 280 ASSERT(Name != NULL);\r
281\r
e386b444 282 Handle = NULL;\r
283 Status = gBS->InstallProtocolInterface (\r
284 &Handle,\r
285 (EFI_GUID *) Name,\r
286 EFI_NATIVE_INTERFACE,\r
287 NULL\r
288 );\r
289 ASSERT_EFI_ERROR (Status);\r
290\r
291 Status = gBS->UninstallProtocolInterface (\r
292 Handle,\r
293 (EFI_GUID *) Name,\r
294 NULL\r
295 );\r
296 ASSERT_EFI_ERROR (Status);\r
297\r
9edc73ad 298 return Status;\r
e386b444 299}\r
300\r
772fb7cb
LE
301/**\r
302 Signals an event group by placing a new event in the group temporarily and\r
303 signaling it.\r
304\r
305 @param[in] EventGroup Supplies the unique identifier of the event\r
306 group to signal.\r
307\r
308 @retval EFI_SUCCESS The event group was signaled successfully.\r
309 @retval EFI_INVALID_PARAMETER EventGroup is NULL.\r
310 @return Error codes that report problems about event\r
311 creation or signaling.\r
312**/\r
313EFI_STATUS\r
314EFIAPI\r
315EfiEventGroupSignal (\r
316 IN CONST EFI_GUID *EventGroup\r
317 )\r
318{\r
319 EFI_STATUS Status;\r
320 EFI_EVENT Event;\r
321\r
322 if (EventGroup == NULL) {\r
323 return EFI_INVALID_PARAMETER;\r
324 }\r
325\r
326 Status = gBS->CreateEventEx (\r
327 EVT_NOTIFY_SIGNAL,\r
328 TPL_CALLBACK,\r
1b197063 329 EfiEventEmptyFunction,\r
772fb7cb
LE
330 NULL,\r
331 EventGroup,\r
332 &Event\r
333 );\r
334 if (EFI_ERROR (Status)) {\r
335 return Status;\r
336 }\r
337\r
338 Status = gBS->SignalEvent (Event);\r
339 gBS->CloseEvent (Event);\r
340\r
341 return Status;\r
342}\r
343\r
1b197063
SZ
344/**\r
345 An empty function that can be used as NotifyFunction parameter of\r
346 CreateEvent() or CreateEventEx().\r
347\r
348 @param Event Event whose notification function is being invoked.\r
349 @param Context The pointer to the notification function's context,\r
350 which is implementation-dependent.\r
351\r
352**/\r
353VOID\r
354EFIAPI\r
355EfiEventEmptyFunction (\r
356 IN EFI_EVENT Event,\r
357 IN VOID *Context\r
358 )\r
359{\r
360}\r
361\r
9095d37b 362/**\r
e386b444 363 Returns the current TPL.\r
364\r
9095d37b
LG
365 This function returns the current TPL. There is no EFI service to directly\r
366 retrieve the current TPL. Instead, the RaiseTPL() function is used to raise\r
367 the TPL to TPL_HIGH_LEVEL. This will return the current TPL. The TPL level\r
368 can then immediately be restored back to the current TPL level with a call\r
e386b444 369 to RestoreTPL().\r
370\r
cf8ae2f6 371 @return The current TPL.\r
e386b444 372\r
373**/\r
374EFI_TPL\r
375EFIAPI\r
376EfiGetCurrentTpl (\r
377 VOID\r
378 )\r
379{\r
380 EFI_TPL Tpl;\r
381\r
382 Tpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r
383 gBS->RestoreTPL (Tpl);\r
384\r
385 return Tpl;\r
386}\r
387\r
388\r
389/**\r
1d37ab9f 390 Initializes a basic mutual exclusion lock.\r
391\r
9095d37b
LG
392 This function initializes a basic mutual exclusion lock to the released state\r
393 and returns the lock. Each lock provides mutual exclusion access at its task\r
e386b444 394 priority level. Since there is no preemption or multiprocessor support in EFI,\r
395 acquiring the lock only consists of raising to the locks TPL.\r
9edc73ad 396 If Lock is NULL, then ASSERT().\r
397 If Priority is not a valid TPL value, then ASSERT().\r
e386b444 398\r
399 @param Lock A pointer to the lock data structure to initialize.\r
58380e9c 400 @param Priority EFI TPL is associated with the lock.\r
e386b444 401\r
402 @return The lock.\r
403\r
404**/\r
405EFI_LOCK *\r
406EFIAPI\r
407EfiInitializeLock (\r
408 IN OUT EFI_LOCK *Lock,\r
409 IN EFI_TPL Priority\r
410 )\r
411{\r
412 ASSERT (Lock != NULL);\r
413 ASSERT (Priority <= TPL_HIGH_LEVEL);\r
414\r
415 Lock->Tpl = Priority;\r
416 Lock->OwnerTpl = TPL_APPLICATION;\r
417 Lock->Lock = EfiLockReleased ;\r
418 return Lock;\r
419}\r
420\r
421/**\r
1d37ab9f 422 Acquires ownership of a lock.\r
423\r
9095d37b
LG
424 This function raises the system's current task priority level to the task\r
425 priority level of the mutual exclusion lock. Then, it places the lock in the\r
e386b444 426 acquired state.\r
9edc73ad 427 If Lock is NULL, then ASSERT().\r
428 If Lock is not initialized, then ASSERT().\r
429 If Lock is already in the acquired state, then ASSERT().\r
e386b444 430\r
1d37ab9f 431 @param Lock A pointer to the lock to acquire.\r
e386b444 432\r
433**/\r
434VOID\r
435EFIAPI\r
436EfiAcquireLock (\r
437 IN EFI_LOCK *Lock\r
438 )\r
439{\r
440 ASSERT (Lock != NULL);\r
441 ASSERT (Lock->Lock == EfiLockReleased);\r
442\r
443 Lock->OwnerTpl = gBS->RaiseTPL (Lock->Tpl);\r
444 Lock->Lock = EfiLockAcquired;\r
445}\r
446\r
447/**\r
cf8ae2f6 448 Acquires ownership of a lock.\r
1d37ab9f 449\r
cf8ae2f6 450 This function raises the system's current task priority level to the task priority\r
451 level of the mutual exclusion lock. Then, it attempts to place the lock in the acquired state.\r
452 If the lock is already in the acquired state, then EFI_ACCESS_DENIED is returned.\r
453 Otherwise, EFI_SUCCESS is returned.\r
1d37ab9f 454 If Lock is NULL, then ASSERT().\r
455 If Lock is not initialized, then ASSERT().\r
e386b444 456\r
457 @param Lock A pointer to the lock to acquire.\r
458\r
459 @retval EFI_SUCCESS The lock was acquired.\r
460 @retval EFI_ACCESS_DENIED The lock could not be acquired because it is already owned.\r
461\r
462**/\r
463EFI_STATUS\r
464EFIAPI\r
465EfiAcquireLockOrFail (\r
466 IN EFI_LOCK *Lock\r
467 )\r
468{\r
469\r
470 ASSERT (Lock != NULL);\r
471 ASSERT (Lock->Lock != EfiLockUninitialized);\r
472\r
473 if (Lock->Lock == EfiLockAcquired) {\r
474 //\r
475 // Lock is already owned, so bail out\r
476 //\r
477 return EFI_ACCESS_DENIED;\r
478 }\r
479\r
480 Lock->OwnerTpl = gBS->RaiseTPL (Lock->Tpl);\r
481\r
482 Lock->Lock = EfiLockAcquired;\r
483\r
484 return EFI_SUCCESS;\r
485}\r
486\r
487/**\r
1d37ab9f 488 Releases ownership of a lock.\r
489\r
9095d37b
LG
490 This function transitions a mutual exclusion lock from the acquired state to\r
491 the released state, and restores the system's task priority level to its\r
e386b444 492 previous level.\r
1d37ab9f 493 If Lock is NULL, then ASSERT().\r
494 If Lock is not initialized, then ASSERT().\r
495 If Lock is already in the released state, then ASSERT().\r
e386b444 496\r
497 @param Lock A pointer to the lock to release.\r
498\r
499**/\r
500VOID\r
501EFIAPI\r
502EfiReleaseLock (\r
503 IN EFI_LOCK *Lock\r
504 )\r
505{\r
506 EFI_TPL Tpl;\r
507\r
508 ASSERT (Lock != NULL);\r
509 ASSERT (Lock->Lock == EfiLockAcquired);\r
510\r
511 Tpl = Lock->OwnerTpl;\r
512\r
513 Lock->Lock = EfiLockReleased;\r
514\r
515 gBS->RestoreTPL (Tpl);\r
516}\r
517\r
518/**\r
519 Tests whether a controller handle is being managed by a specific driver.\r
520\r
521 This function tests whether the driver specified by DriverBindingHandle is\r
522 currently managing the controller specified by ControllerHandle. This test\r
523 is performed by evaluating if the the protocol specified by ProtocolGuid is\r
524 present on ControllerHandle and is was opened by DriverBindingHandle with an\r
9095d37b 525 attribute of EFI_OPEN_PROTOCOL_BY_DRIVER.\r
e386b444 526 If ProtocolGuid is NULL, then ASSERT().\r
527\r
528 @param ControllerHandle A handle for a controller to test.\r
529 @param DriverBindingHandle Specifies the driver binding handle for the\r
530 driver.\r
531 @param ProtocolGuid Specifies the protocol that the driver specified\r
532 by DriverBindingHandle opens in its Start()\r
533 function.\r
534\r
535 @retval EFI_SUCCESS ControllerHandle is managed by the driver\r
28d3e14f 536 specified by DriverBindingHandle.\r
e386b444 537 @retval EFI_UNSUPPORTED ControllerHandle is not managed by the driver\r
28d3e14f 538 specified by DriverBindingHandle.\r
e386b444 539\r
540**/\r
541EFI_STATUS\r
542EFIAPI\r
543EfiTestManagedDevice (\r
544 IN CONST EFI_HANDLE ControllerHandle,\r
545 IN CONST EFI_HANDLE DriverBindingHandle,\r
546 IN CONST EFI_GUID *ProtocolGuid\r
547 )\r
548{\r
549 EFI_STATUS Status;\r
550 VOID *ManagedInterface;\r
551\r
552 ASSERT (ProtocolGuid != NULL);\r
553\r
554 Status = gBS->OpenProtocol (\r
555 ControllerHandle,\r
556 (EFI_GUID *) ProtocolGuid,\r
557 &ManagedInterface,\r
558 DriverBindingHandle,\r
559 ControllerHandle,\r
560 EFI_OPEN_PROTOCOL_BY_DRIVER\r
561 );\r
562 if (!EFI_ERROR (Status)) {\r
563 gBS->CloseProtocol (\r
564 ControllerHandle,\r
565 (EFI_GUID *) ProtocolGuid,\r
566 DriverBindingHandle,\r
567 ControllerHandle\r
568 );\r
569 return EFI_UNSUPPORTED;\r
570 }\r
571\r
572 if (Status != EFI_ALREADY_STARTED) {\r
573 return EFI_UNSUPPORTED;\r
574 }\r
575\r
576 return EFI_SUCCESS;\r
577}\r
578\r
579/**\r
580 Tests whether a child handle is a child device of the controller.\r
581\r
582 This function tests whether ChildHandle is one of the children of\r
583 ControllerHandle. This test is performed by checking to see if the protocol\r
584 specified by ProtocolGuid is present on ControllerHandle and opened by\r
585 ChildHandle with an attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.\r
586 If ProtocolGuid is NULL, then ASSERT().\r
587\r
9095d37b 588 @param ControllerHandle A handle for a (parent) controller to test.\r
e386b444 589 @param ChildHandle A child handle to test.\r
42eedea9 590 @param ProtocolGuid Supplies the protocol that the child controller\r
9095d37b 591 opens on its parent controller.\r
e386b444 592\r
593 @retval EFI_SUCCESS ChildHandle is a child of the ControllerHandle.\r
594 @retval EFI_UNSUPPORTED ChildHandle is not a child of the\r
595 ControllerHandle.\r
596\r
597**/\r
598EFI_STATUS\r
599EFIAPI\r
600EfiTestChildHandle (\r
601 IN CONST EFI_HANDLE ControllerHandle,\r
602 IN CONST EFI_HANDLE ChildHandle,\r
603 IN CONST EFI_GUID *ProtocolGuid\r
604 )\r
605{\r
606 EFI_STATUS Status;\r
607 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfoBuffer;\r
608 UINTN EntryCount;\r
609 UINTN Index;\r
610\r
611 ASSERT (ProtocolGuid != NULL);\r
612\r
613 //\r
614 // Retrieve the list of agents that are consuming the specific protocol\r
615 // on ControllerHandle.\r
616 //\r
617 Status = gBS->OpenProtocolInformation (\r
618 ControllerHandle,\r
619 (EFI_GUID *) ProtocolGuid,\r
620 &OpenInfoBuffer,\r
621 &EntryCount\r
622 );\r
623 if (EFI_ERROR (Status)) {\r
624 return EFI_UNSUPPORTED;\r
625 }\r
626\r
627 //\r
628 // Inspect if ChildHandle is one of the agents.\r
629 //\r
630 Status = EFI_UNSUPPORTED;\r
631 for (Index = 0; Index < EntryCount; Index++) {\r
632 if ((OpenInfoBuffer[Index].ControllerHandle == ChildHandle) &&\r
633 (OpenInfoBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {\r
634 Status = EFI_SUCCESS;\r
635 break;\r
636 }\r
637 }\r
638\r
639 FreePool (OpenInfoBuffer);\r
640 return Status;\r
641}\r
642\r
643/**\r
dd51a993 644 This function looks up a Unicode string in UnicodeStringTable.\r
dd51a993 645\r
cf8ae2f6 646 If Language is a member of SupportedLanguages and a Unicode string is found in\r
1d37ab9f 647 UnicodeStringTable that matches the language code specified by Language, then it\r
648 is returned in UnicodeString.\r
649\r
9095d37b 650 @param Language A pointer to the ISO 639-2 language code for the\r
1d37ab9f 651 Unicode string to look up and return.\r
9095d37b
LG
652 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes\r
653 that the Unicode string table supports. Language\r
1d37ab9f 654 must be a member of this set.\r
655 @param UnicodeStringTable A pointer to the table of Unicode strings.\r
656 @param UnicodeString A pointer to the Unicode string from UnicodeStringTable\r
657 that matches the language specified by Language.\r
658\r
9095d37b 659 @retval EFI_SUCCESS The Unicode string that matches the language\r
1d37ab9f 660 specified by Language was found\r
9095d37b 661 in the table of Unicode strings UnicodeStringTable,\r
1d37ab9f 662 and it was returned in UnicodeString.\r
663 @retval EFI_INVALID_PARAMETER Language is NULL.\r
664 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.\r
665 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.\r
666 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.\r
9095d37b 667 @retval EFI_UNSUPPORTED The language specified by Language is not a\r
1d37ab9f 668 member of SupportedLanguages.\r
9095d37b 669 @retval EFI_UNSUPPORTED The language specified by Language is not\r
1d37ab9f 670 supported by UnicodeStringTable.\r
e386b444 671\r
672**/\r
673EFI_STATUS\r
674EFIAPI\r
675LookupUnicodeString (\r
676 IN CONST CHAR8 *Language,\r
677 IN CONST CHAR8 *SupportedLanguages,\r
678 IN CONST EFI_UNICODE_STRING_TABLE *UnicodeStringTable,\r
679 OUT CHAR16 **UnicodeString\r
680 )\r
681{\r
682 //\r
683 // Make sure the parameters are valid\r
684 //\r
685 if (Language == NULL || UnicodeString == NULL) {\r
686 return EFI_INVALID_PARAMETER;\r
687 }\r
688\r
689 //\r
690 // If there are no supported languages, or the Unicode String Table is empty, then the\r
691 // Unicode String specified by Language is not supported by this Unicode String Table\r
692 //\r
693 if (SupportedLanguages == NULL || UnicodeStringTable == NULL) {\r
694 return EFI_UNSUPPORTED;\r
695 }\r
696\r
697 //\r
698 // Make sure Language is in the set of Supported Languages\r
699 //\r
700 while (*SupportedLanguages != 0) {\r
701 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
702\r
703 //\r
704 // Search the Unicode String Table for the matching Language specifier\r
705 //\r
706 while (UnicodeStringTable->Language != NULL) {\r
707 if (CompareIso639LanguageCode (Language, UnicodeStringTable->Language)) {\r
708\r
709 //\r
710 // A matching string was found, so return it\r
711 //\r
712 *UnicodeString = UnicodeStringTable->UnicodeString;\r
713 return EFI_SUCCESS;\r
714 }\r
715\r
716 UnicodeStringTable++;\r
717 }\r
718\r
719 return EFI_UNSUPPORTED;\r
720 }\r
721\r
722 SupportedLanguages += 3;\r
723 }\r
724\r
725 return EFI_UNSUPPORTED;\r
726}\r
727\r
dd51a993 728\r
729\r
e386b444 730/**\r
dd51a993 731 This function looks up a Unicode string in UnicodeStringTable.\r
cf8ae2f6 732\r
733 If Language is a member of SupportedLanguages and a Unicode string is found in\r
734 UnicodeStringTable that matches the language code specified by Language, then\r
735 it is returned in UnicodeString.\r
736\r
737 @param Language A pointer to an ASCII string containing the ISO 639-2 or the\r
738 RFC 4646 language code for the Unicode string to look up and\r
739 return. If Iso639Language is TRUE, then this ASCII string is\r
740 not assumed to be Null-terminated, and only the first three\r
28d3e14f 741 characters are used. If Iso639Language is FALSE, then this ASCII\r
9095d37b 742 string must be Null-terminated.\r
cf8ae2f6 743 @param SupportedLanguages A pointer to a Null-terminated ASCII string that contains a\r
744 set of ISO 639-2 or RFC 4646 language codes that the Unicode\r
745 string table supports. Language must be a member of this set.\r
746 If Iso639Language is TRUE, then this string contains one or more\r
747 ISO 639-2 language codes with no separator characters. If Iso639Language\r
748 is FALSE, then is string contains one or more RFC 4646 language\r
749 codes separated by ';'.\r
750 @param UnicodeStringTable A pointer to the table of Unicode strings. Type EFI_UNICODE_STRING_TABLE\r
751 is defined in "Related Definitions".\r
752 @param UnicodeString A pointer to the Null-terminated Unicode string from UnicodeStringTable\r
753 that matches the language specified by Language.\r
754 @param Iso639Language Specifies the supported language code format. If it is TRUE, then\r
755 Language and SupportedLanguages follow ISO 639-2 language code format.\r
756 Otherwise, they follow RFC 4646 language code format.\r
757\r
758\r
759 @retval EFI_SUCCESS The Unicode string that matches the language specified by Language\r
760 was found in the table of Unicode strings UnicodeStringTable, and\r
761 it was returned in UnicodeString.\r
9095d37b
LG
762 @retval EFI_INVALID_PARAMETER Language is NULL.\r
763 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.\r
764 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.\r
765 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.\r
766 @retval EFI_UNSUPPORTED The language specified by Language is not a member of SupportedLanguages.\r
cf8ae2f6 767 @retval EFI_UNSUPPORTED The language specified by Language is not supported by UnicodeStringTable.\r
dd51a993 768\r
769**/\r
770EFI_STATUS\r
771EFIAPI\r
772LookupUnicodeString2 (\r
773 IN CONST CHAR8 *Language,\r
774 IN CONST CHAR8 *SupportedLanguages,\r
775 IN CONST EFI_UNICODE_STRING_TABLE *UnicodeStringTable,\r
776 OUT CHAR16 **UnicodeString,\r
777 IN BOOLEAN Iso639Language\r
778 )\r
779{\r
780 BOOLEAN Found;\r
781 UINTN Index;\r
782 CHAR8 *LanguageString;\r
783\r
784 //\r
785 // Make sure the parameters are valid\r
786 //\r
787 if (Language == NULL || UnicodeString == NULL) {\r
788 return EFI_INVALID_PARAMETER;\r
789 }\r
790\r
791 //\r
792 // If there are no supported languages, or the Unicode String Table is empty, then the\r
793 // Unicode String specified by Language is not supported by this Unicode String Table\r
794 //\r
795 if (SupportedLanguages == NULL || UnicodeStringTable == NULL) {\r
796 return EFI_UNSUPPORTED;\r
797 }\r
798\r
799 //\r
800 // Make sure Language is in the set of Supported Languages\r
801 //\r
802 Found = FALSE;\r
803 while (*SupportedLanguages != 0) {\r
804 if (Iso639Language) {\r
805 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
806 Found = TRUE;\r
807 break;\r
808 }\r
809 SupportedLanguages += 3;\r
810 } else {\r
811 for (Index = 0; SupportedLanguages[Index] != 0 && SupportedLanguages[Index] != ';'; Index++);\r
634aa59d 812 if ((AsciiStrnCmp(SupportedLanguages, Language, Index) == 0) && (Language[Index] == 0)) {\r
dd51a993 813 Found = TRUE;\r
814 break;\r
815 }\r
816 SupportedLanguages += Index;\r
817 for (; *SupportedLanguages != 0 && *SupportedLanguages == ';'; SupportedLanguages++);\r
818 }\r
819 }\r
820\r
821 //\r
822 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED\r
823 //\r
824 if (!Found) {\r
825 return EFI_UNSUPPORTED;\r
826 }\r
827\r
828 //\r
829 // Search the Unicode String Table for the matching Language specifier\r
830 //\r
831 while (UnicodeStringTable->Language != NULL) {\r
832 LanguageString = UnicodeStringTable->Language;\r
833 while (0 != *LanguageString) {\r
834 for (Index = 0 ;LanguageString[Index] != 0 && LanguageString[Index] != ';'; Index++);\r
835 if (AsciiStrnCmp(LanguageString, Language, Index) == 0) {\r
836 *UnicodeString = UnicodeStringTable->UnicodeString;\r
837 return EFI_SUCCESS;\r
838 }\r
839 LanguageString += Index;\r
840 for (Index = 0 ;LanguageString[Index] != 0 && LanguageString[Index] == ';'; Index++);\r
841 }\r
842 UnicodeStringTable++;\r
843 }\r
844\r
845 return EFI_UNSUPPORTED;\r
846}\r
847\r
848\r
849/**\r
e386b444 850 This function adds a Unicode string to UnicodeStringTable.\r
1d37ab9f 851\r
9095d37b
LG
852 If Language is a member of SupportedLanguages then UnicodeString is added to\r
853 UnicodeStringTable. New buffers are allocated for both Language and\r
854 UnicodeString. The contents of Language and UnicodeString are copied into\r
855 these new buffers. These buffers are automatically freed when\r
e386b444 856 FreeUnicodeStringTable() is called.\r
857\r
9095d37b 858 @param Language A pointer to the ISO 639-2 language code for the Unicode\r
e386b444 859 string to add.\r
1d37ab9f 860 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes\r
861 that the Unicode string table supports.\r
862 Language must be a member of this set.\r
863 @param UnicodeStringTable A pointer to the table of Unicode strings.\r
864 @param UnicodeString A pointer to the Unicode string to add.\r
865\r
9095d37b
LG
866 @retval EFI_SUCCESS The Unicode string that matches the language\r
867 specified by Language was found in the table of\r
868 Unicode strings UnicodeStringTable, and it was\r
e386b444 869 returned in UnicodeString.\r
870 @retval EFI_INVALID_PARAMETER Language is NULL.\r
871 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.\r
872 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.\r
873 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.\r
9095d37b 874 @retval EFI_ALREADY_STARTED A Unicode string with language Language is\r
1d37ab9f 875 already present in UnicodeStringTable.\r
9095d37b 876 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another\r
1d37ab9f 877 Unicode string to UnicodeStringTable.\r
9095d37b 878 @retval EFI_UNSUPPORTED The language specified by Language is not a\r
1d37ab9f 879 member of SupportedLanguages.\r
e386b444 880\r
881**/\r
882EFI_STATUS\r
883EFIAPI\r
884AddUnicodeString (\r
5b9626e8
MH
885 IN CONST CHAR8 *Language,\r
886 IN CONST CHAR8 *SupportedLanguages,\r
887 IN OUT EFI_UNICODE_STRING_TABLE **UnicodeStringTable,\r
888 IN CONST CHAR16 *UnicodeString\r
e386b444 889 )\r
890{\r
891 UINTN NumberOfEntries;\r
892 EFI_UNICODE_STRING_TABLE *OldUnicodeStringTable;\r
893 EFI_UNICODE_STRING_TABLE *NewUnicodeStringTable;\r
894 UINTN UnicodeStringLength;\r
895\r
896 //\r
897 // Make sure the parameter are valid\r
898 //\r
899 if (Language == NULL || UnicodeString == NULL || UnicodeStringTable == NULL) {\r
900 return EFI_INVALID_PARAMETER;\r
901 }\r
902\r
903 //\r
904 // If there are no supported languages, then a Unicode String can not be added\r
905 //\r
906 if (SupportedLanguages == NULL) {\r
907 return EFI_UNSUPPORTED;\r
908 }\r
909\r
910 //\r
911 // If the Unicode String is empty, then a Unicode String can not be added\r
912 //\r
913 if (UnicodeString[0] == 0) {\r
914 return EFI_INVALID_PARAMETER;\r
915 }\r
916\r
917 //\r
918 // Make sure Language is a member of SupportedLanguages\r
919 //\r
920 while (*SupportedLanguages != 0) {\r
921 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
922\r
923 //\r
924 // Determine the size of the Unicode String Table by looking for a NULL Language entry\r
925 //\r
926 NumberOfEntries = 0;\r
927 if (*UnicodeStringTable != NULL) {\r
928 OldUnicodeStringTable = *UnicodeStringTable;\r
929 while (OldUnicodeStringTable->Language != NULL) {\r
930 if (CompareIso639LanguageCode (Language, OldUnicodeStringTable->Language)) {\r
931 return EFI_ALREADY_STARTED;\r
932 }\r
933\r
934 OldUnicodeStringTable++;\r
935 NumberOfEntries++;\r
936 }\r
937 }\r
938\r
939 //\r
940 // Allocate space for a new Unicode String Table. It must hold the current number of\r
941 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table\r
942 // marker\r
943 //\r
944 NewUnicodeStringTable = AllocatePool ((NumberOfEntries + 2) * sizeof (EFI_UNICODE_STRING_TABLE));\r
945 if (NewUnicodeStringTable == NULL) {\r
946 return EFI_OUT_OF_RESOURCES;\r
947 }\r
948\r
949 //\r
950 // If the current Unicode String Table contains any entries, then copy them to the\r
951 // newly allocated Unicode String Table.\r
952 //\r
953 if (*UnicodeStringTable != NULL) {\r
954 CopyMem (\r
955 NewUnicodeStringTable,\r
956 *UnicodeStringTable,\r
957 NumberOfEntries * sizeof (EFI_UNICODE_STRING_TABLE)\r
958 );\r
959 }\r
960\r
961 //\r
962 // Allocate space for a copy of the Language specifier\r
963 //\r
964 NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (3, Language);\r
965 if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) {\r
6389e32b 966 FreePool (NewUnicodeStringTable);\r
e386b444 967 return EFI_OUT_OF_RESOURCES;\r
968 }\r
969\r
970 //\r
971 // Compute the length of the Unicode String\r
972 //\r
973 for (UnicodeStringLength = 0; UnicodeString[UnicodeStringLength] != 0; UnicodeStringLength++)\r
974 ;\r
975\r
976 //\r
977 // Allocate space for a copy of the Unicode String\r
978 //\r
979 NewUnicodeStringTable[NumberOfEntries].UnicodeString = AllocateCopyPool (\r
980 (UnicodeStringLength + 1) * sizeof (CHAR16),\r
981 UnicodeString\r
982 );\r
983 if (NewUnicodeStringTable[NumberOfEntries].UnicodeString == NULL) {\r
6389e32b
LG
984 FreePool (NewUnicodeStringTable[NumberOfEntries].Language);\r
985 FreePool (NewUnicodeStringTable);\r
e386b444 986 return EFI_OUT_OF_RESOURCES;\r
987 }\r
988\r
989 //\r
990 // Mark the end of the Unicode String Table\r
991 //\r
992 NewUnicodeStringTable[NumberOfEntries + 1].Language = NULL;\r
993 NewUnicodeStringTable[NumberOfEntries + 1].UnicodeString = NULL;\r
994\r
995 //\r
996 // Free the old Unicode String Table\r
997 //\r
998 if (*UnicodeStringTable != NULL) {\r
6389e32b 999 FreePool (*UnicodeStringTable);\r
e386b444 1000 }\r
1001\r
1002 //\r
1003 // Point UnicodeStringTable at the newly allocated Unicode String Table\r
1004 //\r
1005 *UnicodeStringTable = NewUnicodeStringTable;\r
1006\r
1007 return EFI_SUCCESS;\r
1008 }\r
1009\r
1010 SupportedLanguages += 3;\r
1011 }\r
1012\r
1013 return EFI_UNSUPPORTED;\r
1014}\r
1015\r
dd51a993 1016\r
1017/**\r
cf8ae2f6 1018 This function adds the Null-terminated Unicode string specified by UnicodeString\r
1019 to UnicodeStringTable.\r
1020\r
1021 If Language is a member of SupportedLanguages then UnicodeString is added to\r
1022 UnicodeStringTable. New buffers are allocated for both Language and UnicodeString.\r
1023 The contents of Language and UnicodeString are copied into these new buffers.\r
1024 These buffers are automatically freed when EfiLibFreeUnicodeStringTable() is called.\r
1025\r
1026 @param Language A pointer to an ASCII string containing the ISO 639-2 or\r
1027 the RFC 4646 language code for the Unicode string to add.\r
1028 If Iso639Language is TRUE, then this ASCII string is not\r
1029 assumed to be Null-terminated, and only the first three\r
1030 chacters are used. If Iso639Language is FALSE, then this\r
1031 ASCII string must be Null-terminated.\r
1032 @param SupportedLanguages A pointer to a Null-terminated ASCII string that contains\r
1033 a set of ISO 639-2 or RFC 4646 language codes that the Unicode\r
1034 string table supports. Language must be a member of this set.\r
1035 If Iso639Language is TRUE, then this string contains one or more\r
1036 ISO 639-2 language codes with no separator characters.\r
1037 If Iso639Language is FALSE, then is string contains one or more\r
1038 RFC 4646 language codes separated by ';'.\r
1039 @param UnicodeStringTable A pointer to the table of Unicode strings. Type EFI_UNICODE_STRING_TABLE\r
1040 is defined in "Related Definitions".\r
9095d37b 1041 @param UnicodeString A pointer to the Unicode string to add.\r
cf8ae2f6 1042 @param Iso639Language Specifies the supported language code format. If it is TRUE,\r
1043 then Language and SupportedLanguages follow ISO 639-2 language code format.\r
1044 Otherwise, they follow RFC 4646 language code format.\r
1045\r
1046 @retval EFI_SUCCESS The Unicode string that matches the language specified by\r
1047 Language was found in the table of Unicode strings UnicodeStringTable,\r
9095d37b
LG
1048 and it was returned in UnicodeString.\r
1049 @retval EFI_INVALID_PARAMETER Language is NULL.\r
1050 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.\r
1051 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.\r
1052 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.\r
cf8ae2f6 1053 @retval EFI_ALREADY_STARTED A Unicode string with language Language is already present in\r
9095d37b
LG
1054 UnicodeStringTable.\r
1055 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another Unicode string UnicodeStringTable.\r
cf8ae2f6 1056 @retval EFI_UNSUPPORTED The language specified by Language is not a member of SupportedLanguages.\r
dd51a993 1057\r
1058**/\r
1059EFI_STATUS\r
1060EFIAPI\r
1061AddUnicodeString2 (\r
5b9626e8
MH
1062 IN CONST CHAR8 *Language,\r
1063 IN CONST CHAR8 *SupportedLanguages,\r
1064 IN OUT EFI_UNICODE_STRING_TABLE **UnicodeStringTable,\r
1065 IN CONST CHAR16 *UnicodeString,\r
1066 IN BOOLEAN Iso639Language\r
dd51a993 1067 )\r
1068{\r
1069 UINTN NumberOfEntries;\r
1070 EFI_UNICODE_STRING_TABLE *OldUnicodeStringTable;\r
1071 EFI_UNICODE_STRING_TABLE *NewUnicodeStringTable;\r
1072 UINTN UnicodeStringLength;\r
1073 BOOLEAN Found;\r
1074 UINTN Index;\r
1075 CHAR8 *LanguageString;\r
1076\r
1077 //\r
1078 // Make sure the parameter are valid\r
1079 //\r
1080 if (Language == NULL || UnicodeString == NULL || UnicodeStringTable == NULL) {\r
1081 return EFI_INVALID_PARAMETER;\r
1082 }\r
1083\r
1084 //\r
1085 // If there are no supported languages, then a Unicode String can not be added\r
1086 //\r
1087 if (SupportedLanguages == NULL) {\r
1088 return EFI_UNSUPPORTED;\r
1089 }\r
1090\r
1091 //\r
1092 // If the Unicode String is empty, then a Unicode String can not be added\r
1093 //\r
1094 if (UnicodeString[0] == 0) {\r
1095 return EFI_INVALID_PARAMETER;\r
1096 }\r
1097\r
1098 //\r
1099 // Make sure Language is a member of SupportedLanguages\r
1100 //\r
1101 Found = FALSE;\r
1102 while (*SupportedLanguages != 0) {\r
1103 if (Iso639Language) {\r
1104 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
1105 Found = TRUE;\r
1106 break;\r
1107 }\r
1108 SupportedLanguages += 3;\r
1109 } else {\r
1110 for (Index = 0; SupportedLanguages[Index] != 0 && SupportedLanguages[Index] != ';'; Index++);\r
1111 if (AsciiStrnCmp(SupportedLanguages, Language, Index) == 0) {\r
1112 Found = TRUE;\r
1113 break;\r
1114 }\r
1115 SupportedLanguages += Index;\r
1116 for (; *SupportedLanguages != 0 && *SupportedLanguages == ';'; SupportedLanguages++);\r
1117 }\r
1118 }\r
1119\r
1120 //\r
1121 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED\r
1122 //\r
1123 if (!Found) {\r
1124 return EFI_UNSUPPORTED;\r
1125 }\r
1126\r
1127 //\r
1128 // Determine the size of the Unicode String Table by looking for a NULL Language entry\r
1129 //\r
1130 NumberOfEntries = 0;\r
1131 if (*UnicodeStringTable != NULL) {\r
1132 OldUnicodeStringTable = *UnicodeStringTable;\r
1133 while (OldUnicodeStringTable->Language != NULL) {\r
1134 LanguageString = OldUnicodeStringTable->Language;\r
1135\r
42eedea9 1136 while (*LanguageString != 0) {\r
dd51a993 1137 for (Index = 0; LanguageString[Index] != 0 && LanguageString[Index] != ';'; Index++);\r
1138\r
9095d37b 1139 if (AsciiStrnCmp (Language, LanguageString, Index) == 0) {\r
dd51a993 1140 return EFI_ALREADY_STARTED;\r
1141 }\r
1142 LanguageString += Index;\r
1143 for (; *LanguageString != 0 && *LanguageString == ';'; LanguageString++);\r
1144 }\r
1145 OldUnicodeStringTable++;\r
1146 NumberOfEntries++;\r
1147 }\r
1148 }\r
1149\r
1150 //\r
1151 // Allocate space for a new Unicode String Table. It must hold the current number of\r
1152 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table\r
1153 // marker\r
1154 //\r
1155 NewUnicodeStringTable = AllocatePool ((NumberOfEntries + 2) * sizeof (EFI_UNICODE_STRING_TABLE));\r
1156 if (NewUnicodeStringTable == NULL) {\r
1157 return EFI_OUT_OF_RESOURCES;\r
1158 }\r
1159\r
1160 //\r
1161 // If the current Unicode String Table contains any entries, then copy them to the\r
1162 // newly allocated Unicode String Table.\r
1163 //\r
1164 if (*UnicodeStringTable != NULL) {\r
1165 CopyMem (\r
1166 NewUnicodeStringTable,\r
1167 *UnicodeStringTable,\r
1168 NumberOfEntries * sizeof (EFI_UNICODE_STRING_TABLE)\r
1169 );\r
1170 }\r
1171\r
1172 //\r
1173 // Allocate space for a copy of the Language specifier\r
1174 //\r
1175 NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (AsciiStrSize(Language), Language);\r
1176 if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) {\r
6389e32b 1177 FreePool (NewUnicodeStringTable);\r
dd51a993 1178 return EFI_OUT_OF_RESOURCES;\r
1179 }\r
1180\r
1181 //\r
1182 // Compute the length of the Unicode String\r
1183 //\r
1184 for (UnicodeStringLength = 0; UnicodeString[UnicodeStringLength] != 0; UnicodeStringLength++);\r
1185\r
1186 //\r
1187 // Allocate space for a copy of the Unicode String\r
1188 //\r
1189 NewUnicodeStringTable[NumberOfEntries].UnicodeString = AllocateCopyPool (StrSize (UnicodeString), UnicodeString);\r
1190 if (NewUnicodeStringTable[NumberOfEntries].UnicodeString == NULL) {\r
6389e32b
LG
1191 FreePool (NewUnicodeStringTable[NumberOfEntries].Language);\r
1192 FreePool (NewUnicodeStringTable);\r
dd51a993 1193 return EFI_OUT_OF_RESOURCES;\r
1194 }\r
1195\r
1196 //\r
1197 // Mark the end of the Unicode String Table\r
1198 //\r
1199 NewUnicodeStringTable[NumberOfEntries + 1].Language = NULL;\r
1200 NewUnicodeStringTable[NumberOfEntries + 1].UnicodeString = NULL;\r
1201\r
1202 //\r
1203 // Free the old Unicode String Table\r
1204 //\r
1205 if (*UnicodeStringTable != NULL) {\r
6389e32b 1206 FreePool (*UnicodeStringTable);\r
dd51a993 1207 }\r
1208\r
1209 //\r
1210 // Point UnicodeStringTable at the newly allocated Unicode String Table\r
1211 //\r
1212 *UnicodeStringTable = NewUnicodeStringTable;\r
1213\r
1214 return EFI_SUCCESS;\r
1215}\r
1216\r
e386b444 1217/**\r
1218 This function frees the table of Unicode strings in UnicodeStringTable.\r
1d37ab9f 1219\r
e386b444 1220 If UnicodeStringTable is NULL, then EFI_SUCCESS is returned.\r
9095d37b 1221 Otherwise, each language code, and each Unicode string in the Unicode string\r
e386b444 1222 table are freed, and EFI_SUCCESS is returned.\r
1223\r
1224 @param UnicodeStringTable A pointer to the table of Unicode strings.\r
1225\r
1226 @retval EFI_SUCCESS The Unicode string table was freed.\r
1227\r
1228**/\r
1229EFI_STATUS\r
1230EFIAPI\r
1231FreeUnicodeStringTable (\r
1232 IN EFI_UNICODE_STRING_TABLE *UnicodeStringTable\r
1233 )\r
1234{\r
1235 UINTN Index;\r
1236\r
1237 //\r
1238 // If the Unicode String Table is NULL, then it is already freed\r
1239 //\r
1240 if (UnicodeStringTable == NULL) {\r
1241 return EFI_SUCCESS;\r
1242 }\r
1243\r
1244 //\r
1245 // Loop through the Unicode String Table until we reach the end of table marker\r
1246 //\r
1247 for (Index = 0; UnicodeStringTable[Index].Language != NULL; Index++) {\r
1248\r
1249 //\r
1250 // Free the Language string from the Unicode String Table\r
1251 //\r
6389e32b 1252 FreePool (UnicodeStringTable[Index].Language);\r
e386b444 1253\r
1254 //\r
1255 // Free the Unicode String from the Unicode String Table\r
1256 //\r
1257 if (UnicodeStringTable[Index].UnicodeString != NULL) {\r
6389e32b 1258 FreePool (UnicodeStringTable[Index].UnicodeString);\r
e386b444 1259 }\r
1260 }\r
1261\r
1262 //\r
1263 // Free the Unicode String Table itself\r
1264 //\r
6389e32b 1265 FreePool (UnicodeStringTable);\r
e386b444 1266\r
1267 return EFI_SUCCESS;\r
1268}\r
6d28c497 1269\r
bf4a3dbd
ED
1270#ifndef DISABLE_NEW_DEPRECATED_INTERFACES\r
1271\r
6d28c497 1272/**\r
bf4a3dbd
ED
1273 [ATTENTION] This function will be deprecated for security reason.\r
1274\r
9095d37b
LG
1275 Returns a pointer to an allocated buffer that contains the contents of a\r
1276 variable retrieved through the UEFI Runtime Service GetVariable(). The\r
28d3e14f 1277 returned buffer is allocated using AllocatePool(). The caller is responsible\r
1278 for freeing this buffer with FreePool().\r
6d28c497 1279\r
28d3e14f 1280 If Name is NULL, then ASSERT().\r
1281 If Guid is NULL, then ASSERT().\r
6d28c497 1282\r
2fc59a00 1283 @param[in] Name The pointer to a Null-terminated Unicode string.\r
1284 @param[in] Guid The pointer to an EFI_GUID structure\r
6d28c497 1285\r
28d3e14f 1286 @retval NULL The variable could not be retrieved.\r
1287 @retval NULL There are not enough resources available for the variable contents.\r
1288 @retval Other A pointer to allocated buffer containing the variable contents.\r
6d28c497 1289\r
1290**/\r
1291VOID *\r
1292EFIAPI\r
1293GetVariable (\r
35db1186 1294 IN CONST CHAR16 *Name,\r
1295 IN CONST EFI_GUID *Guid\r
1296 )\r
6d28c497 1297{\r
1298 EFI_STATUS Status;\r
1299 UINTN Size;\r
1300 VOID *Value;\r
1301\r
1302 ASSERT (Name != NULL);\r
1303 ASSERT (Guid != NULL);\r
1304\r
1305 //\r
1306 // Try to get the variable size.\r
1307 //\r
1308 Value = NULL;\r
1309 Size = 0;\r
1310 Status = gRT->GetVariable ((CHAR16 *) Name, (EFI_GUID *) Guid, NULL, &Size, Value);\r
1311 if (Status != EFI_BUFFER_TOO_SMALL) {\r
1312 return NULL;\r
1313 }\r
1314\r
1315 //\r
1316 // Allocate buffer to get the variable.\r
1317 //\r
1318 Value = AllocatePool (Size);\r
1319 if (Value == NULL) {\r
1320 return NULL;\r
1321 }\r
1322\r
1323 //\r
1324 // Get the variable data.\r
1325 //\r
1326 Status = gRT->GetVariable ((CHAR16 *) Name, (EFI_GUID *) Guid, NULL, &Size, Value);\r
1327 if (EFI_ERROR (Status)) {\r
1328 FreePool(Value);\r
1329 return NULL;\r
1330 }\r
1331\r
1332 return Value;\r
1333}\r
1334\r
6d28c497 1335/**\r
bf4a3dbd
ED
1336 [ATTENTION] This function will be deprecated for security reason.\r
1337\r
9095d37b
LG
1338 Returns a pointer to an allocated buffer that contains the contents of a\r
1339 variable retrieved through the UEFI Runtime Service GetVariable(). This\r
6d28c497 1340 function always uses the EFI_GLOBAL_VARIABLE GUID to retrieve variables.\r
9095d37b 1341 The returned buffer is allocated using AllocatePool(). The caller is\r
6d28c497 1342 responsible for freeing this buffer with FreePool().\r
1343\r
1344 If Name is NULL, then ASSERT().\r
1345\r
2fc59a00 1346 @param[in] Name The pointer to a Null-terminated Unicode string.\r
6d28c497 1347\r
1348 @retval NULL The variable could not be retrieved.\r
1349 @retval NULL There are not enough resources available for the variable contents.\r
1350 @retval Other A pointer to allocated buffer containing the variable contents.\r
1351\r
1352**/\r
1353VOID *\r
1354EFIAPI\r
1355GetEfiGlobalVariable (\r
1356 IN CONST CHAR16 *Name\r
1357 )\r
1358{\r
1359 return GetVariable (Name, &gEfiGlobalVariableGuid);\r
1360}\r
bf4a3dbd
ED
1361#endif\r
1362\r
1363/**\r
9095d37b
LG
1364 Returns the status whether get the variable success. The function retrieves\r
1365 variable through the UEFI Runtime Service GetVariable(). The\r
bf4a3dbd
ED
1366 returned buffer is allocated using AllocatePool(). The caller is responsible\r
1367 for freeing this buffer with FreePool().\r
1368\r
1369 If Name is NULL, then ASSERT().\r
1370 If Guid is NULL, then ASSERT().\r
1371 If Value is NULL, then ASSERT().\r
1372\r
1373 @param[in] Name The pointer to a Null-terminated Unicode string.\r
1374 @param[in] Guid The pointer to an EFI_GUID structure\r
1375 @param[out] Value The buffer point saved the variable info.\r
1376 @param[out] Size The buffer size of the variable.\r
1377\r
1378 @return EFI_OUT_OF_RESOURCES Allocate buffer failed.\r
1379 @return EFI_SUCCESS Find the specified variable.\r
1380 @return Others Errors Return errors from call to gRT->GetVariable.\r
1381\r
1382**/\r
1383EFI_STATUS\r
1384EFIAPI\r
1385GetVariable2 (\r
1386 IN CONST CHAR16 *Name,\r
1387 IN CONST EFI_GUID *Guid,\r
1388 OUT VOID **Value,\r
1389 OUT UINTN *Size OPTIONAL\r
1390 )\r
1391{\r
1392 EFI_STATUS Status;\r
1393 UINTN BufferSize;\r
1394\r
1395 ASSERT (Name != NULL && Guid != NULL && Value != NULL);\r
1396\r
1397 //\r
1398 // Try to get the variable size.\r
1399 //\r
1400 BufferSize = 0;\r
1401 *Value = NULL;\r
1402 if (Size != NULL) {\r
1403 *Size = 0;\r
1404 }\r
9095d37b 1405\r
bf4a3dbd
ED
1406 Status = gRT->GetVariable ((CHAR16 *) Name, (EFI_GUID *) Guid, NULL, &BufferSize, *Value);\r
1407 if (Status != EFI_BUFFER_TOO_SMALL) {\r
1408 return Status;\r
1409 }\r
1410\r
1411 //\r
1412 // Allocate buffer to get the variable.\r
1413 //\r
1414 *Value = AllocatePool (BufferSize);\r
1415 ASSERT (*Value != NULL);\r
1416 if (*Value == NULL) {\r
1417 return EFI_OUT_OF_RESOURCES;\r
1418 }\r
1419\r
1420 //\r
1421 // Get the variable data.\r
1422 //\r
1423 Status = gRT->GetVariable ((CHAR16 *) Name, (EFI_GUID *) Guid, NULL, &BufferSize, *Value);\r
1424 if (EFI_ERROR (Status)) {\r
1425 FreePool(*Value);\r
1426 *Value = NULL;\r
1427 }\r
1428\r
1429 if (Size != NULL) {\r
1430 *Size = BufferSize;\r
1431 }\r
1432\r
37bf6787
BB
1433 return Status;\r
1434}\r
1435\r
1436/** Return the attributes of the variable.\r
1437\r
1438 Returns the status whether get the variable success. The function retrieves\r
1439 variable through the UEFI Runtime Service GetVariable(). The\r
1440 returned buffer is allocated using AllocatePool(). The caller is responsible\r
1441 for freeing this buffer with FreePool(). The attributes are returned if\r
1442 the caller provides a valid Attribute parameter.\r
1443\r
1444 If Name is NULL, then ASSERT().\r
1445 If Guid is NULL, then ASSERT().\r
1446 If Value is NULL, then ASSERT().\r
1447\r
1448 @param[in] Name The pointer to a Null-terminated Unicode string.\r
1449 @param[in] Guid The pointer to an EFI_GUID structure\r
1450 @param[out] Value The buffer point saved the variable info.\r
1451 @param[out] Size The buffer size of the variable.\r
1452 @param[out] Attr The pointer to the variable attributes as found in var store\r
1453\r
1454 @retval EFI_OUT_OF_RESOURCES Allocate buffer failed.\r
1455 @retval EFI_SUCCESS Find the specified variable.\r
1456 @retval Others Errors Return errors from call to gRT->GetVariable.\r
1457\r
1458**/\r
1459EFI_STATUS\r
1460EFIAPI\r
1461GetVariable3(\r
1462 IN CONST CHAR16 *Name,\r
1463 IN CONST EFI_GUID *Guid,\r
1464 OUT VOID **Value,\r
1465 OUT UINTN *Size OPTIONAL,\r
1466 OUT UINT32 *Attr OPTIONAL\r
1467 )\r
1468{\r
1469 EFI_STATUS Status;\r
1470 UINTN BufferSize;\r
1471\r
1472 ASSERT(Name != NULL && Guid != NULL && Value != NULL);\r
1473\r
1474 //\r
1475 // Try to get the variable size.\r
1476 //\r
1477 BufferSize = 0;\r
1478 *Value = NULL;\r
1479 if (Size != NULL) {\r
1480 *Size = 0;\r
1481 }\r
1482\r
1483 if (Attr != NULL) {\r
1484 *Attr = 0;\r
1485 }\r
1486\r
1487 Status = gRT->GetVariable((CHAR16 *)Name, (EFI_GUID *)Guid, Attr, &BufferSize, *Value);\r
1488 if (Status != EFI_BUFFER_TOO_SMALL) {\r
1489 return Status;\r
1490 }\r
1491\r
1492 //\r
1493 // Allocate buffer to get the variable.\r
1494 //\r
1495 *Value = AllocatePool(BufferSize);\r
1496 ASSERT(*Value != NULL);\r
1497 if (*Value == NULL) {\r
1498 return EFI_OUT_OF_RESOURCES;\r
1499 }\r
1500\r
1501 //\r
1502 // Get the variable data.\r
1503 //\r
1504 Status = gRT->GetVariable((CHAR16 *)Name, (EFI_GUID *)Guid, Attr, &BufferSize, *Value);\r
1505 if (EFI_ERROR(Status)) {\r
1506 FreePool(*Value);\r
1507 *Value = NULL;\r
1508 }\r
1509\r
1510 if (Size != NULL) {\r
1511 *Size = BufferSize;\r
1512 }\r
1513\r
bf4a3dbd
ED
1514 return Status;\r
1515}\r
6d28c497 1516\r
bf4a3dbd 1517/**\r
9095d37b
LG
1518 Returns a pointer to an allocated buffer that contains the contents of a\r
1519 variable retrieved through the UEFI Runtime Service GetVariable(). This\r
bf4a3dbd 1520 function always uses the EFI_GLOBAL_VARIABLE GUID to retrieve variables.\r
9095d37b 1521 The returned buffer is allocated using AllocatePool(). The caller is\r
bf4a3dbd
ED
1522 responsible for freeing this buffer with FreePool().\r
1523\r
1524 If Name is NULL, then ASSERT().\r
1525 If Value is NULL, then ASSERT().\r
1526\r
1527 @param[in] Name The pointer to a Null-terminated Unicode string.\r
1528 @param[out] Value The buffer point saved the variable info.\r
1529 @param[out] Size The buffer size of the variable.\r
1530\r
1531 @return EFI_OUT_OF_RESOURCES Allocate buffer failed.\r
1532 @return EFI_SUCCESS Find the specified variable.\r
1533 @return Others Errors Return errors from call to gRT->GetVariable.\r
1534\r
1535**/\r
1536EFI_STATUS\r
1537EFIAPI\r
1538GetEfiGlobalVariable2 (\r
1539 IN CONST CHAR16 *Name,\r
1540 OUT VOID **Value,\r
1541 OUT UINTN *Size OPTIONAL\r
1542 )\r
1543{\r
1544 return GetVariable2 (Name, &gEfiGlobalVariableGuid, Value, Size);\r
1545}\r
6d28c497 1546\r
1547/**\r
9095d37b
LG
1548 Returns a pointer to an allocated buffer that contains the best matching language\r
1549 from a set of supported languages.\r
1550\r
1551 This function supports both ISO 639-2 and RFC 4646 language codes, but language\r
1552 code types may not be mixed in a single call to this function. The language\r
1553 code returned is allocated using AllocatePool(). The caller is responsible for\r
6d28c497 1554 freeing the allocated buffer using FreePool(). This function supports a variable\r
9095d37b
LG
1555 argument list that allows the caller to pass in a prioritized list of language\r
1556 codes to test against all the language codes in SupportedLanguages.\r
6d28c497 1557\r
1558 If SupportedLanguages is NULL, then ASSERT().\r
1559\r
1560 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that\r
9095d37b 1561 contains a set of language codes in the format\r
6d28c497 1562 specified by Iso639Language.\r
3d7c6cfb
LG
1563 @param[in] Iso639Language If not zero, then all language codes are assumed to be\r
1564 in ISO 639-2 format. If zero, then all language\r
6d28c497 1565 codes are assumed to be in RFC 4646 language format\r
9095d37b 1566 @param[in] ... A variable argument list that contains pointers to\r
6d28c497 1567 Null-terminated ASCII strings that contain one or more\r
1568 language codes in the format specified by Iso639Language.\r
1569 The first language code from each of these language\r
1570 code lists is used to determine if it is an exact or\r
9095d37b 1571 close match to any of the language codes in\r
6d28c497 1572 SupportedLanguages. Close matches only apply to RFC 4646\r
1573 language codes, and the matching algorithm from RFC 4647\r
9095d37b 1574 is used to determine if a close match is present. If\r
6d28c497 1575 an exact or close match is found, then the matching\r
1576 language code from SupportedLanguages is returned. If\r
1577 no matches are found, then the next variable argument\r
9095d37b 1578 parameter is evaluated. The variable argument list\r
6d28c497 1579 is terminated by a NULL.\r
1580\r
1581 @retval NULL The best matching language could not be found in SupportedLanguages.\r
9095d37b 1582 @retval NULL There are not enough resources available to return the best matching\r
6d28c497 1583 language.\r
9095d37b 1584 @retval Other A pointer to a Null-terminated ASCII string that is the best matching\r
6d28c497 1585 language in SupportedLanguages.\r
1586\r
1587**/\r
1588CHAR8 *\r
1589EFIAPI\r
1590GetBestLanguage (\r
9095d37b 1591 IN CONST CHAR8 *SupportedLanguages,\r
d2aafe1e 1592 IN UINTN Iso639Language,\r
6d28c497 1593 ...\r
1594 )\r
1595{\r
1596 VA_LIST Args;\r
1597 CHAR8 *Language;\r
1598 UINTN CompareLength;\r
1599 UINTN LanguageLength;\r
1600 CONST CHAR8 *Supported;\r
1601 CHAR8 *BestLanguage;\r
1602\r
1603 ASSERT (SupportedLanguages != NULL);\r
1604\r
1605 VA_START (Args, Iso639Language);\r
1606 while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {\r
1607 //\r
1608 // Default to ISO 639-2 mode\r
1609 //\r
1610 CompareLength = 3;\r
1611 LanguageLength = MIN (3, AsciiStrLen (Language));\r
1612\r
1613 //\r
1614 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language\r
1615 //\r
3d7c6cfb 1616 if (Iso639Language == 0) {\r
6d28c497 1617 for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);\r
1618 }\r
1619\r
1620 //\r
1621 // Trim back the length of Language used until it is empty\r
1622 //\r
1623 while (LanguageLength > 0) {\r
1624 //\r
1625 // Loop through all language codes in SupportedLanguages\r
1626 //\r
1627 for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {\r
1628 //\r
1629 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages\r
1630 //\r
3d7c6cfb 1631 if (Iso639Language == 0) {\r
6d28c497 1632 //\r
1633 // Skip ';' characters in Supported\r
1634 //\r
1635 for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
1636 //\r
1637 // Determine the length of the next language code in Supported\r
1638 //\r
1639 for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);\r
1640 //\r
1641 // If Language is longer than the Supported, then skip to the next language\r
1642 //\r
1643 if (LanguageLength > CompareLength) {\r
1644 continue;\r
1645 }\r
1646 }\r
1647 //\r
1648 // See if the first LanguageLength characters in Supported match Language\r
1649 //\r
1650 if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {\r
1651 VA_END (Args);\r
1652 //\r
1653 // Allocate, copy, and return the best matching language code from SupportedLanguages\r
1654 //\r
1655 BestLanguage = AllocateZeroPool (CompareLength + 1);\r
1656 if (BestLanguage == NULL) {\r
1657 return NULL;\r
1658 }\r
1659 return CopyMem (BestLanguage, Supported, CompareLength);\r
1660 }\r
1661 }\r
1662\r
3d7c6cfb 1663 if (Iso639Language != 0) {\r
6d28c497 1664 //\r
1665 // If ISO 639 mode, then each language can only be tested once\r
1666 //\r
1667 LanguageLength = 0;\r
1668 } else {\r
1669 //\r
9095d37b 1670 // If RFC 4646 mode, then trim Language from the right to the next '-' character\r
6d28c497 1671 //\r
1672 for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);\r
1673 }\r
1674 }\r
1675 }\r
1676 VA_END (Args);\r
1677\r
1678 //\r
9095d37b 1679 // No matches were found\r
6d28c497 1680 //\r
1681 return NULL;\r
1682}\r
40070a18
MK
1683\r
1684/**\r
1685 Returns an array of protocol instance that matches the given protocol.\r
1686\r
1687 @param[in] Protocol Provides the protocol to search for.\r
1688 @param[out] NoProtocols The number of protocols returned in Buffer.\r
1689 @param[out] Buffer A pointer to the buffer to return the requested\r
1690 array of protocol instances that match Protocol.\r
1691 The returned buffer is allocated using\r
1692 EFI_BOOT_SERVICES.AllocatePool(). The caller is\r
1693 responsible for freeing this buffer with\r
1694 EFI_BOOT_SERVICES.FreePool().\r
1695\r
1696 @retval EFI_SUCCESS The array of protocols was returned in Buffer,\r
1697 and the number of protocols in Buffer was\r
1698 returned in NoProtocols.\r
1699 @retval EFI_NOT_FOUND No protocols found.\r
1700 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the\r
1701 matching results.\r
1702 @retval EFI_INVALID_PARAMETER Protocol is NULL.\r
1703 @retval EFI_INVALID_PARAMETER NoProtocols is NULL.\r
1704 @retval EFI_INVALID_PARAMETER Buffer is NULL.\r
1705\r
1706**/\r
1707EFI_STATUS\r
1708EFIAPI\r
1709EfiLocateProtocolBuffer (\r
1710 IN EFI_GUID *Protocol,\r
1711 OUT UINTN *NoProtocols,\r
1712 OUT VOID ***Buffer\r
1713 )\r
1714{\r
1715 EFI_STATUS Status;\r
1716 UINTN NoHandles;\r
1717 EFI_HANDLE *HandleBuffer;\r
1718 UINTN Index;\r
1719\r
1720 //\r
1721 // Check input parameters\r
1722 //\r
1723 if (Protocol == NULL || NoProtocols == NULL || Buffer == NULL) {\r
1724 return EFI_INVALID_PARAMETER;\r
1725 }\r
1726\r
1727 //\r
1728 // Initialze output parameters\r
1729 //\r
1730 *NoProtocols = 0;\r
1731 *Buffer = NULL;\r
1732\r
1733 //\r
1734 // Retrieve the array of handles that support Protocol\r
1735 //\r
1736 Status = gBS->LocateHandleBuffer (\r
1737 ByProtocol,\r
1738 Protocol,\r
1739 NULL,\r
1740 &NoHandles,\r
1741 &HandleBuffer\r
1742 );\r
1743 if (EFI_ERROR (Status)) {\r
1744 return Status;\r
1745 }\r
1746\r
1747 //\r
1748 // Allocate array of protocol instances\r
1749 //\r
1750 Status = gBS->AllocatePool (\r
1751 EfiBootServicesData,\r
1752 NoHandles * sizeof (VOID *),\r
1753 (VOID **)Buffer\r
1754 );\r
1755 if (EFI_ERROR (Status)) {\r
fe507283
SZ
1756 //\r
1757 // Free the handle buffer\r
1758 //\r
1759 gBS->FreePool (HandleBuffer);\r
40070a18
MK
1760 return EFI_OUT_OF_RESOURCES;\r
1761 }\r
1762 ZeroMem (*Buffer, NoHandles * sizeof (VOID *));\r
1763\r
1764 //\r
1765 // Lookup Protocol on each handle in HandleBuffer to fill in the array of\r
1766 // protocol instances. Handle case where protocol instance was present when\r
1767 // LocateHandleBuffer() was called, but is not present when HandleProtocol()\r
1768 // is called.\r
1769 //\r
1770 for (Index = 0, *NoProtocols = 0; Index < NoHandles; Index++) {\r
1771 Status = gBS->HandleProtocol (\r
1772 HandleBuffer[Index],\r
1773 Protocol,\r
1774 &((*Buffer)[*NoProtocols])\r
1775 );\r
1776 if (!EFI_ERROR (Status)) {\r
1777 (*NoProtocols)++;\r
1778 }\r
1779 }\r
1780\r
1781 //\r
1782 // Free the handle buffer\r
1783 //\r
1784 gBS->FreePool (HandleBuffer);\r
1785\r
1786 //\r
1787 // Make sure at least one protocol instance was found\r
1788 //\r
1789 if (*NoProtocols == 0) {\r
1790 gBS->FreePool (*Buffer);\r
1791 *Buffer = NULL;\r
1792 return EFI_NOT_FOUND;\r
1793 }\r
1794\r
1795 return EFI_SUCCESS;\r
1796}\r
768b6111
LE
1797\r
1798/**\r
1799 Open or create a file or directory, possibly creating the chain of\r
1800 directories leading up to the directory.\r
1801\r
1802 EfiOpenFileByDevicePath() first locates EFI_SIMPLE_FILE_SYSTEM_PROTOCOL on\r
1803 FilePath, and opens the root directory of that filesystem with\r
1804 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL.OpenVolume().\r
1805\r
1806 On the remaining device path, the longest initial sequence of\r
1807 FILEPATH_DEVICE_PATH nodes is node-wise traversed with\r
5dbc768f 1808 EFI_FILE_PROTOCOL.Open().\r
768b6111
LE
1809\r
1810 (As a consequence, if OpenMode includes EFI_FILE_MODE_CREATE, and Attributes\r
1811 includes EFI_FILE_DIRECTORY, and each FILEPATH_DEVICE_PATH specifies a single\r
1812 pathname component, then EfiOpenFileByDevicePath() ensures that the specified\r
1813 series of subdirectories exist on return.)\r
1814\r
1815 The EFI_FILE_PROTOCOL identified by the last FILEPATH_DEVICE_PATH node is\r
1816 output to the caller; intermediate EFI_FILE_PROTOCOL instances are closed. If\r
1817 there are no FILEPATH_DEVICE_PATH nodes past the node that identifies the\r
1818 filesystem, then the EFI_FILE_PROTOCOL of the root directory of the\r
1819 filesystem is output to the caller. If a device path node that is different\r
1820 from FILEPATH_DEVICE_PATH is encountered relative to the filesystem, the\r
1821 traversal is stopped with an error, and a NULL EFI_FILE_PROTOCOL is output.\r
1822\r
1823 @param[in,out] FilePath On input, the device path to the file or directory\r
1824 to open or create. The caller is responsible for\r
1825 ensuring that the device path pointed-to by FilePath\r
1826 is well-formed. On output, FilePath points one past\r
1827 the last node in the original device path that has\r
1828 been successfully processed. FilePath is set on\r
1829 output even if EfiOpenFileByDevicePath() returns an\r
1830 error.\r
1831\r
1832 @param[out] File On error, File is set to NULL. On success, File is\r
1833 set to the EFI_FILE_PROTOCOL of the root directory\r
1834 of the filesystem, if there are no\r
1835 FILEPATH_DEVICE_PATH nodes in FilePath; otherwise,\r
1836 File is set to the EFI_FILE_PROTOCOL identified by\r
1837 the last node in FilePath.\r
1838\r
1839 @param[in] OpenMode The OpenMode parameter to pass to\r
5dbc768f 1840 EFI_FILE_PROTOCOL.Open().\r
768b6111
LE
1841\r
1842 @param[in] Attributes The Attributes parameter to pass to\r
5dbc768f 1843 EFI_FILE_PROTOCOL.Open().\r
768b6111
LE
1844\r
1845 @retval EFI_SUCCESS The file or directory has been opened or\r
1846 created.\r
1847\r
1848 @retval EFI_INVALID_PARAMETER FilePath is NULL; or File is NULL; or FilePath\r
1849 contains a device path node, past the node\r
1850 that identifies\r
1851 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL, that is not a\r
1852 FILEPATH_DEVICE_PATH node.\r
1853\r
1854 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.\r
1855\r
1856 @return Error codes propagated from the\r
1857 LocateDevicePath() and OpenProtocol() boot\r
1858 services, and from the\r
1859 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL.OpenVolume()\r
1860 and EFI_FILE_PROTOCOL.Open() member functions.\r
1861**/\r
1862EFI_STATUS\r
1863EFIAPI\r
1864EfiOpenFileByDevicePath (\r
1865 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath,\r
1866 OUT EFI_FILE_PROTOCOL **File,\r
1867 IN UINT64 OpenMode,\r
1868 IN UINT64 Attributes\r
1869 )\r
1870{\r
1871 EFI_STATUS Status;\r
1872 EFI_HANDLE FileSystemHandle;\r
1873 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *FileSystem;\r
1874 EFI_FILE_PROTOCOL *LastFile;\r
1875 FILEPATH_DEVICE_PATH *FilePathNode;\r
1876 CHAR16 *AlignedPathName;\r
1877 CHAR16 *PathName;\r
1878 EFI_FILE_PROTOCOL *NextFile;\r
1879\r
1880 if (File == NULL) {\r
1881 return EFI_INVALID_PARAMETER;\r
1882 }\r
1883 *File = NULL;\r
1884\r
1885 if (FilePath == NULL) {\r
1886 return EFI_INVALID_PARAMETER;\r
1887 }\r
1888\r
1889 //\r
1890 // Look up the filesystem.\r
1891 //\r
1892 Status = gBS->LocateDevicePath (\r
1893 &gEfiSimpleFileSystemProtocolGuid,\r
1894 FilePath,\r
1895 &FileSystemHandle\r
1896 );\r
1897 if (EFI_ERROR (Status)) {\r
1898 return Status;\r
1899 }\r
1900 Status = gBS->OpenProtocol (\r
1901 FileSystemHandle,\r
1902 &gEfiSimpleFileSystemProtocolGuid,\r
1903 (VOID **)&FileSystem,\r
1904 gImageHandle,\r
1905 NULL,\r
1906 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
1907 );\r
1908 if (EFI_ERROR (Status)) {\r
1909 return Status;\r
1910 }\r
1911\r
1912 //\r
1913 // Open the root directory of the filesystem. After this operation succeeds,\r
1914 // we have to release LastFile on error.\r
1915 //\r
1916 Status = FileSystem->OpenVolume (FileSystem, &LastFile);\r
1917 if (EFI_ERROR (Status)) {\r
1918 return Status;\r
1919 }\r
1920\r
1921 //\r
1922 // Traverse the device path nodes relative to the filesystem.\r
1923 //\r
1924 while (!IsDevicePathEnd (*FilePath)) {\r
1925 if (DevicePathType (*FilePath) != MEDIA_DEVICE_PATH ||\r
1926 DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP) {\r
1927 Status = EFI_INVALID_PARAMETER;\r
1928 goto CloseLastFile;\r
1929 }\r
1930 FilePathNode = (FILEPATH_DEVICE_PATH *)*FilePath;\r
1931\r
1932 //\r
1933 // FilePathNode->PathName may be unaligned, and the UEFI specification\r
1934 // requires pointers that are passed to protocol member functions to be\r
1935 // aligned. Create an aligned copy of the pathname if necessary.\r
1936 //\r
1937 if ((UINTN)FilePathNode->PathName % sizeof *FilePathNode->PathName == 0) {\r
1938 AlignedPathName = NULL;\r
1939 PathName = FilePathNode->PathName;\r
1940 } else {\r
1941 AlignedPathName = AllocateCopyPool (\r
1942 (DevicePathNodeLength (FilePathNode) -\r
1943 SIZE_OF_FILEPATH_DEVICE_PATH),\r
1944 FilePathNode->PathName\r
1945 );\r
1946 if (AlignedPathName == NULL) {\r
1947 Status = EFI_OUT_OF_RESOURCES;\r
1948 goto CloseLastFile;\r
1949 }\r
1950 PathName = AlignedPathName;\r
1951 }\r
1952\r
1953 //\r
5dbc768f 1954 // Open or create the file corresponding to the next pathname fragment.\r
768b6111
LE
1955 //\r
1956 Status = LastFile->Open (\r
1957 LastFile,\r
1958 &NextFile,\r
1959 PathName,\r
5dbc768f
LE
1960 OpenMode,\r
1961 Attributes\r
768b6111
LE
1962 );\r
1963\r
768b6111
LE
1964 //\r
1965 // Release any AlignedPathName on both error and success paths; PathName is\r
1966 // no longer needed.\r
1967 //\r
1968 if (AlignedPathName != NULL) {\r
1969 FreePool (AlignedPathName);\r
1970 }\r
1971 if (EFI_ERROR (Status)) {\r
1972 goto CloseLastFile;\r
1973 }\r
1974\r
1975 //\r
1976 // Advance to the next device path node.\r
1977 //\r
1978 LastFile->Close (LastFile);\r
1979 LastFile = NextFile;\r
1980 *FilePath = NextDevicePathNode (FilePathNode);\r
1981 }\r
1982\r
1983 *File = LastFile;\r
1984 return EFI_SUCCESS;\r
1985\r
1986CloseLastFile:\r
1987 LastFile->Close (LastFile);\r
1988\r
1989 //\r
1990 // We are on the error path; we must have set an error Status for returning\r
1991 // to the caller.\r
1992 //\r
1993 ASSERT (EFI_ERROR (Status));\r
1994 return Status;\r
1995}\r