2 Main file for SetVar shell Debug1 function.
4 Copyright (c) 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
},
27 Function for 'setvar' command.
29 @param[in] ImageHandle Handle to the Image (NULL if Internal).
30 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
34 ShellCommandRunSetVar (
35 IN EFI_HANDLE ImageHandle
,
36 IN EFI_SYSTEM_TABLE
*SystemTable
42 SHELL_STATUS ShellStatus
;
43 CONST CHAR16
*VariableName
;
46 CONST CHAR16
*StringGuid
;
51 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
53 ShellStatus
= SHELL_SUCCESS
;
61 // initialize the shell lib (we must be in non-auto-init...)
63 Status
= ShellInitialize();
64 ASSERT_EFI_ERROR(Status
);
66 Status
= CommandInit();
67 ASSERT_EFI_ERROR(Status
);
70 // parse the command line
72 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
73 if (EFI_ERROR(Status
)) {
74 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
75 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, L
"setvar", ProblemParam
);
76 FreePool(ProblemParam
);
77 ShellStatus
= SHELL_INVALID_PARAMETER
;
82 if (ShellCommandLineGetCount(Package
) < 2) {
83 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
, L
"setvar");
84 ShellStatus
= SHELL_INVALID_PARAMETER
;
85 } else if (ShellCommandLineGetCount(Package
) > 3) {
86 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
, L
"setvar");
87 ShellStatus
= SHELL_INVALID_PARAMETER
;
89 VariableName
= ShellCommandLineGetRawValue(Package
, 1);
90 Data
= ShellCommandLineGetRawValue(Package
, 2);
91 if (!ShellCommandLineGetFlag(Package
, L
"-guid")){
92 CopyGuid(&Guid
, &gEfiGlobalVariableGuid
);
94 StringGuid
= ShellCommandLineGetValue(Package
, L
"-guid");
95 Status
= ConvertStringToGuid(StringGuid
, &Guid
);
96 if (EFI_ERROR(Status
)) {
97 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellDebug1HiiHandle
, L
"setvar", StringGuid
);
98 ShellStatus
= SHELL_INVALID_PARAMETER
;
101 if (Data
== NULL
|| Data
[0] != L
'=') {
103 // Display what's there
105 Status
= gRT
->GetVariable((CHAR16
*)VariableName
, &Guid
, &Attributes
, &Size
, Buffer
);
106 if (Status
== EFI_BUFFER_TOO_SMALL
) {
107 Buffer
= AllocateZeroPool(Size
);
108 Status
= gRT
->GetVariable((CHAR16
*)VariableName
, &Guid
, &Attributes
, &Size
, Buffer
);
110 if (!EFI_ERROR(Status
)&& Buffer
!= NULL
) {
111 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_PRINT
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Size
);
112 for (LoopVar
= 0 ; LoopVar
< Size
; LoopVar
++) {
113 ShellPrintEx(-1, -1, L
"%02x ", ((UINT8
*)Buffer
)[LoopVar
]);
115 ShellPrintEx(-1, -1, L
"\r\n");
117 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_GET
), gShellDebug1HiiHandle
, L
"setvar", &Guid
, VariableName
);
118 ShellStatus
= SHELL_ACCESS_DENIED
;
120 } else if (StrCmp(Data
, L
"=") == 0) {
122 // Delete what's there!
124 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, 0, NULL
);
125 if (EFI_ERROR(Status
)) {
126 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, L
"setvar", &Guid
, VariableName
);
127 ShellStatus
= SHELL_ACCESS_DENIED
;
129 ASSERT(ShellStatus
== SHELL_SUCCESS
);
133 // Change what's there or create a new one.
136 ASSERT(Data
[0] == L
'=');
140 // Determine if the variable exists and get the attributes
142 Status
= gRT
->GetVariable((CHAR16
*)VariableName
, &Guid
, &Attributes
, &Size
, Buffer
);
143 if (Status
== EFI_BUFFER_TOO_SMALL
) {
144 Buffer
= AllocateZeroPool(Size
);
145 Status
= gRT
->GetVariable((CHAR16
*)VariableName
, &Guid
, &Attributes
, &Size
, Buffer
);
148 if (EFI_ERROR(Status
) || Buffer
== NULL
) {
150 // Creating a new variable. determine attributes from command line.
153 if (ShellCommandLineGetFlag(Package
, L
"-bs")) {
154 Attributes
|= EFI_VARIABLE_BOOTSERVICE_ACCESS
;
156 if (ShellCommandLineGetFlag(Package
, L
"-rt")) {
157 Attributes
|= EFI_VARIABLE_RUNTIME_ACCESS
|
158 EFI_VARIABLE_BOOTSERVICE_ACCESS
;
160 if (ShellCommandLineGetFlag(Package
, L
"-nv")) {
161 Attributes
|= EFI_VARIABLE_NON_VOLATILE
;
164 SHELL_FREE_NON_NULL(Buffer
);
167 // What type is the new data.
169 if (ShellIsHexOrDecimalNumber(Data
, TRUE
, FALSE
)) {
170 if (StrLen(Data
) % 2 != 0) {
171 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellDebug1HiiHandle
, L
"setvar", Data
);
172 ShellStatus
= SHELL_INVALID_PARAMETER
;
177 Buffer
= AllocateZeroPool((StrLen(Data
) / 2));
178 if (Buffer
== NULL
) {
179 Status
= EFI_OUT_OF_RESOURCES
;
181 for (LoopVar
= 0 ; LoopVar
< (StrLen(Data
) / 2) ; LoopVar
++) {
182 ((UINT8
*)Buffer
)[LoopVar
] = (UINT8
)(HexCharToUintn(Data
[LoopVar
*2]) * 16);
183 ((UINT8
*)Buffer
)[LoopVar
] = (UINT8
)(((UINT8
*)Buffer
)[LoopVar
] + HexCharToUintn(Data
[LoopVar
*2+1]));
185 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, StrLen(Data
) / 2, Buffer
);
187 if (EFI_ERROR(Status
)) {
188 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, L
"setvar", &Guid
, VariableName
);
189 ShellStatus
= SHELL_ACCESS_DENIED
;
191 ASSERT(ShellStatus
== SHELL_SUCCESS
);
194 } else if (StrnCmp(Data
, L
"\"", 1) == 0) {
199 Buffer
= AllocateZeroPool(StrSize(Data
) / 2);
200 if (Buffer
== NULL
) {
201 Status
= EFI_OUT_OF_RESOURCES
;
203 AsciiSPrint(Buffer
, StrSize(Data
) / 2, "%s", Data
);
204 ((CHAR8
*)Buffer
)[AsciiStrLen(Buffer
)-1] = CHAR_NULL
;
205 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, AsciiStrSize(Buffer
)-sizeof(CHAR8
), Buffer
);
207 if (EFI_ERROR(Status
)) {
208 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, L
"setvar", &Guid
, VariableName
);
209 ShellStatus
= SHELL_ACCESS_DENIED
;
211 ASSERT(ShellStatus
== SHELL_SUCCESS
);
213 } else if (StrnCmp(Data
, L
"L\"", 2) == 0) {
219 Buffer
= AllocateZeroPool(StrSize(Data
));
220 if (Buffer
== NULL
) {
221 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_OUT_MEM
), gShellDebug1HiiHandle
, L
"setvar");
222 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
224 UnicodeSPrint(Buffer
, StrSize(Data
), L
"%s", Data
);
225 ((CHAR16
*)Buffer
)[StrLen(Buffer
)-1] = CHAR_NULL
;
227 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, StrSize(Buffer
)-sizeof(CHAR16
), Buffer
);
228 if (EFI_ERROR(Status
)) {
229 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, L
"setvar", &Guid
, VariableName
);
230 ShellStatus
= SHELL_ACCESS_DENIED
;
232 ASSERT(ShellStatus
== SHELL_SUCCESS
);
235 } else if (StrnCmp(Data
, L
"--", 2) == 0) {
237 // device path in text format
241 DevPath
= ConvertTextToDevicePath(Data
);
242 if (DevPath
== NULL
) {
243 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_DPFT
), gShellDebug1HiiHandle
, L
"setvar");
244 ShellStatus
= SHELL_INVALID_PARAMETER
;
246 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, GetDevicePathSize(DevPath
), DevPath
);
247 if (EFI_ERROR(Status
)) {
248 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, L
"setvar", &Guid
, VariableName
);
249 ShellStatus
= SHELL_ACCESS_DENIED
;
251 ASSERT(ShellStatus
== SHELL_SUCCESS
);
255 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellDebug1HiiHandle
, L
"setvar", Data
);
256 ShellStatus
= SHELL_INVALID_PARAMETER
;
260 ShellCommandLineFreeVarList (Package
);
263 if (Buffer
!= NULL
) {
267 if (DevPath
!= NULL
) {
271 return (ShellStatus
);