3 * Copyright (c) 2011, ARM Limited. All rights reserved.
5 * This program and the accompanying materials
6 * are licensed and made available under the terms and conditions of the BSD License
7 * which accompanies this distribution. The full text of the license may be found at
8 * http://opensource.org/licenses/bsd-license.php
10 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Guid/GlobalVariable.h>
25 EFI_STATUS Status
= EFI_INVALID_PARAMETER
;
28 CHAR8
* AsciiVariableName
= NULL
;
33 AsciiPrint("Variable name is missing.\n");
37 for (Index
= 1; Index
< Argc
; Index
++) {
38 if (Argv
[Index
][0] == '-') {
39 AsciiPrint("Warning: '%a' not recognized.\n",Argv
[Index
]);
41 AsciiVariableName
= Argv
[Index
];
45 if (AsciiVariableName
== NULL
) {
46 AsciiPrint("Variable name is missing.\n");
49 VariableName
= AllocatePool((AsciiStrLen (AsciiVariableName
) + 1) * sizeof (CHAR16
));
50 AsciiStrToUnicodeStr (AsciiVariableName
,VariableName
);
53 // Try to get the variable size.
56 Status
= gRT
->GetVariable (VariableName
, &gEfiGlobalVariableGuid
, NULL
, &Size
, Value
);
57 if (Status
== EFI_NOT_FOUND
) {
58 AsciiPrint("Variable name '%s' not found.\n",VariableName
);
59 } else if (Status
== EFI_BUFFER_TOO_SMALL
) {
60 // Get the environment variable value
61 Value
= AllocatePool (Size
);
63 return EFI_OUT_OF_RESOURCES
;
66 Status
= gRT
->GetVariable ((CHAR16
*)VariableName
, &gEfiGlobalVariableGuid
, NULL
, &Size
, Value
);
67 if (EFI_ERROR (Status
)) {
68 AsciiPrint("Error: '%r'\n",Status
);
70 AsciiPrint("%a=%a\n",AsciiVariableName
,Value
);
74 AsciiPrint("Error: '%r'\n",Status
);
77 FreePool(VariableName
);
87 EFI_STATUS Status
= EFI_INVALID_PARAMETER
;
88 CHAR8
* AsciiVariableSetting
= NULL
;
89 CHAR8
* AsciiVariableName
;
91 UINT32 AsciiValueLength
;
94 UINT32 EscapedQuotes
= 0;
95 BOOLEAN Volatile
= FALSE
;
98 AsciiPrint("Variable name is missing.\n");
102 for (Index
= 1; Index
< Argc
; Index
++) {
103 if (AsciiStrCmp(Argv
[Index
],"-v") == 0) {
105 } else if (Argv
[Index
][0] == '-') {
106 AsciiPrint("Warning: '%a' not recognized.\n",Argv
[Index
]);
108 AsciiVariableSetting
= Argv
[Index
];
112 if (AsciiVariableSetting
== NULL
) {
113 AsciiPrint("Variable name is missing.\n");
117 // Check if it is a valid variable setting
118 AsciiValue
= AsciiStrStr (AsciiVariableSetting
,"=");
119 if (AsciiValue
== NULL
) {
121 // There is no value. It means this variable will be deleted
124 // Convert VariableName into Unicode
125 VariableName
= AllocatePool((AsciiStrLen (AsciiVariableSetting
) + 1) * sizeof (CHAR16
));
126 AsciiStrToUnicodeStr (AsciiVariableSetting
,VariableName
);
128 Status
= gRT
->SetVariable (
130 &gEfiGlobalVariableGuid
,
131 ( !Volatile
? EFI_VARIABLE_NON_VOLATILE
: 0) |
132 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
136 if (!EFI_ERROR(Status
)) {
137 AsciiPrint("Variable '%s' deleted\n",VariableName
);
139 AsciiPrint("Variable setting is incorrect. It should be VariableName=Value\n");
144 AsciiValue
[0] = '\0';
145 AsciiVariableName
= AsciiVariableSetting
;
148 // Clean AsciiValue from quote
149 if (AsciiValue
[0] == '"') {
152 AsciiValueLength
= AsciiStrLen (AsciiValue
);
153 if ((AsciiValue
[AsciiValueLength
-2] != '\\') && (AsciiValue
[AsciiValueLength
-1] == '"')) {
154 AsciiValue
[AsciiValueLength
-1] = '\0';
157 // Clean AsciiValue from escaped quotes
158 for (Index
= 0; Index
< AsciiValueLength
; Index
++) {
159 if ((Index
> 0) && (AsciiValue
[Index
-1] == '\\') && (AsciiValue
[Index
] == '"')) {
162 AsciiValue
[Index
-EscapedQuotes
] = AsciiValue
[Index
];
164 // Fill the end of the value with '\0'
165 for (Index
= 0; Index
< EscapedQuotes
; Index
++) {
166 AsciiValue
[AsciiValueLength
-1-Index
] = '\0';
169 // Convert VariableName into Unicode
170 VariableName
= AllocatePool((AsciiStrLen (AsciiVariableName
) + 1) * sizeof (CHAR16
));
171 AsciiStrToUnicodeStr (AsciiVariableName
,VariableName
);
173 Status
= gRT
->SetVariable (
175 &gEfiGlobalVariableGuid
,
176 ( !Volatile
? EFI_VARIABLE_NON_VOLATILE
: 0) |
177 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
178 AsciiStrLen (AsciiValue
)+1,
181 if (!EFI_ERROR(Status
)) {
182 AsciiPrint("'%a'='%a'\n",AsciiVariableName
,AsciiValue
);
188 GLOBAL_REMOVE_IF_UNREFERENCED
const EBL_COMMAND_TABLE mCmdVariableTemplate
[] =
192 " ; get UEFI variable\n\r [v]; verbose",
198 " ; set UEFI variable\n\r [v]; create volatile variable",
205 Initialize the commands in this in this file
208 EblInitializeVariableCmds (
212 EblAddCommands (mCmdVariableTemplate
, sizeof (mCmdVariableTemplate
)/sizeof (EBL_COMMAND_TABLE
));