]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/UefiLib/UefiLib.c
MdePkg: Change OPTIONAL keyword usage style
[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
d0e2f823 139 IN VOID *NotifyContext OPTIONAL,\r
e386b444 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
d0e2f823 210 IN CONST VOID *NotifyContext OPTIONAL,\r
e386b444 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
ea331a5c 643/**\r
54a07f8f
SZ
644 This function checks the supported languages list for a target language,\r
645 This only supports RFC 4646 Languages.\r
646\r
647 @param SupportedLanguages The supported languages\r
648 @param TargetLanguage The target language\r
649\r
650 @retval Returns EFI_SUCCESS if the language is supported,\r
651 EFI_UNSUPPORTED otherwise\r
652**/\r
ea331a5c
TZ
653EFI_STATUS\r
654EFIAPI\r
655IsLanguageSupported (\r
656 IN CONST CHAR8 *SupportedLanguages,\r
657 IN CONST CHAR8 *TargetLanguage\r
658 )\r
659{\r
660 UINTN Index;\r
661 while (*SupportedLanguages != 0) {\r
662 for (Index = 0; SupportedLanguages[Index] != 0 && SupportedLanguages[Index] != ';'; Index++);\r
663 if ((AsciiStrnCmp(SupportedLanguages, TargetLanguage, Index) == 0) && (TargetLanguage[Index] == 0)) {\r
664 return EFI_SUCCESS;\r
665 }\r
666 SupportedLanguages += Index;\r
667 for (; *SupportedLanguages != 0 && *SupportedLanguages == ';'; SupportedLanguages++);\r
668 }\r
669\r
670 return EFI_UNSUPPORTED;\r
671}\r
672\r
e386b444 673/**\r
dd51a993 674 This function looks up a Unicode string in UnicodeStringTable.\r
dd51a993 675\r
cf8ae2f6 676 If Language is a member of SupportedLanguages and a Unicode string is found in\r
1d37ab9f 677 UnicodeStringTable that matches the language code specified by Language, then it\r
678 is returned in UnicodeString.\r
679\r
9095d37b 680 @param Language A pointer to the ISO 639-2 language code for the\r
1d37ab9f 681 Unicode string to look up and return.\r
9095d37b
LG
682 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes\r
683 that the Unicode string table supports. Language\r
1d37ab9f 684 must be a member of this set.\r
685 @param UnicodeStringTable A pointer to the table of Unicode strings.\r
686 @param UnicodeString A pointer to the Unicode string from UnicodeStringTable\r
687 that matches the language specified by Language.\r
688\r
9095d37b 689 @retval EFI_SUCCESS The Unicode string that matches the language\r
1d37ab9f 690 specified by Language was found\r
9095d37b 691 in the table of Unicode strings UnicodeStringTable,\r
1d37ab9f 692 and it was returned in UnicodeString.\r
693 @retval EFI_INVALID_PARAMETER Language is NULL.\r
694 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.\r
695 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.\r
696 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.\r
9095d37b 697 @retval EFI_UNSUPPORTED The language specified by Language is not a\r
1d37ab9f 698 member of SupportedLanguages.\r
9095d37b 699 @retval EFI_UNSUPPORTED The language specified by Language is not\r
1d37ab9f 700 supported by UnicodeStringTable.\r
e386b444 701\r
702**/\r
703EFI_STATUS\r
704EFIAPI\r
705LookupUnicodeString (\r
706 IN CONST CHAR8 *Language,\r
707 IN CONST CHAR8 *SupportedLanguages,\r
708 IN CONST EFI_UNICODE_STRING_TABLE *UnicodeStringTable,\r
709 OUT CHAR16 **UnicodeString\r
710 )\r
711{\r
712 //\r
713 // Make sure the parameters are valid\r
714 //\r
715 if (Language == NULL || UnicodeString == NULL) {\r
716 return EFI_INVALID_PARAMETER;\r
717 }\r
718\r
719 //\r
720 // If there are no supported languages, or the Unicode String Table is empty, then the\r
721 // Unicode String specified by Language is not supported by this Unicode String Table\r
722 //\r
723 if (SupportedLanguages == NULL || UnicodeStringTable == NULL) {\r
724 return EFI_UNSUPPORTED;\r
725 }\r
726\r
727 //\r
728 // Make sure Language is in the set of Supported Languages\r
729 //\r
730 while (*SupportedLanguages != 0) {\r
731 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
732\r
733 //\r
734 // Search the Unicode String Table for the matching Language specifier\r
735 //\r
736 while (UnicodeStringTable->Language != NULL) {\r
737 if (CompareIso639LanguageCode (Language, UnicodeStringTable->Language)) {\r
738\r
739 //\r
740 // A matching string was found, so return it\r
741 //\r
742 *UnicodeString = UnicodeStringTable->UnicodeString;\r
743 return EFI_SUCCESS;\r
744 }\r
745\r
746 UnicodeStringTable++;\r
747 }\r
748\r
749 return EFI_UNSUPPORTED;\r
750 }\r
751\r
752 SupportedLanguages += 3;\r
753 }\r
754\r
755 return EFI_UNSUPPORTED;\r
756}\r
757\r
dd51a993 758\r
759\r
e386b444 760/**\r
dd51a993 761 This function looks up a Unicode string in UnicodeStringTable.\r
cf8ae2f6 762\r
763 If Language is a member of SupportedLanguages and a Unicode string is found in\r
764 UnicodeStringTable that matches the language code specified by Language, then\r
765 it is returned in UnicodeString.\r
766\r
767 @param Language A pointer to an ASCII string containing the ISO 639-2 or the\r
768 RFC 4646 language code for the Unicode string to look up and\r
769 return. If Iso639Language is TRUE, then this ASCII string is\r
770 not assumed to be Null-terminated, and only the first three\r
28d3e14f 771 characters are used. If Iso639Language is FALSE, then this ASCII\r
9095d37b 772 string must be Null-terminated.\r
cf8ae2f6 773 @param SupportedLanguages A pointer to a Null-terminated ASCII string that contains a\r
774 set of ISO 639-2 or RFC 4646 language codes that the Unicode\r
775 string table supports. Language must be a member of this set.\r
776 If Iso639Language is TRUE, then this string contains one or more\r
777 ISO 639-2 language codes with no separator characters. If Iso639Language\r
778 is FALSE, then is string contains one or more RFC 4646 language\r
779 codes separated by ';'.\r
780 @param UnicodeStringTable A pointer to the table of Unicode strings. Type EFI_UNICODE_STRING_TABLE\r
781 is defined in "Related Definitions".\r
782 @param UnicodeString A pointer to the Null-terminated Unicode string from UnicodeStringTable\r
783 that matches the language specified by Language.\r
784 @param Iso639Language Specifies the supported language code format. If it is TRUE, then\r
785 Language and SupportedLanguages follow ISO 639-2 language code format.\r
786 Otherwise, they follow RFC 4646 language code format.\r
787\r
788\r
789 @retval EFI_SUCCESS The Unicode string that matches the language specified by Language\r
790 was found in the table of Unicode strings UnicodeStringTable, and\r
791 it was returned in UnicodeString.\r
9095d37b
LG
792 @retval EFI_INVALID_PARAMETER Language is NULL.\r
793 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.\r
794 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.\r
795 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.\r
796 @retval EFI_UNSUPPORTED The language specified by Language is not a member of SupportedLanguages.\r
cf8ae2f6 797 @retval EFI_UNSUPPORTED The language specified by Language is not supported by UnicodeStringTable.\r
dd51a993 798\r
799**/\r
800EFI_STATUS\r
801EFIAPI\r
802LookupUnicodeString2 (\r
803 IN CONST CHAR8 *Language,\r
804 IN CONST CHAR8 *SupportedLanguages,\r
805 IN CONST EFI_UNICODE_STRING_TABLE *UnicodeStringTable,\r
806 OUT CHAR16 **UnicodeString,\r
807 IN BOOLEAN Iso639Language\r
808 )\r
809{\r
810 BOOLEAN Found;\r
811 UINTN Index;\r
812 CHAR8 *LanguageString;\r
813\r
814 //\r
815 // Make sure the parameters are valid\r
816 //\r
817 if (Language == NULL || UnicodeString == NULL) {\r
818 return EFI_INVALID_PARAMETER;\r
819 }\r
820\r
821 //\r
822 // If there are no supported languages, or the Unicode String Table is empty, then the\r
823 // Unicode String specified by Language is not supported by this Unicode String Table\r
824 //\r
825 if (SupportedLanguages == NULL || UnicodeStringTable == NULL) {\r
826 return EFI_UNSUPPORTED;\r
827 }\r
828\r
829 //\r
830 // Make sure Language is in the set of Supported Languages\r
831 //\r
832 Found = FALSE;\r
ea331a5c
TZ
833 if (Iso639Language) {\r
834 while (*SupportedLanguages != 0) {\r
dd51a993 835 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
836 Found = TRUE;\r
837 break;\r
838 }\r
839 SupportedLanguages += 3;\r
dd51a993 840 }\r
ea331a5c 841 } else {\r
2e51b27f 842 Found = !IsLanguageSupported(SupportedLanguages, Language);\r
dd51a993 843 }\r
844\r
ea331a5c 845\r
dd51a993 846 //\r
847 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED\r
848 //\r
849 if (!Found) {\r
850 return EFI_UNSUPPORTED;\r
851 }\r
852\r
853 //\r
854 // Search the Unicode String Table for the matching Language specifier\r
855 //\r
856 while (UnicodeStringTable->Language != NULL) {\r
857 LanguageString = UnicodeStringTable->Language;\r
858 while (0 != *LanguageString) {\r
859 for (Index = 0 ;LanguageString[Index] != 0 && LanguageString[Index] != ';'; Index++);\r
860 if (AsciiStrnCmp(LanguageString, Language, Index) == 0) {\r
861 *UnicodeString = UnicodeStringTable->UnicodeString;\r
862 return EFI_SUCCESS;\r
863 }\r
864 LanguageString += Index;\r
865 for (Index = 0 ;LanguageString[Index] != 0 && LanguageString[Index] == ';'; Index++);\r
866 }\r
867 UnicodeStringTable++;\r
868 }\r
869\r
870 return EFI_UNSUPPORTED;\r
871}\r
872\r
873\r
874/**\r
e386b444 875 This function adds a Unicode string to UnicodeStringTable.\r
1d37ab9f 876\r
9095d37b
LG
877 If Language is a member of SupportedLanguages then UnicodeString is added to\r
878 UnicodeStringTable. New buffers are allocated for both Language and\r
879 UnicodeString. The contents of Language and UnicodeString are copied into\r
880 these new buffers. These buffers are automatically freed when\r
e386b444 881 FreeUnicodeStringTable() is called.\r
882\r
9095d37b 883 @param Language A pointer to the ISO 639-2 language code for the Unicode\r
e386b444 884 string to add.\r
1d37ab9f 885 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes\r
886 that the Unicode string table supports.\r
887 Language must be a member of this set.\r
888 @param UnicodeStringTable A pointer to the table of Unicode strings.\r
889 @param UnicodeString A pointer to the Unicode string to add.\r
890\r
9095d37b
LG
891 @retval EFI_SUCCESS The Unicode string that matches the language\r
892 specified by Language was found in the table of\r
893 Unicode strings UnicodeStringTable, and it was\r
e386b444 894 returned in UnicodeString.\r
895 @retval EFI_INVALID_PARAMETER Language is NULL.\r
896 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.\r
897 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.\r
898 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.\r
9095d37b 899 @retval EFI_ALREADY_STARTED A Unicode string with language Language is\r
1d37ab9f 900 already present in UnicodeStringTable.\r
9095d37b 901 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another\r
1d37ab9f 902 Unicode string to UnicodeStringTable.\r
9095d37b 903 @retval EFI_UNSUPPORTED The language specified by Language is not a\r
1d37ab9f 904 member of SupportedLanguages.\r
e386b444 905\r
906**/\r
907EFI_STATUS\r
908EFIAPI\r
909AddUnicodeString (\r
5b9626e8
MH
910 IN CONST CHAR8 *Language,\r
911 IN CONST CHAR8 *SupportedLanguages,\r
912 IN OUT EFI_UNICODE_STRING_TABLE **UnicodeStringTable,\r
913 IN CONST CHAR16 *UnicodeString\r
e386b444 914 )\r
915{\r
916 UINTN NumberOfEntries;\r
917 EFI_UNICODE_STRING_TABLE *OldUnicodeStringTable;\r
918 EFI_UNICODE_STRING_TABLE *NewUnicodeStringTable;\r
919 UINTN UnicodeStringLength;\r
920\r
921 //\r
922 // Make sure the parameter are valid\r
923 //\r
924 if (Language == NULL || UnicodeString == NULL || UnicodeStringTable == NULL) {\r
925 return EFI_INVALID_PARAMETER;\r
926 }\r
927\r
928 //\r
929 // If there are no supported languages, then a Unicode String can not be added\r
930 //\r
931 if (SupportedLanguages == NULL) {\r
932 return EFI_UNSUPPORTED;\r
933 }\r
934\r
935 //\r
936 // If the Unicode String is empty, then a Unicode String can not be added\r
937 //\r
938 if (UnicodeString[0] == 0) {\r
939 return EFI_INVALID_PARAMETER;\r
940 }\r
941\r
942 //\r
943 // Make sure Language is a member of SupportedLanguages\r
944 //\r
945 while (*SupportedLanguages != 0) {\r
946 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
947\r
948 //\r
949 // Determine the size of the Unicode String Table by looking for a NULL Language entry\r
950 //\r
951 NumberOfEntries = 0;\r
952 if (*UnicodeStringTable != NULL) {\r
953 OldUnicodeStringTable = *UnicodeStringTable;\r
954 while (OldUnicodeStringTable->Language != NULL) {\r
955 if (CompareIso639LanguageCode (Language, OldUnicodeStringTable->Language)) {\r
956 return EFI_ALREADY_STARTED;\r
957 }\r
958\r
959 OldUnicodeStringTable++;\r
960 NumberOfEntries++;\r
961 }\r
962 }\r
963\r
964 //\r
965 // Allocate space for a new Unicode String Table. It must hold the current number of\r
966 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table\r
967 // marker\r
968 //\r
969 NewUnicodeStringTable = AllocatePool ((NumberOfEntries + 2) * sizeof (EFI_UNICODE_STRING_TABLE));\r
970 if (NewUnicodeStringTable == NULL) {\r
971 return EFI_OUT_OF_RESOURCES;\r
972 }\r
973\r
974 //\r
975 // If the current Unicode String Table contains any entries, then copy them to the\r
976 // newly allocated Unicode String Table.\r
977 //\r
978 if (*UnicodeStringTable != NULL) {\r
979 CopyMem (\r
980 NewUnicodeStringTable,\r
981 *UnicodeStringTable,\r
982 NumberOfEntries * sizeof (EFI_UNICODE_STRING_TABLE)\r
983 );\r
984 }\r
985\r
986 //\r
987 // Allocate space for a copy of the Language specifier\r
988 //\r
989 NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (3, Language);\r
990 if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) {\r
6389e32b 991 FreePool (NewUnicodeStringTable);\r
e386b444 992 return EFI_OUT_OF_RESOURCES;\r
993 }\r
994\r
995 //\r
996 // Compute the length of the Unicode String\r
997 //\r
998 for (UnicodeStringLength = 0; UnicodeString[UnicodeStringLength] != 0; UnicodeStringLength++)\r
999 ;\r
1000\r
1001 //\r
1002 // Allocate space for a copy of the Unicode String\r
1003 //\r
1004 NewUnicodeStringTable[NumberOfEntries].UnicodeString = AllocateCopyPool (\r
1005 (UnicodeStringLength + 1) * sizeof (CHAR16),\r
1006 UnicodeString\r
1007 );\r
1008 if (NewUnicodeStringTable[NumberOfEntries].UnicodeString == NULL) {\r
6389e32b
LG
1009 FreePool (NewUnicodeStringTable[NumberOfEntries].Language);\r
1010 FreePool (NewUnicodeStringTable);\r
e386b444 1011 return EFI_OUT_OF_RESOURCES;\r
1012 }\r
1013\r
1014 //\r
1015 // Mark the end of the Unicode String Table\r
1016 //\r
1017 NewUnicodeStringTable[NumberOfEntries + 1].Language = NULL;\r
1018 NewUnicodeStringTable[NumberOfEntries + 1].UnicodeString = NULL;\r
1019\r
1020 //\r
1021 // Free the old Unicode String Table\r
1022 //\r
1023 if (*UnicodeStringTable != NULL) {\r
6389e32b 1024 FreePool (*UnicodeStringTable);\r
e386b444 1025 }\r
1026\r
1027 //\r
1028 // Point UnicodeStringTable at the newly allocated Unicode String Table\r
1029 //\r
1030 *UnicodeStringTable = NewUnicodeStringTable;\r
1031\r
1032 return EFI_SUCCESS;\r
1033 }\r
1034\r
1035 SupportedLanguages += 3;\r
1036 }\r
1037\r
1038 return EFI_UNSUPPORTED;\r
1039}\r
1040\r
dd51a993 1041\r
1042/**\r
cf8ae2f6 1043 This function adds the Null-terminated Unicode string specified by UnicodeString\r
1044 to UnicodeStringTable.\r
1045\r
1046 If Language is a member of SupportedLanguages then UnicodeString is added to\r
1047 UnicodeStringTable. New buffers are allocated for both Language and UnicodeString.\r
1048 The contents of Language and UnicodeString are copied into these new buffers.\r
1049 These buffers are automatically freed when EfiLibFreeUnicodeStringTable() is called.\r
1050\r
1051 @param Language A pointer to an ASCII string containing the ISO 639-2 or\r
1052 the RFC 4646 language code for the Unicode string to add.\r
1053 If Iso639Language is TRUE, then this ASCII string is not\r
1054 assumed to be Null-terminated, and only the first three\r
1055 chacters are used. If Iso639Language is FALSE, then this\r
1056 ASCII string must be Null-terminated.\r
1057 @param SupportedLanguages A pointer to a Null-terminated ASCII string that contains\r
1058 a set of ISO 639-2 or RFC 4646 language codes that the Unicode\r
1059 string table supports. Language must be a member of this set.\r
1060 If Iso639Language is TRUE, then this string contains one or more\r
1061 ISO 639-2 language codes with no separator characters.\r
1062 If Iso639Language is FALSE, then is string contains one or more\r
1063 RFC 4646 language codes separated by ';'.\r
1064 @param UnicodeStringTable A pointer to the table of Unicode strings. Type EFI_UNICODE_STRING_TABLE\r
1065 is defined in "Related Definitions".\r
9095d37b 1066 @param UnicodeString A pointer to the Unicode string to add.\r
cf8ae2f6 1067 @param Iso639Language Specifies the supported language code format. If it is TRUE,\r
1068 then Language and SupportedLanguages follow ISO 639-2 language code format.\r
1069 Otherwise, they follow RFC 4646 language code format.\r
1070\r
1071 @retval EFI_SUCCESS The Unicode string that matches the language specified by\r
1072 Language was found in the table of Unicode strings UnicodeStringTable,\r
9095d37b
LG
1073 and it was returned in UnicodeString.\r
1074 @retval EFI_INVALID_PARAMETER Language is NULL.\r
1075 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.\r
1076 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.\r
1077 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.\r
cf8ae2f6 1078 @retval EFI_ALREADY_STARTED A Unicode string with language Language is already present in\r
9095d37b
LG
1079 UnicodeStringTable.\r
1080 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another Unicode string UnicodeStringTable.\r
cf8ae2f6 1081 @retval EFI_UNSUPPORTED The language specified by Language is not a member of SupportedLanguages.\r
dd51a993 1082\r
1083**/\r
1084EFI_STATUS\r
1085EFIAPI\r
1086AddUnicodeString2 (\r
5b9626e8
MH
1087 IN CONST CHAR8 *Language,\r
1088 IN CONST CHAR8 *SupportedLanguages,\r
1089 IN OUT EFI_UNICODE_STRING_TABLE **UnicodeStringTable,\r
1090 IN CONST CHAR16 *UnicodeString,\r
1091 IN BOOLEAN Iso639Language\r
dd51a993 1092 )\r
1093{\r
1094 UINTN NumberOfEntries;\r
1095 EFI_UNICODE_STRING_TABLE *OldUnicodeStringTable;\r
1096 EFI_UNICODE_STRING_TABLE *NewUnicodeStringTable;\r
1097 UINTN UnicodeStringLength;\r
1098 BOOLEAN Found;\r
1099 UINTN Index;\r
1100 CHAR8 *LanguageString;\r
1101\r
1102 //\r
1103 // Make sure the parameter are valid\r
1104 //\r
1105 if (Language == NULL || UnicodeString == NULL || UnicodeStringTable == NULL) {\r
1106 return EFI_INVALID_PARAMETER;\r
1107 }\r
1108\r
1109 //\r
1110 // If there are no supported languages, then a Unicode String can not be added\r
1111 //\r
1112 if (SupportedLanguages == NULL) {\r
1113 return EFI_UNSUPPORTED;\r
1114 }\r
1115\r
1116 //\r
1117 // If the Unicode String is empty, then a Unicode String can not be added\r
1118 //\r
1119 if (UnicodeString[0] == 0) {\r
1120 return EFI_INVALID_PARAMETER;\r
1121 }\r
1122\r
1123 //\r
1124 // Make sure Language is a member of SupportedLanguages\r
1125 //\r
1126 Found = FALSE;\r
ea331a5c
TZ
1127 if (Iso639Language) {\r
1128 while (*SupportedLanguages != 0) {\r
dd51a993 1129 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
1130 Found = TRUE;\r
1131 break;\r
1132 }\r
1133 SupportedLanguages += 3;\r
dd51a993 1134 }\r
ea331a5c 1135 } else {\r
2e51b27f 1136 Found = !IsLanguageSupported(SupportedLanguages, Language);\r
dd51a993 1137 }\r
dd51a993 1138 //\r
1139 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED\r
1140 //\r
1141 if (!Found) {\r
1142 return EFI_UNSUPPORTED;\r
1143 }\r
1144\r
1145 //\r
1146 // Determine the size of the Unicode String Table by looking for a NULL Language entry\r
1147 //\r
1148 NumberOfEntries = 0;\r
1149 if (*UnicodeStringTable != NULL) {\r
1150 OldUnicodeStringTable = *UnicodeStringTable;\r
1151 while (OldUnicodeStringTable->Language != NULL) {\r
1152 LanguageString = OldUnicodeStringTable->Language;\r
1153\r
42eedea9 1154 while (*LanguageString != 0) {\r
dd51a993 1155 for (Index = 0; LanguageString[Index] != 0 && LanguageString[Index] != ';'; Index++);\r
1156\r
9095d37b 1157 if (AsciiStrnCmp (Language, LanguageString, Index) == 0) {\r
dd51a993 1158 return EFI_ALREADY_STARTED;\r
1159 }\r
1160 LanguageString += Index;\r
1161 for (; *LanguageString != 0 && *LanguageString == ';'; LanguageString++);\r
1162 }\r
1163 OldUnicodeStringTable++;\r
1164 NumberOfEntries++;\r
1165 }\r
1166 }\r
1167\r
1168 //\r
1169 // Allocate space for a new Unicode String Table. It must hold the current number of\r
1170 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table\r
1171 // marker\r
1172 //\r
1173 NewUnicodeStringTable = AllocatePool ((NumberOfEntries + 2) * sizeof (EFI_UNICODE_STRING_TABLE));\r
1174 if (NewUnicodeStringTable == NULL) {\r
1175 return EFI_OUT_OF_RESOURCES;\r
1176 }\r
1177\r
1178 //\r
1179 // If the current Unicode String Table contains any entries, then copy them to the\r
1180 // newly allocated Unicode String Table.\r
1181 //\r
1182 if (*UnicodeStringTable != NULL) {\r
1183 CopyMem (\r
1184 NewUnicodeStringTable,\r
1185 *UnicodeStringTable,\r
1186 NumberOfEntries * sizeof (EFI_UNICODE_STRING_TABLE)\r
1187 );\r
1188 }\r
1189\r
1190 //\r
1191 // Allocate space for a copy of the Language specifier\r
1192 //\r
1193 NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (AsciiStrSize(Language), Language);\r
1194 if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) {\r
6389e32b 1195 FreePool (NewUnicodeStringTable);\r
dd51a993 1196 return EFI_OUT_OF_RESOURCES;\r
1197 }\r
1198\r
1199 //\r
1200 // Compute the length of the Unicode String\r
1201 //\r
1202 for (UnicodeStringLength = 0; UnicodeString[UnicodeStringLength] != 0; UnicodeStringLength++);\r
1203\r
1204 //\r
1205 // Allocate space for a copy of the Unicode String\r
1206 //\r
1207 NewUnicodeStringTable[NumberOfEntries].UnicodeString = AllocateCopyPool (StrSize (UnicodeString), UnicodeString);\r
1208 if (NewUnicodeStringTable[NumberOfEntries].UnicodeString == NULL) {\r
6389e32b
LG
1209 FreePool (NewUnicodeStringTable[NumberOfEntries].Language);\r
1210 FreePool (NewUnicodeStringTable);\r
dd51a993 1211 return EFI_OUT_OF_RESOURCES;\r
1212 }\r
1213\r
1214 //\r
1215 // Mark the end of the Unicode String Table\r
1216 //\r
1217 NewUnicodeStringTable[NumberOfEntries + 1].Language = NULL;\r
1218 NewUnicodeStringTable[NumberOfEntries + 1].UnicodeString = NULL;\r
1219\r
1220 //\r
1221 // Free the old Unicode String Table\r
1222 //\r
1223 if (*UnicodeStringTable != NULL) {\r
6389e32b 1224 FreePool (*UnicodeStringTable);\r
dd51a993 1225 }\r
1226\r
1227 //\r
1228 // Point UnicodeStringTable at the newly allocated Unicode String Table\r
1229 //\r
1230 *UnicodeStringTable = NewUnicodeStringTable;\r
1231\r
1232 return EFI_SUCCESS;\r
1233}\r
1234\r
e386b444 1235/**\r
1236 This function frees the table of Unicode strings in UnicodeStringTable.\r
1d37ab9f 1237\r
e386b444 1238 If UnicodeStringTable is NULL, then EFI_SUCCESS is returned.\r
9095d37b 1239 Otherwise, each language code, and each Unicode string in the Unicode string\r
e386b444 1240 table are freed, and EFI_SUCCESS is returned.\r
1241\r
1242 @param UnicodeStringTable A pointer to the table of Unicode strings.\r
1243\r
1244 @retval EFI_SUCCESS The Unicode string table was freed.\r
1245\r
1246**/\r
1247EFI_STATUS\r
1248EFIAPI\r
1249FreeUnicodeStringTable (\r
1250 IN EFI_UNICODE_STRING_TABLE *UnicodeStringTable\r
1251 )\r
1252{\r
1253 UINTN Index;\r
1254\r
1255 //\r
1256 // If the Unicode String Table is NULL, then it is already freed\r
1257 //\r
1258 if (UnicodeStringTable == NULL) {\r
1259 return EFI_SUCCESS;\r
1260 }\r
1261\r
1262 //\r
1263 // Loop through the Unicode String Table until we reach the end of table marker\r
1264 //\r
1265 for (Index = 0; UnicodeStringTable[Index].Language != NULL; Index++) {\r
1266\r
1267 //\r
1268 // Free the Language string from the Unicode String Table\r
1269 //\r
6389e32b 1270 FreePool (UnicodeStringTable[Index].Language);\r
e386b444 1271\r
1272 //\r
1273 // Free the Unicode String from the Unicode String Table\r
1274 //\r
1275 if (UnicodeStringTable[Index].UnicodeString != NULL) {\r
6389e32b 1276 FreePool (UnicodeStringTable[Index].UnicodeString);\r
e386b444 1277 }\r
1278 }\r
1279\r
1280 //\r
1281 // Free the Unicode String Table itself\r
1282 //\r
6389e32b 1283 FreePool (UnicodeStringTable);\r
e386b444 1284\r
1285 return EFI_SUCCESS;\r
1286}\r
6d28c497 1287\r
bf4a3dbd
ED
1288\r
1289/**\r
9095d37b
LG
1290 Returns the status whether get the variable success. The function retrieves\r
1291 variable through the UEFI Runtime Service GetVariable(). The\r
bf4a3dbd
ED
1292 returned buffer is allocated using AllocatePool(). The caller is responsible\r
1293 for freeing this buffer with FreePool().\r
1294\r
1295 If Name is NULL, then ASSERT().\r
1296 If Guid is NULL, then ASSERT().\r
1297 If Value is NULL, then ASSERT().\r
1298\r
1299 @param[in] Name The pointer to a Null-terminated Unicode string.\r
1300 @param[in] Guid The pointer to an EFI_GUID structure\r
1301 @param[out] Value The buffer point saved the variable info.\r
1302 @param[out] Size The buffer size of the variable.\r
1303\r
1304 @return EFI_OUT_OF_RESOURCES Allocate buffer failed.\r
1305 @return EFI_SUCCESS Find the specified variable.\r
1306 @return Others Errors Return errors from call to gRT->GetVariable.\r
1307\r
1308**/\r
1309EFI_STATUS\r
1310EFIAPI\r
1311GetVariable2 (\r
1312 IN CONST CHAR16 *Name,\r
1313 IN CONST EFI_GUID *Guid,\r
1314 OUT VOID **Value,\r
1315 OUT UINTN *Size OPTIONAL\r
1316 )\r
1317{\r
1318 EFI_STATUS Status;\r
1319 UINTN BufferSize;\r
1320\r
1321 ASSERT (Name != NULL && Guid != NULL && Value != NULL);\r
1322\r
1323 //\r
1324 // Try to get the variable size.\r
1325 //\r
1326 BufferSize = 0;\r
1327 *Value = NULL;\r
1328 if (Size != NULL) {\r
1329 *Size = 0;\r
1330 }\r
9095d37b 1331\r
bf4a3dbd
ED
1332 Status = gRT->GetVariable ((CHAR16 *) Name, (EFI_GUID *) Guid, NULL, &BufferSize, *Value);\r
1333 if (Status != EFI_BUFFER_TOO_SMALL) {\r
1334 return Status;\r
1335 }\r
1336\r
1337 //\r
1338 // Allocate buffer to get the variable.\r
1339 //\r
1340 *Value = AllocatePool (BufferSize);\r
1341 ASSERT (*Value != NULL);\r
1342 if (*Value == NULL) {\r
1343 return EFI_OUT_OF_RESOURCES;\r
1344 }\r
1345\r
1346 //\r
1347 // Get the variable data.\r
1348 //\r
1349 Status = gRT->GetVariable ((CHAR16 *) Name, (EFI_GUID *) Guid, NULL, &BufferSize, *Value);\r
1350 if (EFI_ERROR (Status)) {\r
1351 FreePool(*Value);\r
1352 *Value = NULL;\r
1353 }\r
1354\r
1355 if (Size != NULL) {\r
1356 *Size = BufferSize;\r
1357 }\r
1358\r
37bf6787
BB
1359 return Status;\r
1360}\r
1361\r
1362/** Return the attributes of the variable.\r
1363\r
1364 Returns the status whether get the variable success. The function retrieves\r
1365 variable through the UEFI Runtime Service GetVariable(). The\r
1366 returned buffer is allocated using AllocatePool(). The caller is responsible\r
1367 for freeing this buffer with FreePool(). The attributes are returned if\r
1368 the caller provides a valid Attribute parameter.\r
1369\r
1370 If Name is NULL, then ASSERT().\r
1371 If Guid is NULL, then ASSERT().\r
1372 If Value is NULL, then ASSERT().\r
1373\r
1374 @param[in] Name The pointer to a Null-terminated Unicode string.\r
1375 @param[in] Guid The pointer to an EFI_GUID structure\r
1376 @param[out] Value The buffer point saved the variable info.\r
1377 @param[out] Size The buffer size of the variable.\r
1378 @param[out] Attr The pointer to the variable attributes as found in var store\r
1379\r
1380 @retval EFI_OUT_OF_RESOURCES Allocate buffer failed.\r
1381 @retval EFI_SUCCESS Find the specified variable.\r
1382 @retval Others Errors Return errors from call to gRT->GetVariable.\r
1383\r
1384**/\r
1385EFI_STATUS\r
1386EFIAPI\r
1387GetVariable3(\r
1388 IN CONST CHAR16 *Name,\r
1389 IN CONST EFI_GUID *Guid,\r
1390 OUT VOID **Value,\r
1391 OUT UINTN *Size OPTIONAL,\r
1392 OUT UINT32 *Attr OPTIONAL\r
1393 )\r
1394{\r
1395 EFI_STATUS Status;\r
1396 UINTN BufferSize;\r
1397\r
1398 ASSERT(Name != NULL && Guid != NULL && Value != NULL);\r
1399\r
1400 //\r
1401 // Try to get the variable size.\r
1402 //\r
1403 BufferSize = 0;\r
1404 *Value = NULL;\r
1405 if (Size != NULL) {\r
1406 *Size = 0;\r
1407 }\r
1408\r
1409 if (Attr != NULL) {\r
1410 *Attr = 0;\r
1411 }\r
1412\r
1413 Status = gRT->GetVariable((CHAR16 *)Name, (EFI_GUID *)Guid, Attr, &BufferSize, *Value);\r
1414 if (Status != EFI_BUFFER_TOO_SMALL) {\r
1415 return Status;\r
1416 }\r
1417\r
1418 //\r
1419 // Allocate buffer to get the variable.\r
1420 //\r
1421 *Value = AllocatePool(BufferSize);\r
1422 ASSERT(*Value != NULL);\r
1423 if (*Value == NULL) {\r
1424 return EFI_OUT_OF_RESOURCES;\r
1425 }\r
1426\r
1427 //\r
1428 // Get the variable data.\r
1429 //\r
1430 Status = gRT->GetVariable((CHAR16 *)Name, (EFI_GUID *)Guid, Attr, &BufferSize, *Value);\r
1431 if (EFI_ERROR(Status)) {\r
1432 FreePool(*Value);\r
1433 *Value = NULL;\r
1434 }\r
1435\r
1436 if (Size != NULL) {\r
1437 *Size = BufferSize;\r
1438 }\r
1439\r
bf4a3dbd
ED
1440 return Status;\r
1441}\r
6d28c497 1442\r
bf4a3dbd 1443/**\r
9095d37b
LG
1444 Returns a pointer to an allocated buffer that contains the contents of a\r
1445 variable retrieved through the UEFI Runtime Service GetVariable(). This\r
bf4a3dbd 1446 function always uses the EFI_GLOBAL_VARIABLE GUID to retrieve variables.\r
9095d37b 1447 The returned buffer is allocated using AllocatePool(). The caller is\r
bf4a3dbd
ED
1448 responsible for freeing this buffer with FreePool().\r
1449\r
1450 If Name is NULL, then ASSERT().\r
1451 If Value is NULL, then ASSERT().\r
1452\r
1453 @param[in] Name The pointer to a Null-terminated Unicode string.\r
1454 @param[out] Value The buffer point saved the variable info.\r
1455 @param[out] Size The buffer size of the variable.\r
1456\r
1457 @return EFI_OUT_OF_RESOURCES Allocate buffer failed.\r
1458 @return EFI_SUCCESS Find the specified variable.\r
1459 @return Others Errors Return errors from call to gRT->GetVariable.\r
1460\r
1461**/\r
1462EFI_STATUS\r
1463EFIAPI\r
1464GetEfiGlobalVariable2 (\r
1465 IN CONST CHAR16 *Name,\r
1466 OUT VOID **Value,\r
1467 OUT UINTN *Size OPTIONAL\r
1468 )\r
1469{\r
1470 return GetVariable2 (Name, &gEfiGlobalVariableGuid, Value, Size);\r
1471}\r
6d28c497 1472\r
1473/**\r
9095d37b
LG
1474 Returns a pointer to an allocated buffer that contains the best matching language\r
1475 from a set of supported languages.\r
1476\r
1477 This function supports both ISO 639-2 and RFC 4646 language codes, but language\r
1478 code types may not be mixed in a single call to this function. The language\r
1479 code returned is allocated using AllocatePool(). The caller is responsible for\r
6d28c497 1480 freeing the allocated buffer using FreePool(). This function supports a variable\r
9095d37b
LG
1481 argument list that allows the caller to pass in a prioritized list of language\r
1482 codes to test against all the language codes in SupportedLanguages.\r
6d28c497 1483\r
1484 If SupportedLanguages is NULL, then ASSERT().\r
1485\r
1486 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that\r
9095d37b 1487 contains a set of language codes in the format\r
6d28c497 1488 specified by Iso639Language.\r
3d7c6cfb
LG
1489 @param[in] Iso639Language If not zero, then all language codes are assumed to be\r
1490 in ISO 639-2 format. If zero, then all language\r
6d28c497 1491 codes are assumed to be in RFC 4646 language format\r
9095d37b 1492 @param[in] ... A variable argument list that contains pointers to\r
6d28c497 1493 Null-terminated ASCII strings that contain one or more\r
1494 language codes in the format specified by Iso639Language.\r
1495 The first language code from each of these language\r
1496 code lists is used to determine if it is an exact or\r
9095d37b 1497 close match to any of the language codes in\r
6d28c497 1498 SupportedLanguages. Close matches only apply to RFC 4646\r
1499 language codes, and the matching algorithm from RFC 4647\r
9095d37b 1500 is used to determine if a close match is present. If\r
6d28c497 1501 an exact or close match is found, then the matching\r
1502 language code from SupportedLanguages is returned. If\r
1503 no matches are found, then the next variable argument\r
9095d37b 1504 parameter is evaluated. The variable argument list\r
6d28c497 1505 is terminated by a NULL.\r
1506\r
1507 @retval NULL The best matching language could not be found in SupportedLanguages.\r
9095d37b 1508 @retval NULL There are not enough resources available to return the best matching\r
6d28c497 1509 language.\r
9095d37b 1510 @retval Other A pointer to a Null-terminated ASCII string that is the best matching\r
6d28c497 1511 language in SupportedLanguages.\r
1512\r
1513**/\r
1514CHAR8 *\r
1515EFIAPI\r
1516GetBestLanguage (\r
9095d37b 1517 IN CONST CHAR8 *SupportedLanguages,\r
d2aafe1e 1518 IN UINTN Iso639Language,\r
6d28c497 1519 ...\r
1520 )\r
1521{\r
1522 VA_LIST Args;\r
1523 CHAR8 *Language;\r
1524 UINTN CompareLength;\r
1525 UINTN LanguageLength;\r
1526 CONST CHAR8 *Supported;\r
1527 CHAR8 *BestLanguage;\r
1528\r
1529 ASSERT (SupportedLanguages != NULL);\r
1530\r
1531 VA_START (Args, Iso639Language);\r
1532 while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {\r
1533 //\r
1534 // Default to ISO 639-2 mode\r
1535 //\r
1536 CompareLength = 3;\r
1537 LanguageLength = MIN (3, AsciiStrLen (Language));\r
1538\r
1539 //\r
1540 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language\r
1541 //\r
3d7c6cfb 1542 if (Iso639Language == 0) {\r
6d28c497 1543 for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);\r
1544 }\r
1545\r
1546 //\r
1547 // Trim back the length of Language used until it is empty\r
1548 //\r
1549 while (LanguageLength > 0) {\r
1550 //\r
1551 // Loop through all language codes in SupportedLanguages\r
1552 //\r
1553 for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {\r
1554 //\r
1555 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages\r
1556 //\r
3d7c6cfb 1557 if (Iso639Language == 0) {\r
6d28c497 1558 //\r
1559 // Skip ';' characters in Supported\r
1560 //\r
1561 for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
1562 //\r
1563 // Determine the length of the next language code in Supported\r
1564 //\r
1565 for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);\r
1566 //\r
1567 // If Language is longer than the Supported, then skip to the next language\r
1568 //\r
1569 if (LanguageLength > CompareLength) {\r
1570 continue;\r
1571 }\r
1572 }\r
1573 //\r
1574 // See if the first LanguageLength characters in Supported match Language\r
1575 //\r
1576 if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {\r
1577 VA_END (Args);\r
1578 //\r
1579 // Allocate, copy, and return the best matching language code from SupportedLanguages\r
1580 //\r
1581 BestLanguage = AllocateZeroPool (CompareLength + 1);\r
1582 if (BestLanguage == NULL) {\r
1583 return NULL;\r
1584 }\r
1585 return CopyMem (BestLanguage, Supported, CompareLength);\r
1586 }\r
1587 }\r
1588\r
3d7c6cfb 1589 if (Iso639Language != 0) {\r
6d28c497 1590 //\r
1591 // If ISO 639 mode, then each language can only be tested once\r
1592 //\r
1593 LanguageLength = 0;\r
1594 } else {\r
1595 //\r
9095d37b 1596 // If RFC 4646 mode, then trim Language from the right to the next '-' character\r
6d28c497 1597 //\r
1598 for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);\r
1599 }\r
1600 }\r
1601 }\r
1602 VA_END (Args);\r
1603\r
1604 //\r
9095d37b 1605 // No matches were found\r
6d28c497 1606 //\r
1607 return NULL;\r
1608}\r
40070a18
MK
1609\r
1610/**\r
1611 Returns an array of protocol instance that matches the given protocol.\r
1612\r
1613 @param[in] Protocol Provides the protocol to search for.\r
1614 @param[out] NoProtocols The number of protocols returned in Buffer.\r
1615 @param[out] Buffer A pointer to the buffer to return the requested\r
1616 array of protocol instances that match Protocol.\r
1617 The returned buffer is allocated using\r
1618 EFI_BOOT_SERVICES.AllocatePool(). The caller is\r
1619 responsible for freeing this buffer with\r
1620 EFI_BOOT_SERVICES.FreePool().\r
1621\r
1622 @retval EFI_SUCCESS The array of protocols was returned in Buffer,\r
1623 and the number of protocols in Buffer was\r
1624 returned in NoProtocols.\r
1625 @retval EFI_NOT_FOUND No protocols found.\r
1626 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the\r
1627 matching results.\r
1628 @retval EFI_INVALID_PARAMETER Protocol is NULL.\r
1629 @retval EFI_INVALID_PARAMETER NoProtocols is NULL.\r
1630 @retval EFI_INVALID_PARAMETER Buffer is NULL.\r
1631\r
1632**/\r
1633EFI_STATUS\r
1634EFIAPI\r
1635EfiLocateProtocolBuffer (\r
1636 IN EFI_GUID *Protocol,\r
1637 OUT UINTN *NoProtocols,\r
1638 OUT VOID ***Buffer\r
1639 )\r
1640{\r
1641 EFI_STATUS Status;\r
1642 UINTN NoHandles;\r
1643 EFI_HANDLE *HandleBuffer;\r
1644 UINTN Index;\r
1645\r
1646 //\r
1647 // Check input parameters\r
1648 //\r
1649 if (Protocol == NULL || NoProtocols == NULL || Buffer == NULL) {\r
1650 return EFI_INVALID_PARAMETER;\r
1651 }\r
1652\r
1653 //\r
1654 // Initialze output parameters\r
1655 //\r
1656 *NoProtocols = 0;\r
1657 *Buffer = NULL;\r
1658\r
1659 //\r
1660 // Retrieve the array of handles that support Protocol\r
1661 //\r
1662 Status = gBS->LocateHandleBuffer (\r
1663 ByProtocol,\r
1664 Protocol,\r
1665 NULL,\r
1666 &NoHandles,\r
1667 &HandleBuffer\r
1668 );\r
1669 if (EFI_ERROR (Status)) {\r
1670 return Status;\r
1671 }\r
1672\r
1673 //\r
1674 // Allocate array of protocol instances\r
1675 //\r
1676 Status = gBS->AllocatePool (\r
1677 EfiBootServicesData,\r
1678 NoHandles * sizeof (VOID *),\r
1679 (VOID **)Buffer\r
1680 );\r
1681 if (EFI_ERROR (Status)) {\r
fe507283
SZ
1682 //\r
1683 // Free the handle buffer\r
1684 //\r
1685 gBS->FreePool (HandleBuffer);\r
40070a18
MK
1686 return EFI_OUT_OF_RESOURCES;\r
1687 }\r
1688 ZeroMem (*Buffer, NoHandles * sizeof (VOID *));\r
1689\r
1690 //\r
1691 // Lookup Protocol on each handle in HandleBuffer to fill in the array of\r
1692 // protocol instances. Handle case where protocol instance was present when\r
1693 // LocateHandleBuffer() was called, but is not present when HandleProtocol()\r
1694 // is called.\r
1695 //\r
1696 for (Index = 0, *NoProtocols = 0; Index < NoHandles; Index++) {\r
1697 Status = gBS->HandleProtocol (\r
1698 HandleBuffer[Index],\r
1699 Protocol,\r
1700 &((*Buffer)[*NoProtocols])\r
1701 );\r
1702 if (!EFI_ERROR (Status)) {\r
1703 (*NoProtocols)++;\r
1704 }\r
1705 }\r
1706\r
1707 //\r
1708 // Free the handle buffer\r
1709 //\r
1710 gBS->FreePool (HandleBuffer);\r
1711\r
1712 //\r
1713 // Make sure at least one protocol instance was found\r
1714 //\r
1715 if (*NoProtocols == 0) {\r
1716 gBS->FreePool (*Buffer);\r
1717 *Buffer = NULL;\r
1718 return EFI_NOT_FOUND;\r
1719 }\r
1720\r
1721 return EFI_SUCCESS;\r
1722}\r
768b6111
LE
1723\r
1724/**\r
1725 Open or create a file or directory, possibly creating the chain of\r
1726 directories leading up to the directory.\r
1727\r
1728 EfiOpenFileByDevicePath() first locates EFI_SIMPLE_FILE_SYSTEM_PROTOCOL on\r
1729 FilePath, and opens the root directory of that filesystem with\r
1730 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL.OpenVolume().\r
1731\r
1732 On the remaining device path, the longest initial sequence of\r
1733 FILEPATH_DEVICE_PATH nodes is node-wise traversed with\r
5dbc768f 1734 EFI_FILE_PROTOCOL.Open().\r
768b6111
LE
1735\r
1736 (As a consequence, if OpenMode includes EFI_FILE_MODE_CREATE, and Attributes\r
1737 includes EFI_FILE_DIRECTORY, and each FILEPATH_DEVICE_PATH specifies a single\r
1738 pathname component, then EfiOpenFileByDevicePath() ensures that the specified\r
1739 series of subdirectories exist on return.)\r
1740\r
1741 The EFI_FILE_PROTOCOL identified by the last FILEPATH_DEVICE_PATH node is\r
1742 output to the caller; intermediate EFI_FILE_PROTOCOL instances are closed. If\r
1743 there are no FILEPATH_DEVICE_PATH nodes past the node that identifies the\r
1744 filesystem, then the EFI_FILE_PROTOCOL of the root directory of the\r
1745 filesystem is output to the caller. If a device path node that is different\r
1746 from FILEPATH_DEVICE_PATH is encountered relative to the filesystem, the\r
1747 traversal is stopped with an error, and a NULL EFI_FILE_PROTOCOL is output.\r
1748\r
1749 @param[in,out] FilePath On input, the device path to the file or directory\r
1750 to open or create. The caller is responsible for\r
1751 ensuring that the device path pointed-to by FilePath\r
1752 is well-formed. On output, FilePath points one past\r
1753 the last node in the original device path that has\r
1754 been successfully processed. FilePath is set on\r
1755 output even if EfiOpenFileByDevicePath() returns an\r
1756 error.\r
1757\r
1758 @param[out] File On error, File is set to NULL. On success, File is\r
1759 set to the EFI_FILE_PROTOCOL of the root directory\r
1760 of the filesystem, if there are no\r
1761 FILEPATH_DEVICE_PATH nodes in FilePath; otherwise,\r
1762 File is set to the EFI_FILE_PROTOCOL identified by\r
1763 the last node in FilePath.\r
1764\r
1765 @param[in] OpenMode The OpenMode parameter to pass to\r
5dbc768f 1766 EFI_FILE_PROTOCOL.Open().\r
768b6111
LE
1767\r
1768 @param[in] Attributes The Attributes parameter to pass to\r
5dbc768f 1769 EFI_FILE_PROTOCOL.Open().\r
768b6111
LE
1770\r
1771 @retval EFI_SUCCESS The file or directory has been opened or\r
1772 created.\r
1773\r
1774 @retval EFI_INVALID_PARAMETER FilePath is NULL; or File is NULL; or FilePath\r
1775 contains a device path node, past the node\r
1776 that identifies\r
1777 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL, that is not a\r
1778 FILEPATH_DEVICE_PATH node.\r
1779\r
1780 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.\r
1781\r
1782 @return Error codes propagated from the\r
1783 LocateDevicePath() and OpenProtocol() boot\r
1784 services, and from the\r
1785 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL.OpenVolume()\r
1786 and EFI_FILE_PROTOCOL.Open() member functions.\r
1787**/\r
1788EFI_STATUS\r
1789EFIAPI\r
1790EfiOpenFileByDevicePath (\r
1791 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath,\r
1792 OUT EFI_FILE_PROTOCOL **File,\r
1793 IN UINT64 OpenMode,\r
1794 IN UINT64 Attributes\r
1795 )\r
1796{\r
1797 EFI_STATUS Status;\r
1798 EFI_HANDLE FileSystemHandle;\r
1799 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *FileSystem;\r
1800 EFI_FILE_PROTOCOL *LastFile;\r
1801 FILEPATH_DEVICE_PATH *FilePathNode;\r
1802 CHAR16 *AlignedPathName;\r
1803 CHAR16 *PathName;\r
1804 EFI_FILE_PROTOCOL *NextFile;\r
1805\r
1806 if (File == NULL) {\r
1807 return EFI_INVALID_PARAMETER;\r
1808 }\r
1809 *File = NULL;\r
1810\r
1811 if (FilePath == NULL) {\r
1812 return EFI_INVALID_PARAMETER;\r
1813 }\r
1814\r
1815 //\r
1816 // Look up the filesystem.\r
1817 //\r
1818 Status = gBS->LocateDevicePath (\r
1819 &gEfiSimpleFileSystemProtocolGuid,\r
1820 FilePath,\r
1821 &FileSystemHandle\r
1822 );\r
1823 if (EFI_ERROR (Status)) {\r
1824 return Status;\r
1825 }\r
1826 Status = gBS->OpenProtocol (\r
1827 FileSystemHandle,\r
1828 &gEfiSimpleFileSystemProtocolGuid,\r
1829 (VOID **)&FileSystem,\r
1830 gImageHandle,\r
1831 NULL,\r
1832 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
1833 );\r
1834 if (EFI_ERROR (Status)) {\r
1835 return Status;\r
1836 }\r
1837\r
1838 //\r
1839 // Open the root directory of the filesystem. After this operation succeeds,\r
1840 // we have to release LastFile on error.\r
1841 //\r
1842 Status = FileSystem->OpenVolume (FileSystem, &LastFile);\r
1843 if (EFI_ERROR (Status)) {\r
1844 return Status;\r
1845 }\r
1846\r
1847 //\r
1848 // Traverse the device path nodes relative to the filesystem.\r
1849 //\r
1850 while (!IsDevicePathEnd (*FilePath)) {\r
1851 if (DevicePathType (*FilePath) != MEDIA_DEVICE_PATH ||\r
1852 DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP) {\r
1853 Status = EFI_INVALID_PARAMETER;\r
1854 goto CloseLastFile;\r
1855 }\r
1856 FilePathNode = (FILEPATH_DEVICE_PATH *)*FilePath;\r
1857\r
1858 //\r
1859 // FilePathNode->PathName may be unaligned, and the UEFI specification\r
1860 // requires pointers that are passed to protocol member functions to be\r
1861 // aligned. Create an aligned copy of the pathname if necessary.\r
1862 //\r
1863 if ((UINTN)FilePathNode->PathName % sizeof *FilePathNode->PathName == 0) {\r
1864 AlignedPathName = NULL;\r
1865 PathName = FilePathNode->PathName;\r
1866 } else {\r
1867 AlignedPathName = AllocateCopyPool (\r
1868 (DevicePathNodeLength (FilePathNode) -\r
1869 SIZE_OF_FILEPATH_DEVICE_PATH),\r
1870 FilePathNode->PathName\r
1871 );\r
1872 if (AlignedPathName == NULL) {\r
1873 Status = EFI_OUT_OF_RESOURCES;\r
1874 goto CloseLastFile;\r
1875 }\r
1876 PathName = AlignedPathName;\r
1877 }\r
1878\r
1879 //\r
5dbc768f 1880 // Open or create the file corresponding to the next pathname fragment.\r
768b6111
LE
1881 //\r
1882 Status = LastFile->Open (\r
1883 LastFile,\r
1884 &NextFile,\r
1885 PathName,\r
5dbc768f
LE
1886 OpenMode,\r
1887 Attributes\r
768b6111
LE
1888 );\r
1889\r
768b6111
LE
1890 //\r
1891 // Release any AlignedPathName on both error and success paths; PathName is\r
1892 // no longer needed.\r
1893 //\r
1894 if (AlignedPathName != NULL) {\r
1895 FreePool (AlignedPathName);\r
1896 }\r
1897 if (EFI_ERROR (Status)) {\r
1898 goto CloseLastFile;\r
1899 }\r
1900\r
1901 //\r
1902 // Advance to the next device path node.\r
1903 //\r
1904 LastFile->Close (LastFile);\r
1905 LastFile = NextFile;\r
1906 *FilePath = NextDevicePathNode (FilePathNode);\r
1907 }\r
1908\r
1909 *File = LastFile;\r
1910 return EFI_SUCCESS;\r
1911\r
1912CloseLastFile:\r
1913 LastFile->Close (LastFile);\r
1914\r
1915 //\r
1916 // We are on the error path; we must have set an error Status for returning\r
1917 // to the caller.\r
1918 //\r
1919 ASSERT (EFI_ERROR (Status));\r
1920 return Status;\r
1921}\r