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