]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/SetVar.c
Add "Debug1" profile (all but Edit and HexEdit commands)
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / SetVar.c
1 /** @file
2 Main file for SetVar shell Debug1 function.
3
4 Copyright (c) 2010, 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 SHELL_STATUS
26 EFIAPI
27 ShellCommandRunSetVar (
28 IN EFI_HANDLE ImageHandle,
29 IN EFI_SYSTEM_TABLE *SystemTable
30 )
31 {
32 EFI_STATUS Status;
33 LIST_ENTRY *Package;
34 CHAR16 *ProblemParam;
35 SHELL_STATUS ShellStatus;
36 CONST CHAR16 *VariableName;
37 CONST CHAR16 *Data;
38 EFI_GUID Guid;
39 CONST CHAR16 *StringGuid;
40 UINT32 Attributes;
41 VOID *Buffer;
42 UINTN Size;
43 UINTN LoopVar;
44 EFI_DEVICE_PATH_PROTOCOL *DevPath;
45 EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL *DevPathFromText;
46
47 ShellStatus = SHELL_SUCCESS;
48 Status = EFI_SUCCESS;
49 Buffer = NULL;
50 Size = 0;
51 Attributes = 0;
52 DevPath = NULL;
53
54 //
55 // initialize the shell lib (we must be in non-auto-init...)
56 //
57 Status = ShellInitialize();
58 ASSERT_EFI_ERROR(Status);
59
60 Status = CommandInit();
61 ASSERT_EFI_ERROR(Status);
62
63 //
64 // parse the command line
65 //
66 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
67 if (EFI_ERROR(Status)) {
68 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
69 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, ProblemParam);
70 FreePool(ProblemParam);
71 ShellStatus = SHELL_INVALID_PARAMETER;
72 } else {
73 ASSERT(FALSE);
74 }
75 } else {
76 if (ShellCommandLineGetCount(Package) < 2) {
77 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle);
78 ShellStatus = SHELL_INVALID_PARAMETER;
79 } else if (ShellCommandLineGetCount(Package) > 3) {
80 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle);
81 ShellStatus = SHELL_INVALID_PARAMETER;
82 } else {
83 VariableName = ShellCommandLineGetRawValue(Package, 1);
84 Data = ShellCommandLineGetRawValue(Package, 2);
85 if (!ShellCommandLineGetFlag(Package, L"-guid")){
86 CopyGuid(&Guid, &gEfiGlobalVariableGuid);
87 } else {
88 StringGuid = ShellCommandLineGetValue(Package, L"-guid");
89 Status = ConvertStringToGuid(StringGuid, &Guid);
90 if (EFI_ERROR(Status)) {
91 ShellStatus = SHELL_NOT_FOUND;
92 }
93 }
94 if (Data == NULL) {
95 //
96 // Display what's there
97 //
98 Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes, &Size, Buffer);
99 if (Status == EFI_BUFFER_TOO_SMALL) {
100 Buffer = AllocatePool(Size);
101 Status = gRT->GetVariable((CHAR16*)VariableName, &Guid, &Attributes, &Size, Buffer);
102 }
103 if (!EFI_ERROR(Status)&& Buffer != NULL) {
104 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_PRINT), gShellDebug1HiiHandle, &Guid, VariableName, Size);
105 for (LoopVar = 0 ; LoopVar < Size ; LoopVar++) {
106 ShellPrintEx(-1, -1, L"%02x ", ((UINT8*)Buffer)[LoopVar]);
107 }
108 ShellPrintEx(-1, -1, L"\r\n");
109 } else {
110 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_GET), gShellDebug1HiiHandle, &Guid, VariableName, Status);
111 ShellStatus = SHELL_ACCESS_DENIED;
112 }
113 } else if (StrCmp(Data, L"=") == 0) {
114 //
115 // Delete what's there!
116 //
117 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, 0, NULL);
118 if (EFI_ERROR(Status)) {
119 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, &Guid, VariableName, Status);
120 ShellStatus = SHELL_ACCESS_DENIED;
121 } else {
122 ASSERT(ShellStatus == SHELL_SUCCESS);
123 }
124 } else {
125 if (Data[0] == L'=') {
126 Data++;
127 }
128 //
129 // Change what's there
130 //
131 if (ShellCommandLineGetFlag(Package, L"-bs")) {
132 Attributes |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
133 }
134 if (ShellCommandLineGetFlag(Package, L"-rt")) {
135 Attributes |= EFI_VARIABLE_RUNTIME_ACCESS;
136 }
137 if (ShellCommandLineGetFlag(Package, L"-nv")) {
138 Attributes |= EFI_VARIABLE_NON_VOLATILE;
139 }
140 if (ShellIsHexOrDecimalNumber(Data, TRUE, FALSE)) {
141 //
142 // arbitrary buffer
143 //
144 Buffer = AllocateZeroPool((StrLen(Data) / 2));
145 for (LoopVar = 0 ; LoopVar < (StrLen(Data) / 2) ; LoopVar++) {
146 ((UINT8*)Buffer)[LoopVar] = (UINT8)(HexCharToUintn(Data[LoopVar*2]) * 16);
147 ((UINT8*)Buffer)[LoopVar] = (UINT8)(((UINT8*)Buffer)[LoopVar] + HexCharToUintn(Data[LoopVar*2+1]));
148 }
149 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, StrLen(Data) / 2, Buffer);
150 if (EFI_ERROR(Status)) {
151 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, &Guid, VariableName, Status);
152 ShellStatus = SHELL_ACCESS_DENIED;
153 } else {
154 ASSERT(ShellStatus == SHELL_SUCCESS);
155 }
156 } else if (StrnCmp(Data, L"\"", 1) == 0) {
157 //
158 // ascii text
159 //
160 Data++;
161 Buffer = AllocateZeroPool(StrSize(Data) / 2);
162 AsciiSPrint(Buffer, StrSize(Data) / 2, "%s", Data);
163 ((CHAR8*)Buffer)[AsciiStrLen(Buffer)-1] = CHAR_NULL;
164
165 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, AsciiStrSize(Buffer)-sizeof(CHAR8), Buffer);
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 } else if (StrnCmp(Data, L"L\"", 2) == 0) {
173 //
174 // ucs2 text
175 //
176 Data++;
177 Data++;
178 Buffer = AllocateZeroPool(StrSize(Data));
179 UnicodeSPrint(Buffer, StrSize(Data), L"%s", Data);
180 ((CHAR16*)Buffer)[StrLen(Buffer)-1] = CHAR_NULL;
181
182 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, StrSize(Buffer)-sizeof(CHAR16), Buffer);
183 if (EFI_ERROR(Status)) {
184 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, &Guid, VariableName, Status);
185 ShellStatus = SHELL_ACCESS_DENIED;
186 } else {
187 ASSERT(ShellStatus == SHELL_SUCCESS);
188 }
189 } else if (StrnCmp(Data, L"--", 2) == 0) {
190 //
191 // device path in text format
192 //
193 Data++;
194 Data++;
195 Status = gBS->LocateProtocol(&gEfiDevicePathFromTextProtocolGuid, NULL, (VOID**)&DevPathFromText);
196 ASSERT_EFI_ERROR(Status);
197 DevPath = DevPathFromText->ConvertTextToDevicePath(Data);
198 if (DevPath == NULL) {
199 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_DPFT), gShellDebug1HiiHandle, Status);
200 ShellStatus = SHELL_INVALID_PARAMETER;
201 } else {
202 Status = gRT->SetVariable((CHAR16*)VariableName, &Guid, Attributes, GetDevicePathSize(DevPath), DevPath);
203 if (EFI_ERROR(Status)) {
204 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SETVAR_ERROR_SET), gShellDebug1HiiHandle, &Guid, VariableName, Status);
205 ShellStatus = SHELL_ACCESS_DENIED;
206 } else {
207 ASSERT(ShellStatus == SHELL_SUCCESS);
208 }
209 }
210 } else {
211 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, Data);
212 ShellStatus = SHELL_INVALID_PARAMETER;
213 }
214 }
215 }
216 ShellCommandLineFreeVarList (Package);
217 }
218
219 if (Buffer != NULL) {
220 FreePool(Buffer);
221 }
222
223 if (DevPath != NULL) {
224 FreePool(DevPath);
225 }
226
227 return (ShellStatus);
228 }