Add the feature "dmpstore -l" and "dmpstore -s".
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / DmpStore.c
1 /** @file
2 Main file for DmpStore shell Debug1 function.
3
4 Copyright (c) 2005 - 2014, 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
9
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.
12
13 **/
14
15 #include "UefiShellDebug1CommandsLib.h"
16
17 typedef enum {
18 DmpStoreDisplay,
19 DmpStoreDelete,
20 DmpStoreSave,
21 DmpStoreLoad
22 } DMP_STORE_TYPE;
23
24 typedef struct {
25 UINT32 Signature;
26 CHAR16 *Name;
27 EFI_GUID Guid;
28 UINT32 Attributes;
29 UINT32 DataSize;
30 UINT8 *Data;
31 LIST_ENTRY Link;
32 } DMP_STORE_VARIABLE;
33
34 #define DMP_STORE_VARIABLE_SIGNATURE SIGNATURE_32 ('_', 'd', 's', 's')
35
36 /**
37 Base on the input attribute value to return the attribute string.
38
39 @param[in] Atts The input attribute value
40
41 @retval The attribute string info.
42 **/
43 CHAR16 *
44 EFIAPI
45 GetAttrType (
46 IN CONST UINT32 Atts
47 )
48 {
49 UINTN BufLen;
50 CHAR16 *RetString;
51
52 BufLen = 0;
53 RetString = NULL;
54
55 if ((Atts & EFI_VARIABLE_NON_VOLATILE) != 0) {
56 StrnCatGrow (&RetString, &BufLen, L"+NV", 0);
57 }
58 if ((Atts & EFI_VARIABLE_RUNTIME_ACCESS) != 0) {
59 StrnCatGrow (&RetString, &BufLen, L"+RS+BS", 0);
60 } else if ((Atts & EFI_VARIABLE_BOOTSERVICE_ACCESS) != 0) {
61 StrnCatGrow (&RetString, &BufLen, L"+BS", 0);
62 }
63 if ((Atts & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {
64 StrnCatGrow (&RetString, &BufLen, L"+HR", 0);
65 }
66 if ((Atts & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {
67 StrnCatGrow (&RetString, &BufLen, L"+AW", 0);
68 }
69 if ((Atts & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) {
70 StrnCatGrow (&RetString, &BufLen, L"+AT", 0);
71 }
72
73 if (RetString == NULL) {
74 RetString = StrnCatGrow(&RetString, &BufLen, L"Invalid", 0);
75 }
76
77 if ((RetString != NULL) && (RetString[0] == L'+')) {
78 CopyMem(RetString, RetString + 1, StrSize(RetString + 1));
79 }
80
81 return RetString;
82 }
83
84 /**
85 Load the variable data from file and set to variable data base.
86
87 @param[in] FileHandle The file to be read.
88 @param[in] Name The name of the variables to be loaded.
89 @param[in] Guid The guid of the variables to be loaded.
90 @param[out] Found TRUE when at least one variable was loaded and set.
91
92 @retval EFI_VOLUME_CORRUPTED The file is in bad format.
93 @retval EFI_OUT_OF_RESOURCES There is not enough memory to perform the operation.
94 @retval EFI_SUCCESS Successfully load and set the variables.
95 **/
96 EFI_STATUS
97 LoadVariablesFromFile (
98 IN SHELL_FILE_HANDLE FileHandle,
99 IN CONST CHAR16 *Name,
100 IN CONST EFI_GUID *Guid,
101 OUT BOOLEAN *Found
102 )
103 {
104 EFI_STATUS Status;
105 UINT32 NameSize;
106 UINT32 DataSize;
107 UINTN BufferSize;
108 UINTN RemainingSize;
109 UINT64 Position;
110 UINT64 FileSize;
111 LIST_ENTRY List;
112 DMP_STORE_VARIABLE *Variable;
113 LIST_ENTRY *Link;
114 CHAR16 *Attributes;
115 UINT8 *Buffer;
116 UINT32 Crc32;
117
118 Status = ShellGetFileSize (FileHandle, &FileSize);
119 if (EFI_ERROR (Status)) {
120 return Status;
121 }
122
123 InitializeListHead (&List);
124
125 Position = 0;
126 while (Position < FileSize) {
127 //
128 // NameSize
129 //
130 BufferSize = sizeof (NameSize);
131 Status = ShellReadFile (FileHandle, &BufferSize, &NameSize);
132 if (EFI_ERROR (Status) || (BufferSize != sizeof (NameSize))) {
133 Status = EFI_VOLUME_CORRUPTED;
134 break;
135 }
136
137 //
138 // DataSize
139 //
140 BufferSize = sizeof (DataSize);
141 Status = ShellReadFile (FileHandle, &BufferSize, &DataSize);
142 if (EFI_ERROR (Status) || (BufferSize != sizeof (DataSize))) {
143 Status = EFI_VOLUME_CORRUPTED;
144 break;
145 }
146
147 //
148 // Name, Guid, Attributes, Data, Crc32
149 //
150 RemainingSize = NameSize + sizeof (EFI_GUID) + sizeof (UINT32) + DataSize + sizeof (Crc32);
151 BufferSize = sizeof (NameSize) + sizeof (DataSize) + RemainingSize;
152 Buffer = AllocatePool (BufferSize);
153 if (Buffer == NULL) {
154 Status = EFI_OUT_OF_RESOURCES;
155 break;
156 }
157 BufferSize = RemainingSize;
158 Status = ShellReadFile (FileHandle, &BufferSize, (UINT32 *) Buffer + 2);
159 if (EFI_ERROR (Status) || (BufferSize != RemainingSize)) {
160 Status = EFI_VOLUME_CORRUPTED;
161 FreePool (Buffer);
162 break;
163 }
164
165 //
166 // Check Crc32
167 //
168 * (UINT32 *) Buffer = NameSize;
169 * ((UINT32 *) Buffer + 1) = DataSize;
170 BufferSize = RemainingSize + sizeof (NameSize) + sizeof (DataSize) - sizeof (Crc32);
171 gBS->CalculateCrc32 (
172 Buffer,
173 BufferSize,
174 &Crc32
175 );
176 if (Crc32 != * (UINT32 *) (Buffer + BufferSize)) {
177 FreePool (Buffer);
178 Status = EFI_VOLUME_CORRUPTED;
179 break;
180 }
181
182 Position += BufferSize + sizeof (Crc32);
183
184 Variable = AllocateZeroPool (sizeof (*Variable) + NameSize + DataSize);
185 if (Variable == NULL) {
186 FreePool (Buffer);
187 Status = EFI_OUT_OF_RESOURCES;
188 break;
189 }
190 Variable->Signature = DMP_STORE_VARIABLE_SIGNATURE;
191 Variable->Name = (CHAR16 *) (Variable + 1);
192 Variable->DataSize = DataSize;
193 Variable->Data = (UINT8 *) Variable->Name + NameSize;
194 CopyMem (Variable->Name, Buffer + sizeof (NameSize) + sizeof (DataSize), NameSize);
195 CopyMem (&Variable->Guid, Buffer + sizeof (NameSize) + sizeof (DataSize) + NameSize, sizeof (EFI_GUID));
196 CopyMem (&Variable->Attributes, Buffer + sizeof (NameSize) + sizeof (DataSize) + NameSize + sizeof (EFI_GUID), sizeof (UINT32));
197 CopyMem (Variable->Data, Buffer + sizeof (NameSize) + sizeof (DataSize) + NameSize + sizeof (EFI_GUID) + sizeof (UINT32), DataSize);
198
199 InsertTailList (&List, &Variable->Link);
200 FreePool (Buffer);
201 }
202
203 if ((Position != FileSize) || EFI_ERROR (Status)) {
204 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_LOAD_BAD_FILE), gShellDebug1HiiHandle);
205 Status = EFI_VOLUME_CORRUPTED;
206 }
207
208 for ( Link = GetFirstNode (&List)
209 ; !IsNull (&List, Link) && !EFI_ERROR (Status)
210 ; Link = GetNextNode (&List, Link)
211 ) {
212 Variable = CR (Link, DMP_STORE_VARIABLE, Link, DMP_STORE_VARIABLE_SIGNATURE);
213
214 if (((Name == NULL) || gUnicodeCollation->MetaiMatch (gUnicodeCollation, Variable->Name, (CHAR16 *) Name)) &&
215 ((Guid == NULL) || CompareGuid (&Variable->Guid, Guid))
216 ) {
217 Attributes = GetAttrType (Variable->Attributes);
218 ShellPrintHiiEx (
219 -1, -1, NULL, STRING_TOKEN(STR_DMPSTORE_HEADER_LINE), gShellDebug1HiiHandle,
220 Attributes, &Variable->Guid, Variable->Name, Variable->DataSize
221 );
222 SHELL_FREE_NON_NULL(Attributes);
223
224 *Found = TRUE;
225 Status = gRT->SetVariable (
226 Variable->Name,
227 &Variable->Guid,
228 Variable->Attributes,
229 Variable->DataSize,
230 Variable->Data
231 );
232 if (EFI_ERROR (Status)) {
233 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_LOAD_GEN_FAIL), gShellDebug1HiiHandle, Variable->Name, Status);
234 //
235 // continue set variable upon failure
236 //
237 Status = EFI_SUCCESS;
238 }
239 }
240 }
241
242 for (Link = GetFirstNode (&List); !IsNull (&List, Link); ) {
243 Variable = CR (Link, DMP_STORE_VARIABLE, Link, DMP_STORE_VARIABLE_SIGNATURE);
244 Link = RemoveEntryList (&Variable->Link);
245 FreePool (Variable);
246 }
247
248 return Status;
249 }
250
251 /**
252 Append one variable to file.
253
254 @param[in] FileHandle The file to be appended.
255 @param[in] Name The variable name.
256 @param[in] Guid The variable GUID.
257 @param[in] Attributes The variable attributes.
258 @param[in] DataSize The variable data size.
259 @param[in] Data The variable data.
260
261 @retval EFI_OUT_OF_RESOURCES There is not enough memory to perform the operation.
262 @retval EFI_SUCCESS The variable is appended to file successfully.
263 @retval others Failed to append the variable to file.
264 **/
265 EFI_STATUS
266 AppendSingleVariableToFile (
267 IN SHELL_FILE_HANDLE FileHandle,
268 IN CONST CHAR16 *Name,
269 IN CONST EFI_GUID *Guid,
270 IN UINT32 Attributes,
271 IN UINT32 DataSize,
272 IN CONST UINT8 *Data
273 )
274 {
275 UINT32 NameSize;
276 UINT8 *Buffer;
277 UINT8 *Ptr;
278 UINTN BufferSize;
279 EFI_STATUS Status;
280
281 NameSize = (UINT32) StrSize (Name);
282 BufferSize = sizeof (NameSize) + sizeof (DataSize)
283 + sizeof (*Guid)
284 + sizeof (Attributes)
285 + NameSize + DataSize
286 + sizeof (UINT32);
287
288 Buffer = AllocatePool (BufferSize);
289 if (Buffer == NULL) {
290 return EFI_OUT_OF_RESOURCES;
291 }
292
293 Ptr = Buffer;
294 //
295 // NameSize and DataSize
296 //
297 * (UINT32 *) Ptr = NameSize;
298 Ptr += sizeof (NameSize);
299 *(UINT32 *) Ptr = DataSize;
300 Ptr += sizeof (DataSize);
301
302 //
303 // Name
304 //
305 CopyMem (Ptr, Name, NameSize);
306 Ptr += NameSize;
307
308 //
309 // Guid
310 //
311 CopyMem (Ptr, Guid, sizeof (*Guid));
312 Ptr += sizeof (*Guid);
313
314 //
315 // Attributes
316 //
317 * (UINT32 *) Ptr = Attributes;
318 Ptr += sizeof (Attributes);
319
320 //
321 // Data
322 //
323 CopyMem (Ptr, Data, DataSize);
324 Ptr += DataSize;
325
326 //
327 // Crc32
328 //
329 gBS->CalculateCrc32 (Buffer, (UINTN) (Ptr - Buffer), (UINT32 *) Ptr);
330
331 Status = ShellWriteFile (FileHandle, &BufferSize, Buffer);
332 FreePool (Buffer);
333
334 if (!EFI_ERROR (Status) &&
335 (BufferSize != sizeof (NameSize) + sizeof (DataSize) + sizeof (*Guid) + sizeof (Attributes) + NameSize + DataSize)
336 ) {
337 Status = EFI_DEVICE_ERROR;
338 }
339
340 return Status;
341 }
342
343 /**
344 Recursive function to display or delete variables.
345
346 This function will call itself to create a stack-based list of allt he variables to process,
347 then fromt he last to the first, they will do either printing or deleting.
348
349 This is necessary since once a delete happens GetNextVariableName() will work.
350
351 @param[in] Name The variable name of the EFI variable (or NULL).
352 @param[in] Guid The GUID of the variable set (or NULL).
353 @param[in] Type The operation type.
354 @param[in] FileHandle The file to operate on (or NULL).
355 @param[in] PrevName The previous variable name from GetNextVariableName. L"" to start.
356 @param[in] FoundVarGuid The previous GUID from GetNextVariableName. ignored at start.
357 @param[in] FoundOne If a VariableName or Guid was specified and one was printed or
358 deleted, then set this to TRUE, otherwise ignored.
359
360 @retval SHELL_SUCCESS The operation was successful.
361 @retval SHELL_OUT_OF_RESOURCES A memorty allocation failed.
362 @retval SHELL_ABORTED The abort message was received.
363 @retval SHELL_DEVICE_ERROR UEFI Variable Services returned an error.
364 @retval SHELL_NOT_FOUND the Name/Guid pair could not be found.
365 **/
366 SHELL_STATUS
367 EFIAPI
368 CascadeProcessVariables (
369 IN CONST CHAR16 *Name OPTIONAL,
370 IN CONST EFI_GUID *Guid OPTIONAL,
371 IN DMP_STORE_TYPE Type,
372 IN EFI_FILE_PROTOCOL *FileHandle OPTIONAL,
373 IN CONST CHAR16 * CONST PrevName,
374 IN EFI_GUID FoundVarGuid,
375 IN BOOLEAN *FoundOne
376 )
377 {
378 EFI_STATUS Status;
379 CHAR16 *FoundVarName;
380 UINT8 *DataBuffer;
381 UINTN DataSize;
382 UINT32 Atts;
383 SHELL_STATUS ShellStatus;
384 UINTN NameSize;
385 CHAR16 *RetString;
386
387 if (ShellGetExecutionBreakFlag()) {
388 return (SHELL_ABORTED);
389 }
390
391 NameSize = 0;
392 FoundVarName = NULL;
393
394 if (PrevName!=NULL) {
395 StrnCatGrow(&FoundVarName, &NameSize, PrevName, 0);
396 } else {
397 FoundVarName = AllocateZeroPool(sizeof(CHAR16));
398 }
399
400 Status = gRT->GetNextVariableName (&NameSize, FoundVarName, &FoundVarGuid);
401 if (Status == EFI_BUFFER_TOO_SMALL) {
402 SHELL_FREE_NON_NULL(FoundVarName);
403 FoundVarName = AllocateZeroPool (NameSize);
404 if (FoundVarName != NULL) {
405 if (PrevName != NULL) {
406 StrCpy(FoundVarName, PrevName);
407 }
408
409 Status = gRT->GetNextVariableName (&NameSize, FoundVarName, &FoundVarGuid);
410 } else {
411 Status = EFI_OUT_OF_RESOURCES;
412 }
413 }
414
415 //
416 // No more is fine.
417 //
418 if (Status == EFI_NOT_FOUND) {
419 SHELL_FREE_NON_NULL(FoundVarName);
420 return (SHELL_SUCCESS);
421 } else if (EFI_ERROR(Status)) {
422 SHELL_FREE_NON_NULL(FoundVarName);
423 return (SHELL_DEVICE_ERROR);
424 }
425
426 //
427 // Recurse to the next iteration. We know "our" variable's name.
428 //
429 ShellStatus = CascadeProcessVariables(Name, Guid, Type, FileHandle, FoundVarName, FoundVarGuid, FoundOne);
430
431 //
432 // No matter what happened we process our own variable
433 // Only continue if Guid and VariableName are each either NULL or a match
434 //
435 if ( ( Name == NULL
436 || gUnicodeCollation->MetaiMatch(gUnicodeCollation, FoundVarName, (CHAR16*) Name) )
437 && ( Guid == NULL
438 || CompareGuid(&FoundVarGuid, Guid) )
439 ) {
440 DataSize = 0;
441 DataBuffer = NULL;
442 //
443 // do the print or delete
444 //
445 *FoundOne = TRUE;
446 Status = gRT->GetVariable (FoundVarName, &FoundVarGuid, &Atts, &DataSize, DataBuffer);
447 if (Status == EFI_BUFFER_TOO_SMALL) {
448 SHELL_FREE_NON_NULL (DataBuffer);
449 DataBuffer = AllocatePool (DataSize);
450 if (DataBuffer == NULL) {
451 Status = EFI_OUT_OF_RESOURCES;
452 } else {
453 Status = gRT->GetVariable (FoundVarName, &FoundVarGuid, &Atts, &DataSize, DataBuffer);
454 }
455 }
456 if ((Type == DmpStoreDisplay) || (Type == DmpStoreSave)) {
457 //
458 // Last error check then print this variable out.
459 //
460 if (!EFI_ERROR(Status) && DataBuffer != NULL) {
461 RetString = GetAttrType(Atts);
462 ShellPrintHiiEx(
463 -1,
464 -1,
465 NULL,
466 STRING_TOKEN(STR_DMPSTORE_HEADER_LINE),
467 gShellDebug1HiiHandle,
468 RetString,
469 &FoundVarGuid,
470 FoundVarName,
471 DataSize);
472 if (Type == DmpStoreDisplay) {
473 DumpHex(2, 0, DataSize, DataBuffer);
474 } else {
475 Status = AppendSingleVariableToFile (
476 FileHandle,
477 FoundVarName,
478 &FoundVarGuid,
479 Atts,
480 (UINT32) DataSize,
481 DataBuffer
482 );
483 }
484 SHELL_FREE_NON_NULL(RetString);
485 }
486 } else if (Type == DmpStoreDelete) {
487 //
488 // We only need name to delete it...
489 //
490 ShellPrintHiiEx (
491 -1,
492 -1,
493 NULL,
494 STRING_TOKEN(STR_DMPSTORE_DELETE_LINE),
495 gShellDebug1HiiHandle,
496 &FoundVarGuid,
497 FoundVarName,
498 gRT->SetVariable (FoundVarName, &FoundVarGuid, Atts, 0, NULL)
499 );
500 }
501 SHELL_FREE_NON_NULL(DataBuffer);
502 }
503
504 SHELL_FREE_NON_NULL(FoundVarName);
505
506 if (Status == EFI_DEVICE_ERROR) {
507 ShellStatus = SHELL_DEVICE_ERROR;
508 } else if (Status == EFI_SECURITY_VIOLATION) {
509 ShellStatus = SHELL_SECURITY_VIOLATION;
510 } else if (EFI_ERROR(Status)) {
511 ShellStatus = SHELL_NOT_READY;
512 }
513
514 return (ShellStatus);
515 }
516
517 /**
518 Function to display or delete variables. This will set up and call into the recursive function.
519
520 @param[in] Name The variable name of the EFI variable (or NULL).
521 @param[in] Guid The GUID of the variable set (or NULL).
522 @param[in] Type The operation type.
523 @param[in] FileHandle The file to save or load variables.
524
525 @retval SHELL_SUCCESS The operation was successful.
526 @retval SHELL_OUT_OF_RESOURCES A memorty allocation failed.
527 @retval SHELL_ABORTED The abort message was received.
528 @retval SHELL_DEVICE_ERROR UEFI Variable Services returned an error.
529 @retval SHELL_NOT_FOUND the Name/Guid pair could not be found.
530 **/
531 SHELL_STATUS
532 EFIAPI
533 ProcessVariables (
534 IN CONST CHAR16 *Name OPTIONAL,
535 IN CONST EFI_GUID *Guid OPTIONAL,
536 IN DMP_STORE_TYPE Type,
537 IN SHELL_FILE_HANDLE FileHandle OPTIONAL
538 )
539 {
540 SHELL_STATUS ShellStatus;
541 BOOLEAN Found;
542 EFI_GUID FoundVarGuid;
543
544 Found = FALSE;
545 ShellStatus = SHELL_SUCCESS;
546 ZeroMem (&FoundVarGuid, sizeof(EFI_GUID));
547
548 if (Type == DmpStoreLoad) {
549 ShellStatus = LoadVariablesFromFile (FileHandle, Name, Guid, &Found);
550 } else {
551 ShellStatus = CascadeProcessVariables(Name, Guid, Type, FileHandle, NULL, FoundVarGuid, &Found);
552 }
553
554 if (!Found) {
555 if (ShellStatus == SHELL_OUT_OF_RESOURCES) {
556 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellDebug1HiiHandle);
557 return (ShellStatus);
558 } else if (Name != NULL && Guid == NULL) {
559 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_NO_VAR_FOUND_N), gShellDebug1HiiHandle, Name);
560 } else if (Name != NULL && Guid != NULL) {
561 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_NO_VAR_FOUND_GN), gShellDebug1HiiHandle, Guid, Name);
562 } else if (Name == NULL && Guid == NULL) {
563 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_NO_VAR_FOUND), gShellDebug1HiiHandle);
564 } else if (Name == NULL && Guid != NULL) {
565 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_NO_VAR_FOUND_G), gShellDebug1HiiHandle, Guid);
566 }
567 return (SHELL_NOT_FOUND);
568 }
569 return (ShellStatus);
570 }
571
572 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
573 {L"-d", TypeFlag},
574 {L"-l", TypeValue},
575 {L"-s", TypeValue},
576 {L"-all", TypeFlag},
577 {L"-guid", TypeValue},
578 {NULL, TypeMax}
579 };
580
581 /**
582 Function for 'dmpstore' command.
583
584 @param[in] ImageHandle Handle to the Image (NULL if Internal).
585 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
586 **/
587 SHELL_STATUS
588 EFIAPI
589 ShellCommandRunDmpStore (
590 IN EFI_HANDLE ImageHandle,
591 IN EFI_SYSTEM_TABLE *SystemTable
592 )
593 {
594 EFI_STATUS Status;
595 LIST_ENTRY *Package;
596 CHAR16 *ProblemParam;
597 SHELL_STATUS ShellStatus;
598 CONST CHAR16 *GuidStr;
599 CONST CHAR16 *File;
600 EFI_GUID *Guid;
601 EFI_GUID GuidData;
602 CONST CHAR16 *Name;
603 DMP_STORE_TYPE Type;
604 SHELL_FILE_HANDLE FileHandle;
605 EFI_FILE_INFO *FileInfo;
606
607 ShellStatus = SHELL_SUCCESS;
608 Package = NULL;
609 FileHandle = NULL;
610 File = NULL;
611 Type = DmpStoreDisplay;
612
613 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
614 if (EFI_ERROR(Status)) {
615 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
616 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, ProblemParam);
617 FreePool(ProblemParam);
618 ShellStatus = SHELL_INVALID_PARAMETER;
619 } else {
620 ASSERT(FALSE);
621 }
622 } else {
623 if (ShellCommandLineGetCount(Package) > 2) {
624 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle);
625 ShellStatus = SHELL_INVALID_PARAMETER;
626 } else if (ShellCommandLineGetFlag(Package, L"-all") && ShellCommandLineGetFlag(Package, L"-guid")) {
627 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_CONFLICT), gShellDebug1HiiHandle, L"-all", L"-guid");
628 ShellStatus = SHELL_INVALID_PARAMETER;
629 } else if (ShellCommandLineGetFlag(Package, L"-s") && ShellCommandLineGetFlag(Package, L"-l")) {
630 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_CONFLICT), gShellDebug1HiiHandle, L"-l", L"-s");
631 ShellStatus = SHELL_INVALID_PARAMETER;
632 } else if ((ShellCommandLineGetFlag(Package, L"-s") || ShellCommandLineGetFlag(Package, L"-l")) && ShellCommandLineGetFlag(Package, L"-d")) {
633 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_CONFLICT), gShellDebug1HiiHandle, L"-l or -s", L"-d");
634 ShellStatus = SHELL_INVALID_PARAMETER;
635 } else {
636 if (!ShellCommandLineGetFlag(Package, L"-all")) {
637 GuidStr = ShellCommandLineGetValue(Package, L"-guid");
638 if (GuidStr != NULL) {
639 Status = ConvertStringToGuid(GuidStr, &GuidData);
640 if (EFI_ERROR(Status)) {
641 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, GuidStr);
642 ShellStatus = SHELL_INVALID_PARAMETER;
643 }
644 Guid = &GuidData;
645 } else {
646 Guid = &gEfiGlobalVariableGuid;
647 }
648 Name = ShellCommandLineGetRawValue(Package, 1);
649 } else {
650 Name = NULL;
651 Guid = NULL;
652 }
653 if (ShellStatus == SHELL_SUCCESS) {
654 if (ShellCommandLineGetFlag(Package, L"-s")) {
655 Type = DmpStoreSave;
656 File = ShellCommandLineGetValue(Package, L"-s");
657 if (File == NULL) {
658 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VALUE), gShellDebug1HiiHandle, L"-s");
659 ShellStatus = SHELL_INVALID_PARAMETER;
660 } else {
661 Status = ShellOpenFileByName (File, &FileHandle, EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ, 0);
662 if (!EFI_ERROR (Status)) {
663 //
664 // Delete existing file, but do not delete existing directory
665 //
666 FileInfo = ShellGetFileInfo (FileHandle);
667 if (FileInfo == NULL) {
668 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, File);
669 Status = EFI_DEVICE_ERROR;
670 } else {
671 if ((FileInfo->Attribute & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY) {
672 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_IS_DIRECTORY), gShellDebug1HiiHandle, File);
673 Status = EFI_INVALID_PARAMETER;
674 } else {
675 Status = ShellDeleteFile (&FileHandle);
676 if (EFI_ERROR (Status)) {
677 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_DELETE_FAIL), gShellDebug1HiiHandle, File);
678 }
679 }
680 FreePool (FileInfo);
681 }
682 } else if (Status == EFI_NOT_FOUND) {
683 //
684 // Good when file doesn't exist
685 //
686 Status = EFI_SUCCESS;
687 } else {
688 //
689 // Otherwise it's bad.
690 //
691 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, File);
692 }
693
694 if (!EFI_ERROR (Status)) {
695 Status = ShellOpenFileByName (File, &FileHandle, EFI_FILE_MODE_CREATE | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ, 0);
696 if (EFI_ERROR (Status)) {
697 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, File);
698 }
699 }
700
701 if (EFI_ERROR (Status)) {
702 ShellStatus = SHELL_INVALID_PARAMETER;
703 }
704 }
705 } else if (ShellCommandLineGetFlag(Package, L"-l")) {
706 Type = DmpStoreLoad;
707 File = ShellCommandLineGetValue(Package, L"-l");
708 if (File == NULL) {
709 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VALUE), gShellDebug1HiiHandle, L"-l");
710 ShellStatus = SHELL_INVALID_PARAMETER;
711 } else {
712 Status = ShellOpenFileByName (File, &FileHandle, EFI_FILE_MODE_READ, 0);
713 if (EFI_ERROR (Status)) {
714 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, File);
715 ShellStatus = SHELL_INVALID_PARAMETER;
716 } else {
717 FileInfo = ShellGetFileInfo (FileHandle);
718 if (FileInfo == NULL) {
719 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, File);
720 ShellStatus = SHELL_DEVICE_ERROR;
721 } else {
722 if ((FileInfo->Attribute & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY) {
723 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_IS_DIRECTORY), gShellDebug1HiiHandle, File);
724 ShellStatus = SHELL_INVALID_PARAMETER;
725 }
726 FreePool (FileInfo);
727 }
728 }
729 }
730 } else if (ShellCommandLineGetFlag(Package, L"-d")) {
731 Type = DmpStoreDelete;
732 }
733 }
734
735 if (ShellStatus == SHELL_SUCCESS) {
736 if (Type == DmpStoreSave) {
737 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_SAVE), gShellDebug1HiiHandle, File);
738 } else if (Type == DmpStoreLoad) {
739 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_LOAD), gShellDebug1HiiHandle, File);
740 }
741 ShellStatus = ProcessVariables (Name, Guid, Type, FileHandle);
742 if ((Type == DmpStoreLoad) || (Type == DmpStoreSave)) {
743 ShellCloseFile (&FileHandle);
744 }
745 }
746 }
747 }
748
749 if (Package != NULL) {
750 ShellCommandLineFreeVarList (Package);
751 }
752 return ShellStatus;
753 }
754