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