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