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