]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellDebug1CommandsLib/HexEdit/DiskImage.c
ShellPkg/Hexedit: Fix FreePool() ASSERT() when writing disk
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / HexEdit / DiskImage.c
index 140db71671247678478d3dfa7747152b3763e5df..1c93cd8958412fddd6db76dae63cb3c84f3592bb 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Functions to deal with Disk buffer.\r
 \r
-  Copyright (c) 2005 - 2011, Intel Corporation. All rights reserved. <BR>\r
+  Copyright (c) 2005 - 2016, 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
@@ -37,26 +37,16 @@ HEFI_EDITOR_DISK_IMAGE            HDiskImageConst = {
   0\r
 };\r
 \r
+/**\r
+  Initialization function for HDiskImage.\r
\r
+  @retval EFI_SUCCESS     The operation was successful.\r
+  @retval EFI_LOAD_ERROR  A load error occured.\r
+**/\r
 EFI_STATUS\r
 HDiskImageInit (\r
   VOID\r
   )\r
-/*++\r
-\r
-Routine Description: \r
-\r
-  Initialization function for HDiskImage\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 HDiskImage\r
@@ -68,29 +58,17 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Backup function for HDiskImage. Only a few fields need to be backup.   \r
+  This is for making the Disk buffer refresh as few as possible.\r
+\r
+  @retval EFI_SUCCESS           The operation was successful.\r
+  @retval EFI_OUT_OF_RESOURCES  gST->ConOut of resources.\r
+**/\r
 EFI_STATUS\r
 HDiskImageBackup (\r
   VOID\r
   )\r
