2 Main file for SetVar shell Debug1 function.
4 Copyright (c) 2010 - 2011, 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
;
52 EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL
*DevPathFromText
;
54 ShellStatus
= SHELL_SUCCESS
;
62 // initialize the shell lib (we must be in non-auto-init...)
64 Status
= ShellInitialize();
65 ASSERT_EFI_ERROR(Status
);
67 Status
= CommandInit();
68 ASSERT_EFI_ERROR(Status
);
71 // parse the command line
73 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
74 if (EFI_ERROR(Status
)) {
75 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
76 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, ProblemParam
);
77 FreePool(ProblemParam
);
78 ShellStatus
= SHELL_INVALID_PARAMETER
;
83 if (ShellCommandLineGetCount(Package
) < 2) {
84 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
);
85 ShellStatus
= SHELL_INVALID_PARAMETER
;
86 } else if (ShellCommandLineGetCount(Package
) > 3) {
87 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
);
88 ShellStatus
= SHELL_INVALID_PARAMETER
;
90 VariableName
= ShellCommandLineGetRawValue(Package
, 1);
91 Data
= ShellCommandLineGetRawValue(Package
, 2);
92 if (!ShellCommandLineGetFlag(Package
, L
"-guid")){
93 CopyGuid(&Guid
, &gEfiGlobalVariableGuid
);
95 StringGuid
= ShellCommandLineGetValue(Package
, L
"-guid");
96 Status
= ConvertStringToGuid(StringGuid
, &Guid
);
97 if (EFI_ERROR(Status
)) {
98 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, StringGuid
);
99 ShellStatus
= SHELL_INVALID_PARAMETER
;
104 // Display what's there
106 Status
= gRT
->GetVariable((CHAR16
*)VariableName
, &Guid
, &Attributes
, &Size
, Buffer
);
107 if (Status
== EFI_BUFFER_TOO_SMALL
) {
108 Buffer
= AllocateZeroPool(Size
);
109 Status
= gRT
->GetVariable((CHAR16
*)VariableName
, &Guid
, &Attributes
, &Size
, Buffer
);
111 if (!EFI_ERROR(Status
)&& Buffer
!= NULL
) {
112 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_PRINT
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Size
);
113 for (LoopVar
= 0 ; LoopVar
< Size
; LoopVar
++) {
114 ShellPrintEx(-1, -1, L
"%02x ", ((UINT8
*)Buffer
)[LoopVar
]);
116 ShellPrintEx(-1, -1, L
"\r\n");
118 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_GET
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Status
);
119 ShellStatus
= SHELL_ACCESS_DENIED
;
121 } else if (StrCmp(Data
, L
"=") == 0) {
123 // Delete what's there!
125 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, 0, NULL
);
126 if (EFI_ERROR(Status
)) {
127 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Status
);
128 ShellStatus
= SHELL_ACCESS_DENIED
;
130 ASSERT(ShellStatus
== SHELL_SUCCESS
);
133 if (Data
[0] == L
'=') {
137 // Change what's there
139 if (ShellCommandLineGetFlag(Package
, L
"-bs")) {
140 Attributes
|= EFI_VARIABLE_BOOTSERVICE_ACCESS
;
142 if (ShellCommandLineGetFlag(Package
, L
"-rt")) {
143 Attributes
|= EFI_VARIABLE_RUNTIME_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
);
181 Attributes
= Attributes2
;
187 Buffer
= AllocateZeroPool(StrSize(Data
) / 2);
188 if (Buffer
== NULL
) {
189 Status
= EFI_OUT_OF_RESOURCES
;
191 AsciiSPrint(Buffer
, StrSize(Data
) / 2, "%s", Data
);
192 ((CHAR8
*)Buffer
)[AsciiStrLen(Buffer
)-1] = CHAR_NULL
;
193 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, AsciiStrSize(Buffer
)-sizeof(CHAR8
), Buffer
);
195 if (EFI_ERROR(Status
)) {
196 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Status
);
197 ShellStatus
= SHELL_ACCESS_DENIED
;
199 ASSERT(ShellStatus
== SHELL_SUCCESS
);
201 } else if (StrnCmp(Data
, L
"L\"", 2) == 0) {
207 Buffer
= AllocateZeroPool(StrSize(Data
));
208 if (Buffer
== NULL
) {
209 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_OUT_MEM
), gShellDebug1HiiHandle
);
210 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
212 UnicodeSPrint(Buffer
, StrSize(Data
), L
"%s", Data
);
213 ((CHAR16
*)Buffer
)[StrLen(Buffer
)-1] = CHAR_NULL
;
215 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, StrSize(Buffer
)-sizeof(CHAR16
), Buffer
);
216 if (EFI_ERROR(Status
)) {
217 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Status
);
218 ShellStatus
= SHELL_ACCESS_DENIED
;
220 ASSERT(ShellStatus
== SHELL_SUCCESS
);
223 } else if (StrnCmp(Data
, L
"--", 2) == 0) {
225 // device path in text format
229 Status
= gBS
->LocateProtocol(&gEfiDevicePathFromTextProtocolGuid
, NULL
, (VOID
**)&DevPathFromText
);
230 ASSERT_EFI_ERROR(Status
);
231 DevPath
= DevPathFromText
->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
);