1
/* Mednafen - Multi-system Emulator
3
* This program is free software; you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License as published by
5
* the Free Software Foundation; either version 2 of the License, or
6
* (at your option) any later version.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
#define EXTERNAL_LIBCDIO_CONFIG_H 1
20
#include "../mednafen.h"
21
#include "../general.h"
24
#include "CDAccess_Physical.h"
28
#include <cdio/cdio.h>
31
#if LIBCDIO_VERSION_NUM >= 83
32
#include <cdio/mmc_cmds.h>
35
using namespace CDUtility;
37
void CDAccess_Physical::DetermineFeatures(void)
41
mmc_cdb_t cdb = {{0, }};
43
CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_MODE_SENSE_10);
45
memset(buf, 0, sizeof(buf));
49
cdb.field[7] = sizeof(buf) >> 8;
50
cdb.field[8] = sizeof(buf) & 0xFF;
52
if(mmc_run_cmd ((CdIo *)p_cdio, MMC_TIMEOUT_DEFAULT,
58
throw(MDFN_Error(0, _("MMC [MODE SENSE 10] command failed.")));
62
const uint8 *pd = &buf[8];
64
if(pd[0] != 0x2A || pd[1] < 0x14)
66
throw(MDFN_Error(0, _("MMC [MODE SENSE 10] command returned bogus data for mode page 0x2A.")));
71
throw(MDFN_Error(0, _("Drive does not support reading Mode 2 Form 1 sectors.")));
76
throw(MDFN_Error(0, _("Drive does not support reading Mode 2 Form 2 sectors.")));
81
throw(MDFN_Error(0, _("Reading CD-DA sectors via \"READ CD\" is not supported.")));
86
throw(MDFN_Error(0, _("Read CD-DA sectors via \"READ CD\" are not positionally-accurate.")));
91
throw(MDFN_Error(0, _("Reading raw subchannel data via \"READ CD\" is not supported.")));
96
void CDAccess_Physical::PreventAllowMediumRemoval(bool prevent)
99
mmc_cdb_t cdb = {{0, }};
106
cdb.field[4] = 0x00; //prevent;
109
printf("%d\n", mmc_run_cmd_len (p_cdio, MMC_TIMEOUT_DEFAULT,
111
SCSI_MMC_DATA_READ, 0, buf));
116
void CDAccess_Physical::ReadPhysDiscInfo(unsigned retry)
118
mmc_cdb_t cdb = {{0, }};
119
uint8 toc_buffer[8192];
120
int64 start_time = time(NULL);
123
cdb.field[0] = 0x43; // Read TOC
125
cdb.field[2] = 0x00; // Format 0000b
129
cdb.field[6] = 0x01; // Track number
130
cdb.field[7] = sizeof(toc_buffer) >> 8;
131
cdb.field[8] = sizeof(toc_buffer) & 0xFF;
134
memset(toc_buffer, 0, sizeof(toc_buffer));
136
while((cdio_rc = mmc_run_cmd ((CdIo *)p_cdio, MMC_TIMEOUT_DEFAULT,
142
if(!retry || time(NULL) >= (start_time + retry))
144
throw(MDFN_Error(0, _("Error reading disc TOC.")));
150
PhysTOC.first_track = toc_buffer[2];
151
PhysTOC.last_track = toc_buffer[3];
153
if(PhysTOC.first_track < 1 || PhysTOC.first_track > 99)
155
throw(MDFN_Error(0, _("Invalid first track: %d\n"), PhysTOC.first_track));
158
if(PhysTOC.last_track > 99 || PhysTOC.last_track < PhysTOC.first_track)
160
throw(MDFN_Error(0, _("Invalid last track: %d\n"), PhysTOC.last_track));
163
int32 len_counter = MDFN_de16msb(&toc_buffer[0]) - 2;
164
uint8 *tbi = &toc_buffer[4];
166
assert(len_counter >= 0);
167
assert((len_counter & 7) == 0);
171
uint8 adr = tbi[1] >> 4;
172
uint8 control = tbi[1] & 0xF;
174
uint32 lba = MDFN_de32msb(&tbi[4]);
178
PhysTOC.tracks[100].adr = adr;
179
PhysTOC.tracks[100].control = control;
180
PhysTOC.tracks[100].lba = lba;
182
else if(tnum >= PhysTOC.first_track && tnum <= PhysTOC.last_track)
184
PhysTOC.tracks[tnum].adr = adr;
185
PhysTOC.tracks[tnum].control = control;
186
PhysTOC.tracks[tnum].lba = lba;
193
// Convenience leadout track duplication.
194
if(PhysTOC.last_track < 99)
195
PhysTOC.tracks[PhysTOC.last_track + 1] = PhysTOC.tracks[100];
198
void CDAccess_Physical::Read_TOC(TOC *toc)
203
void CDAccess_Physical::Read_Raw_Sector(uint8 *buf, int32 lba)
205
mmc_cdb_t cdb = {{0, }};
208
CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_READ_CD);
209
CDIO_MMC_SET_READ_TYPE (cdb.field, CDIO_MMC_READ_TYPE_ANY);
210
CDIO_MMC_SET_READ_LBA (cdb.field, lba);
211
CDIO_MMC_SET_READ_LENGTH24(cdb.field, 1);
213
if(SkipSectorRead[(lba >> 3) & 0xFFFF] & (1 << (lba & 7)))
215
printf("Read(skipped): %d\n", lba);
216
memset(buf, 0, 2352);
219
cdb.field[10] = 0x01;
221
if((cdio_rc = mmc_run_cmd ((CdIo *)p_cdio, MMC_TIMEOUT_DEFAULT,
227
throw(MDFN_Error(0, _("MMC Read Error; libcdio return code %d"), cdio_rc));
233
cdb.field[10] = 0x01;
235
if((cdio_rc = mmc_run_cmd ((CdIo *)p_cdio, MMC_TIMEOUT_DEFAULT,
241
throw(MDFN_Error(0, _("MMC Read Error; libcdio return code %d"), cdio_rc));
246
CDAccess_Physical::CDAccess_Physical(const char *path)
248
char **devices = NULL;
249
char **parseit = NULL;
260
devices = cdio_get_devices(DRIVER_DEVICE);
264
MDFN_printf(_("Connected physical devices:\n"));
268
MDFN_printf("%s\n", *parseit);
274
if(!parseit || parseit == devices)
276
throw(MDFN_Error(0, _("No CDROM drives detected(or no disc present).")));
281
cdio_free_device_list(devices);
285
p_cdio = cdio_open_cd(path); //, DRIVER_UNKNOWN); //NULL, DRIVER_UNKNOWN);
288
throw(MDFN_Error(0, _("Unknown error opening physical CD")));
291
//PreventAllowMediumRemoval(true);
295
// Determine how we can read this CD.
299
memset(SkipSectorRead, 0, sizeof(SkipSectorRead));
301
catch(std::exception &e)
304
cdio_free_device_list(devices);
307
cdio_destroy((CdIo *)p_cdio);
313
CDAccess_Physical::~CDAccess_Physical()
315
cdio_destroy((CdIo *)p_cdio);
318
bool CDAccess_Physical::Is_Physical(void)
323
void CDAccess_Physical::Eject(bool eject_status)
327
#if LIBCDIO_VERSION_NUM >= 83
328
if((cdio_rc = mmc_start_stop_unit((CdIo *)p_cdio, eject_status, false, 0, 0)) != 0)
330
if(cdio_rc != DRIVER_OP_UNSUPPORTED) // Don't error out if it's just an unsupported operation.
331
throw(MDFN_Error(0, _("Error ejecting medium;; libcdio return code %d"), cdio_rc));
334
if((cdio_rc = mmc_start_stop_media((CdIo *)p_cdio, eject_status, false, 0)) != 0)
336
if(cdio_rc != DRIVER_OP_UNSUPPORTED) // Don't error out if it's just an unsupported operation.
337
throw(MDFN_Error(0, _("Error ejecting medium;; libcdio return code %d"), cdio_rc));
345
ReadPhysDiscInfo(10);
347
catch(std::exception &e)
349
#if LIBCDIO_VERSION_NUM >= 83
350
mmc_start_stop_unit((CdIo *)p_cdio, true, false, 0, 0); // Eject disc, if possible.
352
mmc_start_stop_media((CdIo *)p_cdio, true, false, 0); // Eject disc, if possible.