1) Added BIT0, BIT1, …, BIT63 to the Base Defines
[mirror_edk2.git] / MdePkg / Library / UefiLib / UefiLib.c
CommitLineData
878ddf1f 1/** @file\r
2 Mde UEFI library functions.\r
3\r
ca21f1a2 4 Copyright (c) 2006 - 2007, Intel Corporation<BR>\r
878ddf1f 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 Compare whether two names of languages are identical.\r
19\r
20 @param Language1 Name of language 1.\r
21 @param Language2 Name of language 2.\r
22\r
23 @retval TRUE Language 1 and language 2 are the same.\r
24 @retval FALSE Language 1 and language 2 are not the same.\r
25\r
26**/\r
58251024 27STATIC\r
878ddf1f 28BOOLEAN\r
29CompareIso639LanguageCode (\r
30 IN CONST CHAR8 *Language1,\r
31 IN CONST CHAR8 *Language2\r
32 )\r
33{\r
58251024 34 UINT32 Name1;\r
35 UINT32 Name2;\r
36\r
37 Name1 = ReadUnaligned24 ((CONST UINT32 *) Language1);\r
38 Name2 = ReadUnaligned24 ((CONST UINT32 *) Language2);\r
39\r
40 return (BOOLEAN) (Name1 == Name2);\r
878ddf1f 41}\r
42\r
43/**\r
44 This function searches the list of configuration tables stored in the EFI System \r
45 Table for a table with a GUID that matches TableGuid. If a match is found, \r
46 then a pointer to the configuration table is returned in Table, and EFI_SUCCESS \r
47 is returned. If a matching GUID is not found, then EFI_NOT_FOUND is returned.\r
48\r
49 @param TableGuid Pointer to table's GUID type..\r
50 @param Table Pointer to the table associated with TableGuid in the EFI System Table.\r
51\r
52 @retval EFI_SUCCESS A configuration table matching TableGuid was found.\r
53 @retval EFI_NOT_FOUND A configuration table matching TableGuid could not be found.\r
54\r
55**/\r
56EFI_STATUS\r
57EFIAPI\r
58EfiGetSystemConfigurationTable ( \r
59 IN EFI_GUID *TableGuid,\r
60 OUT VOID **Table\r
61 )\r
62{\r
63 EFI_SYSTEM_TABLE *SystemTable;\r
64 UINTN Index;\r
65\r
66 ASSERT (TableGuid != NULL);\r
67 ASSERT (Table != NULL);\r
68\r
69 SystemTable = gST;\r
70 *Table = NULL;\r
71 for (Index = 0; Index < SystemTable->NumberOfTableEntries; Index++) {\r
72 if (CompareGuid (TableGuid, &(SystemTable->ConfigurationTable[Index].VendorGuid))) {\r
73 *Table = SystemTable->ConfigurationTable[Index].VendorTable;\r
74 return EFI_SUCCESS;\r
75 }\r
76 }\r
77\r
78 return EFI_NOT_FOUND;\r
79}\r
80\r
81/**\r
82 This function causes the notification function to be executed for every protocol \r
83 of type ProtocolGuid instance that exists in the system when this function is \r
84 invoked. In addition, every time a protocol of type ProtocolGuid instance is \r
85 installed or reinstalled, the notification function is also executed.\r
86\r
87 @param ProtocolGuid Supplies GUID of the protocol upon whose installation the event is fired.\r
88 @param NotifyTpl Supplies the task priority level of the event notifications.\r
89 @param NotifyFunction Supplies the function to notify when the event is signaled.\r
90 @param NotifyContext The context parameter to pass to NotifyFunction.\r
91 @param Registration A pointer to a memory location to receive the registration value.\r
92\r
93 @return The notification event that was created. \r
94\r
95**/\r
96EFI_EVENT\r
97EFIAPI\r
98EfiCreateProtocolNotifyEvent(\r
99 IN EFI_GUID *ProtocolGuid,\r
100 IN EFI_TPL NotifyTpl,\r
101 IN EFI_EVENT_NOTIFY NotifyFunction,\r
102 IN VOID *NotifyContext, OPTIONAL\r
f2df6a00 103 OUT VOID **Registration\r
878ddf1f 104 )\r
105{\r
106 EFI_STATUS Status;\r
107 EFI_EVENT Event;\r
108\r
109 //\r
110 // Create the event\r
111 //\r
112\r
113 Status = gBS->CreateEvent (\r
114 EFI_EVENT_NOTIFY_SIGNAL,\r
115 NotifyTpl,\r
116 NotifyFunction,\r
117 NotifyContext,\r
118 &Event\r
119 );\r
120 ASSERT_EFI_ERROR (Status);\r
121\r
122 //\r
123 // Register for protocol notifactions on this event\r
124 //\r
125\r
126 Status = gBS->RegisterProtocolNotify (\r
127 ProtocolGuid,\r
128 Event,\r
129 Registration\r
130 );\r
131\r
132 ASSERT_EFI_ERROR (Status);\r
133\r
134 //\r
135 // Kick the event so we will perform an initial pass of\r
136 // current installed drivers\r
137 //\r
138\r
139 gBS->SignalEvent (Event);\r
140 return Event;\r
141}\r
142\r
143/**\r
144 This function creates an event using NotifyTpl, NoifyFunction, and NotifyContext.\r
145 This event is signaled with EfiNamedEventSignal(). This provide the ability for \r
146 one or more listeners on the same event named by the GUID specified by Name.\r
147\r
148 @param Name Supplies GUID name of the event.\r
149 @param NotifyTpl Supplies the task priority level of the event notifications.\r
150 @param NotifyFunction Supplies the function to notify when the event is signaled.\r
151 @param NotifyContext The context parameter to pass to NotifyFunction. \r
152 @param Registration A pointer to a memory location to receive the registration value.\r
153\r
154 @retval EFI_SUCCESS A named event was created.\r
155 @retval EFI_OUT_OF_RESOURCES There are not enough resource to create the named event.\r
156\r
157**/\r
158EFI_STATUS\r
159EFIAPI\r
160EfiNamedEventListen (\r
161 IN CONST EFI_GUID *Name,\r
162 IN EFI_TPL NotifyTpl,\r
163 IN EFI_EVENT_NOTIFY NotifyFunction,\r
164 IN CONST VOID *NotifyContext, OPTIONAL\r
13c84604 165 OUT VOID *Registration OPTIONAL\r
878ddf1f 166 )\r
167{\r
168 EFI_STATUS Status;\r
169 EFI_EVENT Event;\r
170 VOID *RegistrationLocal;\r
171\r
172 //\r
173 // Create event\r
174 //\r
175 Status = gBS->CreateEvent (\r
176 EFI_EVENT_NOTIFY_SIGNAL,\r
177 NotifyTpl,\r
178 NotifyFunction,\r
179 (VOID *) NotifyContext,\r
180 &Event\r
181 );\r
182 ASSERT_EFI_ERROR (Status);\r
183\r
184 //\r
185 // The Registration is not optional to RegisterProtocolNotify().\r
186 // To make it optional to EfiNamedEventListen(), may need to substitute with a local.\r
187 //\r
188 if (Registration != NULL) {\r
189 RegistrationLocal = Registration;\r
190 } else {\r
191 RegistrationLocal = &RegistrationLocal;\r
192 }\r
193\r
194 //\r
195 // Register for an installation of protocol interface\r
196 //\r
197\r
198 Status = gBS->RegisterProtocolNotify (\r
199 (EFI_GUID *) Name,\r
200 Event,\r
201 RegistrationLocal\r
202 );\r
203 ASSERT_EFI_ERROR (Status);\r
204\r
205 return EFI_SUCCESS;\r
206}\r
207\r
208/**\r
209 This function signals the named event specified by Name. The named event must \r
210 have been created with EfiNamedEventListen().\r
211\r
212 @param Name Supplies GUID name of the event.\r
213\r
214 @retval EFI_SUCCESS A named event was signaled.\r
215 @retval EFI_OUT_OF_RESOURCES There are not enough resource to signal the named event.\r
216\r
217**/\r
218EFI_STATUS\r
219EFIAPI\r
220EfiNamedEventSignal (\r
221 IN CONST EFI_GUID *Name\r
222 )\r
223{\r
224 EFI_STATUS Status;\r
225 EFI_HANDLE Handle;\r
226\r
227 Handle = NULL;\r
228 Status = gBS->InstallProtocolInterface (\r
229 &Handle,\r
230 (EFI_GUID *) Name,\r
231 EFI_NATIVE_INTERFACE,\r
232 NULL\r
233 );\r
234 ASSERT_EFI_ERROR (Status);\r
235\r
236 Status = gBS->UninstallProtocolInterface (\r
237 Handle,\r
238 (EFI_GUID *) Name,\r
239 NULL\r
240 );\r
241 ASSERT_EFI_ERROR (Status);\r
242\r
243 return EFI_SUCCESS;\r
244}\r
245\r
d958721a 246/** \r
247 Returns the current TPL.\r
248\r
249 This function returns the current TPL. There is no EFI service to directly \r
250 retrieve the current TPL. Instead, the RaiseTPL() function is used to raise \r
251 the TPL to TPL_HIGH_LEVEL. This will return the current TPL. The TPL level \r
252 can then immediately be restored back to the current TPL level with a call \r
253 to RestoreTPL().\r
254\r
255 @param VOID\r
256\r
257 @retvale EFI_TPL The current TPL.\r
258\r
259**/\r
260EFI_TPL\r
261EFIAPI\r
262EfiGetCurrentTpl (\r
263 VOID\r
264 )\r
265{\r
266 EFI_TPL Tpl;\r
267\r
268 Tpl = gBS->RaiseTPL (EFI_TPL_HIGH_LEVEL); \r
269 gBS->RestoreTPL (Tpl);\r
270\r
271 return Tpl;\r
272}\r
273\r
878ddf1f 274\r
275/**\r
276 This function initializes a basic mutual exclusion lock to the released state \r
277 and returns the lock. Each lock provides mutual exclusion access at its task \r
278 priority level. Since there is no preemption or multiprocessor support in EFI,\r
279 acquiring the lock only consists of raising to the locks TPL.\r
280\r
281 @param Lock A pointer to the lock data structure to initialize.\r
282 @param Priority EFI TPL associated with the lock.\r
283\r
284 @return The lock.\r
285\r
286**/\r
287EFI_LOCK *\r
288EFIAPI\r
289EfiInitializeLock (\r
290 IN OUT EFI_LOCK *Lock,\r
291 IN EFI_TPL Priority\r
292 )\r
293{\r
294 ASSERT (Lock != NULL);\r
295 ASSERT (Priority <= EFI_TPL_HIGH_LEVEL);\r
296\r
297 Lock->Tpl = Priority;\r
298 Lock->OwnerTpl = EFI_TPL_APPLICATION;\r
299 Lock->Lock = EfiLockReleased ;\r
300 return Lock;\r
301}\r
302\r
303/**\r
511710d6 304 This function raises the system's current task priority level to the task \r
878ddf1f 305 priority level of the mutual exclusion lock. Then, it places the lock in the \r
306 acquired state.\r
307\r
308 @param Priority The task priority level of the lock.\r
309\r
310**/\r
311VOID\r
312EFIAPI\r
313EfiAcquireLock (\r
314 IN EFI_LOCK *Lock\r
315 )\r
316{\r
317 ASSERT (Lock != NULL);\r
318 ASSERT (Lock->Lock == EfiLockReleased);\r
319\r
320 Lock->OwnerTpl = gBS->RaiseTPL (Lock->Tpl);\r
321 Lock->Lock = EfiLockAcquired;\r
322}\r
323\r
324/**\r
511710d6 325 This function raises the system's current task priority level to the task \r
878ddf1f 326 priority level of the mutual exclusion lock. Then, it attempts to place the \r
327 lock in the acquired state.\r
328\r
329 @param Lock A pointer to the lock to acquire.\r
330\r
331 @retval EFI_SUCCESS The lock was acquired.\r
332 @retval EFI_ACCESS_DENIED The lock could not be acquired because it is already owned.\r
333\r
334**/\r
335EFI_STATUS\r
336EFIAPI\r
337EfiAcquireLockOrFail (\r
338 IN EFI_LOCK *Lock\r
339 )\r
340{\r
341\r
342 ASSERT (Lock != NULL);\r
343 ASSERT (Lock->Lock != EfiLockUninitialized);\r
344\r
345 if (Lock->Lock == EfiLockAcquired) {\r
346 //\r
347 // Lock is already owned, so bail out\r
348 //\r
349 return EFI_ACCESS_DENIED;\r
350 }\r
351\r
352 Lock->OwnerTpl = gBS->RaiseTPL (Lock->Tpl);\r
353\r
354 Lock->Lock = EfiLockAcquired;\r
355\r
356 return EFI_SUCCESS;\r
357}\r
358\r
359/**\r
360 This function transitions a mutual exclusion lock from the acquired state to \r
511710d6 361 the released state, and restores the system's task priority level to its \r
878ddf1f 362 previous level.\r
363\r
364 @param Lock A pointer to the lock to release.\r
365\r
366**/\r
367VOID\r
368EFIAPI\r
369EfiReleaseLock (\r
370 IN EFI_LOCK *Lock\r
371 )\r
372{\r
373 EFI_TPL Tpl;\r
374\r
375 ASSERT (Lock != NULL);\r
376 ASSERT (Lock->Lock == EfiLockAcquired);\r
377\r
378 Tpl = Lock->OwnerTpl;\r
379 \r
380 Lock->Lock = EfiLockReleased;\r
381\r
382 gBS->RestoreTPL (Tpl);\r
383}\r
384\r
ca21f1a2 385/**
386 Tests whether a controller is managed by a specific driver.
387
388 This function tests whether a specific driver manages ControllerHandle by\r
389 opening on DriverBindingHandle a protocol specified by ProtocolGuid with\r
390 attribute EFI_OPEN_PROTOCOL_BY_DRIVER. This library function is used to\r
391 implement the Component Name Protocol for EFI Drivers.\r
392 If ProtocolGuid is NULL, then ASSERT().\r
393
394 @param ControllerHandle A handle for a controller to test.
395 @param DriverBindingHandle Specifies the driver binding handle for the
396 driver.
397 @param ProtocolGuid Supplies GUID for the protocol opened by the
398 driver on the controller.
399
400 @retval EFI_SUCCESS ControllerHandle is managed by the specific
401 driver.
402 @retval EFI_UNSUPPORTED ControllerHandle is not managed by the specific
403 driver.
404
405**/\r
406EFI_STATUS\r
407EFIAPI\r
408EfiTestManagedDevice (\r
409 IN CONST EFI_HANDLE ControllerHandle,\r
410 IN CONST EFI_HANDLE DriverBindingHandle,\r
411 IN CONST EFI_GUID *ProtocolGuid\r
412 )\r
413{\r
414 EFI_STATUS Status;\r
415 VOID *ManagedInterface;\r
416\r
417 ASSERT (ProtocolGuid != NULL);\r
418\r
419 Status = gBS->OpenProtocol (\r
420 ControllerHandle,\r
421 (EFI_GUID *) ProtocolGuid,\r
422 &ManagedInterface,\r
423 DriverBindingHandle,\r
424 ControllerHandle,\r
425 EFI_OPEN_PROTOCOL_BY_DRIVER\r
426 );\r
427 if (!EFI_ERROR (Status)) {\r
428 gBS->CloseProtocol (\r
429 ControllerHandle,\r
430 (EFI_GUID *) ProtocolGuid,\r
431 DriverBindingHandle,\r
432 ControllerHandle\r
433 );\r
434 return EFI_UNSUPPORTED;\r
435 }\r
436\r
437 if (Status != EFI_ALREADY_STARTED) {\r
438 return EFI_UNSUPPORTED;\r
439 }\r
440\r
441 return EFI_SUCCESS;\r
442}\r
443\r
444/**
445 Tests whether a child handle is a children device of the controller.
446
447 This function tests whether ChildHandle is one of the children of
448 ControllerHandle which are consuming a protocol specified by ProtocolGuid
449 with the attribute bit EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER set. This
450 library function is used to implement the Component Name Protocol for EFI
451 Drivers.
452 If ProtocolGuid is NULL, then ASSERT().
453
454 @param ControllerHandle A handle for a (parent) controller to test.
455 @param ChildHandle A child handle to test.
456 @param ConsumsedGuid Supplies GUID for the protocol consumed by
457 children from controller.
458
459 @retval EFI_SUCCESS ChildHandle is a child of the ControllerHandle.
460 @retval EFI_UNSUPPORTED ChildHandle is not a child of the
461 ControllerHandle.
462
463**/\r
464EFI_STATUS\r
465EFIAPI\r
466EfiTestChildHandle (\r
467 IN CONST EFI_HANDLE ControllerHandle,\r
468 IN CONST EFI_HANDLE ChildHandle,\r
469 IN CONST EFI_GUID *ProtocolGuid\r
470 )\r
471{\r
472 EFI_STATUS Status;\r
473 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfoBuffer;\r
474 UINTN EntryCount;\r
475 UINTN Index;\r
476\r
477 ASSERT (ProtocolGuid != NULL);\r
478\r
479 //\r
480 // Retrieve the list of agents that are consuming the specific protocol\r
481 // on ControllerHandle.\r
482 //\r
483 Status = gBS->OpenProtocolInformation (\r
484 ControllerHandle,\r
485 (EFI_GUID *) ProtocolGuid,\r
486 &OpenInfoBuffer,\r
487 &EntryCount\r
488 );\r
489 if (EFI_ERROR (Status)) {\r
490 return EFI_UNSUPPORTED;\r
491 }\r
492\r
493 //\r
494 // Inspect if ChildHandle is one of the agents.\r
495 //\r
496 Status = EFI_UNSUPPORTED;\r
497 for (Index = 0; Index < EntryCount; Index++) {\r
498 if ((OpenInfoBuffer[Index].ControllerHandle == ChildHandle) &&\r
499 (OpenInfoBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {\r
500 Status = EFI_SUCCESS;\r
501 break;\r
502 }\r
503 }\r
504 \r
505 FreePool (OpenInfoBuffer);\r
506 return Status;\r
507}\r
508\r
878ddf1f 509/**\r
510 This function looks up a Unicode string in UnicodeStringTable. If Language is \r
511 a member of SupportedLanguages and a Unicode string is found in UnicodeStringTable\r
512 that matches the language code specified by Language, then it is returned in \r
513 UnicodeString.\r
514\r
515 @param Language A pointer to the ISO 639-2 language code for the \r
516 Unicode string to look up and return.\r
517 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes \r
518 that the Unicode string table supports. Language \r
519 must be a member of this set.\r
520 @param UnicodeStringTable A pointer to the table of Unicode strings.\r
521 @param UnicodeString A pointer to the Unicode string from UnicodeStringTable\r
522 that matches the language specified by Language.\r
523\r
524 @retval EFI_SUCCESS The Unicode string that matches the language \r
525 specified by Language was found\r
526 in the table of Unicoide strings UnicodeStringTable, \r
527 and it was returned in UnicodeString.\r
528 @retval EFI_INVALID_PARAMETER Language is NULL.\r
529 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.\r
530 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.\r
531 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.\r
532 @retval EFI_UNSUPPORTED The language specified by Language is not a \r
533 member of SupportedLanguages.\r
534 @retval EFI_UNSUPPORTED The language specified by Language is not \r
535 supported by UnicodeStringTable.\r
536\r
537**/\r
538EFI_STATUS\r
539EFIAPI\r
540LookupUnicodeString (\r
541 IN CONST CHAR8 *Language,\r
542 IN CONST CHAR8 *SupportedLanguages,\r
543 IN CONST EFI_UNICODE_STRING_TABLE *UnicodeStringTable,\r
544 OUT CHAR16 **UnicodeString\r
545 )\r
546{\r
547 //\r
548 // Make sure the parameters are valid\r
549 //\r
550 if (Language == NULL || UnicodeString == NULL) {\r
551 return EFI_INVALID_PARAMETER;\r
552 }\r
553\r
554 //\r
555 // If there are no supported languages, or the Unicode String Table is empty, then the\r
556 // Unicode String specified by Language is not supported by this Unicode String Table\r
557 //\r
558 if (SupportedLanguages == NULL || UnicodeStringTable == NULL) {\r
559 return EFI_UNSUPPORTED;\r
560 }\r
561\r
562 //\r
563 // Make sure Language is in the set of Supported Languages\r
564 //\r
565 while (*SupportedLanguages != 0) {\r
566 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
567\r
568 //\r
569 // Search the Unicode String Table for the matching Language specifier\r
570 //\r
571 while (UnicodeStringTable->Language != NULL) {\r
572 if (CompareIso639LanguageCode (Language, UnicodeStringTable->Language)) {\r
573\r
574 //\r
575 // A matching string was found, so return it\r
576 //\r
577 *UnicodeString = UnicodeStringTable->UnicodeString;\r
578 return EFI_SUCCESS;\r
579 }\r
580\r
581 UnicodeStringTable++;\r
582 }\r
583\r
584 return EFI_UNSUPPORTED;\r
585 }\r
586\r
587 SupportedLanguages += 3;\r
588 }\r
589\r
590 return EFI_UNSUPPORTED;\r
591}\r
592\r
593/**\r
594 This function adds a Unicode string to UnicodeStringTable.\r
595 If Language is a member of SupportedLanguages then UnicodeString is added to \r
596 UnicodeStringTable. New buffers are allocated for both Language and \r
597 UnicodeString. The contents of Language and UnicodeString are copied into \r
598 these new buffers. These buffers are automatically freed when \r
599 FreeUnicodeStringTable() is called.\r
600\r
601 @param Language A pointer to the ISO 639-2 language code for the Unicode \r
602 string to add.\r
603 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes\r
604 that the Unicode string table supports.\r
605 Language must be a member of this set.\r
606 @param UnicodeStringTable A pointer to the table of Unicode strings.\r
607 @param UnicodeString A pointer to the Unicode string to add.\r
608\r
609 @retval EFI_SUCCESS The Unicode string that matches the language \r
610 specified by Language was found in the table of \r
611 Unicode strings UnicodeStringTable, and it was \r
612 returned in UnicodeString.\r
613 @retval EFI_INVALID_PARAMETER Language is NULL.\r
614 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.\r
615 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.\r
616 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.\r
617 @retval EFI_ALREADY_STARTED A Unicode string with language Language is \r
618 already present in UnicodeStringTable.\r
619 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another \r
620 Unicode string to UnicodeStringTable.\r
621 @retval EFI_UNSUPPORTED The language specified by Language is not a \r
622 member of SupportedLanguages.\r
623\r
624**/\r
625EFI_STATUS\r
626EFIAPI\r
627AddUnicodeString (\r
628 IN CONST CHAR8 *Language,\r
629 IN CONST CHAR8 *SupportedLanguages,\r
630 IN EFI_UNICODE_STRING_TABLE **UnicodeStringTable,\r
631 IN CONST CHAR16 *UnicodeString\r
632 )\r
633{\r
634 UINTN NumberOfEntries;\r
635 EFI_UNICODE_STRING_TABLE *OldUnicodeStringTable;\r
636 EFI_UNICODE_STRING_TABLE *NewUnicodeStringTable;\r
637 UINTN UnicodeStringLength;\r
638\r
639 //\r
640 // Make sure the parameter are valid\r
641 //\r
642 if (Language == NULL || UnicodeString == NULL || UnicodeStringTable == NULL) {\r
643 return EFI_INVALID_PARAMETER;\r
644 }\r
645\r
646 //\r
647 // If there are no supported languages, then a Unicode String can not be added\r
648 //\r
649 if (SupportedLanguages == NULL) {\r
650 return EFI_UNSUPPORTED;\r
651 }\r
652\r
653 //\r
654 // If the Unicode String is empty, then a Unicode String can not be added\r
655 //\r
656 if (UnicodeString[0] == 0) {\r
657 return EFI_INVALID_PARAMETER;\r
658 }\r
659\r
660 //\r
661 // Make sure Language is a member of SupportedLanguages\r
662 //\r
663 while (*SupportedLanguages != 0) {\r
664 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
665\r
666 //\r
667 // Determine the size of the Unicode String Table by looking for a NULL Language entry\r
668 //\r
669 NumberOfEntries = 0;\r
670 if (*UnicodeStringTable != NULL) {\r
671 OldUnicodeStringTable = *UnicodeStringTable;\r
672 while (OldUnicodeStringTable->Language != NULL) {\r
673 if (CompareIso639LanguageCode (Language, OldUnicodeStringTable->Language)) {\r
674 return EFI_ALREADY_STARTED;\r
675 }\r
676\r
677 OldUnicodeStringTable++;\r
678 NumberOfEntries++;\r
679 }\r
680 }\r
681\r
682 //\r
683 // Allocate space for a new Unicode String Table. It must hold the current number of\r
684 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table\r
685 // marker\r
686 //\r
687 NewUnicodeStringTable = AllocatePool ((NumberOfEntries + 2) * sizeof (EFI_UNICODE_STRING_TABLE));\r
688 if (NewUnicodeStringTable == NULL) {\r
689 return EFI_OUT_OF_RESOURCES;\r
690 }\r
691\r
692 //\r
693 // If the current Unicode String Table contains any entries, then copy them to the\r
694 // newly allocated Unicode String Table.\r
695 //\r
696 if (*UnicodeStringTable != NULL) {\r
697 CopyMem (\r
698 NewUnicodeStringTable,\r
699 *UnicodeStringTable,\r
700 NumberOfEntries * sizeof (EFI_UNICODE_STRING_TABLE)\r
701 );\r
702 }\r
703\r
704 //\r
705 // Allocate space for a copy of the Language specifier\r
706 //\r
707 NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (3, Language);\r
708 if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) {\r
709 gBS->FreePool (NewUnicodeStringTable);\r
710 return EFI_OUT_OF_RESOURCES;\r
711 }\r
712\r
713 //\r
714 // Compute the length of the Unicode String\r
715 //\r
716 for (UnicodeStringLength = 0; UnicodeString[UnicodeStringLength] != 0; UnicodeStringLength++)\r
717 ;\r
718\r
719 //\r
720 // Allocate space for a copy of the Unicode String\r
721 //\r
722 NewUnicodeStringTable[NumberOfEntries].UnicodeString = AllocateCopyPool (\r
723 (UnicodeStringLength + 1) * sizeof (CHAR16),\r
724 UnicodeString\r
725 );\r
726 if (NewUnicodeStringTable[NumberOfEntries].UnicodeString == NULL) {\r
727 gBS->FreePool (NewUnicodeStringTable[NumberOfEntries].Language);\r
728 gBS->FreePool (NewUnicodeStringTable);\r
729 return EFI_OUT_OF_RESOURCES;\r
730 }\r
731\r
732 //\r
733 // Mark the end of the Unicode String Table\r
734 //\r
735 NewUnicodeStringTable[NumberOfEntries + 1].Language = NULL;\r
736 NewUnicodeStringTable[NumberOfEntries + 1].UnicodeString = NULL;\r
737\r
738 //\r
739 // Free the old Unicode String Table\r
740 //\r
741 if (*UnicodeStringTable != NULL) {\r
742 gBS->FreePool (*UnicodeStringTable);\r
743 }\r
744\r
745 //\r
746 // Point UnicodeStringTable at the newly allocated Unicode String Table\r
747 //\r
748 *UnicodeStringTable = NewUnicodeStringTable;\r
749\r
750 return EFI_SUCCESS;\r
751 }\r
752\r
753 SupportedLanguages += 3;\r
754 }\r
755\r
756 return EFI_UNSUPPORTED;\r
757}\r
758\r
759/**\r
760 This function frees the table of Unicode strings in UnicodeStringTable.\r
761 If UnicodeStringTable is NULL, then EFI_SUCCESS is returned.\r
762 Otherwise, each language code, and each Unicode string in the Unicode string \r
763 table are freed, and EFI_SUCCESS is returned.\r
764\r
765 @param UnicodeStringTable A pointer to the table of Unicode strings.\r
766\r
767 @retval EFI_SUCCESS The Unicode string table was freed.\r
768\r
769**/\r
770EFI_STATUS\r
771EFIAPI\r
772FreeUnicodeStringTable (\r
773 IN EFI_UNICODE_STRING_TABLE *UnicodeStringTable\r
774 )\r
775{\r
776 UINTN Index;\r
777\r
778 //\r
779 // If the Unicode String Table is NULL, then it is already freed\r
780 //\r
781 if (UnicodeStringTable == NULL) {\r
782 return EFI_SUCCESS;\r
783 }\r
784\r
785 //\r
786 // Loop through the Unicode String Table until we reach the end of table marker\r
787 //\r
788 for (Index = 0; UnicodeStringTable[Index].Language != NULL; Index++) {\r
789\r
790 //\r
791 // Free the Language string from the Unicode String Table\r
792 //\r
793 gBS->FreePool (UnicodeStringTable[Index].Language);\r
794\r
795 //\r
796 // Free the Unicode String from the Unicode String Table\r
797 //\r
798 if (UnicodeStringTable[Index].UnicodeString != NULL) {\r
799 gBS->FreePool (UnicodeStringTable[Index].UnicodeString);\r
800 }\r
801 }\r
802\r
803 //\r
804 // Free the Unicode String Table itself\r
805 //\r
806 gBS->FreePool (UnicodeStringTable);\r
807\r
808 return EFI_SUCCESS;\r
809}\r
d958721a 810\r