]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/UefiLib/UefiLib.c
Code and comments have been checked with spec.
[mirror_edk2.git] / MdePkg / Library / UefiLib / UefiLib.c
index f4eb3eb43baedec3890827011713ef0c04e76df4..53a46106e158bd5d6364438e24433b1bfbb78706 100644 (file)
@@ -1,5 +1,9 @@
 /** @file\r
-  Mde UEFI library functions.\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 - 2007, Intel Corporation<BR>\r
   All rights reserved. This program and the accompanying materials\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
-  Module Name:  UefiLib.c\r
-\r
 **/\r
 \r
-//\r
-// Include common header file for this module.\r
-//\r
-#include "CommonHeader.h"\r
+\r
+#include "UefiLibInternal.h"\r
 \r
 /**\r
   Compare whether two names of languages are identical.\r
@@ -29,7 +29,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
@@ -46,10 +45,15 @@ 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
@@ -84,16 +88,26 @@ 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.\r
+  In addition, every time a protocol of type ProtocolGuid instance is installed or reinstalled,\r
+  the notification function is also executed.  This function returns the notification event\r
+  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
   @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
@@ -111,6 +125,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
@@ -146,14 +164,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  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
@@ -174,6 +197,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
@@ -207,12 +234,15 @@ 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
+  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 GUID name of the event.\r
 \r
@@ -229,6 +259,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
@@ -245,7 +277,7 @@ EfiNamedEventSignal (
                   );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
-  return EFI_SUCCESS;\r
+  return Status;\r
 }\r
 \r
 /**\r
@@ -259,7 +291,7 @@ EfiNamedEventSignal (
 \r
   @param  VOID\r
 \r
-  @retvale EFI_TPL              The current TPL.\r
+  @retval EFI_TPL              The current TPL.\r
 \r
 **/\r
 EFI_TPL\r
@@ -278,10 +310,14 @@ 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
@@ -306,11 +342,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
@@ -327,9 +368,13 @@ 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
+  Acquires ownership of a lock.  If the lock is already owned , then an error is returned.\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
+  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
@@ -362,9 +407,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
@@ -459,7 +509,7 @@ EfiTestManagedDevice (
 \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
+  @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
@@ -514,50 +564,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
+  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 Unicoide 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
@@ -627,28 +659,23 @@ LookupUnicodeString (
   @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
+                                  return.  \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
+                                  be a member of this set.  \r
   @param  UnicodeStringTable      A pointer to the table of\r
-                                  Unicode strings.\r
-  \r
+                                  Unicode strings.  \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
@@ -656,24 +683,20 @@ LookupUnicodeString (
                                   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_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
-  \r
   @retval  EFI_UNSUPPORTED        The language specified by\r
                                   Language is not a member\r
-                                  ofSupportedLanguages.\r
-  \r
+                                  ofSupportedLanguages.  \r
   @retval EFI_UNSUPPORTED         The language specified by\r
                                   Language is not supported by\r
                                   UnicodeStringTable.\r
 \r
 **/\r
 EFI_STATUS\r
+\r
 EFIAPI\r
 LookupUnicodeString2 (\r
   IN CONST CHAR8                     *Language,\r
@@ -715,7 +738,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
@@ -753,57 +776,36 @@ 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
-                                  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
+  @param  Language                A pointer to the ISO 639-2 language code for 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
@@ -942,8 +944,8 @@ AddUnicodeString (
 \r
 \r
 /**\r
-  \r
   This function adds a Unicode string to UnicodeStringTable.\r
+\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
@@ -953,26 +955,21 @@ AddUnicodeString (
 \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
+                                  Unicode string to add.  \r
   @param  SupportedLanguages      A pointer to the set of ISO\r
-                                  639-2 or RFC 3.66 language\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
+                                  be a member of this set.  \r
   @param  UnicodeStringTable      A pointer to the table of\r
-                                  Unicode strings.\r
-  \r
+                                  Unicode strings.  \r
   @param  UnicodeString           A pointer to the Unicode\r
-                                  string to add.\r
-  \r
+                                  string to add.  \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
@@ -980,22 +977,16 @@ AddUnicodeString (
                                   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_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\r
                                   Language is already present in\r
-                                  UnicodeStringTable.\r
-  \r
+                                  UnicodeStringTable.  \r
   @retval EFI_OUT_OF_RESOURCES    There is not enough memory to\r
                                   add another Unicode string to\r
-                                  UnicodeStringTable.\r
-  \r
+                                  UnicodeStringTable.  \r
   @retval EFI_UNSUPPORTED         The language specified by\r
                                   Language is not a member of\r
                                   SupportedLanguages.\r
@@ -1078,7 +1069,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
@@ -1161,6 +1152,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
@@ -1211,3 +1203,49 @@ FreeUnicodeStringTable (
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Determine what is the current language setting. The space reserved for Lang\r
+  must be at least RFC_3066_ENTRY_SIZE bytes;\r
+\r
+  If Lang is NULL, then ASSERT.\r
+\r
+  @param  Lang   Pointer of system language. Lang will always be filled with a valid RFC 3066\r
+                 language string. If "PlatformLang" is not set in the system, the default\r
+                 language specifed by PcdUefiVariableDefaultPlatformLang is returned.\r
+\r
+  @return  EFI_SUCCESS     If the EFI Variable with "PlatformLang" is set and return in Lang.\r
+  @return  EFI_NOT_FOUND If the EFI Variable with "PlatformLang" is not set, but a valid default language is return in Lang.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetCurrentLanguage (\r
+  OUT     CHAR8               *Lang\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       Size;\r
+\r
+  ASSERT (Lang != NULL);\r
+\r
+  //\r
+  // Get current language setting\r
+  //\r
+  Size = RFC_3066_ENTRY_SIZE;\r
+  Status = gRT->GetVariable (\r
+                  L"PlatformLang",\r
+                  &gEfiGlobalVariableGuid,\r
+                  NULL,\r
+                  &Size,\r
+                  Lang\r
+                  );\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    AsciiStrCpy (Lang, (CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultPlatformLang));\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+\r
+\r