Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/psx/mednadisc/cdrom/CDAccess_Physical.cpp
2 views
1
/* Mednafen - Multi-system Emulator
2
*
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.
7
*
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.
12
*
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
16
*/
17
18
#define EXTERNAL_LIBCDIO_CONFIG_H 1
19
20
#include "../mednafen.h"
21
#include "../general.h"
22
23
#include "CDAccess.h"
24
#include "CDAccess_Physical.h"
25
26
#include <time.h>
27
#include <stdlib.h>
28
#include <string>
29
#include <vector>
30
31
#include <cdio/cdio.h>
32
#include <cdio/mmc.h>
33
#include <cdio/logging.h>
34
35
#if LIBCDIO_VERSION_NUM >= 83
36
#include <cdio/mmc_cmds.h>
37
#endif
38
39
using namespace CDUtility;
40
41
static bool Logging = false;
42
static std::string LogMessage;
43
static void LogHandler(cdio_log_level_t level, const char message[])
44
{
45
if(!Logging)
46
return;
47
48
try
49
{
50
if(LogMessage.size() > 0)
51
LogMessage.append(" - ");
52
53
LogMessage.append(message);
54
}
55
catch(...) // Don't throw exceptions through libcdio's code.
56
{
57
LogMessage.clear();
58
}
59
}
60
61
static INLINE void StartLogging(void)
62
{
63
Logging = true;
64
LogMessage.clear();
65
}
66
67
static INLINE void ClearLogging(void)
68
{
69
LogMessage.clear();
70
}
71
72
static INLINE std::string StopLogging(void)
73
{
74
std::string ret = LogMessage;
75
76
Logging = false;
77
LogMessage.clear();
78
79
return(ret);
80
}
81
82
void CDAccess_Physical::DetermineFeatures(void)
83
{
84
uint8 buf[256];
85
86
mmc_cdb_t cdb = {{0, }};
87
88
CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_MODE_SENSE_10);
89
90
memset(buf, 0, sizeof(buf));
91
92
cdb.field[2] = 0x2A;
93
94
cdb.field[7] = sizeof(buf) >> 8;
95
cdb.field[8] = sizeof(buf) & 0xFF;
96
97
StartLogging();
98
if(mmc_run_cmd ((CdIo *)p_cdio, MMC_TIMEOUT_DEFAULT,
99
&cdb,
100
SCSI_MMC_DATA_READ,
101
sizeof(buf),
102
buf))
103
{
104
throw(MDFN_Error(0, _("MMC [MODE SENSE 10] command failed: %s"), StopLogging().c_str()));
105
}
106
else
107
{
108
const uint8 *pd = &buf[8];
109
110
StopLogging();
111
112
if(pd[0] != 0x2A || pd[1] < 0x14)
113
{
114
throw(MDFN_Error(0, _("MMC [MODE SENSE 10] command returned bogus data for mode page 0x2A.")));
115
}
116
117
if(!(pd[4] & 0x10))
118
{
119
throw(MDFN_Error(0, _("Drive does not support reading Mode 2 Form 1 sectors.")));
120
}
121
122
if(!(pd[4] & 0x20))
123
{
124
throw(MDFN_Error(0, _("Drive does not support reading Mode 2 Form 2 sectors.")));
125
}
126
127
if(!(pd[5] & 0x01))
128
{
129
throw(MDFN_Error(0, _("Reading CD-DA sectors via \"READ CD\" is not supported.")));
130
}
131
132
if(!(pd[5] & 0x02))
133
{
134
throw(MDFN_Error(0, _("Read CD-DA sectors via \"READ CD\" are not positionally-accurate.")));
135
}
136
137
if(!(pd[5] & 0x04))
138
{
139
throw(MDFN_Error(0, _("Reading raw subchannel data via \"READ CD\" is not supported.")));
140
}
141
}
142
}
143
144
void CDAccess_Physical::PreventAllowMediumRemoval(bool prevent)
145
{
146
#if 0
147
mmc_cdb_t cdb = {{0, }};
148
uint8 buf[8];
149
150
cdb.field[0] = 0x1E;
151
cdb.field[1] = 0x00;
152
cdb.field[2] = 0x00;
153
cdb.field[3] = 0x00;
154
cdb.field[4] = 0x00; //prevent;
155
cdb.field[5] = 0x00;
156
157
printf("%d\n", mmc_run_cmd_len (p_cdio, MMC_TIMEOUT_DEFAULT,
158
&cdb, 6,
159
SCSI_MMC_DATA_READ, 0, buf));
160
assert(0);
161
#endif
162
}
163
164
165
// To be used in the future for constructing semi-raw TOC data.
166
#if 0
167
static uint8 cond_hex_to_bcd(uint8 val)
168
{
169
if( ((val & 0xF) > 0x9) || ((val & 0xF0) > 0x90) )
170
return val;
171
172
return U8_to_BCD(val);
173
}
174
#endif
175
176
void CDAccess_Physical::ReadPhysDiscInfo(unsigned retry)
177
{
178
mmc_cdb_t cdb = {{0, }};
179
std::vector<uint8> toc_buffer;
180
int64 start_time = time(NULL);
181
int cdio_rc;
182
183
toc_buffer.resize(0x3FFF); // (2**(8 * 2 - 1 - 1)) - 1, in case the drive has buggy firmware which chops upper bits off or overflows with values near
184
// the max of a 16-bit signed value
185
186
cdb.field[0] = 0x43; // Read TOC
187
cdb.field[1] = 0x00;
188
cdb.field[2] = 0x02; // Format 0010b
189
cdb.field[3] = 0x00;
190
cdb.field[4] = 0x00;
191
cdb.field[5] = 0x00;
192
cdb.field[6] = 0x01; // First session number
193
cdb.field[7] = toc_buffer.size() >> 8;
194
cdb.field[8] = toc_buffer.size() & 0xFF;
195
cdb.field[9] = 0x00;
196
197
StartLogging();
198
while((cdio_rc = mmc_run_cmd ((CdIo *)p_cdio, MMC_TIMEOUT_DEFAULT,
199
&cdb,
200
SCSI_MMC_DATA_READ,
201
toc_buffer.size(),
202
&toc_buffer[0])))
203
{
204
if(!retry || time(NULL) >= (start_time + retry))
205
{
206
throw(MDFN_Error(0, _("Error reading disc TOC: %s"), StopLogging().c_str()));
207
}
208
else
209
ClearLogging();
210
}
211
StopLogging();
212
213
PhysTOC.Clear();
214
215
{
216
int32 len_counter = MDFN_de16msb(&toc_buffer[0]) - 2;
217
uint8 *tbi = &toc_buffer[4];
218
219
if(len_counter < 0 || (len_counter % 11) != 0)
220
throw MDFN_Error(0, _("READ TOC command response data is of an invalid length."));
221
222
while(len_counter)
223
{
224
// Ref: MMC-3 draft revision 10g, page 221
225
uint8 sess MDFN_NOWARN_UNUSED = tbi[0];
226
uint8 adr_ctrl = tbi[1];
227
uint8 tno MDFN_NOWARN_UNUSED = tbi[2];
228
uint8 point = tbi[3];
229
uint8 min MDFN_NOWARN_UNUSED = tbi[4];
230
uint8 sec MDFN_NOWARN_UNUSED = tbi[5];
231
uint8 frame MDFN_NOWARN_UNUSED = tbi[6];
232
uint8 hour_phour MDFN_NOWARN_UNUSED = tbi[7];
233
uint8 pmin = tbi[8];
234
uint8 psec = tbi[9];
235
uint8 pframe = tbi[10];
236
237
if((adr_ctrl >> 4) == 1)
238
{
239
switch(((adr_ctrl >> 4) << 8) | point)
240
{
241
case 0x101 ... 0x163:
242
PhysTOC.tracks[point].adr = adr_ctrl >> 4;
243
PhysTOC.tracks[point].control = adr_ctrl & 0xF;
244
PhysTOC.tracks[point].lba = AMSF_to_LBA(pmin, psec, pframe);
245
break;
246
247
case 0x1A0:
248
PhysTOC.first_track = pmin;
249
PhysTOC.disc_type = psec;
250
break;
251
252
case 0x1A1:
253
PhysTOC.last_track = pmin;
254
break;
255
256
case 0x1A2:
257
PhysTOC.tracks[100].adr = adr_ctrl >> 4;
258
PhysTOC.tracks[100].control = adr_ctrl & 0xF;
259
PhysTOC.tracks[100].lba = AMSF_to_LBA(pmin, psec, pframe);
260
break;
261
262
default:
263
//MDFN_printf("%02x %02x\n", adr_ctrl >> 4, point);
264
break;
265
}
266
}
267
268
tbi += 11;
269
len_counter -= 11;
270
}
271
}
272
273
274
if(PhysTOC.first_track < 1 || PhysTOC.first_track > 99)
275
{
276
throw(MDFN_Error(0, _("Invalid first track: %d\n"), PhysTOC.first_track));
277
}
278
279
if(PhysTOC.last_track > 99 || PhysTOC.last_track < PhysTOC.first_track)
280
{
281
throw(MDFN_Error(0, _("Invalid last track: %d\n"), PhysTOC.last_track));
282
}
283
284
// Convenience leadout track duplication.
285
if(PhysTOC.last_track < 99)
286
PhysTOC.tracks[PhysTOC.last_track + 1] = PhysTOC.tracks[100];
287
}
288
289
void CDAccess_Physical::Read_TOC(TOC *toc)
290
{
291
*toc = PhysTOC;
292
}
293
294
void CDAccess_Physical::Read_Raw_Sector(uint8 *buf, int32 lba)
295
{
296
mmc_cdb_t cdb = {{0, }};
297
int cdio_rc;
298
299
CDIO_MMC_SET_COMMAND(cdb.field, CDIO_MMC_GPCMD_READ_CD);
300
CDIO_MMC_SET_READ_TYPE (cdb.field, CDIO_MMC_READ_TYPE_ANY);
301
CDIO_MMC_SET_READ_LBA (cdb.field, lba);
302
CDIO_MMC_SET_READ_LENGTH24(cdb.field, 1);
303
304
StartLogging();
305
if(SkipSectorRead[(lba >> 3) & 0xFFFF] & (1 << (lba & 7)))
306
{
307
printf("Read(skipped): %d\n", lba);
308
memset(buf, 0, 2352);
309
310
cdb.field[9] = 0x00;
311
cdb.field[10] = 0x01;
312
313
if((cdio_rc = mmc_run_cmd ((CdIo *)p_cdio, MMC_TIMEOUT_DEFAULT,
314
&cdb,
315
SCSI_MMC_DATA_READ,
316
96,
317
buf + 2352)))
318
{
319
throw(MDFN_Error(0, _("MMC Read Error: %s"), StopLogging().c_str()));
320
}
321
}
322
else
323
{
324
cdb.field[9] = 0xF8;
325
cdb.field[10] = 0x01;
326
327
if((cdio_rc = mmc_run_cmd ((CdIo *)p_cdio, MMC_TIMEOUT_DEFAULT,
328
&cdb,
329
SCSI_MMC_DATA_READ,
330
2352 + 96,
331
buf)))
332
{
333
throw(MDFN_Error(0, _("MMC Read Error: %s"), StopLogging().c_str()));
334
}
335
}
336
StopLogging();
337
}
338
339
CDAccess_Physical::CDAccess_Physical(const std::string& path)
340
{
341
char **devices = NULL;
342
char **parseit = NULL;
343
344
p_cdio = NULL;
345
346
cdio_init();
347
cdio_log_set_handler(LogHandler);
348
349
//
350
//
351
//
352
try
353
{
354
devices = cdio_get_devices(DRIVER_DEVICE);
355
parseit = devices;
356
if(parseit)
357
{
358
MDFN_printf(_("Connected physical devices:\n"));
359
MDFN_indent(1);
360
while(*parseit)
361
{
362
MDFN_printf("%s\n", *parseit);
363
parseit++;
364
}
365
MDFN_indent(-1);
366
}
367
368
if(!parseit || parseit == devices)
369
{
370
throw(MDFN_Error(0, _("No CDROM drives detected(or no disc present).")));
371
}
372
373
if(devices)
374
{
375
cdio_free_device_list(devices);
376
devices = NULL;
377
}
378
379
StartLogging();
380
p_cdio = cdio_open_cd(path.c_str());
381
if(!p_cdio)
382
{
383
throw(MDFN_Error(0, _("Error opening physical CD: %s"), StopLogging().c_str()));
384
}
385
StopLogging();
386
387
//PreventAllowMediumRemoval(true);
388
ReadPhysDiscInfo(0);
389
390
//
391
// Determine how we can read this CD.
392
//
393
DetermineFeatures();
394
395
memset(SkipSectorRead, 0, sizeof(SkipSectorRead));
396
}
397
catch(std::exception &e)
398
{
399
if(devices)
400
cdio_free_device_list(devices);
401
402
if(p_cdio)
403
cdio_destroy((CdIo *)p_cdio);
404
405
throw;
406
}
407
}
408
409
CDAccess_Physical::~CDAccess_Physical()
410
{
411
cdio_destroy((CdIo *)p_cdio);
412
}
413
414
bool CDAccess_Physical::Is_Physical(void) throw()
415
{
416
return(true);
417
}
418
419
void CDAccess_Physical::Eject(bool eject_status)
420
{
421
int cdio_rc;
422
423
StartLogging();
424
#if LIBCDIO_VERSION_NUM >= 83
425
if((cdio_rc = mmc_start_stop_unit((CdIo *)p_cdio, eject_status, false, 0, 0)) != 0)
426
{
427
if(cdio_rc != DRIVER_OP_UNSUPPORTED) // Don't error out if it's just an unsupported operation.
428
throw(MDFN_Error(0, _("Error ejecting medium: %s"), StopLogging().c_str()));
429
}
430
#else
431
if((cdio_rc = mmc_start_stop_media((CdIo *)p_cdio, eject_status, false, 0)) != 0)
432
{
433
if(cdio_rc != DRIVER_OP_UNSUPPORTED) // Don't error out if it's just an unsupported operation.
434
throw(MDFN_Error(0, _("Error ejecting medium: %s"), StopLogging().c_str()));
435
}
436
#endif
437
StopLogging();
438
439
if(!eject_status)
440
{
441
try
442
{
443
ReadPhysDiscInfo(10);
444
}
445
catch(std::exception &e)
446
{
447
#if LIBCDIO_VERSION_NUM >= 83
448
mmc_start_stop_unit((CdIo *)p_cdio, true, false, 0, 0); // Eject disc, if possible.
449
#else
450
mmc_start_stop_media((CdIo *)p_cdio, true, false, 0); // Eject disc, if possible.
451
#endif
452
throw;
453
}
454
}
455
}
456
457
458