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 for (LoopVar
= 0 ; LoopVar
< (StrLen(Data
) / 2) ; LoopVar
++) {
158 ((UINT8
*)Buffer
)[LoopVar
] = (UINT8
)(HexCharToUintn(Data
[LoopVar
*2]) * 16);
159 ((UINT8
*)Buffer
)[LoopVar
] = (UINT8
)(((UINT8
*)Buffer
)[LoopVar
] + HexCharToUintn(Data
[LoopVar
*2+1]));
161 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, StrLen(Data
) / 2, Buffer
);
162 if (EFI_ERROR(Status
)) {
163 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Status
);
164 ShellStatus
= SHELL_ACCESS_DENIED
;
166 ASSERT(ShellStatus
== SHELL_SUCCESS
);
169 } else if (StrnCmp(Data
, L
"\"", 1) == 0) {
172 Status
= gRT
->GetVariable((CHAR16
*)VariableName
, &Guid
, &Attributes2
, &Size
, Buffer
);
173 if (Status
== EFI_BUFFER_TOO_SMALL
) {
174 Buffer
= AllocateZeroPool(Size
);
175 Status
= gRT
->GetVariable((CHAR16
*)VariableName
, &Guid
, &Attributes2
, &Size
, Buffer
);
177 Attributes
= Attributes2
;
183 Buffer
= AllocateZeroPool(StrSize(Data
) / 2);
184 AsciiSPrint(Buffer
, StrSize(Data
) / 2, "%s", Data
);
185 ((CHAR8
*)Buffer
)[AsciiStrLen(Buffer
)-1] = CHAR_NULL
;
188 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, AsciiStrSize(Buffer
)-sizeof(CHAR8
), Buffer
);
189 if (EFI_ERROR(Status
)) {
190 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Status
);
191 ShellStatus
= SHELL_ACCESS_DENIED
;
193 ASSERT(ShellStatus
== SHELL_SUCCESS
);
195 } else if (StrnCmp(Data
, L
"L\"", 2) == 0) {
201 Buffer
= AllocateZeroPool(StrSize(Data
));
202 UnicodeSPrint(Buffer
, StrSize(Data
), L
"%s", Data
);
203 ((CHAR16
*)Buffer
)[StrLen(Buffer
)-1] = CHAR_NULL
;
205 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, StrSize(Buffer
)-sizeof(CHAR16
), 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
"--", 2) == 0) {
214 // device path in text format
218 Status
= gBS
->LocateProtocol(&gEfiDevicePathFromTextProtocolGuid
, NULL
, (VOID
**)&DevPathFromText
);
219 ASSERT_EFI_ERROR(Status
);
220 DevPath
= DevPathFromText
->ConvertTextToDevicePath(Data
);
221 if (DevPath
== NULL
) {
222 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_DPFT
), gShellDebug1HiiHandle
, Status
);
223 ShellStatus
= SHELL_INVALID_PARAMETER
;
225 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, GetDevicePathSize(DevPath
), DevPath
);
226 if (EFI_ERROR(Status
)) {
227 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Status
);
228 ShellStatus
= SHELL_ACCESS_DENIED
;
230 ASSERT(ShellStatus
== SHELL_SUCCESS
);
234 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, Data
);
235 ShellStatus
= SHELL_INVALID_PARAMETER
;
239 ShellCommandLineFreeVarList (Package
);
242 if (Buffer
!= NULL
) {
246 if (DevPath
!= NULL
) {
250 return (ShellStatus
);