3 * Copyright (C) 2002 MandrakeSoft S.A.
8 * http://www.linux-mandrake.com/
9 * http://www.mandrakesoft.com/
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * Major modifications made for the V3VEE project
27 * The V3VEE Project is a joint project between Northwestern University
28 * and the University of New Mexico. You can find out more at
29 * http://www.v3vee.org
31 * Copyright (c) 2008, Zheng Cui <cuizheng@cs.unm.edu>
32 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
33 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
34 * All rights reserved for original changes
39 #include <devices/ramdisk.h>
40 #include <palacios/vmm.h>
41 #include <devices/cdrom.h>
42 #include <devices/ide.h>
47 #define PrintDebug(_f, _a...)
52 * Data type definitions
55 #define INDEX_PULSE_CYCLE 10
60 #define INTR_REASON_BIT_ERR 0x01
61 #define UNABLE_FIND_TAT_CHANNEL_ERR 0x02
63 #define READ_BUF_GT_512 0x04
67 #define PRI_DATA_PORT 0x1f0
68 #define PRI_FEATURES_PORT 0x1f1
69 #define PRI_SECT_CNT_PORT 0x1f2
70 #define PRI_SECT_ADDR1_PORT 0x1f3
71 #define PRI_SECT_ADDR2_PORT 0x1f4
72 #define PRI_SECT_ADDR3_PORT 0x1f5
73 #define PRI_DRV_SEL_PORT 0x1f6
74 #define PRI_CMD_PORT 0x1f7
75 #define PRI_CTRL_PORT 0x3f6
76 #define PRI_ADDR_REG_PORT 0x3f7
78 #define SEC_DATA_PORT 0x170
79 #define SEC_FEATURES_PORT 0x171
80 #define SEC_SECT_CNT_PORT 0x172
81 #define SEC_SECT_ADDR1_PORT 0x173
82 #define SEC_SECT_ADDR2_PORT 0x174
83 #define SEC_SECT_ADDR3_PORT 0x175
84 #define SEC_DRV_SEL_PORT 0x176
85 #define SEC_CMD_PORT 0x177
86 #define SEC_CTRL_PORT 0x376
87 #define SEC_ADDR_REG_PORT 0x377
93 #define PACKET_SIZE 12
98 // Open a image. Returns non-negative if successful.
99 //int open (const char* pathname);
101 // Open an image with specific flags. Returns non-negative if successful.
102 int rd_open (const char* pathname, int flags);
107 // Position ourselves. Return the resulting offset from the
108 // beginning of the file.
109 off_t rd_lseek (off_t offset, int whence);
111 // Read count bytes to the buffer buf. Return the number of
112 // bytes read (count).
113 ssize_t rd_read (void* buf, size_t count);
115 // Write count bytes from buf. Return the number of bytes
117 ssize_t rd_write (const void* buf, size_t count);
127 #define ATA_DETECT 0xf0 //0X3E8
128 #define ATA_RESET 0xf1 //0X3E9
129 #define ATA_CMD_DATA_IN 0xf2 //0X3EA
130 #define ATA_CMD_DATA_OUT 0xf3 //0X3EB
131 #define ATA_CMD_PACKET 0xf4 //0X3EC
132 #define ATAPI_GET_SENSE 0xf5 //0X3ED
133 #define ATAPI_IS_READY 0xf6 //0X3EE
134 #define ATAPI_IS_CDROM 0xf7 //0X3EF
136 #define CDEMU_INIT 0xf8 //0X2E8
137 #define CDEMU_ISACTIVE 0xf9 //0X2E9
138 #define CDEMU_EMULATED_DRIVE 0xfa //0X2EA
139 #define CDROM_BOOT 0xfb //0X2EB
142 #define HARD_DRIVE_POST 0xfc //0X2EC
145 #define ATA_DEVICE_NO 0xfd //0X2ED
146 #define ATA_DEVICE_TYPE 0xfe //0X2ED
148 #define INT13_HARDDISK 0xff //0x2ef
149 #define INT13_CDROM 0xe0 //0x2f8
150 #define INT13_CDEMU 0xe1 //0x2f9
151 #define INT13_ELTORITO 0xe2 //0x2fa
152 #define INT13_DISKETTE_FUNCTION 0xe3 //0x2fb
157 static const char cdrom_str[] = "CD-ROM";
158 static const char harddisk_str[] = "HARDDISK";
159 static const char none_str[] = "NONE";
162 static inline const char * device_type_to_str(device_type_t type) {
176 static inline void write_features(struct channel_t * channel, uchar_t value) {
177 channel->drives[0].controller.features = value;
178 channel->drives[1].controller.features = value;
182 static inline void write_sector_count(struct channel_t * channel, uchar_t value) {
183 channel->drives[0].controller.sector_count = value;
184 channel->drives[1].controller.sector_count = value;
187 static inline void write_sector_number(struct channel_t * channel, uchar_t value) {
188 channel->drives[0].controller.sector_no = value;
189 channel->drives[1].controller.sector_no = value;
193 static inline void write_cylinder_low(struct channel_t * channel, uchar_t value) {
194 channel->drives[0].controller.cylinder_no &= 0xff00;
195 channel->drives[0].controller.cylinder_no |= value;
196 channel->drives[1].controller.cylinder_no &= 0xff00;
197 channel->drives[1].controller.cylinder_no |= value;
200 static inline void write_cylinder_high(struct channel_t * channel, uchar_t value) {
201 ushort_t val2 = value;
203 channel->drives[0].controller.cylinder_no &= 0x00ff;
204 channel->drives[0].controller.cylinder_no |= (val2 & 0xff00);
206 channel->drives[1].controller.cylinder_no &= 0x00ff;
207 channel->drives[1].controller.cylinder_no |= (val2 & 0xff00);
210 static inline void write_head_no(struct channel_t * channel, uchar_t value) {
211 channel->drives[0].controller.head_no = value;
212 channel->drives[1].controller.head_no = value;
215 static inline void write_lba_mode(struct channel_t * channel, uchar_t value) {
216 channel->drives[0].controller.lba_mode = value;
217 channel->drives[1].controller.lba_mode = value;
221 static inline uint_t get_channel_no(struct ramdisk_t * ramdisk, struct channel_t * channel) {
222 return (((uchar_t *)channel - (uchar_t *)(ramdisk->channels)) / sizeof(struct channel_t));
225 static inline uint_t get_drive_no(struct channel_t * channel, struct drive_t * drive) {
226 return (((uchar_t *)drive - (uchar_t*)(channel->drives)) / sizeof(struct drive_t));
229 static inline struct drive_t * get_selected_drive(struct channel_t * channel) {
230 return &(channel->drives[channel->drive_select]);
234 static inline int is_primary_port(struct ramdisk_t * ramdisk, ushort_t port) {
238 case PRI_FEATURES_PORT:
239 case PRI_SECT_CNT_PORT:
240 case PRI_SECT_ADDR1_PORT:
241 case PRI_SECT_ADDR2_PORT:
242 case PRI_SECT_ADDR3_PORT:
243 case PRI_DRV_SEL_PORT:
254 static inline int is_secondary_port(struct ramdisk_t * ramdisk, ushort_t port) {
258 case SEC_FEATURES_PORT:
259 case SEC_SECT_CNT_PORT:
260 case SEC_SECT_ADDR1_PORT:
261 case SEC_SECT_ADDR2_PORT:
262 case SEC_SECT_ADDR3_PORT:
263 case SEC_DRV_SEL_PORT:
272 static inline int num_drives_on_channel(struct channel_t * channel) {
273 if ((channel->drives[0].device_type == IDE_NONE) &&
274 (channel->drives[1].device_type == IDE_NONE)) {
276 } else if ((channel->drives[0].device_type != IDE_NONE) &&
277 (channel->drives[1].device_type != IDE_NONE)) {
286 static inline uchar_t extract_bits(uchar_t * buf, uint_t buf_offset, uint_t bit_offset, uint_t num_bits) {
287 uchar_t val = buf[buf_offset];
288 val = val >> bit_offset;
289 val &= ((1 << num_bits) -1);
294 static inline uchar_t get_packet_field(struct channel_t * channel, uint_t packet_offset, uint_t bit_offset, uint_t num_bits) {
295 struct drive_t * drive = get_selected_drive(channel);
296 return extract_bits(drive->controller.buffer, packet_offset, bit_offset, num_bits);
300 static inline uchar_t get_packet_byte(struct channel_t * channel, uint_t offset) {
301 struct drive_t * drive = get_selected_drive(channel);
302 return drive->controller.buffer[offset];
305 static inline uint16_t get_packet_word(struct channel_t * channel, uint_t offset) {
306 struct drive_t * drive = get_selected_drive(channel);
307 uint16_t val = drive->controller.buffer[offset];
309 val |= drive->controller.buffer[offset + 1];
314 static inline uint16_t rd_read_16bit(const uint8_t* buf) {
315 return (buf[0] << 8) | buf[1];
320 static inline uint32_t rd_read_32bit(const uint8_t* buf) {
321 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
324 ////////////////////////////////////////////////////////////////////////////
332 static void rd_init_mode_sense_single(struct vm_device * dev, struct channel_t * channel, const void * src, int size);
334 static void rd_command_aborted(struct vm_device * dev, struct channel_t * channel, unsigned value);
339 static int handle_atapi_packet_command(struct vm_device * dev,
340 struct channel_t * channel,
343 static int rd_init_send_atapi_command(struct vm_device * dev,
344 struct channel_t * channel,
345 Bit8u command, int req_length,
346 int alloc_length, bool lazy);
348 static void rd_ready_to_send_atapi(struct vm_device * dev,
349 struct channel_t * channel);
351 static void rd_atapi_cmd_error(struct vm_device * dev,
352 struct channel_t * channel,
353 sense_t sense_key, asc_t asc);
355 static void rd_atapi_cmd_nop(struct vm_device * dev, struct channel_t * channel);
356 static void rd_identify_ATAPI_drive(struct vm_device * dev, struct channel_t * channel);
363 static void rd_raise_interrupt(struct vm_device * dev, struct channel_t * channel);
364 static void rd_lower_irq(struct vm_device *dev, Bit32u irq);
375 static void rd_print_state(struct ramdisk_t *ramdisk);
376 static int check_bit_fields(struct controller_t * controller);
378 ////////////////////////////////////////////////////////////////////
384 static Bit32u rd_init_hardware(struct ramdisk_t *ramdisk) {
387 struct channel_t *channels = (struct channel_t *)(&(ramdisk->channels));
389 PrintDebug("[rd_init_harddrive]\n");
391 for (channel_num = 0; channel_num < MAX_ATA_CHANNEL; channel_num++) {
392 memset((char *)(channels + channel_num), 0, sizeof(struct channel_t));
395 for (channel_num = 0; channel_num < MAX_ATA_CHANNEL; channel_num++){
396 struct channel_t * channel = &(channels[channel_num]);
398 channel->ioaddr1 = 0x0;
399 channel->ioaddr2 = 0x0;
402 for (device = 0; device < 2; device++){
403 struct drive_t * drive = &(channel->drives[device]);
404 struct controller_t * controller = &(drive->controller);
406 controller->status.busy = 0;
407 controller->status.drive_ready = 1;
408 controller->status.write_fault = 0;
409 controller->status.seek_complete = 1;
410 controller->status.drq = 0;
411 controller->status.corrected_data = 0;
412 controller->status.index_pulse = 0;
413 controller->status.index_pulse_count = 0;
414 controller->status.err = 0;
416 controller->error_register = 0x01; // diagnostic code: no error
417 controller->head_no = 0;
418 controller->sector_count = 1;
419 controller->sector_no = 1;
420 controller->cylinder_no = 0;
421 controller->current_command = 0x00;
422 controller->buffer_index = 0;
424 controller->control.reset = 0;
425 controller->control.disable_irq = 0;
426 controller->reset_in_progress = 0;
428 controller->sectors_per_block = 0x80;
429 controller->lba_mode = 0;
432 controller->features = 0;
435 drive->device_type = IDE_NONE;
438 strncpy((char*)(drive->model_no), "", 40);
439 while(strlen((char *)(drive->model_no)) < 40) {
440 strcat ((char*)(drive->model_no), " ");
446 if (channel_num == 1) {
448 channel->ioaddr1 = 0x170;
449 channel->ioaddr2 = 0x370;
451 channel->drive_select = 0;
455 strncpy((char*)(drive->model_no), "V3VEE Ramdisk", 40);
456 while (strlen((char *)(drive->model_no)) < 40) {
457 strcat ((char*)(drive->model_no), " ");
460 PrintDebug("CDROM on target %d/%d\n", channel_num, device);
462 drive->device_type = IDE_CDROM;
463 drive->cdrom.locked = 0;
464 drive->sense.sense_key = SENSE_NONE;
465 drive->sense.asc = 0;
466 drive->sense.ascq = 0;
469 if (check_bit_fields(controller) == INTR_REASON_BIT_ERR) {
470 PrintDebug("interrupt reason: bit field error\n");
471 return INTR_REASON_BIT_ERR;
475 controller->sector_count = 0;
477 // allocate low level driver
478 drive->cdrom.cd = (struct cdrom_interface *)V3_Malloc(sizeof(struct cdrom_interface));
479 PrintDebug("cd = %x\n", drive->cdrom.cd);
480 V3_ASSERT(drive->cdrom.cd != NULL);
482 struct cdrom_interface * cdif = drive->cdrom.cd;
483 memset(cdif, 0, sizeof(struct cdrom_interface));
485 cdif->ops.init(cdif);
487 PrintDebug("\t\tCD on ata%d-%d: '%s'\n",
491 if((drive->cdrom.cd->ops).insert_cdrom(cdif, NULL)) {
492 PrintDebug("\t\tMedia present in CD-ROM drive\n");
493 drive->cdrom.ready = 1;
494 drive->cdrom.capacity = drive->cdrom.cd->ops.capacity(cdif);
496 PrintDebug("\t\tCould not locate CD-ROM, continuing with media not present\n");
497 drive->cdrom.ready = 0;
506 rd_print_state(ramdisk);
513 static void rd_reset_harddrive(struct ramdisk_t *ramdisk, unsigned type) {
518 static void rd_close_harddrive(struct ramdisk_t *ramdisk) {
523 ////////////////////////////////////////////////////////////////////
527 static int read_data_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
528 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
529 struct channel_t * channel = NULL;
530 struct drive_t * drive = NULL;
531 struct controller_t * controller = NULL;
535 if (is_primary_port(ramdisk, port)) {
536 channel = &(ramdisk->channels[0]);
537 } else if (is_secondary_port(ramdisk, port)) {
538 channel = &(ramdisk->channels[1]);
540 PrintError("Invalid Port: %d\n", port);
544 drive = get_selected_drive(channel);
545 controller = &(drive->controller);
548 PrintDebug("[read_data_handler] IO Read at 0x%x, on drive %d/%d (current_cmd = 0x%02x)\n",
550 get_channel_no(ramdisk, channel),
551 get_drive_no(channel, drive),
552 controller->current_command);
554 switch (controller->current_command) {
555 case 0xec: // IDENTIFY DEVICE
560 controller->status.busy = 0;
561 controller->status.drive_ready = 1;
562 controller->status.write_fault = 0;
563 controller->status.seek_complete = 1;
564 controller->status.corrected_data = 0;
565 controller->status.err = 0;
568 value32 = controller->buffer[index];
572 value32 |= (controller->buffer[index] << 8);
577 value32 |= (controller->buffer[index] << 16);
578 value32 |= (controller->buffer[index+1] << 24);
582 controller->buffer_index = index;
585 memcpy(dst, controller->buffer + controller->buffer_index, length);
586 controller->buffer_index += length;
588 if (controller->buffer_index >= 512) {
589 controller->status.drq = 0;
594 case 0xa0: //send packet cmd
596 uint_t index = controller->buffer_index;
599 PrintDebug("\t\tatapi.command(%02x), index(%d), cdrom.remaining_blocks(%d)\n",
600 drive->atapi.command,
602 drive->cdrom.remaining_blocks);
604 // Load block if necessary
608 PrintError("\t\tindex > 2048 : 0x%x\n", index);
612 switch (drive->atapi.command) {
613 case 0x28: // read (10)
614 case 0xa8: // read (12)
616 struct cdrom_interface * cdif = drive->cdrom.cd;
618 if (!(drive->cdrom.ready)) {
619 PrintError("\t\tRead with CDROM not ready\n");
623 drive->cdrom.cd->ops.read_block(cdif, controller->buffer,
624 drive->cdrom.next_lba);
625 drive->cdrom.next_lba++;
626 drive->cdrom.remaining_blocks--;
629 if (!(drive->cdrom.remaining_blocks)) {
630 PrintDebug("\t\tLast READ block loaded {CDROM}\n");
632 PrintDebug("\t\tREAD block loaded (%d remaining) {CDROM}\n",
633 drive->cdrom.remaining_blocks);
636 // one block transfered, start at beginning
640 default: // no need to load a new block
648 value32 = controller->buffer[index + increment];
652 value32 |= (controller->buffer[index + increment] << 8);
657 value32 |= (controller->buffer[index + increment] << 16);
658 value32 |= (controller->buffer[index + increment + 1] << 24);
662 controller->buffer_index = index + increment;
663 controller->drq_index += increment;
666 /* JRL: CHECK THAT there is enough data in the buffer to copy.... */
668 memcpy(dst, controller->buffer + index, length);
670 controller->buffer_index = index + length;
671 controller->drq_index += length;
676 if (controller->drq_index >= (unsigned)drive->atapi.drq_bytes) {
677 controller->status.drq = 0;
678 controller->drq_index = 0;
680 drive->atapi.total_bytes_remaining -= drive->atapi.drq_bytes;
682 if (drive->atapi.total_bytes_remaining > 0) {
683 // one or more blocks remaining (works only for single block commands)
685 PrintDebug("\t\tPACKET drq bytes read\n");
686 controller->interrupt_reason.i_o = 1;
687 controller->status.busy = 0;
688 controller->status.drq = 1;
689 controller->interrupt_reason.c_d = 0;
691 // set new byte count if last block
692 if (drive->atapi.total_bytes_remaining < controller->byte_count) {
693 controller->byte_count = drive->atapi.total_bytes_remaining;
695 drive->atapi.drq_bytes = controller->byte_count;
697 rd_raise_interrupt(dev, channel);
700 PrintDebug("\t\tPACKET all bytes read\n");
702 controller->interrupt_reason.i_o = 1;
703 controller->interrupt_reason.c_d = 1;
704 controller->status.drive_ready = 1;
705 controller->interrupt_reason.rel = 0;
706 controller->status.busy = 0;
707 controller->status.drq = 0;
708 controller->status.err = 0;
710 rd_raise_interrupt(dev, channel);
718 PrintDebug("\t\tread need support more command: %02x\n", controller->current_command);
728 static int write_data_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
729 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
730 struct channel_t * channel = NULL;
731 struct drive_t * drive = NULL;
732 struct controller_t * controller = NULL;
734 if (is_primary_port(ramdisk, port)) {
735 channel = &(ramdisk->channels[0]);
736 } else if (is_secondary_port(ramdisk, port)) {
737 channel = &(ramdisk->channels[1]);
739 PrintError("Invalid Port: %d\n", port);
743 drive = get_selected_drive(channel);
744 controller = &(drive->controller);
746 PrintDebug("[write_data_handler] IO write at 0x%x, current_cmd = 0x%02x\n",
747 port, controller->current_command);
749 switch (controller->current_command) {
750 case 0x30: // WRITE SECTORS
751 PrintError("\t\tneed to implement 0x30(write sector) to port 0x%x\n", port);
756 if (handle_atapi_packet_command(dev, channel, *(ushort_t *)src) == -1) {
763 PrintError("\t\tIO write(0x%x): current command is %02xh\n",
764 port, controller->current_command);
776 static int read_status_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
777 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
778 struct channel_t * channel = NULL;
779 struct drive_t * drive = NULL;
780 struct controller_t * controller = NULL;
784 PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
788 if (is_primary_port(ramdisk, port)) {
789 channel = &(ramdisk->channels[0]);
790 } else if (is_secondary_port(ramdisk, port)) {
791 channel = &(ramdisk->channels[1]);
793 PrintError("Invalid Port: %d\n", port);
797 drive = get_selected_drive(channel);
798 controller = &(drive->controller);
800 PrintDebug("[read_status_handler] IO read at 0x%x, on drive %d/%d\n",
801 port, get_channel_no(ramdisk, channel),
802 channel->drive_select);
805 if (num_drives_on_channel(channel) == 0) {
806 // (mch) Just return zero for these registers
807 memset(dst, 0, length);
811 (controller->status.busy << 7) |
812 (controller->status.drive_ready << 6) |
813 (controller->status.write_fault << 5) |
814 (controller->status.seek_complete << 4) |
815 (controller->status.drq << 3) |
816 (controller->status.corrected_data << 2) |
817 (controller->status.index_pulse << 1) |
818 (controller->status.err) );
820 memcpy(dst, &val, length);
822 controller->status.index_pulse_count++;
823 controller->status.index_pulse = 0;
825 if (controller->status.index_pulse_count >= INDEX_PULSE_CYCLE) {
826 controller->status.index_pulse = 1;
827 controller->status.index_pulse_count = 0;
831 if ((port == SEC_CMD_PORT) || (port == PRI_CMD_PORT)) {
832 rd_lower_irq(dev, channel->irq);
840 static int write_cmd_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
841 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
842 struct channel_t * channel = NULL;
843 struct drive_t * drive = NULL;
844 struct controller_t * controller = NULL;
845 uchar_t value = *(uchar_t *)src;
848 PrintError("Invalid Command port write length: %d (port=%d)\n", length, port);
852 if (is_primary_port(ramdisk, port)) {
853 channel = &(ramdisk->channels[0]);
854 } else if (is_secondary_port(ramdisk, port)) {
855 channel = &(ramdisk->channels[1]);
857 PrintError("Invalid Port: %d\n", port);
861 drive = get_selected_drive(channel);
862 controller = &(drive->controller);
865 PrintDebug("[write_command_handler] IO write at 0x%x, on drive %d/%d (val = 0x%x)\n",
866 port, get_channel_no(ramdisk, channel),
867 channel->drive_select,
872 case 0xa1: // IDENTIFY PACKET DEVICE
874 if (drive->device_type == IDE_CDROM) {
875 controller->current_command = value;
876 controller->error_register = 0;
878 controller->status.busy = 0;
879 controller->status.drive_ready = 1;
880 controller->status.write_fault = 0;
881 controller->status.drq = 1;
882 controller->status.err = 0;
884 controller->status.seek_complete = 1;
885 controller->status.corrected_data = 0;
887 controller->buffer_index = 0;
888 rd_raise_interrupt(dev, channel);
889 rd_identify_ATAPI_drive(dev, channel);
891 rd_command_aborted(dev, channel, 0xa1);
895 case 0xa0: // SEND PACKET (atapi)
897 if (drive->device_type == IDE_CDROM) {
900 if (controller->features & (1 << 0)) {
901 PrintError("\t\tPACKET-DMA not supported");
905 if (controller->features & (1 << 1)) {
906 PrintError("\t\tPACKET-overlapped not supported");
910 // We're already ready!
911 controller->sector_count = 1;
912 controller->status.busy = 0;
913 controller->status.write_fault = 0;
916 controller->status.drq = 1;
917 controller->status.err = 0;
919 // NOTE: no interrupt here
920 controller->current_command = value;
921 controller->buffer_index = 0;
923 rd_command_aborted (dev, channel, 0xa0);
928 PrintError("\t\tneed translate command %2x\n", value);
936 static int write_ctrl_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
937 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
938 struct channel_t * channel = NULL;
939 struct drive_t * master_drive = NULL;
940 struct drive_t * slave_drive = NULL;
941 struct controller_t * controller = NULL;
942 uchar_t value = *(uchar_t *)src;
943 rd_bool prev_control_reset;
946 PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
950 if (is_primary_port(ramdisk, port)) {
951 channel = &(ramdisk->channels[0]);
952 } else if (is_secondary_port(ramdisk, port)) {
953 channel = &(ramdisk->channels[1]);
955 PrintError("Invalid Port: %d\n", port);
959 master_drive = &(channel->drives[0]);
960 slave_drive = &(channel->drives[1]);
962 controller = &(get_selected_drive(channel)->controller);
965 PrintDebug("[write_control_handler] IO write at 0x%x, on drive %d/%d (val = 0x%x)\n",
966 port, get_channel_no(ramdisk, channel),
967 channel->drive_select,
970 // (mch) Even if device 1 was selected, a write to this register
971 // goes to device 0 (if device 1 is absent)
973 prev_control_reset = controller->control.reset;
975 master_drive->controller.control.reset = value & 0x04;
976 slave_drive->controller.control.reset = value & 0x04;
978 // CGS: was: SELECTED_CONTROLLER(channel).control.disable_irq = value & 0x02;
979 master_drive->controller.control.disable_irq = value & 0x02;
980 slave_drive->controller.control.disable_irq = value & 0x02;
982 PrintDebug("\t\tadpater control reg: reset controller = %d\n",
983 (unsigned) (controller->control.reset) ? 1 : 0);
984 PrintDebug("\t\tadpater control reg: disable_irq(X) = %d\n",
985 (unsigned) (controller->control.disable_irq) ? 1 : 0);
987 if ((!prev_control_reset) && (controller->control.reset)) {
990 // transition from 0 to 1 causes all drives to reset
991 PrintDebug("\t\thard drive: RESET\n");
993 // (mch) Set BSY, drive not ready
994 for (id = 0; id < 2; id++) {
995 struct controller_t * ctrl = NULL;
998 ctrl = &(master_drive->controller);
999 } else if (id == 1) {
1000 ctrl = &(slave_drive->controller);
1003 ctrl->status.busy = 1;
1004 ctrl->status.drive_ready = 0;
1005 ctrl->reset_in_progress = 1;
1007 ctrl->status.write_fault = 0;
1008 ctrl->status.seek_complete = 1;
1009 ctrl->status.drq = 0;
1010 ctrl->status.corrected_data = 0;
1011 ctrl->status.err = 0;
1013 ctrl->error_register = 0x01; // diagnostic code: no error
1015 ctrl->current_command = 0x00;
1016 ctrl->buffer_index = 0;
1018 ctrl->sectors_per_block = 0x80;
1021 ctrl->control.disable_irq = 0;
1024 rd_lower_irq(dev, channel->irq);
1026 } else if ((controller->reset_in_progress) &&
1027 (!controller->control.reset)) {
1029 // Clear BSY and DRDY
1030 PrintDebug("\t\tReset complete {%s}\n", device_type_to_str(get_selected_drive(channel)->device_type));
1032 for (id = 0; id < 2; id++) {
1033 struct controller_t * ctrl = NULL;
1034 struct drive_t * drv = NULL;
1037 ctrl = &(master_drive->controller);
1039 } else if (id == 1) {
1040 ctrl = &(slave_drive->controller);
1044 ctrl->status.busy = 0;
1045 ctrl->status.drive_ready = 1;
1046 ctrl->reset_in_progress = 0;
1049 if (drv->device_type == IDE_DISK) {
1050 PrintDebug("\t\tdrive %d/%d is harddrive\n", get_channel_no(ramdisk, channel), id);
1052 ctrl->sector_count = 1;
1053 ctrl->sector_no = 1;
1054 ctrl->cylinder_no = 0;
1057 ctrl->sector_count = 1;
1058 ctrl->sector_no = 1;
1059 ctrl->cylinder_no = 0xeb14;
1064 PrintDebug("\t\ts[0].controller.control.disable_irq = %02x\n",
1065 master_drive->controller.control.disable_irq);
1066 PrintDebug("\t\ts[1].controller.control.disable_irq = %02x\n",
1067 slave_drive->controller.control.disable_irq);
1072 static int read_general_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
1073 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1074 struct channel_t * channel = NULL;
1075 struct drive_t * drive = NULL;
1076 struct controller_t * controller = NULL;
1080 PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
1084 if (is_primary_port(ramdisk, port)) {
1085 channel = &(ramdisk->channels[0]);
1086 } else if (is_secondary_port(ramdisk, port)) {
1087 channel = &(ramdisk->channels[1]);
1089 PrintError("Invalid Port: %d\n", port);
1093 drive = get_selected_drive(channel);
1094 controller = &(drive->controller);
1097 PrintDebug("[read_general_handler] IO read addr at %x, on drive %d/%d, curcmd = %02x\n",
1098 port, get_channel_no(ramdisk, channel),
1099 channel->drive_select,
1100 controller->current_command);
1104 case PRI_FEATURES_PORT:
1105 case SEC_FEATURES_PORT: // hard disk error register 0x1f1
1107 uchar_t val = (drive->device_type == IDE_NONE) ? 0 : controller->error_register;
1109 controller->status.err = 0;
1111 *(uchar_t *)dst = val;
1117 case PRI_SECT_CNT_PORT:
1118 case SEC_SECT_CNT_PORT: // hard disk sector count / interrupt reason 0x1f2
1120 uchar_t val = (drive->device_type == IDE_NONE) ? 0 : controller->sector_count;
1122 *(uchar_t *)dst = val;
1127 case PRI_SECT_ADDR1_PORT:
1128 case SEC_SECT_ADDR1_PORT: // sector number 0x1f3
1130 uchar_t val = (drive->device_type == IDE_NONE) ? 0 : controller->sector_no;
1132 *(uchar_t *)dst = val;
1138 case PRI_SECT_ADDR2_PORT:
1139 case SEC_SECT_ADDR2_PORT: // cylinder low 0x1f4
1141 // -- WARNING : On real hardware the controller registers are shared between drives.
1142 // So we must respond even if the select device is not present. Some OS uses this fact
1143 // to detect the disks.... minix2 for example
1144 uchar_t val = (num_drives_on_channel(channel) == 0) ? 0 : (controller->cylinder_no & 0x00ff);
1146 *(uchar_t *)dst = val;
1152 case PRI_SECT_ADDR3_PORT:
1153 case SEC_SECT_ADDR3_PORT: // cylinder high 0x1f5
1155 // -- WARNING : On real hardware the controller registers are shared between drives.
1156 // So we must respond even if the select device is not present. Some OS uses this fact
1157 // to detect the disks.... minix2 for example
1158 uchar_t val = (num_drives_on_channel(channel) == 0) ? 0 : (controller->cylinder_no >> 8);
1160 *(uchar_t *)dst = val;
1165 case PRI_DRV_SEL_PORT:
1166 case SEC_DRV_SEL_PORT: // hard disk drive and head register 0x1f6
1168 // b7 Extended data field for ECC
1169 // b6/b5: Used to be sector size. 00=256,01=512,10=1024,11=128
1170 // Since 512 was always used, bit 6 was taken to mean LBA mode:
1171 // b6 1=LBA mode, 0=CHS mode
1175 uchar_t val = ((1 << 7) |
1176 ((controller->lba_mode > 0) << 6) |
1177 (1 << 5) | // 01b = 512 sector size
1178 (channel->drive_select << 4) |
1179 (controller->head_no << 0));
1181 *(uchar_t *)dst = val;
1186 case PRI_ADDR_REG_PORT:
1187 case SEC_ADDR_REG_PORT: // Hard Disk Address Register 0x3f7
1189 // Obsolete and unsupported register. Not driven by hard
1190 // disk controller. Report all 1's. If floppy controller
1191 // is handling this address, it will call this function
1192 // set/clear D7 (the only bit it handles), then return
1193 // the combined value
1194 *(uchar_t *)dst = 0xff;
1199 PrintError("Invalid Port: %d\n", port);
1207 static int write_general_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
1208 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1209 struct channel_t * channel = NULL;
1210 struct drive_t * drive = NULL;
1211 struct controller_t * controller = NULL;
1212 uchar_t value = *(uchar_t *)src;
1215 PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
1219 if (is_primary_port(ramdisk, port)) {
1220 channel = &(ramdisk->channels[0]);
1221 } else if (is_secondary_port(ramdisk, port)) {
1222 channel = &(ramdisk->channels[1]);
1224 PrintError("Invalid Port: %d\n", port);
1228 drive = get_selected_drive(channel);
1229 controller = &(drive->controller);
1232 PrintDebug("[write_general_handler] IO write to port %x (val=0x%02x), channel = %d\n",
1233 port, value, get_channel_no(ramdisk, channel));
1237 case PRI_FEATURES_PORT:
1238 case SEC_FEATURES_PORT: // hard disk write precompensation 0x1f1
1240 write_features(channel, value);
1243 case PRI_SECT_CNT_PORT:
1244 case SEC_SECT_CNT_PORT: // hard disk sector count 0x1f2
1246 write_sector_count(channel, value);
1249 case PRI_SECT_ADDR1_PORT:
1250 case SEC_SECT_ADDR1_PORT: // hard disk sector number 0x1f3
1252 write_sector_number(channel, value);
1255 case PRI_SECT_ADDR2_PORT:
1256 case SEC_SECT_ADDR2_PORT: // hard disk cylinder low 0x1f4
1258 write_cylinder_low(channel, value);
1261 case PRI_SECT_ADDR3_PORT:
1262 case SEC_SECT_ADDR3_PORT: // hard disk cylinder high 0x1f5
1264 write_cylinder_high(channel, value);
1267 case PRI_DRV_SEL_PORT:
1268 case SEC_DRV_SEL_PORT: // hard disk drive and head register 0x1f6
1270 // b7 Extended data field for ECC
1271 // b6/b5: Used to be sector size. 00=256,01=512,10=1024,11=128
1272 // Since 512 was always used, bit 6 was taken to mean LBA mode:
1273 // b6 1=LBA mode, 0=CHS mode
1280 PrintDebug("\tDrive Select value=%x\n", value);
1282 if ((value & 0xa0) != 0xa0) {
1283 PrintDebug("\t\tIO write 0x%x (%02x): not 1x1xxxxxb\n", port, (unsigned) value);
1286 write_head_no(channel, value & 0xf);
1287 if ((controller->lba_mode == 0) && (((value >> 6) & 1) == 1)) {
1288 PrintDebug("\t\tenabling LBA mode\n");
1291 write_lba_mode(channel, (value >> 6) & 1);
1292 drive->cdrom.cd->lba = (value >> 6) & 1;
1295 channel->drive_select = (value >> 4) & 0x01;
1296 drive = get_selected_drive(channel);
1298 if (drive->device_type == IDE_NONE) {
1299 PrintDebug("\t\tError: device set to %d which does not exist! channel = 0x%x\n",
1300 channel->drive_select, get_channel_no(ramdisk, channel));
1302 controller->error_register = 0x04; // aborted
1303 controller->status.err = 1;
1309 PrintError("\t\thard drive: io write to unhandled port 0x%x (value = %c)\n", port, value);
1320 static void rd_raise_interrupt(struct vm_device * dev, struct channel_t * channel) {
1322 // struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1323 struct drive_t * drive = get_selected_drive(channel);
1324 struct controller_t * controller = &(drive->controller);
1326 PrintDebug("[raise_interrupt] disable_irq = %02x\n", controller->control.disable_irq);
1328 if (!(controller->control.disable_irq)) {
1331 PrintDebug("\t\tRaising interrupt %d {%s}\n\n", irq, device_type_to_str(drive->device_type));
1333 dev->vm->vm_ops.raise_irq(dev->vm, irq);
1335 PrintDebug("\t\tirq is disabled\n");
1341 static void rd_lower_irq(struct vm_device *dev, Bit32u irq) // __attribute__(regparm(1))
1343 PrintDebug("[lower_irq] irq = %d\n", irq);
1344 dev->vm->vm_ops.lower_irq(dev->vm, irq);
1353 //////////////////////////////////////////////////////////////////////////
1361 int handle_atapi_packet_command(struct vm_device * dev, struct channel_t * channel, ushort_t value) {
1362 //struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1363 struct drive_t * drive = get_selected_drive(channel);
1364 struct controller_t * controller = &(drive->controller);
1366 if (controller->buffer_index >= PACKET_SIZE) {
1367 PrintError("ATAPI packet exceeded maximum length: buffer_index (%d) >= PACKET_SIZE\n",
1368 controller->buffer_index);
1372 controller->buffer[controller->buffer_index] = value;
1373 controller->buffer[controller->buffer_index + 1] = (value >> 8);
1374 controller->buffer_index += 2;
1377 /* if packet completely writtten */
1378 if (controller->buffer_index >= PACKET_SIZE) {
1379 // complete command received
1380 Bit8u atapi_command = controller->buffer[0];
1382 PrintDebug("\t\tcdrom: ATAPI command 0x%x started\n", atapi_command);
1384 switch (atapi_command) {
1385 case 0x00: // test unit ready
1387 if (drive->cdrom.ready) {
1388 rd_atapi_cmd_nop(dev, channel);
1390 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1393 rd_raise_interrupt(dev, channel);
1397 case 0x03: // request sense
1399 int alloc_length = controller->buffer[4];
1401 if (rd_init_send_atapi_command(dev, channel, atapi_command, 18, alloc_length, false) == -1) {
1406 controller->buffer[0] = 0x70 | (1 << 7);
1407 controller->buffer[1] = 0;
1408 controller->buffer[2] = drive->sense.sense_key;
1409 controller->buffer[3] = drive->sense.information.arr[0];
1410 controller->buffer[4] = drive->sense.information.arr[1];
1411 controller->buffer[5] = drive->sense.information.arr[2];
1412 controller->buffer[6] = drive->sense.information.arr[3];
1413 controller->buffer[7] = 17 - 7;
1414 controller->buffer[8] = drive->sense.specific_inf.arr[0];
1415 controller->buffer[9] = drive->sense.specific_inf.arr[1];
1416 controller->buffer[10] = drive->sense.specific_inf.arr[2];
1417 controller->buffer[11] = drive->sense.specific_inf.arr[3];
1418 controller->buffer[12] = drive->sense.asc;
1419 controller->buffer[13] = drive->sense.ascq;
1420 controller->buffer[14] = drive->sense.fruc;
1421 controller->buffer[15] = drive->sense.key_spec.arr[0];
1422 controller->buffer[16] = drive->sense.key_spec.arr[1];
1423 controller->buffer[17] = drive->sense.key_spec.arr[2];
1425 rd_ready_to_send_atapi(dev, channel);
1428 case 0x1b: // start stop unit
1430 //bx_bool Immed = (controller->buffer[1] >> 0) & 1;
1431 rd_bool LoEj = (controller->buffer[4] >> 1) & 1;
1432 rd_bool Start = (controller->buffer[4] >> 0) & 1;
1435 if ((!LoEj) && (!Start)) {
1436 PrintError("FIXME: Stop disc not implemented\n");
1438 rd_atapi_cmd_nop(dev, channel);
1439 rd_raise_interrupt(dev, channel);
1440 } else if (!LoEj && Start) { // start (spin up) the disc
1442 drive->cdrom.cd->ops.start_cdrom(drive->cdrom.cd);
1444 PrintError("FIXME: ATAPI start disc not reading TOC\n");
1445 rd_atapi_cmd_nop(dev, channel);
1446 rd_raise_interrupt(dev, channel);
1447 } else if (LoEj && !Start) { // Eject the disc
1448 rd_atapi_cmd_nop(dev, channel);
1450 if (drive->cdrom.ready) {
1452 drive->cdrom.cd->ops.eject_cdrom(drive->cdrom.cd);
1454 drive->cdrom.ready = 0;
1455 //bx_options.atadevice[channel][SLAVE_SELECTED(channel)].Ostatus->set(EJECTED);
1456 //bx_gui->update_drive_status_buttons();
1458 rd_raise_interrupt(dev, channel);
1459 } else { // Load the disc
1460 // My guess is that this command only closes the tray, that's a no-op for us
1461 rd_atapi_cmd_nop(dev, channel);
1462 rd_raise_interrupt(dev, channel);
1466 case 0xbd: // mechanism status
1468 uint16_t alloc_length = rd_read_16bit(controller->buffer + 8);
1470 if (alloc_length == 0) {
1471 PrintError("Zero allocation length to MECHANISM STATUS not impl.\n");
1475 if (rd_init_send_atapi_command(dev, channel, atapi_command, 8, alloc_length, false) == -1) {
1479 controller->buffer[0] = 0; // reserved for non changers
1480 controller->buffer[1] = 0; // reserved for non changers
1482 controller->buffer[2] = 0; // Current LBA (TODO!)
1483 controller->buffer[3] = 0; // Current LBA (TODO!)
1484 controller->buffer[4] = 0; // Current LBA (TODO!)
1486 controller->buffer[5] = 1; // one slot
1488 controller->buffer[6] = 0; // slot table length
1489 controller->buffer[7] = 0; // slot table length
1491 rd_ready_to_send_atapi(dev, channel);
1494 case 0x5a: // mode sense
1496 uint16_t alloc_length = rd_read_16bit(controller->buffer + 7);
1498 Bit8u PC = controller->buffer[2] >> 6;
1499 Bit8u PageCode = controller->buffer[2] & 0x3f;
1502 case 0x0: // current values
1505 case 0x01: // error recovery
1508 if (rd_init_send_atapi_command(dev, channel, atapi_command, sizeof(struct error_recovery_t) + 8, alloc_length, false) == -1) {
1512 rd_init_mode_sense_single(dev, channel, &(drive->cdrom.current.error_recovery),
1513 sizeof(struct error_recovery_t));
1514 rd_ready_to_send_atapi(dev, channel);
1517 case 0x2a: // CD-ROM capabilities & mech. status
1520 if (rd_init_send_atapi_command(dev, channel, atapi_command, 28, alloc_length, false) == -1) {
1524 rd_init_mode_sense_single(dev, channel, &(controller->buffer[8]), 28);
1526 controller->buffer[8] = 0x2a;
1527 controller->buffer[9] = 0x12;
1528 controller->buffer[10] = 0x00;
1529 controller->buffer[11] = 0x00;
1530 // Multisession, Mode 2 Form 2, Mode 2 Form 1
1531 controller->buffer[12] = 0x70;
1532 controller->buffer[13] = (3 << 5);
1533 controller->buffer[14] = (unsigned char) (1 |
1534 (drive->cdrom.locked ? (1 << 1) : 0) |
1537 controller->buffer[15] = 0x00;
1538 controller->buffer[16] = (706 >> 8) & 0xff;
1539 controller->buffer[17] = 706 & 0xff;
1540 controller->buffer[18] = 0;
1541 controller->buffer[19] = 2;
1542 controller->buffer[20] = (512 >> 8) & 0xff;
1543 controller->buffer[21] = 512 & 0xff;
1544 controller->buffer[22] = (706 >> 8) & 0xff;
1545 controller->buffer[23] = 706 & 0xff;
1546 controller->buffer[24] = 0;
1547 controller->buffer[25] = 0;
1548 controller->buffer[26] = 0;
1549 controller->buffer[27] = 0;
1550 rd_ready_to_send_atapi(dev, channel);
1553 case 0x0d: // CD-ROM
1554 case 0x0e: // CD-ROM audio control
1557 PrintError("Ramdisk: cdrom: MODE SENSE (curr), code=%x not implemented yet\n",
1559 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1560 ASC_INV_FIELD_IN_CMD_PACKET);
1561 rd_raise_interrupt(dev, channel);
1566 // not implemeted by this device
1567 PrintDebug("\t\tcdrom: MODE SENSE PC=%x, PageCode=%x, not implemented by device\n",
1569 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1570 ASC_INV_FIELD_IN_CMD_PACKET);
1571 rd_raise_interrupt(dev, channel);
1577 case 0x1: // changeable values
1580 case 0x01: // error recovery
1581 case 0x0d: // CD-ROM
1582 case 0x0e: // CD-ROM audio control
1583 case 0x2a: // CD-ROM capabilities & mech. status
1586 PrintError("cdrom: MODE SENSE (chg), code=%x not implemented yet\n",
1588 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1589 ASC_INV_FIELD_IN_CMD_PACKET);
1590 rd_raise_interrupt(dev, channel);
1595 // not implemeted by this device
1596 PrintDebug("\t\tcdrom: MODE SENSE PC=%x, PageCode=%x, not implemented by device\n",
1598 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1599 ASC_INV_FIELD_IN_CMD_PACKET);
1600 rd_raise_interrupt(dev, channel);
1606 case 0x2: // default values
1609 case 0x01: // error recovery
1610 case 0x0d: // CD-ROM
1611 case 0x0e: // CD-ROM audio control
1612 case 0x2a: // CD-ROM capabilities & mech. status
1614 PrintError("cdrom: MODE SENSE (dflt), code=%x\n",
1620 // not implemeted by this device
1621 PrintDebug("\t\tcdrom: MODE SENSE PC=%x, PageCode=%x, not implemented by device\n",
1623 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1624 ASC_INV_FIELD_IN_CMD_PACKET);
1625 rd_raise_interrupt(dev, channel);
1631 case 0x3: // saved values not implemented
1633 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
1634 rd_raise_interrupt(dev, channel);
1639 PrintError("Should not get here!\n");
1646 case 0x12: // inquiry
1648 uint8_t alloc_length = controller->buffer[4];
1650 if (rd_init_send_atapi_command(dev, channel, atapi_command, 36, alloc_length, false) == -1) {
1654 controller->buffer[0] = 0x05; // CD-ROM
1655 controller->buffer[1] = 0x80; // Removable
1656 controller->buffer[2] = 0x00; // ISO, ECMA, ANSI version
1657 controller->buffer[3] = 0x21; // ATAPI-2, as specified
1658 controller->buffer[4] = 31; // additional length (total 36)
1659 controller->buffer[5] = 0x00; // reserved
1660 controller->buffer[6] = 0x00; // reserved
1661 controller->buffer[7] = 0x00; // reserved
1664 const char* vendor_id = "VTAB ";
1666 for (i = 0; i < 8; i++) {
1667 controller->buffer[8+i] = vendor_id[i];
1671 const char* product_id = "Turbo CD-ROM ";
1672 for (i = 0; i < 16; i++) {
1673 controller->buffer[16+i] = product_id[i];
1676 // Product Revision level
1677 const char* rev_level = "1.0 ";
1678 for (i = 0; i < 4; i++) {
1679 controller->buffer[32 + i] = rev_level[i];
1682 rd_ready_to_send_atapi(dev, channel);
1685 case 0x25: // read cd-rom capacity
1687 // no allocation length???
1688 if (rd_init_send_atapi_command(dev, channel, atapi_command, 8, 8, false) == -1) {
1692 if (drive->cdrom.ready) {
1693 uint32_t capacity = drive->cdrom.capacity;
1695 PrintDebug("\t\tCapacity is %d sectors (%d bytes)\n", capacity, capacity * 2048);
1697 controller->buffer[0] = (capacity >> 24) & 0xff;
1698 controller->buffer[1] = (capacity >> 16) & 0xff;
1699 controller->buffer[2] = (capacity >> 8) & 0xff;
1700 controller->buffer[3] = (capacity >> 0) & 0xff;
1701 controller->buffer[4] = (2048 >> 24) & 0xff;
1702 controller->buffer[5] = (2048 >> 16) & 0xff;
1703 controller->buffer[6] = (2048 >> 8) & 0xff;
1704 controller->buffer[7] = (2048 >> 0) & 0xff;
1706 rd_ready_to_send_atapi(dev, channel);
1708 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1709 rd_raise_interrupt(dev, channel);
1715 case 0xbe: // read cd
1717 if (drive->cdrom.ready) {
1718 PrintError("Read CD with CD present not implemented\n");
1719 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1720 rd_raise_interrupt(dev, channel);
1722 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1723 rd_raise_interrupt(dev, channel);
1727 case 0x43: // read toc
1729 if (drive->cdrom.ready) {
1731 bool msf = (controller->buffer[1] >> 1) & 1;
1732 uint8_t starting_track = controller->buffer[6];
1734 uint16_t alloc_length = rd_read_16bit(controller->buffer + 7);
1736 uint8_t format = (controller->buffer[9] >> 6);
1741 if (!(drive->cdrom.cd->ops.read_toc(drive->cdrom.cd, controller->buffer,
1742 &toc_length, msf, starting_track))) {
1743 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1744 ASC_INV_FIELD_IN_CMD_PACKET);
1745 rd_raise_interrupt(dev, channel);
1747 if (rd_init_send_atapi_command(dev, channel, atapi_command, toc_length, alloc_length, false) == -1) {
1750 rd_ready_to_send_atapi(dev, channel);
1755 // multi session stuff. we ignore this and emulate a single session only
1756 if (rd_init_send_atapi_command(dev, channel, atapi_command, 12, alloc_length, false) == -1) {
1760 controller->buffer[0] = 0;
1761 controller->buffer[1] = 0x0a;
1762 controller->buffer[2] = 1;
1763 controller->buffer[3] = 1;
1765 for (i = 0; i < 8; i++) {
1766 controller->buffer[4 + i] = 0;
1769 rd_ready_to_send_atapi(dev, channel);
1774 PrintError("(READ TOC) Format %d not supported\n", format);
1778 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1779 rd_raise_interrupt(dev, channel);
1783 case 0x28: // read (10)
1784 case 0xa8: // read (12)
1787 uint32_t transfer_length;
1788 if (atapi_command == 0x28) {
1789 transfer_length = rd_read_16bit(controller->buffer + 7);
1791 transfer_length = rd_read_32bit(controller->buffer + 6);
1794 uint32_t lba = rd_read_32bit(controller->buffer + 2);
1796 if (!(drive->cdrom.ready)) {
1797 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1798 rd_raise_interrupt(dev, channel);
1802 if (transfer_length == 0) {
1803 rd_atapi_cmd_nop(dev, channel);
1804 rd_raise_interrupt(dev, channel);
1805 PrintDebug("\t\tREAD(%d) with transfer length 0, ok\n", (atapi_command == 0x28) ? 10 : 12);
1809 if (lba + transfer_length > drive->cdrom.capacity) {
1810 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
1811 rd_raise_interrupt(dev, channel);
1815 PrintDebug("\t\tcdrom: READ (%d) LBA=%d LEN=%d\n",
1816 (atapi_command == 0x28) ? 10 : 12,
1817 lba, transfer_length);
1820 if (rd_init_send_atapi_command(dev, channel, atapi_command, transfer_length * 2048,
1821 transfer_length * 2048, true) == -1) {
1824 drive->cdrom.remaining_blocks = transfer_length;
1825 drive->cdrom.next_lba = lba;
1826 rd_ready_to_send_atapi(dev, channel);
1831 uint32_t lba = rd_read_32bit(controller->buffer + 2);
1833 if (!(drive->cdrom.ready)) {
1834 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1835 rd_raise_interrupt(dev, channel);
1839 if (lba > drive->cdrom.capacity) {
1840 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
1841 rd_raise_interrupt(dev, channel);
1845 PrintDebug("\t\tcdrom: SEEK (ignored)\n");
1847 rd_atapi_cmd_nop(dev, channel);
1848 rd_raise_interrupt(dev, channel);
1852 case 0x1e: // prevent/allow medium removal
1855 if (drive->cdrom.ready) {
1856 drive->cdrom.locked = controller->buffer[4] & 1;
1857 rd_atapi_cmd_nop(dev, channel);
1859 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1862 rd_raise_interrupt(dev, channel);
1866 case 0x42: // read sub-channel
1868 //bool msf = get_packet_field(channel,1, 1, 1);
1869 bool sub_q = get_packet_field(channel,2, 6, 1);
1870 //uint8_t data_format = get_packet_byte(channel,3);
1871 //uint8_t track_number = get_packet_byte(channel,6);
1872 uint16_t alloc_length = get_packet_word(channel,7);
1877 UNUSED(data_format);
1878 UNUSED(track_number);
1880 if (!(drive->cdrom.ready)) {
1881 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1882 rd_raise_interrupt(dev, channel);
1884 controller->buffer[0] = 0;
1885 controller->buffer[1] = 0; // audio not supported
1886 controller->buffer[2] = 0;
1887 controller->buffer[3] = 0;
1889 int ret_len = 4; // header size
1891 if (sub_q) { // !sub_q == header only
1892 PrintError("Read sub-channel with SubQ not implemented\n");
1893 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1894 ASC_INV_FIELD_IN_CMD_PACKET);
1895 rd_raise_interrupt(dev, channel);
1898 if (rd_init_send_atapi_command(dev, channel, atapi_command, ret_len, alloc_length, false) == -1) {
1901 rd_ready_to_send_atapi(dev, channel);
1905 case 0x51: // read disc info
1907 // no-op to keep the Linux CD-ROM driver happy
1908 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1909 rd_raise_interrupt(dev, channel);
1912 case 0x55: // mode select
1913 case 0xa6: // load/unload cd
1914 case 0x4b: // pause/resume
1915 case 0x45: // play audio
1916 case 0x47: // play audio msf
1917 case 0xbc: // play cd
1918 case 0xb9: // read cd msf
1919 case 0x44: // read header
1921 case 0xbb: // set cd speed
1922 case 0x4e: // stop play/scan
1925 PrintError("ATAPI command 0x%x not implemented yet\n",
1927 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1928 rd_raise_interrupt(dev, channel);
1931 PrintError("Unknown ATAPI command 0x%x (%d)\n",
1932 atapi_command, atapi_command);
1933 // We'd better signal the error if the user chose to continue
1934 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1935 rd_raise_interrupt(dev, channel);
1945 int rd_init_send_atapi_command(struct vm_device * dev, struct channel_t * channel, Bit8u command, int req_length, int alloc_length, bool lazy)
1947 struct drive_t * drive = &(channel->drives[channel->drive_select]);
1948 struct controller_t * controller = &(drive->controller);
1950 // controller->byte_count is a union of controller->cylinder_no;
1951 // lazy is used to force a data read in the buffer at the next read.
1953 PrintDebug("[rd_init_send_atapi_cmd]\n");
1955 if (controller->byte_count == 0xffff) {
1956 controller->byte_count = 0xfffe;
1959 if ((controller->byte_count & 1) &&
1960 !(alloc_length <= controller->byte_count)) {
1962 PrintDebug("\t\tOdd byte count (0x%04x) to ATAPI command 0x%02x, using 0x%x\n",
1963 controller->byte_count,
1965 controller->byte_count - 1);
1967 controller->byte_count -= 1;
1970 if (controller->byte_count == 0) {
1971 PrintError("\t\tATAPI command with zero byte count\n");
1975 if (alloc_length < 0) {
1976 PrintError("\t\tAllocation length < 0\n");
1980 if (alloc_length == 0) {
1981 alloc_length = controller->byte_count;
1984 controller->interrupt_reason.i_o = 1;
1985 controller->interrupt_reason.c_d = 0;
1986 controller->status.busy = 0;
1987 controller->status.drq = 1;
1988 controller->status.err = 0;
1990 // no bytes transfered yet
1992 controller->buffer_index = 2048;
1994 controller->buffer_index = 0;
1997 controller->drq_index = 0;
1999 if (controller->byte_count > req_length) {
2000 controller->byte_count = req_length;
2003 if (controller->byte_count > alloc_length) {
2004 controller->byte_count = alloc_length;
2007 drive->atapi.command = command;
2008 drive->atapi.drq_bytes = controller->byte_count;
2009 drive->atapi.total_bytes_remaining = (req_length < alloc_length) ? req_length : alloc_length;
2012 // // bias drq_bytes and total_bytes_remaining
2013 // SELECTED_DRIVE(channel).atapi.drq_bytes += 2048;
2014 // SELECTED_DRIVE(channel).atapi.total_bytes_remaining += 2048;
2022 void rd_ready_to_send_atapi(struct vm_device * dev, struct channel_t * channel) {
2023 PrintDebug("[rd_ready_to_send_atapi]\n");
2025 rd_raise_interrupt(dev, channel);
2032 void rd_atapi_cmd_error(struct vm_device * dev, struct channel_t * channel, sense_t sense_key, asc_t asc)
2034 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2035 struct controller_t * controller = &(drive->controller);
2037 #ifdef DEBUG_RAMDISK
2039 struct ramdisk_t *ramdisk = (struct ramdisk_t *)(dev->private_data);
2040 PrintDebug("[rd_atapi_cmd_error]\n");
2041 PrintDebug("Error: atapi_cmd_error channel=%02x key=%02x asc=%02x\n",
2042 get_channel_no(ramdisk, channel), sense_key, asc);
2046 controller->error_register = sense_key << 4;
2047 controller->interrupt_reason.i_o = 1;
2048 controller->interrupt_reason.c_d = 1;
2049 controller->interrupt_reason.rel = 0;
2050 controller->status.busy = 0;
2051 controller->status.drive_ready = 1;
2052 controller->status.write_fault = 0;
2053 controller->status.drq = 0;
2054 controller->status.err = 1;
2056 drive->sense.sense_key = sense_key;
2057 drive->sense.asc = asc;
2058 drive->sense.ascq = 0;
2063 void rd_atapi_cmd_nop(struct vm_device * dev, struct channel_t * channel)
2065 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2066 struct controller_t * controller = &(drive->controller);
2068 PrintDebug("[rd_atapi_cmd_nop]\n");
2069 controller->interrupt_reason.i_o = 1;
2070 controller->interrupt_reason.c_d = 1;
2071 controller->interrupt_reason.rel = 0;
2072 controller->status.busy = 0;
2073 controller->status.drive_ready = 1;
2074 controller->status.drq = 0;
2075 controller->status.err = 0;
2081 void rd_identify_ATAPI_drive(struct vm_device * dev, struct channel_t * channel)
2083 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2084 struct controller_t * controller = &(drive->controller);
2088 const char* serial_number = " VT00001\0\0\0\0\0\0\0\0\0\0\0\0";
2089 const char* firmware = "ALPHA1 ";
2091 drive->id_drive[0] = (2 << 14) | (5 << 8) | (1 << 7) | (2 << 5) | (0 << 0); // Removable CDROM, 50us response, 12 byte packets
2093 for (i = 1; i <= 9; i++) {
2094 drive->id_drive[i] = 0;
2097 for (i = 0; i < 10; i++) {
2098 drive->id_drive[10 + i] = ((serial_number[i * 2] << 8) |
2099 (serial_number[(i * 2) + 1]));
2102 for (i = 20; i <= 22; i++) {
2103 drive->id_drive[i] = 0;
2106 for (i = 0; i < strlen(firmware)/2; i++) {
2107 drive->id_drive[23 + i] = ((firmware[i * 2] << 8) |
2108 (firmware[(i * 2) + 1]));
2110 V3_ASSERT((23 + i) == 27);
2112 for (i = 0; i < strlen((char *)(drive->model_no)) / 2; i++) {
2113 drive->id_drive[27 + i] = ((drive->model_no[i * 2] << 8) |
2114 (drive->model_no[(i * 2) + 1]));
2116 V3_ASSERT((27 + i) == 47);
2118 drive->id_drive[47] = 0;
2119 drive->id_drive[48] = 1; // 32 bits access
2121 drive->id_drive[49] = (1 << 9); // LBA supported
2123 drive->id_drive[50] = 0;
2124 drive->id_drive[51] = 0;
2125 drive->id_drive[52] = 0;
2127 drive->id_drive[53] = 3; // words 64-70, 54-58 valid
2129 for (i = 54; i <= 62; i++) {
2130 drive->id_drive[i] = 0;
2133 // copied from CFA540A
2134 drive->id_drive[63] = 0x0103; // variable (DMA stuff)
2135 drive->id_drive[64] = 0x0001; // PIO
2136 drive->id_drive[65] = 0x00b4;
2137 drive->id_drive[66] = 0x00b4;
2138 drive->id_drive[67] = 0x012c;
2139 drive->id_drive[68] = 0x00b4;
2141 drive->id_drive[69] = 0;
2142 drive->id_drive[70] = 0;
2143 drive->id_drive[71] = 30; // faked
2144 drive->id_drive[72] = 30; // faked
2145 drive->id_drive[73] = 0;
2146 drive->id_drive[74] = 0;
2148 drive->id_drive[75] = 0;
2150 for (i = 76; i <= 79; i++) {
2151 drive->id_drive[i] = 0;
2154 drive->id_drive[80] = 0x1e; // supports up to ATA/ATAPI-4
2155 drive->id_drive[81] = 0;
2156 drive->id_drive[82] = 0;
2157 drive->id_drive[83] = 0;
2158 drive->id_drive[84] = 0;
2159 drive->id_drive[85] = 0;
2160 drive->id_drive[86] = 0;
2161 drive->id_drive[87] = 0;
2162 drive->id_drive[88] = 0;
2164 for (i = 89; i <= 126; i++) {
2165 drive->id_drive[i] = 0;
2168 drive->id_drive[127] = 0;
2169 drive->id_drive[128] = 0;
2171 for (i = 129; i <= 159; i++) {
2172 drive->id_drive[i] = 0;
2175 for (i = 160; i <= 255; i++) {
2176 drive->id_drive[i] = 0;
2179 // now convert the id_drive array (native 256 word format) to
2180 // the controller buffer (512 bytes)
2182 for (i = 0; i <= 255; i++) {
2183 temp16 = drive->id_drive[i];
2184 controller->buffer[i * 2] = temp16 & 0x00ff;
2185 controller->buffer[i * 2 + 1] = temp16 >> 8;
2198 void rd_init_mode_sense_single(struct vm_device * dev,
2199 struct channel_t * channel, const void* src, int size)
2201 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2202 struct controller_t * controller = &(drive->controller);
2204 PrintDebug("[rd_init_mode_sense_single]\n");
2207 controller->buffer[0] = (size + 6) >> 8;
2208 controller->buffer[1] = (size + 6) & 0xff;
2209 controller->buffer[2] = 0x70; // no media present
2210 controller->buffer[3] = 0; // reserved
2211 controller->buffer[4] = 0; // reserved
2212 controller->buffer[5] = 0; // reserved
2213 controller->buffer[6] = 0; // reserved
2214 controller->buffer[7] = 0; // reserved
2217 memcpy(controller->buffer + 8, src, size);
2222 static void rd_command_aborted(struct vm_device * dev,
2223 struct channel_t * channel, unsigned value) {
2224 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2225 struct controller_t * controller = &(drive->controller);
2227 PrintDebug("[rd_command_aborted]\n");
2228 PrintDebug("\t\taborting on command 0x%02x {%s}\n", value, device_type_to_str(drive->device_type));
2230 controller->current_command = 0;
2231 controller->status.busy = 0;
2232 controller->status.drive_ready = 1;
2233 controller->status.err = 1;
2234 controller->error_register = 0x04; // command ABORTED
2235 controller->status.drq = 0;
2236 controller->status.seek_complete = 0;
2237 controller->status.corrected_data = 0;
2238 controller->buffer_index = 0;
2240 rd_raise_interrupt(dev, channel);
2244 static int ramdisk_init_device(struct vm_device *dev) {
2245 struct ramdisk_t *ramdisk= (struct ramdisk_t *)dev->private_data;
2247 PrintDebug("Initializing Ramdisk\n");
2250 rd_init_hardware(ramdisk);
2253 dev_hook_io(dev, PRI_CTRL_PORT,
2254 &read_status_port, &write_ctrl_port);
2256 dev_hook_io(dev, PRI_DATA_PORT,
2257 &read_data_port, &write_data_port);
2258 dev_hook_io(dev, PRI_FEATURES_PORT,
2259 &read_general_port, &write_general_port);
2260 dev_hook_io(dev, PRI_SECT_CNT_PORT,
2261 &read_general_port, &write_general_port);
2262 dev_hook_io(dev, PRI_SECT_ADDR1_PORT,
2263 &read_general_port, &write_general_port);
2264 dev_hook_io(dev, PRI_SECT_ADDR2_PORT,
2265 &read_general_port, &write_general_port);
2266 dev_hook_io(dev, PRI_SECT_ADDR3_PORT,
2267 &read_general_port, &write_general_port);
2268 dev_hook_io(dev, PRI_DRV_SEL_PORT,
2269 &read_general_port, &write_general_port);
2270 dev_hook_io(dev, PRI_CMD_PORT,
2271 &read_status_port, &write_cmd_port);
2274 dev_hook_io(dev, SEC_CTRL_PORT,
2275 &read_status_port, &write_ctrl_port);
2277 dev_hook_io(dev, SEC_DATA_PORT,
2278 &read_data_port, &write_data_port);
2279 dev_hook_io(dev, SEC_FEATURES_PORT,
2280 &read_general_port, &write_general_port);
2281 dev_hook_io(dev, SEC_SECT_CNT_PORT,
2282 &read_general_port, &write_general_port);
2283 dev_hook_io(dev, SEC_SECT_ADDR1_PORT,
2284 &read_general_port, &write_general_port);
2285 dev_hook_io(dev, SEC_SECT_ADDR2_PORT,
2286 &read_general_port, &write_general_port);
2287 dev_hook_io(dev, SEC_SECT_ADDR3_PORT,
2288 &read_general_port, &write_general_port);
2289 dev_hook_io(dev, SEC_DRV_SEL_PORT,
2290 &read_general_port, &write_general_port);
2291 dev_hook_io(dev, SEC_CMD_PORT,
2292 &read_status_port, &write_cmd_port);
2296 dev_hook_io(dev, SEC_ADDR_REG_PORT,
2297 &read_general_port, &write_general_port);
2299 dev_hook_io(dev, PRI_ADDR_REG_PORT,
2300 &read_general_port, &write_general_port);
2309 static int ramdisk_deinit_device(struct vm_device *dev) {
2310 struct ramdisk_t *ramdisk = (struct ramdisk_t *)(dev->private_data);
2311 rd_close_harddrive(ramdisk);
2315 static struct vm_device_ops dev_ops = {
2316 .init = ramdisk_init_device,
2317 .deinit = ramdisk_deinit_device,
2326 struct vm_device *create_ramdisk()
2329 struct ramdisk_t *ramdisk;
2330 ramdisk = (struct ramdisk_t *)V3_Malloc(sizeof(struct ramdisk_t));
2331 V3_ASSERT(ramdisk != NULL);
2333 PrintDebug("[create_ramdisk]\n");
2335 struct vm_device *device = create_device("RAMDISK", &dev_ops, ramdisk);
2343 #ifdef DEBUG_RAMDISK
2345 static void rd_print_state(struct ramdisk_t * ramdisk) {
2348 struct channel_t * channels = (struct channel_t *)(&(ramdisk->channels));
2351 for (channel = 0; channel < MAX_ATA_CHANNEL; channel++) {
2352 memset((char *)(channels + channel), 0, sizeof(struct channel_t));
2355 PrintDebug("sizeof(*channels) = %d\n", sizeof(*channels));
2356 PrintDebug("sizeof(channles->drives[0].controller) = %d\n", sizeof((channels->drives[0].controller)));
2357 PrintDebug("sizeof(channles->drives[0].cdrom) = %d\n", sizeof((channels->drives[0].cdrom)));
2358 PrintDebug("sizeof(channles->drives[0].sense) = %d\n", sizeof((channels->drives[0].sense)));
2359 PrintDebug("sizeof(channles->drives[0].atapi) = %d\n", sizeof((channels->drives[0].atapi)));
2362 PrintDebug("sizeof(channles->drives[0].controller.status) = %d\n",
2363 sizeof((channels->drives[0].controller.status)));
2364 PrintDebug("sizeof(channles->drives[0].controller.sector_count) = %d\n",
2365 sizeof((channels->drives[0].controller.sector_count)));
2366 PrintDebug("sizeof(channles->drives[0].controller.interrupt_reason) = %d\n",
2367 sizeof((channels->drives[0].controller.interrupt_reason)));
2369 PrintDebug("sizeof(channles->drives[0].controller.cylinder_no) = %d\n",
2370 sizeof((channels->drives[0].controller.cylinder_no)));
2371 PrintDebug("sizeof(channles->drives[0].controller.byte_count) = %d\n",
2372 sizeof((channels->drives[0].controller.byte_count)));
2375 PrintDebug("sizeof(channles->drives[0].controller.control) = %d\n",
2376 sizeof((channels->drives[0].controller.control)));
2379 for (channel = 0; channel < MAX_ATA_CHANNEL; channel++){
2381 for (device = 0; device < 2; device++){
2383 // Initialize controller state, even if device is not present
2384 PrintDebug("channels[%d].drives[%d].controller.status.busy = %d\n",
2386 channels[channel].drives[device].controller.status.busy);
2387 PrintDebug("channels[%d].drives[%d].controller.status.drive_ready = %d\n",
2389 channels[channel].drives[device].controller.status.drive_ready);
2390 PrintDebug("channels[%d].drives[%d].controller.status.write_fault = %d\n",
2392 channels[channel].drives[device].controller.status.write_fault);
2393 PrintDebug("channels[%d].drives[%d].controller.status.seek_complete = %d\n",
2395 channels[channel].drives[device].controller.status.seek_complete);
2396 PrintDebug("channels[%d].drives[%d].controller.status.drq = %d\n",
2398 channels[channel].drives[device].controller.status.drq);
2399 PrintDebug("channels[%d].drives[%d].controller.status.corrected_data = %d\n",
2401 channels[channel].drives[device].controller.status.corrected_data);
2402 PrintDebug("channels[%d].drives[%d].controller.status.index_pulse = %d\n",
2404 channels[channel].drives[device].controller.status.index_pulse);
2405 PrintDebug("channels[%d].drives[%d].controller.status.index_pulse_count = %d\n",
2407 channels[channel].drives[device].controller.status.index_pulse_count);
2408 PrintDebug("channels[%d].drives[%d].controller.status.err = %d\n",
2410 channels[channel].drives[device].controller.status.err);
2413 PrintDebug("channels[%d].drives[%d].controller.error_register = %d\n",
2415 channels[channel].drives[device].controller.error_register);
2416 PrintDebug("channels[%d].drives[%d].controller.head_no = %d\n",
2418 channels[channel].drives[device].controller.head_no);
2419 PrintDebug("channels[%d].drives[%d].controller.sector_count = %d\n",
2421 channels[channel].drives[device].controller.sector_count);
2422 PrintDebug("channels[%d].drives[%d].controller.sector_no = %d\n",
2424 channels[channel].drives[device].controller.sector_no);
2425 PrintDebug("channels[%d].drives[%d].controller.cylinder_no = %d\n",
2427 channels[channel].drives[device].controller.cylinder_no);
2428 PrintDebug("channels[%d].drives[%d].controller.current_command = %02x\n",
2430 channels[channel].drives[device].controller.current_command);
2431 PrintDebug("channels[%d].drives[%d].controller.buffer_index = %d\n",
2433 channels[channel].drives[device].controller.buffer_index);
2436 PrintDebug("channels[%d].drives[%d].controller.control.reset = %d\n",
2438 channels[channel].drives[device].controller.control.reset);
2439 PrintDebug("channels[%d].drives[%d].controller.control.disable_irq = %d\n",
2441 channels[channel].drives[device].controller.control.disable_irq);
2444 PrintDebug("channels[%d].drives[%d].controller.reset_in_progress = %d\n",
2446 channels[channel].drives[device].controller.reset_in_progress);
2447 PrintDebug("channels[%d].drives[%d].controller.sectors_per_block = %02x\n",
2449 channels[channel].drives[device].controller.sectors_per_block);
2450 PrintDebug("channels[%d].drives[%d].controller.lba_mode = %d\n",
2452 channels[channel].drives[device].controller.lba_mode);
2453 PrintDebug("channels[%d].drives[%d].controller.features = %d\n",
2455 channels[channel].drives[device].controller.features);
2458 PrintDebug("channels[%d].drives[%d].model_no = %s\n",
2460 channels[channel].drives[device].model_no);
2461 PrintDebug("channels[%d].drives[%d].device_type = %d\n",
2463 channels[channel].drives[device].device_type);
2464 PrintDebug("channels[%d].drives[%d].cdrom.locked = %d\n",
2466 channels[channel].drives[device].cdrom.locked);
2467 PrintDebug("channels[%d].drives[%d].sense.sense_key = %d\n",
2469 channels[channel].drives[device].sense.sense_key);
2470 PrintDebug("channels[%d].drives[%d].sense.asc = %d\n",
2472 channels[channel].drives[device].sense.asc);
2473 PrintDebug("channels[%d].drives[%d].sense.ascq = %d\n",
2475 channels[channel].drives[device].sense.ascq);
2479 PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.c_d = %02x\n",
2481 channels[channel].drives[device].controller.interrupt_reason.c_d);
2483 PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.i_o = %02x\n",
2485 channels[channel].drives[device].controller.interrupt_reason.i_o);
2487 PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.rel = %02x\n",
2489 channels[channel].drives[device].controller.interrupt_reason.rel);
2491 PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.tag = %02x\n",
2493 channels[channel].drives[device].controller.interrupt_reason.tag);
2495 PrintDebug("channels[%d].drives[%d].cdrom.ready = %d\n",
2497 channels[channel].drives[device].cdrom.ready);
2506 static void trace_info(ushort_t port, void *src, uint_t length) {
2511 if (length == 1 && *((uchar_t*) src) == ATA_DETECT)
2512 PrintDebug("ata_detect()\n");
2516 if (length == 1 && *((uchar_t*) src) == ATA_RESET)
2517 PrintDebug("ata_reset()\n");
2521 if (length == 1 && *((uchar_t*) src) == ATA_CMD_DATA_IN)
2522 PrintDebug("ata_cmd_data_in()\n");
2526 if (length == 1 && *((uchar_t*) src) == ATA_CMD_DATA_OUT)
2527 PrintDebug("ata_cmd_data_out()\n");
2531 if (length == 1 && *((uchar_t*) src) == ATA_CMD_PACKET)
2532 PrintDebug("ata_cmd_packet()\n");
2536 if (length == 1 && *((uchar_t*) src) == ATAPI_GET_SENSE)
2537 PrintDebug("atapi_get_sense()\n");
2541 if (length == 1 && *((uchar_t*) src) == ATAPI_IS_READY)
2542 PrintDebug("atapi_is_ready()\n");
2546 if (length == 1 && *((uchar_t*) src) == ATAPI_IS_CDROM)
2547 PrintDebug("atapi_is_cdrom()\n");
2552 if (length == 1 && *((uchar_t*) src) == CDEMU_INIT)
2553 PrintDebug("cdemu_init()\n");
2557 if (length == 1 && *((uchar_t*) src) == CDEMU_ISACTIVE)
2558 PrintDebug("cdemu_isactive()\n");
2562 if (length == 1 && *((uchar_t*) src) == CDEMU_EMULATED_DRIVE)
2563 PrintDebug("cdemu_emulated_drive()\n");
2567 if (length == 1 && *((uchar_t*) src) == CDROM_BOOT)
2568 PrintDebug("cdrom_boot()\n");
2572 if (length == 1 && *((uchar_t*) src) == HARD_DRIVE_POST)
2573 PrintDebug("ata_hard_drive_post()\n");
2578 PrintDebug("ata_device_no(%d)\n", *((uchar_t*) src));
2583 PrintDebug("ata_device_type(%d)\n", *((uchar_t*) src));
2587 if (length == 1 && *((uchar_t*) src) == INT13_HARDDISK)
2588 PrintDebug("int13_harddrive()\n");
2592 if (length == 1 && *((uchar_t*) src) == INT13_CDROM)
2593 PrintDebug("int13_cdrom()\n");
2597 if (length == 1 && *((uchar_t*) src) == INT13_CDEMU)
2598 PrintDebug("int13_cdemu()\n");
2602 if (length == 1 && *((uchar_t*) src) == INT13_ELTORITO)
2603 PrintDebug("int13_eltorito()\n");
2607 if (length == 1 && *((uchar_t*) src) == INT13_DISKETTE_FUNCTION)
2608 PrintDebug("int13_diskette_function()\n");
2619 static int check_bit_fields(struct controller_t * controller) {
2621 controller->sector_count = 0;
2622 controller->interrupt_reason.c_d = 1;
2623 if (controller->sector_count != 0x01) {
2624 return INTR_REASON_BIT_ERR;
2627 controller->sector_count = 0;
2628 controller->interrupt_reason.i_o = 1;
2629 if (controller->sector_count != 0x02) {
2630 return INTR_REASON_BIT_ERR;
2633 controller->sector_count = 0;
2634 controller->interrupt_reason.rel = 1;
2635 if (controller->sector_count != 0x04) {
2636 return INTR_REASON_BIT_ERR;
2639 controller->sector_count = 0;
2640 controller->interrupt_reason.tag = 3;
2641 if (controller->sector_count != 0x18) {
2642 return INTR_REASON_BIT_ERR;