2007-03-27 17:47:10 +00:00
|
|
|
/*
|
|
|
|
* CiderPress
|
|
|
|
* Copyright (C) 2007 by faddenSoft, LLC. All Rights Reserved.
|
|
|
|
* See the file LICENSE for distribution terms.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* ASPI I/O functions.
|
|
|
|
*
|
|
|
|
* Some notes on ASPI stuff:
|
2014-11-04 00:26:53 +00:00
|
|
|
* - The Nero ASPI provides an interface for IDE hard drives. It also
|
|
|
|
* throws in a couple of mystery devices on a host adapter at the end.
|
|
|
|
* It has "unknown" device type and doesn't respond to SCSI device
|
|
|
|
* inquiries, so it's easy to ignore.
|
|
|
|
* - The Win98 generic ASPI only finds CD-ROM drives on the IDE bus.
|
2007-03-27 17:47:10 +00:00
|
|
|
*/
|
|
|
|
#include "StdAfx.h"
|
Large set of changes to restore CiderPress build.
CiderPress and MDC now compile, and execute far enough to open
their respective "about" boxes, but I doubt they'll do much
more than that.
* Switch from MBCS to UNICODE APIs
Microsoft switched to UTF-16 (by way of UCS-2) a long time ago,
and the support for MBCS seems to be getting phased out. So it's
time to switch to wide strings.
This is a bit awkward for CiderPress because it works with disk
and file archives with 8-bit filenames, and I want NufxLib and
DiskImgLib to continue to work on Linux (which has largely taken
the UTF-8 approach to Unicode). The libraries will continue to
work with 8-bit filenames, with CiderPress/MDC doing the
conversion at the appropriate point.
There were a couple of places where strings from a structure
handed back by one of the libraries were used directly in the UI,
or vice-versa, which is a problem because we have nowhere to
store the result of the conversion. These currently have fixed
place-holder "xyzzy" strings.
All UI strings are now wide.
Various format strings now use "%ls" and "%hs" to explicitly
specify wide and narrow. This doesn't play well with gcc, so
only the Windows-specific parts use those.
* Various updates to vcxproj files
The project-file conversion had some cruft that is now largely
gone. The build now has a common output directory for the EXEs
and libraries, avoiding the old post-build copy steps.
* Added zlib 1.2.8 and nufxlib 2.2.2 source snapshots
The old "prebuilts" directory is now gone. The libraries are now
built as part of building the apps.
I added a minimal set of files for zlib, and a full set for nufxlib.
The Linux-specific nufxlib goodies are included for the benefit of
the Linux utilities, which are currently broken (don't build).
* Replace symbols used for include guards
Symbols with a leading "__" are reserved.
2014-11-10 23:32:55 +00:00
|
|
|
#if defined(_WIN32) && defined (WANT_ASPI)
|
2007-03-27 17:47:10 +00:00
|
|
|
|
|
|
|
#include "DiskImgPriv.h"
|
|
|
|
#include "SCSIDefs.h"
|
|
|
|
#include "CP_wnaspi32.h"
|
|
|
|
#include "ASPI.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize ASPI.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
DIError ASPI::Init(void)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
DWORD aspiStatus;
|
|
|
|
static const char* kASPIDllName = "wnaspi32.dll";
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to load the DLL.
|
|
|
|
*/
|
|
|
|
fhASPI = ::LoadLibrary(kASPIDllName);
|
2014-11-18 05:13:13 +00:00
|
|
|
if (fhASPI == NULL) {
|
2014-11-04 00:26:53 +00:00
|
|
|
DWORD lastErr = ::GetLastError();
|
|
|
|
if (lastErr == ERROR_MOD_NOT_FOUND) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("ASPI DLL '%s' not found", kASPIDllName);
|
2014-11-04 00:26:53 +00:00
|
|
|
} else {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("ASPI LoadLibrary(%s) failed (err=%ld)",
|
2014-11-04 00:26:53 +00:00
|
|
|
kASPIDllName, GetLastError());
|
|
|
|
}
|
|
|
|
return kDIErrGeneric;
|
|
|
|
}
|
|
|
|
|
|
|
|
GetASPI32SupportInfo = (DWORD(*)(void))::GetProcAddress(fhASPI, "GetASPI32SupportInfo");
|
|
|
|
SendASPI32Command = (DWORD(*)(LPSRB))::GetProcAddress(fhASPI, "SendASPI32Command");
|
|
|
|
GetASPI32DLLVersion = (DWORD(*)(void))::GetProcAddress(fhASPI, "GetASPI32DLLVersion");
|
2014-11-18 05:13:13 +00:00
|
|
|
if (GetASPI32SupportInfo == NULL || SendASPI32Command == NULL) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("ASPI functions not found in dll");
|
2014-11-04 00:26:53 +00:00
|
|
|
::FreeLibrary(fhASPI);
|
2014-11-18 05:13:13 +00:00
|
|
|
fhASPI = NULL;
|
2014-11-04 00:26:53 +00:00
|
|
|
return kDIErrGeneric;
|
|
|
|
}
|
|
|
|
|
2014-11-18 05:13:13 +00:00
|
|
|
if (GetASPI32DLLVersion != NULL) {
|
2014-11-04 00:26:53 +00:00
|
|
|
fASPIVersion = GetASPI32DLLVersion();
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" ASPI version is %d.%d.%d.%d",
|
2014-11-04 00:26:53 +00:00
|
|
|
fASPIVersion & 0x0ff,
|
|
|
|
(fASPIVersion >> 8) & 0xff,
|
|
|
|
(fASPIVersion >> 16) & 0xff,
|
|
|
|
(fASPIVersion >> 24) & 0xff);
|
|
|
|
} else {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("ASPI WARNING: couldn't find GetASPI32DLLVersion interface");
|
2014-11-04 00:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Successfully loaded the library. Start it up and see if it works.
|
|
|
|
*/
|
|
|
|
aspiStatus = GetASPI32SupportInfo();
|
|
|
|
if (HIBYTE(LOWORD(aspiStatus)) != SS_COMP) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("ASPI loaded but not working (status=%d)",
|
2014-11-04 00:26:53 +00:00
|
|
|
HIBYTE(LOWORD(aspiStatus)));
|
|
|
|
::FreeLibrary(fhASPI);
|
2014-11-18 05:13:13 +00:00
|
|
|
fhASPI = NULL;
|
2014-11-04 00:26:53 +00:00
|
|
|
return kDIErrASPIFailure;
|
|
|
|
}
|
|
|
|
|
|
|
|
fHostAdapterCount = LOBYTE(LOWORD(aspiStatus));
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("ASPI loaded successfully, hostAdapterCount=%d",
|
2014-11-04 00:26:53 +00:00
|
|
|
fHostAdapterCount);
|
|
|
|
|
|
|
|
return kDIErrNone;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Destructor. Unload the ASPI DLL.
|
|
|
|
*/
|
|
|
|
ASPI::~ASPI(void)
|
|
|
|
{
|
2014-11-18 05:13:13 +00:00
|
|
|
if (fhASPI != NULL) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("Unloading ASPI DLL");
|
2014-11-04 00:26:53 +00:00
|
|
|
::FreeLibrary(fhASPI);
|
2014-11-18 05:13:13 +00:00
|
|
|
fhASPI = NULL;
|
2014-11-04 00:26:53 +00:00
|
|
|
}
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Issue an ASPI host adapter inquiry request for the specified adapter.
|
|
|
|
*
|
|
|
|
* Pass in a pointer to a struct that receives the result.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
DIError ASPI::HostAdapterInquiry(unsigned char adapter, AdapterInfo* pAdapterInfo)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
SRB_HAInquiry req;
|
|
|
|
DWORD result;
|
|
|
|
|
|
|
|
assert(adapter >= 0 && adapter < kMaxAdapters);
|
|
|
|
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
req.SRB_Cmd = SC_HA_INQUIRY;
|
|
|
|
req.SRB_HaId = adapter;
|
|
|
|
|
|
|
|
result = SendASPI32Command(&req);
|
|
|
|
if (result != SS_COMP) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("ASPI(SC_HA_INQUIRY on %d) failed with result=0x%lx",
|
2014-11-04 00:26:53 +00:00
|
|
|
adapter, result);
|
|
|
|
return kDIErrASPIFailure;
|
|
|
|
}
|
|
|
|
|
|
|
|
pAdapterInfo->adapterScsiID = req.HA_SCSI_ID;
|
|
|
|
memcpy(pAdapterInfo->managerID, req.HA_ManagerId,
|
|
|
|
sizeof(pAdapterInfo->managerID)-1);
|
|
|
|
pAdapterInfo->managerID[sizeof(pAdapterInfo->managerID)-1] = '\0';
|
|
|
|
memcpy(pAdapterInfo->identifier, req.HA_Identifier,
|
|
|
|
sizeof(pAdapterInfo->identifier)-1);
|
|
|
|
pAdapterInfo->identifier[sizeof(pAdapterInfo->identifier)-1] = '\0';
|
|
|
|
pAdapterInfo->maxTargets = req.HA_Unique[3];
|
|
|
|
pAdapterInfo->bufferAlignment =
|
|
|
|
(unsigned short) req.HA_Unique[1] << 8 | req.HA_Unique[0];
|
|
|
|
|
|
|
|
return kDIErrNone;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Issue an ASPI query on device type.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
DIError ASPI::GetDeviceType(unsigned char adapter, unsigned char target,
|
2014-11-04 00:26:53 +00:00
|
|
|
unsigned char lun, unsigned char* pType)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
SRB_GDEVBlock req;
|
|
|
|
DWORD result;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
assert(adapter >= 0 && adapter < kMaxAdapters);
|
|
|
|
assert(target >= 0 && target < kMaxTargets);
|
|
|
|
assert(lun >= 0 && lun < kMaxLuns);
|
2014-11-18 05:13:13 +00:00
|
|
|
assert(pType != NULL);
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
req.SRB_Cmd = SC_GET_DEV_TYPE;
|
|
|
|
req.SRB_HaId = adapter;
|
|
|
|
req.SRB_Target = target;
|
|
|
|
req.SRB_Lun = lun;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
result = SendASPI32Command(&req);
|
|
|
|
if (result != SS_COMP)
|
|
|
|
return kDIErrASPIFailure;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
*pType = req.SRB_DeviceType;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
return kDIErrNone;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a printable string for the given device type.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
const char* ASPI::DeviceTypeToString(unsigned char deviceType)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
switch (deviceType) {
|
|
|
|
case kScsiDevTypeDASD: return "Disk device";
|
|
|
|
case kScsiDevTypeSEQD: return "Tape device";
|
|
|
|
case kScsiDevTypePRNT: return "Printer";
|
|
|
|
case kScsiDevTypePROC: return "Processor";
|
|
|
|
case kScsiDevTypeWORM: return "Write-once read-multiple";
|
|
|
|
case kScsiDevTypeCDROM: return "CD-ROM device";
|
|
|
|
case kScsiDevTypeSCAN: return "Scanner device";
|
|
|
|
case kScsiDevTypeOPTI: return "Optical memory device";
|
|
|
|
case kScsiDevTypeJUKE: return "Medium changer device";
|
|
|
|
case kScsiDevTypeCOMM: return "Communications device";
|
|
|
|
case kScsiDevTypeUNKNOWN: return "Unknown or no device type";
|
|
|
|
default: return "Invalid type";
|
|
|
|
}
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Issue a SCSI device inquiry and return the interesting parts.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
DIError ASPI::DeviceInquiry(unsigned char adapter, unsigned char target,
|
2014-11-04 00:26:53 +00:00
|
|
|
unsigned char lun, Inquiry* pInquiry)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
DIError dierr;
|
|
|
|
SRB_ExecSCSICmd srb;
|
|
|
|
CDB6Inquiry* pCDB;
|
|
|
|
unsigned char buf[96]; // enough to hold everything of interest, and more
|
|
|
|
CDB_InquiryData* pInqData = (CDB_InquiryData*) buf;
|
|
|
|
|
|
|
|
assert(sizeof(CDB6Inquiry) == 6);
|
|
|
|
|
|
|
|
memset(&srb, 0, sizeof(srb));
|
|
|
|
srb.SRB_Cmd = SC_EXEC_SCSI_CMD;
|
|
|
|
srb.SRB_HaId = adapter;
|
|
|
|
srb.SRB_Target = target;
|
|
|
|
srb.SRB_Lun = lun;
|
|
|
|
srb.SRB_Flags = SRB_DIR_IN;
|
|
|
|
srb.SRB_BufLen = sizeof(buf);
|
|
|
|
srb.SRB_BufPointer = buf;
|
|
|
|
srb.SRB_SenseLen = SENSE_LEN;
|
|
|
|
srb.SRB_CDBLen = sizeof(*pCDB);
|
|
|
|
|
|
|
|
pCDB = (CDB6Inquiry*) srb.CDBByte;
|
|
|
|
pCDB->operationCode = kScsiOpInquiry;
|
|
|
|
pCDB->allocationLength = sizeof(buf);
|
|
|
|
|
|
|
|
// Don't set pCDB->logicalUnitNumber. It's only there for SCSI-1
|
|
|
|
// devices. SCSI-2 uses an IDENTIFY command; I gather ASPI is doing
|
|
|
|
// this for us.
|
|
|
|
|
|
|
|
dierr = ExecSCSICommand(&srb);
|
|
|
|
if (dierr != kDIErrNone)
|
|
|
|
return dierr;
|
|
|
|
|
|
|
|
memcpy(pInquiry->vendorID, pInqData->vendorId,
|
|
|
|
sizeof(pInquiry->vendorID)-1);
|
|
|
|
pInquiry->vendorID[sizeof(pInquiry->vendorID)-1] = '\0';
|
|
|
|
memcpy(pInquiry->productID, pInqData->productId,
|
|
|
|
sizeof(pInquiry->productID)-1);
|
|
|
|
pInquiry->productID[sizeof(pInquiry->productID)-1] = '\0';
|
|
|
|
pInquiry->productRevision[0] = pInqData->productRevisionLevel[0];
|
|
|
|
pInquiry->productRevision[1] = pInqData->productRevisionLevel[1];
|
|
|
|
pInquiry->productRevision[2] = pInqData->productRevisionLevel[2];
|
|
|
|
pInquiry->productRevision[3] = pInqData->productRevisionLevel[3];
|
|
|
|
|
|
|
|
return kDIErrNone;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the capacity of a SCSI block device.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
DIError ASPI::GetDeviceCapacity(unsigned char adapter, unsigned char target,
|
2014-11-04 00:26:53 +00:00
|
|
|
unsigned char lun, unsigned long* pLastBlock, unsigned long* pBlockSize)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
DIError dierr;
|
|
|
|
SRB_ExecSCSICmd srb;
|
|
|
|
CDB10* pCDB;
|
|
|
|
CDB_ReadCapacityData dataBuf;
|
|
|
|
|
|
|
|
assert(sizeof(dataBuf) == 8); // READ CAPACITY returns two longs
|
|
|
|
assert(sizeof(CDB10) == 10);
|
|
|
|
|
|
|
|
memset(&srb, 0, sizeof(srb));
|
|
|
|
srb.SRB_Cmd = SC_EXEC_SCSI_CMD;
|
|
|
|
srb.SRB_HaId = adapter;
|
|
|
|
srb.SRB_Target = target;
|
|
|
|
srb.SRB_Lun = lun;
|
|
|
|
srb.SRB_Flags = SRB_DIR_IN;
|
|
|
|
srb.SRB_BufLen = sizeof(dataBuf);
|
|
|
|
srb.SRB_BufPointer = (unsigned char*)&dataBuf;
|
|
|
|
srb.SRB_SenseLen = SENSE_LEN;
|
|
|
|
srb.SRB_CDBLen = sizeof(*pCDB);
|
|
|
|
|
|
|
|
pCDB = (CDB10*) srb.CDBByte;
|
|
|
|
pCDB->operationCode = kScsiOpReadCapacity;
|
|
|
|
// rest of CDB is zero
|
|
|
|
|
|
|
|
dierr = ExecSCSICommand(&srb);
|
|
|
|
if (dierr != kDIErrNone)
|
|
|
|
return dierr;
|
|
|
|
|
|
|
|
*pLastBlock =
|
|
|
|
(unsigned long) dataBuf.logicalBlockAddr0 << 24 |
|
|
|
|
(unsigned long) dataBuf.logicalBlockAddr1 << 16 |
|
|
|
|
(unsigned long) dataBuf.logicalBlockAddr2 << 8 |
|
|
|
|
(unsigned long) dataBuf.logicalBlockAddr3;
|
|
|
|
*pBlockSize =
|
|
|
|
(unsigned long) dataBuf.bytesPerBlock0 << 24 |
|
|
|
|
(unsigned long) dataBuf.bytesPerBlock1 << 16 |
|
|
|
|
(unsigned long) dataBuf.bytesPerBlock2 << 8 |
|
|
|
|
(unsigned long) dataBuf.bytesPerBlock3;
|
|
|
|
return kDIErrNone;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test to see if a device is ready.
|
|
|
|
*
|
|
|
|
* Returns "true" if the device is ready, "false" if not.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
DIError ASPI::TestUnitReady(unsigned char adapter, unsigned char target,
|
2014-11-04 00:26:53 +00:00
|
|
|
unsigned char lun, bool* pReady)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
DIError dierr;
|
|
|
|
SRB_ExecSCSICmd srb;
|
|
|
|
CDB6* pCDB;
|
|
|
|
|
|
|
|
assert(sizeof(CDB6) == 6);
|
|
|
|
|
|
|
|
memset(&srb, 0, sizeof(srb));
|
|
|
|
srb.SRB_Cmd = SC_EXEC_SCSI_CMD;
|
|
|
|
srb.SRB_HaId = adapter;
|
|
|
|
srb.SRB_Target = target;
|
|
|
|
srb.SRB_Lun = lun;
|
|
|
|
srb.SRB_Flags = 0; //SRB_DIR_IN;
|
|
|
|
srb.SRB_BufLen = 0;
|
2014-11-18 05:13:13 +00:00
|
|
|
srb.SRB_BufPointer = NULL;
|
2014-11-04 00:26:53 +00:00
|
|
|
srb.SRB_SenseLen = SENSE_LEN;
|
|
|
|
srb.SRB_CDBLen = sizeof(*pCDB);
|
|
|
|
|
|
|
|
pCDB = (CDB6*) srb.CDBByte;
|
|
|
|
pCDB->operationCode = kScsiOpTestUnitReady;
|
|
|
|
// rest of CDB is zero
|
|
|
|
|
|
|
|
dierr = ExecSCSICommand(&srb);
|
|
|
|
if (dierr != kDIErrNone) {
|
|
|
|
const CDB_SenseData* pSense = (const CDB_SenseData*) srb.SenseArea;
|
|
|
|
|
|
|
|
if (srb.SRB_TargStat == kScsiStatCheckCondition &&
|
|
|
|
pSense->senseKey == kScsiSenseNotReady)
|
|
|
|
{
|
|
|
|
// expect pSense->additionalSenseCode to be
|
|
|
|
// kScsiAdSenseNoMediaInDevice; no need to check it really.
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" ASPI TestUnitReady: drive %d:%d:%d is NOT ready",
|
2014-11-04 00:26:53 +00:00
|
|
|
adapter, target, lun);
|
|
|
|
} else {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" ASPI TestUnitReady failed, status=0x%02x sense=0x%02x ASC=0x%02x",
|
2014-11-04 00:26:53 +00:00
|
|
|
srb.SRB_TargStat, pSense->senseKey,
|
|
|
|
pSense->additionalSenseCode);
|
|
|
|
}
|
|
|
|
*pReady = false;
|
|
|
|
} else {
|
|
|
|
const CDB_SenseData* pSense = (const CDB_SenseData*) srb.SenseArea;
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" ASPI TestUnitReady: drive %d:%d:%d is ready",
|
2014-11-04 00:26:53 +00:00
|
|
|
adapter, target, lun);
|
2014-11-18 21:05:15 +00:00
|
|
|
//LOGI(" status=0x%02x sense=0x%02x ASC=0x%02x",
|
2014-11-04 00:26:53 +00:00
|
|
|
// srb.SRB_TargStat, pSense->senseKey, pSense->additionalSenseCode);
|
|
|
|
*pReady = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return kDIErrNone;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read one or more blocks from the device.
|
|
|
|
*
|
|
|
|
* The block size is going to be whatever the device's native size is
|
|
|
|
* (possibly modified by extents, but we'll ignore that). For a CD-ROM
|
|
|
|
* this means 2048-byte blocks.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
DIError ASPI::ReadBlocks(unsigned char adapter, unsigned char target,
|
2014-11-04 00:26:53 +00:00
|
|
|
unsigned char lun, long startBlock, short numBlocks, long blockSize,
|
|
|
|
void* buf)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
SRB_ExecSCSICmd srb;
|
|
|
|
CDB10* pCDB;
|
|
|
|
|
2014-11-18 21:05:15 +00:00
|
|
|
//LOGI(" ASPI ReadBlocks start=%ld num=%d (size=%d)",
|
2014-11-04 00:26:53 +00:00
|
|
|
// startBlock, numBlocks, blockSize);
|
|
|
|
|
|
|
|
assert(sizeof(CDB10) == 10);
|
|
|
|
assert(startBlock >= 0);
|
|
|
|
assert(numBlocks > 0);
|
2014-11-18 05:13:13 +00:00
|
|
|
assert(buf != NULL);
|
2014-11-04 00:26:53 +00:00
|
|
|
|
|
|
|
memset(&srb, 0, sizeof(srb));
|
|
|
|
srb.SRB_Cmd = SC_EXEC_SCSI_CMD;
|
|
|
|
srb.SRB_HaId = adapter;
|
|
|
|
srb.SRB_Target = target;
|
|
|
|
srb.SRB_Lun = lun;
|
|
|
|
srb.SRB_Flags = SRB_DIR_IN;
|
|
|
|
srb.SRB_BufLen = numBlocks * blockSize;
|
|
|
|
srb.SRB_BufPointer = (unsigned char*)buf;
|
|
|
|
srb.SRB_SenseLen = SENSE_LEN;
|
|
|
|
srb.SRB_CDBLen = sizeof(*pCDB);
|
|
|
|
|
|
|
|
pCDB = (CDB10*) srb.CDBByte;
|
|
|
|
pCDB->operationCode = kScsiOpRead;
|
|
|
|
pCDB->logicalBlockAddr0 = (unsigned char) (startBlock >> 24); // MSB
|
|
|
|
pCDB->logicalBlockAddr1 = (unsigned char) (startBlock >> 16);
|
|
|
|
pCDB->logicalBlockAddr2 = (unsigned char) (startBlock >> 8);
|
|
|
|
pCDB->logicalBlockAddr3 = (unsigned char) startBlock; // LSB
|
|
|
|
pCDB->transferLength0 = (unsigned char) (numBlocks >> 8); // MSB
|
|
|
|
pCDB->transferLength1 = (unsigned char) numBlocks; // LSB
|
|
|
|
|
|
|
|
return ExecSCSICommand(&srb);
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write one or more blocks to the device.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
DIError ASPI::WriteBlocks(unsigned char adapter, unsigned char target,
|
2014-11-04 00:26:53 +00:00
|
|
|
unsigned char lun, long startBlock, short numBlocks, long blockSize,
|
|
|
|
const void* buf)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
SRB_ExecSCSICmd srb;
|
|
|
|
CDB10* pCDB;
|
|
|
|
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" ASPI WriteBlocks start=%ld num=%d (size=%d)",
|
2014-11-04 00:26:53 +00:00
|
|
|
startBlock, numBlocks, blockSize);
|
|
|
|
|
|
|
|
assert(sizeof(CDB10) == 10);
|
|
|
|
assert(startBlock >= 0);
|
|
|
|
assert(numBlocks > 0);
|
2014-11-18 05:13:13 +00:00
|
|
|
assert(buf != NULL);
|
2014-11-04 00:26:53 +00:00
|
|
|
|
|
|
|
memset(&srb, 0, sizeof(srb));
|
|
|
|
srb.SRB_Cmd = SC_EXEC_SCSI_CMD;
|
|
|
|
srb.SRB_HaId = adapter;
|
|
|
|
srb.SRB_Target = target;
|
|
|
|
srb.SRB_Lun = lun;
|
|
|
|
srb.SRB_Flags = SRB_DIR_IN;
|
|
|
|
srb.SRB_BufLen = numBlocks * blockSize;
|
|
|
|
srb.SRB_BufPointer = (unsigned char*)buf;
|
|
|
|
srb.SRB_SenseLen = SENSE_LEN;
|
|
|
|
srb.SRB_CDBLen = sizeof(*pCDB);
|
|
|
|
|
|
|
|
pCDB = (CDB10*) srb.CDBByte;
|
|
|
|
pCDB->operationCode = kScsiOpWrite;
|
|
|
|
pCDB->logicalBlockAddr0 = (unsigned char) (startBlock >> 24); // MSB
|
|
|
|
pCDB->logicalBlockAddr1 = (unsigned char) (startBlock >> 16);
|
|
|
|
pCDB->logicalBlockAddr2 = (unsigned char) (startBlock >> 8);
|
|
|
|
pCDB->logicalBlockAddr3 = (unsigned char) startBlock; // LSB
|
|
|
|
pCDB->transferLength0 = (unsigned char) (numBlocks >> 8); // MSB
|
|
|
|
pCDB->transferLength1 = (unsigned char) numBlocks; // LSB
|
|
|
|
|
|
|
|
return ExecSCSICommand(&srb);
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Execute a SCSI command.
|
|
|
|
*
|
|
|
|
* Returns an error if ASPI reports an error or the SCSI status isn't
|
|
|
|
* kScsiStatGood.
|
|
|
|
*
|
|
|
|
* The Nero ASPI layer typically returns immediately, and hands back an
|
|
|
|
* SS_ERR when something fails. Win98 ASPI does the SS_PENDING thang.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
DIError ASPI::ExecSCSICommand(SRB_ExecSCSICmd* pSRB)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-18 05:13:13 +00:00
|
|
|
HANDLE completionEvent = NULL;
|
2014-11-04 00:26:53 +00:00
|
|
|
DWORD eventStatus;
|
|
|
|
DWORD aspiStatus;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
assert(pSRB->SRB_Cmd == SC_EXEC_SCSI_CMD);
|
|
|
|
assert(pSRB->SRB_Flags == SRB_DIR_IN ||
|
|
|
|
pSRB->SRB_Flags == SRB_DIR_OUT ||
|
|
|
|
pSRB->SRB_Flags == 0);
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
/*
|
|
|
|
* Set up event-waiting stuff, as described in the Adaptec ASPI docs.
|
|
|
|
*/
|
|
|
|
pSRB->SRB_Flags |= SRB_EVENT_NOTIFY;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
|
|
|
completionEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
|
2014-11-18 05:13:13 +00:00
|
|
|
if (completionEvent == NULL) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("Failed creating a completion event?");
|
2014-11-04 00:26:53 +00:00
|
|
|
return kDIErrGeneric;
|
|
|
|
}
|
|
|
|
|
|
|
|
pSRB->SRB_PostProc = completionEvent;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send the request.
|
|
|
|
*/
|
|
|
|
(void)SendASPI32Command((LPSRB) pSRB);
|
|
|
|
aspiStatus = pSRB->SRB_Status;
|
|
|
|
if (aspiStatus == SS_PENDING) {
|
2014-11-18 21:05:15 +00:00
|
|
|
//LOGI(" (waiting for completion)");
|
2014-11-04 00:26:53 +00:00
|
|
|
eventStatus = ::WaitForSingleObject(completionEvent, kTimeout * 1000);
|
|
|
|
|
|
|
|
::CloseHandle(completionEvent);
|
|
|
|
|
|
|
|
if (eventStatus == WAIT_TIMEOUT) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" ASPI exec timed out!");
|
2014-11-04 00:26:53 +00:00
|
|
|
return kDIErrSCSIFailure;
|
|
|
|
} else if (eventStatus != WAIT_OBJECT_0) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" ASPI exec returned weird wait state %ld", eventStatus);
|
2014-11-04 00:26:53 +00:00
|
|
|
return kDIErrGeneric;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check the final status.
|
|
|
|
*/
|
|
|
|
aspiStatus = pSRB->SRB_Status;
|
|
|
|
|
|
|
|
if (aspiStatus == SS_COMP) {
|
|
|
|
/* success! */
|
|
|
|
} else if (aspiStatus == SS_ERR) {
|
|
|
|
const CDB_SenseData* pSense = (const CDB_SenseData*) pSRB->SenseArea;
|
|
|
|
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" ASPI SCSI command 0x%02x failed: scsiStatus=0x%02x"
|
2014-11-04 00:26:53 +00:00
|
|
|
" senseKey=0x%02x ASC=0x%02x\n",
|
|
|
|
pSRB->CDBByte[0], pSRB->SRB_TargStat,
|
|
|
|
pSense->senseKey, pSense->additionalSenseCode);
|
|
|
|
return kDIErrSCSIFailure;
|
|
|
|
} else {
|
|
|
|
// SS_ABORTED, SS_ABORT_FAIL, SS_NO_DEVICE, ...
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" ASPI failed on command 0x%02x: aspiStatus=%d scsiStatus=%d",
|
2014-11-04 00:26:53 +00:00
|
|
|
pSRB->CDBByte[0], aspiStatus, pSRB->SRB_TargStat);
|
|
|
|
return kDIErrASPIFailure;
|
|
|
|
}
|
|
|
|
|
|
|
|
return kDIErrNone;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return an array of accessible devices we found.
|
|
|
|
*
|
|
|
|
* Only return the devices matching device types in "deviceMask".
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
DIError ASPI::GetAccessibleDevices(int deviceMask, ASPIDevice** ppDeviceArray,
|
2014-11-04 00:26:53 +00:00
|
|
|
int* pNumDevices)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
DIError dierr;
|
2014-11-18 05:13:13 +00:00
|
|
|
ASPIDevice* deviceArray = NULL;
|
2014-11-04 00:26:53 +00:00
|
|
|
int idx = 0;
|
|
|
|
|
|
|
|
assert(deviceMask != 0);
|
|
|
|
assert((deviceMask & ~(kDevMaskCDROM | kDevMaskHardDrive)) == 0);
|
2014-11-18 05:13:13 +00:00
|
|
|
assert(ppDeviceArray != NULL);
|
|
|
|
assert(pNumDevices != NULL);
|
2014-11-04 00:26:53 +00:00
|
|
|
|
|
|
|
deviceArray = new ASPIDevice[kMaxAccessibleDrives];
|
2014-11-18 05:13:13 +00:00
|
|
|
if (deviceArray == NULL)
|
2014-11-04 00:26:53 +00:00
|
|
|
return kDIErrMalloc;
|
|
|
|
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("ASPI scanning %d host adapters", fHostAdapterCount);
|
2014-11-04 00:26:53 +00:00
|
|
|
|
|
|
|
for (int ha = 0; ha < fHostAdapterCount; ha++) {
|
|
|
|
AdapterInfo adi;
|
|
|
|
|
|
|
|
dierr = HostAdapterInquiry(ha, &adi);
|
|
|
|
if (dierr != kDIErrNone) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" ASPI inquiry on %d failed", ha);
|
2014-11-04 00:26:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" ASPI host adapter %d (SCSI ID=%d)", ha, adi.adapterScsiID);
|
|
|
|
LOGI(" identifier='%s' managerID='%s'",
|
2014-11-04 00:26:53 +00:00
|
|
|
adi.identifier, adi.managerID);
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" maxTargets=%d bufferAlignment=%d",
|
2014-11-04 00:26:53 +00:00
|
|
|
adi.maxTargets, adi.bufferAlignment);
|
|
|
|
|
|
|
|
int maxTargets = adi.maxTargets;
|
|
|
|
if (!maxTargets) {
|
|
|
|
/* Win98 ASPI reports zero here for ATAPI */
|
|
|
|
maxTargets = 8;
|
|
|
|
}
|
|
|
|
if (maxTargets > kMaxTargets)
|
|
|
|
maxTargets = kMaxTargets;
|
|
|
|
for (int targ = 0; targ < maxTargets; targ++) {
|
|
|
|
for (int lun = 0; lun < kMaxLuns; lun++) {
|
|
|
|
Inquiry inq;
|
|
|
|
unsigned char deviceType;
|
|
|
|
char addrString[48];
|
|
|
|
bool deviceReady;
|
|
|
|
|
|
|
|
dierr = GetDeviceType(ha, targ, lun, &deviceType);
|
|
|
|
if (dierr != kDIErrNone)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
sprintf(addrString, "%d:%d:%d", ha, targ, lun);
|
|
|
|
|
|
|
|
dierr = DeviceInquiry(ha, targ, lun, &inq);
|
|
|
|
if (dierr != kDIErrNone) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" ASPI DeviceInquiry for '%s' (type=%d) failed",
|
2014-11-04 00:26:53 +00:00
|
|
|
addrString, deviceType);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" Device %s is %s '%s' '%s'",
|
2014-11-04 00:26:53 +00:00
|
|
|
addrString, DeviceTypeToString(deviceType),
|
|
|
|
inq.vendorID, inq.productID);
|
|
|
|
|
|
|
|
if ((deviceMask & kDevMaskCDROM) != 0 &&
|
|
|
|
deviceType == kScsiDevTypeCDROM)
|
|
|
|
{
|
|
|
|
/* found CD-ROM */
|
|
|
|
} else if ((deviceMask & kDevMaskHardDrive) != 0 &&
|
|
|
|
deviceType == kScsiDevTypeDASD)
|
|
|
|
{
|
|
|
|
/* found hard drive */
|
|
|
|
} else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (idx >= kMaxAccessibleDrives) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("GLITCH: ran out of places to stuff CD-ROM drives");
|
2014-11-04 00:26:53 +00:00
|
|
|
assert(false);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
dierr = TestUnitReady(ha, targ, lun, &deviceReady);
|
|
|
|
if (dierr != kDIErrNone) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" ASPI TestUnitReady for '%s' failed", addrString);
|
2014-11-04 00:26:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
deviceArray[idx].Init(ha, targ, lun, inq.vendorID,
|
|
|
|
inq.productID, deviceType, deviceReady);
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-03-27 17:47:10 +00:00
|
|
|
|
|
|
|
done:
|
2014-11-04 00:26:53 +00:00
|
|
|
*ppDeviceArray = deviceArray;
|
|
|
|
*pNumDevices = idx;
|
|
|
|
return kDIErrNone;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /*_WIN32*/
|