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 StrCpy(VariableName
, L
"");
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
= AllocateZeroPool(StrSize(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 StrCpy(VarList
->Key
, VariableName
);
188 InsertTailList(ListHead
, &VarList
->Link
);
194 FreePool(VariableName
);
196 if (EFI_ERROR(Status
)) {
197 FreeEnvironmentVariableList(ListHead
);
204 Sets a list of all Shell-Guid-based environment variables. this will
205 also eliminate all existing shell environment variables (even if they
206 are not on the list).
208 This function will also deallocate the memory from List.
210 @param[in] ListHead The pointer to LIST_ENTRY from
211 GetShellEnvVarList().
213 @retval EFI_SUCCESS the list was Set sucessfully.
217 SetEnvironmentVariableList(
218 IN LIST_ENTRY
*ListHead
221 ENV_VAR_LIST VarList
;
226 InitializeListHead(&VarList
.Link
);
229 // Delete all the current environment variables
231 Status
= GetEnvironmentVariableList(&VarList
.Link
);
232 ASSERT_EFI_ERROR(Status
);
234 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(&VarList
.Link
)
235 ; !IsNull(&VarList
.Link
, &Node
->Link
)
236 ; Node
= (ENV_VAR_LIST
*)GetNextNode(&VarList
.Link
, &Node
->Link
)
238 if (Node
->Key
!= NULL
) {
239 Status
= SHELL_DELETE_ENVIRONMENT_VARIABLE(Node
->Key
);
241 ASSERT_EFI_ERROR(Status
);
244 FreeEnvironmentVariableList(&VarList
.Link
);
247 // set all the variables fron the list
249 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(ListHead
)
250 ; !IsNull(ListHead
, &Node
->Link
)
251 ; Node
= (ENV_VAR_LIST
*)GetNextNode(ListHead
, &Node
->Link
)
253 Size
= StrSize(Node
->Val
);
254 if (Node
->Atts
& EFI_VARIABLE_NON_VOLATILE
) {
255 Status
= SHELL_SET_ENVIRONMENT_VARIABLE_NV(Node
->Key
, Size
, Node
->Val
);
257 Status
= SHELL_SET_ENVIRONMENT_VARIABLE_V (Node
->Key
, Size
, Node
->Val
);
259 ASSERT_EFI_ERROR(Status
);
261 FreeEnvironmentVariableList(ListHead
);
267 sets a list of all Shell-Guid-based environment variables.
269 @param Environment Points to a NULL-terminated array of environment
270 variables with the format 'x=y', where x is the
271 environment variable name and y is the value.
273 @retval EFI_SUCCESS The command executed successfully.
274 @retval EFI_INVALID_PARAMETER The parameter is invalid.
275 @retval EFI_OUT_OF_RESOURCES Out of resources.
277 @sa SetEnvironmentVariableList
281 SetEnvironmentVariables(
282 IN CONST CHAR16
**Environment
285 CONST CHAR16
*CurrentString
;
287 ENV_VAR_LIST
*VarList
;
293 if (Environment
== NULL
) {
294 return (EFI_INVALID_PARAMETER
);
298 // Build a list identical to the ones used for get/set list functions above
300 for ( CurrentCount
= 0
304 CurrentString
= Environment
[CurrentCount
];
305 if (CurrentString
== NULL
) {
308 ASSERT(StrStr(CurrentString
, L
"=") != NULL
);
309 Node
= AllocateZeroPool(sizeof(ENV_VAR_LIST
));
310 ASSERT(Node
!= NULL
);
311 Node
->Key
= AllocateZeroPool((StrStr(CurrentString
, L
"=") - CurrentString
+ 1) * sizeof(CHAR16
));
312 ASSERT(Node
->Key
!= NULL
);
313 StrnCpy(Node
->Key
, CurrentString
, StrStr(CurrentString
, L
"=") - CurrentString
);
314 NewSize
= StrSize(CurrentString
);
315 NewSize
-= StrLen(Node
->Key
) - 1;
316 Node
->Val
= AllocateZeroPool(NewSize
);
317 ASSERT(Node
->Val
!= NULL
);
318 StrCpy(Node
->Val
, CurrentString
+ StrLen(Node
->Key
) + 1);
319 Node
->Atts
= EFI_VARIABLE_BOOTSERVICE_ACCESS
;
321 if (VarList
== NULL
) {
322 VarList
= AllocateZeroPool(sizeof(ENV_VAR_LIST
));
323 ASSERT(VarList
!= NULL
);
324 InitializeListHead(&VarList
->Link
);
326 InsertTailList(&VarList
->Link
, &Node
->Link
);
331 // set this new list as the set of all environment variables.
332 // this function also frees the memory and deletes all pre-existing
333 // shell-guid based environment variables.
335 return (SetEnvironmentVariableList(&VarList
->Link
));