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