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
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
70 SHELL_STATUS ShellStatus
;
71 CONST CHAR16
*VariableName
;
74 CONST CHAR16
*StringGuid
;
79 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
81 ShellStatus
= SHELL_SUCCESS
;
89 // initialize the shell lib (we must be in non-auto-init...)
91 Status
= ShellInitialize();
92 ASSERT_EFI_ERROR(Status
);
94 Status
= CommandInit();
95 ASSERT_EFI_ERROR(Status
);
98 // parse the command line
100 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
101 if (EFI_ERROR(Status
)) {
102 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
103 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, L
"setvar", ProblemParam
);
104 FreePool(ProblemParam
);
105 ShellStatus
= SHELL_INVALID_PARAMETER
;
110 if (ShellCommandLineGetCount(Package
) < 2) {
111 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
, L
"setvar");
112 ShellStatus
= SHELL_INVALID_PARAMETER
;
113 } else if (ShellCommandLineGetCount(Package
) > 3) {
114 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
, L
"setvar");
115 ShellStatus
= SHELL_INVALID_PARAMETER
;
117 VariableName
= ShellCommandLineGetRawValue(Package
, 1);
118 Data
= ShellCommandLineGetRawValue(Package
, 2);
119 if (!ShellCommandLineGetFlag(Package
, L
"-guid")){
120 CopyGuid(&Guid
, &gEfiGlobalVariableGuid
);
122 StringGuid
= ShellCommandLineGetValue(Package
, L
"-guid");
123 Status
= ConvertStringToGuid(StringGuid
, &Guid
);
124 if (EFI_ERROR(Status
)) {
125 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellDebug1HiiHandle
, L
"setvar", StringGuid
);
126 ShellStatus
= SHELL_INVALID_PARAMETER
;
129 if (Data
== NULL
|| Data
[0] != L
'=') {
131 // Display what's there
133 Status
= gRT
->GetVariable((CHAR16
*)VariableName
, &Guid
, &Attributes
, &Size
, Buffer
);
134 if (Status
== EFI_BUFFER_TOO_SMALL
) {
135 Buffer
= AllocateZeroPool(Size
);
136 Status
= gRT
->GetVariable((CHAR16
*)VariableName
, &Guid
, &Attributes
, &Size
, Buffer
);
138 if (!EFI_ERROR(Status
)&& Buffer
!= NULL
) {
139 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_PRINT
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Size
);
140 for (LoopVar
= 0 ; LoopVar
< Size
; LoopVar
++) {
141 ShellPrintEx(-1, -1, L
"%02x ", ((UINT8
*)Buffer
)[LoopVar
]);
143 ShellPrintEx(-1, -1, L
"\r\n");
145 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_GET
), gShellDebug1HiiHandle
, L
"setvar", &Guid
, VariableName
);
146 ShellStatus
= SHELL_ACCESS_DENIED
;
148 } else if (StrCmp(Data
, L
"=") == 0) {
150 // Delete what's there!
152 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, 0, NULL
);
153 if (EFI_ERROR(Status
)) {
154 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, L
"setvar", &Guid
, VariableName
);
155 ShellStatus
= SHELL_ACCESS_DENIED
;
157 ASSERT(ShellStatus
== SHELL_SUCCESS
);
161 // Change what's there or create a new one.
164 ASSERT(Data
[0] == L
'=');
166 ASSERT(Data
[0] != L
'\0');
169 // Determine if the variable exists and get the attributes
171 Status
= gRT
->GetVariable((CHAR16
*)VariableName
, &Guid
, &Attributes
, &Size
, Buffer
);
172 if (Status
== EFI_BUFFER_TOO_SMALL
) {
173 Buffer
= AllocateZeroPool(Size
);
174 Status
= gRT
->GetVariable((CHAR16
*)VariableName
, &Guid
, &Attributes
, &Size
, Buffer
);
177 if (EFI_ERROR(Status
) || Buffer
== NULL
) {
179 // Creating a new variable. determine attributes from command line.
182 if (ShellCommandLineGetFlag(Package
, L
"-bs")) {
183 Attributes
|= EFI_VARIABLE_BOOTSERVICE_ACCESS
;
185 if (ShellCommandLineGetFlag(Package
, L
"-rt")) {
186 Attributes
|= EFI_VARIABLE_RUNTIME_ACCESS
|
187 EFI_VARIABLE_BOOTSERVICE_ACCESS
;
189 if (ShellCommandLineGetFlag(Package
, L
"-nv")) {
190 Attributes
|= EFI_VARIABLE_NON_VOLATILE
;
193 SHELL_FREE_NON_NULL(Buffer
);
196 // What type is the new data.
198 if (IsStringOfHexNibbles(Data
)) {
199 if (StrLen(Data
) % 2 != 0) {
200 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellDebug1HiiHandle
, L
"setvar", Data
);
201 ShellStatus
= SHELL_INVALID_PARAMETER
;
206 Buffer
= AllocateZeroPool((StrLen(Data
) / 2));
207 if (Buffer
== NULL
) {
208 Status
= EFI_OUT_OF_RESOURCES
;
210 for (LoopVar
= 0 ; LoopVar
< (StrLen(Data
) / 2) ; LoopVar
++) {
211 ((UINT8
*)Buffer
)[LoopVar
] = (UINT8
)(HexCharToUintn(Data
[LoopVar
*2]) * 16);
212 ((UINT8
*)Buffer
)[LoopVar
] = (UINT8
)(((UINT8
*)Buffer
)[LoopVar
] + HexCharToUintn(Data
[LoopVar
*2+1]));
214 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, StrLen(Data
) / 2, Buffer
);
216 if (EFI_ERROR(Status
)) {
217 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, L
"setvar", &Guid
, VariableName
);
218 ShellStatus
= SHELL_ACCESS_DENIED
;
220 ASSERT(ShellStatus
== SHELL_SUCCESS
);
223 } else if (StrnCmp(Data
, L
"\"", 1) == 0) {
228 Buffer
= AllocateZeroPool(StrSize(Data
) / 2);
229 if (Buffer
== NULL
) {
230 Status
= EFI_OUT_OF_RESOURCES
;
232 AsciiSPrint(Buffer
, StrSize(Data
) / 2, "%s", Data
);
233 ((CHAR8
*)Buffer
)[AsciiStrLen(Buffer
)-1] = CHAR_NULL
;
234 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, AsciiStrSize(Buffer
)-sizeof(CHAR8
), Buffer
);
236 if (EFI_ERROR(Status
)) {
237 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, L
"setvar", &Guid
, VariableName
);
238 ShellStatus
= SHELL_ACCESS_DENIED
;
240 ASSERT(ShellStatus
== SHELL_SUCCESS
);
242 } else if (StrnCmp(Data
, L
"L\"", 2) == 0) {
248 Buffer
= AllocateZeroPool(StrSize(Data
));
249 if (Buffer
== NULL
) {
250 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_OUT_MEM
), gShellDebug1HiiHandle
, L
"setvar");
251 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
253 UnicodeSPrint(Buffer
, StrSize(Data
), L
"%s", Data
);
254 ((CHAR16
*)Buffer
)[StrLen(Buffer
)-1] = CHAR_NULL
;
256 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, StrSize(Buffer
)-sizeof(CHAR16
), Buffer
);
257 if (EFI_ERROR(Status
)) {
258 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, L
"setvar", &Guid
, VariableName
);
259 ShellStatus
= SHELL_ACCESS_DENIED
;
261 ASSERT(ShellStatus
== SHELL_SUCCESS
);
264 } else if (StrnCmp(Data
, L
"--", 2) == 0) {
266 // device path in text format
270 DevPath
= ConvertTextToDevicePath(Data
);
271 if (DevPath
== NULL
) {
272 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_DPFT
), gShellDebug1HiiHandle
, L
"setvar");
273 ShellStatus
= SHELL_INVALID_PARAMETER
;
275 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, GetDevicePathSize(DevPath
), DevPath
);
276 if (EFI_ERROR(Status
)) {
277 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, L
"setvar", &Guid
, VariableName
);
278 ShellStatus
= SHELL_ACCESS_DENIED
;
280 ASSERT(ShellStatus
== SHELL_SUCCESS
);
284 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellDebug1HiiHandle
, L
"setvar", Data
);
285 ShellStatus
= SHELL_INVALID_PARAMETER
;
289 ShellCommandLineFreeVarList (Package
);
292 if (Buffer
!= NULL
) {
296 if (DevPath
!= NULL
) {
300 return (ShellStatus
);