]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellLib/UefiShellLib.c
ShellPkg: Verify memory allocations without ASSERT.
[mirror_edk2.git] / ShellPkg / Library / UefiShellLib / UefiShellLib.c
index 1694f463789a7b11b0ed0cb5220023ff2678e72e..474aecb7ae5ebffcd11e7463ff727738d24cb1a2 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Provides interface to shell functionality for shell commands and applications.\r
 \r
-  Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2006 - 2011, 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
@@ -14,6 +14,7 @@
 \r
 #include "UefiShellLib.h"\r
 #include <ShellBase.h>\r
+#include <Library/SortLib.h>\r
 \r
 #define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)\r
 \r
@@ -29,12 +30,10 @@ SHELL_PARAM_ITEM SfoParamList[] = {
   };\r
 EFI_SHELL_ENVIRONMENT2        *mEfiShellEnvironment2;\r
 EFI_SHELL_INTERFACE           *mEfiShellInterface;\r
-EFI_SHELL_PROTOCOL            *mEfiShellProtocol;\r
-EFI_SHELL_PARAMETERS_PROTOCOL *mEfiShellParametersProtocol;\r
+EFI_SHELL_PROTOCOL            *gEfiShellProtocol;\r
+EFI_SHELL_PARAMETERS_PROTOCOL *gEfiShellParametersProtocol;\r
 EFI_HANDLE                    mEfiShellEnvironment2Handle;\r
 FILE_HANDLE_FUNCTION_MAP      FileFunctionMap;\r
-CHAR16                        *mPostReplaceFormat;\r
-CHAR16                        *mPostReplaceFormat2;\r
 \r
 /**\r
   Check if a Unicode character is a hexadecimal character.\r
@@ -85,6 +84,8 @@ ShellIsDecimalDigitCharacter (
   Helper function to find ShellEnvironment2 for constructor.\r
 \r
   @param[in] ImageHandle    A copy of the calling image's handle.\r
+\r
+  @retval EFI_OUT_OF_RESOURCES    Memory allocation failed.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -123,8 +124,10 @@ ShellFindSE2 (
     // maybe it's not there???\r
     //\r
     if (Status == EFI_BUFFER_TOO_SMALL) {\r
-      Buffer = (EFI_HANDLE*)AllocatePool(BufferSize);\r
-      ASSERT(Buffer != NULL);\r
+      Buffer = (EFI_HANDLE*)AllocateZeroPool(BufferSize);\r
+      if (Buffer == NULL) {\r
+        return (EFI_OUT_OF_RESOURCES);\r
+      }\r
       Status = gBS->LocateHandle (ByProtocol,\r
                                   &gEfiShellEnvironment2Guid,\r
                                   NULL, // ignored for ByProtocol\r
@@ -159,12 +162,14 @@ ShellFindSE2 (
   return (Status);\r
 }\r
 \r
-/*/\r
+/**\r
   Function to do most of the work of the constructor.  Allows for calling \r
   multiple times without complete re-initialization.\r
 \r
   @param[in] ImageHandle  A copy of the ImageHandle.\r
   @param[in] SystemTable  A pointer to the SystemTable for the application.\r
+\r
+  @retval EFI_SUCCESS   The operationw as successful.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -174,10 +179,6 @@ ShellLibConstructorWorker (
   )\r
 {\r
   EFI_STATUS  Status;\r
-  mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));\r
-  ASSERT (mPostReplaceFormat != NULL);\r
-  mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));\r
-  ASSERT (mPostReplaceFormat2 != NULL);\r
 \r
   //\r
   // UEFI 2.0 shell interfaces (used preferentially)\r
@@ -185,7 +186,7 @@ ShellLibConstructorWorker (
   Status = gBS->OpenProtocol(\r
     ImageHandle,\r
     &gEfiShellProtocolGuid,\r
-    (VOID **)&mEfiShellProtocol,\r
+    (VOID **)&gEfiShellProtocol,\r
     ImageHandle,\r
     NULL,\r
     EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
@@ -197,25 +198,25 @@ ShellLibConstructorWorker (
     Status = gBS->LocateProtocol(\r
       &gEfiShellProtocolGuid,\r
       NULL,\r
-      (VOID **)&mEfiShellProtocol\r
+      (VOID **)&gEfiShellProtocol\r
      );\r
     if (EFI_ERROR(Status)) {\r
-      mEfiShellProtocol = NULL;\r
+      gEfiShellProtocol = NULL;\r
     }\r
   }\r
   Status = gBS->OpenProtocol(\r
     ImageHandle,\r
     &gEfiShellParametersProtocolGuid,\r
-    (VOID **)&mEfiShellParametersProtocol,\r
+    (VOID **)&gEfiShellParametersProtocol,\r
     ImageHandle,\r
     NULL,\r
     EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
    );\r
   if (EFI_ERROR(Status)) {\r
-    mEfiShellParametersProtocol = NULL;\r
+    gEfiShellParametersProtocol = NULL;\r
   }\r
 \r
-  if (mEfiShellParametersProtocol == NULL || mEfiShellProtocol == NULL) {\r
+  if (gEfiShellParametersProtocol == NULL || gEfiShellProtocol == NULL) {\r
     //\r
     // Moved to seperate function due to complexity\r
     //\r
@@ -241,18 +242,18 @@ ShellLibConstructorWorker (
   // only success getting 2 of either the old or new, but no 1/2 and 1/2\r
   //\r
   if ((mEfiShellEnvironment2 != NULL && mEfiShellInterface          != NULL) ||\r
-      (mEfiShellProtocol     != NULL && mEfiShellParametersProtocol != NULL)   ) {\r
-    if (mEfiShellProtocol != NULL) {\r
-      FileFunctionMap.GetFileInfo     = mEfiShellProtocol->GetFileInfo;\r
-      FileFunctionMap.SetFileInfo     = mEfiShellProtocol->SetFileInfo;\r
-      FileFunctionMap.ReadFile        = mEfiShellProtocol->ReadFile;\r
-      FileFunctionMap.WriteFile       = mEfiShellProtocol->WriteFile;\r
-      FileFunctionMap.CloseFile       = mEfiShellProtocol->CloseFile;\r
-      FileFunctionMap.DeleteFile      = mEfiShellProtocol->DeleteFile;\r
-      FileFunctionMap.GetFilePosition = mEfiShellProtocol->GetFilePosition;\r
-      FileFunctionMap.SetFilePosition = mEfiShellProtocol->SetFilePosition;\r
-      FileFunctionMap.FlushFile       = mEfiShellProtocol->FlushFile;\r
-      FileFunctionMap.GetFileSize     = mEfiShellProtocol->GetFileSize;\r
+      (gEfiShellProtocol     != NULL && gEfiShellParametersProtocol != NULL)   ) {\r
+    if (gEfiShellProtocol != NULL) {\r
+      FileFunctionMap.GetFileInfo     = gEfiShellProtocol->GetFileInfo;\r
+      FileFunctionMap.SetFileInfo     = gEfiShellProtocol->SetFileInfo;\r
+      FileFunctionMap.ReadFile        = gEfiShellProtocol->ReadFile;\r
+      FileFunctionMap.WriteFile       = gEfiShellProtocol->WriteFile;\r
+      FileFunctionMap.CloseFile       = gEfiShellProtocol->CloseFile;\r
+      FileFunctionMap.DeleteFile      = gEfiShellProtocol->DeleteFile;\r
+      FileFunctionMap.GetFilePosition = gEfiShellProtocol->GetFilePosition;\r
+      FileFunctionMap.SetFilePosition = gEfiShellProtocol->SetFilePosition;\r
+      FileFunctionMap.FlushFile       = gEfiShellProtocol->FlushFile;\r
+      FileFunctionMap.GetFileSize     = gEfiShellProtocol->GetFileSize;\r
     } else {\r
       FileFunctionMap.GetFileInfo     = (EFI_SHELL_GET_FILE_INFO)FileHandleGetInfo;\r
       FileFunctionMap.SetFileInfo     = (EFI_SHELL_SET_FILE_INFO)FileHandleSetInfo;\r
@@ -288,12 +289,10 @@ ShellLibConstructor (
   )\r
 {\r
   mEfiShellEnvironment2       = NULL;\r
-  mEfiShellProtocol           = NULL;\r
-  mEfiShellParametersProtocol = NULL;\r
+  gEfiShellProtocol           = NULL;\r
+  gEfiShellParametersProtocol = NULL;\r
   mEfiShellInterface          = NULL;\r
   mEfiShellEnvironment2Handle = NULL;\r
-  mPostReplaceFormat          = NULL;\r
-  mPostReplaceFormat2         = NULL;\r
 \r
   //\r
   // verify that auto initialize is not set false\r
@@ -335,31 +334,22 @@ ShellLibDestructor (
                        NULL);\r
     mEfiShellInterface = NULL;\r
   }\r
-  if (mEfiShellProtocol != NULL) {\r
+  if (gEfiShellProtocol != NULL) {\r
     gBS->CloseProtocol(ImageHandle,\r
                        &gEfiShellProtocolGuid,\r
                        ImageHandle,\r
                        NULL);\r
-    mEfiShellProtocol = NULL;\r
+    gEfiShellProtocol = NULL;\r
   }\r
-  if (mEfiShellParametersProtocol != NULL) {\r
+  if (gEfiShellParametersProtocol != NULL) {\r
     gBS->CloseProtocol(ImageHandle,\r
                        &gEfiShellParametersProtocolGuid,\r
                        ImageHandle,\r
                        NULL);\r
-    mEfiShellParametersProtocol = NULL;\r
+    gEfiShellParametersProtocol = NULL;\r
   }\r
   mEfiShellEnvironment2Handle = NULL;\r
 \r
-  if (mPostReplaceFormat != NULL) {\r
-    FreePool(mPostReplaceFormat);\r
-  }\r
-  if (mPostReplaceFormat2 != NULL) {\r
-    FreePool(mPostReplaceFormat2);\r
-  }\r
-  mPostReplaceFormat          = NULL;\r
-  mPostReplaceFormat2         = NULL;\r
-\r
   return (EFI_SUCCESS);\r
 }\r
 \r
@@ -497,20 +487,18 @@ ShellOpenFileByDevicePath(
   EFI_FILE_PROTOCOL               *Handle1;\r
   EFI_FILE_PROTOCOL               *Handle2;\r
 \r
-  //\r
-  // ASERT for FileHandle, FilePath, and DeviceHandle being NULL\r
-  //\r
-  ASSERT(FilePath != NULL);\r
-  ASSERT(FileHandle != NULL);\r
-  ASSERT(DeviceHandle != NULL);\r
+  if (FilePath == NULL || FileHandle == NULL || DeviceHandle == NULL) {\r
+    return (EFI_INVALID_PARAMETER);\r
+  }\r
+\r
   //\r
   // which shell interface should we use\r
   //\r
-  if (mEfiShellProtocol != NULL) {\r
+  if (gEfiShellProtocol != NULL) {\r
     //\r
     // use UEFI Shell 2.0 method.\r
     //\r
-    FileName = mEfiShellProtocol->GetFilePathFromDevicePath(*FilePath);\r
+    FileName = gEfiShellProtocol->GetFilePathFromDevicePath(*FilePath);\r
     if (FileName == NULL) {\r
       return (EFI_INVALID_PARAMETER);\r
     }\r
@@ -615,7 +603,7 @@ ShellOpenFileByDevicePath(
   otherwise, the Filehandle is NULL. The Attributes is valid only for\r
   EFI_FILE_MODE_CREATE.\r
 \r
-  if FileNAme is NULL then ASSERT()\r
+  if FileName is NULL then ASSERT()\r
 \r
   @param  FileName               pointer to file name\r
   @param  FileHandle           pointer to the file handle.\r
@@ -662,14 +650,14 @@ ShellOpenFileByName(
     return (EFI_INVALID_PARAMETER);\r
   }\r
 \r
-  if (mEfiShellProtocol != NULL) {\r
+  if (gEfiShellProtocol != NULL) {\r
     if ((OpenMode & EFI_FILE_MODE_CREATE) == EFI_FILE_MODE_CREATE && (Attributes & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY) {\r
       return ShellCreateDirectory(FileName, FileHandle);\r
     }\r
     //\r
     // Use UEFI Shell 2.0 method\r
     //\r
-    Status = mEfiShellProtocol->OpenFileByName(FileName,\r
+    Status = gEfiShellProtocol->OpenFileByName(FileName,\r
                                                FileHandle,\r
                                                OpenMode);\r
     if (StrCmp(FileName, L"NUL") != 0 && !EFI_ERROR(Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)){\r
@@ -732,11 +720,11 @@ ShellCreateDirectory(
   OUT SHELL_FILE_HANDLE                  *FileHandle\r
   )\r
 {\r
-  if (mEfiShellProtocol != NULL) {\r
+  if (gEfiShellProtocol != NULL) {\r
     //\r
     // Use UEFI Shell 2.0 method\r
     //\r
-    return (mEfiShellProtocol->CreateFile(DirectoryName,\r
+    return (gEfiShellProtocol->CreateFile(DirectoryName,\r
                           EFI_FILE_DIRECTORY,\r
                           FileHandle\r
                          ));\r
@@ -817,7 +805,7 @@ ShellReadFile(
 EFI_STATUS\r
 EFIAPI\r
 ShellWriteFile(\r
-  IN SHELL_FILE_HANDLE                     FileHandle,\r
+  IN SHELL_FILE_HANDLE          FileHandle,\r
   IN OUT UINTN                  *BufferSize,\r
   IN VOID                       *Buffer\r
   )\r
@@ -1047,12 +1035,12 @@ ShellGetExecutionBreakFlag(
   //\r
   // Check for UEFI Shell 2.0 protocols\r
   //\r
-  if (mEfiShellProtocol != NULL) {\r
+  if (gEfiShellProtocol != NULL) {\r
 \r
     //\r
     // We are using UEFI Shell 2.0; see if the event has been triggered\r
     //\r
-    if (gBS->CheckEvent(mEfiShellProtocol->ExecutionBreak) != EFI_SUCCESS) {\r
+    if (gBS->CheckEvent(gEfiShellProtocol->ExecutionBreak) != EFI_SUCCESS) {\r
       return (FALSE);\r
     }\r
     return (TRUE);\r
@@ -1061,8 +1049,11 @@ ShellGetExecutionBreakFlag(
   //\r
   // using EFI Shell; call the function to check\r
   //\r
-  ASSERT(mEfiShellEnvironment2 != NULL);\r
-  return (mEfiShellEnvironment2->GetExecutionBreak());\r
+  if (mEfiShellEnvironment2 != NULL) {\r
+    return (mEfiShellEnvironment2->GetExecutionBreak());\r
+  }\r
+\r
+  return (FALSE);\r
 }\r
 /**\r
   return the value of an environment variable\r
@@ -1084,19 +1075,18 @@ ShellGetEnvironmentVariable (
   //\r
   // Check for UEFI Shell 2.0 protocols\r
   //\r
-  if (mEfiShellProtocol != NULL) {\r
-    return (mEfiShellProtocol->GetEnv(EnvKey));\r
+  if (gEfiShellProtocol != NULL) {\r
+    return (gEfiShellProtocol->GetEnv(EnvKey));\r
   }\r
 \r
   //\r
-  // ASSERT that we must have EFI shell\r
+  // Check for EFI shell\r
   //\r
-  ASSERT(mEfiShellEnvironment2 != NULL);\r
+  if (mEfiShellEnvironment2 != NULL) {\r
+    return (mEfiShellEnvironment2->GetEnv((CHAR16*)EnvKey));\r
+  }\r
 \r
-  //\r
-  // using EFI Shell\r
-  //\r
-  return (mEfiShellEnvironment2->GetEnv((CHAR16*)EnvKey));\r
+  return NULL;\r
 }\r
 /**\r
   set the value of an environment variable\r
@@ -1129,8 +1119,8 @@ ShellSetEnvironmentVariable (
   //\r
   // Check for UEFI Shell 2.0 protocols\r
   //\r
-  if (mEfiShellProtocol != NULL) {\r
-    return (mEfiShellProtocol->SetEnv(EnvKey, EnvVal, Volatile));\r
+  if (gEfiShellProtocol != NULL) {\r
+    return (gEfiShellProtocol->SetEnv(EnvKey, EnvVal, Volatile));\r
   }\r
 \r
   //\r
@@ -1169,7 +1159,6 @@ ShellSetEnvironmentVariable (
   @retval EFI_OUT_OF_RESOURCES    Out of resources.\r
   @retval EFI_UNSUPPORTED         The operation is not allowed.\r
 **/\r
