The patch changes the behavior to not zero out file content
when the file size is not multiple of block size.
Instead, it just provides access to the contents that are
multiple of block size and leaves the remaining content (less than
block size) untouched.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Ruiyu Ni <[email protected]>
Cc: Hao Wu <[email protected]>
Cc: Andrew Fish <[email protected]>
---
 EmulatorPkg/Win/Host/WinBlockIo.c | 28 +++++++++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)

diff --git a/EmulatorPkg/Win/Host/WinBlockIo.c 
b/EmulatorPkg/Win/Host/WinBlockIo.c
index 14491a6e90..7df7d42c7c 100644
--- a/EmulatorPkg/Win/Host/WinBlockIo.c
+++ b/EmulatorPkg/Win/Host/WinBlockIo.c
@@ -90,6 +90,7 @@ WinNtBlockIoOpenDevice (
 {
   EFI_STATUS            Status;
   UINT64                FileSize;
+  UINT64                EndOfFile;
 
   //
   // If the device is already opened, close it
@@ -112,7 +113,7 @@ WinNtBlockIoOpenDevice (
   );
 
   if (Private->NtHandle == INVALID_HANDLE_VALUE) {
-    DEBUG ((EFI_D_INFO, "OpenBlock: Could not open %S, %x\n", 
Private->FileName, GetLastError ()));
+    DEBUG ((EFI_D_INFO, "PlOpenBlock: Could not open %S, %x\n", 
Private->FileName, GetLastError ()));
     Media->MediaPresent = FALSE;
     Status = EFI_NO_MEDIA;
     goto Done;
@@ -124,14 +125,35 @@ WinNtBlockIoOpenDevice (
   Status = SetFilePointer64 (Private, 0, &FileSize, FILE_END);
 
   if (EFI_ERROR (Status)) {
-    DEBUG ((EFI_D_ERROR, "OpenBlock: Could not get filesize of %s\n", 
Private->FileName));
+    DEBUG ((EFI_D_ERROR, "PlOpenBlock: Could not get filesize of %s\n", 
Private->FileName));
     Status = EFI_UNSUPPORTED;
     goto Done;
   }
 
   Media->LastBlock = DivU64x32 (FileSize, (UINT32)Private->BlockSize) - 1;
 
-  DEBUG ((EFI_D_INIT, "OpenBlock: opened %S\n", Private->FileName));
+  EndOfFile = MultU64x32 (Media->LastBlock + 1, (UINT32)Private->BlockSize);
+
+  if (FileSize != EndOfFile) {
+    //
+    // file is not the proper size, change it
+    //
+    DEBUG ((EFI_D_INIT, "PlOpenBlock: Initializing block device: %hs\n", 
Private->FileName));
+
+    //
+    // first set it to 0
+    //
+    SetFilePointer64 (Private, 0, NULL, FILE_BEGIN);
+    SetEndOfFile (Private->NtHandle);
+
+    //
+    // then set it to the needed file size (OS will zero fill it)
+    //
+    SetFilePointer64 (Private, EndOfFile, NULL, FILE_BEGIN);
+    SetEndOfFile (Private->NtHandle);
+  }
+
+  DEBUG ((EFI_D_INIT, "PlOpenBlock: opened %S\n", Private->FileName));
   Status = EFI_SUCCESS;
 
 Done:
-- 
2.16.1.windows.1

_______________________________________________
edk2-devel mailing list
[email protected]
https://lists.01.org/mailman/listinfo/edk2-devel

Reply via email to