2 Main file for SetVar shell Debug1 function.
4 Copyright (c) 2010 - 2014, 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
;
50 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
52 ShellStatus
= SHELL_SUCCESS
;
60 // initialize the shell lib (we must be in non-auto-init...)
62 Status
= ShellInitialize();
63 ASSERT_EFI_ERROR(Status
);
65 Status
= CommandInit();
66 ASSERT_EFI_ERROR(Status
);
69 // parse the command line
71 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
72 if (EFI_ERROR(Status
)) {
73 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
74 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, ProblemParam
);
75 FreePool(ProblemParam
);
76 ShellStatus
= SHELL_INVALID_PARAMETER
;
81 if (ShellCommandLineGetCount(Package
) < 2) {
82 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
);
83 ShellStatus
= SHELL_INVALID_PARAMETER
;
84 } else if (ShellCommandLineGetCount(Package
) > 3) {
85 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
);
86 ShellStatus
= SHELL_INVALID_PARAMETER
;
88 VariableName
= ShellCommandLineGetRawValue(Package
, 1);
89 Data
= ShellCommandLineGetRawValue(Package
, 2);
90 if (!ShellCommandLineGetFlag(Package
, L
"-guid")){
91 CopyGuid(&Guid
, &gEfiGlobalVariableGuid
);
93 StringGuid
= ShellCommandLineGetValue(Package
, L
"-guid");
94 Status
= ConvertStringToGuid(StringGuid
, &Guid
);
95 if (EFI_ERROR(Status
)) {
96 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, StringGuid
);
97 ShellStatus
= SHELL_INVALID_PARAMETER
;
100 if (Data
== NULL
|| Data
[0] != L
'=') {
102 // Display what's there
104 Status
= gRT
->GetVariable((CHAR16
*)VariableName
, &Guid
, &Attributes
, &Size
, Buffer
);
105 if (Status
== EFI_BUFFER_TOO_SMALL
) {
106 Buffer
= AllocateZeroPool(Size
);
107 Status
= gRT
->GetVariable((CHAR16
*)VariableName
, &Guid
, &Attributes
, &Size
, Buffer
);
109 if (!EFI_ERROR(Status
)&& Buffer
!= NULL
) {
110 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_PRINT
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Size
);
111 for (LoopVar
= 0 ; LoopVar
< Size
; LoopVar
++) {
112 ShellPrintEx(-1, -1, L
"%02x ", ((UINT8
*)Buffer
)[LoopVar
]);
114 ShellPrintEx(-1, -1, L
"\r\n");
116 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_GET
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Status
);
117 ShellStatus
= SHELL_ACCESS_DENIED
;
119 } else if (StrCmp(Data
, L
"=") == 0) {
121 // Delete what's there!
123 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, 0, NULL
);
124 if (EFI_ERROR(Status
)) {
125 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Status
);
126 ShellStatus
= SHELL_ACCESS_DENIED
;
128 ASSERT(ShellStatus
== SHELL_SUCCESS
);
132 // Change what's there or create a new one.
135 ASSERT(Data
[0] == L
'=');
139 // Determine if the variable exists and get the attributes
141 Status
= gRT
->GetVariable((CHAR16
*)VariableName
, &Guid
, &Attributes
, &Size
, Buffer
);
142 if (Status
== EFI_BUFFER_TOO_SMALL
) {
143 Buffer
= AllocateZeroPool(Size
);
144 Status
= gRT
->GetVariable((CHAR16
*)VariableName
, &Guid
, &Attributes
, &Size
, Buffer
);
147 if (EFI_ERROR(Status
) || Buffer
== NULL
) {
149 // Creating a new variable. determine attributes from command line.
152 if (ShellCommandLineGetFlag(Package
, L
"-bs")) {
153 Attributes
|= EFI_VARIABLE_BOOTSERVICE_ACCESS
;
155 if (ShellCommandLineGetFlag(Package
, L
"-rt")) {
156 Attributes
|= EFI_VARIABLE_RUNTIME_ACCESS
|
157 EFI_VARIABLE_BOOTSERVICE_ACCESS
;
159 if (ShellCommandLineGetFlag(Package
, L
"-nv")) {
160 Attributes
|= EFI_VARIABLE_NON_VOLATILE
;
163 SHELL_FREE_NON_NULL(Buffer
);
166 // What type is the new data.
168 if (ShellIsHexOrDecimalNumber(Data
, TRUE
, FALSE
)) {
169 if (StrLen(Data
) % 2 != 0) {
170 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM_VAL
), gShellDebug1HiiHandle
, Data
);
171 ShellStatus
= SHELL_INVALID_PARAMETER
;
176 Buffer
= AllocateZeroPool((StrLen(Data
) / 2));
177 if (Buffer
== NULL
) {
178 Status
= EFI_OUT_OF_RESOURCES
;
180 for (LoopVar
= 0 ; LoopVar
< (StrLen(Data
) / 2) ; LoopVar
++) {
181 ((UINT8
*)Buffer
)[LoopVar
] = (UINT8
)(HexCharToUintn(Data
[LoopVar
*2]) * 16);
182 ((UINT8
*)Buffer
)[LoopVar
] = (UINT8
)(((UINT8
*)Buffer
)[LoopVar
] + HexCharToUintn(Data
[LoopVar
*2+1]));
184 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, StrLen(Data
) / 2, Buffer
);
186 if (EFI_ERROR(Status
)) {
187 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Status
);
188 ShellStatus
= SHELL_ACCESS_DENIED
;
190 ASSERT(ShellStatus
== SHELL_SUCCESS
);
193 } else if (StrnCmp(Data
, L
"\"", 1) == 0) {
198 Buffer
= AllocateZeroPool(StrSize(Data
) / 2);
199 if (Buffer
== NULL
) {
200 Status
= EFI_OUT_OF_RESOURCES
;
202 AsciiSPrint(Buffer
, StrSize(Data
) / 2, "%s", Data
);
203 ((CHAR8
*)Buffer
)[AsciiStrLen(Buffer
)-1] = CHAR_NULL
;
204 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, AsciiStrSize(Buffer
)-sizeof(CHAR8
), Buffer
);
206 if (EFI_ERROR(Status
)) {
207 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Status
);
208 ShellStatus
= SHELL_ACCESS_DENIED
;
210 ASSERT(ShellStatus
== SHELL_SUCCESS
);
212 } else if (StrnCmp(Data
, L
"L\"", 2) == 0) {
218 Buffer
= AllocateZeroPool(StrSize(Data
));
219 if (Buffer
== NULL
) {
220 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_OUT_MEM
), gShellDebug1HiiHandle
);
221 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
223 UnicodeSPrint(Buffer
, StrSize(Data
), L
"%s", Data
);
224 ((CHAR16
*)Buffer
)[StrLen(Buffer
)-1] = CHAR_NULL
;
226 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, StrSize(Buffer
)-sizeof(CHAR16
), Buffer
);
227 if (EFI_ERROR(Status
)) {
228 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Status
);
229 ShellStatus
= SHELL_ACCESS_DENIED
;
231 ASSERT(ShellStatus
== SHELL_SUCCESS
);
234 } else if (StrnCmp(Data
, L
"--", 2) == 0) {
236 // device path in text format
240 DevPath
= ConvertTextToDevicePath(Data
);
241 if (DevPath
== NULL
) {
242 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_DPFT
), gShellDebug1HiiHandle
, Status
);
243 ShellStatus
= SHELL_INVALID_PARAMETER
;
245 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, GetDevicePathSize(DevPath
), DevPath
);
246 if (EFI_ERROR(Status
)) {
247 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Status
);
248 ShellStatus
= SHELL_ACCESS_DENIED
;
250 ASSERT(ShellStatus
== SHELL_SUCCESS
);
254 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, Data
);
255 ShellStatus
= SHELL_INVALID_PARAMETER
;
259 ShellCommandLineFreeVarList (Package
);
262 if (Buffer
!= NULL
) {
266 if (DevPath
!= NULL
) {
270 return (ShellStatus
);