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