1 /* dvdisaster: Additional error correction for optical media.
2 * Copyright (C) 2004-2015 Carsten Gnoerlich.
3 *
4 * Email: carsten@dvdisaster.org -or- cgnoerlich@fsfe.org
5 * Project homepage: http://www.dvdisaster.org
6 *
7 * This file is part of dvdisaster.
8 *
9 * dvdisaster is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * dvdisaster is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with dvdisaster. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "dvdisaster.h"
24
25 #include "scsi-layer.h"
26 #include "udf.h"
27
28 /***
29 *** Forward declarations
30 ***/
31
32 static int query_type(DeviceHandle*, int);
33 static gint64 query_size(Image*);
34 static int query_copyright(DeviceHandle*);
35
36 static int read_dvd_sector(DeviceHandle*, unsigned char*, int, int);
37 static int read_cd_sector(DeviceHandle*, unsigned char*, int, int);
38 static int read_raw_cd_sector(DeviceHandle*, unsigned char*, int, int);
39
40 /***
41 *** Create a buffer aligned at a 4096 byte boundary.
42 *** Some SCSI drivers seem to need this.
43 */
44
CreateAlignedBuffer(int size)45 AlignedBuffer* CreateAlignedBuffer(int size)
46 { AlignedBuffer *ab = g_malloc0(sizeof(AlignedBuffer));
47
48 ab->base = g_malloc(size+4096);
49 ab->buf = ab->base + (4096 - ((unsigned long)ab->base & 4095));
50
51 return ab;
52 }
53
FreeAlignedBuffer(AlignedBuffer * ab)54 void FreeAlignedBuffer(AlignedBuffer *ab)
55 { g_free(ab->base);
56 g_free(ab);
57 }
58
59 /*
60 * Align a length to a multiple of 4.
61 * Some broken chipsets fail on DMA otherways.
62 */
63
length_align(unsigned int * length)64 static void length_align(unsigned int *length)
65 {
66 if(*length & 3)
67 { Verbose("# Warning: Realigning length from %d to %d\n",
68 *length, *length & ~3);
69 *length &= ~3;
70 }
71 }
72
73 /***
74 *** CD and DVD query routines.
75 ***/
76
77 /*
78 * Send INQUIRY to the device.
79 */
80
InquireDevice(DeviceHandle * dh,int probe_only)81 int InquireDevice(DeviceHandle *dh, int probe_only)
82 { AlignedBuffer *ab = CreateAlignedBuffer(2048);
83 Sense sense;
84 char *ibuf,*vbuf;
85 unsigned char cmd[MAX_CDB_SIZE];
86 unsigned char device_type;
87
88 /*** Try to learn something about the device vendor */
89
90 memset(cmd, 0, MAX_CDB_SIZE);
91 cmd[0] = 0x12; /* INQUIRY */
92 cmd[4] = 36; /* allocation length */
93
94 if(SendPacket(dh, cmd, 6, ab->buf, 36, &sense, DATA_READ)<0)
95 { FreeAlignedBuffer(ab);
96 if(probe_only) return 0x1f; /* don't care about failure, just return invalid device */
97
98 strcpy(dh->devinfo, _("unknown"));
99
100 #ifdef SYS_LINUX
101 PrintCLI("\n");
102 Stop(_("Can open %s, but INQUIRY fails.\n"
103 "Chances are that you're using ide-scsi emulation for an ATAPI drive,\n"
104 "and try to access it via /dev/cdrom or /dev/hd?.\n"
105 "Either use /dev/scd? or /dev/sr? instead, or disable ide-scsi emulation.\n"),
106 dh->device);
107 return 0;
108 #else
109 PrintCLI("\n");
110 Stop(_("INQUIRY failed. Something is wrong with drive %s.\n"),dh->device);
111 return 0;
112 #endif
113 }
114 else
115 { int i,j,vidx=0;
116
117 ibuf = dh->devinfo;
118 vbuf = dh->vendor;
119 for(i=0,j=8; j<36; j++)
120 { if(j==32)
121 { vidx = i;
122 if(i>0 && !isspace((int)ibuf[i-1])) /* separate the version string */
123 ibuf[i++] = ' ';
124 }
125 if( isprint(ab->buf[j]) /* eliminate multiple spaces and unprintables */
126 && (!isspace(ab->buf[j]) || (i>0 && !isspace((int)ibuf[i-1]))))
127 { vbuf[i] = ab->buf[j];
128 ibuf[i++] = ab->buf[j];
129 }
130 }
131 ibuf[i] = vbuf[i] = 0;
132
133 if(vidx) vbuf[vidx--] = 0;
134 while(vidx >= 0 && vbuf[vidx] == ' ')
135 vbuf[vidx--] =0;
136
137 if(ab->buf[0] != 0x05 && !probe_only)
138 { PrintCLI("\n");
139 if(ab->buf[0]) Stop(_("Device %s (%s) is not an optical drive."),dh->device,ibuf);
140 else Stop(_("Device %s (%s) is a hard disk."),dh->device,ibuf);
141 }
142 }
143
144 device_type = ab->buf[0] & 0x1f;
145 FreeAlignedBuffer(ab);
146
147 return device_type; /* return the SCSI peripheral device type */
148 }
149
150 /*
151 * Some drives do not know about profiles.
152 * In theory only plain CD-ROM type drives have no choices between
153 * different profiles, but we must consider buggy firmware also.
154 * Do a quick and dirty differentiation between the possible formats.
155 */
156
try_fallback_type_check(DeviceHandle * dh)157 static int try_fallback_type_check(DeviceHandle *dh)
158 { AlignedBuffer *ab;
159 Sense *sense = &dh->sense;
160 unsigned char cmd[MAX_CDB_SIZE];
161 int length;
162
163 Verbose("# *** try_fallback_type_check(%s) ***\n", dh->devinfo);
164
165 ab = CreateAlignedBuffer(2048);
166
167 /*** If the medium is a BD, the following will succeed. */
168
169 /* Query length of returned data */
170
171 memset(cmd, 0, MAX_CDB_SIZE);
172 cmd[0] = 0xad; /* READ DVD STRUCTURE */
173 cmd[1] = 0x01; /* subcommand for BD */
174 cmd[6] = 0; /* First layer */
175 cmd[7] = 0; /* We want DI (disc information) */
176 cmd[8] = 0; /* Allocation length */
177 cmd[9] = MIN_TRANSFER_LEN;
178
179 /*** Only a BD should respond positively here */
180
181 Verbose("# BD: trying READ DVD with BD subcommand for size\n");
182 if(SendPacket(dh, cmd, 12, ab->buf, MIN_TRANSFER_LEN, sense, DATA_READ)<0)
183 {
184 Verbose("# failed -> not a BD type medium.\n");
185 goto try_dvd;
186 }
187
188 /*** Some DVD drives ignore the media type 0x01 and return the dvd structure.
189 Since the DVD structure is 2052 bytes while the BD DI is 4100 bytes,
190 we can tell from the size whether we have been fooled. */
191
192 length = ab->buf[0]<<8 | ab->buf[1];
193 length += 2;
194 if(length != 4100) /* not a BD */
195 { Verbose("# allocation length = %d != 4100 -> not a BD type medium.\n", length);
196 goto try_dvd;
197 }
198
199 Verbose("# -> Looks like a BD type medium.\n");
200 dh->profileDescr = "(BD)";
201 dh->shortProfile = "(BD)";
202 dh->mainType = DVD;
203 dh->subType = DVD;
204 FreeAlignedBuffer(ab);
205 return TRUE;
206
207 /*** If the medium is a DVD, the following query will succeed. */
208
209 /* Query length of returned data */
210 try_dvd:
211 Verbose("# DVD: Trying READ DVD STRUCTURE.\n");
212
213 memset(cmd, 0, MAX_CDB_SIZE);
214 cmd[0] = 0xad; /* READ DVD STRUCTURE */
215 cmd[6] = 0; /* First layer */
216 cmd[7] = 0; /* We want PHYSICAL info */
217 cmd[8] = 0; /* Allocation length */
218 cmd[9] = MIN_TRANSFER_LEN;
219
220 /* Different drives react with different error codes on this request;
221 especially CDROMs seem to react very indeterministic here
222 (okay, they obviously do not know about DVD cdbs).
223 So we do not look for specific error and regard any failure as a sign
224 that the medium is not a DVD. */
225
226 if(SendPacket(dh, cmd, 12, ab->buf, MIN_TRANSFER_LEN, sense, DATA_READ)<0)
227 {
228 Verbose("# failed -> not a DVD type medium\n");
229 goto assume_cd;
230 }
231
232 Verbose("# -> Looks like a DVD type medium.\n");
233 dh->profileDescr = "(DVD)";
234 dh->shortProfile = "(DVD)";
235 dh->mainType = DVD;
236 dh->subType = DVD;
237 FreeAlignedBuffer(ab);
238 return TRUE;
239
240 /*** No need to investigate further; if it is not a CD some
241 subsequent tests will fail. */
242
243 assume_cd:
244 Verbose("# CD: It's either a CD or totally broken.\n");
245 dh->profileDescr = "(CD)";
246 dh->shortProfile = "(CD)";
247 dh->mainType = CD;
248 FreeAlignedBuffer(ab);
249 return TRUE;
250 }
251
252 /*
253 * Ask drive for active profile; this gives us a hint
254 * about the kind of medium the drive believes to see
255 */
256
get_configuration(DeviceHandle * dh)257 static int get_configuration(DeviceHandle *dh)
258 { AlignedBuffer *ab;
259 Sense *sense = &dh->sense;
260 unsigned char cmd[MAX_CDB_SIZE];
261 guint32 data_length;
262 int len,ret;
263
264 Verbose("# *** get_configuration(%s) ***\n", dh->devinfo);
265
266 len = 2048;
267 ab = CreateAlignedBuffer(len);
268
269 memset(cmd, 0, MAX_CDB_SIZE);
270 cmd[0] = 0x46; /* GET CONFIGURATION */
271 cmd[1] = 1; /* only current features */
272 cmd[7] = len>>8; /* buffer size */
273 cmd[8] = len&0xff;
274
275 ret = SendPacket(dh, cmd, 10, ab->buf, len, sense, DATA_READ);
276
277 if(ret<0)
278 { RememberSense(sense->sense_key, sense->asc, sense->ascq);
279 Verbose("# -> failure\n");
280 FreeAlignedBuffer(ab);
281 if(try_fallback_type_check(dh))
282 return 0;
283 return ret;
284 }
285
286 data_length = ab->buf[0]<<24 | ab->buf[1] | ab->buf[2] | ab->buf[3];
287 dh->profile = ab->buf[6]<<8 | ab->buf[7];
288 FreeAlignedBuffer(ab);
289
290 Verbose("# %d data len, %x current\n", data_length, dh->profile);
291
292 switch(dh->profile)
293 { /* Note: subType handling is different for CD for historical reasons */
294 case 0x08: dh->profileDescr = "CD-ROM";
295 dh->shortProfile = "CD-ROM";
296 dh->mainType = CD;
297 break;
298 case 0x09: dh->profileDescr = "CD-R";
299 dh->shortProfile = "CD-R";
300 dh->mainType = CD;
301 break;
302 case 0x0a: dh->profileDescr = "CD-RW";
303 dh->shortProfile = "CD-RW";
304 dh->mainType = CD;
305 dh->rewriteable = TRUE;
306 break;
307
308 case 0x10: dh->profileDescr = "DVD-ROM";
309 dh->shortProfile = "DVD-ROM";
310 dh->mainType = DVD;
311 dh->subType = DVD;
312 break;
313 case 0x11: dh->profileDescr = "DVD-R Sequential recording";
314 dh->shortProfile = "DVD-R";
315 dh->mainType = DVD; dh->isDash = TRUE;
316 dh->subType = DVD_DASH_R;
317 break;
318 case 0x12: dh->profileDescr = "DVD-RAM";
319 dh->shortProfile = "DVD-RAM";
320 dh->mainType = DVD;
321 dh->rewriteable = TRUE;
322 dh->subType = DVD_RAM;
323 break;
324 case 0x13: dh->profileDescr = "DVD-RW Restricted overwrite";
325 dh->shortProfile = "DVD-RW";
326 dh->mainType = DVD; dh->isDash = TRUE;
327 dh->rewriteable = TRUE;
328 dh->subType = DVD_DASH_RW;
329 break;
330 case 0x14: dh->profileDescr = "DVD-RW Sequential overwrite";
331 dh->shortProfile = "DVD-RW";
332 dh->mainType = DVD; dh->isDash = TRUE;
333 dh->rewriteable = TRUE;
334 dh->subType = DVD_DASH_RW;
335 break;
336 case 0x15: dh->profileDescr = "DVD-R DL Sequential recording";
337 dh->shortProfile = "DVD-R DL";
338 dh->mainType = DVD; dh->isDash = TRUE;
339 dh->subType = DVD_DASH_R_DL;
340 break;
341 case 0x16: dh->profileDescr = "DVD-R DL Layer jump recording";
342 dh->shortProfile = "DVD-R DL";
343 dh->mainType = DVD; dh->isDash = TRUE;
344 dh->subType = DVD_DASH_R_DL;
345 break;
346 case 0x17: dh->profileDescr = "DVD-RW DL";
347 dh->shortProfile = "DVD-RW DL";
348 dh->mainType = DVD; dh->isDash = TRUE;
349 dh->rewriteable = TRUE;
350 dh->subType = DVD_DASH_RW_DL;
351 break;
352
353 case 0x1a: dh->profileDescr = "DVD+RW";
354 dh->shortProfile = "DVD+RW";
355 dh->mainType = DVD; dh->isPlus = TRUE;
356 dh->rewriteable = TRUE;
357 dh->subType = DVD_PLUS_RW;
358 break;
359 case 0x1b: dh->profileDescr = "DVD+R";
360 dh->shortProfile = "DVD+R";
361 dh->mainType = DVD; dh->isPlus = TRUE;
362 dh->subType = DVD_PLUS_R;
363 break;
364 case 0x2a: dh->profileDescr = "DVD+RW DL";
365 dh->shortProfile = "DVD+RW DL";
366 dh->mainType = DVD; dh->isPlus = TRUE;
367 dh->rewriteable = TRUE;
368 dh->subType = DVD_PLUS_RW_DL;
369 break;
370 case 0x2b: dh->profileDescr = "DVD+R DL";
371 dh->shortProfile = "DVD+R DL";
372 dh->mainType = DVD; dh->isPlus = TRUE;
373 dh->subType = DVD_PLUS_R_DL;
374 break;
375
376 case 0x40: dh->profileDescr = "BD-ROM";
377 dh->shortProfile = "BD-ROM";
378 dh->mainType = BD;
379 dh->subType = BD;
380 break;
381 case 0x41: dh->profileDescr = "BD-R Sequential recording mode";
382 dh->shortProfile = "BD-R";
383 dh->mainType = BD;
384 dh->subType = BD_R;
385 break;
386 case 0x42: dh->profileDescr = "BD-R Random recording mode";
387 dh->shortProfile = "BD-R";
388 dh->mainType = BD;
389 dh->subType = BD_R;
390 break;
391 case 0x43: dh->profileDescr = "BD-RE";
392 dh->shortProfile = "BD-RE";
393 dh->mainType = BD;
394 dh->rewriteable = TRUE;
395 dh->subType = BD_RE;
396 break;
397
398 default: dh->profileDescr = "Unknown profile";
399 dh->mainType = UNSUPPORTED;
400 dh->subType = UNSUPPORTED;
401 break;
402 }
403
404 Verbose("-> profile %x: %s\n", dh->profile, dh->profileDescr);
405 return ret;
406 }
407
408 /*
409 * Incomplete/unfinalized media usually cause big trouble
410 * as the usual probes (reading the TOC etc. fail).
411 * However sometimes they are still readable sectorwise,
412 * so see if we can read the ISO root sector.
413 * If we can, close eyes and continue.
414 */
415
query_incomplete(DeviceHandle * dh,int probe_only)416 static int query_incomplete(DeviceHandle *dh, int probe_only)
417 { AlignedBuffer *ab = CreateAlignedBuffer(2048);
418 int status;
419
420 Verbose("#QUERY INCOMPLETE: probing ISO root sector\n");
421
422 status = dh->read(dh, ab->buf, 16, 1);
423 if(status)
424 { Verbose("# Reading the ISO root sector failed\n");
425 FreeAlignedBuffer(ab);
426 return FALSE;
427 }
428
429 if(!strncmp((char*)(ab->buf+1), "CD001", 5))
430 { Verbose("#QUERY INCOMPLETE: sector 16 looks like an ISO root sector\n");
431 dh->typeDescr = g_strdup_printf(_("Incomplete %s"), dh->shortProfile);
432 FreeAlignedBuffer(ab);
433 return TRUE;
434 }
435 else Verbose("-> not an ISO root sector\n");
436
437 FreeAlignedBuffer(ab);
438 return FALSE;
439 }
440
441 /*
442 * CD specific probing
443 */
444
query_cd(DeviceHandle * dh,int probe_only)445 static int query_cd(DeviceHandle *dh, int probe_only)
446 { AlignedBuffer *ab = CreateAlignedBuffer(4096);
447 unsigned char *buf = ab->buf;
448 unsigned char cmd[MAX_CDB_SIZE];
449 Sense *sense = &dh->sense;
450 unsigned int length;
451 int control;
452
453 Verbose("#CD: starting media probe\n");
454
455 /*** First, do a READ TOC with format 0 to fetch the CONTROL field. */
456
457 memset(cmd, 0, MAX_CDB_SIZE);
458 cmd[0] = 0x43; /* READ TOC/PMA/ATIP */
459 cmd[2] = 0; /* format; we want the TOC */
460 cmd[6] = 1; /* track/session number */
461 cmd[7] = 0; /* allocation length */
462 cmd[8] = MIN_TRANSFER_LEN;
463
464 Verbose("#CD: querying size of READ TOC/PMA/ATIP (for TOC)\n");
465 if(SendPacket(dh, cmd, 10, buf, MIN_TRANSFER_LEN, sense, DATA_READ)<0)
466 {
467 FreeAlignedBuffer(ab);
468 if(!probe_only)
469 Stop(_("%s\nCould not query TOC length.\n"),
470 GetSenseString(sense->sense_key, sense->asc, sense->ascq, TRUE));
471
472 /* Blank CDs have no TOC, so they fail here.
473 Give back some meaningful medium description. */
474 dh->typeDescr = g_strdup_printf("%s (%s)", dh->shortProfile, _("blank"));
475 return FALSE;
476 }
477
478 length = buf[0]<<8 | buf[1];
479 length += 2 ; /* MMC3: "Disc information length excludes itself" */
480 length_align(&length);
481 Verbose("#CD: size returned is %d\n", length);
482
483 if(length>1024) /* don't let the drive hack us using a buffer overflow ;-) */
484 { if(Closure->verbose)
485 HexDump(buf, 1024, 16);
486
487 FreeAlignedBuffer(ab);
488 if(!probe_only)
489 Stop(_("TOC info too long (%d), probably multisession.\n"),length);
490 return FALSE;
491 }
492
493 memset(cmd, 0, MAX_CDB_SIZE);
494 cmd[0] = 0x43; /* READ TOC/PMA/ATIP */
495 cmd[2] = 0; /* format; we want the TOC */
496 cmd[6] = 1; /* track/session number */
497 cmd[7] = (length>>8) & 0xff; /* allocation length */
498 cmd[8] = length & 0xff;
499
500 Verbose("#CD: querying real READ TOC/PMA/ATIP (for TOC)\n");
501 if(SendPacket(dh, cmd, 10, buf, length, sense, DATA_READ)<0)
502 { FreeAlignedBuffer(ab);
503 if(!probe_only)
504 Stop(_("%s\nCould not read TOC.\n"),
505 GetSenseString(sense->sense_key, sense->asc, sense->ascq, TRUE));
506 return FALSE;
507 }
508 if(Closure->verbose)
509 HexDump(buf, length, 16);
510
511 control = buf[5];
512 Verbose("#CD: control is 0x%x\n", control);
513
514 /*** Do the READ TOC again with format 2 to fetch the full toc
515 as we want the disc type info also.
516 We do not use the CONTROL data included here as it turned
517 out to be invalid on certain CD-ROMs. Bleah. */
518
519 memset(cmd, 0, MAX_CDB_SIZE);
520 cmd[0] = 0x43; /* READ TOC/PMA/ATIP */
521 cmd[1] = 0x02; /* TIME bit required for this format */
522 cmd[2] = 2; /* format; we want the full TOC */
523 cmd[6] = 1; /* track/session number */
524 cmd[7] = 0; /* allocation length */
525 cmd[8] = MIN_TRANSFER_LEN;
526
527 Verbose("#CD: querying size of READ TOC/PMA/ATIP (for full TOC)\n");
528 if(SendPacket(dh, cmd, 10, buf, MIN_TRANSFER_LEN, sense, DATA_READ)<0)
529 { FreeAlignedBuffer(ab);
530 if(!probe_only)
531 Stop(_("%s\nCould not query full TOC length.\n"),
532 GetSenseString(sense->sense_key, sense->asc, sense->ascq, TRUE));
533 return FALSE;
534 }
535
536 length = buf[0]<<8 | buf[1];
537 length += 2; /* MMC3: "Disc information length excludes itself" */
538 length_align(&length);
539 Verbose("#CD: size returned is %d\n", length);
540
541 if(length < 15)
542 { FreeAlignedBuffer(ab);
543 if(!probe_only)
544 Stop(_("TOC info too short, length %d.\n"),length);
545 return FALSE;
546 }
547 if(length>1024) /* don't let the drive hack us using a buffer overflow ;-) */
548 { FreeAlignedBuffer(ab);
549 if(!probe_only)
550 Stop(_("TOC info too long (%d), probably multisession.\n"),length);
551 return FALSE;
552 }
553
554 memset(cmd, 0, MAX_CDB_SIZE);
555 cmd[0] = 0x43; /* READ TOC/PMA/ATIP */
556 cmd[1] = 0x02; /* TIME bit required for this format */
557 cmd[2] = 2; /* format; we want the full TOC */
558 cmd[6] = 1; /* track/session number */
559 cmd[7] = (length>>8) & 0xff; /* allocation length */
560 cmd[8] = length & 0xff;
561
562 Verbose("#CD: querying real READ TOC/PMA/ATIP (for full TOC)\n");
563 if(SendPacket(dh, cmd, 10, buf, length, sense, DATA_READ)<0)
564 { FreeAlignedBuffer(ab);
565 if(!probe_only)
566 Stop(_("%s\nCould not read full TOC.\n"),
567 GetSenseString(sense->sense_key, sense->asc, sense->ascq, TRUE));
568 return FALSE;
569 }
570 if(Closure->verbose)
571 HexDump(buf, length, 16);
572
573 if(buf[7] != 0xa0)
574 { int i;
575 PrintLog(_("\nUnexpected TOC format (length %d):\n"),length);
576
577 for(i=0; i<(int)length; i++)
578 { PrintLog("%02x ",buf[i]);
579 if(i==3 || (i-3)%11 == 0) PrintLog("\n");
580 }
581 FreeAlignedBuffer(ab);
582 if(!probe_only)
583 Stop(_("Consider sending a bug report.\n"));
584 return FALSE;
585 }
586
587 dh->sessions = buf[3];
588 Verbose("#CD: %d sessions\n", dh->sessions);
589
590 if(control & 4)
591 switch(buf[13])
592 { case 0x00: dh->typeDescr = g_strdup_printf("%s mode 1", dh->profileDescr); dh->subType = DATA1; break;
593 case 0x10: dh->typeDescr = g_strdup_printf("%s/CD-I", dh->profileDescr); dh->subType = UNSUPPORTED; break;
594 case 0x20: dh->typeDescr = g_strdup_printf("%s XA",dh->profileDescr); dh->subType = XA21; break;
595 default: dh->typeDescr = g_strdup_printf("%s ??",dh->profileDescr); dh->subType = UNSUPPORTED; break;
596 }
597 else
598 { dh->typeDescr = g_strdup_printf("%s Audio", dh->profileDescr);
599 dh->subType = UNSUPPORTED;
600 }
601
602 FreeAlignedBuffer(ab);
603 Verbose("#CD: CD medium detected, type: %s\n", dh->typeDescr);
604 return TRUE;
605 }
606
607 /*
608 * DVD specific probing
609 */
610
query_dvd(DeviceHandle * dh,int probe_only)611 static int query_dvd(DeviceHandle *dh, int probe_only)
612 { AlignedBuffer *ab = CreateAlignedBuffer(4096);
613 unsigned char *buf = ab->buf;
614 unsigned char cmd[MAX_CDB_SIZE];
615 Sense *sense = &dh->sense;
616 unsigned int ua_start,ua_end,ua_end0;
617 int phy_info4, phy_info6;
618 unsigned int length;
619 int i;
620
621 Verbose("#DVD: starting media probe\n");
622
623 /* Query length of returned data for READ DVD STRUCTURE */
624
625 memset(cmd, 0, MAX_CDB_SIZE);
626 cmd[0] = 0xad; /* READ DVD STRUCTURE */
627 cmd[6] = 0; /* First layer */
628 cmd[7] = 0; /* We want PHYSICAL info */
629 cmd[8] = 0; /* Allocation length */
630 cmd[9] = MIN_TRANSFER_LEN;
631
632 Verbose("#DVD: trying READ DVD for size of PHYSICAL info\n");
633 if(SendPacket(dh, cmd, 12, buf, MIN_TRANSFER_LEN, sense, DATA_READ)<0)
634 { FreeAlignedBuffer(ab);
635 if(!probe_only)
636 Stop(_("%s\nCould not query dvd structure length.\n"),
637 GetSenseString(sense->sense_key, sense->asc, sense->ascq, TRUE));
638 return FALSE;
639 }
640
641 length = buf[0]<<8 | buf[1];
642 length += 2;
643 length_align(&length);
644
645 if(length>4096) /* don't let the drive hack us using a buffer overflow ;-) */
646 { FreeAlignedBuffer(ab);
647 if(!probe_only)
648 Stop(_("Could not query dvd physical structure - implausible packet length %d\n"),length);
649 return FALSE;
650 }
651 Verbose("#DVD: size returned is %d\n", length);
652
653 /* Do the real query */
654
655 memset(cmd, 0, MAX_CDB_SIZE);
656 cmd[0] = 0xad; /* READ DVD STRUCTURE */
657 cmd[6] = 0; /* First layer */
658 cmd[7] = 0; /* We want PHYSICAL info */
659 cmd[8] = (length>>8) & 0xff; /* Allocation length */
660 cmd[9] = length & 0xff;
661
662 Verbose("#DVD: trying READ DVD for real PHYSICAL info\n");
663 if(SendPacket(dh, cmd, 12, buf, length, sense, DATA_READ)<0)
664 { FreeAlignedBuffer(ab);
665 if(!probe_only)
666 Stop(_("%s\nCould not query physical dvd structure.\n"),
667 GetSenseString(sense->sense_key, sense->asc, sense->ascq, TRUE));
668 return FALSE;
669 }
670 if(Closure->verbose)
671 HexDump(buf, length, 16);
672
673 dh->bookType = (buf[4]>>4) & 0x0f;
674 Verbose("#DVD: book type %d\n", dh->bookType);
675
676 /* Determine number of layers */
677
678 dh->layers = 1 + ((buf[6] & 0x60) >> 5);
679 dh->sessions = 1;
680 Verbose("#DVD: %d layers\n", dh->layers);
681
682 /* While we're at it, extract the user area size.
683 For +RW media, this is better than the value provided by READ CAPACITY. */
684
685 ua_start = /*buf[ 8]<<24 |*/ buf[ 9]<<16 | buf[10]<<8 | buf[11];
686 ua_end = /*buf[12]<<24 |*/ buf[13]<<16 | buf[14]<<8 | buf[15];
687 ua_end0 = /*buf[16]<<24 |*/ buf[17]<<16 | buf[18]<<8 | buf[19];
688 Verbose("#DVD: ua_start/_end/_end0: %d %d %d\n", ua_start, ua_end, ua_end0);
689
690 if(dh->layers == 1)
691 {
692 dh->userAreaSize = (gint64)(ua_end-ua_start);
693
694 if(dh->userAreaSize < 0 || dh->userAreaSize > MAX_DVD_SL_SIZE)
695 { LogWarning(_("READ DVD STRUCTURE: implausible medium size, %lld-%lld=%lld sectors\n"),
696 (gint64)ua_end, (gint64)ua_start, (gint64)dh->userAreaSize);
697 dh->userAreaSize = 0;
698 }
699 }
700 else
701 {
702 dh->userAreaSize = (gint64)(ua_end0-ua_start)*2;
703
704 if(dh->userAreaSize < 0 || dh->userAreaSize > MAX_DVD_DL_SIZE)
705 { LogWarning(_("READ DVD STRUCTURE: implausible medium size, %lld-%lld=%lld sectors\n"),
706 (gint64)ua_end0, (gint64)ua_start, (gint64)dh->userAreaSize);
707 dh->userAreaSize = 0;
708 }
709 }
710
711 /*** Some drives report lead in information along with physical info.
712 This allows us to collect some info on DVD-ROM drives which
713 can not do READ DVD STRUCTURE with subtype 0x0e. */
714
715 dh->manuID[0] = 0;
716
717 /* Check if some nonzero bytes have been returned */
718
719 for(i=0x200; i<length; i++)
720 if(buf[i])
721 { int j;
722
723 Verbose("#DVD: physical info contains lead-in data\n");
724 for(j=0; j<6; j++)
725 dh->manuID[j] = isprint(buf[0x225+j]) ? buf[0x225+j] : ' ';
726 dh->manuID[6] = ' ';
727
728 for(j=0; j<6; j++)
729 dh->manuID[j+7] = isprint(buf[0x22d+j]) ? buf[0x22d+j] : ' ';
730 dh->manuID[13] = 0;
731
732 for(j=11; j>=0; j--)
733 if(dh->manuID[j] == ' ') dh->manuID[j] = 0;
734 else break;
735
736 Verbose("#DVD: manufacturer id %s\n", dh->manuID);
737 break;
738 }
739
740 /*** Find out medium type */
741
742 phy_info4 = buf[4];
743 phy_info6 = buf[6];
744 Verbose("#DVD: phy_info4/6: 0x%x 0x%x\n", phy_info4, phy_info6);
745
746 /* Try getting ADIP information.
747 This is more reliable than the physical info. */
748
749 memset(cmd, 0, MAX_CDB_SIZE);
750 cmd[0] = 0xad; /* READ DVD STRUCTURE */
751 cmd[6] = 0; /* First layer */
752 cmd[7] = 0x11; /* We want the ADIP */
753 cmd[8] = 0; /* Allocation length */
754 cmd[9] = MIN_TRANSFER_LEN;
755
756 Verbose("#DVD: trying READ DVD for size of ADIP\n");
757 if(SendPacket(dh, cmd, 12, buf, MIN_TRANSFER_LEN, sense, DATA_READ) == 0)
758 { length = buf[0]<<8 | buf[1];
759 length += 2;
760 length_align(&length);
761
762 Verbose("#DVD: size returned is %d\n", length);
763
764 if(length < 4096)
765 { memset(cmd, 0, MAX_CDB_SIZE);
766 cmd[0] = 0xad; /* READ DVD STRUCTURE */
767 cmd[6] = 0; /* First layer */
768 cmd[7] = 0x11; /* We want the ADIP */
769 cmd[8] = (length>>8) & 0xff; /* Allocation length */
770 cmd[9] = length & 0xff;
771
772 Verbose("#DVD: trying READ DVD for real ADIP\n");
773 if(SendPacket(dh, cmd, 12, buf, length, sense, DATA_READ) == 0)
774 { int i;
775
776 if(Closure->verbose)
777 HexDump(buf, length, 16);
778
779 dh->isPlus = TRUE;
780 phy_info4 = buf[4];
781 phy_info6 = buf[6];
782 Verbose("#DVD: assuming DVD plus; phy_info4/6 now 0x%x 0x%x\n", phy_info4, phy_info6);
783
784 for(i=0; i<11; i++)
785 dh->manuID[i] = isprint(buf[23+i]) ? buf[23+i] : ' ';
786 dh->manuID[11] = 0;
787
788 for(i=10; i>=0; i--)
789 if(dh->manuID[i] == ' ') dh->manuID[i] = 0;
790 else break;
791 }
792 }
793 }
794 else Verbose("#DVD: no ADIP\n");
795
796 /* Get pre-recorded info from lead-in (only on -R/-RW media).
797 Only used for getting the manufacturer ID. */
798
799 memset(cmd, 0, MAX_CDB_SIZE);
800 cmd[0] = 0xad; /* READ DVD STRUCTURE */
801 cmd[6] = 0; /* First layer */
802 cmd[7] = 0x0E; /* We want the lead-in info */
803 cmd[8] = 0; /* Allocation length */
804 cmd[9] = MIN_TRANSFER_LEN;
805
806 Verbose("#DVD: trying READ DVD for size of lead-in\n");
807 if(SendPacket(dh, cmd, 12, buf, MIN_TRANSFER_LEN, sense, DATA_READ) == 0)
808 { length = buf[0]<<8 | buf[1];
809 length += 2;
810 length_align(&length);
811
812 Verbose("#DVD: size returned is %d\n", length);
813 if(length < 4096)
814 { memset(cmd, 0, MAX_CDB_SIZE);
815 cmd[0] = 0xad; /* READ DVD STRUCTURE */
816 cmd[6] = 0; /* First layer */
817 cmd[7] = 0x0E; /* We want the lead-in info */
818 cmd[8] = (length>>8) & 0xff; /* Allocation length */
819 cmd[9] = length & 0xff;
820
821 Verbose("#DVD: trying READ DVD for real lead-in\n");
822 if(SendPacket(dh, cmd, 12, buf, length, sense, DATA_READ) == 0)
823 { int i;
824
825 dh->isDash = TRUE;
826 Verbose("#DVD: assuming DVD dash\n");
827
828 for(i=0; i<6; i++)
829 dh->manuID[i] = isprint(buf[21+i]) ? buf[21+i] : ' ';
830 dh->manuID[6] = ' ';
831
832 for(i=0; i<6; i++)
833 dh->manuID[i+7] = isprint(buf[29+i]) ? buf[29+i] : ' ';
834 dh->manuID[13] = 0;
835
836 for(i=11; i>=0; i--)
837 if(dh->manuID[i] == ' ') dh->manuID[i] = 0;
838 else break;
839 }
840 }
841 } else Verbose("#DVD: lead-in could not be queried\n");
842
843 /*** Layer type info (may be faked) */
844
845 if(Closure->verbose)
846 { int layer_type = phy_info6 & 0x0f;
847 Verbose("#DVD: Layer type(s): ");
848 if(layer_type & 0x01) Verbose("embossed ");
849 if(layer_type & 0x02) Verbose("recordable ");
850 if(layer_type & 0x04) Verbose("rewriteable ");
851 Verbose("\n");
852 }
853
854 /*** Evaluate book type */
855
856 dh->bookType = (phy_info4>>4) & 0x0f;
857 Verbose("#DVD: book type (%d) evaluation...\n", dh->bookType);
858 switch(dh->bookType) /* evaluate the book type */
859 { case 1: dh->bookDescr = "DVD-RAM";
860 dh->rewriteable = TRUE;
861 dh->subType = DVD_RAM;
862 break;
863 case 2: dh->bookDescr = dh->layers == 1 ? "DVD-R" : "DVD-R DL";
864 dh->subType = dh->layers == 1 ? DVD_DASH_R : DVD_DASH_R_DL;
865 break;
866 case 3: dh->bookDescr = "DVD-RW";
867 dh->subType = DVD_DASH_RW;
868 dh->rewriteable = TRUE;
869 break;
870 case 4: dh->bookDescr = "HD DVD-ROM";
871 dh->subType = UNSUPPORTED;
872 break;
873 case 5: dh->bookDescr = "HD DVD-RAM";
874 dh->subType = UNSUPPORTED;
875 break;
876 case 6: dh->bookDescr = "HD DVD-R";
877 dh->subType = UNSUPPORTED;
878 break;
879 case 9: dh->bookDescr = "DVD+RW";
880 dh->rewriteable = TRUE;
881 dh->subType = DVD_PLUS_RW;
882 break;
883 case 10: dh->bookDescr = "DVD+R";
884 dh->subType = DVD_PLUS_R;
885 break;
886 case 13: dh->bookDescr = "DVD+RW DL";
887 dh->rewriteable = TRUE;
888 dh->subType = DVD_PLUS_RW_DL;
889 break;
890 case 14: dh->bookDescr = "DVD+R DL";
891 dh->subType = DVD_PLUS_R_DL;
892 break;
893
894 case 0: /* tricky case: real or faked DVD-ROM? */
895 { int layer_type = phy_info6 & 0x0f;
896
897 Verbose("#DVD: fake DVD-ROM detection (%d %d 0x%x)\n",
898 dh->isDash, dh->isPlus, layer_type);
899 if(dh->isDash)
900 { dh->typeDescr = g_strdup(dh->layers == 1 ? "DVD-R/-RW" : "DVD-R DL");
901 dh->subType = DVD;
902 break;
903 }
904
905 if(dh->isPlus)
906 { dh->typeDescr = g_strdup(dh->layers == 1 ? "DVD+R/+RW" : "DVD+R DL");
907 dh->subType = DVD;
908 break;
909 }
910
911 if(layer_type & 0x06) /* strange thing: (re-)writeable but neither plus nor dash */
912 { dh->typeDescr = g_strdup("DVD-ROM (fake)");
913 dh->subType = DVD;
914 break;
915 }
916
917 dh->typeDescr = g_strdup("DVD-ROM");
918 dh->subType = UNSUPPORTED;
919 break;
920 }
921
922 default:
923 dh->typeDescr = g_strdup_printf("DVD book type 0x%02x",(phy_info4>>4) & 0x0f);
924 dh->subType = UNSUPPORTED;
925 break;
926 }
927
928 if(!dh->typeDescr)
929 dh->typeDescr = g_strdup(dh->bookDescr);
930
931 FreeAlignedBuffer(ab);
932 Verbose("#DVD: DVD medium detected, type %s\n", dh->typeDescr);
933 return TRUE;
934 }
935
936 /*
937 * BD specific probing
938 */
939
query_bd(DeviceHandle * dh,int probe_only)940 static int query_bd(DeviceHandle *dh, int probe_only)
941 { AlignedBuffer *ab = CreateAlignedBuffer(8196);
942 unsigned char *buf = ab->buf;
943 unsigned char cmd[MAX_CDB_SIZE];
944 Sense *sense = &dh->sense;
945 unsigned int length;
946 int i,j;
947
948 Verbose("#BD: starting media probe\n");
949
950 /* Query length of returned data.
951 Maybe skip this in the future; it must be 4098 (+2) on BD */
952
953 memset(cmd, 0, MAX_CDB_SIZE);
954 cmd[0] = 0xad; /* READ DISC STRUCTURE */
955 cmd[1] = 0x01; /* subcommand for BD */
956 cmd[6] = 0; /* First layer */
957 cmd[7] = 0; /* We want DI (disc information) */
958 cmd[8] = 0; /* Allocation length */
959 cmd[9] = MIN_TRANSFER_LEN;
960
961 Verbose("#BD: trying READ DISC STRUCTURE for size\n");
962 if(SendPacket(dh, cmd, 12, buf, MIN_TRANSFER_LEN, sense, DATA_READ)<0)
963 { FreeAlignedBuffer(ab);
964 if(!probe_only)
965 Stop(_("%s\nCould not query BD disc structure length.\n"),
966 GetSenseString(sense->sense_key, sense->asc, sense->ascq, TRUE));
967 return FALSE;
968 }
969
970 length = buf[0]<<8 | buf[1];
971 length += 2;
972 length_align(&length);
973 Verbose("#BD: disc structure query succeeded, length %d bytes\n", length);
974
975 /* Do the real query */
976
977 memset(cmd, 0, MAX_CDB_SIZE);
978 cmd[0] = 0xad; /* READ DISC STRUCTURE */
979 cmd[1] = 0x01; /* media type BD */
980 cmd[6] = 0; /* First layer */
981 cmd[7] = 0; /* We want DI (disc information) */
982 cmd[8] = (length>>8) & 0xff; /* Allocation length */
983 cmd[9] = length & 0xff;
984
985 Verbose("#BD: trying READ DISC STRUCTURE for real query\n");
986 if(SendPacket(dh, cmd, 12, buf, length, sense, DATA_READ)<0)
987 { FreeAlignedBuffer(ab);
988 if(!probe_only)
989 Stop(_("%s\nCould not query BD disc structure.\n"),
990 GetSenseString(sense->sense_key, sense->asc, sense->ascq, TRUE));
991 return FALSE;
992 }
993
994 if(Closure->verbose)
995 HexDump(buf, length, 16);
996
997 dh->layers = 0; /* n.a. ? */
998 dh->sessions = 1;
999 dh->userAreaSize = 0; /* n.a. ? */
1000
1001 /* Assemble manufacturer info */
1002
1003 for(i=0,j=-1; i<6; i++) /* Disc Manufacturer ID */
1004 { char c = buf[4+100+i];
1005
1006 if(isprint(c))
1007 { dh->manuID[i] = c; j=i;
1008 }
1009 else dh->manuID[i] = ' ';
1010 }
1011 dh->manuID[++j]=',';
1012
1013 for(i=0; i<3; i++) /* Media type ID */
1014 { char c = buf[4+106+i];
1015 dh->manuID[++j] = isprint(c) ? c : ' ';
1016 }
1017 if(dh->manuID[j] == ',')
1018 dh->manuID[j]=0;
1019 else dh->manuID[++j] = 0;
1020
1021 /* Media type recognition */
1022
1023 if(!strncmp((char*)&buf[4+8], "BDO", 3))
1024 { dh->typeDescr = g_strdup("BD-ROM");
1025 dh->subType = UNSUPPORTED;
1026 }
1027
1028 if(!strncmp((char*)&buf[4+8], "BDW", 3))
1029 { dh->typeDescr = g_strdup("BD-RE");
1030 dh->rewriteable = TRUE;
1031 dh->subType = BD_RE;
1032 }
1033
1034 if(!strncmp((char*)&buf[4+8], "BDR", 3))
1035 { dh->typeDescr = g_strdup("BD-R");
1036 dh->subType = BD_R;
1037 }
1038
1039 Verbose("#BD: BD medium successfully probed, type %s\n", dh->typeDescr);
1040 FreeAlignedBuffer(ab);
1041 return TRUE;
1042 }
1043
1044 /*
1045 * Find out what type of disc has been inserted.
1046 */
1047
query_type(DeviceHandle * dh,int probe_only)1048 static int query_type(DeviceHandle *dh, int probe_only)
1049 { AlignedBuffer *ab;
1050 unsigned char *buf;
1051 unsigned char cmd[MAX_CDB_SIZE];
1052 Sense *sense = &dh->sense;
1053 unsigned int length;
1054 int status;
1055
1056 /*** See which profile the drive selected.
1057 This should at least give us a hint to decide
1058 between CD, DVD and BD. */
1059
1060 Verbose("# *** query_type(%s, %d) ***\n", dh->devinfo, probe_only);
1061
1062 status = get_configuration(dh);
1063 if(status) /* something went terribly wrong */
1064 return FALSE;
1065
1066 /*** Get disc information to learn the disc state (blank, finalized etc.) */
1067
1068 ab = CreateAlignedBuffer(4096);
1069 buf = ab->buf;
1070 memset(cmd, 0, MAX_CDB_SIZE);
1071 cmd[0] = 0x51; /* READ DISC INFORMATION */
1072 cmd[1] = 0; /* standard disc info */
1073 cmd[7] = 0; /* Allocation length */
1074 cmd[8] = MIN_TRANSFER_LEN;
1075
1076 Verbose("# trying READ DISC INFORMATION for size\n");
1077 if(SendPacket(dh, cmd, 10, buf, MIN_TRANSFER_LEN, sense, DATA_READ) == 0)
1078 { length = buf[0]<<8 | buf[1];
1079 length_align(&length);
1080
1081 Verbose("# size returned is %d\n", length);
1082
1083 memset(cmd, 0, MAX_CDB_SIZE);
1084 cmd[0] = 0x51; /* READ DISC INFORMATION */
1085 cmd[1] = 0; /* standard disc info */
1086 cmd[7] = length>>8; /* Allocation length */
1087 cmd[8] = length&0xff;
1088
1089 Verbose("# trying READ DISC INFORMATION for real info\n");
1090 if(SendPacket(dh, cmd, 10, buf, length, sense, DATA_READ) == 0)
1091 { dh->discStatus = buf[2];
1092 status = (dh->discStatus>>2)&3;
1093 if(status == 1 || status == 2) /* incomplete or damaged */
1094 dh->incomplete = TRUE;
1095 if(dh->mainType == CD)
1096 switch(buf[8])
1097 { case 0: dh->subType = DATA1; break;
1098 case 0x20: dh->subType = XA21; break;
1099 }
1100
1101 if(Closure->verbose)
1102 HexDump(buf, length, 16);
1103 Verbose("# status is %x%s disc type %x\n",
1104 dh->discStatus, dh->incomplete ? " (incomplete!),":",", buf[8]);
1105 }
1106 else Verbose("# READ DISC INFORMATION failed\n");
1107 }
1108 else Verbose("# Getting size of READ DISC INFORMATION failed\n");
1109 FreeAlignedBuffer(ab);
1110
1111 /*** Some defaults */
1112
1113 strcpy(dh->manuID, "-");
1114 dh->bookDescr = "-";
1115
1116 /*** Main type has been decided now */
1117
1118 switch(dh->mainType)
1119 { case CD:
1120 dh->read = read_cd_sector;
1121 dh->readRaw = read_raw_cd_sector;
1122 dh->singleRate = 150.0;
1123 dh->maxRate = 52;
1124 dh->clusterSize = 16; /* really 1, but this is faster */
1125 return dh->incomplete ? query_incomplete(dh, probe_only) : query_cd(dh, probe_only);
1126
1127 case DVD:
1128 dh->read = read_dvd_sector;
1129 dh->singleRate = 1352.54;
1130 dh->maxRate = 17;
1131 dh->clusterSize = 16;
1132 if(!dh->incomplete) return query_dvd(dh, probe_only);
1133 else
1134 { if(query_dvd(dh, TRUE)) return TRUE;
1135 return query_incomplete(dh, probe_only);
1136 }
1137 break;
1138
1139 case BD:
1140 dh->read = read_dvd_sector;
1141 dh->singleRate = 36000.0/8.0; /* 1x = 36 kbit */
1142 dh->maxRate = 9;
1143 dh->clusterSize = 32;
1144 if(!dh->incomplete) return query_bd(dh, probe_only);
1145 else
1146 { if(query_bd(dh, TRUE)) return TRUE;
1147 return query_incomplete(dh, probe_only);
1148 }
1149 break;
1150
1151 default: /* maybe HD DVD or sth else we do not support */
1152 return FALSE;
1153 }
1154
1155 return FALSE; /* unreachable */
1156 }
1157
1158 /*
1159 * Find out whether the disc is blank,
1160 * and the blank disc capacity.
1161 */
1162
query_blank(DeviceHandle * dh)1163 static int query_blank(DeviceHandle *dh)
1164 { AlignedBuffer *ab = CreateAlignedBuffer(4096);
1165 unsigned char *buf = ab->buf;
1166 unsigned char cmd[MAX_CDB_SIZE];
1167 Sense *sense = &dh->sense;
1168 unsigned int length;
1169
1170 Verbose("# *** QueryBlank(%s) ***\n", dh->devinfo);
1171
1172 /*** Determine blank size for CD media ***/
1173
1174 if(dh->mainType == CD)
1175 { memset(cmd, 0, MAX_CDB_SIZE);
1176 cmd[0] = 0x43; /* READ TOC/PMA/ATIP */
1177 cmd[1] = 0x02; /* TIME bit required for this format */
1178 cmd[2] = 4; /* format; we want the ATIP */
1179 cmd[6] = 0; /* track/session number */
1180 cmd[7] = 0; /* allocation length */
1181 cmd[8] = MIN_TRANSFER_LEN;
1182
1183 Verbose("#CD: querying size of READ TOC/PMA/ATIP (for ATIP)\n");
1184 if(SendPacket(dh, cmd, 10, buf, MIN_TRANSFER_LEN, sense, DATA_READ)<0)
1185 { FreeAlignedBuffer(ab);
1186 return FALSE;
1187 }
1188
1189 length = buf[0]<<8 | buf[1];
1190 length += 2; /* MMC3: "Disc information length excludes itself" */
1191 length_align(&length);
1192 Verbose("#CD: size returned is %d\n", length);
1193
1194 if(length < 15 || length > 1024) /* implausible */
1195 { FreeAlignedBuffer(ab);
1196 return FALSE;
1197 }
1198
1199 memset(cmd, 0, MAX_CDB_SIZE);
1200 cmd[0] = 0x43; /* READ TOC/PMA/ATIP */
1201 cmd[1] = 0x02; /* TIME bit required for this format */
1202 cmd[2] = 4; /* format; we want the full ATIP */
1203 cmd[6] = 0; /* track/session number */
1204 cmd[7] = (length>>8) & 0xff; /* allocation length */
1205 cmd[8] = length & 0xff;
1206
1207 Verbose("#CD: querying real READ TOC/PMA/ATIP (for ATIP)\n");
1208 if(SendPacket(dh, cmd, 10, buf, length, sense, DATA_READ)<0)
1209 { FreeAlignedBuffer(ab);
1210 return FALSE;
1211 }
1212
1213 if(Closure->verbose)
1214 HexDump(buf, length, 16);
1215
1216 Verbose("#CD: Lead-in @ %d:%d.%d; last possible lead-out %d:%d.%d\n",
1217 buf[8], buf[9], buf[10], buf[12], buf[13], buf[14]);
1218
1219 /* frame + 75 * (second - 2 + 60 * minute) */
1220 dh->blankCapacity = buf[14] + 75 * (buf[13] - 2 + 60 * buf[12]);
1221 }
1222
1223 /*** Determine blank size for DVD media ***/
1224
1225 if(dh->mainType == DVD)
1226 { switch(dh->subType)
1227 { case DVD_DASH_R:
1228 case DVD_PLUS_R:
1229 case DVD_DASH_R_DL:
1230 case DVD_PLUS_R_DL:
1231 memset(cmd, 0, MAX_CDB_SIZE);
1232 cmd[0] = 0x52; /* READ TRACK INFORMATION */
1233 cmd[1] = 0x01; /* TCDB (track number) addressing) */
1234 cmd[5] = 1; /* we want the first track info */
1235 cmd[7] = 0; /* allocation length */
1236 cmd[8] = MIN_TRANSFER_LEN;
1237
1238 Verbose("#DVD: querying size of READ TRACK INFORMATION\n");
1239 if(SendPacket(dh, cmd, 10, buf, MIN_TRANSFER_LEN, sense, DATA_READ)<0)
1240 { FreeAlignedBuffer(ab);
1241 return FALSE;
1242 }
1243
1244 length = buf[0]<<8 | buf[1];
1245 length += 2; /* MMC3: "Disc information length excludes itself" */
1246 length_align(&length);
1247 Verbose("#DVD: size returned is %d\n", length);
1248
1249 memset(cmd, 0, MAX_CDB_SIZE);
1250 cmd[0] = 0x52; /* READ TRACK INFORMATION */
1251 cmd[1] = 0x01; /* TCDB (track number) addressing) */
1252 cmd[5] = 1; /* we want the first track info */
1253 cmd[7] = length>>8; /* allocation length */
1254 cmd[8] = length&0xff;
1255
1256 Verbose("#DVD: querying size of READ TRACK INFORMATION\n");
1257 if(SendPacket(dh, cmd, 10, buf, length, sense, DATA_READ)<0)
1258 { FreeAlignedBuffer(ab);
1259 return FALSE;
1260 }
1261
1262 if(Closure->verbose)
1263 HexDump(buf, length, 16);
1264
1265 Verbose("#DVD: free: %d\n", buf[16]<<24|buf[17]<<16|buf[18]<<8|buf[19]);
1266 Verbose("#DVD: size: %d\n", buf[24]<<24|buf[25]<<16|buf[26]<<8|buf[27]);
1267
1268 dh->blankCapacity = buf[16]<<24|buf[17]<<16|buf[18]<<8|buf[19];
1269 break;
1270
1271 case DVD_DASH_RW:
1272 case DVD_PLUS_RW:
1273 case DVD_DASH_RW_DL:
1274 case DVD_PLUS_RW_DL:
1275 case DVD_RAM:
1276 { int cmc_size;
1277 int n_lists;
1278 int i,idx;
1279
1280 memset(cmd, 0, MAX_CDB_SIZE);
1281 cmd[0] = 0x23; /* READ FORMAT CAPACITIES */
1282 cmd[7] = 0; /* allocation length */
1283 cmd[8] = 4;
1284
1285 Verbose("#DVD: querying size of READ FORMAT CAPACITIES\n");
1286 if(SendPacket(dh, cmd, 10, buf, 4, sense, DATA_READ)<0)
1287 { FreeAlignedBuffer(ab);
1288 return FALSE;
1289 }
1290
1291 length = 4+buf[3];
1292 length_align(&length);
1293 Verbose("#DVD: size returned is %d\n", length);
1294
1295 memset(cmd, 0, MAX_CDB_SIZE);
1296 cmd[0] = 0x23; /* READ FORMAT CAPACITIES */
1297 cmd[7] = length>>8; /* allocation length */
1298 cmd[8] = length&0xff;
1299
1300 Verbose("#DVD: real query of READ FORMAT CAPACITIES\n");
1301 if(SendPacket(dh, cmd, 10, buf, length, sense, DATA_READ)<0)
1302 { FreeAlignedBuffer(ab);
1303 return FALSE;
1304 }
1305
1306 if(Closure->verbose)
1307 HexDump(buf, length, 16);
1308
1309 cmc_size = buf[4]<<24 | buf[5]<<16 | buf[6]<<8 | buf[7];
1310
1311 n_lists = buf[3] / 8;
1312 n_lists--;
1313
1314 Verbose("#DVD: Current/Maximum capacity: %d blocks\n", cmc_size);
1315
1316 switch(buf[8] & 3)
1317 { case 1: /* unformatted */
1318 Verbose("#DVD: unformatted\n");
1319 break;
1320 case 2: /* formatted */
1321 Verbose("#DVD: formatted\n");
1322 break;
1323 case 3: /* no medium */
1324 Verbose("#DVD: %d no medium\n", buf[8]);
1325 break;
1326 default: /* b0rked */
1327 Verbose("#DVD: b0rked\n");
1328 break;
1329 }
1330
1331 if(!n_lists)
1332 { FreeAlignedBuffer(ab);
1333 return FALSE;
1334 }
1335
1336 /* Now go through all capacity lists */
1337
1338 for(i=0, idx=12; i<n_lists; i++, idx+=8)
1339 { gint64 size;
1340
1341 size = (gint64)(buf[idx]<<24 | buf[idx+1]<<16 | buf[idx+2]<<8 | buf[idx+3]);
1342 Verbose("#DVD: Cap list %d - type %x, size %lld\n", i, buf[idx+4]>>2, size);
1343
1344 switch(buf[idx+4]>>2) /* format type */
1345 { case 0x00: /* all media */
1346 case 0x10: /* blank CD-RW capacity */
1347 case 0x26: /* blank DVD+RW capacity */
1348 FreeAlignedBuffer(ab);
1349 dh->blankCapacity = size;
1350 return TRUE;
1351 }
1352 }
1353 }
1354 break;
1355 }
1356 }
1357
1358 /*** Determine blank size for BD media ***/
1359
1360 if(dh->mainType == BD)
1361 { unsigned int size,sa_size;
1362 int n_lists, i, idx;
1363
1364 memset(cmd, 0, MAX_CDB_SIZE);
1365 cmd[0] = 0x23; /* READ FORMAT CAPACITIES */
1366 cmd[7] = 0; /* allocation length */
1367 cmd[8] = 4;
1368
1369 Verbose("#BD: querying size of READ FORMAT CAPACITIES\n");
1370 if(SendPacket(dh, cmd, 10, buf, 4, sense, DATA_READ)<0)
1371 { FreeAlignedBuffer(ab);
1372 return FALSE;
1373 }
1374
1375 length = 4+buf[3];
1376 length_align(&length);
1377 Verbose("#BD: size returned is %d\n", length);
1378
1379 memset(cmd, 0, MAX_CDB_SIZE);
1380 cmd[0] = 0x23; /* READ FORMAT CAPACITIES */
1381 cmd[7] = length>>8; /* allocation length */
1382 cmd[8] = length&0xff;
1383
1384 Verbose("#BD: real query of READ FORMAT CAPACITIES\n");
1385 if(SendPacket(dh, cmd, 10, buf, length, sense, DATA_READ)<0)
1386 { FreeAlignedBuffer(ab);
1387 return FALSE;
1388 }
1389
1390 if(Closure->verbose)
1391 HexDump(buf, length, 16);
1392
1393 size = (buf[4]<<24 | buf[5]<<16 | buf[6]<<8 | buf[7]);
1394 sa_size = buf[9]<<16 | buf[10]<<8 | buf[11];
1395 Verbose("#BD: Current/Maximum capacity: %d blocks, %d spare area\n", size, sa_size);
1396
1397 n_lists = buf[3] / 8;
1398 n_lists--;
1399
1400 if(!n_lists)
1401 { FreeAlignedBuffer(ab);
1402 return FALSE;
1403 }
1404
1405 /* Now go through all capacity lists */
1406
1407 for(i=0, idx=12; i<n_lists; i++, idx+=8)
1408 { gint64 size;
1409
1410 size = (gint64)(buf[idx]<<24 | buf[idx+1]<<16 | buf[idx+2]<<8 | buf[idx+3]);
1411 sa_size = buf[idx+5]<<16 | buf[idx+6]<<8 | buf[idx+7];
1412 Verbose("#BD: Cap list %d - type %x, size %lld, spare %d\n",
1413 i, buf[idx+4]>>2, size, sa_size);
1414
1415 switch(buf[idx+4]>>2) /* format type */
1416 { case 0x00: /* all media */
1417 // FreeAlignedBuffer(ab);
1418 dh->blankCapacity = size;
1419 // return TRUE;
1420 }
1421 }
1422 }
1423
1424 FreeAlignedBuffer(ab);
1425 return TRUE;
1426 }
1427
1428 /*
1429 * Find out whether the drive can do C2 scans.
1430 * Done by empirically trying a READ CD;
1431 * maybe we should examine the drive feature codes instead.
1432 */
1433
try_c2_scan(DeviceHandle * dh)1434 static void try_c2_scan(DeviceHandle *dh)
1435 { Sense *sense = &dh->sense;
1436 unsigned char cdb[MAX_CDB_SIZE];
1437 RawBuffer *rb;
1438 int lba = 0;
1439 int ret;
1440
1441 dh->canC2Scan = FALSE;
1442
1443 if(!(rb=dh->rawBuffer)) /* sanity check: No C2 scans without RAW reading */
1444 return;
1445
1446 memset(cdb, 0, MAX_CDB_SIZE);
1447 cdb[0] = 0xbe; /* READ CD */
1448
1449 switch(dh->subType) /* Expected sector type */
1450 { case DATA1: /* data mode 1 */
1451 cdb[1] = 2<<2;
1452 cdb[9] = 0xba; /* we want Sync + Header + User data + EDC/ECC + C2 */
1453 break;
1454
1455 case XA21: /* xa mode 2 form 1 */
1456 cdb[1] = 4<<2;
1457 cdb[9] = 0xfc;
1458 break;
1459 }
1460
1461 cdb[2] = (lba >> 24) & 0xff;
1462 cdb[3] = (lba >> 16) & 0xff;
1463 cdb[4] = (lba >> 8) & 0xff;
1464 cdb[5] = lba & 0xff;
1465 cdb[6] = 0; /* number of sectors to read (3 bytes) */
1466 cdb[7] = 0;
1467 cdb[8] = 1; /* read 1 sector */
1468
1469 cdb[10] = 0; /* reserved stuff */
1470 cdb[11] = 0; /* no special wishes for the control byte */
1471
1472 ret = SendPacket(dh, cdb, 12, rb->workBuf->buf, CD_RAW_C2_SECTOR_SIZE, sense, DATA_READ);
1473
1474 Verbose("C2 scan probing: %d (%s); Sense: %s\n",
1475 ret, !ret ? "good" : "failed",
1476 GetSenseString(sense->sense_key, sense->asc, sense->ascq, 0));
1477
1478 if(!ret || sense->sense_key != 5) /* good status or error != illegal request means C2 works */
1479 { rb->sampleSize = CD_RAW_C2_SECTOR_SIZE;
1480 dh->canC2Scan = TRUE;
1481 return;
1482 }
1483 else
1484 { rb->sampleSize = CD_RAW_SECTOR_SIZE;
1485 dh->canC2Scan = FALSE;
1486 return;
1487 }
1488 }
1489
1490 /*
1491 * Find out whether the drive can return raw sectors with
1492 * uncorrected read errors. Depending on override,
1493 * mode is or'ed with existing flags (override = FALSE),
1494 * or written over existing flags (override = TRUE).
1495 */
1496
1497 /*
1498 * Read mode page 1
1499 */
1500
read_mode_page(DeviceHandle * dh,AlignedBuffer * ab,int * parameter_list_length,unsigned char * read_mode,unsigned char * read_retries)1501 static int read_mode_page(DeviceHandle *dh, AlignedBuffer *ab, int *parameter_list_length,
1502 unsigned char *read_mode, unsigned char *read_retries)
1503 { Sense sense;
1504 unsigned char cdb[16];
1505 unsigned char *buf = ab->buf;
1506 int ret;
1507
1508 memset(cdb, 0, MAX_CDB_SIZE);
1509 cdb[0] = 0x5a; /* MODE SENSE(10) */
1510 cdb[2] = 1; /* Page code */
1511 cdb[8] = 252; /* Allocation length */
1512 ret = SendPacket(dh, cdb, 10, buf, 252, &sense, DATA_READ);
1513
1514 if(ret<0)
1515 { Verbose("\nRead mode page 01h failed: %d (%s); Sense: %s\n",
1516 ret, !ret ? "good" : "failed",
1517 GetSenseString(sense.sense_key, sense.asc, sense.ascq, 0));
1518 return FALSE;
1519 }
1520
1521 Verbose("\nMode page 01h:\n");
1522 Verbose(" mode data length = %d\n", buf[0]<<8 | buf[1]);
1523 Verbose(" block descriptor length = %d\n", buf[6]<<8 | buf[7]);
1524 Verbose(" page byte 0 = %2x\n", buf[8]);
1525 Verbose(" page byte 1 = %2x\n", buf[9]);
1526 Verbose(" page byte 2 = %2x\n", buf[10]);
1527 Verbose(" page byte 3 = %2x\n", buf[11]);
1528
1529 *parameter_list_length = buf[1] + 2; /* mode data length + 2 */
1530 *read_mode = buf[10];
1531 *read_retries = buf[11];
1532
1533 Verbose(" using mode data length %d\n", *parameter_list_length);
1534 return TRUE;
1535 }
1536
set_mode_page(DeviceHandle * dh,AlignedBuffer * ab,int parameter_list_length,unsigned char read_mode,unsigned char read_retries)1537 static int set_mode_page(DeviceHandle *dh, AlignedBuffer *ab, int parameter_list_length,
1538 unsigned char read_mode, unsigned char read_retries)
1539 { Sense sense;
1540 unsigned char cdb[16];
1541 int ret;
1542
1543 memset(cdb, 0, MAX_CDB_SIZE);
1544 cdb[0] = 0x55; /* MODE SELECT(10) */
1545 cdb[1] = 0x10; /* set page format (PF) bit */
1546 cdb[7] = 0;
1547 cdb[8] = parameter_list_length;
1548
1549 ab->buf[10] = read_mode;
1550 ab->buf[11] = read_retries;
1551
1552 ret = SendPacket(dh, cdb, 10, ab->buf, parameter_list_length, &sense, DATA_WRITE);
1553
1554 if(ret<0)
1555 { Verbose("Setting mode page 01h to 0x%2x failed: %s\n", read_mode,
1556 GetSenseString(sense.sense_key, sense.asc, sense.ascq, 0));
1557 return FALSE;
1558 }
1559
1560 return TRUE;
1561 }
1562
SetRawMode(DeviceHandle * dh,int action)1563 void SetRawMode(DeviceHandle *dh, int action)
1564 { AlignedBuffer *ab = CreateAlignedBuffer(2048);
1565 unsigned char new_read_mode, new_read_retries;
1566 unsigned char drive_read_mode, ignore;
1567 int pll;
1568
1569 dh->canReadDefective = FALSE;
1570
1571 /*** If MODE_PAGE_UNSET, apply the old settings */
1572
1573 if(action == MODE_PAGE_UNSET)
1574 { unsigned char ignore;
1575
1576 if(!read_mode_page(dh, ab, &pll, &ignore, &ignore))
1577 { FreeAlignedBuffer(ab);
1578 return;
1579 }
1580
1581 if(!set_mode_page(dh, ab, pll, dh->previousReadMode, dh->previousRetries))
1582 { FreeAlignedBuffer(ab);
1583 return;
1584 }
1585
1586 if(!read_mode_page(dh, ab, &pll, &dh->currentReadMode, &ignore))
1587 { FreeAlignedBuffer(ab);
1588 return;
1589 }
1590
1591 FreeAlignedBuffer(ab);
1592 return;
1593 }
1594
1595 /*** Otherwise we have MODE_PAGE_SET. Set new raw reading mode */
1596
1597 /* Remember current settings */
1598
1599 if(!read_mode_page(dh, ab, &pll, &dh->previousReadMode, &dh->previousRetries))
1600 { FreeAlignedBuffer(ab);
1601 return;
1602 }
1603
1604 /* Try to set the mode page */
1605
1606 new_read_mode = dh->previousReadMode | Closure->rawMode;
1607
1608 if(Closure->internalAttempts >= 0)
1609 new_read_retries = Closure->internalAttempts;
1610 else new_read_retries = dh->previousRetries;
1611
1612 Verbose("Trying read mode 0x%02x, %d read attempts.\n",
1613 new_read_mode, new_read_retries);
1614
1615 if(!set_mode_page(dh, ab, pll, new_read_mode, new_read_retries))
1616 goto reset_mode_page;
1617
1618 /* Check if drive accepted the change */
1619
1620 if(!read_mode_page(dh, ab, &pll, &drive_read_mode, &ignore))
1621 goto reset_mode_page;
1622
1623 if(drive_read_mode != new_read_mode)
1624 { Verbose("Setting raw mode failed: %2x instead of %2x\n",
1625 drive_read_mode, new_read_mode);
1626 goto reset_mode_page;
1627 }
1628
1629 dh->rawBuffer->sampleSize = CD_RAW_SECTOR_SIZE;
1630 dh->currentReadMode = drive_read_mode;
1631 dh->canReadDefective = TRUE;
1632 FreeAlignedBuffer(ab);
1633 return;
1634
1635 reset_mode_page:
1636 Verbose("Resetting mode page 01h.\n");
1637 // set_mode_page(dh, ab, pll, dh->previousReadMode, dh->previousRetries);
1638 /* Using the new read retries wont hurt */
1639 set_mode_page(dh, ab, pll, dh->previousReadMode, new_read_retries);
1640 FreeAlignedBuffer(ab);
1641 }
1642
1643 /*
1644 * Find out whether we are allowed to create an image from the DVD.
1645 */
1646
query_copyright(DeviceHandle * dh)1647 static int query_copyright(DeviceHandle *dh)
1648 { Sense sense;
1649 AlignedBuffer *ab = CreateAlignedBuffer(2048);
1650 unsigned char *buf = ab->buf;
1651 unsigned char cmd[MAX_CDB_SIZE];
1652 unsigned char result;
1653 unsigned int length;
1654
1655 /* The following test does neither apply nor is necessary
1656 for incomplete (damaged) discs. */
1657
1658 if(dh->incomplete) return FALSE;
1659
1660 /* Query length of returned data */
1661
1662 memset(cmd, 0, MAX_CDB_SIZE);
1663 cmd[0] = 0xad; /* READ DVD STRUCTURE */
1664 cmd[6] = 0; /* First layer */
1665 cmd[7] = 1; /* We want copyright info */
1666 cmd[8] = 0; /* Allocation length */
1667 cmd[9] = MIN_TRANSFER_LEN;
1668
1669 if(SendPacket(dh, cmd, 12, buf, MIN_TRANSFER_LEN, &sense, DATA_READ)<0)
1670 { FreeAlignedBuffer(ab);
1671 Stop(_("%s\nCould not query dvd structure length for format code 1.\n"),
1672 GetSenseString(sense.sense_key, sense.asc, sense.ascq, TRUE));
1673 return TRUE;
1674 }
1675
1676 length = buf[0]<<8 | buf[1];
1677 length += 2;
1678 length_align(&length);
1679
1680 if(length>4096) /* don't let the drive hack us using a buffer overflow ;-) */
1681 { FreeAlignedBuffer(ab);
1682 Stop(_("Could not query dvd copyright info - implausible packet length %d\n"),length);
1683 return TRUE;
1684 }
1685
1686 /* Do the real query */
1687
1688 memset(cmd, 0, MAX_CDB_SIZE);
1689 cmd[0] = 0xad; /* READ DVD STRUCTURE */
1690 cmd[6] = 0; /* First layer */
1691 cmd[7] = 1; /* We want copyright info */
1692 cmd[8] = (length>>8) & 0xff; /* Allocation length */
1693 cmd[9] = length & 0xff;
1694
1695 if(SendPacket(dh, cmd, 12, buf, length, &sense, DATA_READ)<0)
1696 { FreeAlignedBuffer(ab);
1697 Stop(_("%s\nCould not query copyright info.\n"),
1698 GetSenseString(sense.sense_key, sense.asc, sense.ascq, TRUE));
1699 return TRUE;
1700 }
1701
1702 result = buf[4] & 0x11;
1703 FreeAlignedBuffer(ab);
1704
1705 return result;
1706 }
1707
1708 /*
1709 * See whether a sector lies within the user area.
1710 */
1711
check_sector(DeviceHandle * dh,GString * msg_out,guint64 sector,int n_sectors)1712 static int check_sector(DeviceHandle *dh, GString *msg_out, guint64 sector, int n_sectors)
1713 { AlignedBuffer *scratch = CreateAlignedBuffer(MAX_CLUSTER_SIZE);
1714 int status,result;
1715 char *msg;
1716
1717 if(sector<2) return 4;
1718
1719 status = read_dvd_sector(dh, scratch->buf, sector, n_sectors);
1720 FreeAlignedBuffer(scratch);
1721
1722 if(!status) /* Sector was readable */
1723 { msg = _("readable");
1724 result = 0;
1725 }
1726 else /* Read error */
1727 { msg = GetLastSenseString(FALSE);
1728
1729 if( dh->sense.sense_key == 0x05 /* illegal request */
1730 || dh->sense.sense_key == 0x00) /* or None; happens at least on Pioneer drives */
1731 { if( dh->sense.asc == 0x63
1732 && dh->sense.ascq == 0x00)
1733 result = 1; /* end of user area */
1734 else result = 2; /* other illegal request, */
1735 /* usually 0x21 Logical block address out of range*/
1736 }
1737 else result = 3; /* other read error */
1738 }
1739
1740 if(n_sectors == 1)
1741 g_string_append_printf(msg_out, _("Sector %lld: %s\n"), sector, msg);
1742 else g_string_append_printf(msg_out, _("Sectors %lld-%lld: %s\n"),
1743 sector, sector+n_sectors-1, msg);
1744
1745 return result;
1746 }
1747
1748 /*
1749 * Try to find out if the given sectors really lie
1750 * at the end of the user area.
1751 */
1752
evaluate_results(int res0,int res1,int * result,char ** msg)1753 static void evaluate_results(int res0, int res1, int *result, char **msg)
1754 {
1755 if(res0 == 3 || res1 == 3)
1756 { *result = FALSE;
1757 *msg = _("is undecideable because of read error");
1758 return;
1759 }
1760
1761 if(res0 == 0 && (res1 == 1 || res1 == 2))
1762 { *result = TRUE;
1763 *msg = _("looks GOOD");
1764 return;
1765 }
1766
1767 if(res0 == 1 && res1 == 2)
1768 { *result = FALSE;
1769 *msg = _("gives unformatted size (UNUSABLE)");
1770 return;
1771 }
1772
1773 *result = FALSE;
1774 *msg = _("is UNUSABLE");
1775 }
1776
1777 /*
1778 * Do a READ CAPACITY.
1779 * Needs to be separated as we need this value to invoke the RS02/RS03
1780 * size heuristics which in turn must be executed before calling query_size().
1781 */
1782
read_capacity(Image * image)1783 static void read_capacity(Image *image)
1784 { DeviceHandle *dh = image->dh;
1785 Sense sense;
1786 AlignedBuffer *ab = CreateAlignedBuffer(2048);
1787 unsigned char *buf = ab->buf;
1788 unsigned char cmd[MAX_CDB_SIZE];
1789 int implausible = FALSE;
1790
1791 /*** Query size by doing READ CAPACITY */
1792
1793 Verbose("# *** read_capacity(%s) ***\n", dh->devinfo);
1794
1795 memset(cmd, 0, MAX_CDB_SIZE);
1796 cmd[0] = 0x25; /* READ CAPACITY */
1797
1798 if(SendPacket(dh, cmd, 10, buf, 8, &sense, DATA_READ)<0)
1799 { FreeAlignedBuffer(ab);
1800 Stop(_("%s\nCould not query medium size.\n"),
1801 GetSenseString(sense.sense_key, sense.asc, sense.ascq, TRUE));
1802 dh->readCapacity = 0;
1803 return;
1804 }
1805
1806 dh->readCapacity = (gint64)(buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3]);
1807 Verbose(" -> %lld\n", dh->readCapacity);
1808 FreeAlignedBuffer(ab);
1809
1810 /*** Validate capacity */
1811
1812 if(dh->mainType == CD && dh->readCapacity > MAX_CDR_SIZE)
1813 implausible = TRUE;
1814
1815 if(dh->mainType == DVD && dh->layers == 1 && dh->readCapacity > MAX_DVD_SL_SIZE)
1816 implausible = TRUE;
1817
1818 if(dh->mainType == DVD && dh->layers == 2 && dh->readCapacity > MAX_DVD_DL_SIZE)
1819 implausible = TRUE;
1820
1821 if(implausible && !dh->simImage)
1822 { LogWarning(_("READ CAPACITY: implausible medium size, %lld sectors\n"),
1823 (gint64)dh->readCapacity);
1824 dh->readCapacity = 0;
1825 }
1826 }
1827
1828 /*
1829 * Query the medium size.
1830 */
1831
query_size(Image * image)1832 static gint64 query_size(Image *image)
1833 { DeviceHandle *dh = image->dh;
1834
1835 Verbose("# *** query_size(%s) ***\n", dh->devinfo);
1836
1837 /*** If an EccHeader was already found and the codec specified
1838 the overall image size, use that as an authoritative source
1839 for the medium size. */
1840
1841 if(image->expectedSectors > 0)
1842 { Verbose("Medium size obtained from ECC header: %lld sectors\n", image->expectedSectors);
1843 return image->expectedSectors;
1844 }
1845 else Verbose("Medium size could NOT be determined from ECC header.\n");
1846
1847 /*** Try getting the size from the ISO/UDF filesystem. */
1848
1849 if(image->isoInfo)
1850 { if(Closure->ignoreIsoSize)
1851 { Verbose("Note: NOT examining ISO/UDF filesystem as requested by user option!\n");
1852 }
1853 else
1854 { Verbose("Medium size obtained from ISO/UDF file system: %d sectors\n",
1855 image->isoInfo->volumeSize);
1856 return image->isoInfo->volumeSize;
1857 }
1858 }
1859 else Verbose("Medium size could NOT be determined from ISO/UDF filesystem.\n");
1860
1861 /*** If everything else fails, query the drive. */
1862
1863 /* For CD media, thats all we have to do */
1864
1865 if(dh->mainType == CD)
1866 { Verbose("CD medium - using size from READ CAPACITY: %lld sectors\n", dh->readCapacity+1);
1867 return dh->readCapacity+1; /* size is the number of the last sector, starting with 0 */
1868 }
1869
1870 /* BD drives have neither dh->userAreaSize nor will the following heuristics
1871 work as unformatted sectors can be always read. Stick with READ CAPACITY. */
1872
1873 if(dh->mainType == BD)
1874 { Verbose("BD medium - using size from READ CAPACITY: %lld sectors\n", dh->readCapacity+1);
1875 return dh->readCapacity+1; /* size is the number of the last sector, starting with 0 */
1876 }
1877
1878 /* For DVD media, READ CAPACITY should give the real image size.
1879 READ DVD STRUCTURE may be same value or the unformatted size.
1880 But some drives appear to have both functions reversed,
1881 so we have to be careful here. */
1882
1883 if(dh->readCapacity == dh->userAreaSize) /* If they are equal just return one */
1884 { Verbose("READ CAPACITY and READ DVD STRUCTURE agree: %lld sectors\n", dh->readCapacity+1);
1885 return dh->readCapacity+1;
1886 }
1887 else /* Tricky case. Try some heuristics. */
1888 { int last_valid, first_out;
1889 gint test_sector;
1890 int cap_result,strct_result,decision;
1891 char *cap_msg, *strct_msg, *decision_msg;
1892 GString *warning;
1893
1894 /*** Try to find out the correct value empirically. */
1895
1896 warning = g_string_sized_new(1024);
1897 g_string_printf(warning,
1898 _("Different media sizes depending on query method:\n"
1899 "READ CAPACITY: %lld sectors\n"
1900 "READ DVD STRUCTURE: %lld sectors\n\n"),
1901 dh->readCapacity+1, dh->userAreaSize+1);
1902
1903 g_string_append(warning, _("Evaluation of returned medium sizes:\n\n"));
1904
1905 /*** Look at READ CAPACITY results */
1906
1907 test_sector = dh->readCapacity;
1908 last_valid = check_sector(dh, warning, test_sector, 1);
1909 first_out = check_sector(dh, warning, test_sector+1, 1);
1910
1911 /*** Some drives can read about 16 or 32 sectors past the image
1912 into the border/lead out. Drats. */
1913
1914 if(last_valid == 0 && first_out == 0)
1915 { test_sector &= ~((long long)15);
1916 first_out = check_sector(dh, warning, test_sector, 16);
1917 if(!first_out)
1918 first_out = check_sector(dh, warning, test_sector+16, 16);
1919 if(!first_out)
1920 first_out = check_sector(dh, warning, test_sector+32, 16);
1921 if(!first_out)
1922 first_out = check_sector(dh, warning, test_sector+48, 16);
1923 }
1924
1925 evaluate_results(last_valid, first_out, &cap_result, &cap_msg);
1926 g_string_append_printf(warning, "-> READ CAPACITY %s\n\n", cap_msg);
1927
1928 /*** Look at READ DVD STRUCTURE results */
1929
1930 test_sector = dh->userAreaSize;
1931 last_valid = check_sector(dh, warning, test_sector, 1);
1932 first_out = check_sector(dh, warning, test_sector+1, 1);
1933
1934 /*** Some drives can read about 16 or 32 sectors past the image
1935 into the border/lead out. Drats. */
1936
1937 if(last_valid == 0 && first_out == 0)
1938 { test_sector &= ~((long long)15);
1939 first_out = check_sector(dh, warning, test_sector, 16);
1940 if(!first_out)
1941 first_out = check_sector(dh, warning, test_sector+16, 16);
1942 if(!first_out)
1943 first_out = check_sector(dh, warning, test_sector+32, 16);
1944 if(!first_out)
1945 first_out = check_sector(dh, warning, test_sector+48, 16);
1946 }
1947
1948 evaluate_results(last_valid, first_out, &strct_result, &strct_msg);
1949 g_string_append_printf(warning, "-> READ DVD STRUCTURE %s\n\n", strct_msg);
1950
1951 /*** Decide what to use */
1952
1953 decision = 0;
1954 decision_msg = NULL;
1955
1956 if(cap_result)
1957 { decision = 1;
1958 decision_msg = _("Using value from READ CAPACITY");
1959 }
1960 if(strct_result)
1961 { decision = 2;
1962 decision_msg = _("Using value from READ DVD STRUCTURE");
1963 }
1964
1965 if(!decision)
1966 { if(!dh->readCapacity) decision = 2;
1967 if(!dh->userAreaSize) decision = 1;
1968
1969 if(dh->readCapacity && dh->userAreaSize)
1970 decision = dh->readCapacity < dh->userAreaSize ? 1 : 2;
1971
1972 decision_msg = _("FAILED to determine image size.\n"
1973 "Using smaller value as this is right on >90%% of all drives,\n"
1974 "but CONTINUE AT YOUR OWN RISK (the image may be incomplete/unusable)");
1975 }
1976
1977 g_string_append_printf(warning, _("Final decision: %s\n\n"), decision_msg);
1978 LogWarning(warning->str);
1979
1980 g_string_free(warning, TRUE);
1981
1982 switch(decision)
1983 { case 1 : return dh->readCapacity+1;
1984 case 2 : return dh->userAreaSize+1;
1985 default : Stop(_("Failed to determine image size.\n"
1986 "Try using a different drive."));
1987 return 0;
1988 }
1989 }
1990
1991 return 0;
1992 }
1993
1994 /*
1995 * Return size of current drive and medium (if any)
1996 */
1997
CurrentMediumSize(int get_blank_size)1998 gint64 CurrentMediumSize(int get_blank_size)
1999 { Image *image;
2000 gint64 size;
2001
2002 #ifdef SYS_UNKNOWN
2003 return 0;
2004 #endif
2005
2006 image = OpenImageFromDevice(Closure->device);
2007 if(!image) return 0;
2008 if(InquireDevice(image->dh, 1) != 0x05)
2009 { CloseImage(image);
2010 return 0;
2011 }
2012
2013 /* Avoid size query on unknown media */
2014
2015 if(!query_type(image->dh, 1) && !get_blank_size)
2016 { CloseImage(image);
2017 return 0;
2018 }
2019
2020 /* A size query on unsupported media would derail */
2021
2022 if((image->dh->subType == UNSUPPORTED) && !get_blank_size)
2023 { CloseImage(image);
2024 return 0;
2025 }
2026
2027 /* We can return either the image size or the size of blank media. */
2028
2029 if(get_blank_size)
2030 { if(!query_blank(image->dh))
2031 { CloseImage(image);
2032 return 0;
2033 }
2034
2035 size = image->dh->blankCapacity;
2036 }
2037 else
2038 { read_capacity(image);
2039 size = query_size(image);
2040 }
2041
2042 CloseImage(image);
2043
2044 return size;
2045 }
2046
2047 /***
2048 *** Optional drive settings
2049 ***/
2050
2051 /*
2052 * Spin up drive.
2053 * Most drives give a *beep* about sending the START STOP CDB,
2054 * so we simply nudge them with reading request until the spin up
2055 * time is over. Pathetic ;-)
2056 */
2057
SpinupDevice(DeviceHandle * dh)2058 void SpinupDevice(DeviceHandle *dh)
2059 { AlignedBuffer *ab;
2060 GTimer *timer;
2061 gint64 s;
2062
2063 if(!Closure->spinupDelay)
2064 return;
2065
2066 PrintCLI(_("Waiting %d seconds for drive to spin up...\n"), Closure->spinupDelay);
2067
2068 ab = CreateAlignedBuffer(MAX_CLUSTER_SIZE);
2069
2070 timer = g_timer_new();
2071 g_timer_start(timer);
2072
2073 for(s=0; ;s+=dh->clusterSize)
2074 { int status;
2075 double elapsed;
2076 gulong ignore;
2077
2078 if(s>=dh->sectors) return;
2079
2080 status = ReadSectorsFast(dh, ab->buf, s, dh->clusterSize);
2081 if(status) return;
2082
2083 elapsed = g_timer_elapsed(timer, &ignore);
2084 if(elapsed > Closure->spinupDelay)
2085 break;
2086 }
2087
2088 FreeAlignedBuffer(ab);
2089 g_timer_destroy(timer);
2090 }
2091
2092 /*
2093 * Load/eject the medium
2094 */
2095
LoadMedium(DeviceHandle * dh,int load)2096 void LoadMedium(DeviceHandle *dh, int load)
2097 { Sense sense;
2098 unsigned char cmd[MAX_CDB_SIZE];
2099
2100 /* Try to load or eject the medium */
2101
2102 memset(cmd, 0, MAX_CDB_SIZE);
2103 cmd[0] = 0x1b; /* START STOP */
2104 cmd[4] = load ? 0x03 : 0x02; /* LOEJ=1; START=load/eject */
2105
2106 if(SendPacket(dh, cmd, 6, NULL, 0, &sense, DATA_NONE)<0
2107 && (sense.asc != 0x53 || sense.ascq != 0x02))
2108 {
2109 PrintLog(_("%s\nCould not load/unload the medium.\n"),
2110 GetSenseString(sense.sense_key, sense.asc, sense.ascq, TRUE));
2111 return;
2112 }
2113
2114 if(load) return;
2115
2116 /* Some drives lock the tray when it was closed via START STOP.
2117 That sucks especially under newer GNU/Linux kernels as we need to
2118 be root to unlock it. Try anyways; maybe this changes in
2119 future kernels. */
2120
2121 memset(cmd, 0, MAX_CDB_SIZE);
2122 cmd[0] = 0x1e; /* PREVENT ALLOW MEDIUM REMOVAL */
2123
2124 if(SendPacket(dh, cmd, 6, NULL, 0, &sense, DATA_NONE)<0)
2125 PrintLog(_("%s\nCould not unlock the medium.\n"),
2126 GetSenseString(sense.sense_key, sense.asc, sense.ascq, TRUE));
2127
2128 /* Try ejecting again */
2129
2130 memset(cmd, 0, MAX_CDB_SIZE);
2131 cmd[0] = 0x1b; /* START STOP */
2132 cmd[4] = 0x02; /* LOEJ=1; START=eject */
2133
2134 if(SendPacket(dh, cmd, 6, NULL, 0, &sense, DATA_NONE)<0)
2135 PrintLog(_("%s\nCould not load/unload the medium.\n"),
2136 GetSenseString(sense.sense_key, sense.asc, sense.ascq, TRUE));
2137 }
2138
2139 /*
2140 * Wait for the drive to become ready
2141 */
2142
TestUnitReady(DeviceHandle * dh)2143 int TestUnitReady(DeviceHandle *dh)
2144 { unsigned char cmd[MAX_CDB_SIZE];
2145 int i;
2146
2147 /*** Send TEST UNIT READY, return if drive says go */
2148
2149 memset(cmd, 0, MAX_CDB_SIZE);
2150 cmd[0] = 0x00; /* TEST UNIT READY */
2151
2152 if(SendPacket(dh, cmd, 6, NULL, 0, &dh->sense, DATA_NONE) != -1)
2153 return TRUE;
2154
2155 /*** If no medium present, try closing the tray. */
2156
2157 if( dh->sense.sense_key == 2 /* Not Ready */
2158 && dh->sense.asc == 0x3a) /* Medium not present */
2159 LoadMedium(dh, TRUE);
2160
2161 /*** Wait 10 seconds for drives reporting that they are
2162 becoming ready */
2163
2164 for(i=0; i<10; i++)
2165 { int continue_waiting = FALSE;
2166
2167 memset(cmd, 0, MAX_CDB_SIZE);
2168 cmd[0] = 0x00; /* TEST UNIT READY */
2169
2170 if(SendPacket(dh, cmd, 6, NULL, 0, &dh->sense, DATA_NONE) != -1)
2171 { if(Closure->guiMode)
2172 SetLabelText(Closure->status, "");
2173 return TRUE;
2174 }
2175
2176 if(dh->sense.sense_key == 2) /* Not Ready */
2177 if( (dh->sense.asc == 0x04 && dh->sense.ascq == 0x01) /* but in process of becoming so */
2178 || dh->sense.asc == 0x3a /* flawed firmware workaround */
2179 )
2180 continue_waiting = TRUE;
2181
2182 if(dh->sense.sense_key == 6) /* Unit attention */
2183 if(dh->sense.asc == 0x28 && dh->sense.ascq == 0x00)
2184 continue_waiting = TRUE;
2185
2186 if(continue_waiting)
2187 { PrintCLIorLabel(Closure->status,
2188 _("Waiting 10 seconds for drive: %d\n"),9-i);
2189
2190 if(Closure->stopActions)
2191 return FALSE;
2192
2193 g_usleep(G_USEC_PER_SEC);
2194 continue;
2195 }
2196
2197 Verbose("fell out waiting for drive %x %x %x\n",dh->sense.sense_key,dh->sense.asc,dh->sense.ascq);
2198 break; /* Something is wrong with the drive */
2199 }
2200
2201 if(Closure->guiMode)
2202 SetLabelText(Closure->status, "");
2203
2204 return FALSE;
2205 }
2206
2207 /*
2208 * Set the reading speed. Does not work reliably yet.
2209 */
2210
2211 /***
2212 *** Sector reading routines
2213 ***/
2214
2215 /*
2216 * Sector reading using the packet interface.
2217 */
2218
read_dvd_sector(DeviceHandle * dh,unsigned char * buf,int lba,int nsectors)2219 static int read_dvd_sector(DeviceHandle *dh, unsigned char *buf, int lba, int nsectors)
2220 { Sense *sense = &dh->sense;
2221 unsigned char cmd[MAX_CDB_SIZE];
2222 int ret;
2223
2224 memset(cmd, 0, MAX_CDB_SIZE);
2225 cmd[0] = 0x28; /* READ(10) */
2226 cmd[1] = 0; /* no special flags */
2227 cmd[2] = (lba >> 24) & 0xff;
2228 cmd[3] = (lba >> 16) & 0xff;
2229 cmd[4] = (lba >> 8) & 0xff;
2230 cmd[5] = lba & 0xff;
2231 cmd[6] = 0; /* reserved */
2232 cmd[7] = 0; /* number of sectors */
2233 cmd[8] = nsectors; /* read 1 sector */
2234
2235 ret = SendPacket(dh, cmd, 10, buf, 2048*nsectors, sense, DATA_READ);
2236
2237 if(ret<0) RememberSense(sense->sense_key, sense->asc, sense->ascq);
2238
2239 return ret;
2240 }
2241
read_cd_sector(DeviceHandle * dh,unsigned char * buf,int lba,int nsectors)2242 static int read_cd_sector(DeviceHandle *dh, unsigned char *buf, int lba, int nsectors)
2243 { Sense *sense = &dh->sense;
2244 unsigned char cmd[MAX_CDB_SIZE];
2245 int ret;
2246
2247 memset(cmd, 0, MAX_CDB_SIZE);
2248 cmd[0] = 0xbe; /* READ CD */
2249 switch(dh->subType)
2250 { case DATA1: cmd[1] = 2<<2; break; /* data mode 1 */
2251 case XA21: cmd[1] = 4<<2; break; /* xa mode 2 form 1 */
2252 }
2253 cmd[2] = (lba >> 24) & 0xff;
2254 cmd[3] = (lba >> 16) & 0xff;
2255 cmd[4] = (lba >> 8) & 0xff;
2256 cmd[5] = lba & 0xff;
2257 cmd[6] = 0; /* number of sectors to read (3 bytes) */
2258 cmd[7] = 0;
2259 cmd[8] = nsectors; /* read nsectors */
2260
2261 cmd[9] = 0x10; /* we want the user data only */
2262 cmd[10] = 0; /* reserved stuff */
2263 cmd[11] = 0; /* no special wishes for the control byte */
2264
2265 ret = SendPacket(dh, cmd, 12, buf, 2048*nsectors, sense, DATA_READ);
2266
2267 #if 0
2268 #define BORK 34999
2269 if(lba<=BORK && BORK<lba+nsectors) /* fixme */
2270 { int offset = 2048*(BORK-lba);
2271 buf[offset+240]^=255;
2272 }
2273 #endif
2274
2275 #if 0
2276 #define BORK2 300
2277 if(lba<=BORK2 && BORK2<lba+nsectors) /* fixme */
2278 { int offset = 2048*(BORK2-lba);
2279 buf[offset+240]^=255;
2280 }
2281 #endif
2282
2283 if(ret<0) RememberSense(sense->sense_key, sense->asc, sense->ascq);
2284 return ret;
2285 }
2286
read_raw_cd_sector(DeviceHandle * dh,unsigned char * outbuf,int lba,int nsectors)2287 static int read_raw_cd_sector(DeviceHandle *dh, unsigned char *outbuf, int lba, int nsectors)
2288 { Sense *sense = &dh->sense;
2289 unsigned char cdb[MAX_CDB_SIZE];
2290 unsigned char c2bit;
2291 RawBuffer *rb;
2292 int ret = -1;
2293 int i,s;
2294 int offset = 16;
2295
2296 /* Sanity checks */
2297
2298 if(!dh->rawBuffer)
2299 { RememberSense(3, 255, 4); /* RAW buffer not allocated */
2300 return -1;
2301 }
2302
2303 if(nsectors > dh->clusterSize)
2304 { RememberSense(3, 255, 3); /* RAW reading > cluster size not supported */
2305 return -1;
2306 }
2307
2308 rb = dh->rawBuffer;
2309 rb->lba = lba;
2310
2311 /*** Perform the raw read */
2312
2313 c2bit = dh->canC2Scan ? 0x02 : 0;
2314 memset(cdb, 0, MAX_CDB_SIZE);
2315 cdb[0] = 0xbe; /* READ CD */
2316 switch(dh->subType) /* Expected sector type */
2317 { case DATA1: /* data mode 1 */
2318 cdb[1] = 2<<2;
2319 cdb[9] = 0xb8 | c2bit; /* we want Sync + Header + User data + EDC/ECC + C2 */
2320 offset=16;
2321 break;
2322
2323 case XA21: /* xa mode 2 form 1 */
2324 cdb[1] = 4<<2;
2325 cdb[9] = 0xf8 | c2bit;
2326 offset=24;
2327 break;
2328 }
2329
2330 cdb[2] = (lba >> 24) & 0xff;
2331 cdb[3] = (lba >> 16) & 0xff;
2332 cdb[4] = (lba >> 8) & 0xff;
2333 cdb[5] = lba & 0xff;
2334 cdb[6] = 0; /* number of sectors to read (3 bytes) */
2335 cdb[7] = 0;
2336 cdb[8] = nsectors; /* read nsectors */
2337
2338 cdb[10] = 0; /* reserved stuff */
2339 cdb[11] = 0; /* no special wishes for the control byte */
2340
2341 /* initialize sectors with our marker */
2342
2343 for(i=0, s=0; i<nsectors; i++, s+=rb->sampleSize)
2344 CreateMissingSector(rb->workBuf->buf+s, lba+i, NULL, 0,
2345 "read_raw_cd_sector() dummy sector");
2346
2347 ret = SendPacket(dh, cdb, 12, rb->workBuf->buf, nsectors*rb->sampleSize, sense, DATA_READ);
2348 RememberSense(sense->sense_key, sense->asc, sense->ascq);
2349
2350 #if 0
2351 if(lba==16000) /* fixme */
2352 { rb->workBuf->buf[0*rb->sampleSize+230]^=255;
2353 }
2354 #endif
2355
2356 /* count c2 error bits */
2357
2358 if(dh->canC2Scan)
2359 { for(i=0, s=0; i<nsectors; i++, s+=rb->sampleSize)
2360 dh->c2[i] = CountC2Errors(rb->workBuf->buf+s);
2361 }
2362
2363 /* The drive screws up sometimes and returns a damaged sector as good.
2364 When nsectors==1, this sector is still interesting for data recovery.
2365 We flag it as bad so that the next if() will pick it up correctly. */
2366
2367 if(!ret && nsectors == 1 && !ValidateRawSector(rb, rb->workBuf->buf, "verify incoming"))
2368 { ret = -1;
2369 }
2370
2371 /*** See if defective sector reading is enabled and applicable. */
2372
2373 if(ret<0 && dh->canReadDefective)
2374 {
2375 /* Fail if multiple sectors were requested.
2376 Caller must retry reading single sectors. */
2377
2378 if(nsectors != 1)
2379 return ret;
2380
2381 return TryCDFrameRecovery(rb, outbuf);
2382 }
2383
2384 /*** If we reach this, only the simple L-EC check is requested. */
2385
2386 /* See if drive returned some data at all. */
2387
2388 for(i=0, s=0; i<nsectors; i++, s+=rb->sampleSize)
2389 if(CheckForMissingSector(rb->workBuf->buf+s, lba+i, NULL, 0) != SECTOR_PRESENT)
2390 { if(dh->canReadDefective)
2391 RememberSense(3, 255, 0); /* report that nothing came back */
2392 return -1;
2393 }
2394
2395 /* Verify data and copy it back */
2396
2397 ret = 0;
2398 rb->lba = lba;
2399
2400 for(i=0, s=0; i<nsectors; i++, s+=rb->sampleSize)
2401 { if(!ValidateRawSector(rb, rb->workBuf->buf+s, "verify outgoing"))
2402 return -1;
2403
2404 memcpy(outbuf, rb->workBuf->buf+s+offset, 2048);
2405 outbuf += 2048;
2406 rb->lba++;
2407 }
2408
2409 return ret;
2410 }
2411
2412 /*
2413 * Sector reading through the device handle.
2414 * dh->read dispatches to one the routines above.
2415 */
2416
ReadSectors(DeviceHandle * dh,unsigned char * buf,gint64 s,int nsectors)2417 int ReadSectors(DeviceHandle *dh, unsigned char *buf, gint64 s, int nsectors)
2418 { int retry,status = -1;
2419 int recommended_attempts = Closure->minReadAttempts;
2420
2421 /* See if we are in a simulated defective area */
2422
2423 if(dh->defects)
2424 { gint64 i,idx;
2425
2426 for(idx=s,i=0; i<nsectors; idx++,i++)
2427 if(GetBit(dh->defects, idx) && !((Random() & 15)))
2428 { dh->sense.sense_key = 3;
2429 dh->sense.asc = 255;
2430 dh->sense.ascq = 255;
2431 RememberSense(dh->sense.sense_key, dh->sense.asc, dh->sense.ascq);
2432 return TRUE;
2433 }
2434 }
2435
2436 #if 0
2437 if( (s == 331600 && nsectors > 16)
2438 || s >331605)
2439 { dh->sense.sense_key = 3;
2440 dh->sense.asc = 255;
2441 dh->sense.ascq = 255;
2442 RememberSense(dh->sense.sense_key, dh->sense.asc, dh->sense.ascq);
2443
2444 return TRUE;
2445 }
2446 #endif
2447
2448 /* Reset raw reading buffer (if there is one) */
2449
2450 if(Closure->readRaw && dh->rawBuffer)
2451 { ResetRawBuffer(dh->rawBuffer);
2452 dh->rawBuffer->recommendedAttempts = Closure->minReadAttempts;
2453 }
2454
2455 /* Try normal read */
2456
2457 for(retry=1; retry<=recommended_attempts; retry++)
2458 {
2459 /* Dispatch between normal reader and raw reader */
2460
2461 if(Closure->readRaw && dh->readRaw)
2462 status = dh->readRaw(dh, buf, s, nsectors);
2463 else status = dh->read(dh, buf, s, nsectors);
2464
2465 if(Closure->readRaw && dh->rawBuffer)
2466 recommended_attempts = dh->rawBuffer->recommendedAttempts;
2467
2468 if(status) /* current try was unsucessful */
2469 { int last_key, last_asc, last_ascq;
2470
2471 if(Closure->stopActions) /* user break */
2472 return status;
2473
2474 /* Do not attempt multiple re-reads if nsectors > 1 and sectorSkip == 0
2475 as these will be re-read with nsectors==1 anyways. */
2476
2477 // Why only apply this shortcut to raw reading?
2478 // if(dh->canReadDefective && nsectors > 1 && Closure->sectorSkip == 0)
2479 if(nsectors > 1 && Closure->sectorSkip == 0)
2480 { PrintCLIorLabel(Closure->status,
2481 _("Sectors %lld - %lld: %s\n"),
2482 s, s+nsectors-1, GetLastSenseString(FALSE));
2483 return status;
2484 }
2485
2486 /* Print results of current attempt.
2487 If the error was a wrong MSF in the sector,
2488 print info about the sector which was returned. */
2489
2490 GetLastSense(&last_key, &last_asc, &last_ascq);
2491
2492 if(last_key == 3 && last_asc == 255 && last_ascq == 2 && dh->rawBuffer)
2493 { unsigned char *frame = dh->rawBuffer->workBuf->buf;
2494 PrintCLIorLabel(Closure->status,
2495 _("Sector %lld, try %d: %s Sector returned: %d.\n"),
2496 s, retry, GetLastSenseString(FALSE),
2497 MSFtoLBA(frame[12], frame[13], frame[14]));
2498 }
2499 else
2500 PrintCLIorLabel(Closure->status,
2501 _("Sector %lld, try %d: %s\n"),
2502 s, retry, GetLastSenseString(FALSE));
2503
2504 /* Last attempt; create failure notice */
2505
2506 if(dh->canReadDefective && retry >= recommended_attempts)
2507 RememberSense(3, 255, 7); /* Recovery failed */
2508 }
2509 else /* good return status */
2510 { if(recommended_attempts > 1 && retry > 1)
2511 PrintCLIorLabel(Closure->status,
2512 _("Sector %lld, try %d: success\n"), s, retry);
2513
2514 break;
2515 }
2516 }
2517
2518 /* Update / use the defective sector cache */
2519
2520 if(dh->canReadDefective && status && Closure->defectiveDump)
2521 {
2522 if(SaveDefectiveSector(dh->rawBuffer,dh->canC2Scan)) /* any new sectors? */
2523 { status = TryDefectiveSectorCache(dh->rawBuffer, buf);
2524 if(status)
2525 RememberSense(3, 255, 7); /* Recovery failed */
2526 }
2527 }
2528 return status;
2529 }
2530
2531 /*
2532 * Sector reading through the device handle.
2533 * dh->read dispatches to one the routines above.
2534 * No reading retries and/or raw reading are used.
2535 */
2536
ReadSectorsFast(DeviceHandle * dh,unsigned char * buf,gint64 s,int nsectors)2537 int ReadSectorsFast(DeviceHandle *dh, unsigned char *buf, gint64 s, int nsectors)
2538 { int status = -1;
2539
2540 /* See if we are in a simulated defective area */
2541
2542 if(dh->defects)
2543 { gint64 i,idx;
2544
2545 for(idx=s,i=0; i<nsectors; idx++,i++)
2546 if(GetBit(dh->defects, idx))
2547 { dh->sense.sense_key = 3;
2548 dh->sense.asc = 255;
2549 dh->sense.ascq = 255;
2550 RememberSense(dh->sense.sense_key, dh->sense.asc, dh->sense.ascq);
2551 return TRUE;
2552 }
2553 }
2554
2555 /* Try normal read */
2556
2557 status = dh->read(dh, buf, s, nsectors);
2558
2559 return status;
2560 }
2561
2562 /***
2563 *** Open the device and query some of its properties.
2564 ***/
2565
OpenImageFromDevice(char * device)2566 Image* OpenImageFromDevice(char *device)
2567 { Image *image = NULL;
2568 DeviceHandle *dh = NULL;
2569
2570 /* Open the device. */
2571
2572 Verbose("# *** OpenImageFromDevice(%s) ***\n", device);
2573 dh = OpenDevice(device);
2574 if(!dh) return NULL;
2575
2576 InquireDevice(dh, 0);
2577 Verbose("# InquireDevice returned: %s\n", dh->devinfo);
2578
2579 if(!TestUnitReady(dh))
2580 { if( dh->sense.sense_key == 2 /* Not Ready */
2581 && dh->sense.asc == 0x3a) /* Medium not present */
2582 { CloseDevice(dh);
2583 Stop(_("Device %s: no medium present\n"), device);
2584 return NULL;
2585 }
2586 else
2587 { CloseDevice(dh);
2588 Stop(_("Device %s does not become ready:\n%s\n\n"), device,
2589 GetSenseString(dh->sense.sense_key, dh->sense.asc, dh->sense.ascq, FALSE));
2590 return NULL;
2591 }
2592 }
2593
2594 PrintLog(_("\nDevice: %s, %s\n"),device, dh->devinfo);
2595
2596 /* Query the type and fail immediately if incompatible medium is found
2597 so that the later tests are not derailed by the wrong medium type */
2598
2599 if(!query_type(dh, 0))
2600 { CloseDevice(dh);
2601 Stop(_("Drive failed to report media type."));
2602 return NULL;
2603 }
2604
2605 Verbose("# query_type() returned.\n");
2606
2607 if(dh->subType == UNSUPPORTED)
2608 { char *td = alloca(strlen(dh->typeDescr)+1);
2609
2610 strcpy(td, dh->typeDescr);
2611 CloseDevice(dh);
2612 Stop(_("This software does not support \"%s\" type media."), td);
2613 return NULL;
2614 }
2615
2616 if(dh->sessions>1)
2617 { int sessions = dh->sessions;
2618
2619 CloseDevice(dh);
2620 Stop(_("This software does not support multisession (%d sessions) media."), sessions);
2621 return NULL;
2622 }
2623
2624 /* Create the Image structure.
2625 From here we need it to store additional information about the image. */
2626
2627 image = g_malloc0(sizeof(Image));
2628 image->type = IMAGE_MEDIUM;
2629 image->fpSector = -1;
2630 image->dh = dh;
2631
2632 /* Activate raw reading features if possible,
2633 output selected reading mode */
2634
2635 Verbose("# deciding reading strategy...\n");
2636 switch(dh->mainType)
2637 { case CD:
2638 if(Closure->readRaw)
2639 { dh->rawBuffer = CreateRawBuffer(MAX_RAW_TRANSFER_SIZE);
2640
2641 dh->rawBuffer->validFP = GetImageFingerprint(image, dh->rawBuffer->mediumFP, FINGERPRINT_SECTOR);
2642
2643 if(dh->subType == XA21)
2644 { dh->rawBuffer->dataOffset = 24;
2645 dh->rawBuffer->xaMode = TRUE;
2646 }
2647
2648 SetRawMode(dh, MODE_PAGE_SET);
2649 try_c2_scan(dh);
2650
2651 PrintLog(_("Using READ CD"));
2652 PrintLog(_(", RAW reading"));
2653
2654 if(dh->canReadDefective)
2655 PrintLog(_(", Mode page 1 ERP = %02xh"), dh->currentReadMode);
2656
2657 if(dh->canC2Scan)
2658 PrintLog(_(", C2 scanning"));
2659
2660 } else PrintLog(_("Using READ CD"));
2661 PrintLog(".\n");
2662 break;
2663
2664 default:
2665 PrintLog(_("Using READ(10).\n"));
2666 break;
2667 }
2668
2669 /* Examine medium type */
2670
2671 GetImageFingerprint(image, NULL, FINGERPRINT_SECTOR);
2672 ExamineUDF(image);
2673
2674 read_capacity(image); /* Needed for ExamineECC() ! */
2675 ExamineECC(image);
2676
2677 Verbose("# Calling query_size()\n");
2678 dh->sectors = query_size(image);
2679 Verbose("# returned: %lld sectors\n", dh->sectors);
2680
2681 switch(dh->subType & MAIN_TYPE_MASK)
2682 { case BD:
2683 case DVD:
2684 case CD:
2685 { char *tmp;
2686 if(!image->isoInfo) // || dh->rs02Size > 0)
2687 tmp = g_strdup_printf(_("Medium: %s, %lld sectors%s"),
2688 dh->typeDescr, dh->sectors,
2689 image->expectedSectors ? ", Ecc" : ""); //fixme: validate
2690 else
2691 tmp = g_strdup_printf(_("Medium \"%s\": %s, %lld sectors%s created %s"),
2692 image->isoInfo->volumeLabel,
2693 dh->typeDescr, dh->sectors,
2694 image->expectedSectors ? ", Ecc," : ",", //fixme: validate
2695 image->isoInfo->creationDate);
2696
2697 if(dh->manuID[0] && dh->manuID[0] != '-')
2698 dh->mediumDescr = g_strdup_printf("%s, %s %s.", tmp, _("Manuf.-ID:"), dh->manuID);
2699 else dh->mediumDescr = g_strdup_printf("%s.", tmp);
2700 g_free(tmp);
2701 PrintLog("%s\n\n", dh->mediumDescr);
2702 break;
2703 }
2704
2705 default:
2706 { char *td = alloca(strlen(dh->typeDescr)+1);
2707
2708 strcpy(td, dh->typeDescr);
2709 CloseImage(image);
2710 Stop(_("This software does not support \"%s\" type media."), td);
2711 return NULL;
2712 }
2713 }
2714
2715 if(dh->mainType == DVD && query_copyright(dh))
2716 { CloseImage(image);
2717 Stop(_("This software does not support encrypted media.\n"));
2718 return NULL;
2719 }
2720
2721 /* Create the bitmap of simulated defects */
2722
2723 if(Closure->simulateDefects)
2724 dh->defects = SimulateDefects(dh->sectors);
2725
2726 return image;
2727 }
2728
2729 /***
2730 *** Debugging function for sending a cdb to the drive
2731 ***/
2732
SendReadCDB(char * device,unsigned char * buf,unsigned char * cdb,int cdb_len,int alloc_len)2733 int SendReadCDB(char *device, unsigned char *buf, unsigned char *cdb, int cdb_len, int alloc_len)
2734 { DeviceHandle *dh = NULL;
2735 Sense sense;
2736 int i,status;
2737
2738 dh = OpenDevice(device);
2739 if(!dh) return 0;
2740
2741 InquireDevice(dh, 0);
2742
2743 PrintLog("Sending cdb to device: %s, %s\n", device, dh->devinfo);
2744
2745 PrintLog("cdb:");
2746 for(i=0; i<cdb_len; i++)
2747 PrintLog(" %02x",cdb[i]);
2748 PrintLog(", length %d, allocation length %d\n", cdb_len, alloc_len);
2749
2750 status = SendPacket(dh, cdb, cdb_len, buf, alloc_len, &sense, DATA_READ);
2751
2752 CloseDevice(dh);
2753
2754 if(status < 0)
2755 { PrintLog("\nOperation failed with status = %d\n", status);
2756 PrintLog("Sense key: %02x, asc/ascq: %02x/%02x\n", sense.sense_key, sense.asc, sense.ascq);
2757 PrintLog("%s\n", GetSenseString(sense.sense_key, sense.asc, sense.ascq, FALSE));
2758 }
2759
2760 return status;
2761 }
2762