]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellLib/UefiShellLib.c
ShellPkg/UefiShellLib: rebase ShellOpenFileByDevicePath() to UefiLib API
[mirror_edk2.git] / ShellPkg / Library / UefiShellLib / UefiShellLib.c
index dac0524fcbdd98652f4fb43368290b1bada372e4..f04adbb63ffe650a9a0b77b06d5e324a57dbcc4b 100644 (file)
@@ -1,8 +1,9 @@
 /** @file\r
   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 - 2015, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2006 - 2018, 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
 **/\r
 \r
 #include "UefiShellLib.h"\r
-#include <ShellBase.h>\r
 #include <Library/SortLib.h>\r
 #include <Library/BaseLib.h>\r
 \r
-#define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)\r
-\r
 //\r
 // globals...\r
 //\r
@@ -36,6 +34,7 @@ EFI_SHELL_PROTOCOL            *gEfiShellProtocol;
 EFI_SHELL_PARAMETERS_PROTOCOL *gEfiShellParametersProtocol;\r
 EFI_HANDLE                    mEfiShellEnvironment2Handle;\r
 FILE_HANDLE_FUNCTION_MAP      FileFunctionMap;\r
+EFI_UNICODE_COLLATION_PROTOCOL  *mUnicodeCollationProtocol;\r
 \r
 /**\r
   Check if a Unicode character is a hexadecimal character.\r
@@ -90,7 +89,6 @@ ShellIsDecimalDigitCharacter (
   @retval EFI_OUT_OF_RESOURCES    Memory allocation failed.\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 ShellFindSE2 (\r
   IN EFI_HANDLE        ImageHandle\r
   )\r
@@ -174,7 +172,6 @@ ShellFindSE2 (
   @retval EFI_SUCCESS   The operationw as successful.\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 ShellLibConstructorWorker (\r
   IN EFI_HANDLE        ImageHandle,\r
   IN EFI_SYSTEM_TABLE  *SystemTable\r
@@ -182,43 +179,48 @@ ShellLibConstructorWorker (
 {\r
   EFI_STATUS  Status;\r
 \r
-  //\r
-  // UEFI 2.0 shell interfaces (used preferentially)\r
-  //\r
-  Status = gBS->OpenProtocol(\r
-    ImageHandle,\r
-    &gEfiShellProtocolGuid,\r
-    (VOID **)&gEfiShellProtocol,\r
-    ImageHandle,\r
-    NULL,\r
-    EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
-   );\r
-  if (EFI_ERROR(Status)) {\r
+  if (gEfiShellProtocol == NULL) {\r
     //\r
-    // Search for the shell protocol\r
+    // UEFI 2.0 shell interfaces (used preferentially)\r
     //\r
-    Status = gBS->LocateProtocol(\r
+    Status = gBS->OpenProtocol (\r
+      ImageHandle,\r
       &gEfiShellProtocolGuid,\r
+      (VOID **)&gEfiShellProtocol,\r
+      ImageHandle,\r
       NULL,\r
-      (VOID **)&gEfiShellProtocol\r
-     );\r
-    if (EFI_ERROR(Status)) {\r
-      gEfiShellProtocol = NULL;\r
+      EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+    );\r
+    if (EFI_ERROR (Status)) {\r
+      //\r
+      // Search for the shell protocol\r
+      //\r
+      Status = gBS->LocateProtocol (\r
+        &gEfiShellProtocolGuid,\r
+        NULL,\r
+        (VOID **)&gEfiShellProtocol\r
+      );\r
+      if (EFI_ERROR (Status)) {\r
+        gEfiShellProtocol = NULL;\r
+      }\r
     }\r
   }\r
-  Status = gBS->OpenProtocol(\r
-    ImageHandle,\r
-    &gEfiShellParametersProtocolGuid,\r
-    (VOID **)&gEfiShellParametersProtocol,\r
-    ImageHandle,\r
-    NULL,\r
-    EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
-   );\r
-  if (EFI_ERROR(Status)) {\r
-    gEfiShellParametersProtocol = NULL;\r
+\r
+  if (gEfiShellParametersProtocol == NULL) {\r
+    Status = gBS->OpenProtocol (\r
+      ImageHandle,\r
+      &gEfiShellParametersProtocolGuid,\r
+      (VOID **)&gEfiShellParametersProtocol,\r
+      ImageHandle,\r
+      NULL,\r
+      EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+    );\r
+    if (EFI_ERROR (Status)) {\r
+      gEfiShellParametersProtocol = NULL;\r
+    }\r
   }\r
 \r
-  if (gEfiShellParametersProtocol == NULL || gEfiShellProtocol == NULL) {\r
+  if (gEfiShellProtocol == NULL) {\r
     //\r
     // Moved to seperate function due to complexity\r
     //\r
@@ -241,10 +243,14 @@ ShellLibConstructorWorker (
   }\r
 \r
   //\r
-  // only success getting 2 of either the old or new, but no 1/2 and 1/2\r
+  // Getting either EDK Shell's ShellEnvironment2 and ShellInterface protocol\r
+  //  or UEFI Shell's Shell protocol.\r
+  // When ShellLib is linked to a driver producing DynamicCommand protocol,\r
+  //  ShellParameters protocol is set by DynamicCommand.Handler().\r
   //\r
-  if ((mEfiShellEnvironment2 != NULL && mEfiShellInterface          != NULL) ||\r
-      (gEfiShellProtocol     != NULL && gEfiShellParametersProtocol != NULL)   ) {\r
+  if ((mEfiShellEnvironment2 != NULL && mEfiShellInterface != NULL) ||\r
+      (gEfiShellProtocol     != NULL)\r
+      ) {\r
     if (gEfiShellProtocol != NULL) {\r
       FileFunctionMap.GetFileInfo     = gEfiShellProtocol->GetFileInfo;\r
       FileFunctionMap.SetFileInfo     = gEfiShellProtocol->SetFileInfo;\r
@@ -295,6 +301,7 @@ ShellLibConstructor (
   gEfiShellParametersProtocol = NULL;\r
   mEfiShellInterface          = NULL;\r
   mEfiShellEnvironment2Handle = NULL;\r
+  mUnicodeCollationProtocol   = NULL;\r
 \r
   //\r
   // verify that auto initialize is not set false\r
@@ -322,35 +329,45 @@ ShellLibDestructor (
   IN EFI_SYSTEM_TABLE  *SystemTable\r
   )\r
 {\r
+  EFI_STATUS           Status;\r
+\r
   if (mEfiShellEnvironment2 != NULL) {\r
-    gBS->CloseProtocol(mEfiShellEnvironment2Handle==NULL?ImageHandle:mEfiShellEnvironment2Handle,\r
+    Status = gBS->CloseProtocol(mEfiShellEnvironment2Handle==NULL?ImageHandle:mEfiShellEnvironment2Handle,\r
                        &gEfiShellEnvironment2Guid,\r
                        ImageHandle,\r
                        NULL);\r
-    mEfiShellEnvironment2 = NULL;\r
+    if (!EFI_ERROR (Status)) {\r
+      mEfiShellEnvironment2       = NULL;\r
+      mEfiShellEnvironment2Handle = NULL;\r
+    }\r
   }\r
   if (mEfiShellInterface != NULL) {\r
-    gBS->CloseProtocol(ImageHandle,\r
+    Status = gBS->CloseProtocol(ImageHandle,\r
                        &gEfiShellInterfaceGuid,\r
                        ImageHandle,\r
                        NULL);\r
-    mEfiShellInterface = NULL;\r
+    if (!EFI_ERROR (Status)) {\r
+      mEfiShellInterface = NULL;\r
+    }\r
   }\r
   if (gEfiShellProtocol != NULL) {\r
-    gBS->CloseProtocol(ImageHandle,\r
+    Status = gBS->CloseProtocol(ImageHandle,\r
                        &gEfiShellProtocolGuid,\r
                        ImageHandle,\r
                        NULL);\r
-    gEfiShellProtocol = NULL;\r
+    if (!EFI_ERROR (Status)) {\r
+      gEfiShellProtocol = NULL;\r
+    }\r
   }\r
   if (gEfiShellParametersProtocol != NULL) {\r
-    gBS->CloseProtocol(ImageHandle,\r
+    Status = gBS->CloseProtocol(ImageHandle,\r
                        &gEfiShellParametersProtocolGuid,\r
                        ImageHandle,\r
                        NULL);\r
-    gEfiShellParametersProtocol = NULL;\r
+    if (!EFI_ERROR (Status)) {\r
+      gEfiShellParametersProtocol = NULL;\r
+    }\r
   }\r
-  mEfiShellEnvironment2Handle = NULL;\r
 \r
   return (EFI_SUCCESS);\r
 }\r
@@ -371,8 +388,11 @@ ShellLibDestructor (
 EFI_STATUS\r
 EFIAPI\r
 ShellInitialize (\r
+  VOID\r
   )\r
 {\r
+  EFI_STATUS Status;\r
+\r
   //\r
   // if auto initialize is not false then skip\r
   //\r
@@ -383,7 +403,8 @@ ShellInitialize (
   //\r
   // deinit the current stuff\r
   //\r
-  ASSERT_EFI_ERROR(ShellLibDestructor(gImageHandle, gST));\r
+  Status = ShellLibDestructor (gImageHandle, gST);\r
+  ASSERT_EFI_ERROR (Status);\r
 \r
   //\r
   // init the new stuff\r
@@ -451,7 +472,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
@@ -477,7 +497,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
@@ -485,13 +504,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
@@ -515,117 +530,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
+  Status = EfiOpenFileByDevicePath (FilePath, &File, OpenMode, Attributes);\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
-  Status = EfiSimpleFileSystemProtocol->OpenVolume(EfiSimpleFileSystemProtocol, &Handle1);\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
@@ -669,7 +582,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
@@ -699,7 +611,7 @@ ShellOpenFileByName(
       // Create the directory to create the file in\r
       //\r
       FileNameCopy = AllocateCopyPool (StrSize (FileName), FileName);\r
-      if (FileName == NULL) {\r
+      if (FileNameCopy == NULL) {\r
         return (EFI_OUT_OF_RESOURCES);\r
       }\r
       PathCleanUpDirectories (FileNameCopy);\r
@@ -717,7 +629,21 @@ ShellOpenFileByName(
     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
+    if (EFI_ERROR(Status)) {\r
+      return Status;\r
+    }\r
+\r
+    if (mUnicodeCollationProtocol == NULL) {\r
+      Status = gBS->LocateProtocol (&gEfiUnicodeCollation2ProtocolGuid, NULL, (VOID**)&mUnicodeCollationProtocol);\r
+      if (EFI_ERROR (Status)) {\r
+        gEfiShellProtocol->CloseFile (*FileHandle);\r
+        return Status;\r
+      }\r
+    }\r
+\r
+    if ((mUnicodeCollationProtocol->StriColl (mUnicodeCollationProtocol, (CHAR16*)FileName, L"NUL") != 0) &&\r
+        (mUnicodeCollationProtocol->StriColl (mUnicodeCollationProtocol, (CHAR16*)FileName, L"NULL") != 0) &&\r
+         !EFI_ERROR(Status) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)){\r
       FileInfo = FileFunctionMap.GetFileInfo(*FileHandle);\r
       ASSERT(FileInfo != NULL);\r
       FileInfo->Attribute = Attributes;\r
@@ -739,7 +665,6 @@ ShellOpenFileByName(
   FilePath = mEfiShellEnvironment2->NameToPath ((CHAR16*)FileName);\r
   if (FilePath != NULL) {\r
     return (ShellOpenFileByDevicePath(&FilePath,\r
-                                      &DeviceHandle,\r
                                       FileHandle,\r
                                       OpenMode,\r
                                       Attributes));\r
@@ -1403,7 +1328,6 @@ typedef struct {
   @retval the resultant head of the double linked new format list;\r
 **/\r
 LIST_ENTRY*\r
