From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 192.55.52.136, mailfrom: zhiguang.liu@intel.com) Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by groups.io with SMTP; Wed, 22 May 2019 21:59:20 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 22 May 2019 21:59:19 -0700 X-ExtLoop1: 1 Received: from fieedk002.ccr.corp.intel.com ([10.239.157.133]) by fmsmga008.fm.intel.com with ESMTP; 22 May 2019 21:59:18 -0700 From: "Zhiguang Liu" To: devel@edk2.groups.io Cc: Jordan Justen , Andrew Fish , Ray Ni Subject: [PATCH] EmulatorPkg: Fix bugs about BlockIo2 Date: Thu, 23 May 2019 12:59:04 +0800 Message-Id: <20190523045905.17208-1-zhiguang.liu@intel.com> X-Mailer: git-send-email 2.21.0.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Cc: Jordan Justen Cc: Andrew Fish Cc: Ray Ni --- 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