3 * Copyright (c) 2011, ARM Limited. All rights reserved.
4 * (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
6 * This program and the accompanying materials
7 * are licensed and made available under the terms and conditions of the BSD License
8 * which accompanies this distribution. The full text of the license may be found at
9 * http://opensource.org/licenses/bsd-license.php
11 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Guid/GlobalVariable.h>
27 EFI_STATUS Status
= EFI_INVALID_PARAMETER
;
30 CHAR8
* AsciiVariableName
= NULL
;
35 AsciiPrint("Variable name is missing.\n");
39 for (Index
= 1; Index
< Argc
; Index
++) {
40 if (Argv
[Index
][0] == '-') {
41 AsciiPrint("Warning: '%a' not recognized.\n",Argv
[Index
]);
43 AsciiVariableName
= Argv
[Index
];
47 if (AsciiVariableName
== NULL
) {
48 AsciiPrint("Variable name is missing.\n");
51 VariableName
= AllocatePool((AsciiStrLen (AsciiVariableName
) + 1) * sizeof (CHAR16
));
52 AsciiStrToUnicodeStr (AsciiVariableName
,VariableName
);
55 // Try to get the variable size.
58 Status
= gRT
->GetVariable (VariableName
, &gEfiGlobalVariableGuid
, NULL
, &Size
, Value
);
59 if (Status
== EFI_NOT_FOUND
) {
60 AsciiPrint("Variable name '%s' not found.\n",VariableName
);
61 } else if (Status
== EFI_BUFFER_TOO_SMALL
) {
62 // Get the environment variable value
63 Value
= AllocatePool (Size
);
65 return EFI_OUT_OF_RESOURCES
;
68 Status
= gRT
->GetVariable ((CHAR16
*)VariableName
, &gEfiGlobalVariableGuid
, NULL
, &Size
, Value
);
69 if (EFI_ERROR (Status
)) {
70 AsciiPrint("Error: '%r'\n",Status
);
72 AsciiPrint("%a=%a\n",AsciiVariableName
,Value
);
76 AsciiPrint("Error: '%r'\n",Status
);
79 FreePool(VariableName
);
90 EFI_STATUS Status
= EFI_INVALID_PARAMETER
;
91 CHAR8
* AsciiVariableSetting
= NULL
;
92 CHAR8
* AsciiVariableName
;
94 UINT32 AsciiValueLength
;
97 UINT32 EscapedQuotes
= 0;
98 BOOLEAN Volatile
= FALSE
;
101 AsciiPrint("Variable name is missing.\n");
105 for (Index
= 1; Index
< Argc
; Index
++) {
106 if (AsciiStrCmp(Argv
[Index
],"-v") == 0) {
108 } else if (Argv
[Index
][0] == '-') {
109 AsciiPrint("Warning: '%a' not recognized.\n",Argv
[Index
]);
111 AsciiVariableSetting
= Argv
[Index
];
115 if (AsciiVariableSetting
== NULL
) {
116 AsciiPrint("Variable name is missing.\n");
120 // Check if it is a valid variable setting
121 AsciiValue
= AsciiStrStr (AsciiVariableSetting
,"=");
122 if (AsciiValue
== NULL
) {
124 // There is no value. It means this variable will be deleted
127 // Convert VariableName into Unicode
128 VariableName
= AllocatePool((AsciiStrLen (AsciiVariableSetting
) + 1) * sizeof (CHAR16
));
129 AsciiStrToUnicodeStr (AsciiVariableSetting
,VariableName
);
131 Status
= gRT
->SetVariable (
133 &gEfiGlobalVariableGuid
,
134 ( !Volatile
? EFI_VARIABLE_NON_VOLATILE
: 0) |
135 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
139 if (!EFI_ERROR(Status
)) {
140 AsciiPrint("Variable '%s' deleted\n",VariableName
);
142 AsciiPrint("Variable setting is incorrect. It should be VariableName=Value\n");
147 AsciiValue
[0] = '\0';
148 AsciiVariableName
= AsciiVariableSetting
;
151 // Clean AsciiValue from quote
152 if (AsciiValue
[0] == '"') {
155 AsciiValueLength
= AsciiStrLen (AsciiValue
);
156 if ((AsciiValue
[AsciiValueLength
-2] != '\\') && (AsciiValue
[AsciiValueLength
-1] == '"')) {
157 AsciiValue
[AsciiValueLength
-1] = '\0';
160 // Clean AsciiValue from escaped quotes
161 for (Index
= 0; Index
< AsciiValueLength
; Index
++) {
162 if ((Index
> 0) && (AsciiValue
[Index
-1] == '\\') && (AsciiValue
[Index
] == '"')) {
165 AsciiValue
[Index
-EscapedQuotes
] = AsciiValue
[Index
];
167 // Fill the end of the value with '\0'
168 for (Index
= 0; Index
< EscapedQuotes
; Index
++) {
169 AsciiValue
[AsciiValueLength
-1-Index
] = '\0';
172 // Convert VariableName into Unicode
173 VariableName
= AllocatePool((AsciiStrLen (AsciiVariableName
) + 1) * sizeof (CHAR16
));
174 AsciiStrToUnicodeStr (AsciiVariableName
,VariableName
);
176 Status
= gRT
->SetVariable (
178 &gEfiGlobalVariableGuid
,
179 ( !Volatile
? EFI_VARIABLE_NON_VOLATILE
: 0) |
180 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
181 AsciiStrLen (AsciiValue
)+1,
184 if (!EFI_ERROR(Status
)) {
185 AsciiPrint("'%a'='%a'\n",AsciiVariableName
,AsciiValue
);
191 GLOBAL_REMOVE_IF_UNREFERENCED
const EBL_COMMAND_TABLE mCmdVariableTemplate
[] =
195 " ; get UEFI variable\n\r [v]; verbose",
201 " ; set UEFI variable\n\r [v]; create volatile variable",
208 Initialize the commands in this in this file
211 EblInitializeVariableCmds (
215 EblAddCommands (mCmdVariableTemplate
, sizeof (mCmdVariableTemplate
)/sizeof (EBL_COMMAND_TABLE
));