VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/FirmwareNew/ShellPkg/Library/UefiShellLevel3CommandsLib/Alias.c@ 99404

Last change on this file since 99404 was 99404, checked in by vboxsync, 23 months ago

Devices/EFI/FirmwareNew: Update to edk2-stable202302 and make it build, bugref:4643

  • Property svn:eol-style set to native
File size: 8.8 KB
Line 
1/** @file
2 Main file for Alias shell level 3 function.
3
4 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved. <BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8**/
9
10#include "UefiShellLevel3CommandsLib.h"
11
12#include <Library/ShellLib.h>
13
14/**
15 Print out single alias registered with the Shell.
16
17 @param[in] Alias Points to the NULL-terminated shell alias.
18 If this parameter is NULL, then all
19 aliases will be returned in ReturnedData.
20 @retval SHELL_SUCCESS the printout was sucessful
21**/
22SHELL_STATUS
23PrintSingleShellAlias (
24 IN CONST CHAR16 *Alias
25 )
26{
27 CONST CHAR16 *ConstAliasVal;
28 SHELL_STATUS ShellStatus;
29 BOOLEAN Volatile;
30
31 ShellStatus = SHELL_SUCCESS;
32 ConstAliasVal = gEfiShellProtocol->GetAlias (Alias, &Volatile);
33 if (ConstAliasVal == NULL) {
34 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellLevel3HiiHandle, L"alias", Alias);
35 ShellStatus = SHELL_INVALID_PARAMETER;
36 } else {
37 if (ShellCommandIsOnAliasList (Alias)) {
38 Volatile = FALSE;
39 }
40
41 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_ALIAS_OUTPUT), gShellLevel3HiiHandle, !Volatile ? L' ' : L'*', Alias, ConstAliasVal);
42 }
43
44 return ShellStatus;
45}
46
47/**
48 Print out each alias registered with the Shell.
49
50 @retval STATUS_SUCCESS the printout was sucessful
51 @return any return code from GetNextVariableName except EFI_NOT_FOUND
52**/
53SHELL_STATUS
54PrintAllShellAlias (
55 VOID
56 )
57{
58 CONST CHAR16 *ConstAllAliasList;
59 CHAR16 *Alias;
60 CHAR16 *Walker;
61
62 ConstAllAliasList = gEfiShellProtocol->GetAlias (NULL, NULL);
63 if (ConstAllAliasList == NULL) {
64 return (SHELL_SUCCESS);
65 }
66
67 Alias = AllocateZeroPool (StrSize (ConstAllAliasList));
68 if (Alias == NULL) {
69 return (SHELL_OUT_OF_RESOURCES);
70 }
71
72 Walker = (CHAR16 *)ConstAllAliasList;
73
74 do {
75 CopyMem (Alias, Walker, StrSize (Walker));
76 Walker = StrStr (Alias, L";");
77 if (Walker != NULL) {
78 Walker[0] = CHAR_NULL;
79 Walker = Walker + 1;
80 }
81
82 PrintSingleShellAlias (Alias);
83 } while (Walker != NULL && Walker[0] != CHAR_NULL);
84
85 FreePool (Alias);
86
87 return (SHELL_SUCCESS);
88}
89
90/**
91 Changes a shell command alias.
92
93 This function creates an alias for a shell command or if Alias is NULL it will delete an existing alias.
94
95
96 @param[in] Command Points to the NULL-terminated shell command or existing alias.
97 @param[in] Alias Points to the NULL-terminated alias for the shell command. If this is NULL, and
98 Command refers to an alias, that alias will be deleted.
99 @param[in] Replace If TRUE and the alias already exists, then the existing alias will be replaced. If
100 FALSE and the alias already exists, then the existing alias is unchanged and
101 EFI_ACCESS_DENIED is returned.
102 @param[in] Volatile if TRUE the Alias being set will be stored in a volatile fashion. if FALSE the
103 Alias being set will be stored in a non-volatile fashion.
104
105 @retval SHELL_SUCCESS Alias created or deleted successfully.
106 @retval SHELL_NOT_FOUND the Alias intended to be deleted was not found
107 @retval SHELL_ACCESS_DENIED The alias is a built-in alias or already existed and Replace was set to
108 FALSE.
109 @retval SHELL_DEVICE_ERROR Command is null or the empty string.
110**/
111SHELL_STATUS
112ShellLevel3CommandsLibSetAlias (
113 IN CONST CHAR16 *Command,
114 IN CONST CHAR16 *Alias,
115 IN BOOLEAN Replace,
116 IN BOOLEAN Volatile
117 )
118{
119 SHELL_STATUS ShellStatus;
120 EFI_STATUS Status;
121
122 ShellStatus = SHELL_SUCCESS;
123 Status = gEfiShellProtocol->SetAlias (Command, Alias, Replace, Volatile);
124 if (EFI_ERROR (Status)) {
125 if (Status == EFI_ACCESS_DENIED) {
126 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_AD), gShellLevel3HiiHandle, L"alias");
127 ShellStatus = SHELL_ACCESS_DENIED;
128 } else if (Status == EFI_NOT_FOUND) {
129 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_NOT_FOUND), gShellLevel3HiiHandle, L"alias", Command);
130 ShellStatus = SHELL_NOT_FOUND;
131 } else {
132 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_UK), gShellLevel3HiiHandle, L"alias", Status);
133 ShellStatus = SHELL_DEVICE_ERROR;
134 }
135 }
136
137 return ShellStatus;
138}
139
140STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
141 { L"-v", TypeFlag },
142 { L"-d", TypeValue },
143 { NULL, TypeMax }
144};
145
146/**
147 Function for 'alias' command.
148
149 @param[in] ImageHandle Handle to the Image (NULL if Internal).
150 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
151**/
152SHELL_STATUS
153EFIAPI
154ShellCommandRunAlias (
155 IN EFI_HANDLE ImageHandle,
156 IN EFI_SYSTEM_TABLE *SystemTable
157 )
158{
159 EFI_STATUS Status;
160 LIST_ENTRY *Package;
161 CHAR16 *ProblemParam;
162 SHELL_STATUS ShellStatus;
163 CONST CHAR16 *Param1;
164 CONST CHAR16 *Param2;
165 CONST CHAR16 *ParamStrD;
166 CHAR16 *CleanParam2;
167 BOOLEAN DeleteFlag;
168 BOOLEAN VolatileFlag;
169
170 ProblemParam = NULL;
171 ShellStatus = SHELL_SUCCESS;
172 CleanParam2 = NULL;
173
174 //
175 // initialize the shell lib (we must be in non-auto-init...)
176 //
177 Status = ShellInitialize ();
178 ASSERT_EFI_ERROR (Status);
179
180 Status = CommandInit ();
181 ASSERT_EFI_ERROR (Status);
182
183 //
184 // parse the command line
185 //
186 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
187 if (EFI_ERROR (Status)) {
188 if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) {
189 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel3HiiHandle, L"alias", ProblemParam);
190 FreePool (ProblemParam);
191 ShellStatus = SHELL_INVALID_PARAMETER;
192 } else {
193 ASSERT (FALSE);
194 }
195 } else {
196 Param1 = ShellCommandLineGetRawValue (Package, 1);
197 Param2 = ShellCommandLineGetRawValue (Package, 2);
198
199 DeleteFlag = ShellCommandLineGetFlag (Package, L"-d");
200 VolatileFlag = ShellCommandLineGetFlag (Package, L"-v");
201
202 if (Param2 != NULL) {
203 CleanParam2 = AllocateCopyPool (StrSize (Param2), Param2);
204 if (CleanParam2 == NULL) {
205 ShellCommandLineFreeVarList (Package);
206 return SHELL_OUT_OF_RESOURCES;
207 }
208
209 if ((CleanParam2[0] == L'\"') && (CleanParam2[StrLen (CleanParam2)-1] == L'\"')) {
210 CleanParam2[StrLen (CleanParam2)-1] = L'\0';
211 CopyMem (CleanParam2, CleanParam2 + 1, StrSize (CleanParam2) - sizeof (CleanParam2[0]));
212 }
213 }
214
215 if (!DeleteFlag && !VolatileFlag) {
216 switch (ShellCommandLineGetCount (Package)) {
217 case 1:
218 //
219 // "alias"
220 //
221 ShellStatus = PrintAllShellAlias ();
222 break;
223 case 2:
224 //
225 // "alias Param1"
226 //
227 ShellStatus = PrintSingleShellAlias (Param1);
228 break;
229 case 3:
230 //
231 // "alias Param1 CleanParam2"
232 //
233 ShellStatus = ShellLevel3CommandsLibSetAlias (CleanParam2, Param1, FALSE, VolatileFlag);
234 break;
235 default:
236 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel3HiiHandle, L"alias");
237 ShellStatus = SHELL_INVALID_PARAMETER;
238 }
239 } else if (DeleteFlag) {
240 if (VolatileFlag || (ShellCommandLineGetCount (Package) > 1)) {
241 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel3HiiHandle, L"alias");
242 ShellStatus = SHELL_INVALID_PARAMETER;
243 } else {
244 ParamStrD = ShellCommandLineGetValue (Package, L"-d");
245 if (ParamStrD == NULL) {
246 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel3HiiHandle, L"alias");
247 ShellStatus = SHELL_INVALID_PARAMETER;
248 } else {
249 //
250 // Delete an alias: "alias -d ParamStrD"
251 //
252 ShellStatus = ShellLevel3CommandsLibSetAlias (ParamStrD, NULL, TRUE, FALSE);
253 }
254 }
255 } else {
256 //
257 // Set volatile alias.
258 //
259 ASSERT (VolatileFlag);
260 ASSERT (!DeleteFlag);
261 switch (ShellCommandLineGetCount (Package)) {
262 case 1:
263 case 2:
264 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel3HiiHandle, L"alias");
265 ShellStatus = SHELL_INVALID_PARAMETER;
266 break;
267 case 3:
268 //
269 // "alias -v Param1 CleanParam2"
270 //
271 ShellStatus = ShellLevel3CommandsLibSetAlias (CleanParam2, Param1, FALSE, VolatileFlag);
272 break;
273 default:
274 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel3HiiHandle, L"alias");
275 ShellStatus = SHELL_INVALID_PARAMETER;
276 }
277 }
278
279 //
280 // free the command line package
281 //
282 ShellCommandLineFreeVarList (Package);
283 }
284
285 SHELL_FREE_NON_NULL (CleanParam2);
286 return (ShellStatus);
287}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette