blob: e711d22729e08fb0fcff11913b11176b4d7791b5 [file] [log] [blame]
Vishal Bhoj82c80712015-12-15 21:13:33 +05301/** @file
2 Main file for SetVar shell Debug1 function.
3
4 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14**/
15
16#include "UefiShellDebug1CommandsLib.h"
17
18STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
19 {L"-guid", TypeValue},
20 {L"-bs", TypeFlag},
21 {L"-rt", TypeFlag},
22 {L"-nv", TypeFlag},
23 {NULL, TypeMax}
24 };
25
26/**
27 Function for 'setvar' command.
28
29 @param[in] ImageHandle Handle to the Image (NULL if Internal).
30 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
31**/
32SHELL_STATUS
33EFIAPI
34ShellCommandRunSetVar (
35 IN EFI_HANDLE ImageHandle,
36 IN EFI_SYSTEM_TABLE *SystemTable
37 )
38{
39 EFI_STATUS Status;
40 LIST_ENTRY *Package;
41 CHAR16 *ProblemParam;
42 SHELL_STATUS ShellStatus;
43 CONST CHAR16 *VariableName;
44 CONST CHAR16 *Data;
45 EFI_GUID Guid;
46 CONST CHAR16 *StringGuid;
47 UINT32 Attributes;
48 VOID *Buffer;
49 UINTN Size;
50 UINTN LoopVar;
51 EFI_DEVICE_PATH_PROTOCOL *DevPath;
52
53 ShellStatus = SHELL_SUCCESS;
54 Status = EFI_SUCCESS;
55 Buffer = NULL;
56 Size = 0;
57 Attributes = 0;
58 DevPath = NULL;
59
60 //
61 // initialize the shell lib (we must be in non-auto-init...)
62 //
63 Status = ShellInitialize();
64 ASSERT_EFI_ERROR(Status);
65
66 Status = CommandInit();
67 ASSERT_EFI_ERROR(Status);
68
69 //
70 // parse the command line
71 //
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, L"setvar", ProblemParam);
76 FreePool(ProblemParam);
77 ShellStatus = SHELL_INVALID_PARAMETER;
78 } else {
79 ASSERT(FALSE);
80 }
81 } else {
82 if (ShellCommandLineGetCount(Package) < 2) {
83 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle, L"setvar");
84 ShellStatus = SHELL_INVALID_PARAMETER;
85 } else if (ShellCommandLineGetCount(Package) > 3) {
86 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"setvar");
87 ShellStatus = SHELL_INVALID_PARAMETER;
88 } else {
89 VariableName = ShellCommandLineGetRawValue(Package, 1);
90 Data = ShellCommandLineGetRawValue(Package, 2);
91 if (!ShellCommandLineGetFlag(Package, L"-guid")){
92 CopyGuid(&Guid, &gEfiGlobalVariableGuid);
93 } else {
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_PARAM_INV), gShellDebug1HiiHandle, L"setvar", StringGuid);
98 ShellStatus = SHELL_INVALID_PARAMETER;
99 }
100 }
101 if (Data == NULL || Data[0] != L'=') {
102 //
103 // Display what's there
104 //
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);
109 }
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]);
114 }
115 ShellPrintEx(-1, -1, L"\r\n");
116 } else {
117 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_GET), gShellDebug1HiiHandle, L"setvar", &Guid, VariableName);
118 ShellStatus = SHELL_ACCESS_DENIED;
119 }
120 } else if (StrCmp(Data, L"=") == 0) {
121 //
122 // Delete what's there!
123 //
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, L"setvar", &Guid, VariableName);
127 ShellStatus = SHELL_ACCESS_DENIED;
128 } else {
129 ASSERT(ShellStatus == SHELL_SUCCESS);
130 }
131 } else {
132 //
133 // Change what's there or create a new one.
134 //
135
136 ASSERT(Data[0] == L'=');
137 Data++;
138
139 //
140 // Determine if the variable exists and get the attributes
141 //
142 Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes, &Size, Buffer);
143 if (Status == EFI_BUFFER_TOO_SMALL) {
144 Buffer = AllocateZeroPool(Size);
145 Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes, &Size, Buffer);
146 }
147
148 if (EFI_ERROR(Status) || Buffer == NULL) {
149 //
150 // Creating a new variable. determine attributes from command line.
151 //
152 Attributes = 0;
153 if (ShellCommandLineGetFlag(Package, L"-bs")) {
154 Attributes |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
155 }
156 if (ShellCommandLineGetFlag(Package, L"-rt")) {
157 Attributes |= EFI_VARIABLE_RUNTIME_ACCESS |
158 EFI_VARIABLE_BOOTSERVICE_ACCESS;
159 }
160 if (ShellCommandLineGetFlag(Package, L"-nv")) {
161 Attributes |= EFI_VARIABLE_NON_VOLATILE;
162 }
163 }
164 SHELL_FREE_NON_NULL(Buffer);
165
166 //
167 // What type is the new data.
168 //
169 if (ShellIsHexOrDecimalNumber(Data, TRUE, FALSE)) {
170 if (StrLen(Data) % 2 != 0) {
171 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"setvar", Data);
172 ShellStatus = SHELL_INVALID_PARAMETER;
173 } else {
174 //
175 // arbitrary buffer
176 //
177 Buffer = AllocateZeroPool((StrLen(Data) / 2));
178 if (Buffer == NULL) {
179 Status = EFI_OUT_OF_RESOURCES;
180 } else {
181 for (LoopVar = 0 ; LoopVar < (StrLen(Data) / 2) ; LoopVar++) {
182 ((UINT8*)Buffer)[LoopVar] = (UINT8)(HexCharToUintn(Data[LoopVar*2]) * 16);
183 ((UINT8*)Buffer)[LoopVar] = (UINT8)(((UINT8*)Buffer)[LoopVar] + HexCharToUintn(Data[LoopVar*2+1]));
184 }
185 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, StrLen(Data) / 2, Buffer);
186 }
187 if (EFI_ERROR(Status)) {
188 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, L"setvar", &Guid, VariableName);
189 ShellStatus = SHELL_ACCESS_DENIED;
190 } else {
191 ASSERT(ShellStatus == SHELL_SUCCESS);
192 }
193 }
194 } else if (StrnCmp(Data, L"\"", 1) == 0) {
195 //
196 // ascii text
197 //
198 Data++;
199 Buffer = AllocateZeroPool(StrSize(Data) / 2);
200 if (Buffer == NULL) {
201 Status = EFI_OUT_OF_RESOURCES;
202 } else {
203 AsciiSPrint(Buffer, StrSize(Data) / 2, "%s", Data);
204 ((CHAR8*)Buffer)[AsciiStrLen(Buffer)-1] = CHAR_NULL;
205 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, AsciiStrSize(Buffer)-sizeof(CHAR8), Buffer);
206 }
207 if (EFI_ERROR(Status)) {
208 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, L"setvar", &Guid, VariableName);
209 ShellStatus = SHELL_ACCESS_DENIED;
210 } else {
211 ASSERT(ShellStatus == SHELL_SUCCESS);
212 }
213 } else if (StrnCmp(Data, L"L\"", 2) == 0) {
214 //
215 // ucs2 text
216 //
217 Data++;
218 Data++;
219 Buffer = AllocateZeroPool(StrSize(Data));
220 if (Buffer == NULL) {
221 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellDebug1HiiHandle, L"setvar");
222 ShellStatus = SHELL_OUT_OF_RESOURCES;
223 } else {
224 UnicodeSPrint(Buffer, StrSize(Data), L"%s", Data);
225 ((CHAR16*)Buffer)[StrLen(Buffer)-1] = CHAR_NULL;
226
227 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, StrSize(Buffer)-sizeof(CHAR16), Buffer);
228 if (EFI_ERROR(Status)) {
229 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, L"setvar", &Guid, VariableName);
230 ShellStatus = SHELL_ACCESS_DENIED;
231 } else {
232 ASSERT(ShellStatus == SHELL_SUCCESS);
233 }
234 }
235 } else if (StrnCmp(Data, L"--", 2) == 0) {
236 //
237 // device path in text format
238 //
239 Data++;
240 Data++;
241 DevPath = ConvertTextToDevicePath(Data);
242 if (DevPath == NULL) {
243 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_DPFT), gShellDebug1HiiHandle, L"setvar");
244 ShellStatus = SHELL_INVALID_PARAMETER;
245 } else {
246 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, GetDevicePathSize(DevPath), DevPath);
247 if (EFI_ERROR(Status)) {
248 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, L"setvar", &Guid, VariableName);
249 ShellStatus = SHELL_ACCESS_DENIED;
250 } else {
251 ASSERT(ShellStatus == SHELL_SUCCESS);
252 }
253 }
254 } else {
255 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"setvar", Data);
256 ShellStatus = SHELL_INVALID_PARAMETER;
257 }
258 }
259 }
260 ShellCommandLineFreeVarList (Package);
261 }
262
263 if (Buffer != NULL) {
264 FreePool(Buffer);
265 }
266
267 if (DevPath != NULL) {
268 FreePool(DevPath);
269 }
270
271 return (ShellStatus);
272}