-/*++\r
-\r
-Routine Description: \r
-\r
-  Backup function for HDiskImage\r
-  Only a few fields need to be backup. \r
-  This is for making the Disk buffer refresh \r
-  as few as possible.\r
-\r
-Arguments:  \r
-\r
-  None\r
-\r
-Returns:  \r
-\r
-  EFI_SUCCESS          - Success\r
-  EFI_OUT_OF_RESOURCES - gST->ConOut of resources\r
-\r
---*/\r
 {\r
   //\r
   // backup the disk name, offset and size\r
@@ -109,25 +87,15 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Cleanup function for HDiskImage.\r
+\r
+  @retval EFI_SUCCESS           The operation was successful.\r
+**/\r
 EFI_STATUS\r
 HDiskImageCleanup (\r
   VOID\r
   )\r
-/*++\r
-\r
-Routine Description: \r
-\r
-  Cleanup function for HDiskImage\r
-\r
-Arguments:  \r
-\r
-  None\r
-\r
-Returns:  \r
-\r
-  EFI_SUCCESS\r
-\r
---*/\r
 {\r
   SHELL_FREE_NON_NULL (HDiskImage.Name);\r
   SHELL_FREE_NON_NULL (HDiskImageBackupVar.Name);\r
@@ -135,6 +103,16 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Set FileName field in HFileImage.\r
+\r
+  @param[in] Str      File name to set.\r
+  @param[in] Offset   The offset.\r
+  @param[in] Size     The size.\r
+\r
+  @retval EFI_SUCCESS           The operation was successful.\r
+  @retval EFI_OUT_OF_RESOURCES  A memory allocation failed.\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 HDiskImageSetDiskNameOffsetSize (\r
@@ -142,24 +120,6 @@ HDiskImageSetDiskNameOffsetSize (
   IN UINTN    Offset,\r
   IN UINTN    Size\r
   )\r
-/*++\r
-\r
-Routine Description: \r
-\r
-  Set FileName field in HFileImage\r
-\r
-Arguments:  \r
-\r
-  Str    - File name to set\r
-  Offset - The offset\r
-  Size   - The size\r
-\r
-Returns:  \r
-\r
-  EFI_SUCCESS\r
-  EFI_OUT_OF_RESOURCES\r
-\r
---*/\r
 {\r
   UINTN Len;\r
   UINTN Index;\r
@@ -188,6 +148,19 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Read a disk from disk into HBufferImage.\r
+\r
+  @param[in] DeviceName   filename to read.\r
+  @param[in] Offset       The offset.\r
+  @param[in] Size         The size.\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
+  @retval EFI_INVALID_PARAMETER A parameter was invalid.  \r
+**/\r
 EFI_STATUS\r
 HDiskImageRead (\r
   IN CONST CHAR16   *DeviceName,\r
@@ -195,30 +168,10 @@ HDiskImageRead (
   IN UINTN    Size,\r
   IN BOOLEAN  Recover\r
   )\r
-/*++\r
-\r
-Routine Description: \r
-\r
-  Read a disk from disk into HBufferImage\r
-\r
-Arguments:  \r
-\r
-  DeviceName - filename to read\r
-  Offset     - The offset\r
-  Size       - The size\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
-  EFI_INVALID_PARAMETER \r
-  \r
---*/\r
 {\r
   CONST EFI_DEVICE_PATH_PROTOCOL  *DevicePath;\r
   EFI_DEVICE_PATH_PROTOCOL        *DupDevicePath;\r
+  EFI_DEVICE_PATH_PROTOCOL        *DupDevicePathForFree;\r
   EFI_HANDLE                      Handle;\r
   EFI_BLOCK_IO_PROTOCOL           *BlkIo;\r
   EFI_STATUS                      Status;\r
@@ -228,11 +181,7 @@ Returns:
   UINTN                           Bytes;\r
 \r
   HEFI_EDITOR_LINE                *Line;\r
-  UINT64                          ByteOffset;\r
 \r
-  EDIT_FILE_TYPE                  BufferTypeBackup;\r
-\r
-  BufferTypeBackup        = HBufferImage.BufferType;\r
   HBufferImage.BufferType = FileTypeDiskBuffer;\r
 \r
   DevicePath              = gEfiShellProtocol->GetDevicePathFromMap(DeviceName);\r
@@ -241,11 +190,12 @@ Returns:
     return EFI_INVALID_PARAMETER;\r
   }\r
   DupDevicePath = DuplicateDevicePath(DevicePath);\r
+  DupDevicePathForFree = DupDevicePath;\r
   //\r
   // get blkio interface\r
   //\r
   Status = gBS->LocateDevicePath(&gEfiBlockIoProtocolGuid,&DupDevicePath,&Handle);\r
-  FreePool(DupDevicePath);\r
+  FreePool(DupDevicePathForFree);\r
   if (EFI_ERROR (Status)) {\r
     StatusBarSetStatusString (L"Read Disk Failed");\r
     return Status;\r
@@ -272,8 +222,6 @@ Returns:
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
-  ByteOffset = MultU64x32 (Offset, BlkIo->Media->BlockSize);\r
-\r
   //\r
   // read from disk\r
   //\r
@@ -372,47 +320,36 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Save lines in HBufferImage to disk.\r
+  NOT ALLOW TO WRITE TO ANOTHER DISK!!!!!!!!!\r
+\r
+  @param[in] DeviceName   The device name.\r
+  @param[in] Offset       The offset.\r
+  @param[in] Size         The size.\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
+  @retval EFI_INVALID_PARAMETER A parameter was invalid.  \r
+**/\r
 EFI_STATUS\r
 HDiskImageSave (\r
   IN CHAR16 *DeviceName,\r
   IN UINTN  Offset,\r
   IN UINTN  Size\r
   )\r
-/*++\r
-\r
-Routine Description: \r
-\r
-  Save lines in HBufferImage to disk\r
-  NOT ALLOW TO WRITE TO ANOTHER DISK!!!!!!!!!\r
-\r
-Arguments:  \r
-\r
-  DeviceName - The device name\r
-  Offset     - The offset\r
-  Size       - The size\r
-\r
-Returns:  \r
-\r
-  EFI_SUCCESS\r
-  EFI_LOAD_ERROR\r
-  EFI_OUT_OF_RESOURCES\r
-  EFI_INVALID_PARAMETER\r
-\r
---*/\r
 {\r
 \r
   CONST EFI_DEVICE_PATH_PROTOCOL  *DevicePath;\r
   EFI_DEVICE_PATH_PROTOCOL        *DupDevicePath;\r
+  EFI_DEVICE_PATH_PROTOCOL        *DupDevicePathForFree;\r
   EFI_BLOCK_IO_PROTOCOL           *BlkIo;\r
   EFI_STATUS                      Status;\r
   EFI_HANDLE                      Handle;\r
   VOID                            *Buffer;\r
   UINTN                           Bytes;\r
 \r
-  UINT64                          ByteOffset;\r
-\r
-  EDIT_FILE_TYPE                  BufferTypeBackup;\r
-\r
   //\r
   // if not modified, directly return\r
   //\r
@@ -420,7 +357,6 @@ Returns:
     return EFI_SUCCESS;\r
   }\r
 \r
-  BufferTypeBackup        = HBufferImage.BufferType;\r
   HBufferImage.BufferType = FileTypeDiskBuffer;\r
 \r
   DevicePath              = gEfiShellProtocol->GetDevicePathFromMap(DeviceName);\r
@@ -429,12 +365,13 @@ Returns:
     return EFI_INVALID_PARAMETER;\r
   }\r
   DupDevicePath = DuplicateDevicePath(DevicePath);\r
+  DupDevicePathForFree = DupDevicePath;\r
 \r
   //\r
   // get blkio interface\r
   //\r
   Status = gBS->LocateDevicePath(&gEfiBlockIoProtocolGuid,&DupDevicePath,&Handle);\r
-  FreePool(DupDevicePath);\r
+  FreePool(DupDevicePathForFree);\r
   if (EFI_ERROR (Status)) {\r
 //    StatusBarSetStatusString (L"Read Disk Failed");\r
     return Status;\r
@@ -460,8 +397,6 @@ Returns:
     return Status;\r
   }\r
 \r
-  ByteOffset = MultU64x32 (Offset, BlkIo->Media->BlockSize);\r
-\r
   //\r
   // write the buffer to disk\r
   //\r