forked from mist-devel/mist-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hdd.c
1817 lines (1674 loc) · 56.7 KB
/
hdd.c
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
/*
Copyright 2008, 2009 Jakub Bednarski
This file is part of Minimig
Minimig 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 3 of the License, or
(at your option) any later version.
Minimig 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, see <http://www.gnu.org/licenses/>.
*/
// 2009-11-22 - read/write multiple implemented
// 2020-11-14 - AMR: Simplified and combined read / readm + write / writem. AROS IDE now works.
#include <stdio.h>
#include <string.h>
#include "swab.h"
#include "utils.h"
#include "errors.h"
#include "hardware.h"
#include "fat_compat.h"
#include "FatFs/diskio.h"
#include "hdd.h"
#include "hdd_internal.h"
#include "menu.h"
#include "fpga.h"
#include "scsi.h"
#include "cue_parser.h"
#ifdef HAVE_QSPI
#include "qspi.h"
#include "user_io.h"
#endif
#include "debug.h"
hardfileTYPE *hardfile[HARDFILES];
// hardfile structure
hdfTYPE hdf[HARDFILES];
#define AUDIO_PLAYING 0x11
#define AUDIO_PAUSED 0x12
#define AUDIO_COMPLETE 0x13
#define AUDIO_ERROR 0x14
#define AUDIO_NOSTAT 0x15
typedef struct
{
unsigned char key;
unsigned char asc;
unsigned char ascq;
unsigned short blocksize;
unsigned int currentlba;
unsigned int endlba;
unsigned char audiostatus;
} cdrom_t;
static cdrom_t cdrom;
static void SwapBytes(char *c, unsigned int len)
{
char temp;
while(len) {
temp = *c;
*c=c[1];
c[1]=temp;
len-=2;
c+=2;
}
}
// RDBChecksum()
static void RDBChecksum(unsigned long *p)
{
unsigned long count=p[1];
unsigned long c2;
long result=0;
p[2]=0;
for(c2=0;c2<count;++c2) result+=p[c2];
p[2]=(unsigned long)-result;
}
// FakeRDB()
// if the hardfile doesn't have a RigidDiskBlock, we synthesize one
static void FakeRDB(int unit,int block)
{
int i;
// start by clearing the sector buffer
memset(sector_buffer, 0, 512);
// if we're asked for LBA 0 we create an RDSK block, and if LBA 1, a PART block
switch(block) {
case 0: {
// RDB
hdd_debugf("FAKE: RDB");
struct RigidDiskBlock *rdb=(struct RigidDiskBlock *)sector_buffer;
rdb->rdb_ID = 'R'<<24 | 'D' << 16 | 'S' << 8 | 'K';
rdb->rdb_Summedlongs=0x40;
rdb->rdb_HostID=0x07;
rdb->rdb_BlockBytes=0x200;
rdb->rdb_Flags=0x12; // (Disk ID valid, no LUNs after this one)
rdb->rdb_BadBlockList=0xffffffff; // We don't provide a bad block list
rdb->rdb_PartitionList=1;
rdb->rdb_FileSysHeaderList=0xffffffff;
rdb->rdb_DriveInit=0xffffffff;
rdb->rdb_Reserved1[0]=0xffffffff;
rdb->rdb_Reserved1[1]=0xffffffff;
rdb->rdb_Reserved1[2]=0xffffffff;
rdb->rdb_Reserved1[3]=0xffffffff;
rdb->rdb_Reserved1[4]=0xffffffff;
rdb->rdb_Reserved1[5]=0xffffffff;
rdb->rdb_Cylinders=hdf[unit].cylinders;
rdb->rdb_Sectors=hdf[unit].sectors;
rdb->rdb_Heads=hdf[unit].heads;
rdb->rdb_Interleave=1;
rdb->rdb_Park=rdb->rdb_Cylinders;
rdb->rdb_WritePreComp=rdb->rdb_Cylinders;
rdb->rdb_ReducedWrite=rdb->rdb_Cylinders;
rdb->rdb_StepRate=3;
rdb->rdb_RDBBlocksLo=0;
rdb->rdb_RDBBlocksHi=1;
rdb->rdb_LoCylinder=1;
rdb->rdb_HiCylinder=rdb->rdb_Cylinders-1;
rdb->rdb_CylBlocks=rdb->rdb_Heads * rdb->rdb_Sectors;
rdb->rdb_AutoParkSeconds=0;
rdb->rdb_HighRDSKBlock=1;
strcpy(rdb->rdb_DiskVendor,"Do not ");
strcpy(rdb->rdb_DiskProduct, "repartition!");
// swap byte order of strings to be able to "unswap" them after checksum
unsigned long *p = (unsigned long*)rdb;
for(i=0;i<(8+16)/4;i++) p[40+i] = swab32(p[40+i]);
RDBChecksum((unsigned long *)rdb);
// swap byte order of first 0x40 long values
for(i=0;i<0x40;i++) p[i] = swab32(p[i]);
break;
}
case 1: {
// Partition
hdd_debugf("FAKE: Partition");
struct PartitionBlock *pb=(struct PartitionBlock *)sector_buffer;
pb->pb_ID = 'P'<<24 | 'A' << 16 | 'R' << 8 | 'T';
pb->pb_Summedlongs=0x40;
pb->pb_HostID=0x07;
pb->pb_Next=0xffffffff;
pb->pb_Flags=0x1; // bootable
pb->pb_DevFlags=0;
strcpy(pb->pb_DriveName,unit?"1HD\003":"0HD\003"); // "DH0"/"DH1" BCPL string
pb->pb_Environment.de_TableSize=0x10;
pb->pb_Environment.de_SizeBlock=0x80;
pb->pb_Environment.de_Surfaces=hdf[unit].heads;
pb->pb_Environment.de_SectorPerBlock=1;
pb->pb_Environment.de_BlocksPerTrack=hdf[unit].sectors;
pb->pb_Environment.de_Reserved=2;
pb->pb_Environment.de_LowCyl=1;
pb->pb_Environment.de_HighCyl=hdf[unit].cylinders-1;
pb->pb_Environment.de_NumBuffers=30;
pb->pb_Environment.de_MaxTransfer=0xffffff;
pb->pb_Environment.de_Mask=0x7ffffffe;
pb->pb_Environment.de_DosType=0x444f5301;
RDBChecksum((unsigned long *)pb);
// swap byte order of first 0x40 entries
unsigned long *p = (unsigned long*)pb;
for(i=0;i<0x40;i++) p[i] = swab32(p[i]);
break;
}
default: {
break;
}
}
}
// IdentifiyDevice()
// builds Identify Device struct
static void IdentifyDevice(unsigned short *pBuffer, unsigned char unit)
{
char *p, i, x;
unsigned long total_sectors = hdf[unit].cylinders * hdf[unit].heads * hdf[unit].sectors;
memset(pBuffer, 0, 512);
switch(hdf[unit].type) {
case HDF_FILE | HDF_SYNTHRDB:
case HDF_FILE:
pBuffer[0] = 1 << 6; // hard disk
pBuffer[1] = hdf[unit].cylinders; // cyl count
pBuffer[3] = hdf[unit].heads; // head count
pBuffer[6] = hdf[unit].sectors; // sectors per track
// FIXME - can get serial no from card itself.
memcpy((char*)&pBuffer[10], "iMTSiMiniMHgrafdli e", 20); // serial number - byte swapped
memcpy((char*)&pBuffer[23], ".100 ", 8); // firmware version - byte swapped
p = (char*)&pBuffer[27];
// FIXME - likewise the model name can be fetched from the card.
if (hdf[unit].type & HDF_SYNTHRDB) {
memcpy(p, "DON'T ", 40);
p += 8;
memcpy(p, "REPARTITION! ", 16);
} else {
memcpy(p, "YAQUBE ", 40); // model name - byte swapped
p += 8;
for (i = 0; (x = hardfile[unit]->name[i]) && i < 16; i++) // copy file name as model name
p[i] = x;
}
SwapBytes((char*)&pBuffer[27], 40);
break;
case HDF_CARD:
case HDF_CARDPART0:
case HDF_CARDPART1:
case HDF_CARDPART2:
case HDF_CARDPART3:
pBuffer[0] = 1 << 6; // hard disk
pBuffer[1] = hdf[unit].cylinders; // cyl count
pBuffer[3] = hdf[unit].heads; // head count
pBuffer[6] = hdf[unit].sectors; // sectors per track
// FIXME - can get serial no from card itself.
memcpy((char*)&pBuffer[10], "iMTSiMiniMSg0D ", 20); // serial number - byte swapped
pBuffer[23]+=hdf[unit].type-HDF_CARD;
memcpy((char*)&pBuffer[23], ".100 ", 8); // firmware version - byte swapped
p = (char*)&pBuffer[27];
// FIXME - likewise the model name can be fetched from the card.
memcpy(p, "YAQUBE ", 40); // model name - byte swapped
p += 8;
if (hdf[unit].type==HDF_CARD)
memcpy(p, "SD/MMC Card", 11); // copy file name as model name
else {
memcpy(p, "Card Part 1", 11); // copy file name as model name
p[10]+=hdf[unit].partition;
}
SwapBytes((char*)&pBuffer[27], 40);
break;
}
pBuffer[47] = 0x8010; // maximum sectors per block in Read/Write Multiple command
pBuffer[49] = 0x0200; // support LBA addressing
pBuffer[53] = 1;
pBuffer[54] = hdf[unit].cylinders;
pBuffer[55] = hdf[unit].heads;
pBuffer[56] = hdf[unit].sectors;
pBuffer[57] = (unsigned short)total_sectors;
pBuffer[58] = (unsigned short)(total_sectors >> 16);
pBuffer[60] = (unsigned short)total_sectors;
pBuffer[61] = (unsigned short)(total_sectors >> 16);
}
// IdentifiyDevice()
// builds Identify Packet Device struct
static void IdentifyPacketDevice(unsigned short *pBuffer, unsigned char unit)
{
memset(pBuffer, 0, 512);
pBuffer[0] = 0x8580;
memcpy((char*)&pBuffer[10], "iMTSiMiniMCgRDMO ", 20); // serial number - byte swapped
memcpy((char*)&pBuffer[23], ".100 ", 8); // firmware version - byte swapped
memcpy((char*)&pBuffer[27], " ", 40); // Model number - byte swapped
pBuffer[49] = 0x0100;
pBuffer[53] = 1;
pBuffer[82] = 0x4214; // command set supported (NOP, DEVICE RESET, PACKET, Removable media)
pBuffer[126] = 0xfffe; // byte count = 0 behavior
}
// chs2lba()
static unsigned long chs2lba(unsigned short cylinder, unsigned char head, unsigned short sector, unsigned char unit, char lbamode)
{
if (lbamode){
return ((head<<24) + (cylinder<<8) + sector);
}else
return (cylinder * hdf[unit].heads + head) * hdf[unit].sectors + sector - 1;
}
// HardFileSeek()
static unsigned char HardFileSeek(hdfTYPE *pHDF, unsigned long lba)
{
FSIZE_t seek_pos = (FSIZE_t) lba << 9;
FRESULT res;
res = f_lseek(&pHDF->idxfile->file, seek_pos);
if (res != FR_OK || f_tell(&pHDF->idxfile->file) != seek_pos) {
hdd_debugf("Seek error: %llu, %llu", seek_pos, f_tell(&pHDF->idxfile->file));
return 0;
}
return 1;
}
// WriteTaskFile()
static void WriteTaskFile(unsigned char error, unsigned char sector_count, unsigned char sector_number, unsigned char cylinder_low, unsigned char cylinder_high, unsigned char drive_head)
{
EnableFpga();
SPI(CMD_IDE_REGS_WR); // write task file registers command
SPI(0x00);
SPI(0x00); // dummy
SPI(0x00);
SPI(0x00); // dummy
SPI(0x00);
SPI(0x00); // dummy
SPI(0x00);
SPI(0x00);
SPI(error); // error
SPI(0x00);
SPI(sector_count); // sector count
SPI(0x00);
SPI(sector_number); // sector number
SPI(0x00);
SPI(cylinder_low); // cylinder low
SPI(0x00);
SPI(cylinder_high); // cylinder high
SPI(0x00);
SPI(drive_head); // drive/head
DisableFpga();
}
// WriteStatus()
static void WriteStatus(unsigned char status)
{
EnableFpga();
SPI(CMD_IDE_STATUS_WR);
SPI(status);
SPI(0x00);
SPI(0x00);
SPI(0x00);
SPI(0x00);
DisableFpga();
}
static void WritePacket(unsigned char unit, const unsigned char *buf, unsigned short bufsize, unsigned short bytelimit, char lastpacket)
{
unsigned short bytes;
do {
bytes = MIN(bufsize, bytelimit);
while (!(GetFPGAStatus() & CMD_IDECMD)); // wait for empty sector buffer
WriteTaskFile(0, 0x02, 0, bytes & 0xff, (bytes>>8) & 0xff, 0xa0 | ((unit & 0x01)<<4));
if (bytes) {
#ifdef HAVE_QSPI
if(minimig_v2()) {
qspi_start_write();
qspi_write_block(buf, bytes);
qspi_end();
} else {
#else
EnableFpga();
SPI(CMD_IDE_DATA_WR); // write data command
SPI(0x00);
SPI(0x00);
SPI(0x00);
SPI(0x00);
SPI(0x00);
spi_write(buf, bytes);
DisableFpga();
#endif
#ifdef HAVE_QSPI
}
#endif
}
buf += bytes;
bufsize -= bytes;
if (lastpacket && !bufsize)
WriteStatus(IDE_STATUS_IRQ | IDE_STATUS_END);
else
WriteStatus(IDE_STATUS_IRQ);
} while (bufsize);
}
static void cdrom_reset()
{
cdrom.key = cdrom.asc = cdrom.ascq = 0;
cdrom.currentlba = 0;
cdrom.audiostatus = AUDIO_NOSTAT;
cdrom.blocksize = 2048;
}
static void cdrom_setsense(unsigned char key, unsigned char asc, unsigned char ascq)
{
cdrom.key = key;
cdrom.asc = asc;
cdrom.ascq = ascq;
}
static void cdrom_ok()
{
cdrom.key = cdrom.asc = cdrom.ascq = 0;
}
static void cdrom_send_error(unsigned char unit)
{
WriteTaskFile((cdrom.key << 4) | 0x04, 0x03, 0, 0, 0, 0xa0 | ((unit & 0x01)<<4));
WriteStatus(IDE_STATUS_END | IDE_STATUS_ERR | IDE_STATUS_IRQ);
}
static void cdrom_generate_header(unsigned char *pBuffer, unsigned int lba)
{
// TODO: implement
}
static void cdrom_generate_ecc(unsigned char *pBuffer, unsigned int lba)
{
// TODO: implement
}
static void cdrom_playaudio()
{
UINT br;
unsigned char track = cue_gettrackbylba(cdrom.currentlba);
if ((toc.tracks[track].type != SECTOR_AUDIO) || (toc.tracks[track].sector_size != 2352)) {
cdrom.audiostatus = AUDIO_ERROR;
return;
}
DISKLED_ON
int offset = (cdrom.currentlba - toc.tracks[track].start) * toc.tracks[track].sector_size + toc.tracks[track].offset;
f_lseek(&toc.file->file, offset);
f_read(&toc.file->file, sector_buffer, 2352, &br);
EnableFpga();
SPI(CMD_IDE_CDDA_WR); // write cdda command
SPI(0x00);
SPI(0x00);
SPI(0x00);
SPI(0x00);
SPI(0x00);
spi_write(sector_buffer, 2352);
DisableFpga();
DISKLED_OFF
if (cdrom.currentlba == cdrom.endlba)
cdrom.audiostatus = AUDIO_COMPLETE;
else
cdrom.currentlba++;
}
static void PKT_Read(unsigned char unit, unsigned int lba, unsigned int len, unsigned short bytelimit, unsigned short blocksize)
{
UINT br;
unsigned char *pBuffer;
if (!toc.valid) {
cdrom_setsense(SENSEKEY_NOT_READY, 0x3a, 0);
cdrom_send_error(unit);
return;
}
cdrom.audiostatus = AUDIO_NOSTAT;
cdrom_ok();
WriteStatus(IDE_STATUS_RDY | IDE_STATUS_PKT); // pio in (class 1) command type
while (len--) {
unsigned char track = cue_gettrackbylba(lba);
int offset = (lba - toc.tracks[track].start) * toc.tracks[track].sector_size + toc.tracks[track].offset;
if ((blocksize == 2048 && toc.tracks[track].type != SECTOR_DATA_MODE1 && toc.tracks[track].type != SECTOR_DATA_MODE2) ||
(blocksize != 2048 && blocksize !=2352) ||
(toc.tracks[track].sector_size != 2048 && toc.tracks[track].sector_size != 2352 && toc.tracks[track].sector_size != 2336)) {
cdrom_setsense(SENSEKEY_ILLEGAL_REQUEST, 0x26, 2);
cdrom_send_error(unit);
return;
}
pBuffer = sector_buffer;
cdrom.currentlba = lba;
if (blocksize == 2048 && toc.tracks[track].sector_size == 2352) offset+=16;
if (blocksize == 2048 && toc.tracks[track].sector_size >= 2336 && toc.tracks[track].type == SECTOR_DATA_MODE2) offset+=8; // CD-XA with 8 subheader bytes
if (blocksize == 2352 && (toc.tracks[track].sector_size == 2048 || toc.tracks[track].sector_size == 2336)) {
cdrom_generate_header(pBuffer, lba);
pBuffer+=16;
}
hdd_debugf("lba: %d track: %d, offset: %d, blocksize: %d sector_size: %d", lba, track, offset, blocksize, toc.tracks[track].sector_size);
f_lseek(&toc.file->file, offset);
f_read(&toc.file->file, pBuffer, MIN(toc.tracks[track].sector_size, blocksize), &br);
if (blocksize == 2352 && toc.tracks[track].sector_size == 2048) {
cdrom_generate_ecc(sector_buffer, lba);
}
lba++;
WritePacket(unit, sector_buffer, blocksize, bytelimit, !len);
}
}
static void PKT_Read12(unsigned char *cmd, unsigned char unit, unsigned short bytelimit)
{
hdd_debugf("IDE%d: PKT_Read12 (bytelimit=%d)", unit, bytelimit);
unsigned int lba = cmd[5] | (cmd[4] << 8) | (cmd[3] << 16) | (cmd[2] << 24);
unsigned int len = cmd[9] | (cmd[8] << 8) | (cmd[7] << 16) | (cmd[6] << 24);
cdrom.audiostatus = AUDIO_NOSTAT;
PKT_Read(unit, lba, len, bytelimit, cdrom.blocksize);
}
static void PKT_Read10(unsigned char *cmd, unsigned char unit, unsigned short bytelimit)
{
hdd_debugf("IDE%d: PKT_Read10 (bytelimit=%d)", unit, bytelimit);
unsigned int lba = cmd[5] | (cmd[4] << 8) | (cmd[3] << 16) | (cmd[2] << 24);
unsigned int len = cmd[8] | (cmd[7] << 8);
cdrom.audiostatus = AUDIO_NOSTAT;
PKT_Read(unit, lba, len, bytelimit, cdrom.blocksize);
}
static void PKT_DoReadCD(unsigned char *cmd, unsigned char unit, unsigned short bytelimit, unsigned int lba, unsigned int len)
{
unsigned char errorfield = (cmd[9] & 0x06) >> 1;
unsigned char edcecc = (cmd[9] & 0x08) >> 3;
unsigned char userdata = (cmd[9] & 0x10) >> 4;
unsigned char header = (cmd[9] & 0x60) >> 5;
unsigned char sync = (cmd[9] & 0x80) >> 7;
// FIXME: userdata only or full sector (without subchannels) are allowed
if (!userdata || ((edcecc || header || sync) != (edcecc && header && sync))) {
cdrom_setsense(SENSEKEY_ILLEGAL_REQUEST, 0x24, 0);
cdrom_send_error(unit);
return;
}
unsigned char track = cue_gettrackbylba(lba);
unsigned short blocksize = 2048;
if (toc.tracks[track].type == SECTOR_AUDIO || (edcecc && header && sync))
blocksize = 2352;
hdd_debugf("ReadCD: lba=%d len=%d blocksize=%d", lba, len, blocksize);
PKT_Read(unit, lba, len, bytelimit, blocksize);
}
static void PKT_ReadCD(unsigned char *cmd, unsigned char unit, unsigned short bytelimit)
{
hdd_debugf("IDE%d: PKT_ReadCD (bytelimit=%d)", unit, bytelimit);
unsigned int lba = cmd[5] | (cmd[4] << 8) | (cmd[3] << 16) | (cmd[2] << 24);
unsigned int len = cmd[8] | (cmd[7] << 8) | (cmd[6] << 16);
cdrom.audiostatus = AUDIO_NOSTAT;
PKT_DoReadCD(cmd, unit, bytelimit, lba, len);
}
static void PKT_ReadCDMSF(unsigned char *cmd, unsigned char unit, unsigned short bytelimit)
{
hdd_debugf("IDE%d: PKT_ReadCDMSF (bytelimit=%d)", unit, bytelimit);
unsigned int start = MSF2LBA(cmd[3], cmd[4], cmd[5]);
unsigned int end = MSF2LBA(cmd[6], cmd[7], cmd[8]);
cdrom.audiostatus = AUDIO_NOSTAT;
if (start > end) {
cdrom_setsense(SENSEKEY_ILLEGAL_REQUEST, 0x24, 0);
cdrom_send_error(unit);
return;
}
PKT_DoReadCD(cmd, unit, bytelimit, start, end-start);
}
static void PKT_PlayAudio(unsigned char unit, unsigned int start, unsigned int end)
{
if (!toc.valid) {
cdrom_setsense(SENSEKEY_NOT_READY, 0x3a, 0);
cdrom_send_error(unit);
return;
}
if (start > toc.end || end > toc.end || start > end) {
cdrom_setsense(SENSEKEY_ILLEGAL_REQUEST, 0x21, 0);
cdrom_send_error(unit);
return;
}
unsigned char track = cue_gettrackbylba(start);
if (toc.tracks[track].type != SECTOR_AUDIO) {
cdrom_setsense(SENSEKEY_ILLEGAL_REQUEST, 0x64, 0);
cdrom_send_error(unit);
return;
}
cdrom.currentlba = start;
cdrom.endlba = end;
cdrom.audiostatus = AUDIO_PLAYING;
cdrom_ok();
WriteTaskFile(0, 0x03, 0, 0, 0, 0xa0 | ((unit & 0x01)<<4));
WriteStatus(IDE_STATUS_END | IDE_STATUS_IRQ);
}
static void PKT_PlayAudioMSF(unsigned char *cmd, unsigned char unit)
{
hdd_debugf("IDE%d: PKT_PlayAudioMSF", unit);
unsigned int start = (cmd[2] == 0xff && cmd[3] == 0xff && cmd[5] == 0xff) ? cdrom.currentlba : MSF2LBA(cmd[3], cmd[4], cmd[5]);
unsigned int end = MSF2LBA(cmd[6], cmd[7], cmd[8]);
cdrom.audiostatus = AUDIO_NOSTAT;
PKT_PlayAudio(unit, start, end);
}
static void PKT_PlayAudio10(unsigned char *cmd, unsigned char unit)
{
hdd_debugf("IDE%d: PKT_PlayAudio10", unit);
unsigned int start = (cmd[2] << 24) | (cmd[3] << 16) | (cmd[4] << 8) | cmd[5];
if (start == 0xffffffff) start = cdrom.currentlba;
unsigned int end = start + (cmd[7] << 8) + cmd[8];
cdrom.audiostatus = AUDIO_NOSTAT;
PKT_PlayAudio(unit, start, end);
}
// obsolete, but used by Napalm
static void PKT_PlayAudioTrackIndex(unsigned char *cmd, unsigned char unit)
{
hdd_debugf("IDE%d: PKT_PlayAudioTrackIndex", unit);
unsigned char starttrack = cmd[4];
unsigned char endtrack = cmd[7];
cdrom.audiostatus = AUDIO_NOSTAT;
if (!toc.valid) {
cdrom_setsense(SENSEKEY_NOT_READY, 0x3a, 0);
cdrom_send_error(unit);
return;
}
if (starttrack > endtrack || starttrack >= toc.last || !starttrack || !endtrack || endtrack >= toc.last) {
cdrom_setsense(SENSEKEY_ILLEGAL_REQUEST, 0x21, 0);
cdrom_send_error(unit);
return;
}
PKT_PlayAudio(unit, toc.tracks[starttrack-1].start, toc.tracks[endtrack-1].end);
}
static void PKT_PauseResume(unsigned char *cmd, unsigned char unit)
{
hdd_debugf("IDE%d: PKT_PauseResume", unit);
int resume = cmd[8] & 0x01;
if (!toc.valid) {
cdrom_setsense(SENSEKEY_NOT_READY, 0x3a, 0);
cdrom_send_error(unit);
return;
}
if ((resume && cdrom.audiostatus != AUDIO_PAUSED) || (!resume && cdrom.audiostatus != AUDIO_PLAYING)) {
cdrom_setsense(SENSEKEY_ILLEGAL_REQUEST, 0x2c, 0);
cdrom_send_error(unit);
} else {
cdrom.audiostatus = resume ? AUDIO_PLAYING : AUDIO_PAUSED;
cdrom_ok();
WriteTaskFile(0, 0x03, 0, 0, 0, 0xa0 | ((unit & 0x01)<<4));
WriteStatus(IDE_STATUS_END | IDE_STATUS_IRQ);
}
}
static void PKT_StopPlayScan(unsigned char *cmd, unsigned char unit)
{
hdd_debugf("IDE%d: PKT_StopPlayScan", unit);
if (!toc.valid) {
cdrom_setsense(SENSEKEY_NOT_READY, 0x3a, 0);
cdrom_send_error(unit);
return;
}
cdrom.audiostatus = AUDIO_NOSTAT;
cdrom_ok();
WriteTaskFile(0, 0x03, 0, 0, 0, 0xa0 | ((unit & 0x01)<<4));
WriteStatus(IDE_STATUS_END | IDE_STATUS_IRQ);
}
static void PKT_SubChannel(unsigned char *cmd, unsigned char unit, unsigned short bytelimit)
{
hdd_debugf("IDE%d: PKT_SubChannel (bytelimit=%d)", unit, bytelimit);
unsigned short bufsize = (cmd[7] << 8) | cmd[8];
unsigned char track;
unsigned char msftime = cmd[1] & 0x02;
unsigned short respsize = 0;
if (!toc.valid) {
cdrom_setsense(SENSEKEY_NOT_READY, 0x3a, 0);
cdrom_send_error(unit);
return;
}
track = cue_gettrackbylba(cdrom.currentlba);
if (track >= toc.last) track = toc.last - 1;
memset(sector_buffer, 0, 24);
sector_buffer[1] = cdrom.audiostatus;
switch (cmd[3]) {
case 1:
// current position
respsize = (cmd[2] & 0x40) ? 16 : 4;
sector_buffer[4] = 0x01;
sector_buffer[5] = (1 << 4) | (toc.tracks[track-1].type == SECTOR_AUDIO ? 0 : 4);
sector_buffer[6] = track + 1;
sector_buffer[7] = cdrom.currentlba >= toc.tracks[track].start ? 1 : 0; // TODO: support more than 1 indices
unsigned int rellba = cdrom.currentlba - toc.tracks[track].start;
if (msftime) {
msf_t MSF;
LBA2MSF(cdrom.currentlba+150, &MSF);
sector_buffer[9] = MSF.m;
sector_buffer[10] = MSF.s;
sector_buffer[11] = MSF.f;
LBA2MSF(rellba, &MSF);
sector_buffer[13] = MSF.m;
sector_buffer[14] = MSF.s;
sector_buffer[15] = MSF.f;
} else {
sector_buffer[8] = (cdrom.currentlba >> 24) & 0xff;
sector_buffer[9] = (cdrom.currentlba >> 16) & 0xff;
sector_buffer[10] = (cdrom.currentlba >> 8) & 0xff;
sector_buffer[11] = cdrom.currentlba & 0xff;
sector_buffer[12] = (rellba >> 24) & 0xff;
sector_buffer[13] = (rellba >> 16) & 0xff;
sector_buffer[14] = (rellba >> 8) & 0xff;
sector_buffer[15] = rellba & 0xff;
}
break;
case 2:
// Media Catalog number
respsize = 24;
sector_buffer[4] = 0x02;
break;
case 3:
// ISRC
respsize = 24;
sector_buffer[4] = 0x02;
break;
default:
// reserved
break;
}
sector_buffer[3] = respsize - 4;
cdrom_ok();
bufsize = MIN(bufsize, respsize);
WriteStatus(IDE_STATUS_RDY | IDE_STATUS_PKT); // pio in (class 1) command type
WritePacket(unit, sector_buffer, bufsize, bytelimit, 1);
}
static void PKT_ReadCapacity(unsigned char *cmd, unsigned char unit, unsigned short bytelimit)
{
hdd_debugf("IDE%d: PKT_ReadCapacity (bytelimit=%d)", unit, bytelimit);
if (!toc.valid) {
cdrom_setsense(SENSEKEY_NOT_READY, 0x3a, 0);
cdrom_send_error(unit);
return;
}
sector_buffer[0] = (toc.end >> 24) & 0xff;
sector_buffer[1] = (toc.end >> 16) & 0xff;
sector_buffer[2] = (toc.end >> 8) & 0xff;
sector_buffer[3] = toc.end & 0xff;
sector_buffer[4] = sector_buffer[5] = 0;
sector_buffer[6] = (2048 >> 8) & 0xff;
sector_buffer[7] = 2048 & 0xff;
//hexdump(sector_buffer, 8, 0);
cdrom_ok();
WriteStatus(IDE_STATUS_RDY | IDE_STATUS_PKT); // pio in (class 1) command type
WritePacket(unit, sector_buffer, 8, bytelimit, 1);
}
static void PKT_ReadTOC(unsigned char *cmd, unsigned char unit, unsigned short bytelimit)
{
hdd_debugf("IDE%d: PKT_ReadTOC (bytelimit=%d)", unit, bytelimit);
unsigned short bufsize = (cmd[7] << 8) | cmd[8];
unsigned char track = cmd[6];
unsigned char msftime = cmd[1] & 0x02;
unsigned short tocsize = 4;
unsigned char *p = sector_buffer;
int lba;
if (!toc.valid) {
cdrom_setsense(SENSEKEY_NOT_READY, 0x3a, 0);
cdrom_send_error(unit);
return;
}
switch(cmd[2] & 0x0f) {
case 0:
hdd_debugf("TOC format 0");
if (track > toc.last && track != 0xAA) {
cdrom_setsense(SENSEKEY_ILLEGAL_REQUEST, 0x26, 2);
cdrom_send_error(unit);
return;
}
sector_buffer[2] = 1;
sector_buffer[3] = toc.last;
p += 4;
for (int i=1; i<=toc.last; i++) {
if (i>=track) {
p[0] = p[3] = 0;
p[1] = (1 << 4) | (toc.tracks[i-1].type == SECTOR_AUDIO ? 0 : 4);
p[2] = i;
lba = toc.tracks[i-1].start;
hdd_debugf("track %d lba: %d", i, lba);
if (msftime) {
msf_t MSF;
LBA2MSF(lba+150, &MSF);
p[4] = 0;
p[5] = MSF.m;
p[6] = MSF.s;
p[7] = MSF.f;
} else {
p[4] = (lba >> 24) & 0xff;
p[5] = (lba >> 16) & 0xff;
p[6] = (lba >> 8) & 0xff;
p[7] = lba & 0xff;
}
p+=8;
tocsize+=8;
}
}
p[0] = p[3] = 0;
p[1] = 0x14;
p[2] = 0xAA;
lba = toc.end;
if (msftime) {
msf_t MSF;
LBA2MSF(lba+150, &MSF);
p[4] = 0;
p[5] = MSF.m;
p[6] = MSF.s;
p[7] = MSF.f;
} else {
p[4] = (lba >> 24) & 0xff;
p[5] = (lba >> 16) & 0xff;
p[6] = (lba >> 8) & 0xff;
p[7] = lba & 0xff;
}
tocsize += 8;
break;
case 1:
hdd_debugf("TOC format 1");
tocsize = 12;
memset(sector_buffer, 0, tocsize);
sector_buffer[2] = sector_buffer[3] = 1; // first/last session numbers
sector_buffer[5] = (1 << 4) | (toc.tracks[0].type == SECTOR_AUDIO ? 0 : 4);
sector_buffer[6] = 1;
lba = toc.tracks[0].start;
if (msftime) {
msf_t MSF;
LBA2MSF(lba+150, &MSF);
sector_buffer[9] = MSF.m;
sector_buffer[10] = MSF.s;
sector_buffer[11] = MSF.f;
} else {
sector_buffer[8] = (lba >> 24) & 0xff;
sector_buffer[9] = (lba >> 16) & 0xff;
sector_buffer[10] = (lba >> 8) & 0xff;
sector_buffer[11] = lba & 0xff;
}
break;
default:
cdrom_setsense(SENSEKEY_ILLEGAL_REQUEST, 0x26, 1);
cdrom_send_error(unit);
return;
}
sector_buffer[0] = ((tocsize-2)>>8) & 0xff;
sector_buffer[1] = (tocsize-2) & 0xff;
cdrom_ok();
bufsize = MIN(bufsize, tocsize);
//hexdump(sector_buffer, bufsize, 0);
WriteStatus(IDE_STATUS_RDY | IDE_STATUS_PKT); // pio in (class 1) command type
WritePacket(unit, sector_buffer, bufsize, bytelimit, 1);
}
static void PKT_ModeSelect(unsigned char unit, unsigned short bytelimit, char sel10)
{
bytelimit = MIN(bytelimit, SECTOR_BUFFER_SIZE);
WriteTaskFile(0, 0, 0, bytelimit & 0xff, (bytelimit>>8) & 0xff, 0xa0 | ((unit & 0x01)<<4));
WriteStatus(IDE_STATUS_REQ | IDE_STATUS_PKT | IDE_STATUS_IRQ); // wait for parameter list
unsigned long to = GetTimer(100);
while (!(GetFPGAStatus() & CMD_IDEDAT)) { // wait for full write buffer
if (CheckTimer(to)) {
cdrom_setsense(SENSEKEY_NOT_READY, 0x04, 0);
cdrom_send_error(unit);
return;
}
}
EnableFpga();
SPI(CMD_IDE_DATA_RD); // read parameter list
SPI(0x00);
SPI(0x00);
SPI(0x00);
SPI(0x00);
SPI(0x00);
for (int i=0; i<bytelimit; i++) {
sector_buffer[i] = SPI(0xFF);
}
DisableFpga();
//hexdump(sector_buffer, bytelimit, 0);
unsigned short dlen = sel10 ? (sector_buffer[1] | (sector_buffer[0] << 8)) : sector_buffer[0];
unsigned short blen = sel10 ? (sector_buffer[7] | (sector_buffer[6] << 8)) : sector_buffer[3];
unsigned char *pBuffer = sel10 ? sector_buffer + 8 : sector_buffer + 4;
if ((sel10 && bytelimit < 8) || (!sel10 && bytelimit < 4)) {
cdrom_setsense(SENSEKEY_ILLEGAL_REQUEST, 0x1A, 0);
cdrom_send_error(unit);
return;
}
bytelimit -= sel10 ? 8 : 4;
// parse block descriptor(s)
// disabled, as ATAPI compliance doesn't use block descriptors (MMC-3 B.2.2)
#if 0
while (blen >= 8) {
if (bytelimit < 8) {
cdrom_setsense(SENSEKEY_ILLEGAL_REQUEST, 0x1A, 0);
cdrom_send_error(unit);
return;
}
cdrom.blocksize = pBuffer[7] | (pBuffer[6] << 8);
iprintf("CDROM blocksize changed to: %d\n", cdrom.blocksize);
pBuffer+=8;
blen-=8;
bytelimit-=8;
}
#endif
// TODO: parse page descriptors
cdrom_ok();
WriteTaskFile(0, 0x03, 0, 0, 0, 0xa0 | ((unit & 0x01)<<4));
WriteStatus(IDE_STATUS_END | IDE_STATUS_IRQ);
}
static void PKT_ModeSelect10(unsigned char *cmd, unsigned char unit, unsigned short bytelimit)
{
hdd_debugf("IDE%d: PKT_ModeSelect10 (bytelimit=%d)", unit, bytelimit);
unsigned short bufsize = MIN(bytelimit, (cmd[7] << 8) | cmd[8]);
PKT_ModeSelect(unit, bufsize, 1);
}
static void PKT_ModeSelect6(unsigned char *cmd, unsigned char unit, unsigned short bytelimit)
{
hdd_debugf("IDE%d: PKT_ModeSelect6 (bytelimit=%d)", unit, bytelimit);
unsigned short bufsize = MIN(bytelimit, cmd[4]);
PKT_ModeSelect(unit, bufsize, 0);
}
static void PKT_ModeSense(unsigned char *cmd, unsigned char unit, unsigned short bytelimit, unsigned short bufsize, unsigned char page)
{
//TODO: implement
cdrom_setsense(SENSEKEY_ILLEGAL_REQUEST, 0x20, 0);
cdrom_send_error(unit);
}
static void PKT_ModeSense10(unsigned char *cmd, unsigned char unit, unsigned short bytelimit)
{
hdd_debugf("IDE%d: PKT_ModeSense10 (bytelimit=%d)", unit, bytelimit);
unsigned short bufsize = (cmd[7] << 8) | cmd[8];
unsigned char page = cmd[2] & 0x3f;
PKT_ModeSense(cmd, unit, bytelimit, bufsize, page);
}
static void PKT_ModeSense6(unsigned char *cmd, unsigned char unit, unsigned short bytelimit)
{
hdd_debugf("IDE%d: PKT_ModeSense6 (bytelimit=%d)", unit, bytelimit);
unsigned short bufsize = cmd[4];
unsigned char page = cmd[2] & 0x3f;
PKT_ModeSense(cmd, unit, bytelimit, bufsize, page);
}
static void PKT_Inquiry(unsigned char *cmd, unsigned char unit, unsigned short bytelimit)
{
hdd_debugf("IDE%d: PKT_Inquiry (bytelimit=%d)", unit, bytelimit);
unsigned short bufsize = (cmd[3] << 8) | cmd[4];
INQUIRYDATA_t *inq = (INQUIRYDATA_t*)§or_buffer;
memset(sector_buffer, 0, 36);
inq->DeviceType = 0x05; // MMC3
inq->RemovableMedia = 1; // removable
inq->Versions = 0x02;
inq->ResponseDataFormat = 0x02;
memcpy(inq->VendorId, "MiST ", 8);
memcpy(inq->ProductId, "CDROM ", 16);
bufsize = MIN(bufsize, 36);
cdrom_ok();
WriteStatus(IDE_STATUS_RDY | IDE_STATUS_PKT); // pio in (class 1) command type
WritePacket(unit, sector_buffer, bufsize, bytelimit, 1);
}
static void PKT_TestUnitReady(unsigned char *cmd, unsigned char unit)
{
hdd_debugf("IDE%d: PKT_TestUnitReady", unit);
if (toc.valid) {
cdrom_ok();
WriteTaskFile(0, 0x03, 0, 0, 0, 0xa0 | ((unit & 0x01)<<4));
WriteStatus(IDE_STATUS_END | IDE_STATUS_IRQ);
} else {
cdrom_setsense(SENSEKEY_NOT_READY, 0x3a, 0);
cdrom_send_error(unit);
}
}
static void PKT_RequestSense(unsigned char *cmd, unsigned char unit, unsigned short bytelimit)
{
hdd_debugf("IDE%d: PKT_RequestSense (bytelimit=%d)", unit, bytelimit);
unsigned short bufsize = MIN(cmd[4] ? cmd[4] : 4, 16);
SENSEDATA_t *sense = (SENSEDATA_t*)§or_buffer;
memset(sector_buffer, 0, 16);
sense->ErrorCode = 0x70;
sense->Valid = 1;
sense->SenseKey = cdrom.key;
sense->AdditionalSenseCode = cdrom.asc;
sense->AdditionalSenseCodeQualifier = cdrom.ascq;
WriteStatus(IDE_STATUS_RDY | IDE_STATUS_PKT); // pio in (class 1) command type
WritePacket(unit, sector_buffer, bufsize, bytelimit, 1);
}
static void PKT_StartStopUnit(unsigned char *cmd, unsigned char unit)
{
hdd_debugf("IDE%d: PKT_StartStopUnit", unit);
char start = cmd[4] & 0x01;
cdrom.audiostatus = AUDIO_NOSTAT;
if ((start && toc.valid) || !start) {
cdrom_ok();
WriteTaskFile(0, 0x03, 0, 0, 0, 0xa0 | ((unit & 0x01)<<4));
WriteStatus(IDE_STATUS_END | IDE_STATUS_IRQ);
} else {