]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellDebug1CommandsLib/UefiShellDebug1CommandsLib.c
ShellPkg/editor: Fix system hang when console max column > 200
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / UefiShellDebug1CommandsLib.c
index 094e429fede2bb05c857c3278b75a147fef8b1da..d26d08f95c14c365ce2c6990eb55681172978950 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Main file for NULL named library for debug1 profile shell command functions.\r
 \r
-  Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2010 - 2017, 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 "UefiShellDebug1CommandsLib.h"\r
+#include <Library/BcfgCommandLib.h>\r
 \r
 STATIC CONST CHAR16 mFileName[] = L"Debug1Commands";\r
 EFI_HANDLE gShellDebug1HiiHandle = NULL;\r
-CONST EFI_GUID gShellDebug1HiiGuid = \\r
-  { \\r
-    0x25f200aa, 0xd3cb, 0x470a, { 0xbf, 0x51, 0xe7, 0xd1, 0x62, 0xd2, 0x2e, 0x6f } \\r
-  };\r
 \r
+/**\r
+  Gets the debug file name.  This will be used if HII is not working.\r
+\r
+  @retval NULL    No file is available.\r
+  @return         The NULL-terminated filename to get help from.\r
+**/\r
 CONST CHAR16*\r
 EFIAPI\r
 ShellCommandGetManFileNameDebug1 (\r
@@ -50,7 +53,7 @@ UefiShellDebug1CommandsLibConstructor (
   // check our bit of the profiles mask\r
   //\r
   if ((PcdGet8(PcdShellProfileMask) & BIT1) == 0) {\r
-    return (EFI_UNSUPPORTED);\r
+    return (EFI_SUCCESS);\r
   }\r
 \r
   //\r
@@ -64,41 +67,36 @@ UefiShellDebug1CommandsLibConstructor (
   //\r
   // install our shell command handlers that are always installed\r
   //\r
-  ShellCommandRegisterCommandName(L"SetSize",       ShellCommandRunSetSize            , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_SETSIZE)      );\r
+  ShellCommandRegisterCommandName(L"setsize",       ShellCommandRunSetSize            , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_SETSIZE)      );\r
   ShellCommandRegisterCommandName(L"comp",          ShellCommandRunComp               , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_COMP)         );\r
   ShellCommandRegisterCommandName(L"mode",          ShellCommandRunMode               , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_MODE)         );\r
   ShellCommandRegisterCommandName(L"memmap",        ShellCommandRunMemMap             , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_MEMMAP)       );\r
   ShellCommandRegisterCommandName(L"eficompress",   ShellCommandRunEfiCompress        , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_EFICOMPRESS)  );\r
   ShellCommandRegisterCommandName(L"efidecompress", ShellCommandRunEfiDecompress      , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_EFIDCOMPRESS) );\r
   ShellCommandRegisterCommandName(L"dmem",          ShellCommandRunDmem               , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_DMEM)         );\r
-  ShellCommandRegisterCommandName(L"LoadPciRom",    ShellCommandRunLoadPciRom         , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_LOAD_PCI_ROM) );\r
+  ShellCommandRegisterCommandName(L"loadpcirom",    ShellCommandRunLoadPciRom         , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_LOAD_PCI_ROM) );\r
   ShellCommandRegisterCommandName(L"mm",            ShellCommandRunMm                 , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_MM)           );\r
