2 function declarations for shell environment functions.
4 Copyright (c) 2009 - 2015, 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 Reports whether an environment variable is Volatile or Non-Volatile.
23 @param EnvVarName The name of the environment variable in question
25 @retval TRUE This environment variable is Volatile
26 @retval FALSE This environment variable is NON-Volatile
31 IN CONST CHAR16
*EnvVarName
45 Status
= gRT
->GetVariable((CHAR16
*)EnvVarName
,
50 if (Status
== EFI_BUFFER_TOO_SMALL
) {
51 Buffer
= AllocateZeroPool(Size
);
52 ASSERT(Buffer
!= NULL
);
53 Status
= gRT
->GetVariable((CHAR16
*)EnvVarName
,
61 // not found means volatile
63 if (Status
== EFI_NOT_FOUND
) {
66 ASSERT_EFI_ERROR(Status
);
69 // check for the Non Volatile bit
71 if ((Attribs
& EFI_VARIABLE_NON_VOLATILE
) == EFI_VARIABLE_NON_VOLATILE
) {
76 // everything else is volatile
82 free function for ENV_VAR_LIST objects.
84 @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 sucessfully.
125 GetEnvironmentVariableList(
126 IN OUT LIST_ENTRY
*ListHead
129 CHAR16
*VariableName
;
131 UINTN NameBufferSize
;
136 ENV_VAR_LIST
*VarList
;
138 if (ListHead
== NULL
) {
139 return (EFI_INVALID_PARAMETER
);
142 Status
= EFI_SUCCESS
;
144 ValBufferSize
= INIT_DATA_BUFFER_SIZE
;
145 NameBufferSize
= INIT_NAME_BUFFER_SIZE
;
146 VariableName
= AllocateZeroPool(NameBufferSize
);
147 if (VariableName
== NULL
) {
148 return (EFI_OUT_OF_RESOURCES
);
150 *VariableName
= CHAR_NULL
;
152 while (!EFI_ERROR(Status
)) {
153 NameSize
= NameBufferSize
;
154 Status
= gRT
->GetNextVariableName(&NameSize
, VariableName
, &Guid
);
155 if (Status
== EFI_NOT_FOUND
){
156 Status
= EFI_SUCCESS
;
158 } else if (Status
== EFI_BUFFER_TOO_SMALL
) {
159 NameBufferSize
= NameSize
> NameBufferSize
* 2 ? NameSize
: NameBufferSize
* 2;
160 SHELL_FREE_NON_NULL(VariableName
);
161 VariableName
= AllocateZeroPool(NameBufferSize
);
162 if (VariableName
== NULL
) {
163 Status
= EFI_OUT_OF_RESOURCES
;
166 NameSize
= NameBufferSize
;
167 Status
= gRT
->GetNextVariableName(&NameSize
, VariableName
, &Guid
);
170 if (!EFI_ERROR(Status
) && CompareGuid(&Guid
, &gShellVariableGuid
)){
171 VarList
= AllocateZeroPool(sizeof(ENV_VAR_LIST
));
172 if (VarList
== NULL
) {
173 Status
= EFI_OUT_OF_RESOURCES
;
175 ValSize
= ValBufferSize
;
176 VarList
->Val
= AllocateZeroPool(ValSize
);
177 if (VarList
->Val
== NULL
) {
178 SHELL_FREE_NON_NULL(VarList
);
179 Status
= EFI_OUT_OF_RESOURCES
;
182 Status
= SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(VariableName
, &VarList
->Atts
, &ValSize
, VarList
->Val
);
183 if (Status
== EFI_BUFFER_TOO_SMALL
){
184 ValBufferSize
= ValSize
> ValBufferSize
* 2 ? ValSize
: ValBufferSize
* 2;
185 SHELL_FREE_NON_NULL (VarList
->Val
);
186 VarList
->Val
= AllocateZeroPool(ValBufferSize
);
187 if (VarList
->Val
== NULL
) {
188 SHELL_FREE_NON_NULL(VarList
);
189 Status
= EFI_OUT_OF_RESOURCES
;
193 ValSize
= ValBufferSize
;
194 Status
= SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(VariableName
, &VarList
->Atts
, &ValSize
, VarList
->Val
);
196 if (!EFI_ERROR(Status
)) {
197 VarList
->Key
= AllocateCopyPool(StrSize(VariableName
), VariableName
);
198 if (VarList
->Key
== NULL
) {
199 SHELL_FREE_NON_NULL(VarList
->Val
);
200 SHELL_FREE_NON_NULL(VarList
);
201 Status
= EFI_OUT_OF_RESOURCES
;
203 InsertTailList(ListHead
, &VarList
->Link
);
206 SHELL_FREE_NON_NULL(VarList
->Val
);
207 SHELL_FREE_NON_NULL(VarList
);
209 } // if (VarList == NULL) ... else ...
212 SHELL_FREE_NON_NULL (VariableName
);
214 if (EFI_ERROR(Status
)) {
215 FreeEnvironmentVariableList(ListHead
);
222 Sets a list of all Shell-Guid-based environment variables. this will
223 also eliminate all existing shell environment variables (even if they
224 are not on the list).
226 This function will also deallocate the memory from List.
228 @param[in] ListHead The pointer to LIST_ENTRY from
229 GetShellEnvVarList().
231 @retval EFI_SUCCESS the list was Set sucessfully.
235 SetEnvironmentVariableList(
236 IN LIST_ENTRY
*ListHead
239 ENV_VAR_LIST VarList
;
244 InitializeListHead(&VarList
.Link
);
247 // Delete all the current environment variables
249 Status
= GetEnvironmentVariableList(&VarList
.Link
);
250 ASSERT_EFI_ERROR(Status
);
252 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(&VarList
.Link
)
253 ; !IsNull(&VarList
.Link
, &Node
->Link
)
254 ; Node
= (ENV_VAR_LIST
*)GetNextNode(&VarList
.Link
, &Node
->Link
)
256 if (Node
->Key
!= NULL
) {
257 Status
= SHELL_DELETE_ENVIRONMENT_VARIABLE(Node
->Key
);
259 ASSERT_EFI_ERROR(Status
);
262 FreeEnvironmentVariableList(&VarList
.Link
);
265 // set all the variables fron the list
267 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(ListHead
)
268 ; !IsNull(ListHead
, &Node
->Link
)
269 ; Node
= (ENV_VAR_LIST
*)GetNextNode(ListHead
, &Node
->Link
)
271 Size
= StrSize(Node
->Val
);
272 if (Node
->Atts
& EFI_VARIABLE_NON_VOLATILE
) {
273 Status
= SHELL_SET_ENVIRONMENT_VARIABLE_NV(Node
->Key
, Size
, Node
->Val
);
275 Status
= SHELL_SET_ENVIRONMENT_VARIABLE_V (Node
->Key
, Size
, Node
->Val
);
277 ASSERT_EFI_ERROR(Status
);
279 FreeEnvironmentVariableList(ListHead
);
285 sets a list of all Shell-Guid-based environment variables.
287 @param Environment Points to a NULL-terminated array of environment
288 variables with the format 'x=y', where x is the
289 environment variable name and y is the value.
291 @retval EFI_SUCCESS The command executed successfully.
292 @retval EFI_INVALID_PARAMETER The parameter is invalid.
293 @retval EFI_OUT_OF_RESOURCES Out of resources.
295 @sa SetEnvironmentVariableList
299 SetEnvironmentVariables(
300 IN CONST CHAR16
**Environment
303 CONST CHAR16
*CurrentString
;
305 ENV_VAR_LIST
*VarList
;
310 if (Environment
== NULL
) {
311 return (EFI_INVALID_PARAMETER
);
315 // Build a list identical to the ones used for get/set list functions above
317 for ( CurrentCount
= 0
321 CurrentString
= Environment
[CurrentCount
];
322 if (CurrentString
== NULL
) {
325 ASSERT(StrStr(CurrentString
, L
"=") != NULL
);
326 Node
= AllocateZeroPool(sizeof(ENV_VAR_LIST
));
328 SetEnvironmentVariableList(&VarList
->Link
);
329 return (EFI_OUT_OF_RESOURCES
);
332 Node
->Key
= AllocateZeroPool((StrStr(CurrentString
, L
"=") - CurrentString
+ 1) * sizeof(CHAR16
));
333 if (Node
->Key
== NULL
) {
334 SHELL_FREE_NON_NULL(Node
);
335 SetEnvironmentVariableList(&VarList
->Link
);
336 return (EFI_OUT_OF_RESOURCES
);
340 // Copy the string into the Key, leaving the last character allocated as NULL to terminate
343 StrStr(CurrentString
, L
"=") - CurrentString
+ 1,
348 // ValueSize = TotalSize - already removed size - size for '=' + size for terminator (the last 2 items cancel each other)
350 Node
->Val
= AllocateCopyPool(StrSize(CurrentString
) - StrSize(Node
->Key
), CurrentString
+ StrLen(Node
->Key
) + 1);
351 if (Node
->Val
== NULL
) {
352 SHELL_FREE_NON_NULL(Node
->Key
);
353 SHELL_FREE_NON_NULL(Node
);
354 SetEnvironmentVariableList(&VarList
->Link
);
355 return (EFI_OUT_OF_RESOURCES
);
358 Node
->Atts
= EFI_VARIABLE_BOOTSERVICE_ACCESS
;
360 if (VarList
== NULL
) {
361 VarList
= AllocateZeroPool(sizeof(ENV_VAR_LIST
));
362 if (VarList
== NULL
) {
363 SHELL_FREE_NON_NULL(Node
->Key
);
364 SHELL_FREE_NON_NULL(Node
->Val
);
365 SHELL_FREE_NON_NULL(Node
);
366 return (EFI_OUT_OF_RESOURCES
);
368 InitializeListHead(&VarList
->Link
);
370 InsertTailList(&VarList
->Link
, &Node
->Link
);
375 // set this new list as the set of all environment variables.
376 // this function also frees the memory and deletes all pre-existing
377 // shell-guid based environment variables.
379 return (SetEnvironmentVariableList(&VarList
->Link
));