-\r
 EFI_STATUS\r
 EFIAPI\r
 ShellExecute (\r
@@ -1183,26 +1172,30 @@ ShellExecute (
   //\r
   // Check for UEFI Shell 2.0 protocols\r
   //\r
-  if (mEfiShellProtocol != NULL) {\r
+  if (gEfiShellProtocol != NULL) {\r
     //\r
     // Call UEFI Shell 2.0 version (not using Output parameter)\r
     //\r
-    return (mEfiShellProtocol->Execute(ParentHandle,\r
+    return (gEfiShellProtocol->Execute(ParentHandle,\r
                                       CommandLine,\r
                                       EnvironmentVariables,\r
                                       Status));\r
   }\r
+\r
   //\r
-  // ASSERT that we must have EFI shell\r
-  //\r
-  ASSERT(mEfiShellEnvironment2 != NULL);\r
-  //\r
-  // Call EFI Shell version (not using EnvironmentVariables or Status parameters)\r
-  // Due to oddity in the EFI shell we want to dereference the ParentHandle here\r
+  // Check for EFI shell\r
   //\r
-  return (mEfiShellEnvironment2->Execute(*ParentHandle,\r
-                                        CommandLine,\r
-                                        Output));\r
+  if (mEfiShellEnvironment2 != NULL) {\r
+    //\r
+    // Call EFI Shell version (not using EnvironmentVariables or Status parameters)\r
+    // Due to oddity in the EFI shell we want to dereference the ParentHandle here\r
+    //\r
+    return (mEfiShellEnvironment2->Execute(*ParentHandle,\r
+                                          CommandLine,\r
+                                          Output));\r
+  }\r
+\r
+  return (EFI_UNSUPPORTED);\r
 }\r
 /**\r
   Retreives the current directory path\r
@@ -1225,14 +1218,18 @@ ShellGetCurrentDir (
   //\r
   // Check for UEFI Shell 2.0 protocols\r
   //\r
-  if (mEfiShellProtocol != NULL) {\r
-    return (mEfiShellProtocol->GetCurDir(DeviceName));\r
+  if (gEfiShellProtocol != NULL) {\r
+    return (gEfiShellProtocol->GetCurDir(DeviceName));\r
   }\r
+\r
   //\r
-  // ASSERT that we must have EFI shell\r
+  // Check for EFI shell\r
   //\r
-  ASSERT(mEfiShellEnvironment2 != NULL);\r
-  return (mEfiShellEnvironment2->CurDir(DeviceName));\r
+  if (mEfiShellEnvironment2 != NULL) {\r
+    return (mEfiShellEnvironment2->CurDir(DeviceName));\r
+  }\r
+\r
+  return (NULL);\r
 }\r
 /**\r
   sets (enabled or disabled) the page break mode\r
@@ -1255,43 +1252,45 @@ ShellSetPageBreakMode (
     //\r
     // check for UEFI Shell 2.0\r
     //\r
-    if (mEfiShellProtocol != NULL) {\r
+    if (gEfiShellProtocol != NULL) {\r
       //\r
       // Enable with UEFI 2.0 Shell\r
       //\r
-      mEfiShellProtocol->EnablePageBreak();\r
+      gEfiShellProtocol->EnablePageBreak();\r
       return;\r
     } else {\r
       //\r
-      // ASSERT that must have EFI Shell\r
-      //\r
-      ASSERT(mEfiShellEnvironment2 != NULL);\r
+      // Check for EFI shell\r
       //\r
-      // Enable with EFI Shell\r
-      //\r
-      mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);\r
-      return;\r
+      if (mEfiShellEnvironment2 != NULL) {\r
+        //\r
+        // Enable with EFI Shell\r
+        //\r
+        mEfiShellEnvironment2->EnablePageBreak (DEFAULT_INIT_ROW, DEFAULT_AUTO_LF);\r
+        return;\r
+      }\r
     }\r
   } else {\r
     //\r
     // check for UEFI Shell 2.0\r
     //\r
-    if (mEfiShellProtocol != NULL) {\r
+    if (gEfiShellProtocol != NULL) {\r
       //\r
       // Disable with UEFI 2.0 Shell\r
       //\r
-      mEfiShellProtocol->DisablePageBreak();\r
+      gEfiShellProtocol->DisablePageBreak();\r
       return;\r
     } else {\r
       //\r
-      // ASSERT that must have EFI Shell\r
+      // Check for EFI shell\r
       //\r
-      ASSERT(mEfiShellEnvironment2 != NULL);\r
-      //\r
-      // Disable with EFI Shell\r
-      //\r
-      mEfiShellEnvironment2->DisablePageBreak ();\r
-      return;\r
+      if (mEfiShellEnvironment2 != NULL) {\r
+        //\r
+        // Disable with EFI Shell\r
+        //\r
+        mEfiShellEnvironment2->DisablePageBreak ();\r
+        return;\r
+      }\r
     }\r
   }\r
 }\r
@@ -1318,8 +1317,8 @@ typedef struct {
   EFI_SHELL_FILE_INFO based list.  it is up to the caller to free the memory via\r
   the ShellCloseFileMetaArg function.\r
 \r
-  @param[in] FileList          the EFI shell list type\r
-  @param[in,out] ListHead      the list to add to\r
+  @param[in] FileList           the EFI shell list type\r
+  @param[in, out] ListHead      the list to add to\r
 \r
   @retval the resultant head of the double linked new format list;\r
 **/\r
@@ -1365,8 +1364,9 @@ InternalShellConvertFileListType (
     // allocate a new EFI_SHELL_FILE_INFO object\r
     //\r
     NewInfo               = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
-    ASSERT(NewInfo != NULL);\r
     if (NewInfo == NULL) {\r
+      ShellCloseFileMetaArg(&(EFI_SHELL_FILE_INFO*)ListHead);\r
+      ListHead = NULL;\r
       break;\r
     }\r
 \r
@@ -1389,9 +1389,11 @@ InternalShellConvertFileListType (
     //\r
     // make sure all the memory allocations were sucessful\r
     //\r
-    ASSERT(NewInfo->FullName != NULL);\r
-    ASSERT(NewInfo->FileName != NULL);\r
-    ASSERT(NewInfo->Info     != NULL);\r
+    if (NULL == NewInfo->FullName || NewInfo->FileName == NULL || NewInfo->Info == NULL) {\r
+      ShellCloseFileMetaArg(&(EFI_SHELL_FILE_INFO*)ListHead);\r
+      ListHead = NULL;\r
+      break;\r
+    }\r
 \r
     //\r
     // Copt the strings and structure\r
@@ -1450,7 +1452,7 @@ ShellOpenFileMetaArg (
   //\r
   // Check for UEFI Shell 2.0 protocols\r
   //\r
-  if (mEfiShellProtocol != NULL) {\r
+  if (gEfiShellProtocol != NULL) {\r
     if (*ListHead == NULL) {\r
       *ListHead = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
       if (*ListHead == NULL) {\r
@@ -1458,13 +1460,13 @@ ShellOpenFileMetaArg (
       }\r
       InitializeListHead(&((*ListHead)->Link));\r
     }\r
-    Status = mEfiShellProtocol->OpenFileList(Arg,\r
+    Status = gEfiShellProtocol->OpenFileList(Arg,\r
                                            OpenMode,\r
                                            ListHead);\r
     if (EFI_ERROR(Status)) {\r
-      mEfiShellProtocol->RemoveDupInFileList(ListHead);\r
+      gEfiShellProtocol->RemoveDupInFileList(ListHead);\r
     } else {\r
-      Status = mEfiShellProtocol->RemoveDupInFileList(ListHead);\r
+      Status = gEfiShellProtocol->RemoveDupInFileList(ListHead);\r
     }\r
     if (*ListHead != NULL && IsListEmpty(&(*ListHead)->Link)) {\r
       FreePool(*ListHead);\r
@@ -1475,49 +1477,50 @@ ShellOpenFileMetaArg (
   }\r
 \r
   //\r
-  // ASSERT that we must have EFI shell\r
-  //\r
-  ASSERT(mEfiShellEnvironment2 != NULL);\r
-\r
-  //\r
-  // make sure the list head is initialized\r
+  // Check for EFI shell\r
   //\r
-  InitializeListHead(&mOldStyleFileList);\r
+  if (mEfiShellEnvironment2 != NULL) {\r
+    //\r
+    // make sure the list head is initialized\r
+    //\r
+    InitializeListHead(&mOldStyleFileList);\r
 \r
-  //\r
-  // Get the EFI Shell list of files\r
-  //\r
-  Status = mEfiShellEnvironment2->FileMetaArg(Arg, &mOldStyleFileList);\r
-  if (EFI_ERROR(Status)) {\r
-    *ListHead = NULL;\r
-    return (Status);\r
-  }\r
+    //\r
+    // Get the EFI Shell list of files\r
+    //\r
+    Status = mEfiShellEnvironment2->FileMetaArg(Arg, &mOldStyleFileList);\r
+    if (EFI_ERROR(Status)) {\r
+      *ListHead = NULL;\r
+      return (Status);\r
+    }\r
 \r
-  if (*ListHead == NULL) {\r
-    *ListHead = (EFI_SHELL_FILE_INFO    *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
     if (*ListHead == NULL) {\r
-      return (EFI_OUT_OF_RESOURCES);\r
+      *ListHead = (EFI_SHELL_FILE_INFO    *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
+      if (*ListHead == NULL) {\r
+        return (EFI_OUT_OF_RESOURCES);\r
+      }\r
+      InitializeListHead(&((*ListHead)->Link));\r
     }\r
-    InitializeListHead(&((*ListHead)->Link));\r
-  }\r
 \r
-  //\r
-  // Convert that to equivalent of UEFI Shell 2.0 structure\r
-  //\r
-  InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);\r
+    //\r
+    // Convert that to equivalent of UEFI Shell 2.0 structure\r
+    //\r
+    InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);\r
 \r
-  //\r
-  // Free the EFI Shell version that was converted.\r
-  //\r
-  mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);\r
+    //\r
+    // Free the EFI Shell version that was converted.\r
+    //\r
+    mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);\r
 \r
-  if ((*ListHead)->Link.ForwardLink == (*ListHead)->Link.BackLink && (*ListHead)->Link.BackLink == &((*ListHead)->Link)) {\r
-    FreePool(*ListHead);\r
-    *ListHead = NULL;\r
-    Status = EFI_NOT_FOUND;\r
+    if ((*ListHead)->Link.ForwardLink == (*ListHead)->Link.BackLink && (*ListHead)->Link.BackLink == &((*ListHead)->Link)) {\r
+      FreePool(*ListHead);\r
+      *ListHead = NULL;\r
+      Status = EFI_NOT_FOUND;\r
+    }\r
+    return (Status);\r
   }\r
 \r
-  return (Status);\r
+  return (EFI_UNSUPPORTED);\r
 }\r
 /**\r
   Free the linked list returned from ShellOpenFileMetaArg.\r
@@ -1544,9 +1547,9 @@ ShellCloseFileMetaArg (
   //\r
   // Check for UEFI Shell 2.0 protocols\r
   //\r
-  if (mEfiShellProtocol != NULL) {\r
-    return (mEfiShellProtocol->FreeFileList(ListHead));\r
-  } else {\r
+  if (gEfiShellProtocol != NULL) {\r
+    return (gEfiShellProtocol->FreeFileList(ListHead));\r
+  } else if (mEfiShellEnvironment2 != NULL) {\r
     //\r
     // Since this is EFI Shell version we need to free our internally made copy\r
     // of the list\r
@@ -1563,6 +1566,8 @@ ShellCloseFileMetaArg (
     }\r
     return EFI_SUCCESS;\r
   }\r
+\r
+  return (EFI_UNSUPPORTED);\r
 }\r
 \r
 /**\r
@@ -1614,7 +1619,6 @@ ShellFindFilePath (
     Size = StrSize(Path);\r
     Size += StrSize(FileName);\r
     TestPath = AllocateZeroPool(Size);\r
-    ASSERT(TestPath != NULL);\r
     if (TestPath == NULL) {\r
       return (NULL);\r
     }\r
@@ -1639,31 +1643,39 @@ ShellFindFilePath (
     Size = StrSize(Path)+sizeof(CHAR16);\r
     Size += StrSize(FileName);\r
     TestPath = AllocateZeroPool(Size);\r
+    if (TestPath == NULL) {\r
+      return (NULL);\r
+    }\r
     Walker = (CHAR16*)Path;\r
     do {\r
       CopyMem(TestPath, Walker, StrSize(Walker));\r
-      TempChar = StrStr(TestPath, L";");\r
-      if (TempChar != NULL) {\r
-        *TempChar = CHAR_NULL;\r
-      }\r
-      if (TestPath[StrLen(TestPath)-1] != L'\\') {\r
-        StrCat(TestPath, L"\\");\r
-      }\r
-      StrCat(TestPath, FileName);\r
-      if (StrStr(Walker, L";") != NULL) {\r
-        Walker = StrStr(Walker, L";") + 1;\r
-      } else {\r
-        Walker = NULL;\r
-      }\r
-      Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);\r
-      if (!EFI_ERROR(Status)){\r
-        if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {\r
-          ASSERT(RetVal == NULL);\r
-          RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);\r
-          ShellCloseFile(&Handle);\r
-          break;\r
+      if (TestPath != NULL) {\r
+        TempChar = StrStr(TestPath, L";");\r
+        if (TempChar != NULL) {\r
+          *TempChar = CHAR_NULL;\r
+        }\r
+        if (TestPath[StrLen(TestPath)-1] != L'\\') {\r
+          StrCat(TestPath, L"\\");\r
+        }\r
+        if (FileName[0] == L'\\') {\r
+          FileName++;\r
+        }\r
+        StrCat(TestPath, FileName);\r
+        if (StrStr(Walker, L";") != NULL) {\r
+          Walker = StrStr(Walker, L";") + 1;\r
         } else {\r
-          ShellCloseFile(&Handle);\r
+          Walker = NULL;\r
+        }\r
+        Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);\r
+        if (!EFI_ERROR(Status)){\r
+          if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {\r
+            ASSERT(RetVal == NULL);\r
+            RetVal = StrnCatGrow(&RetVal, NULL, TestPath, 0);\r
+            ShellCloseFile(&Handle);\r
+            break;\r
+          } else {\r
+            ShellCloseFile(&Handle);\r
+          }\r
         }\r
       }\r
     } while (Walker != NULL && Walker[0] != CHAR_NULL);\r
@@ -1713,7 +1725,6 @@ ShellFindFilePathEx (
   Size =  StrSize(FileName);\r
   Size += StrSize(FileExtension);\r
   TestPath = AllocateZeroPool(Size);\r
-  ASSERT(TestPath != NULL);\r
   if (TestPath == NULL) {\r
     return (NULL);\r
   }\r
@@ -1765,10 +1776,11 @@ EFIAPI
 InternalIsOnCheckList (\r
   IN CONST CHAR16               *Name,\r
   IN CONST SHELL_PARAM_ITEM     *CheckList,\r
-  OUT SHELL_PARAM_TYPE                 *Type\r
+  OUT SHELL_PARAM_TYPE          *Type\r
   )\r
 {\r
   SHELL_PARAM_ITEM              *TempListItem;\r
+  CHAR16                        *TempString;\r
 \r
   //\r
   // ASSERT that all 3 pointer parameters aren't NULL\r
@@ -1783,6 +1795,7 @@ InternalIsOnCheckList (
   if ((StrCmp(Name, L"-?") == 0) ||\r
       (StrCmp(Name, L"-b") == 0)\r
      ) {\r
+     *Type = TypeFlag;\r
      return (TRUE);\r
   }\r
 \r
@@ -1794,10 +1807,22 @@ InternalIsOnCheckList (
     // If the Type is TypeStart only check the first characters of the passed in param\r
     // If it matches set the type and return TRUE\r
     //\r
-    if (TempListItem->Type == TypeStart && StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {\r
-      *Type = TempListItem->Type;\r
-      return (TRUE);\r
-    } else if (StrCmp(Name, TempListItem->Name) == 0) {\r
+    if (TempListItem->Type == TypeStart) { \r
+      if (StrnCmp(Name, TempListItem->Name, StrLen(TempListItem->Name)) == 0) {\r
+        *Type = TempListItem->Type;\r
+        return (TRUE);\r
+      }\r
+      TempString = NULL;\r
+      TempString = StrnCatGrow(&TempString, NULL, Name, StrLen(TempListItem->Name));\r
+      if (TempString != NULL) {\r
+        if (StringNoCaseCompare(&TempString, &TempListItem->Name) == 0) {\r
+          *Type = TempListItem->Type;\r
+          FreePool(TempString);\r
+          return (TRUE);\r
+        }\r
+        FreePool(TempString);\r
+      }\r
+    } else if (StringNoCaseCompare(&Name, &TempListItem->Name) == 0) {\r
       *Type = TempListItem->Type;\r
       return (TRUE);\r
     }\r
@@ -1883,11 +1908,12 @@ InternalCommandLineParse (
   )\r
 {\r
   UINTN                         LoopCounter;\r
-  SHELL_PARAM_TYPE                     CurrentItemType;\r
+  SHELL_PARAM_TYPE              CurrentItemType;\r
   SHELL_PARAM_PACKAGE           *CurrentItemPackage;\r
   UINTN                         GetItemValue;\r
   UINTN                         ValueSize;\r
   UINTN                         Count;\r
+  CONST CHAR16                  *TempPointer;\r
 \r
   CurrentItemPackage = NULL;\r
   GetItemValue = 0;\r
@@ -1912,6 +1938,10 @@ InternalCommandLineParse (
   // initialize the linked list\r
   //\r
   *CheckPackage = (LIST_ENTRY*)AllocateZeroPool(sizeof(LIST_ENTRY));\r
+  if (*CheckPackage == NULL) {\r
+    return (EFI_OUT_OF_RESOURCES);\r
+  }\r
+\r
   InitializeListHead(*CheckPackage);\r
 \r
   //\r
@@ -1933,10 +1963,18 @@ InternalCommandLineParse (
       //\r
       // this is a flag\r
       //\r
-      CurrentItemPackage = AllocatePool(sizeof(SHELL_PARAM_PACKAGE));\r
-      ASSERT(CurrentItemPackage != NULL);\r
-      CurrentItemPackage->Name  = AllocatePool(StrSize(Argv[LoopCounter]));\r
-      ASSERT(CurrentItemPackage->Name != NULL);\r
+      CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));\r
+      if (CurrentItemPackage == NULL) {\r
+        ShellCommandLineFreeVarList(*CheckPackage);\r
+        *CheckPackage = NULL;\r
+        return (EFI_OUT_OF_RESOURCES);\r
+      }\r
+      CurrentItemPackage->Name  = AllocateZeroPool(StrSize(Argv[LoopCounter]));\r
+      if (CurrentItemPackage->Name == NULL) {\r
+        ShellCommandLineFreeVarList(*CheckPackage);\r
+        *CheckPackage = NULL;\r
+        return (EFI_OUT_OF_RESOURCES);\r
+      }\r
       StrCpy(CurrentItemPackage->Name,  Argv[LoopCounter]);\r
       CurrentItemPackage->Type  = CurrentItemType;\r
       CurrentItemPackage->OriginalPosition = (UINTN)(-1);\r
@@ -1991,27 +2029,44 @@ InternalCommandLineParse (
       //\r
       // add this one as a non-flag\r
       //\r
-      CurrentItemPackage = AllocatePool(sizeof(SHELL_PARAM_PACKAGE));\r
-      ASSERT(CurrentItemPackage != NULL);\r
+\r
+      TempPointer = Argv[LoopCounter];\r
+      if ((*TempPointer == L'^' && *(TempPointer+1) == L'-') \r
+       || (*TempPointer == L'^' && *(TempPointer+1) == L'/')\r
+       || (*TempPointer == L'^' && *(TempPointer+1) == L'+')\r
+      ){\r
+        TempPointer++;\r
+      }\r
+      CurrentItemPackage = AllocateZeroPool(sizeof(SHELL_PARAM_PACKAGE));\r
+      if (CurrentItemPackage == NULL) {\r
+        ShellCommandLineFreeVarList(*CheckPackage);\r
+        *CheckPackage = NULL;\r
+        return (EFI_OUT_OF_RESOURCES);\r
+      }\r
       CurrentItemPackage->Name  = NULL;\r
       CurrentItemPackage->Type  = TypePosition;\r
-      CurrentItemPackage->Value = AllocatePool(StrSize(Argv[LoopCounter]));\r
-      ASSERT(CurrentItemPackage->Value != NULL);\r
-      StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);\r
+      CurrentItemPackage->Value = AllocateZeroPool(StrSize(TempPointer));\r
+      if (CurrentItemPackage->Value == NULL) {\r
+        ShellCommandLineFreeVarList(*CheckPackage);\r
+        *CheckPackage = NULL;\r
+        return (EFI_OUT_OF_RESOURCES);\r
+      }\r
+      StrCpy(CurrentItemPackage->Value, TempPointer);\r
       CurrentItemPackage->OriginalPosition = Count++;\r
       InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
-    } else if (ProblemParam != NULL) {\r
+    } else {\r
       //\r
       // this was a non-recognised flag... error!\r
       //\r
-      *ProblemParam = AllocatePool(StrSize(Argv[LoopCounter]));\r
-      ASSERT(*ProblemParam != NULL);\r
-      StrCpy(*ProblemParam, Argv[LoopCounter]);\r
+      if (ProblemParam != NULL) {\r
+        *ProblemParam = AllocateZeroPool(StrSize(Argv[LoopCounter]));\r
+        if (*ProblemParam != NULL) {\r
+          StrCpy(*ProblemParam, Argv[LoopCounter]);      \r
+        }\r
+      }\r
       ShellCommandLineFreeVarList(*CheckPackage);\r
       *CheckPackage = NULL;\r
       return (EFI_VOLUME_CORRUPTED);\r
-    } else {\r
-      //ASSERT(FALSE);\r
     }\r
   }\r
   if (GetItemValue != 0) {\r
@@ -2070,13 +2125,13 @@ ShellCommandLineParseEx (
   //\r
   // Check for UEFI Shell 2.0 protocols\r
   //\r
-  if (mEfiShellParametersProtocol != NULL) {\r
+  if (gEfiShellParametersProtocol != NULL) {\r
     return (InternalCommandLineParse(CheckList,\r
                                      CheckPackage,\r
                                      ProblemParam,\r
                                      AutoPageBreak,\r
-                                     (CONST CHAR16**) mEfiShellParametersProtocol->Argv,\r
-                                     mEfiShellParametersProtocol->Argc,\r
+                                     (CONST CHAR16**) gEfiShellParametersProtocol->Argv,\r
+                                     gEfiShellParametersProtocol->Argc,\r
                                      AlwaysAllowNumbers));\r
   }\r
 \r
@@ -2178,6 +2233,7 @@ ShellCommandLineGetFlag (
   )\r
 {\r
   LIST_ENTRY                    *Node;\r
+  CHAR16                        *TempString;\r
 \r
   //\r
   // ASSERT that both CheckPackage and KeyString aren't NULL\r
@@ -2197,7 +2253,7 @@ ShellCommandLineGetFlag (
   for ( Node = GetFirstNode(CheckPackage)\r
       ; !IsNull (CheckPackage, Node)\r
       ; Node = GetNextNode(CheckPackage, Node)\r
-     ){\r
+      ){\r
     //\r
     // If the Name matches, return TRUE (and there may be NULL name)\r
     //\r
@@ -2205,11 +2261,20 @@ ShellCommandLineGetFlag (
       //\r
       // If Type is TypeStart then only compare the begining of the strings\r
       //\r
-      if ( ((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart\r
-        && StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0\r
-       ){\r
-        return (TRUE);\r
-      } else if (StrCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
+      if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {\r
+        if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {\r
+          return (TRUE);\r
+        }\r
+        TempString = NULL;\r
+        TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));\r
+        if (TempString != NULL) {\r
+          if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
+            FreePool(TempString);\r
+            return (TRUE);\r
+          }\r
+          FreePool(TempString);\r
+        }\r
+      } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
         return (TRUE);\r
       }\r
     }\r
@@ -2237,6 +2302,7 @@ ShellCommandLineGetValue (
   )\r
 {\r
   LIST_ENTRY                    *Node;\r
+  CHAR16                        *TempString;\r
 \r
   //\r
   // check for CheckPackage == NULL\r
@@ -2251,25 +2317,28 @@ ShellCommandLineGetValue (
   for ( Node = GetFirstNode(CheckPackage)\r
       ; !IsNull (CheckPackage, Node)\r
       ; Node = GetNextNode(CheckPackage, Node)\r
-     ){\r
+      ){\r
     //\r
-    // If the Name matches, return the value (name can be NULL)\r
+    // If the Name matches, return TRUE (and there may be NULL name)\r
     //\r
     if (((SHELL_PARAM_PACKAGE*)Node)->Name != NULL) {\r
       //\r
       // If Type is TypeStart then only compare the begining of the strings\r
       //\r
-      if ( ((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart\r
-        && StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0\r
-       ){\r
-        //\r
-        // return the string part after the flag\r
-        //\r
-        return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));\r
-      } else if (StrCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
-        //\r
-        // return the value\r
-        //\r
+      if (((SHELL_PARAM_PACKAGE*)Node)->Type == TypeStart) {\r
+        if (StrnCmp(KeyString, ((SHELL_PARAM_PACKAGE*)Node)->Name, StrLen(KeyString)) == 0) {\r
+          return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));\r
+        }\r
+        TempString = NULL;\r
+        TempString = StrnCatGrow(&TempString, NULL, KeyString, StrLen(((SHELL_PARAM_PACKAGE*)Node)->Name));\r
+        if (TempString != NULL) {\r
+          if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
+            FreePool(TempString);\r
+            return (((SHELL_PARAM_PACKAGE*)Node)->Name + StrLen(KeyString));\r
+          }\r
+          FreePool(TempString);\r
+        }\r
+      } else if (StringNoCaseCompare(&KeyString, &((SHELL_PARAM_PACKAGE*)Node)->Name) == 0) {\r
         return (((SHELL_PARAM_PACKAGE*)Node)->Value);\r
       }\r
     }\r
@@ -2410,14 +2479,14 @@ ShellCommandLineCheckDuplicate (
 \r
   If the string would grow bigger than NewSize it will halt and return error.\r
 \r
-  @param[in] SourceString             The string with source buffer.\r
-  @param[in,out] NewString            The string with resultant buffer.\r
-  @param[in] NewSize                  The size in bytes of NewString.\r
-  @param[in] FindTarget               The string to look for.\r
-  @param[in] ReplaceWith              The string to replace FindTarget with.\r
-  @param[in] SkipPreCarrot            If TRUE will skip a FindTarget that has a '^'\r
-                                      immediately before it.\r
-  @param[in] ParameterReplacing       If TRUE will add "" around items with spaces.\r
+  @param[in] SourceString              The string with source buffer.\r
+  @param[in, out] NewString            The string with resultant buffer.\r
+  @param[in] NewSize                   The size in bytes of NewString.\r
+  @param[in] FindTarget                The string to look for.\r
+  @param[in] ReplaceWith               The string to replace FindTarget with.\r
+  @param[in] SkipPreCarrot             If TRUE will skip a FindTarget that has a '^'\r
+                                       immediately before it.\r
+  @param[in] ParameterReplacing        If TRUE will add "" around items with spaces.\r
 \r
   @retval EFI_INVALID_PARAMETER       SourceString was NULL.\r
   @retval EFI_INVALID_PARAMETER       NewString was NULL.\r
@@ -2458,7 +2527,12 @@ ShellCopySearchAndReplace(
     Replace = StrnCatGrow(&Replace, NULL, ReplaceWith, 0);\r
   } else {\r
     Replace = AllocateZeroPool(StrSize(ReplaceWith) + 2*sizeof(CHAR16));\r
-    UnicodeSPrint(Replace, StrSize(ReplaceWith) + 2*sizeof(CHAR16), L"\"%s\"", ReplaceWith);\r
+    if (Replace != NULL) {\r
+      UnicodeSPrint(Replace, StrSize(ReplaceWith) + 2*sizeof(CHAR16), L"\"%s\"", ReplaceWith);\r
+    }\r
+  }\r
+  if (Replace == NULL) {\r
+    return (EFI_OUT_OF_RESOURCES);\r
   }\r
   NewString = SetMem16(NewString, NewSize, CHAR_NULL);\r
   while (*SourceString != CHAR_NULL) {\r
@@ -2511,8 +2585,8 @@ InternalPrintTo (
   if (Size == 0) {\r
     return (EFI_SUCCESS);\r
   }\r
-  if (mEfiShellParametersProtocol != NULL) {\r
-    return (mEfiShellProtocol->WriteFile(mEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));\r
+  if (gEfiShellParametersProtocol != NULL) {\r
+    return (gEfiShellProtocol->WriteFile(gEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));\r
   }\r
   if (mEfiShellInterface          != NULL) {\r
     //\r
@@ -2546,8 +2620,8 @@ InternalPrintTo (
 \r
   Note: The background color is controlled by the shell command cls.\r
 \r
-  @param[in] Row        the row to print at\r
   @param[in] Col        the column to print at\r
+  @param[in] Row        the row to print at\r
   @param[in] Format     the format string\r
   @param[in] Marker     the marker for the variable argument list\r
 \r
@@ -2560,13 +2634,24 @@ InternalShellPrintWorker(
   IN INT32                Col OPTIONAL,\r
   IN INT32                Row OPTIONAL,\r
   IN CONST CHAR16         *Format,\r
-  VA_LIST                 Marker\r
+  IN VA_LIST              Marker\r
   )\r
 {\r
   EFI_STATUS        Status;\r
   CHAR16            *ResumeLocation;\r
   CHAR16            *FormatWalker;\r
   UINTN             OriginalAttribute;\r
+  CHAR16            *mPostReplaceFormat;\r
+  CHAR16            *mPostReplaceFormat2;\r
+\r
+  mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));\r
+  mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));\r
+\r
+  if (mPostReplaceFormat == NULL || mPostReplaceFormat2 == NULL) {\r
+    SHELL_FREE_NON_NULL(mPostReplaceFormat);\r
+    SHELL_FREE_NON_NULL(mPostReplaceFormat2);\r
+    return (EFI_OUT_OF_RESOURCES);\r
+  }\r
 \r
   Status            = EFI_SUCCESS;\r
   OriginalAttribute = gST->ConOut->Mode->Attribute;\r
@@ -2592,7 +2677,6 @@ InternalShellPrintWorker(
 \r
   if (Col != -1 && Row != -1) {\r
     Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);\r
-    ASSERT_EFI_ERROR(Status);\r
   }\r
 \r
   FormatWalker = mPostReplaceFormat2;\r
@@ -2618,32 +2702,40 @@ InternalShellPrintWorker(
     // update the attribute\r
     //\r
     if (ResumeLocation != NULL) {\r
-      switch (*(ResumeLocation+1)) {\r
-        case (L'N'):\r
-          gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);\r
-          break;\r
-        case (L'E'):\r
-          gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
-          break;\r
-        case (L'H'):\r
-          gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
-          break;\r
-        case (L'B'):\r
-          gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
-          break;\r
-        case (L'V'):\r
-          gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
-          break;\r
-        default:\r
-          //\r
-          // Print a simple '%' symbol\r
-          //\r
-          Status = InternalPrintTo(L"%");\r
-          if (EFI_ERROR(Status)) {\r
+      if (*(ResumeLocation-1) == L'^') {\r
+        //\r
+        // Print a simple '%' symbol\r
+        //\r
+        Status = InternalPrintTo(L"%");\r
+        ResumeLocation = ResumeLocation - 1;\r
+      } else {\r
+        switch (*(ResumeLocation+1)) {\r
+          case (L'N'):\r
+            gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);\r
             break;\r
-          }\r
-          ResumeLocation = ResumeLocation - 1;\r
-          break;\r
+          case (L'E'):\r
+            gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
+            break;\r
+          case (L'H'):\r
+            gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
+            break;\r
+          case (L'B'):\r
+            gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
+            break;\r
+          case (L'V'):\r
+            gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
+            break;\r
+          default:\r
+            //\r
+            // Print a simple '%' symbol\r
+            //\r
+            Status = InternalPrintTo(L"%");\r
+            if (EFI_ERROR(Status)) {\r
+              break;\r
+            }\r
+            ResumeLocation = ResumeLocation - 1;\r
+            break;\r
+        }\r
       }\r
     } else {\r
       //\r
@@ -2659,6 +2751,9 @@ InternalShellPrintWorker(
   }\r
 \r
   gST->ConOut->SetAttribute(gST->ConOut, OriginalAttribute);\r
+\r
+  SHELL_FREE_NON_NULL(mPostReplaceFormat);\r
+  SHELL_FREE_NON_NULL(mPostReplaceFormat2);\r
   return (Status);\r
 }\r
 \r
@@ -2702,6 +2797,9 @@ ShellPrintEx(
 {\r
   VA_LIST           Marker;\r
   EFI_STATUS        RetVal;\r
+  if (Format == NULL) {\r
+    return (EFI_INVALID_PARAMETER);\r
+  }\r
   VA_START (Marker, Format);\r
   RetVal = InternalShellPrintWorker(Col, Row, Format, Marker);\r
   VA_END(Marker);\r
@@ -2784,7 +2882,8 @@ ShellIsDirectory(
 {\r
   EFI_STATUS        Status;\r
   SHELL_FILE_HANDLE Handle;\r
-        CHAR16      *TempLocation;\r
+  CHAR16            *TempLocation;\r
+  CHAR16            *TempLocation2;\r
 \r
   ASSERT(DirName != NULL);\r
 \r
@@ -2796,12 +2895,13 @@ ShellIsDirectory(
     //\r
     // try good logic first.\r
     //\r
-    if (mEfiShellProtocol != NULL) {\r
-      TempLocation = StrnCatGrow(&TempLocation, NULL, DirName, 0);\r
-      if (StrStr(TempLocation, L":") != NULL && StrLen(StrStr(TempLocation, L":")) == 2) {\r
-        *(StrStr(TempLocation, L":")+1) = CHAR_NULL;\r
+    if (gEfiShellProtocol != NULL) {\r
+      TempLocation  = StrnCatGrow(&TempLocation, NULL, DirName, 0);\r
+      TempLocation2 = StrStr(TempLocation, L":");\r
+      if (TempLocation2 != NULL && StrLen(StrStr(TempLocation, L":")) == 2) {\r
+        *(TempLocation2+1) = CHAR_NULL;\r
       }\r
-      if (mEfiShellProtocol->GetDevicePathFromMap(TempLocation) != NULL) {\r
+      if (gEfiShellProtocol->GetDevicePathFromMap(TempLocation) != NULL) {\r
         FreePool(TempLocation);\r
         return (EFI_SUCCESS);\r
       }\r
@@ -2895,13 +2995,15 @@ ShellIsFileInPath(
   FreePool(NewName);\r
   return (Status);\r
 }\r
+\r
 /**\r
   Function to determine whether a string is decimal or hex representation of a number\r
   and return the number converted from the string.\r
 \r
   @param[in] String   String representation of a number\r
 \r
-  @retval all         the number\r
+  @return             the number\r
+  @retval (UINTN)(-1) An error ocurred.\r
 **/\r
 UINTN\r
 EFIAPI\r
@@ -2909,17 +3011,19 @@ ShellStrToUintn(
   IN CONST CHAR16 *String\r
   )\r
 {\r
-  CONST CHAR16  *Walker;\r
-  for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++);\r
-  if (Walker == NULL || *Walker == CHAR_NULL) {\r
-    ASSERT(FALSE);\r
-    return ((UINTN)(-1));\r
-  } else {\r
-    if (StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){\r
-      return (StrHexToUintn(Walker));\r
-    }\r
-    return (StrDecimalToUintn(Walker));\r
+  UINT64        RetVal;\r
+  BOOLEAN       Hex;\r
+\r
+  Hex = FALSE;\r
+\r
+  if (!InternalShellIsHexOrDecimalNumber(String, Hex, TRUE)) {\r
+    Hex = TRUE;\r
+  }\r
+\r
+  if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, Hex, TRUE))) {\r
+    return ((UINTN)RetVal);\r
   }\r
+  return ((UINTN)(-1));\r
 }\r
 \r
 /**\r
@@ -2944,8 +3048,8 @@ ShellStrToUintn(
   if Destination's current length (including NULL terminator) is already more then\r
   CurrentSize, then ASSERT()\r
 \r
-  @param[in,out] Destination   The String to append onto\r
-  @param[in,out] CurrentSize   on call the number of bytes in Destination.  On\r
+  @param[in, out] Destination   The String to append onto\r
+  @param[in, out] CurrentSize   on call the number of bytes in Destination.  On\r
                                 return possibly the new size (still in bytes).  if NULL\r
                                 then allocate whatever is needed.\r
   @param[in]      Source        The String to append from\r
@@ -3013,11 +3117,9 @@ StrnCatGrow (
       NewSize += 2 * Count * sizeof(CHAR16);\r
     }\r
     *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);\r
-    ASSERT(*Destination != NULL);\r
     *CurrentSize = NewSize;\r
   } else {\r
     *Destination = AllocateZeroPool((Count+1)*sizeof(CHAR16));\r
-    ASSERT(*Destination != NULL);\r
   }\r
 \r
   //\r
@@ -3073,7 +3175,10 @@ ShellPromptForResponse (
   Buffer  = NULL;\r
   Size    = 0;\r
   if (Type != ShellPromptResponseTypeFreeform) {\r
-    Resp = (SHELL_PROMPT_RESPONSE*)AllocatePool(sizeof(SHELL_PROMPT_RESPONSE));\r
+    Resp = (SHELL_PROMPT_RESPONSE*)AllocateZeroPool(sizeof(SHELL_PROMPT_RESPONSE));\r
+    if (Resp == NULL) {\r
+      return (EFI_OUT_OF_RESOURCES);\r
+    }\r
   }\r
 \r
   switch(Type) {\r
@@ -3299,7 +3404,7 @@ ShellPromptForResponseHii (
 **/\r
 BOOLEAN\r
 EFIAPI\r
-ShellIsHexOrDecimalNumber (\r
+InternalShellIsHexOrDecimalNumber (\r
   IN CONST CHAR16   *String,\r
   IN CONST BOOLEAN  ForceHex,\r
   IN CONST BOOLEAN  StopAtSpace\r
@@ -3356,6 +3461,7 @@ ShellIsHexOrDecimalNumber (
       }\r
     }\r
   }\r
+\r
   return (TRUE);\r
 }\r
 \r
@@ -3389,3 +3495,505 @@ ShellFileExists(
 \r
   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
+EFIAPI\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
+  This internal function only deal with Unicode character\r
+  which maps to a valid hexadecimal ASII character, i.e.\r
+  L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other \r
+  Unicode character, the value returned does not make sense.\r
+\r
+  @param  Char  The character to convert.\r
+\r
+  @return The numerical value converted.\r
+\r
+**/\r
+UINTN\r
+EFIAPI\r
+InternalShellHexCharToUintn (\r
+  IN      CHAR16                    Char\r
+  )\r
+{\r
+  if (ShellIsDecimalDigitCharacter (Char)) {\r
+    return Char - L'0';\r
+  }\r
+\r
+  return (UINTN) (10 + InternalShellCharToUpper (Char) - L'A');\r
+}\r
+\r
+/**\r
+  Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.\r
+\r
+  This function returns a value of type UINTN by interpreting the contents\r
+  of the Unicode string specified by String as a hexadecimal number.\r
+  The format of the input Unicode string String is:\r
+\r
+                  [spaces][zeros][x][hexadecimal digits].\r
+\r
+  The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].\r
+  The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.\r
+  If "x" appears in the input string, it must be prefixed with at least one 0.\r
+  The function will ignore the pad space, which includes spaces or tab characters,\r
+  before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or\r
+  [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the\r
+  first valid hexadecimal digit. Then, the function stops at the first character that is\r
+  a not a valid hexadecimal character or NULL, whichever one comes first.\r
+\r
+  If String has only pad spaces, then zero is returned.\r
+  If String has no leading pad spaces, leading zeros or valid hexadecimal digits,\r
+  then zero is returned.\r
+\r
+  @param[in]  String      A pointer to a Null-terminated Unicode string.\r
+  @param[out] Value       Upon a successful return the value of the conversion.\r
+  @param[in] StopAtSpace  FALSE to skip spaces.\r
+\r
+  @retval EFI_SUCCESS             The conversion was successful.\r
+  @retval EFI_INVALID_PARAMETER   A parameter was NULL or invalid.\r
+  @retval EFI_DEVICE_ERROR        An overflow occured.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+InternalShellStrHexToUint64 (\r
+  IN CONST CHAR16   *String,\r
+     OUT   UINT64   *Value,\r
+  IN CONST BOOLEAN  StopAtSpace\r
+  )\r
+{\r
+  UINT64    Result;\r
+\r
+  if (String == NULL || StrSize(String) == 0 || Value == NULL) {\r
+    return (EFI_INVALID_PARAMETER);\r
+  }\r
+  \r
+  //\r
+  // Ignore the pad spaces (space or tab) \r
+  //\r
+  while ((*String == L' ') || (*String == L'\t')) {\r
+    String++;\r
+  }\r
+\r
+  //\r
+  // Ignore leading Zeros after the spaces\r
+  //\r
+  while (*String == L'0') {\r
+    String++;\r
+  }\r
+\r
+  if (InternalShellCharToUpper (*String) == L'X') {\r
+    if (*(String - 1) != L'0') {\r
+      return 0;\r
+    }\r
+    //\r
+    // Skip the 'X'\r
+    //\r
+    String++;\r
+  }\r
+\r
+  Result = 0;\r
+  \r
+  //\r
+  // Skip spaces if requested\r
+  //\r
+  while (StopAtSpace && *String == L' ') {\r
+    String++;\r
+  }\r
+  \r
+  while (ShellIsHexaDecimalDigitCharacter (*String)) {\r
+    //\r
+    // If the Hex Number represented by String overflows according \r
+    // to the range defined by UINTN, then ASSERT().\r
+    //\r
+    if (!(Result <= (RShiftU64((((UINT64) ~0) - InternalShellHexCharToUintn (*String)), 4)))) {\r
+//    if (!(Result <= ((((UINT64) ~0) - InternalShellHexCharToUintn (*String)) >> 4))) {\r
+      return (EFI_DEVICE_ERROR);\r
+    }\r
+\r
+    Result = (LShiftU64(Result, 4));\r
+    Result += InternalShellHexCharToUintn (*String);\r
+    String++;\r
+\r
+    //\r
+    // Skip spaces if requested\r
+    //\r
+    while (StopAtSpace && *String == L' ') {\r
+      String++;\r
+    }\r
+  }\r
+\r
+  *Value = Result;\r
+  return (EFI_SUCCESS);\r
+}\r
+\r
+/**\r
+  Convert a Null-terminated Unicode decimal string to a value of\r
+  type UINT64.\r
+\r
+  This function returns a value of type UINT64 by interpreting the contents\r
+  of the Unicode string specified by String as a decimal number. The format\r
+  of the input Unicode string String is:\r
+\r
+                  [spaces] [decimal digits].\r
+\r
+  The valid decimal digit character is in the range [0-9]. The\r
+  function will ignore the pad space, which includes spaces or\r
+  tab characters, before [decimal digits]. The running zero in the\r
+  beginning of [decimal digits] will be ignored. Then, the function\r
+  stops at the first character that is a not a valid decimal character\r
+  or a Null-terminator, whichever one comes first.\r
+\r
+  If String has only pad spaces, then 0 is returned.\r
+  If String has no pad spaces or valid decimal digits,\r
+  then 0 is returned.\r
+\r
+  @param[in]  String      A pointer to a Null-terminated Unicode string.\r
+  @param[out] Value       Upon a successful return the value of the conversion.\r
+  @param[in] StopAtSpace  FALSE to skip spaces.\r
+\r
+  @retval EFI_SUCCESS             The conversion was successful.\r
+  @retval EFI_INVALID_PARAMETER   A parameter was NULL or invalid.\r
+  @retval EFI_DEVICE_ERROR        An overflow occured.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+InternalShellStrDecimalToUint64 (\r
+  IN CONST CHAR16 *String,\r
+     OUT   UINT64 *Value,\r
+  IN CONST BOOLEAN  StopAtSpace\r
+  )\r
+{\r
+  UINT64     Result;\r
+\r
+  if (String == NULL || StrSize (String) == 0 || Value == NULL) {\r
+    return (EFI_INVALID_PARAMETER);\r
+  }\r
+\r
+  //\r
+  // Ignore the pad spaces (space or tab)\r
+  //\r
+  while ((*String == L' ') || (*String == L'\t')) {\r
+    String++;\r
+  }\r
+\r
+  //\r
+  // Ignore leading Zeros after the spaces\r
+  //\r
+  while (*String == L'0') {\r
+    String++;\r
+  }\r
+\r
+  Result = 0;\r
+\r
+  //\r
+  // Skip spaces if requested\r
+  //\r
+  while (StopAtSpace && *String == L' ') {\r
+    String++;\r
+  }\r
+  while (ShellIsDecimalDigitCharacter (*String)) {\r
+    //\r
+    // If the number represented by String overflows according \r
+    // to the range defined by UINT64, then ASSERT().\r
+    //\r
+    \r
+    if (!(Result <= (DivU64x32((((UINT64) ~0) - (*String - L'0')),10)))) {\r
+      return (EFI_DEVICE_ERROR);\r
+    }\r
+\r
+    Result = MultU64x32(Result, 10) + (*String - L'0');\r
+    String++;\r
+\r
+    //\r
+    // Stop at spaces if requested\r
+    //\r
+    if (StopAtSpace && *String == L' ') {\r
+      break;\r
+    }\r
+  }\r
+\r
+  *Value = Result;\r
+  \r
+  return (EFI_SUCCESS);\r
+}\r
+\r
+/**\r
+  Function to verify and convert a string to its numerical value.\r
+\r
+  If Hex it must be preceeded with a 0x, 0X, or has ForceHex set TRUE.\r
+\r
+  @param[in] String       The string to evaluate.\r
+  @param[out] Value       Upon a successful return the value of the conversion.\r
+  @param[in] ForceHex     TRUE - always assume hex.\r
+  @param[in] StopAtSpace  FALSE to skip spaces.\r
+  \r
+  @retval EFI_SUCCESS             The conversion was successful.\r
+  @retval EFI_INVALID_PARAMETER   String contained an invalid character.\r
+  @retval EFI_NOT_FOUND           String was a number, but Value was NULL.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+ShellConvertStringToUint64(\r
+  IN CONST CHAR16   *String,\r
+     OUT   UINT64   *Value,\r
+  IN CONST BOOLEAN  ForceHex,\r
+  IN CONST BOOLEAN  StopAtSpace\r
+  )\r
+{\r
+  UINT64        RetVal;\r
+  CONST CHAR16  *Walker;\r
+  EFI_STATUS    Status;\r
+  BOOLEAN       Hex;\r
+\r
+  Hex = ForceHex;\r
+\r
+  if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace)) {\r
+    if (!Hex) {\r
+      Hex = TRUE;\r
+      if (!InternalShellIsHexOrDecimalNumber(String, Hex, StopAtSpace)) {\r
+        return (EFI_INVALID_PARAMETER);\r
+      }\r
+    } else {\r
+      return (EFI_INVALID_PARAMETER);\r
+    }\r
+  }\r
+\r
+  //\r
+  // Chop off leading spaces\r
+  //\r
+  for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++);\r
+\r
+  //\r
+  // make sure we have something left that is numeric.\r
+  //\r
+  if (Walker == NULL || *Walker == CHAR_NULL || !InternalShellIsHexOrDecimalNumber(Walker, Hex, StopAtSpace)) {\r
+    return (EFI_INVALID_PARAMETER);\r
+  } \r
+\r
+  //\r
+  // do the conversion.\r
+  //\r
+  if (Hex || StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){\r
+    Status = InternalShellStrHexToUint64(Walker, &RetVal, StopAtSpace);\r
+  } else {\r
+    Status = InternalShellStrDecimalToUint64(Walker, &RetVal, StopAtSpace);\r
+  }\r
+\r
+  if (Value == NULL && !EFI_ERROR(Status)) {\r
+    return (EFI_NOT_FOUND);\r
+  }\r
+\r
+  if (Value != NULL) {\r
+    *Value = RetVal;\r
+  }\r
+\r
+  return (Status);\r
+}\r
+\r
+/**\r
+  Function to determin if an entire string is a valid number.\r
+\r
+  If Hex it must be preceeded with a 0x or has ForceHex, set TRUE.\r
+\r
+  @param[in] String       The string to evaluate.\r
+  @param[in] ForceHex     TRUE - always assume hex.\r
+  @param[in] StopAtSpace  TRUE to halt upon finding a space, FALSE to keep going.\r
+\r
+  @retval TRUE        It is all numeric (dec/hex) characters.\r
+  @retval FALSE       There is a non-numeric character.\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+ShellIsHexOrDecimalNumber (\r
+  IN CONST CHAR16   *String,\r
+  IN CONST BOOLEAN  ForceHex,\r
+  IN CONST BOOLEAN  StopAtSpace\r
+  )\r
+{\r
+  if (ShellConvertStringToUint64(String, NULL, ForceHex, StopAtSpace) == EFI_NOT_FOUND) {\r
+    return (TRUE);\r
+  }\r
+  return (FALSE);\r
+}\r
+\r
+/**\r
+  Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned\r
+  buffer.  The returned buffer must be callee freed.\r
+\r
+  If the position upon start is 0, then the Ascii Boolean will be set.  This should be\r
+  maintained and not changed for all operations with the same file.\r
+\r
+  @param[in]       Handle        SHELL_FILE_HANDLE to read from.\r
+  @param[in, out]  Ascii         Boolean value for indicating whether the file is\r
+                                 Ascii (TRUE) or UCS2 (FALSE).\r
+\r
+  @return                        The line of text from the file.\r
+  @retval NULL                   There was not enough memory available.\r
+\r
+  @sa ShellFileHandleReadLine\r
+**/\r
+CHAR16*\r
+EFIAPI\r
+ShellFileHandleReturnLine(\r
+  IN SHELL_FILE_HANDLE            Handle,\r
+  IN OUT BOOLEAN                *Ascii\r
+  )\r
+{\r
+  CHAR16          *RetVal;\r
+  UINTN           Size;\r
+  EFI_STATUS      Status;\r
+\r
+  Size = 0;\r
+  RetVal = NULL;\r
+\r
+  Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);\r
+  if (Status == EFI_BUFFER_TOO_SMALL) {\r
+    RetVal = AllocateZeroPool(Size);\r
+    if (RetVal == NULL) {\r
+      return (NULL);\r
+    }\r
+    Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);\r
+    \r
+  }\r
+  if (EFI_ERROR(Status) && (RetVal != NULL)) {\r
+    FreePool(RetVal);\r
+    RetVal = NULL;\r
+  }\r
+  return (RetVal);\r
+}\r
+\r
+/**\r
+  Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE.\r
+\r
+  If the position upon start is 0, then the Ascii Boolean will be set.  This should be\r
+  maintained and not changed for all operations with the same file.\r
+\r
+  @param[in]       Handle        SHELL_FILE_HANDLE to read from.\r
+  @param[in, out]  Buffer        The pointer to buffer to read into.\r
+  @param[in, out]  Size          The pointer to number of bytes in Buffer.\r
+  @param[in]       Truncate      If the buffer is large enough, this has no effect.\r
+                                 If the buffer is is too small and Truncate is TRUE,\r
+                                 the line will be truncated.\r
+                                 If the buffer is is too small and Truncate is FALSE,\r
+                                 then no read will occur.\r
+\r
+  @param[in, out]  Ascii         Boolean value for indicating whether the file is\r
+                                 Ascii (TRUE) or UCS2 (FALSE).\r
+\r
+  @retval EFI_SUCCESS           The operation was successful.  The line is stored in\r
+                                Buffer.\r
+  @retval EFI_INVALID_PARAMETER Handle was NULL.\r
+  @retval EFI_INVALID_PARAMETER Size was NULL.\r
+  @retval EFI_BUFFER_TOO_SMALL  Size was not large enough to store the line.\r
+                                Size was updated to the minimum space required.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+ShellFileHandleReadLine(\r
+  IN SHELL_FILE_HANDLE          Handle,\r
+  IN OUT CHAR16                 *Buffer,\r
+  IN OUT UINTN                  *Size,\r
+  IN BOOLEAN                    Truncate,\r
+  IN OUT BOOLEAN                *Ascii\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  CHAR16      CharBuffer;\r
+  UINTN       CharSize;\r
+  UINTN       CountSoFar;\r
+  UINT64      OriginalFilePosition;\r
+\r
+\r
+  if (Handle == NULL\r
+    ||Size   == NULL\r
+   ){\r
+    return (EFI_INVALID_PARAMETER);\r
+  }\r
+  if (Buffer == NULL) {\r
+    ASSERT(*Size == 0);\r
+  } else {\r
+    *Buffer = CHAR_NULL;\r
+  }\r
+  gEfiShellProtocol->GetFilePosition(Handle, &OriginalFilePosition);\r
+  if (OriginalFilePosition == 0) {\r
+    CharSize = sizeof(CHAR16);\r
+    Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);\r
+    ASSERT_EFI_ERROR(Status);\r
+    if (CharBuffer == gUnicodeFileTag) {\r
+      *Ascii = FALSE;\r
+    } else {\r
+      *Ascii = TRUE;\r
+      gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);\r
+    }\r
+  }\r
+\r
+  for (CountSoFar = 0;;CountSoFar++){\r
+    CharBuffer = 0;\r
+    if (*Ascii) {\r
+      CharSize = sizeof(CHAR8);\r
+    } else {\r
+      CharSize = sizeof(CHAR16);\r
+    }\r
+    Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer);\r
+    if (  EFI_ERROR(Status)\r
+       || CharSize == 0\r
+       || (CharBuffer == L'\n' && !(*Ascii))\r
+       || (CharBuffer ==  '\n' && *Ascii)\r
+     ){\r
+      break;\r
+    }\r
+    //\r
+    // if we have space save it...\r
+    //\r
+    if ((CountSoFar+1)*sizeof(CHAR16) < *Size){\r
+      ASSERT(Buffer != NULL);\r
+      ((CHAR16*)Buffer)[CountSoFar] = CharBuffer;\r
+      ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;\r
+    }\r
+  }\r
+\r
+  //\r
+  // if we ran out of space tell when...\r
+  //\r
+  if ((CountSoFar+1)*sizeof(CHAR16) > *Size){\r
+    *Size = (CountSoFar+1)*sizeof(CHAR16);\r
+    if (!Truncate) {\r
+      gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);\r
+    } else {\r
+      DEBUG((DEBUG_WARN, "The line was truncated in ShellFileHandleReadLine"));\r
+    }\r
+    return (EFI_BUFFER_TOO_SMALL);\r
+  }\r
+  while(Buffer[StrLen(Buffer)-1] == L'\r') {\r
+    Buffer[StrLen(Buffer)-1] = CHAR_NULL;\r
+  }\r
+\r
+  return (Status);\r
+}\r