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"
|
2016-04-26 05:24:08 -06:00
|
|
|
#else
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <stdio.h> // fileno
|
|
|
|
#if defined __linux__
|
|
|
|
#include <linux/fs.h> // BLKGETSIZE64
|
|
|
|
#elif defined __FreeBSD__
|
|
|
|
#include <sys/disk.h> // DIOCGMEDIASIZE
|
|
|
|
#elif defined __APPLE__
|
|
|
|
#include <sys/disk.h> // DKIOCGETBLOCKCOUNT / DKIOCGETBLOCKSIZE
|
|
|
|
#endif
|
2014-02-20 17:47:53 -07:00
|
|
|
#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
|
|
|
{
|
2016-04-26 05:24:08 -06:00
|
|
|
// 32 sectors is roughly the optimal amount a CD Drive can read in
|
|
|
|
// a single IO cycle. Larger values yield no performance improvement
|
|
|
|
// and just cause IO stalls from the read delay. Smaller values allow
|
|
|
|
// the OS IO and seeking overhead to ourstrip the time actually spent
|
|
|
|
// transferring bytes from the media.
|
|
|
|
SetChunkSize(32); // 32*2048 = 64KiB
|
|
|
|
SetSectorSize(2048);
|
2009-02-21 16:44:40 -07:00
|
|
|
#ifdef _WIN32
|
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);
|
2016-04-26 05:24:08 -06:00
|
|
|
if (IsOK())
|
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;
|
2016-04-26 05:24:08 -06:00
|
|
|
std::vector<u8> buffer(GetSectorSize());
|
|
|
|
if (!ReadFile(m_disc_handle, buffer.data(), GetSectorSize(), ¬_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;
|
2009-02-24 00:30:10 -07:00
|
|
|
}
|
2016-04-26 05:24:08 -06:00
|
|
|
}
|
|
|
|
if (IsOK())
|
|
|
|
{
|
|
|
|
// Initialize m_size by querying the volume capacity.
|
|
|
|
STORAGE_READ_CAPACITY storage_size;
|
|
|
|
storage_size.Version = sizeof(storage_size);
|
|
|
|
DWORD bytes = 0;
|
|
|
|
DeviceIoControl(m_disc_handle, IOCTL_STORAGE_READ_CAPACITY, nullptr, 0,
|
|
|
|
&storage_size, sizeof(storage_size), &bytes, nullptr);
|
|
|
|
m_size = bytes ? storage_size.DiskLength.QuadPart : 0;
|
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
|
2014-09-01 13:48:02 -06:00
|
|
|
m_file.Open(drive, "rb");
|
|
|
|
if (m_file)
|
2010-01-11 16:28:54 -07:00
|
|
|
{
|
2016-04-26 05:24:08 -06:00
|
|
|
int fd = fileno(m_file.GetHandle());
|
|
|
|
#if defined __linux__
|
|
|
|
// NOTE: Doesn't matter if it fails, m_size was initialized to zero
|
|
|
|
ioctl(fd, BLKGETSIZE64, &m_size); // u64*
|
|
|
|
#elif defined __FreeBSD__
|
|
|
|
off_t size = 0;
|
|
|
|
ioctl(fd, DIOCGMEDIASIZE, &size); // off_t*
|
|
|
|
m_size = size;
|
|
|
|
#elif defined __APPLE__
|
|
|
|
u64 count = 0;
|
|
|
|
u32 block_size = 0;
|
|
|
|
ioctl(fd, DKIOCGETBLOCKCOUNT, &count); // u64*
|
|
|
|
ioctl(fd, DKIOCGETBLOCKSIZE, &block_size); // u32*
|
|
|
|
m_size = count * block_size;
|
|
|
|
#endif
|
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;
|
|
|
|
}
|
|
|
|
|
2016-04-26 05:24:08 -06:00
|
|
|
bool DriveReader::GetBlock(u64 block_num, u8* out_ptr)
|
2010-01-11 16:28:54 -07:00
|
|
|
{
|
2016-04-26 05:24:08 -06:00
|
|
|
return DriveReader::ReadMultipleAlignedBlocks(block_num, 1, 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
|
2016-04-26 05:24:08 -06:00
|
|
|
LARGE_INTEGER offset;
|
|
|
|
offset.QuadPart = GetSectorSize() * block_num;
|
|
|
|
SetFilePointerEx(m_disc_handle, offset, nullptr, FILE_BEGIN);
|
|
|
|
DWORD bytes_read;
|
|
|
|
if (!ReadFile(m_disc_handle, out_ptr, static_cast<DWORD>(GetSectorSize() * num_blocks),
|
|
|
|
&bytes_read, 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
|
|
|
}
|
2016-04-26 05:24:08 -06:00
|
|
|
return bytes_read == GetSectorSize() * num_blocks;
|
2010-01-11 16:28:54 -07:00
|
|
|
#else
|
2016-04-26 05:24:08 -06:00
|
|
|
m_file.Seek(GetSectorSize() * block_num, SEEK_SET);
|
|
|
|
if (m_file.ReadBytes(out_ptr, num_blocks * GetSectorSize()))
|
|
|
|
return true;
|
|
|
|
m_file.Clear();
|
|
|
|
return false;
|
2010-01-11 16:28:54 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-12-07 22:30:24 -07:00
|
|
|
} // namespace
|