]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellLevel3CommandsLib/Alias.c
ShellPkg/Dp: Add null pointer check
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel3CommandsLib / Alias.c
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 - 2017, Intel Corporation. All rights reserved. <BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "UefiShellLevel3CommandsLib.h"
17
18 #include <Library/ShellLib.h>
19
20 /**
21 Print out single alias registered with the Shell.
22
23 @param[in] Alias Points to the NULL-terminated shell alias.
24 If this parameter is NULL, then all
25 aliases will be returned in ReturnedData.
26 @retval SHELL_SUCCESS the printout was sucessful
27 **/
28 SHELL_STATUS
29 PrintSingleShellAlias(
30 IN CONST CHAR16 *Alias
31 )
32 {
33 CONST CHAR16 *ConstAliasVal;
34 SHELL_STATUS ShellStatus;
35 BOOLEAN Volatile;
36
37 ShellStatus = SHELL_SUCCESS;
38 ConstAliasVal = gEfiShellProtocol->GetAlias (Alias, &Volatile);
39 if (ConstAliasVal == NULL) {
40 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellLevel3HiiHandle, L"alias", Alias);
41 ShellStatus = SHELL_INVALID_PARAMETER;
42 } else {
43 if (ShellCommandIsOnAliasList (Alias)) {
44 Volatile = FALSE;
45 }
46 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_ALIAS_OUTPUT), gShellLevel3HiiHandle, !Volatile ? L' ' : L'*', Alias, ConstAliasVal);
47 }
48 return ShellStatus;
49 }
50
51 /**
52 Print out each alias registered with the Shell.
53
54 @retval STATUS_SUCCESS the printout was sucessful
55 @return any return code from GetNextVariableName except EFI_NOT_FOUND
56 **/
57 SHELL_STATUS
58 PrintAllShellAlias(
59 VOID
60 )
61 {
62 CONST CHAR16 *ConstAllAliasList;
63 CHAR16 *Alias;
64 CHAR16 *Walker;
65
66 ConstAllAliasList = gEfiShellProtocol->GetAlias(NULL, NULL);
67 if (ConstAllAliasList == NULL) {
68 return (SHELL_SUCCESS);
69 }
70 Alias = AllocateZeroPool(StrSize(ConstAllAliasList));
71 if (Alias == NULL) {
72 return (SHELL_OUT_OF_RESOURCES);
73 }
74 Walker = (CHAR16*)ConstAllAliasList;
75
76 do {
77 CopyMem(Alias, Walker, StrSize(Walker));
78 Walker = StrStr(Alias, L";");
79 if (Walker != NULL) {
80 Walker[0] = CHAR_NULL;
81 Walker = Walker + 1;
82 }
83 PrintSingleShellAlias(Alias);
84 } while (Walker != NULL && Walker[0] != CHAR_NULL);
85
86 FreePool(Alias);
87
88 return (SHELL_SUCCESS);
89 }
90
91 /**
92 Changes a shell command alias.
93
94 This function creates an alias for a shell command or if Alias is NULL it will delete an existing alias.
95
96
97 @param[in] Command Points to the NULL-terminated shell command or existing alias.
98 @param[in] Alias Points to the NULL-terminated alias for the shell command. If this is NULL, and
99 Command refers to an alias, that alias will be deleted.
100 @param[in] Replace If TRUE and the alias already exists, then the existing alias will be replaced. If
101 FALSE and the alias already exists, then the existing alias is unchanged and
102 EFI_ACCESS_DENIED is returned.
103 @param[in] Volatile if TRUE the Alias being set will be stored in a volatile fashion. if FALSE the
104 Alias being set will be stored in a non-volatile fashion.
105
106 @retval SHELL_SUCCESS Alias created or deleted successfully.
107 @retval SHELL_NOT_FOUND the Alias intended to be deleted was not found
108 @retval SHELL_ACCESS_DENIED The alias is a built-in alias or already existed and Replace was set to
109 FALSE.
110 @retval SHELL_DEVICE_ERROR Command is null or the empty string.
111 **/
112 SHELL_STATUS
113 ShellLevel3CommandsLibSetAlias(
114 IN CONST CHAR16 *Command,
115 IN CONST CHAR16 *Alias,
116 IN BOOLEAN Replace,
117 IN BOOLEAN Volatile
118 )
119 {
120 SHELL_STATUS ShellStatus;
121 EFI_STATUS Status;
122
123 ShellStatus = SHELL_SUCCESS;
124 Status = gEfiShellProtocol->SetAlias (Command, Alias, Replace, Volatile);
125 if (EFI_ERROR(Status)) {
126 if (Status == EFI_ACCESS_DENIED) {
127 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_AD), gShellLevel3HiiHandle, L"alias");
128 ShellStatus = SHELL_ACCESS_DENIED;
129 } else if (Status == EFI_NOT_FOUND) {
130 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_NOT_FOUND), gShellLevel3HiiHandle, L"alias", Command);
131 ShellStatus = SHELL_NOT_FOUND;
132 } else {
133 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_UK), gShellLevel3HiiHandle, L"alias", Status);
134 ShellStatus = SHELL_DEVICE_ERROR;
135 }
136 }
137 return ShellStatus;
138 }
139
140 STATIC 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 **/
152 SHELL_STATUS
153 EFIAPI
154 ShellCommandRunAlias (
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 // free the command line package
280 //
281 ShellCommandLineFreeVarList (Package);
282 }
283
284 SHELL_FREE_NON_NULL (CleanParam2);
285 return (ShellStatus);
286 }