-EFIAPI\r
 InternalShellConvertFileListType (\r
   IN LIST_ENTRY                 *FileList,\r
   IN OUT LIST_ENTRY             *ListHead\r
@@ -1873,7 +1797,6 @@ typedef struct {
   @retval FALSE                 the Parameter was not found.  Type is not valid.\r
 **/\r
 BOOLEAN\r
-EFIAPI\r
 InternalIsOnCheckList (\r
   IN CONST CHAR16               *Name,\r
   IN CONST SHELL_PARAM_ITEM     *CheckList,\r
@@ -1942,7 +1865,6 @@ InternalIsOnCheckList (
   @retval FALSE                 the Parameter not a flag.\r
 **/\r
 BOOLEAN\r
-EFIAPI\r
 InternalIsFlag (\r
   IN CONST CHAR16               *Name,\r
   IN CONST BOOLEAN              AlwaysAllowNumbers,\r
@@ -1999,7 +1921,6 @@ InternalIsFlag (
                                 ProblemParam if provided.\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 InternalCommandLineParse (\r
   IN CONST SHELL_PARAM_ITEM     *CheckList,\r
   OUT LIST_ENTRY                **CheckPackage,\r
@@ -2018,6 +1939,7 @@ InternalCommandLineParse (
   UINTN                         Count;\r
   CONST CHAR16                  *TempPointer;\r
   UINTN                         CurrentValueSize;\r
+  CHAR16                        *NewValue;\r
 \r
   CurrentItemPackage = NULL;\r
   GetItemValue = 0;\r
@@ -2116,25 +2038,32 @@ InternalCommandLineParse (
       // get the item VALUE for a previous flag\r
       //\r
       CurrentValueSize = ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16);\r
-      CurrentItemPackage->Value = ReallocatePool(ValueSize, CurrentValueSize, CurrentItemPackage->Value);\r
-      ASSERT(CurrentItemPackage->Value != NULL);\r
+      NewValue = ReallocatePool(ValueSize, CurrentValueSize, CurrentItemPackage->Value);\r
+      if (NewValue == NULL) {\r
+        SHELL_FREE_NON_NULL (CurrentItemPackage->Value);\r
+        SHELL_FREE_NON_NULL (CurrentItemPackage);\r
+        ShellCommandLineFreeVarList (*CheckPackage);\r
+        *CheckPackage = NULL;\r
+        return EFI_OUT_OF_RESOURCES;\r
+      }\r
+      CurrentItemPackage->Value = NewValue;\r
       if (ValueSize == 0) {\r
-        StrCpyS( CurrentItemPackage->Value, \r
-                  CurrentValueSize/sizeof(CHAR16), \r
+        StrCpyS( CurrentItemPackage->Value,\r
+                  CurrentValueSize/sizeof(CHAR16),\r
                   Argv[LoopCounter]\r
                   );\r
       } else {\r
-        StrCatS( CurrentItemPackage->Value, \r
-                  CurrentValueSize/sizeof(CHAR16), \r
+        StrCatS( CurrentItemPackage->Value,\r
+                  CurrentValueSize/sizeof(CHAR16),\r
                   L" "\r
                   );\r
-        StrCatS( CurrentItemPackage->Value, \r
-                  CurrentValueSize/sizeof(CHAR16), \r
+        StrCatS( CurrentItemPackage->Value,\r
+                  CurrentValueSize/sizeof(CHAR16),\r
                   Argv[LoopCounter]\r
                   );\r
       }\r
       ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);\r
-      \r
+\r
       GetItemValue--;\r
       if (GetItemValue == 0) {\r
         InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
@@ -2680,7 +2609,6 @@ ShellCopySearchAndReplace(
   @retval !EFI_SUCCESS    The operation failed.\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 InternalPrintTo (\r
   IN CONST CHAR16 *String\r
   )\r
@@ -2694,10 +2622,10 @@ InternalPrintTo (
     return (gEfiShellProtocol->WriteFile(gEfiShellParametersProtocol->StdOut, &Size, (VOID*)String));\r
   }\r
   if (mEfiShellInterface          != NULL) {\r
-    if (mEfiShellInterface->RedirArgc == 0) { \r
+    if (mEfiShellInterface->RedirArgc == 0) {\r
     //\r
     // Divide in half for old shell.  Must be string length not size.\r
-      // \r
+      //\r
       Size /=2;  // Divide in half only when no redirection.\r
     }\r
     return (mEfiShellInterface->StdOut->Write(mEfiShellInterface->StdOut,          &Size, (VOID*)String));\r
@@ -2736,7 +2664,6 @@ InternalPrintTo (
   @return EFI_DEVICE_ERROR      The console device reported an error.\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 InternalShellPrintWorker(\r
   IN INT32                Col OPTIONAL,\r
   IN INT32                Row OPTIONAL,\r
@@ -2809,7 +2736,7 @@ InternalShellPrintWorker(
     // update the attribute\r
     //\r
     if (ResumeLocation != NULL) {\r
-      if (*(ResumeLocation-1) == L'^') {\r
+      if ((ResumeLocation != mPostReplaceFormat2) && (*(ResumeLocation-1) == L'^')) {\r
         //\r
         // Move cursor back 1 position to overwrite the ^\r
         //\r
@@ -2832,10 +2759,10 @@ InternalShellPrintWorker(
             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
+            gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_LIGHTBLUE, ((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
+            gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_LIGHTGREEN, ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
             break;\r
           default:\r
             //\r
@@ -2965,13 +2892,14 @@ ShellPrintHiiEx(
   CHAR16            *HiiFormatString;\r
   EFI_STATUS        RetVal;\r
 \r
+  RetVal = EFI_DEVICE_ERROR;\r
+\r
   VA_START (Marker, HiiFormatHandle);\r
   HiiFormatString = HiiGetString(HiiFormatHandle, HiiFormatStringId, Language);\r
-  ASSERT(HiiFormatString != NULL);\r
-\r
-  RetVal = InternalShellPrintWorker(Col, Row, HiiFormatString, Marker);\r
-\r
-  SHELL_FREE_NON_NULL(HiiFormatString);\r
+  if (HiiFormatString != NULL) {\r
+    RetVal = InternalShellPrintWorker (Col, Row, HiiFormatString, Marker);\r
+    SHELL_FREE_NON_NULL (HiiFormatString);\r
+  }\r
   VA_END(Marker);\r
 \r
   return (RetVal);\r
@@ -3135,7 +3063,7 @@ ShellHexStrToUintn(
   if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, TRUE, TRUE))) {\r
     return ((UINTN)RetVal);\r
   }\r
-  \r
+\r
   return ((UINTN)(-1));\r
 }\r
 \r
@@ -3274,7 +3202,7 @@ StrnCatGrow (
   if (*Destination == NULL) {\r
     return (NULL);\r
   }\r
-  \r
+\r
   StrnCatS(*Destination, NewSize/sizeof(CHAR16), Source, Count);\r
   return *Destination;\r
 }\r
@@ -3283,7 +3211,7 @@ StrnCatGrow (
   Prompt the user and return the resultant answer to the requestor.\r
 \r
   This function will display the requested question on the shell prompt and then\r
-  wait for an apropriate answer to be input from the console.\r
+  wait for an appropriate answer to be input from the console.\r
 \r
   if the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue\r
   or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE.\r
@@ -3383,7 +3311,8 @@ ShellPromptForResponse (
             break;\r
         }\r
       }\r
-      break;    case ShellPromptResponseTypeYesNoAllCancel:\r
+      break;\r
+      case ShellPromptResponseTypeYesNoAllCancel:\r
        if (Prompt != NULL) {\r
         ShellPrintEx(-1, -1, L"%s", Prompt);\r
       }\r
@@ -3401,7 +3330,11 @@ ShellPromptForResponse (
         if (EFI_ERROR(Status)) {\r
           break;\r
         }\r
-        ShellPrintEx(-1, -1, L"%c", Key.UnicodeChar);\r
+\r
+        if (Key.UnicodeChar <= 127 && Key.UnicodeChar >= 32) {\r
+          ShellPrintEx (-1, -1, L"%c", Key.UnicodeChar);\r
+        }\r
+\r
         switch (Key.UnicodeChar) {\r
           case L'Y':\r
           case L'y':\r
@@ -3585,7 +3518,6 @@ ShellPromptForResponseHii (
   @retval FALSE       There is a non-numeric character.\r
 **/\r
 BOOLEAN\r
-EFIAPI\r
 InternalShellIsHexOrDecimalNumber (\r
   IN CONST CHAR16   *String,\r
   IN CONST BOOLEAN  ForceHex,\r
@@ -3594,29 +3526,36 @@ InternalShellIsHexOrDecimalNumber (
   )\r
 {\r
   BOOLEAN Hex;\r
+  BOOLEAN LeadingZero;\r
+\r
+  if (String == NULL) {\r
+    return FALSE;\r
+  }\r
 \r
   //\r
   // chop off a single negative sign\r
   //\r
-  if (String != NULL && *String == L'-') {\r
+  if (*String == L'-') {\r
     String++;\r
   }\r
 \r
-  if (String == NULL) {\r
-    return (FALSE);\r
+  if (*String == CHAR_NULL) {\r
+    return FALSE;\r
   }\r
 \r
   //\r
   // chop leading zeroes\r
   //\r
-  while(String != NULL && *String == L'0'){\r
+  LeadingZero = FALSE;\r
+  while(*String == L'0'){\r
     String++;\r
+    LeadingZero = TRUE;\r
   }\r
   //\r
   // allow '0x' or '0X', but not 'x' or 'X'\r
   //\r
-  if (String != NULL && (*String == L'x' || *String == L'X')) {\r
-    if (*(String-1) != L'0') {\r
+  if (*String == L'x' || *String == L'X') {\r
+    if (!LeadingZero) {\r
       //\r
       // we got an x without a preceeding 0\r
       //\r
@@ -3633,7 +3572,7 @@ InternalShellIsHexOrDecimalNumber (
   //\r
   // loop through the remaining characters and use the lib function\r
   //\r
-  for ( ; String != NULL && *String != CHAR_NULL && !(StopAtSpace && *String == L' ') ; String++){\r
+  for ( ; *String != CHAR_NULL && !(StopAtSpace && *String == L' ') ; String++){\r
     if (TimeNumbers && (String[0] == L':')) {\r
       continue;\r
     }\r
@@ -3698,7 +3637,6 @@ ShellFileExists(
 \r
 **/\r
 CHAR16\r
-EFIAPI\r
 InternalShellCharToUpper (\r
   IN      CHAR16                    Char\r
   )\r
@@ -3724,7 +3662,6 @@ InternalShellCharToUpper (
 \r
 **/\r
 UINTN\r
-EFIAPI\r
 InternalShellHexCharToUintn (\r
   IN      CHAR16                    Char\r
   )\r
@@ -3733,7 +3670,7 @@ InternalShellHexCharToUintn (
     return Char - L'0';\r
   }\r
 \r
-  return (UINTN) (10 + InternalShellCharToUpper (Char) - L'A');\r
+  return (10 + InternalShellCharToUpper (Char) - L'A');\r
 }\r
 \r
 /**\r
@@ -3767,7 +3704,6 @@ InternalShellHexCharToUintn (
   @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
@@ -3869,7 +3805,6 @@ InternalShellStrHexToUint64 (
   @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
@@ -3899,7 +3834,7 @@ InternalShellStrDecimalToUint64 (
   Result = 0;\r
 \r
   //\r
-  // Stop upon space if requested \r
+  // Stop upon space if requested\r
   // (if the whole value was 0)\r
   //\r
   if (StopAtSpace && *String == L' ') {\r
@@ -4071,6 +4006,9 @@ ShellFileHandleReturnLine(
     Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);\r
 \r
   }\r
+  if (Status == EFI_END_OF_FILE && RetVal != NULL && *RetVal != CHAR_NULL) {\r
+    Status = EFI_SUCCESS;\r
+  }\r
   if (EFI_ERROR(Status) && (RetVal != NULL)) {\r
     FreePool(RetVal);\r
     RetVal = NULL;\r
@@ -4176,43 +4114,27 @@ ShellFileHandleReadLine(
     //\r
     // if we have space save it...\r
     //\r
-    if ((CountSoFar + 1) * CharSize < *Size){\r
+    if ((CountSoFar+1)*sizeof(CHAR16) < *Size){\r
       ASSERT(Buffer != NULL);\r
-      if (*Ascii) {\r
-        ((CHAR8*)Buffer)[CountSoFar] = (CHAR8) CharBuffer;\r
-        ((CHAR8*)Buffer)[CountSoFar+1] = '\0';\r
-      }\r
-      else {\r
-        ((CHAR16*)Buffer)[CountSoFar] = CharBuffer;\r
-        ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;\r
-      }\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 (Status != EFI_END_OF_FILE){\r
-    if ((CountSoFar + 1) * CharSize > *Size){\r
-      *Size = (CountSoFar + 1) * CharSize;\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
-\r
-    if (*Ascii) {\r
-      if (CountSoFar && ((CHAR8*)Buffer)[CountSoFar - 1] == '\r') {\r
-        ((CHAR8*)Buffer)[CountSoFar - 1] = '\0';\r
-      }\r
-    }\r
-    else {\r
-      if (CountSoFar && Buffer[CountSoFar - 1] == L'\r') {\r
-        Buffer[CountSoFar - 1] = CHAR_NULL;\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
@@ -4223,7 +4145,7 @@ ShellFileHandleReadLine(
 \r
   @param[in] CommandToGetHelpOn  Pointer to a string containing the command name of help file to be printed.\r
   @param[in] SectionToGetHelpOn  Pointer to the section specifier(s).\r
-  @param[in] PrintCommandText    If TRUE, prints the command followed by the help content, otherwise prints \r
+  @param[in] PrintCommandText    If TRUE, prints the command followed by the help content, otherwise prints\r
                                  the help content only.\r
   @retval EFI_DEVICE_ERROR       The help data format was incorrect.\r
   @retval EFI_NOT_FOUND          The help data could not be found.\r
@@ -4237,33 +4159,33 @@ ShellPrintHelp (
   IN BOOLEAN          PrintCommandText\r
   )\r
 {\r
-       EFI_STATUS          Status;\r
-       CHAR16              *OutText;\r
-         \r
-       OutText = NULL;\r
-       \r
+  EFI_STATUS          Status;\r
+  CHAR16              *OutText;\r
+\r
+  OutText = NULL;\r
+\r
   //\r
   // Get the string to print based\r
   //\r
-       Status = gEfiShellProtocol->GetHelpText (CommandToGetHelpOn, SectionToGetHelpOn, &OutText);\r
-  \r
+  Status = gEfiShellProtocol->GetHelpText (CommandToGetHelpOn, SectionToGetHelpOn, &OutText);\r
+\r
   //\r
   // make sure we got a valid string\r
   //\r
   if (EFI_ERROR(Status)){\r
     return Status;\r
-       } \r
+  }\r
   if (OutText == NULL || StrLen(OutText) == 0) {\r
-    return EFI_NOT_FOUND;  \r
-       }\r
-  \r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
   //\r
   // Chop off trailing stuff we dont need\r
   //\r
   while (OutText[StrLen(OutText)-1] == L'\r' || OutText[StrLen(OutText)-1] == L'\n' || OutText[StrLen(OutText)-1] == L' ') {\r
     OutText[StrLen(OutText)-1] = CHAR_NULL;\r
   }\r
-  \r
+\r
   //\r
   // Print this out to the console\r
   //\r
@@ -4272,17 +4194,17 @@ ShellPrintHelp (
   } else {\r
     ShellPrintEx(-1, -1, L"%N%s\r\n", OutText);\r
   }\r
-  \r
+\r
   SHELL_FREE_NON_NULL(OutText);\r
 \r
-       return EFI_SUCCESS;\r
+  return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
   Function to delete a file by name\r
-  \r
+\r
   @param[in]       FileName       Pointer to file name to delete.\r
-  \r
+\r
   @retval EFI_SUCCESS             the file was deleted sucessfully\r
   @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not\r
                                   deleted\r
@@ -4309,39 +4231,38 @@ ShellDeleteFileByName(
 {\r
   EFI_STATUS                Status;\r
   SHELL_FILE_HANDLE         FileHandle;\r
-  \r
+\r
   Status = ShellFileExists(FileName);\r
-  \r
+\r
   if (Status == EFI_SUCCESS){\r
     Status = ShellOpenFileByName(FileName, &FileHandle, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0x0);\r
     if (Status == EFI_SUCCESS){\r
       Status = ShellDeleteFile(&FileHandle);\r
     }\r
-  } \r
+  }\r
 \r
   return(Status);\r
-  \r
+\r
 }\r
 \r
 /**\r
   Cleans off all the quotes in the string.\r
 \r
   @param[in]     OriginalString   pointer to the string to be cleaned.\r
-  @param[out]   CleanString      The new string with all quotes removed. \r
-                                                  Memory allocated in the function and free \r
+  @param[out]   CleanString      The new string with all quotes removed.\r
+                                                  Memory allocated in the function and free\r
                                                   by caller.\r
 \r
   @retval EFI_SUCCESS   The operation was successful.\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 InternalShellStripQuotes (\r
   IN  CONST CHAR16     *OriginalString,\r
   OUT CHAR16           **CleanString\r
   )\r
 {\r
   CHAR16            *Walker;\r
-  \r
+\r
   if (OriginalString == NULL || CleanString == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r