summaryrefslogtreecommitdiff
path: root/Source/Core/DiscIO/Src/DriveBlob.cpp
blob: e69f8ff34ca4a5a852bdddd3fb55214ba8577f52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (C) 2003 Dolphin Project.

// 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 "DriveBlob.h"

namespace DiscIO
{

DriveReader::DriveReader(const char *drive)
{
#ifdef _WIN32
	char path[MAX_PATH];
	strncpy(path, drive, 3);
	path[2] = 0;
	sprintf(path, "\\\\.\\%s", drive);
	SectorReader::SetSectorSize(2048);
	hDisc = CreateFile(path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
						NULL, OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, NULL);
	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];
		if (!ReadFile(hDisc, buffer, m_blocksize, (LPDWORD)&not_used, NULL))
		{
			delete [] buffer;
			// OK, something is wrong.
			CloseHandle(hDisc);
			hDisc = INVALID_HANDLE_VALUE;
			return;
		}
		delete [] buffer;
		
	#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,
                   &pmrLockCDROM, sizeof(pmrLockCDROM), NULL,
                   0, &dwNotUsed, NULL);
	#endif
#else
	SectorReader::SetSectorSize(2048);
	file_.Open(drive, "rb");
	if (file_)
	{
#endif
	}
	else
		NOTICE_LOG(DISCIO, "Load from DVD backup failed or no disc in drive %s", drive);
}  // 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
	if (hDisc != INVALID_HANDLE_VALUE)
	{
		CloseHandle(hDisc);
		hDisc = INVALID_HANDLE_VALUE;
	}
#else
	file_.Close();
#endif	
}

DriveReader *DriveReader::Create(const char *drive)
{
	DriveReader *reader = new DriveReader(drive);
	if (!reader->IsOK())
	{
		delete reader;
		return 0;
	}
	return reader;
}

void DriveReader::GetBlock(u64 block_num, u8 *out_ptr)
{
	u8* const lpSector = new u8[m_blocksize];
#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);
	if (!ReadFile(hDisc, lpSector, m_blocksize, (LPDWORD)&NotUsed, NULL))
		PanicAlertT("Disc Read Error");
#else
	file_.Seek(m_blocksize * block_num, SEEK_SET);
	file_.ReadBytes(lpSector, m_blocksize);
#endif
	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);
	if (!ReadFile(hDisc, out_ptr, (DWORD)(m_blocksize * num_blocks), (LPDWORD)&NotUsed, NULL))
	{
		PanicAlertT("Disc Read Error");
		return false;
	}
#else
	fseeko(file_.GetHandle(), m_blocksize*block_num, SEEK_SET);
	if(fread(out_ptr, 1, m_blocksize * num_blocks, file_.GetHandle()) != m_blocksize * num_blocks)
		return false;
#endif
	return true;
}

}  // namespace