X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=ShellPkg%2FLibrary%2FUefiShellLevel2CommandsLib%2FCd.c;h=9ae81763f72fd585769ce1943a200b55d8e56239;hb=4e6394455a8a9741dafe2928373db461ef21671e;hp=78403c22748bae815907aed756d1dd99a02a9047;hpb=a405b86d274d32b92f69842bfb9a1ab143128f57;p=mirror_edk2.git diff --git a/ShellPkg/Library/UefiShellLevel2CommandsLib/Cd.c b/ShellPkg/Library/UefiShellLevel2CommandsLib/Cd.c index 78403c2274..9ae81763f7 100644 --- a/ShellPkg/Library/UefiShellLevel2CommandsLib/Cd.c +++ b/ShellPkg/Library/UefiShellLevel2CommandsLib/Cd.c @@ -1,7 +1,9 @@ /** @file Main file for attrib shell level 2 function. - Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.
+ (C) Copyright 2016 Hewlett Packard Enterprise Development LP
+ (C) Copyright 2015 Hewlett-Packard Development Company, L.P.
+ Copyright (c) 2009 - 2017, 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 @@ -14,6 +16,156 @@ #include "UefiShellLevel2CommandsLib.h" +/** + Function will replace drive identifier with CWD. + + If FullPath begining with ':' is invalid path, then ASSERT. + If FullPath not include dirve identifier , then do nothing. + If FullPath likes "fs0:\xx" or "fs0:/xx" , then do nothing. + If FullPath likes "fs0:xxx" or "fs0:", the drive replaced by CWD. + + @param[in, out] FullPath The pointer to the string containing the path. + @param[in] Cwd Current directory. + + @retval EFI_SUCCESS Success. + @retval EFI_OUT_OF_SOURCES A memory allocation failed. +**/ +EFI_STATUS +ReplaceDriveWithCwd ( + IN OUT CHAR16 **FullPath, + IN CONST CHAR16 *Cwd + ) +{ + CHAR16 *Splitter; + CHAR16 *TempBuffer; + UINTN TotalSize; + + Splitter = NULL; + TempBuffer = NULL; + TotalSize = 0; + + if (FullPath == NULL || *FullPath == NULL) { + return EFI_SUCCESS; + } + + Splitter = StrStr (*FullPath, L":"); + ASSERT(Splitter != *FullPath); + + if (Splitter != NULL && *(Splitter + 1) != L'\\' && *(Splitter + 1) != L'/') { + TotalSize = StrSize (Cwd) + StrSize (Splitter + 1); + TempBuffer = AllocateZeroPool (TotalSize); + if (TempBuffer == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + StrCpyS (TempBuffer, TotalSize / sizeof(CHAR16), Cwd); + StrCatS (TempBuffer, TotalSize / sizeof(CHAR16), L"\\"); + StrCatS (TempBuffer, TotalSize / sizeof(CHAR16), Splitter + 1); + + FreePool(*FullPath); + *FullPath = TempBuffer; + } + + return EFI_SUCCESS; +} + +/** + function to determine if FullPath is under current filesystem. + + @param[in] FullPath The target location to determine. + @param[in] Cwd Current directory. + + @retval TRUE The FullPath is in the current filesystem. + @retval FALSE The FullPaht isn't in the current filesystem. +**/ +BOOLEAN +IsCurrentFileSystem ( + IN CONST CHAR16 *FullPath, + IN CONST CHAR16 *Cwd + ) +{ + CHAR16 *Splitter1; + CHAR16 *Splitter2; + + Splitter1 = NULL; + Splitter2 = NULL; + + ASSERT(FullPath != NULL); + + Splitter1 = StrStr (FullPath, L":"); + if (Splitter1 == NULL) { + return TRUE; + } + + Splitter2 = StrStr (Cwd, L":"); + + if (((UINTN) Splitter1 - (UINTN) FullPath) != ((UINTN) Splitter2 - (UINTN) Cwd)) { + return FALSE; + } else { + if (StrniCmp (FullPath, Cwd, ((UINTN) Splitter1 - (UINTN) FullPath) / sizeof (CHAR16)) == NULL) { + return TRUE; + } else { + return FALSE; + } + } +} + +/** + Extract drive string and path string from FullPath. + + The caller must be free Drive and Path. + + @param[in] FullPath A path to be extracted. + @param[out] Drive Buffer to save drive identifier. + @param[out] Path Buffer to save path. + + @retval EFI_SUCCESS Success. + @retval EFI_OUT_OF_RESOUCES A memory allocation failed. +**/ +EFI_STATUS +ExtractDriveAndPath ( + IN CONST CHAR16 *FullPath, + OUT CHAR16 **Drive, + OUT CHAR16 **Path + ) +{ + CHAR16 *Splitter; + + ASSERT (FullPath != NULL); + + Splitter = StrStr (FullPath, L":"); + + if (Splitter == NULL) { + *Drive = NULL; + *Path = AllocateCopyPool (StrSize (FullPath), FullPath); + if (*Path == NULL) { + return EFI_OUT_OF_RESOURCES; + } + } else { + if (*(Splitter + 1) == CHAR_NULL) { + *Drive = AllocateCopyPool (StrSize (FullPath), FullPath); + *Path = NULL; + if (*Drive == NULL) { + return EFI_OUT_OF_RESOURCES; + } + } else { + *Drive = AllocateCopyPool ((Splitter - FullPath + 2) * sizeof(CHAR16), FullPath); + if (*Drive == NULL) { + return EFI_OUT_OF_RESOURCES; + } + (*Drive)[Splitter - FullPath + 1] = CHAR_NULL; + + *Path = AllocateCopyPool (StrSize (Splitter + 1), Splitter + 1); + if (*Path == NULL) { + FreePool (*Drive); + return EFI_OUT_OF_RESOURCES; + } + } + } + + return EFI_SUCCESS; +} + /** Function for 'cd' command. @@ -29,19 +181,26 @@ ShellCommandRunCd ( { EFI_STATUS Status; LIST_ENTRY *Package; - CONST CHAR16 *Directory; + CONST CHAR16 *Cwd; CHAR16 *Path; CHAR16 *Drive; - UINTN DriveSize; CHAR16 *ProblemParam; SHELL_STATUS ShellStatus; - SHELL_FILE_HANDLE Handle; CONST CHAR16 *Param1; + CHAR16 *Param1Copy; + CHAR16 *Walker; + CHAR16 *Splitter; + CHAR16 *TempBuffer; + UINTN TotalSize; - ProblemParam = NULL; - ShellStatus = SHELL_SUCCESS; - Drive = NULL; - DriveSize = 0; + ProblemParam = NULL; + ShellStatus = SHELL_SUCCESS; + Cwd = NULL; + Path = NULL; + Drive = NULL; + Splitter = NULL; + TempBuffer = NULL; + TotalSize = 0; Status = CommandInit(); ASSERT_EFI_ERROR(Status); @@ -58,7 +217,7 @@ ShellCommandRunCd ( Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE); if (EFI_ERROR(Status)) { if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, ProblemParam); + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"cd", ProblemParam); FreePool(ProblemParam); ShellStatus = SHELL_INVALID_PARAMETER; } else { @@ -72,7 +231,7 @@ ShellCommandRunCd ( if (ShellCommandLineGetFlag(Package, L"-?")) { ASSERT(FALSE); } else if (ShellCommandLineGetRawValue(Package, 2) != NULL) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle); + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle, L"cd"); ShellStatus = SHELL_INVALID_PARAMETER; } else { // @@ -81,130 +240,104 @@ ShellCommandRunCd ( // else If there are 2 value parameters, then print the error message // else If there is 1 value paramerer , then change the directory // - Param1 = ShellCommandLineGetRawValue(Package, 1); - if (Param1 == NULL) { - // - // display the current directory - // - Directory = ShellGetCurrentDir(NULL); - if (Directory != NULL) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_PRINT), gShellLevel2HiiHandle, Directory); - } else { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle); - ShellStatus = SHELL_NOT_FOUND; - } + Cwd = ShellGetCurrentDir (NULL); + if (Cwd == NULL) { + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN(STR_GEN_NO_CWD), gShellLevel2HiiHandle, L"cd"); + ShellStatus = SHELL_NOT_FOUND; } else { - if (StrCmp(Param1, L".") == 0) { - // - // nothing to do... change to current directory + Param1 = ShellCommandLineGetRawValue (Package, 1); + if (Param1 == NULL) { // - } else if (StrCmp(Param1, L"..") == 0) { + // display the current directory // - // Change up one directory... - // - Directory = ShellGetCurrentDir(NULL); - if (Directory == NULL) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle); - ShellStatus = SHELL_NOT_FOUND; - } else { - Drive = GetFullyQualifiedPath(Directory); - ChopLastSlash(Drive); - } - if (ShellStatus == SHELL_SUCCESS && Drive != NULL) { - // - // change directory on current drive letter - // - Status = gEfiShellProtocol->SetCurDir(NULL, Drive); - if (Status == EFI_NOT_FOUND) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle); - ShellStatus = SHELL_NOT_FOUND; + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN(STR_CD_PRINT), gShellLevel2HiiHandle, Cwd); + } else { + Param1Copy = CatSPrint (NULL, L"%s", Param1, NULL); + for (Walker = Param1Copy; Walker != NULL && *Walker != CHAR_NULL; Walker++) { + if (*Walker == L'\"') { + CopyMem (Walker, Walker + 1, StrSize(Walker) - sizeof(Walker[0])); } } - } else if (StrCmp(Param1, L"\\") == 0) { - // - // Move to root of current drive - // - Directory = ShellGetCurrentDir(NULL); - if (Directory == NULL) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle); - ShellStatus = SHELL_NOT_FOUND; + + if (Param1Copy != NULL && IsCurrentFileSystem (Param1Copy, Cwd)) { + Status = ReplaceDriveWithCwd (&Param1Copy,Cwd); + if (!EFI_ERROR (Status)) { + Param1Copy = PathCleanUpDirectories (Param1Copy); + } } else { - Drive = GetFullyQualifiedPath(Directory); - while (ChopLastSlash(Drive)) ; - } - if (ShellStatus == SHELL_SUCCESS && Drive != NULL) { // - // change directory on current drive letter + // Can't use cd command to change filesystem. // - Status = gEfiShellProtocol->SetCurDir(NULL, Drive); - if (Status == EFI_NOT_FOUND) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle); - ShellStatus = SHELL_NOT_FOUND; - } + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle, L"cd"); + Status = EFI_NOT_FOUND; } - } else if (StrStr(Param1, L":") == NULL) { - if (ShellGetCurrentDir(NULL) == NULL) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle); - ShellStatus = SHELL_NOT_FOUND; - } else { - ASSERT((Drive == NULL && DriveSize == 0) || (Drive != NULL)); - Drive = StrnCatGrow(&Drive, &DriveSize, ShellGetCurrentDir(NULL), 0); - if (*Param1 == L'\\') { - while (ChopLastSlash(Drive)) ; - Drive = StrnCatGrow(&Drive, &DriveSize, Param1+1, 0); + + if (!EFI_ERROR(Status) && Param1Copy != NULL) { + Splitter = StrStr (Cwd, L":"); + if (Param1Copy[0] == L'\\') { + // + // Absolute Path on current drive letter. + // + TotalSize = ((Splitter - Cwd + 1) * sizeof(CHAR16)) + StrSize(Param1Copy); + TempBuffer = AllocateZeroPool (TotalSize); + if (TempBuffer == NULL) { + Status = EFI_OUT_OF_RESOURCES; + } else { + StrnCpyS (TempBuffer, TotalSize / sizeof(CHAR16), Cwd, (Splitter - Cwd + 1)); + StrCatS (TempBuffer, TotalSize / sizeof(CHAR16), Param1Copy); + + FreePool (Param1Copy); + Param1Copy = TempBuffer; + TempBuffer = NULL; + } } else { - Drive = StrnCatGrow(&Drive, &DriveSize, Param1, 0); + if (StrStr (Param1Copy,L":") == NULL) { + TotalSize = StrSize (Cwd) + StrSize (Param1Copy); + TempBuffer = AllocateZeroPool (TotalSize); + if (TempBuffer == NULL) { + Status = EFI_OUT_OF_RESOURCES; + } else { + StrCpyS (TempBuffer, TotalSize / sizeof (CHAR16), Cwd); + StrCatS (TempBuffer, TotalSize / sizeof (CHAR16), L"\\"); + StrCatS (TempBuffer, TotalSize / sizeof (CHAR16), Param1Copy); + + FreePool (Param1Copy); + Param1Copy = PathCleanUpDirectories (TempBuffer); + } + } } - // - // Verify that this is a valid directory - // - Status = gEfiShellProtocol->OpenFileByName(Drive, &Handle, EFI_FILE_MODE_READ); - if (EFI_ERROR(Status)) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, Drive); - ShellStatus = SHELL_NOT_FOUND; - } else if (EFI_ERROR(FileHandleIsDirectory(Handle))) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, Drive); + } + + if (!EFI_ERROR(Status)) { + Status = ExtractDriveAndPath (Param1Copy, &Drive, &Path); + } + + if (!EFI_ERROR (Status) && Drive != NULL && Path != NULL) { + if (EFI_ERROR(ShellIsDirectory (Param1Copy))) { + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN(STR_GEN_NOT_DIR), gShellLevel2HiiHandle, L"cd", Param1Copy); ShellStatus = SHELL_NOT_FOUND; - } - if (ShellStatus == SHELL_SUCCESS && Drive != NULL) { - // - // change directory on current drive letter - // - Status = gEfiShellProtocol->SetCurDir(NULL, Drive); - if (Status == EFI_NOT_FOUND) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle); + } else { + Status = gEfiShellProtocol->SetCurDir (Drive, Path + 1); + if (EFI_ERROR (Status)) { + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN(STR_GEN_DIR_NF), gShellLevel2HiiHandle, L"cd", Param1Copy); ShellStatus = SHELL_NOT_FOUND; } } - if (Handle != NULL) { - gEfiShellProtocol->CloseFile(Handle); - DEBUG_CODE(Handle = NULL;); - } } - } else { - // - // change directory on other drive letter - // - Drive = AllocateZeroPool(StrSize(Param1)); - Drive = StrCpy(Drive, Param1); - Path = StrStr(Drive, L":"); - *(++Path) = CHAR_NULL; - Status = gEfiShellProtocol->SetCurDir(Drive, ++Path); - - if (Status == EFI_NOT_FOUND) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle); - Status = SHELL_NOT_FOUND; - } else if (EFI_ERROR(Status)) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, Param1); - Status = SHELL_NOT_FOUND; + + if (Drive != NULL) { + FreePool (Drive); + } + + if (Path != NULL) { + FreePool (Path); } + + FreePool (Param1Copy); } } } - if (Drive != NULL) { - FreePool(Drive); - } // // free the command line package //