2 function declarations for shell environment functions.
4 Copyright (c) 2009 - 2016, 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.
17 #define INIT_NAME_BUFFER_SIZE 128
18 #define INIT_DATA_BUFFER_SIZE 1024
21 // The list is used to cache the environment variables.
23 ENV_VAR_LIST gShellEnvVarList
;
26 Reports whether an environment variable is Volatile or Non-Volatile.
28 @param EnvVarName The name of the environment variable in question
29 @param Volatile Return TRUE if the environment variable is volatile
31 @retval EFI_SUCCESS The volatile attribute is returned successfully
32 @retval others Some errors happened.
36 IN CONST CHAR16
*EnvVarName
,
45 ASSERT (Volatile
!= NULL
);
53 Status
= gRT
->GetVariable((CHAR16
*)EnvVarName
,
58 if (Status
== EFI_BUFFER_TOO_SMALL
) {
59 Buffer
= AllocateZeroPool(Size
);
61 return EFI_OUT_OF_RESOURCES
;
63 Status
= gRT
->GetVariable((CHAR16
*)EnvVarName
,
71 // not found means volatile
73 if (Status
== EFI_NOT_FOUND
) {
77 if (EFI_ERROR (Status
)) {
82 // check for the Non Volatile bit
84 *Volatile
= !(BOOLEAN
) ((Attribs
& EFI_VARIABLE_NON_VOLATILE
) == EFI_VARIABLE_NON_VOLATILE
);
89 free function for ENV_VAR_LIST objects.
91 @param[in] List The pointer to pointer to list.
94 FreeEnvironmentVariableList(
100 ASSERT (List
!= NULL
);
105 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(List
)
107 ; Node
= (ENV_VAR_LIST
*)GetFirstNode(List
)
109 ASSERT(Node
!= NULL
);
110 RemoveEntryList(&Node
->Link
);
111 if (Node
->Key
!= NULL
) {
114 if (Node
->Val
!= NULL
) {
122 Creates a list of all Shell-Guid-based environment variables.
124 @param[in, out] ListHead The pointer to pointer to LIST ENTRY object for
127 @retval EFI_SUCCESS the list was created sucessfully.
130 GetEnvironmentVariableList(
131 IN OUT LIST_ENTRY
*ListHead
134 CHAR16
*VariableName
;
136 UINTN NameBufferSize
;
141 ENV_VAR_LIST
*VarList
;
143 if (ListHead
== NULL
) {
144 return (EFI_INVALID_PARAMETER
);
147 Status
= EFI_SUCCESS
;
149 ValBufferSize
= INIT_DATA_BUFFER_SIZE
;
150 NameBufferSize
= INIT_NAME_BUFFER_SIZE
;
151 VariableName
= AllocateZeroPool(NameBufferSize
);
152 if (VariableName
== NULL
) {
153 return (EFI_OUT_OF_RESOURCES
);
155 *VariableName
= CHAR_NULL
;
157 while (!EFI_ERROR(Status
)) {
158 NameSize
= NameBufferSize
;
159 Status
= gRT
->GetNextVariableName(&NameSize
, VariableName
, &Guid
);
160 if (Status
== EFI_NOT_FOUND
){
161 Status
= EFI_SUCCESS
;
163 } else if (Status
== EFI_BUFFER_TOO_SMALL
) {
164 NameBufferSize
= NameSize
> NameBufferSize
* 2 ? NameSize
: NameBufferSize
* 2;
165 SHELL_FREE_NON_NULL(VariableName
);
166 VariableName
= AllocateZeroPool(NameBufferSize
);
167 if (VariableName
== NULL
) {
168 Status
= EFI_OUT_OF_RESOURCES
;
171 NameSize
= NameBufferSize
;
172 Status
= gRT
->GetNextVariableName(&NameSize
, VariableName
, &Guid
);
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
;
180 ValSize
= ValBufferSize
;
182 // We need another CHAR16 to save '\0' in VarList->Val.
184 VarList
->Val
= AllocateZeroPool (ValSize
+ sizeof (CHAR16
));
185 if (VarList
->Val
== NULL
) {
186 SHELL_FREE_NON_NULL(VarList
);
187 Status
= EFI_OUT_OF_RESOURCES
;
190 Status
= SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(VariableName
, &VarList
->Atts
, &ValSize
, VarList
->Val
);
191 if (Status
== EFI_BUFFER_TOO_SMALL
){
192 ValBufferSize
= ValSize
> ValBufferSize
* 2 ? ValSize
: ValBufferSize
* 2;
193 SHELL_FREE_NON_NULL (VarList
->Val
);
195 // We need another CHAR16 to save '\0' in VarList->Val.
197 VarList
->Val
= AllocateZeroPool (ValBufferSize
+ sizeof (CHAR16
));
198 if (VarList
->Val
== NULL
) {
199 SHELL_FREE_NON_NULL(VarList
);
200 Status
= EFI_OUT_OF_RESOURCES
;
204 ValSize
= ValBufferSize
;
205 Status
= SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(VariableName
, &VarList
->Atts
, &ValSize
, VarList
->Val
);
207 if (!EFI_ERROR(Status
)) {
208 VarList
->Key
= AllocateCopyPool(StrSize(VariableName
), VariableName
);
209 if (VarList
->Key
== NULL
) {
210 SHELL_FREE_NON_NULL(VarList
->Val
);
211 SHELL_FREE_NON_NULL(VarList
);
212 Status
= EFI_OUT_OF_RESOURCES
;
214 InsertTailList(ListHead
, &VarList
->Link
);
217 SHELL_FREE_NON_NULL(VarList
->Val
);
218 SHELL_FREE_NON_NULL(VarList
);
220 } // if (VarList == NULL) ... else ...
223 SHELL_FREE_NON_NULL (VariableName
);
225 if (EFI_ERROR(Status
)) {
226 FreeEnvironmentVariableList(ListHead
);
233 Sets a list of all Shell-Guid-based environment variables. this will
234 also eliminate all existing shell environment variables (even if they
235 are not on the list).
237 This function will also deallocate the memory from List.
239 @param[in] ListHead The pointer to LIST_ENTRY from
240 GetShellEnvVarList().
242 @retval EFI_SUCCESS the list was Set sucessfully.
245 SetEnvironmentVariableList(
246 IN LIST_ENTRY
*ListHead
249 ENV_VAR_LIST VarList
;
254 InitializeListHead(&VarList
.Link
);
257 // Delete all the current environment variables
259 Status
= GetEnvironmentVariableList(&VarList
.Link
);
260 ASSERT_EFI_ERROR(Status
);
262 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(&VarList
.Link
)
263 ; !IsNull(&VarList
.Link
, &Node
->Link
)
264 ; Node
= (ENV_VAR_LIST
*)GetNextNode(&VarList
.Link
, &Node
->Link
)
266 if (Node
->Key
!= NULL
) {
267 Status
= SHELL_DELETE_ENVIRONMENT_VARIABLE(Node
->Key
);
269 ASSERT_EFI_ERROR(Status
);
272 FreeEnvironmentVariableList(&VarList
.Link
);
275 // set all the variables fron the list
277 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(ListHead
)
278 ; !IsNull(ListHead
, &Node
->Link
)
279 ; Node
= (ENV_VAR_LIST
*)GetNextNode(ListHead
, &Node
->Link
)
281 Size
= StrSize (Node
->Val
) - sizeof (CHAR16
);
282 if (Node
->Atts
& EFI_VARIABLE_NON_VOLATILE
) {
283 Status
= SHELL_SET_ENVIRONMENT_VARIABLE_NV(Node
->Key
, Size
, Node
->Val
);
285 Status
= SHELL_SET_ENVIRONMENT_VARIABLE_V (Node
->Key
, Size
, Node
->Val
);
287 ASSERT_EFI_ERROR(Status
);
289 FreeEnvironmentVariableList(ListHead
);
295 sets a list of all Shell-Guid-based environment variables.
297 @param Environment Points to a NULL-terminated array of environment
298 variables with the format 'x=y', where x is the
299 environment variable name and y is the value.
301 @retval EFI_SUCCESS The command executed successfully.
302 @retval EFI_INVALID_PARAMETER The parameter is invalid.
303 @retval EFI_OUT_OF_RESOURCES Out of resources.
305 @sa SetEnvironmentVariableList
308 SetEnvironmentVariables(
309 IN CONST CHAR16
**Environment
312 CONST CHAR16
*CurrentString
;
314 ENV_VAR_LIST
*VarList
;
319 if (Environment
== NULL
) {
320 return (EFI_INVALID_PARAMETER
);
324 // Build a list identical to the ones used for get/set list functions above
326 for ( CurrentCount
= 0
330 CurrentString
= Environment
[CurrentCount
];
331 if (CurrentString
== NULL
) {
334 ASSERT(StrStr(CurrentString
, L
"=") != NULL
);
335 Node
= AllocateZeroPool(sizeof(ENV_VAR_LIST
));
337 SetEnvironmentVariableList(&VarList
->Link
);
338 return (EFI_OUT_OF_RESOURCES
);
341 Node
->Key
= AllocateZeroPool((StrStr(CurrentString
, L
"=") - CurrentString
+ 1) * sizeof(CHAR16
));
342 if (Node
->Key
== NULL
) {
343 SHELL_FREE_NON_NULL(Node
);
344 SetEnvironmentVariableList(&VarList
->Link
);
345 return (EFI_OUT_OF_RESOURCES
);
349 // Copy the string into the Key, leaving the last character allocated as NULL to terminate
352 StrStr(CurrentString
, L
"=") - CurrentString
+ 1,
354 StrStr(CurrentString
, L
"=") - CurrentString
358 // ValueSize = TotalSize - already removed size - size for '=' + size for terminator (the last 2 items cancel each other)
360 Node
->Val
= AllocateCopyPool(StrSize(CurrentString
) - StrSize(Node
->Key
), CurrentString
+ StrLen(Node
->Key
) + 1);
361 if (Node
->Val
== NULL
) {
362 SHELL_FREE_NON_NULL(Node
->Key
);
363 SHELL_FREE_NON_NULL(Node
);
364 SetEnvironmentVariableList(&VarList
->Link
);
365 return (EFI_OUT_OF_RESOURCES
);
368 Node
->Atts
= EFI_VARIABLE_BOOTSERVICE_ACCESS
;
370 if (VarList
== NULL
) {
371 VarList
= AllocateZeroPool(sizeof(ENV_VAR_LIST
));
372 if (VarList
== NULL
) {
373 SHELL_FREE_NON_NULL(Node
->Key
);
374 SHELL_FREE_NON_NULL(Node
->Val
);
375 SHELL_FREE_NON_NULL(Node
);
376 return (EFI_OUT_OF_RESOURCES
);
378 InitializeListHead(&VarList
->Link
);
380 InsertTailList(&VarList
->Link
, &Node
->Link
);
385 // set this new list as the set of all environment variables.
386 // this function also frees the memory and deletes all pre-existing
387 // shell-guid based environment variables.
389 return (SetEnvironmentVariableList(&VarList
->Link
));
393 Find an environment variable in the gShellEnvVarList.
395 @param Key The name of the environment variable.
396 @param Value The value of the environment variable, the buffer
397 shoule be freed by the caller.
398 @param ValueSize The size in bytes of the environment variable
399 including the tailing CHAR_NELL.
400 @param Atts The attributes of the variable.
402 @retval EFI_SUCCESS The command executed successfully.
403 @retval EFI_NOT_FOUND The environment variable is not found in
408 ShellFindEnvVarInList (
409 IN CONST CHAR16
*Key
,
411 OUT UINTN
*ValueSize
,
412 OUT UINT32
*Atts OPTIONAL
417 if (Key
== NULL
|| Value
== NULL
|| ValueSize
== NULL
) {
418 return SHELL_INVALID_PARAMETER
;
421 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(&gShellEnvVarList
.Link
)
422 ; !IsNull(&gShellEnvVarList
.Link
, &Node
->Link
)
423 ; Node
= (ENV_VAR_LIST
*)GetNextNode(&gShellEnvVarList
.Link
, &Node
->Link
)
425 if (Node
->Key
!= NULL
&& StrCmp(Key
, Node
->Key
) == 0) {
426 *Value
= AllocateCopyPool(StrSize(Node
->Val
), Node
->Val
);
427 *ValueSize
= StrSize(Node
->Val
);
435 return EFI_NOT_FOUND
;
439 Add an environment variable into gShellEnvVarList.
441 @param Key The name of the environment variable.
442 @param Value The value of environment variable.
443 @param ValueSize The size in bytes of the environment variable
444 including the tailing CHAR_NULL
445 @param Atts The attributes of the variable.
447 @retval EFI_SUCCESS The environment variable was added to list successfully.
448 @retval others Some errors happened.
452 ShellAddEnvVarToList (
453 IN CONST CHAR16
*Key
,
454 IN CONST CHAR16
*Value
,
463 if (Key
== NULL
|| Value
== NULL
|| ValueSize
== 0) {
464 return EFI_INVALID_PARAMETER
;
467 LocalValue
= AllocateCopyPool (ValueSize
, Value
);
468 if (LocalValue
== NULL
) {
469 return EFI_OUT_OF_RESOURCES
;
473 // Update the variable value if it exists in gShellEnvVarList.
475 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(&gShellEnvVarList
.Link
)
476 ; !IsNull(&gShellEnvVarList
.Link
, &Node
->Link
)
477 ; Node
= (ENV_VAR_LIST
*)GetNextNode(&gShellEnvVarList
.Link
, &Node
->Link
)
479 if (Node
->Key
!= NULL
&& StrCmp(Key
, Node
->Key
) == 0) {
481 SHELL_FREE_NON_NULL(Node
->Val
);
482 Node
->Val
= LocalValue
;
488 // If the environment varialbe key doesn't exist in list just insert
491 LocalKey
= AllocateCopyPool (StrSize(Key
), Key
);
492 if (LocalKey
== NULL
) {
493 FreePool (LocalValue
);
494 return EFI_OUT_OF_RESOURCES
;
496 Node
= (ENV_VAR_LIST
*)AllocateZeroPool (sizeof(ENV_VAR_LIST
));
499 FreePool (LocalValue
);
500 return EFI_OUT_OF_RESOURCES
;
502 Node
->Key
= LocalKey
;
503 Node
->Val
= LocalValue
;
505 InsertTailList(&gShellEnvVarList
.Link
, &Node
->Link
);
511 Remove a specified environment variable in gShellEnvVarList.
513 @param Key The name of the environment variable.
515 @retval EFI_SUCCESS The command executed successfully.
516 @retval EFI_NOT_FOUND The environment variable is not found in
520 ShellRemvoeEnvVarFromList (
527 return EFI_INVALID_PARAMETER
;
530 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(&gShellEnvVarList
.Link
)
531 ; !IsNull(&gShellEnvVarList
.Link
, &Node
->Link
)
532 ; Node
= (ENV_VAR_LIST
*)GetNextNode(&gShellEnvVarList
.Link
, &Node
->Link
)
534 if (Node
->Key
!= NULL
&& StrCmp(Key
, Node
->Key
) == 0) {
535 SHELL_FREE_NON_NULL(Node
->Key
);
536 SHELL_FREE_NON_NULL(Node
->Val
);
537 RemoveEntryList(&Node
->Link
);
538 SHELL_FREE_NON_NULL(Node
);
543 return EFI_NOT_FOUND
;
547 Initialize the gShellEnvVarList and cache all Shell-Guid-based environment
552 ShellInitEnvVarList (
558 InitializeListHead(&gShellEnvVarList
.Link
);
559 Status
= GetEnvironmentVariableList (&gShellEnvVarList
.Link
);
565 Destructe the gShellEnvVarList.
569 ShellFreeEnvVarList (
573 FreeEnvironmentVariableList (&gShellEnvVarList
.Link
);
574 InitializeListHead(&gShellEnvVarList
.Link
);