]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/SetVar.c
9e57998ff85f797be72b4caed6e7ee3bff24cc2d
[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 EFI_VARIABLE_BOOTSERVICE_ACCESS;
144 }
145 if (ShellCommandLineGetFlag(Package, L"-nv")) {
146 Attributes |= EFI_VARIABLE_NON_VOLATILE;
147 }
148 if (ShellIsHexOrDecimalNumber(Data, TRUE, FALSE)) {
149 if (StrLen(Data) % 2 != 0) {
150 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM_VAL), gShellDebug1HiiHandle, Data);
151 ShellStatus = SHELL_INVALID_PARAMETER;
152 } else {
153 //
154 // arbitrary buffer
155 //
156 Buffer = AllocateZeroPool((StrLen(Data) / 2));
157 if (Buffer == NULL) {
158 Status = EFI_OUT_OF_RESOURCES;
159 } else {
160 for (LoopVar = 0 ; LoopVar < (StrLen(Data) / 2) ; LoopVar++) {
161 ((UINT8*)Buffer)[LoopVar] = (UINT8)(HexCharToUintn(Data[LoopVar*2]) * 16);
162 ((UINT8*)Buffer)[LoopVar] = (UINT8)(((UINT8*)Buffer)[LoopVar] + HexCharToUintn(Data[LoopVar*2+1]));
163 }
164 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, StrLen(Data) / 2, Buffer);
165 }
166 if (EFI_ERROR(Status)) {
167 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, &Guid, VariableName, Status);
168 ShellStatus = SHELL_ACCESS_DENIED;
169 } else {
170 ASSERT(ShellStatus == SHELL_SUCCESS);
171 }
172 }
173 } else if (StrnCmp(Data, L"\"", 1) == 0) {
174 Size = 0;
175 Attributes2 = 0;
176 Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes2, &Size, Buffer);
177 if (Status == EFI_BUFFER_TOO_SMALL) {
178 Buffer = AllocateZeroPool(Size);
179 Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes2, &Size, Buffer);
180 if (Buffer != NULL) {
181 FreePool(Buffer);
182 }
183 Attributes = Attributes2;
184 }
185 //
186 // ascii text
187 //
188 Data++;
189 Buffer = AllocateZeroPool(StrSize(Data) / 2);
190 if (Buffer == NULL) {
191 Status = EFI_OUT_OF_RESOURCES;
192 } else {
193 AsciiSPrint(Buffer, StrSize(Data) / 2, "%s", Data);
194 ((CHAR8*)Buffer)[AsciiStrLen(Buffer)-1] = CHAR_NULL;
195 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, AsciiStrSize(Buffer)-sizeof(CHAR8), Buffer);
196 }
197 if (EFI_ERROR(Status)) {
198 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, &Guid, VariableName, Status);
199 ShellStatus = SHELL_ACCESS_DENIED;
200 } else {
201 ASSERT(ShellStatus == SHELL_SUCCESS);
202 }
203 } else if (StrnCmp(Data, L"L\"", 2) == 0) {
204 //
205 // ucs2 text
206 //
207 Data++;
208 Data++;
209 Buffer = AllocateZeroPool(StrSize(Data));
210 if (Buffer == NULL) {
211 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellDebug1HiiHandle);
212 ShellStatus = SHELL_OUT_OF_RESOURCES;
213 } else {
214 UnicodeSPrint(Buffer, StrSize(Data), L"%s", Data);
215 ((CHAR16*)Buffer)[StrLen(Buffer)-1] = CHAR_NULL;
216
217 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, StrSize(Buffer)-sizeof(CHAR16), Buffer);
218 if (EFI_ERROR(Status)) {
219 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, &Guid, VariableName, Status);
220 ShellStatus = SHELL_ACCESS_DENIED;
221 } else {
222 ASSERT(ShellStatus == SHELL_SUCCESS);
223 }
224 }
225 } else if (StrnCmp(Data, L"--", 2) == 0) {
226 //
227 // device path in text format
228 //
229 Data++;
230 Data++;
231 DevPath = ConvertTextToDevicePath(Data);
232 if (DevPath == NULL) {
233 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_DPFT), gShellDebug1HiiHandle, Status);
234 ShellStatus = SHELL_INVALID_PARAMETER;
235 } else {
236 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, GetDevicePathSize(DevPath), DevPath);
237 if (EFI_ERROR(Status)) {
238 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, &Guid, VariableName, Status);
239 ShellStatus = SHELL_ACCESS_DENIED;
240 } else {
241 ASSERT(ShellStatus == SHELL_SUCCESS);
242 }
243 }
244 } else {
245 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, Data);
246 ShellStatus = SHELL_INVALID_PARAMETER;
247 }
248 }
249 }
250 ShellCommandLineFreeVarList (Package);
251 }
252
253 if (Buffer != NULL) {
254 FreePool(Buffer);
255 }
256
257 if (DevPath != NULL) {
258 FreePool(DevPath);
259 }
260
261 return (ShellStatus);
262 }