]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/SetVar.c
ShellPkg/SetVar: Fix typo in comments
[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] Package 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 ASSERT (TempData != NULL);
283
284 if (TempData[0] != L'=') {
285 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"setvar", TempData);
286 return EFI_INVALID_PARAMETER;
287 }
288
289 TempData = TempData + 1;
290 Size = 0;
291 Status = ParseParameterData (TempData, NULL, &Size);
292 if (EFI_ERROR (Status)) {
293 if (Status == EFI_BUFFER_TOO_SMALL) {
294 //
295 // We expect return EFI_BUFFER_TOO_SMALL when pass 'NULL' as second parameter to the function ParseParameterData.
296 //
297 TotalSize += Size;
298 } else {
299 if (Status == EFI_INVALID_PARAMETER) {
300 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"setvar", TempData);
301 } else if (Status == EFI_NOT_FOUND) {
302 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_DPFT), gShellDebug1HiiHandle, L"setvar");
303 }
304 return Status;
305 }
306 }
307 }
308
309 *BufferSize = TotalSize;
310 *Buffer = AllocateZeroPool (TotalSize);
311
312 if (*Buffer == NULL) {
313 Status = EFI_OUT_OF_RESOURCES;
314 } else {
315 BufferWalker = *Buffer;
316 for (Index = 2; Index < ShellCommandLineGetCount (Package); Index++) {
317 TempData = ShellCommandLineGetRawValue (Package, Index);
318 TempData = TempData + 1;
319
320 Size = TotalSize;
321 Status = ParseParameterData (TempData, (VOID *)BufferWalker, &Size);
322 if (!EFI_ERROR (Status)) {
323 BufferWalker = BufferWalker + Size;
324 TotalSize = TotalSize - Size;
325 } else {
326 return Status;
327 }
328 }
329 }
330
331 return EFI_SUCCESS;
332 }
333
334 /**
335 Function for 'setvar' command.
336
337 @param[in] ImageHandle Handle to the Image (NULL if Internal).
338 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
339 **/
340 SHELL_STATUS
341 EFIAPI
342 ShellCommandRunSetVar (
343 IN EFI_HANDLE ImageHandle,
344 IN EFI_SYSTEM_TABLE *SystemTable
345 )
346 {
347 EFI_STATUS Status;
348 RETURN_STATUS RStatus;
349 LIST_ENTRY *Package;
350 CHAR16 *ProblemParam;
351 SHELL_STATUS ShellStatus;
352 CONST CHAR16 *VariableName;
353 EFI_GUID Guid;
354 CONST CHAR16 *StringGuid;
355 UINT32 Attributes;
356 VOID *Buffer;
357 UINTN Size;
358 UINTN LoopVar;
359
360 ShellStatus = SHELL_SUCCESS;
361 Status = EFI_SUCCESS;
362 Buffer = NULL;
363 Size = 0;
364 Attributes = 0;
365
366 //
367 // initialize the shell lib (we must be in non-auto-init...)
368 //
369 Status = ShellInitialize();
370 ASSERT_EFI_ERROR(Status);
371
372 Status = CommandInit();
373 ASSERT_EFI_ERROR(Status);
374
375 //
376 // parse the command line
377 //
378 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
379 if (EFI_ERROR(Status)) {
380 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
381 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"setvar", ProblemParam);
382 FreePool(ProblemParam);
383 ShellStatus = SHELL_INVALID_PARAMETER;
384 } else {
385 ASSERT(FALSE);
386 }
387 } else {
388 if (ShellCommandLineGetCount(Package) < 2) {
389 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle, L"setvar");
390 ShellStatus = SHELL_INVALID_PARAMETER;
391 } else {
392 VariableName = ShellCommandLineGetRawValue(Package, 1);
393 if (!ShellCommandLineGetFlag(Package, L"-guid")){
394 CopyGuid(&Guid, &gEfiGlobalVariableGuid);
395 } else {
396 StringGuid = ShellCommandLineGetValue(Package, L"-guid");
397 RStatus = StrToGuid (StringGuid, &Guid);
398 if (RETURN_ERROR (RStatus) || (StringGuid[GUID_STRING_LENGTH] != L'\0')) {
399 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"setvar", StringGuid);
400 ShellStatus = SHELL_INVALID_PARAMETER;
401 }
402 }
403
404 if (ShellCommandLineGetCount(Package) == 2) {
405 //
406 // Display
407 //
408 Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes, &Size, Buffer);
409 if (Status == EFI_BUFFER_TOO_SMALL) {
410 Buffer = AllocateZeroPool(Size);
411 Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes, &Size, Buffer);
412 }
413 if (!EFI_ERROR(Status) && Buffer != NULL) {
414 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_SETVAR_PRINT), gShellDebug1HiiHandle, &Guid, VariableName, Size);
415 for (LoopVar = 0; LoopVar < Size; LoopVar++) {
416 ShellPrintEx(-1, -1, L"%02x ", ((UINT8*)Buffer)[LoopVar]);
417 }
418 ShellPrintEx(-1, -1, L"\r\n");
419 } else {
420 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_GET), gShellDebug1HiiHandle, L"setvar", &Guid, VariableName);
421 ShellStatus = SHELL_ACCESS_DENIED;
422 }
423 } else {
424 //
425 // Create, Delete or Modify.
426 //
427 Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes, &Size, Buffer);
428 if (Status == EFI_BUFFER_TOO_SMALL) {
429 Buffer = AllocateZeroPool(Size);
430 Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes, &Size, Buffer);
431 }
432 if (EFI_ERROR(Status) || Buffer == NULL) {
433 //
434 // Creating a new variable. determine attributes from command line.
435 //
436 Attributes = 0;
437 if (ShellCommandLineGetFlag(Package, L"-bs")) {
438 Attributes |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
439 }
440 if (ShellCommandLineGetFlag(Package, L"-rt")) {
441 Attributes |= EFI_VARIABLE_RUNTIME_ACCESS |
442 EFI_VARIABLE_BOOTSERVICE_ACCESS;
443 }
444 if (ShellCommandLineGetFlag(Package, L"-nv")) {
445 Attributes |= EFI_VARIABLE_NON_VOLATILE;
446 }
447 }
448 SHELL_FREE_NON_NULL(Buffer);
449
450 Size = 0;
451 Status = GetVariableDataFromParameter(Package, (UINT8 **)&Buffer, &Size);
452 if (!EFI_ERROR(Status)) {
453 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, Size, Buffer);
454 }
455 if (EFI_ERROR(Status)) {
456 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, L"setvar", &Guid, VariableName);
457 ShellStatus = SHELL_ACCESS_DENIED;
458 } else {
459 ASSERT(ShellStatus == SHELL_SUCCESS);
460 }
461 }
462 }
463 ShellCommandLineFreeVarList (Package);
464 }
465
466 if (Buffer != NULL) {
467 FreePool(Buffer);
468 }
469
470 return (ShellStatus);
471 }