]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/DmpStore.c
pointer verification (not NULL) and buffer overrun fixes.
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / DmpStore.c
1 /** @file
2 Main file for DmpStore shell Debug1 function.
3
4 Copyright (c) 2005 - 2011, 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 }
149 }
150
151 if (FoundVarName != NULL) {
152 FreePool(FoundVarName);
153 }
154 if (DataBuffer != NULL) {
155 FreePool(DataBuffer);
156 }
157 if (!Found) {
158 if (VariableName != NULL && Guid == NULL) {
159 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_NO_VAR_FOUND_N), gShellDebug1HiiHandle, VariableName);
160 } else if (VariableName != NULL && Guid != NULL) {
161 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_NO_VAR_FOUND_GN), gShellDebug1HiiHandle, Guid, VariableName);
162 } else if (VariableName == NULL && Guid == NULL) {
163 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_NO_VAR_FOUND), gShellDebug1HiiHandle);
164 } else if (VariableName == NULL && Guid != NULL) {
165 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_NO_VAR_FOUND_G), gShellDebug1HiiHandle, Guid);
166 }
167 return (SHELL_NOT_FOUND);
168 }
169 return (SHELL_SUCCESS);
170 }
171
172 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
173 {L"-d", TypeFlag},
174 {L"-l", TypeFlag},
175 {L"-s", TypeFlag},
176 {L"-all", TypeFlag},
177 {L"-guid", TypeValue},
178 {NULL, TypeMax}
179 };
180
181 /**
182 Function for 'dmpstore' command.
183
184 @param[in] ImageHandle Handle to the Image (NULL if Internal).
185 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
186 **/
187 SHELL_STATUS
188 EFIAPI
189 ShellCommandRunDmpStore (
190 IN EFI_HANDLE ImageHandle,
191 IN EFI_SYSTEM_TABLE *SystemTable
192 )
193 {
194 EFI_STATUS Status;
195 LIST_ENTRY *Package;
196 CHAR16 *ProblemParam;
197 SHELL_STATUS ShellStatus;
198 CONST CHAR16 *Temp;
199 EFI_GUID *Guid;
200 EFI_GUID GuidData;
201 CONST CHAR16 *VariableName;
202
203 ShellStatus = SHELL_SUCCESS;
204 Package = NULL;
205
206 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
207 if (EFI_ERROR(Status)) {
208 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
209 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, ProblemParam);
210 FreePool(ProblemParam);
211 ShellStatus = SHELL_INVALID_PARAMETER;
212 } else {
213 ASSERT(FALSE);
214 }
215 } else {
216 if (ShellCommandLineGetCount(Package) > 2) {
217 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle);
218 ShellStatus = SHELL_INVALID_PARAMETER;
219 } else if (ShellCommandLineGetFlag(Package, L"-all") && ShellCommandLineGetFlag(Package, L"-guid")) {
220 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_CONFLICT), gShellDebug1HiiHandle, L"-all", L"-guid");
221 ShellStatus = SHELL_INVALID_PARAMETER;
222 } else if ((ShellCommandLineGetFlag(Package, L"-s") || ShellCommandLineGetFlag(Package, L"-l")) && ShellCommandLineGetFlag(Package, L"-d")) {
223 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_CONFLICT), gShellDebug1HiiHandle, L"-l or -s", L"-d");
224 ShellStatus = SHELL_INVALID_PARAMETER;
225 } else {
226 if (!ShellCommandLineGetFlag(Package, L"-all")) {
227 Temp = ShellCommandLineGetValue(Package, L"-guid");
228 if (Temp != NULL) {
229 Status = ConvertStringToGuid(Temp, &GuidData);
230 if (EFI_ERROR(Status)) {
231 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, Temp);
232 ShellStatus = SHELL_INVALID_PARAMETER;
233 }
234 Guid = &GuidData;
235 } else {
236 Guid = &gEfiGlobalVariableGuid;
237 }
238 VariableName = ShellCommandLineGetRawValue(Package, 1);
239 } else {
240 VariableName = NULL;
241 Guid = NULL;
242 }
243 if (ShellStatus == SHELL_SUCCESS) {
244 if (ShellCommandLineGetFlag(Package, L"-s") || ShellCommandLineGetFlag(Package, L"-l")) {
245 ///@todo fix this after Jordan makes lib...
246 ShellPrintEx(-1, -1, L"Not implemeneted yet.\r\n");
247 ShellStatus = SHELL_UNSUPPORTED;
248 } else {
249 ShellStatus = ProcessVariables (VariableName, Guid, ShellCommandLineGetFlag(Package, L"-d"));
250 }
251 }
252 }
253 }
254
255 if (Package != NULL) {
256 ShellCommandLineFreeVarList (Package);
257 }
258 return ShellStatus;
259 }
260