]> git.proxmox.com Git - mirror_edk2.git/blame - OldMdePkg/Library/UefiLib/UefiLib.c
1. Sync the latest network stack. Add NetLibCreateIPv4DPathNode () in netlib library.
[mirror_edk2.git] / OldMdePkg / 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
93b0fbc8 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
878ddf1f 9\r
93b0fbc8 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
878ddf1f 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
93b0fbc8 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
878ddf1f 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
93b0fbc8 58EfiGetSystemConfigurationTable (\r
878ddf1f 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
93b0fbc8 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
878ddf1f 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
93b0fbc8 93 @return The notification event that was created.\r
878ddf1f 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
93b0fbc8 114 EVT_NOTIFY_SIGNAL,\r
878ddf1f 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
93b0fbc8 145 This event is signaled with EfiNamedEventSignal(). This provide the ability for\r
878ddf1f 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
93b0fbc8 151 @param NotifyContext The context parameter to pass to NotifyFunction.\r
878ddf1f 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
93b0fbc8 176 EVT_NOTIFY_SIGNAL,\r
878ddf1f 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
93b0fbc8 209 This function signals the named event specified by Name. The named event must\r
878ddf1f 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
93b0fbc8 246/**\r
d958721a 247 Returns the current TPL.\r
248\r
93b0fbc8 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
d958721a 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
93b0fbc8 268 Tpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r
d958721a 269 gBS->RestoreTPL (Tpl);\r
270\r
271 return Tpl;\r
272}\r
273\r
878ddf1f 274\r
275/**\r
93b0fbc8 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
878ddf1f 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
93b0fbc8 295 ASSERT (Priority <= TPL_HIGH_LEVEL);\r
878ddf1f 296\r
297 Lock->Tpl = Priority;\r
93b0fbc8 298 Lock->OwnerTpl = TPL_APPLICATION;\r
878ddf1f 299 Lock->Lock = EfiLockReleased ;\r
300 return Lock;\r
301}\r
302\r
303/**\r
93b0fbc8 304 This function raises the system's current task priority level to the task\r
305 priority level of the mutual exclusion lock. Then, it places the lock in the\r
878ddf1f 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
93b0fbc8 325 This function raises the system's current task priority level to the task\r
326 priority level of the mutual exclusion lock. Then, it attempts to place the\r
878ddf1f 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
93b0fbc8 360 This function transitions a mutual exclusion lock from the acquired state to\r
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
93b0fbc8 379\r
878ddf1f 380 Lock->Lock = EfiLockReleased;\r
381\r
382 gBS->RestoreTPL (Tpl);\r
383}\r
384\r
ca21f1a2 385/**
0d49d8f6 386 Tests whether a controller handle is being managed by a specific driver.
ca21f1a2 387
0d49d8f6 388 This function tests whether the driver specified by DriverBindingHandle is\r
389 currently managing the controller specified by ControllerHandle. This test\r
390 is performed by evaluating if the the protocol specified by ProtocolGuid is\r
391 present on ControllerHandle and is was opened by DriverBindingHandle with an\r
93b0fbc8 392 attribute of EFI_OPEN_PROTOCOL_BY_DRIVER.\r
ca21f1a2 393 If ProtocolGuid is NULL, then ASSERT().\r
394
395 @param ControllerHandle A handle for a controller to test.
396 @param DriverBindingHandle Specifies the driver binding handle for the
397 driver.
0d49d8f6 398 @param ProtocolGuid Specifies the protocol that the driver specified
399 by DriverBindingHandle opens in its Start()
400 function.
ca21f1a2 401
0d49d8f6 402 @retval EFI_SUCCESS ControllerHandle is managed by the driver
403 specifed by DriverBindingHandle.
404 @retval EFI_UNSUPPORTED ControllerHandle is not managed by the driver
405 specifed by DriverBindingHandle.
ca21f1a2 406
407**/\r
408EFI_STATUS\r
409EFIAPI\r
410EfiTestManagedDevice (\r
411 IN CONST EFI_HANDLE ControllerHandle,\r
412 IN CONST EFI_HANDLE DriverBindingHandle,\r
413 IN CONST EFI_GUID *ProtocolGuid\r
414 )\r
415{\r
416 EFI_STATUS Status;\r
417 VOID *ManagedInterface;\r
418\r
419 ASSERT (ProtocolGuid != NULL);\r
420\r
421 Status = gBS->OpenProtocol (\r
422 ControllerHandle,\r
423 (EFI_GUID *) ProtocolGuid,\r
424 &ManagedInterface,\r
425 DriverBindingHandle,\r
426 ControllerHandle,\r
427 EFI_OPEN_PROTOCOL_BY_DRIVER\r
428 );\r
429 if (!EFI_ERROR (Status)) {\r
430 gBS->CloseProtocol (\r
431 ControllerHandle,\r
432 (EFI_GUID *) ProtocolGuid,\r
433 DriverBindingHandle,\r
434 ControllerHandle\r
435 );\r
436 return EFI_UNSUPPORTED;\r
437 }\r
438\r
439 if (Status != EFI_ALREADY_STARTED) {\r
440 return EFI_UNSUPPORTED;\r
441 }\r
442\r
443 return EFI_SUCCESS;\r
444}\r
445\r
446/**
0d49d8f6 447 Tests whether a child handle is a child device of the controller.
ca21f1a2 448
0d49d8f6 449 This function tests whether ChildHandle is one of the children of\r
450 ControllerHandle. This test is performed by checking to see if the protocol\r
451 specified by ProtocolGuid is present on ControllerHandle and opened by\r
452 ChildHandle with an attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.\r
453 If ProtocolGuid is NULL, then ASSERT().\r
ca21f1a2 454
93b0fbc8 455 @param ControllerHandle A handle for a (parent) controller to test.
ca21f1a2 456 @param ChildHandle A child handle to test.
0d49d8f6 457 @param ConsumsedGuid Supplies the protocol that the child controller
93b0fbc8 458 opens on its parent controller.
ca21f1a2 459
460 @retval EFI_SUCCESS ChildHandle is a child of the ControllerHandle.
461 @retval EFI_UNSUPPORTED ChildHandle is not a child of the
462 ControllerHandle.
463
464**/\r
465EFI_STATUS\r
466EFIAPI\r
467EfiTestChildHandle (\r
468 IN CONST EFI_HANDLE ControllerHandle,\r
469 IN CONST EFI_HANDLE ChildHandle,\r
470 IN CONST EFI_GUID *ProtocolGuid\r
471 )\r
472{\r
473 EFI_STATUS Status;\r
474 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfoBuffer;\r
475 UINTN EntryCount;\r
476 UINTN Index;\r
477\r
478 ASSERT (ProtocolGuid != NULL);\r
479\r
480 //\r
481 // Retrieve the list of agents that are consuming the specific protocol\r
482 // on ControllerHandle.\r
483 //\r
484 Status = gBS->OpenProtocolInformation (\r
485 ControllerHandle,\r
486 (EFI_GUID *) ProtocolGuid,\r
487 &OpenInfoBuffer,\r
488 &EntryCount\r
489 );\r
490 if (EFI_ERROR (Status)) {\r
491 return EFI_UNSUPPORTED;\r
492 }\r
493\r
494 //\r
495 // Inspect if ChildHandle is one of the agents.\r
496 //\r
497 Status = EFI_UNSUPPORTED;\r
498 for (Index = 0; Index < EntryCount; Index++) {\r
499 if ((OpenInfoBuffer[Index].ControllerHandle == ChildHandle) &&\r
500 (OpenInfoBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {\r
501 Status = EFI_SUCCESS;\r
502 break;\r
503 }\r
504 }\r
93b0fbc8 505\r
ca21f1a2 506 FreePool (OpenInfoBuffer);\r
507 return Status;\r
508}\r
509\r
878ddf1f 510/**\r
93b0fbc8 511 This function looks up a Unicode string in UnicodeStringTable. If Language is\r
878ddf1f 512 a member of SupportedLanguages and a Unicode string is found in UnicodeStringTable\r
93b0fbc8 513 that matches the language code specified by Language, then it is returned in\r
878ddf1f 514 UnicodeString.\r
515\r
93b0fbc8 516 @param Language A pointer to the ISO 639-2 language code for the\r
878ddf1f 517 Unicode string to look up and return.\r
93b0fbc8 518 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes\r
519 that the Unicode string table supports. Language\r
878ddf1f 520 must be a member of this set.\r
521 @param UnicodeStringTable A pointer to the table of Unicode strings.\r
522 @param UnicodeString A pointer to the Unicode string from UnicodeStringTable\r
523 that matches the language specified by Language.\r
524\r
93b0fbc8 525 @retval EFI_SUCCESS The Unicode string that matches the language\r
878ddf1f 526 specified by Language was found\r
93b0fbc8 527 in the table of Unicoide strings UnicodeStringTable,\r
878ddf1f 528 and it was returned in UnicodeString.\r
529 @retval EFI_INVALID_PARAMETER Language is NULL.\r
530 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.\r
531 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.\r
532 @retval EFI_UNSUPPORTED UnicodeStringTable is NULL.\r
93b0fbc8 533 @retval EFI_UNSUPPORTED The language specified by Language is not a\r
878ddf1f 534 member of SupportedLanguages.\r
93b0fbc8 535 @retval EFI_UNSUPPORTED The language specified by Language is not\r
878ddf1f 536 supported by UnicodeStringTable.\r
537\r
538**/\r
539EFI_STATUS\r
540EFIAPI\r
541LookupUnicodeString (\r
542 IN CONST CHAR8 *Language,\r
543 IN CONST CHAR8 *SupportedLanguages,\r
544 IN CONST EFI_UNICODE_STRING_TABLE *UnicodeStringTable,\r
545 OUT CHAR16 **UnicodeString\r
546 )\r
547{\r
548 //\r
549 // Make sure the parameters are valid\r
550 //\r
551 if (Language == NULL || UnicodeString == NULL) {\r
552 return EFI_INVALID_PARAMETER;\r
553 }\r
554\r
555 //\r
556 // If there are no supported languages, or the Unicode String Table is empty, then the\r
557 // Unicode String specified by Language is not supported by this Unicode String Table\r
558 //\r
559 if (SupportedLanguages == NULL || UnicodeStringTable == NULL) {\r
560 return EFI_UNSUPPORTED;\r
561 }\r
562\r
563 //\r
564 // Make sure Language is in the set of Supported Languages\r
565 //\r
566 while (*SupportedLanguages != 0) {\r
567 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
568\r
569 //\r
570 // Search the Unicode String Table for the matching Language specifier\r
571 //\r
572 while (UnicodeStringTable->Language != NULL) {\r
573 if (CompareIso639LanguageCode (Language, UnicodeStringTable->Language)) {\r
574\r
575 //\r
576 // A matching string was found, so return it\r
577 //\r
578 *UnicodeString = UnicodeStringTable->UnicodeString;\r
579 return EFI_SUCCESS;\r
580 }\r
581\r
582 UnicodeStringTable++;\r
583 }\r
584\r
585 return EFI_UNSUPPORTED;\r
586 }\r
587\r
588 SupportedLanguages += 3;\r
589 }\r
590\r
591 return EFI_UNSUPPORTED;\r
592}\r
593\r
594/**\r
595 This function adds a Unicode string to UnicodeStringTable.\r
93b0fbc8 596 If Language is a member of SupportedLanguages then UnicodeString is added to\r
597 UnicodeStringTable. New buffers are allocated for both Language and\r
598 UnicodeString. The contents of Language and UnicodeString are copied into\r
599 these new buffers. These buffers are automatically freed when\r
878ddf1f 600 FreeUnicodeStringTable() is called.\r
601\r
93b0fbc8 602 @param Language A pointer to the ISO 639-2 language code for the Unicode\r
878ddf1f 603 string to add.\r
604 @param SupportedLanguages A pointer to the set of ISO 639-2 language codes\r
605 that the Unicode string table supports.\r
606 Language must be a member of this set.\r
607 @param UnicodeStringTable A pointer to the table of Unicode strings.\r
608 @param UnicodeString A pointer to the Unicode string to add.\r
609\r
93b0fbc8 610 @retval EFI_SUCCESS The Unicode string that matches the language\r
611 specified by Language was found in the table of\r
612 Unicode strings UnicodeStringTable, and it was\r
878ddf1f 613 returned in UnicodeString.\r
614 @retval EFI_INVALID_PARAMETER Language is NULL.\r
615 @retval EFI_INVALID_PARAMETER UnicodeString is NULL.\r
616 @retval EFI_INVALID_PARAMETER UnicodeString is an empty string.\r
617 @retval EFI_UNSUPPORTED SupportedLanguages is NULL.\r
93b0fbc8 618 @retval EFI_ALREADY_STARTED A Unicode string with language Language is\r
878ddf1f 619 already present in UnicodeStringTable.\r
93b0fbc8 620 @retval EFI_OUT_OF_RESOURCES There is not enough memory to add another\r
878ddf1f 621 Unicode string to UnicodeStringTable.\r
93b0fbc8 622 @retval EFI_UNSUPPORTED The language specified by Language is not a\r
878ddf1f 623 member of SupportedLanguages.\r
624\r
625**/\r
626EFI_STATUS\r
627EFIAPI\r
628AddUnicodeString (\r
629 IN CONST CHAR8 *Language,\r
630 IN CONST CHAR8 *SupportedLanguages,\r
631 IN EFI_UNICODE_STRING_TABLE **UnicodeStringTable,\r
632 IN CONST CHAR16 *UnicodeString\r
633 )\r
634{\r
635 UINTN NumberOfEntries;\r
636 EFI_UNICODE_STRING_TABLE *OldUnicodeStringTable;\r
637 EFI_UNICODE_STRING_TABLE *NewUnicodeStringTable;\r
638 UINTN UnicodeStringLength;\r
639\r
640 //\r
641 // Make sure the parameter are valid\r
642 //\r
643 if (Language == NULL || UnicodeString == NULL || UnicodeStringTable == NULL) {\r
644 return EFI_INVALID_PARAMETER;\r
645 }\r
646\r
647 //\r
648 // If there are no supported languages, then a Unicode String can not be added\r
649 //\r
650 if (SupportedLanguages == NULL) {\r
651 return EFI_UNSUPPORTED;\r
652 }\r
653\r
654 //\r
655 // If the Unicode String is empty, then a Unicode String can not be added\r
656 //\r
657 if (UnicodeString[0] == 0) {\r
658 return EFI_INVALID_PARAMETER;\r
659 }\r
660\r
661 //\r
662 // Make sure Language is a member of SupportedLanguages\r
663 //\r
664 while (*SupportedLanguages != 0) {\r
665 if (CompareIso639LanguageCode (Language, SupportedLanguages)) {\r
666\r
667 //\r
668 // Determine the size of the Unicode String Table by looking for a NULL Language entry\r
669 //\r
670 NumberOfEntries = 0;\r
671 if (*UnicodeStringTable != NULL) {\r
672 OldUnicodeStringTable = *UnicodeStringTable;\r
673 while (OldUnicodeStringTable->Language != NULL) {\r
674 if (CompareIso639LanguageCode (Language, OldUnicodeStringTable->Language)) {\r
675 return EFI_ALREADY_STARTED;\r
676 }\r
677\r
678 OldUnicodeStringTable++;\r
679 NumberOfEntries++;\r
680 }\r
681 }\r
682\r
683 //\r
684 // Allocate space for a new Unicode String Table. It must hold the current number of\r
685 // entries, plus 1 entry for the new Unicode String, plus 1 entry for the end of table\r
686 // marker\r
687 //\r
688 NewUnicodeStringTable = AllocatePool ((NumberOfEntries + 2) * sizeof (EFI_UNICODE_STRING_TABLE));\r
689 if (NewUnicodeStringTable == NULL) {\r
690 return EFI_OUT_OF_RESOURCES;\r
691 }\r
692\r
693 //\r
694 // If the current Unicode String Table contains any entries, then copy them to the\r
695 // newly allocated Unicode String Table.\r
696 //\r
697 if (*UnicodeStringTable != NULL) {\r
698 CopyMem (\r
699 NewUnicodeStringTable,\r
700 *UnicodeStringTable,\r
701 NumberOfEntries * sizeof (EFI_UNICODE_STRING_TABLE)\r
702 );\r
703 }\r
704\r
705 //\r
706 // Allocate space for a copy of the Language specifier\r
707 //\r
708 NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (3, Language);\r
709 if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) {\r
710 gBS->FreePool (NewUnicodeStringTable);\r
711 return EFI_OUT_OF_RESOURCES;\r
712 }\r
713\r
714 //\r
715 // Compute the length of the Unicode String\r
716 //\r
717 for (UnicodeStringLength = 0; UnicodeString[UnicodeStringLength] != 0; UnicodeStringLength++)\r
718 ;\r
719\r
720 //\r
721 // Allocate space for a copy of the Unicode String\r
722 //\r
723 NewUnicodeStringTable[NumberOfEntries].UnicodeString = AllocateCopyPool (\r
724 (UnicodeStringLength + 1) * sizeof (CHAR16),\r
725 UnicodeString\r
726 );\r
727 if (NewUnicodeStringTable[NumberOfEntries].UnicodeString == NULL) {\r
728 gBS->FreePool (NewUnicodeStringTable[NumberOfEntries].Language);\r
729 gBS->FreePool (NewUnicodeStringTable);\r
730 return EFI_OUT_OF_RESOURCES;\r
731 }\r
732\r
733 //\r
734 // Mark the end of the Unicode String Table\r
735 //\r
736 NewUnicodeStringTable[NumberOfEntries + 1].Language = NULL;\r
737 NewUnicodeStringTable[NumberOfEntries + 1].UnicodeString = NULL;\r
738\r
739 //\r
740 // Free the old Unicode String Table\r
741 //\r
742 if (*UnicodeStringTable != NULL) {\r
743 gBS->FreePool (*UnicodeStringTable);\r
744 }\r
745\r
746 //\r
747 // Point UnicodeStringTable at the newly allocated Unicode String Table\r
748 //\r
749 *UnicodeStringTable = NewUnicodeStringTable;\r
750\r
751 return EFI_SUCCESS;\r
752 }\r
753\r
754 SupportedLanguages += 3;\r
755 }\r
756\r
757 return EFI_UNSUPPORTED;\r
758}\r
759\r
760/**\r
761 This function frees the table of Unicode strings in UnicodeStringTable.\r
762 If UnicodeStringTable is NULL, then EFI_SUCCESS is returned.\r
93b0fbc8 763 Otherwise, each language code, and each Unicode string in the Unicode string\r
878ddf1f 764 table are freed, and EFI_SUCCESS is returned.\r
765\r
766 @param UnicodeStringTable A pointer to the table of Unicode strings.\r
767\r
768 @retval EFI_SUCCESS The Unicode string table was freed.\r
769\r
770**/\r
771EFI_STATUS\r
772EFIAPI\r
773FreeUnicodeStringTable (\r
774 IN EFI_UNICODE_STRING_TABLE *UnicodeStringTable\r
775 )\r
776{\r
777 UINTN Index;\r
778\r
779 //\r
780 // If the Unicode String Table is NULL, then it is already freed\r
781 //\r
782 if (UnicodeStringTable == NULL) {\r
783 return EFI_SUCCESS;\r
784 }\r
785\r
786 //\r
787 // Loop through the Unicode String Table until we reach the end of table marker\r
788 //\r
789 for (Index = 0; UnicodeStringTable[Index].Language != NULL; Index++) {\r
790\r
791 //\r
792 // Free the Language string from the Unicode String Table\r
793 //\r
794 gBS->FreePool (UnicodeStringTable[Index].Language);\r
795\r
796 //\r
797 // Free the Unicode String from the Unicode String Table\r
798 //\r
799 if (UnicodeStringTable[Index].UnicodeString != NULL) {\r
800 gBS->FreePool (UnicodeStringTable[Index].UnicodeString);\r
801 }\r
802 }\r
803\r
804 //\r
805 // Free the Unicode String Table itself\r
806 //\r
807 gBS->FreePool (UnicodeStringTable);\r
808\r
809 return EFI_SUCCESS;\r
810}\r
d958721a 811\r