2 Main file for SetVar shell Debug1 function.
4 Copyright (c) 2010 - 2013, Intel Corporation. All rights reserved.<BR>
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.
15 #include "UefiShellDebug1CommandsLib.h"
17 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
18 {L
"-guid", TypeValue
},
26 Function for 'setvar' command.
28 @param[in] ImageHandle Handle to the Image (NULL if Internal).
29 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
33 ShellCommandRunSetVar (
34 IN EFI_HANDLE ImageHandle
,
35 IN EFI_SYSTEM_TABLE
*SystemTable
41 SHELL_STATUS ShellStatus
;
42 CONST CHAR16
*VariableName
;
45 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
, 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
);
84 ShellStatus
= SHELL_INVALID_PARAMETER
;
85 } else if (ShellCommandLineGetCount(Package
) > 3) {
86 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
);
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_PROBLEM
), gShellDebug1HiiHandle
, StringGuid
);
98 ShellStatus
= SHELL_INVALID_PARAMETER
;
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
, &Guid
, VariableName
, Status
);
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
, &Guid
, VariableName
, Status
);
127 ShellStatus
= SHELL_ACCESS_DENIED
;
129 ASSERT(ShellStatus
== SHELL_SUCCESS
);
132 if (Data
[0] == L
'=') {
136 // Change what's there
138 if (ShellCommandLineGetFlag(Package
, L
"-bs")) {
139 Attributes
|= EFI_VARIABLE_BOOTSERVICE_ACCESS
;
141 if (ShellCommandLineGetFlag(Package
, L
"-rt")) {
142 Attributes
|= EFI_VARIABLE_RUNTIME_ACCESS
|
143 EFI_VARIABLE_BOOTSERVICE_ACCESS
;
145 if (ShellCommandLineGetFlag(Package
, L
"-nv")) {
146 Attributes
|= EFI_VARIABLE_NON_VOLATILE
;
148 if (ShellIsHexOrDecimalNumber(Data
, TRUE
, FALSE
)) {
149 if (StrLen(Data
) % 2 != 0) {
150 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM_VAL
), gShellDebug1HiiHandle
, Data
);
151 ShellStatus
= SHELL_INVALID_PARAMETER
;
156 Buffer
= AllocateZeroPool((StrLen(Data
) / 2));
157 if (Buffer
== NULL
) {
158 Status
= EFI_OUT_OF_RESOURCES
;
160 for (LoopVar
= 0 ; LoopVar
< (StrLen(Data
) / 2) ; LoopVar
++) {
161 ((UINT8
*)Buffer
)[LoopVar
] = (UINT8
)(HexCharToUintn(Data
[LoopVar
*2]) * 16);
162 ((UINT8
*)Buffer
)[LoopVar
] = (UINT8
)(((UINT8
*)Buffer
)[LoopVar
] + HexCharToUintn(Data
[LoopVar
*2+1]));
164 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, StrLen(Data
) / 2, Buffer
);
166 if (EFI_ERROR(Status
)) {
167 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Status
);
168 ShellStatus
= SHELL_ACCESS_DENIED
;
170 ASSERT(ShellStatus
== SHELL_SUCCESS
);
173 } else if (StrnCmp(Data
, L
"\"", 1) == 0) {
176 Status
= gRT
->GetVariable((CHAR16
*)VariableName
, &Guid
, &Attributes2
, &Size
, Buffer
);
177 if (Status
== EFI_BUFFER_TOO_SMALL
) {
178 Buffer
= AllocateZeroPool(Size
);
179 Status
= gRT
->GetVariable((CHAR16
*)VariableName
, &Guid
, &Attributes2
, &Size
, Buffer
);
180 if (Buffer
!= NULL
) {
183 Attributes
= Attributes2
;
189 Buffer
= AllocateZeroPool(StrSize(Data
) / 2);
190 if (Buffer
== NULL
) {
191 Status
= EFI_OUT_OF_RESOURCES
;
193 AsciiSPrint(Buffer
, StrSize(Data
) / 2, "%s", Data
);
194 ((CHAR8
*)Buffer
)[AsciiStrLen(Buffer
)-1] = CHAR_NULL
;
195 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, AsciiStrSize(Buffer
)-sizeof(CHAR8
), Buffer
);
197 if (EFI_ERROR(Status
)) {
198 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Status
);
199 ShellStatus
= SHELL_ACCESS_DENIED
;
201 ASSERT(ShellStatus
== SHELL_SUCCESS
);
203 } else if (StrnCmp(Data
, L
"L\"", 2) == 0) {
209 Buffer
= AllocateZeroPool(StrSize(Data
));
210 if (Buffer
== NULL
) {
211 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_OUT_MEM
), gShellDebug1HiiHandle
);
212 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
214 UnicodeSPrint(Buffer
, StrSize(Data
), L
"%s", Data
);
215 ((CHAR16
*)Buffer
)[StrLen(Buffer
)-1] = CHAR_NULL
;
217 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, StrSize(Buffer
)-sizeof(CHAR16
), Buffer
);
218 if (EFI_ERROR(Status
)) {
219 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Status
);
220 ShellStatus
= SHELL_ACCESS_DENIED
;
222 ASSERT(ShellStatus
== SHELL_SUCCESS
);
225 } else if (StrnCmp(Data
, L
"--", 2) == 0) {
227 // device path in text format
231 DevPath
= ConvertTextToDevicePath(Data
);
232 if (DevPath
== NULL
) {
233 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_DPFT
), gShellDebug1HiiHandle
, Status
);
234 ShellStatus
= SHELL_INVALID_PARAMETER
;
236 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, GetDevicePathSize(DevPath
), DevPath
);
237 if (EFI_ERROR(Status
)) {
238 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Status
);
239 ShellStatus
= SHELL_ACCESS_DENIED
;
241 ASSERT(ShellStatus
== SHELL_SUCCESS
);
245 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, Data
);
246 ShellStatus
= SHELL_INVALID_PARAMETER
;
250 ShellCommandLineFreeVarList (Package
);
253 if (Buffer
!= NULL
) {
257 if (DevPath
!= NULL
) {
261 return (ShellStatus
);