]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Application/Shell/ShellEnvVar.c
remove additional space from comments and pass ICC/GCC44 build
[mirror_edk2.git] / ShellPkg / Application / Shell / ShellEnvVar.c
CommitLineData
a405b86d 1/** @file\r
2 function declarations for shell environment functions.\r
3\r
4 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>\r
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
16\r
17#include <Guid/ShellVariableGuid.h>\r
18\r
19#include <Library/BaseLib.h>\r
20#include <Library/UefiRuntimeServicesTableLib.h>\r
21#include <Library/MemoryAllocationLib.h>\r
22#include <Library/DebugLib.h>\r
23#include <Library/BaseMemoryLib.h>\r
24\r
25#include "ShellEnvVar.h"\r
26\r
27\r
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
59 Buffer = AllocatePool(Size);\r
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
126 @param[in,out] ListHead The pointer to pointer to LIST ENTRY object for\r
127 storing this list.\r
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
147 ASSERT(ListHead != NULL);\r
148\r
149 Status = gRT->QueryVariableInfo(EFI_VARIABLE_NON_VOLATILE|EFI_VARIABLE_BOOTSERVICE_ACCESS, &MaxStorSize, &RemStorSize, &MaxVarSize);\r
150 ASSERT_EFI_ERROR(Status);\r
151\r
152 NameSize = (UINTN)MaxVarSize;\r
153 VariableName = AllocatePool(NameSize);\r
154 StrCpy(VariableName, L"");\r
155\r
156 while (TRUE) {\r
157 NameSize = (UINTN)MaxVarSize;\r
158 Status = gRT->GetNextVariableName(&NameSize, VariableName, &Guid);\r
159 if (Status == EFI_NOT_FOUND){\r
160 Status = EFI_SUCCESS;\r
161 break;\r
162 }\r
163 ASSERT_EFI_ERROR(Status);\r
164 if (EFI_ERROR(Status)) {\r
165 break;\r
166 }\r
167 if (CompareGuid(&Guid, &gShellVariableGuid)){\r
168 VarList = AllocateZeroPool(sizeof(ENV_VAR_LIST));\r
169 ValSize = 0;\r
170 Status = SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(VariableName, &VarList->Atts, &ValSize, VarList->Val);\r
171 if (Status == EFI_BUFFER_TOO_SMALL){\r
172 VarList->Val = AllocatePool(ValSize);\r
173 ASSERT(VarList->Val != NULL);\r
174 Status = SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(VariableName, &VarList->Atts, &ValSize, VarList->Val);\r
175 }\r
176 ASSERT_EFI_ERROR(Status);\r
177 VarList->Key = AllocatePool(StrSize(VariableName));\r
178 ASSERT(VarList->Key != NULL);\r
179 StrCpy(VarList->Key, VariableName);\r
180\r
181 InsertTailList(ListHead, &VarList->Link);\r
182 } // compare guid\r
183 } // while\r
184 FreePool(VariableName);\r
185\r
186 if (EFI_ERROR(Status)) {\r
187 FreeEnvironmentVariableList(ListHead);\r
188 }\r
189\r
190 return (Status);\r
191}\r
192\r
193/**\r
194 Sets a list of all Shell-Guid-based environment variables. this will\r
195 also eliminate all existing shell environment variables (even if they\r
196 are not on the list).\r
197\r
198 This function will also deallocate the memory from List.\r
199\r
200 @param[in] ListHead The pointer to LIST_ENTRY from\r
201 GetShellEnvVarList().\r
202\r
203 @retval EFI_SUCCESS the list was Set sucessfully.\r
204**/\r
205EFI_STATUS\r
206EFIAPI\r
207SetEnvironmentVariableList(\r
208 IN LIST_ENTRY *ListHead\r
209 )\r
210{\r
211 ENV_VAR_LIST VarList;\r
212 ENV_VAR_LIST *Node;\r
213 EFI_STATUS Status;\r
214 UINTN Size;\r
215\r
216 InitializeListHead(&VarList.Link);\r
217\r
218 //\r
219 // Delete all the current environment variables\r
220 //\r
221 Status = GetEnvironmentVariableList(&VarList.Link);\r
222 ASSERT_EFI_ERROR(Status);\r
223\r
224 for ( Node = (ENV_VAR_LIST*)GetFirstNode(&VarList.Link)\r
225 ; !IsNull(&VarList.Link, &Node->Link)\r
226 ; Node = (ENV_VAR_LIST*)GetNextNode(&VarList.Link, &Node->Link)\r
227 ){\r
228 if (Node->Key != NULL) {\r
229 Status = SHELL_DELETE_ENVIRONMENT_VARIABLE(Node->Key);\r
230 }\r
231 ASSERT_EFI_ERROR(Status);\r
232 }\r
233\r
234 FreeEnvironmentVariableList(&VarList.Link);\r
235\r
236 //\r
237 // set all the variables fron the list\r
238 //\r
239 for ( Node = (ENV_VAR_LIST*)GetFirstNode(ListHead)\r
240 ; !IsNull(ListHead, &Node->Link)\r
241 ; Node = (ENV_VAR_LIST*)GetNextNode(ListHead, &Node->Link)\r
242 ){\r
243 Size = StrSize(Node->Val);\r
244 if (Node->Atts & EFI_VARIABLE_NON_VOLATILE) {\r
245 Status = SHELL_SET_ENVIRONMENT_VARIABLE_NV(Node->Key, Size, Node->Val);\r
246 } else {\r
247 Status = SHELL_SET_ENVIRONMENT_VARIABLE_V (Node->Key, Size, Node->Val);\r
248 }\r
249 ASSERT_EFI_ERROR(Status);\r
250 }\r
251 FreeEnvironmentVariableList(ListHead);\r
252\r
253 return (Status);\r
254}\r
255\r
256/**\r
257 sets a list of all Shell-Guid-based environment variables.\r
258\r
259 @param Environment Points to a NULL-terminated array of environment\r
260 variables with the format 'x=y', where x is the\r
261 environment variable name and y is the value.\r
262\r
263 @retval EFI_SUCCESS The command executed successfully.\r
264 @retval EFI_INVALID_PARAMETER The parameter is invalid.\r
265 @retval EFI_OUT_OF_RESOURCES Out of resources.\r
266\r
267 @sa SetEnvironmentVariableList\r
268**/\r
269EFI_STATUS\r
270EFIAPI\r
271SetEnvironmentVariables(\r
272 IN CONST CHAR16 **Environment\r
273 )\r
274{\r
275 CONST CHAR16 *CurrentString;\r
276 UINTN CurrentCount;\r
277 ENV_VAR_LIST *VarList;\r
278 ENV_VAR_LIST *Node;\r
279 UINTN NewSize;\r
280\r
281 VarList = NULL;\r
282\r
283 if (Environment == NULL) {\r
284 return (EFI_INVALID_PARAMETER);\r
285 }\r
286\r
287 //\r
288 // Build a list identical to the ones used for get/set list functions above\r
289 //\r
290 for ( CurrentCount = 0\r
291 ;\r
292 ; CurrentCount++\r
293 ){\r
294 CurrentString = Environment[CurrentCount];\r
295 if (CurrentString == NULL) {\r
296 break;\r
297 }\r
298 ASSERT(StrStr(CurrentString, L"=") != NULL);\r
299 Node = AllocatePool(sizeof(ENV_VAR_LIST));\r
300 ASSERT(Node != NULL);\r
301 Node->Key = AllocateZeroPool((StrStr(CurrentString, L"=") - CurrentString + 1) * sizeof(CHAR16));\r
302 ASSERT(Node->Key != NULL);\r
303 StrnCpy(Node->Key, CurrentString, StrStr(CurrentString, L"=") - CurrentString);\r
304 NewSize = StrSize(CurrentString);\r
305 NewSize -= StrLen(Node->Key) - 1;\r
306 Node->Val = AllocateZeroPool(NewSize);\r
307 ASSERT(Node->Val != NULL);\r
308 StrCpy(Node->Val, CurrentString + StrLen(Node->Key) + 1);\r
309 Node->Atts = EFI_VARIABLE_BOOTSERVICE_ACCESS;\r
310\r
311 if (VarList == NULL) {\r
312 VarList = AllocateZeroPool(sizeof(ENV_VAR_LIST));\r
313 ASSERT(VarList != NULL);\r
314 InitializeListHead(&VarList->Link);\r
315 }\r
316 InsertTailList(&VarList->Link, &Node->Link);\r
317\r
318 } // for loop\r
319\r
320 //\r
321 // set this new list as the set of all environment variables.\r
322 // this function also frees the memory and deletes all pre-existing\r
323 // shell-guid based environment variables.\r
324 //\r
325 return (SetEnvironmentVariableList(&VarList->Link));\r
326}\r