]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/DmpStore.c
ShellPkg: Fix recursion when deleting all variables.
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / DmpStore.c
1 /** @file
2 Main file for DmpStore shell Debug1 function.
3
4 Copyright (c) 2005 - 2012, 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 STATIC CHAR16 *AttrType[] = {
18 L"invalid", // 000
19 L"invalid", // 001
20 L"BS", // 010
21 L"NV+BS", // 011
22 L"RT+BS", // 100
23 L"NV+RT+BS", // 101
24 L"RT+BS", // 110
25 L"NV+RT+BS", // 111
26 };
27
28 /**
29 Function to display or delete variables.
30
31 @param[in] VariableName The variable name of the EFI variable (or NULL).
32 @param[in] Guid The GUID of the variable set (or NULL).
33 @param[in] Delete TRUE to delete, FALSE otherwise.
34
35 @retval SHELL_SUCCESS The operation was successful.
36 @retval SHELL_OUT_OF_RESOURCES A memorty allocation failed.
37 @retval SHELL_ABORTED The abort message was received.
38 @retval SHELL_DEVICE_ERROR UEFI Variable Services returned an error.
39 @retval SHELL_NOT_FOUND the Name/Guid pair could not be found.
40 **/
41 SHELL_STATUS
42 EFIAPI
43 ProcessVariables (
44 IN CONST CHAR16 *VariableName OPTIONAL,
45 IN CONST EFI_GUID *Guid OPTIONAL,
46 IN BOOLEAN Delete
47 )
48 {
49 EFI_STATUS Status;
50 UINT64 MaxStorSize;
51 UINT64 RemStorSize;
52 UINT64 MaxVarSize;
53 CHAR16 *FoundVarName;
54 UINTN Size;
55 EFI_GUID FoundVarGuid;
56 UINT8 *DataBuffer;
57 UINTN DataSize;
58 UINT32 Atts;
59 SHELL_STATUS ShellStatus;
60 BOOLEAN Found;
61
62 Status = gRT->QueryVariableInfo(EFI_VARIABLE_BOOTSERVICE_ACCESS|EFI_VARIABLE_RUNTIME_ACCESS|EFI_VARIABLE_NON_VOLATILE, &MaxStorSize, &RemStorSize, &MaxVarSize);
63 if (EFI_ERROR(Status)) {
64 return (SHELL_DEVICE_ERROR);
65 }
66
67 Found = FALSE;
68 ShellStatus = SHELL_SUCCESS;
69 Size = PcdGet16(PcdShellFileOperationSize);
70 FoundVarName = AllocateZeroPool(Size);
71
72 if (FoundVarName == NULL) {
73 return (SHELL_OUT_OF_RESOURCES);
74 }
75 FoundVarName[0] = CHAR_NULL;
76
77
78 DataSize = (UINTN)MaxVarSize;
79 DataBuffer = AllocateZeroPool(DataSize);
80 if (DataBuffer == NULL) {
81 FreePool(FoundVarName);
82 return (SHELL_OUT_OF_RESOURCES);
83 }
84
85 for (;;){
86 if (ShellGetExecutionBreakFlag()) {
87 ShellStatus = SHELL_ABORTED;
88 break;
89 }
90 Size = (UINTN)PcdGet16(PcdShellFileOperationSize);
91 DataSize = (UINTN)MaxVarSize;
92
93 Status = gRT->GetNextVariableName(&Size, FoundVarName, &FoundVarGuid);
94 if (Status == EFI_NOT_FOUND) {
95 break;
96 }
97 ASSERT_EFI_ERROR(Status);
98
99 Status = gRT->GetVariable(FoundVarName, &FoundVarGuid, &Atts, &DataSize, DataBuffer);
100 ASSERT_EFI_ERROR(Status);
101
102 //
103 // Check if it matches
104 //
105 if (VariableName != NULL) {
106 if (!gUnicodeCollation->MetaiMatch(gUnicodeCollation, FoundVarName, (CHAR16*)VariableName)) {
107 continue;
108 }
109 }
110 if (Guid != NULL) {
111 if (!CompareGuid(&FoundVarGuid, Guid)) {
112 continue;
113 }
114 }
115
116 //
117 // do the print or delete
118 //
119 Found = TRUE;
120 if (!Delete) {
121 ShellPrintHiiEx(
122 -1,
123 -1,
124 NULL,
125 STRING_TOKEN(STR_DMPSTORE_HEADER_LINE),
126 gShellDebug1HiiHandle,
127 AttrType[Atts & 7],
128 &FoundVarGuid,
129 FoundVarName,
130 DataSize);
131 DumpHex(2, 0, DataSize, DataBuffer);
132 } else {
133 ShellPrintHiiEx(
134 -1,
135 -1,
136 NULL,
137 STRING_TOKEN(STR_DMPSTORE_DELETE_LINE),
138 gShellDebug1HiiHandle,
139 &FoundVarGuid,
140 FoundVarName);
141 ShellPrintHiiEx(
142 -1,
143 -1,
144 NULL,
145 STRING_TOKEN(STR_DMPSTORE_DELETE_DONE),
146 gShellDebug1HiiHandle,
147 gRT->SetVariable(FoundVarName, &FoundVarGuid, Atts, 0, NULL));
148 FoundVarName[0] = CHAR_NULL;
149 }
150 }
151
152 if (FoundVarName != NULL) {
153 FreePool(FoundVarName);
154 }
155 if (DataBuffer != NULL) {
156 FreePool(DataBuffer);
157 }
158 if (!Found) {
159 if (VariableName != NULL && Guid == NULL) {
160 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_NO_VAR_FOUND_N), gShellDebug1HiiHandle, VariableName);
161 } else if (VariableName != NULL && Guid != NULL) {
162 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_NO_VAR_FOUND_GN), gShellDebug1HiiHandle, Guid, VariableName);
163 } else if (VariableName == NULL && Guid == NULL) {
164 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_NO_VAR_FOUND), gShellDebug1HiiHandle);
165 } else if (VariableName == NULL && Guid != NULL) {
166 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_NO_VAR_FOUND_G), gShellDebug1HiiHandle, Guid);
167 }
168 return (SHELL_NOT_FOUND);
169 }
170 return (SHELL_SUCCESS);
171 }
172
173 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
174 {L"-d", TypeFlag},
175 {L"-l", TypeFlag},
176 {L"-s", TypeFlag},
177 {L"-all", TypeFlag},
178 {L"-guid", TypeValue},
179 {NULL, TypeMax}
180 };
181
182 /**
183 Function for 'dmpstore' command.
184
185 @param[in] ImageHandle Handle to the Image (NULL if Internal).
186 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
187 **/
188 SHELL_STATUS
189 EFIAPI
190 ShellCommandRunDmpStore (
191 IN EFI_HANDLE ImageHandle,
192 IN EFI_SYSTEM_TABLE *SystemTable
193 )
194 {
195 EFI_STATUS Status;
196 LIST_ENTRY *Package;
197 CHAR16 *ProblemParam;
198 SHELL_STATUS ShellStatus;
199 CONST CHAR16 *Temp;
200 EFI_GUID *Guid;
201 EFI_GUID GuidData;
202 CONST CHAR16 *VariableName;
203
204 ShellStatus = SHELL_SUCCESS;
205 Package = NULL;
206
207 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
208 if (EFI_ERROR(Status)) {
209 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
210 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, ProblemParam);
211 FreePool(ProblemParam);
212 ShellStatus = SHELL_INVALID_PARAMETER;
213 } else {
214 ASSERT(FALSE);
215 }
216 } else {
217 if (ShellCommandLineGetCount(Package) > 2) {
218 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle);
219 ShellStatus = SHELL_INVALID_PARAMETER;
220 } else if (ShellCommandLineGetFlag(Package, L"-all") && ShellCommandLineGetFlag(Package, L"-guid")) {
221 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_CONFLICT), gShellDebug1HiiHandle, L"-all", L"-guid");
222 ShellStatus = SHELL_INVALID_PARAMETER;
223 } else if ((ShellCommandLineGetFlag(Package, L"-s") || ShellCommandLineGetFlag(Package, L"-l")) && ShellCommandLineGetFlag(Package, L"-d")) {
224 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_CONFLICT), gShellDebug1HiiHandle, L"-l or -s", L"-d");
225 ShellStatus = SHELL_INVALID_PARAMETER;
226 } else {
227 if (!ShellCommandLineGetFlag(Package, L"-all")) {
228 Temp = ShellCommandLineGetValue(Package, L"-guid");
229 if (Temp != NULL) {
230 Status = ConvertStringToGuid(Temp, &GuidData);
231 if (EFI_ERROR(Status)) {
232 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, Temp);
233 ShellStatus = SHELL_INVALID_PARAMETER;
234 }
235 Guid = &GuidData;
236 } else {
237 Guid = &gEfiGlobalVariableGuid;
238 }
239 VariableName = ShellCommandLineGetRawValue(Package, 1);
240 } else {
241 VariableName = NULL;
242 Guid = NULL;
243 }
244 if (ShellStatus == SHELL_SUCCESS) {
245 if (ShellCommandLineGetFlag(Package, L"-s") || ShellCommandLineGetFlag(Package, L"-l")) {
246 ///@todo fix this after lib ready...
247 ShellPrintEx(-1, -1, L"Not implemeneted yet.\r\n");
248 ShellStatus = SHELL_UNSUPPORTED;
249 } else {
250 ShellStatus = ProcessVariables (VariableName, Guid, ShellCommandLineGetFlag(Package, L"-d"));
251 }
252 }
253 }
254 }
255
256 if (Package != NULL) {
257 ShellCommandLineFreeVarList (Package);
258 }
259 return ShellStatus;
260 }
261