]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/SetVar.c
Update all the code to consume the ConvertDevicePathToText, ConvertDevicePathNodeToTe...
[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 FreePool(Buffer);
180 Attributes = Attributes2;
181 }
182 //
183 // ascii text
184 //
185 Data++;
186 Buffer = AllocateZeroPool(StrSize(Data) / 2);
187 if (Buffer == NULL) {
188 Status = EFI_OUT_OF_RESOURCES;
189 } else {
190 AsciiSPrint(Buffer, StrSize(Data) / 2, "%s", Data);
191 ((CHAR8*)Buffer)[AsciiStrLen(Buffer)-1] = CHAR_NULL;
192 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, AsciiStrSize(Buffer)-sizeof(CHAR8), Buffer);
193 }
194 if (EFI_ERROR(Status)) {
195 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, &Guid, VariableName, Status);
196 ShellStatus = SHELL_ACCESS_DENIED;
197 } else {
198 ASSERT(ShellStatus == SHELL_SUCCESS);
199 }
200 } else if (StrnCmp(Data, L"L\"", 2) == 0) {
201 //
202 // ucs2 text
203 //
204 Data++;
205 Data++;
206 Buffer = AllocateZeroPool(StrSize(Data));
207 if (Buffer == NULL) {
208 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellDebug1HiiHandle);
209 ShellStatus = SHELL_OUT_OF_RESOURCES;
210 } else {
211 UnicodeSPrint(Buffer, StrSize(Data), L"%s", Data);
212 ((CHAR16*)Buffer)[StrLen(Buffer)-1] = CHAR_NULL;
213
214 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, StrSize(Buffer)-sizeof(CHAR16), Buffer);
215 if (EFI_ERROR(Status)) {
216 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, &Guid, VariableName, Status);
217 ShellStatus = SHELL_ACCESS_DENIED;
218 } else {
219 ASSERT(ShellStatus == SHELL_SUCCESS);
220 }
221 }
222 } else if (StrnCmp(Data, L"--", 2) == 0) {
223 //
224 // device path in text format
225 //
226 Data++;
227 Data++;
228 DevPath = ConvertTextToDevicePath(Data);
229 if (DevPath == NULL) {
230 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_DPFT), gShellDebug1HiiHandle, Status);
231 ShellStatus = SHELL_INVALID_PARAMETER;
232 } else {
233 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, GetDevicePathSize(DevPath), DevPath);
234 if (EFI_ERROR(Status)) {
235 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, &Guid, VariableName, Status);
236 ShellStatus = SHELL_ACCESS_DENIED;
237 } else {
238 ASSERT(ShellStatus == SHELL_SUCCESS);
239 }
240 }
241 } else {
242 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, Data);
243 ShellStatus = SHELL_INVALID_PARAMETER;
244 }
245 }
246 }
247 ShellCommandLineFreeVarList (Package);
248 }
249
250 if (Buffer != NULL) {
251 FreePool(Buffer);
252 }
253
254 if (DevPath != NULL) {
255 FreePool(DevPath);
256 }
257
258 return (ShellStatus);
259 }