]> git.proxmox.com Git - mirror_edk2.git/blobdiff - IntelFrameworkPkg/Library/FrameworkUefiLib/UefiLib.c
Sync FrameworkUefiLib with the MdePkg UefiLib
[mirror_edk2.git] / IntelFrameworkPkg / Library / FrameworkUefiLib / UefiLib.c
index 7d28b700dc6acde9fc4b4ac666a7a063e0f0e64e..554af2281e1c84d578e39518c538a5d4f2032c37 100644 (file)
@@ -5,7 +5,7 @@
   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. All rights reserved.<BR>\r
+  Copyright (c) 2006 - 2008, 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
@@ -92,15 +92,17 @@ EfiGetSystemConfigurationTable (
   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
+  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
@@ -143,7 +145,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
@@ -454,9 +456,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
@@ -579,7 +581,7 @@ EfiTestChildHandle (
 \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
+                                  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
@@ -659,7 +661,7 @@ LookupUnicodeString (
                                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
-                               chacters are used. If Iso639Language is FALSE, then this ASCII\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
@@ -689,7 +691,6 @@ LookupUnicodeString (
 \r
 **/\r
 EFI_STATUS\r
-\r
 EFIAPI\r
 LookupUnicodeString2 (\r
   IN CONST CHAR8                     *Language,\r
@@ -1188,3 +1189,229 @@ FreeUnicodeStringTable (
 \r
   return EFI_SUCCESS;\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().  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  Pointer to a Null-terminated Unicode string.\r
+  @param[in]  Guid  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
+/**\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  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
+\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 BOOLEAN      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