-  ShellCommandRegisterCommandName(L"SetVar",        ShellCommandRunSetVar             , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_SETVAR)       );\r
-  ShellCommandRegisterCommandName(L"SerMode",       ShellCommandRunSerMode            , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_SERMODE)      );\r
-  ShellCommandRegisterCommandName(L"Pci",           ShellCommandRunPci                , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_PCI)          );\r
+  ShellCommandRegisterCommandName(L"setvar",        ShellCommandRunSetVar             , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_SETVAR)       );\r
+  ShellCommandRegisterCommandName(L"sermode",       ShellCommandRunSerMode            , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_SERMODE)      );\r
+  ShellCommandRegisterCommandName(L"pci",           ShellCommandRunPci                , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_PCI)          );\r
   ShellCommandRegisterCommandName(L"smbiosview",    ShellCommandRunSmbiosView         , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_SMBIOSVIEW)   );\r
   ShellCommandRegisterCommandName(L"dmpstore",      ShellCommandRunDmpStore           , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_DMPSTORE)     );\r
   ShellCommandRegisterCommandName(L"dblk",          ShellCommandRunDblk               , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_DBLK)         );\r
-\r
-  //\r
-  // check install profile bit of the profiles mask is set\r
-  //\r
-  if ((PcdGet8(PcdShellProfileMask) & BIT2) == 0) {\r
-    ShellCommandRegisterCommandName(L"bcfg",        ShellCommandRunBcfg               , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_BCFG)         );\r
-  }\r
-\r
-/*\r
-  ShellCommandRegisterCommandName(L"hexedit",       ShellCommandRunHexEdit            , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_HEXEDIT       );\r
   ShellCommandRegisterCommandName(L"edit",          ShellCommandRunEdit               , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_EDIT)         );\r
-*/\r
+  ShellCommandRegisterCommandName(L"hexedit",       ShellCommandRunHexEdit            , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_HEXEDIT)      );\r
 \r
   ShellCommandRegisterAlias(L"dmem", L"mem");\r
 \r
+  BcfgLibraryRegisterBcfgCommand(ImageHandle, SystemTable, L"Debug1");\r
+\r
   return (EFI_SUCCESS);\r
 }\r
 \r
 /**\r
   Destructor for the library.  free any resources.\r
+\r
+  @param ImageHandle            The image handle of the process.\r
+  @param SystemTable            The EFI System Table pointer.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -110,72 +108,9 @@ UefiShellDebug1CommandsLibDestructor (
   if (gShellDebug1HiiHandle != NULL) {\r
     HiiRemovePackages(gShellDebug1HiiHandle);\r
   }\r
-  return (EFI_SUCCESS);\r
-}\r
-\r
-STATIC CONST CHAR8 Hex[] = {\r
-  '0',\r
-  '1',\r
-  '2',\r
-  '3',\r
-  '4',\r
-  '5',\r
-  '6',\r
-  '7',\r
-  '8',\r
-  '9',\r
-  'A',\r
-  'B',\r
-  'C',\r
-  'D',\r
-  'E',\r
-  'F'\r
-};\r
-\r
-VOID\r
-EFIAPI\r
-DumpHex (\r
-  IN UINTN        Indent,\r
-  IN UINTN        Offset,\r
-  IN UINTN        DataSize,\r
-  IN VOID         *UserData\r
-  )\r
-{\r
-  UINT8 *Data;\r
-\r
-  CHAR8 Val[50];\r
-\r
-  CHAR8 Str[20];\r
-\r
-  UINT8 c;\r
-  UINTN Size;\r
-  UINTN Index;\r
-\r
-  ASSERT (UserData != NULL);\r
-\r
-  Data = UserData;\r
-  while (DataSize != 0) {\r
-    Size = 16;\r
-    if (Size > DataSize) {\r
-      Size = DataSize;\r
-    }\r
-\r
-    for (Index = 0; Index < Size; Index += 1) {\r
-      c                   = Data[Index];\r
-      Val[Index * 3 + 0]  = Hex[c >> 4];\r
-      Val[Index * 3 + 1]  = Hex[c & 0xF];\r
-      Val[Index * 3 + 2]  = (CHAR8) ((Index == 7) ? '-' : ' ');\r
-      Str[Index]          = (CHAR8) ((c < ' ' || c > 'z') ? '.' : c);\r
-    }\r
 \r
-    Val[Index * 3]  = 0;\r
-    Str[Index]      = 0;\r
-    ShellPrintEx(-1, -1, L"%*a%02X: %-.48a *%a*\r\n", Indent, "", Offset, Val, Str);\r
-\r
-    Data += Size;\r
-    Offset += Size;\r
-    DataSize -= Size;\r
-  }\r
+  BcfgLibraryUnregisterBcfgCommand(ImageHandle, SystemTable);\r
+  return (EFI_SUCCESS);\r
 }\r
 \r
 /**\r
@@ -196,7 +131,6 @@ DumpHex (
   //Stolen from MdePkg Baselib\r
 **/\r
 CHAR16\r
