]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/UefiLib/UefiLib.c
MdePkg UefiLib: Use safe string functions
[mirror_edk2.git] / MdePkg / Library / UefiLib / UefiLib.c
index 6fa3b34e83cdc3cdd249d022963f7df12cc81d21..112766ad86345ee9a5c3261e16c15081bf7e133e 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 - 2010, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2006 - 2012, 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
 \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
@@ -886,7 +908,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
@@ -904,8 +926,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
@@ -919,7 +941,7 @@ AddUnicodeString (
       // Free the old Unicode String Table\r
       //\r
       if (*UnicodeStringTable != NULL) {\r
-        gBS->FreePool (*UnicodeStringTable);\r
+        FreePool (*UnicodeStringTable);\r
       }\r
 \r
       //\r
@@ -1097,7 +1119,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
@@ -1111,8 +1133,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
@@ -1126,7 +1148,7 @@ AddUnicodeString2 (
   // Free the old Unicode String Table\r
   //\r
   if (*UnicodeStringTable != NULL) {\r
-    gBS->FreePool (*UnicodeStringTable);\r
+    FreePool (*UnicodeStringTable);\r
   }\r
 \r
   //\r
@@ -1172,25 +1194,29 @@ 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
@@ -1251,8 +1277,9 @@ GetVariable (
   return Value;\r
 }\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
@@ -1276,7 +1303,110 @@ GetEfiGlobalVariable (
 {\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