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