📄 block.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
Module Name:
block.cpp
Abstract:
Block-based SCSI-2 direct-access device (hard disk drive) emulator.
--*/
//------------------------------------------------------------------------------
//
// Copyright (C) 2005-2006, Freescale Semiconductor, Inc. All Rights Reserved.
// THIS SOURCE CODE, AND ITS USE AND DISTRIBUTION, IS SUBJECT TO THE TERMS
// AND CONDITIONS OF THE APPLICABLE LICENSE AGREEMENT
//
//------------------------------------------------------------------------------
#include <windows.h>
#include <diskio.h>
#include <storemgr.h>
#include <devload.h>
#include "proxy.h"
#include "scsi2.h"
#include "transporttypes.h"
#include "usbmsfndbg.h"
#include <pshpack1.h>
//#undef DEBUGMSG
//#define DEBUGMSG(a,b) RETAILMSG(1,b)
typedef struct _UFI_CB {
BYTE bOpCode; // 0
BYTE bReserved1:5; // 1
BYTE bLogicalUnitNumber : 3;
DWORD dwLogicalBlockAddress; // 2-5
BYTE bReserved2; // 6
WORD wTransferLength; // 7-8
BYTE bReserved3[3]; // 9-11
} UFI_CB, *PUFI_CB;
#include <poppack.h>
#if DEBUG
#define DUMP_DISKINFO(di) { \
DEBUGMSG(1, (_T("%s bytes per sector = %u\r\n"), pszFname, di.di_bytes_per_sect)); \
DEBUGMSG(1, (_T("%s cylinders = %u\r\n"), pszFname, di.di_cylinders)); \
DEBUGMSG(1, (_T("%s flags = 0x%x\r\n"), pszFname, di.di_flags)); \
DEBUGMSG(1, (_T("%s heads = %u\r\n"), pszFname, di.di_heads)); \
DEBUGMSG(1, (_T("%s sectors = %u\r\n"), pszFname, di.di_sectors)); \
DEBUGMSG(1, (_T("%s total sectors = %u\r\n"), pszFname, di.di_total_sectors)); \
}
#define DUMP_PARTENTRY(ppe) { \
DEBUGMSG(1, (_T("%s bootind=0x%x \r\n"), pszFname, ppe->Part_BootInd)); \
DEBUGMSG(1, (_T("%s filesystem=0x%x \r\n"), pszFname, ppe->Part_FileSystem)); \
DEBUGMSG(1, (_T("%s firsthead=%u\r\n"), pszFname, ppe->Part_FirstHead)); \
DEBUGMSG(1, (_T("%s firstsector=%u\r\n"), pszFname, ppe->Part_FirstSector)); \
DEBUGMSG(1, (_T("%s firsttrack=%u\r\n"), pszFname, ppe->Part_FirstTrack)); \
DEBUGMSG(1, (_T("%s lasthead=%u\r\n"), pszFname, ppe->Part_LastHead)); \
DEBUGMSG(1, (_T("%s lastsector=%u\r\n"), pszFname, ppe->Part_LastSector)); \
DEBUGMSG(1, (_T("%s lasttrack=%u\r\n"), pszFname, ppe->Part_LastTrack)); \
DEBUGMSG(1, (_T("%s startsector=%u\r\n"), pszFname, ppe->Part_StartSector)); \
DEBUGMSG(1, (_T("%s totalsectors=%u\r\n"), pszFname, ppe->Part_TotalSectors)); \
}
#else
#define DUMP_DISKINFO(di)
#define DUMP_PARTENTRY(pe)
#endif // DEBUG
// Helpful macros
#define CLOSE_KEY(x) if (x != NULL) RegCloseKey(x)
#define LOCAL_ALLOC(x) if (x != NULL) LocalAlloc(x)
#define LOCAL_FREE(x) if (x != NULL) LocalFree(x)
#define CLOSE_HANDLE(x) if (x != NULL) CloseHandle(x)
// SCSI-2 command data size definitions
#define DATASIZE_INQUIRY 36
#define DATASIZE_MODE_SENSE6 8
#define DATASIZE_MODE_SENSE10 512
#define DATASIZE_REQUEST_SENSE 18
// SCSI-2 direct-access device state
static const UCHAR g_bSenseKey = 0;
static const UCHAR g_bASC = 0;
static const UCHAR g_bASCQ = 0;
static BOOL g_fInitialized = FALSE;
// Properties of exposed device
static HANDLE g_hStore;
static DISK_INFO g_diDiskInfo;
static TCHAR g_szDeviceName[PRX_DEVICE_NAME_LEN];
static TCHAR g_szDeviceName2[PRX_DEVICE_NAME_LEN];
static BOOL g_fLegacyBlockDriver = FALSE; // IOCTL_DISK_Xxx or DISK_IOCTL_Xxx
// Whether to report to USB host that exposed device is removable
static DWORD g_dwRemovable = 0;
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
#define PART_PRIMARY 0x00 // Part_BootInd stores the partition type; see PARTENTRY
#define PART_EXTENDED 0x05 // Part_FileSystem stores the file system type; see PARTENTRY
// Partition table entry
typedef struct _PARTENTRY {
BYTE Part_BootInd; // Boot index (80h = boot partition)
BYTE Part_FirstHead; // Partition starting head based 0
BYTE Part_FirstSector; // Partition starting sector based 1
BYTE Part_FirstTrack; // Partition starting track based 0
BYTE Part_FileSystem; // Partition type signature field
BYTE Part_LastHead; // Partition ending head based 0
BYTE Part_LastSector; // Partition ending sector based 1
BYTE Part_LastTrack; // Partition ending track based 0
DWORD Part_StartSector; // Physical starting sector based 0
DWORD Part_TotalSectors; // Total physical sectors in partition
} PARTENTRY;
typedef PARTENTRY UNALIGNED *PPARTENTRY;
#define DEFAULT_SECTOR_SIZE 512
#define MAX_PARTTABLE_ENTRIES 4
#define SIZE_PARTTABLE_ENTRIES 16
#define PARTTABLE_OFFSET (DEFAULT_SECTOR_SIZE - 2 - (SIZE_PARTTABLE_ENTRIES * MAX_PARTTABLE_ENTRIES))
typedef struct _PARTTABLE {
PARTENTRY PartEntry[MAX_PARTTABLE_ENTRIES];
} PARTTABLE;
typedef PARTTABLE UNALIGNED *PPARTTABLE;
// List of partition names
static PTCHAR g_ptcPartitions = NULL;
// Copy of physical disk's MBR (to facilitate write-protection); it is possible
// for a host to attempt to reorganize a disk while only a subset of its
// partitions are exposed; unexposed partitions appear as unallocated space;
// we need a copy of the actual MBR to determine the sector range of an
// unexposed partition, to protect unexposed partitions from modification
static LPBYTE g_lpbPhysMBR = NULL;
// Virtual disk's MBR; this is the modified copy of the MBR that we advertise to
// the USB host when the disk is only exposing a subset of its partitions; the
// modified MBR contains "holes" where unexposed partitions exist
static LPBYTE g_lpbMBR = NULL;
// Bit mask of primary partitions selected for exposure; bit 0 = primary 1
// selected, bit 1 = primary 2 selected, etc.
static BYTE g_bmPartitions = 0;
// ----------------------------------------------------------------------------
// Function: BytesPerSector
// Return number of bytes per sector on exposed device
//
// Parameters:
// None
// ----------------------------------------------------------------------------
static
DWORD
inline
BytesPerSector(
)
{
DEBUGCHK(g_fInitialized);
return g_diDiskInfo.di_bytes_per_sect;
}
// ----------------------------------------------------------------------------
// Function: ByteSwapUlong
// Same as _byteswap_ulong
//
// Parameters:
// ULONG to swap
// ----------------------------------------------------------------------------
static
inline
ULONG
ByteSwapUlong(
ULONG ul
)
{
PBYTE pb = (PBYTE)&ul;
ULONG ulRet = pb[3];
ulRet |= pb[2] << 8;
ulRet |= pb[1] << 16;
ulRet |= pb[0] << 24;
return ulRet;
}
// ----------------------------------------------------------------------------
// Function: ByteSwapUshort
// Same as _byteswap_ushort
//
// Parameters:
// USHORT to swap
// ----------------------------------------------------------------------------
static
inline
USHORT
ByteSwapUshort(
USHORT us
)
{
PBYTE pb = (PBYTE)&us;
USHORT usRet = pb[1];
usRet |= pb[0] << 8;
return usRet;
}
// ----------------------------------------------------------------------------
// Function: ProcessScsiInquiry
// Process a SCSI-2 INQURY command
//
// Parameters:
// ptcCommand - command block wrapper
// ptdData - data block wrapper
// ----------------------------------------------------------------------------
static
DWORD
ProcessScsiInquiry(
PTRANSPORT_COMMAND ptcCommand,
PTRANSPORT_DATA ptdData
)
{
SETFNAME(_T("ProcessScsiInquiry"));
PBYTE pbCommand = (PBYTE) ptcCommand->CommandBlock;
PBYTE pbData = (PBYTE) ptdData->DataBlock;
DWORD dwResult = EXECUTE_FAIL;
// don't test if LUN is valid; LUNs are deprecated
// test if data block is large enough
if (ptdData->RequestLength < DATASIZE_INQUIRY) {
DEBUGMSG(ZONE_ERROR, (_T(
"%s host requesting less than required; ptdData->RequestLength < DATASIZE_INQUIRY\r\n"
), pszFname));
goto EXIT;
}
ZeroMemory(pbData, ptdData->RequestLength);
ptdData->TransferLength = ptdData->RequestLength;
// peripherial qualifier = 0x0
// peripherial device type = 0x0 (SCSI_DEVICE_DIRECT_ACCESS)
// RMB = 0x0 (non-removable medium)
pbData[1] = ((g_dwRemovable ? 1 : 0) << 7); // RMB = 1; removable medium
dwResult = EXECUTE_PASS;
EXIT:;
return dwResult;
}
// ----------------------------------------------------------------------------
// Function: ProcessScsiModeSense6
// Process a SCSI-2 MODE SENSE (6) command
//
// Parameters:
// ptcCommand - command block wrapper
// ptdData - data block wrapper
// ----------------------------------------------------------------------------
static
DWORD
ProcessScsiModeSense6(
PTRANSPORT_COMMAND ptcCommand,
PTRANSPORT_DATA ptdData
)
{
SETFNAME(_T("ProcessScsiModeSense6"));
PBYTE pbCommand = (PBYTE) ptcCommand->CommandBlock;
PBYTE pbData = (PBYTE) ptdData->DataBlock;
DWORD dwResult = EXECUTE_FAIL;
// don't test if LUN is valid; LUNs are deprecated
// test if data block is large enough
if (ptdData->RequestLength < DATASIZE_MODE_SENSE6) {
DEBUGMSG(ZONE_ERROR, (_T(
"%s host requesting less than required; ptdData->RequestLength < DATASIZE_MODE_SENSE6\r\n"
), pszFname));
goto EXIT;
}
ZeroMemory(pbData, ptdData->RequestLength);
ptdData->TransferLength = ptdData->RequestLength;
// mode data length
pbData[0] = 0; // msb
pbData[1] = 6; // lsb
// medium type, 9.3.3
pbData[2] = 0x00; // default medium type
// device-specific parameter, 9.3.3
pbData[3] = 0x00; // bit 7 = WP (write protected)
dwResult = EXECUTE_PASS;
EXIT:;
return dwResult;
}
// ----------------------------------------------------------------------------
// Function: ProcessScsiModeSense10
// Process a SCSI-2 MODE SENSE (10) command
//
// Parameters:
// ptcCommand - command block wrapper
// ptdData - data block wrapper
// ----------------------------------------------------------------------------
static
DWORD
ProcessScsiModeSense10(
PTRANSPORT_COMMAND ptcCommand,
PTRANSPORT_DATA ptdData
)
{
SETFNAME(_T("ProcessScsiModeSense10"));
PBYTE pbCommand = (PBYTE) ptcCommand->CommandBlock;
PBYTE pbData = (PBYTE) ptdData->DataBlock;
DWORD dwResult = EXECUTE_FAIL;
// don't test if LUN is valid; LUNs are deprecated
// test if data block is large enough
if (ptdData->RequestLength < DATASIZE_MODE_SENSE10) {
DEBUGMSG(ZONE_ERROR, (_T(
"%s host requesting less than required; ptdData->RequestLength < DATASIZE_MODE_SENSE10\r\n"
), pszFname));
goto EXIT;
}
// test if DBD (disable block descriptors) bit is high
if (!(pbCommand[1] & 0x08)) {
DEBUGMSG(ZONE_ERROR, (_T("%s DBD is not enabled\r\n"), pszFname));
goto EXIT;
}
// test if PC is 0
if ((pbCommand[2] & 0xC0) != 0) {
DEBUGMSG(ZONE_ERROR, (_T("%s PC is not 0\r\n"), pszFname));
goto EXIT;
}
// test if page code is 0
if ((pbCommand[2] & 0x3F) != 0) {
DEBUGMSG(ZONE_ERROR, (_T("%s page code is not 0\r\n"), pszFname));
goto EXIT;
}
ZeroMemory(pbData, ptdData->RequestLength);
ptdData->TransferLength = ptdData->RequestLength;
// only return the header
// mode data length
pbData[0] = 0; // msb
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -