X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=ShellPkg%2FApplication%2FShell%2FShellEnvVar.c;h=5eb382a5866f551e511848c5f892d43dd682547f;hb=b62bb8854fc5ab36b9b88f8bee9a276558bbcd96;hp=a4bd6a61797c5a9bee45035be2af3d44ea01bcb0;hpb=4ff7e37b4f7e336a8ecb7080b8f48eef4b52d396;p=mirror_edk2.git diff --git a/ShellPkg/Application/Shell/ShellEnvVar.c b/ShellPkg/Application/Shell/ShellEnvVar.c index a4bd6a6179..5eb382a586 100644 --- a/ShellPkg/Application/Shell/ShellEnvVar.c +++ b/ShellPkg/Application/Shell/ShellEnvVar.c @@ -1,7 +1,7 @@ /** @file function declarations for shell environment functions. - Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.
+ Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -12,18 +12,15 @@ **/ -#include -#include +#include "Shell.h" -#include +#define INIT_NAME_BUFFER_SIZE 128 +#define INIT_DATA_BUFFER_SIZE 1024 -#include -#include -#include -#include -#include - -#include "ShellEnvVar.h" +// +// The list is used to cache the environment variables. +// +ENV_VAR_LIST gShellEnvVarList; /** Reports whether an environment variable is Volatile or Non-Volatile. @@ -105,7 +102,7 @@ FreeEnvironmentVariableList( } for ( Node = (ENV_VAR_LIST*)GetFirstNode(List) - ; IsListEmpty(List) + ; !IsListEmpty(List) ; Node = (ENV_VAR_LIST*)GetFirstNode(List) ){ ASSERT(Node != NULL); @@ -136,73 +133,88 @@ GetEnvironmentVariableList( { CHAR16 *VariableName; UINTN NameSize; - UINT64 MaxStorSize; - UINT64 RemStorSize; - UINT64 MaxVarSize; + UINTN NameBufferSize; EFI_STATUS Status; EFI_GUID Guid; UINTN ValSize; + UINTN ValBufferSize; ENV_VAR_LIST *VarList; if (ListHead == NULL) { return (EFI_INVALID_PARAMETER); } - - if (gRT->Hdr.Revision >= EFI_2_00_SYSTEM_TABLE_REVISION) { - Status = gRT->QueryVariableInfo(EFI_VARIABLE_NON_VOLATILE|EFI_VARIABLE_BOOTSERVICE_ACCESS, &MaxStorSize, &RemStorSize, &MaxVarSize); - if (EFI_ERROR(Status)) { - return (Status); - } - } else { - Status = EFI_SUCCESS; - MaxVarSize = 16384; - } - - NameSize = (UINTN)MaxVarSize; - VariableName = AllocateZeroPool(NameSize); + + Status = EFI_SUCCESS; + + ValBufferSize = INIT_DATA_BUFFER_SIZE; + NameBufferSize = INIT_NAME_BUFFER_SIZE; + VariableName = AllocateZeroPool(NameBufferSize); if (VariableName == NULL) { return (EFI_OUT_OF_RESOURCES); } - StrCpy(VariableName, L""); + *VariableName = CHAR_NULL; while (!EFI_ERROR(Status)) { - NameSize = (UINTN)MaxVarSize; + NameSize = NameBufferSize; Status = gRT->GetNextVariableName(&NameSize, VariableName, &Guid); if (Status == EFI_NOT_FOUND){ Status = EFI_SUCCESS; break; + } else if (Status == EFI_BUFFER_TOO_SMALL) { + NameBufferSize = NameSize > NameBufferSize * 2 ? NameSize : NameBufferSize * 2; + SHELL_FREE_NON_NULL(VariableName); + VariableName = AllocateZeroPool(NameBufferSize); + if (VariableName == NULL) { + Status = EFI_OUT_OF_RESOURCES; + break; + } + NameSize = NameBufferSize; + Status = gRT->GetNextVariableName(&NameSize, VariableName, &Guid); } + if (!EFI_ERROR(Status) && CompareGuid(&Guid, &gShellVariableGuid)){ VarList = AllocateZeroPool(sizeof(ENV_VAR_LIST)); if (VarList == NULL) { Status = EFI_OUT_OF_RESOURCES; } else { - ValSize = 0; + ValSize = ValBufferSize; + VarList->Val = AllocateZeroPool(ValSize); + if (VarList->Val == NULL) { + SHELL_FREE_NON_NULL(VarList); + Status = EFI_OUT_OF_RESOURCES; + break; + } Status = SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(VariableName, &VarList->Atts, &ValSize, VarList->Val); if (Status == EFI_BUFFER_TOO_SMALL){ - VarList->Val = AllocateZeroPool(ValSize); + ValBufferSize = ValSize > ValBufferSize * 2 ? ValSize : ValBufferSize * 2; + SHELL_FREE_NON_NULL (VarList->Val); + VarList->Val = AllocateZeroPool(ValBufferSize); if (VarList->Val == NULL) { SHELL_FREE_NON_NULL(VarList); Status = EFI_OUT_OF_RESOURCES; - } else { - Status = SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(VariableName, &VarList->Atts, &ValSize, VarList->Val); + break; } + + ValSize = ValBufferSize; + Status = SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(VariableName, &VarList->Atts, &ValSize, VarList->Val); } - if (!EFI_ERROR(Status) && VarList != NULL) { - VarList->Key = AllocateZeroPool(StrSize(VariableName)); + if (!EFI_ERROR(Status)) { + VarList->Key = AllocateCopyPool(StrSize(VariableName), VariableName); if (VarList->Key == NULL) { SHELL_FREE_NON_NULL(VarList->Val); SHELL_FREE_NON_NULL(VarList); Status = EFI_OUT_OF_RESOURCES; } else { - StrCpy(VarList->Key, VariableName); InsertTailList(ListHead, &VarList->Link); } + } else { + SHELL_FREE_NON_NULL(VarList->Val); + SHELL_FREE_NON_NULL(VarList); } - } + } // if (VarList == NULL) ... else ... } // compare guid } // while - FreePool(VariableName); + SHELL_FREE_NON_NULL (VariableName); if (EFI_ERROR(Status)) { FreeEnvironmentVariableList(ListHead); @@ -297,7 +309,6 @@ SetEnvironmentVariables( UINTN CurrentCount; ENV_VAR_LIST *VarList; ENV_VAR_LIST *Node; - UINTN NewSize; VarList = NULL; @@ -318,20 +329,48 @@ SetEnvironmentVariables( } ASSERT(StrStr(CurrentString, L"=") != NULL); Node = AllocateZeroPool(sizeof(ENV_VAR_LIST)); - ASSERT(Node != NULL); + if (Node == NULL) { + SetEnvironmentVariableList(&VarList->Link); + return (EFI_OUT_OF_RESOURCES); + } + Node->Key = AllocateZeroPool((StrStr(CurrentString, L"=") - CurrentString + 1) * sizeof(CHAR16)); - ASSERT(Node->Key != NULL); - StrnCpy(Node->Key, CurrentString, StrStr(CurrentString, L"=") - CurrentString); - NewSize = StrSize(CurrentString); - NewSize -= StrLen(Node->Key) - 1; - Node->Val = AllocateZeroPool(NewSize); - ASSERT(Node->Val != NULL); - StrCpy(Node->Val, CurrentString + StrLen(Node->Key) + 1); + if (Node->Key == NULL) { + SHELL_FREE_NON_NULL(Node); + SetEnvironmentVariableList(&VarList->Link); + return (EFI_OUT_OF_RESOURCES); + } + + // + // Copy the string into the Key, leaving the last character allocated as NULL to terminate + // + StrnCpyS( Node->Key, + StrStr(CurrentString, L"=") - CurrentString + 1, + CurrentString, + StrStr(CurrentString, L"=") - CurrentString + ); + + // + // ValueSize = TotalSize - already removed size - size for '=' + size for terminator (the last 2 items cancel each other) + // + Node->Val = AllocateCopyPool(StrSize(CurrentString) - StrSize(Node->Key), CurrentString + StrLen(Node->Key) + 1); + if (Node->Val == NULL) { + SHELL_FREE_NON_NULL(Node->Key); + SHELL_FREE_NON_NULL(Node); + SetEnvironmentVariableList(&VarList->Link); + return (EFI_OUT_OF_RESOURCES); + } + Node->Atts = EFI_VARIABLE_BOOTSERVICE_ACCESS; if (VarList == NULL) { VarList = AllocateZeroPool(sizeof(ENV_VAR_LIST)); - ASSERT(VarList != NULL); + if (VarList == NULL) { + SHELL_FREE_NON_NULL(Node->Key); + SHELL_FREE_NON_NULL(Node->Val); + SHELL_FREE_NON_NULL(Node); + return (EFI_OUT_OF_RESOURCES); + } InitializeListHead(&VarList->Link); } InsertTailList(&VarList->Link, &Node->Link); @@ -345,3 +384,176 @@ SetEnvironmentVariables( // return (SetEnvironmentVariableList(&VarList->Link)); } + +/** + Find an environment variable in the gShellEnvVarList. + + @param Key The name of the environment variable. + @param Value The value of the environment variable, the buffer + shoule be freed by the caller. + @param ValueSize The size in bytes of the environment variable + including the tailing CHAR_NELL. + @param Atts The attributes of the variable. + + @retval EFI_SUCCESS The command executed successfully. + @retval EFI_NOT_FOUND The environment variable is not found in + gShellEnvVarList. + +**/ +EFI_STATUS +ShellFindEnvVarInList ( + IN CONST CHAR16 *Key, + OUT CHAR16 **Value, + OUT UINTN *ValueSize, + OUT UINT32 *Atts OPTIONAL + ) +{ + ENV_VAR_LIST *Node; + + if (Key == NULL || Value == NULL || ValueSize == NULL) { + return SHELL_INVALID_PARAMETER; + } + + for ( Node = (ENV_VAR_LIST*)GetFirstNode(&gShellEnvVarList.Link) + ; !IsNull(&gShellEnvVarList.Link, &Node->Link) + ; Node = (ENV_VAR_LIST*)GetNextNode(&gShellEnvVarList.Link, &Node->Link) + ){ + if (Node->Key != NULL && StrCmp(Key, Node->Key) == 0) { + *Value = AllocateCopyPool(StrSize(Node->Val), Node->Val); + *ValueSize = StrSize(Node->Val); + if (Atts != NULL) { + *Atts = Node->Atts; + } + return EFI_SUCCESS; + } + } + + return EFI_NOT_FOUND; +} + +/** + Add an environment variable into gShellEnvVarList. + + @param Key The name of the environment variable. + @param Value The value of environment variable. + @param ValueSize The size in bytes of the environment variable + including the tailing CHAR_NULL + @param Atts The attributes of the variable. + +**/ +VOID +ShellAddEnvVarToList ( + IN CONST CHAR16 *Key, + IN CONST CHAR16 *Value, + IN UINTN ValueSize, + IN UINT32 Atts + ) +{ + ENV_VAR_LIST *Node; + + if (Key == NULL || Value == NULL || ValueSize == 0) { + return; + } + + // + // Update the variable value if it exists in gShellEnvVarList. + // + for ( Node = (ENV_VAR_LIST*)GetFirstNode(&gShellEnvVarList.Link) + ; !IsNull(&gShellEnvVarList.Link, &Node->Link) + ; Node = (ENV_VAR_LIST*)GetNextNode(&gShellEnvVarList.Link, &Node->Link) + ){ + if (Node->Key != NULL && StrCmp(Key, Node->Key) == 0) { + Node->Atts = Atts; + SHELL_FREE_NON_NULL(Node->Val); + Node->Val = AllocateZeroPool (ValueSize); + ASSERT (Node->Val != NULL); + CopyMem(Node->Val, Value, ValueSize); + return; + } + } + + // + // If the environment varialbe key doesn't exist in list just insert + // a new node. + // + Node = (ENV_VAR_LIST*)AllocateZeroPool (sizeof(ENV_VAR_LIST)); + ASSERT (Node != NULL); + Node->Key = AllocateCopyPool(StrSize(Key), Key); + ASSERT (Node->Key != NULL); + Node->Val = AllocateCopyPool(ValueSize, Value); + ASSERT (Node->Val != NULL); + Node->Atts = Atts; + InsertTailList(&gShellEnvVarList.Link, &Node->Link); + + return; +} + +/** + Remove a specified environment variable in gShellEnvVarList. + + @param Key The name of the environment variable. + + @retval EFI_SUCCESS The command executed successfully. + @retval EFI_NOT_FOUND The environment variable is not found in + gShellEnvVarList. +**/ +EFI_STATUS +ShellRemvoeEnvVarFromList ( + IN CONST CHAR16 *Key + ) +{ + ENV_VAR_LIST *Node; + + if (Key == NULL) { + return EFI_INVALID_PARAMETER; + } + + for ( Node = (ENV_VAR_LIST*)GetFirstNode(&gShellEnvVarList.Link) + ; !IsNull(&gShellEnvVarList.Link, &Node->Link) + ; Node = (ENV_VAR_LIST*)GetNextNode(&gShellEnvVarList.Link, &Node->Link) + ){ + if (Node->Key != NULL && StrCmp(Key, Node->Key) == 0) { + SHELL_FREE_NON_NULL(Node->Key); + SHELL_FREE_NON_NULL(Node->Val); + RemoveEntryList(&Node->Link); + SHELL_FREE_NON_NULL(Node); + return EFI_SUCCESS; + } + } + + return EFI_NOT_FOUND; +} + +/** + Initialize the gShellEnvVarList and cache all Shell-Guid-based environment + variables. + +**/ +EFI_STATUS +ShellInitEnvVarList ( + VOID + ) +{ + EFI_STATUS Status; + + InitializeListHead(&gShellEnvVarList.Link); + Status = GetEnvironmentVariableList (&gShellEnvVarList.Link); + + return Status; +} + +/** + Destructe the gShellEnvVarList. + +**/ +VOID +ShellFreeEnvVarList ( + VOID + ) +{ + FreeEnvironmentVariableList (&gShellEnvVarList.Link); + InitializeListHead(&gShellEnvVarList.Link); + + return; +} +