]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/UefiLib/UefiLib.c
comment CpuRuntimeDxe driver to not break Nt32Pkg build
[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
20#include "CommonHeader.h"\r
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
516 This function looks up a Unicode string in UnicodeStringTable. If Language is\r
517 a member of SupportedLanguages and a Unicode string is found in UnicodeStringTable\r
518 that matches the language code specified by Language, then it is returned in\r
519 UnicodeString.\r
520\r
521 @param Language A pointer to the ISO 639-2 language code for the\r
522 Unicode string to look up and return.\r
523 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes\r
524 that the Unicode string table supports. Language\r
525 must be a member of this set.\r
526 @param UnicodeStringTable A pointer to the table of Unicode strings.\r
527 @param UnicodeString A pointer to the Unicode string from UnicodeStringTable\r
528 that matches the language specified by Language.\r
529\r
530 @retval EFI_SUCCESS The Unicode string that matches the language\r
531 specified by Language was found\r
532 in the table of Unicoide strings UnicodeStringTable,\r
533 and it was returned in UnicodeString.\r
534 @retval EFI_INVALID_PARAMETER Language is NULL.\r
535 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.\r
536 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.\r
537 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.\r
538 @retval EFI_UNSUPPORTED The language specified by Language is not a\r
539 member of SupportedLanguages.\r
540 @retval EFI_UNSUPPORTED The language specified by Language is not\r
541 supported by UnicodeStringTable.\r
542\r
543**/\r
544EFI_STATUS\r
545EFIAPI\r
546LookupUnicodeString (\r
547 IN CONST CHAR8 *Language,\r
548 IN CONST CHAR8 *SupportedLanguages,\r
549 IN CONST EFI_UNICODE_STRING_TABLE *UnicodeStringTable,\r
550 OUT CHAR16 **UnicodeString\r
551 )\r
552{\r
553 //\r
554 // Make sure the parameters are valid\r
555 //\r
556 if (Language == NULL || UnicodeString == NULL) {\r
557 return EFI_INVALID_PARAMETER;\r
558 }\r
559\r
560 //\r
561 // If there are no supported languages, or the Unicode String Table is empty, then the\r
562 // Unicode String specified by Language is not supported by this Unicode String Table\r
563 //\r
564 if (SupportedLanguages == NULL || UnicodeStringTable == NULL) {\r
565 return EFI_UNSUPPORTED;\r
566 }\r
567\r
568 //\r
569 // Make sure Language is in the set of Supported Languages\r
570 //\r
571 while (*SupportedLanguages != 0) {\r
572 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
573\r
574 //\r
575 // Search the Unicode String Table for the matching Language specifier\r
576 //\r
577 while (UnicodeStringTable->Language != NULL) {\r
578 if (CompareIso639LanguageCode (Language, UnicodeStringTable->Language)) {\r
579\r
580 //\r
581 // A matching string was found, so return it\r
582 //\r
583 *UnicodeString = UnicodeStringTable->UnicodeString;\r
584 return EFI_SUCCESS;\r
585 }\r
586\r
587 UnicodeStringTable++;\r
588 }\r
589\r
590 return EFI_UNSUPPORTED;\r
591 }\r
592\r
593 SupportedLanguages += 3;\r
594 }\r
595\r
596 return EFI_UNSUPPORTED;\r
597}\r
598\r
599/**\r
600 This function adds a Unicode string to UnicodeStringTable.\r
601 If Language is a member of SupportedLanguages then UnicodeString is added to\r
602 UnicodeStringTable. New buffers are allocated for both Language and\r
603 UnicodeString. The contents of Language and UnicodeString are copied into\r
604 these new buffers. These buffers are automatically freed when\r
605 FreeUnicodeStringTable() is called.\r
606\r
607 @param Language A pointer to the ISO 639-2 language code for the Unicode\r
608 string to add.\r
609 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes\r
610 that the Unicode string table supports.\r
611 Language must be a member of this set.\r
612 @param UnicodeStringTable A pointer to the table of Unicode strings.\r
613 @param UnicodeString A pointer to the Unicode string to add.\r
614\r
615 @retval EFI_SUCCESS The Unicode string that matches the language\r
616 specified by Language was found in the table of\r
617 Unicode strings UnicodeStringTable, and it was\r
618 returned in UnicodeString.\r
619 @retval EFI_INVALID_PARAMETER Language is NULL.\r
620 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.\r
621 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.\r
622 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.\r
623 @retval EFI_ALREADY_STARTED A Unicode string with language Language is\r
624 already present in UnicodeStringTable.\r
625 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another\r
626 Unicode string to UnicodeStringTable.\r
627 @retval EFI_UNSUPPORTED The language specified by Language is not a\r
628 member of SupportedLanguages.\r
629\r
630**/\r
631EFI_STATUS\r
632EFIAPI\r
633AddUnicodeString (\r
634 IN CONST CHAR8 *Language,\r
635 IN CONST CHAR8 *SupportedLanguages,\r
636 IN EFI_UNICODE_STRING_TABLE **UnicodeStringTable,\r
637 IN CONST CHAR16 *UnicodeString\r
638 )\r
639{\r
640 UINTN NumberOfEntries;\r
641 EFI_UNICODE_STRING_TABLE *OldUnicodeStringTable;\r
642 EFI_UNICODE_STRING_TABLE *NewUnicodeStringTable;\r
643 UINTN UnicodeStringLength;\r
644\r
645 //\r
646 // Make sure the parameter are valid\r
647 //\r
648 if (Language == NULL || UnicodeString == NULL || UnicodeStringTable == NULL) {\r
649 return EFI_INVALID_PARAMETER;\r
650 }\r
651\r
652 //\r
653 // If there are no supported languages, then a Unicode String can not be added\r
654 //\r
655 if (SupportedLanguages == NULL) {\r
656 return EFI_UNSUPPORTED;\r
657 }\r
658\r
659 //\r
660 // If the Unicode String is empty, then a Unicode String can not be added\r
661 //\r
662 if (UnicodeString[0] == 0) {\r
663 return EFI_INVALID_PARAMETER;\r
664 }\r
665\r
666 //\r
667 // Make sure Language is a member of SupportedLanguages\r
668 //\r
669 while (*SupportedLanguages != 0) {\r
670 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
671\r
672 //\r
673 // Determine the size of the Unicode String Table by looking for a NULL Language entry\r
674 //\r
675 NumberOfEntries = 0;\r
676 if (*UnicodeStringTable != NULL) {\r
677 OldUnicodeStringTable = *UnicodeStringTable;\r
678 while (OldUnicodeStringTable->Language != NULL) {\r
679 if (CompareIso639LanguageCode (Language, OldUnicodeStringTable->Language)) {\r
680 return EFI_ALREADY_STARTED;\r
681 }\r
682\r
683 OldUnicodeStringTable++;\r
684 NumberOfEntries++;\r
685 }\r
686 }\r
687\r
688 //\r
689 // Allocate space for a new Unicode String Table. It must hold the current number of\r
690 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table\r
691 // marker\r
692 //\r
693 NewUnicodeStringTable = AllocatePool ((NumberOfEntries + 2) * sizeof (EFI_UNICODE_STRING_TABLE));\r
694 if (NewUnicodeStringTable == NULL) {\r
695 return EFI_OUT_OF_RESOURCES;\r
696 }\r
697\r
698 //\r
699 // If the current Unicode String Table contains any entries, then copy them to the\r
700 // newly allocated Unicode String Table.\r
701 //\r
702 if (*UnicodeStringTable != NULL) {\r
703 CopyMem (\r
704 NewUnicodeStringTable,\r
705 *UnicodeStringTable,\r
706 NumberOfEntries * sizeof (EFI_UNICODE_STRING_TABLE)\r
707 );\r
708 }\r
709\r
710 //\r
711 // Allocate space for a copy of the Language specifier\r
712 //\r
713 NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (3, Language);\r
714 if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) {\r
715 gBS->FreePool (NewUnicodeStringTable);\r
716 return EFI_OUT_OF_RESOURCES;\r
717 }\r
718\r
719 //\r
720 // Compute the length of the Unicode String\r
721 //\r
722 for (UnicodeStringLength = 0; UnicodeString[UnicodeStringLength] != 0; UnicodeStringLength++)\r
723 ;\r
724\r
725 //\r
726 // Allocate space for a copy of the Unicode String\r
727 //\r
728 NewUnicodeStringTable[NumberOfEntries].UnicodeString = AllocateCopyPool (\r
729 (UnicodeStringLength + 1) * sizeof (CHAR16),\r
730 UnicodeString\r
731 );\r
732 if (NewUnicodeStringTable[NumberOfEntries].UnicodeString == NULL) {\r
733 gBS->FreePool (NewUnicodeStringTable[NumberOfEntries].Language);\r
734 gBS->FreePool (NewUnicodeStringTable);\r
735 return EFI_OUT_OF_RESOURCES;\r
736 }\r
737\r
738 //\r
739 // Mark the end of the Unicode String Table\r
740 //\r
741 NewUnicodeStringTable[NumberOfEntries + 1].Language = NULL;\r
742 NewUnicodeStringTable[NumberOfEntries + 1].UnicodeString = NULL;\r
743\r
744 //\r
745 // Free the old Unicode String Table\r
746 //\r
747 if (*UnicodeStringTable != NULL) {\r
748 gBS->FreePool (*UnicodeStringTable);\r
749 }\r
750\r
751 //\r
752 // Point UnicodeStringTable at the newly allocated Unicode String Table\r
753 //\r
754 *UnicodeStringTable = NewUnicodeStringTable;\r
755\r
756 return EFI_SUCCESS;\r
757 }\r
758\r
759 SupportedLanguages += 3;\r
760 }\r
761\r
762 return EFI_UNSUPPORTED;\r
763}\r
764\r
765/**\r
766 This function frees the table of Unicode strings in UnicodeStringTable.\r
767 If UnicodeStringTable is NULL, then EFI_SUCCESS is returned.\r
768 Otherwise, each language code, and each Unicode string in the Unicode string\r
769 table are freed, and EFI_SUCCESS is returned.\r
770\r
771 @param UnicodeStringTable A pointer to the table of Unicode strings.\r
772\r
773 @retval EFI_SUCCESS The Unicode string table was freed.\r
774\r
775**/\r
776EFI_STATUS\r
777EFIAPI\r
778FreeUnicodeStringTable (\r
779 IN EFI_UNICODE_STRING_TABLE *UnicodeStringTable\r
780 )\r
781{\r
782 UINTN Index;\r
783\r
784 //\r
785 // If the Unicode String Table is NULL, then it is already freed\r
786 //\r
787 if (UnicodeStringTable == NULL) {\r
788 return EFI_SUCCESS;\r
789 }\r
790\r
791 //\r
792 // Loop through the Unicode String Table until we reach the end of table marker\r
793 //\r
794 for (Index = 0; UnicodeStringTable[Index].Language != NULL; Index++) {\r
795\r
796 //\r
797 // Free the Language string from the Unicode String Table\r
798 //\r
799 gBS->FreePool (UnicodeStringTable[Index].Language);\r
800\r
801 //\r
802 // Free the Unicode String from the Unicode String Table\r
803 //\r
804 if (UnicodeStringTable[Index].UnicodeString != NULL) {\r
805 gBS->FreePool (UnicodeStringTable[Index].UnicodeString);\r
806 }\r
807 }\r
808\r
809 //\r
810 // Free the Unicode String Table itself\r
811 //\r
812 gBS->FreePool (UnicodeStringTable);\r
813\r
814 return EFI_SUCCESS;\r
815}\r
816\r
817/**\r
818 Intialize a driver by installing the Driver Binding Protocol onto the\r
819 driver's DriverBindingHandle. This is typically the same as the driver's\r
820 ImageHandle, but it can be different if the driver produces multiple\r
821 DriverBinding Protocols. This function also initializes the EFI Driver\r
822 Library that initializes the global variables gST, gBS, gRT.\r
823\r
824 @param ImageHandle The image handle of the driver\r
825 @param SystemTable The EFI System Table that was passed to the driver's entry point\r
826 @param DriverBinding A Driver Binding Protocol instance that this driver is producing\r
827 @param DriverBindingHandle The handle that DriverBinding is to be installe onto. If this\r
828 parameter is NULL, then a new handle is created.\r
829\r
830 @retval EFI_SUCCESS DriverBinding is installed onto DriverBindingHandle\r
831 @retval Other Status from gBS->InstallProtocolInterface()\r
832\r
833**/\r
834EFI_STATUS\r
835EfiLibInstallDriverBinding (\r
836 IN const EFI_HANDLE ImageHandle,\r
837 IN const EFI_SYSTEM_TABLE *SystemTable,\r
838 IN EFI_DRIVER_BINDING_PROTOCOL *DriverBinding,\r
839 IN EFI_HANDLE DriverBindingHandle\r
840 )\r
841{\r
842 //\r
843 // bugbug:Need to implement ...\r
844 //\r
845\r
846 return EFI_SUCCESS;\r
847}\r
848\r
849/**\r
850 Intialize a driver by installing the Driver Binding Protocol onto the\r
851 driver's DriverBindingHandle. This is typically the same as the driver's\r
852 ImageHandle, but it can be different if the driver produces multiple\r
853 DriverBinding Protocols. This function also initializes the EFI Driver\r
854 Library that initializes the global variables gST, gBS, gRT.\r
855\r
856 @ImageHandle The image handle of the driver\r
857 @SystemTable The EFI System Table that was passed to the driver's entry point\r
858 @DriverBinding A Driver Binding Protocol instance that this driver is producing\r
859 @DriverBindingHandle The handle that DriverBinding is to be installe onto. If this\r
860 parameter is NULL, then a new handle is created.\r
861 @ComponentName A Component Name Protocol instance that this driver is producing\r
862 @DriverConfiguration A Driver Configuration Protocol instance that this driver is producing\r
863 @DriverDiagnostics A Driver Diagnostics Protocol instance that this driver is producing\r
864\r
865 @retval EFI_SUCCESS DriverBinding is installed onto DriverBindingHandle\r
866 @retval Other Status from gBS->InstallProtocolInterface()\r
867\r
868**/\r
869EFI_STATUS\r
870EfiLibInstallAllDriverProtocols (\r
871 IN const EFI_HANDLE ImageHandle,\r
872 IN const EFI_SYSTEM_TABLE *SystemTable,\r
873 IN EFI_DRIVER_BINDING_PROTOCOL *DriverBinding,\r
874 IN EFI_HANDLE DriverBindingHandle,\r
875 IN const EFI_COMPONENT_NAME_PROTOCOL *ComponentName, OPTIONAL\r
876 IN const EFI_DRIVER_CONFIGURATION_PROTOCOL *DriverConfiguration, OPTIONAL\r
877 IN const EFI_DRIVER_DIAGNOSTICS_PROTOCOL *DriverDiagnostics OPTIONAL\r
878 )\r
879{\r
880 //\r
881 // bugbug:Need to implement ...\r
882 //\r
883\r
884 return EFI_SUCCESS;\r
885}\r
886\r