]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/SetVar.c
comp - add comments and add input verification
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / SetVar.c
1 /** @file
2 Main file for SetVar shell Debug1 function.
3
4 Copyright (c) 2010 - 2011, 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 EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL *DevPathFromText;
53
54 ShellStatus = SHELL_SUCCESS;
55 Status = EFI_SUCCESS;
56 Buffer = NULL;
57 Size = 0;
58 Attributes = 0;
59 DevPath = NULL;
60
61 //
62 // initialize the shell lib (we must be in non-auto-init...)
63 //
64 Status = ShellInitialize();
65 ASSERT_EFI_ERROR(Status);
66
67 Status = CommandInit();
68 ASSERT_EFI_ERROR(Status);
69
70 //
71 // parse the command line
72 //
73 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
74 if (EFI_ERROR(Status)) {
75 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
76 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, ProblemParam);
77 FreePool(ProblemParam);
78 ShellStatus = SHELL_INVALID_PARAMETER;
79 } else {
80 ASSERT(FALSE);
81 }
82 } else {
83 if (ShellCommandLineGetCount(Package) < 2) {
84 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle);
85 ShellStatus = SHELL_INVALID_PARAMETER;
86 } else if (ShellCommandLineGetCount(Package) > 3) {
87 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle);
88 ShellStatus = SHELL_INVALID_PARAMETER;
89 } else {
90 VariableName = ShellCommandLineGetRawValue(Package, 1);
91 Data = ShellCommandLineGetRawValue(Package, 2);
92 if (!ShellCommandLineGetFlag(Package, L"-guid")){
93 CopyGuid(&Guid, &gEfiGlobalVariableGuid);
94 } else {
95 StringGuid = ShellCommandLineGetValue(Package, L"-guid");
96 Status = ConvertStringToGuid(StringGuid, &Guid);
97 if (EFI_ERROR(Status)) {
98 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, StringGuid);
99 ShellStatus = SHELL_INVALID_PARAMETER;
100 }
101 }
102 if (Data == NULL) {
103 //
104 // Display what's there
105 //
106 Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes, &Size, Buffer);
107 if (Status == EFI_BUFFER_TOO_SMALL) {
108 Buffer = AllocateZeroPool(Size);
109 Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes, &Size, Buffer);
110 }
111 if (!EFI_ERROR(Status)&& Buffer != NULL) {
112 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_PRINT), gShellDebug1HiiHandle, &Guid, VariableName, Size);
113 for (LoopVar = 0 ; LoopVar < Size ; LoopVar++) {
114 ShellPrintEx(-1, -1, L"%02x ", ((UINT8*)Buffer)[LoopVar]);
115 }
116 ShellPrintEx(-1, -1, L"\r\n");
117 } else {
118 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_GET), gShellDebug1HiiHandle, &Guid, VariableName, Status);
119 ShellStatus = SHELL_ACCESS_DENIED;
120 }
121 } else if (StrCmp(Data, L"=") == 0) {
122 //
123 // Delete what's there!
124 //
125 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, 0, NULL);
126 if (EFI_ERROR(Status)) {
127 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, &Guid, VariableName, Status);
128 ShellStatus = SHELL_ACCESS_DENIED;
129 } else {
130 ASSERT(ShellStatus == SHELL_SUCCESS);
131 }
132 } else {
133 if (Data[0] == L'=') {
134 Data++;
135 }
136 //
137 // Change what's there
138 //
139 if (ShellCommandLineGetFlag(Package, L"-bs")) {
140 Attributes |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
141 }
142 if (ShellCommandLineGetFlag(Package, L"-rt")) {
143 Attributes |= EFI_VARIABLE_RUNTIME_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 for (LoopVar = 0 ; LoopVar < (StrLen(Data) / 2) ; LoopVar++) {
158 ((UINT8*)Buffer)[LoopVar] = (UINT8)(HexCharToUintn(Data[LoopVar*2]) * 16);
159 ((UINT8*)Buffer)[LoopVar] = (UINT8)(((UINT8*)Buffer)[LoopVar] + HexCharToUintn(Data[LoopVar*2+1]));
160 }
161 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, StrLen(Data) / 2, Buffer);
162 if (EFI_ERROR(Status)) {
163 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, &Guid, VariableName, Status);
164 ShellStatus = SHELL_ACCESS_DENIED;
165 } else {
166 ASSERT(ShellStatus == SHELL_SUCCESS);
167 }
168 }
169 } else if (StrnCmp(Data, L"\"", 1) == 0) {
170 Size = 0;
171 Attributes2 = 0;
172 Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes2, &Size, Buffer);
173 if (Status == EFI_BUFFER_TOO_SMALL) {
174 Buffer = AllocateZeroPool(Size);
175 Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes2, &Size, Buffer);
176 FreePool(Buffer);
177 Attributes = Attributes2;
178 }
179 //
180 // ascii text
181 //
182 Data++;
183 Buffer = AllocateZeroPool(StrSize(Data) / 2);
184 AsciiSPrint(Buffer, StrSize(Data) / 2, "%s", Data);
185 ((CHAR8*)Buffer)[AsciiStrLen(Buffer)-1] = CHAR_NULL;
186
187
188 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, AsciiStrSize(Buffer)-sizeof(CHAR8), Buffer);
189 if (EFI_ERROR(Status)) {
190 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, &Guid, VariableName, Status);
191 ShellStatus = SHELL_ACCESS_DENIED;
192 } else {
193 ASSERT(ShellStatus == SHELL_SUCCESS);
194 }
195 } else if (StrnCmp(Data, L"L\"", 2) == 0) {
196 //
197 // ucs2 text
198 //
199 Data++;
200 Data++;
201 Buffer = AllocateZeroPool(StrSize(Data));
202 UnicodeSPrint(Buffer, StrSize(Data), L"%s", Data);
203 ((CHAR16*)Buffer)[StrLen(Buffer)-1] = CHAR_NULL;
204
205 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, StrSize(Buffer)-sizeof(CHAR16), Buffer);
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"--", 2) == 0) {
213 //
214 // device path in text format
215 //
216 Data++;
217 Data++;
218 Status = gBS->LocateProtocol(&gEfiDevicePathFromTextProtocolGuid, NULL, (VOID**)&DevPathFromText);
219 ASSERT_EFI_ERROR(Status);
220 DevPath = DevPathFromText->ConvertTextToDevicePath(Data);
221 if (DevPath == NULL) {
222 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_DPFT), gShellDebug1HiiHandle, Status);
223 ShellStatus = SHELL_INVALID_PARAMETER;
224 } else {
225 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, GetDevicePathSize(DevPath), DevPath);
226 if (EFI_ERROR(Status)) {
227 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, &Guid, VariableName, Status);
228 ShellStatus = SHELL_ACCESS_DENIED;
229 } else {
230 ASSERT(ShellStatus == SHELL_SUCCESS);
231 }
232 }
233 } else {
234 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, Data);
235 ShellStatus = SHELL_INVALID_PARAMETER;
236 }
237 }
238 }
239 ShellCommandLineFreeVarList (Package);
240 }
241
242 if (Buffer != NULL) {
243 FreePool(Buffer);
244 }
245
246 if (DevPath != NULL) {
247 FreePool(DevPath);
248 }
249
250 return (ShellStatus);
251 }