]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellLib/UefiShellLib.c
EmbeddedPkg/PrePiLib: Correct function name
[mirror_edk2.git] / ShellPkg / Library / UefiShellLib / UefiShellLib.c
index 3c24ba1742bf1362f51f0adc643ff6b8391400ea..f3d6e42b2082f83c8d8eec1f0a3951410a74f0a2 100644 (file)
@@ -2,8 +2,8 @@
   Provides interface to shell functionality for shell commands and applications.\r
 \r
   (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
-  Copyright 2016 Dell Inc.\r
-  Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+  Copyright 2016-2018 Dell Technologies.<BR>\r
+  Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
@@ -36,6 +36,125 @@ EFI_HANDLE                    mEfiShellEnvironment2Handle;
 FILE_HANDLE_FUNCTION_MAP      FileFunctionMap;\r
 EFI_UNICODE_COLLATION_PROTOCOL  *mUnicodeCollationProtocol;\r
 \r
+/**\r
+  Return a clean, fully-qualified version of an input path.  If the return value\r
+  is non-NULL the caller must free the memory when it is no longer needed.\r
+\r
+  If asserts are disabled, and if the input parameter is NULL, NULL is returned.\r
+\r
+  If there is not enough memory available to create the fully-qualified path or\r
+  a copy of the input path, NULL is returned.\r
+\r
+  If there is no working directory, a clean copy of Path is returned.\r
+\r
+  Otherwise, the current file system or working directory (as appropriate) is\r
+  prepended to Path and the resulting path is cleaned and returned.\r
+\r
+  NOTE: If the input path is an empty string, then the current working directory\r
+  (if it exists) is returned.  In other words, an empty input path is treated\r
+  exactly the same as ".".\r
+\r
+  @param[in] Path  A pointer to some file or directory path.\r
+\r
+  @retval NULL          The input path is NULL or out of memory.\r
+\r
+  @retval non-NULL      A pointer to a clean, fully-qualified version of Path.\r
+                        If there is no working directory, then a pointer to a\r
+                        clean, but not necessarily fully-qualified version of\r
+                        Path.  The caller must free this memory when it is no\r
+                        longer needed.\r
+**/\r
+CHAR16*\r
+EFIAPI\r
+FullyQualifyPath(\r
+  IN     CONST CHAR16     *Path\r
+  )\r
+{\r
+  CONST CHAR16         *WorkingPath;\r
+  CONST CHAR16         *InputPath;\r
+  CHAR16               *CharPtr;\r
+  CHAR16               *InputFileSystem;\r
+  UINTN                FileSystemCharCount;\r
+  CHAR16               *FullyQualifiedPath;\r
+  UINTN                Size;\r
+\r
+  FullyQualifiedPath = NULL;\r
+\r
+  ASSERT(Path != NULL);\r
+  //\r
+  // Handle erroneous input when asserts are disabled.\r
+  //\r
+  if (Path == NULL) {\r
+    return NULL;\r
+  }\r
+  //\r
+  // In paths that contain ":", like fs0:dir/file.ext and fs2:\fqpath\file.ext,\r
+  // we  have to consider the file system part separately from the "path" part.\r
+  // If there is a file system in the path, we have to get the current working\r
+  // directory for that file system. Then we need to use the part of the path\r
+  // following the ":".  If a path does not contain ":", we use it as given.\r
+  //\r
+  InputPath = StrStr(Path, L":");\r
+  if (InputPath != NULL) {\r
+    InputPath++;\r
+    FileSystemCharCount = ((UINTN)InputPath - (UINTN)Path + sizeof(CHAR16)) / sizeof(CHAR16);\r
+    InputFileSystem = AllocateCopyPool(FileSystemCharCount * sizeof(CHAR16), Path);\r
+    if (InputFileSystem != NULL) {\r
+      InputFileSystem[FileSystemCharCount - 1] = CHAR_NULL;\r
+    }\r
+    WorkingPath = ShellGetCurrentDir(InputFileSystem);\r
+    SHELL_FREE_NON_NULL(InputFileSystem);\r
+  } else {\r
+    InputPath = Path;\r
+    WorkingPath = ShellGetEnvironmentVariable(L"cwd");\r
+  }\r
+\r
+  if (WorkingPath == NULL) {\r
+    //\r
+    // With no working directory, all we can do is copy and clean the input path.\r
+    //\r
+    FullyQualifiedPath = AllocateCopyPool(StrSize(Path), Path);\r
+  } else {\r
+    //\r
+    // Allocate space for both strings plus one more character.\r
+    //\r
+    Size = StrSize(WorkingPath) + StrSize(InputPath);\r
+    FullyQualifiedPath = AllocateZeroPool(Size);\r
+    if (FullyQualifiedPath == NULL) {\r
+      //\r
+      // Try to copy and clean just the input. No harm if not enough memory.\r
+      //\r
+      FullyQualifiedPath = AllocateCopyPool(StrSize(Path), Path);\r
+    } else {\r
+      if (*InputPath == L'\\' || *InputPath == L'/') {\r
+        //\r
+        // Absolute path: start with the current working directory, then\r
+        // truncate the new path after the file system part.\r
+        //\r
+        StrCpyS(FullyQualifiedPath, Size/sizeof(CHAR16), WorkingPath);\r
+        CharPtr = StrStr(FullyQualifiedPath, L":");\r
+        if (CharPtr != NULL) {\r
+          *(CharPtr + 1) = CHAR_NULL;\r
+        }\r
+      } else {\r
+        //\r
+        // Relative path: start with the working directory and append "\".\r
+        //\r
+        StrCpyS(FullyQualifiedPath, Size/sizeof(CHAR16), WorkingPath);\r
+        StrCatS(FullyQualifiedPath, Size/sizeof(CHAR16), L"\\");\r
+      }\r
+      //\r
+      // Now append the absolute or relative path.\r
+      //\r
+      StrCatS(FullyQualifiedPath, Size/sizeof(CHAR16), InputPath);\r
+    }\r
+  }\r
+\r
+  PathCleanUpDirectories(FullyQualifiedPath);\r
+\r
+  return FullyQualifiedPath;\r
+}\r
+\r
 /**\r
   Check if a Unicode character is a hexadecimal character.\r
 \r
@@ -472,7 +591,6 @@ ShellSetFileInfo (
 \r
   @param  FilePath        on input the device path to the file.  On output\r
                           the remaining device path.\r
-  @param  DeviceHandle    pointer to the system device handle.\r
   @param  FileHandle      pointer to the file handle.\r
   @param  OpenMode        the mode to open the file with.\r
   @param  Attributes      the file's file attributes.\r
@@ -498,7 +616,6 @@ EFI_STATUS
 EFIAPI\r
 ShellOpenFileByDevicePath(\r
   IN OUT EFI_DEVICE_PATH_PROTOCOL     **FilePath,\r
-  OUT EFI_HANDLE                      *DeviceHandle,\r
   OUT SHELL_FILE_HANDLE               *FileHandle,\r
   IN UINT64                           OpenMode,\r
   IN UINT64                           Attributes\r
@@ -506,13 +623,9 @@ ShellOpenFileByDevicePath(
 {\r
   CHAR16                          *FileName;\r
   EFI_STATUS                      Status;\r
-  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *EfiSimpleFileSystemProtocol;\r
-  EFI_FILE_PROTOCOL               *Handle1;\r
-  EFI_FILE_PROTOCOL               *Handle2;\r
-  CHAR16                          *FnafPathName;\r
-  UINTN                           PathLen;\r
+  EFI_FILE_PROTOCOL               *File;\r
 \r
-  if (FilePath == NULL || FileHandle == NULL || DeviceHandle == NULL) {\r
+  if (FilePath == NULL || FileHandle == NULL) {\r
     return (EFI_INVALID_PARAMETER);\r
   }\r
 \r
@@ -536,117 +649,15 @@ ShellOpenFileByDevicePath(
   //\r
   // use old shell method.\r
   //\r
-  Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid,\r
-                                  FilePath,\r
-                                  DeviceHandle);\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-  Status = gBS->OpenProtocol(*DeviceHandle,\r
-                             &gEfiSimpleFileSystemProtocolGuid,\r
-                             (VOID**)&EfiSimpleFileSystemProtocol,\r
-                             gImageHandle,\r
-                             NULL,\r
-                             EFI_OPEN_PROTOCOL_GET_PROTOCOL);\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-  Status = EfiSimpleFileSystemProtocol->OpenVolume(EfiSimpleFileSystemProtocol, &Handle1);\r
+  Status = EfiOpenFileByDevicePath (FilePath, &File, OpenMode, Attributes);\r
   if (EFI_ERROR (Status)) {\r
-    FileHandle = NULL;\r
     return Status;\r
   }\r
 \r
-  //\r
-  // go down directories one node at a time.\r
-  //\r
-  while (!IsDevicePathEnd (*FilePath)) {\r
-    //\r
-    // For file system access each node should be a file path component\r
-    //\r
-    if (DevicePathType    (*FilePath) != MEDIA_DEVICE_PATH ||\r
-        DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP\r
-       ) {\r
-      FileHandle = NULL;\r
-      return (EFI_INVALID_PARAMETER);\r
-    }\r
-    //\r
-    // Open this file path node\r
-    //\r
-    Handle2  = Handle1;\r
-    Handle1 = NULL;\r
-\r
-    //\r
-    // File Name Alignment Fix (FNAF)\r
-    // Handle2->Open may be incapable of handling a unaligned CHAR16 data.\r
-    // The structure pointed to by FilePath may be not CHAR16 aligned.\r
-    // This code copies the potentially unaligned PathName data from the\r
-    // FilePath structure to the aligned FnafPathName for use in the\r
-    // calls to Handl2->Open.\r
-    //\r
-\r
-    //\r
-    // Determine length of PathName, in bytes.\r
-    //\r
-    PathLen = DevicePathNodeLength (*FilePath) - SIZE_OF_FILEPATH_DEVICE_PATH;\r
-\r
-    //\r
-    // Allocate memory for the aligned copy of the string Extra allocation is to allow for forced alignment\r
-    // Copy bytes from possibly unaligned location to aligned location\r
-    //\r
-    FnafPathName = AllocateCopyPool(PathLen, (UINT8 *)((FILEPATH_DEVICE_PATH*)*FilePath)->PathName);\r
-    if (FnafPathName == NULL) {\r
-      return EFI_OUT_OF_RESOURCES;\r
-    }\r
-\r
-    //\r
-    // Try to test opening an existing file\r
-    //\r
-    Status = Handle2->Open (\r
-                          Handle2,\r
-                          &Handle1,\r
-                          FnafPathName,\r
-                          OpenMode &~EFI_FILE_MODE_CREATE,\r
-                          0\r
-                         );\r
-\r
-    //\r
-    // see if the error was that it needs to be created\r
-    //\r
-    if ((EFI_ERROR (Status)) && (OpenMode != (OpenMode &~EFI_FILE_MODE_CREATE))) {\r
-      Status = Handle2->Open (\r
-                            Handle2,\r
-                            &Handle1,\r
-                            FnafPathName,\r
-                            OpenMode,\r
-                            Attributes\r
-                           );\r
-    }\r
-\r
-    //\r
-    // Free the alignment buffer\r
-    //\r
-    FreePool(FnafPathName);\r
-\r
-    //\r
-    // Close the last node\r
-    //\r
-    Handle2->Close (Handle2);\r
-\r
-    if (EFI_ERROR(Status)) {\r
-      return (Status);\r
-    }\r
-\r
-    //\r
-    // Get the next node\r
-    //\r
-    *FilePath = NextDevicePathNode (*FilePath);\r
-  }\r
-\r
   //\r
   // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!\r
   //\r
-  *FileHandle = (VOID*)Handle1;\r
+  *FileHandle = (VOID*)File;\r
   return (EFI_SUCCESS);\r
 }\r
 \r
@@ -690,7 +701,6 @@ ShellOpenFileByName(
   IN UINT64                     Attributes\r
   )\r
 {\r
-  EFI_HANDLE                    DeviceHandle;\r
   EFI_DEVICE_PATH_PROTOCOL      *FilePath;\r
   EFI_STATUS                    Status;\r
   EFI_FILE_INFO                 *FileInfo;\r
@@ -774,7 +784,6 @@ ShellOpenFileByName(
   FilePath = mEfiShellEnvironment2->NameToPath ((CHAR16*)FileName);\r
   if (FilePath != NULL) {\r
     return (ShellOpenFileByDevicePath(&FilePath,\r
-                                      &DeviceHandle,\r
                                       FileHandle,\r
                                       OpenMode,\r
                                       Attributes));\r
@@ -3731,33 +3740,6 @@ ShellFileExists(
   return (EFI_SUCCESS);\r
 }\r
 \r
-/**\r
-  Convert a Unicode character to upper case only if\r
-  it maps to a valid small-case ASCII character.\r
-\r
-  This internal function only deal with Unicode character\r
-  which maps to a valid small-case ASCII character, i.e.\r
-  L'a' to L'z'. For other Unicode character, the input character\r
-  is returned directly.\r
-\r
-  @param  Char  The character to convert.\r
-\r
-  @retval LowerCharacter   If the Char is with range L'a' to L'z'.\r
-  @retval Unchanged        Otherwise.\r
-\r
-**/\r
-CHAR16\r
-InternalShellCharToUpper (\r
-  IN      CHAR16                    Char\r
-  )\r
-{\r
-  if (Char >= L'a' && Char <= L'z') {\r
-    return (CHAR16) (Char - (L'a' - L'A'));\r
-  }\r
-\r
-  return Char;\r
-}\r
-\r
 /**\r
   Convert a Unicode character to numerical value.\r
 \r
@@ -3780,7 +3762,7 @@ InternalShellHexCharToUintn (
     return Char - L'0';\r
   }\r
 \r
-  return (10 + InternalShellCharToUpper (Char) - L'A');\r
+  return (10 + CharToUpper (Char) - L'A');\r
 }\r
 \r
 /**\r
@@ -3840,7 +3822,7 @@ InternalShellStrHexToUint64 (
     String++;\r
   }\r
 \r
-  if (InternalShellCharToUpper (*String) == L'X') {\r
+  if (CharToUpper (*String) == L'X') {\r
     if (*(String - 1) != L'0') {\r
       return 0;\r
     }\r