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