2009-07-28 15:32:10 -06:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "DriveBlob.h"
|
|
|
|
|
|
|
|
namespace DiscIO
|
|
|
|
{
|
2009-02-21 16:44:40 -07:00
|
|
|
DriveReader::DriveReader(const char *drive)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
char path[MAX_PATH];
|
|
|
|
strncpy(path, drive, 3);
|
|
|
|
path[2] = 0;
|
|
|
|
sprintf(path, "\\\\.\\%s", drive);
|
2009-02-24 12:57:29 -07:00
|
|
|
SectorReader::SetSectorSize(2048);
|
2009-02-21 19:39:16 -07:00
|
|
|
hDisc = CreateFile(path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
NULL, OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, NULL);
|
2009-02-24 00:30:10 -07:00
|
|
|
if (hDisc != INVALID_HANDLE_VALUE)
|
2009-02-21 16:44:40 -07:00
|
|
|
{
|
2009-02-24 12:57:29 -07:00
|
|
|
// Do a test read to make sure everything is OK, since it seems you can get
|
|
|
|
// handles to empty drives.
|
|
|
|
DWORD not_used;
|
2009-02-24 15:25:45 -07:00
|
|
|
u8 *buffer = new u8[m_blocksize];
|
|
|
|
if (!ReadFile(hDisc, buffer, m_blocksize, (LPDWORD)¬_used, NULL))
|
2009-02-24 12:57:29 -07:00
|
|
|
{
|
2009-02-24 15:25:45 -07:00
|
|
|
delete [] buffer;
|
2009-02-24 12:57:29 -07:00
|
|
|
// OK, something is wrong.
|
|
|
|
CloseHandle(hDisc);
|
|
|
|
hDisc = INVALID_HANDLE_VALUE;
|
|
|
|
return;
|
|
|
|
}
|
2009-02-24 15:25:45 -07:00
|
|
|
delete [] buffer;
|
2009-02-24 00:30:10 -07:00
|
|
|
|
|
|
|
#ifdef _LOCKDRIVE // Do we want to lock the drive?
|
2009-02-21 16:44:40 -07:00
|
|
|
// Lock the compact disc in the CD-ROM drive to prevent accidental
|
|
|
|
// removal while reading from it.
|
|
|
|
pmrLockCDROM.PreventMediaRemoval = TRUE;
|
2009-02-24 12:57:29 -07:00
|
|
|
DeviceIoControl(hDisc, IOCTL_CDROM_MEDIA_REMOVAL,
|
2009-02-21 16:44:40 -07:00
|
|
|
&pmrLockCDROM, sizeof(pmrLockCDROM), NULL,
|
|
|
|
0, &dwNotUsed, NULL);
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
file_ = fopen(drive, "rb");
|
2009-02-24 00:30:10 -07:00
|
|
|
if (file_)
|
|
|
|
{
|
2009-02-21 16:44:40 -07:00
|
|
|
#endif
|
2009-02-24 00:30:10 -07:00
|
|
|
}
|
|
|
|
else
|
2009-02-24 12:57:29 -07:00
|
|
|
{
|
|
|
|
PanicAlert("Load from DVD backup failed or no disc in drive %s", drive);
|
2009-02-24 00:30:10 -07:00
|
|
|
}
|
2009-02-21 16:44:40 -07:00
|
|
|
} // DriveReader::DriveReader
|
|
|
|
|
|
|
|
DriveReader::~DriveReader()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
#ifdef _LOCKDRIVE // Do we want to lock the drive?
|
|
|
|
// Unlock the disc in the CD-ROM drive.
|
|
|
|
pmrLockCDROM.PreventMediaRemoval = FALSE;
|
|
|
|
DeviceIoControl (hDisc, IOCTL_CDROM_MEDIA_REMOVAL,
|
|
|
|
&pmrLockCDROM, sizeof(pmrLockCDROM), NULL,
|
|
|
|
0, &dwNotUsed, NULL);
|
|
|
|
#endif
|
2009-02-24 00:30:10 -07:00
|
|
|
if (hDisc != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
CloseHandle(hDisc);
|
2009-02-24 12:57:29 -07:00
|
|
|
hDisc = INVALID_HANDLE_VALUE;
|
2009-02-24 00:30:10 -07:00
|
|
|
}
|
2009-02-21 16:44:40 -07:00
|
|
|
#else
|
|
|
|
fclose(file_);
|
2009-02-24 12:57:29 -07:00
|
|
|
file_ = 0;
|
2009-02-21 16:44:40 -07:00
|
|
|
#endif
|
2009-02-22 09:48:54 -07:00
|
|
|
}
|
2009-02-21 16:44:40 -07:00
|
|
|
|
2009-02-24 12:57:29 -07:00
|
|
|
DriveReader *DriveReader::Create(const char *drive)
|
2009-02-21 16:44:40 -07:00
|
|
|
{
|
2009-02-24 12:57:29 -07:00
|
|
|
DriveReader *reader = new DriveReader(drive);
|
|
|
|
if (!reader->IsOK())
|
|
|
|
{
|
|
|
|
delete reader;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return reader;
|
2009-02-21 16:44:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void DriveReader::GetBlock(u64 block_num, u8 *out_ptr)
|
|
|
|
{
|
|
|
|
u8 * lpSector = new u8[m_blocksize];
|
|
|
|
#ifdef _WIN32
|
2009-03-07 01:07:11 -07:00
|
|
|
u32 NotUsed;
|
2009-02-22 05:40:38 -07:00
|
|
|
u64 offset = m_blocksize * block_num;
|
|
|
|
LONG off_low = (LONG)offset & 0xFFFFFFFF;
|
|
|
|
LONG off_high = (LONG)(offset >> 32);
|
|
|
|
SetFilePointer(hDisc, off_low, &off_high, FILE_BEGIN);
|
|
|
|
if (!ReadFile(hDisc, lpSector, m_blocksize, (LPDWORD)&NotUsed, NULL))
|
2009-02-24 12:57:29 -07:00
|
|
|
PanicAlert("Disc Read Error");
|
2009-02-21 16:44:40 -07:00
|
|
|
#else
|
|
|
|
fseek(file_, m_blocksize*block_num, SEEK_SET);
|
|
|
|
fread(lpSector, 1, m_blocksize, file_);
|
|
|
|
#endif
|
|
|
|
memcpy(out_ptr, lpSector, m_blocksize);
|
2009-08-03 16:51:13 -06:00
|
|
|
delete[] lpSector;
|
2009-02-21 16:44:40 -07:00
|
|
|
}
|
|
|
|
|
2009-02-22 09:48:54 -07:00
|
|
|
bool DriveReader::ReadMultipleAlignedBlocks(u64 block_num, u64 num_blocks, u8 *out_ptr)
|
2009-02-21 16:44:40 -07:00
|
|
|
{
|
2009-02-22 09:48:54 -07:00
|
|
|
u32 NotUsed;
|
|
|
|
#ifdef _WIN32
|
|
|
|
u64 offset = m_blocksize * block_num;
|
|
|
|
LONG off_low = (LONG)offset & 0xFFFFFFFF;
|
|
|
|
LONG off_high = (LONG)(offset >> 32);
|
|
|
|
SetFilePointer(hDisc, off_low, &off_high, FILE_BEGIN);
|
|
|
|
if (!ReadFile(hDisc, out_ptr, (DWORD)(m_blocksize * num_blocks), (LPDWORD)&NotUsed, NULL))
|
2009-02-24 00:30:10 -07:00
|
|
|
{
|
2009-02-24 12:57:29 -07:00
|
|
|
PanicAlert("Disc Read Error");
|
2009-02-24 00:30:10 -07:00
|
|
|
return false;
|
|
|
|
}
|
2009-02-22 09:48:54 -07:00
|
|
|
#else
|
|
|
|
fseek(file_, m_blocksize*block_num, SEEK_SET);
|
2009-02-24 00:30:10 -07:00
|
|
|
if(fread(out_ptr, 1, m_blocksize * num_blocks, file_) != m_blocksize * num_blocks)
|
|
|
|
return false;
|
2009-02-22 09:48:54 -07:00
|
|
|
#endif
|
|
|
|
return true;
|
2009-02-21 16:44:40 -07:00
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
} // namespace
|