]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/DmpStore.c
be3d6f64724a51714d7479438c4c5a3818a896e1
[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
18 #define INIT_NAME_BUFFER_SIZE 128
19 #define INIT_DATA_BUFFER_SIZE 1024
20 #define INIT_ATTS_BUFFER_SIZE 64
21
22 /**
23 Base on the input attribute value to return the attribute string.
24
25 @param[in] Atts The input attribute value
26 @param[in,out] RetString The buffer to save the attribute string.
27
28 @retval The attribute string info.
29 **/
30 CONST CHAR16 *
31 EFIAPI
32 GetAttrType (
33 IN CONST UINT32 Atts,
34 IN OUT CHAR16 *RetString
35 )
36 {
37 StrCpy(RetString, L"");
38
39 if ((Atts & EFI_VARIABLE_NON_VOLATILE) != 0) {
40 StrCat(RetString, L"+NV");
41 }
42 if ((Atts & EFI_VARIABLE_RUNTIME_ACCESS) != 0) {
43 StrCat(RetString, L"+RS+BS");
44 } else if ((Atts & EFI_VARIABLE_BOOTSERVICE_ACCESS) != 0) {
45 StrCat(RetString, L"+BS");
46 }
47 if ((Atts & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {
48 StrCat(RetString, L"+HR");
49 }
50 if ((Atts & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {
51 StrCat(RetString, L"+AW");
52 }
53 if ((Atts & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) {
54 StrCat(RetString, L"+AT");
55 }
56
57 if (RetString[0] == L'+') {
58 return (RetString+1);
59 }
60 if (RetString[0] == CHAR_NULL) {
61 StrCpy(RetString, L"invalid");
62 return (RetString);
63 }
64 return (RetString);
65 }
66
67 /**
68 Function to display or delete variables.
69
70 @param[in] VariableName The variable name of the EFI variable (or NULL).
71 @param[in] Guid The GUID of the variable set (or NULL).
72 @param[in] Delete TRUE to delete, FALSE otherwise.
73
74 @retval SHELL_SUCCESS The operation was successful.
75 @retval SHELL_OUT_OF_RESOURCES A memorty allocation failed.
76 @retval SHELL_ABORTED The abort message was received.
77 @retval SHELL_DEVICE_ERROR UEFI Variable Services returned an error.
78 @retval SHELL_NOT_FOUND the Name/Guid pair could not be found.
79 **/
80 SHELL_STATUS
81 EFIAPI
82 ProcessVariables (
83 IN CONST CHAR16 *VariableName OPTIONAL,
84 IN CONST EFI_GUID *Guid OPTIONAL,
85 IN BOOLEAN Delete
86 )
87 {
88 EFI_STATUS Status;
89 CHAR16 *FoundVarName;
90 EFI_GUID FoundVarGuid;
91 UINT8 *DataBuffer;
92 UINTN DataSize;
93 UINT32 Atts;
94 SHELL_STATUS ShellStatus;
95 BOOLEAN Found;
96 UINTN NameBufferSize; // Allocated Name buffer size
97 UINTN NameSize;
98 CHAR16 *OldName;
99 UINTN OldNameBufferSize;
100 UINTN DataBufferSize; // Allocated data buffer size
101 CHAR16 RetString[INIT_ATTS_BUFFER_SIZE];
102
103 Found = FALSE;
104 ShellStatus = SHELL_SUCCESS;
105 Status = EFI_SUCCESS;
106
107 NameBufferSize = INIT_NAME_BUFFER_SIZE;
108 DataBufferSize = INIT_DATA_BUFFER_SIZE;
109 FoundVarName = AllocateZeroPool (NameBufferSize);
110 if (FoundVarName == NULL) {
111 return (SHELL_OUT_OF_RESOURCES);
112 }
113 DataBuffer = AllocatePool (DataBufferSize);
114 if (DataBuffer == NULL) {
115 FreePool (FoundVarName);
116 return (SHELL_OUT_OF_RESOURCES);
117 }
118
119 for (;;){
120 if (ShellGetExecutionBreakFlag()) {
121 ShellStatus = SHELL_ABORTED;
122 break;
123 }
124
125 NameSize = NameBufferSize;
126 Status = gRT->GetNextVariableName (&NameSize, FoundVarName, &FoundVarGuid);
127 if (Status == EFI_BUFFER_TOO_SMALL) {
128 OldName = FoundVarName;
129 OldNameBufferSize = NameBufferSize;
130 //
131 // Expand at least twice to avoid reallocate many times
132 //
133 NameBufferSize = NameSize > NameBufferSize * 2 ? NameSize : NameBufferSize * 2;
134 FoundVarName = AllocateZeroPool (NameBufferSize);
135 if (FoundVarName == NULL) {
136 Status = EFI_OUT_OF_RESOURCES;
137 FreePool (OldName);
138 break;
139 }
140 //
141 // Preserve the original content to get correct iteration for GetNextVariableName() call
142 //
143 CopyMem (FoundVarName, OldName, OldNameBufferSize);
144 FreePool (OldName);
145 NameSize = NameBufferSize;
146 Status = gRT->GetNextVariableName (&NameSize, FoundVarName, &FoundVarGuid);
147 }
148 if (Status == EFI_NOT_FOUND) {
149 break;
150 }
151 ASSERT_EFI_ERROR(Status);
152
153 //
154 // Check if it matches
155 //
156 if (VariableName != NULL) {
157 if (!gUnicodeCollation->MetaiMatch(gUnicodeCollation, FoundVarName, (CHAR16*)VariableName)) {
158 continue;
159 }
160 }
161 if (Guid != NULL) {
162 if (!CompareGuid(&FoundVarGuid, Guid)) {
163 continue;
164 }
165 }
166
167 DataSize = DataBufferSize;
168 Status = gRT->GetVariable (FoundVarName, &FoundVarGuid, &Atts, &DataSize, DataBuffer);
169 if (Status == EFI_BUFFER_TOO_SMALL) {
170 //
171 // Expand at least twice to avoid reallocate many times
172 //
173 FreePool (DataBuffer);
174 DataBufferSize = DataSize > DataBufferSize * 2 ? DataSize : DataBufferSize * 2;
175 DataBuffer = AllocatePool (DataBufferSize);
176 if (DataBuffer == NULL) {
177 Status = EFI_OUT_OF_RESOURCES;
178 break;
179 }
180 DataSize = DataBufferSize;
181 Status = gRT->GetVariable (FoundVarName, &FoundVarGuid, &Atts, &DataSize, DataBuffer);
182 }
183 ASSERT_EFI_ERROR(Status);
184
185 //
186 // do the print or delete
187 //
188 Found = TRUE;
189 if (!Delete) {
190 ShellPrintHiiEx(
191 -1,
192 -1,
193 NULL,
194 STRING_TOKEN(STR_DMPSTORE_HEADER_LINE),
195 gShellDebug1HiiHandle,
196 GetAttrType(Atts, RetString),
197 &FoundVarGuid,
198 FoundVarName,
199 DataSize);
200 DumpHex(2, 0, DataSize, DataBuffer);
201 } else {
202 ShellPrintHiiEx(
203 -1,
204 -1,
205 NULL,
206 STRING_TOKEN(STR_DMPSTORE_DELETE_LINE),
207 gShellDebug1HiiHandle,
208 &FoundVarGuid,
209 FoundVarName);
210 ShellPrintHiiEx(
211 -1,
212 -1,
213 NULL,
214 STRING_TOKEN(STR_DMPSTORE_DELETE_DONE),
215 gShellDebug1HiiHandle,
216 gRT->SetVariable(FoundVarName, &FoundVarGuid, Atts, 0, NULL));
217 FoundVarName[0] = CHAR_NULL;
218 }
219 }
220
221 if (FoundVarName != NULL) {
222 FreePool(FoundVarName);
223 }
224 if (DataBuffer != NULL) {
225 FreePool(DataBuffer);
226 }
227 if (!Found) {
228 if (Status == EFI_OUT_OF_RESOURCES) {
229 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellDebug1HiiHandle);
230 return SHELL_OUT_OF_RESOURCES;
231 }
232
233 if (VariableName != NULL && Guid == NULL) {
234 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_NO_VAR_FOUND_N), gShellDebug1HiiHandle, VariableName);
235 } else if (VariableName != NULL && Guid != NULL) {
236 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_NO_VAR_FOUND_GN), gShellDebug1HiiHandle, Guid, VariableName);
237 } else if (VariableName == NULL && Guid == NULL) {
238 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_NO_VAR_FOUND), gShellDebug1HiiHandle);
239 } else if (VariableName == NULL && Guid != NULL) {
240 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_NO_VAR_FOUND_G), gShellDebug1HiiHandle, Guid);
241 }
242 return (SHELL_NOT_FOUND);
243 }
244 return (SHELL_SUCCESS);
245 }
246
247 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
248 {L"-d", TypeFlag},
249 {L"-l", TypeFlag},
250 {L"-s", TypeFlag},
251 {L"-all", TypeFlag},
252 {L"-guid", TypeValue},
253 {NULL, TypeMax}
254 };
255
256 /**
257 Function for 'dmpstore' command.
258
259 @param[in] ImageHandle Handle to the Image (NULL if Internal).
260 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
261 **/
262 SHELL_STATUS
263 EFIAPI
264 ShellCommandRunDmpStore (
265 IN EFI_HANDLE ImageHandle,
266 IN EFI_SYSTEM_TABLE *SystemTable
267 )
268 {
269 EFI_STATUS Status;
270 LIST_ENTRY *Package;
271 CHAR16 *ProblemParam;
272 SHELL_STATUS ShellStatus;
273 CONST CHAR16 *Temp;
274 EFI_GUID *Guid;
275 EFI_GUID GuidData;
276 CONST CHAR16 *VariableName;
277
278 ShellStatus = SHELL_SUCCESS;
279 Package = NULL;
280
281 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
282 if (EFI_ERROR(Status)) {
283 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
284 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, ProblemParam);
285 FreePool(ProblemParam);
286 ShellStatus = SHELL_INVALID_PARAMETER;
287 } else {
288 ASSERT(FALSE);
289 }
290 } else {
291 if (ShellCommandLineGetCount(Package) > 2) {
292 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle);
293 ShellStatus = SHELL_INVALID_PARAMETER;
294 } else if (ShellCommandLineGetFlag(Package, L"-all") && ShellCommandLineGetFlag(Package, L"-guid")) {
295 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_CONFLICT), gShellDebug1HiiHandle, L"-all", L"-guid");
296 ShellStatus = SHELL_INVALID_PARAMETER;
297 } else if ((ShellCommandLineGetFlag(Package, L"-s") || ShellCommandLineGetFlag(Package, L"-l")) && ShellCommandLineGetFlag(Package, L"-d")) {
298 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_CONFLICT), gShellDebug1HiiHandle, L"-l or -s", L"-d");
299 ShellStatus = SHELL_INVALID_PARAMETER;
300 } else {
301 if (!ShellCommandLineGetFlag(Package, L"-all")) {
302 Temp = ShellCommandLineGetValue(Package, L"-guid");
303 if (Temp != NULL) {
304 Status = ConvertStringToGuid(Temp, &GuidData);
305 if (EFI_ERROR(Status)) {
306 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, Temp);
307 ShellStatus = SHELL_INVALID_PARAMETER;
308 }
309 Guid = &GuidData;
310 } else {
311 Guid = &gEfiGlobalVariableGuid;
312 }
313 VariableName = ShellCommandLineGetRawValue(Package, 1);
314 } else {
315 VariableName = NULL;
316 Guid = NULL;
317 }
318 if (ShellStatus == SHELL_SUCCESS) {
319 if (ShellCommandLineGetFlag(Package, L"-s") || ShellCommandLineGetFlag(Package, L"-l")) {
320 ///@todo fix this after lib ready...
321 ShellPrintEx(-1, -1, L"Not implemeneted yet.\r\n");
322 ShellStatus = SHELL_UNSUPPORTED;
323 } else {
324 ShellStatus = ProcessVariables (VariableName, Guid, ShellCommandLineGetFlag(Package, L"-d"));
325 }
326 }
327 }
328 }
329
330 if (Package != NULL) {
331 ShellCommandLineFreeVarList (Package);
332 }
333 return ShellStatus;
334 }
335