-
Notifications
You must be signed in to change notification settings - Fork 1
/
Disk.cpp
1707 lines (1501 loc) · 52 KB
/
Disk.cpp
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
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ES40 emulator.
* Copyright (C) 2007-2008 by the ES40 Emulator Project
*
* WWW : http://sourceforge.net/projects/es40
* E-mail : [email protected]
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Although this is not required, the author would appreciate being notified of,
* and receiving any modifications you may make to the source code that might serve
* the general public.
*/
/**
* \file
* Contains code for the disk base class.
*
* $Id: Disk.cpp,v 1.27 2008/02/27 12:04:22 iamcamiel Exp $
*
* X-1.27 Brian Wheeler 27-FEB-2008
* Avoid compiler warnings.
*
* X-1.26 David Leonard 20-FEB-2008
* Return SYSTEM RESOURCE FAILURE sense if dato/dati buffer size is
* exceeded.
*
* X-1.25 Brian Wheeler 20-FEB-2008
* Support MSF in READ TOC scsi command.
*
* X-1.24 Brian Wheeler 18-FEB-2008
* Added vital product data page 0 (Required for Tru64).
*
* X-1.23 Camiel Vanderhoeven 18-FEB-2008
* Removed support for CD-R/RW specific SCSI commands.
*
* X-1.22 Camiel Vanderhoeven 18-FEB-2008
* READ CAPACITY returns the number of the last LBA (n-1); not the
* number of LBA's (n).
*
* X-1.20 Camiel Vanderhoeven 17-FEB-2008
* Set up sense data when error occurs.
*
* X-1.19 Camiel Vanderhoeven 17-FEB-2008
* Added REQUEST_SENSE scsi command.
*
* X-1.18 Camiel Vanderhoeven 16-FEB-2008
* Added READ_LONG scsi command, and support for MODE_SENSE changeable
* parameter pages.
*
* X-1.17 Camiel Vanderhoeven 21-JAN-2008
* OpenVMs doesn't like max 255 sectors.
*
* X-1.16 Camiel Vanderhoeven 16-JAN-2008
* Less messages without debugging enabled.
*
* X-1.15 Brian Wheeler 14-JAN-2008
* Corrected output from read track info and read toc.
*
* X-1.14 Brian Wheeler/Camiel Vanderhoeven 14-JAN-2008
* Added read_track_info command, completed read_toc command.
*
* X-1.13 Camiel Vanderhoeven 13-JAN-2008
* Determine best-fitting C/H/S lay-out.
*
* X-1.11 Brian Wheeler 13-JAN-2008
* More CD-ROM commands supported.
*
* X-1.10 Camiel Vanderhoeven 12-JAN-2008
* Include SCSI engine, because this is common to both SCSI and ATAPI
* devices.
*
* X-1.9 Camiel Vanderhoeven 09-JAN-2008
* Save disk state to state file.
*
* X-1.8 Camiel Vanderhoeven 06-JAN-2008
* Set default blocksize to 2048 for cd-rom devices.
*
* X-1.7 Camiel Vanderhoeven 06-JAN-2008
* Support changing the block size (required for SCSI, ATAPI).
*
* X-1.6 Camiel Vanderhoeven 02-JAN-2008
* Cleanup.
*
* X-1.5 Camiel Vanderhoeven 29-DEC-2007
* Fix memory-leak.
*
* X-1.4 Camiel Vanderhoeven 28-DEC-2007
* Throw exceptions rather than just exiting when errors occur.
*
* X-1.3 Camiel Vanderhoeven 28-DEC-2007
* Keep the compiler happy.
*
* X-1.2 Brian Wheeler 16-DEC-2007
* Fixed case of StdAfx.h.
*
* X-1.1 Camiel Vanderhoeven 12-DEC-2007
* Initial version in CVS.
**/
#include "StdAfx.h"
#include "Disk.h"
/**
* \brief Constructor.
**/
CDisk::CDisk(CConfigurator * cfg, CSystem * sys, CDiskController * ctrl, int idebus, int idedev) : CSystemComponent(cfg, sys)
{
char * a;
char * b;
char * c;
char * d;
myCfg = cfg;
myCtrl = ctrl;
myBus = idebus;
myDev = idedev;
atapi_mode = false;
a = myCfg->get_myName();
b = myCfg->get_myValue();
c = myCfg->get_myParent()->get_myName();
d = myCfg->get_myParent()->get_myValue();
free(devid_string); // we override the default to include the controller.
CHECK_ALLOCATION(devid_string = (char*) malloc(strlen(a)+strlen(b)+strlen(c)+strlen(d)+6));
sprintf(devid_string,"%s(%s).%s(%s)",c,d,a,b);
serial_number = myCfg->get_text_value("serial_num", "ES40EM00000");
revision_number = myCfg->get_text_value("rev_num", "0.0");
read_only = myCfg->get_bool_value("read_only");
is_cdrom = myCfg->get_bool_value("cdrom");
state.block_size = is_cdrom?2048:512;
state.scsi.sense.available = false;
myCtrl->register_disk(this,myBus,myDev);
}
/**
* \brief Destructor.
**/
CDisk::~CDisk(void)
{
free(devid_string);
}
/**
* \Calculate the number of cylinders to report.
**/
void CDisk::calc_cylinders()
{
cylinders = byte_size/state.block_size/sectors/heads;
off_t_large chs_size = sectors*cylinders*heads*state.block_size;
if (chs_size<byte_size)
cylinders++;
}
/**
* \brief Called when this device is selected.
*
* Set status fields up to begin a new SCSI command sequence
* and set the SCSI bus phase to Message Out.
**/
void CDisk::scsi_select_me(int bus)
{
state.scsi.msgo.written = 0;
state.scsi.msgi.available = 0;
state.scsi.msgi.read = 0;
state.scsi.cmd.written = 0;
state.scsi.dati.available = 0;
state.scsi.dati.read = 0;
state.scsi.dato.expected = 0;
state.scsi.dato.written = 0;
state.scsi.stat.available = 0;
state.scsi.stat.read = 0;
state.scsi.lun_selected = false;
//state.scsi.disconnect_priv = false;
//state.scsi.will_disconnect = false;
//state.scsi.disconnected = false;
if (atapi_mode)
scsi_set_phase(bus, SCSI_PHASE_COMMAND);
else
scsi_set_phase(bus, SCSI_PHASE_MSG_OUT);
}
static u32 disk_magic1 = 0xD15D15D1;
static u32 disk_magic2 = 0x15D15D5;
/**
* Save state to a Virtual Machine State file.
**/
int CDisk::SaveState(FILE *f)
{
long ss = sizeof(state);
fwrite(&disk_magic1,sizeof(u32),1,f);
fwrite(&ss,sizeof(long),1,f);
fwrite(&state,sizeof(state),1,f);
fwrite(&disk_magic2,sizeof(u32),1,f);
printf("%s: %d bytes saved.\n",devid_string,(int)ss);
return 0;
}
/**
* Restore state from a Virtual Machine State file.
**/
int CDisk::RestoreState(FILE *f)
{
long ss;
u32 m1;
u32 m2;
size_t r;
r = fread(&m1,sizeof(u32),1,f);
if (r!=1)
{
printf("%s: unexpected end of file!\n",devid_string);
return -1;
}
if (m1 != disk_magic1)
{
printf("%s: MAGIC 1 does not match!\n",devid_string);
return -1;
}
fread(&ss,sizeof(long),1,f);
if (r!=1)
{
printf("%s: unexpected end of file!\n",devid_string);
return -1;
}
if (ss != sizeof(state))
{
printf("%s: STRUCT SIZE does not match!\n",devid_string);
return -1;
}
fread(&state,sizeof(state),1,f);
if (r!=1)
{
printf("%s: unexpected end of file!\n",devid_string);
return -1;
}
r = fread(&m2,sizeof(u32),1,f);
if (r!=1)
{
printf("%s: unexpected end of file!\n",devid_string);
return -1;
}
if (m2 != disk_magic2)
{
printf("%s: MAGIC 1 does not match!\n",devid_string);
return -1;
}
//calc_cylinders(); // state.block_size may have changed.
determine_layout();
printf("%s: %d bytes restored.\n",devid_string,(int)ss);
return 0;
}
/**
* \brief Return the number of bytes expected or available.
*
* Return the number of bytes we still expect to receive
* from the initiator, or still have available for the
* initiator, in the current SCSI phase.
*
* For an overview of data transfer during a SCSI bus phase,
* see SCSIDevice::scsi_xfer_ptr.
**/
size_t CDisk::scsi_expected_xfer_me(int bus)
{
switch (scsi_get_phase(0))
{
case SCSI_PHASE_DATA_OUT:
return state.scsi.dato.expected - state.scsi.dato.written;
case SCSI_PHASE_DATA_IN:
return state.scsi.dati.available - state.scsi.dati.read;
case SCSI_PHASE_COMMAND:
return 256 - state.scsi.cmd.written;
case SCSI_PHASE_STATUS:
return state.scsi.stat.available - state.scsi.stat.read;
case SCSI_PHASE_MSG_OUT:
return 256 - state.scsi.msgo.written;
case SCSI_PHASE_MSG_IN:
return state.scsi.msgi.available - state.scsi.msgi.read;
default:
printf("%s: transfer requested in phase %d\n",devid_string, scsi_get_phase(0));
FAILURE("SCSI Transfer Failed");
}
}
/**
* \brief Return a pointer where the initiator can read or write data.
*
* Return a pointer to where the initiator can read or write
* (the remainder of) our data in the current SCSI phase.
*
* For an overview of data transfer during a SCSI bus phase,
* see SCSIDevice::scsi_xfer_ptr.
**/
void * CDisk::scsi_xfer_ptr_me(int bus, size_t bytes)
{
void * res = 0;
switch (scsi_get_phase(0))
{
case SCSI_PHASE_DATA_OUT:
res = &(state.scsi.dato.data[state.scsi.dato.written]);
state.scsi.dato.written += bytes;
break;
case SCSI_PHASE_DATA_IN:
res = &(state.scsi.dati.data[state.scsi.dati.read]);
state.scsi.dati.read += bytes;
break;
case SCSI_PHASE_COMMAND:
res = &(state.scsi.cmd.data[state.scsi.cmd.written]);
state.scsi.cmd.written += bytes;
break;
case SCSI_PHASE_STATUS:
res = &(state.scsi.stat.data[state.scsi.stat.read]);
state.scsi.stat.read += bytes;
break;
case SCSI_PHASE_MSG_OUT:
res = &(state.scsi.msgo.data[state.scsi.msgo.written]);
state.scsi.msgo.written += bytes;
break;
case SCSI_PHASE_MSG_IN:
//if (PT.reselected)
//{
// retval = 0x80; // identify
// break;
//}
//if (PT.disconnected)
//{
// if (!PT.dati_ptr)
// retval = 0x04; // disconnect
// else
// {
// if (state.scsi.msgi.read==0)
// {
// retval = 0x02; // save data pointer
// state.scsi.msgi.read=1;
// }
// else if (state.scsi.msgi.read==1)
// {
// retval = 0x04; // disconnect
// state.scsi.msgi.read=0;
// }
// }
// break;
//}
res = &(state.scsi.msgi.data[state.scsi.msgi.read]);
state.scsi.msgi.read += bytes;
break;
default:
printf("%s: transfer requested in phase %d\n",devid_string, scsi_get_phase(0));
FAILURE("SCSI Transfer Failed");
}
return res;
}
/**
* \brief Process data written or read.
*
* Determine what action (if any) should be taken after a
* transfer, and what the next SCSI bus phase should be.
*
* For an overview of data transfer during a SCSI bus phase,
* see SCSIDevice::scsi_xfer_ptr.
*
* \todo Handle disconnect/reconnect properly.
**/
void CDisk::scsi_xfer_done_me(int bus)
{
int res;
int newphase = scsi_get_phase(0);
switch (scsi_get_phase(0))
{
case SCSI_PHASE_DATA_OUT:
if (state.scsi.dato.written < state.scsi.dato.expected)
break;
res = do_scsi_command();
if (res == 2)
FAILURE("do_command returned 2 after DATA OUT phase");
if (state.scsi.dati.available)
newphase = SCSI_PHASE_DATA_IN;
else
newphase = SCSI_PHASE_STATUS;
break;
case SCSI_PHASE_DATA_IN:
if (state.scsi.dati.read < state.scsi.dati.available)
break;
newphase = SCSI_PHASE_STATUS;
break;
case SCSI_PHASE_COMMAND:
res = do_scsi_command();
if (res == 2)
newphase = SCSI_PHASE_DATA_OUT;
else if (state.scsi.dati.available)
newphase = SCSI_PHASE_DATA_IN;
else
newphase = SCSI_PHASE_STATUS;
break;
case SCSI_PHASE_STATUS:
if (state.scsi.stat.read < state.scsi.stat.available)
break;
if (atapi_mode)
{
scsi_free(0);
return;
}
newphase = SCSI_PHASE_MSG_IN;
break;
case SCSI_PHASE_MSG_OUT:
newphase = do_scsi_message(); // command
break;
case SCSI_PHASE_MSG_IN:
//if (state.scsi.reselected)
//{
// state.scsi.reselected = false;
// newphase = state.scsi.disconnect_phase;
//}
//else if (state.scsi.disconnected)
//{
// if (!state.scsi.msgi.read)
// newphase = -1;
//}
if (state.scsi.msgi.read < state.scsi.msgi.available)
break;
if (state.scsi.cmd.written)
{
scsi_free(0);
return;
}
else
newphase = SCSI_PHASE_COMMAND;
break;
default:
printf("%s: transfer requested in phase %d\n",devid_string, scsi_get_phase(0));
FAILURE("SCSI Transfer Failed");
}
// if data in and can disconnect...
//if (state.phase!=7 && newphase==1 && PT.will_disconnect && !PT.disconnected)
//{
// printf("%s: Disconnecting now...\n",devid_string);
// PT.disconnected = true;
// PT.disconnect_phase = newphase;
// newphase = 7; // msg in
//}
if (newphase != scsi_get_phase(0))
{
// if (newphase==-1)
// {
// printf("%s: Disconnect. Timer started!\n",devid_string);
// // disconnect. generate interrupt?
// state.disconnected = 20;
// }
scsi_set_phase(0, newphase);
}
//getchar();
}
// SCSI commands:
#define SCSICMD_TEST_UNIT_READY 0x00
#define SCSICMD_REQUEST_SENSE 0x03
#define SCSICMD_INQUIRY 0x12
#define SCSICMD_READ 0x08
#define SCSICMD_READ_10 0x28
#define SCSICMD_READ_12 0xA8
#define SCSICMD_READ_16 0x88
#define SCSICMD_READ_32 0x7F
#define SCSICMD_READ_LONG 0x3E
#define SCSICMD_READ_CD 0xBE
#define SCSICMD_WRITE 0x0A
#define SCSICMD_WRITE_10 0x2A
#define SCSICMD_WRITE_12 0xAA
#define SCSICMD_WRITE_LONG 0x3F
#define SCSICMD_MODE_SELECT 0x15
#define SCSICMD_MODE_SENSE 0x1a
#define SCSICMD_START_STOP_UNIT 0x1b
#define SCSICMD_PREVENT_ALLOW_REMOVE 0x1e
#define SCSICMD_MODE_SENSE_10 0x5a
#define SCSICMD_SYNCHRONIZE_CACHE 0x35
// SCSI block device commands:
#define SCSIBLOCKCMD_READ_CAPACITY 0x25
// SCSI CD-ROM commands:
#define SCSICDROM_READ_SUBCHANNEL 0x42
#define SCSICDROM_READ_TOC 0x43
// SCSI CD-R/RW commands:
#define SCSICDRRW_FORMAT 0x04
#define SCSICDRRW_READ_DISC_INFO 0x51
#define SCSICDRRW_READ_TRACK_INFO 0x52
#define SCSICDRRW_RESERVE_TRACK 0x53
#define SCSICDRRW_SEND_OPC_INFO 0x54
#define SCSICDRRW_REPAIR_TRACK 0x58
#define SCSICDRRW_READ_MASTER_CUE 0x59
#define SCSICDRRW_CLOSE_TRACK 0x5b
#define SCSICDRRW_READ_BUFFER_CAP 0x5c
#define SCSICDRRW_SEND_CUE_SHEET 0x5d
#define SCSICDRRW_BLANK 0xa1
// SCSI tape commands:
#define SCSICMD_REWIND 0x01
#define SCSICMD_READ_BLOCK_LIMITS 0x05
#define SCSICMD_SPACE 0x11
// SCSI mode pages:
#define SCSIMP_VENDOR 0x00
#define SCSIMP_READ_WRITE_ERRREC 0x01
#define SCSIMP_DISCONNECT_RECONNECT 0x02
#define SCSIMP_FORMAT_PARAMS 0x03
#define SCSIMP_RIGID_GEOMETRY 0x04
#define SCSIMP_FLEX_PARAMS 0x05
#define SCSIMP_CACHING 0x08
#define SCSIMP_CDROM_CAP 0x2A
#define SCSI_OK 0
#define SCSI_ILL_CMD -1 /* illegal command */
#define SCSI_LBA_RANGE -2 /* LBA out of range */
#define SCSI_TOO_BIG -3 /* Too big for buffer */
void CDisk::do_scsi_error(int errcode)
{
state.scsi.stat.available = 1;
state.scsi.stat.data[0] = 0;
state.scsi.stat.read = 0;
state.scsi.msgi.available = 1;
state.scsi.msgi.data[0] = 0;
state.scsi.msgi.read = 0;
if (errcode == SCSI_OK)
{
#if defined(DEBUG_SCSI)
printf("%s: Command returns OK status.\n",devid_string);
#endif
return;
}
state.scsi.stat.data[0] = 0x02; // check sense
state.scsi.sense.data[0] = 0xf0; // error code
state.scsi.sense.data[1] = 0x00; // segment number
state.scsi.sense.data[3] = 0x00; // info
state.scsi.sense.data[4] = 0x00;
state.scsi.sense.data[5] = 0x00;
state.scsi.sense.data[6] = 0x00;
state.scsi.sense.data[7] = 10; // additional sense length
state.scsi.sense.data[8] = 0x00; // command specific
state.scsi.sense.data[9] = 0x00;
state.scsi.sense.data[10] = 0x00;
state.scsi.sense.data[11] = 0x00;
state.scsi.sense.data[14] = 0x00; // FRU code
state.scsi.sense.data[15] = 0x00; // sense key specific
state.scsi.sense.data[16] = 0x00;
state.scsi.sense.data[17] = 0x00;
state.scsi.sense.available = 18;
switch(errcode)
{
case SCSI_ILL_CMD:
state.scsi.sense.data[2] = 0x05; // illegal request
state.scsi.sense.data[12] = 0x20; // invalid command
state.scsi.sense.data[13] = 0x00;
#if defined(DEBUG_SCSI)
printf("%s: Command returns check sense status (sense: ILLEGAL COMMAND).\n",devid_string);
#endif
break;
case SCSI_LBA_RANGE:
state.scsi.sense.data[2] = 0x05; // illegal request
state.scsi.sense.data[12] = 0x21; // LBA out of range
state.scsi.sense.data[13] = 0x00;
#if defined(DEBUG_SCSI)
printf("%s: Command returns check sense status (sense: LBA OUT OF RANGE).\n",devid_string);
#endif
break;
case SCSI_TOO_BIG:
state.scsi.sense.data[2] = 0x05; // illegal request
state.scsi.sense.data[12] = 0x55; // system resource failure
state.scsi.sense.data[13] = 0x00;
#if defined(DEBUG_SCSI)
printf("%s: Command returns check sense status (sense: SYSTEM RESOURCE FAILURE).\n",devid_string);
#endif
}
}
/**
* Basic algorithm taken from libcdio for converting lba to msf
**/
static u32 lba2msf(off_t_large lba)
{
#define PREGAP_SECTORS 150
#define CD_FRAMES_PER_SEC 75
#define CD_MAX_LSN 450150
#define bin2bcd(x) ((x / 10) << 4) | (x % 10)
int m,s,f;
lba -= PREGAP_SECTORS;
if(lba >= -PREGAP_SECTORS) {
m = (lba + PREGAP_SECTORS) / (CD_FRAMES_PER_SEC * 60);
lba -= m * (CD_FRAMES_PER_SEC * 60);
s = (lba + PREGAP_SECTORS) / CD_FRAMES_PER_SEC;
lba -= s * CD_FRAMES_PER_SEC;
f = lba + PREGAP_SECTORS;
} else {
m = (lba + CD_MAX_LSN) / (CD_FRAMES_PER_SEC * 60);
lba -= m * (CD_FRAMES_PER_SEC * 60);
s = (lba + CD_MAX_LSN) / CD_FRAMES_PER_SEC;
lba -= s * CD_FRAMES_PER_SEC;
f = lba + CD_MAX_LSN;
}
if(m > 99) m=99;
//printf("m=%d, s=%d, f=%d == m=%x, s=%x, f=%x\n", m,s,f,bin2bcd(m),bin2bcd(s),bin2bcd(f));
return bin2bcd(m)<<16 | bin2bcd(s)<<8 | bin2bcd(f);
}
/**
* \brief Handle a SCSI command.
*
* Called when a SCSI command has been received. We parse
* the command, and set up the state for the data in or
* data out phases.
*
* If a data out phase is required, we return the value 2
* to indicate this. In that case, do_scsi_command will be
* called again once the data out has been received from
* the initiator.
**/
int CDisk::do_scsi_command()
{
unsigned int retlen = 0;
int q;
int pagecode;
u32 ofs = 0;
#if defined(DEBUG_SCSI)
printf("%s: %d-byte command ",devid_string,state.scsi.cmd.written);
for (unsigned int x=0;x<state.scsi.cmd.written;x++) printf("%02x ",state.scsi.cmd.data[x]);
printf("\n");
#endif
if (state.scsi.cmd.written<1)
return 0;
if (state.scsi.cmd.data[1] & 0xe0)
{
#if defined(DEBUG_SCSI)
printf("%s: LUN selected...\n",devid_string);
#endif
state.scsi.lun_selected = true;
}
if (state.scsi.lun_selected && state.scsi.cmd.data[0] != SCSICMD_INQUIRY && state.scsi.cmd.data[0] != SCSICMD_REQUEST_SENSE)
{
printf("%s: LUN not supported!\n",devid_string);
FAILURE("SCSI Command Failed");
}
switch(state.scsi.cmd.data[0])
{
case SCSICMD_TEST_UNIT_READY:
#if defined(DEBUG_SCSI)
printf("%s: TEST UNIT READY.\n",devid_string);
#endif
// unit is always ready...
do_scsi_error(SCSI_OK);
break;
case SCSICMD_REQUEST_SENSE:
#if defined(DEBUG_SCSI)
printf("%s: REQUEST SENSE.\n",devid_string);
#endif
retlen = state.scsi.cmd.data[4];
// FAILURE("Sense requested");
if (!state.scsi.sense.available)
{
#if defined(DEBUG_SCSI)
printf("%s: NO SENSE.\n",devid_string);
#endif
state.scsi.sense.data[0] = 0xf0; // error code
state.scsi.sense.data[1] = 0x00; // segment number
state.scsi.sense.data[2] = 0x00; // sense key: no sense
state.scsi.sense.data[3] = 0x00; // info
state.scsi.sense.data[4] = 0x00;
state.scsi.sense.data[5] = 0x00;
state.scsi.sense.data[6] = 0x00;
state.scsi.sense.data[7] = 10; // additional sense length
state.scsi.sense.data[8] = 0x00; // command specific
state.scsi.sense.data[9] = 0x00;
state.scsi.sense.data[10] = 0x00;
state.scsi.sense.data[11] = 0x00;
state.scsi.sense.data[12] = 0x00; // additional sense code: no additional sense
state.scsi.sense.data[13] = 0x00; // additional qualifier
state.scsi.sense.data[14] = 0x00; // FRU code
state.scsi.sense.data[15] = 0x00; // sense key specific
state.scsi.sense.data[16] = 0x00;
state.scsi.sense.data[17] = 0x00;
state.scsi.sense.available = 18;
}
#if defined(DEBUG_SCSI)
printf("%s: Returning data: ",devid_string);
for (unsigned int x1 = 0; x1 <state.scsi.sense.available; x1++) printf("%02x ",state.scsi.sense.data[x1]);
printf("\n");
#endif
state.scsi.dati.read = 0;
state.scsi.dati.available = retlen;
memcpy(state.scsi.dati.data,state.scsi.sense.data,state.scsi.sense.available);
for (unsigned int x2 = state.scsi.sense.available; x2 < retlen; x2++)
state.scsi.dati.data[x2] = 0;
do_scsi_error(SCSI_OK);
break;
case SCSICMD_INQUIRY:
{
#if defined(DEBUG_SCSI)
printf("%s: INQUIRY.\n",devid_string);
#endif
if ((state.scsi.cmd.data[1] & 0x1e) != 0x00)
{
printf("%s: Don't know how to handle INQUIRY with cmd[1]=0x%02x.\n", devid_string, state.scsi.cmd.data[1]);
FAILURE("SCSI Command Failed");
break;
}
u8 qual_dev = state.scsi.lun_selected ? 0x7F : (cdrom() ? 0x05 : 0x00);
retlen = state.scsi.cmd.data[4];
state.scsi.dati.data[0] = qual_dev; // device type
if (state.scsi.cmd.data[1] & 0x01)
{
// Vital Product Data
switch(state.scsi.cmd.data[2])
{
case 0x00:
// Page 0 is basically a list of page codes supported, so if
// any others are added, make sure to insert them in the proper
// place and increase the page length.
state.scsi.dati.data[1] = 0x00; // page code 0
state.scsi.dati.data[2] = 0x00; // reserved
state.scsi.dati.data[3] = 0x02; // page length
state.scsi.dati.data[4] = 0x00; // page 0 is supported.
state.scsi.dati.data[5] = 0x80; // page 0x80 is supported.
break;
case 0x80:
char serial_number[20];
sprintf(serial_number,"SRL%04x",scsi_initiator_id[0]*0x0101);
// unit serial number page
state.scsi.dati.data[1] = 0x80; // page code: 0x80
state.scsi.dati.data[2] = 0x00; // reserved
state.scsi.dati.data[3] = (u8)strlen(serial_number);
memcpy(&state.scsi.dati.data[4],serial_number,strlen(serial_number));
break;
default:
printf("Don't know format for vital product data page %02x!!\n",state.scsi.cmd.data[2]);
FAILURE("SCSI Command Failed");
state.scsi.dati.data[1] = state.scsi.cmd.data[2]; // page code
state.scsi.dati.data[2] = 0x00; // reserved
}
}
else
{
// Return values:
if (retlen < 36) {
printf("%s: SCSI inquiry len=%i, <36!\n", devid_string, retlen);
retlen = 36;
}
state.scsi.dati.data[1] = 0; // not removable;
state.scsi.dati.data[2] = 0x02; // ANSI scsi 2
state.scsi.dati.data[3] = 0x02; // response format
state.scsi.dati.data[4] = 32; // additional length
state.scsi.dati.data[5] = 0; // reserved
state.scsi.dati.data[6] = 0x04; // reserved
state.scsi.dati.data[7] = 0x60; // capabilities
// vendor model rev.
memcpy(&(state.scsi.dati.data[8]),"DEC RZ58 (C) DEC2000",28);
// Some data is different for CD-ROM drives:
if (cdrom()) {
state.scsi.dati.data[1] = 0x80; // 0x80 = removable
// vendor model rev.
memcpy(&(state.scsi.dati.data[8]), "DEC RRD42 (C) DEC 4.5d",28);
}
}
state.scsi.dati.read = 0;
state.scsi.dati.available = retlen;
#if defined(DEBUG_SCSI)
printf("%s: Returning data: ",devid_string);
for (unsigned int x1 = 0; x1 <36; x1++) printf("%02x ",state.scsi.dati.data[x1]);
printf("\n");
#endif
do_scsi_error(SCSI_OK);
}
break;
case SCSICMD_MODE_SENSE:
case SCSICMD_MODE_SENSE_10:
#if defined(DEBUG_SCSI)
printf("%s: MODE SENSE.\n",devid_string);
#endif
{
int num_blk_desc = 1;
if (state.scsi.cmd.data[0] == SCSICMD_MODE_SENSE)
{
q = 4;
retlen = state.scsi.cmd.data[4];
state.scsi.dati.data[0] = retlen; // mode data length
state.scsi.dati.data[1] = cdrom() ? 0x01 : 0x00; // medium type (120 mm data for CD-ROM)
state.scsi.dati.data[2] = 0x00; // device specific parameter
state.scsi.dati.data[3] = 8 * num_blk_desc; // block descriptor length: 1 page (?)
}
else
{
q = 8;
retlen = state.scsi.cmd.data[7] * 256 + state.scsi.cmd.data[8];
state.scsi.dati.data[0] = (u8)(retlen>>8); // mode data length
state.scsi.dati.data[1] = (u8)retlen;
state.scsi.dati.data[2] = cdrom() ? 0x01 : 0x00; // medium type (120 mm data for CD-ROM)
state.scsi.dati.data[3] = 0x00; // device specific parameter
state.scsi.dati.data[4] = 0x00; // reserved
state.scsi.dati.data[5] = 0x00; // reserved
state.scsi.dati.data[6] = (u8)((8 * num_blk_desc)>>8); // block descriptor length: 1 page (?)
state.scsi.dati.data[7] = (u8)(8 * num_blk_desc);
}
if ((state.scsi.cmd.data[2] & 0xc0) > 0x40)
{
printf(" mode sense, cmd[2] = 0x%02x.\n", state.scsi.cmd.data[2]);
FAILURE("SCSI Command Failed");
}
bool changeable = ((state.scsi.cmd.data[2] & 0xc0) ==0x40);
// Return data:
if (retlen > DATI_BUFSZ) {
printf("%s: read too big (%d)\n", devid_string, retlen);
do_scsi_error(SCSI_TOO_BIG);
break;
}
state.scsi.dati.available = retlen; // Restore size.
pagecode = state.scsi.cmd.data[2] & 0x3f;
//printf("[ MODE SENSE pagecode=%i ]\n", pagecode);
state.scsi.dati.data[q++] = 0x00; // density code
state.scsi.dati.data[q++] = 0; // nr of blocks, high (0 = all remaining blocks)
state.scsi.dati.data[q++] = 0; // nr of blocks, mid
state.scsi.dati.data[q++] = 0; // nr of blocks, low
state.scsi.dati.data[q++] = 0x00; // reserved
state.scsi.dati.data[q++] = (u8)(get_block_size() >> 16) & 255;
state.scsi.dati.data[q++] = (u8)(get_block_size() >> 8) & 255;
state.scsi.dati.data[q++] = (u8)(get_block_size() >> 0) & 255;
for (unsigned int x1 = q; x1 < retlen; x1++)
state.scsi.dati.data[x1] = 0;
do_scsi_error(SCSI_OK);
// descriptors, 8 bytes (each)
// page, n bytes (each)
switch (pagecode) {
case SCSIMP_VENDOR: // vendor specific
// TODO: Nothing here?
break;
case SCSIMP_READ_WRITE_ERRREC: // read-write error recovery page
state.scsi.dati.data[q + 0] = pagecode;
state.scsi.dati.data[q + 1] = 10;
break;
case SCSIMP_FORMAT_PARAMS: // format device page
state.scsi.dati.data[q + 0] = pagecode;
state.scsi.dati.data[q + 1] = 22;
if (!changeable)
{
// 10,11 = sectors per track
state.scsi.dati.data[q + 10] = 0;
state.scsi.dati.data[q + 11] = (u8)get_sectors();
// 12,13 = physical sector size
state.scsi.dati.data[q + 12] = (u8)(get_block_size() >> 8) & 255;
state.scsi.dati.data[q + 13] = (u8)(get_block_size() >> 0) & 255;
}
break;
case SCSIMP_RIGID_GEOMETRY: // rigid disk geometry page
state.scsi.dati.data[q + 0] = pagecode;
state.scsi.dati.data[q + 1] = 22;
if (!changeable)
{
state.scsi.dati.data[q + 2] = (u8)(get_cylinders() >> 16) & 255;
state.scsi.dati.data[q + 3] = (u8)(get_cylinders() >> 8) & 255;
state.scsi.dati.data[q + 4] = (u8)get_cylinders() & 255;
state.scsi.dati.data[q + 5] = (u8)get_heads();
//rpms
state.scsi.dati.data[q + 20] = (7200 >> 8) & 255;
state.scsi.dati.data[q + 21] = 7200 & 255;
}
break;
case SCSIMP_FLEX_PARAMS: // flexible disk page
if (cdrom())
{
printf("%s: CD-ROM write parameter page not implemented.\n",devid_string);
FAILURE("SCSI Command Failed");
}
state.scsi.dati.data[q + 0] = pagecode;
state.scsi.dati.data[q + 1] = 0x1e; // length
if (!changeable)
{
// 2,3 = transfer rate
state.scsi.dati.data[q + 2] = ((5000) >> 8) & 255;
state.scsi.dati.data[q + 3] = (5000) & 255;
state.scsi.dati.data[q + 4] = (u8)get_heads();
state.scsi.dati.data[q + 5] = (u8)get_sectors();
// 6,7 = data bytes per sector
state.scsi.dati.data[q + 6] = (u8)(get_block_size() >> 8) & 255;
state.scsi.dati.data[q + 7] = (u8)(get_block_size() >> 0) & 255;
state.scsi.dati.data[q + 8] = (u8)(get_cylinders() >> 8) & 255;
state.scsi.dati.data[q + 9] = (u8)get_cylinders() & 255;
//rpms
state.scsi.dati.data[q + 28] = (7200 >> 8) & 255;
state.scsi.dati.data[q + 29] = 7200 & 255;
}
break;