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