2 Main file for SetVar shell Debug1 function.
4 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2010 - 2014, 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
40 IsStringOfHexNibbles (
41 IN CONST CHAR16
*String
46 for (Pos
= String
; *Pos
!= L
'\0'; ++Pos
) {
47 if (!ShellIsHexaDecimalDigitCharacter (*Pos
)) {
56 Function for 'setvar' command.
58 @param[in] ImageHandle Handle to the Image (NULL if Internal).
59 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
63 ShellCommandRunSetVar (
64 IN EFI_HANDLE ImageHandle
,
65 IN EFI_SYSTEM_TABLE
*SystemTable
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 Status
= ConvertStringToGuid(StringGuid
, &Guid
);
125 if (EFI_ERROR(Status
)) {
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 for (LoopVar
= 0 ; LoopVar
< (StrLen(Data
) / 2) ; LoopVar
++) {
212 ((UINT8
*)Buffer
)[LoopVar
] = (UINT8
)(HexCharToUintn(Data
[LoopVar
*2]) * 16);
213 ((UINT8
*)Buffer
)[LoopVar
] = (UINT8
)(((UINT8
*)Buffer
)[LoopVar
] + HexCharToUintn(Data
[LoopVar
*2+1]));
215 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, StrLen(Data
) / 2, Buffer
);
217 if (EFI_ERROR(Status
)) {
218 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, L
"setvar", &Guid
, VariableName
);
219 ShellStatus
= SHELL_ACCESS_DENIED
;
221 ASSERT(ShellStatus
== SHELL_SUCCESS
);
224 } else if (StrnCmp(Data
, L
"\"", 1) == 0) {
229 Buffer
= AllocateZeroPool(StrSize(Data
) / 2);
230 if (Buffer
== NULL
) {
231 Status
= EFI_OUT_OF_RESOURCES
;
233 AsciiSPrint(Buffer
, StrSize(Data
) / 2, "%s", Data
);
234 ((CHAR8
*)Buffer
)[AsciiStrLen(Buffer
)-1] = CHAR_NULL
;
235 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, AsciiStrSize(Buffer
)-sizeof(CHAR8
), Buffer
);
237 if (EFI_ERROR(Status
)) {
238 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, L
"setvar", &Guid
, VariableName
);
239 ShellStatus
= SHELL_ACCESS_DENIED
;
241 ASSERT(ShellStatus
== SHELL_SUCCESS
);
243 } else if (StrnCmp(Data
, L
"L\"", 2) == 0) {
249 Buffer
= AllocateZeroPool(StrSize(Data
));
250 if (Buffer
== NULL
) {
251 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_OUT_MEM
), gShellDebug1HiiHandle
, L
"setvar");
252 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
254 UnicodeSPrint(Buffer
, StrSize(Data
), L
"%s", Data
);
255 ((CHAR16
*)Buffer
)[StrLen(Buffer
)-1] = CHAR_NULL
;
257 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, StrSize(Buffer
)-sizeof(CHAR16
), Buffer
);
258 if (EFI_ERROR(Status
)) {
259 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, L
"setvar", &Guid
, VariableName
);
260 ShellStatus
= SHELL_ACCESS_DENIED
;
262 ASSERT(ShellStatus
== SHELL_SUCCESS
);
265 } else if (StrnCmp(Data
, L
"--", 2) == 0) {
267 // device path in text format
271 DevPath
= ConvertTextToDevicePath(Data
);
272 if (DevPath
== NULL
) {
273 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_DPFT
), gShellDebug1HiiHandle
, L
"setvar");
274 ShellStatus
= SHELL_INVALID_PARAMETER
;
276 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, GetDevicePathSize(DevPath
), DevPath
);
277 if (EFI_ERROR(Status
)) {
278 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, L
"setvar", &Guid
, VariableName
);
279 ShellStatus
= SHELL_ACCESS_DENIED
;
281 ASSERT(ShellStatus
== SHELL_SUCCESS
);
285 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellDebug1HiiHandle
, L
"setvar", Data
);
286 ShellStatus
= SHELL_INVALID_PARAMETER
;
290 ShellCommandLineFreeVarList (Package
);
293 if (Buffer
!= NULL
) {
297 if (DevPath
!= NULL
) {
301 return (ShellStatus
);