2 function declarations for shell environment functions.
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #define INIT_NAME_BUFFER_SIZE 128
12 #define INIT_DATA_BUFFER_SIZE 1024
15 // The list is used to cache the environment variables.
17 ENV_VAR_LIST gShellEnvVarList
;
20 Reports whether an environment variable is Volatile or Non-Volatile.
22 @param EnvVarName The name of the environment variable in question
23 @param Volatile Return TRUE if the environment variable is volatile
25 @retval EFI_SUCCESS The volatile attribute is returned successfully
26 @retval others Some errors happened.
30 IN CONST CHAR16
*EnvVarName
,
39 ASSERT (Volatile
!= NULL
);
47 Status
= gRT
->GetVariable((CHAR16
*)EnvVarName
,
52 if (Status
== EFI_BUFFER_TOO_SMALL
) {
53 Buffer
= AllocateZeroPool(Size
);
55 return EFI_OUT_OF_RESOURCES
;
57 Status
= gRT
->GetVariable((CHAR16
*)EnvVarName
,
65 // not found means volatile
67 if (Status
== EFI_NOT_FOUND
) {
71 if (EFI_ERROR (Status
)) {
76 // check for the Non Volatile bit
78 *Volatile
= !(BOOLEAN
) ((Attribs
& EFI_VARIABLE_NON_VOLATILE
) == EFI_VARIABLE_NON_VOLATILE
);
83 free function for ENV_VAR_LIST objects.
85 @param[in] List The pointer to pointer to list.
88 FreeEnvironmentVariableList(
94 ASSERT (List
!= NULL
);
99 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(List
)
101 ; Node
= (ENV_VAR_LIST
*)GetFirstNode(List
)
103 ASSERT(Node
!= NULL
);
104 RemoveEntryList(&Node
->Link
);
105 if (Node
->Key
!= NULL
) {
108 if (Node
->Val
!= NULL
) {
116 Creates a list of all Shell-Guid-based environment variables.
118 @param[in, out] ListHead The pointer to pointer to LIST ENTRY object for
121 @retval EFI_SUCCESS the list was created successfully.
124 GetEnvironmentVariableList(
125 IN OUT LIST_ENTRY
*ListHead
128 CHAR16
*VariableName
;
130 UINTN NameBufferSize
;
135 ENV_VAR_LIST
*VarList
;
137 if (ListHead
== NULL
) {
138 return (EFI_INVALID_PARAMETER
);
141 Status
= EFI_SUCCESS
;
143 ValBufferSize
= INIT_DATA_BUFFER_SIZE
;
144 NameBufferSize
= INIT_NAME_BUFFER_SIZE
;
145 VariableName
= AllocateZeroPool(NameBufferSize
);
146 if (VariableName
== NULL
) {
147 return (EFI_OUT_OF_RESOURCES
);
149 *VariableName
= CHAR_NULL
;
151 while (!EFI_ERROR(Status
)) {
152 NameSize
= NameBufferSize
;
153 Status
= gRT
->GetNextVariableName(&NameSize
, VariableName
, &Guid
);
154 if (Status
== EFI_NOT_FOUND
){
155 Status
= EFI_SUCCESS
;
157 } else if (Status
== EFI_BUFFER_TOO_SMALL
) {
158 NameBufferSize
= NameSize
> NameBufferSize
* 2 ? NameSize
: NameBufferSize
* 2;
159 SHELL_FREE_NON_NULL(VariableName
);
160 VariableName
= AllocateZeroPool(NameBufferSize
);
161 if (VariableName
== NULL
) {
162 Status
= EFI_OUT_OF_RESOURCES
;
165 NameSize
= NameBufferSize
;
166 Status
= gRT
->GetNextVariableName(&NameSize
, VariableName
, &Guid
);
169 if (!EFI_ERROR(Status
) && CompareGuid(&Guid
, &gShellVariableGuid
)){
170 VarList
= AllocateZeroPool(sizeof(ENV_VAR_LIST
));
171 if (VarList
== NULL
) {
172 Status
= EFI_OUT_OF_RESOURCES
;
174 ValSize
= ValBufferSize
;
176 // We need another CHAR16 to save '\0' in VarList->Val.
178 VarList
->Val
= AllocateZeroPool (ValSize
+ sizeof (CHAR16
));
179 if (VarList
->Val
== NULL
) {
180 SHELL_FREE_NON_NULL(VarList
);
181 Status
= EFI_OUT_OF_RESOURCES
;
184 Status
= SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(VariableName
, &VarList
->Atts
, &ValSize
, VarList
->Val
);
185 if (Status
== EFI_BUFFER_TOO_SMALL
){
186 ValBufferSize
= ValSize
> ValBufferSize
* 2 ? ValSize
: ValBufferSize
* 2;
187 SHELL_FREE_NON_NULL (VarList
->Val
);
189 // We need another CHAR16 to save '\0' in VarList->Val.
191 VarList
->Val
= AllocateZeroPool (ValBufferSize
+ sizeof (CHAR16
));
192 if (VarList
->Val
== NULL
) {
193 SHELL_FREE_NON_NULL(VarList
);
194 Status
= EFI_OUT_OF_RESOURCES
;
198 ValSize
= ValBufferSize
;
199 Status
= SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(VariableName
, &VarList
->Atts
, &ValSize
, VarList
->Val
);
201 if (!EFI_ERROR(Status
)) {
202 VarList
->Key
= AllocateCopyPool(StrSize(VariableName
), VariableName
);
203 if (VarList
->Key
== NULL
) {
204 SHELL_FREE_NON_NULL(VarList
->Val
);
205 SHELL_FREE_NON_NULL(VarList
);
206 Status
= EFI_OUT_OF_RESOURCES
;
208 InsertTailList(ListHead
, &VarList
->Link
);
211 SHELL_FREE_NON_NULL(VarList
->Val
);
212 SHELL_FREE_NON_NULL(VarList
);
214 } // if (VarList == NULL) ... else ...
217 SHELL_FREE_NON_NULL (VariableName
);
219 if (EFI_ERROR(Status
)) {
220 FreeEnvironmentVariableList(ListHead
);
227 Sets a list of all Shell-Guid-based environment variables. this will
228 also eliminate all existing shell environment variables (even if they
229 are not on the list).
231 This function will also deallocate the memory from List.
233 @param[in] ListHead The pointer to LIST_ENTRY from
234 GetShellEnvVarList().
236 @retval EFI_SUCCESS the list was Set successfully.
239 SetEnvironmentVariableList(
240 IN LIST_ENTRY
*ListHead
243 ENV_VAR_LIST VarList
;
248 InitializeListHead(&VarList
.Link
);
251 // Delete all the current environment variables
253 Status
= GetEnvironmentVariableList(&VarList
.Link
);
254 ASSERT_EFI_ERROR(Status
);
256 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(&VarList
.Link
)
257 ; !IsNull(&VarList
.Link
, &Node
->Link
)
258 ; Node
= (ENV_VAR_LIST
*)GetNextNode(&VarList
.Link
, &Node
->Link
)
260 if (Node
->Key
!= NULL
) {
261 Status
= SHELL_DELETE_ENVIRONMENT_VARIABLE(Node
->Key
);
263 ASSERT_EFI_ERROR(Status
);
266 FreeEnvironmentVariableList(&VarList
.Link
);
269 // set all the variables from the list
271 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(ListHead
)
272 ; !IsNull(ListHead
, &Node
->Link
)
273 ; Node
= (ENV_VAR_LIST
*)GetNextNode(ListHead
, &Node
->Link
)
275 Size
= StrSize (Node
->Val
) - sizeof (CHAR16
);
276 if (Node
->Atts
& EFI_VARIABLE_NON_VOLATILE
) {
277 Status
= SHELL_SET_ENVIRONMENT_VARIABLE_NV(Node
->Key
, Size
, Node
->Val
);
279 Status
= SHELL_SET_ENVIRONMENT_VARIABLE_V (Node
->Key
, Size
, Node
->Val
);
281 ASSERT_EFI_ERROR(Status
);
283 FreeEnvironmentVariableList(ListHead
);
289 sets a list of all Shell-Guid-based environment variables.
291 @param Environment Points to a NULL-terminated array of environment
292 variables with the format 'x=y', where x is the
293 environment variable name and y is the value.
295 @retval EFI_SUCCESS The command executed successfully.
296 @retval EFI_INVALID_PARAMETER The parameter is invalid.
297 @retval EFI_OUT_OF_RESOURCES Out of resources.
299 @sa SetEnvironmentVariableList
302 SetEnvironmentVariables(
303 IN CONST CHAR16
**Environment
306 CONST CHAR16
*CurrentString
;
308 ENV_VAR_LIST
*VarList
;
313 if (Environment
== NULL
) {
314 return (EFI_INVALID_PARAMETER
);
318 // Build a list identical to the ones used for get/set list functions above
320 for ( CurrentCount
= 0
324 CurrentString
= Environment
[CurrentCount
];
325 if (CurrentString
== NULL
) {
328 ASSERT(StrStr(CurrentString
, L
"=") != NULL
);
329 Node
= AllocateZeroPool(sizeof(ENV_VAR_LIST
));
331 SetEnvironmentVariableList(&VarList
->Link
);
332 return (EFI_OUT_OF_RESOURCES
);
335 Node
->Key
= AllocateZeroPool((StrStr(CurrentString
, L
"=") - CurrentString
+ 1) * sizeof(CHAR16
));
336 if (Node
->Key
== NULL
) {
337 SHELL_FREE_NON_NULL(Node
);
338 SetEnvironmentVariableList(&VarList
->Link
);
339 return (EFI_OUT_OF_RESOURCES
);
343 // Copy the string into the Key, leaving the last character allocated as NULL to terminate
346 StrStr(CurrentString
, L
"=") - CurrentString
+ 1,
348 StrStr(CurrentString
, L
"=") - CurrentString
352 // ValueSize = TotalSize - already removed size - size for '=' + size for terminator (the last 2 items cancel each other)
354 Node
->Val
= AllocateCopyPool(StrSize(CurrentString
) - StrSize(Node
->Key
), CurrentString
+ StrLen(Node
->Key
) + 1);
355 if (Node
->Val
== NULL
) {
356 SHELL_FREE_NON_NULL(Node
->Key
);
357 SHELL_FREE_NON_NULL(Node
);
358 SetEnvironmentVariableList(&VarList
->Link
);
359 return (EFI_OUT_OF_RESOURCES
);
362 Node
->Atts
= EFI_VARIABLE_BOOTSERVICE_ACCESS
;
364 if (VarList
== NULL
) {
365 VarList
= AllocateZeroPool(sizeof(ENV_VAR_LIST
));
366 if (VarList
== NULL
) {
367 SHELL_FREE_NON_NULL(Node
->Key
);
368 SHELL_FREE_NON_NULL(Node
->Val
);
369 SHELL_FREE_NON_NULL(Node
);
370 return (EFI_OUT_OF_RESOURCES
);
372 InitializeListHead(&VarList
->Link
);
374 InsertTailList(&VarList
->Link
, &Node
->Link
);
379 // set this new list as the set of all environment variables.
380 // this function also frees the memory and deletes all pre-existing
381 // shell-guid based environment variables.
383 return (SetEnvironmentVariableList(&VarList
->Link
));
387 Find an environment variable in the gShellEnvVarList.
389 @param Key The name of the environment variable.
390 @param Value The value of the environment variable, the buffer
391 shoule be freed by the caller.
392 @param ValueSize The size in bytes of the environment variable
393 including the tailing CHAR_NELL.
394 @param Atts The attributes of the variable.
396 @retval EFI_SUCCESS The command executed successfully.
397 @retval EFI_NOT_FOUND The environment variable is not found in
402 ShellFindEnvVarInList (
403 IN CONST CHAR16
*Key
,
405 OUT UINTN
*ValueSize
,
406 OUT UINT32
*Atts OPTIONAL
411 if (Key
== NULL
|| Value
== NULL
|| ValueSize
== NULL
) {
412 return SHELL_INVALID_PARAMETER
;
415 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(&gShellEnvVarList
.Link
)
416 ; !IsNull(&gShellEnvVarList
.Link
, &Node
->Link
)
417 ; Node
= (ENV_VAR_LIST
*)GetNextNode(&gShellEnvVarList
.Link
, &Node
->Link
)
419 if (Node
->Key
!= NULL
&& StrCmp(Key
, Node
->Key
) == 0) {
420 *Value
= AllocateCopyPool(StrSize(Node
->Val
), Node
->Val
);
421 *ValueSize
= StrSize(Node
->Val
);
429 return EFI_NOT_FOUND
;
433 Add an environment variable into gShellEnvVarList.
435 @param Key The name of the environment variable.
436 @param Value The value of environment variable.
437 @param ValueSize The size in bytes of the environment variable
438 including the tailing CHAR_NULL
439 @param Atts The attributes of the variable.
441 @retval EFI_SUCCESS The environment variable was added to list successfully.
442 @retval others Some errors happened.
446 ShellAddEnvVarToList (
447 IN CONST CHAR16
*Key
,
448 IN CONST CHAR16
*Value
,
457 if (Key
== NULL
|| Value
== NULL
|| ValueSize
== 0) {
458 return EFI_INVALID_PARAMETER
;
461 LocalValue
= AllocateCopyPool (ValueSize
, Value
);
462 if (LocalValue
== NULL
) {
463 return EFI_OUT_OF_RESOURCES
;
467 // Update the variable value if it exists in gShellEnvVarList.
469 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(&gShellEnvVarList
.Link
)
470 ; !IsNull(&gShellEnvVarList
.Link
, &Node
->Link
)
471 ; Node
= (ENV_VAR_LIST
*)GetNextNode(&gShellEnvVarList
.Link
, &Node
->Link
)
473 if (Node
->Key
!= NULL
&& StrCmp(Key
, Node
->Key
) == 0) {
475 SHELL_FREE_NON_NULL(Node
->Val
);
476 Node
->Val
= LocalValue
;
482 // If the environment variable key doesn't exist in list just insert
485 LocalKey
= AllocateCopyPool (StrSize(Key
), Key
);
486 if (LocalKey
== NULL
) {
487 FreePool (LocalValue
);
488 return EFI_OUT_OF_RESOURCES
;
490 Node
= (ENV_VAR_LIST
*)AllocateZeroPool (sizeof(ENV_VAR_LIST
));
493 FreePool (LocalValue
);
494 return EFI_OUT_OF_RESOURCES
;
496 Node
->Key
= LocalKey
;
497 Node
->Val
= LocalValue
;
499 InsertTailList(&gShellEnvVarList
.Link
, &Node
->Link
);
505 Remove a specified environment variable in gShellEnvVarList.
507 @param Key The name of the environment variable.
509 @retval EFI_SUCCESS The command executed successfully.
510 @retval EFI_NOT_FOUND The environment variable is not found in
514 ShellRemvoeEnvVarFromList (
521 return EFI_INVALID_PARAMETER
;
524 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(&gShellEnvVarList
.Link
)
525 ; !IsNull(&gShellEnvVarList
.Link
, &Node
->Link
)
526 ; Node
= (ENV_VAR_LIST
*)GetNextNode(&gShellEnvVarList
.Link
, &Node
->Link
)
528 if (Node
->Key
!= NULL
&& StrCmp(Key
, Node
->Key
) == 0) {
529 SHELL_FREE_NON_NULL(Node
->Key
);
530 SHELL_FREE_NON_NULL(Node
->Val
);
531 RemoveEntryList(&Node
->Link
);
532 SHELL_FREE_NON_NULL(Node
);
537 return EFI_NOT_FOUND
;
541 Initialize the gShellEnvVarList and cache all Shell-Guid-based environment
546 ShellInitEnvVarList (
552 InitializeListHead(&gShellEnvVarList
.Link
);
553 Status
= GetEnvironmentVariableList (&gShellEnvVarList
.Link
);
559 Destructe the gShellEnvVarList.
563 ShellFreeEnvVarList (
567 FreeEnvironmentVariableList (&gShellEnvVarList
.Link
);
568 InitializeListHead(&gShellEnvVarList
.Link
);