]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/UefiLib/UefiLib.c
MdePkg UefiLib: Fix in EfiLocateProtocolBuffer()
[mirror_edk2.git] / MdePkg / Library / UefiLib / UefiLib.c
index 28f5c7d883b8e7ade3637efe325ffbf5d26bb3e5..9ade1b0ce1a0e5842a7815af6dbfddbb92daf450 100644 (file)
@@ -1,22 +1,46 @@
 /** @file\r
-  Mde UEFI library functions.\r
-\r
-  Copyright (c) 2006 - 2007, Intel Corporation<BR>\r
-  All rights reserved. This program and the accompanying materials\r
+  The UEFI Library provides functions and macros that simplify the development of \r
+  UEFI Drivers and UEFI Applications.  These functions and macros help manage EFI \r
+  events, build simple locks utilizing EFI Task Priority Levels (TPLs), install \r
+  EFI Driver Model related protocols, manage Unicode string tables for UEFI Drivers, \r
+  and print messages on the console output and standard error devices.\r
+\r
+  Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+  This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
-  http://opensource.org/licenses/bsd-license.php\r
+  http://opensource.org/licenses/bsd-license.php.\r
 \r
   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
 **/\r
 \r
-//\r
-// Include common header file for this module.\r
-//\r
+\r
 #include "UefiLibInternal.h"\r
 \r
+/**\r
+  Empty constructor function that is required to resolve dependencies between \r
+  libraries.\r
+  \r
+    ** DO NOT REMOVE **\r
+  \r
+  @param  ImageHandle   The firmware allocated handle for the EFI image.\r
+  @param  SystemTable   A pointer to the EFI System Table.\r
+  \r
+  @retval EFI_SUCCESS   The constructor executed correctly.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+UefiLibConstructor (\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable\r
+  )\r
+{\r
+  return EFI_SUCCESS;\r
+}\r
+\r
 /**\r
   Compare whether two names of languages are identical.\r
 \r
@@ -27,7 +51,6 @@
   @retval FALSE     Language 1 and language 2 are not the same.\r
 \r
 **/\r
