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