TianoCore EDK2 master
Loading...
Searching...
No Matches
VariablePolicy.c
Go to the documentation of this file.
1
12#include "VariablePolicy.h"
13
14#include <Library/BaseLib.h>
16#include <Library/DebugLib.h>
17#include <Library/HiiLib.h>
19#include <Library/PrintLib.h>
20#include <Library/ShellLib.h>
24
26#include <Protocol/VariablePolicy.h>
27
28#define VAR_POLICY_FLAG_STATS_STR L"-s"
29#define VAR_POLICY_FLAG_POLICY_STR L"-p"
30#define VAR_POLICY_FLAG_VERBOSE_STR L"-v"
31
32#define VAR_POLICY_CMD_MIN_ATTR_STR_LEN 64
33
34EFI_HII_HANDLE mVarPolicyShellCommandHiiHandle = NULL;
35
36STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
37 { VAR_POLICY_FLAG_POLICY_STR, TypeFlag },
38 { VAR_POLICY_FLAG_STATS_STR, TypeFlag },
39 { VAR_POLICY_FLAG_VERBOSE_STR, TypeFlag },
40 { NULL, TypeMax }
41};
42
43STATIC CONST VAR_POLICY_CMD_VAR_NAMESPACE mVarNamespaces[] = {
44 {
45 VariableVendorCapsule,
46 &gEfiCapsuleVendorGuid,
47 L"Capsule"
48 },
49 {
50 VariableVendorCapsuleReport,
51 &gEfiCapsuleReportGuid,
52 L"Capsule Reporting"
53 },
54 {
55 VariableVendorGlobal,
56 &gEfiGlobalVariableGuid,
57 L"UEFI Global"
58 },
59 {
60 VariableVendorMemoryTypeInfo,
61 &gEfiMemoryTypeInformationGuid,
62 L"Memory Type Information"
63 },
64 {
65 VariableVendorMonotonicCounter,
66 &gMtcVendorGuid,
67 L"Monotonic Counter"
68 },
69 {
70 VariableVendorMorControl,
71 &gEfiMemoryOverwriteRequestControlLockGuid,
72 L"Memory Overwrite Request (MOR) Control Lock"
73 },
74 {
75 VariableVendorShell,
76 &gShellVariableGuid,
77 L"UEFI Shell"
78 },
79 {
80 VariableVendorShell,
81 &gShellAliasGuid,
82 L"UEFI Shell Alias"
83 }
84};
85
102 IN UINT32 Attributes,
103 IN UINTN AttributesStrSize,
104 OUT CHAR16 *AttributesStr
105 )
106{
107 if ((AttributesStr == NULL) || (AttributesStrSize < VAR_POLICY_CMD_MIN_ATTR_STR_LEN)) {
108 return EFI_INVALID_PARAMETER;
109 }
110
111 AttributesStr[0] = L'0';
112 AttributesStr[1] = L'x';
113 AttributesStr[2] = L'\0';
114
115 UnicodeValueToStringS (AttributesStr + 2, AttributesStrSize - 2, (RADIX_HEX), (INT64)Attributes, 30);
116
117 if (Attributes == 0) {
118 StrCatS (AttributesStr, AttributesStrSize, L" No Attributes");
119 } else {
121 StrCatS (AttributesStr, AttributesStrSize, L" NV");
122 Attributes ^= EFI_VARIABLE_NON_VOLATILE;
123 }
124
125 if ((Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS) == EFI_VARIABLE_BOOTSERVICE_ACCESS) {
126 StrCatS (AttributesStr, AttributesStrSize, L" BS");
127 Attributes ^= EFI_VARIABLE_BOOTSERVICE_ACCESS;
128 }
129
130 if ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == EFI_VARIABLE_RUNTIME_ACCESS) {
131 StrCatS (AttributesStr, AttributesStrSize, L" RT");
132 Attributes ^= EFI_VARIABLE_RUNTIME_ACCESS;
133 }
134
136 StrCatS (AttributesStr, AttributesStrSize, L" HW-Error");
138 }
139
141 StrCatS (AttributesStr, AttributesStrSize, L" Auth-WA");
143 }
144
146 StrCatS (AttributesStr, AttributesStrSize, L" Auth-TIME-WA");
148 }
149
150 if ((Attributes & EFI_VARIABLE_APPEND_WRITE) == EFI_VARIABLE_APPEND_WRITE) {
151 StrCatS (AttributesStr, AttributesStrSize, L" APPEND-W");
152 Attributes ^= EFI_VARIABLE_APPEND_WRITE;
153 }
154
155 if (Attributes != 0) {
156 StrCatS (AttributesStr, AttributesStrSize, L" <Unknown Attribute>");
157 }
158 }
159
160 return EFI_SUCCESS;
161}
162
170VOID
172 IN UINTN TotalVariables,
173 IN UINTN TotalVariablesSize
174 )
175{
176 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_STATS_HEADER_1), mVarPolicyShellCommandHiiHandle);
177 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_STATS_HEADER_2), mVarPolicyShellCommandHiiHandle);
178 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_STATS_HEADER_1), mVarPolicyShellCommandHiiHandle);
179 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_STATS_TOTAL_VARS), mVarPolicyShellCommandHiiHandle, TotalVariables);
180 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_STATS_TOTAL_SIZE), mVarPolicyShellCommandHiiHandle, TotalVariablesSize, TotalVariablesSize);
181 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_STATS_HEADER_1), mVarPolicyShellCommandHiiHandle);
182}
183
195 IN EFI_GUID *VariableGuid
196 )
197{
198 UINTN Index;
199
200 if (VariableGuid == NULL) {
201 ASSERT (VariableGuid != NULL);
202 return NULL;
203 }
204
205 for (Index = 0; Index < ARRAY_SIZE (mVarNamespaces); Index++) {
206 if (CompareGuid (mVarNamespaces[Index].VendorGuid, VariableGuid)) {
207 return &mVarNamespaces[Index];
208 }
209 }
210
211 return NULL;
212}
213
229 IN CHAR16 *VariableName,
230 IN EFI_GUID *VariableGuid,
231 IN UINTN VariableSize,
232 IN UINT32 VariableAttributes
233 )
234{
235 EFI_STATUS Status;
236 CHAR16 *AttributesStr;
237 CHAR16 *DescriptionStr;
238 CONST VAR_POLICY_CMD_VAR_NAMESPACE *CmdVarNamespace;
239
240 AttributesStr = NULL;
241 DescriptionStr = NULL;
242
243 if ((VariableName == NULL) || (VariableGuid == NULL)) {
244 return EFI_INVALID_PARAMETER;
245 }
246
247 CmdVarNamespace = GetNameSpaceInfo (VariableGuid);
248
249 if (CmdVarNamespace == NULL) {
250 DescriptionStr = AllocatePages (1);
251 if (DescriptionStr == NULL) {
252 Status = EFI_OUT_OF_RESOURCES;
253 goto Exit;
254 }
255
256 ZeroMem ((VOID *)DescriptionStr, EFI_PAGES_TO_SIZE (1));
257 UnicodeSPrint (DescriptionStr, EFI_PAGES_TO_SIZE (1), L"Unknown Vendor (%g)", VariableGuid);
258 } else {
259 DescriptionStr = CmdVarNamespace->Description;
260 }
261
262 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_VAR_TYPE), mVarPolicyShellCommandHiiHandle, DescriptionStr);
263 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_VAR_NAME), mVarPolicyShellCommandHiiHandle, VariableName);
264 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_VAR_SIZE), mVarPolicyShellCommandHiiHandle, VariableSize, VariableSize);
265
266 AttributesStr = AllocatePages (1);
267 if (AttributesStr == NULL) {
268 Status = EFI_OUT_OF_RESOURCES;
269 goto Exit;
270 }
271
272 ZeroMem ((VOID *)AttributesStr, EFI_PAGES_TO_SIZE (1));
273 Status = GetAttributesString (VariableAttributes, EFI_PAGES_TO_SIZE (1), AttributesStr);
274 if (Status == EFI_SUCCESS) {
276 -1,
277 -1,
278 NULL,
279 STRING_TOKEN (STR_VAR_POL_VAR_ATTR),
280 mVarPolicyShellCommandHiiHandle,
281 AttributesStr
282 );
283 }
284
285 Status = EFI_SUCCESS;
286
287Exit:
288 if (AttributesStr != NULL) {
289 FreePages (AttributesStr, 1);
290 }
291
292 if ((CmdVarNamespace == NULL) && (DescriptionStr != NULL)) {
293 FreePages (DescriptionStr, 1);
294 }
295
296 return Status;
297}
298
311 IN VOID *Data,
312 IN UINTN DataSize
313 )
314{
315 if ((DataSize == 0) || (Data == NULL)) {
316 return EFI_INVALID_PARAMETER;
317 }
318
319 VAR_POLICY_CMD_SHELL_DUMP_HEX (0, Data, DataSize);
320
321 return EFI_SUCCESS;
322}
323
335BOOLEAN
337 IN CHAR16 *VariableName,
338 IN EFI_GUID *VendorGuid
339 )
340{
341 EFI_STATUS Status;
342 VARIABLE_POLICY_ENTRY VariablePolicyEntry;
343 VARIABLE_LOCK_ON_VAR_STATE_POLICY LockOnVarStatePolicy;
344 UINTN VariablePolicyVariableNameBufferSize;
345 UINTN ReturnedVariableNameSize;
346 BOOLEAN PolicyHeaderPresent;
347 CHAR16 *VariablePolicyVariableName;
348 CHAR16 *VariableAttributesStr;
349 EDKII_VARIABLE_POLICY_PROTOCOL *VariablePolicy;
350
351 PolicyHeaderPresent = FALSE;
352 VariableAttributesStr = NULL;
353 VariablePolicyVariableName = NULL;
354
355 if ((VariableName == NULL) || (VendorGuid == NULL)) {
356 ASSERT ((VariableName != NULL) && (VendorGuid != NULL));
357 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_POLICY_INT_ERR), mVarPolicyShellCommandHiiHandle);
358 return FALSE;
359 }
360
361 Status = gBS->LocateProtocol (&gEdkiiVariablePolicyProtocolGuid, NULL, (VOID **)&VariablePolicy);
362 if (EFI_ERROR (Status)) {
363 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_POLICY_NO_PROT), mVarPolicyShellCommandHiiHandle);
364 return FALSE;
365 }
366
367 VariablePolicyVariableNameBufferSize = EFI_PAGES_TO_SIZE (1);
368 VariablePolicyVariableName = AllocatePages (EFI_SIZE_TO_PAGES (VariablePolicyVariableNameBufferSize));
369 if (VariablePolicyVariableName == NULL) {
370 Status = EFI_OUT_OF_RESOURCES;
371 ASSERT_EFI_ERROR (Status);
372 goto Done;
373 }
374
375 ZeroMem (VariablePolicyVariableName, VariablePolicyVariableNameBufferSize);
376 ReturnedVariableNameSize = VariablePolicyVariableNameBufferSize;
377 Status = VariablePolicy->GetVariablePolicyInfo (
378 VariableName,
379 VendorGuid,
380 &ReturnedVariableNameSize,
381 &VariablePolicyEntry,
382 VariablePolicyVariableName
383 );
384 if (Status == EFI_NOT_READY) {
385 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_POLICY_NOT_INIT), mVarPolicyShellCommandHiiHandle);
386 } else if (Status == EFI_NOT_FOUND) {
387 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_POLICY_NOT_FOUND), mVarPolicyShellCommandHiiHandle);
388 } else if (EFI_ERROR (Status)) {
389 // A different error return code is not expected
390 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_POLICY_UNEXP_ERR), mVarPolicyShellCommandHiiHandle, Status);
391 } else {
392 PolicyHeaderPresent = TRUE;
393 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_POLICY_HEADER_1), mVarPolicyShellCommandHiiHandle);
394 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_POLICY_HEADER_2), mVarPolicyShellCommandHiiHandle);
395 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_POLICY_HEADER_1), mVarPolicyShellCommandHiiHandle);
396 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_POLICY_VERSION), mVarPolicyShellCommandHiiHandle, VariablePolicyEntry.Version);
397
398 if ((ReturnedVariableNameSize > 0) && (VariablePolicyVariableName[0] != CHAR_NULL)) {
399 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_POLICY_VARIABLE), mVarPolicyShellCommandHiiHandle, VariablePolicyVariableName);
400 } else {
401 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_POLICY_VARIABLE), mVarPolicyShellCommandHiiHandle, L"<Entire Namespace>");
402 }
403
404 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_POLICY_NAMESPACE), mVarPolicyShellCommandHiiHandle, &VariablePolicyEntry.Namespace);
405 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_POLICY_MIN_SIZE), mVarPolicyShellCommandHiiHandle, VariablePolicyEntry.MinSize);
406 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_POLICY_MAX_SIZE), mVarPolicyShellCommandHiiHandle, VariablePolicyEntry.MaxSize);
407
408 switch (VariablePolicyEntry.LockPolicyType) {
409 case VARIABLE_POLICY_TYPE_NO_LOCK:
410 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_POLICY_LOCK_TYPE), mVarPolicyShellCommandHiiHandle, L"No Lock");
411 break;
412 case VARIABLE_POLICY_TYPE_LOCK_NOW:
413 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_POLICY_LOCK_TYPE), mVarPolicyShellCommandHiiHandle, L"Lock Now");
414 break;
415 case VARIABLE_POLICY_TYPE_LOCK_ON_CREATE:
416 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_POLICY_LOCK_TYPE), mVarPolicyShellCommandHiiHandle, L"On Create");
417 break;
418 case VARIABLE_POLICY_TYPE_LOCK_ON_VAR_STATE:
419 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_POLICY_LOCK_TYPE), mVarPolicyShellCommandHiiHandle, L"On Variable State");
420
421 ZeroMem (VariablePolicyVariableName, VariablePolicyVariableNameBufferSize);
422 ReturnedVariableNameSize = VariablePolicyVariableNameBufferSize;
423 Status = VariablePolicy->GetLockOnVariableStateVariablePolicyInfo (
424 VariableName,
425 VendorGuid,
426 &ReturnedVariableNameSize,
427 &LockOnVarStatePolicy,
428 VariablePolicyVariableName
429 );
430 if (EFI_ERROR (Status)) {
431 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_POLICY_UNEXP_ERR), mVarPolicyShellCommandHiiHandle, Status);
432 goto Done;
433 } else {
434 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_POLICY_STATE_NS), mVarPolicyShellCommandHiiHandle, &LockOnVarStatePolicy.Namespace);
435 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_POLICY_STATE_VAL), mVarPolicyShellCommandHiiHandle, LockOnVarStatePolicy.Value);
436 if ((ReturnedVariableNameSize > 0) && (VariablePolicyVariableName[0] != CHAR_NULL)) {
437 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_POLICY_STATE_NAME), mVarPolicyShellCommandHiiHandle, VariablePolicyVariableName);
438 } else {
439 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_POLICY_STATE_NAME), mVarPolicyShellCommandHiiHandle, L"<Entire Namespace>");
440 }
441 }
442
443 break;
444 default:
445 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_POLICY_LOCK_TYPE), mVarPolicyShellCommandHiiHandle, L"Unknown");
446 break;
447 }
448
449 VariableAttributesStr = AllocatePages (1);
450 if (VariableAttributesStr == NULL) {
451 Status = EFI_OUT_OF_RESOURCES;
452 ASSERT_EFI_ERROR (Status);
453 goto Done;
454 }
455
456 ZeroMem (VariableAttributesStr, EFI_PAGES_TO_SIZE (1));
457 Status = GetAttributesString (VariablePolicyEntry.AttributesMustHave, EFI_PAGES_TO_SIZE (1), VariableAttributesStr);
458 if (Status == EFI_SUCCESS) {
460 -1,
461 -1,
462 NULL,
463 STRING_TOKEN (STR_VAR_POL_POLICY_ATTR_MUST),
464 mVarPolicyShellCommandHiiHandle
465 );
467 -1,
468 -1,
469 NULL,
470 STRING_TOKEN (STR_VAR_POL_POLICY_ATTR_GEN),
471 mVarPolicyShellCommandHiiHandle,
472 VariableAttributesStr
473 );
474 }
475
476 ZeroMem (VariableAttributesStr, EFI_PAGES_TO_SIZE (1));
477 Status = GetAttributesString (VariablePolicyEntry.AttributesCantHave, EFI_PAGES_TO_SIZE (1), VariableAttributesStr);
478 if (Status == EFI_SUCCESS) {
480 -1,
481 -1,
482 NULL,
483 STRING_TOKEN (STR_VAR_POL_POLICY_ATTR_NOT),
484 mVarPolicyShellCommandHiiHandle
485 );
487 -1,
488 -1,
489 NULL,
490 STRING_TOKEN (STR_VAR_POL_POLICY_ATTR_GEN),
491 mVarPolicyShellCommandHiiHandle,
492 VariableAttributesStr
493 );
494 }
495 }
496
497Done:
498 if (PolicyHeaderPresent) {
499 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VAR_POL_POLICY_HEADER_1), mVarPolicyShellCommandHiiHandle);
500 }
501
502 if (VariableAttributesStr != NULL) {
503 FreePages (VariableAttributesStr, 1);
504 }
505
506 if (VariablePolicyVariableName != NULL) {
507 FreePages (VariablePolicyVariableName, 1);
508 }
509
510 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_LINE_BREAK), mVarPolicyShellCommandHiiHandle);
511
512 return Status == EFI_SUCCESS;
513}
514
544 IN OUT UINTN *VariableNameBufferSize,
545 IN OUT CHAR16 **VariableName,
546 IN OUT EFI_GUID *VariableGuid
547 )
548{
549 EFI_STATUS Status;
550 UINTN NextVariableNameBufferSize;
551
552 if ((VariableNameBufferSize == NULL) || (VariableName == NULL) || (VariableGuid == NULL)) {
553 return EFI_INVALID_PARAMETER;
554 }
555
556 if (*VariableNameBufferSize == 0) {
557 if (*VariableName != NULL) {
558 return EFI_INVALID_PARAMETER;
559 }
560
561 //
562 // Allocate a buffer to temporarily hold variable names. To reduce memory
563 // allocations, the default buffer size is 256 characters. The buffer can
564 // be reallocated if expansion is necessary (should be very rare).
565 //
566 *VariableNameBufferSize = sizeof (CHAR16) * 256;
567 *VariableName = AllocateZeroPool (*VariableNameBufferSize);
568 if (*VariableName == NULL) {
569 return EFI_OUT_OF_RESOURCES;
570 }
571
572 ZeroMem ((VOID *)VariableGuid, sizeof (EFI_GUID));
573 }
574
575 NextVariableNameBufferSize = *VariableNameBufferSize;
576 Status = gRT->GetNextVariableName (
577 &NextVariableNameBufferSize,
578 *VariableName,
579 VariableGuid
580 );
581 if (Status == EFI_BUFFER_TOO_SMALL) {
582 *VariableName = ReallocatePool (
583 *VariableNameBufferSize,
584 NextVariableNameBufferSize,
585 *VariableName
586 );
587 if (*VariableName == NULL) {
588 return EFI_OUT_OF_RESOURCES;
589 }
590
591 *VariableNameBufferSize = NextVariableNameBufferSize;
592
593 Status = gRT->GetNextVariableName (
594 &NextVariableNameBufferSize,
595 *VariableName,
596 VariableGuid
597 );
598 ASSERT (Status != EFI_BUFFER_TOO_SMALL);
599 }
600
601 return Status;
602}
603
622 IN BOOLEAN Verbose,
623 IN BOOLEAN Stats,
624 IN BOOLEAN PolicyCheck
625 )
626{
627 EFI_STATUS Status;
628 EFI_STATUS GetNextVariableStatus;
629 UINT32 Attributes;
630 UINTN CurrentVariableDataBufferSize;
631 UINTN DataSize;
632 UINTN TotalDataSize;
633 UINTN TotalVariables;
634 UINTN TotalVariablesWithPolicy;
635 UINTN VariableNameBufferSize;
636 EFI_GUID VariableGuid;
637 CHAR16 *VariableName;
638 VOID *Data;
639
640 Status = EFI_SUCCESS;
641 Data = NULL;
642 VariableName = NULL;
643 CurrentVariableDataBufferSize = 0;
644 TotalDataSize = 0;
645 TotalVariables = 0;
646 TotalVariablesWithPolicy = 0;
647 VariableNameBufferSize = 0;
648
649 do {
650 GetNextVariableStatus = GetNextVariableNameWithDynamicReallocation (
651 &VariableNameBufferSize,
652 &VariableName,
653 &VariableGuid
654 );
655
656 if (!EFI_ERROR (GetNextVariableStatus)) {
657 DataSize = 0;
658 Status = gRT->GetVariable (
659 VariableName,
660 &VariableGuid,
661 &Attributes,
662 &DataSize,
663 NULL
664 );
665 if (Status != EFI_BUFFER_TOO_SMALL) {
666 // If the variable exists, a zero size buffer should be too small
667 Status = EFI_DEVICE_ERROR;
668 goto DeallocateAndExit;
669 }
670
671 TotalDataSize += DataSize;
672 TotalVariables++;
673
674 if (!Stats || Verbose) {
675 Status = PrintNonVerboseVarInfo (VariableName, &VariableGuid, DataSize, Attributes);
676 if (!EFI_ERROR (Status)) {
677 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_LINE_BREAK), mVarPolicyShellCommandHiiHandle);
678 }
679 }
680
681 if (PolicyCheck || Verbose) {
682 if (PrintVariablePolicyInfo (VariableName, &VariableGuid)) {
683 TotalVariablesWithPolicy++;
684 }
685 }
686
687 if (Verbose) {
688 if (CurrentVariableDataBufferSize < DataSize) {
689 if (Data != NULL) {
690 FreePool (Data);
691 }
692
693 Data = AllocateZeroPool (DataSize);
694 if (Data == NULL) {
695 Status = EFI_OUT_OF_RESOURCES;
696 goto DeallocateAndExit;
697 }
698
699 CurrentVariableDataBufferSize = DataSize;
700 }
701
702 Status = gRT->GetVariable (
703 VariableName,
704 &VariableGuid,
705 NULL,
706 &DataSize,
707 Data
708 );
709 if (EFI_ERROR (Status)) {
710 Status = EFI_DEVICE_ERROR;
711 goto DeallocateAndExit;
712 }
713
714 Status = PrintVerboseVarInfo (Data, DataSize);
715 if (EFI_ERROR (Status)) {
716 Status = EFI_DEVICE_ERROR;
717 goto DeallocateAndExit;
718 }
719 }
720 }
721 } while (!EFI_ERROR (GetNextVariableStatus));
722
723 if (TotalVariables == 0) {
724 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VARS), mVarPolicyShellCommandHiiHandle);
725 } else {
726 if (Verbose || Stats) {
727 PrintStats (TotalVariables, TotalDataSize);
728 }
729
730 if (Verbose || PolicyCheck) {
731 ASSERT (TotalVariablesWithPolicy <= TotalVariables);
732
733 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_LINE_BREAK), mVarPolicyShellCommandHiiHandle);
734 if (TotalVariablesWithPolicy == TotalVariables) {
736 -1,
737 -1,
738 NULL,
739 STRING_TOKEN (STR_VAR_POL_POLICY_STATS_PASS),
740 mVarPolicyShellCommandHiiHandle,
741 TotalVariablesWithPolicy,
742 TotalVariables
743 );
744 } else {
746 -1,
747 -1,
748 NULL,
749 STRING_TOKEN (STR_VAR_POL_POLICY_STATS_FAIL),
750 mVarPolicyShellCommandHiiHandle,
751 TotalVariablesWithPolicy,
752 TotalVariables
753 );
754 }
755
756 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_LINE_BREAK), mVarPolicyShellCommandHiiHandle);
757 }
758 }
759
760 Status = EFI_SUCCESS;
761
762DeallocateAndExit:
763 if (VariableName != NULL) {
764 FreePool (VariableName);
765 }
766
767 if (Data != NULL) {
768 FreePool (Data);
769 }
770
771 ASSERT_EFI_ERROR (Status);
772
773 return Status;
774}
775
789EFIAPI
791 IN EFI_HANDLE ImageHandle,
792 IN EFI_SYSTEM_TABLE *SystemTable
793 )
794{
795 EFI_STATUS Status;
796 SHELL_STATUS ShellStatus;
797 BOOLEAN PolicyCheck;
798 BOOLEAN StatsDump;
799 BOOLEAN VerboseDump;
800 LIST_ENTRY *Package;
801 CHAR16 *ProblemParam;
802
803 Package = NULL;
804 ShellStatus = SHELL_INVALID_PARAMETER;
805 Status = EFI_SUCCESS;
806 PolicyCheck = FALSE;
807 StatsDump = FALSE;
808 VerboseDump = FALSE;
809
810 Status = ShellInitialize ();
811 if (EFI_ERROR (Status)) {
812 ASSERT_EFI_ERROR (Status);
813 return SHELL_ABORTED;
814 }
815
816 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
817 if (EFI_ERROR (Status)) {
818 if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) {
819 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), mVarPolicyShellCommandHiiHandle, VAR_POLICY_COMMAND_NAME, ProblemParam);
820 FreePool (ProblemParam);
821 ShellStatus = SHELL_INVALID_PARAMETER;
822 goto Done;
823 } else {
824 ASSERT (FALSE);
825 }
826 } else {
827 if (ShellCommandLineGetCount (Package) > 1) {
828 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), mVarPolicyShellCommandHiiHandle, VAR_POLICY_COMMAND_NAME);
829 ShellStatus = SHELL_INVALID_PARAMETER;
830 goto Done;
831 }
832
833 PolicyCheck = ShellCommandLineGetFlag (Package, VAR_POLICY_FLAG_POLICY_STR);
834 StatsDump = ShellCommandLineGetFlag (Package, VAR_POLICY_FLAG_STATS_STR);
835 VerboseDump = ShellCommandLineGetFlag (Package, VAR_POLICY_FLAG_VERBOSE_STR);
836
837 Status = DumpVars (VerboseDump, StatsDump, PolicyCheck);
838 ASSERT_EFI_ERROR (Status);
839 }
840
841Done:
842 if (Package != NULL) {
844 }
845
846 return ShellStatus;
847}
848
859 IN EFI_HANDLE ImageHandle
860 )
861{
862 EFI_STATUS Status;
863 EFI_HII_PACKAGE_LIST_HEADER *PackageList;
864 EFI_HII_HANDLE HiiHandle;
865
866 //
867 // Retrieve HII package list from ImageHandle
868 //
869 Status = gBS->OpenProtocol (
870 ImageHandle,
871 &gEfiHiiPackageListProtocolGuid,
872 (VOID **)&PackageList,
873 ImageHandle,
874 NULL,
875 EFI_OPEN_PROTOCOL_GET_PROTOCOL
876 );
877 ASSERT_EFI_ERROR (Status);
878 if (EFI_ERROR (Status)) {
879 return NULL;
880 }
881
882 //
883 // Publish HII package list to HII Database.
884 //
885 Status = gHiiDatabase->NewPackageList (
887 PackageList,
888 NULL,
889 &HiiHandle
890 );
891 ASSERT_EFI_ERROR (Status);
892 if (EFI_ERROR (Status)) {
893 return NULL;
894 }
895
896 return HiiHandle;
897}
UINT64 UINTN
RETURN_STATUS EFIAPI StrCatS(IN OUT CHAR16 *Destination, IN UINTN DestMax, IN CONST CHAR16 *Source)
Definition: SafeString.c:405
BOOLEAN EFIAPI CompareGuid(IN CONST GUID *Guid1, IN CONST GUID *Guid2)
Definition: MemLibGuid.c:73
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
VOID EFIAPI FreePages(IN VOID *Buffer, IN UINTN Pages)
VOID *EFIAPI ReallocatePool(IN UINTN OldSize, IN UINTN NewSize, IN VOID *OldBuffer OPTIONAL)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
RETURN_STATUS EFIAPI UnicodeValueToStringS(IN OUT CHAR16 *Buffer, IN UINTN BufferSize, IN UINTN Flags, IN INT64 Value, IN UINTN Width)
Definition: PrintLib.c:652
UINTN EFIAPI UnicodeSPrint(OUT CHAR16 *StartOfBuffer, IN UINTN BufferSize, IN CONST CHAR16 *FormatString,...)
Definition: PrintLib.c:408
EFI_RUNTIME_SERVICES * gRT
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define STATIC
Definition: Base.h:264
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define ARRAY_SIZE(Array)
Definition: Base.h:1393
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
SHELL_STATUS
Definition: Shell.h:21
@ SHELL_ABORTED
Definition: Shell.h:128
@ SHELL_INVALID_PARAMETER
Definition: Shell.h:35
VOID *EFIAPI AllocatePages(IN UINTN Pages)
#define ShellCommandLineParse(CheckList, CheckPackage, ProblemParam, AutoPageBreak)
Make it easy to upgrade from older versions of the shell library.
Definition: ShellLib.h:755
EFI_STATUS EFIAPI ShellPrintHiiEx(IN INT32 Col OPTIONAL, IN INT32 Row OPTIONAL, IN CONST CHAR8 *Language OPTIONAL, IN CONST EFI_STRING_ID HiiFormatStringId, IN CONST EFI_HII_HANDLE HiiFormatHandle,...)
BOOLEAN EFIAPI ShellCommandLineGetFlag(IN CONST LIST_ENTRY *CONST CheckPackage, IN CONST CHAR16 *CONST KeyString)
@ TypeFlag
A flag that is present or not present only (IE "-a").
Definition: ShellLib.h:699
VOID EFIAPI ShellCommandLineFreeVarList(IN LIST_ENTRY *CheckPackage)
EFI_STATUS EFIAPI ShellInitialize(VOID)
Definition: UefiShellLib.c:532
UINTN EFIAPI ShellCommandLineGetCount(IN CONST LIST_ENTRY *CheckPackage)
#define VAR_POLICY_CMD_SHELL_DUMP_HEX(Offset, Data, DataSize)
VOID EFIAPI Exit(IN EFI_STATUS Status)
#define EFI_PAGES_TO_SIZE(Pages)
Definition: UefiBaseType.h:213
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SIZE_TO_PAGES(Size)
Definition: UefiBaseType.h:200
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_BOOT_SERVICES * gBS
EFI_HII_DATABASE_PROTOCOL * gHiiDatabase
#define STRING_TOKEN(t)
VOID * EFI_HII_HANDLE
#define EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
#define EFI_VARIABLE_HARDWARE_ERROR_RECORD
#define EFI_VARIABLE_NON_VOLATILE
#define EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
SHELL_STATUS EFIAPI RunVarPolicy(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
EFI_STATUS DumpVars(IN BOOLEAN Verbose, IN BOOLEAN Stats, IN BOOLEAN PolicyCheck)
EFI_STATUS PrintNonVerboseVarInfo(IN CHAR16 *VariableName, IN EFI_GUID *VariableGuid, IN UINTN VariableSize, IN UINT32 VariableAttributes)
EFI_STATUS GetNextVariableNameWithDynamicReallocation(IN OUT UINTN *VariableNameBufferSize, IN OUT CHAR16 **VariableName, IN OUT EFI_GUID *VariableGuid)
EFI_STATUS GetAttributesString(IN UINT32 Attributes, IN UINTN AttributesStrSize, OUT CHAR16 *AttributesStr)
EFI_HII_HANDLE InitializeHiiPackage(IN EFI_HANDLE ImageHandle)
EFI_STATUS PrintVerboseVarInfo(IN VOID *Data, IN UINTN DataSize)
VOID PrintStats(IN UINTN TotalVariables, IN UINTN TotalVariablesSize)
CONST VAR_POLICY_CMD_VAR_NAMESPACE * GetNameSpaceInfo(IN EFI_GUID *VariableGuid)
BOOLEAN PrintVariablePolicyInfo(IN CHAR16 *VariableName, IN EFI_GUID *VendorGuid)
Definition: Base.h:213