2013-04-18 03:09:55 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2014-02-21 00:47:53 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include "Common/Common.h"
|
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "DiscIO/Blob.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "DiscIO/DriveBlob.h"
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2014-02-21 00:47:53 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
#endif
|
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
namespace DiscIO
|
|
|
|
{
|
2009-02-21 23:44:40 +00:00
|
|
|
|
2010-01-11 23:28:54 +00:00
|
|
|
DriveReader::DriveReader(const char *drive)
|
|
|
|
{
|
2009-02-21 23:44:40 +00:00
|
|
|
#ifdef _WIN32
|
2010-01-11 23:28:54 +00:00
|
|
|
SectorReader::SetSectorSize(2048);
|
2013-02-28 02:04:07 +00:00
|
|
|
auto const path = UTF8ToTStr(std::string("\\\\.\\") + drive);
|
|
|
|
hDisc = CreateFile(path.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
|
2014-03-09 20:14:26 +00:00
|
|
|
nullptr, OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, nullptr);
|
2010-01-11 23:28:54 +00:00
|
|
|
if (hDisc != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
// Do a test read to make sure everything is OK, since it seems you can get
|
|
|
|
// handles to empty drives.
|
|
|
|
DWORD not_used;
|
|
|
|
u8 *buffer = new u8[m_blocksize];
|
2014-03-09 20:14:26 +00:00
|
|
|
if (!ReadFile(hDisc, buffer, m_blocksize, (LPDWORD)¬_used, nullptr))
|
2009-02-24 07:30:10 +00:00
|
|
|
{
|
2010-01-11 23:28:54 +00:00
|
|
|
delete [] buffer;
|
|
|
|
// OK, something is wrong.
|
2009-02-24 07:30:10 +00:00
|
|
|
CloseHandle(hDisc);
|
2009-02-24 19:57:29 +00:00
|
|
|
hDisc = INVALID_HANDLE_VALUE;
|
2010-01-11 23:28:54 +00:00
|
|
|
return;
|
2009-02-24 07:30:10 +00:00
|
|
|
}
|
2010-01-11 23:28:54 +00:00
|
|
|
delete [] buffer;
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2010-01-11 23:28:54 +00:00
|
|
|
#ifdef _LOCKDRIVE // Do we want to lock the drive?
|
|
|
|
// Lock the compact disc in the CD-ROM drive to prevent accidental
|
|
|
|
// removal while reading from it.
|
|
|
|
pmrLockCDROM.PreventMediaRemoval = TRUE;
|
|
|
|
DeviceIoControl(hDisc, IOCTL_CDROM_MEDIA_REMOVAL,
|
2014-03-09 20:14:26 +00:00
|
|
|
&pmrLockCDROM, sizeof(pmrLockCDROM), nullptr,
|
|
|
|
0, &dwNotUsed, nullptr);
|
2010-01-11 23:28:54 +00:00
|
|
|
#endif
|
2009-02-21 23:44:40 +00:00
|
|
|
#else
|
2010-03-17 01:54:40 +00:00
|
|
|
SectorReader::SetSectorSize(2048);
|
2011-03-11 10:21:46 +00:00
|
|
|
file_.Open(drive, "rb");
|
2010-01-11 23:28:54 +00:00
|
|
|
if (file_)
|
|
|
|
{
|
|
|
|
#endif
|
2009-02-22 16:48:54 +00:00
|
|
|
}
|
2010-01-11 23:28:54 +00:00
|
|
|
else
|
2010-07-07 19:47:45 +00:00
|
|
|
NOTICE_LOG(DISCIO, "Load from DVD backup failed or no disc in drive %s", drive);
|
2010-01-11 23:28:54 +00:00
|
|
|
} // DriveReader::DriveReader
|
2009-02-21 23:44:40 +00:00
|
|
|
|
2010-01-11 23:28:54 +00:00
|
|
|
DriveReader::~DriveReader()
|
|
|
|
{
|
2009-02-21 23:44:40 +00:00
|
|
|
#ifdef _WIN32
|
2010-01-11 23:28:54 +00:00
|
|
|
#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,
|
2014-03-09 20:14:26 +00:00
|
|
|
&pmrLockCDROM, sizeof(pmrLockCDROM), nullptr,
|
|
|
|
0, &dwNotUsed, nullptr);
|
2009-02-21 23:44:40 +00:00
|
|
|
#endif
|
2010-01-11 23:28:54 +00:00
|
|
|
if (hDisc != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
CloseHandle(hDisc);
|
|
|
|
hDisc = INVALID_HANDLE_VALUE;
|
2009-02-21 23:44:40 +00:00
|
|
|
}
|
2010-01-11 23:28:54 +00:00
|
|
|
#else
|
2011-03-11 10:21:46 +00:00
|
|
|
file_.Close();
|
2013-10-18 07:32:56 +00:00
|
|
|
#endif
|
2010-01-11 23:28:54 +00:00
|
|
|
}
|
2009-02-21 23:44:40 +00:00
|
|
|
|
2010-01-11 23:28:54 +00:00
|
|
|
DriveReader *DriveReader::Create(const char *drive)
|
|
|
|
{
|
|
|
|
DriveReader *reader = new DriveReader(drive);
|
|
|
|
if (!reader->IsOK())
|
2009-02-21 23:44:40 +00:00
|
|
|
{
|
2010-01-11 23:28:54 +00:00
|
|
|
delete reader;
|
2014-03-09 20:14:26 +00:00
|
|
|
return nullptr;
|
2010-01-11 23:28:54 +00:00
|
|
|
}
|
|
|
|
return reader;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DriveReader::GetBlock(u64 block_num, u8 *out_ptr)
|
|
|
|
{
|
2011-03-11 10:21:46 +00:00
|
|
|
u8* const lpSector = new u8[m_blocksize];
|
2009-02-22 16:48:54 +00:00
|
|
|
#ifdef _WIN32
|
2010-01-11 23:28:54 +00:00
|
|
|
u32 NotUsed;
|
|
|
|
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);
|
2014-03-09 20:14:26 +00:00
|
|
|
if (!ReadFile(hDisc, lpSector, m_blocksize, (LPDWORD)&NotUsed, nullptr))
|
2011-01-13 02:05:58 +00:00
|
|
|
PanicAlertT("Disc Read Error");
|
2009-02-22 16:48:54 +00:00
|
|
|
#else
|
2011-03-11 10:21:46 +00:00
|
|
|
file_.Seek(m_blocksize * block_num, SEEK_SET);
|
|
|
|
file_.ReadBytes(lpSector, m_blocksize);
|
2009-02-22 16:48:54 +00:00
|
|
|
#endif
|
2010-01-11 23:28:54 +00:00
|
|
|
memcpy(out_ptr, lpSector, m_blocksize);
|
|
|
|
delete[] lpSector;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DriveReader::ReadMultipleAlignedBlocks(u64 block_num, u64 num_blocks, u8 *out_ptr)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
u32 NotUsed;
|
|
|
|
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);
|
2014-03-09 20:14:26 +00:00
|
|
|
if (!ReadFile(hDisc, out_ptr, (DWORD)(m_blocksize * num_blocks), (LPDWORD)&NotUsed, nullptr))
|
2010-01-11 23:28:54 +00:00
|
|
|
{
|
2011-01-13 02:05:58 +00:00
|
|
|
PanicAlertT("Disc Read Error");
|
2010-01-11 23:28:54 +00:00
|
|
|
return false;
|
2009-02-21 23:44:40 +00:00
|
|
|
}
|
2010-01-11 23:28:54 +00:00
|
|
|
#else
|
2011-03-11 12:56:26 +00:00
|
|
|
fseeko(file_.GetHandle(), m_blocksize*block_num, SEEK_SET);
|
2014-03-10 11:30:55 +00:00
|
|
|
if (fread(out_ptr, 1, m_blocksize * num_blocks, file_.GetHandle()) != m_blocksize * num_blocks)
|
2010-01-11 23:28:54 +00:00
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
} // namespace
|