public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: Vladimir Olovyannikov <vladimir.olovyannikov@broadcom.com>
To: edk2-devel@lists.01.org, jaben.carsey@intel.com, ruiyu.ni@intel.com
Cc: Vladimir Olovyannikov <vladimir.olovyannikov@broadcom.com>
Subject: [PATCH] ShellPkg: Fix Shell not able to run startup.nsh from first location
Date: Mon,  6 Mar 2017 10:14:34 -0800	[thread overview]
Message-ID: <1488824074-5202-1-git-send-email-vladimir.olovyannikov@broadcom.com> (raw)

If startup.nsh is placed into first location (embedded into the firmware image), 
and the current directory has not been set (Internal Shell has just started), 
Shell cannot execute startup because of the bug in the DoStartupScript: 
after finding the correct path of the startup.nsh from the "First location" 
and opening the file, and getting of the file handle, the correct path is 
forgotten, and then RunScriptFile() receives just the name of the file 
(from mStartupScript ariable). 
It then attempts to check if this is a file with ShellIsFile() which fails 
with "EFI_INVALID_PARAMETER" (current directory is NULL, so it cannot get 
fully qualified file name), which causes Shell to exit and unload. 
This patch fixes the issue.
---
 ShellPkg/Application/Shell/Shell.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/ShellPkg/Application/Shell/Shell.c b/ShellPkg/Application/Shell/Shell.c
index 731ba187e4d9..4967fe598448 100644
--- a/ShellPkg/Application/Shell/Shell.c
+++ b/ShellPkg/Application/Shell/Shell.c
@@ -1162,6 +1162,7 @@ DoStartupScript(
   Key.UnicodeChar = CHAR_NULL;
   Key.ScanCode    = 0;
   FileHandle      = NULL;
+  FileStringPath = NULL;
 
   if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.Startup && ShellInfoObject.ShellInitSettings.FileName != NULL) {
     //
@@ -1228,7 +1229,6 @@ DoStartupScript(
   //
   MapName = ShellInfoObject.NewEfiShellProtocol->GetMapFromDevicePath(&ImagePath);
   if (MapName != NULL) {
-    FileStringPath = NULL;
     NewSize = 0;
     FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, MapName, 0);
     if (FileStringPath == NULL) {
@@ -1242,10 +1242,10 @@ DoStartupScript(
       PathRemoveLastItem(FileStringPath);
       FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, mStartupScript, 0);
       Status = ShellInfoObject.NewEfiShellProtocol->OpenFileByName(FileStringPath, &FileHandle, EFI_FILE_MODE_READ);
-      FreePool(FileStringPath);
     }
   }
   if (EFI_ERROR(Status)) {
+    SHELL_FREE_NON_NULL (FileStringPath);
     NamePath = FileDevicePath (NULL, mStartupScript);
     NewPath = AppendDevicePathNode (ImagePath, NamePath);
     FreePool(NamePath);
@@ -1254,15 +1254,21 @@ DoStartupScript(
     // Try the location
     //
     Status = InternalOpenFileDevicePath(NewPath, &FileHandle, EFI_FILE_MODE_READ, 0);
-    FreePool(NewPath);
+    if (!EFI_ERROR (Status)) {
+      FileStringPath = gEfiShellProtocol->GetFilePathFromDevicePath(NewPath);
+      if (FileStringPath == NULL) {
+        Status = EFI_OUT_OF_RESOURCES;
+      }
+    }
+    FreePool (NewPath);
   }
   //
   // If we got a file, run it
   //
   if (!EFI_ERROR(Status) && FileHandle != NULL) {
-    Status = RunScriptFile (mStartupScript, FileHandle, L"", ShellInfoObject.NewShellParametersProtocol);
-    ShellInfoObject.NewEfiShellProtocol->CloseFile(FileHandle);
+    Status = RunScriptFile (FileStringPath, FileHandle, L"", ShellInfoObject.NewShellParametersProtocol);
   } else {
+    SHELL_FREE_NON_NULL (FileStringPath);
     FileStringPath = ShellFindFilePath(mStartupScript);
     if (FileStringPath == NULL) {
       //
@@ -1272,10 +1278,13 @@ DoStartupScript(
       ASSERT(FileHandle == NULL);
     } else {
       Status = RunScriptFile(FileStringPath, NULL, L"", ShellInfoObject.NewShellParametersProtocol);
-      FreePool(FileStringPath);
     }
   }
 
+  SHELL_FREE_NON_NULL (FileStringPath);
+  if (FileHandle != NULL) {
+    ShellInfoObject.NewEfiShellProtocol->CloseFile(FileHandle);
+  }
 
   return (Status);
 }
-- 
1.9.1



             reply	other threads:[~2017-03-06 18:15 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-06 18:14 Vladimir Olovyannikov [this message]
2017-03-06 18:27 ` [PATCH] ShellPkg: Fix Shell not able to run startup.nsh from first location Carsey, Jaben

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1488824074-5202-1-git-send-email-vladimir.olovyannikov@broadcom.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox