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
;
32 UINTN VariableNameLen
;
36 AsciiPrint("Variable name is missing.\n");
40 for (Index
= 1; Index
< Argc
; Index
++) {
41 if (Argv
[Index
][0] == '-') {
42 AsciiPrint("Warning: '%a' not recognized.\n",Argv
[Index
]);
44 AsciiVariableName
= Argv
[Index
];
48 if (AsciiVariableName
== NULL
) {
49 AsciiPrint("Variable name is missing.\n");
52 VariableNameLen
= AsciiStrLen (AsciiVariableName
) + 1;
53 VariableName
= AllocatePool (VariableNameLen
* sizeof (CHAR16
));
54 AsciiStrToUnicodeStrS (AsciiVariableName
, VariableName
, VariableNameLen
);
57 // Try to get the variable size.
60 Status
= gRT
->GetVariable (VariableName
, &gEfiGlobalVariableGuid
, NULL
, &Size
, Value
);
61 if (Status
== EFI_NOT_FOUND
) {
62 AsciiPrint("Variable name '%s' not found.\n",VariableName
);
63 } else if (Status
== EFI_BUFFER_TOO_SMALL
) {
64 // Get the environment variable value
65 Value
= AllocatePool (Size
);
67 return EFI_OUT_OF_RESOURCES
;
70 Status
= gRT
->GetVariable ((CHAR16
*)VariableName
, &gEfiGlobalVariableGuid
, NULL
, &Size
, Value
);
71 if (EFI_ERROR (Status
)) {
72 AsciiPrint("Error: '%r'\n",Status
);
74 AsciiPrint("%a=%a\n",AsciiVariableName
,Value
);
78 AsciiPrint("Error: '%r'\n",Status
);
81 FreePool(VariableName
);
92 EFI_STATUS Status
= EFI_INVALID_PARAMETER
;
93 CHAR8
* AsciiVariableSetting
= NULL
;
94 CHAR8
* AsciiVariableName
;
96 UINT32 AsciiValueLength
;
98 UINTN VariableNameLen
;
100 UINT32 EscapedQuotes
= 0;
101 BOOLEAN Volatile
= FALSE
;
104 AsciiPrint("Variable name is missing.\n");
108 for (Index
= 1; Index
< Argc
; Index
++) {
109 if (AsciiStrCmp(Argv
[Index
],"-v") == 0) {
111 } else if (Argv
[Index
][0] == '-') {
112 AsciiPrint("Warning: '%a' not recognized.\n",Argv
[Index
]);
114 AsciiVariableSetting
= Argv
[Index
];
118 if (AsciiVariableSetting
== NULL
) {
119 AsciiPrint("Variable name is missing.\n");
123 // Check if it is a valid variable setting
124 AsciiValue
= AsciiStrStr (AsciiVariableSetting
,"=");
125 if (AsciiValue
== NULL
) {
127 // There is no value. It means this variable will be deleted
130 // Convert VariableName into Unicode
131 VariableNameLen
= AsciiStrLen (AsciiVariableSetting
) + 1;
132 VariableName
= AllocatePool (VariableNameLen
* sizeof (CHAR16
));
133 AsciiStrToUnicodeStrS (AsciiVariableSetting
, VariableName
, VariableNameLen
);
135 Status
= gRT
->SetVariable (
137 &gEfiGlobalVariableGuid
,
138 ( !Volatile
? EFI_VARIABLE_NON_VOLATILE
: 0) |
139 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
143 if (!EFI_ERROR(Status
)) {
144 AsciiPrint("Variable '%s' deleted\n",VariableName
);
146 AsciiPrint("Variable setting is incorrect. It should be VariableName=Value\n");
151 AsciiValue
[0] = '\0';
152 AsciiVariableName
= AsciiVariableSetting
;
155 // Clean AsciiValue from quote
156 if (AsciiValue
[0] == '"') {
159 AsciiValueLength
= AsciiStrLen (AsciiValue
);
160 if ((AsciiValue
[AsciiValueLength
-2] != '\\') && (AsciiValue
[AsciiValueLength
-1] == '"')) {
161 AsciiValue
[AsciiValueLength
-1] = '\0';
164 // Clean AsciiValue from escaped quotes
165 for (Index
= 0; Index
< AsciiValueLength
; Index
++) {
166 if ((Index
> 0) && (AsciiValue
[Index
-1] == '\\') && (AsciiValue
[Index
] == '"')) {
169 AsciiValue
[Index
-EscapedQuotes
] = AsciiValue
[Index
];
171 // Fill the end of the value with '\0'
172 for (Index
= 0; Index
< EscapedQuotes
; Index
++) {
173 AsciiValue
[AsciiValueLength
-1-Index
] = '\0';
176 // Convert VariableName into Unicode
177 VariableNameLen
= AsciiStrLen (AsciiVariableName
) + 1;
178 VariableName
= AllocatePool (VariableNameLen
* sizeof (CHAR16
));
179 AsciiStrToUnicodeStrS (AsciiVariableName
, VariableName
, VariableNameLen
);
181 Status
= gRT
->SetVariable (
183 &gEfiGlobalVariableGuid
,
184 ( !Volatile
? EFI_VARIABLE_NON_VOLATILE
: 0) |
185 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
186 AsciiStrLen (AsciiValue
)+1,
189 if (!EFI_ERROR(Status
)) {
190 AsciiPrint("'%a'='%a'\n",AsciiVariableName
,AsciiValue
);
196 GLOBAL_REMOVE_IF_UNREFERENCED
const EBL_COMMAND_TABLE mCmdVariableTemplate
[] =
200 " ; get UEFI variable\n\r [v]; verbose",
206 " ; set UEFI variable\n\r [v]; create volatile variable",
213 Initialize the commands in this in this file
216 EblInitializeVariableCmds (
220 EblAddCommands (mCmdVariableTemplate
, sizeof (mCmdVariableTemplate
)/sizeof (EBL_COMMAND_TABLE
));