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.
18 Reports whether an environment variable is Volatile or Non-Volatile.
20 @param EnvVarName The name of the environment variable in question
22 @retval TRUE This environment variable is Volatile
23 @retval FALSE This environment variable is NON-Volatile
28 IN CONST CHAR16
*EnvVarName
42 Status
= gRT
->GetVariable((CHAR16
*)EnvVarName
,
47 if (Status
== EFI_BUFFER_TOO_SMALL
) {
48 Buffer
= AllocateZeroPool(Size
);
49 ASSERT(Buffer
!= NULL
);
50 Status
= gRT
->GetVariable((CHAR16
*)EnvVarName
,
58 // not found means volatile
60 if (Status
== EFI_NOT_FOUND
) {
63 ASSERT_EFI_ERROR(Status
);
66 // check for the Non Volatile bit
68 if ((Attribs
& EFI_VARIABLE_NON_VOLATILE
) == EFI_VARIABLE_NON_VOLATILE
) {
73 // everything else is volatile
79 free function for ENV_VAR_LIST objects.
81 @param[in] List The pointer to pointer to list.
85 FreeEnvironmentVariableList(
91 ASSERT (List
!= NULL
);
96 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(List
)
98 ; Node
= (ENV_VAR_LIST
*)GetFirstNode(List
)
100 ASSERT(Node
!= NULL
);
101 RemoveEntryList(&Node
->Link
);
102 if (Node
->Key
!= NULL
) {
105 if (Node
->Val
!= NULL
) {
113 Creates a list of all Shell-Guid-based environment variables.
115 @param[in, out] ListHead The pointer to pointer to LIST ENTRY object for
118 @retval EFI_SUCCESS the list was created sucessfully.
122 GetEnvironmentVariableList(
123 IN OUT LIST_ENTRY
*ListHead
126 CHAR16
*VariableName
;
134 ENV_VAR_LIST
*VarList
;
136 if (ListHead
== NULL
) {
137 return (EFI_INVALID_PARAMETER
);
140 if (gRT
->Hdr
.Revision
>= EFI_2_00_SYSTEM_TABLE_REVISION
) {
141 Status
= gRT
->QueryVariableInfo(EFI_VARIABLE_NON_VOLATILE
|EFI_VARIABLE_BOOTSERVICE_ACCESS
, &MaxStorSize
, &RemStorSize
, &MaxVarSize
);
142 if (EFI_ERROR(Status
)) {
146 Status
= EFI_SUCCESS
;
150 NameSize
= (UINTN
)MaxVarSize
;
151 VariableName
= AllocateZeroPool(NameSize
);
152 if (VariableName
== NULL
) {
153 return (EFI_OUT_OF_RESOURCES
);
155 *VariableName
= CHAR_NULL
;
157 while (!EFI_ERROR(Status
)) {
158 NameSize
= (UINTN
)MaxVarSize
;
159 Status
= gRT
->GetNextVariableName(&NameSize
, VariableName
, &Guid
);
160 if (Status
== EFI_NOT_FOUND
){
161 Status
= EFI_SUCCESS
;
164 if (!EFI_ERROR(Status
) && CompareGuid(&Guid
, &gShellVariableGuid
)){
165 VarList
= AllocateZeroPool(sizeof(ENV_VAR_LIST
));
166 if (VarList
== NULL
) {
167 Status
= EFI_OUT_OF_RESOURCES
;
170 Status
= SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(VariableName
, &VarList
->Atts
, &ValSize
, VarList
->Val
);
171 if (Status
== EFI_BUFFER_TOO_SMALL
){
172 VarList
->Val
= AllocateZeroPool(ValSize
);
173 if (VarList
->Val
== NULL
) {
174 SHELL_FREE_NON_NULL(VarList
);
175 Status
= EFI_OUT_OF_RESOURCES
;
177 Status
= SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(VariableName
, &VarList
->Atts
, &ValSize
, VarList
->Val
);
180 if (!EFI_ERROR(Status
) && VarList
!= NULL
) {
181 VarList
->Key
= AllocateCopyPool(StrSize(VariableName
), VariableName
);
182 if (VarList
->Key
== NULL
) {
183 SHELL_FREE_NON_NULL(VarList
->Val
);
184 SHELL_FREE_NON_NULL(VarList
);
185 Status
= EFI_OUT_OF_RESOURCES
;
187 InsertTailList(ListHead
, &VarList
->Link
);
193 FreePool(VariableName
);
195 if (EFI_ERROR(Status
)) {
196 FreeEnvironmentVariableList(ListHead
);
203 Sets a list of all Shell-Guid-based environment variables. this will
204 also eliminate all existing shell environment variables (even if they
205 are not on the list).
207 This function will also deallocate the memory from List.
209 @param[in] ListHead The pointer to LIST_ENTRY from
210 GetShellEnvVarList().
212 @retval EFI_SUCCESS the list was Set sucessfully.
216 SetEnvironmentVariableList(
217 IN LIST_ENTRY
*ListHead
220 ENV_VAR_LIST VarList
;
225 InitializeListHead(&VarList
.Link
);
228 // Delete all the current environment variables
230 Status
= GetEnvironmentVariableList(&VarList
.Link
);
231 ASSERT_EFI_ERROR(Status
);
233 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(&VarList
.Link
)
234 ; !IsNull(&VarList
.Link
, &Node
->Link
)
235 ; Node
= (ENV_VAR_LIST
*)GetNextNode(&VarList
.Link
, &Node
->Link
)
237 if (Node
->Key
!= NULL
) {
238 Status
= SHELL_DELETE_ENVIRONMENT_VARIABLE(Node
->Key
);
240 ASSERT_EFI_ERROR(Status
);
243 FreeEnvironmentVariableList(&VarList
.Link
);
246 // set all the variables fron the list
248 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(ListHead
)
249 ; !IsNull(ListHead
, &Node
->Link
)
250 ; Node
= (ENV_VAR_LIST
*)GetNextNode(ListHead
, &Node
->Link
)
252 Size
= StrSize(Node
->Val
);
253 if (Node
->Atts
& EFI_VARIABLE_NON_VOLATILE
) {
254 Status
= SHELL_SET_ENVIRONMENT_VARIABLE_NV(Node
->Key
, Size
, Node
->Val
);
256 Status
= SHELL_SET_ENVIRONMENT_VARIABLE_V (Node
->Key
, Size
, Node
->Val
);
258 ASSERT_EFI_ERROR(Status
);
260 FreeEnvironmentVariableList(ListHead
);
266 sets a list of all Shell-Guid-based environment variables.
268 @param Environment Points to a NULL-terminated array of environment
269 variables with the format 'x=y', where x is the
270 environment variable name and y is the value.
272 @retval EFI_SUCCESS The command executed successfully.
273 @retval EFI_INVALID_PARAMETER The parameter is invalid.
274 @retval EFI_OUT_OF_RESOURCES Out of resources.
276 @sa SetEnvironmentVariableList
280 SetEnvironmentVariables(
281 IN CONST CHAR16
**Environment
284 CONST CHAR16
*CurrentString
;
286 ENV_VAR_LIST
*VarList
;
291 if (Environment
== NULL
) {
292 return (EFI_INVALID_PARAMETER
);
296 // Build a list identical to the ones used for get/set list functions above
298 for ( CurrentCount
= 0
302 CurrentString
= Environment
[CurrentCount
];
303 if (CurrentString
== NULL
) {
306 ASSERT(StrStr(CurrentString
, L
"=") != NULL
);
307 Node
= AllocateZeroPool(sizeof(ENV_VAR_LIST
));
309 SetEnvironmentVariableList(&VarList
->Link
);
310 return (EFI_OUT_OF_RESOURCES
);
313 Node
->Key
= AllocateZeroPool((StrStr(CurrentString
, L
"=") - CurrentString
+ 1) * sizeof(CHAR16
));
314 if (Node
->Key
== NULL
) {
315 SHELL_FREE_NON_NULL(Node
);
316 SetEnvironmentVariableList(&VarList
->Link
);
317 return (EFI_OUT_OF_RESOURCES
);
321 // Copy the string into the Key, leaving the last character allocated as NULL to terminate
323 StrnCpy(Node
->Key
, CurrentString
, StrStr(CurrentString
, L
"=") - CurrentString
);
326 // ValueSize = TotalSize - already removed size - size for '=' + size for terminator (the last 2 items cancel each other)
328 Node
->Val
= AllocateCopyPool(StrSize(CurrentString
) - StrSize(Node
->Key
), CurrentString
+ StrLen(Node
->Key
) + 1);
329 if (Node
->Val
== NULL
) {
330 SHELL_FREE_NON_NULL(Node
->Key
);
331 SHELL_FREE_NON_NULL(Node
);
332 SetEnvironmentVariableList(&VarList
->Link
);
333 return (EFI_OUT_OF_RESOURCES
);
336 Node
->Atts
= EFI_VARIABLE_BOOTSERVICE_ACCESS
;
338 if (VarList
== NULL
) {
339 VarList
= AllocateZeroPool(sizeof(ENV_VAR_LIST
));
340 if (VarList
== NULL
) {
341 SHELL_FREE_NON_NULL(Node
->Key
);
342 SHELL_FREE_NON_NULL(Node
->Val
);
343 SHELL_FREE_NON_NULL(Node
);
344 return (EFI_OUT_OF_RESOURCES
);
346 InitializeListHead(&VarList
->Link
);
348 InsertTailList(&VarList
->Link
, &Node
->Link
);
353 // set this new list as the set of all environment variables.
354 // this function also frees the memory and deletes all pre-existing
355 // shell-guid based environment variables.
357 return (SetEnvironmentVariableList(&VarList
->Link
));