]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Application/Shell/ShellProtocol.c
ShellPkg: Fix the support for command line comments.
[mirror_edk2.git] / ShellPkg / Application / Shell / ShellProtocol.c
index 24fa485137dc6f1d39e5364171ed38940553e5eb..11f5d481f9b0f19e61eb240859eeed5490dd6252 100644 (file)
@@ -2,6 +2,7 @@
   Member functions of EFI_SHELL_PROTOCOL and functions for creation,\r
   manipulation, and initialization of EFI_SHELL_PROTOCOL.\r
 \r
+  (C) Copyright 2014, Hewlett-Packard Development Company, L.P.\r
   Copyright (c) 2009 - 2014, 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
@@ -529,18 +530,17 @@ EfiShellGetDevicePathFromFilePath(
     if (Cwd == NULL) {\r
       return (NULL);\r
     }\r
-    Size = StrSize(Cwd);\r
-    Size += StrSize(Path);\r
+    Size = StrSize(Cwd) + StrSize(Path) - sizeof(CHAR16);\r
     NewPath = AllocateZeroPool(Size);\r
     if (NewPath == NULL) {\r
       return (NULL);\r
     }\r
-    StrCpy(NewPath, Cwd);\r
+    StrnCpy(NewPath, Cwd, Size/sizeof(CHAR16)-1);\r
     if (*Path == L'\\') {\r
       Path++;\r
       while (PathRemoveLastItem(NewPath)) ;\r
     }\r
-    StrCat(NewPath, Path);\r
+    StrnCat(NewPath, Path, Size/sizeof(CHAR16) - 1 - StrLen(NewPath));\r
     DevicePathForReturn = EfiShellGetDevicePathFromFilePath(NewPath);\r
     FreePool(NewPath);\r
     return (DevicePathForReturn);\r
@@ -1433,6 +1433,8 @@ InternalShellExecuteDevicePath(
   UINTN                         *ExitDataSizePtr;\r
   CHAR16                        *ImagePath;\r
   UINTN                         Index;\r
+  CHAR16                        *Walker;\r
+  CHAR16                        *NewCmdLine;\r
 \r
   // ExitDataSize is not OPTIONAL for gBS->BootServices, provide somewhere for\r
   // it to be dumped if the caller doesn't want it.\r
@@ -1449,6 +1451,17 @@ InternalShellExecuteDevicePath(
   InitializeListHead(&OrigEnvs);\r
 \r
   NewHandle = NULL;\r
+  \r
+  NewCmdLine = AllocateCopyPool (StrSize (CommandLine), CommandLine);\r
+  if (NewCmdLine == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  for (Walker = NewCmdLine; Walker != NULL && *Walker != CHAR_NULL ; Walker++) {\r
+    if (*Walker == L'^' && *(Walker+1) == L'#') {\r
+      CopyMem(Walker, Walker+1, StrSize(Walker) - sizeof(Walker[0]));\r
+    }\r
+  }\r
 \r
   //\r
   // Load the image with:\r
@@ -1478,9 +1491,9 @@ InternalShellExecuteDevicePath(
 \r
   if (!EFI_ERROR(Status)) {\r
     ASSERT(LoadedImage->LoadOptionsSize == 0);\r
-    if (CommandLine != NULL) {\r
-      LoadedImage->LoadOptionsSize  = (UINT32)StrSize(CommandLine);\r
-      LoadedImage->LoadOptions      = (VOID*)CommandLine;\r
+    if (NewCmdLine != NULL) {\r
+      LoadedImage->LoadOptionsSize  = (UINT32)StrSize(NewCmdLine);\r
+      LoadedImage->LoadOptions      = (VOID*)NewCmdLine;\r
     }\r
 \r
     //\r
@@ -1499,7 +1512,7 @@ InternalShellExecuteDevicePath(
     ShellParamsProtocol.StdIn   = ShellInfoObject.NewShellParametersProtocol->StdIn;\r
     ShellParamsProtocol.StdOut  = ShellInfoObject.NewShellParametersProtocol->StdOut;\r
     ShellParamsProtocol.StdErr  = ShellInfoObject.NewShellParametersProtocol->StdErr;\r
-    Status = UpdateArgcArgv(&ShellParamsProtocol, CommandLine, NULL, NULL);\r
+    Status = UpdateArgcArgv(&ShellParamsProtocol, NewCmdLine, NULL, NULL);\r
     ASSERT_EFI_ERROR(Status);\r
     //\r
     // Replace Argv[0] with the full path of the binary we're executing:\r
@@ -1578,6 +1591,8 @@ FreeAlloc:
     ASSERT_EFI_ERROR (CleanupStatus);\r
   }\r
 \r
+  FreePool (NewCmdLine);\r
+\r
   return(Status);\r
 }\r
 /**\r
@@ -1845,10 +1860,9 @@ InternalDuplicateShellFileInfo(
   if (NewNode == NULL) {\r
     return (NULL);\r
   }\r
-  NewNode->FullName = AllocateZeroPool(StrSize(Node->FullName));\r
-\r
-  NewNode->FileName = AllocateZeroPool(StrSize(Node->FileName));\r
-  NewNode->Info     = AllocateZeroPool((UINTN)Node->Info->Size);\r
+  NewNode->FullName = AllocateCopyPool(StrSize(Node->FullName), Node->FullName);\r
+  NewNode->FileName = AllocateCopyPool(StrSize(Node->FileName), Node->FileName);\r
+  NewNode->Info     = AllocateCopyPool((UINTN)Node->Info->Size, Node->Info);\r
   if ( NewNode->FullName == NULL\r
     || NewNode->FileName == NULL\r
     || NewNode->Info == NULL\r
@@ -1864,9 +1878,6 @@ InternalDuplicateShellFileInfo(
   if (!Save) {\r
     Node->Handle = NULL;\r
   }\r
-  StrCpy((CHAR16*)NewNode->FullName, Node->FullName);\r
-  StrCpy((CHAR16*)NewNode->FileName, Node->FileName);\r
-  CopyMem(NewNode->Info, Node->Info, (UINTN)Node->Info->Size);\r
 \r
   return((EFI_SHELL_FILE_INFO*)NewNode);\r
 }\r
@@ -2312,8 +2323,8 @@ ShellSearchHandle(
             if (NewFullName == NULL) {\r
               Status = EFI_OUT_OF_RESOURCES;\r
             } else {\r
-              StrCpy(NewFullName, MapName);\r
-              StrCat(NewFullName, ShellInfoNode->FullName+1);\r
+              StrnCpy(NewFullName, MapName, Size/sizeof(CHAR16)-1);\r
+              StrnCat(NewFullName, ShellInfoNode->FullName+1, (Size/sizeof(CHAR16))-StrLen(NewFullName)-1);\r
               FreePool((VOID*)ShellInfoNode->FullName);\r
               ShellInfoNode->FullName = NewFullName;\r
             }\r
@@ -2436,11 +2447,10 @@ EfiShellFindFiles(
   RootDevicePath = NULL;\r
   RootFileHandle = NULL;\r
   MapName        = NULL;\r
-  PatternCopy = AllocateZeroPool(StrSize(FilePattern));\r
+  PatternCopy = AllocateCopyPool(StrSize(FilePattern), FilePattern);\r
   if (PatternCopy == NULL) {\r
     return (EFI_OUT_OF_RESOURCES);\r
   }\r
-  StrCpy(PatternCopy, FilePattern);\r
 \r
   PatternCopy = PathCleanUpDirectories(PatternCopy);\r
 \r
@@ -2644,7 +2654,7 @@ EfiShellGetEnvEx(
       ; Node = (ENV_VAR_LIST*)GetNextNode(&List, &Node->Link)\r
      ){\r
       ASSERT(Node->Key != NULL);\r
-      StrCpy(CurrentWriteLocation, Node->Key);\r
+      StrnCpy(CurrentWriteLocation, Node->Key,  (Size)/sizeof(CHAR16) - (CurrentWriteLocation - ((CHAR16*)Buffer)) - 1);\r
       CurrentWriteLocation += StrLen(CurrentWriteLocation) + 1;\r
     }\r
 \r
@@ -2668,7 +2678,6 @@ EfiShellGetEnvEx(
       // Allocate the space and recall the get function\r
       //\r
       Buffer = AllocateZeroPool(Size);\r
-      ASSERT(Buffer != NULL);\r
       Status = SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(Name, Attributes, &Size, Buffer);\r
     }\r
     //\r
@@ -3186,7 +3195,7 @@ InternalEfiShellGetListAlias(
   \r
   @return        The null-terminated string converted into all lowercase.  \r
 **/\r
-STATIC CHAR16 *\r
+CHAR16 *\r
 ToLower (\r
   CHAR16 *Str\r
   )\r
@@ -3379,7 +3388,7 @@ EfiShellSetAlias(
 \r
 // Pure FILE_HANDLE operations are passed to FileHandleLib\r
 // these functions are indicated by the *\r
-EFI_SHELL_PROTOCOL21         mShellProtocol = {\r
+EFI_SHELL_PROTOCOL         mShellProtocol = {\r
   EfiShellExecute,\r
   EfiShellGetEnv,\r
   EfiShellSetEnv,\r
@@ -3420,8 +3429,8 @@ EFI_SHELL_PROTOCOL21         mShellProtocol = {
   EfiShellOpenRoot,\r
   EfiShellOpenRootByHandle,\r
   NULL,\r
-  2, // SHELL_MAJOR_VERSION,\r
-  1, // SHELL_MINOR_VERSION,\r
+  SHELL_MAJOR_VERSION,\r
+  SHELL_MINOR_VERSION,\r
 \r
   // New for UEFI Shell 2.1\r
   EfiShellRegisterGuidName,\r
@@ -3447,7 +3456,7 @@ EFI_SHELL_PROTOCOL21         mShellProtocol = {
 EFI_STATUS\r
 EFIAPI\r
 CreatePopulateInstallShellProtocol (\r
-  IN OUT EFI_SHELL_PROTOCOL21  **NewShell\r
+  IN OUT EFI_SHELL_PROTOCOL  **NewShell\r
   )\r
 {\r
   EFI_STATUS                  Status;\r
@@ -3574,7 +3583,7 @@ CreatePopulateInstallShellProtocol (
 EFI_STATUS\r
 EFIAPI\r
 CleanUpShellProtocol (\r
-  IN OUT EFI_SHELL_PROTOCOL21  *NewShell\r
+  IN OUT EFI_SHELL_PROTOCOL  *NewShell\r
   )\r
 {\r
   EFI_STATUS                        Status;\r