]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Application/Shell/ShellEnvVar.c
f40a867cc7ae564a6f4e607e36e4ed56205fab5f
[mirror_edk2.git] / ShellPkg / Application / Shell / ShellEnvVar.c
1 /** @file
2 function declarations for shell environment functions.
3
4 Copyright (c) 2009 - 2011, 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 "Shell.h"
16
17 /**
18 Reports whether an environment variable is Volatile or Non-Volatile.
19
20 @param EnvVarName The name of the environment variable in question
21
22 @retval TRUE This environment variable is Volatile
23 @retval FALSE This environment variable is NON-Volatile
24 **/
25 BOOLEAN
26 EFIAPI
27 IsVolatileEnv (
28 IN CONST CHAR16 *EnvVarName
29 )
30 {
31 EFI_STATUS Status;
32 UINTN Size;
33 VOID *Buffer;
34 UINT32 Attribs;
35
36 Size = 0;
37 Buffer = NULL;
38
39 //
40 // get the variable
41 //
42 Status = gRT->GetVariable((CHAR16*)EnvVarName,
43 &gShellVariableGuid,
44 &Attribs,
45 &Size,
46 Buffer);
47 if (Status == EFI_BUFFER_TOO_SMALL) {
48 Buffer = AllocateZeroPool(Size);
49 ASSERT(Buffer != NULL);
50 Status = gRT->GetVariable((CHAR16*)EnvVarName,
51 &gShellVariableGuid,
52 &Attribs,
53 &Size,
54 Buffer);
55 FreePool(Buffer);
56 }
57 //
58 // not found means volatile
59 //
60 if (Status == EFI_NOT_FOUND) {
61 return (TRUE);
62 }
63 ASSERT_EFI_ERROR(Status);
64
65 //
66 // check for the Non Volatile bit
67 //
68 if ((Attribs & EFI_VARIABLE_NON_VOLATILE) == EFI_VARIABLE_NON_VOLATILE) {
69 return (FALSE);
70 }
71
72 //
73 // everything else is volatile
74 //
75 return (TRUE);
76 }
77
78 /**
79 free function for ENV_VAR_LIST objects.
80
81 @param[in] List The pointer to pointer to list.
82 **/
83 VOID
84 EFIAPI
85 FreeEnvironmentVariableList(
86 IN LIST_ENTRY *List
87 )
88 {
89 ENV_VAR_LIST *Node;
90
91 ASSERT (List != NULL);
92 if (List == NULL) {
93 return;
94 }
95
96 for ( Node = (ENV_VAR_LIST*)GetFirstNode(List)
97 ; !IsListEmpty(List)
98 ; Node = (ENV_VAR_LIST*)GetFirstNode(List)
99 ){
100 ASSERT(Node != NULL);
101 RemoveEntryList(&Node->Link);
102 if (Node->Key != NULL) {
103 FreePool(Node->Key);
104 }
105 if (Node->Val != NULL) {
106 FreePool(Node->Val);
107 }
108 FreePool(Node);
109 }
110 }
111
112 /**
113 Creates a list of all Shell-Guid-based environment variables.
114
115 @param[in, out] ListHead The pointer to pointer to LIST ENTRY object for
116 storing this list.
117
118 @retval EFI_SUCCESS the list was created sucessfully.
119 **/
120 EFI_STATUS
121 EFIAPI
122 GetEnvironmentVariableList(
123 IN OUT LIST_ENTRY *ListHead
124 )
125 {
126 CHAR16 *VariableName;
127 UINTN NameSize;
128 UINT64 MaxStorSize;
129 UINT64 RemStorSize;
130 UINT64 MaxVarSize;
131 EFI_STATUS Status;
132 EFI_GUID Guid;
133 UINTN ValSize;
134 ENV_VAR_LIST *VarList;
135
136 if (ListHead == NULL) {
137 return (EFI_INVALID_PARAMETER);
138 }
139
140 if (gRT->Hdr.Revision >= EFI_2_00_SYSTEM_TABLE_REVISION) {
141 Status = gRT->QueryVariableInfo(EFI_VARIABLE_NON_VOLATILE|EFI_VARIABLE_BOOTSERVICE_ACCESS, &MaxStorSize, &RemStorSize, &MaxVarSize);
142 if (EFI_ERROR(Status)) {
143 return (Status);
144 }
145 } else {
146 Status = EFI_SUCCESS;
147 MaxVarSize = 16384;
148 }
149
150 NameSize = (UINTN)MaxVarSize;
151 VariableName = AllocateZeroPool(NameSize);
152 if (VariableName == NULL) {
153 return (EFI_OUT_OF_RESOURCES);
154 }
155 StrCpy(VariableName, L"");
156
157 while (!EFI_ERROR(Status)) {
158 NameSize = (UINTN)MaxVarSize;
159 Status = gRT->GetNextVariableName(&NameSize, VariableName, &Guid);
160 if (Status == EFI_NOT_FOUND){
161 Status = EFI_SUCCESS;
162 break;
163 }
164 if (!EFI_ERROR(Status) && CompareGuid(&Guid, &gShellVariableGuid)){
165 VarList = AllocateZeroPool(sizeof(ENV_VAR_LIST));
166 if (VarList == NULL) {
167 Status = EFI_OUT_OF_RESOURCES;
168 } else {
169 ValSize = 0;
170 Status = SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(VariableName, &VarList->Atts, &ValSize, VarList->Val);
171 if (Status == EFI_BUFFER_TOO_SMALL){
172 VarList->Val = AllocateZeroPool(ValSize);
173 if (VarList->Val == NULL) {
174 SHELL_FREE_NON_NULL(VarList);
175 Status = EFI_OUT_OF_RESOURCES;
176 } else {
177 Status = SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(VariableName, &VarList->Atts, &ValSize, VarList->Val);
178 }
179 }
180 if (!EFI_ERROR(Status) && VarList != NULL) {
181 VarList->Key = AllocateZeroPool(StrSize(VariableName));
182 if (VarList->Key == NULL) {
183 SHELL_FREE_NON_NULL(VarList->Val);
184 SHELL_FREE_NON_NULL(VarList);
185 Status = EFI_OUT_OF_RESOURCES;
186 } else {
187 StrCpy(VarList->Key, VariableName);
188 InsertTailList(ListHead, &VarList->Link);
189 }
190 }
191 }
192 } // compare guid
193 } // while
194 FreePool(VariableName);
195
196 if (EFI_ERROR(Status)) {
197 FreeEnvironmentVariableList(ListHead);
198 }
199
200 return (Status);
201 }
202
203 /**
204 Sets a list of all Shell-Guid-based environment variables. this will
205 also eliminate all existing shell environment variables (even if they
206 are not on the list).
207
208 This function will also deallocate the memory from List.
209
210 @param[in] ListHead The pointer to LIST_ENTRY from
211 GetShellEnvVarList().
212
213 @retval EFI_SUCCESS the list was Set sucessfully.
214 **/
215 EFI_STATUS
216 EFIAPI
217 SetEnvironmentVariableList(
218 IN LIST_ENTRY *ListHead
219 )
220 {
221 ENV_VAR_LIST VarList;
222 ENV_VAR_LIST *Node;
223 EFI_STATUS Status;
224 UINTN Size;
225
226 InitializeListHead(&VarList.Link);
227
228 //
229 // Delete all the current environment variables
230 //
231 Status = GetEnvironmentVariableList(&VarList.Link);
232 ASSERT_EFI_ERROR(Status);
233
234 for ( Node = (ENV_VAR_LIST*)GetFirstNode(&VarList.Link)
235 ; !IsNull(&VarList.Link, &Node->Link)
236 ; Node = (ENV_VAR_LIST*)GetNextNode(&VarList.Link, &Node->Link)
237 ){
238 if (Node->Key != NULL) {
239 Status = SHELL_DELETE_ENVIRONMENT_VARIABLE(Node->Key);
240 }
241 ASSERT_EFI_ERROR(Status);
242 }
243
244 FreeEnvironmentVariableList(&VarList.Link);
245
246 //
247 // set all the variables fron the list
248 //
249 for ( Node = (ENV_VAR_LIST*)GetFirstNode(ListHead)
250 ; !IsNull(ListHead, &Node->Link)
251 ; Node = (ENV_VAR_LIST*)GetNextNode(ListHead, &Node->Link)
252 ){
253 Size = StrSize(Node->Val);
254 if (Node->Atts & EFI_VARIABLE_NON_VOLATILE) {
255 Status = SHELL_SET_ENVIRONMENT_VARIABLE_NV(Node->Key, Size, Node->Val);
256 } else {
257 Status = SHELL_SET_ENVIRONMENT_VARIABLE_V (Node->Key, Size, Node->Val);
258 }
259 ASSERT_EFI_ERROR(Status);
260 }
261 FreeEnvironmentVariableList(ListHead);
262
263 return (Status);
264 }
265
266 /**
267 sets a list of all Shell-Guid-based environment variables.
268
269 @param Environment Points to a NULL-terminated array of environment
270 variables with the format 'x=y', where x is the
271 environment variable name and y is the value.
272
273 @retval EFI_SUCCESS The command executed successfully.
274 @retval EFI_INVALID_PARAMETER The parameter is invalid.
275 @retval EFI_OUT_OF_RESOURCES Out of resources.
276
277 @sa SetEnvironmentVariableList
278 **/
279 EFI_STATUS
280 EFIAPI
281 SetEnvironmentVariables(
282 IN CONST CHAR16 **Environment
283 )
284 {
285 CONST CHAR16 *CurrentString;
286 UINTN CurrentCount;
287 ENV_VAR_LIST *VarList;
288 ENV_VAR_LIST *Node;
289 UINTN NewSize;
290
291 VarList = NULL;
292
293 if (Environment == NULL) {
294 return (EFI_INVALID_PARAMETER);
295 }
296
297 //
298 // Build a list identical to the ones used for get/set list functions above
299 //
300 for ( CurrentCount = 0
301 ;
302 ; CurrentCount++
303 ){
304 CurrentString = Environment[CurrentCount];
305 if (CurrentString == NULL) {
306 break;
307 }
308 ASSERT(StrStr(CurrentString, L"=") != NULL);
309 Node = AllocateZeroPool(sizeof(ENV_VAR_LIST));
310 ASSERT(Node != NULL);
311 Node->Key = AllocateZeroPool((StrStr(CurrentString, L"=") - CurrentString + 1) * sizeof(CHAR16));
312 ASSERT(Node->Key != NULL);
313 StrnCpy(Node->Key, CurrentString, StrStr(CurrentString, L"=") - CurrentString);
314 NewSize = StrSize(CurrentString);
315 NewSize -= StrLen(Node->Key) - 1;
316 Node->Val = AllocateZeroPool(NewSize);
317 ASSERT(Node->Val != NULL);
318 StrCpy(Node->Val, CurrentString + StrLen(Node->Key) + 1);
319 Node->Atts = EFI_VARIABLE_BOOTSERVICE_ACCESS;
320
321 if (VarList == NULL) {
322 VarList = AllocateZeroPool(sizeof(ENV_VAR_LIST));
323 ASSERT(VarList != NULL);
324 InitializeListHead(&VarList->Link);
325 }
326 InsertTailList(&VarList->Link, &Node->Link);
327
328 } // for loop
329
330 //
331 // set this new list as the set of all environment variables.
332 // this function also frees the memory and deletes all pre-existing
333 // shell-guid based environment variables.
334 //
335 return (SetEnvironmentVariableList(&VarList->Link));
336 }