-EFIAPI\r
 CharToUpper (\r
   IN      CHAR16                    Char\r
   )\r
@@ -212,14 +146,13 @@ CharToUpper (
   Function returns a system configuration table that is stored in the\r
   EFI System Table based on the provided GUID.\r
 \r
-  @param[in]  TableGuid    A pointer to the table's GUID type.\r
-  @param[out] Table        On exit, a pointer to a system configuration table.\r
+  @param[in]  TableGuid     A pointer to the table's GUID type.\r
+  @param[in, out] Table     On exit, a pointer to a system configuration table.\r
 \r
   @retval EFI_SUCCESS      A configuration table matching TableGuid was found.\r
   @retval EFI_NOT_FOUND    A configuration table matching TableGuid was not found.\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 GetSystemConfigurationTable (\r
   IN EFI_GUID *TableGuid,\r
   IN OUT VOID **Table\r
@@ -229,7 +162,7 @@ GetSystemConfigurationTable (
   ASSERT (Table != NULL);\r
 \r
   for (Index = 0; Index < gST->NumberOfTableEntries; Index++) {\r
-    if (CompareGuid (TableGuid, &(gST->ConfigurationTable[Index].VendorGuid)) == 0) {\r
+    if (CompareGuid (TableGuid, &(gST->ConfigurationTable[Index].VendorGuid))) {\r
       *Table = gST->ConfigurationTable[Index].VendorTable;\r
       return EFI_SUCCESS;\r
     }\r
@@ -239,71 +172,284 @@ GetSystemConfigurationTable (
 }\r
 \r
 /**\r
-  Convert a Unicode character to numerical value.\r
+  Clear the line at the specified Row.\r
+  \r
+  @param[in] Row                The row number to be cleared ( start from 1 )\r
+  @param[in] LastCol            The last printable column.\r
+  @param[in] LastRow            The last printable row.\r
+**/\r
+VOID\r
+EditorClearLine (\r
+  IN UINTN Row,\r
+  IN UINTN LastCol,\r
+  IN UINTN LastRow\r
+  )\r
+{\r
+  UINTN  Col;\r
+  CHAR16 Line[200];\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
+  if (Row == 0) {\r
+    Row = 1;\r
+  }\r
 \r
-  @param  Char  The character to convert.\r
+  //\r
+  // prepare a blank line\r
+  // If max column is larger, split to multiple prints.\r
+  //\r
+  SetMem16 (Line, sizeof (Line), L' ');\r
+  Line[ARRAY_SIZE (Line) - 1] = CHAR_NULL;\r
+\r
+  for (Col = 1; Col <= LastCol; Col += ARRAY_SIZE (Line) - 1) {\r
+    if (Col + ARRAY_SIZE (Line) - 1 > LastCol) {\r
+      if (Row == LastRow) {\r
+        //\r
+        // if CHAR_NULL is still at position LastCol, it will cause first line error\r
+        //\r
+        Line[(LastCol % (ARRAY_SIZE (Line) - 1)) - 1] = CHAR_NULL;\r
+      } else {\r
+        Line[LastCol % (ARRAY_SIZE (Line) - 1)] = CHAR_NULL;\r
+      }\r
+    }\r
\r
+    //\r
+    // print out the blank line\r
+    //\r
+    ShellPrintEx ((INT32) Col - 1, (INT32) Row - 1, Line);\r
+  }\r
+}\r
 \r
-  @return The numerical value converted.\r
+/**\r
+  Determine if the character is valid for a filename.\r
 \r
+  @param[in] Ch     The character to test.\r
+\r
+  @retval TRUE      The character is valid.\r
+  @retval FALSE     The character is not valid.\r
 **/\r
-UINTN\r
-EFIAPI\r
-HexCharToUintn (\r
-  IN      CHAR16                    Char\r
+BOOLEAN\r
+IsValidFileNameChar (\r
+  IN CONST CHAR16 Ch\r
+  )\r
+{\r
+  //\r
+  // See if there are any illegal characters within the name\r
+  //\r
+  if (Ch < 0x20 || Ch == L'\"' || Ch == L'*' || Ch == L'/' || Ch == L'<' || Ch == L'>' || Ch == L'?' || Ch == L'|') {\r
+    return FALSE;\r
+  }\r
+\r
+  return TRUE;\r
+}\r
+\r
+/**\r
+  Check if file name has illegal characters.\r
+  \r
+  @param Name       The filename to check.\r
+\r
+  @retval TRUE      The filename is ok.\r
+  @retval FALSE     The filename is not ok.\r
+**/\r
+BOOLEAN\r
+IsValidFileName (\r
+  IN CONST CHAR16 *Name\r
   )\r
 {\r
-  if (Char >= L'0' && Char <= L'9') {\r
-    return Char - L'0';\r
+\r
+  UINTN Index;\r
+  UINTN Len;\r
+\r
+  //\r
+  // check the length of Name\r
+  //\r
+  for (Len = 0, Index = StrLen (Name) - 1; Index + 1 != 0; Index--, Len++) {\r
+    if (Name[Index] == '\\' || Name[Index] == ':') {\r
+      break;\r
+    }\r
+  }\r
+\r
+  if (Len == 0 || Len > 255) {\r
+    return FALSE;\r
+  }\r
+  //\r
+  // check whether any char in Name not appears in valid file name char\r
+  //\r
+  for (Index = 0; Index < StrLen (Name); Index++) {\r
+    if (!IsValidFileNameChar (Name[Index])) {\r
+      return FALSE;\r
+    }\r
   }\r
 \r
-  return (UINTN) (10 + CharToUpper (Char) - L'A');\r
+  return TRUE;\r
+}\r
+\r
+/**\r
+  Find a filename that is valid (not taken) with the given extension.\r
+\r
+  @param[in] Extension      The file extension.\r
+\r
+  @retval NULL  Something went wrong.\r
+  @return the valid filename.\r
+**/\r
+CHAR16 *\r
+EditGetDefaultFileName (\r
+  IN CONST CHAR16 *Extension\r
+  )\r
+{\r
+  EFI_STATUS         Status;\r
+  UINTN              Suffix;\r
+  CHAR16             *FileNameTmp;\r
+\r
+  Suffix       = 0;\r
+\r
+  do {\r
+    FileNameTmp = CatSPrint (NULL, L"NewFile%d.%s", Suffix, Extension);\r
+\r
+    //\r
+    // after that filename changed to path\r
+    //\r
+    Status = ShellFileExists (FileNameTmp);\r
+\r
+    if (Status == EFI_NOT_FOUND) {\r
+      return FileNameTmp;\r
+    }\r
+\r
+    FreePool (FileNameTmp);\r
+    FileNameTmp = NULL;\r
+    Suffix++;\r
+  } while (Suffix != 0);\r
+\r
+  FreePool (FileNameTmp);\r
+  return NULL;\r
 }\r
 \r
+/**\r
+  Read a file into an allocated buffer.  The buffer is the responsibility \r
+  of the caller to free.\r
+\r
+  @param[in]  FileName          The filename of the file to open.\r
+  @param[out] Buffer            Upon successful return, the pointer to the \r
+                                address of the allocated buffer.                                  \r
+  @param[out] BufferSize        If not NULL, then the pointer to the size\r
+                                of the allocated buffer.\r
+  @param[out] ReadOnly          Upon successful return TRUE if the file is\r
+                                read only.  FALSE otherwise.\r
+\r
+  @retval EFI_NOT_FOUND         The filename did not represent a file in the \r
+                                file system.\r
+  @retval EFI_SUCCESS           The file was read into the buffer.\r
+  @retval EFI_OUT_OF_RESOURCES  A memory allocation failed.\r
+  @retval EFI_LOAD_ERROR        The file read operation failed.\r
+  @retval EFI_INVALID_PARAMETER A parameter was invalid.\r
+  @retval EFI_INVALID_PARAMETER FileName was NULL.\r
+  @retval EFI_INVALID_PARAMETER FileName was a directory.\r
+**/\r
 EFI_STATUS\r
-EFIAPI\r
-ConvertStringToGuid (\r
-  IN CONST CHAR16 *StringGuid,\r
-  IN OUT EFI_GUID *Guid\r
+ReadFileIntoBuffer (\r
+  IN CONST CHAR16 *FileName,\r
+  OUT VOID        **Buffer,\r
+  OUT UINTN       *BufferSize OPTIONAL,\r
+  OUT BOOLEAN     *ReadOnly\r
   )\r
 {\r
-  if (StrLen(StringGuid) != 35) {\r
+  VOID              *InternalBuffer;\r
+  UINTN             FileSize;\r
+  SHELL_FILE_HANDLE FileHandle;\r
+  BOOLEAN           CreateFile;\r
+  EFI_STATUS        Status;\r
+  EFI_FILE_INFO     *Info;\r
+\r
+  InternalBuffer  = NULL;\r
+  FileSize        = 0;\r
+  FileHandle      = NULL;\r
+  CreateFile      = FALSE;\r
+  Status          = EFI_SUCCESS;\r
+  Info            = NULL;\r
+\r
+  if (FileName == NULL || Buffer == NULL || ReadOnly == NULL) {\r
     return (EFI_INVALID_PARAMETER);\r
-  } else {\r
-    Guid->Data1 = (UINT32)StrHexToUintn(StringGuid);\r
-    StringGuid += 9;\r
-    Guid->Data2 = (UINT16)StrHexToUintn(StringGuid);\r
-    StringGuid += 5;\r
-    Guid->Data3 = (UINT16)StrHexToUintn(StringGuid);\r
-    StringGuid += 5;\r
-    Guid->Data4[0] = (UINT8)(HexCharToUintn(StringGuid[0]) * 16);\r
-    Guid->Data4[0] = (UINT8)(Guid->Data4[0]+ (UINT8)HexCharToUintn(StringGuid[1]));\r
-    StringGuid += 2;\r
-    Guid->Data4[1] = (UINT8)(HexCharToUintn(StringGuid[0]) * 16);\r
-    Guid->Data4[1] = (UINT8)(Guid->Data4[1] + (UINT8)HexCharToUintn(StringGuid[1]));\r
-    StringGuid += 2;\r
-    Guid->Data4[2] = (UINT8)(HexCharToUintn(StringGuid[0]) * 16);\r
-    Guid->Data4[2] = (UINT8)(Guid->Data4[2] + (UINT8)HexCharToUintn(StringGuid[1]));\r
-    StringGuid += 2;\r
-    Guid->Data4[3] = (UINT8)(HexCharToUintn(StringGuid[0]) * 16);\r
-    Guid->Data4[3] = (UINT8)(Guid->Data4[3] + (UINT8)HexCharToUintn(StringGuid[1]));\r
-    StringGuid += 2;\r
-    Guid->Data4[4] = (UINT8)(HexCharToUintn(StringGuid[0]) * 16);\r
-    Guid->Data4[4] = (UINT8)(Guid->Data4[4] + (UINT8)HexCharToUintn(StringGuid[1]));\r
-    StringGuid += 2;\r
-    Guid->Data4[5] = (UINT8)(HexCharToUintn(StringGuid[0]) * 16);\r
-    Guid->Data4[5] = (UINT8)(Guid->Data4[5] + (UINT8)HexCharToUintn(StringGuid[1]));\r
-    StringGuid += 2;\r
-    Guid->Data4[6] = (UINT8)(HexCharToUintn(StringGuid[0]) * 16);\r
-    Guid->Data4[6] = (UINT8)(Guid->Data4[6] + (UINT8)HexCharToUintn(StringGuid[1]));\r
-    StringGuid += 2;\r
-    Guid->Data4[7] = (UINT8)(HexCharToUintn(StringGuid[0]) * 16);\r
-    Guid->Data4[7] = (UINT8)(Guid->Data4[7] = (UINT8)HexCharToUintn(StringGuid[1]));\r
-    return (EFI_SUCCESS);\r
   }\r
-}\r
 \r
+  //\r
+  // try to open the file\r
+  //\r
+  Status = ShellOpenFileByName (FileName, &FileHandle, EFI_FILE_MODE_READ, 0);\r
+\r
+  if (!EFI_ERROR(Status)) {\r
+    ASSERT(CreateFile == FALSE);\r
+    if (FileHandle == NULL) {\r
+      return EFI_LOAD_ERROR;\r
+    }\r
+\r
+    Info = ShellGetFileInfo(FileHandle);\r
+    \r
+    if (Info->Attribute & EFI_FILE_DIRECTORY) {\r
+      FreePool (Info);\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
+\r
+    if (Info->Attribute & EFI_FILE_READ_ONLY) {\r
+      *ReadOnly = TRUE;\r
+    } else {\r
+      *ReadOnly = FALSE;\r
+    }\r
+    //\r
+    // get file size\r
+    //\r
+    FileSize = (UINTN) Info->FileSize;\r
+\r
+    FreePool (Info);\r
+  } else if (Status == EFI_NOT_FOUND) {\r
+    //\r
+    // file not exists.  add create and try again\r
+    //\r
+    Status = ShellOpenFileByName (FileName, &FileHandle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE|EFI_FILE_MODE_CREATE, 0);\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    } else {\r
+      //\r
+      // it worked.  now delete it and move on with the name (now validated)\r
+      //\r
+      Status = ShellDeleteFile (&FileHandle);\r
+      if (Status == EFI_WARN_DELETE_FAILURE) {\r
+        Status = EFI_ACCESS_DENIED;\r
+      }\r
+      if (EFI_ERROR (Status)) {\r
+        return Status;\r
+      }\r
+    }\r
+    //\r
+    // file doesn't exist, so set CreateFile to TRUE and can't be read-only\r
+    //\r
+    CreateFile = TRUE;\r
+    *ReadOnly  = FALSE;\r
+  }\r
+\r
+  //\r
+  // the file exists\r
+  //\r
+  if (!CreateFile) {\r
+    //\r
+    // allocate buffer to read file\r
+    //\r
+    InternalBuffer = AllocateZeroPool (FileSize);\r
+    if (InternalBuffer == NULL) {\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+    //\r
+    // read file into InternalBuffer\r
+    //\r
+    Status = ShellReadFile (FileHandle, &FileSize, InternalBuffer);\r
+    ShellCloseFile(&FileHandle);\r
+    FileHandle = NULL;\r
+    if (EFI_ERROR (Status)) {\r
+      SHELL_FREE_NON_NULL (InternalBuffer);\r
+      return EFI_LOAD_ERROR;\r
+    }\r
+  }\r
+  *Buffer = InternalBuffer;\r
+  if (BufferSize != NULL) {\r
+    *BufferSize = FileSize;\r
+  }\r
+  return (EFI_SUCCESS);\r
+\r
+}\r