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
;
144 if (ShellCommandLineGetFlag(Package
, L
"-nv")) {
145 Attributes
|= EFI_VARIABLE_NON_VOLATILE
;
147 if (ShellIsHexOrDecimalNumber(Data
, TRUE
, FALSE
)) {
148 if (StrLen(Data
) % 2 != 0) {
149 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM_VAL
), gShellDebug1HiiHandle
, Data
);
150 ShellStatus
= SHELL_INVALID_PARAMETER
;
155 Buffer
= AllocateZeroPool((StrLen(Data
) / 2));
156 if (Buffer
== NULL
) {
157 Status
= EFI_OUT_OF_RESOURCES
;
159 for (LoopVar
= 0 ; LoopVar
< (StrLen(Data
) / 2) ; LoopVar
++) {
160 ((UINT8
*)Buffer
)[LoopVar
] = (UINT8
)(HexCharToUintn(Data
[LoopVar
*2]) * 16);
161 ((UINT8
*)Buffer
)[LoopVar
] = (UINT8
)(((UINT8
*)Buffer
)[LoopVar
] + HexCharToUintn(Data
[LoopVar
*2+1]));
163 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, StrLen(Data
) / 2, Buffer
);
165 if (EFI_ERROR(Status
)) {
166 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Status
);
167 ShellStatus
= SHELL_ACCESS_DENIED
;
169 ASSERT(ShellStatus
== SHELL_SUCCESS
);
172 } else if (StrnCmp(Data
, L
"\"", 1) == 0) {
175 Status
= gRT
->GetVariable((CHAR16
*)VariableName
, &Guid
, &Attributes2
, &Size
, Buffer
);
176 if (Status
== EFI_BUFFER_TOO_SMALL
) {
177 Buffer
= AllocateZeroPool(Size
);
178 Status
= gRT
->GetVariable((CHAR16
*)VariableName
, &Guid
, &Attributes2
, &Size
, Buffer
);
179 if (Buffer
!= NULL
) {
182 Attributes
= Attributes2
;
188 Buffer
= AllocateZeroPool(StrSize(Data
) / 2);
189 if (Buffer
== NULL
) {
190 Status
= EFI_OUT_OF_RESOURCES
;
192 AsciiSPrint(Buffer
, StrSize(Data
) / 2, "%s", Data
);
193 ((CHAR8
*)Buffer
)[AsciiStrLen(Buffer
)-1] = CHAR_NULL
;
194 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, AsciiStrSize(Buffer
)-sizeof(CHAR8
), Buffer
);
196 if (EFI_ERROR(Status
)) {
197 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Status
);
198 ShellStatus
= SHELL_ACCESS_DENIED
;
200 ASSERT(ShellStatus
== SHELL_SUCCESS
);
202 } else if (StrnCmp(Data
, L
"L\"", 2) == 0) {
208 Buffer
= AllocateZeroPool(StrSize(Data
));
209 if (Buffer
== NULL
) {
210 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_OUT_MEM
), gShellDebug1HiiHandle
);
211 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
213 UnicodeSPrint(Buffer
, StrSize(Data
), L
"%s", Data
);
214 ((CHAR16
*)Buffer
)[StrLen(Buffer
)-1] = CHAR_NULL
;
216 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, StrSize(Buffer
)-sizeof(CHAR16
), Buffer
);
217 if (EFI_ERROR(Status
)) {
218 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Status
);
219 ShellStatus
= SHELL_ACCESS_DENIED
;
221 ASSERT(ShellStatus
== SHELL_SUCCESS
);
224 } else if (StrnCmp(Data
, L
"--", 2) == 0) {
226 // device path in text format
230 DevPath
= ConvertTextToDevicePath(Data
);
231 if (DevPath
== NULL
) {
232 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_DPFT
), gShellDebug1HiiHandle
, Status
);
233 ShellStatus
= SHELL_INVALID_PARAMETER
;
235 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, GetDevicePathSize(DevPath
), DevPath
);
236 if (EFI_ERROR(Status
)) {
237 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Status
);
238 ShellStatus
= SHELL_ACCESS_DENIED
;
240 ASSERT(ShellStatus
== SHELL_SUCCESS
);
244 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, Data
);
245 ShellStatus
= SHELL_INVALID_PARAMETER
;
249 ShellCommandLineFreeVarList (Package
);
252 if (Buffer
!= NULL
) {
256 if (DevPath
!= NULL
) {
260 return (ShellStatus
);