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