2 Main file for SetVar shell Debug1 function.
4 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<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.
16 #include "UefiShellDebug1CommandsLib.h"
18 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
19 {L
"-guid", TypeValue
},
28 Check if the input is a (potentially empty) string of hexadecimal nibbles.
30 @param[in] String The CHAR16 string to check.
32 @retval FALSE A character has been found in String for which
33 ShellIsHexaDecimalDigitCharacter() returned FALSE.
35 @retval TRUE Otherwise. (Note that this covers the case when String is
39 IsStringOfHexNibbles (
40 IN CONST CHAR16
*String
45 for (Pos
= String
; *Pos
!= L
'\0'; ++Pos
) {
46 if (!ShellIsHexaDecimalDigitCharacter (*Pos
)) {
55 Function for 'setvar' command.
57 @param[in] ImageHandle Handle to the Image (NULL if Internal).
58 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
62 ShellCommandRunSetVar (
63 IN EFI_HANDLE ImageHandle
,
64 IN EFI_SYSTEM_TABLE
*SystemTable
68 RETURN_STATUS RStatus
;
71 SHELL_STATUS ShellStatus
;
72 CONST CHAR16
*VariableName
;
75 CONST CHAR16
*StringGuid
;
80 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
82 ShellStatus
= SHELL_SUCCESS
;
90 // initialize the shell lib (we must be in non-auto-init...)
92 Status
= ShellInitialize();
93 ASSERT_EFI_ERROR(Status
);
95 Status
= CommandInit();
96 ASSERT_EFI_ERROR(Status
);
99 // parse the command line
101 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
102 if (EFI_ERROR(Status
)) {
103 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
104 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, L
"setvar", ProblemParam
);
105 FreePool(ProblemParam
);
106 ShellStatus
= SHELL_INVALID_PARAMETER
;
111 if (ShellCommandLineGetCount(Package
) < 2) {
112 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
, L
"setvar");
113 ShellStatus
= SHELL_INVALID_PARAMETER
;
114 } else if (ShellCommandLineGetCount(Package
) > 3) {
115 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
, L
"setvar");
116 ShellStatus
= SHELL_INVALID_PARAMETER
;
118 VariableName
= ShellCommandLineGetRawValue(Package
, 1);
119 Data
= ShellCommandLineGetRawValue(Package
, 2);
120 if (!ShellCommandLineGetFlag(Package
, L
"-guid")){
121 CopyGuid(&Guid
, &gEfiGlobalVariableGuid
);
123 StringGuid
= ShellCommandLineGetValue(Package
, L
"-guid");
124 RStatus
= StrToGuid (StringGuid
, &Guid
);
125 if (RETURN_ERROR (RStatus
) || (StringGuid
[GUID_STRING_LENGTH
] != L
'\0')) {
126 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellDebug1HiiHandle
, L
"setvar", StringGuid
);
127 ShellStatus
= SHELL_INVALID_PARAMETER
;
130 if (Data
== NULL
|| Data
[0] != L
'=') {
132 // Display what's there
134 Status
= gRT
->GetVariable((CHAR16
*)VariableName
, &Guid
, &Attributes
, &Size
, Buffer
);
135 if (Status
== EFI_BUFFER_TOO_SMALL
) {
136 Buffer
= AllocateZeroPool(Size
);
137 Status
= gRT
->GetVariable((CHAR16
*)VariableName
, &Guid
, &Attributes
, &Size
, Buffer
);
139 if (!EFI_ERROR(Status
)&& Buffer
!= NULL
) {
140 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_PRINT
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Size
);
141 for (LoopVar
= 0 ; LoopVar
< Size
; LoopVar
++) {
142 ShellPrintEx(-1, -1, L
"%02x ", ((UINT8
*)Buffer
)[LoopVar
]);
144 ShellPrintEx(-1, -1, L
"\r\n");
146 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_GET
), gShellDebug1HiiHandle
, L
"setvar", &Guid
, VariableName
);
147 ShellStatus
= SHELL_ACCESS_DENIED
;
149 } else if (StrCmp(Data
, L
"=") == 0) {
151 // Delete what's there!
153 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, 0, NULL
);
154 if (EFI_ERROR(Status
)) {
155 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, L
"setvar", &Guid
, VariableName
);
156 ShellStatus
= SHELL_ACCESS_DENIED
;
158 ASSERT(ShellStatus
== SHELL_SUCCESS
);
162 // Change what's there or create a new one.
165 ASSERT(Data
[0] == L
'=');
167 ASSERT(Data
[0] != L
'\0');
170 // Determine if the variable exists and get the attributes
172 Status
= gRT
->GetVariable((CHAR16
*)VariableName
, &Guid
, &Attributes
, &Size
, Buffer
);
173 if (Status
== EFI_BUFFER_TOO_SMALL
) {
174 Buffer
= AllocateZeroPool(Size
);
175 Status
= gRT
->GetVariable((CHAR16
*)VariableName
, &Guid
, &Attributes
, &Size
, Buffer
);
178 if (EFI_ERROR(Status
) || Buffer
== NULL
) {
180 // Creating a new variable. determine attributes from command line.
183 if (ShellCommandLineGetFlag(Package
, L
"-bs")) {
184 Attributes
|= EFI_VARIABLE_BOOTSERVICE_ACCESS
;
186 if (ShellCommandLineGetFlag(Package
, L
"-rt")) {
187 Attributes
|= EFI_VARIABLE_RUNTIME_ACCESS
|
188 EFI_VARIABLE_BOOTSERVICE_ACCESS
;
190 if (ShellCommandLineGetFlag(Package
, L
"-nv")) {
191 Attributes
|= EFI_VARIABLE_NON_VOLATILE
;
194 SHELL_FREE_NON_NULL(Buffer
);
197 // What type is the new data.
199 if (IsStringOfHexNibbles(Data
)) {
200 if (StrLen(Data
) % 2 != 0) {
201 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellDebug1HiiHandle
, L
"setvar", Data
);
202 ShellStatus
= SHELL_INVALID_PARAMETER
;
207 Buffer
= AllocateZeroPool((StrLen(Data
) / 2));
208 if (Buffer
== NULL
) {
209 Status
= EFI_OUT_OF_RESOURCES
;
211 StrHexToBytes (Data
, StrLen (Data
), Buffer
, StrLen (Data
) / 2);
212 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, StrLen(Data
) / 2, Buffer
);
214 if (EFI_ERROR(Status
)) {
215 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, L
"setvar", &Guid
, VariableName
);
216 ShellStatus
= SHELL_ACCESS_DENIED
;
218 ASSERT(ShellStatus
== SHELL_SUCCESS
);
221 } else if (StrnCmp(Data
, L
"\"", 1) == 0) {
226 Buffer
= AllocateZeroPool(StrSize(Data
) / 2);
227 if (Buffer
== NULL
) {
228 Status
= EFI_OUT_OF_RESOURCES
;
230 AsciiSPrint(Buffer
, StrSize(Data
) / 2, "%s", Data
);
231 ((CHAR8
*)Buffer
)[AsciiStrLen(Buffer
)-1] = CHAR_NULL
;
232 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, AsciiStrSize(Buffer
)-sizeof(CHAR8
), Buffer
);
234 if (EFI_ERROR(Status
)) {
235 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, L
"setvar", &Guid
, VariableName
);
236 ShellStatus
= SHELL_ACCESS_DENIED
;
238 ASSERT(ShellStatus
== SHELL_SUCCESS
);
240 } else if (StrnCmp(Data
, L
"L\"", 2) == 0) {
246 Buffer
= AllocateZeroPool(StrSize(Data
));
247 if (Buffer
== NULL
) {
248 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_OUT_MEM
), gShellDebug1HiiHandle
, L
"setvar");
249 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
251 UnicodeSPrint(Buffer
, StrSize(Data
), L
"%s", Data
);
252 ((CHAR16
*)Buffer
)[StrLen(Buffer
)-1] = CHAR_NULL
;
254 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, StrSize(Buffer
)-sizeof(CHAR16
), Buffer
);
255 if (EFI_ERROR(Status
)) {
256 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, L
"setvar", &Guid
, VariableName
);
257 ShellStatus
= SHELL_ACCESS_DENIED
;
259 ASSERT(ShellStatus
== SHELL_SUCCESS
);
262 } else if (StrnCmp(Data
, L
"--", 2) == 0) {
264 // device path in text format
268 DevPath
= ConvertTextToDevicePath(Data
);
269 if (DevPath
== NULL
) {
270 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_DPFT
), gShellDebug1HiiHandle
, L
"setvar");
271 ShellStatus
= SHELL_INVALID_PARAMETER
;
273 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, GetDevicePathSize(DevPath
), DevPath
);
274 if (EFI_ERROR(Status
)) {
275 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, L
"setvar", &Guid
, VariableName
);
276 ShellStatus
= SHELL_ACCESS_DENIED
;
278 ASSERT(ShellStatus
== SHELL_SUCCESS
);
282 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellDebug1HiiHandle
, L
"setvar", Data
);
283 ShellStatus
= SHELL_INVALID_PARAMETER
;
287 ShellCommandLineFreeVarList (Package
);
290 if (Buffer
!= NULL
) {
294 if (DevPath
!= NULL
) {
298 return (ShellStatus
);