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 - 2016, 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 each alias registered with the Shell.
22 
23   @retval STATUS_SUCCESS  the printout was sucessful
24   @return any return code from GetNextVariableName except EFI_NOT_FOUND
25 **/
26 SHELL_STATUS
PrintAllShellAlias(VOID)27 PrintAllShellAlias(
28   VOID
29   )
30 {
31   CONST CHAR16      *ConstAllAliasList;
32   CHAR16            *Alias;
33   CONST CHAR16      *Command;
34   CHAR16            *Walker;
35   BOOLEAN           Volatile;
36 
37   Volatile = FALSE;
38 
39   ConstAllAliasList = gEfiShellProtocol->GetAlias(NULL, NULL);
40   if (ConstAllAliasList == NULL) {
41     return (SHELL_SUCCESS);
42   }
43   Alias = AllocateZeroPool(StrSize(ConstAllAliasList));
44   if (Alias == NULL) {
45     return (SHELL_OUT_OF_RESOURCES);
46   }
47   Walker = (CHAR16*)ConstAllAliasList;
48 
49   do {
50     CopyMem(Alias, Walker, StrSize(Walker));
51     Walker = StrStr(Alias, L";");
52     if (Walker != NULL) {
53       Walker[0] = CHAR_NULL;
54       Walker = Walker + 1;
55     }
56     Command = gEfiShellProtocol->GetAlias(Alias, &Volatile);
57     if (ShellCommandIsOnAliasList(Alias)) {
58       Volatile = FALSE;
59     }
60     ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_ALIAS_OUTPUT), gShellLevel3HiiHandle, !Volatile?L' ':L'*', Alias, Command);
61   } while (Walker != NULL && Walker[0] != CHAR_NULL);
62 
63   FreePool(Alias);
64 
65   return (SHELL_SUCCESS);
66 }
67 
68 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
69   {L"-v", TypeFlag},
70   {L"-d", TypeFlag},
71   {NULL, TypeMax}
72   };
73 
74 /**
75   Function for 'alias' command.
76 
77   @param[in] ImageHandle  Handle to the Image (NULL if Internal).
78   @param[in] SystemTable  Pointer to the System Table (NULL if Internal).
79 **/
80 SHELL_STATUS
81 EFIAPI
ShellCommandRunAlias(IN EFI_HANDLE ImageHandle,IN EFI_SYSTEM_TABLE * SystemTable)82 ShellCommandRunAlias (
83   IN EFI_HANDLE        ImageHandle,
84   IN EFI_SYSTEM_TABLE  *SystemTable
85   )
86 {
87   EFI_STATUS          Status;
88   LIST_ENTRY          *Package;
89   CHAR16              *ProblemParam;
90   SHELL_STATUS        ShellStatus;
91   CONST CHAR16        *Param1;
92   CONST CHAR16        *Param2;
93   CHAR16              *CleanParam2;
94   CONST CHAR16        *ConstAliasVal;
95   BOOLEAN             Volatile;
96 
97   ProblemParam        = NULL;
98   ShellStatus         = SHELL_SUCCESS;
99   CleanParam2         = NULL;
100 
101   //
102   // initialize the shell lib (we must be in non-auto-init...)
103   //
104   Status = ShellInitialize();
105   ASSERT_EFI_ERROR(Status);
106 
107   Status = CommandInit();
108   ASSERT_EFI_ERROR(Status);
109 
110   //
111   // parse the command line
112   //
113   Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
114   if (EFI_ERROR(Status)) {
115     if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
116       ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel3HiiHandle, L"alias", ProblemParam);
117       FreePool(ProblemParam);
118       ShellStatus = SHELL_INVALID_PARAMETER;
119     } else {
120       ASSERT(FALSE);
121     }
122   } else {
123     Param1 = ShellCommandLineGetRawValue(Package, 1);
124     Param2 = ShellCommandLineGetRawValue(Package, 2);
125 
126     if (Param2 != NULL) {
127       CleanParam2 = AllocateCopyPool (StrSize(Param2), Param2);
128       if (CleanParam2 == NULL) {
129         return SHELL_OUT_OF_RESOURCES;
130       }
131 
132       if (CleanParam2[0] == L'\"' && CleanParam2[StrLen(CleanParam2)-1] == L'\"') {
133         CleanParam2[StrLen(CleanParam2)-1] = L'\0';
134         CopyMem (CleanParam2, CleanParam2 + 1, StrSize(CleanParam2) - sizeof(CleanParam2[0]));
135       }
136     }
137 
138     //
139     // check for "-?"
140     //
141     if (ShellCommandLineGetFlag(Package, L"-?")) {
142       ASSERT(FALSE);
143     }
144     if (ShellCommandLineGetCount(Package) == 1) {
145       //
146       // print out alias'
147       //
148       Status = PrintAllShellAlias();
149     } else if (ShellCommandLineGetFlag(Package, L"-d")) {
150       //
151       // delete an alias
152       //
153       Status = gEfiShellProtocol->SetAlias(Param1, NULL, TRUE, FALSE);
154     } else if (ShellCommandLineGetCount(Package) == 3) {
155       //
156       // must be adding an alias
157       //
158       Status = gEfiShellProtocol->SetAlias(CleanParam2, Param1, FALSE, ShellCommandLineGetFlag(Package, L"-v"));
159       if (EFI_ERROR(Status)) {
160         if (Status == EFI_ACCESS_DENIED) {
161           ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_AD), gShellLevel3HiiHandle, L"alias");
162           ShellStatus = SHELL_ACCESS_DENIED;
163         } else {
164           ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_UK), gShellLevel3HiiHandle, L"alias", Status);
165           ShellStatus = SHELL_DEVICE_ERROR;
166         }
167       }
168     } else if (ShellCommandLineGetCount(Package) == 2) {
169       //
170       // print out a single alias
171       //
172       ConstAliasVal = gEfiShellProtocol->GetAlias(Param1, &Volatile);
173       if (ConstAliasVal == NULL) {
174         ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellLevel3HiiHandle, L"alias", Param1);
175         ShellStatus = SHELL_INVALID_PARAMETER;
176       } else {
177         if (ShellCommandIsOnAliasList(Param1)) {
178           Volatile = FALSE;
179         }
180         ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_ALIAS_OUTPUT), gShellLevel3HiiHandle, !Volatile?L' ':L'*', Param1, ConstAliasVal);
181       }
182     } else {
183       ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel3HiiHandle, L"alias");
184       ShellStatus = SHELL_INVALID_PARAMETER;
185     }
186     //
187     // free the command line package
188     //
189     ShellCommandLineFreeVarList (Package);
190   }
191 
192   SHELL_FREE_NON_NULL (CleanParam2);
193   return (ShellStatus);
194 }
195