From 416a423f0815b2ad2bd8dc1ceae7d306253462de Mon Sep 17 00:00:00 2001 From: Chris Phillips Date: Fri, 22 Nov 2013 21:15:19 +0000 Subject: [PATCH] ShellPkg: Remove trailing \r\n when redirect to env variable (EX: use ">v") Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Chris Phillips Reviewed-by: Jaben Carsey git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@14891 6f19259b-4bc3-4df7-8a09-765794883524 --- .../Application/Shell/FileHandleWrappers.c | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/ShellPkg/Application/Shell/FileHandleWrappers.c b/ShellPkg/Application/Shell/FileHandleWrappers.c index 2ca13cb3f3..ef8293c1de 100644 --- a/ShellPkg/Application/Shell/FileHandleWrappers.c +++ b/ShellPkg/Application/Shell/FileHandleWrappers.c @@ -3,6 +3,7 @@ StdIn, StdOut, StdErr, etc...). Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.
+ Copyright (c) 2013, Hewlett-Packard Development Company, L.P. 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 @@ -950,8 +951,48 @@ FileInterfaceEnvClose( IN EFI_FILE_PROTOCOL *This ) { + VOID* NewBuffer; + UINTN NewSize; + EFI_STATUS Status; + + // + // Most if not all UEFI commands will have an '\r\n' at the end of any output. + // Since the output was redirected to a variable, it does not make sense to + // keep this. So, before closing, strip the trailing '\r\n' from the variable + // if it exists. + // + NewBuffer = NULL; + NewSize = 0; + + Status = SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, &NewSize, NewBuffer); + if (Status == EFI_BUFFER_TOO_SMALL) { + NewBuffer = AllocateZeroPool(NewSize + sizeof(CHAR16)); + if (NewBuffer == NULL) { + return EFI_OUT_OF_RESOURCES; + } + Status = SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, &NewSize, NewBuffer); + } + + if (!EFI_ERROR(Status)) { + + if (StrSize(NewBuffer) > 6) + { + if ((((CHAR16*)NewBuffer)[(StrSize(NewBuffer)/2) - 2] == CHAR_LINEFEED) + && (((CHAR16*)NewBuffer)[(StrSize(NewBuffer)/2) - 3] == CHAR_CARRIAGE_RETURN)) { + ((CHAR16*)NewBuffer)[(StrSize(NewBuffer)/2) - 3] = CHAR_NULL; + } + + if (IsVolatileEnv(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name)) { + Status = SHELL_SET_ENVIRONMENT_VARIABLE_V(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, StrSize(NewBuffer), NewBuffer); + } else { + Status = SHELL_SET_ENVIRONMENT_VARIABLE_NV(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, StrSize(NewBuffer), NewBuffer); + } + } + } + + SHELL_FREE_NON_NULL(NewBuffer); FreePool((EFI_FILE_PROTOCOL_ENVIRONMENT*)This); - return (EFI_SUCCESS); + return (Status); } /** -- 2.39.2