]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellDebug1CommandsLib/HexEdit/FileImage.c
ShellPkg: Clean up source files
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / HexEdit / FileImage.c
index fd6387a20a9a78e98e8817dc696259c859a6d245..9f9e54c598f1b96da6f3cf4bdd5d81113be320db 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Functions to deal with file buffer.\r
 \r
-  Copyright (c) 2005 - 2011, Intel Corporation. All rights reserved. <BR>\r
+  Copyright (c) 2005 - 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
@@ -35,26 +35,15 @@ HEFI_EDITOR_BUFFER_IMAGE          HFileImageConst = {
   FALSE\r
 };\r
 \r
+/**\r
+  Initialization function for HFileImage\r
+\r
+  @retval EFI_SUCCESS     The operation was successful.\r
+**/\r
 EFI_STATUS\r
 HFileImageInit (\r
   VOID\r
   )\r
-/*++\r
-\r
-Routine Description: \r
-\r
-  Initialization function for HFileImage\r
-\r
-Arguments:  \r
-\r
-  None\r
-\r
-Returns:  \r
-\r
-  EFI_SUCCESS\r
-  EFI_LOAD_ERROR\r
-\r
---*/\r
 {\r
   //\r
   // basically initialize the HFileImage\r
@@ -70,29 +59,17 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Backup function for HFileImage. Only a few fields need to be backup.\r
+  This is for making the file buffer refresh as few as possible.\r
+\r
+  @retval EFI_SUCCESS           The operation was successful.\r
+  @retval EFI_OUT_OF_RESOURCES  A memory allocation failed.\r
+**/\r
 EFI_STATUS\r
 HFileImageBackup (\r
   VOID\r
   )\r
-/*++\r
-\r
-Routine Description: \r
-\r
-  Backup function for HFileImage\r
-  Only a few fields need to be backup. \r
-  This is for making the file buffer refresh \r
-  as few as possible.\r
-\r
-Arguments:  \r
-\r
-  None\r
-\r
-Returns:  \r
-\r
-  EFI_SUCCESS\r
-  EFI_OUT_OF_RESOURCES\r
-\r
---*/\r
 {\r
   SHELL_FREE_NON_NULL (HFileImageBackupVar.FileName);\r
   HFileImageBackupVar.FileName = CatSPrint(NULL, L"%s", HFileImage.FileName);\r
@@ -103,25 +80,15 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Cleanup function for HFileImage.\r
+\r
+  @retval EFI_SUCCESS           The operation was successful.\r
+**/\r
 EFI_STATUS\r
 HFileImageCleanup (\r
   VOID\r
   )\r
-/*++\r
-\r
-Routine Description: \r
-\r
-  Cleanup function for HFileImage\r
-\r
-Arguments:  \r
-\r
-  None\r
-\r
-Returns:  \r
-\r
-  EFI_SUCCESS\r
-\r
---*/\r
 {\r
 \r
   SHELL_FREE_NON_NULL (HFileImage.FileName);\r
@@ -130,124 +97,53 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Set FileName field in HFileImage\r
+\r
+  @param[in] Str  File name to set.\r
+\r
+  @retval EFI_SUCCESS           The operation was successful.\r
+  @retval EFI_OUT_OF_RESOURCES  A memory allocation failed.\r
+**/\r
 EFI_STATUS\r
 HFileImageSetFileName (\r
   IN CONST CHAR16 *Str\r
   )\r
-/*++\r
-\r
-Routine Description: \r
-\r
-  Set FileName field in HFileImage\r
-\r
-Arguments:  \r
-\r
-  Str -- File name to set\r
-\r
-Returns:  \r
-\r
-  EFI_SUCCESS\r
-  EFI_OUT_OF_RESOURCES\r
-\r
---*/\r
 {\r
-  UINTN Size;\r
-  UINTN Index;\r
-\r
+  if (Str == HFileImage.FileName) {\r
+    //\r
+    // This function might be called using HFileImage.FileName as Str.\r
+    // Directly return without updating HFileImage.FileName.\r
+    //\r
+    return EFI_SUCCESS;\r
+  }\r
   //\r
   // free the old file name\r
   //\r
   SHELL_FREE_NON_NULL (HFileImage.FileName);\r
-\r
-  Size                = StrLen (Str);\r
-\r
-  HFileImage.FileName = AllocateZeroPool (2 * (Size + 1));\r
+  HFileImage.FileName = AllocateCopyPool (StrSize (Str), Str);\r
   if (HFileImage.FileName == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
-  for (Index = 0; Index < Size; Index++) {\r
-    HFileImage.FileName[Index] = Str[Index];\r
-  }\r
-\r
-  HFileImage.FileName[Size] = L'\0';\r
-\r
   return EFI_SUCCESS;\r
 }\r
 \r
-EFI_STATUS\r
-HFileImageGetFileInfo (\r
-  IN  EFI_FILE_HANDLE Handle,\r
-  IN  CHAR16          *FileName,\r
-  OUT EFI_FILE_INFO   **InfoOut\r
-  )\r
-/*++\r
-\r
-Routine Description: \r
-\r
-  Get this file's information\r
-\r
-Arguments:  \r
-\r
-  Handle   - in NT32 mode Directory handle, in other mode File Handle\r
-  FileName - The file name\r
-  InfoOut  - parameter to pass file information out\r
-\r
-Returns:  \r
-\r
-  EFI_SUCCESS\r
-  EFI_OUT_OF_RESOURCES\r
-  EFI_LOAD_ERROR\r
-\r
---*/\r
-{\r
-\r
-  VOID        *Info;\r
-  UINTN       Size;\r
-  EFI_STATUS  Status;\r
-\r
-  Size  = SIZE_OF_EFI_FILE_INFO + 1024;\r
-  Info  = AllocateZeroPool (Size);\r
-  if (!Info) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-  //\r
-  // get file information\r
-  //\r
-  Status = Handle->GetInfo (Handle, &gEfiFileInfoGuid, &Size, Info);\r
-  if (EFI_ERROR (Status)) {\r
-    return EFI_LOAD_ERROR;\r
-  }\r
-\r
-  *InfoOut = (EFI_FILE_INFO *) Info;\r
+/**\r
+  Read a file from disk into HBufferImage.\r
 \r
-  return EFI_SUCCESS;\r
-\r
-}\r
+  @param[in] FileName     filename to read.\r
+  @param[in] Recover      if is for recover, no information print.\r
 \r
+  @retval EFI_SUCCESS           The operation was successful.\r
+  @retval EFI_OUT_OF_RESOURCES  A memory allocation failed.\r
+  @retval EFI_LOAD_ERROR        A load error occured.\r
+**/\r
 EFI_STATUS\r
 HFileImageRead (\r
   IN CONST CHAR16  *FileName,\r
   IN BOOLEAN Recover\r
   )\r
-/*++\r
-\r
-Routine Description: \r
-\r
-  Read a file from disk into HBufferImage\r
-\r
-Arguments:  \r
-\r
-  FileName -- filename to read\r
-  Recover -- if is for recover, no information print\r
-\r
-Returns:  \r
-\r
-  EFI_SUCCESS\r
-  EFI_LOAD_ERROR\r
-  EFI_OUT_OF_RESOURCES\r
-  \r
---*/\r
 {\r
   HEFI_EDITOR_LINE                *Line;\r
   UINT8                           *Buffer;\r
@@ -268,8 +164,11 @@ Returns:
   // you should set the status string\r
   //\r
   Status = ReadFileIntoBuffer (FileName, (VOID**)&Buffer, &HFileImage.Size, &HFileImage.ReadOnly);\r
-  if (EFI_ERROR(Status)) {\r
-    UnicodeBuffer = CatSPrint(NULL, L"Read error on file &s: %r", FileName, Status);\r
+  //\r
+  // NULL pointer is only also a failure for a non-zero file size.\r
+  //\r
+  if ((EFI_ERROR(Status)) || (Buffer == NULL && HFileImage.Size != 0)) {\r
+    UnicodeBuffer = CatSPrint(NULL, L"Read error on file %s: %r", FileName, Status);\r
     if (UnicodeBuffer == NULL) {\r
       SHELL_FREE_NON_NULL(Buffer);\r
       return EFI_OUT_OF_RESOURCES;\r
@@ -277,6 +176,7 @@ Returns:
 \r
     StatusBarSetStatusString (UnicodeBuffer);\r
     FreePool (UnicodeBuffer);\r
+    return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
   HFileImageSetFileName (FileName);\r
@@ -343,27 +243,19 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Save lines in HBufferImage to disk.\r
+\r
+  @param[in] FileName     The file name.\r
+\r
+  @retval EFI_SUCCESS           The operation was successful.\r
+  @retval EFI_OUT_OF_RESOURCES  A memory allocation failed.\r
+  @retval EFI_LOAD_ERROR        A load error occured.\r
+**/\r
 EFI_STATUS\r
 HFileImageSave (\r
   IN CHAR16 *FileName\r
   )\r
-/*++\r
-\r
-Routine Description: \r
-\r
-  Save lines in HBufferImage to disk\r
-\r
-Arguments:  \r
-\r
-  FileName - The file name\r
-\r
-Returns:  \r
-\r
-  EFI_SUCCESS\r
-  EFI_LOAD_ERROR\r
-  EFI_OUT_OF_RESOURCES\r
-\r
---*/\r
 {\r
 \r
   LIST_ENTRY                      *Link;\r
@@ -489,7 +381,8 @@ Returns:
   //\r
   // now everything is ready , you can set the new file name to filebuffer\r
   //\r
-  if (BufferTypeBackup != FileTypeFileBuffer || StringNoCaseCompare (&FileName, &HFileImage.FileName) != 0) {\r
+  if ((BufferTypeBackup != FileTypeFileBuffer && FileName != NULL) ||\r
+     (FileName != NULL && HFileImage.FileName != NULL && StringNoCaseCompare (&FileName, &HFileImage.FileName) != 0)){\r
     //\r
     // not the same\r
     //\r