]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/SetVar.c
ShellPkg/setvar: Support data format in Shell 2.2 spec
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / SetVar.c
1 /** @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 - 2017, 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
18 STATIC 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 typedef enum {
27 DataTypeHexNumber = 0,
28 DataTypeHexArray = 1,
29 DataTypeAscii = 2,
30 DataTypeUnicode = 3,
31 DataTypeDevicePath = 4,
32 DataTypeUnKnow = 5
33 } DATA_TYPE;
34
35 typedef union {
36 UINT8 HexNumber8;
37 UINT16 HexNumber16;
38 UINT32 HexNumber32;
39 UINT64 HexNumber64;
40 } HEX_NUMBER;
41
42 /**
43 Check if the input is a (potentially empty) string of hexadecimal nibbles.
44
45 @param[in] String The CHAR16 string to check.
46
47 @retval FALSE A character has been found in String for which
48 ShellIsHexaDecimalDigitCharacter() returned FALSE.
49
50 @retval TRUE Otherwise. (Note that this covers the case when String is
51 empty.)
52 **/
53 BOOLEAN
54 IsStringOfHexNibbles (
55 IN CONST CHAR16 *String
56 )
57 {
58 CONST CHAR16 *Pos;
59
60 for (Pos = String; *Pos != L'\0'; ++Pos) {
61 if (!ShellIsHexaDecimalDigitCharacter (*Pos)) {
62 return FALSE;
63 }
64 }
65 return TRUE;
66 }
67
68 /**
69 Function to check the TYPE of Data.
70
71 @param[in] Data The Data to be check.
72
73 @retval DATA_TYPE The TYPE of Data.
74 **/
75 DATA_TYPE
76 TestDataType (
77 IN CONST CHAR16 *Data
78 )
79 {
80 if (Data[0] == L'0' && (Data[1] == L'x' || Data[1] == L'X')) {
81 if (IsStringOfHexNibbles (Data+2) && StrLen (Data + 2) <= 16) {
82 return DataTypeHexNumber;
83 } else {
84 return DataTypeUnKnow;
85 }
86 } else if (Data[0] == L'H') {
87 if (IsStringOfHexNibbles (Data + 1) && StrLen (Data + 1) % 2 == 0) {
88 return DataTypeHexArray;
89 } else {
90 return DataTypeUnKnow;
91 }
92 } else if (Data[0] == L'S') {
93 return DataTypeAscii;
94 } else if (Data[0] == L'L') {
95 return DataTypeUnicode;
96 } else if (Data[0] == L'P' || StrnCmp (Data, L"--", 2) == 0) {
97 return DataTypeDevicePath;
98 }
99
100 if (IsStringOfHexNibbles (Data) && StrLen (Data) % 2 == 0) {
101 return DataTypeHexArray;
102 }
103
104 return DataTypeAscii;
105 }
106
107 /**
108 Function to parse the Data by the type of Data, and save in the Buffer.
109
110 @param[in] Data A pointer to a buffer to be parsed.
111 @param[out] Buffer A pointer to a buffer to hold the return data.
112 @param[in,out] BufferSize On input, indicates the size of Buffer in bytes.
113 On output,indicates the size of data return in Buffer.
114 Or the size in bytes of the buffer needed to obtain.
115
116 @retval EFI_INVALID_PARAMETER The Buffer or BufferSize is NULL.
117 @retval EFI_BUFFER_TOO_SMALL The Buffer is too small to hold the data.
118 @retval EFI_OUT_OF_RESOURCES A memory allcation failed.
119 @retval EFI_SUCCESS The Data parsed successful and save in the Buffer.
120 **/
121 EFI_STATUS
122 ParseParameterData (
123 IN CONST CHAR16 *Data,
124 OUT VOID *Buffer,
125 IN OUT UINTN *BufferSize
126 )
127 {
128 UINT64 HexNumber;
129 UINTN HexNumberLen;
130 UINTN Size;
131 CHAR8 *AsciiBuffer;
132 DATA_TYPE DataType;
133 EFI_DEVICE_PATH_PROTOCOL *DevPath;
134 EFI_STATUS Status;
135
136 HexNumber = 0;
137 HexNumberLen = 0;
138 Size = 0;
139 AsciiBuffer = NULL;
140 DevPath = NULL;
141 Status = EFI_SUCCESS;
142
143 if (Data == NULL || BufferSize == NULL) {
144 return EFI_INVALID_PARAMETER;
145 }
146
147 DataType = TestDataType (Data);
148 if (DataType == DataTypeHexNumber) {
149 //
150 // hex number
151 //
152 StrHexToUint64S (Data + 2, NULL, &HexNumber);
153 HexNumberLen = StrLen (Data + 2);
154 if (HexNumberLen >= 1 && HexNumberLen <= 2) {
155 Size = 1;
156 } else if (HexNumberLen >= 3 && HexNumberLen <= 4) {
157 Size = 2;
158 } else if (HexNumberLen >= 5 && HexNumberLen <= 8) {
159 Size = 4;
160 } else if (HexNumberLen >= 9 && HexNumberLen <= 16) {
161 Size = 8;
162 }
163 if (Buffer != NULL && *BufferSize >= Size) {
164 CopyMem(Buffer, (VOID *)&HexNumber, Size);
165 } else {
166 Status = EFI_BUFFER_TOO_SMALL;
167 }
168 *BufferSize = Size;
169 } else if (DataType == DataTypeHexArray) {
170 //
171 // hex array
172 //
173 if (*Data == L'H') {
174 Data = Data + 1;
175 }
176
177 Size = StrLen (Data) / 2;
178 if (Buffer != NULL && *BufferSize >= Size) {
179 StrHexToBytes(Data, StrLen (Data), (UINT8 *)Buffer, Size);
180 } else {
181 Status = EFI_BUFFER_TOO_SMALL;
182 }
183 *BufferSize = Size;
184 } else if (DataType == DataTypeAscii) {
185 //
186 // ascii text
187 //
188 if (*Data == L'S') {
189 Data = Data + 1;
190 }
191 AsciiBuffer = AllocateZeroPool (StrSize (Data) / 2);
192 if (AsciiBuffer == NULL) {
193 Status = EFI_OUT_OF_RESOURCES;
194 } else {
195 AsciiSPrint (AsciiBuffer, StrSize (Data) / 2, "%s", (CHAR8 *)Data);
196
197 Size = StrSize (Data) / 2 - 1;
198 if (Buffer != NULL && *BufferSize >= Size) {
199 CopyMem (Buffer, AsciiBuffer, Size);
200 } else {
201 Status = EFI_BUFFER_TOO_SMALL;
202 }
203 *BufferSize = Size;
204 }
205 SHELL_FREE_NON_NULL (AsciiBuffer);
206 } else if (DataType == DataTypeUnicode) {
207 //
208 // unicode text
209 //
210 if (*Data == L'L') {
211 Data = Data + 1;
212 }
213 Size = StrSize (Data) - sizeof (CHAR16);
214 if (Buffer != NULL && *BufferSize >= Size) {
215 CopyMem (Buffer, Data, Size);
216 } else {
217 Status = EFI_BUFFER_TOO_SMALL;
218 }
219 *BufferSize = Size;
220 } else if (DataType == DataTypeDevicePath) {
221 if (*Data == L'P') {
222 Data = Data + 1;
223 } else if (StrnCmp (Data, L"--", 2) == 0) {
224 Data = Data + 2;
225 }
226 DevPath = ConvertTextToDevicePath (Data);
227 if (DevPath == NULL) {
228 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_DPFT), gShellDebug1HiiHandle, L"setvar");
229 Status = EFI_INVALID_PARAMETER;
230 } else {
231 Size = GetDevicePathSize (DevPath);
232 if (Buffer != NULL && *BufferSize >= Size) {
233 CopyMem (Buffer, DevPath, Size);
234 } else {
235 Status = EFI_BUFFER_TOO_SMALL;
236 }
237 *BufferSize = Size;
238 }
239 SHELL_FREE_NON_NULL (DevPath);
240 } else {
241 Status = EFI_INVALID_PARAMETER;
242 }
243
244 return Status;
245 }
246
247 /**
248 Function to get each data from parameters.
249
250 @param[in] Pacakge The package of checked values.
251 @param[out] Buffer A pointer to a buffer to hold the return data.
252 @param[out] BufferSize Indicates the size of data in bytes return in Buffer.
253
254 @retval EFI_INVALID_PARAMETER Buffer or BufferSize is NULL.
255 @retval EFI_OUT_OF_RESOURCES A memory allcation failed.
256 @retval EFI_SUCCESS Get each parameter data was successful.
257 **/
258 EFI_STATUS
259 GetVariableDataFromParameter (
260 IN CONST LIST_ENTRY *Package,
261 OUT UINT8 **Buffer,
262 OUT UINTN *BufferSize
263 )
264 {
265 CONST CHAR16 *TempData;
266 UINTN Index;
267 UINTN TotalSize;
268 UINTN Size;
269 UINT8 *BufferWalker;
270 EFI_STATUS Status;
271
272 TotalSize = 0;
273 Size = 0;
274 Status = EFI_SUCCESS;
275
276 if (BufferSize == NULL || Buffer == NULL || ShellCommandLineGetCount (Package) < 3) {
277 return EFI_INVALID_PARAMETER;
278 }
279
280 for (Index = 2; Index < ShellCommandLineGetCount (Package); Index++) {
281 TempData = ShellCommandLineGetRawValue (Package, Index);
282
283 if (TempData[0] != L'=') {
284 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"setvar", TempData);
285 return EFI_INVALID_PARAMETER;
286 }
287
288 TempData = TempData + 1;
289 Size = 0;
290 Status = ParseParameterData (TempData, NULL, &Size);
291 if (EFI_ERROR (Status)) {
292 if (Status == EFI_BUFFER_TOO_SMALL) {
293 //
294 // We expect return EFI_BUFFER_TOO_SMALL when pass 'NULL' as second parameter to the function ParseParameterData.
295 //
296 TotalSize += Size;
297 } else {
298 if (Status == EFI_INVALID_PARAMETER) {
299 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"setvar", TempData);
300 } else if (Status == EFI_NOT_FOUND) {
301 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_DPFT), gShellDebug1HiiHandle, L"setvar");
302 }
303 return Status;
304 }
305 }
306 }
307
308 *BufferSize = TotalSize;
309 *Buffer = AllocateZeroPool (TotalSize);
310
311 if (*Buffer == NULL) {
312 Status = EFI_OUT_OF_RESOURCES;
313 } else {
314 BufferWalker = *Buffer;
315 for (Index = 2; Index < ShellCommandLineGetCount (Package); Index++) {
316 TempData = ShellCommandLineGetRawValue (Package, Index);
317 TempData = TempData + 1;
318
319 Size = TotalSize;
320 Status = ParseParameterData (TempData, (VOID *)BufferWalker, &Size);
321 if (!EFI_ERROR (Status)) {
322 BufferWalker = BufferWalker + Size;
323 TotalSize = TotalSize - Size;
324 } else {
325 return Status;
326 }
327 }
328 }
329
330 return EFI_SUCCESS;
331 }
332
333 /**
334 Function for 'setvar' command.
335
336 @param[in] ImageHandle Handle to the Image (NULL if Internal).
337 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
338 **/
339 SHELL_STATUS
340 EFIAPI
341 ShellCommandRunSetVar (
342 IN EFI_HANDLE ImageHandle,
343 IN EFI_SYSTEM_TABLE *SystemTable
344 )
345 {
346 EFI_STATUS Status;
347 RETURN_STATUS RStatus;
348 LIST_ENTRY *Package;
349 CHAR16 *ProblemParam;
350 SHELL_STATUS ShellStatus;
351 CONST CHAR16 *VariableName;
352 EFI_GUID Guid;
353 CONST CHAR16 *StringGuid;
354 UINT32 Attributes;
355 VOID *Buffer;
356 UINTN Size;
357 UINTN LoopVar;
358
359 ShellStatus = SHELL_SUCCESS;
360 Status = EFI_SUCCESS;
361 Buffer = NULL;
362 Size = 0;
363 Attributes = 0;
364
365 //
366 // initialize the shell lib (we must be in non-auto-init...)
367 //
368 Status = ShellInitialize();
369 ASSERT_EFI_ERROR(Status);
370
371 Status = CommandInit();
372 ASSERT_EFI_ERROR(Status);
373
374 //
375 // parse the command line
376 //
377 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
378 if (EFI_ERROR(Status)) {
379 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
380 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"setvar", ProblemParam);
381 FreePool(ProblemParam);
382 ShellStatus = SHELL_INVALID_PARAMETER;
383 } else {
384 ASSERT(FALSE);
385 }
386 } else {
387 if (ShellCommandLineGetCount(Package) < 2) {
388 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle, L"setvar");
389 ShellStatus = SHELL_INVALID_PARAMETER;
390 } else {
391 VariableName = ShellCommandLineGetRawValue(Package, 1);
392 if (!ShellCommandLineGetFlag(Package, L"-guid")){
393 CopyGuid(&Guid, &gEfiGlobalVariableGuid);
394 } else {
395 StringGuid = ShellCommandLineGetValue(Package, L"-guid");
396 RStatus = StrToGuid (StringGuid, &Guid);
397 if (RETURN_ERROR (RStatus) || (StringGuid[GUID_STRING_LENGTH] != L'\0')) {
398 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"setvar", StringGuid);
399 ShellStatus = SHELL_INVALID_PARAMETER;
400 }
401 }
402
403 if (ShellCommandLineGetCount(Package) == 2) {
404 //
405 // Display
406 //
407 Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes, &Size, Buffer);
408 if (Status == EFI_BUFFER_TOO_SMALL) {
409 Buffer = AllocateZeroPool(Size);
410 Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes, &Size, Buffer);
411 }
412 if (!EFI_ERROR(Status) && Buffer != NULL) {
413 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_SETVAR_PRINT), gShellDebug1HiiHandle, &Guid, VariableName, Size);
414 for (LoopVar = 0; LoopVar < Size; LoopVar++) {
415 ShellPrintEx(-1, -1, L"%02x ", ((UINT8*)Buffer)[LoopVar]);
416 }
417 ShellPrintEx(-1, -1, L"\r\n");
418 } else {
419 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_GET), gShellDebug1HiiHandle, L"setvar", &Guid, VariableName);
420 ShellStatus = SHELL_ACCESS_DENIED;
421 }
422 } else {
423 //
424 // Create, Delete or Modify.
425 //
426 Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes, &Size, Buffer);
427 if (Status == EFI_BUFFER_TOO_SMALL) {
428 Buffer = AllocateZeroPool(Size);
429 Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes, &Size, Buffer);
430 }
431 if (EFI_ERROR(Status) || Buffer == NULL) {
432 //
433 // Creating a new variable. determine attributes from command line.
434 //
435 Attributes = 0;
436 if (ShellCommandLineGetFlag(Package, L"-bs")) {
437 Attributes |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
438 }
439 if (ShellCommandLineGetFlag(Package, L"-rt")) {
440 Attributes |= EFI_VARIABLE_RUNTIME_ACCESS |
441 EFI_VARIABLE_BOOTSERVICE_ACCESS;
442 }
443 if (ShellCommandLineGetFlag(Package, L"-nv")) {
444 Attributes |= EFI_VARIABLE_NON_VOLATILE;
445 }
446 }
447 SHELL_FREE_NON_NULL(Buffer);
448
449 Size = 0;
450 Status = GetVariableDataFromParameter(Package, (UINT8 **)&Buffer, &Size);
451 if (!EFI_ERROR(Status)) {
452 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, Size, Buffer);
453 }
454 if (EFI_ERROR(Status)) {
455 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, L"setvar", &Guid, VariableName);
456 ShellStatus = SHELL_ACCESS_DENIED;
457 } else {
458 ASSERT(ShellStatus == SHELL_SUCCESS);
459 }
460 }
461 }
462 ShellCommandLineFreeVarList (Package);
463 }
464
465 if (Buffer != NULL) {
466 FreePool(Buffer);
467 }
468
469 return (ShellStatus);
470 }