1 /** @file
2   Main file for attrib shell level 2 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 "UefiShellLevel2CommandsLib.h"
11 
12 /**
13   Print out each environment variable registered with the Shell 2.0 GUID.
14 
15   If you spawn a pre 2.0 shell from the Shell 2.0 the environment variable may not carry through.
16 
17   @retval STATUS_SUCCESS  the printout was sucessful
18   @return any return code from GetNextVariableName except EFI_NOT_FOUND
19 **/
20 SHELL_STATUS
PrintAllShellEnvVars(VOID)21 PrintAllShellEnvVars(
22   VOID
23   )
24 {
25   CONST CHAR16      *Value;
26   CONST CHAR16      *ConstEnvNameList;
27 
28   ConstEnvNameList = gEfiShellProtocol->GetEnv(NULL);
29   if (ConstEnvNameList == NULL) {
30     return (SHELL_SUCCESS);
31   }
32   while (*ConstEnvNameList != CHAR_NULL){
33     Value = gEfiShellProtocol->GetEnv(ConstEnvNameList);
34     ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SET_DISP), gShellLevel2HiiHandle, ConstEnvNameList, Value);
35     ConstEnvNameList += StrLen(ConstEnvNameList)+1;
36   }
37 
38   return (SHELL_SUCCESS);
39 }
40 
41 STATIC CONST SHELL_PARAM_ITEM SetParamList[] = {
42   {L"-d", TypeValue},
43   {L"-v", TypeFlag},
44   {NULL, TypeMax}
45   };
46 
47 /**
48   Function for 'set' command.
49 
50   @param[in] ImageHandle  Handle to the Image (NULL if Internal).
51   @param[in] SystemTable  Pointer to the System Table (NULL if Internal).
52 **/
53 SHELL_STATUS
54 EFIAPI
ShellCommandRunSet(IN EFI_HANDLE ImageHandle,IN EFI_SYSTEM_TABLE * SystemTable)55 ShellCommandRunSet (
56   IN EFI_HANDLE        ImageHandle,
57   IN EFI_SYSTEM_TABLE  *SystemTable
58   )
59 {
60   EFI_STATUS    Status;
61   LIST_ENTRY    *Package;
62   CONST CHAR16  *KeyName;
63   CONST CHAR16  *Value;
64   CHAR16        *ProblemParam;
65   SHELL_STATUS  ShellStatus;
66 
67   ProblemParam = NULL;
68   ShellStatus  = SHELL_SUCCESS;
69 
70   //
71   // initialize the shell lib (we must be in non-auto-init...)
72   //
73   Status = ShellInitialize();
74   ASSERT_EFI_ERROR(Status);
75 
76   //
77   // Make sure globals are good...
78   //
79   Status = CommandInit();
80   ASSERT_EFI_ERROR(Status);
81 
82   //
83   // parse the command line
84   //
85   Status = ShellCommandLineParse (SetParamList, &Package, &ProblemParam, TRUE);
86   if (EFI_ERROR(Status)) {
87     if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
88       ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"set", ProblemParam);
89       FreePool(ProblemParam);
90       return (SHELL_INVALID_PARAMETER);
91     } else {
92       ASSERT(FALSE);
93     }
94   } else {
95     //
96     // check for "-?"
97     //
98     if (ShellCommandLineGetFlag(Package, L"-?")) {
99       ASSERT(FALSE);
100     } else if (ShellCommandLineGetRawValue(Package, 3) != NULL) {
101       ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle, L"set");
102       ShellStatus = SHELL_INVALID_PARAMETER;
103     } else if (ShellCommandLineGetRawValue(Package, 1) != NULL && ShellCommandLineGetFlag(Package, L"-d")) {
104       ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle, L"set");
105       ShellStatus = SHELL_INVALID_PARAMETER;
106     } else if (ShellCommandLineGetFlag(Package, L"-d")) {
107       //
108       // delete a environment variable
109       //
110       KeyName = ShellCommandLineGetValue(Package, L"-d");
111       if (KeyName == NULL) {
112         ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VALUE), gShellLevel2HiiHandle, L"set", L"-d");
113         ShellStatus = SHELL_INVALID_PARAMETER;
114       } else {
115         Status = ShellSetEnvironmentVariable(KeyName, L"", ShellCommandLineGetFlag(Package, L"-v"));
116         if (EFI_ERROR(Status)) {
117           ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SET_ND), gShellLevel2HiiHandle, L"set", KeyName);
118           ShellStatus = SHELL_DEVICE_ERROR;
119         }
120       }
121     } else if (ShellCommandLineGetRawValue(Package, 1) == NULL) {
122       //
123       // print out all current environment variables
124       //
125       return(PrintAllShellEnvVars());
126     } else {
127       //
128       // we are either printing one or assigning one
129       //
130       KeyName = ShellCommandLineGetRawValue(Package, 1);
131       Value   = ShellCommandLineGetRawValue(Package, 2);
132       if (KeyName != NULL && Value != NULL) {
133         //
134         // assigning one
135         //
136         Status = ShellSetEnvironmentVariable(KeyName, Value, ShellCommandLineGetFlag(Package, L"-v"));
137         if (EFI_ERROR(Status)) {
138           ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SET_ERROR_SET), gShellLevel2HiiHandle, L"set", KeyName);
139           ShellStatus = (SHELL_STATUS) (Status & (~MAX_BIT));
140         }
141 
142       } else {
143         if (KeyName != NULL) {
144           //
145           // print out value for this one only.
146           //
147           Value = ShellGetEnvironmentVariable(KeyName);
148           if (Value == NULL) {
149             ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SET_NF), gShellLevel2HiiHandle, L"set", KeyName);
150             ShellStatus = SHELL_SUCCESS;
151           } else {
152             ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SET_DISP), gShellLevel2HiiHandle, KeyName, Value);
153             ShellStatus = SHELL_SUCCESS;
154           }
155         } else {
156           ASSERT(FALSE);
157         }
158       }
159     }
160   }
161 
162   //
163   // free the command line package
164   //
165   ShellCommandLineFreeVarList (Package);
166 
167   return (ShellStatus);
168 }
169