1 /** @file
2   Main file for Alias shell level 3 function.
3 
4   Copyright (c) 2009 - 2014, 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 "UefiShellLevel3CommandsLib.h"
16 
17 #include <Library/ShellLib.h>
18 
19 /**
20   Print out each alias registered with the Shell.
21 
22   @retval STATUS_SUCCESS  the printout was sucessful
23   @return any return code from GetNextVariableName except EFI_NOT_FOUND
24 **/
25 SHELL_STATUS
26 EFIAPI
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 
95   ProblemParam        = NULL;
96   ShellStatus         = SHELL_SUCCESS;
97   CleanParam2         = NULL;
98 
99   //
100   // initialize the shell lib (we must be in non-auto-init...)
101   //
102   Status = ShellInitialize();
103   ASSERT_EFI_ERROR(Status);
104 
105   Status = CommandInit();
106   ASSERT_EFI_ERROR(Status);
107 
108   //
109   // parse the command line
110   //
111   Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
112   if (EFI_ERROR(Status)) {
113     if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
114       ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel3HiiHandle, ProblemParam);
115       FreePool(ProblemParam);
116       ShellStatus = SHELL_INVALID_PARAMETER;
117     } else {
118       ASSERT(FALSE);
119     }
120   } else {
121     Param1 = ShellCommandLineGetRawValue(Package, 1);
122     Param2 = ShellCommandLineGetRawValue(Package, 2);
123 
124     if (Param2 != NULL) {
125       CleanParam2 = AllocateCopyPool (StrSize(Param2), Param2);
126       if (CleanParam2 == NULL) {
127         return SHELL_OUT_OF_RESOURCES;
128       }
129 
130       if (CleanParam2[0] == L'\"' && CleanParam2[StrLen(CleanParam2)-1] == L'\"') {
131         CleanParam2[StrLen(CleanParam2)-1] = L'\0';
132         CopyMem (CleanParam2, CleanParam2 + 1, StrSize(CleanParam2) - sizeof(CleanParam2[0]));
133       }
134     }
135 
136     //
137     // check for "-?"
138     //
139     if (ShellCommandLineGetFlag(Package, L"-?")) {
140       ASSERT(FALSE);
141     }
142     if (ShellCommandLineGetCount(Package) == 1) {
143       //
144       // print out alias'
145       //
146       Status = PrintAllShellAlias();
147     } else if (ShellCommandLineGetFlag(Package, L"-d")) {
148       //
149       // delete an alias
150       //
151       Status = gEfiShellProtocol->SetAlias(Param1, NULL, TRUE, FALSE);
152     } else if (ShellCommandLineGetCount(Package) == 3) {
153       //
154       // must be adding an alias
155       //
156       Status = gEfiShellProtocol->SetAlias(CleanParam2, Param1, FALSE, ShellCommandLineGetFlag(Package, L"-v"));
157       if (EFI_ERROR(Status)) {
158         if (Status == EFI_ACCESS_DENIED) {
159           ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_AD), gShellLevel3HiiHandle);
160           ShellStatus = SHELL_ACCESS_DENIED;
161         } else {
162           ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_UK), gShellLevel3HiiHandle, Status);
163           ShellStatus = SHELL_DEVICE_ERROR;
164         }
165       }
166     } else if (ShellCommandLineGetCount(Package) == 2) {
167       ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel3HiiHandle);
168       ShellStatus = SHELL_INVALID_PARAMETER;
169     } else {
170       ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel3HiiHandle);
171       ShellStatus = SHELL_INVALID_PARAMETER;
172     }
173     //
174     // free the command line package
175     //
176     ShellCommandLineFreeVarList (Package);
177   }
178 
179   SHELL_FREE_NON_NULL (CleanParam2);
180   return (ShellStatus);
181 }
182