-STATIC\r
 BOOLEAN\r
 CompareIso639LanguageCode (\r
   IN CONST CHAR8  *Language1,\r
@@ -44,13 +67,18 @@ CompareIso639LanguageCode (
 }\r
 \r
 /**\r
-  This function searches the list of configuration tables stored in the EFI System\r
-  Table for a table with a GUID that matches TableGuid.  If a match is found,\r
-  then a pointer to the configuration table is returned in Table, and EFI_SUCCESS\r
-  is returned.  If a matching GUID is not found, then EFI_NOT_FOUND is returned.\r
+  Retrieves a pointer to the system configuration table from the EFI System Table\r
+  based on a specified GUID.\r
+  \r
+  This function searches the list of configuration tables stored in the EFI System Table\r
+  for a table with a GUID that matches TableGuid.  If a match is found, then a pointer to\r
+  the configuration table is returned in Table., and EFI_SUCCESS is returned. If a matching GUID\r
+  is not found, then EFI_NOT_FOUND is returned.\r
+  If TableGuid is NULL, then ASSERT().\r
+  If Table is NULL, then ASSERT().\r
 \r
-  @param  TableGuid       Pointer to table's GUID type..\r
-  @param  Table           Pointer to the table associated with TableGuid in the EFI System Table.\r
+  @param  TableGuid       The pointer to table's GUID type.\r
+  @param  Table           The pointer to the table associated with TableGuid in the EFI System Table.\r
 \r
   @retval EFI_SUCCESS     A configuration table matching TableGuid was found.\r
   @retval EFI_NOT_FOUND   A configuration table matching TableGuid could not be found.\r
@@ -82,16 +110,28 @@ EfiGetSystemConfigurationTable (
 }\r
 \r
 /**\r
-  This function causes the notification function to be executed for every protocol\r
-  of type ProtocolGuid instance that exists in the system when this function is\r
-  invoked.  In addition, every time a protocol of type ProtocolGuid instance is\r
-  installed or reinstalled, the notification function is also executed.\r
+  Creates and returns a notification event and registers that event with all the protocol\r
+  instances specified by ProtocolGuid.\r
+\r
+  This function causes the notification function to be executed for every protocol of type\r
+  ProtocolGuid instance that exists in the system when this function is invoked. If there are\r
+  no instances of ProtocolGuid in the handle database at the time this function is invoked,\r
+  then the notification function is still executed one time. In addition, every time a protocol\r
+  of type ProtocolGuid instance is installed or reinstalled, the notification function is also\r
+  executed. This function returns the notification event that was created. \r
+  If ProtocolGuid is NULL, then ASSERT().\r
+  If NotifyTpl is not a legal TPL value, then ASSERT().\r
+  If NotifyFunction is NULL, then ASSERT().\r
+  If Registration is NULL, then ASSERT().\r
+\r
 \r
   @param  ProtocolGuid    Supplies GUID of the protocol upon whose installation the event is fired.\r
   @param  NotifyTpl       Supplies the task priority level of the event notifications.\r
   @param  NotifyFunction  Supplies the function to notify when the event is signaled.\r
   @param  NotifyContext   The context parameter to pass to NotifyFunction.\r
   @param  Registration    A pointer to a memory location to receive the registration value.\r
+                          This value is passed to LocateHandle() to obtain new handles that\r
+                          have been added that support the ProtocolGuid-specified protocol. \r
 \r
   @return The notification event that was created.\r
 \r
@@ -109,6 +149,10 @@ EfiCreateProtocolNotifyEvent(
   EFI_STATUS  Status;\r
   EFI_EVENT   Event;\r
 \r
+  ASSERT (ProtocolGuid != NULL);\r
+  ASSERT (NotifyFunction != NULL);\r
+  ASSERT (Registration != NULL);\r
+\r
   //\r
   // Create the event\r
   //\r
@@ -123,7 +167,7 @@ EfiCreateProtocolNotifyEvent(
   ASSERT_EFI_ERROR (Status);\r
 \r
   //\r
-  // Register for protocol notifactions on this event\r
+  // Register for protocol notifications on this event\r
   //\r
 \r
   Status = gBS->RegisterProtocolNotify (\r
@@ -144,14 +188,19 @@ EfiCreateProtocolNotifyEvent(
 }\r
 \r
 /**\r
+  Creates a named event that can be signaled with EfiNamedEventSignal().\r
+\r
   This function creates an event using NotifyTpl, NoifyFunction, and NotifyContext.\r
-  This event is signaled with EfiNamedEventSignal().  This provide the ability for\r
-  one or more listeners on the same event named by the GUID specified by Name.\r
+  This event is signaled with EfiNamedEventSignal(). This provides the ability for one or more\r
+  listeners on the same event named by the GUID specified by Name. \r
+  If Name is NULL, then ASSERT().\r
+  If NotifyTpl is not a legal TPL value, then ASSERT().\r
+  If NotifyFunction is NULL, then ASSERT().\r
 \r
-  @param  Name                  Supplies GUID name of the event.\r
+  @param  Name                  Supplies the GUID name of the event.\r
   @param  NotifyTpl             Supplies the task priority level of the event notifications.\r
   @param  NotifyFunction        Supplies the function to notify when the event is signaled.\r
-  @param  NotifyContext         The context parameter to pass to NotifyFunction.\r
+  @param  NotifyContext         The context parameter to pass to NotifyFunction. \r
   @param  Registration          A pointer to a memory location to receive the registration value.\r
 \r
   @retval EFI_SUCCESS           A named event was created.\r
@@ -172,6 +221,10 @@ EfiNamedEventListen (
   EFI_EVENT   Event;\r
   VOID        *RegistrationLocal;\r
 \r
+  ASSERT (Name != NULL);\r
+  ASSERT (NotifyFunction != NULL);\r
+  ASSERT (NotifyTpl <= TPL_HIGH_LEVEL);\r
+  \r
   //\r
   // Create event\r
   //\r
@@ -205,14 +258,17 @@ EfiNamedEventListen (
                   );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
-  return EFI_SUCCESS;\r
+  return Status;\r
 }\r
 \r
 /**\r
-  This function signals the named event specified by Name.  The named event must\r
-  have been created with EfiNamedEventListen().\r
+  Signals a named event created with EfiNamedEventListen().\r
 \r
-  @param  Name                  Supplies GUID name of the event.\r
+  This function signals the named event specified by Name. The named event must have been\r
+  created with EfiNamedEventListen().\r
+  If Name is NULL, then ASSERT().\r
+\r
+  @param  Name                  Supplies the GUID name of the event.\r
 \r
   @retval EFI_SUCCESS           A named event was signaled.\r
   @retval EFI_OUT_OF_RESOURCES  There are not enough resource to signal the named event.\r
@@ -227,6 +283,8 @@ EfiNamedEventSignal (
   EFI_STATUS  Status;\r
   EFI_HANDLE  Handle;\r
 \r
+  ASSERT(Name != NULL);\r
+\r
   Handle = NULL;\r
   Status = gBS->InstallProtocolInterface (\r
                   &Handle,\r
@@ -243,21 +301,80 @@ EfiNamedEventSignal (
                   );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
-  return EFI_SUCCESS;\r
+  return Status;\r
 }\r
 \r
 /**\r
+  Signals an event group by placing a new event in the group temporarily and\r
+  signaling it.\r
+\r
+  @param[in] EventGroup          Supplies the unique identifier of the event\r
+                                 group to signal.\r
+\r
+  @retval EFI_SUCCESS            The event group was signaled successfully.\r
+  @retval EFI_INVALID_PARAMETER  EventGroup is NULL.\r
+  @return                        Error codes that report problems about event\r
+                                 creation or signaling.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+EfiEventGroupSignal (\r
+  IN CONST EFI_GUID *EventGroup\r
+  )\r
+{\r
+  EFI_STATUS Status;\r
+  EFI_EVENT  Event;\r
+\r
+  if (EventGroup == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Status = gBS->CreateEventEx (\r
+                  EVT_NOTIFY_SIGNAL,\r
+                  TPL_CALLBACK,\r
+                  EfiEventEmptyFunction,\r
+                  NULL,\r
+                  EventGroup,\r
+                  &Event\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  Status = gBS->SignalEvent (Event);\r
+  gBS->CloseEvent (Event);\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  An empty function that can be used as NotifyFunction parameter of\r
+  CreateEvent() or CreateEventEx().\r
+\r
+  @param Event              Event whose notification function is being invoked.\r
+  @param Context            The pointer to the notification function's context,\r
+                            which is implementation-dependent.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+EfiEventEmptyFunction (\r
+  IN EFI_EVENT              Event,\r
+  IN VOID                   *Context\r
+  )\r
+{\r
+}\r
+\r
+/** \r
   Returns the current TPL.\r
 \r
-  This function returns the current TPL.  There is no EFI service to directly\r
-  retrieve the current TPL. Instead, the RaiseTPL() function is used to raise\r
-  the TPL to TPL_HIGH_LEVEL.  This will return the current TPL.  The TPL level\r
-  can then immediately be restored back to the current TPL level with a call\r
+  This function returns the current TPL.  There is no EFI service to directly \r
+  retrieve the current TPL. Instead, the RaiseTPL() function is used to raise \r
+  the TPL to TPL_HIGH_LEVEL.  This will return the current TPL.  The TPL level \r
+  can then immediately be restored back to the current TPL level with a call \r
   to RestoreTPL().\r
 \r
-  @param  VOID\r
-\r
-  @retvale EFI_TPL              The current TPL.\r
+  @return The current TPL.\r
 \r
 **/\r
 EFI_TPL\r
@@ -276,13 +393,17 @@ EfiGetCurrentTpl (
 \r
 \r
 /**\r
-  This function initializes a basic mutual exclusion lock to the released state\r
-  and returns the lock.  Each lock provides mutual exclusion access at its task\r
+  Initializes a basic mutual exclusion lock.\r
+\r
+  This function initializes a basic mutual exclusion lock to the released state \r
+  and returns the lock.  Each lock provides mutual exclusion access at its task \r
   priority level.  Since there is no preemption or multiprocessor support in EFI,\r
   acquiring the lock only consists of raising to the locks TPL.\r
+  If Lock is NULL, then ASSERT().\r
+  If Priority is not a valid TPL value, then ASSERT().\r
 \r
   @param  Lock       A pointer to the lock data structure to initialize.\r
-  @param  Priority   EFI TPL associated with the lock.\r
+  @param  Priority   EFI TPL is associated with the lock.\r
 \r
   @return The lock.\r
 \r
@@ -304,11 +425,16 @@ EfiInitializeLock (
 }\r
 \r
 /**\r
-  This function raises the system's current task priority level to the task\r
-  priority level of the mutual exclusion lock.  Then, it places the lock in the\r
+  Acquires ownership of a lock.\r
+\r
+  This function raises the system's current task priority level to the task \r
+  priority level of the mutual exclusion lock.  Then, it places the lock in the \r
   acquired state.\r
+  If Lock is NULL, then ASSERT().\r
+  If Lock is not initialized, then ASSERT().\r
+  If Lock is already in the acquired state, then ASSERT().\r
 \r
-  @param  Priority  The task priority level of the lock.\r
+  @param  Lock              A pointer to the lock to acquire.\r
 \r
 **/\r
 VOID\r
@@ -325,9 +451,14 @@ EfiAcquireLock (
 }\r
 \r
 /**\r
-  This function raises the system's current task priority level to the task\r
-  priority level of the mutual exclusion lock.  Then, it attempts to place the\r
-  lock in the acquired state.\r
+  Acquires ownership of a lock.\r
+\r
+  This function raises the system's current task priority level to the task priority\r
+  level of the mutual exclusion lock.  Then, it attempts to place the lock in the acquired state.\r
+  If the lock is already in the acquired state, then EFI_ACCESS_DENIED is returned.\r
+  Otherwise, EFI_SUCCESS is returned.\r
+  If Lock is NULL, then ASSERT().\r
+  If Lock is not initialized, then ASSERT().\r
 \r
   @param  Lock              A pointer to the lock to acquire.\r
 \r
@@ -360,9 +491,14 @@ EfiAcquireLockOrFail (
 }\r
 \r
 /**\r
-  This function transitions a mutual exclusion lock from the acquired state to\r
-  the released state, and restores the system's task priority level to its\r
+  Releases ownership of a lock.\r
+\r
+  This function transitions a mutual exclusion lock from the acquired state to \r
+  the released state, and restores the system's task priority level to its \r
   previous level.\r
+  If Lock is NULL, then ASSERT().\r
+  If Lock is not initialized, then ASSERT().\r
+  If Lock is already in the released state, then ASSERT().\r
 \r
   @param  Lock  A pointer to the lock to release.\r
 \r
@@ -392,7 +528,7 @@ EfiReleaseLock (
   currently managing the controller specified by ControllerHandle.  This test\r
   is performed by evaluating if the the protocol specified by ProtocolGuid is\r
   present on ControllerHandle and is was opened by DriverBindingHandle with an\r
-  attribute of EFI_OPEN_PROTOCOL_BY_DRIVER.\r
+  attribute of EFI_OPEN_PROTOCOL_BY_DRIVER. \r
   If ProtocolGuid is NULL, then ASSERT().\r
 \r
   @param  ControllerHandle     A handle for a controller to test.\r
@@ -403,9 +539,9 @@ EfiReleaseLock (
                                function.\r
 \r
   @retval EFI_SUCCESS          ControllerHandle is managed by the driver\r
-                               specifed by DriverBindingHandle.\r
+                               specified by DriverBindingHandle.\r
   @retval EFI_UNSUPPORTED      ControllerHandle is not managed by the driver\r
-                               specifed by DriverBindingHandle.\r
+                               specified by DriverBindingHandle.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -455,10 +591,10 @@ EfiTestManagedDevice (
   ChildHandle with an attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.\r
   If ProtocolGuid is NULL, then ASSERT().\r
 \r
-  @param  ControllerHandle     A handle for a (parent) controller to test.\r
+  @param  ControllerHandle     A handle for a (parent) controller to test. \r
   @param  ChildHandle          A child handle to test.\r
-  @param  ConsumsedGuid        Supplies the protocol that the child controller\r
-                               opens on its parent controller.\r
+  @param  ProtocolGuid         Supplies the protocol that the child controller\r
+                               opens on its parent controller. \r
 \r
   @retval EFI_SUCCESS          ChildHandle is a child of the ControllerHandle.\r
   @retval EFI_UNSUPPORTED      ChildHandle is not a child of the\r
@@ -512,50 +648,32 @@ EfiTestChildHandle (
 \r
 /**\r
   This function looks up a Unicode string in UnicodeStringTable.\r
-  If Language is a member of SupportedLanguages and a Unicode\r
-  string is found in UnicodeStringTable that matches the\r
-  language code specified by Language, then it is returned in\r
-  UnicodeString.\r
-\r
-  @param  Language                A pointer to the ISO 639-2\r
-                                  language code for the Unicode\r
-                                  string to look up and return.\r
-  \r
-  @param  SupportedLanguages      A pointer to the set of ISO\r
-                                  639-2language\r
-                                  codes that the Unicode string\r
-                                  table supports. Language must\r
-                                  be a member of this set.\r
-  \r
-  @param  UnicodeStringTable      A pointer to the table of\r
-                                  Unicode strings.\r
-  \r
-  @param  UnicodeString           A pointer to the Unicode\r
-                                  string from UnicodeStringTable\r
-                                  that matches the language\r
-                                  specified by Language.\r
-\r
-  @retval  EFI_SUCCESS            The Unicode string that\r
-                                  matches the language specified\r
-                                  by Language was found in the\r
-                                  table of Unicoide strings\r
-                                  UnicodeStringTable, and it was\r
-                                  returned in UnicodeString.\r
-  \r
-  @retval  EFI_INVALID_PARAMETER  Language is NULL.\r
-  \r
-  @retval  EFI_INVALID_PARAMETER  UnicodeString is NULL.\r
-  @retval  EFI_UNSUPPORTED        SupportedLanguages is NULL.\r
-  \r
-  @retval  EFI_UNSUPPORTED        UnicodeStringTable is NULL.\r
-  \r
-  @retval  EFI_UNSUPPORTED        The language specified by\r
-                                  Language is not a member\r
-                                  ofSupportedLanguages.\r
-  \r
-  @retval EFI_UNSUPPORTED         The language specified by\r
-                                  Language is not supported by\r
-                                  UnicodeStringTable.\r
+\r
+  If Language is a member of SupportedLanguages and a Unicode string is found in\r
+  UnicodeStringTable that matches the language code specified by Language, then it\r
+  is returned in UnicodeString.\r
+\r
+  @param  Language                A pointer to the ISO 639-2 language code for the \r
+                                  Unicode string to look up and return.\r
+  @param  SupportedLanguages      A pointer to the set of ISO 639-2 language codes \r
+                                  that the Unicode string table supports.  Language \r
+                                  must be a member of this set.\r
+  @param  UnicodeStringTable      A pointer to the table of Unicode strings.\r
+  @param  UnicodeString           A pointer to the Unicode string from UnicodeStringTable\r
+                                  that matches the language specified by Language.\r
+\r
+  @retval EFI_SUCCESS             The Unicode string that matches the language \r
+                                  specified by Language was found\r
+                                  in the table of Unicode strings UnicodeStringTable, \r
+                                  and it was returned in UnicodeString.\r
+  @retval EFI_INVALID_PARAMETER   Language is NULL.\r
+  @retval EFI_INVALID_PARAMETER   UnicodeString is NULL.\r
+  @retval EFI_UNSUPPORTED         SupportedLanguages is NULL.\r
+  @retval EFI_UNSUPPORTED         UnicodeStringTable is NULL.\r
+  @retval EFI_UNSUPPORTED         The language specified by Language is not a \r
+                                  member of SupportedLanguages.\r
+  @retval EFI_UNSUPPORTED         The language specified by Language is not \r
+                                  supported by UnicodeStringTable.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -617,58 +735,42 @@ LookupUnicodeString (
 \r
 /**\r
   This function looks up a Unicode string in UnicodeStringTable.\r
-  If Language is a member of SupportedLanguages and a Unicode\r
-  string is found in UnicodeStringTable that matches the\r
-  language code specified by Language, then it is returned in\r
-  UnicodeString.\r
-\r
-  @param  Language                A pointer to the ISO 639-2 or\r
-                                  RFC 3066 language code for the\r
-                                  Unicode string to look up and\r
-                                  return.\r
-  \r
-  @param  SupportedLanguages      A pointer to the set of ISO\r
-                                  639-2 or RFC 3066 language\r
-                                  codes that the Unicode string\r
-                                  table supports. Language must\r
-                                  be a member of this set.\r
-  \r
-  @param  UnicodeStringTable      A pointer to the table of\r
-                                  Unicode strings.\r
-  \r
-  @param  UnicodeString           A pointer to the Unicode\r
-                                  string from UnicodeStringTable\r
-                                  that matches the language\r
-                                  specified by Language.\r
-\r
-  @param  Iso639Language          Specify the language code\r
-                                  format supported. If true,\r
-                                  then the format follow ISO\r
-                                  639-2. If false, then it\r
-                                  follows RFC3066.\r
-\r
-  @retval  EFI_SUCCESS            The Unicode string that\r
-                                  matches the language specified\r
-                                  by Language was found in the\r
-                                  table of Unicoide strings\r
-                                  UnicodeStringTable, and it was\r
-                                  returned in UnicodeString.\r
-  \r
-  @retval  EFI_INVALID_PARAMETER  Language is NULL.\r
-  \r
-  @retval  EFI_INVALID_PARAMETER  UnicodeString is NULL.\r
-  \r
-  @retval  EFI_UNSUPPORTED        SupportedLanguages is NULL.\r
-  \r
-  @retval  EFI_UNSUPPORTED        UnicodeStringTable is NULL.\r
-  \r
-  @retval  EFI_UNSUPPORTED        The language specified by\r
-                                  Language is not a member\r
-                                  ofSupportedLanguages.\r
-  \r
-  @retval EFI_UNSUPPORTED         The language specified by\r
-                                  Language is not supported by\r
-                                  UnicodeStringTable.\r
+\r
+  If Language is a member of SupportedLanguages and a Unicode string is found in\r
+  UnicodeStringTable that matches the language code specified by Language, then\r
+  it is returned in UnicodeString.\r
+\r
+  @param  Language             A pointer to an ASCII string containing the ISO 639-2 or the\r
+                               RFC 4646 language code for the Unicode string to look up and\r
+                               return. If Iso639Language is TRUE, then this ASCII string is\r
+                               not assumed to be Null-terminated, and only the first three\r
+                               characters are used. If Iso639Language is FALSE, then this ASCII\r
+                               string must be Null-terminated. \r
+  @param  SupportedLanguages   A pointer to a Null-terminated ASCII string that contains a\r
+                               set of ISO 639-2 or RFC 4646 language codes that the Unicode\r
+                               string table supports.  Language must be a member of this set.\r
+                               If Iso639Language is TRUE, then this string contains one or more\r
+                               ISO 639-2 language codes with no separator characters. If Iso639Language\r
+                               is FALSE, then is string contains one or more RFC 4646 language\r
+                               codes separated by ';'.\r
+  @param  UnicodeStringTable   A pointer to the table of Unicode strings. Type EFI_UNICODE_STRING_TABLE\r
+                               is defined in "Related Definitions".\r
+  @param  UnicodeString        A pointer to the Null-terminated Unicode string from UnicodeStringTable\r
+                               that matches the language specified by Language.\r
+  @param  Iso639Language       Specifies the supported language code format. If it is TRUE, then\r
+                               Language and SupportedLanguages follow ISO 639-2 language code format.\r
+                               Otherwise, they follow RFC 4646 language code format.\r
+\r
+\r
+  @retval  EFI_SUCCESS            The Unicode string that matches the language specified by Language\r
+                                  was found in the table of Unicode strings UnicodeStringTable, and\r
+                                  it was returned in UnicodeString.\r
+  @retval  EFI_INVALID_PARAMETER  Language is NULL.  \r
+  @retval  EFI_INVALID_PARAMETER  UnicodeString is NULL.  \r
+  @retval  EFI_UNSUPPORTED        SupportedLanguages is NULL.  \r
+  @retval  EFI_UNSUPPORTED        UnicodeStringTable is NULL.  \r
+  @retval  EFI_UNSUPPORTED        The language specified by Language is not a member of SupportedLanguages.  \r
+  @retval  EFI_UNSUPPORTED        The language specified by Language is not supported by UnicodeStringTable.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -713,7 +815,7 @@ LookupUnicodeString2 (
       SupportedLanguages += 3;\r
     } else {\r
       for (Index = 0; SupportedLanguages[Index] != 0 && SupportedLanguages[Index] != ';'; Index++);\r
-      if (AsciiStrnCmp(SupportedLanguages, Language, Index) == 0) {\r
+      if ((AsciiStrnCmp(SupportedLanguages, Language, Index) == 0) && (Language[Index] == 0)) {\r
         Found = TRUE;\r
         break;\r
       }\r
@@ -751,66 +853,45 @@ LookupUnicodeString2 (
 \r
 \r
 /**\r
-  \r
   This function adds a Unicode string to UnicodeStringTable.\r
-  If Language is a member of SupportedLanguages then\r
-  UnicodeString is added to UnicodeStringTable.  New buffers are\r
-  allocated for both Language and UnicodeString.  The contents\r
-  of Language and UnicodeString are copied into these new\r
-  buffers.  These buffers are automatically freed when\r
+\r
+  If Language is a member of SupportedLanguages then UnicodeString is added to \r
+  UnicodeStringTable.  New buffers are allocated for both Language and \r
+  UnicodeString.  The contents of Language and UnicodeString are copied into \r
+  these new buffers.  These buffers are automatically freed when \r
   FreeUnicodeStringTable() is called.\r
 \r
-  @param  Language                A pointer to the ISO 639-2\r
-                                  language code for the Unicode\r
+  @param  Language                A pointer to the ISO 639-2 language code for the Unicode \r
                                   string to add.\r
-  \r
-  @param  SupportedLanguages      A pointer to the set of ISO\r
-                                  639-2 language codes that the\r
-                                  Unicode string table supports.\r
-                                  Language must be a member of\r
-                                  this set.\r
-  \r
-  @param  UnicodeStringTable      A pointer to the table of\r
-                                  Unicode strings.\r
-  \r
-  @param  UnicodeString           A pointer to the Unicode\r
-                                  string to add.\r
-\r
-  @retval EFI_SUCCESS             The Unicode string that\r
-                                  matches the language specified\r
-                                  by Language was found in the\r
-                                  table of Unicode strings\r
-                                  UnicodeStringTable, and it was\r
+  @param  SupportedLanguages      A pointer to the set of ISO 639-2 language codes\r
+                                  that the Unicode string table supports.\r
+                                  Language must be a member of this set.\r
+  @param  UnicodeStringTable      A pointer to the table of Unicode strings.\r
+  @param  UnicodeString           A pointer to the Unicode string to add.\r
+\r
+  @retval EFI_SUCCESS             The Unicode string that matches the language \r
+                                  specified by Language was found in the table of \r
+                                  Unicode strings UnicodeStringTable, and it was \r
                                   returned in UnicodeString.\r
-  \r
   @retval EFI_INVALID_PARAMETER   Language is NULL.\r
-  \r
   @retval EFI_INVALID_PARAMETER   UnicodeString is NULL.\r
-  \r
   @retval EFI_INVALID_PARAMETER   UnicodeString is an empty string.\r
-  \r
   @retval EFI_UNSUPPORTED         SupportedLanguages is NULL.\r
-  \r
-  @retval EFI_ALREADY_STARTED     A Unicode string with language\r
-                                  Language is already present in\r
-                                  UnicodeStringTable.\r
-  \r
-  @retval EFI_OUT_OF_RESOURCES    There is not enough memory to\r
-                                  add another Unicode string to\r
-                                  UnicodeStringTable.\r
-  \r
-  @retval EFI_UNSUPPORTED         The language specified by\r
-                                  Language is not a member of\r
-                                  SupportedLanguages.\r
+  @retval EFI_ALREADY_STARTED     A Unicode string with language Language is \r
+                                  already present in UnicodeStringTable.\r
+  @retval EFI_OUT_OF_RESOURCES    There is not enough memory to add another \r
+                                  Unicode string to UnicodeStringTable.\r
+  @retval EFI_UNSUPPORTED         The language specified by Language is not a \r
+                                  member of SupportedLanguages.\r
 \r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 AddUnicodeString (\r
-  IN CONST CHAR8               *Language,\r
-  IN CONST CHAR8               *SupportedLanguages,\r
-  IN EFI_UNICODE_STRING_TABLE  **UnicodeStringTable,\r
-  IN CONST CHAR16              *UnicodeString\r
+  IN     CONST CHAR8               *Language,\r
+  IN     CONST CHAR8               *SupportedLanguages,\r
+  IN OUT EFI_UNICODE_STRING_TABLE  **UnicodeStringTable,\r
+  IN     CONST CHAR16              *UnicodeString\r
   )\r
 {\r
   UINTN                     NumberOfEntries;\r
@@ -888,7 +969,7 @@ AddUnicodeString (
       //\r
       NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (3, Language);\r
       if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) {\r
-        gBS->FreePool (NewUnicodeStringTable);\r
+        FreePool (NewUnicodeStringTable);\r
         return EFI_OUT_OF_RESOURCES;\r
       }\r
 \r
@@ -906,8 +987,8 @@ AddUnicodeString (
                                                               UnicodeString\r
                                                               );\r
       if (NewUnicodeStringTable[NumberOfEntries].UnicodeString == NULL) {\r
-        gBS->FreePool (NewUnicodeStringTable[NumberOfEntries].Language);\r
-        gBS->FreePool (NewUnicodeStringTable);\r
+        FreePool (NewUnicodeStringTable[NumberOfEntries].Language);\r
+        FreePool (NewUnicodeStringTable);\r
         return EFI_OUT_OF_RESOURCES;\r
       }\r
 \r
@@ -921,7 +1002,7 @@ AddUnicodeString (
       // Free the old Unicode String Table\r
       //\r
       if (*UnicodeStringTable != NULL) {\r
-        gBS->FreePool (*UnicodeStringTable);\r
+        FreePool (*UnicodeStringTable);\r
       }\r
 \r
       //\r
@@ -940,73 +1021,55 @@ AddUnicodeString (
 \r
 \r
 /**\r
-  \r
-  This function adds a Unicode string to UnicodeStringTable.\r
-  If Language is a member of SupportedLanguages then\r
-  UnicodeString is added to UnicodeStringTable.  New buffers are\r
-  allocated for both Language and UnicodeString.  The contents\r
-  of Language and UnicodeString are copied into these new\r
-  buffers.  These buffers are automatically freed when\r
-  FreeUnicodeStringTable() is called.\r
-\r
-  @param  Language                A pointer to the ISO 639-2 or\r
-                                  RFC 3066 language code for the\r
-                                  Unicode string to add.\r
-  \r
-  @param  SupportedLanguages      A pointer to the set of ISO\r
-                                  639-2 or RFC 3.66 language\r
-                                  codes that the Unicode string\r
-                                  table supports. Language must\r
-                                  be a member of this set.\r
-  \r
-  @param  UnicodeStringTable      A pointer to the table of\r
-                                  Unicode strings.\r
-  \r
-  @param  UnicodeString           A pointer to the Unicode\r
-                                  string to add.\r
-  \r
-  @param  Iso639Language          Specify the language code\r
-                                  format supported. If true,\r
-                                  then the format follow ISO\r
-                                  639-2. If false, then it\r
-                                  follows RFC3066.\r
-\r
-  @retval EFI_SUCCESS             The Unicode string that\r
-                                  matches the language specified\r
-                                  by Language was found in the\r
-                                  table of Unicode strings\r
-                                  UnicodeStringTable, and it was\r
-                                  returned in UnicodeString.\r
-  \r
-  @retval EFI_INVALID_PARAMETER   Language is NULL.\r
-  \r
-  @retval EFI_INVALID_PARAMETER   UnicodeString is NULL.\r
-  \r
-  @retval EFI_INVALID_PARAMETER   UnicodeString is an empty string.\r
-  \r
-  @retval EFI_UNSUPPORTED         SupportedLanguages is NULL.\r
-  \r
-  @retval EFI_ALREADY_STARTED     A Unicode string with language\r
-                                  Language is already present in\r
-                                  UnicodeStringTable.\r
-  \r
-  @retval EFI_OUT_OF_RESOURCES    There is not enough memory to\r
-                                  add another Unicode string to\r
-                                  UnicodeStringTable.\r
-  \r
-  @retval EFI_UNSUPPORTED         The language specified by\r
-                                  Language is not a member of\r
-                                  SupportedLanguages.\r
+  This function adds the Null-terminated Unicode string specified by UnicodeString\r
+  to UnicodeStringTable.\r
+\r
+  If Language is a member of SupportedLanguages then UnicodeString is added to\r
+  UnicodeStringTable.  New buffers are allocated for both Language and UnicodeString.\r
+  The contents of Language and UnicodeString are copied into these new buffers.\r
+  These buffers are automatically freed when EfiLibFreeUnicodeStringTable() is called.\r
+\r
+  @param  Language            A pointer to an ASCII string containing the ISO 639-2 or\r
+                              the RFC 4646 language code for the Unicode string to add.\r
+                              If Iso639Language is TRUE, then this ASCII string is not\r
+                              assumed to be Null-terminated, and only the first three\r
+                              chacters are used. If Iso639Language is FALSE, then this\r
+                              ASCII string must be Null-terminated.\r
+  @param  SupportedLanguages  A pointer to a Null-terminated ASCII string that contains\r
+                              a set of ISO 639-2 or RFC 4646 language codes that the Unicode\r
+                              string table supports.  Language must be a member of this set.\r
+                              If Iso639Language is TRUE, then this string contains one or more\r
+                              ISO 639-2 language codes with no separator characters.\r
+                              If Iso639Language is FALSE, then is string contains one or more\r
+                              RFC 4646 language codes separated by ';'.\r
+  @param  UnicodeStringTable  A pointer to the table of Unicode strings. Type EFI_UNICODE_STRING_TABLE\r
+                              is defined in "Related Definitions".\r
+  @param  UnicodeString       A pointer to the Unicode string to add.  \r
+  @param  Iso639Language      Specifies the supported language code format. If it is TRUE,\r
+                              then Language and SupportedLanguages follow ISO 639-2 language code format.\r
+                              Otherwise, they follow RFC 4646 language code format.\r
+\r
+  @retval EFI_SUCCESS            The Unicode string that matches the language specified by\r
+                                 Language was found in the table of Unicode strings UnicodeStringTable,\r
+                                 and it was returned in UnicodeString.  \r
+  @retval EFI_INVALID_PARAMETER  Language is NULL.  \r
+  @retval EFI_INVALID_PARAMETER  UnicodeString is NULL.  \r
+  @retval EFI_INVALID_PARAMETER  UnicodeString is an empty string.  \r
+  @retval EFI_UNSUPPORTED        SupportedLanguages is NULL.  \r
+  @retval EFI_ALREADY_STARTED    A Unicode string with language Language is already present in\r
+                                 UnicodeStringTable.  \r
+  @retval EFI_OUT_OF_RESOURCES   There is not enough memory to add another Unicode string UnicodeStringTable.  \r
+  @retval EFI_UNSUPPORTED        The language specified by Language is not a member of SupportedLanguages.\r
 \r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 AddUnicodeString2 (\r
-  IN CONST CHAR8               *Language,\r
-  IN CONST CHAR8               *SupportedLanguages,\r
-  IN EFI_UNICODE_STRING_TABLE  **UnicodeStringTable,\r
-  IN CONST CHAR16              *UnicodeString,\r
-  IN BOOLEAN                   Iso639Language\r
+  IN     CONST CHAR8               *Language,\r
+  IN     CONST CHAR8               *SupportedLanguages,\r
+  IN OUT EFI_UNICODE_STRING_TABLE  **UnicodeStringTable,\r
+  IN     CONST CHAR16              *UnicodeString,\r
+  IN     BOOLEAN                   Iso639Language\r
   )\r
 {\r
   UINTN                     NumberOfEntries;\r
@@ -1076,7 +1139,7 @@ AddUnicodeString2 (
     while (OldUnicodeStringTable->Language != NULL) {\r
       LanguageString = OldUnicodeStringTable->Language;\r
 \r
-      while (*LanguageString) {\r
+      while (*LanguageString != 0) {\r
         for (Index = 0; LanguageString[Index] != 0 && LanguageString[Index] != ';'; Index++);\r
 \r
         if (AsciiStrnCmp (Language, LanguageString, Index) == 0) { \r
@@ -1117,7 +1180,7 @@ AddUnicodeString2 (
   //\r
   NewUnicodeStringTable[NumberOfEntries].Language = AllocateCopyPool (AsciiStrSize(Language), Language);\r
   if (NewUnicodeStringTable[NumberOfEntries].Language == NULL) {\r
-    gBS->FreePool (NewUnicodeStringTable);\r
+    FreePool (NewUnicodeStringTable);\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
@@ -1131,8 +1194,8 @@ AddUnicodeString2 (
   //\r
   NewUnicodeStringTable[NumberOfEntries].UnicodeString = AllocateCopyPool (StrSize (UnicodeString), UnicodeString);\r
   if (NewUnicodeStringTable[NumberOfEntries].UnicodeString == NULL) {\r
-    gBS->FreePool (NewUnicodeStringTable[NumberOfEntries].Language);\r
-    gBS->FreePool (NewUnicodeStringTable);\r
+    FreePool (NewUnicodeStringTable[NumberOfEntries].Language);\r
+    FreePool (NewUnicodeStringTable);\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
@@ -1146,7 +1209,7 @@ AddUnicodeString2 (
   // Free the old Unicode String Table\r
   //\r
   if (*UnicodeStringTable != NULL) {\r
-    gBS->FreePool (*UnicodeStringTable);\r
+    FreePool (*UnicodeStringTable);\r
   }\r
 \r
   //\r
@@ -1159,6 +1222,7 @@ AddUnicodeString2 (
 \r
 /**\r
   This function frees the table of Unicode strings in UnicodeStringTable.\r
+\r
   If UnicodeStringTable is NULL, then EFI_SUCCESS is returned.\r
   Otherwise, each language code, and each Unicode string in the Unicode string \r
   table are freed, and EFI_SUCCESS is returned.\r
@@ -1191,21 +1255,467 @@ FreeUnicodeStringTable (
     //\r
     // Free the Language string from the Unicode String Table\r
     //\r
-    gBS->FreePool (UnicodeStringTable[Index].Language);\r
+    FreePool (UnicodeStringTable[Index].Language);\r
 \r
     //\r
     // Free the Unicode String from the Unicode String Table\r
     //\r
     if (UnicodeStringTable[Index].UnicodeString != NULL) {\r
-      gBS->FreePool (UnicodeStringTable[Index].UnicodeString);\r
+      FreePool (UnicodeStringTable[Index].UnicodeString);\r
     }\r
   }\r
 \r
   //\r
   // Free the Unicode String Table itself\r
   //\r
-  gBS->FreePool (UnicodeStringTable);\r
+  FreePool (UnicodeStringTable);\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
+#ifndef DISABLE_NEW_DEPRECATED_INTERFACES\r
+\r
+/**\r
+  [ATTENTION] This function will be deprecated for security reason.\r
+\r
+  Returns a pointer to an allocated buffer that contains the contents of a \r
+  variable retrieved through the UEFI Runtime Service GetVariable().  The \r
+  returned buffer is allocated using AllocatePool().  The caller is responsible\r
+  for freeing this buffer with FreePool().\r
+\r
+  If Name is NULL, then ASSERT().\r
+  If Guid is NULL, then ASSERT().\r
+\r
+  @param[in]  Name  The pointer to a Null-terminated Unicode string.\r
+  @param[in]  Guid  The pointer to an EFI_GUID structure\r
+\r
+  @retval NULL   The variable could not be retrieved.\r
+  @retval NULL   There are not enough resources available for the variable contents.\r
+  @retval Other  A pointer to allocated buffer containing the variable contents.\r
+\r
+**/\r
+VOID *\r
+EFIAPI\r
+GetVariable (\r
+  IN CONST CHAR16    *Name,\r
+  IN CONST EFI_GUID  *Guid\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       Size;\r
+  VOID        *Value;\r
+\r
+  ASSERT (Name != NULL);\r
+  ASSERT (Guid != NULL);\r
+\r
+  //\r
+  // Try to get the variable size.\r
+  //\r
+  Value = NULL;\r
+  Size = 0;\r
+  Status = gRT->GetVariable ((CHAR16 *) Name, (EFI_GUID *) Guid, NULL, &Size, Value);\r
+  if (Status != EFI_BUFFER_TOO_SMALL) {\r
+    return NULL;\r
+  }\r
+\r
+  //\r
+  // Allocate buffer to get the variable.\r
+  //\r
+  Value = AllocatePool (Size);\r
+  if (Value == NULL) {\r
+    return NULL;\r
+  }\r
+\r
+  //\r
+  // Get the variable data.\r
+  //\r
+  Status = gRT->GetVariable ((CHAR16 *) Name, (EFI_GUID *) Guid, NULL, &Size, Value);\r
+  if (EFI_ERROR (Status)) {\r
+    FreePool(Value);\r
+    return NULL;\r
+  }\r
+\r
+  return Value;\r
+}\r
+\r
+/**\r
+  [ATTENTION] This function will be deprecated for security reason.\r
+\r
+  Returns a pointer to an allocated buffer that contains the contents of a \r
+  variable retrieved through the UEFI Runtime Service GetVariable().  This \r
+  function always uses the EFI_GLOBAL_VARIABLE GUID to retrieve variables.\r
+  The returned buffer is allocated using AllocatePool().  The caller is \r
+  responsible for freeing this buffer with FreePool().\r
+\r
+  If Name is NULL, then ASSERT().\r
+\r
+  @param[in]  Name  The pointer to a Null-terminated Unicode string.\r
+\r
+  @retval NULL   The variable could not be retrieved.\r
+  @retval NULL   There are not enough resources available for the variable contents.\r
+  @retval Other  A pointer to allocated buffer containing the variable contents.\r
+\r
+**/\r
+VOID *\r
+EFIAPI\r
+GetEfiGlobalVariable (\r
+  IN CONST CHAR16  *Name\r
+  )\r
+{\r
+  return GetVariable (Name, &gEfiGlobalVariableGuid);\r
+}\r
+#endif\r
+\r
+/**\r
+  Returns the status whether get the variable success. The function retrieves \r
+  variable  through the UEFI Runtime Service GetVariable().  The \r
+  returned buffer is allocated using AllocatePool().  The caller is responsible\r
+  for freeing this buffer with FreePool().\r
+\r
+  If Name  is NULL, then ASSERT().\r
+  If Guid  is NULL, then ASSERT().\r
+  If Value is NULL, then ASSERT().\r
+\r
+  @param[in]  Name  The pointer to a Null-terminated Unicode string.\r
+  @param[in]  Guid  The pointer to an EFI_GUID structure\r
+  @param[out] Value The buffer point saved the variable info.\r
+  @param[out] Size  The buffer size of the variable.\r
+\r
+  @return EFI_OUT_OF_RESOURCES      Allocate buffer failed.\r
+  @return EFI_SUCCESS               Find the specified variable.\r
+  @return Others Errors             Return errors from call to gRT->GetVariable.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetVariable2 (\r
+  IN CONST CHAR16    *Name,\r
+  IN CONST EFI_GUID  *Guid,\r
+  OUT VOID           **Value,\r
+  OUT UINTN          *Size OPTIONAL\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       BufferSize;\r
+\r
+  ASSERT (Name != NULL && Guid != NULL && Value != NULL);\r
+\r
+  //\r
+  // Try to get the variable size.\r
+  //\r
+  BufferSize = 0;\r
+  *Value     = NULL;\r
+  if (Size != NULL) {\r
+    *Size  = 0;\r
+  }\r
+  \r
+  Status = gRT->GetVariable ((CHAR16 *) Name, (EFI_GUID *) Guid, NULL, &BufferSize, *Value);\r
+  if (Status != EFI_BUFFER_TOO_SMALL) {\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Allocate buffer to get the variable.\r
+  //\r
+  *Value = AllocatePool (BufferSize);\r
+  ASSERT (*Value != NULL);\r
+  if (*Value == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  //\r
+  // Get the variable data.\r
+  //\r
+  Status = gRT->GetVariable ((CHAR16 *) Name, (EFI_GUID *) Guid, NULL, &BufferSize, *Value);\r
+  if (EFI_ERROR (Status)) {\r
+    FreePool(*Value);\r
+    *Value = NULL;\r
+  }\r
+\r
+  if (Size != NULL) {\r
+    *Size = BufferSize;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Returns a pointer to an allocated buffer that contains the contents of a \r
+  variable retrieved through the UEFI Runtime Service GetVariable().  This \r
+  function always uses the EFI_GLOBAL_VARIABLE GUID to retrieve variables.\r
+  The returned buffer is allocated using AllocatePool().  The caller is \r
+  responsible for freeing this buffer with FreePool().\r
+\r
+  If Name is NULL, then ASSERT().\r
+  If Value is NULL, then ASSERT().\r
+\r
+  @param[in]  Name  The pointer to a Null-terminated Unicode string.\r
+  @param[out] Value The buffer point saved the variable info.\r
+  @param[out] Size  The buffer size of the variable.\r
+\r
+  @return EFI_OUT_OF_RESOURCES      Allocate buffer failed.\r
+  @return EFI_SUCCESS               Find the specified variable.\r
+  @return Others Errors             Return errors from call to gRT->GetVariable.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetEfiGlobalVariable2 (\r
+  IN CONST CHAR16    *Name,\r
+  OUT VOID           **Value,\r
+  OUT UINTN          *Size OPTIONAL\r
+  )\r
+{\r
+  return GetVariable2 (Name, &gEfiGlobalVariableGuid, Value, Size);\r
+}\r
+\r
+/**\r
+  Returns a pointer to an allocated buffer that contains the best matching language \r
+  from a set of supported languages.  \r
+  \r
+  This function supports both ISO 639-2 and RFC 4646 language codes, but language \r
+  code types may not be mixed in a single call to this function.  The language \r
+  code returned is allocated using AllocatePool().  The caller is responsible for \r
+  freeing the allocated buffer using FreePool().  This function supports a variable\r
+  argument list that allows the caller to pass in a prioritized list of language \r
+  codes to test against all the language codes in SupportedLanguages. \r
+\r
+  If SupportedLanguages is NULL, then ASSERT().\r
+\r
+  @param[in]  SupportedLanguages  A pointer to a Null-terminated ASCII string that\r
+                                  contains a set of language codes in the format \r
+                                  specified by Iso639Language.\r
+  @param[in]  Iso639Language      If TRUE, then all language codes are assumed to be\r
+                                  in ISO 639-2 format.  If FALSE, then all language\r
+                                  codes are assumed to be in RFC 4646 language format\r
+  @param[in]  ...                 A variable argument list that contains pointers to \r
+                                  Null-terminated ASCII strings that contain one or more\r
+                                  language codes in the format specified by Iso639Language.\r
+                                  The first language code from each of these language\r
+                                  code lists is used to determine if it is an exact or\r
+                                  close match to any of the language codes in \r
+                                  SupportedLanguages.  Close matches only apply to RFC 4646\r
+                                  language codes, and the matching algorithm from RFC 4647\r
+                                  is used to determine if a close match is present.  If \r
+                                  an exact or close match is found, then the matching\r
+                                  language code from SupportedLanguages is returned.  If\r
+                                  no matches are found, then the next variable argument\r
+                                  parameter is evaluated.  The variable argument list \r
+                                  is terminated by a NULL.\r
+\r
+  @retval NULL   The best matching language could not be found in SupportedLanguages.\r
+  @retval NULL   There are not enough resources available to return the best matching \r
+                 language.\r
+  @retval Other  A pointer to a Null-terminated ASCII string that is the best matching \r
+                 language in SupportedLanguages.\r
+\r
+**/\r
+CHAR8 *\r
+EFIAPI\r
+GetBestLanguage (\r
+  IN CONST CHAR8  *SupportedLanguages, \r
+  IN UINTN        Iso639Language,\r
+  ...\r
+  )\r
+{\r
+  VA_LIST      Args;\r
+  CHAR8        *Language;\r
+  UINTN        CompareLength;\r
+  UINTN        LanguageLength;\r
+  CONST CHAR8  *Supported;\r
+  CHAR8        *BestLanguage;\r
+\r
+  ASSERT (SupportedLanguages != NULL);\r
+\r
+  VA_START (Args, Iso639Language);\r
+  while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {\r
+    //\r
+    // Default to ISO 639-2 mode\r
+    //\r
+    CompareLength  = 3;\r
+    LanguageLength = MIN (3, AsciiStrLen (Language));\r
+\r
+    //\r
+    // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language\r
+    //\r
+    if (!Iso639Language) {\r
+      for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);\r
+    }\r
+\r
+    //\r
+    // Trim back the length of Language used until it is empty\r
+    //\r
+    while (LanguageLength > 0) {\r
+      //\r
+      // Loop through all language codes in SupportedLanguages\r
+      //\r
+      for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {\r
+        //\r
+        // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages\r
+        //\r
+        if (!Iso639Language) {\r
+          //\r
+          // Skip ';' characters in Supported\r
+          //\r
+          for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
+          //\r
+          // Determine the length of the next language code in Supported\r
+          //\r
+          for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);\r
+          //\r
+          // If Language is longer than the Supported, then skip to the next language\r
+          //\r
+          if (LanguageLength > CompareLength) {\r
+            continue;\r
+          }\r
+        }\r
+        //\r
+        // See if the first LanguageLength characters in Supported match Language\r
+        //\r
+        if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {\r
+          VA_END (Args);\r
+          //\r
+          // Allocate, copy, and return the best matching language code from SupportedLanguages\r
+          //\r
+          BestLanguage = AllocateZeroPool (CompareLength + 1);\r
+          if (BestLanguage == NULL) {\r
+            return NULL;\r
+          }\r
+          return CopyMem (BestLanguage, Supported, CompareLength);\r
+        }\r
+      }\r
+\r
+      if (Iso639Language) {\r
+        //\r
+        // If ISO 639 mode, then each language can only be tested once\r
+        //\r
+        LanguageLength = 0;\r
+      } else {\r
+        //\r
+        // If RFC 4646 mode, then trim Language from the right to the next '-' character \r
+        //\r
+        for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);\r
+      }\r
+    }\r
+  }\r
+  VA_END (Args);\r
+\r
+  //\r
+  // No matches were found \r
+  //\r
+  return NULL;\r
+}\r
+\r
+/**\r
+  Returns an array of protocol instance that matches the given protocol.\r
+\r
+  @param[in]  Protocol      Provides the protocol to search for.\r
+  @param[out] NoProtocols   The number of protocols returned in Buffer.\r
+  @param[out] Buffer        A pointer to the buffer to return the requested\r
+                            array of protocol instances that match Protocol.\r
+                            The returned buffer is allocated using\r
+                            EFI_BOOT_SERVICES.AllocatePool().  The caller is\r
+                            responsible for freeing this buffer with\r
+                            EFI_BOOT_SERVICES.FreePool().\r
+\r
+  @retval EFI_SUCCESS            The array of protocols was returned in Buffer,\r
+                                 and the number of protocols in Buffer was\r
+                                 returned in NoProtocols.\r
+  @retval EFI_NOT_FOUND          No protocols found.\r
+  @retval EFI_OUT_OF_RESOURCES   There is not enough pool memory to store the\r
+                                 matching results.\r
+  @retval EFI_INVALID_PARAMETER  Protocol is NULL.\r
+  @retval EFI_INVALID_PARAMETER  NoProtocols is NULL.\r
+  @retval EFI_INVALID_PARAMETER  Buffer is NULL.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+EfiLocateProtocolBuffer (\r
+  IN  EFI_GUID  *Protocol,\r
+  OUT UINTN     *NoProtocols,\r
+  OUT VOID      ***Buffer\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       NoHandles;\r
+  EFI_HANDLE  *HandleBuffer;\r
+  UINTN       Index;\r
+\r
+  //\r
+  // Check input parameters\r
+  //\r
+  if (Protocol == NULL || NoProtocols == NULL || Buffer == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // Initialze output parameters\r
+  //\r
+  *NoProtocols = 0;\r
+  *Buffer = NULL;\r
+\r
+  //\r
+  // Retrieve the array of handles that support Protocol\r
+  //\r
+  Status = gBS->LocateHandleBuffer (\r
+                  ByProtocol,\r
+                  Protocol,\r
+                  NULL,\r
+                  &NoHandles,\r
+                  &HandleBuffer\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Allocate array of protocol instances\r
+  //\r
+  Status = gBS->AllocatePool (\r
+                  EfiBootServicesData,\r
+                  NoHandles * sizeof (VOID *),\r
+                  (VOID **)Buffer\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    //\r
+    // Free the handle buffer\r
+    //\r
+    gBS->FreePool (HandleBuffer);\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  ZeroMem (*Buffer, NoHandles * sizeof (VOID *));\r
+\r
+  //\r
+  // Lookup Protocol on each handle in HandleBuffer to fill in the array of\r
+  // protocol instances.  Handle case where protocol instance was present when\r
+  // LocateHandleBuffer() was called, but is not present when HandleProtocol()\r
+  // is called.\r
+  //\r
+  for (Index = 0, *NoProtocols = 0; Index < NoHandles; Index++) {\r
+    Status = gBS->HandleProtocol (\r
+                    HandleBuffer[Index],\r
+                    Protocol,\r
+                    &((*Buffer)[*NoProtocols])\r
+                    );\r
+    if (!EFI_ERROR (Status)) {\r
+      (*NoProtocols)++;\r
+    }\r
+  }\r
+\r
+  //\r
+  // Free the handle buffer\r
+  //\r
+  gBS->FreePool (HandleBuffer);\r
+\r
+  //\r
+  // Make sure at least one protocol instance was found\r
+  //\r
+  if (*NoProtocols == 0) {\r
+    gBS->FreePool (*Buffer);\r
+    *Buffer = NULL;\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r