[edk2-devel] [PATCH] EmulatorPkg: Fix bugs about BlockIo2

Zhiguang Liu posted 1 patch 4 years, 10 months ago
Failed in applying to current master (apply log)
EmulatorPkg/EmuBlockIoDxe/EmuBlockIo.c | 13 ++++++++++---
EmulatorPkg/Win/Host/WinBlockIo.c      | 41 +++++++++++++++++++++++++++++++++++------
2 files changed, 45 insertions(+), 9 deletions(-)
[edk2-devel] [PATCH] EmulatorPkg: Fix bugs about BlockIo2
Posted by Zhiguang Liu 4 years, 10 months ago
REF:https://bugzilla.tianocore.org/show_bug.cgi?id=1760

Signal BlockIO2 callback event manually
Add some checks before readfile/writefile function.

Signed-off-by: Zhiguang Liu <zhiguang.liu@intel.com>

Cc: Jordan Justen <jordan.l.justen@intel.com>

Cc: Andrew Fish <afish@apple.com>

Cc: Ray Ni <ray.ni@intel.com>
---
 EmulatorPkg/EmuBlockIoDxe/EmuBlockIo.c | 13 ++++++++++---
 EmulatorPkg/Win/Host/WinBlockIo.c      | 41 +++++++++++++++++++++++++++++++++++------
 2 files changed, 45 insertions(+), 9 deletions(-)

diff --git a/EmulatorPkg/EmuBlockIoDxe/EmuBlockIo.c b/EmulatorPkg/EmuBlockIoDxe/EmuBlockIo.c
index 96424a82ae..b275d908c7 100644
--- a/EmulatorPkg/EmuBlockIoDxe/EmuBlockIo.c
+++ b/EmulatorPkg/EmuBlockIoDxe/EmuBlockIo.c
@@ -95,7 +95,11 @@ EmuBlockIo2ReadBlocksEx (
   OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
 
   Status = Private->Io->ReadBlocks (Private->Io, MediaId, LBA, Token, BufferSize, Buffer);
-
+  if (Token != NULL && Token->Event != NULL) {
+    if (!EFI_ERROR (Status)) {
+      gBS->SignalEvent (Token->Event);
+    }
+  }
   gBS->RestoreTPL (OldTpl);
   return Status;
 }
@@ -150,9 +154,12 @@ EmuBlockIo2WriteBlocksEx (
   Private = EMU_BLOCK_IO2_PRIVATE_DATA_FROM_THIS (This);
 
   OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
-
   Status = Private->Io->WriteBlocks (Private->Io, MediaId, LBA, Token, BufferSize, Buffer);
-
+  if (Token != NULL && Token->Event != NULL) {
+    if (!EFI_ERROR (Status)) {
+      gBS->SignalEvent (Token->Event);
+    }
+  }
   gBS->RestoreTPL (OldTpl);
   return Status;
 }
diff --git a/EmulatorPkg/Win/Host/WinBlockIo.c b/EmulatorPkg/Win/Host/WinBlockIo.c
index 5ccd17388e..8941ff1b17 100644
--- a/EmulatorPkg/Win/Host/WinBlockIo.c
+++ b/EmulatorPkg/Win/Host/WinBlockIo.c
@@ -300,9 +300,24 @@ WinNtBlockIoReadBlocks (
   DWORD                   BytesRead;
   UINT64                  DistanceToMove;
   UINT64                  DistanceMoved;
-
+  UINT64                  LastBlock;
   Private = WIN_NT_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This);
-
+  if (!Private->Media->MediaPresent) {
+    return EFI_NO_MEDIA;
+  }
+  if (Private->Media->MediaId != MediaId) {
+    return EFI_MEDIA_CHANGED;
+  }
+  if ((UINTN) Buffer % Private->Media->IoAlign != 0) {
+    return EFI_INVALID_PARAMETER;
+  }
+  if ((BufferSize % Private->BlockSize) != 0) {
+    return EFI_BAD_BUFFER_SIZE;
+  }
+  LastBlock = Lba + (BufferSize / Private->BlockSize);
+  if (LastBlock > Private->Media->LastBlock+1) {
+    return EFI_INVALID_PARAMETER;
+  }
   //
   // Seek to proper position
   //
@@ -371,9 +386,24 @@ WinNtBlockIoWriteBlocks (
   EFI_STATUS              Status;
   UINT64                  DistanceToMove;
   UINT64                  DistanceMoved;
-
+  UINT64                  LastBlock;
   Private = WIN_NT_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This);
-
+  if (!Private->Media->MediaPresent) {
+    return EFI_NO_MEDIA;
+  }
+  if (Private->Media->MediaId != MediaId) {
+    return EFI_MEDIA_CHANGED;
+  }
+  if ((UINTN) Buffer % Private->Media->IoAlign != 0) {
+    return EFI_INVALID_PARAMETER;
+  }
+  if ((BufferSize % Private->BlockSize) != 0) {
+    return EFI_BAD_BUFFER_SIZE;
+  }
+  LastBlock = Lba + (BufferSize / Private->BlockSize);
+  if (LastBlock > Private->Media->LastBlock+1) {
+    return EFI_INVALID_PARAMETER;
+  }
   //
   // Seek to proper position
   //
@@ -450,14 +480,13 @@ WinNtBlockIoReset (
   )
 {
   WIN_NT_BLOCK_IO_PRIVATE *Private;
-
   Private = WIN_NT_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This);
 
   if (Private->NtHandle != INVALID_HANDLE_VALUE) {
     CloseHandle (Private->NtHandle);
     Private->NtHandle = INVALID_HANDLE_VALUE;
   }
-
+  WinNtBlockIoCreateMapping (This, Private->Media);
   return EFI_SUCCESS;
 }
 
-- 
2.21.0.windows.1


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.

View/Reply Online (#41265): https://edk2.groups.io/g/devel/message/41265
Mute This Topic: https://groups.io/mt/31728072/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub  [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-