-
Notifications
You must be signed in to change notification settings - Fork 87
/
ide.c
2146 lines (1974 loc) · 84.5 KB
/
ide.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
#include "cpuapi.h" // Needed for IDE DMA
#include "devices.h"
#include "drive.h"
#include "platform.h"
#include <string.h>
// A mostly-complete ATA implementation.
// This version works around a bug in the Bochs BIOS. It does not check if BSY is set after a write command has been executed.
#define IDE_LOG(x, ...) LOG("IDE", x, ##__VA_ARGS__)
#define IDE_FATAL(x, ...) \
FATAL("IDE", x, ##__VA_ARGS__);
// Status bits: Seagate Manual Page 21
#define ATA_STATUS_BSY 0x80 // Busy
#define ATA_STATUS_DRDY 0x40 // Drive ready
#define ATA_STATUS_DF 0x20 // Drive write fault
#define ATA_STATUS_DSC 0x10 // Drive seek complete
#define ATA_STATUS_DRQ 0x08 // Data request ready
#define ATA_STATUS_CORR 0x04 // Corrected data
#define ATA_STATUS_IDX 0x02 // Index
#define ATA_STATUS_ERR 0x01
#define ATA_ERROR_BBK 0x80 // Bad sector
#define ATA_ERROR_UNC 0x40 // Uncorrectable data
#define ATA_ERROR_MC 0x20 // No media
#define ATA_ERROR_IDNF 0x10 // ID mark not found
#define ATA_ERROR_MCR 0x08 // No media
#define ATA_ERROR_ABRT 0x04 // Command aborted
#define ATA_ERROR_TK0NF 0x02 // Track 0 not found
#define ATA_ERROR_AMNF 0x01 // No address mark
// https://www.bswd.com/sff8020i.pdf
#define ATAPI_INTERRUPT_REASON_REL 0x04 // Always 0
#define ATAPI_INTERRUPT_REASON_IO 0x02 // 1 if transferring data out
#define ATAPI_INTERRUPT_REASON_CoD 0x01 // 1 if command bytes, 0 if data bytes
#define ATAPI_SENSE_NONE 0x00
#define ATAPI_SENSE_NOT_READY 0x02
#define ATAPI_SENSE_MEDIUM_ERROR 0x03
#define ATAPI_SENSE_HARDWARE_ERROR 0x04
#define ATAPI_SENSE_ILLEGAL_REQUEST 0x05
#define ATAPI_SENSE_UNIT_ATTENTION 0x06
#define ATAPI_SENSE_ABORTED 0x0B
// Upper 8 bits represent ASC, lower 8 bits rperesent ASCQ
#define ATAPI_ASC_CAUSE_NOT_REPORTABLE 0x0400
#define ATAPI_ASC_GETTING_READY 0x0401
#define ATAPI_ASC_MANUAL_INTERVENTION 0x0403
#define ATAPI_ASC_BSY 0x0407 // Operation in progress
#define ATAPI_ASC_OFFLINE 0x0412
#define ATAPI_ASC_MAINTENANCE 0x0481
#define ATAPI_ASC_OUT_OF_RANGE 0x2000 // Also illegal opcode
#define ATAPI_ASC_CLEANING_CARTRIDGE 0x3003
#define ATAPI_ASC_NOT_PRESENT 0x3A02 // Also set if mailslot is open
#define ATAPI_ASC_INVALID_FIELD 0x2400
#define ATAPI_ASC_INVALID_OFFSET 0x2100
#define ATAPI_ERROR_ABRT 0x04 // Command aborted
#define ATAPI_ERROR_EOM 0x02 // Command specific
#define ATAPI_ERROR_ILI 0x01 // Command specific
#define DISABLE_MULTIPLE_SECTORS
#ifdef DISABLE_MULTIPLE_SECTORS
#define MAX_MULTIPLE_SECTORS 1
#else
#define MAX_MULTIPLE_SECTORS 16 // According to QEMU and Bochs
#endif
// We keep all fields inside one big struct to make it easy for the autogen savestate
static struct ide_controller {
// <<< BEGIN STRUCT "struct" >>>
/// ignore: canary_below, canary_above
// The ID of the currently selected drive for master (0) or slave (1)
int selected;
// Is LBA enabled for this controller?
int lba;
// The number of chunks to be transferred over every single ide_read/write_sector is called.
// Note that it should be less than or equal to MAX_MULTIPLE_SECTORS
int sectors_read;
// Whether the current command requires lba48
int lba48;
// Set during the set multiple mode command.
int multiple_sectors_count;
// Various IDE registers. Note "sector number" refers to the position of sector while "sector count" refers to the number of sectors
uint8_t error, feature, drive_and_head;
// This is important for LBA48
uint16_t sector_number, cylinder_low, cylinder_high, sector_count;
uint8_t device_control, status;
// The last command issued to the controller. Used to handle callbacks when the PIO buffer needs to be updated.
uint8_t command_issued;
// PIO buffer
uint32_t pio_position, pio_length;
uint32_t canary_below;
union {
uint8_t pio_buffer[16 * 512]; // MAX_MULTIPLE_SECTORS * 512
// These are for aligned accesses
uint16_t pio_buffer16[16 * 512 / 2];
uint32_t pio_buffer32[16 * 512 / 4];
};
uint32_t canary_above;
int irq_status;
// === The following registers are per-drive ===
// Type of each drive attached to controller (DRIVE_TYPE_*)
int type[2];
// Drives can be attached to a controller, but they may or may not have media inserted.
int media_inserted[2];
// Disk geometry information
// If an OS wants a different translation mode (specified by the "Initialize Drive Parameters" command), then the value corresponding to the drive will be set to 1.
int translated[2];
// Sectors per track -- master non-translated, master translated, slave non-translated, slave translated
uint32_t sectors_per_track[4];
// Same as above, but total number of heads
uint32_t heads[4];
// Same as above, but total number of heads
uint32_t cylinders[4];
// The total number of sectors accessible by CHS addressing
uint32_t total_sectors_chs[2];
// Total number of sectors on disk
uint32_t total_sectors[2];
// === The following registers are for PCI IDE ===
uint8_t dma_command,
dma_status;
uint32_t prdt_address;
int dma_enabled;
// Multiword DMA, Ultimate DMA support (only used for IDENTIFY commands)
uint16_t mdma, udma;
// === The following registers are for ATAPI ===
uint8_t sense_key;
uint16_t asc;
uint32_t atapi_lba;
uint32_t atapi_sectors_to_read, atapi_sector_size;
uint32_t
// Total number of bytes to send per ATAPI read
atapi_bytes_to_transfer,
// Total number of bytes to send before we raise an IRQ
atapi_cylinder_count,
// Total number of bytes we've sent in this frame
atapi_frame_bytes_to_transfer,
atapi_frame_bytes_transferred,
atapi_total_bytes_transferred;
uint8_t atapi_command;
uint8_t atapi_can_eject_cdrom;
uint8_t atapi_dma_enabled;
// <<< END STRUCT "struct" >>>
struct drive_info* info[2];
} ide[2];
static void ide_state(void)
{
// <<< BEGIN AUTOGENERATE "state" >>>
struct bjson_object* obj = state_obj("ide[NUMBER]", (46) * 2);
state_field(obj, 4, "ide[0].selected", &ide[0].selected);
state_field(obj, 4, "ide[1].selected", &ide[1].selected);
state_field(obj, 4, "ide[0].lba", &ide[0].lba);
state_field(obj, 4, "ide[1].lba", &ide[1].lba);
state_field(obj, 4, "ide[0].sectors_read", &ide[0].sectors_read);
state_field(obj, 4, "ide[1].sectors_read", &ide[1].sectors_read);
state_field(obj, 4, "ide[0].lba48", &ide[0].lba48);
state_field(obj, 4, "ide[1].lba48", &ide[1].lba48);
state_field(obj, 4, "ide[0].multiple_sectors_count", &ide[0].multiple_sectors_count);
state_field(obj, 4, "ide[1].multiple_sectors_count", &ide[1].multiple_sectors_count);
state_field(obj, 1, "ide[0].error", &ide[0].error);
state_field(obj, 1, "ide[1].error", &ide[1].error);
state_field(obj, 1, "ide[0].feature", &ide[0].feature);
state_field(obj, 1, "ide[1].feature", &ide[1].feature);
state_field(obj, 1, "ide[0].drive_and_head", &ide[0].drive_and_head);
state_field(obj, 1, "ide[1].drive_and_head", &ide[1].drive_and_head);
state_field(obj, 2, "ide[0].sector_number", &ide[0].sector_number);
state_field(obj, 2, "ide[1].sector_number", &ide[1].sector_number);
state_field(obj, 2, "ide[0].cylinder_low", &ide[0].cylinder_low);
state_field(obj, 2, "ide[1].cylinder_low", &ide[1].cylinder_low);
state_field(obj, 2, "ide[0].cylinder_high", &ide[0].cylinder_high);
state_field(obj, 2, "ide[1].cylinder_high", &ide[1].cylinder_high);
state_field(obj, 2, "ide[0].sector_count", &ide[0].sector_count);
state_field(obj, 2, "ide[1].sector_count", &ide[1].sector_count);
state_field(obj, 1, "ide[0].device_control", &ide[0].device_control);
state_field(obj, 1, "ide[1].device_control", &ide[1].device_control);
state_field(obj, 1, "ide[0].status", &ide[0].status);
state_field(obj, 1, "ide[1].status", &ide[1].status);
state_field(obj, 1, "ide[0].command_issued", &ide[0].command_issued);
state_field(obj, 1, "ide[1].command_issued", &ide[1].command_issued);
state_field(obj, 4, "ide[0].pio_position", &ide[0].pio_position);
state_field(obj, 4, "ide[1].pio_position", &ide[1].pio_position);
state_field(obj, 4, "ide[0].pio_length", &ide[0].pio_length);
state_field(obj, 4, "ide[1].pio_length", &ide[1].pio_length);
state_field(obj, 8192, "ide[0].pio_buffer", &ide[0].pio_buffer);
state_field(obj, 8192, "ide[1].pio_buffer", &ide[1].pio_buffer);
state_field(obj, 4, "ide[0].irq_status", &ide[0].irq_status);
state_field(obj, 4, "ide[1].irq_status", &ide[1].irq_status);
state_field(obj, 8, "ide[0].type", &ide[0].type);
state_field(obj, 8, "ide[1].type", &ide[1].type);
state_field(obj, 8, "ide[0].media_inserted", &ide[0].media_inserted);
state_field(obj, 8, "ide[1].media_inserted", &ide[1].media_inserted);
state_field(obj, 8, "ide[0].translated", &ide[0].translated);
state_field(obj, 8, "ide[1].translated", &ide[1].translated);
state_field(obj, 16, "ide[0].sectors_per_track", &ide[0].sectors_per_track);
state_field(obj, 16, "ide[1].sectors_per_track", &ide[1].sectors_per_track);
state_field(obj, 16, "ide[0].heads", &ide[0].heads);
state_field(obj, 16, "ide[1].heads", &ide[1].heads);
state_field(obj, 16, "ide[0].cylinders", &ide[0].cylinders);
state_field(obj, 16, "ide[1].cylinders", &ide[1].cylinders);
state_field(obj, 8, "ide[0].total_sectors_chs", &ide[0].total_sectors_chs);
state_field(obj, 8, "ide[1].total_sectors_chs", &ide[1].total_sectors_chs);
state_field(obj, 8, "ide[0].total_sectors", &ide[0].total_sectors);
state_field(obj, 8, "ide[1].total_sectors", &ide[1].total_sectors);
state_field(obj, 1, "ide[0].dma_command", &ide[0].dma_command);
state_field(obj, 1, "ide[1].dma_command", &ide[1].dma_command);
state_field(obj, 1, "ide[0].dma_status", &ide[0].dma_status);
state_field(obj, 1, "ide[1].dma_status", &ide[1].dma_status);
state_field(obj, 4, "ide[0].prdt_address", &ide[0].prdt_address);
state_field(obj, 4, "ide[1].prdt_address", &ide[1].prdt_address);
state_field(obj, 4, "ide[0].dma_enabled", &ide[0].dma_enabled);
state_field(obj, 4, "ide[1].dma_enabled", &ide[1].dma_enabled);
state_field(obj, 2, "ide[0].mdma", &ide[0].mdma);
state_field(obj, 2, "ide[1].mdma", &ide[1].mdma);
state_field(obj, 2, "ide[0].udma", &ide[0].udma);
state_field(obj, 2, "ide[1].udma", &ide[1].udma);
state_field(obj, 1, "ide[0].sense_key", &ide[0].sense_key);
state_field(obj, 1, "ide[1].sense_key", &ide[1].sense_key);
state_field(obj, 2, "ide[0].asc", &ide[0].asc);
state_field(obj, 2, "ide[1].asc", &ide[1].asc);
state_field(obj, 4, "ide[0].atapi_lba", &ide[0].atapi_lba);
state_field(obj, 4, "ide[1].atapi_lba", &ide[1].atapi_lba);
state_field(obj, 4, "ide[0].atapi_sectors_to_read", &ide[0].atapi_sectors_to_read);
state_field(obj, 4, "ide[1].atapi_sectors_to_read", &ide[1].atapi_sectors_to_read);
state_field(obj, 4, "ide[0].atapi_sector_size", &ide[0].atapi_sector_size);
state_field(obj, 4, "ide[1].atapi_sector_size", &ide[1].atapi_sector_size);
state_field(obj, 4, "ide[0].atapi_bytes_to_transfer", &ide[0].atapi_bytes_to_transfer);
state_field(obj, 4, "ide[1].atapi_bytes_to_transfer", &ide[1].atapi_bytes_to_transfer);
state_field(obj, 4, "ide[0].atapi_cylinder_count", &ide[0].atapi_cylinder_count);
state_field(obj, 4, "ide[1].atapi_cylinder_count", &ide[1].atapi_cylinder_count);
state_field(obj, 4, "ide[0].atapi_frame_bytes_to_transfer", &ide[0].atapi_frame_bytes_to_transfer);
state_field(obj, 4, "ide[1].atapi_frame_bytes_to_transfer", &ide[1].atapi_frame_bytes_to_transfer);
state_field(obj, 4, "ide[0].atapi_frame_bytes_transferred", &ide[0].atapi_frame_bytes_transferred);
state_field(obj, 4, "ide[1].atapi_frame_bytes_transferred", &ide[1].atapi_frame_bytes_transferred);
state_field(obj, 4, "ide[0].atapi_total_bytes_transferred", &ide[0].atapi_total_bytes_transferred);
state_field(obj, 4, "ide[1].atapi_total_bytes_transferred", &ide[1].atapi_total_bytes_transferred);
state_field(obj, 1, "ide[0].atapi_command", &ide[0].atapi_command);
state_field(obj, 1, "ide[1].atapi_command", &ide[1].atapi_command);
state_field(obj, 1, "ide[0].atapi_can_eject_cdrom", &ide[0].atapi_can_eject_cdrom);
state_field(obj, 1, "ide[1].atapi_can_eject_cdrom", &ide[1].atapi_can_eject_cdrom);
state_field(obj, 1, "ide[0].atapi_dma_enabled", &ide[0].atapi_dma_enabled);
state_field(obj, 1, "ide[1].atapi_dma_enabled", &ide[1].atapi_dma_enabled);
// <<< END AUTOGENERATE "state" >>>
char filename[1000];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
struct drive_info* info = ide[i].info[j];
if (ide[i].media_inserted[j]) {
sprintf(filename, "ide%d-%d", i, j);
drive_state(info, filename);
}
}
}
}
#define SELECTED(obj, field) obj->field[obj->selected]
#define TRANSLATED(obj, field) obj->field[(obj->selected << 1) | (SELECTED(obj, translated))]
// Returns 0 for master, 1 for slave
static inline int get_ctrl_id(struct ide_controller* ctrl)
{
return ctrl == &ide[1];
}
static inline int selected_drive_has_media(struct ide_controller* ctrl)
{
return SELECTED(ctrl, type) != DRIVE_TYPE_NONE;
}
static inline int controller_has_media(struct ide_controller* ctrl)
{
return ctrl->media_inserted[0] | ctrl->media_inserted[1];
}
static void ide_update_irq(struct ide_controller* ctrl)
{
if (ctrl->irq_status && !(ctrl->device_control & 2))
pic_raise_irq(get_ctrl_id(ctrl) | 14);
else
pic_lower_irq(get_ctrl_id(ctrl) | 14);
}
static inline void ide_lower_irq(struct ide_controller* ctrl)
{
ctrl->irq_status = 0;
ide_update_irq(ctrl);
}
static inline void ide_raise_irq(struct ide_controller* ctrl)
{
ctrl->dma_status |= 0x04;
ctrl->irq_status = 1;
ide_update_irq(ctrl);
}
// Indicates that the command has been aborted for one reason or another.
static void ide_abort_command(struct ide_controller* ctrl)
{
ctrl->status = ATA_STATUS_DRDY | ATA_STATUS_DSC | ATA_STATUS_ERR;
ctrl->error = ATA_ERROR_ABRT;
ctrl->pio_position = 0;
ctrl->dma_status |= 2; // Failed
ide_raise_irq(ctrl);
}
// Reset the IDE. Simply resets the selected drives and removes translations
static void ide_reset(void)
{
// Make both drives select thir master drive
ide[0].selected = 0;
ide[1].selected = 0;
// Remove all translation modes
ide[0].translated[0] = 0;
ide[0].translated[1] = 0;
ide[1].translated[0] = 0;
ide[1].translated[1] = 0;
}
// Get the number of sectors specified by the sector_count register. Special case for zero
static uint32_t ide_get_sector_count(struct ide_controller* ctrl, int lba48)
{
if (lba48)
return (!ctrl->sector_count) << 16 | ctrl->sector_count;
else {
uint8_t real_sector_count = ctrl->sector_count;
return (!real_sector_count) << 8 | real_sector_count;
}
}
// Get the sector offset (i.e. place to seek in file divided by 512)
static uint64_t ide_get_sector_offset(struct ide_controller* ctrl, int lba48)
{
uint64_t res;
switch ((lba48 << 1 | ctrl->lba) & 3) {
case 0: { // CHS
uint64_t cyl = (ctrl->cylinder_low & 0xFF) | ((ctrl->cylinder_high << 8) & 0xFFFF);
res = cyl * TRANSLATED(ctrl, heads) * TRANSLATED(ctrl, sectors_per_track);
uint64_t heads = ctrl->drive_and_head & 0x0F;
res += heads * TRANSLATED(ctrl, sectors_per_track);
res += (ctrl->sector_number & 0xFF) - 1;
break;
}
case 1: // LBA24
res = ctrl->sector_number & 0xFF;
res |= (ctrl->cylinder_low & 0xFF) << 8;
res |= (ctrl->cylinder_high & 0xFF) << 16;
res |= (ctrl->drive_and_head & 0x0F) << 24;
break;
case 2 ... 3: { // LBA48 commands override any IDE setting you may have set.
res = ctrl->sector_number & 0xFF;
res |= (uint64_t)(ctrl->cylinder_low & 0xFF) << 8L;
res |= (uint64_t)(ctrl->cylinder_high & 0xFF) << 16L;
res |= (uint64_t)(ctrl->sector_count >> 8 & 0xFF) << 24L;
res |= (uint64_t)(ctrl->cylinder_low >> 8 & 0xFF) << 32L;
res |= (uint64_t)(ctrl->cylinder_high >> 8 & 0xFF) << 40L;
break;
}
}
return res;
}
static void ide_set_sector_offset(struct ide_controller* ctrl, int lba48, uint64_t position)
{
switch (lba48 << 1 | ctrl->lba) {
case 0: { // CHS
uint32_t heads_spt = TRANSLATED(ctrl, heads) * TRANSLATED(ctrl, sectors_per_track);
uint32_t c = position / heads_spt;
uint32_t t = position % heads_spt;
uint32_t h = t / TRANSLATED(ctrl, sectors_per_track);
uint32_t s = t % TRANSLATED(ctrl, sectors_per_track);
ctrl->cylinder_low = c & 0xFF;
ctrl->cylinder_high = c >> 8 & 0xFF;
ctrl->drive_and_head = (ctrl->drive_and_head & 0xF0) | (h & 0x0F);
ctrl->sector_number = s + 1;
break;
}
case 1: // LBA24
ctrl->drive_and_head = (ctrl->drive_and_head & 0xF0) | (position >> 24 & 0x0F);
ctrl->cylinder_high = position >> 16 & 0xFF;
ctrl->cylinder_low = position >> 8 & 0xFF;
ctrl->sector_number = position >> 0 & 0xFF;
break;
case 2:
case 3: { // LBA48
// TODO: Make this more efficient
ctrl->sector_number = position & 0xFF;
ctrl->cylinder_low = position >> 8 & 0xFF;
ctrl->cylinder_high = position >> 16 & 0xFF;
ctrl->sector_number |= (position >> 24 & 0xFF) << 8;
ctrl->cylinder_low |= (position >> 32 & 0xFF) << 8;
ctrl->cylinder_high |= (position >> 40 & 0xFF) << 8;
break;
}
}
}
static void ide_check_canary(struct ide_controller* ctrl)
{
if (ctrl->canary_above != 0xDEADBEEF || ctrl->canary_below != 0xBEEFDEAD) {
fprintf(stderr, "IDE PIO smashing canaries overwritten\n");
IDE_FATAL("bad");
}
}
// PIO buffer utilities. Useful for commands like IDENTIFY
static void ide_pio_store_byte(struct ide_controller* ctrl, int offset, uint8_t value)
{
ctrl->pio_buffer[offset] = value;
}
static void ide_pio_store_word(struct ide_controller* ctrl, int offset, uint16_t value)
{
ctrl->pio_buffer16[offset >> 1] = value;
}
static void ide_pio_store_word_be(struct ide_controller* ctrl, int offset, uint16_t value)
{
ctrl->pio_buffer[offset] = value >> 8;
ctrl->pio_buffer[offset + 1] = value;
}
static void ide_pio_store_dword_be(struct ide_controller* ctrl, int offset, uint32_t value)
{
ctrl->pio_buffer[offset] = value >> 24;
ctrl->pio_buffer[offset + 1] = value >> 16;
ctrl->pio_buffer[offset + 2] = value >> 8;
ctrl->pio_buffer[offset + 3] = value;
}
static void ide_pio_clear(struct ide_controller* ctrl, int offset, int length)
{
for (int i = offset; i < (offset + length); i++)
ctrl->pio_buffer[i] = 0;
}
// Store a string in the IDE PIO buffer.
// Right justified strings (justify_left=0): " HELLO WORLD"
// Left justified strings (justify_left=1): "HELLO WORLD "
// Swapped strings: "HELLO " --> "EHLL O"
static void ide_pio_store_string(struct ide_controller* ctrl, char* string, int pos, int length, int swap, int justify_left)
{
char* buffer = alloca(length + 1); // Account for null-terminator
// Justify the string.
if (justify_left) {
sprintf(buffer, "%-*s", length, string);
} else {
sprintf(buffer, "%*s", length, string);
}
for (int i = 0; i < length; i++) {
ide_pio_store_byte(ctrl, pos + (i ^ swap), buffer[i]);
}
}
// Simple utility functions to read data in big endian format
static inline uint16_t read16be(void* x)
{
uint8_t* buf = x;
return buf[0] << 8 | buf[1];
}
static inline uint32_t read32be(void* x)
{
uint8_t* buf = x;
return buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
}
// These functions mess around with the interrupt status register.
// This function indicates that there is a transfer pending
static void ide_atapi_init_transfer(struct ide_controller* ctrl)
{
ctrl->sector_count = (ctrl->sector_count & 0xF8) | ATAPI_INTERRUPT_REASON_IO;
ctrl->status |= ATA_STATUS_DRQ;
}
// This function indicates that it's ready to send command bytes
static void ide_atapi_init_command(struct ide_controller* ctrl)
{
ctrl->sector_count = (ctrl->sector_count & 0xF8) | ATAPI_INTERRUPT_REASON_CoD;
ctrl->status |= ATA_STATUS_DRQ;
}
// This function indicatesCPU that there is no transfer pending
static void ide_atapi_no_transfer(struct ide_controller* ctrl)
{
ctrl->sector_count = (ctrl->sector_count & 0xF8) | ATAPI_INTERRUPT_REASON_IO | ATAPI_INTERRUPT_REASON_CoD;
ctrl->status &= ~ATA_STATUS_DRQ;
}
// Abort an IDE command and mess around with the sense keys and "additional sense codes"
static void ide_atapi_abort(struct ide_controller* ctrl, int sense_key, int asc)
{
IDE_LOG("ATAPI abort!!\n");
ctrl->error = sense_key << 4;
ctrl->status = ATA_STATUS_DRDY | ATA_STATUS_ERR;
ide_atapi_no_transfer(ctrl);
ctrl->sense_key = sense_key;
ctrl->asc = asc;
}
static void ide_atapi_start_transfer(struct ide_controller* ctrl, int size)
{
ctrl->pio_position = 0;
// Set byte count high/low
ctrl->cylinder_low = size;
ctrl->cylinder_high = size >> 8;
ctrl->pio_length = size;
ide_atapi_init_transfer(ctrl);
ctrl->status = ATA_STATUS_DRDY | ATA_STATUS_DSC | ATA_STATUS_DRQ;
if (ctrl->atapi_dma_enabled)
IDE_FATAL("todo: dma trans\n");
ide_raise_irq(ctrl);
}
static void ide_atapi_stop_command(struct ide_controller* ctrl)
{
ctrl->pio_position = 0;
ctrl->pio_length = 0;
ide_atapi_no_transfer(ctrl);
ctrl->status = ATA_STATUS_DRDY;
}
static void ide_atapi_read_complete(void* thisptr, int x)
{
struct ide_controller* ctrl = thisptr;
if (x == -1) {
ide_atapi_abort(ctrl, ATAPI_SENSE_ILLEGAL_REQUEST, 0); // ?
IDE_FATAL("ATAPI Read error todo\n");
}
ctrl->status &= ~ATA_STATUS_BSY;
ide_atapi_init_transfer(ctrl);
ctrl->status |= ATA_STATUS_DSC | ATA_STATUS_DRDY;
ctrl->atapi_sectors_to_read--;
ctrl->atapi_lba++;
ctrl->cylinder_low = ctrl->atapi_bytes_to_transfer & 0xFF;
ctrl->cylinder_high = ctrl->atapi_bytes_to_transfer >> 8 & 0xFF;
//ctrl->pio_length = ctrl->atapi_sector_size;
IDE_LOG("ATAPI: finished reading left=%d\n", ctrl->atapi_sectors_to_read);
ide_raise_irq(ctrl);
}
// Read 1 sector of CD-ROM
// The number of bytes in ctrl->cylinder_[low|high] is how many sectors we transfer before we refill.
// The number of sectors in ctrl->sectors_to_read is how many sectors we have to read from the disk, in total.
static void ide_atapi_read(struct ide_controller* ctrl)
{
IDE_LOG(" atapi read sector=%d\n", ctrl->atapi_lba);
// XXX -- make sure that ctrl->atapi_lba * ctrl->atapi_sector_size can be over 0xFFFFFFFF
int res = drive_read(SELECTED(ctrl, info), ctrl, ctrl->pio_buffer, ctrl->atapi_sector_size, ctrl->atapi_lba * ctrl->atapi_sector_size, ide_atapi_read_complete);
// We have already prefetched this data
if (res != DRIVE_RESULT_SYNC) {
printf(" == Internal IDE inconsistency == ");
printf("Fetch offset: %08x [blk%08x.bin]\n", ctrl->atapi_lba * ctrl->atapi_sector_size, (ctrl->atapi_lba * ctrl->atapi_sector_size) / (256 << 10));
printf("Fetch bytes: %d\n", ctrl->atapi_sector_size);
IDE_FATAL("Error trying to fetch already-fetched ATAPI data\n");
}
ide_atapi_init_transfer(ctrl);
ctrl->status |= ATA_STATUS_DSC | ATA_STATUS_DRDY;
ctrl->atapi_sectors_to_read--;
ctrl->atapi_lba++;
// Determine bytes to transfer
uint32_t total_bytes = ctrl->atapi_cylinder_count;
if (total_bytes > ctrl->atapi_bytes_to_transfer)
total_bytes = ctrl->atapi_bytes_to_transfer;
ctrl->atapi_frame_bytes_to_transfer = total_bytes;
ctrl->atapi_frame_bytes_transferred = 0;
ctrl->cylinder_low = total_bytes & 0xFF;
ctrl->cylinder_high = total_bytes >> 8 & 0xFF;
ctrl->pio_position = 0;
ctrl->pio_length = total_bytes > ctrl->atapi_sector_size ? ctrl->atapi_sector_size : total_bytes;
//ctrl->pio_length = ctrl->atapi_sector_size;
IDE_LOG("ATAPI: finished reading\n");
ide_raise_irq(ctrl);
}
static void ide_atapi_read_cb(void* thisptr, int stat)
{
struct ide_controller* ctrl = thisptr;
if (stat == -1) {
IDE_FATAL("ATAPI: failed to read sector\n");
}
ctrl->status &= ~ATA_STATUS_BSY;
ide_atapi_read(ctrl);
}
// Run an ATAPI command
static void ide_atapi_run_command(struct ide_controller* ctrl)
{
// Copy all 12 bytes to a safe place
uint8_t command[12];
for (int i = 0; i < 12; i++)
command[i] = ctrl->pio_buffer[i];
ctrl->atapi_command = command[0];
int dont_xor = -1;
IDE_LOG("Command issued: %02x\n", command[0]);
switch (command[0]) {
case 0x00: // Test if ready
IDE_LOG("Command: ATAPI: Test if ready\n");
if (SELECTED(ctrl, media_inserted)) {
ide_atapi_stop_command(ctrl);
} else {
ide_atapi_abort(ctrl, ATAPI_SENSE_NOT_READY, ATAPI_ASC_NOT_PRESENT);
}
ide_raise_irq(ctrl);
break;
case 0x03: // Request Sense
IDE_LOG("Command: ATAPI Request Sense\n");
ide_pio_clear(ctrl, 0, 18);
ide_pio_store_byte(ctrl, 0, 0xF0);
ide_pio_store_byte(ctrl, 2, ctrl->sense_key);
ide_pio_store_byte(ctrl, 7, 10);
ide_pio_store_byte(ctrl, 12, ctrl->asc >> 8); // ASC
ide_pio_store_byte(ctrl, 13, ctrl->asc); // ASCQ
if (ctrl->sense_key == 6)
ctrl->sense_key = 0;
ide_atapi_start_transfer(ctrl, command[4] > 18 ? 18 : command[4]);
break;
case 0x12: // Inquiry
IDE_LOG("Command: ATAPI: Inquiry\n");
ide_pio_store_byte(ctrl, 0, 0x05); // CD-ROM drive
ide_pio_store_byte(ctrl, 1, 0x80); // Removable
ide_pio_store_byte(ctrl, 2, 0x00); // Version
ide_pio_store_byte(ctrl, 3, 0x21); // Version
ide_pio_store_byte(ctrl, 4, 0x1F); // Extra data length
ide_pio_store_byte(ctrl, 5, 0x00); // ?
ide_pio_store_byte(ctrl, 6, 0x00); // ?
ide_pio_store_byte(ctrl, 7, 0x00); // ?
ide_pio_store_string(ctrl, "Halfix", 8, 8, 0, 1);
ide_pio_store_string(ctrl, "Halfix CD-ROM", 16, 16, 0, 1);
ide_pio_store_string(ctrl, "1.0", 24, 4, 0, 1);
ide_atapi_start_transfer(ctrl, command[4] > 36 ? 36 : command[4]);
break;
case 0x1E: // Lock CD-ROM doors
IDE_LOG("Command: ATAPI: %sock Doors\n", ~command[4] & 1 ? "Unl" : "L");
if (SELECTED(ctrl, media_inserted)) {
ctrl->atapi_can_eject_cdrom = ~command[4] & 1;
ide_atapi_stop_command(ctrl); // everything is done
} else {
ide_atapi_abort(ctrl, ATAPI_SENSE_NOT_READY, ATAPI_ASC_NOT_PRESENT);
}
ide_raise_irq(ctrl);
break;
case 0x25: // Get media capacity
IDE_LOG("Command: ATAPI: Get Capacity\n");
if (SELECTED(ctrl, media_inserted)) {
ide_pio_store_dword_be(ctrl, 0, SELECTED(ctrl, total_sectors) - 1);
ide_pio_store_dword_be(ctrl, 4, 2048);
ide_atapi_start_transfer(ctrl, 8);
} else {
ide_atapi_abort(ctrl, ATAPI_SENSE_NOT_READY, ATAPI_ASC_NOT_PRESENT);
ide_raise_irq(ctrl);
}
break;
case 0x43: {
IDE_LOG("Command: ATAPI: Read table of contents\n");
// Read table of contents. Based on values observed from Bochs and QEMU
// https://www.bswd.com/sff8020i.pdf starting page 183
int length = read16be(command + 7), nlength,
format = command[9] >> 6,
track_start = command[6],
bufpos, sectors;
ide_pio_clear(ctrl, 0, length);
switch (format) {
case 0: // Read TOC data format
ide_pio_store_byte(ctrl, 2, 1);
ide_pio_store_byte(ctrl, 3, 1);
bufpos = 4;
if (track_start < 2) {
ide_pio_store_byte(ctrl, bufpos++, 0);
ide_pio_store_byte(ctrl, bufpos++, 0x14);
ide_pio_store_byte(ctrl, bufpos++, 0x01);
ide_pio_store_byte(ctrl, bufpos++, 0);
ide_pio_store_byte(ctrl, bufpos++, 0);
ide_pio_store_byte(ctrl, bufpos++, 0);
ide_pio_store_byte(ctrl, bufpos++, command[1] & 2);
ide_pio_store_byte(ctrl, bufpos++, 0);
}
ide_pio_store_byte(ctrl, bufpos++, 0);
ide_pio_store_byte(ctrl, bufpos++, 0x16);
ide_pio_store_byte(ctrl, bufpos++, 0xAA);
ide_pio_store_byte(ctrl, bufpos++, 0);
sectors = SELECTED(ctrl, total_sectors);
if (command[1] & 2) {
ide_pio_store_byte(ctrl, bufpos++, 0);
ide_pio_store_byte(ctrl, bufpos++, ((sectors + 150) / 75) / 60);
ide_pio_store_byte(ctrl, bufpos++, ((sectors + 150) / 75) % 60);
ide_pio_store_byte(ctrl, bufpos++, ((sectors + 150) % 75));
} else {
ide_pio_store_dword_be(ctrl, bufpos, sectors);
bufpos += 4;
}
ide_pio_store_word_be(ctrl, 0, bufpos - 2);
nlength = bufpos;
break;
case 1: // Multi-session
nlength = 12;
ide_pio_store_word_be(ctrl, 0, 0x0A); // TOC data length
ide_pio_store_byte(ctrl, 2, 1); // First session
ide_pio_store_byte(ctrl, 3, 1); // Last session
break;
case 2: // Raw TOC data
ide_pio_store_byte(ctrl, 2, 1);
ide_pio_store_byte(ctrl, 3, 1);
bufpos = 4;
for (int i = 0; i < 4; i++) {
ide_pio_store_byte(ctrl, bufpos++, 0x01);
ide_pio_store_byte(ctrl, bufpos++, 0x14);
ide_pio_store_byte(ctrl, bufpos++, 0);
if (i == 3)
ide_pio_store_byte(ctrl, bufpos++, 0xA3);
else
ide_pio_store_byte(ctrl, bufpos++, i);
ide_pio_store_byte(ctrl, bufpos++, 0);
ide_pio_store_byte(ctrl, bufpos++, 0);
ide_pio_store_byte(ctrl, bufpos++, 0);
if (i & 2) {
sectors = SELECTED(ctrl, total_sectors);
if (command[1] & 2) {
ide_pio_store_byte(ctrl, bufpos++, 0);
ide_pio_store_byte(ctrl, bufpos++, ((sectors + 150) / 75) / 60);
ide_pio_store_byte(ctrl, bufpos++, ((sectors + 150) / 75) % 60);
ide_pio_store_byte(ctrl, bufpos++, ((sectors + 150) % 75));
} else {
ide_pio_store_dword_be(ctrl, bufpos, sectors);
bufpos += 4;
}
} else {
ide_pio_store_byte(ctrl, bufpos++, 0);
ide_pio_store_byte(ctrl, bufpos++, 1);
ide_pio_store_byte(ctrl, bufpos++, 0);
ide_pio_store_byte(ctrl, bufpos++, 0);
}
}
ide_pio_store_word_be(ctrl, 0, bufpos - 2);
nlength = bufpos;
break;
case 3:
IDE_FATAL("Unknown toc command 3\n");
}
IDE_LOG("nlength=%d length=%d\n", nlength, length);
//ide_atapi_start_transfer(ctrl, nlength < length ? nlength : length);
UNUSED(length);
ide_atapi_start_transfer(ctrl, nlength);
ide_raise_irq(ctrl);
break;
}
case 0x1B:
ide_atapi_no_transfer(ctrl);
ide_raise_irq(ctrl);
break;
case 0x1A:
case 0x5A: { // Mode sense
int length, nlength;
if (command[0] & 0x40)
length = read16be(command + 6);
else
length = command[4];
IDE_LOG("ATAPI: Mode Sense [len=%d x=%d]\n", length, command[2]);
switch (command[2]) {
// Current values
case 1: // Error recovery
ide_pio_clear(ctrl, 0, nlength = 16);
ide_pio_store_word_be(ctrl, 0, 22);
ide_pio_store_byte(ctrl, 2, 0x70);
ide_pio_store_byte(ctrl, 8, 0x01);
ide_pio_store_byte(ctrl, 9, 0x06);
ide_pio_store_byte(ctrl, 11, 0x05); // Retry five times
break;
case 0x2A: // Capabilities
case (2 << 6) | 0x2A:
ide_pio_clear(ctrl, 0, nlength = 28);
ide_pio_store_word_be(ctrl, 0, 34);
ide_pio_store_byte(ctrl, 2, 0x70);
ide_pio_store_byte(ctrl, 8, 0x2A);
ide_pio_store_byte(ctrl, 9, 0x12);
ide_pio_store_byte(ctrl, 12, 0x70);
ide_pio_store_byte(ctrl, 13, 0x60);
ide_pio_store_byte(ctrl, 14, 41 | 0); // TODO: Locked bit (bit 2)
ide_pio_store_word_be(ctrl, 16, 706);
ide_pio_store_word_be(ctrl, 18, 2);
ide_pio_store_word_be(ctrl, 20, 512);
ide_pio_store_word_be(ctrl, 22, 706);
break;
default:
IDE_LOG("ATAPI: Unknown Mode Sense: %02x\n", command[2]);
ide_atapi_abort(ctrl, ATAPI_SENSE_ILLEGAL_REQUEST, ATAPI_ASC_INVALID_FIELD);
ide_raise_irq(ctrl);
return;
}
ide_atapi_start_transfer(ctrl, nlength < length ? nlength : length);
break;
}
case 0x28: // Read sectors
case 0xA8: { // Read sectors
uint32_t sectors, lba, total_sectors, bytecount;
if (command[0] & 0x80)
sectors = read32be(command + 6);
else
sectors = read16be(command + 7);
lba = read32be(command + 2);
IDE_LOG("ATAPI: Read %d sector starting %d ending %d\n", sectors, lba, lba + sectors);
if (SELECTED(ctrl, media_inserted)) {
total_sectors = SELECTED(ctrl, total_sectors);
int tmp;
if ((lba + sectors) >= total_sectors) {
tmp = total_sectors - lba + 1;
if (tmp < 0) {
// LBA is out of range
ide_atapi_abort(ctrl, ATAPI_SENSE_NOT_READY, ATAPI_ASC_OUT_OF_RANGE);
ide_raise_irq(ctrl);
break;
} else if (tmp == 0) {
// LBA is in range
ide_atapi_stop_command(ctrl);
ide_raise_irq(ctrl);
break;
} else
sectors = tmp - 1;
}
if (sectors == 0) {
ide_atapi_stop_command(ctrl);
break;
}
ctrl->atapi_lba = lba;
ctrl->atapi_sectors_to_read = sectors;
ctrl->atapi_sector_size = 2048;
// Total number of bytes to transfer
ctrl->atapi_cylinder_count = (ctrl->cylinder_high << 8 & 0xFF00) | (ctrl->cylinder_low & 0xFF);
ctrl->atapi_bytes_to_transfer = bytecount = ctrl->atapi_sector_size * ctrl->atapi_sectors_to_read;
ctrl->atapi_total_bytes_transferred = 0;
// Reset cylinder low/high values
ctrl->cylinder_low = 0;
ctrl->cylinder_high = 0;
// Bytecount must be even
if (ctrl->atapi_cylinder_count & 1)
ctrl->atapi_cylinder_count--;
//if(command[4] == 5 && command[5] == 0x1D && command[8] == 0x3A) __asm__("int3");
// Prefetch all the data beforehand
IDE_LOG("Prefetch: %d start=%08x end=%08x\n", ctrl->atapi_cylinder_count, ctrl->atapi_lba * ctrl->atapi_sector_size, ctrl->atapi_lba * ctrl->atapi_sector_size + ctrl->atapi_bytes_to_transfer);
//fprintf(stderr, "cylinder bytes=%d [0x%x] offs=%08x [blk%08x.bin] real bytes=0x%x\n", ctrl->atapi_cylinder_count, ctrl->atapi_cylinder_count, ctrl->atapi_lba * ctrl->atapi_sector_size, (ctrl->atapi_lba * ctrl->atapi_sector_size) / (256 * 1024), ctrl->atapi_bytes_to_transfer);
int res = drive_prefetch(SELECTED(ctrl, info), ctrl, ctrl->atapi_bytes_to_transfer, ctrl->atapi_lba * ctrl->atapi_sector_size, ide_atapi_read_cb);
if (res == DRIVE_RESULT_ASYNC) {
ctrl->status |= ATA_STATUS_BSY | ATA_STATUS_DRDY | ATA_STATUS_DSC;
} else if (res == DRIVE_RESULT_SYNC) {
ide_atapi_read_cb(ctrl, 0);
} else {
ide_atapi_abort(ctrl, ATAPI_SENSE_NOT_READY, ATAPI_ASC_NOT_PRESENT);
ide_raise_irq(ctrl);
}
dont_xor = 0;
} else {
ide_atapi_abort(ctrl, ATAPI_SENSE_NOT_READY, ATAPI_ASC_NOT_PRESENT);
ide_raise_irq(ctrl);
}
break;
}
case 0x2B: // Seek
IDE_LOG("ATAPI: Seek\n");
if (SELECTED(ctrl, media_inserted)) {
uint32_t lba = read32be(command + 2);
if (lba >= SELECTED(ctrl, total_sectors)) {
ide_atapi_abort(ctrl, ATAPI_SENSE_ILLEGAL_REQUEST, ATAPI_ASC_INVALID_OFFSET);
ide_raise_irq(ctrl);
break;
}
ide_atapi_stop_command(ctrl);
ide_raise_irq(ctrl);
} else {
ide_atapi_abort(ctrl, ATAPI_SENSE_NOT_READY, ATAPI_ASC_NOT_PRESENT);
ide_raise_irq(ctrl);
}
break;
case 0x42: // Read sub-channel
IDE_LOG("ATAPI: Read Sub-Channel (stubbed)\n");
if (SELECTED(ctrl, media_inserted)) {
int length = command[8] < 8 ? command[8] : 8;
ide_pio_clear(ctrl, 0, length);
ide_atapi_start_transfer(ctrl, length);
} else {
ide_atapi_abort(ctrl, ATAPI_SENSE_NOT_READY, ATAPI_ASC_NOT_PRESENT);
ide_raise_irq(ctrl);
}
break;
case 0x51: // Read disk information
IDE_LOG("ATAPI: Read disk information (stubbed)\n");
ide_atapi_abort(ctrl, ATAPI_SENSE_ILLEGAL_REQUEST, 36);
break;
case 0xBD: { // Mechanism status
IDE_LOG("ATAPI: Mechanism status\n");
int x = read16be(command + 8);
ide_pio_clear(ctrl, 0, 8);
ide_pio_store_byte(ctrl, 5, 1);
ide_atapi_start_transfer(ctrl, x > 8 ? 8 : x);
break;
}
case 0xBE: // Read CD (todo)
IDE_LOG("ATAPI: Read CD (unimplemented)\n");
if (SELECTED(ctrl, media_inserted)) {
uint32_t length = read32be(command + 5) & 0x00FFFFFF,
lba = read32be(command + 2);
if (length == 0) {
ide_atapi_stop_command(ctrl);
break;
}
UNUSED(lba);
IDE_FATAL("TODO: ATAPI Read CD command\n");
} else {
ide_atapi_abort(ctrl, ATAPI_SENSE_NOT_READY, ATAPI_ASC_NOT_PRESENT);
ide_raise_irq(ctrl);
}
break;
case 0x46:
case 0x8D:
case 0x55:
case 0xa6:
case 0x4b:
case 0x45:
case 0x47:
case 0xbc:
case 0xb9:
case 0x44:
case 0xba:
case 0xbb:
case 0x4e:
case 0x4a:
IDE_LOG("ATAPI: Unknown command '%02x'\n", command[0]);
ide_atapi_abort(ctrl, 5, ATAPI_ASC_OUT_OF_RANGE);
ide_raise_irq(ctrl);
break;
default:
DRIVE_FATAL("Unknown ATAPI command: %02x\n", command[0]);
}
if (!dont_xor)
return;
int bit = ATAPI_INTERRUPT_REASON_IO & dont_xor;
if (!(ctrl->status & ATA_STATUS_BSY)) {
ide_raise_irq(ctrl);
if (ctrl->pio_length == 0) {
bit |= ATAPI_INTERRUPT_REASON_CoD & dont_xor;
ctrl->status &= ~ATA_STATUS_DRQ;
}
}
ctrl->sector_count &= 0xF8;
ctrl->sector_count |= bit;
}
// We need these functions to restart the command
static void ide_read_sectors(struct ide_controller* ctrl, int lba48, int chunk_count);
static void drive_write_callback(void* this, int res);
// After the PIO buffer is emptied, this function is called so that the drive knows what to do with the data