1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
|
/*
*
* $Id: k3bdevice_mmc.cpp 690628 2007-07-21 16:05:08Z trueg $
* Copyright (C) 2003-2007 Sebastian Trueg <trueg@k3b.org>
*
* This file is part of the K3b project.
* Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* See the file "COPYING" for the exact licensing terms.
*/
/**
This file contains all the MMC command implementations of the K3b device class
to make the code more readable.
**/
#include "k3bdevice.h"
#include "k3bscsicommand.h"
#include "k3bdeviceglobals.h"
#include "k3bdebug.h"
#include <string.h>
bool K3bDevice::Device::testUnitReady() const
{
ScsiCommand cmd( this );
cmd.enableErrorMessages( false );
cmd[0] = MMC_TEST_UNIT_READY;
cmd[5] = 0; // Necessary to set the proper command length
return( cmd.transport() == 0 );
}
bool K3bDevice::Device::getFeature( unsigned char** data, unsigned int& dataLen, unsigned int feature ) const
{
unsigned char header[2048];
::memset( header, 0, 2048 );
ScsiCommand cmd( this );
cmd[0] = MMC_GET_CONFIGURATION;
cmd[1] = 2; // read only specified feature
cmd[2] = feature>>8;
cmd[3] = feature;
cmd[8] = 8; // we only read the data length first
cmd[9] = 0; // Necessary to set the proper command length
// we only read the data length first
dataLen = 8;
if( cmd.transport( TR_DIR_READ, header, 8 ) )
dataLen = from4Byte( header ) + 4;
else
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName() << ": GET CONFIGURATION length det failed." << endl;
//
// Some buggy firmwares do not return the size of the available data
// but the returned data or something invalid altogether.
// So we simply use the maximum possible value to be on the safe side
// with these buggy drives.
// We cannot use this as default since many firmwares fail with a too high data length.
//
if( (dataLen-8) % 8 || dataLen <= 8 )
dataLen = 0xFFFF;
// again with real length
*data = new unsigned char[dataLen];
::memset( *data, 0, dataLen );
cmd[7] = dataLen>>8;
cmd[8] = dataLen;
if( cmd.transport( TR_DIR_READ, *data, dataLen ) == 0 ) {
dataLen = TQMIN( dataLen, from4Byte( *data ) + 4 );
return true;
}
else {
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName() << ": GET CONFIGURATION with real length "
<< dataLen << " failed." << endl;
delete [] *data;
}
return false;
}
int K3bDevice::Device::featureCurrent( unsigned int feature ) const
{
unsigned char* data = 0;
unsigned int dataLen = 0;
if( getFeature( &data, dataLen, feature ) ) {
int ret = -1;
if( dataLen >= 11 )
ret = ( data[8+2]&1 ? 1 : 0 ); // check the current flag
delete [] data;
return ret;
}
else
return -1;
}
bool K3bDevice::Device::readIsrc( unsigned int track, TQCString& isrc ) const
{
unsigned char* data = 0;
unsigned int dataLen = 0;
if( readSubChannel( &data, dataLen, 0x3, track ) ) {
bool isrcValid = false;
if( dataLen >= 8+18 ) {
isrcValid = (data[8+4]>>7 & 0x1);
if( isrcValid ) {
isrc = TQCString( reinterpret_cast<char*>(data[8+5]), 13 );
// TODO: check the range of the chars
}
}
delete [] data;
return isrcValid;
}
else
return false;
}
bool K3bDevice::Device::readMcn( TQCString& mcn ) const
{
unsigned char* data = 0;
unsigned int dataLen = 0;
if( readSubChannel( &data, dataLen, 0x2, 0 ) ) {
bool mcnValid = false;
if( dataLen >= 8+18 ) {
mcnValid = (data[8+4]>>7 & 0x1);
if( mcnValid )
mcn = TQCString( reinterpret_cast<char*>(data[8+5]), 14 );
}
delete [] data;
return mcnValid;
}
else
return false;
}
bool K3bDevice::Device::getPerformance( unsigned char** data, unsigned int& dataLen,
unsigned int type,
unsigned int dataType,
unsigned int lba ) const
{
unsigned int descLen = 0;
switch( type ) {
case 0x0:
descLen = 16;
break;
case 0x1:
descLen = 8;
break;
case 0x2:
descLen = 2048;
break;
case 0x3:
descLen = 16;
break;
case 0x4:
descLen = 8;
break;
case 0x5:
descLen = 8; // FIXME: ??
break;
}
unsigned char header[8];
::memset( header, 0, 8 );
dataLen = 8;
ScsiCommand cmd( this );
cmd[0] = MMC_GET_PERFORMANCE;
cmd[1] = dataType;
cmd[2] = lba >> 24;
cmd[3] = lba >> 16;
cmd[4] = lba >> 8;
cmd[5] = lba;
cmd[9] = 1; // first we read one descriptor
cmd[10] = type;
cmd[11] = 0; // Necessary to set the proper command length
if( cmd.transport( TR_DIR_READ, header, 8 ) ) {
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName()
<< ": GET PERFORMANCE length det failed." << endl;
return false;
}
dataLen = from4Byte( header ) + 4;
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName()
<< ": GET PERFORMANCE dataLen = " << dataLen << endl;
if( (dataLen-8) % descLen ||
dataLen <= 8 ||
dataLen > 2048 ) {
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName()
<< ": GET PERFORMANCE reports bogus dataLen: " << dataLen << endl;
return false;
}
*data = new unsigned char[dataLen];
::memset( *data, 0, dataLen );
unsigned int numDesc = (dataLen-8)/descLen;
cmd[8] = numDesc>>8;
cmd[9] = numDesc;
if( cmd.transport( TR_DIR_READ, *data, dataLen ) == 0 ) {
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName()
<< ": GET PERFORMANCE successful with reported length: " << from4Byte( *data ) << endl;
dataLen = TQMIN( dataLen, from4Byte( *data ) + 4 );
return true;
}
else {
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName()
<< ": GET PERFORMANCE with real length "
<< dataLen << " failed." << endl;
delete [] *data;
return false;
}
}
bool K3bDevice::Device::setSpeed( unsigned int readingSpeed,
unsigned int writingSpeed,
bool cav ) const
{
ScsiCommand cmd( this );
cmd[0] = MMC_SET_SPEED;
cmd[1] = ( cav ? 0x1 : 0x0 );
cmd[2] = readingSpeed >> 8;
cmd[3] = readingSpeed;
cmd[4] = writingSpeed >> 8;
cmd[5] = writingSpeed;
cmd[11] = 0; // Necessary to set the proper command length
return ( cmd.transport( TR_DIR_WRITE ) == 0 );
}
bool K3bDevice::Device::seek( unsigned long lba ) const
{
ScsiCommand cmd( this );
cmd[0] = MMC_SEEK_10;
cmd[2] = lba>>24;
cmd[3] = lba>>16;
cmd[4] = lba>>8;
cmd[5] = lba;
cmd[9] = 0; // Necessary to set the proper command length
return !cmd.transport();
}
bool K3bDevice::Device::readTrackInformation( unsigned char** data, unsigned int& dataLen, int type, int value ) const
{
unsigned char header[2048];
::memset( header, 0, 2048 );
ScsiCommand cmd( this );
cmd[0] = MMC_READ_TRACK_INFORMATION;
cmd[9] = 0; // Necessary to set the proper command length
switch( type ) {
case 0:
case 1:
case 2:
cmd[1] = type & 0x3;
cmd[2] = value>>24;
cmd[3] = value>>16;
cmd[4] = value>>8;
cmd[5] = value;
break;
default:
k3bDebug() << "(K3bDevice::readTrackInformation) wrong type parameter: " << type << endl;
return false;
}
// first we read the header
dataLen = 4;
cmd[8] = 4;
if( cmd.transport( TR_DIR_READ, header, 4 ) == 0 )
dataLen = from2Byte( header ) + 2;
else
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName() << ": READ TRACK INFORMATION length det failed." << endl;
//
// Some buggy firmwares do not return the size of the available data
// but the returned data.
// So we try to determine the correct size based on the medium type
// DVD+R: 40 (MMC4)
// DVD-DL: 48 (MMC5)
// CD: 36 (MMC2)
//
if( dataLen <= 4 ) {
int m = mediaType();
if( m & (MEDIA_DVD_R_DL|MEDIA_DVD_R_DL_SEQ|MEDIA_DVD_R_DL_JUMP) )
dataLen = 48;
else if( m & (MEDIA_DVD_PLUS_R|MEDIA_DVD_PLUS_R_DL) )
dataLen = 40;
else
dataLen = 36;
}
// again with real length
*data = new unsigned char[dataLen];
::memset( *data, 0, dataLen );
cmd[7] = dataLen>>8;
cmd[8] = dataLen;
if( cmd.transport( TR_DIR_READ, *data, dataLen ) == 0 ) {
dataLen = TQMIN( dataLen, from2Byte( *data ) + 2u );
return true;
}
else {
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName() << ": READ TRACK INFORMATION with real length "
<< dataLen << " failed." << endl;
delete [] *data;
}
return false;
}
bool K3bDevice::Device::read10( unsigned char* data,
unsigned int dataLen,
unsigned long startAdress,
unsigned int length,
bool fua ) const
{
::memset( data, 0, dataLen );
ScsiCommand cmd( this );
cmd[0] = MMC_READ_10;
cmd[1] = ( fua ? 0x8 : 0x0 );
cmd[2] = startAdress>>24;
cmd[3] = startAdress>>16;
cmd[4] = startAdress>>8;
cmd[5] = startAdress;
cmd[7] = length>>8;
cmd[8] = length;
cmd[9] = 0; // Necessary to set the proper command length
if( cmd.transport( TR_DIR_READ, data, dataLen ) ) {
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName() << ": READ 10 failed!" << endl;
return false;
}
else
return true;
}
bool K3bDevice::Device::read12( unsigned char* data,
unsigned int dataLen,
unsigned long startAdress,
unsigned long length,
bool streaming,
bool fua ) const
{
::memset( data, 0, dataLen );
ScsiCommand cmd( this );
cmd[0] = MMC_READ_12;
cmd[1] = ( fua ? 0x8 : 0x0 );
cmd[2] = startAdress>>24;
cmd[3] = startAdress>>16;
cmd[4] = startAdress>>8;
cmd[5] = startAdress;
cmd[6] = length>>24;
cmd[7] = length>>16;
cmd[8] = length>>8;
cmd[9] = length;
cmd[10] = (streaming ? 0x80 : 0 );
cmd[11] = 0; // Necessary to set the proper command length
if( cmd.transport( TR_DIR_READ, data, dataLen ) ) {
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName() << ": READ 12 failed!" << endl;
return false;
}
else
return true;
}
bool K3bDevice::Device::readCd( unsigned char* data,
unsigned int dataLen,
int sectorType,
bool dap,
unsigned long startAdress,
unsigned long length,
bool sync,
bool header,
bool subHeader,
bool userData,
bool edcEcc,
int c2,
int subChannel ) const
{
::memset( data, 0, dataLen );
ScsiCommand cmd( this );
cmd[0] = MMC_READ_CD;
cmd[1] = (sectorType<<2 & 0x1c) | ( dap ? 0x2 : 0x0 );
cmd[2] = startAdress>>24;
cmd[3] = startAdress>>16;
cmd[4] = startAdress>>8;
cmd[5] = startAdress;
cmd[6] = length>>16;
cmd[7] = length>>8;
cmd[8] = length;
cmd[9] = ( ( sync ? 0x80 : 0x0 ) |
( subHeader ? 0x40 : 0x0 ) |
( header ? 0x20 : 0x0 ) |
( userData ? 0x10 : 0x0 ) |
( edcEcc ? 0x8 : 0x0 ) |
( c2<<1 & 0x6 ) );
cmd[10] = subChannel & 0x7;
cmd[11] = 0; // Necessary to set the proper command length
if( cmd.transport( TR_DIR_READ, data, dataLen ) ) {
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName() << ": READ CD failed!" << endl;
return false;
}
else {
return true;
}
}
bool K3bDevice::Device::readCdMsf( unsigned char* data,
unsigned int dataLen,
int sectorType,
bool dap,
const K3b::Msf& startAdress,
const K3b::Msf& endAdress,
bool sync,
bool header,
bool subHeader,
bool userData,
bool edcEcc,
int c2,
int subChannel ) const
{
::memset( data, 0, dataLen );
ScsiCommand cmd( this );
cmd[0] = MMC_READ_CD_MSF;
cmd[1] = (sectorType<<2 & 0x1c) | ( dap ? 0x2 : 0x0 );
cmd[3] = (startAdress+150).minutes();
cmd[4] = (startAdress+150).seconds();
cmd[5] = (startAdress+150).frames();
cmd[6] = (endAdress+150).minutes();
cmd[7] = (endAdress+150).seconds();
cmd[8] = (endAdress+150).frames();
cmd[9] = ( ( sync ? 0x80 : 0x0 ) |
( subHeader ? 0x40 : 0x0 ) |
( header ? 0x20 : 0x0 ) |
( userData ? 0x10 : 0x0 ) |
( edcEcc ? 0x8 : 0x0 ) |
( c2<<1 & 0x6 ) );
cmd[10] = subChannel & 0x7;
cmd[11] = 0; // Necessary to set the proper command length
if( cmd.transport( TR_DIR_READ, data, dataLen ) ) {
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName() << ": READ CD MSF failed!" << endl;
return false;
}
else
return true;
}
bool K3bDevice::Device::readSubChannel( unsigned char** data, unsigned int& dataLen,
unsigned int subchannelParam,
unsigned int trackNumber ) const
{
unsigned char header[2048];
::memset( header, 0, 2048 );
ScsiCommand cmd( this );
cmd[0] = MMC_READ_SUB_CHANNEL;
cmd[2] = 0x40; // SUBQ
cmd[3] = subchannelParam;
cmd[6] = trackNumber; // only used when subchannelParam == 03h (ISRC)
cmd[8] = 4;
cmd[9] = 0; // Necessary to set the proper command length
// first we read the header
dataLen = 4;
if( cmd.transport( TR_DIR_READ, header, 4 ) == 0 )
dataLen = from2Byte( &header[2] ) + 4;
else
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName() << ": READ SUB-CHANNEL length det failed." << endl;
//
// Some buggy firmwares do not return the size of the available data
// but the returned data. So we simply use the maximum possible value to be on the safe side
// with these buggy drives.
// We cannot use this as default since many firmwares fail with a too high data length.
//
if( dataLen <= 4 )
dataLen = 0xFFFF;
// again with real length
*data = new unsigned char[dataLen];
::memset( *data, 0, dataLen );
cmd[7] = dataLen>>8;
cmd[8] = dataLen;
if( cmd.transport( TR_DIR_READ, *data, dataLen ) == 0 ) {
dataLen = TQMIN( dataLen, from2Byte( (*data)+2 ) + 4u );
return true;
}
else {
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName() << ": READ SUB-CHANNEL with real length "
<< dataLen << " failed." << endl;
delete [] *data;
}
return false;
}
bool K3bDevice::Device::readTocPmaAtip( unsigned char** data, unsigned int& dataLen, int format, bool time, int track ) const
{
unsigned int descLen = 0;
switch( format ) {
case 0x0:
descLen = 8;
break;
case 0x1:
descLen = 8;
break;
case 0x2:
descLen = 11;
break;
case 0x3:
descLen = 11;
break;
case 0x4:
descLen = 4; // MMC2: 24 and MMC4: 28, so we use the highest common factor
break;
case 0x5:
descLen = 18;
break;
}
unsigned char header[2048];
::memset( header, 0, 2048 );
ScsiCommand cmd( this );
cmd[0] = MMC_READ_TOC_PMA_ATIP;
cmd[1] = ( time ? 0x2 : 0x0 );
cmd[2] = format & 0x0F;
cmd[6] = track;
cmd[8] = 4;
cmd[9] = 0; // Necessary to set the proper command length
// we only read the header
dataLen = 4;
if( cmd.transport( TR_DIR_READ, header, 4 ) == 0 )
dataLen = from2Byte( header ) + 2;
else
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName() << ": READ TOC/PMA/ATIP length det failed." << endl;
//
// Some buggy firmwares return an invalid size here
// So we simply use the maximum possible value to be on the safe side
// with these buggy drives.
// We cannot use this as default since many firmwares fail with a too high data length.
//
if( (dataLen-4) % descLen || dataLen < 4+descLen ) {
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName() << ": READ TOC/PMA/ATIP invalid length returned: " << dataLen << endl;
dataLen = 0xFFFF;
}
//
// Not all drives like uneven numbers
//
if( dataLen%2 )
++dataLen;
// again with real length
*data = new unsigned char[dataLen];
::memset( *data, 0, dataLen );
cmd[7] = dataLen>>8;
cmd[8] = dataLen;
if( cmd.transport( TR_DIR_READ, *data, dataLen ) == 0 ) {
dataLen = TQMIN( dataLen, from2Byte( *data ) + 2u );
if( (dataLen-4) % descLen || dataLen < 4+descLen ) {
// useless length
delete [] *data;
return false;
}
else
return true;
}
else {
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName() << ": READ TOC/PMA/ATIP format "
<< format << " with real length "
<< dataLen << " failed." << endl;
delete [] *data;
}
return false;
}
bool K3bDevice::Device::mechanismtqStatus( unsigned char** data, unsigned int& dataLen ) const
{
unsigned char header[2048];
::memset( header, 0, 2048 );
ScsiCommand cmd( this );
cmd[0] = MMC_MECHANISM_STATUS;
cmd[9] = 8;
cmd[11] = 0; // Necessary to set the proper command length
// first we read the header
dataLen = 8;
if( cmd.transport( TR_DIR_READ, header, 8 ) == 0 )
dataLen = from4Byte( &header[6] ) + 8;
else
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName() << ": MECHANISM STATUS length det failed." << endl;
//
// Some buggy firmwares do not return the size of the available data
// but the returned data or something invalid altogether.
// So we simply use the maximum possible value to be on the safe side
// with these buggy drives.
// We cannot use this as default since many firmwares fail with a too high data length.
//
if( (dataLen-8) % 4 || dataLen <= 8 )
dataLen = 0xFFFF;
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName() << ": MECHANISM STATUS "
<< (int)header[5] << " slots." << endl;
// again with real length
*data = new unsigned char[dataLen];
::memset( *data, 0, dataLen );
cmd[8] = dataLen>>8;
cmd[9] = dataLen;
if( cmd.transport( TR_DIR_READ, *data, dataLen ) == 0 ) {
dataLen = TQMIN( dataLen, from4Byte( (*data)+6 ) + 8 );
return true;
}
else {
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName() << ": MECHANISM STATUS with real length "
<< dataLen << " failed." << endl;
delete [] *data;
}
return false;
}
bool K3bDevice::Device::modeSense( unsigned char** pageData, unsigned int& pageLen, int page ) const
{
unsigned char header[2048];
::memset( header, 0, 2048 );
ScsiCommand cmd( this );
cmd[0] = MMC_MODE_SENSE;
cmd[1] = 0x8; // Disable Block Descriptors
cmd[2] = page & 0x3F;
cmd[8] = 8;
cmd[9] = 0; // Necessary to set the proper command length
// first we determine the data length
pageLen = 8;
if( cmd.transport( TR_DIR_READ, header, 8 ) == 0 )
pageLen = from2Byte( header ) + 2;
else
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName() << ": MODE SENSE length det failed." << endl;
//
// Some buggy firmwares do not return the size of the available data
// but the returned data. So we simply use the maximum possible value to be on the safe side
// with these buggy drives.
// We cannot use this as default since many firmwares fail with a too high data length.
//
if( pageLen == 8 )
pageLen = 0xFFFF;
// again with real length
*pageData = new unsigned char[pageLen];
::memset( *pageData, 0, pageLen );
cmd[7] = pageLen>>8;
cmd[8] = pageLen;
if( cmd.transport( TR_DIR_READ, *pageData, pageLen ) == 0 ) {
pageLen = TQMIN( pageLen, from2Byte( *pageData ) + 2u );
return true;
}
else {
delete [] *pageData;
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName() << ": MODE SENSE with real length "
<< pageLen << " failed." << endl;
}
return false;
}
bool K3bDevice::Device::modeSelect( unsigned char* page, unsigned int pageLen, bool pf, bool sp ) const
{
page[0] = 0;
page[1] = 0;
page[4] = 0;
page[5] = 0;
// we do not support Block Descriptors here
page[6] = 0;
page[7] = 0;
// PS bit reserved
page[8] &= 0x3F;
ScsiCommand cmd( this );
cmd[0] = MMC_MODE_SELECT;
cmd[1] = ( sp ? 1 : 0 ) | ( pf ? 0x10 : 0 );
cmd[7] = pageLen>>8;
cmd[8] = pageLen;
cmd[9] = 0;
return( cmd.transport( TR_DIR_WRITE, page, pageLen ) == 0 );
}
// does only make sense for complete media
bool K3bDevice::Device::readCapacity( K3b::Msf& r ) const
{
ScsiCommand cmd( this );
cmd[0] = MMC_READ_CAPACITY;
cmd[9] = 0; // Necessary to set the proper command length
unsigned char buf[8];
::memset( buf, 0, 8 );
if( cmd.transport( TR_DIR_READ, buf, 8 ) == 0 ) {
r = from4Byte( buf );
return true;
}
else
return false;
}
bool K3bDevice::Device::readFormatCapacity( int wantedFormat, K3b::Msf& r,
K3b::Msf* currentMax, int* currentMaxFormat ) const
{
bool success = false;
// the maximal length as stated in MMC4
static const unsigned int maxLen = 4 + (8*32);
unsigned char buffer[maxLen];
::memset( buffer, 0, maxLen );
ScsiCommand cmd( this );
cmd[0] = MMC_READ_FORMAT_CAPACITIES;
cmd[7] = maxLen >> 8;
cmd[8] = maxLen & 0xFF;
cmd[9] = 0; // Necessary to set the proper command length
if( cmd.transport( TR_DIR_READ, buffer, maxLen ) == 0 ) {
unsigned int realLength = buffer[3] + 4;
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName() << " READ FORMAT CAPACITY: Current/Max "
<< (int)(buffer[8]&0x3) << " " << from4Byte( &buffer[4] ) << endl;
if( currentMax )
*currentMax = from4Byte( &buffer[4] );
if( currentMaxFormat )
*currentMaxFormat = (int)(buffer[8]&0x3);
//
// Descriptor Type:
// 0 - reserved
// 1 - unformatted :)
// 2 - formatted. Here we get the used capacity (lead-in to last lead-out/border-out)
// 3 - No media present
//
for( unsigned int i = 12; i < realLength-4; i+=8 ) {
int format = (int)((buffer[i+4]>>2)&0x3f);
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName() << " READ FORMAT CAPACITY: "
<< format << " " << from4Byte( &buffer[i] )
<< " " << (int)( buffer[i+5] << 16 & 0xFF0000 |
buffer[i+6] << 8 & 0xFF00 |
buffer[i+7] & 0xFF ) << endl;
if( format == wantedFormat ) {
// found the descriptor
r = TQMAX( (int)from4Byte( &buffer[i] ), r.lba() );
success = true;
}
}
}
return success;
}
bool K3bDevice::Device::readDiscInformation( unsigned char** data, unsigned int& dataLen ) const
{
unsigned char header[2];
::memset( header, 0, 2 );
ScsiCommand cmd( this );
cmd[0] = MMC_READ_DISC_INFORMATION;
cmd[8] = 2;
cmd[9] = 0; // Necessary to set the proper command length
if( cmd.transport( TR_DIR_READ, header, 2 ) == 0 )
dataLen = from2Byte( header ) + 2;
else
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName()
<< ": READ DISC INFORMATION length det failed" << endl;
if( dataLen < 32 ) {
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName()
<< ": Device reports bogus disc information length of " << dataLen << endl;
dataLen = 32;
}
*data = new unsigned char[dataLen];
::memset( *data, 0, dataLen );
cmd[7] = dataLen>>8;
cmd[8] = dataLen;
if( cmd.transport( TR_DIR_READ, *data, dataLen ) == 0 ) {
dataLen = TQMIN( dataLen, from2Byte( *data ) + 2u );
return true;
}
else {
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName() << ": READ DISC INFORMATION with real length "
<< dataLen << " failed." << endl;
delete [] *data;
}
return false;
}
bool K3bDevice::Device::readDvdStructure( unsigned char** data, unsigned int& dataLen,
unsigned int format,
unsigned int layer,
unsigned long adress,
unsigned int agid ) const
{
return readDiscStructure( data, dataLen, 0x0, format, layer, adress, agid );
}
bool K3bDevice::Device::readDiscStructure( unsigned char** data, unsigned int& dataLen,
unsigned int mediaType,
unsigned int format,
unsigned int layer,
unsigned long adress,
unsigned int agid ) const
{
unsigned char header[4];
::memset( header, 0, 4 );
ScsiCommand cmd( this );
cmd[0] = MMC_READ_DVD_STRUCTURE;
cmd[1] = mediaType & 0xF;
cmd[2] = adress>>24;
cmd[3] = adress>>16;
cmd[4] = adress>>8;
cmd[5] = adress;
cmd[6] = layer;
cmd[7] = format;
cmd[10] = (agid<<6);
cmd[11] = 0; // Necessary to set the proper command length
cmd[9] = 4;
if( cmd.transport( TR_DIR_READ, header, 4 ) == 0 ) {
// again with real length
dataLen = from2Byte( header ) + 2;
*data = new unsigned char[dataLen];
::memset( *data, 0, dataLen );
cmd[8] = dataLen>>8;
cmd[9] = dataLen;
if( cmd.transport( TR_DIR_READ, *data, dataLen ) == 0 ) {
dataLen = TQMIN( dataLen, from2Byte( *data ) + 2u );
return true;
}
else {
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName() << ": READ DVD STRUCTURE with real length failed." << endl;
delete [] *data;
}
}
else
k3bDebug() << "(K3bDevice::Device) " << blockDeviceName() << ": READ DVD STRUCTURE length det failed" << endl;
return false;
}
int K3bDevice::Device::readBufferCapacity( long long& bufferLength, long long& bufferAvail ) const
{
unsigned char data[12];
::memset( data, 0, 12 );
ScsiCommand cmd( this );
cmd[0] = MMC_READ_BUFFER_CAPACITY;
cmd[8] = 12;
cmd[9] = 0; // Necessary to set the proper command length
int r = cmd.transport( TR_DIR_READ, data, 12 );
if( r )
return r;
unsigned int dataLength = from2Byte( data );
if( dataLength >= 10 ) {
bufferLength = from4Byte( &data[4] );
bufferAvail = from4Byte( &data[8] );
}
else {
bufferAvail = bufferLength = 0;
}
return 0;
}
|