2 function declarations for shell environment functions.
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
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.
16 #include <ShellBase.h>
18 #include <Guid/ShellVariableGuid.h>
20 #include <Library/BaseLib.h>
21 #include <Library/UefiRuntimeServicesTableLib.h>
22 #include <Library/MemoryAllocationLib.h>
23 #include <Library/DebugLib.h>
24 #include <Library/BaseMemoryLib.h>
26 #include "ShellEnvVar.h"
29 Reports whether an environment variable is Volatile or Non-Volatile.
31 @param EnvVarName The name of the environment variable in question
33 @retval TRUE This environment variable is Volatile
34 @retval FALSE This environment variable is NON-Volatile
39 IN CONST CHAR16
*EnvVarName
53 Status
= gRT
->GetVariable((CHAR16
*)EnvVarName
,
58 if (Status
== EFI_BUFFER_TOO_SMALL
) {
59 Buffer
= AllocateZeroPool(Size
);
60 ASSERT(Buffer
!= NULL
);
61 Status
= gRT
->GetVariable((CHAR16
*)EnvVarName
,
69 // not found means volatile
71 if (Status
== EFI_NOT_FOUND
) {
74 ASSERT_EFI_ERROR(Status
);
77 // check for the Non Volatile bit
79 if ((Attribs
& EFI_VARIABLE_NON_VOLATILE
) == EFI_VARIABLE_NON_VOLATILE
) {
84 // everything else is volatile
90 free function for ENV_VAR_LIST objects.
92 @param[in] List The pointer to pointer to list.
96 FreeEnvironmentVariableList(
102 ASSERT (List
!= NULL
);
107 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(List
)
109 ; Node
= (ENV_VAR_LIST
*)GetFirstNode(List
)
111 ASSERT(Node
!= NULL
);
112 RemoveEntryList(&Node
->Link
);
113 if (Node
->Key
!= NULL
) {
116 if (Node
->Val
!= NULL
) {
124 Creates a list of all Shell-Guid-based environment variables.
126 @param[in,out] ListHead The pointer to pointer to LIST ENTRY object for
129 @retval EFI_SUCCESS the list was created sucessfully.
133 GetEnvironmentVariableList(
134 IN OUT LIST_ENTRY
*ListHead
137 CHAR16
*VariableName
;
145 ENV_VAR_LIST
*VarList
;
147 if (ListHead
== NULL
) {
148 return (EFI_INVALID_PARAMETER
);
151 if (gRT
->Hdr
.Revision
>= EFI_2_00_SYSTEM_TABLE_REVISION
) {
152 Status
= gRT
->QueryVariableInfo(EFI_VARIABLE_NON_VOLATILE
|EFI_VARIABLE_BOOTSERVICE_ACCESS
, &MaxStorSize
, &RemStorSize
, &MaxVarSize
);
153 if (EFI_ERROR(Status
)) {
157 Status
= EFI_SUCCESS
;
161 NameSize
= (UINTN
)MaxVarSize
;
162 VariableName
= AllocateZeroPool(NameSize
);
163 if (VariableName
== NULL
) {
164 return (EFI_OUT_OF_RESOURCES
);
166 StrCpy(VariableName
, L
"");
168 while (!EFI_ERROR(Status
)) {
169 NameSize
= (UINTN
)MaxVarSize
;
170 Status
= gRT
->GetNextVariableName(&NameSize
, VariableName
, &Guid
);
171 if (Status
== EFI_NOT_FOUND
){
172 Status
= EFI_SUCCESS
;
175 if (!EFI_ERROR(Status
) && CompareGuid(&Guid
, &gShellVariableGuid
)){
176 VarList
= AllocateZeroPool(sizeof(ENV_VAR_LIST
));
177 if (VarList
== NULL
) {
178 Status
= EFI_OUT_OF_RESOURCES
;
181 Status
= SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(VariableName
, &VarList
->Atts
, &ValSize
, VarList
->Val
);
182 if (Status
== EFI_BUFFER_TOO_SMALL
){
183 VarList
->Val
= AllocateZeroPool(ValSize
);
184 if (VarList
->Val
== NULL
) {
185 SHELL_FREE_NON_NULL(VarList
);
186 Status
= EFI_OUT_OF_RESOURCES
;
188 Status
= SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(VariableName
, &VarList
->Atts
, &ValSize
, VarList
->Val
);
191 if (!EFI_ERROR(Status
) && VarList
!= NULL
) {
192 VarList
->Key
= AllocateZeroPool(StrSize(VariableName
));
193 if (VarList
->Key
== NULL
) {
194 SHELL_FREE_NON_NULL(VarList
->Val
);
195 SHELL_FREE_NON_NULL(VarList
);
196 Status
= EFI_OUT_OF_RESOURCES
;
198 StrCpy(VarList
->Key
, VariableName
);
199 InsertTailList(ListHead
, &VarList
->Link
);
205 FreePool(VariableName
);
207 if (EFI_ERROR(Status
)) {
208 FreeEnvironmentVariableList(ListHead
);
215 Sets a list of all Shell-Guid-based environment variables. this will
216 also eliminate all existing shell environment variables (even if they
217 are not on the list).
219 This function will also deallocate the memory from List.
221 @param[in] ListHead The pointer to LIST_ENTRY from
222 GetShellEnvVarList().
224 @retval EFI_SUCCESS the list was Set sucessfully.
228 SetEnvironmentVariableList(
229 IN LIST_ENTRY
*ListHead
232 ENV_VAR_LIST VarList
;
237 InitializeListHead(&VarList
.Link
);
240 // Delete all the current environment variables
242 Status
= GetEnvironmentVariableList(&VarList
.Link
);
243 ASSERT_EFI_ERROR(Status
);
245 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(&VarList
.Link
)
246 ; !IsNull(&VarList
.Link
, &Node
->Link
)
247 ; Node
= (ENV_VAR_LIST
*)GetNextNode(&VarList
.Link
, &Node
->Link
)
249 if (Node
->Key
!= NULL
) {
250 Status
= SHELL_DELETE_ENVIRONMENT_VARIABLE(Node
->Key
);
252 ASSERT_EFI_ERROR(Status
);
255 FreeEnvironmentVariableList(&VarList
.Link
);
258 // set all the variables fron the list
260 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(ListHead
)
261 ; !IsNull(ListHead
, &Node
->Link
)
262 ; Node
= (ENV_VAR_LIST
*)GetNextNode(ListHead
, &Node
->Link
)
264 Size
= StrSize(Node
->Val
);
265 if (Node
->Atts
& EFI_VARIABLE_NON_VOLATILE
) {
266 Status
= SHELL_SET_ENVIRONMENT_VARIABLE_NV(Node
->Key
, Size
, Node
->Val
);
268 Status
= SHELL_SET_ENVIRONMENT_VARIABLE_V (Node
->Key
, Size
, Node
->Val
);
270 ASSERT_EFI_ERROR(Status
);
272 FreeEnvironmentVariableList(ListHead
);
278 sets a list of all Shell-Guid-based environment variables.
280 @param Environment Points to a NULL-terminated array of environment
281 variables with the format 'x=y', where x is the
282 environment variable name and y is the value.
284 @retval EFI_SUCCESS The command executed successfully.
285 @retval EFI_INVALID_PARAMETER The parameter is invalid.
286 @retval EFI_OUT_OF_RESOURCES Out of resources.
288 @sa SetEnvironmentVariableList
292 SetEnvironmentVariables(
293 IN CONST CHAR16
**Environment
296 CONST CHAR16
*CurrentString
;
298 ENV_VAR_LIST
*VarList
;
304 if (Environment
== NULL
) {
305 return (EFI_INVALID_PARAMETER
);
309 // Build a list identical to the ones used for get/set list functions above
311 for ( CurrentCount
= 0
315 CurrentString
= Environment
[CurrentCount
];
316 if (CurrentString
== NULL
) {
319 ASSERT(StrStr(CurrentString
, L
"=") != NULL
);
320 Node
= AllocateZeroPool(sizeof(ENV_VAR_LIST
));
321 ASSERT(Node
!= NULL
);
322 Node
->Key
= AllocateZeroPool((StrStr(CurrentString
, L
"=") - CurrentString
+ 1) * sizeof(CHAR16
));
323 ASSERT(Node
->Key
!= NULL
);
324 StrnCpy(Node
->Key
, CurrentString
, StrStr(CurrentString
, L
"=") - CurrentString
);
325 NewSize
= StrSize(CurrentString
);
326 NewSize
-= StrLen(Node
->Key
) - 1;
327 Node
->Val
= AllocateZeroPool(NewSize
);
328 ASSERT(Node
->Val
!= NULL
);
329 StrCpy(Node
->Val
, CurrentString
+ StrLen(Node
->Key
) + 1);
330 Node
->Atts
= EFI_VARIABLE_BOOTSERVICE_ACCESS
;
332 if (VarList
== NULL
) {
333 VarList
= AllocateZeroPool(sizeof(ENV_VAR_LIST
));
334 ASSERT(VarList
!= NULL
);
335 InitializeListHead(&VarList
->Link
);
337 InsertTailList(&VarList
->Link
, &Node
->Link
);
342 // set this new list as the set of all environment variables.
343 // this function also frees the memory and deletes all pre-existing
344 // shell-guid based environment variables.
346 return (SetEnvironmentVariableList(&VarList
->Link
));