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