]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkPkg/Library/FrameworkUefiLib/UefiLib.c
MdePkg/SmmPeriodicSmiLib: Get Periodic SMI Context More Robustly
[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
b6d5def2
MH
868 IN CONST CHAR8 *Language,\r
869 IN CONST CHAR8 *SupportedLanguages,\r
870 IN OUT EFI_UNICODE_STRING_TABLE **UnicodeStringTable,\r
871 IN CONST CHAR16 *UnicodeString\r
79964ac8 872 )\r
873{\r
874 UINTN NumberOfEntries;\r
875 EFI_UNICODE_STRING_TABLE *OldUnicodeStringTable;\r
876 EFI_UNICODE_STRING_TABLE *NewUnicodeStringTable;\r
877 UINTN UnicodeStringLength;\r
878\r
879 //\r
880 // Make sure the parameter are valid\r
881 //\r
882 if (Language == NULL || UnicodeString == NULL || UnicodeStringTable == NULL) {\r
883 return EFI_INVALID_PARAMETER;\r
884 }\r
885\r
886 //\r
887 // If there are no supported languages, then a Unicode String can not be added\r
888 //\r
889 if (SupportedLanguages == NULL) {\r
890 return EFI_UNSUPPORTED;\r
891 }\r
892\r
893 //\r
894 // If the Unicode String is empty, then a Unicode String can not be added\r
895 //\r
896 if (UnicodeString[0] == 0) {\r
897 return EFI_INVALID_PARAMETER;\r
898 }\r
899\r
900 //\r
901 // Make sure Language is a member of SupportedLanguages\r
902 //\r
903 while (*SupportedLanguages != 0) {\r
904 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
905\r
906 //\r
907 // Determine the size of the Unicode String Table by looking for a NULL Language entry\r
908 //\r
909 NumberOfEntries = 0;\r
910 if (*UnicodeStringTable != NULL) {\r
911 OldUnicodeStringTable = *UnicodeStringTable;\r
912 while (OldUnicodeStringTable->Language != NULL) {\r
913 if (CompareIso639LanguageCode (Language, OldUnicodeStringTable->Language)) {\r
914 return EFI_ALREADY_STARTED;\r
915 }\r
916\r
917 OldUnicodeStringTable++;\r
918 NumberOfEntries++;\r
919 }\r
920 }\r
921\r
922 //\r
923 // Allocate space for a new Unicode String Table. It must hold the current number of\r
924 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table\r
925 // marker\r
926 //\r
927 NewUnicodeStringTable = AllocatePool ((NumberOfEntries + 2) * sizeof (EFI_UNICODE_STRING_TABLE));\r
928 if (NewUnicodeStringTable == NULL) {\r
929 return EFI_OUT_OF_RESOURCES;\r
930 }\r
931\r
932 //\r
933 // If the current Unicode String Table contains any entries, then copy them to the\r
934 // newly allocated Unicode String Table.\r
935 //\r
936 if (*UnicodeStringTable != NULL) {\r
937 CopyMem (\r
938 NewUnicodeStringTable,\r
939 *UnicodeStringTable,\r
940 NumberOfEntries * sizeof (EFI_UNICODE_STRING_TABLE)\r
941 );\r
942 }\r
943\r
944 //\r
945 // Allocate space for a copy of the Language specifier\r
946 //\r
947 NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (3, Language);\r
948 if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) {\r
949 gBS->FreePool (NewUnicodeStringTable);\r
950 return EFI_OUT_OF_RESOURCES;\r
951 }\r
952\r
953 //\r
954 // Compute the length of the Unicode String\r
955 //\r
956 for (UnicodeStringLength = 0; UnicodeString[UnicodeStringLength] != 0; UnicodeStringLength++)\r
957 ;\r
958\r
959 //\r
960 // Allocate space for a copy of the Unicode String\r
961 //\r
962 NewUnicodeStringTable[NumberOfEntries].UnicodeString = AllocateCopyPool (\r
963 (UnicodeStringLength + 1) * sizeof (CHAR16),\r
964 UnicodeString\r
965 );\r
966 if (NewUnicodeStringTable[NumberOfEntries].UnicodeString == NULL) {\r
967 gBS->FreePool (NewUnicodeStringTable[NumberOfEntries].Language);\r
968 gBS->FreePool (NewUnicodeStringTable);\r
969 return EFI_OUT_OF_RESOURCES;\r
970 }\r
971\r
972 //\r
973 // Mark the end of the Unicode String Table\r
974 //\r
975 NewUnicodeStringTable[NumberOfEntries + 1].Language = NULL;\r
976 NewUnicodeStringTable[NumberOfEntries + 1].UnicodeString = NULL;\r
977\r
978 //\r
979 // Free the old Unicode String Table\r
980 //\r
981 if (*UnicodeStringTable != NULL) {\r
982 gBS->FreePool (*UnicodeStringTable);\r
983 }\r
984\r
985 //\r
986 // Point UnicodeStringTable at the newly allocated Unicode String Table\r
987 //\r
988 *UnicodeStringTable = NewUnicodeStringTable;\r
989\r
990 return EFI_SUCCESS;\r
991 }\r
992\r
993 SupportedLanguages += 3;\r
994 }\r
995\r
996 return EFI_UNSUPPORTED;\r
997}\r
998\r
bf428cb3 999\r
1000/**\r
1001 This function adds the Null-terminated Unicode string specified by UnicodeString\r
1002 to UnicodeStringTable.\r
1003\r
1004 If Language is a member of SupportedLanguages then UnicodeString is added to\r
1005 UnicodeStringTable. New buffers are allocated for both Language and UnicodeString.\r
1006 The contents of Language and UnicodeString are copied into these new buffers.\r
1007 These buffers are automatically freed when EfiLibFreeUnicodeStringTable() is called.\r
1008\r
1009 @param Language A pointer to an ASCII string containing the ISO 639-2 or\r
1010 the RFC 4646 language code for the Unicode string to add.\r
1011 If Iso639Language is TRUE, then this ASCII string is not\r
1012 assumed to be Null-terminated, and only the first three\r
1013 chacters are used. If Iso639Language is FALSE, then this\r
1014 ASCII string must be Null-terminated.\r
1015 @param SupportedLanguages A pointer to a Null-terminated ASCII string that contains\r
1016 a set of ISO 639-2 or RFC 4646 language codes that the Unicode\r
1017 string table supports. Language must be a member of this set.\r
1018 If Iso639Language is TRUE, then this string contains one or more\r
1019 ISO 639-2 language codes with no separator characters.\r
1020 If Iso639Language is FALSE, then is string contains one or more\r
1021 RFC 4646 language codes separated by ';'.\r
1022 @param UnicodeStringTable A pointer to the table of Unicode strings. Type EFI_UNICODE_STRING_TABLE\r
1023 is defined in "Related Definitions".\r
1024 @param UnicodeString A pointer to the Unicode string to add. \r
1025 @param Iso639Language Specifies the supported language code format. If it is TRUE,\r
1026 then Language and SupportedLanguages follow ISO 639-2 language code format.\r
1027 Otherwise, they follow RFC 4646 language code format.\r
1028\r
1029 @retval EFI_SUCCESS The Unicode string that matches the language specified by\r
1030 Language was found in the table of Unicode strings UnicodeStringTable,\r
1031 and it was returned in UnicodeString. \r
1032 @retval EFI_INVALID_PARAMETER Language is NULL. \r
1033 @retval EFI_INVALID_PARAMETER UnicodeString is NULL. \r
1034 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string. \r
1035 @retval EFI_UNSUPPORTED SupportedLanguages is NULL. \r
1036 @retval EFI_ALREADY_STARTED A Unicode string with language Language is already present in\r
1037 UnicodeStringTable. \r
1038 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another Unicode string UnicodeStringTable. \r
1039 @retval EFI_UNSUPPORTED The language specified by Language is not a member of SupportedLanguages.\r
1040\r
1041**/\r
1042EFI_STATUS\r
1043EFIAPI\r
1044AddUnicodeString2 (\r
b6d5def2
MH
1045 IN CONST CHAR8 *Language,\r
1046 IN CONST CHAR8 *SupportedLanguages,\r
1047 IN OUT EFI_UNICODE_STRING_TABLE **UnicodeStringTable,\r
1048 IN CONST CHAR16 *UnicodeString,\r
1049 IN BOOLEAN Iso639Language\r
bf428cb3 1050 )\r
1051{\r
1052 UINTN NumberOfEntries;\r
1053 EFI_UNICODE_STRING_TABLE *OldUnicodeStringTable;\r
1054 EFI_UNICODE_STRING_TABLE *NewUnicodeStringTable;\r
1055 UINTN UnicodeStringLength;\r
1056 BOOLEAN Found;\r
1057 UINTN Index;\r
1058 CHAR8 *LanguageString;\r
1059\r
1060 //\r
1061 // Make sure the parameter are valid\r
1062 //\r
1063 if (Language == NULL || UnicodeString == NULL || UnicodeStringTable == NULL) {\r
1064 return EFI_INVALID_PARAMETER;\r
1065 }\r
1066\r
1067 //\r
1068 // If there are no supported languages, then a Unicode String can not be added\r
1069 //\r
1070 if (SupportedLanguages == NULL) {\r
1071 return EFI_UNSUPPORTED;\r
1072 }\r
1073\r
1074 //\r
1075 // If the Unicode String is empty, then a Unicode String can not be added\r
1076 //\r
1077 if (UnicodeString[0] == 0) {\r
1078 return EFI_INVALID_PARAMETER;\r
1079 }\r
1080\r
1081 //\r
1082 // Make sure Language is a member of SupportedLanguages\r
1083 //\r
1084 Found = FALSE;\r
1085 while (*SupportedLanguages != 0) {\r
1086 if (Iso639Language) {\r
1087 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
1088 Found = TRUE;\r
1089 break;\r
1090 }\r
1091 SupportedLanguages += 3;\r
1092 } else {\r
1093 for (Index = 0; SupportedLanguages[Index] != 0 && SupportedLanguages[Index] != ';'; Index++);\r
1094 if (AsciiStrnCmp(SupportedLanguages, Language, Index) == 0) {\r
1095 Found = TRUE;\r
1096 break;\r
1097 }\r
1098 SupportedLanguages += Index;\r
1099 for (; *SupportedLanguages != 0 && *SupportedLanguages == ';'; SupportedLanguages++);\r
1100 }\r
1101 }\r
1102\r
1103 //\r
1104 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED\r
1105 //\r
1106 if (!Found) {\r
1107 return EFI_UNSUPPORTED;\r
1108 }\r
1109\r
1110 //\r
1111 // Determine the size of the Unicode String Table by looking for a NULL Language entry\r
1112 //\r
1113 NumberOfEntries = 0;\r
1114 if (*UnicodeStringTable != NULL) {\r
1115 OldUnicodeStringTable = *UnicodeStringTable;\r
1116 while (OldUnicodeStringTable->Language != NULL) {\r
1117 LanguageString = OldUnicodeStringTable->Language;\r
1118\r
1119 while (*LanguageString != 0) {\r
1120 for (Index = 0; LanguageString[Index] != 0 && LanguageString[Index] != ';'; Index++);\r
1121\r
1122 if (AsciiStrnCmp (Language, LanguageString, Index) == 0) { \r
1123 return EFI_ALREADY_STARTED;\r
1124 }\r
1125 LanguageString += Index;\r
1126 for (; *LanguageString != 0 && *LanguageString == ';'; LanguageString++);\r
1127 }\r
1128 OldUnicodeStringTable++;\r
1129 NumberOfEntries++;\r
1130 }\r
1131 }\r
1132\r
1133 //\r
1134 // Allocate space for a new Unicode String Table. It must hold the current number of\r
1135 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table\r
1136 // marker\r
1137 //\r
1138 NewUnicodeStringTable = AllocatePool ((NumberOfEntries + 2) * sizeof (EFI_UNICODE_STRING_TABLE));\r
1139 if (NewUnicodeStringTable == NULL) {\r
1140 return EFI_OUT_OF_RESOURCES;\r
1141 }\r
1142\r
1143 //\r
1144 // If the current Unicode String Table contains any entries, then copy them to the\r
1145 // newly allocated Unicode String Table.\r
1146 //\r
1147 if (*UnicodeStringTable != NULL) {\r
1148 CopyMem (\r
1149 NewUnicodeStringTable,\r
1150 *UnicodeStringTable,\r
1151 NumberOfEntries * sizeof (EFI_UNICODE_STRING_TABLE)\r
1152 );\r
1153 }\r
1154\r
1155 //\r
1156 // Allocate space for a copy of the Language specifier\r
1157 //\r
1158 NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (AsciiStrSize(Language), Language);\r
1159 if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) {\r
1160 gBS->FreePool (NewUnicodeStringTable);\r
1161 return EFI_OUT_OF_RESOURCES;\r
1162 }\r
1163\r
1164 //\r
1165 // Compute the length of the Unicode String\r
1166 //\r
1167 for (UnicodeStringLength = 0; UnicodeString[UnicodeStringLength] != 0; UnicodeStringLength++);\r
1168\r
1169 //\r
1170 // Allocate space for a copy of the Unicode String\r
1171 //\r
1172 NewUnicodeStringTable[NumberOfEntries].UnicodeString = AllocateCopyPool (StrSize (UnicodeString), UnicodeString);\r
1173 if (NewUnicodeStringTable[NumberOfEntries].UnicodeString == NULL) {\r
1174 gBS->FreePool (NewUnicodeStringTable[NumberOfEntries].Language);\r
1175 gBS->FreePool (NewUnicodeStringTable);\r
1176 return EFI_OUT_OF_RESOURCES;\r
1177 }\r
1178\r
1179 //\r
1180 // Mark the end of the Unicode String Table\r
1181 //\r
1182 NewUnicodeStringTable[NumberOfEntries + 1].Language = NULL;\r
1183 NewUnicodeStringTable[NumberOfEntries + 1].UnicodeString = NULL;\r
1184\r
1185 //\r
1186 // Free the old Unicode String Table\r
1187 //\r
1188 if (*UnicodeStringTable != NULL) {\r
1189 gBS->FreePool (*UnicodeStringTable);\r
1190 }\r
1191\r
1192 //\r
1193 // Point UnicodeStringTable at the newly allocated Unicode String Table\r
1194 //\r
1195 *UnicodeStringTable = NewUnicodeStringTable;\r
1196\r
1197 return EFI_SUCCESS;\r
1198}\r
1199\r
79964ac8 1200/**\r
1201 This function frees the table of Unicode strings in UnicodeStringTable.\r
bf428cb3 1202\r
79964ac8 1203 If UnicodeStringTable is NULL, then EFI_SUCCESS is returned.\r
bf428cb3 1204 Otherwise, each language code, and each Unicode string in the Unicode string \r
79964ac8 1205 table are freed, and EFI_SUCCESS is returned.\r
1206\r
1207 @param UnicodeStringTable A pointer to the table of Unicode strings.\r
1208\r
1209 @retval EFI_SUCCESS The Unicode string table was freed.\r
1210\r
1211**/\r
1212EFI_STATUS\r
1213EFIAPI\r
1214FreeUnicodeStringTable (\r
1215 IN EFI_UNICODE_STRING_TABLE *UnicodeStringTable\r
1216 )\r
1217{\r
1218 UINTN Index;\r
1219\r
1220 //\r
1221 // If the Unicode String Table is NULL, then it is already freed\r
1222 //\r
1223 if (UnicodeStringTable == NULL) {\r
1224 return EFI_SUCCESS;\r
1225 }\r
1226\r
1227 //\r
1228 // Loop through the Unicode String Table until we reach the end of table marker\r
1229 //\r
1230 for (Index = 0; UnicodeStringTable[Index].Language != NULL; Index++) {\r
1231\r
1232 //\r
1233 // Free the Language string from the Unicode String Table\r
1234 //\r
1235 gBS->FreePool (UnicodeStringTable[Index].Language);\r
1236\r
1237 //\r
1238 // Free the Unicode String from the Unicode String Table\r
1239 //\r
1240 if (UnicodeStringTable[Index].UnicodeString != NULL) {\r
1241 gBS->FreePool (UnicodeStringTable[Index].UnicodeString);\r
1242 }\r
1243 }\r
1244\r
1245 //\r
1246 // Free the Unicode String Table itself\r
1247 //\r
1248 gBS->FreePool (UnicodeStringTable);\r
1249\r
1250 return EFI_SUCCESS;\r
1251}\r
4ea439fb 1252\r
1253/**\r
1254 Returns a pointer to an allocated buffer that contains the contents of a \r
1255 variable retrieved through the UEFI Runtime Service GetVariable(). The \r
1256 returned buffer is allocated using AllocatePool(). The caller is responsible\r
1257 for freeing this buffer with FreePool().\r
1258\r
1259 If Name is NULL, then ASSERT().\r
1260 If Guid is NULL, then ASSERT().\r
1261\r
1262 @param[in] Name Pointer to a Null-terminated Unicode string.\r
1263 @param[in] Guid Pointer to an EFI_GUID structure\r
1264\r
1265 @retval NULL The variable could not be retrieved.\r
1266 @retval NULL There are not enough resources available for the variable contents.\r
1267 @retval Other A pointer to allocated buffer containing the variable contents.\r
1268\r
1269**/\r
1270VOID *\r
1271EFIAPI\r
1272GetVariable (\r
1273 IN CONST CHAR16 *Name,\r
1274 IN CONST EFI_GUID *Guid\r
1275 )\r
1276{\r
1277 EFI_STATUS Status;\r
1278 UINTN Size;\r
1279 VOID *Value;\r
1280\r
1281 ASSERT (Name != NULL);\r
1282 ASSERT (Guid != NULL);\r
1283\r
1284 //\r
1285 // Try to get the variable size.\r
1286 //\r
1287 Value = NULL;\r
1288 Size = 0;\r
1289 Status = gRT->GetVariable ((CHAR16 *) Name, (EFI_GUID *) Guid, NULL, &Size, Value);\r
1290 if (Status != EFI_BUFFER_TOO_SMALL) {\r
1291 return NULL;\r
1292 }\r
1293\r
1294 //\r
1295 // Allocate buffer to get the variable.\r
1296 //\r
1297 Value = AllocatePool (Size);\r
1298 if (Value == NULL) {\r
1299 return NULL;\r
1300 }\r
1301\r
1302 //\r
1303 // Get the variable data.\r
1304 //\r
1305 Status = gRT->GetVariable ((CHAR16 *) Name, (EFI_GUID *) Guid, NULL, &Size, Value);\r
1306 if (EFI_ERROR (Status)) {\r
1307 FreePool(Value);\r
1308 return NULL;\r
1309 }\r
1310\r
1311 return Value;\r
1312}\r
1313\r
1314\r
1315/**\r
1316 Returns a pointer to an allocated buffer that contains the contents of a \r
1317 variable retrieved through the UEFI Runtime Service GetVariable(). This \r
1318 function always uses the EFI_GLOBAL_VARIABLE GUID to retrieve variables.\r
1319 The returned buffer is allocated using AllocatePool(). The caller is \r
1320 responsible for freeing this buffer with FreePool().\r
1321\r
1322 If Name is NULL, then ASSERT().\r
1323\r
1324 @param[in] Name Pointer to a Null-terminated Unicode string.\r
1325\r
1326 @retval NULL The variable could not be retrieved.\r
1327 @retval NULL There are not enough resources available for the variable contents.\r
1328 @retval Other A pointer to allocated buffer containing the variable contents.\r
1329\r
1330**/\r
1331VOID *\r
1332EFIAPI\r
1333GetEfiGlobalVariable (\r
1334 IN CONST CHAR16 *Name\r
1335 )\r
1336{\r
1337 return GetVariable (Name, &gEfiGlobalVariableGuid);\r
1338}\r
1339\r
7aaa7e67
KM
1340/**\r
1341 Returns the status whether get the variable success. The function retrieves\r
1342 variable through the UEFI Runtime Service GetVariable(). The\r
1343 returned buffer is allocated using AllocatePool(). The caller is responsible\r
1344 for freeing this buffer with FreePool().\r
1345\r
1346 If Name is NULL, then ASSERT().\r
1347 If Guid is NULL, then ASSERT().\r
1348 If Value is NULL, then ASSERT().\r
1349\r
1350 @param[in] Name The pointer to a Null-terminated Unicode string.\r
1351 @param[in] Guid The pointer to an EFI_GUID structure\r
1352 @param[out] Value The buffer point saved the variable info.\r
1353 @param[out] Size The buffer size of the variable.\r
1354\r
1355 @return EFI_OUT_OF_RESOURCES Allocate buffer failed.\r
1356 @return EFI_SUCCESS Find the specified variable.\r
1357 @return Others Errors Return errors from call to gRT->GetVariable.\r
1358\r
1359**/\r
1360EFI_STATUS\r
1361EFIAPI\r
1362GetVariable2 (\r
1363 IN CONST CHAR16 *Name,\r
1364 IN CONST EFI_GUID *Guid,\r
1365 OUT VOID **Value,\r
1366 OUT UINTN *Size OPTIONAL\r
1367 )\r
1368{\r
1369 EFI_STATUS Status;\r
1370 UINTN BufferSize;\r
1371\r
1372 ASSERT (Name != NULL && Guid != NULL && Value != NULL);\r
1373\r
1374 //\r
1375 // Try to get the variable size.\r
1376 //\r
1377 BufferSize = 0;\r
1378 *Value = NULL;\r
1379 if (Size != NULL) {\r
1380 *Size = 0;\r
1381 }\r
1382\r
1383 Status = gRT->GetVariable ((CHAR16 *) Name, (EFI_GUID *) Guid, NULL, &BufferSize, *Value);\r
1384 if (Status != EFI_BUFFER_TOO_SMALL) {\r
1385 return Status;\r
1386 }\r
1387\r
1388 //\r
1389 // Allocate buffer to get the variable.\r
1390 //\r
1391 *Value = AllocatePool (BufferSize);\r
1392 ASSERT (*Value != NULL);\r
1393 if (*Value == NULL) {\r
1394 return EFI_OUT_OF_RESOURCES;\r
1395 }\r
1396\r
1397 //\r
1398 // Get the variable data.\r
1399 //\r
1400 Status = gRT->GetVariable ((CHAR16 *) Name, (EFI_GUID *) Guid, NULL, &BufferSize, *Value);\r
1401 if (EFI_ERROR (Status)) {\r
1402 FreePool(*Value);\r
1403 *Value = NULL;\r
1404 }\r
1405\r
1406 if (Size != NULL) {\r
1407 *Size = BufferSize;\r
1408 }\r
1409\r
1410 return Status;\r
1411}\r
1412\r
1413/**\r
1414 Returns a pointer to an allocated buffer that contains the contents of a\r
1415 variable retrieved through the UEFI Runtime Service GetVariable(). This\r
1416 function always uses the EFI_GLOBAL_VARIABLE GUID to retrieve variables.\r
1417 The returned buffer is allocated using AllocatePool(). The caller is\r
1418 responsible for freeing this buffer with FreePool().\r
1419\r
1420 If Name is NULL, then ASSERT().\r
1421 If Value is NULL, then ASSERT().\r
1422\r
1423 @param[in] Name The pointer to a Null-terminated Unicode string.\r
1424 @param[out] Value The buffer point saved the variable info.\r
1425 @param[out] Size The buffer size of the variable.\r
1426\r
1427 @return EFI_OUT_OF_RESOURCES Allocate buffer failed.\r
1428 @return EFI_SUCCESS Find the specified variable.\r
1429 @return Others Errors Return errors from call to gRT->GetVariable.\r
1430\r
1431**/\r
1432EFI_STATUS\r
1433EFIAPI\r
1434GetEfiGlobalVariable2 (\r
1435 IN CONST CHAR16 *Name,\r
1436 OUT VOID **Value,\r
1437 OUT UINTN *Size OPTIONAL\r
1438 )\r
1439{\r
1440 return GetVariable2 (Name, &gEfiGlobalVariableGuid, Value, Size);\r
1441}\r
4ea439fb 1442\r
1443/**\r
1444 Returns a pointer to an allocated buffer that contains the best matching language \r
1445 from a set of supported languages. \r
1446 \r
1447 This function supports both ISO 639-2 and RFC 4646 language codes, but language \r
1448 code types may not be mixed in a single call to this function. The language \r
1449 code returned is allocated using AllocatePool(). The caller is responsible for \r
1450 freeing the allocated buffer using FreePool(). This function supports a variable\r
1451 argument list that allows the caller to pass in a prioritized list of language \r
1452 codes to test against all the language codes in SupportedLanguages. \r
1453\r
1454 If SupportedLanguages is NULL, then ASSERT().\r
1455\r
1456 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that\r
1457 contains a set of language codes in the format \r
1458 specified by Iso639Language.\r
1459 @param[in] Iso639Language If TRUE, then all language codes are assumed to be\r
1460 in ISO 639-2 format. If FALSE, then all language\r
1461 codes are assumed to be in RFC 4646 language format\r
1462 @param[in] ... A variable argument list that contains pointers to \r
1463 Null-terminated ASCII strings that contain one or more\r
1464 language codes in the format specified by Iso639Language.\r
1465 The first language code from each of these language\r
1466 code lists is used to determine if it is an exact or\r
1467 close match to any of the language codes in \r
1468 SupportedLanguages. Close matches only apply to RFC 4646\r
1469 language codes, and the matching algorithm from RFC 4647\r
1470 is used to determine if a close match is present. If \r
1471 an exact or close match is found, then the matching\r
1472 language code from SupportedLanguages is returned. If\r
1473 no matches are found, then the next variable argument\r
1474 parameter is evaluated. The variable argument list \r
1475 is terminated by a NULL.\r
1476\r
1477 @retval NULL The best matching language could not be found in SupportedLanguages.\r
1478 @retval NULL There are not enough resources available to return the best matching \r
1479 language.\r
1480 @retval Other A pointer to a Null-terminated ASCII string that is the best matching \r
1481 language in SupportedLanguages.\r
1482\r
1483**/\r
1484CHAR8 *\r
1485EFIAPI\r
1486GetBestLanguage (\r
1487 IN CONST CHAR8 *SupportedLanguages, \r
1488 IN BOOLEAN Iso639Language,\r
1489 ...\r
1490 )\r
1491{\r
1492 VA_LIST Args;\r
1493 CHAR8 *Language;\r
1494 UINTN CompareLength;\r
1495 UINTN LanguageLength;\r
1496 CONST CHAR8 *Supported;\r
1497 CHAR8 *BestLanguage;\r
1498\r
1499 ASSERT (SupportedLanguages != NULL);\r
1500\r
1501 VA_START (Args, Iso639Language);\r
1502 while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {\r
1503 //\r
1504 // Default to ISO 639-2 mode\r
1505 //\r
1506 CompareLength = 3;\r
1507 LanguageLength = MIN (3, AsciiStrLen (Language));\r
1508\r
1509 //\r
1510 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language\r
1511 //\r
1512 if (!Iso639Language) {\r
1513 for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);\r
1514 }\r
1515\r
1516 //\r
1517 // Trim back the length of Language used until it is empty\r
1518 //\r
1519 while (LanguageLength > 0) {\r
1520 //\r
1521 // Loop through all language codes in SupportedLanguages\r
1522 //\r
1523 for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {\r
1524 //\r
1525 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages\r
1526 //\r
1527 if (!Iso639Language) {\r
1528 //\r
1529 // Skip ';' characters in Supported\r
1530 //\r
1531 for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
1532 //\r
1533 // Determine the length of the next language code in Supported\r
1534 //\r
1535 for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);\r
1536 //\r
1537 // If Language is longer than the Supported, then skip to the next language\r
1538 //\r
1539 if (LanguageLength > CompareLength) {\r
1540 continue;\r
1541 }\r
1542 }\r
1543 //\r
1544 // See if the first LanguageLength characters in Supported match Language\r
1545 //\r
1546 if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {\r
1547 VA_END (Args);\r
1548 //\r
1549 // Allocate, copy, and return the best matching language code from SupportedLanguages\r
1550 //\r
1551 BestLanguage = AllocateZeroPool (CompareLength + 1);\r
1552 if (BestLanguage == NULL) {\r
1553 return NULL;\r
1554 }\r
1555 return CopyMem (BestLanguage, Supported, CompareLength);\r
1556 }\r
1557 }\r
1558\r
1559 if (Iso639Language) {\r
1560 //\r
1561 // If ISO 639 mode, then each language can only be tested once\r
1562 //\r
1563 LanguageLength = 0;\r
1564 } else {\r
1565 //\r
1566 // If RFC 4646 mode, then trim Language from the right to the next '-' character \r
1567 //\r
1568 for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);\r
1569 }\r
1570 }\r
1571 }\r
1572 VA_END (Args);\r
1573\r
1574 //\r
1575 // No matches were found \r
1576 //\r
1577 return NULL;\r
1578}\r
3499b150
KM
1579\r
1580/**\r
1581 Returns an array of protocol instance that matches the given protocol.\r
1582\r
1583 @param[in] Protocol Provides the protocol to search for.\r
1584 @param[out] NoProtocols The number of protocols returned in Buffer.\r
1585 @param[out] Buffer A pointer to the buffer to return the requested\r
1586 array of protocol instances that match Protocol.\r
1587 The returned buffer is allocated using\r
1588 EFI_BOOT_SERVICES.AllocatePool(). The caller is\r
1589 responsible for freeing this buffer with\r
1590 EFI_BOOT_SERVICES.FreePool().\r
1591\r
1592 @retval EFI_SUCCESS The array of protocols was returned in Buffer,\r
1593 and the number of protocols in Buffer was\r
1594 returned in NoProtocols.\r
1595 @retval EFI_NOT_FOUND No protocols found.\r
1596 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the\r
1597 matching results.\r
1598 @retval EFI_INVALID_PARAMETER Protocol is NULL.\r
1599 @retval EFI_INVALID_PARAMETER NoProtocols is NULL.\r
1600 @retval EFI_INVALID_PARAMETER Buffer is NULL.\r
1601\r
1602**/\r
1603EFI_STATUS\r
1604EFIAPI\r
1605EfiLocateProtocolBuffer (\r
1606 IN EFI_GUID *Protocol,\r
1607 OUT UINTN *NoProtocols,\r
1608 OUT VOID ***Buffer\r
1609 )\r
1610{\r
1611 EFI_STATUS Status;\r
1612 UINTN NoHandles;\r
1613 EFI_HANDLE *HandleBuffer;\r
1614 UINTN Index;\r
1615\r
1616 //\r
1617 // Check input parameters\r
1618 //\r
1619 if (Protocol == NULL || NoProtocols == NULL || Buffer == NULL) {\r
1620 return EFI_INVALID_PARAMETER;\r
1621 }\r
1622\r
1623 //\r
1624 // Initialze output parameters\r
1625 //\r
1626 *NoProtocols = 0;\r
1627 *Buffer = NULL;\r
1628\r
1629 //\r
1630 // Retrieve the array of handles that support Protocol\r
1631 //\r
1632 Status = gBS->LocateHandleBuffer (\r
1633 ByProtocol,\r
1634 Protocol,\r
1635 NULL,\r
1636 &NoHandles,\r
1637 &HandleBuffer\r
1638 );\r
1639 if (EFI_ERROR (Status)) {\r
1640 return Status;\r
1641 }\r
1642\r
1643 //\r
1644 // Allocate array of protocol instances\r
1645 //\r
1646 Status = gBS->AllocatePool (\r
1647 EfiBootServicesData,\r
1648 NoHandles * sizeof (VOID *),\r
1649 (VOID **)Buffer\r
1650 );\r
1651 if (EFI_ERROR (Status)) {\r
1652 return EFI_OUT_OF_RESOURCES;\r
1653 }\r
1654 ZeroMem (*Buffer, NoHandles * sizeof (VOID *));\r
1655\r
1656 //\r
1657 // Lookup Protocol on each handle in HandleBuffer to fill in the array of\r
1658 // protocol instances. Handle case where protocol instance was present when\r
1659 // LocateHandleBuffer() was called, but is not present when HandleProtocol()\r
1660 // is called.\r
1661 //\r
1662 for (Index = 0, *NoProtocols = 0; Index < NoHandles; Index++) {\r
1663 Status = gBS->HandleProtocol (\r
1664 HandleBuffer[Index],\r
1665 Protocol,\r
1666 &((*Buffer)[*NoProtocols])\r
1667 );\r
1668 if (!EFI_ERROR (Status)) {\r
1669 (*NoProtocols)++;\r
1670 }\r
1671 }\r
1672\r
1673 //\r
1674 // Free the handle buffer\r
1675 //\r
1676 gBS->FreePool (HandleBuffer);\r
1677\r
1678 //\r
1679 // Make sure at least one protocol instance was found\r
1680 //\r
1681 if (*NoProtocols == 0) {\r
1682 gBS->FreePool (*Buffer);\r
1683 *Buffer = NULL;\r
1684 return EFI_NOT_FOUND;\r
1685 }\r
1686\r
1687 return EFI_SUCCESS;\r
1688}\r