]> git.proxmox.com Git - mirror_edk2.git/blobdiff - EmbeddedPkg/Ebl/Variable.c
Fix EBL GetCurrentIpAddress & GetCurrentMacAddress commands. Add variable services...
[mirror_edk2.git] / EmbeddedPkg / Ebl / Variable.c
diff --git a/EmbeddedPkg/Ebl/Variable.c b/EmbeddedPkg/Ebl/Variable.c
new file mode 100644 (file)
index 0000000..330a143
--- /dev/null
@@ -0,0 +1,192 @@
+/** @file
+*
+*  Copyright (c) 2011, ARM Limited. All rights reserved.
+*  
+*  This program and the accompanying materials                          
+*  are licensed and made available under the terms and conditions of the BSD License         
+*  which accompanies this distribution.  The full text of the license may be found at        
+*  http://opensource.org/licenses/bsd-license.php                                            
+*
+*  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
+*  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             
+*
+**/
+\r
+#include "Ebl.h"\r
+
+#include <Guid/GlobalVariable.h>
+\r
+EFI_STATUS\r
+EblGetCmd (\r
+  IN UINTN  Argc,\r
+  IN CHAR8  **Argv\r
+  )\r
+{\r
+  EFI_STATUS        Status = EFI_INVALID_PARAMETER;\r
+  UINTN       Size;
+  VOID*       Value;
+  CHAR8*      AsciiVariableName = NULL;
+  CHAR16*     VariableName;
+  UINT32      Index;
+
+  if (Argc == 1) {
+    AsciiPrint("Variable name is missing.\n");
+    return Status;
+  }
+
+  for (Index = 1; Index < Argc; Index++) {
+    if (Argv[Index][0] == '-') {
+      AsciiPrint("Warning: '%a' not recognized.\n",Argv[Index]);
+    } else {
+      AsciiVariableName = Argv[Index];
+    }
+  }
+
+  if (AsciiVariableName == NULL) {
+    AsciiPrint("Variable name is missing.\n");
+    return Status;
+  } else {
+    VariableName = AllocatePool((AsciiStrLen (AsciiVariableName) + 1) * sizeof (CHAR16));
+    AsciiStrToUnicodeStr (AsciiVariableName,VariableName);
+  }
+
+  // Try to get the variable size.
+  Value = NULL;
+  Size = 0;
+  Status = gRT->GetVariable (VariableName, &gEfiGlobalVariableGuid, NULL, &Size, Value);
+  if (Status == EFI_NOT_FOUND) {
+    AsciiPrint("Variable name '%a' not found.\n",VariableName);
+  } else if (Status == EFI_BUFFER_TOO_SMALL) {
+    // Get the environment variable value
+    Value = AllocatePool (Size);
+    if (Value == NULL) {
+      return EFI_OUT_OF_RESOURCES;
+    }
+
+    Status = gRT->GetVariable ((CHAR16 *)VariableName, &gEfiGlobalVariableGuid, NULL, &Size, Value);
+    if (EFI_ERROR (Status)) {
+      AsciiPrint("Error: '%r'\n",Status);
+    } else {
+      AsciiPrint("%a=%a\n",AsciiVariableName,Value);
+    }
+    FreePool(Value);
+  } else {
+    AsciiPrint("Error: '%r'\n",Status);
+  }\r
+\r
+  FreePool(VariableName);\r
+  return Status;\r
+}\r
+\r
+EFI_STATUS\r
+EblSetCmd (\r
+  IN UINTN  Argc,\r
+  IN CHAR8  **Argv\r
+  )\r
+{\r
+  EFI_STATUS    Status = EFI_INVALID_PARAMETER;\r
+  CHAR8*        AsciiVariableSetting = NULL;
+  CHAR8*        AsciiVariableName;
+  CHAR8*        AsciiValue;
+  UINT32        AsciiValueLength;
+  CHAR16*       VariableName;
+  UINT32        Index;\r
+  UINT32        EscapedQuotes = 0;\r
+  BOOLEAN       Volatile = FALSE;\r
+\r
+  if (Argc == 1) {
+    AsciiPrint("Variable name is missing.\n");
+    return Status;
+  }
+
+  for (Index = 1; Index < Argc; Index++) {
+    if (AsciiStrCmp(Argv[Index],"-v") == 0) {
+      Volatile = 0;
+    } else if (Argv[Index][0] == '-') {
+      AsciiPrint("Warning: '%a' not recognized.\n",Argv[Index]);
+    } else {
+      AsciiVariableSetting = Argv[Index];
+    }
+  }
+
+  if (AsciiVariableSetting == NULL) {
+    AsciiPrint("Variable name is missing.\n");
+    return Status;
+  }
+  
+  // Check if it is a valid variable setting
+  AsciiValue = AsciiStrStr (AsciiVariableSetting,"=");\r
+  if (AsciiValue == NULL) {
+    AsciiPrint("Variable setting is incorrect. It should be VariableName=Value\n");
+    return Status;
+  }\r
+\r
+  AsciiValue[0] = '\0';\r
+  AsciiVariableName = AsciiVariableSetting;\r
+  AsciiValue++;\r
+\r
+  // Clean AsciiValue from quote\r
+  if (AsciiValue[0] == '"') {\r
+    AsciiValue++;\r
+  }\r
+  AsciiValueLength = AsciiStrLen (AsciiValue);\r
+  if ((AsciiValue[AsciiValueLength-2] != '\\') && (AsciiValue[AsciiValueLength-1] == '"')) {\r
+    AsciiValue[AsciiValueLength-1] = '\0';\r
+  }\r
+\r
+  // Clean AsciiValue from escaped quotes\r
+  for (Index = 0; Index < AsciiValueLength; Index++) {\r
+    if ((Index > 0) && (AsciiValue[Index-1] == '\\') && (AsciiValue[Index] == '"')) {\r
+      EscapedQuotes++;\r
+    }\r
+    AsciiValue[Index-EscapedQuotes] = AsciiValue[Index];\r
+  }\r
+  // Fill the end of the value with '\0'\r
+  for (Index = 0; Index < EscapedQuotes; Index++) {\r
+    AsciiValue[AsciiValueLength-1-Index] = '\0';\r
+  }\r
+\r
+  // Convert VariableName into Unicode\r
+  VariableName = AllocatePool((AsciiStrLen (AsciiVariableName) + 1) * sizeof (CHAR16));
+  AsciiStrToUnicodeStr (AsciiVariableName,VariableName);\r
+\r
+  Status = gRT->SetVariable (
+                      VariableName,
+                      &gEfiGlobalVariableGuid,
+                      ( !Volatile ? EFI_VARIABLE_NON_VOLATILE : 0) |
+                      EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
+                      AsciiStrLen (AsciiValue)+1,
+                      AsciiValue
+                      );\r
+\r
+  AsciiPrint("'%a'='%a'\n",AsciiVariableName,AsciiValue);\r
+\r
+  return Status;\r
+}\r
+\r
+GLOBAL_REMOVE_IF_UNREFERENCED const EBL_COMMAND_TABLE mCmdVariableTemplate[] =\r
+{\r
+  {\r
+    "get",\r
+    " ; get UEFI variable\n\r   [v]; verbose",\r
+    NULL,\r
+    EblGetCmd\r
+  },\r
+  {\r
+    "set",\r
+    " ; set UEFI variable\n\r   [v]; create volatile variable",\r
+    NULL,\r
+    EblSetCmd\r
+  }\r
+};\r
+\r
+/**\r
+  Initialize the commands in this in this file\r
+**/\r
+VOID\r
+EblInitializeVariableCmds (\r
+  VOID\r
+  )\r
+{\r
+  EblAddCommands (mCmdVariableTemplate, sizeof (mCmdVariableTemplate)/sizeof (EBL_COMMAND_TABLE));\r
+}\r