]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/UefiLib/UefiLib.c
Make MDE package pass intel IPF compiler with /W4 /WX switched on.
[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
246\r
247/**\r
248 This function initializes a basic mutual exclusion lock to the released state \r
249 and returns the lock. Each lock provides mutual exclusion access at its task \r
250 priority level. Since there is no preemption or multiprocessor support in EFI,\r
251 acquiring the lock only consists of raising to the locks TPL.\r
252\r
253 @param Lock A pointer to the lock data structure to initialize.\r
254 @param Priority EFI TPL associated with the lock.\r
255\r
256 @return The lock.\r
257\r
258**/\r
259EFI_LOCK *\r
260EFIAPI\r
261EfiInitializeLock (\r
262 IN OUT EFI_LOCK *Lock,\r
263 IN EFI_TPL Priority\r
264 )\r
265{\r
266 ASSERT (Lock != NULL);\r
267 ASSERT (Priority <= EFI_TPL_HIGH_LEVEL);\r
268\r
269 Lock->Tpl = Priority;\r
270 Lock->OwnerTpl = EFI_TPL_APPLICATION;\r
271 Lock->Lock = EfiLockReleased ;\r
272 return Lock;\r
273}\r
274\r
275/**\r
511710d6 276 This function raises the system's current task priority level to the task \r
878ddf1f 277 priority level of the mutual exclusion lock. Then, it places the lock in the \r
278 acquired state.\r
279\r
280 @param Priority The task priority level of the lock.\r
281\r
282**/\r
283VOID\r
284EFIAPI\r
285EfiAcquireLock (\r
286 IN EFI_LOCK *Lock\r
287 )\r
288{\r
289 ASSERT (Lock != NULL);\r
290 ASSERT (Lock->Lock == EfiLockReleased);\r
291\r
292 Lock->OwnerTpl = gBS->RaiseTPL (Lock->Tpl);\r
293 Lock->Lock = EfiLockAcquired;\r
294}\r
295\r
296/**\r
511710d6 297 This function raises the system's current task priority level to the task \r
878ddf1f 298 priority level of the mutual exclusion lock. Then, it attempts to place the \r
299 lock in the acquired state.\r
300\r
301 @param Lock A pointer to the lock to acquire.\r
302\r
303 @retval EFI_SUCCESS The lock was acquired.\r
304 @retval EFI_ACCESS_DENIED The lock could not be acquired because it is already owned.\r
305\r
306**/\r
307EFI_STATUS\r
308EFIAPI\r
309EfiAcquireLockOrFail (\r
310 IN EFI_LOCK *Lock\r
311 )\r
312{\r
313\r
314 ASSERT (Lock != NULL);\r
315 ASSERT (Lock->Lock != EfiLockUninitialized);\r
316\r
317 if (Lock->Lock == EfiLockAcquired) {\r
318 //\r
319 // Lock is already owned, so bail out\r
320 //\r
321 return EFI_ACCESS_DENIED;\r
322 }\r
323\r
324 Lock->OwnerTpl = gBS->RaiseTPL (Lock->Tpl);\r
325\r
326 Lock->Lock = EfiLockAcquired;\r
327\r
328 return EFI_SUCCESS;\r
329}\r
330\r
331/**\r
332 This function transitions a mutual exclusion lock from the acquired state to \r
511710d6 333 the released state, and restores the system's task priority level to its \r
878ddf1f 334 previous level.\r
335\r
336 @param Lock A pointer to the lock to release.\r
337\r
338**/\r
339VOID\r
340EFIAPI\r
341EfiReleaseLock (\r
342 IN EFI_LOCK *Lock\r
343 )\r
344{\r
345 EFI_TPL Tpl;\r
346\r
347 ASSERT (Lock != NULL);\r
348 ASSERT (Lock->Lock == EfiLockAcquired);\r
349\r
350 Tpl = Lock->OwnerTpl;\r
351 \r
352 Lock->Lock = EfiLockReleased;\r
353\r
354 gBS->RestoreTPL (Tpl);\r
355}\r
356\r
ca21f1a2 357/**
358 Tests whether a controller is managed by a specific driver.
359
360 This function tests whether a specific driver manages ControllerHandle by\r
361 opening on DriverBindingHandle a protocol specified by ProtocolGuid with\r
362 attribute EFI_OPEN_PROTOCOL_BY_DRIVER. This library function is used to\r
363 implement the Component Name Protocol for EFI Drivers.\r
364 If ProtocolGuid is NULL, then ASSERT().\r
365
366 @param ControllerHandle A handle for a controller to test.
367 @param DriverBindingHandle Specifies the driver binding handle for the
368 driver.
369 @param ProtocolGuid Supplies GUID for the protocol opened by the
370 driver on the controller.
371
372 @retval EFI_SUCCESS ControllerHandle is managed by the specific
373 driver.
374 @retval EFI_UNSUPPORTED ControllerHandle is not managed by the specific
375 driver.
376
377**/\r
378EFI_STATUS\r
379EFIAPI\r
380EfiTestManagedDevice (\r
381 IN CONST EFI_HANDLE ControllerHandle,\r
382 IN CONST EFI_HANDLE DriverBindingHandle,\r
383 IN CONST EFI_GUID *ProtocolGuid\r
384 )\r
385{\r
386 EFI_STATUS Status;\r
387 VOID *ManagedInterface;\r
388\r
389 ASSERT (ProtocolGuid != NULL);\r
390\r
391 Status = gBS->OpenProtocol (\r
392 ControllerHandle,\r
393 (EFI_GUID *) ProtocolGuid,\r
394 &ManagedInterface,\r
395 DriverBindingHandle,\r
396 ControllerHandle,\r
397 EFI_OPEN_PROTOCOL_BY_DRIVER\r
398 );\r
399 if (!EFI_ERROR (Status)) {\r
400 gBS->CloseProtocol (\r
401 ControllerHandle,\r
402 (EFI_GUID *) ProtocolGuid,\r
403 DriverBindingHandle,\r
404 ControllerHandle\r
405 );\r
406 return EFI_UNSUPPORTED;\r
407 }\r
408\r
409 if (Status != EFI_ALREADY_STARTED) {\r
410 return EFI_UNSUPPORTED;\r
411 }\r
412\r
413 return EFI_SUCCESS;\r
414}\r
415\r
416/**
417 Tests whether a child handle is a children device of the controller.
418
419 This function tests whether ChildHandle is one of the children of
420 ControllerHandle which are consuming a protocol specified by ProtocolGuid
421 with the attribute bit EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER set. This
422 library function is used to implement the Component Name Protocol for EFI
423 Drivers.
424 If ProtocolGuid is NULL, then ASSERT().
425
426 @param ControllerHandle A handle for a (parent) controller to test.
427 @param ChildHandle A child handle to test.
428 @param ConsumsedGuid Supplies GUID for the protocol consumed by
429 children from controller.
430
431 @retval EFI_SUCCESS ChildHandle is a child of the ControllerHandle.
432 @retval EFI_UNSUPPORTED ChildHandle is not a child of the
433 ControllerHandle.
434
435**/\r
436EFI_STATUS\r
437EFIAPI\r
438EfiTestChildHandle (\r
439 IN CONST EFI_HANDLE ControllerHandle,\r
440 IN CONST EFI_HANDLE ChildHandle,\r
441 IN CONST EFI_GUID *ProtocolGuid\r
442 )\r
443{\r
444 EFI_STATUS Status;\r
445 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfoBuffer;\r
446 UINTN EntryCount;\r
447 UINTN Index;\r
448\r
449 ASSERT (ProtocolGuid != NULL);\r
450\r
451 //\r
452 // Retrieve the list of agents that are consuming the specific protocol\r
453 // on ControllerHandle.\r
454 //\r
455 Status = gBS->OpenProtocolInformation (\r
456 ControllerHandle,\r
457 (EFI_GUID *) ProtocolGuid,\r
458 &OpenInfoBuffer,\r
459 &EntryCount\r
460 );\r
461 if (EFI_ERROR (Status)) {\r
462 return EFI_UNSUPPORTED;\r
463 }\r
464\r
465 //\r
466 // Inspect if ChildHandle is one of the agents.\r
467 //\r
468 Status = EFI_UNSUPPORTED;\r
469 for (Index = 0; Index < EntryCount; Index++) {\r
470 if ((OpenInfoBuffer[Index].ControllerHandle == ChildHandle) &&\r
471 (OpenInfoBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {\r
472 Status = EFI_SUCCESS;\r
473 break;\r
474 }\r
475 }\r
476 \r
477 FreePool (OpenInfoBuffer);\r
478 return Status;\r
479}\r
480\r
878ddf1f 481/**\r
482 This function looks up a Unicode string in UnicodeStringTable. If Language is \r
483 a member of SupportedLanguages and a Unicode string is found in UnicodeStringTable\r
484 that matches the language code specified by Language, then it is returned in \r
485 UnicodeString.\r
486\r
487 @param Language A pointer to the ISO 639-2 language code for the \r
488 Unicode string to look up and return.\r
489 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes \r
490 that the Unicode string table supports. Language \r
491 must be a member of this set.\r
492 @param UnicodeStringTable A pointer to the table of Unicode strings.\r
493 @param UnicodeString A pointer to the Unicode string from UnicodeStringTable\r
494 that matches the language specified by Language.\r
495\r
496 @retval EFI_SUCCESS The Unicode string that matches the language \r
497 specified by Language was found\r
498 in the table of Unicoide strings UnicodeStringTable, \r
499 and it was returned in UnicodeString.\r
500 @retval EFI_INVALID_PARAMETER Language is NULL.\r
501 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.\r
502 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.\r
503 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.\r
504 @retval EFI_UNSUPPORTED The language specified by Language is not a \r
505 member of SupportedLanguages.\r
506 @retval EFI_UNSUPPORTED The language specified by Language is not \r
507 supported by UnicodeStringTable.\r
508\r
509**/\r
510EFI_STATUS\r
511EFIAPI\r
512LookupUnicodeString (\r
513 IN CONST CHAR8 *Language,\r
514 IN CONST CHAR8 *SupportedLanguages,\r
515 IN CONST EFI_UNICODE_STRING_TABLE *UnicodeStringTable,\r
516 OUT CHAR16 **UnicodeString\r
517 )\r
518{\r
519 //\r
520 // Make sure the parameters are valid\r
521 //\r
522 if (Language == NULL || UnicodeString == NULL) {\r
523 return EFI_INVALID_PARAMETER;\r
524 }\r
525\r
526 //\r
527 // If there are no supported languages, or the Unicode String Table is empty, then the\r
528 // Unicode String specified by Language is not supported by this Unicode String Table\r
529 //\r
530 if (SupportedLanguages == NULL || UnicodeStringTable == NULL) {\r
531 return EFI_UNSUPPORTED;\r
532 }\r
533\r
534 //\r
535 // Make sure Language is in the set of Supported Languages\r
536 //\r
537 while (*SupportedLanguages != 0) {\r
538 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
539\r
540 //\r
541 // Search the Unicode String Table for the matching Language specifier\r
542 //\r
543 while (UnicodeStringTable->Language != NULL) {\r
544 if (CompareIso639LanguageCode (Language, UnicodeStringTable->Language)) {\r
545\r
546 //\r
547 // A matching string was found, so return it\r
548 //\r
549 *UnicodeString = UnicodeStringTable->UnicodeString;\r
550 return EFI_SUCCESS;\r
551 }\r
552\r
553 UnicodeStringTable++;\r
554 }\r
555\r
556 return EFI_UNSUPPORTED;\r
557 }\r
558\r
559 SupportedLanguages += 3;\r
560 }\r
561\r
562 return EFI_UNSUPPORTED;\r
563}\r
564\r
565/**\r
566 This function adds a Unicode string to UnicodeStringTable.\r
567 If Language is a member of SupportedLanguages then UnicodeString is added to \r
568 UnicodeStringTable. New buffers are allocated for both Language and \r
569 UnicodeString. The contents of Language and UnicodeString are copied into \r
570 these new buffers. These buffers are automatically freed when \r
571 FreeUnicodeStringTable() is called.\r
572\r
573 @param Language A pointer to the ISO 639-2 language code for the Unicode \r
574 string to add.\r
575 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes\r
576 that the Unicode string table supports.\r
577 Language must be a member of this set.\r
578 @param UnicodeStringTable A pointer to the table of Unicode strings.\r
579 @param UnicodeString A pointer to the Unicode string to add.\r
580\r
581 @retval EFI_SUCCESS The Unicode string that matches the language \r
582 specified by Language was found in the table of \r
583 Unicode strings UnicodeStringTable, and it was \r
584 returned in UnicodeString.\r
585 @retval EFI_INVALID_PARAMETER Language is NULL.\r
586 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.\r
587 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.\r
588 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.\r
589 @retval EFI_ALREADY_STARTED A Unicode string with language Language is \r
590 already present in UnicodeStringTable.\r
591 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another \r
592 Unicode string to UnicodeStringTable.\r
593 @retval EFI_UNSUPPORTED The language specified by Language is not a \r
594 member of SupportedLanguages.\r
595\r
596**/\r
597EFI_STATUS\r
598EFIAPI\r
599AddUnicodeString (\r
600 IN CONST CHAR8 *Language,\r
601 IN CONST CHAR8 *SupportedLanguages,\r
602 IN EFI_UNICODE_STRING_TABLE **UnicodeStringTable,\r
603 IN CONST CHAR16 *UnicodeString\r
604 )\r
605{\r
606 UINTN NumberOfEntries;\r
607 EFI_UNICODE_STRING_TABLE *OldUnicodeStringTable;\r
608 EFI_UNICODE_STRING_TABLE *NewUnicodeStringTable;\r
609 UINTN UnicodeStringLength;\r
610\r
611 //\r
612 // Make sure the parameter are valid\r
613 //\r
614 if (Language == NULL || UnicodeString == NULL || UnicodeStringTable == NULL) {\r
615 return EFI_INVALID_PARAMETER;\r
616 }\r
617\r
618 //\r
619 // If there are no supported languages, then a Unicode String can not be added\r
620 //\r
621 if (SupportedLanguages == NULL) {\r
622 return EFI_UNSUPPORTED;\r
623 }\r
624\r
625 //\r
626 // If the Unicode String is empty, then a Unicode String can not be added\r
627 //\r
628 if (UnicodeString[0] == 0) {\r
629 return EFI_INVALID_PARAMETER;\r
630 }\r
631\r
632 //\r
633 // Make sure Language is a member of SupportedLanguages\r
634 //\r
635 while (*SupportedLanguages != 0) {\r
636 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
637\r
638 //\r
639 // Determine the size of the Unicode String Table by looking for a NULL Language entry\r
640 //\r
641 NumberOfEntries = 0;\r
642 if (*UnicodeStringTable != NULL) {\r
643 OldUnicodeStringTable = *UnicodeStringTable;\r
644 while (OldUnicodeStringTable->Language != NULL) {\r
645 if (CompareIso639LanguageCode (Language, OldUnicodeStringTable->Language)) {\r
646 return EFI_ALREADY_STARTED;\r
647 }\r
648\r
649 OldUnicodeStringTable++;\r
650 NumberOfEntries++;\r
651 }\r
652 }\r
653\r
654 //\r
655 // Allocate space for a new Unicode String Table. It must hold the current number of\r
656 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table\r
657 // marker\r
658 //\r
659 NewUnicodeStringTable = AllocatePool ((NumberOfEntries + 2) * sizeof (EFI_UNICODE_STRING_TABLE));\r
660 if (NewUnicodeStringTable == NULL) {\r
661 return EFI_OUT_OF_RESOURCES;\r
662 }\r
663\r
664 //\r
665 // If the current Unicode String Table contains any entries, then copy them to the\r
666 // newly allocated Unicode String Table.\r
667 //\r
668 if (*UnicodeStringTable != NULL) {\r
669 CopyMem (\r
670 NewUnicodeStringTable,\r
671 *UnicodeStringTable,\r
672 NumberOfEntries * sizeof (EFI_UNICODE_STRING_TABLE)\r
673 );\r
674 }\r
675\r
676 //\r
677 // Allocate space for a copy of the Language specifier\r
678 //\r
679 NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (3, Language);\r
680 if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) {\r
681 gBS->FreePool (NewUnicodeStringTable);\r
682 return EFI_OUT_OF_RESOURCES;\r
683 }\r
684\r
685 //\r
686 // Compute the length of the Unicode String\r
687 //\r
688 for (UnicodeStringLength = 0; UnicodeString[UnicodeStringLength] != 0; UnicodeStringLength++)\r
689 ;\r
690\r
691 //\r
692 // Allocate space for a copy of the Unicode String\r
693 //\r
694 NewUnicodeStringTable[NumberOfEntries].UnicodeString = AllocateCopyPool (\r
695 (UnicodeStringLength + 1) * sizeof (CHAR16),\r
696 UnicodeString\r
697 );\r
698 if (NewUnicodeStringTable[NumberOfEntries].UnicodeString == NULL) {\r
699 gBS->FreePool (NewUnicodeStringTable[NumberOfEntries].Language);\r
700 gBS->FreePool (NewUnicodeStringTable);\r
701 return EFI_OUT_OF_RESOURCES;\r
702 }\r
703\r
704 //\r
705 // Mark the end of the Unicode String Table\r
706 //\r
707 NewUnicodeStringTable[NumberOfEntries + 1].Language = NULL;\r
708 NewUnicodeStringTable[NumberOfEntries + 1].UnicodeString = NULL;\r
709\r
710 //\r
711 // Free the old Unicode String Table\r
712 //\r
713 if (*UnicodeStringTable != NULL) {\r
714 gBS->FreePool (*UnicodeStringTable);\r
715 }\r
716\r
717 //\r
718 // Point UnicodeStringTable at the newly allocated Unicode String Table\r
719 //\r
720 *UnicodeStringTable = NewUnicodeStringTable;\r
721\r
722 return EFI_SUCCESS;\r
723 }\r
724\r
725 SupportedLanguages += 3;\r
726 }\r
727\r
728 return EFI_UNSUPPORTED;\r
729}\r
730\r
731/**\r
732 This function frees the table of Unicode strings in UnicodeStringTable.\r
733 If UnicodeStringTable is NULL, then EFI_SUCCESS is returned.\r
734 Otherwise, each language code, and each Unicode string in the Unicode string \r
735 table are freed, and EFI_SUCCESS is returned.\r
736\r
737 @param UnicodeStringTable A pointer to the table of Unicode strings.\r
738\r
739 @retval EFI_SUCCESS The Unicode string table was freed.\r
740\r
741**/\r
742EFI_STATUS\r
743EFIAPI\r
744FreeUnicodeStringTable (\r
745 IN EFI_UNICODE_STRING_TABLE *UnicodeStringTable\r
746 )\r
747{\r
748 UINTN Index;\r
749\r
750 //\r
751 // If the Unicode String Table is NULL, then it is already freed\r
752 //\r
753 if (UnicodeStringTable == NULL) {\r
754 return EFI_SUCCESS;\r
755 }\r
756\r
757 //\r
758 // Loop through the Unicode String Table until we reach the end of table marker\r
759 //\r
760 for (Index = 0; UnicodeStringTable[Index].Language != NULL; Index++) {\r
761\r
762 //\r
763 // Free the Language string from the Unicode String Table\r
764 //\r
765 gBS->FreePool (UnicodeStringTable[Index].Language);\r
766\r
767 //\r
768 // Free the Unicode String from the Unicode String Table\r
769 //\r
770 if (UnicodeStringTable[Index].UnicodeString != NULL) {\r
771 gBS->FreePool (UnicodeStringTable[Index].UnicodeString);\r
772 }\r
773 }\r
774\r
775 //\r
776 // Free the Unicode String Table itself\r
777 //\r
778 gBS->FreePool (UnicodeStringTable);\r
779\r
780 return EFI_SUCCESS;\r
781}\r