]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/UefiLib/UefiLib.c
Removed CommonHeader.h generated file from the MdePkg.
[mirror_edk2.git] / MdePkg / Library / UefiLib / UefiLib.c
CommitLineData
e386b444 1/** @file\r
2 Mde UEFI library functions.\r
3\r
4 Copyright (c) 2006 - 2007, Intel Corporation<BR>\r
5 All rights reserved. This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13 Module Name: UefiLib.c\r
14\r
15**/\r
16\r
17//\r
18// Include common header file for this module.\r
19//\r
f734a10a 20#include "UefiLibInternal.h"\r
e386b444 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
32STATIC\r
33BOOLEAN\r
34CompareIso639LanguageCode (\r
35 IN CONST CHAR8 *Language1,\r
36 IN CONST CHAR8 *Language2\r
37 )\r
38{\r
39 UINT32 Name1;\r
40 UINT32 Name2;\r
41\r
42 Name1 = ReadUnaligned24 ((CONST UINT32 *) Language1);\r
43 Name2 = ReadUnaligned24 ((CONST UINT32 *) Language2);\r
44\r
45 return (BOOLEAN) (Name1 == Name2);\r
46}\r
47\r
48/**\r
49 This function searches the list of configuration tables stored in the EFI System\r
50 Table for a table with a GUID that matches TableGuid. If a match is found,\r
51 then a pointer to the configuration table is returned in Table, and EFI_SUCCESS\r
52 is returned. If a matching GUID is not found, then EFI_NOT_FOUND is returned.\r
53\r
54 @param TableGuid Pointer to table's GUID type..\r
55 @param Table Pointer to the table associated with TableGuid in the EFI System Table.\r
56\r
57 @retval EFI_SUCCESS A configuration table matching TableGuid was found.\r
58 @retval EFI_NOT_FOUND A configuration table matching TableGuid could not be found.\r
59\r
60**/\r
61EFI_STATUS\r
62EFIAPI\r
63EfiGetSystemConfigurationTable (\r
64 IN EFI_GUID *TableGuid,\r
65 OUT VOID **Table\r
66 )\r
67{\r
68 EFI_SYSTEM_TABLE *SystemTable;\r
69 UINTN Index;\r
70\r
71 ASSERT (TableGuid != NULL);\r
72 ASSERT (Table != NULL);\r
73\r
74 SystemTable = gST;\r
75 *Table = NULL;\r
76 for (Index = 0; Index < SystemTable->NumberOfTableEntries; Index++) {\r
77 if (CompareGuid (TableGuid, &(SystemTable->ConfigurationTable[Index].VendorGuid))) {\r
78 *Table = SystemTable->ConfigurationTable[Index].VendorTable;\r
79 return EFI_SUCCESS;\r
80 }\r
81 }\r
82\r
83 return EFI_NOT_FOUND;\r
84}\r
85\r
86/**\r
87 This function causes the notification function to be executed for every protocol\r
88 of type ProtocolGuid instance that exists in the system when this function is\r
89 invoked. In addition, every time a protocol of type ProtocolGuid instance is\r
90 installed or reinstalled, the notification function is also executed.\r
91\r
92 @param ProtocolGuid Supplies GUID of the protocol upon whose installation the event is fired.\r
93 @param NotifyTpl Supplies the task priority level of the event notifications.\r
94 @param NotifyFunction Supplies the function to notify when the event is signaled.\r
95 @param NotifyContext The context parameter to pass to NotifyFunction.\r
96 @param Registration A pointer to a memory location to receive the registration value.\r
97\r
98 @return The notification event that was created.\r
99\r
100**/\r
101EFI_EVENT\r
102EFIAPI\r
103EfiCreateProtocolNotifyEvent(\r
104 IN EFI_GUID *ProtocolGuid,\r
105 IN EFI_TPL NotifyTpl,\r
106 IN EFI_EVENT_NOTIFY NotifyFunction,\r
107 IN VOID *NotifyContext, OPTIONAL\r
108 OUT VOID **Registration\r
109 )\r
110{\r
111 EFI_STATUS Status;\r
112 EFI_EVENT Event;\r
113\r
114 //\r
115 // Create the event\r
116 //\r
117\r
118 Status = gBS->CreateEvent (\r
119 EVT_NOTIFY_SIGNAL,\r
120 NotifyTpl,\r
121 NotifyFunction,\r
122 NotifyContext,\r
123 &Event\r
124 );\r
125 ASSERT_EFI_ERROR (Status);\r
126\r
127 //\r
128 // Register for protocol notifactions on this event\r
129 //\r
130\r
131 Status = gBS->RegisterProtocolNotify (\r
132 ProtocolGuid,\r
133 Event,\r
134 Registration\r
135 );\r
136\r
137 ASSERT_EFI_ERROR (Status);\r
138\r
139 //\r
140 // Kick the event so we will perform an initial pass of\r
141 // current installed drivers\r
142 //\r
143\r
144 gBS->SignalEvent (Event);\r
145 return Event;\r
146}\r
147\r
148/**\r
149 This function creates an event using NotifyTpl, NoifyFunction, and NotifyContext.\r
150 This event is signaled with EfiNamedEventSignal(). This provide the ability for\r
151 one or more listeners on the same event named by the GUID specified by Name.\r
152\r
153 @param Name Supplies GUID name of the event.\r
154 @param NotifyTpl Supplies the task priority level of the event notifications.\r
155 @param NotifyFunction Supplies the function to notify when the event is signaled.\r
156 @param NotifyContext The context parameter to pass to NotifyFunction.\r
157 @param Registration A pointer to a memory location to receive the registration value.\r
158\r
159 @retval EFI_SUCCESS A named event was created.\r
160 @retval EFI_OUT_OF_RESOURCES There are not enough resource to create the named event.\r
161\r
162**/\r
163EFI_STATUS\r
164EFIAPI\r
165EfiNamedEventListen (\r
166 IN CONST EFI_GUID *Name,\r
167 IN EFI_TPL NotifyTpl,\r
168 IN EFI_EVENT_NOTIFY NotifyFunction,\r
169 IN CONST VOID *NotifyContext, OPTIONAL\r
170 OUT VOID *Registration OPTIONAL\r
171 )\r
172{\r
173 EFI_STATUS Status;\r
174 EFI_EVENT Event;\r
175 VOID *RegistrationLocal;\r
176\r
177 //\r
178 // Create event\r
179 //\r
180 Status = gBS->CreateEvent (\r
181 EVT_NOTIFY_SIGNAL,\r
182 NotifyTpl,\r
183 NotifyFunction,\r
184 (VOID *) NotifyContext,\r
185 &Event\r
186 );\r
187 ASSERT_EFI_ERROR (Status);\r
188\r
189 //\r
190 // The Registration is not optional to RegisterProtocolNotify().\r
191 // To make it optional to EfiNamedEventListen(), may need to substitute with a local.\r
192 //\r
193 if (Registration != NULL) {\r
194 RegistrationLocal = Registration;\r
195 } else {\r
196 RegistrationLocal = &RegistrationLocal;\r
197 }\r
198\r
199 //\r
200 // Register for an installation of protocol interface\r
201 //\r
202\r
203 Status = gBS->RegisterProtocolNotify (\r
204 (EFI_GUID *) Name,\r
205 Event,\r
206 RegistrationLocal\r
207 );\r
208 ASSERT_EFI_ERROR (Status);\r
209\r
210 return EFI_SUCCESS;\r
211}\r
212\r
213/**\r
214 This function signals the named event specified by Name. The named event must\r
215 have been created with EfiNamedEventListen().\r
216\r
217 @param Name Supplies GUID name of the event.\r
218\r
219 @retval EFI_SUCCESS A named event was signaled.\r
220 @retval EFI_OUT_OF_RESOURCES There are not enough resource to signal the named event.\r
221\r
222**/\r
223EFI_STATUS\r
224EFIAPI\r
225EfiNamedEventSignal (\r
226 IN CONST EFI_GUID *Name\r
227 )\r
228{\r
229 EFI_STATUS Status;\r
230 EFI_HANDLE Handle;\r
231\r
232 Handle = NULL;\r
233 Status = gBS->InstallProtocolInterface (\r
234 &Handle,\r
235 (EFI_GUID *) Name,\r
236 EFI_NATIVE_INTERFACE,\r
237 NULL\r
238 );\r
239 ASSERT_EFI_ERROR (Status);\r
240\r
241 Status = gBS->UninstallProtocolInterface (\r
242 Handle,\r
243 (EFI_GUID *) Name,\r
244 NULL\r
245 );\r
246 ASSERT_EFI_ERROR (Status);\r
247\r
248 return EFI_SUCCESS;\r
249}\r
250\r
251/**\r
252 Returns the current TPL.\r
253\r
254 This function returns the current TPL. There is no EFI service to directly\r
255 retrieve the current TPL. Instead, the RaiseTPL() function is used to raise\r
256 the TPL to TPL_HIGH_LEVEL. This will return the current TPL. The TPL level\r
257 can then immediately be restored back to the current TPL level with a call\r
258 to RestoreTPL().\r
259\r
260 @param VOID\r
261\r
262 @retvale EFI_TPL The current TPL.\r
263\r
264**/\r
265EFI_TPL\r
266EFIAPI\r
267EfiGetCurrentTpl (\r
268 VOID\r
269 )\r
270{\r
271 EFI_TPL Tpl;\r
272\r
273 Tpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r
274 gBS->RestoreTPL (Tpl);\r
275\r
276 return Tpl;\r
277}\r
278\r
279\r
280/**\r
281 This function initializes a basic mutual exclusion lock to the released state\r
282 and returns the lock. Each lock provides mutual exclusion access at its task\r
283 priority level. Since there is no preemption or multiprocessor support in EFI,\r
284 acquiring the lock only consists of raising to the locks TPL.\r
285\r
286 @param Lock A pointer to the lock data structure to initialize.\r
287 @param Priority EFI TPL associated with the lock.\r
288\r
289 @return The lock.\r
290\r
291**/\r
292EFI_LOCK *\r
293EFIAPI\r
294EfiInitializeLock (\r
295 IN OUT EFI_LOCK *Lock,\r
296 IN EFI_TPL Priority\r
297 )\r
298{\r
299 ASSERT (Lock != NULL);\r
300 ASSERT (Priority <= TPL_HIGH_LEVEL);\r
301\r
302 Lock->Tpl = Priority;\r
303 Lock->OwnerTpl = TPL_APPLICATION;\r
304 Lock->Lock = EfiLockReleased ;\r
305 return Lock;\r
306}\r
307\r
308/**\r
309 This function raises the system's current task priority level to the task\r
310 priority level of the mutual exclusion lock. Then, it places the lock in the\r
311 acquired state.\r
312\r
313 @param Priority The task priority level of the lock.\r
314\r
315**/\r
316VOID\r
317EFIAPI\r
318EfiAcquireLock (\r
319 IN EFI_LOCK *Lock\r
320 )\r
321{\r
322 ASSERT (Lock != NULL);\r
323 ASSERT (Lock->Lock == EfiLockReleased);\r
324\r
325 Lock->OwnerTpl = gBS->RaiseTPL (Lock->Tpl);\r
326 Lock->Lock = EfiLockAcquired;\r
327}\r
328\r
329/**\r
330 This function raises the system's current task priority level to the task\r
331 priority level of the mutual exclusion lock. Then, it attempts to place the\r
332 lock in the acquired state.\r
333\r
334 @param Lock A pointer to the lock to acquire.\r
335\r
336 @retval EFI_SUCCESS The lock was acquired.\r
337 @retval EFI_ACCESS_DENIED The lock could not be acquired because it is already owned.\r
338\r
339**/\r
340EFI_STATUS\r
341EFIAPI\r
342EfiAcquireLockOrFail (\r
343 IN EFI_LOCK *Lock\r
344 )\r
345{\r
346\r
347 ASSERT (Lock != NULL);\r
348 ASSERT (Lock->Lock != EfiLockUninitialized);\r
349\r
350 if (Lock->Lock == EfiLockAcquired) {\r
351 //\r
352 // Lock is already owned, so bail out\r
353 //\r
354 return EFI_ACCESS_DENIED;\r
355 }\r
356\r
357 Lock->OwnerTpl = gBS->RaiseTPL (Lock->Tpl);\r
358\r
359 Lock->Lock = EfiLockAcquired;\r
360\r
361 return EFI_SUCCESS;\r
362}\r
363\r
364/**\r
365 This function transitions a mutual exclusion lock from the acquired state to\r
366 the released state, and restores the system's task priority level to its\r
367 previous level.\r
368\r
369 @param Lock A pointer to the lock to release.\r
370\r
371**/\r
372VOID\r
373EFIAPI\r
374EfiReleaseLock (\r
375 IN EFI_LOCK *Lock\r
376 )\r
377{\r
378 EFI_TPL Tpl;\r
379\r
380 ASSERT (Lock != NULL);\r
381 ASSERT (Lock->Lock == EfiLockAcquired);\r
382\r
383 Tpl = Lock->OwnerTpl;\r
384\r
385 Lock->Lock = EfiLockReleased;\r
386\r
387 gBS->RestoreTPL (Tpl);\r
388}\r
389\r
390/**\r
391 Tests whether a controller handle is being managed by a specific driver.\r
392\r
393 This function tests whether the driver specified by DriverBindingHandle is\r
394 currently managing the controller specified by ControllerHandle. This test\r
395 is performed by evaluating if the the protocol specified by ProtocolGuid is\r
396 present on ControllerHandle and is was opened by DriverBindingHandle with an\r
397 attribute of EFI_OPEN_PROTOCOL_BY_DRIVER.\r
398 If ProtocolGuid is NULL, then ASSERT().\r
399\r
400 @param ControllerHandle A handle for a controller to test.\r
401 @param DriverBindingHandle Specifies the driver binding handle for the\r
402 driver.\r
403 @param ProtocolGuid Specifies the protocol that the driver specified\r
404 by DriverBindingHandle opens in its Start()\r
405 function.\r
406\r
407 @retval EFI_SUCCESS ControllerHandle is managed by the driver\r
408 specifed by DriverBindingHandle.\r
409 @retval EFI_UNSUPPORTED ControllerHandle is not managed by the driver\r
410 specifed by DriverBindingHandle.\r
411\r
412**/\r
413EFI_STATUS\r
414EFIAPI\r
415EfiTestManagedDevice (\r
416 IN CONST EFI_HANDLE ControllerHandle,\r
417 IN CONST EFI_HANDLE DriverBindingHandle,\r
418 IN CONST EFI_GUID *ProtocolGuid\r
419 )\r
420{\r
421 EFI_STATUS Status;\r
422 VOID *ManagedInterface;\r
423\r
424 ASSERT (ProtocolGuid != NULL);\r
425\r
426 Status = gBS->OpenProtocol (\r
427 ControllerHandle,\r
428 (EFI_GUID *) ProtocolGuid,\r
429 &ManagedInterface,\r
430 DriverBindingHandle,\r
431 ControllerHandle,\r
432 EFI_OPEN_PROTOCOL_BY_DRIVER\r
433 );\r
434 if (!EFI_ERROR (Status)) {\r
435 gBS->CloseProtocol (\r
436 ControllerHandle,\r
437 (EFI_GUID *) ProtocolGuid,\r
438 DriverBindingHandle,\r
439 ControllerHandle\r
440 );\r
441 return EFI_UNSUPPORTED;\r
442 }\r
443\r
444 if (Status != EFI_ALREADY_STARTED) {\r
445 return EFI_UNSUPPORTED;\r
446 }\r
447\r
448 return EFI_SUCCESS;\r
449}\r
450\r
451/**\r
452 Tests whether a child handle is a child device of the controller.\r
453\r
454 This function tests whether ChildHandle is one of the children of\r
455 ControllerHandle. This test is performed by checking to see if the protocol\r
456 specified by ProtocolGuid is present on ControllerHandle and opened by\r
457 ChildHandle with an attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.\r
458 If ProtocolGuid is NULL, then ASSERT().\r
459\r
460 @param ControllerHandle A handle for a (parent) controller to test.\r
461 @param ChildHandle A child handle to test.\r
462 @param ConsumsedGuid Supplies the protocol that the child controller\r
463 opens on its parent controller.\r
464\r
465 @retval EFI_SUCCESS ChildHandle is a child of the ControllerHandle.\r
466 @retval EFI_UNSUPPORTED ChildHandle is not a child of the\r
467 ControllerHandle.\r
468\r
469**/\r
470EFI_STATUS\r
471EFIAPI\r
472EfiTestChildHandle (\r
473 IN CONST EFI_HANDLE ControllerHandle,\r
474 IN CONST EFI_HANDLE ChildHandle,\r
475 IN CONST EFI_GUID *ProtocolGuid\r
476 )\r
477{\r
478 EFI_STATUS Status;\r
479 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfoBuffer;\r
480 UINTN EntryCount;\r
481 UINTN Index;\r
482\r
483 ASSERT (ProtocolGuid != NULL);\r
484\r
485 //\r
486 // Retrieve the list of agents that are consuming the specific protocol\r
487 // on ControllerHandle.\r
488 //\r
489 Status = gBS->OpenProtocolInformation (\r
490 ControllerHandle,\r
491 (EFI_GUID *) ProtocolGuid,\r
492 &OpenInfoBuffer,\r
493 &EntryCount\r
494 );\r
495 if (EFI_ERROR (Status)) {\r
496 return EFI_UNSUPPORTED;\r
497 }\r
498\r
499 //\r
500 // Inspect if ChildHandle is one of the agents.\r
501 //\r
502 Status = EFI_UNSUPPORTED;\r
503 for (Index = 0; Index < EntryCount; Index++) {\r
504 if ((OpenInfoBuffer[Index].ControllerHandle == ChildHandle) &&\r
505 (OpenInfoBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {\r
506 Status = EFI_SUCCESS;\r
507 break;\r
508 }\r
509 }\r
510\r
511 FreePool (OpenInfoBuffer);\r
512 return Status;\r
513}\r
514\r
515/**\r
dd51a993 516 This function looks up a Unicode string in UnicodeStringTable.\r
517 If Language is a member of SupportedLanguages and a Unicode\r
518 string is found in UnicodeStringTable that matches the\r
519 language code specified by Language, then it is returned in\r
e386b444 520 UnicodeString.\r
521\r
dd51a993 522 @param Language A pointer to the ISO 639-2\r
523 language code for the Unicode\r
524 string to look up and return.\r
525 \r
526 @param SupportedLanguages A pointer to the set of ISO\r
527 639-2language\r
528 codes that the Unicode string\r
529 table supports. Language must\r
530 be a member of this set.\r
531 \r
532 @param UnicodeStringTable A pointer to the table of\r
533 Unicode strings.\r
534 \r
535 @param UnicodeString A pointer to the Unicode\r
536 string from UnicodeStringTable\r
537 that matches the language\r
538 specified by Language.\r
539\r
540 @retval EFI_SUCCESS The Unicode string that\r
541 matches the language specified\r
542 by Language was found in the\r
543 table of Unicoide strings\r
544 UnicodeStringTable, and it was\r
545 returned in UnicodeString.\r
546 \r
e386b444 547 @retval EFI_INVALID_PARAMETER Language is NULL.\r
dd51a993 548 \r
e386b444 549 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.\r
550 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.\r
dd51a993 551 \r
e386b444 552 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.\r
dd51a993 553 \r
554 @retval EFI_UNSUPPORTED The language specified by\r
555 Language is not a member\r
556 ofSupportedLanguages.\r
557 \r
558 @retval EFI_UNSUPPORTED The language specified by\r
559 Language is not supported by\r
560 UnicodeStringTable.\r
e386b444 561\r
562**/\r
563EFI_STATUS\r
564EFIAPI\r
565LookupUnicodeString (\r
566 IN CONST CHAR8 *Language,\r
567 IN CONST CHAR8 *SupportedLanguages,\r
568 IN CONST EFI_UNICODE_STRING_TABLE *UnicodeStringTable,\r
569 OUT CHAR16 **UnicodeString\r
570 )\r
571{\r
572 //\r
573 // Make sure the parameters are valid\r
574 //\r
575 if (Language == NULL || UnicodeString == NULL) {\r
576 return EFI_INVALID_PARAMETER;\r
577 }\r
578\r
579 //\r
580 // If there are no supported languages, or the Unicode String Table is empty, then the\r
581 // Unicode String specified by Language is not supported by this Unicode String Table\r
582 //\r
583 if (SupportedLanguages == NULL || UnicodeStringTable == NULL) {\r
584 return EFI_UNSUPPORTED;\r
585 }\r
586\r
587 //\r
588 // Make sure Language is in the set of Supported Languages\r
589 //\r
590 while (*SupportedLanguages != 0) {\r
591 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
592\r
593 //\r
594 // Search the Unicode String Table for the matching Language specifier\r
595 //\r
596 while (UnicodeStringTable->Language != NULL) {\r
597 if (CompareIso639LanguageCode (Language, UnicodeStringTable->Language)) {\r
598\r
599 //\r
600 // A matching string was found, so return it\r
601 //\r
602 *UnicodeString = UnicodeStringTable->UnicodeString;\r
603 return EFI_SUCCESS;\r
604 }\r
605\r
606 UnicodeStringTable++;\r
607 }\r
608\r
609 return EFI_UNSUPPORTED;\r
610 }\r
611\r
612 SupportedLanguages += 3;\r
613 }\r
614\r
615 return EFI_UNSUPPORTED;\r
616}\r
617\r
dd51a993 618\r
619\r
e386b444 620/**\r
dd51a993 621 This function looks up a Unicode string in UnicodeStringTable.\r
622 If Language is a member of SupportedLanguages and a Unicode\r
623 string is found in UnicodeStringTable that matches the\r
624 language code specified by Language, then it is returned in\r
625 UnicodeString.\r
626\r
627 @param Language A pointer to the ISO 639-2 or\r
628 RFC 3066 language code for the\r
629 Unicode string to look up and\r
630 return.\r
631 \r
632 @param SupportedLanguages A pointer to the set of ISO\r
633 639-2 or RFC 3066 language\r
634 codes that the Unicode string\r
635 table supports. Language must\r
636 be a member of this set.\r
637 \r
638 @param UnicodeStringTable A pointer to the table of\r
639 Unicode strings.\r
640 \r
641 @param UnicodeString A pointer to the Unicode\r
642 string from UnicodeStringTable\r
643 that matches the language\r
644 specified by Language.\r
645\r
646 @param Iso639Language Specify the language code\r
647 format supported. If true,\r
648 then the format follow ISO\r
649 639-2. If false, then it\r
650 follows RFC3066.\r
651\r
652 @retval EFI_SUCCESS The Unicode string that\r
653 matches the language specified\r
654 by Language was found in the\r
655 table of Unicoide strings\r
656 UnicodeStringTable, and it was\r
657 returned in UnicodeString.\r
658 \r
659 @retval EFI_INVALID_PARAMETER Language is NULL.\r
660 \r
661 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.\r
662 \r
663 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.\r
664 \r
665 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.\r
666 \r
667 @retval EFI_UNSUPPORTED The language specified by\r
668 Language is not a member\r
669 ofSupportedLanguages.\r
670 \r
671 @retval EFI_UNSUPPORTED The language specified by\r
672 Language is not supported by\r
673 UnicodeStringTable.\r
674\r
675**/\r
676EFI_STATUS\r
677EFIAPI\r
678LookupUnicodeString2 (\r
679 IN CONST CHAR8 *Language,\r
680 IN CONST CHAR8 *SupportedLanguages,\r
681 IN CONST EFI_UNICODE_STRING_TABLE *UnicodeStringTable,\r
682 OUT CHAR16 **UnicodeString,\r
683 IN BOOLEAN Iso639Language\r
684 )\r
685{\r
686 BOOLEAN Found;\r
687 UINTN Index;\r
688 CHAR8 *LanguageString;\r
689\r
690 //\r
691 // Make sure the parameters are valid\r
692 //\r
693 if (Language == NULL || UnicodeString == NULL) {\r
694 return EFI_INVALID_PARAMETER;\r
695 }\r
696\r
697 //\r
698 // If there are no supported languages, or the Unicode String Table is empty, then the\r
699 // Unicode String specified by Language is not supported by this Unicode String Table\r
700 //\r
701 if (SupportedLanguages == NULL || UnicodeStringTable == NULL) {\r
702 return EFI_UNSUPPORTED;\r
703 }\r
704\r
705 //\r
706 // Make sure Language is in the set of Supported Languages\r
707 //\r
708 Found = FALSE;\r
709 while (*SupportedLanguages != 0) {\r
710 if (Iso639Language) {\r
711 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
712 Found = TRUE;\r
713 break;\r
714 }\r
715 SupportedLanguages += 3;\r
716 } else {\r
717 for (Index = 0; SupportedLanguages[Index] != 0 && SupportedLanguages[Index] != ';'; Index++);\r
718 if (AsciiStrnCmp(SupportedLanguages, Language, Index) == 0) {\r
719 Found = TRUE;\r
720 break;\r
721 }\r
722 SupportedLanguages += Index;\r
723 for (; *SupportedLanguages != 0 && *SupportedLanguages == ';'; SupportedLanguages++);\r
724 }\r
725 }\r
726\r
727 //\r
728 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED\r
729 //\r
730 if (!Found) {\r
731 return EFI_UNSUPPORTED;\r
732 }\r
733\r
734 //\r
735 // Search the Unicode String Table for the matching Language specifier\r
736 //\r
737 while (UnicodeStringTable->Language != NULL) {\r
738 LanguageString = UnicodeStringTable->Language;\r
739 while (0 != *LanguageString) {\r
740 for (Index = 0 ;LanguageString[Index] != 0 && LanguageString[Index] != ';'; Index++);\r
741 if (AsciiStrnCmp(LanguageString, Language, Index) == 0) {\r
742 *UnicodeString = UnicodeStringTable->UnicodeString;\r
743 return EFI_SUCCESS;\r
744 }\r
745 LanguageString += Index;\r
746 for (Index = 0 ;LanguageString[Index] != 0 && LanguageString[Index] == ';'; Index++);\r
747 }\r
748 UnicodeStringTable++;\r
749 }\r
750\r
751 return EFI_UNSUPPORTED;\r
752}\r
753\r
754\r
755/**\r
756 \r
e386b444 757 This function adds a Unicode string to UnicodeStringTable.\r
dd51a993 758 If Language is a member of SupportedLanguages then\r
759 UnicodeString is added to UnicodeStringTable. New buffers are\r
760 allocated for both Language and UnicodeString. The contents\r
761 of Language and UnicodeString are copied into these new\r
762 buffers. These buffers are automatically freed when\r
e386b444 763 FreeUnicodeStringTable() is called.\r
764\r
dd51a993 765 @param Language A pointer to the ISO 639-2\r
766 language code for the Unicode\r
e386b444 767 string to add.\r
dd51a993 768 \r
769 @param SupportedLanguages A pointer to the set of ISO\r
770 639-2 language codes that the\r
771 Unicode string table supports.\r
772 Language must be a member of\r
773 this set.\r
774 \r
775 @param UnicodeStringTable A pointer to the table of\r
776 Unicode strings.\r
777 \r
778 @param UnicodeString A pointer to the Unicode\r
779 string to add.\r
780\r
781 @retval EFI_SUCCESS The Unicode string that\r
782 matches the language specified\r
783 by Language was found in the\r
784 table of Unicode strings\r
785 UnicodeStringTable, and it was\r
e386b444 786 returned in UnicodeString.\r
dd51a993 787 \r
e386b444 788 @retval EFI_INVALID_PARAMETER Language is NULL.\r
dd51a993 789 \r
e386b444 790 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.\r
dd51a993 791 \r
e386b444 792 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.\r
dd51a993 793 \r
e386b444 794 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.\r
dd51a993 795 \r
796 @retval EFI_ALREADY_STARTED A Unicode string with language\r
797 Language is already present in\r
798 UnicodeStringTable.\r
799 \r
800 @retval EFI_OUT_OF_RESOURCES There is not enough memory to\r
801 add another Unicode string to\r
802 UnicodeStringTable.\r
803 \r
804 @retval EFI_UNSUPPORTED The language specified by\r
805 Language is not a member of\r
806 SupportedLanguages.\r
e386b444 807\r
808**/\r
809EFI_STATUS\r
810EFIAPI\r
811AddUnicodeString (\r
812 IN CONST CHAR8 *Language,\r
813 IN CONST CHAR8 *SupportedLanguages,\r
814 IN EFI_UNICODE_STRING_TABLE **UnicodeStringTable,\r
815 IN CONST CHAR16 *UnicodeString\r
816 )\r
817{\r
818 UINTN NumberOfEntries;\r
819 EFI_UNICODE_STRING_TABLE *OldUnicodeStringTable;\r
820 EFI_UNICODE_STRING_TABLE *NewUnicodeStringTable;\r
821 UINTN UnicodeStringLength;\r
822\r
823 //\r
824 // Make sure the parameter are valid\r
825 //\r
826 if (Language == NULL || UnicodeString == NULL || UnicodeStringTable == NULL) {\r
827 return EFI_INVALID_PARAMETER;\r
828 }\r
829\r
830 //\r
831 // If there are no supported languages, then a Unicode String can not be added\r
832 //\r
833 if (SupportedLanguages == NULL) {\r
834 return EFI_UNSUPPORTED;\r
835 }\r
836\r
837 //\r
838 // If the Unicode String is empty, then a Unicode String can not be added\r
839 //\r
840 if (UnicodeString[0] == 0) {\r
841 return EFI_INVALID_PARAMETER;\r
842 }\r
843\r
844 //\r
845 // Make sure Language is a member of SupportedLanguages\r
846 //\r
847 while (*SupportedLanguages != 0) {\r
848 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
849\r
850 //\r
851 // Determine the size of the Unicode String Table by looking for a NULL Language entry\r
852 //\r
853 NumberOfEntries = 0;\r
854 if (*UnicodeStringTable != NULL) {\r
855 OldUnicodeStringTable = *UnicodeStringTable;\r
856 while (OldUnicodeStringTable->Language != NULL) {\r
857 if (CompareIso639LanguageCode (Language, OldUnicodeStringTable->Language)) {\r
858 return EFI_ALREADY_STARTED;\r
859 }\r
860\r
861 OldUnicodeStringTable++;\r
862 NumberOfEntries++;\r
863 }\r
864 }\r
865\r
866 //\r
867 // Allocate space for a new Unicode String Table. It must hold the current number of\r
868 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table\r
869 // marker\r
870 //\r
871 NewUnicodeStringTable = AllocatePool ((NumberOfEntries + 2) * sizeof (EFI_UNICODE_STRING_TABLE));\r
872 if (NewUnicodeStringTable == NULL) {\r
873 return EFI_OUT_OF_RESOURCES;\r
874 }\r
875\r
876 //\r
877 // If the current Unicode String Table contains any entries, then copy them to the\r
878 // newly allocated Unicode String Table.\r
879 //\r
880 if (*UnicodeStringTable != NULL) {\r
881 CopyMem (\r
882 NewUnicodeStringTable,\r
883 *UnicodeStringTable,\r
884 NumberOfEntries * sizeof (EFI_UNICODE_STRING_TABLE)\r
885 );\r
886 }\r
887\r
888 //\r
889 // Allocate space for a copy of the Language specifier\r
890 //\r
891 NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (3, Language);\r
892 if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) {\r
893 gBS->FreePool (NewUnicodeStringTable);\r
894 return EFI_OUT_OF_RESOURCES;\r
895 }\r
896\r
897 //\r
898 // Compute the length of the Unicode String\r
899 //\r
900 for (UnicodeStringLength = 0; UnicodeString[UnicodeStringLength] != 0; UnicodeStringLength++)\r
901 ;\r
902\r
903 //\r
904 // Allocate space for a copy of the Unicode String\r
905 //\r
906 NewUnicodeStringTable[NumberOfEntries].UnicodeString = AllocateCopyPool (\r
907 (UnicodeStringLength + 1) * sizeof (CHAR16),\r
908 UnicodeString\r
909 );\r
910 if (NewUnicodeStringTable[NumberOfEntries].UnicodeString == NULL) {\r
911 gBS->FreePool (NewUnicodeStringTable[NumberOfEntries].Language);\r
912 gBS->FreePool (NewUnicodeStringTable);\r
913 return EFI_OUT_OF_RESOURCES;\r
914 }\r
915\r
916 //\r
917 // Mark the end of the Unicode String Table\r
918 //\r
919 NewUnicodeStringTable[NumberOfEntries + 1].Language = NULL;\r
920 NewUnicodeStringTable[NumberOfEntries + 1].UnicodeString = NULL;\r
921\r
922 //\r
923 // Free the old Unicode String Table\r
924 //\r
925 if (*UnicodeStringTable != NULL) {\r
926 gBS->FreePool (*UnicodeStringTable);\r
927 }\r
928\r
929 //\r
930 // Point UnicodeStringTable at the newly allocated Unicode String Table\r
931 //\r
932 *UnicodeStringTable = NewUnicodeStringTable;\r
933\r
934 return EFI_SUCCESS;\r
935 }\r
936\r
937 SupportedLanguages += 3;\r
938 }\r
939\r
940 return EFI_UNSUPPORTED;\r
941}\r
942\r
dd51a993 943\r
944/**\r
945 \r
946 This function adds a Unicode string to UnicodeStringTable.\r
947 If Language is a member of SupportedLanguages then\r
948 UnicodeString is added to UnicodeStringTable. New buffers are\r
949 allocated for both Language and UnicodeString. The contents\r
950 of Language and UnicodeString are copied into these new\r
951 buffers. These buffers are automatically freed when\r
952 FreeUnicodeStringTable() is called.\r
953\r
954 @param Language A pointer to the ISO 639-2 or\r
955 RFC 3066 language code for the\r
956 Unicode string to add.\r
957 \r
958 @param SupportedLanguages A pointer to the set of ISO\r
959 639-2 or RFC 3.66 language\r
960 codes that the Unicode string\r
961 table supports. Language must\r
962 be a member of this set.\r
963 \r
964 @param UnicodeStringTable A pointer to the table of\r
965 Unicode strings.\r
966 \r
967 @param UnicodeString A pointer to the Unicode\r
968 string to add.\r
969 \r
970 @param Iso639Language Specify the language code\r
971 format supported. If true,\r
972 then the format follow ISO\r
973 639-2. If false, then it\r
974 follows RFC3066.\r
975\r
976 @retval EFI_SUCCESS The Unicode string that\r
977 matches the language specified\r
978 by Language was found in the\r
979 table of Unicode strings\r
980 UnicodeStringTable, and it was\r
981 returned in UnicodeString.\r
982 \r
983 @retval EFI_INVALID_PARAMETER Language is NULL.\r
984 \r
985 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.\r
986 \r
987 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.\r
988 \r
989 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.\r
990 \r
991 @retval EFI_ALREADY_STARTED A Unicode string with language\r
992 Language is already present in\r
993 UnicodeStringTable.\r
994 \r
995 @retval EFI_OUT_OF_RESOURCES There is not enough memory to\r
996 add another Unicode string to\r
997 UnicodeStringTable.\r
998 \r
999 @retval EFI_UNSUPPORTED The language specified by\r
1000 Language is not a member of\r
1001 SupportedLanguages.\r
1002\r
1003**/\r
1004EFI_STATUS\r
1005EFIAPI\r
1006AddUnicodeString2 (\r
1007 IN CONST CHAR8 *Language,\r
1008 IN CONST CHAR8 *SupportedLanguages,\r
1009 IN EFI_UNICODE_STRING_TABLE **UnicodeStringTable,\r
1010 IN CONST CHAR16 *UnicodeString,\r
1011 IN BOOLEAN Iso639Language\r
1012 )\r
1013{\r
1014 UINTN NumberOfEntries;\r
1015 EFI_UNICODE_STRING_TABLE *OldUnicodeStringTable;\r
1016 EFI_UNICODE_STRING_TABLE *NewUnicodeStringTable;\r
1017 UINTN UnicodeStringLength;\r
1018 BOOLEAN Found;\r
1019 UINTN Index;\r
1020 CHAR8 *LanguageString;\r
1021\r
1022 //\r
1023 // Make sure the parameter are valid\r
1024 //\r
1025 if (Language == NULL || UnicodeString == NULL || UnicodeStringTable == NULL) {\r
1026 return EFI_INVALID_PARAMETER;\r
1027 }\r
1028\r
1029 //\r
1030 // If there are no supported languages, then a Unicode String can not be added\r
1031 //\r
1032 if (SupportedLanguages == NULL) {\r
1033 return EFI_UNSUPPORTED;\r
1034 }\r
1035\r
1036 //\r
1037 // If the Unicode String is empty, then a Unicode String can not be added\r
1038 //\r
1039 if (UnicodeString[0] == 0) {\r
1040 return EFI_INVALID_PARAMETER;\r
1041 }\r
1042\r
1043 //\r
1044 // Make sure Language is a member of SupportedLanguages\r
1045 //\r
1046 Found = FALSE;\r
1047 while (*SupportedLanguages != 0) {\r
1048 if (Iso639Language) {\r
1049 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
1050 Found = TRUE;\r
1051 break;\r
1052 }\r
1053 SupportedLanguages += 3;\r
1054 } else {\r
1055 for (Index = 0; SupportedLanguages[Index] != 0 && SupportedLanguages[Index] != ';'; Index++);\r
1056 if (AsciiStrnCmp(SupportedLanguages, Language, Index) == 0) {\r
1057 Found = TRUE;\r
1058 break;\r
1059 }\r
1060 SupportedLanguages += Index;\r
1061 for (; *SupportedLanguages != 0 && *SupportedLanguages == ';'; SupportedLanguages++);\r
1062 }\r
1063 }\r
1064\r
1065 //\r
1066 // If Language is not a member of SupportedLanguages, then return EFI_UNSUPPORTED\r
1067 //\r
1068 if (!Found) {\r
1069 return EFI_UNSUPPORTED;\r
1070 }\r
1071\r
1072 //\r
1073 // Determine the size of the Unicode String Table by looking for a NULL Language entry\r
1074 //\r
1075 NumberOfEntries = 0;\r
1076 if (*UnicodeStringTable != NULL) {\r
1077 OldUnicodeStringTable = *UnicodeStringTable;\r
1078 while (OldUnicodeStringTable->Language != NULL) {\r
1079 LanguageString = OldUnicodeStringTable->Language;\r
1080\r
1081 while (*LanguageString) {\r
1082 for (Index = 0; LanguageString[Index] != 0 && LanguageString[Index] != ';'; Index++);\r
1083\r
1084 if (AsciiStrnCmp (Language, LanguageString, Index) == 0) { \r
1085 return EFI_ALREADY_STARTED;\r
1086 }\r
1087 LanguageString += Index;\r
1088 for (; *LanguageString != 0 && *LanguageString == ';'; LanguageString++);\r
1089 }\r
1090 OldUnicodeStringTable++;\r
1091 NumberOfEntries++;\r
1092 }\r
1093 }\r
1094\r
1095 //\r
1096 // Allocate space for a new Unicode String Table. It must hold the current number of\r
1097 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table\r
1098 // marker\r
1099 //\r
1100 NewUnicodeStringTable = AllocatePool ((NumberOfEntries + 2) * sizeof (EFI_UNICODE_STRING_TABLE));\r
1101 if (NewUnicodeStringTable == NULL) {\r
1102 return EFI_OUT_OF_RESOURCES;\r
1103 }\r
1104\r
1105 //\r
1106 // If the current Unicode String Table contains any entries, then copy them to the\r
1107 // newly allocated Unicode String Table.\r
1108 //\r
1109 if (*UnicodeStringTable != NULL) {\r
1110 CopyMem (\r
1111 NewUnicodeStringTable,\r
1112 *UnicodeStringTable,\r
1113 NumberOfEntries * sizeof (EFI_UNICODE_STRING_TABLE)\r
1114 );\r
1115 }\r
1116\r
1117 //\r
1118 // Allocate space for a copy of the Language specifier\r
1119 //\r
1120 NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (AsciiStrSize(Language), Language);\r
1121 if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) {\r
1122 gBS->FreePool (NewUnicodeStringTable);\r
1123 return EFI_OUT_OF_RESOURCES;\r
1124 }\r
1125\r
1126 //\r
1127 // Compute the length of the Unicode String\r
1128 //\r
1129 for (UnicodeStringLength = 0; UnicodeString[UnicodeStringLength] != 0; UnicodeStringLength++);\r
1130\r
1131 //\r
1132 // Allocate space for a copy of the Unicode String\r
1133 //\r
1134 NewUnicodeStringTable[NumberOfEntries].UnicodeString = AllocateCopyPool (StrSize (UnicodeString), UnicodeString);\r
1135 if (NewUnicodeStringTable[NumberOfEntries].UnicodeString == NULL) {\r
1136 gBS->FreePool (NewUnicodeStringTable[NumberOfEntries].Language);\r
1137 gBS->FreePool (NewUnicodeStringTable);\r
1138 return EFI_OUT_OF_RESOURCES;\r
1139 }\r
1140\r
1141 //\r
1142 // Mark the end of the Unicode String Table\r
1143 //\r
1144 NewUnicodeStringTable[NumberOfEntries + 1].Language = NULL;\r
1145 NewUnicodeStringTable[NumberOfEntries + 1].UnicodeString = NULL;\r
1146\r
1147 //\r
1148 // Free the old Unicode String Table\r
1149 //\r
1150 if (*UnicodeStringTable != NULL) {\r
1151 gBS->FreePool (*UnicodeStringTable);\r
1152 }\r
1153\r
1154 //\r
1155 // Point UnicodeStringTable at the newly allocated Unicode String Table\r
1156 //\r
1157 *UnicodeStringTable = NewUnicodeStringTable;\r
1158\r
1159 return EFI_SUCCESS;\r
1160}\r
1161\r
e386b444 1162/**\r
1163 This function frees the table of Unicode strings in UnicodeStringTable.\r
1164 If UnicodeStringTable is NULL, then EFI_SUCCESS is returned.\r
dd51a993 1165 Otherwise, each language code, and each Unicode string in the Unicode string \r
e386b444 1166 table are freed, and EFI_SUCCESS is returned.\r
1167\r
1168 @param UnicodeStringTable A pointer to the table of Unicode strings.\r
1169\r
1170 @retval EFI_SUCCESS The Unicode string table was freed.\r
1171\r
1172**/\r
1173EFI_STATUS\r
1174EFIAPI\r
1175FreeUnicodeStringTable (\r
1176 IN EFI_UNICODE_STRING_TABLE *UnicodeStringTable\r
1177 )\r
1178{\r
1179 UINTN Index;\r
1180\r
1181 //\r
1182 // If the Unicode String Table is NULL, then it is already freed\r
1183 //\r
1184 if (UnicodeStringTable == NULL) {\r
1185 return EFI_SUCCESS;\r
1186 }\r
1187\r
1188 //\r
1189 // Loop through the Unicode String Table until we reach the end of table marker\r
1190 //\r
1191 for (Index = 0; UnicodeStringTable[Index].Language != NULL; Index++) {\r
1192\r
1193 //\r
1194 // Free the Language string from the Unicode String Table\r
1195 //\r
1196 gBS->FreePool (UnicodeStringTable[Index].Language);\r
1197\r
1198 //\r
1199 // Free the Unicode String from the Unicode String Table\r
1200 //\r
1201 if (UnicodeStringTable[Index].UnicodeString != NULL) {\r
1202 gBS->FreePool (UnicodeStringTable[Index].UnicodeString);\r
1203 }\r
1204 }\r
1205\r
1206 //\r
1207 // Free the Unicode String Table itself\r
1208 //\r
1209 gBS->FreePool (UnicodeStringTable);\r
1210\r
1211 return EFI_SUCCESS;\r
1212}\r
1213\r