2 Main file for SetVar shell Debug1 function.
4 Copyright (c) 2010, 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
},
27 ShellCommandRunSetVar (
28 IN EFI_HANDLE ImageHandle
,
29 IN EFI_SYSTEM_TABLE
*SystemTable
35 SHELL_STATUS ShellStatus
;
36 CONST CHAR16
*VariableName
;
39 CONST CHAR16
*StringGuid
;
44 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
45 EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL
*DevPathFromText
;
47 ShellStatus
= SHELL_SUCCESS
;
55 // initialize the shell lib (we must be in non-auto-init...)
57 Status
= ShellInitialize();
58 ASSERT_EFI_ERROR(Status
);
60 Status
= CommandInit();
61 ASSERT_EFI_ERROR(Status
);
64 // parse the command line
66 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
67 if (EFI_ERROR(Status
)) {
68 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
69 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, ProblemParam
);
70 FreePool(ProblemParam
);
71 ShellStatus
= SHELL_INVALID_PARAMETER
;
76 if (ShellCommandLineGetCount(Package
) < 2) {
77 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
);
78 ShellStatus
= SHELL_INVALID_PARAMETER
;
79 } else if (ShellCommandLineGetCount(Package
) > 3) {
80 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
);
81 ShellStatus
= SHELL_INVALID_PARAMETER
;
83 VariableName
= ShellCommandLineGetRawValue(Package
, 1);
84 Data
= ShellCommandLineGetRawValue(Package
, 2);
85 if (!ShellCommandLineGetFlag(Package
, L
"-guid")){
86 CopyGuid(&Guid
, &gEfiGlobalVariableGuid
);
88 StringGuid
= ShellCommandLineGetValue(Package
, L
"-guid");
89 Status
= ConvertStringToGuid(StringGuid
, &Guid
);
90 if (EFI_ERROR(Status
)) {
91 ShellStatus
= SHELL_NOT_FOUND
;
96 // Display what's there
98 Status
= gRT
->GetVariable((CHAR16
*)VariableName
, &Guid
, &Attributes
, &Size
, Buffer
);
99 if (Status
== EFI_BUFFER_TOO_SMALL
) {
100 Buffer
= AllocatePool(Size
);
101 Status
= gRT
->GetVariable((CHAR16
*)VariableName
, &Guid
, &Attributes
, &Size
, Buffer
);
103 if (!EFI_ERROR(Status
)&& Buffer
!= NULL
) {
104 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_PRINT
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Size
);
105 for (LoopVar
= 0 ; LoopVar
< Size
; LoopVar
++) {
106 ShellPrintEx(-1, -1, L
"%02x ", ((UINT8
*)Buffer
)[LoopVar
]);
108 ShellPrintEx(-1, -1, L
"\r\n");
110 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_GET
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Status
);
111 ShellStatus
= SHELL_ACCESS_DENIED
;
113 } else if (StrCmp(Data
, L
"=") == 0) {
115 // Delete what's there!
117 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, 0, NULL
);
118 if (EFI_ERROR(Status
)) {
119 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Status
);
120 ShellStatus
= SHELL_ACCESS_DENIED
;
122 ASSERT(ShellStatus
== SHELL_SUCCESS
);
125 if (Data
[0] == L
'=') {
129 // Change what's there
131 if (ShellCommandLineGetFlag(Package
, L
"-bs")) {
132 Attributes
|= EFI_VARIABLE_BOOTSERVICE_ACCESS
;
134 if (ShellCommandLineGetFlag(Package
, L
"-rt")) {
135 Attributes
|= EFI_VARIABLE_RUNTIME_ACCESS
;
137 if (ShellCommandLineGetFlag(Package
, L
"-nv")) {
138 Attributes
|= EFI_VARIABLE_NON_VOLATILE
;
140 if (ShellIsHexOrDecimalNumber(Data
, TRUE
, FALSE
)) {
144 Buffer
= AllocateZeroPool((StrLen(Data
) / 2));
145 for (LoopVar
= 0 ; LoopVar
< (StrLen(Data
) / 2) ; LoopVar
++) {
146 ((UINT8
*)Buffer
)[LoopVar
] = (UINT8
)(HexCharToUintn(Data
[LoopVar
*2]) * 16);
147 ((UINT8
*)Buffer
)[LoopVar
] = (UINT8
)(((UINT8
*)Buffer
)[LoopVar
] + HexCharToUintn(Data
[LoopVar
*2+1]));
149 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, StrLen(Data
) / 2, Buffer
);
150 if (EFI_ERROR(Status
)) {
151 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Status
);
152 ShellStatus
= SHELL_ACCESS_DENIED
;
154 ASSERT(ShellStatus
== SHELL_SUCCESS
);
156 } else if (StrnCmp(Data
, L
"\"", 1) == 0) {
161 Buffer
= AllocateZeroPool(StrSize(Data
) / 2);
162 AsciiSPrint(Buffer
, StrSize(Data
) / 2, "%s", Data
);
163 ((CHAR8
*)Buffer
)[AsciiStrLen(Buffer
)-1] = CHAR_NULL
;
165 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, AsciiStrSize(Buffer
)-sizeof(CHAR8
), 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
);
172 } else if (StrnCmp(Data
, L
"L\"", 2) == 0) {
178 Buffer
= AllocateZeroPool(StrSize(Data
));
179 UnicodeSPrint(Buffer
, StrSize(Data
), L
"%s", Data
);
180 ((CHAR16
*)Buffer
)[StrLen(Buffer
)-1] = CHAR_NULL
;
182 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, StrSize(Buffer
)-sizeof(CHAR16
), Buffer
);
183 if (EFI_ERROR(Status
)) {
184 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Status
);
185 ShellStatus
= SHELL_ACCESS_DENIED
;
187 ASSERT(ShellStatus
== SHELL_SUCCESS
);
189 } else if (StrnCmp(Data
, L
"--", 2) == 0) {
191 // device path in text format
195 Status
= gBS
->LocateProtocol(&gEfiDevicePathFromTextProtocolGuid
, NULL
, (VOID
**)&DevPathFromText
);
196 ASSERT_EFI_ERROR(Status
);
197 DevPath
= DevPathFromText
->ConvertTextToDevicePath(Data
);
198 if (DevPath
== NULL
) {
199 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_DPFT
), gShellDebug1HiiHandle
, Status
);
200 ShellStatus
= SHELL_INVALID_PARAMETER
;
202 Status
= gRT
->SetVariable((CHAR16
*)VariableName
, &Guid
, Attributes
, GetDevicePathSize(DevPath
), DevPath
);
203 if (EFI_ERROR(Status
)) {
204 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SETVAR_ERROR_SET
), gShellDebug1HiiHandle
, &Guid
, VariableName
, Status
);
205 ShellStatus
= SHELL_ACCESS_DENIED
;
207 ASSERT(ShellStatus
== SHELL_SUCCESS
);
211 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, Data
);
212 ShellStatus
= SHELL_INVALID_PARAMETER
;
216 ShellCommandLineFreeVarList (Package
);
219 if (Buffer
!= NULL
) {
223 if (DevPath
!= NULL
) {
227 return (ShellStatus
);