2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-17 17:08:10 -06:00
|
|
|
// Licensed under GPLv2+
|
2013-04-17 21:09:55 -06:00
|
|
|
// Refer to the license.txt file included.
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2015-12-07 17:53:42 -07:00
|
|
|
#include <algorithm>
|
2014-02-20 17:47:53 -07:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2015-12-06 21:15:51 -07:00
|
|
|
#include <memory>
|
2014-03-12 13:33:41 -06:00
|
|
|
#include <string>
|
2015-12-07 17:53:42 -07:00
|
|
|
#include <vector>
|
2014-02-20 17:47:53 -07:00
|
|
|
|
2014-09-07 19:06:58 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-20 17:47:53 -07:00
|
|
|
#include "Common/FileUtil.h"
|
2015-09-26 15:13:07 -06:00
|
|
|
#include "Common/MsgHandler.h"
|
|
|
|
#include "Common/Logging/Log.h"
|
2014-02-20 17:47:53 -07:00
|
|
|
#include "DiscIO/Blob.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "DiscIO/DriveBlob.h"
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2014-02-20 17:47:53 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
#endif
|
|
|
|
|
2008-12-07 22:30:24 -07:00
|
|
|
namespace DiscIO
|
|
|
|
{
|
2009-02-21 16:44:40 -07:00
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
DriveReader::DriveReader(const std::string& drive)
|
2010-01-11 16:28:54 -07:00
|
|
|
{
|
2009-02-21 16:44:40 -07:00
|
|
|
#ifdef _WIN32
|
2010-01-11 16:28:54 -07:00
|
|
|
SectorReader::SetSectorSize(2048);
|
2013-02-27 19:04:07 -07:00
|
|
|
auto const path = UTF8ToTStr(std::string("\\\\.\\") + drive);
|
2014-09-01 13:48:02 -06:00
|
|
|
m_disc_handle = CreateFile(path.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
nullptr, OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, nullptr);
|
|
|
|
if (m_disc_handle != INVALID_HANDLE_VALUE)
|
2010-01-11 16:28:54 -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;
|
2015-12-07 17:53:42 -07:00
|
|
|
std::vector<u8> buffer(m_blocksize);
|
|
|
|
if (!ReadFile(m_disc_handle, buffer.data(), m_blocksize, ¬_used, nullptr))
|
2009-02-24 00:30:10 -07:00
|
|
|
{
|
2010-01-11 16:28:54 -07:00
|
|
|
// OK, something is wrong.
|
2014-09-01 13:48:02 -06:00
|
|
|
CloseHandle(m_disc_handle);
|
|
|
|
m_disc_handle = INVALID_HANDLE_VALUE;
|
2010-01-11 16:28:54 -07:00
|
|
|
return;
|
2009-02-24 00:30:10 -07:00
|
|
|
}
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2010-01-11 16:28:54 -07: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.
|
2014-09-01 13:48:02 -06:00
|
|
|
m_lock_cdrom.PreventMediaRemoval = TRUE;
|
|
|
|
DeviceIoControl(m_disc_handle, IOCTL_CDROM_MEDIA_REMOVAL,
|
|
|
|
&m_lock_cdrom, sizeof(m_lock_cdrom), nullptr,
|
|
|
|
0, &dwNotUsed, nullptr);
|
2010-01-11 16:28:54 -07:00
|
|
|
#endif
|
2009-02-21 16:44:40 -07:00
|
|
|
#else
|
2010-03-16 19:54:40 -06:00
|
|
|
SectorReader::SetSectorSize(2048);
|
2014-09-01 13:48:02 -06:00
|
|
|
m_file.Open(drive, "rb");
|
|
|
|
if (m_file)
|
2010-01-11 16:28:54 -07:00
|
|
|
{
|
|
|
|
#endif
|
2009-02-22 09:48:54 -07:00
|
|
|
}
|
2010-01-11 16:28:54 -07:00
|
|
|
else
|
2014-03-12 13:33:41 -06:00
|
|
|
{
|
|
|
|
NOTICE_LOG(DISCIO, "Load from DVD backup failed or no disc in drive %s", drive.c_str());
|
|
|
|
}
|
|
|
|
}
|
2009-02-21 16:44:40 -07:00
|
|
|
|
2010-01-11 16:28:54 -07:00
|
|
|
DriveReader::~DriveReader()
|
|
|
|
{
|
2009-02-21 16:44:40 -07:00
|
|
|
#ifdef _WIN32
|
2010-01-11 16:28:54 -07:00
|
|
|
#ifdef _LOCKDRIVE // Do we want to lock the drive?
|
|
|
|
// Unlock the disc in the CD-ROM drive.
|
2014-09-01 13:48:02 -06:00
|
|
|
m_lock_cdrom.PreventMediaRemoval = FALSE;
|
|
|
|
DeviceIoControl (m_disc_handle, IOCTL_CDROM_MEDIA_REMOVAL,
|
|
|
|
&m_lock_cdrom, sizeof(m_lock_cdrom), nullptr,
|
2014-03-09 14:14:26 -06:00
|
|
|
0, &dwNotUsed, nullptr);
|
2009-02-21 16:44:40 -07:00
|
|
|
#endif
|
2014-09-01 13:48:02 -06:00
|
|
|
if (m_disc_handle != INVALID_HANDLE_VALUE)
|
2010-01-11 16:28:54 -07:00
|
|
|
{
|
2014-09-01 13:48:02 -06:00
|
|
|
CloseHandle(m_disc_handle);
|
|
|
|
m_disc_handle = INVALID_HANDLE_VALUE;
|
2009-02-21 16:44:40 -07:00
|
|
|
}
|
2010-01-11 16:28:54 -07:00
|
|
|
#else
|
2014-09-01 13:48:02 -06:00
|
|
|
m_file.Close();
|
2013-10-18 01:32:56 -06:00
|
|
|
#endif
|
2010-01-11 16:28:54 -07:00
|
|
|
}
|
2009-02-21 16:44:40 -07:00
|
|
|
|
2015-12-06 21:15:51 -07:00
|
|
|
std::unique_ptr<DriveReader> DriveReader::Create(const std::string& drive)
|
2010-01-11 16:28:54 -07:00
|
|
|
{
|
2015-12-06 21:15:51 -07:00
|
|
|
auto reader = std::unique_ptr<DriveReader>(new DriveReader(drive));
|
2014-03-12 13:33:41 -06:00
|
|
|
|
2010-01-11 16:28:54 -07:00
|
|
|
if (!reader->IsOK())
|
2015-12-06 21:15:51 -07:00
|
|
|
reader.reset();
|
2014-03-12 13:33:41 -06:00
|
|
|
|
2010-01-11 16:28:54 -07:00
|
|
|
return reader;
|
|
|
|
}
|
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
void DriveReader::GetBlock(u64 block_num, u8* out_ptr)
|
2010-01-11 16:28:54 -07:00
|
|
|
{
|
2015-12-07 17:53:42 -07:00
|
|
|
std::vector<u8> sector(m_blocksize);
|
|
|
|
|
2009-02-22 09:48:54 -07:00
|
|
|
#ifdef _WIN32
|
2010-01-11 16:28:54 -07:00
|
|
|
u64 offset = m_blocksize * block_num;
|
|
|
|
LONG off_low = (LONG)offset & 0xFFFFFFFF;
|
|
|
|
LONG off_high = (LONG)(offset >> 32);
|
2015-12-07 17:53:42 -07:00
|
|
|
DWORD not_used;
|
2014-09-01 13:48:02 -06:00
|
|
|
SetFilePointer(m_disc_handle, off_low, &off_high, FILE_BEGIN);
|
2015-12-07 17:53:42 -07:00
|
|
|
if (!ReadFile(m_disc_handle, sector.data(), m_blocksize, ¬_used, nullptr))
|
2011-01-12 19:05:58 -07:00
|
|
|
PanicAlertT("Disc Read Error");
|
2009-02-22 09:48:54 -07:00
|
|
|
#else
|
2014-09-01 13:48:02 -06:00
|
|
|
m_file.Seek(m_blocksize * block_num, SEEK_SET);
|
2015-12-07 17:53:42 -07:00
|
|
|
m_file.ReadBytes(sector.data(), m_blocksize);
|
2009-02-22 09:48:54 -07:00
|
|
|
#endif
|
2015-12-07 17:53:42 -07:00
|
|
|
|
|
|
|
std::copy(sector.begin(), sector.end(), out_ptr);
|
2010-01-11 16:28:54 -07:00
|
|
|
}
|
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
bool DriveReader::ReadMultipleAlignedBlocks(u64 block_num, u64 num_blocks, u8* out_ptr)
|
2010-01-11 16:28:54 -07:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
u64 offset = m_blocksize * block_num;
|
|
|
|
LONG off_low = (LONG)offset & 0xFFFFFFFF;
|
|
|
|
LONG off_high = (LONG)(offset >> 32);
|
2015-12-07 17:53:42 -07:00
|
|
|
DWORD not_used;
|
2014-09-01 13:48:02 -06:00
|
|
|
SetFilePointer(m_disc_handle, off_low, &off_high, FILE_BEGIN);
|
2015-12-07 17:53:42 -07:00
|
|
|
if (!ReadFile(m_disc_handle, out_ptr, (DWORD)(m_blocksize * num_blocks), ¬_used, nullptr))
|
2010-01-11 16:28:54 -07:00
|
|
|
{
|
2011-01-12 19:05:58 -07:00
|
|
|
PanicAlertT("Disc Read Error");
|
2010-01-11 16:28:54 -07:00
|
|
|
return false;
|
2009-02-21 16:44:40 -07:00
|
|
|
}
|
2010-01-11 16:28:54 -07:00
|
|
|
#else
|
2014-09-01 13:48:02 -06:00
|
|
|
fseeko(m_file.GetHandle(), (m_blocksize * block_num), SEEK_SET);
|
|
|
|
if (fread(out_ptr, 1, (m_blocksize * num_blocks), m_file.GetHandle()) != (m_blocksize * num_blocks))
|
2010-01-11 16:28:54 -07:00
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-12-07 22:30:24 -07:00
|
|
|
} // namespace
|