]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/SetVar.c
ShellPkg/Debug1CommandLib: Use StrToGuid/StrHexToBytes in BaseLib
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / SetVar.c
1 /** @file
2 Main file for SetVar shell Debug1 function.
3
4 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "UefiShellDebug1CommandsLib.h"
17
18 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
19 {L"-guid", TypeValue},
20 {L"-bs", TypeFlag},
21 {L"-rt", TypeFlag},
22 {L"-nv", TypeFlag},
23 {NULL, TypeMax}
24 };
25
26
27 /**
28 Check if the input is a (potentially empty) string of hexadecimal nibbles.
29
30 @param[in] String The CHAR16 string to check.
31
32 @retval FALSE A character has been found in String for which
33 ShellIsHexaDecimalDigitCharacter() returned FALSE.
34
35 @retval TRUE Otherwise. (Note that this covers the case when String is
36 empty.)
37 **/
38 BOOLEAN
39 IsStringOfHexNibbles (
40 IN CONST CHAR16 *String
41 )
42 {
43 CONST CHAR16 *Pos;
44
45 for (Pos = String; *Pos != L'\0'; ++Pos) {
46 if (!ShellIsHexaDecimalDigitCharacter (*Pos)) {
47 return FALSE;
48 }
49 }
50 return TRUE;
51 }
52
53
54 /**
55 Function for 'setvar' command.
56
57 @param[in] ImageHandle Handle to the Image (NULL if Internal).
58 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
59 **/
60 SHELL_STATUS
61 EFIAPI
62 ShellCommandRunSetVar (
63 IN EFI_HANDLE ImageHandle,
64 IN EFI_SYSTEM_TABLE *SystemTable
65 )
66 {
67 EFI_STATUS Status;
68 RETURN_STATUS RStatus;
69 LIST_ENTRY *Package;
70 CHAR16 *ProblemParam;
71 SHELL_STATUS ShellStatus;
72 CONST CHAR16 *VariableName;
73 CONST CHAR16 *Data;
74 EFI_GUID Guid;
75 CONST CHAR16 *StringGuid;
76 UINT32 Attributes;
77 VOID *Buffer;
78 UINTN Size;
79 UINTN LoopVar;
80 EFI_DEVICE_PATH_PROTOCOL *DevPath;
81
82 ShellStatus = SHELL_SUCCESS;
83 Status = EFI_SUCCESS;
84 Buffer = NULL;
85 Size = 0;
86 Attributes = 0;
87 DevPath = NULL;
88
89 //
90 // initialize the shell lib (we must be in non-auto-init...)
91 //
92 Status = ShellInitialize();
93 ASSERT_EFI_ERROR(Status);
94
95 Status = CommandInit();
96 ASSERT_EFI_ERROR(Status);
97
98 //
99 // parse the command line
100 //
101 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
102 if (EFI_ERROR(Status)) {
103 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
104 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"setvar", ProblemParam);
105 FreePool(ProblemParam);
106 ShellStatus = SHELL_INVALID_PARAMETER;
107 } else {
108 ASSERT(FALSE);
109 }
110 } else {
111 if (ShellCommandLineGetCount(Package) < 2) {
112 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle, L"setvar");
113 ShellStatus = SHELL_INVALID_PARAMETER;
114 } else if (ShellCommandLineGetCount(Package) > 3) {
115 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"setvar");
116 ShellStatus = SHELL_INVALID_PARAMETER;
117 } else {
118 VariableName = ShellCommandLineGetRawValue(Package, 1);
119 Data = ShellCommandLineGetRawValue(Package, 2);
120 if (!ShellCommandLineGetFlag(Package, L"-guid")){
121 CopyGuid(&Guid, &gEfiGlobalVariableGuid);
122 } else {
123 StringGuid = ShellCommandLineGetValue(Package, L"-guid");
124 RStatus = StrToGuid (StringGuid, &Guid);
125 if (RETURN_ERROR (RStatus) || (StringGuid[GUID_STRING_LENGTH] != L'\0')) {
126 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"setvar", StringGuid);
127 ShellStatus = SHELL_INVALID_PARAMETER;
128 }
129 }
130 if (Data == NULL || Data[0] != L'=') {
131 //
132 // Display what's there
133 //
134 Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes, &Size, Buffer);
135 if (Status == EFI_BUFFER_TOO_SMALL) {
136 Buffer = AllocateZeroPool(Size);
137 Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes, &Size, Buffer);
138 }
139 if (!EFI_ERROR(Status)&& Buffer != NULL) {
140 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_PRINT), gShellDebug1HiiHandle, &Guid, VariableName, Size);
141 for (LoopVar = 0 ; LoopVar < Size ; LoopVar++) {
142 ShellPrintEx(-1, -1, L"%02x ", ((UINT8*)Buffer)[LoopVar]);
143 }
144 ShellPrintEx(-1, -1, L"\r\n");
145 } else {
146 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_GET), gShellDebug1HiiHandle, L"setvar", &Guid, VariableName);
147 ShellStatus = SHELL_ACCESS_DENIED;
148 }
149 } else if (StrCmp(Data, L"=") == 0) {
150 //
151 // Delete what's there!
152 //
153 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, 0, NULL);
154 if (EFI_ERROR(Status)) {
155 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, L"setvar", &Guid, VariableName);
156 ShellStatus = SHELL_ACCESS_DENIED;
157 } else {
158 ASSERT(ShellStatus == SHELL_SUCCESS);
159 }
160 } else {
161 //
162 // Change what's there or create a new one.
163 //
164
165 ASSERT(Data[0] == L'=');
166 Data++;
167 ASSERT(Data[0] != L'\0');
168
169 //
170 // Determine if the variable exists and get the attributes
171 //
172 Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes, &Size, Buffer);
173 if (Status == EFI_BUFFER_TOO_SMALL) {
174 Buffer = AllocateZeroPool(Size);
175 Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes, &Size, Buffer);
176 }
177
178 if (EFI_ERROR(Status) || Buffer == NULL) {
179 //
180 // Creating a new variable. determine attributes from command line.
181 //
182 Attributes = 0;
183 if (ShellCommandLineGetFlag(Package, L"-bs")) {
184 Attributes |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
185 }
186 if (ShellCommandLineGetFlag(Package, L"-rt")) {
187 Attributes |= EFI_VARIABLE_RUNTIME_ACCESS |
188 EFI_VARIABLE_BOOTSERVICE_ACCESS;
189 }
190 if (ShellCommandLineGetFlag(Package, L"-nv")) {
191 Attributes |= EFI_VARIABLE_NON_VOLATILE;
192 }
193 }
194 SHELL_FREE_NON_NULL(Buffer);
195
196 //
197 // What type is the new data.
198 //
199 if (IsStringOfHexNibbles(Data)) {
200 if (StrLen(Data) % 2 != 0) {
201 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"setvar", Data);
202 ShellStatus = SHELL_INVALID_PARAMETER;
203 } else {
204 //
205 // arbitrary buffer
206 //
207 Buffer = AllocateZeroPool((StrLen(Data) / 2));
208 if (Buffer == NULL) {
209 Status = EFI_OUT_OF_RESOURCES;
210 } else {
211 StrHexToBytes (Data, StrLen (Data), Buffer, StrLen (Data) / 2);
212 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, StrLen(Data) / 2, Buffer);
213 }
214 if (EFI_ERROR(Status)) {
215 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, L"setvar", &Guid, VariableName);
216 ShellStatus = SHELL_ACCESS_DENIED;
217 } else {
218 ASSERT(ShellStatus == SHELL_SUCCESS);
219 }
220 }
221 } else if (StrnCmp(Data, L"\"", 1) == 0) {
222 //
223 // ascii text
224 //
225 Data++;
226 Buffer = AllocateZeroPool(StrSize(Data) / 2);
227 if (Buffer == NULL) {
228 Status = EFI_OUT_OF_RESOURCES;
229 } else {
230 AsciiSPrint(Buffer, StrSize(Data) / 2, "%s", Data);
231 ((CHAR8*)Buffer)[AsciiStrLen(Buffer)-1] = CHAR_NULL;
232 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, AsciiStrSize(Buffer)-sizeof(CHAR8), Buffer);
233 }
234 if (EFI_ERROR(Status)) {
235 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, L"setvar", &Guid, VariableName);
236 ShellStatus = SHELL_ACCESS_DENIED;
237 } else {
238 ASSERT(ShellStatus == SHELL_SUCCESS);
239 }
240 } else if (StrnCmp(Data, L"L\"", 2) == 0) {
241 //
242 // ucs2 text
243 //
244 Data++;
245 Data++;
246 Buffer = AllocateZeroPool(StrSize(Data));
247 if (Buffer == NULL) {
248 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellDebug1HiiHandle, L"setvar");
249 ShellStatus = SHELL_OUT_OF_RESOURCES;
250 } else {
251 UnicodeSPrint(Buffer, StrSize(Data), L"%s", Data);
252 ((CHAR16*)Buffer)[StrLen(Buffer)-1] = CHAR_NULL;
253
254 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, StrSize(Buffer)-sizeof(CHAR16), Buffer);
255 if (EFI_ERROR(Status)) {
256 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, L"setvar", &Guid, VariableName);
257 ShellStatus = SHELL_ACCESS_DENIED;
258 } else {
259 ASSERT(ShellStatus == SHELL_SUCCESS);
260 }
261 }
262 } else if (StrnCmp(Data, L"--", 2) == 0) {
263 //
264 // device path in text format
265 //
266 Data++;
267 Data++;
268 DevPath = ConvertTextToDevicePath(Data);
269 if (DevPath == NULL) {
270 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_DPFT), gShellDebug1HiiHandle, L"setvar");
271 ShellStatus = SHELL_INVALID_PARAMETER;
272 } else {
273 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, GetDevicePathSize(DevPath), DevPath);
274 if (EFI_ERROR(Status)) {
275 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, L"setvar", &Guid, VariableName);
276 ShellStatus = SHELL_ACCESS_DENIED;
277 } else {
278 ASSERT(ShellStatus == SHELL_SUCCESS);
279 }
280 }
281 } else {
282 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"setvar", Data);
283 ShellStatus = SHELL_INVALID_PARAMETER;
284 }
285 }
286 }
287 ShellCommandLineFreeVarList (Package);
288 }
289
290 if (Buffer != NULL) {
291 FreePool(Buffer);
292 }
293
294 if (DevPath != NULL) {
295 FreePool(DevPath);
296 }
297
298 return (ShellStatus);
299 }