]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/SetVar.c
ShellPkg: Handle pool allocation failure
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / SetVar.c
1 /** @file
2 Main file for SetVar shell Debug1 function.
3
4 Copyright (c) 2010 - 2013, 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 CONST SHELL_PARAM_ITEM ParamList[] = {
18 {L"-guid", TypeValue},
19 {L"-bs", TypeFlag},
20 {L"-rt", TypeFlag},
21 {L"-nv", TypeFlag},
22 {NULL, TypeMax}
23 };
24
25 /**
26 Function for 'setvar' command.
27
28 @param[in] ImageHandle Handle to the Image (NULL if Internal).
29 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
30 **/
31 SHELL_STATUS
32 EFIAPI
33 ShellCommandRunSetVar (
34 IN EFI_HANDLE ImageHandle,
35 IN EFI_SYSTEM_TABLE *SystemTable
36 )
37 {
38 EFI_STATUS Status;
39 LIST_ENTRY *Package;
40 CHAR16 *ProblemParam;
41 SHELL_STATUS ShellStatus;
42 CONST CHAR16 *VariableName;
43 CONST CHAR16 *Data;
44 EFI_GUID Guid;
45 CONST CHAR16 *StringGuid;
46 UINT32 Attributes;
47 UINT32 Attributes2;
48 VOID *Buffer;
49 UINTN Size;
50 UINTN LoopVar;
51 EFI_DEVICE_PATH_PROTOCOL *DevPath;
52
53 ShellStatus = SHELL_SUCCESS;
54 Status = EFI_SUCCESS;
55 Buffer = NULL;
56 Size = 0;
57 Attributes = 0;
58 DevPath = NULL;
59
60 //
61 // initialize the shell lib (we must be in non-auto-init...)
62 //
63 Status = ShellInitialize();
64 ASSERT_EFI_ERROR(Status);
65
66 Status = CommandInit();
67 ASSERT_EFI_ERROR(Status);
68
69 //
70 // parse the command line
71 //
72 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
73 if (EFI_ERROR(Status)) {
74 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
75 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, ProblemParam);
76 FreePool(ProblemParam);
77 ShellStatus = SHELL_INVALID_PARAMETER;
78 } else {
79 ASSERT(FALSE);
80 }
81 } else {
82 if (ShellCommandLineGetCount(Package) < 2) {
83 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle);
84 ShellStatus = SHELL_INVALID_PARAMETER;
85 } else if (ShellCommandLineGetCount(Package) > 3) {
86 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle);
87 ShellStatus = SHELL_INVALID_PARAMETER;
88 } else {
89 VariableName = ShellCommandLineGetRawValue(Package, 1);
90 Data = ShellCommandLineGetRawValue(Package, 2);
91 if (!ShellCommandLineGetFlag(Package, L"-guid")){
92 CopyGuid(&Guid, &gEfiGlobalVariableGuid);
93 } else {
94 StringGuid = ShellCommandLineGetValue(Package, L"-guid");
95 Status = ConvertStringToGuid(StringGuid, &Guid);
96 if (EFI_ERROR(Status)) {
97 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, StringGuid);
98 ShellStatus = SHELL_INVALID_PARAMETER;
99 }
100 }
101 if (Data == NULL) {
102 //
103 // Display what's there
104 //
105 Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes, &Size, Buffer);
106 if (Status == EFI_BUFFER_TOO_SMALL) {
107 Buffer = AllocateZeroPool(Size);
108 Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes, &Size, Buffer);
109 }
110 if (!EFI_ERROR(Status)&& Buffer != NULL) {
111 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_PRINT), gShellDebug1HiiHandle, &Guid, VariableName, Size);
112 for (LoopVar = 0 ; LoopVar < Size ; LoopVar++) {
113 ShellPrintEx(-1, -1, L"%02x ", ((UINT8*)Buffer)[LoopVar]);
114 }
115 ShellPrintEx(-1, -1, L"\r\n");
116 } else {
117 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_GET), gShellDebug1HiiHandle, &Guid, VariableName, Status);
118 ShellStatus = SHELL_ACCESS_DENIED;
119 }
120 } else if (StrCmp(Data, L"=") == 0) {
121 //
122 // Delete what's there!
123 //
124 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, 0, NULL);
125 if (EFI_ERROR(Status)) {
126 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, &Guid, VariableName, Status);
127 ShellStatus = SHELL_ACCESS_DENIED;
128 } else {
129 ASSERT(ShellStatus == SHELL_SUCCESS);
130 }
131 } else {
132 if (Data[0] == L'=') {
133 Data++;
134 }
135 //
136 // Change what's there
137 //
138 if (ShellCommandLineGetFlag(Package, L"-bs")) {
139 Attributes |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
140 }
141 if (ShellCommandLineGetFlag(Package, L"-rt")) {
142 Attributes |= EFI_VARIABLE_RUNTIME_ACCESS;
143 }
144 if (ShellCommandLineGetFlag(Package, L"-nv")) {
145 Attributes |= EFI_VARIABLE_NON_VOLATILE;
146 }
147 if (ShellIsHexOrDecimalNumber(Data, TRUE, FALSE)) {
148 if (StrLen(Data) % 2 != 0) {
149 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM_VAL), gShellDebug1HiiHandle, Data);
150 ShellStatus = SHELL_INVALID_PARAMETER;
151 } else {
152 //
153 // arbitrary buffer
154 //
155 Buffer = AllocateZeroPool((StrLen(Data) / 2));
156 if (Buffer == NULL) {
157 Status = EFI_OUT_OF_RESOURCES;
158 } else {
159 for (LoopVar = 0 ; LoopVar < (StrLen(Data) / 2) ; LoopVar++) {
160 ((UINT8*)Buffer)[LoopVar] = (UINT8)(HexCharToUintn(Data[LoopVar*2]) * 16);
161 ((UINT8*)Buffer)[LoopVar] = (UINT8)(((UINT8*)Buffer)[LoopVar] + HexCharToUintn(Data[LoopVar*2+1]));
162 }
163 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, StrLen(Data) / 2, Buffer);
164 }
165 if (EFI_ERROR(Status)) {
166 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, &Guid, VariableName, Status);
167 ShellStatus = SHELL_ACCESS_DENIED;
168 } else {
169 ASSERT(ShellStatus == SHELL_SUCCESS);
170 }
171 }
172 } else if (StrnCmp(Data, L"\"", 1) == 0) {
173 Size = 0;
174 Attributes2 = 0;
175 Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes2, &Size, Buffer);
176 if (Status == EFI_BUFFER_TOO_SMALL) {
177 Buffer = AllocateZeroPool(Size);
178 Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes2, &Size, Buffer);
179 if (Buffer != NULL) {
180 FreePool(Buffer);
181 }
182 Attributes = Attributes2;
183 }
184 //
185 // ascii text
186 //
187 Data++;
188 Buffer = AllocateZeroPool(StrSize(Data) / 2);
189 if (Buffer == NULL) {
190 Status = EFI_OUT_OF_RESOURCES;
191 } else {
192 AsciiSPrint(Buffer, StrSize(Data) / 2, "%s", Data);
193 ((CHAR8*)Buffer)[AsciiStrLen(Buffer)-1] = CHAR_NULL;
194 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, AsciiStrSize(Buffer)-sizeof(CHAR8), Buffer);
195 }
196 if (EFI_ERROR(Status)) {
197 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, &Guid, VariableName, Status);
198 ShellStatus = SHELL_ACCESS_DENIED;
199 } else {
200 ASSERT(ShellStatus == SHELL_SUCCESS);
201 }
202 } else if (StrnCmp(Data, L"L\"", 2) == 0) {
203 //
204 // ucs2 text
205 //
206 Data++;
207 Data++;
208 Buffer = AllocateZeroPool(StrSize(Data));
209 if (Buffer == NULL) {
210 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellDebug1HiiHandle);
211 ShellStatus = SHELL_OUT_OF_RESOURCES;
212 } else {
213 UnicodeSPrint(Buffer, StrSize(Data), L"%s", Data);
214 ((CHAR16*)Buffer)[StrLen(Buffer)-1] = CHAR_NULL;
215
216 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, StrSize(Buffer)-sizeof(CHAR16), Buffer);
217 if (EFI_ERROR(Status)) {
218 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, &Guid, VariableName, Status);
219 ShellStatus = SHELL_ACCESS_DENIED;
220 } else {
221 ASSERT(ShellStatus == SHELL_SUCCESS);
222 }
223 }
224 } else if (StrnCmp(Data, L"--", 2) == 0) {
225 //
226 // device path in text format
227 //
228 Data++;
229 Data++;
230 DevPath = ConvertTextToDevicePath(Data);
231 if (DevPath == NULL) {
232 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_DPFT), gShellDebug1HiiHandle, Status);
233 ShellStatus = SHELL_INVALID_PARAMETER;
234 } else {
235 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, GetDevicePathSize(DevPath), DevPath);
236 if (EFI_ERROR(Status)) {
237 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, &Guid, VariableName, Status);
238 ShellStatus = SHELL_ACCESS_DENIED;
239 } else {
240 ASSERT(ShellStatus == SHELL_SUCCESS);
241 }
242 }
243 } else {
244 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, Data);
245 ShellStatus = SHELL_INVALID_PARAMETER;
246 }
247 }
248 }
249 ShellCommandLineFreeVarList (Package);
250 }
251
252 if (Buffer != NULL) {
253 FreePool(Buffer);
254 }
255
256 if (DevPath != NULL) {
257 FreePool(DevPath);
258 }
259
260 return (ShellStatus);
261 }