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