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 PrintTrace(fmt, args...)
53 #define PrintDebug(fmt, args...)
61 * Data type definitions
64 #define INDEX_PULSE_CYCLE 10
69 #define INTR_REASON_BIT_ERR 0x01
70 #define UNABLE_FIND_TAT_CHANNEL_ERR 0x02
72 #define READ_BUF_GT_512 0x04
76 #define PRI_DATA_PORT 0x1f0
77 #define PRI_FEATURES_PORT 0x1f1
78 #define PRI_SECT_CNT_PORT 0x1f2
79 #define PRI_SECT_ADDR1_PORT 0x1f3
80 #define PRI_SECT_ADDR2_PORT 0x1f4
81 #define PRI_SECT_ADDR3_PORT 0x1f5
82 #define PRI_DRV_SEL_PORT 0x1f6
83 #define PRI_CMD_PORT 0x1f7
84 #define PRI_CTRL_PORT 0x3f6
85 #define PRI_ADDR_REG_PORT 0x3f7
87 #define SEC_DATA_PORT 0x170
88 #define SEC_FEATURES_PORT 0x171
89 #define SEC_SECT_CNT_PORT 0x172
90 #define SEC_SECT_ADDR1_PORT 0x173
91 #define SEC_SECT_ADDR2_PORT 0x174
92 #define SEC_SECT_ADDR3_PORT 0x175
93 #define SEC_DRV_SEL_PORT 0x176
94 #define SEC_CMD_PORT 0x177
95 #define SEC_CTRL_PORT 0x376
96 #define SEC_ADDR_REG_PORT 0x377
99 #define PACKET_SIZE 12
103 static const char cdrom_str[] = "CD-ROM";
104 static const char harddisk_str[] = "HARDDISK";
105 static const char none_str[] = "NONE";
108 static inline const char * device_type_to_str(device_type_t type) {
122 static inline void write_features(struct channel_t * channel, uchar_t value) {
123 channel->drives[0].controller.features = value;
124 channel->drives[1].controller.features = value;
128 static inline void write_sector_count(struct channel_t * channel, uchar_t value) {
129 channel->drives[0].controller.sector_count = value;
130 channel->drives[1].controller.sector_count = value;
133 static inline void write_sector_number(struct channel_t * channel, uchar_t value) {
134 channel->drives[0].controller.sector_no = value;
135 channel->drives[1].controller.sector_no = value;
139 static inline void write_cylinder_low(struct channel_t * channel, uchar_t value) {
140 channel->drives[0].controller.cylinder_no &= 0xff00;
141 channel->drives[0].controller.cylinder_no |= value;
142 channel->drives[1].controller.cylinder_no &= 0xff00;
143 channel->drives[1].controller.cylinder_no |= value;
146 static inline void write_cylinder_high(struct channel_t * channel, uchar_t value) {
147 ushort_t val2 = value;
149 channel->drives[0].controller.cylinder_no &= 0x00ff;
150 channel->drives[0].controller.cylinder_no |= (val2 & 0xff00);
152 channel->drives[1].controller.cylinder_no &= 0x00ff;
153 channel->drives[1].controller.cylinder_no |= (val2 & 0xff00);
156 static inline void write_head_no(struct channel_t * channel, uchar_t value) {
157 channel->drives[0].controller.head_no = value;
158 channel->drives[1].controller.head_no = value;
161 static inline void write_lba_mode(struct channel_t * channel, uchar_t value) {
162 channel->drives[0].controller.lba_mode = value;
163 channel->drives[1].controller.lba_mode = value;
167 static inline uint_t get_channel_no(struct ramdisk_t * ramdisk, struct channel_t * channel) {
168 return (((uchar_t *)channel - (uchar_t *)(ramdisk->channels)) / sizeof(struct channel_t));
171 static inline uint_t get_drive_no(struct channel_t * channel, struct drive_t * drive) {
172 return (((uchar_t *)drive - (uchar_t*)(channel->drives)) / sizeof(struct drive_t));
175 static inline struct drive_t * get_selected_drive(struct channel_t * channel) {
176 return &(channel->drives[channel->drive_select]);
180 static inline int is_primary_port(struct ramdisk_t * ramdisk, ushort_t port) {
184 case PRI_FEATURES_PORT:
185 case PRI_SECT_CNT_PORT:
186 case PRI_SECT_ADDR1_PORT:
187 case PRI_SECT_ADDR2_PORT:
188 case PRI_SECT_ADDR3_PORT:
189 case PRI_DRV_SEL_PORT:
200 static inline int is_secondary_port(struct ramdisk_t * ramdisk, ushort_t port) {
204 case SEC_FEATURES_PORT:
205 case SEC_SECT_CNT_PORT:
206 case SEC_SECT_ADDR1_PORT:
207 case SEC_SECT_ADDR2_PORT:
208 case SEC_SECT_ADDR3_PORT:
209 case SEC_DRV_SEL_PORT:
218 static inline int num_drives_on_channel(struct channel_t * channel) {
219 if ((channel->drives[0].device_type == IDE_NONE) &&
220 (channel->drives[1].device_type == IDE_NONE)) {
222 } else if ((channel->drives[0].device_type != IDE_NONE) &&
223 (channel->drives[1].device_type != IDE_NONE)) {
232 static inline uchar_t extract_bits(uchar_t * buf, uint_t buf_offset, uint_t bit_offset, uint_t num_bits) {
233 uchar_t val = buf[buf_offset];
234 val = val >> bit_offset;
235 val &= ((1 << num_bits) -1);
240 static inline uchar_t get_packet_field(struct channel_t * channel, uint_t packet_offset, uint_t bit_offset, uint_t num_bits) {
241 struct drive_t * drive = get_selected_drive(channel);
242 return extract_bits(drive->controller.buffer, packet_offset, bit_offset, num_bits);
246 static inline uchar_t get_packet_byte(struct channel_t * channel, uint_t offset) {
247 struct drive_t * drive = get_selected_drive(channel);
248 return drive->controller.buffer[offset];
251 static inline uint16_t get_packet_word(struct channel_t * channel, uint_t offset) {
252 struct drive_t * drive = get_selected_drive(channel);
253 uint16_t val = drive->controller.buffer[offset];
255 val |= drive->controller.buffer[offset + 1];
260 static inline uint16_t rd_read_16bit(const uint8_t* buf) {
261 return (buf[0] << 8) | buf[1];
266 static inline uint32_t rd_read_32bit(const uint8_t* buf) {
267 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
270 ////////////////////////////////////////////////////////////////////////////
278 static void rd_init_mode_sense_single(struct vm_device * dev, struct channel_t * channel, const void * src, int size);
280 static void rd_command_aborted(struct vm_device * dev, struct channel_t * channel, unsigned value);
285 static int handle_atapi_packet_command(struct vm_device * dev,
286 struct channel_t * channel,
289 static int rd_init_send_atapi_command(struct vm_device * dev,
290 struct channel_t * channel,
291 Bit8u command, int req_length,
292 int alloc_length, bool lazy);
294 static void rd_ready_to_send_atapi(struct vm_device * dev,
295 struct channel_t * channel);
297 static void rd_atapi_cmd_error(struct vm_device * dev,
298 struct channel_t * channel,
299 sense_t sense_key, asc_t asc);
301 static void rd_atapi_cmd_nop(struct vm_device * dev, struct channel_t * channel);
302 static void rd_identify_ATAPI_drive(struct vm_device * dev, struct channel_t * channel);
309 static void rd_raise_interrupt(struct vm_device * dev, struct channel_t * channel);
310 static void rd_lower_irq(struct vm_device *dev, struct channel_t * channel);
321 static void rd_print_state(struct ramdisk_t *ramdisk);
325 ////////////////////////////////////////////////////////////////////
331 int v3_ramdisk_register_cdrom(struct vm_device * dev, uint_t busID, uint_t driveID, struct cdrom_ops* cd, void * private_data) {
332 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
333 struct channel_t * channel = &(ramdisk->channels[busID]);
334 struct drive_t * drive = &(channel->drives[driveID]);
335 struct controller_t * controller = &(drive->controller);
339 if (drive->device_type != IDE_NONE) {
340 PrintError("Device already registered at this location\n");
348 strncpy((char*)(drive->model_no), "V3VEE Ramdisk", 40);
350 while (strlen((char *)(drive->model_no)) < 40) {
351 strcat ((char*)(drive->model_no), " ");
354 PrintDebug("CDROM on target %d/%d\n", busID, driveID);
356 drive->device_type = IDE_CDROM;
357 drive->cdrom.locked = 0;
358 drive->sense.sense_key = SENSE_NONE;
359 drive->sense.asc = 0;
360 drive->sense.ascq = 0;
362 drive->private_data = private_data;
364 controller->sector_count = 0;
366 drive->cdrom.cd = cd;
368 PrintDebug("\t\tCD on ata%d-%d: '%s'\n",
372 if(drive->cdrom.cd->insert_cdrom(drive->private_data)) {
373 PrintDebug("\t\tMedia present in CD-ROM drive\n");
374 drive->cdrom.ready = 1;
375 drive->cdrom.capacity = drive->cdrom.cd->capacity(drive->private_data);
376 PrintDebug("\t\tCDROM capacity is %d\n", drive->cdrom.capacity);
378 PrintDebug("\t\tCould not locate CD-ROM, continuing with media not present\n");
379 drive->cdrom.ready = 0;
386 static Bit32u rd_init_hardware(struct ramdisk_t *ramdisk) {
389 struct channel_t *channels = (struct channel_t *)(&(ramdisk->channels));
391 PrintDebug("[rd_init_harddrive]\n");
393 for (channel_num = 0; channel_num < MAX_ATA_CHANNEL; channel_num++) {
394 memset((char *)(channels + channel_num), 0, sizeof(struct channel_t));
397 for (channel_num = 0; channel_num < MAX_ATA_CHANNEL; channel_num++){
398 struct channel_t * channel = &(channels[channel_num]);
400 channel->ioaddr1 = 0x0;
401 channel->ioaddr2 = 0x0;
404 for (device = 0; device < 2; device++){
405 struct drive_t * drive = &(channel->drives[device]);
406 struct controller_t * controller = &(drive->controller);
408 controller->status.busy = 0;
409 controller->status.drive_ready = 1;
410 controller->status.write_fault = 0;
411 controller->status.seek_complete = 1;
412 controller->status.drq = 0;
413 controller->status.corrected_data = 0;
414 controller->status.index_pulse = 0;
415 controller->status.index_pulse_count = 0;
416 controller->status.err = 0;
418 controller->error_register = 0x01; // diagnostic code: no error
419 controller->head_no = 0;
420 controller->sector_count = 1;
421 controller->sector_no = 1;
422 controller->cylinder_no = 0;
423 controller->current_command = 0x00;
424 controller->buffer_index = 0;
426 controller->control.reset = 0;
427 controller->control.disable_irq = 0;
428 controller->reset_in_progress = 0;
430 controller->sectors_per_block = 0x80;
431 controller->lba_mode = 0;
434 controller->features = 0;
437 drive->device_type = IDE_NONE;
440 strncpy((char*)(drive->model_no), "", 40);
441 while(strlen((char *)(drive->model_no)) < 40) {
442 strcat ((char*)(drive->model_no), " ");
449 rd_print_state(ramdisk);
456 static void rd_reset_harddrive(struct ramdisk_t *ramdisk, unsigned type) {
461 static void rd_close_harddrive(struct ramdisk_t *ramdisk) {
466 ////////////////////////////////////////////////////////////////////
470 static int read_data_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
471 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
472 struct channel_t * channel = NULL;
473 struct drive_t * drive = NULL;
474 struct controller_t * controller = NULL;
478 if (is_primary_port(ramdisk, port)) {
479 channel = &(ramdisk->channels[0]);
480 } else if (is_secondary_port(ramdisk, port)) {
481 channel = &(ramdisk->channels[1]);
483 PrintError("Invalid Port: %d\n", port);
487 drive = get_selected_drive(channel);
488 controller = &(drive->controller);
491 PrintTrace("[read_data_handler] IO Read at 0x%x, on drive %d/%d current cmd=0x%x\n",
493 get_channel_no(ramdisk, channel),
494 get_drive_no(channel, drive),
495 controller->current_command);
497 switch (controller->current_command) {
498 case 0xec: // IDENTIFY DEVICE
503 controller->status.busy = 0;
504 controller->status.drive_ready = 1;
505 controller->status.write_fault = 0;
506 controller->status.seek_complete = 1;
507 controller->status.corrected_data = 0;
508 controller->status.err = 0;
511 value32 = controller->buffer[index];
515 value32 |= (controller->buffer[index] << 8);
520 value32 |= (controller->buffer[index] << 16);
521 value32 |= (controller->buffer[index+1] << 24);
525 controller->buffer_index = index;
528 memcpy(dst, controller->buffer + controller->buffer_index, length);
529 controller->buffer_index += length;
531 if (controller->buffer_index >= 512) {
532 controller->status.drq = 0;
537 case 0xa0: //send packet cmd
539 uint_t index = controller->buffer_index;
542 PrintTrace("\t\tatapi.command(%02x), index(%d), cdrom.remaining_blocks(%d)\n",
543 drive->atapi.command,
545 drive->cdrom.remaining_blocks);
547 // Load block if necessary
551 PrintError("\t\tindex > 2048 : 0x%x\n", index);
555 switch (drive->atapi.command) {
556 case 0x28: // read (10)
557 case 0xa8: // read (12)
560 if (!(drive->cdrom.ready)) {
561 PrintError("\t\tRead with CDROM not ready\n");
565 drive->cdrom.cd->read_block(drive->private_data, controller->buffer,
566 drive->cdrom.next_lba);
567 drive->cdrom.next_lba++;
568 drive->cdrom.remaining_blocks--;
571 if (!(drive->cdrom.remaining_blocks)) {
572 PrintDebug("\t\tLast READ block loaded {CDROM}\n");
574 PrintDebug("\t\tREAD block loaded (%d remaining) {CDROM}\n",
575 drive->cdrom.remaining_blocks);
578 // one block transfered, start at beginning
582 default: // no need to load a new block
590 value32 = controller->buffer[index + increment];
594 value32 |= (controller->buffer[index + increment] << 8);
599 value32 |= (controller->buffer[index + increment] << 16);
600 value32 |= (controller->buffer[index + increment + 1] << 24);
604 controller->buffer_index = index + increment;
605 controller->drq_index += increment;
608 /* JRL: CHECK THAT there is enough data in the buffer to copy.... */
610 memcpy(dst, controller->buffer + index, length);
612 controller->buffer_index = index + length;
613 controller->drq_index += length;
618 if (controller->drq_index >= (unsigned)drive->atapi.drq_bytes) {
619 controller->status.drq = 0;
620 controller->drq_index = 0;
622 drive->atapi.total_bytes_remaining -= drive->atapi.drq_bytes;
624 if (drive->atapi.total_bytes_remaining > 0) {
625 // one or more blocks remaining (works only for single block commands)
627 PrintDebug("\t\tPACKET drq bytes read\n");
628 controller->interrupt_reason.i_o = 1;
629 controller->status.busy = 0;
630 controller->status.drq = 1;
631 controller->interrupt_reason.c_d = 0;
633 // set new byte count if last block
634 if (drive->atapi.total_bytes_remaining < controller->byte_count) {
635 controller->byte_count = drive->atapi.total_bytes_remaining;
637 drive->atapi.drq_bytes = controller->byte_count;
639 rd_raise_interrupt(dev, channel);
642 PrintDebug("\t\tPACKET all bytes read\n");
644 controller->interrupt_reason.i_o = 1;
645 controller->interrupt_reason.c_d = 1;
646 controller->status.drive_ready = 1;
647 controller->interrupt_reason.rel = 0;
648 controller->status.busy = 0;
649 controller->status.drq = 0;
650 controller->status.err = 0;
652 rd_raise_interrupt(dev, channel);
660 PrintError("\t\tunsupported command: %02x\n", controller->current_command);
670 static int write_data_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
671 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
672 struct channel_t * channel = NULL;
673 struct drive_t * drive = NULL;
674 struct controller_t * controller = NULL;
676 if (is_primary_port(ramdisk, port)) {
677 channel = &(ramdisk->channels[0]);
678 } else if (is_secondary_port(ramdisk, port)) {
679 channel = &(ramdisk->channels[1]);
681 PrintError("Invalid Port: %d\n", port);
685 drive = get_selected_drive(channel);
686 controller = &(drive->controller);
689 PrintDebug("[write_data_handler] IO write at 0x%x, current_cmd = 0x%02x\n",
690 port, controller->current_command);
694 //PrintDebug("[write_data_handler]\n");
695 switch (controller->current_command) {
696 case 0x30: // WRITE SECTORS
697 PrintError("\t\tneed to implement 0x30(write sector) to port 0x%x\n", port);
702 if (handle_atapi_packet_command(dev, channel, *(ushort_t *)src) == -1) {
703 PrintError("Error sending atapi packet command in PACKET write to data port\n");
710 PrintError("\t\tIO write(0x%x): current command is %02xh\n",
711 port, controller->current_command);
726 static int read_status_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
727 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
728 struct channel_t * channel = NULL;
729 struct drive_t * drive = NULL;
730 struct controller_t * controller = NULL;
735 if (is_primary_port(ramdisk, port)) {
736 channel = &(ramdisk->channels[0]);
737 } else if (is_secondary_port(ramdisk, port)) {
738 channel = &(ramdisk->channels[1]);
740 PrintError("Invalid Port: %d\n", port);
744 drive = get_selected_drive(channel);
745 controller = &(drive->controller);
748 PrintDebug("[read_status_handler] IO read at 0x%x, on drive %d/%d\n",
749 port, get_channel_no(ramdisk, channel),
750 channel->drive_select);
753 if (num_drives_on_channel(channel) == 0) {
754 PrintDebug("Setting value to zero because 0 devices on channel\n");
755 // (mch) Just return zero for these registers
756 memset(dst, 0, length);
760 (controller->status.busy << 7) |
761 (controller->status.drive_ready << 6) |
762 (controller->status.write_fault << 5) |
763 (controller->status.seek_complete << 4) |
764 (controller->status.drq << 3) |
765 (controller->status.corrected_data << 2) |
766 (controller->status.index_pulse << 1) |
767 (controller->status.err) );
770 memcpy(dst, &val, length);
772 controller->status.index_pulse_count++;
773 controller->status.index_pulse = 0;
775 if (controller->status.index_pulse_count >= INDEX_PULSE_CYCLE) {
776 controller->status.index_pulse = 1;
777 controller->status.index_pulse_count = 0;
781 if ((port == SEC_CMD_PORT) || (port == PRI_CMD_PORT)) {
782 rd_lower_irq(dev, channel);
785 PrintDebug("\t\tRead STATUS = 0x%x\n", *(uchar_t *)dst);
792 static int write_cmd_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
793 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
794 struct channel_t * channel = NULL;
795 struct drive_t * drive = NULL;
796 struct controller_t * controller = NULL;
797 uchar_t value = *(uchar_t *)src;
800 PrintError("Invalid Command port write length: %d (port=%d)\n", length, port);
804 if (is_primary_port(ramdisk, port)) {
805 channel = &(ramdisk->channels[0]);
806 } else if (is_secondary_port(ramdisk, port)) {
807 channel = &(ramdisk->channels[1]);
809 PrintError("Invalid Port: %d\n", port);
813 drive = get_selected_drive(channel);
814 controller = &(drive->controller);
817 PrintDebug("[write_command_handler] IO write at 0x%x, on drive %d/%d (val = 0x%x)\n",
818 port, get_channel_no(ramdisk, channel),
819 get_drive_no(channel, drive),
824 case 0xec: // IDENTIFY DEVICE
827 if (drive->device_type == IDE_NONE) {
828 PrintError("\t\tError: disk ata%d-%d not present, aborting\n",
829 get_channel_no(ramdisk, channel),
830 get_drive_no(channel, drive));
831 rd_command_aborted(dev, channel, value);
833 } else if (drive->device_type == IDE_CDROM) {
834 PrintDebug("Identifying CDROM...Going to abort????\n");
835 controller->head_no = 0;
836 controller->sector_count = 1;
837 controller->sector_no = 1;
838 controller->cylinder_no = 0xeb14;
839 rd_command_aborted(dev, channel, 0xec);
841 PrintError("\t\tError: Want to identify HDD!!\n");
843 SELECTED_CONTROLLER(channel).current_command = value;
844 SELECTED_CONTROLLER(channel).error_register = 0;
846 // See ATA/ATAPI-4, 8.12
847 SELECTED_CONTROLLER(channel).status.busy = 0;
848 SELECTED_CONTROLLER(channel).status.drive_ready = 1;
849 SELECTED_CONTROLLER(channel).status.write_fault = 0;
850 SELECTED_CONTROLLER(channel).status.drq = 1;
851 SELECTED_CONTROLLER(channel).status.err = 0;
853 SELECTED_CONTROLLER(channel).status.seek_complete = 1;
854 SELECTED_CONTROLLER(channel).status.corrected_data = 0;
856 SELECTED_CONTROLLER(channel).buffer_index = 0;
857 raise_interrupt(channel);
858 identify_drive(channel);
866 case 0xa1: // IDENTIFY PACKET DEVICE
868 if (drive->device_type == IDE_CDROM) {
869 controller->current_command = value;
870 controller->error_register = 0;
872 controller->status.busy = 0;
873 controller->status.drive_ready = 1;
874 controller->status.write_fault = 0;
875 controller->status.drq = 1;
876 controller->status.err = 0;
878 controller->status.seek_complete = 1;
879 controller->status.corrected_data = 0;
881 controller->buffer_index = 0;
882 rd_raise_interrupt(dev, channel);
883 rd_identify_ATAPI_drive(dev, channel);
885 PrintError("Identifying non cdrom device not supported - ata %d/%d\n",
886 get_channel_no(ramdisk, channel),
887 get_drive_no(channel, drive));
888 rd_command_aborted(dev, channel, 0xa1);
892 case 0xa0: // SEND PACKET (atapi)
894 if (drive->device_type == IDE_CDROM) {
897 if (controller->features & (1 << 0)) {
898 PrintError("\t\tPACKET-DMA not supported");
902 if (controller->features & (1 << 1)) {
903 PrintError("\t\tPACKET-overlapped not supported");
907 // We're already ready!
908 controller->sector_count = 1;
909 controller->status.busy = 0;
910 controller->status.write_fault = 0;
913 controller->status.drq = 1;
914 controller->status.err = 0;
916 // NOTE: no interrupt here
917 controller->current_command = value;
918 controller->buffer_index = 0;
920 PrintError("Sending packet to non cdrom device not supported\n");
921 rd_command_aborted (dev, channel, 0xa0);
926 PrintError("\t\tneed translate command %2x - ata %d\%d\n", value,
927 get_channel_no(ramdisk, channel),
928 get_drive_no(channel, drive));
930 /* JRL THIS NEEDS TO CHANGE */
938 static int write_ctrl_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
939 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
940 struct channel_t * channel = NULL;
941 struct drive_t * master_drive = NULL;
942 struct drive_t * slave_drive = NULL;
943 struct controller_t * controller = NULL;
944 uchar_t value = *(uchar_t *)src;
945 rd_bool prev_control_reset;
948 PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
952 if (is_primary_port(ramdisk, port)) {
953 channel = &(ramdisk->channels[0]);
954 } else if (is_secondary_port(ramdisk, port)) {
955 channel = &(ramdisk->channels[1]);
957 PrintError("Invalid Port: %d\n", port);
961 master_drive = &(channel->drives[0]);
962 slave_drive = &(channel->drives[1]);
964 controller = &(get_selected_drive(channel)->controller);
967 PrintDebug("[write_control_handler] IO write at 0x%x, on drive %d/%d (val = 0x%x)\n",
968 port, get_channel_no(ramdisk, channel),
969 channel->drive_select,
972 // (mch) Even if device 1 was selected, a write to this register
973 // goes to device 0 (if device 1 is absent)
975 prev_control_reset = controller->control.reset;
979 PrintDebug("RESET Signaled\n");
982 master_drive->controller.control.reset = value & 0x04;
983 slave_drive->controller.control.reset = value & 0x04;
985 // CGS: was: SELECTED_CONTROLLER(channel).control.disable_irq = value & 0x02;
986 master_drive->controller.control.disable_irq = value & 0x02;
987 slave_drive->controller.control.disable_irq = value & 0x02;
989 PrintDebug("\t\tadpater control reg: reset controller = %d\n",
990 (unsigned) (controller->control.reset) ? 1 : 0);
991 PrintDebug("\t\tadpater control reg: disable_irq(X) = %d\n",
992 (unsigned) (controller->control.disable_irq) ? 1 : 0);
994 if ((!prev_control_reset) && (controller->control.reset)) {
997 // transition from 0 to 1 causes all drives to reset
998 PrintDebug("\t\thard drive: RESET\n");
1000 // (mch) Set BSY, drive not ready
1001 for (id = 0; id < 2; id++) {
1002 struct controller_t * ctrl = NULL;
1005 ctrl = &(master_drive->controller);
1006 } else if (id == 1) {
1007 ctrl = &(slave_drive->controller);
1010 ctrl->status.busy = 1;
1011 ctrl->status.drive_ready = 0;
1012 ctrl->reset_in_progress = 1;
1014 ctrl->status.write_fault = 0;
1015 ctrl->status.seek_complete = 1;
1016 ctrl->status.drq = 0;
1017 ctrl->status.corrected_data = 0;
1018 ctrl->status.err = 0;
1020 ctrl->error_register = 0x01; // diagnostic code: no error
1022 ctrl->current_command = 0x00;
1023 ctrl->buffer_index = 0;
1025 ctrl->sectors_per_block = 0x80;
1028 ctrl->control.disable_irq = 0;
1031 rd_lower_irq(dev, channel);
1033 } else if ((controller->reset_in_progress) &&
1034 (!controller->control.reset)) {
1036 // Clear BSY and DRDY
1037 PrintDebug("\t\tReset complete {%s}\n", device_type_to_str(get_selected_drive(channel)->device_type));
1039 for (id = 0; id < 2; id++) {
1040 struct controller_t * ctrl = NULL;
1041 struct drive_t * drv = NULL;
1044 ctrl = &(master_drive->controller);
1046 } else if (id == 1) {
1047 ctrl = &(slave_drive->controller);
1051 ctrl->status.busy = 0;
1052 ctrl->status.drive_ready = 1;
1053 ctrl->reset_in_progress = 0;
1056 if (drv->device_type == IDE_DISK) {
1057 PrintDebug("\t\tdrive %d/%d is harddrive\n", get_channel_no(ramdisk, channel), id);
1059 ctrl->sector_count = 1;
1060 ctrl->sector_no = 1;
1061 ctrl->cylinder_no = 0;
1064 ctrl->sector_count = 1;
1065 ctrl->sector_no = 1;
1066 ctrl->cylinder_no = 0xeb14;
1071 PrintDebug("\t\ts[0].controller.control.disable_irq = %02x\n",
1072 master_drive->controller.control.disable_irq);
1073 PrintDebug("\t\ts[1].controller.control.disable_irq = %02x\n",
1074 slave_drive->controller.control.disable_irq);
1079 static int read_general_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
1080 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1081 struct channel_t * channel = NULL;
1082 struct drive_t * drive = NULL;
1083 struct controller_t * controller = NULL;
1087 PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
1091 if (is_primary_port(ramdisk, port)) {
1092 channel = &(ramdisk->channels[0]);
1093 } else if (is_secondary_port(ramdisk, port)) {
1094 channel = &(ramdisk->channels[1]);
1096 PrintError("Invalid Port: %d\n", port);
1100 drive = get_selected_drive(channel);
1101 controller = &(drive->controller);
1104 PrintDebug("[read_general_handler] IO read addr at %x, on drive %d/%d, curcmd = %02x\n",
1105 port, get_channel_no(ramdisk, channel),
1106 channel->drive_select,
1107 controller->current_command);
1111 case PRI_FEATURES_PORT:
1112 case SEC_FEATURES_PORT: // hard disk error register 0x1f1
1114 uchar_t val = (drive->device_type == IDE_NONE) ? 0 : controller->error_register;
1116 controller->status.err = 0;
1118 PrintDebug("\t\tRead FEATURES = 0x%x\n", val);
1120 *(uchar_t *)dst = val;
1126 case PRI_SECT_CNT_PORT:
1127 case SEC_SECT_CNT_PORT: // hard disk sector count / interrupt reason 0x1f2
1129 uchar_t val = (drive->device_type == IDE_NONE) ? 0 : controller->sector_count;
1130 PrintDebug("\t\tRead SECTOR COUNT = 0x%x\n", val);
1131 *(uchar_t *)dst = val;
1136 case PRI_SECT_ADDR1_PORT:
1137 case SEC_SECT_ADDR1_PORT: // sector number 0x1f3
1139 uchar_t val = (drive->device_type == IDE_NONE) ? 0 : controller->sector_no;
1141 PrintDebug("\t\tRead SECTOR ADDR1 = 0x%x\n", val);
1143 *(uchar_t *)dst = val;
1149 case PRI_SECT_ADDR2_PORT:
1150 case SEC_SECT_ADDR2_PORT: // cylinder low 0x1f4
1152 // -- WARNING : On real hardware the controller registers are shared between drives.
1153 // So we must respond even if the select device is not present. Some OS uses this fact
1154 // to detect the disks.... minix2 for example
1155 uchar_t val = (num_drives_on_channel(channel) == 0) ? 0 : (controller->cylinder_no & 0x00ff);
1157 PrintDebug("\t\tRead SECTOR ADDR2 = 0x%x\n", val);
1159 *(uchar_t *)dst = val;
1165 case PRI_SECT_ADDR3_PORT:
1166 case SEC_SECT_ADDR3_PORT: // cylinder high 0x1f5
1168 // -- WARNING : On real hardware the controller registers are shared between drives.
1169 // So we must respond even if the select device is not present. Some OS uses this fact
1170 // to detect the disks.... minix2 for example
1171 uchar_t val = (num_drives_on_channel(channel) == 0) ? 0 : (controller->cylinder_no >> 8);
1173 PrintDebug("\t\tRead SECTOR ADDR3 = 0x%x\n", val);
1175 *(uchar_t *)dst = val;
1180 case PRI_DRV_SEL_PORT:
1181 case SEC_DRV_SEL_PORT: // hard disk drive and head register 0x1f6
1183 // b7 Extended data field for ECC
1184 // b6/b5: Used to be sector size. 00=256,01=512,10=1024,11=128
1185 // Since 512 was always used, bit 6 was taken to mean LBA mode:
1186 // b6 1=LBA mode, 0=CHS mode
1190 uchar_t val = ((1 << 7) |
1191 ((controller->lba_mode > 0) << 6) |
1192 (1 << 5) | // 01b = 512 sector size
1193 (channel->drive_select << 4) |
1194 (controller->head_no << 0));
1196 PrintDebug("\t\tRead DRIVE SELECT = 0x%x\n", val);
1197 *(uchar_t *)dst = val;
1202 case PRI_ADDR_REG_PORT:
1203 case SEC_ADDR_REG_PORT: // Hard Disk Address Register 0x3f7
1205 // Obsolete and unsupported register. Not driven by hard
1206 // disk controller. Report all 1's. If floppy controller
1207 // is handling this address, it will call this function
1208 // set/clear D7 (the only bit it handles), then return
1209 // the combined value
1210 *(uchar_t *)dst = 0xff;
1215 PrintError("Invalid Port: %d\n", port);
1223 static int write_general_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
1224 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1225 struct channel_t * channel = NULL;
1226 struct drive_t * drive = NULL;
1227 struct controller_t * controller = NULL;
1228 uchar_t value = *(uchar_t *)src;
1231 PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
1235 if (is_primary_port(ramdisk, port)) {
1236 channel = &(ramdisk->channels[0]);
1237 } else if (is_secondary_port(ramdisk, port)) {
1238 channel = &(ramdisk->channels[1]);
1240 PrintError("Invalid Port: %d\n", port);
1244 drive = get_selected_drive(channel);
1245 controller = &(drive->controller);
1248 PrintDebug("[write_general_handler] IO write to port %x (val=0x%02x), channel = %d\n",
1249 port, value, get_channel_no(ramdisk, channel));
1253 case PRI_FEATURES_PORT:
1254 case SEC_FEATURES_PORT: // hard disk write precompensation 0x1f1
1256 write_features(channel, value);
1259 case PRI_SECT_CNT_PORT:
1260 case SEC_SECT_CNT_PORT: // hard disk sector count 0x1f2
1262 write_sector_count(channel, value);
1265 case PRI_SECT_ADDR1_PORT:
1266 case SEC_SECT_ADDR1_PORT: // hard disk sector number 0x1f3
1268 write_sector_number(channel, value);
1271 case PRI_SECT_ADDR2_PORT:
1272 case SEC_SECT_ADDR2_PORT: // hard disk cylinder low 0x1f4
1274 write_cylinder_low(channel, value);
1277 case PRI_SECT_ADDR3_PORT:
1278 case SEC_SECT_ADDR3_PORT: // hard disk cylinder high 0x1f5
1280 write_cylinder_high(channel, value);
1283 case PRI_DRV_SEL_PORT:
1284 case SEC_DRV_SEL_PORT: // hard disk drive and head register 0x1f6
1286 // b7 Extended data field for ECC
1287 // b6/b5: Used to be sector size. 00=256,01=512,10=1024,11=128
1288 // Since 512 was always used, bit 6 was taken to mean LBA mode:
1289 // b6 1=LBA mode, 0=CHS mode
1296 PrintDebug("\tDrive Select value=%x\n", value);
1298 if ((value & 0xa0) != 0xa0) {
1299 PrintDebug("\t\tIO write 0x%x (%02x): not 1x1xxxxxb\n", port, (unsigned) value);
1302 write_head_no(channel, value & 0xf);
1303 if ((controller->lba_mode == 0) && (((value >> 6) & 1) == 1)) {
1304 PrintDebug("\t\tenabling LBA mode\n");
1307 write_lba_mode(channel, (value >> 6) & 1);
1311 if (drive->cdrom.cd) {
1312 PrintDebug("\t\tSetting LBA on CDROM: %d\n", (value >> 6) & 1);
1313 drive->cdrom.cd->set_LBA(drive->private_data, (value >> 6) & 1);
1317 channel->drive_select = (value >> 4) & 0x01;
1318 drive = get_selected_drive(channel);
1320 if (drive->device_type == IDE_NONE) {
1321 PrintError("\t\tError: device set to %d which does not exist! channel = 0x%x\n",
1322 channel->drive_select, get_channel_no(ramdisk, channel));
1324 controller->error_register = 0x04; // aborted
1325 controller->status.err = 1;
1331 PrintError("\t\thard drive: io write to unhandled port 0x%x (value = %c)\n", port, value);
1342 static void rd_raise_interrupt(struct vm_device * dev, struct channel_t * channel) {
1343 // struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1344 struct drive_t * drive = get_selected_drive(channel);
1345 struct controller_t * controller = &(drive->controller);
1347 PrintDebug("[raise_interrupt] disable_irq = 0x%02x\n", controller->control.disable_irq);
1349 if (!(controller->control.disable_irq)) {
1351 PrintDebug("\t\tRaising interrupt %d {%s}\n\n", channel->irq, device_type_to_str(drive->device_type));
1353 v3_raise_irq(dev->vm, channel->irq);
1355 PrintDebug("\t\tRaising irq but irq is disabled\n");
1361 static void rd_lower_irq(struct vm_device *dev, struct channel_t * channel) {
1362 PrintDebug("[lower_irq] irq = %d\n", channel->irq);
1363 v3_lower_irq(dev->vm, channel->irq);
1372 //////////////////////////////////////////////////////////////////////////
1380 int handle_atapi_packet_command(struct vm_device * dev, struct channel_t * channel, ushort_t value) {
1381 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1382 struct drive_t * drive = get_selected_drive(channel);
1383 struct controller_t * controller = &(drive->controller);
1385 if (controller->buffer_index >= PACKET_SIZE) {
1386 PrintError("ATAPI packet exceeded maximum length: buffer_index (%d) >= PACKET_SIZE\n",
1387 controller->buffer_index);
1391 controller->buffer[controller->buffer_index] = value;
1392 controller->buffer[controller->buffer_index + 1] = (value >> 8);
1393 controller->buffer_index += 2;
1396 /* if packet completely writtten */
1397 if (controller->buffer_index >= PACKET_SIZE) {
1398 // complete command received
1399 Bit8u atapi_command = controller->buffer[0];
1401 PrintDebug("\t\tcdrom: ATAPI command 0x%x started\n", atapi_command);
1403 switch (atapi_command) {
1404 case 0x00: // test unit ready
1406 PrintDebug("Testing unit ready\n");
1407 if (drive->cdrom.ready) {
1408 rd_atapi_cmd_nop(dev, channel);
1410 PrintError("CDROM not ready in test unit ready\n");
1411 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1414 rd_raise_interrupt(dev, channel);
1418 case 0x03: // request sense
1420 int alloc_length = controller->buffer[4];
1422 if (rd_init_send_atapi_command(dev, channel, atapi_command, 18, alloc_length, false) == -1) {
1423 PrintError("Error sending atapi command in Request Sense\n");
1428 controller->buffer[0] = 0x70 | (1 << 7);
1429 controller->buffer[1] = 0;
1430 controller->buffer[2] = drive->sense.sense_key;
1431 controller->buffer[3] = drive->sense.information.arr[0];
1432 controller->buffer[4] = drive->sense.information.arr[1];
1433 controller->buffer[5] = drive->sense.information.arr[2];
1434 controller->buffer[6] = drive->sense.information.arr[3];
1435 controller->buffer[7] = 17 - 7;
1436 controller->buffer[8] = drive->sense.specific_inf.arr[0];
1437 controller->buffer[9] = drive->sense.specific_inf.arr[1];
1438 controller->buffer[10] = drive->sense.specific_inf.arr[2];
1439 controller->buffer[11] = drive->sense.specific_inf.arr[3];
1440 controller->buffer[12] = drive->sense.asc;
1441 controller->buffer[13] = drive->sense.ascq;
1442 controller->buffer[14] = drive->sense.fruc;
1443 controller->buffer[15] = drive->sense.key_spec.arr[0];
1444 controller->buffer[16] = drive->sense.key_spec.arr[1];
1445 controller->buffer[17] = drive->sense.key_spec.arr[2];
1447 rd_ready_to_send_atapi(dev, channel);
1450 case 0x1b: // start stop unit
1452 //bx_bool Immed = (controller->buffer[1] >> 0) & 1;
1453 rd_bool LoEj = (controller->buffer[4] >> 1) & 1;
1454 rd_bool Start = (controller->buffer[4] >> 0) & 1;
1457 if ((!LoEj) && (!Start)) {
1458 PrintError("FIXME: Stop disc not implemented\n");
1460 rd_atapi_cmd_nop(dev, channel);
1461 rd_raise_interrupt(dev, channel);
1463 } else if (!LoEj && Start) { // start (spin up) the disc
1465 drive->cdrom.cd->start_cdrom(drive->private_data);
1467 PrintError("FIXME: ATAPI start disc not reading TOC\n");
1468 rd_atapi_cmd_nop(dev, channel);
1469 rd_raise_interrupt(dev, channel);
1471 } else if (LoEj && !Start) { // Eject the disc
1472 rd_atapi_cmd_nop(dev, channel);
1473 PrintDebug("Ejecting Disk\n");
1474 if (drive->cdrom.ready) {
1476 drive->cdrom.cd->eject_cdrom(drive->private_data);
1478 drive->cdrom.ready = 0;
1479 //bx_options.atadevice[channel][SLAVE_SELECTED(channel)].Ostatus->set(EJECTED);
1480 //bx_gui->update_drive_status_buttons();
1482 rd_raise_interrupt(dev, channel);
1484 } else { // Load the disc
1485 // My guess is that this command only closes the tray, that's a no-op for us
1486 rd_atapi_cmd_nop(dev, channel);
1487 rd_raise_interrupt(dev, channel);
1491 case 0xbd: // mechanism status
1493 uint16_t alloc_length = rd_read_16bit(controller->buffer + 8);
1495 if (alloc_length == 0) {
1496 PrintError("Zero allocation length to MECHANISM STATUS not impl.\n");
1500 if (rd_init_send_atapi_command(dev, channel, atapi_command, 8, alloc_length, false) == -1) {
1501 PrintError("Error sending atapi command in mechanism status\n");
1505 controller->buffer[0] = 0; // reserved for non changers
1506 controller->buffer[1] = 0; // reserved for non changers
1508 controller->buffer[2] = 0; // Current LBA (TODO!)
1509 controller->buffer[3] = 0; // Current LBA (TODO!)
1510 controller->buffer[4] = 0; // Current LBA (TODO!)
1512 controller->buffer[5] = 1; // one slot
1514 controller->buffer[6] = 0; // slot table length
1515 controller->buffer[7] = 0; // slot table length
1517 rd_ready_to_send_atapi(dev, channel);
1520 case 0x5a: // mode sense
1522 uint16_t alloc_length = rd_read_16bit(controller->buffer + 7);
1524 Bit8u PC = controller->buffer[2] >> 6;
1525 Bit8u PageCode = controller->buffer[2] & 0x3f;
1528 case 0x0: // current values
1531 case 0x01: // error recovery
1534 if (rd_init_send_atapi_command(dev, channel, atapi_command, sizeof(struct error_recovery_t) + 8, alloc_length, false) == -1) {
1535 PrintError("Error sending atapi command in mode sense error recovery\n");
1539 rd_init_mode_sense_single(dev, channel, &(drive->cdrom.current.error_recovery),
1540 sizeof(struct error_recovery_t));
1541 rd_ready_to_send_atapi(dev, channel);
1544 case 0x2a: // CD-ROM capabilities & mech. status
1547 if (rd_init_send_atapi_command(dev, channel, atapi_command, 28, alloc_length, false) == -1) {
1548 PrintError("Error sending atapi command in CDROM caps/mech mode-sense\n");
1552 rd_init_mode_sense_single(dev, channel, &(controller->buffer[8]), 28);
1554 controller->buffer[8] = 0x2a;
1555 controller->buffer[9] = 0x12;
1556 controller->buffer[10] = 0x00;
1557 controller->buffer[11] = 0x00;
1558 // Multisession, Mode 2 Form 2, Mode 2 Form 1
1559 controller->buffer[12] = 0x70;
1560 controller->buffer[13] = (3 << 5);
1561 controller->buffer[14] = (unsigned char) (1 |
1562 (drive->cdrom.locked ? (1 << 1) : 0) |
1565 controller->buffer[15] = 0x00;
1566 controller->buffer[16] = (706 >> 8) & 0xff;
1567 controller->buffer[17] = 706 & 0xff;
1568 controller->buffer[18] = 0;
1569 controller->buffer[19] = 2;
1570 controller->buffer[20] = (512 >> 8) & 0xff;
1571 controller->buffer[21] = 512 & 0xff;
1572 controller->buffer[22] = (706 >> 8) & 0xff;
1573 controller->buffer[23] = 706 & 0xff;
1574 controller->buffer[24] = 0;
1575 controller->buffer[25] = 0;
1576 controller->buffer[26] = 0;
1577 controller->buffer[27] = 0;
1578 rd_ready_to_send_atapi(dev, channel);
1581 case 0x0d: // CD-ROM
1582 case 0x0e: // CD-ROM audio control
1585 PrintError("Ramdisk: cdrom: MODE SENSE (curr), code=%x not implemented yet\n",
1587 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1588 ASC_INV_FIELD_IN_CMD_PACKET);
1589 rd_raise_interrupt(dev, channel);
1594 // not implemeted by this device
1595 PrintError("\t\tcdrom: MODE SENSE PC=%x, PageCode=%x, not implemented by device\n",
1597 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1598 ASC_INV_FIELD_IN_CMD_PACKET);
1599 rd_raise_interrupt(dev, channel);
1605 case 0x1: // changeable values
1608 case 0x01: // error recovery
1609 case 0x0d: // CD-ROM
1610 case 0x0e: // CD-ROM audio control
1611 case 0x2a: // CD-ROM capabilities & mech. status
1614 PrintError("cdrom: MODE SENSE (chg), code=%x not implemented yet\n",
1616 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1617 ASC_INV_FIELD_IN_CMD_PACKET);
1618 rd_raise_interrupt(dev, channel);
1623 // not implemeted by this device
1624 PrintError("Changeable values of mode sense not supported by cdrom\n");
1625 PrintDebug("\t\tcdrom: MODE SENSE PC=%x, PageCode=%x, not implemented by device\n",
1627 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1628 ASC_INV_FIELD_IN_CMD_PACKET);
1629 rd_raise_interrupt(dev, channel);
1635 case 0x2: // default values
1638 case 0x01: // error recovery
1639 case 0x0d: // CD-ROM
1640 case 0x0e: // CD-ROM audio control
1641 case 0x2a: // CD-ROM capabilities & mech. status
1643 PrintError("Default values of mode sense not supported by cdrom\n");
1644 PrintDebug("cdrom: MODE SENSE (dflt), code=%x\n",
1650 PrintError("Default values of mode sense not implemented in cdrom\n");
1651 // not implemeted by this device
1652 PrintDebug("cdrom: MODE SENSE PC=%x, PageCode=%x, not implemented by device\n",
1654 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1655 ASC_INV_FIELD_IN_CMD_PACKET);
1656 rd_raise_interrupt(dev, channel);
1662 case 0x3: // saved values not implemented
1664 PrintError("\t\tSaved values not implemented in mode sense\n");
1665 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
1666 rd_raise_interrupt(dev, channel);
1671 PrintError("Unsupported Mode sense value\n");
1678 case 0x12: // inquiry
1680 uint8_t alloc_length = controller->buffer[4];
1682 if (rd_init_send_atapi_command(dev, channel, atapi_command, 36, alloc_length, false) == -1) {
1683 PrintError("Error sending atapi command in inquiry\n");
1687 controller->buffer[0] = 0x05; // CD-ROM
1688 controller->buffer[1] = 0x80; // Removable
1689 controller->buffer[2] = 0x00; // ISO, ECMA, ANSI version
1690 controller->buffer[3] = 0x21; // ATAPI-2, as specified
1691 controller->buffer[4] = 31; // additional length (total 36)
1692 controller->buffer[5] = 0x00; // reserved
1693 controller->buffer[6] = 0x00; // reserved
1694 controller->buffer[7] = 0x00; // reserved
1697 const char* vendor_id = "VTAB ";
1699 for (i = 0; i < 8; i++) {
1700 controller->buffer[8+i] = vendor_id[i];
1704 const char* product_id = "Turbo CD-ROM ";
1705 for (i = 0; i < 16; i++) {
1706 controller->buffer[16+i] = product_id[i];
1709 // Product Revision level
1710 const char* rev_level = "1.0 ";
1711 for (i = 0; i < 4; i++) {
1712 controller->buffer[32 + i] = rev_level[i];
1715 rd_ready_to_send_atapi(dev, channel);
1718 case 0x25: // read cd-rom capacity
1720 // no allocation length???
1721 if (rd_init_send_atapi_command(dev, channel, atapi_command, 8, 8, false) == -1) {
1722 PrintError("Error sending atapi command in read cdrom capacity\n");
1726 if (drive->cdrom.ready) {
1727 uint32_t capacity = drive->cdrom.capacity;
1729 PrintDebug("\t\tCapacity is %d sectors (%d bytes)\n", capacity, capacity * 2048);
1731 controller->buffer[0] = (capacity >> 24) & 0xff;
1732 controller->buffer[1] = (capacity >> 16) & 0xff;
1733 controller->buffer[2] = (capacity >> 8) & 0xff;
1734 controller->buffer[3] = (capacity >> 0) & 0xff;
1735 controller->buffer[4] = (2048 >> 24) & 0xff;
1736 controller->buffer[5] = (2048 >> 16) & 0xff;
1737 controller->buffer[6] = (2048 >> 8) & 0xff;
1738 controller->buffer[7] = (2048 >> 0) & 0xff;
1740 rd_ready_to_send_atapi(dev, channel);
1742 PrintError("CDROM not ready in read cdrom capacity\n");
1743 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1744 rd_raise_interrupt(dev, channel);
1750 case 0xbe: // read cd
1752 if (drive->cdrom.ready) {
1753 PrintError("Read CD with CD present not implemented\n");
1754 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1755 rd_raise_interrupt(dev, channel);
1757 PrintError("Drive not ready in read cd with CD present\n");
1758 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1759 rd_raise_interrupt(dev, channel);
1763 case 0x43: // read toc
1765 if (drive->cdrom.ready) {
1767 bool msf = (controller->buffer[1] >> 1) & 1;
1768 uint8_t starting_track = controller->buffer[6];
1770 uint16_t alloc_length = rd_read_16bit(controller->buffer + 7);
1772 uint8_t format = (controller->buffer[9] >> 6);
1775 PrintDebug("Reading CDROM TOC: Format=%d (byte count=%d) (toc length:%d)\n",
1776 format, controller->byte_count, toc_length);
1780 if (!(drive->cdrom.cd->read_toc(drive->private_data, controller->buffer,
1781 &toc_length, msf, starting_track))) {
1782 PrintError("CDROM: Reading Table of Contents Failed\n");
1783 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1784 ASC_INV_FIELD_IN_CMD_PACKET);
1785 rd_raise_interrupt(dev, channel);
1790 if (rd_init_send_atapi_command(dev, channel, atapi_command, toc_length, alloc_length, false) == -1) {
1791 PrintError("Failed to init send atapi command in read toc (fmt=%d)\n", format);
1795 rd_ready_to_send_atapi(dev, channel);
1800 // multi session stuff. we ignore this and emulate a single session only
1802 if (rd_init_send_atapi_command(dev, channel, atapi_command, 12, alloc_length, false) == -1) {
1803 PrintError("Failed to init send atapi command in read toc (fmt=%d)\n", format);
1807 controller->buffer[0] = 0;
1808 controller->buffer[1] = 0x0a;
1809 controller->buffer[2] = 1;
1810 controller->buffer[3] = 1;
1812 for (i = 0; i < 8; i++) {
1813 controller->buffer[4 + i] = 0;
1816 rd_ready_to_send_atapi(dev, channel);
1821 PrintError("(READ TOC) Format %d not supported\n", format);
1825 PrintError("CDROM not ready in read toc\n");
1826 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1827 rd_raise_interrupt(dev, channel);
1831 case 0x28: // read (10)
1832 case 0xa8: // read (12)
1835 uint32_t transfer_length;
1836 if (atapi_command == 0x28) {
1837 transfer_length = rd_read_16bit(controller->buffer + 7);
1839 transfer_length = rd_read_32bit(controller->buffer + 6);
1842 uint32_t lba = rd_read_32bit(controller->buffer + 2);
1844 if (!(drive->cdrom.ready)) {
1845 PrintError("CDROM Error: Not Ready (ATA%d/%d)\n",
1846 get_channel_no(ramdisk, channel), get_drive_no(channel, drive));
1847 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1848 rd_raise_interrupt(dev, channel);
1852 if (transfer_length == 0) {
1853 PrintError("READ(%d) with transfer length 0, ok\n",
1854 (atapi_command == 0x28) ? 10 : 12);
1855 rd_atapi_cmd_nop(dev, channel);
1856 rd_raise_interrupt(dev, channel);
1860 if (lba + transfer_length > drive->cdrom.capacity) {
1861 PrintError("CDROM Error: Capacity exceeded [capacity=%d] (ATA%d/%d)\n",
1862 drive->cdrom.capacity,
1863 get_channel_no(ramdisk, channel), get_drive_no(channel, drive));
1864 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
1865 rd_raise_interrupt(dev, channel);
1869 PrintDebug("\t\tcdrom: READ (%d) LBA=%d LEN=%d\n",
1870 (atapi_command == 0x28) ? 10 : 12,
1871 lba, transfer_length);
1874 if (rd_init_send_atapi_command(dev, channel, atapi_command, transfer_length * 2048,
1875 transfer_length * 2048, true) == -1) {
1876 PrintError("CDROM Error: Atapi command send error\n");
1880 drive->cdrom.remaining_blocks = transfer_length;
1881 drive->cdrom.next_lba = lba;
1882 rd_ready_to_send_atapi(dev, channel);
1887 uint32_t lba = rd_read_32bit(controller->buffer + 2);
1889 if (!(drive->cdrom.ready)) {
1890 PrintError("CDROM not ready in seek\n");
1891 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1892 rd_raise_interrupt(dev, channel);
1896 if (lba > drive->cdrom.capacity) {
1897 PrintError("LBA is greater than CDROM capacity in seek\n");
1898 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
1899 rd_raise_interrupt(dev, channel);
1903 PrintError("\t\tcdrom: SEEK (ignored)\n");
1905 rd_atapi_cmd_nop(dev, channel);
1906 rd_raise_interrupt(dev, channel);
1910 case 0x1e: // prevent/allow medium removal
1913 if (drive->cdrom.ready) {
1914 drive->cdrom.locked = controller->buffer[4] & 1;
1915 rd_atapi_cmd_nop(dev, channel);
1917 PrintError("CD not ready in prevent/allow medium removal\n");
1918 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1921 rd_raise_interrupt(dev, channel);
1925 case 0x42: // read sub-channel
1927 //bool msf = get_packet_field(channel, 1, 1, 1);
1928 bool sub_q = get_packet_field(channel, 2, 6, 1);
1929 //uint8_t data_format = get_packet_byte(channel, 3);
1930 //uint8_t track_number = get_packet_byte(channel, 6);
1931 uint16_t alloc_length = get_packet_word(channel, 7);
1936 UNUSED(data_format);
1937 UNUSED(track_number);
1939 if (!(drive->cdrom.ready)) {
1940 PrintError("CDROM not ready in read sub-channel\n");
1941 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1942 rd_raise_interrupt(dev, channel);
1944 controller->buffer[0] = 0;
1945 controller->buffer[1] = 0; // audio not supported
1946 controller->buffer[2] = 0;
1947 controller->buffer[3] = 0;
1949 int ret_len = 4; // header size
1951 if (sub_q) { // !sub_q == header only
1952 PrintError("Read sub-channel with SubQ not implemented\n");
1953 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1954 ASC_INV_FIELD_IN_CMD_PACKET);
1955 rd_raise_interrupt(dev, channel);
1958 if (rd_init_send_atapi_command(dev, channel, atapi_command, ret_len, alloc_length, false) == -1) {
1959 PrintError("Error sending atapi command in read sub-channel\n");
1962 rd_ready_to_send_atapi(dev, channel);
1966 case 0x51: // read disc info
1968 // no-op to keep the Linux CD-ROM driver happy
1969 PrintError("Error: Read disk info no-op to keep the Linux CD-ROM driver happy\n");
1970 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1971 rd_raise_interrupt(dev, channel);
1974 case 0x55: // mode select
1975 case 0xa6: // load/unload cd
1976 case 0x4b: // pause/resume
1977 case 0x45: // play audio
1978 case 0x47: // play audio msf
1979 case 0xbc: // play cd
1980 case 0xb9: // read cd msf
1981 case 0x44: // read header
1983 case 0xbb: // set cd speed
1984 case 0x4e: // stop play/scan
1987 PrintError("ATAPI command 0x%x not implemented yet\n",
1989 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1990 rd_raise_interrupt(dev, channel);
1993 PrintError("Unknown ATAPI command 0x%x (%d)\n",
1994 atapi_command, atapi_command);
1995 // We'd better signal the error if the user chose to continue
1996 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1997 rd_raise_interrupt(dev, channel);
2009 int rd_init_send_atapi_command(struct vm_device * dev, struct channel_t * channel, Bit8u command, int req_length, int alloc_length, bool lazy)
2011 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2012 struct controller_t * controller = &(drive->controller);
2014 // controller->byte_count is a union of controller->cylinder_no;
2015 // lazy is used to force a data read in the buffer at the next read.
2017 PrintDebug("[rd_init_send_atapi_cmd]\n");
2019 if (controller->byte_count == 0xffff) {
2020 controller->byte_count = 0xfffe;
2023 if ((controller->byte_count & 1) &&
2024 !(alloc_length <= controller->byte_count)) {
2026 PrintDebug("\t\tOdd byte count (0x%04x) to ATAPI command 0x%02x, using 0x%x\n",
2027 controller->byte_count,
2029 controller->byte_count - 1);
2031 controller->byte_count -= 1;
2034 if (controller->byte_count == 0) {
2035 PrintError("\t\tATAPI command with zero byte count\n");
2039 if (alloc_length < 0) {
2040 PrintError("\t\tAllocation length < 0\n");
2044 if (alloc_length == 0) {
2045 alloc_length = controller->byte_count;
2048 controller->interrupt_reason.i_o = 1;
2049 controller->interrupt_reason.c_d = 0;
2050 controller->status.busy = 0;
2051 controller->status.drq = 1;
2052 controller->status.err = 0;
2054 // no bytes transfered yet
2056 controller->buffer_index = 2048;
2058 controller->buffer_index = 0;
2061 controller->drq_index = 0;
2063 if (controller->byte_count > req_length) {
2064 controller->byte_count = req_length;
2067 if (controller->byte_count > alloc_length) {
2068 controller->byte_count = alloc_length;
2071 drive->atapi.command = command;
2072 drive->atapi.drq_bytes = controller->byte_count;
2073 drive->atapi.total_bytes_remaining = (req_length < alloc_length) ? req_length : alloc_length;
2076 // // bias drq_bytes and total_bytes_remaining
2077 // SELECTED_DRIVE(channel).atapi.drq_bytes += 2048;
2078 // SELECTED_DRIVE(channel).atapi.total_bytes_remaining += 2048;
2086 void rd_ready_to_send_atapi(struct vm_device * dev, struct channel_t * channel) {
2087 PrintDebug("[rd_ready_to_send_atapi]\n");
2089 rd_raise_interrupt(dev, channel);
2096 void rd_atapi_cmd_error(struct vm_device * dev, struct channel_t * channel, sense_t sense_key, asc_t asc)
2098 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2099 struct controller_t * controller = &(drive->controller);
2102 struct ramdisk_t *ramdisk = (struct ramdisk_t *)(dev->private_data);
2103 PrintError("[rd_atapi_cmd_error]\n");
2104 PrintError("Error: atapi_cmd_error channel=%02x key=%02x asc=%02x\n",
2105 get_channel_no(ramdisk, channel), sense_key, asc);
2108 controller->error_register = sense_key << 4;
2109 controller->interrupt_reason.i_o = 1;
2110 controller->interrupt_reason.c_d = 1;
2111 controller->interrupt_reason.rel = 0;
2112 controller->status.busy = 0;
2113 controller->status.drive_ready = 1;
2114 controller->status.write_fault = 0;
2115 controller->status.drq = 0;
2116 controller->status.err = 1;
2118 drive->sense.sense_key = sense_key;
2119 drive->sense.asc = asc;
2120 drive->sense.ascq = 0;
2125 void rd_atapi_cmd_nop(struct vm_device * dev, struct channel_t * channel)
2127 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2128 struct controller_t * controller = &(drive->controller);
2130 PrintDebug("[rd_atapi_cmd_nop]\n");
2131 controller->interrupt_reason.i_o = 1;
2132 controller->interrupt_reason.c_d = 1;
2133 controller->interrupt_reason.rel = 0;
2134 controller->status.busy = 0;
2135 controller->status.drive_ready = 1;
2136 controller->status.drq = 0;
2137 controller->status.err = 0;
2143 void rd_identify_ATAPI_drive(struct vm_device * dev, struct channel_t * channel)
2145 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2146 struct controller_t * controller = &(drive->controller);
2150 const char* serial_number = " VT00001\0\0\0\0\0\0\0\0\0\0\0\0";
2151 const char* firmware = "ALPHA1 ";
2153 drive->id_drive[0] = (2 << 14) | (5 << 8) | (1 << 7) | (2 << 5) | (0 << 0); // Removable CDROM, 50us response, 12 byte packets
2155 for (i = 1; i <= 9; i++) {
2156 drive->id_drive[i] = 0;
2159 for (i = 0; i < 10; i++) {
2160 drive->id_drive[10 + i] = ((serial_number[i * 2] << 8) |
2161 (serial_number[(i * 2) + 1]));
2164 for (i = 20; i <= 22; i++) {
2165 drive->id_drive[i] = 0;
2168 for (i = 0; i < strlen(firmware) / 2; i++) {
2169 drive->id_drive[23 + i] = ((firmware[i * 2] << 8) |
2170 (firmware[(i * 2) + 1]));
2172 V3_ASSERT((23 + i) == 27);
2174 for (i = 0; i < strlen((char *)(drive->model_no)) / 2; i++) {
2175 drive->id_drive[27 + i] = ((drive->model_no[i * 2] << 8) |
2176 (drive->model_no[(i * 2) + 1]));
2179 V3_ASSERT((27 + i) == 47);
2181 drive->id_drive[47] = 0;
2182 drive->id_drive[48] = 1; // 32 bits access
2184 drive->id_drive[49] = (1 << 9); // LBA supported
2186 drive->id_drive[50] = 0;
2187 drive->id_drive[51] = 0;
2188 drive->id_drive[52] = 0;
2190 drive->id_drive[53] = 3; // words 64-70, 54-58 valid
2192 for (i = 54; i <= 62; i++) {
2193 drive->id_drive[i] = 0;
2196 // copied from CFA540A
2197 drive->id_drive[63] = 0x0103; // variable (DMA stuff)
2198 drive->id_drive[64] = 0x0001; // PIO
2199 drive->id_drive[65] = 0x00b4;
2200 drive->id_drive[66] = 0x00b4;
2201 drive->id_drive[67] = 0x012c;
2202 drive->id_drive[68] = 0x00b4;
2204 drive->id_drive[69] = 0;
2205 drive->id_drive[70] = 0;
2206 drive->id_drive[71] = 30; // faked
2207 drive->id_drive[72] = 30; // faked
2208 drive->id_drive[73] = 0;
2209 drive->id_drive[74] = 0;
2211 drive->id_drive[75] = 0;
2213 for (i = 76; i <= 79; i++) {
2214 drive->id_drive[i] = 0;
2217 drive->id_drive[80] = 0x1e; // supports up to ATA/ATAPI-4
2218 drive->id_drive[81] = 0;
2219 drive->id_drive[82] = 0;
2220 drive->id_drive[83] = 0;
2221 drive->id_drive[84] = 0;
2222 drive->id_drive[85] = 0;
2223 drive->id_drive[86] = 0;
2224 drive->id_drive[87] = 0;
2225 drive->id_drive[88] = 0;
2227 for (i = 89; i <= 126; i++) {
2228 drive->id_drive[i] = 0;
2231 drive->id_drive[127] = 0;
2232 drive->id_drive[128] = 0;
2234 for (i = 129; i <= 159; i++) {
2235 drive->id_drive[i] = 0;
2238 for (i = 160; i <= 255; i++) {
2239 drive->id_drive[i] = 0;
2242 // now convert the id_drive array (native 256 word format) to
2243 // the controller buffer (512 bytes)
2245 for (i = 0; i <= 255; i++) {
2246 temp16 = drive->id_drive[i];
2247 controller->buffer[i * 2] = temp16 & 0x00ff;
2248 controller->buffer[i * 2 + 1] = temp16 >> 8;
2261 void rd_init_mode_sense_single(struct vm_device * dev,
2262 struct channel_t * channel, const void* src, int size)
2264 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2265 struct controller_t * controller = &(drive->controller);
2267 PrintDebug("[rd_init_mode_sense_single]\n");
2270 controller->buffer[0] = (size + 6) >> 8;
2271 controller->buffer[1] = (size + 6) & 0xff;
2272 controller->buffer[2] = 0x70; // no media present
2273 controller->buffer[3] = 0; // reserved
2274 controller->buffer[4] = 0; // reserved
2275 controller->buffer[5] = 0; // reserved
2276 controller->buffer[6] = 0; // reserved
2277 controller->buffer[7] = 0; // reserved
2280 memcpy(controller->buffer + 8, src, size);
2285 static void rd_command_aborted(struct vm_device * dev,
2286 struct channel_t * channel, unsigned value) {
2287 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2288 struct controller_t * controller = &(drive->controller);
2290 PrintError("[rd_command_aborted]\n");
2291 PrintError("\t\taborting on command 0x%02x {%s}\n", value, device_type_to_str(drive->device_type));
2293 controller->current_command = 0;
2294 controller->status.busy = 0;
2295 controller->status.drive_ready = 1;
2296 controller->status.err = 1;
2297 controller->error_register = 0x04; // command ABORTED
2298 controller->status.drq = 0;
2299 controller->status.seek_complete = 0;
2300 controller->status.corrected_data = 0;
2301 controller->buffer_index = 0;
2303 rd_raise_interrupt(dev, channel);
2307 static int ramdisk_init_device(struct vm_device *dev) {
2308 struct ramdisk_t *ramdisk= (struct ramdisk_t *)dev->private_data;
2310 PrintDebug("Initializing Ramdisk\n");
2313 rd_init_hardware(ramdisk);
2316 v3_dev_hook_io(dev, PRI_CTRL_PORT,
2317 &read_status_port, &write_ctrl_port);
2319 v3_dev_hook_io(dev, PRI_DATA_PORT,
2320 &read_data_port, &write_data_port);
2321 v3_dev_hook_io(dev, PRI_FEATURES_PORT,
2322 &read_general_port, &write_general_port);
2323 v3_dev_hook_io(dev, PRI_SECT_CNT_PORT,
2324 &read_general_port, &write_general_port);
2325 v3_dev_hook_io(dev, PRI_SECT_ADDR1_PORT,
2326 &read_general_port, &write_general_port);
2327 v3_dev_hook_io(dev, PRI_SECT_ADDR2_PORT,
2328 &read_general_port, &write_general_port);
2329 v3_dev_hook_io(dev, PRI_SECT_ADDR3_PORT,
2330 &read_general_port, &write_general_port);
2331 v3_dev_hook_io(dev, PRI_DRV_SEL_PORT,
2332 &read_general_port, &write_general_port);
2333 v3_dev_hook_io(dev, PRI_CMD_PORT,
2334 &read_status_port, &write_cmd_port);
2337 v3_dev_hook_io(dev, SEC_CTRL_PORT,
2338 &read_status_port, &write_ctrl_port);
2340 v3_dev_hook_io(dev, SEC_DATA_PORT,
2341 &read_data_port, &write_data_port);
2342 v3_dev_hook_io(dev, SEC_FEATURES_PORT,
2343 &read_general_port, &write_general_port);
2344 v3_dev_hook_io(dev, SEC_SECT_CNT_PORT,
2345 &read_general_port, &write_general_port);
2346 v3_dev_hook_io(dev, SEC_SECT_ADDR1_PORT,
2347 &read_general_port, &write_general_port);
2348 v3_dev_hook_io(dev, SEC_SECT_ADDR2_PORT,
2349 &read_general_port, &write_general_port);
2350 v3_dev_hook_io(dev, SEC_SECT_ADDR3_PORT,
2351 &read_general_port, &write_general_port);
2352 v3_dev_hook_io(dev, SEC_DRV_SEL_PORT,
2353 &read_general_port, &write_general_port);
2354 v3_dev_hook_io(dev, SEC_CMD_PORT,
2355 &read_status_port, &write_cmd_port);
2359 v3_dev_hook_io(dev, SEC_ADDR_REG_PORT,
2360 &read_general_port, &write_general_port);
2362 v3_dev_hook_io(dev, PRI_ADDR_REG_PORT,
2363 &read_general_port, &write_general_port);
2372 static int ramdisk_deinit_device(struct vm_device *dev) {
2373 struct ramdisk_t *ramdisk = (struct ramdisk_t *)(dev->private_data);
2374 rd_close_harddrive(ramdisk);
2378 static struct vm_device_ops dev_ops = {
2379 .init = ramdisk_init_device,
2380 .deinit = ramdisk_deinit_device,
2389 struct vm_device * v3_create_ramdisk()
2392 struct ramdisk_t *ramdisk;
2393 ramdisk = (struct ramdisk_t *)V3_Malloc(sizeof(struct ramdisk_t));
2394 V3_ASSERT(ramdisk != NULL);
2396 PrintDebug("[create_ramdisk]\n");
2398 struct vm_device * device = v3_create_device("RAMDISK", &dev_ops, ramdisk);
2406 #ifdef DEBUG_RAMDISK
2408 static void rd_print_state(struct ramdisk_t * ramdisk) {
2411 struct channel_t * channels = (struct channel_t *)(&(ramdisk->channels));
2414 for (channel = 0; channel < MAX_ATA_CHANNEL; channel++) {
2415 memset((char *)(channels + channel), 0, sizeof(struct channel_t));
2418 PrintDebug("sizeof(*channels) = %d\n", sizeof(*channels));
2419 PrintDebug("sizeof(channles->drives[0].controller) = %d\n", sizeof((channels->drives[0].controller)));
2420 PrintDebug("sizeof(channles->drives[0].cdrom) = %d\n", sizeof((channels->drives[0].cdrom)));
2421 PrintDebug("sizeof(channles->drives[0].sense) = %d\n", sizeof((channels->drives[0].sense)));
2422 PrintDebug("sizeof(channles->drives[0].atapi) = %d\n", sizeof((channels->drives[0].atapi)));
2425 PrintDebug("sizeof(channles->drives[0].controller.status) = %d\n",
2426 sizeof((channels->drives[0].controller.status)));
2427 PrintDebug("sizeof(channles->drives[0].controller.sector_count) = %d\n",
2428 sizeof((channels->drives[0].controller.sector_count)));
2429 PrintDebug("sizeof(channles->drives[0].controller.interrupt_reason) = %d\n",
2430 sizeof((channels->drives[0].controller.interrupt_reason)));
2432 PrintDebug("sizeof(channles->drives[0].controller.cylinder_no) = %d\n",
2433 sizeof((channels->drives[0].controller.cylinder_no)));
2434 PrintDebug("sizeof(channles->drives[0].controller.byte_count) = %d\n",
2435 sizeof((channels->drives[0].controller.byte_count)));
2438 PrintDebug("sizeof(channles->drives[0].controller.control) = %d\n",
2439 sizeof((channels->drives[0].controller.control)));
2442 for (channel = 0; channel < MAX_ATA_CHANNEL; channel++){
2444 for (device = 0; device < 2; device++){
2446 // Initialize controller state, even if device is not present
2447 PrintDebug("channels[%d].drives[%d].controller.status.busy = %d\n",
2449 channels[channel].drives[device].controller.status.busy);
2450 PrintDebug("channels[%d].drives[%d].controller.status.drive_ready = %d\n",
2452 channels[channel].drives[device].controller.status.drive_ready);
2453 PrintDebug("channels[%d].drives[%d].controller.status.write_fault = %d\n",
2455 channels[channel].drives[device].controller.status.write_fault);
2456 PrintDebug("channels[%d].drives[%d].controller.status.seek_complete = %d\n",
2458 channels[channel].drives[device].controller.status.seek_complete);
2459 PrintDebug("channels[%d].drives[%d].controller.status.drq = %d\n",
2461 channels[channel].drives[device].controller.status.drq);
2462 PrintDebug("channels[%d].drives[%d].controller.status.corrected_data = %d\n",
2464 channels[channel].drives[device].controller.status.corrected_data);
2465 PrintDebug("channels[%d].drives[%d].controller.status.index_pulse = %d\n",
2467 channels[channel].drives[device].controller.status.index_pulse);
2468 PrintDebug("channels[%d].drives[%d].controller.status.index_pulse_count = %d\n",
2470 channels[channel].drives[device].controller.status.index_pulse_count);
2471 PrintDebug("channels[%d].drives[%d].controller.status.err = %d\n",
2473 channels[channel].drives[device].controller.status.err);
2476 PrintDebug("channels[%d].drives[%d].controller.error_register = %d\n",
2478 channels[channel].drives[device].controller.error_register);
2479 PrintDebug("channels[%d].drives[%d].controller.head_no = %d\n",
2481 channels[channel].drives[device].controller.head_no);
2482 PrintDebug("channels[%d].drives[%d].controller.sector_count = %d\n",
2484 channels[channel].drives[device].controller.sector_count);
2485 PrintDebug("channels[%d].drives[%d].controller.sector_no = %d\n",
2487 channels[channel].drives[device].controller.sector_no);
2488 PrintDebug("channels[%d].drives[%d].controller.cylinder_no = %d\n",
2490 channels[channel].drives[device].controller.cylinder_no);
2491 PrintDebug("channels[%d].drives[%d].controller.current_command = %02x\n",
2493 channels[channel].drives[device].controller.current_command);
2494 PrintDebug("channels[%d].drives[%d].controller.buffer_index = %d\n",
2496 channels[channel].drives[device].controller.buffer_index);
2499 PrintDebug("channels[%d].drives[%d].controller.control.reset = %d\n",
2501 channels[channel].drives[device].controller.control.reset);
2502 PrintDebug("channels[%d].drives[%d].controller.control.disable_irq = %d\n",
2504 channels[channel].drives[device].controller.control.disable_irq);
2507 PrintDebug("channels[%d].drives[%d].controller.reset_in_progress = %d\n",
2509 channels[channel].drives[device].controller.reset_in_progress);
2510 PrintDebug("channels[%d].drives[%d].controller.sectors_per_block = %02x\n",
2512 channels[channel].drives[device].controller.sectors_per_block);
2513 PrintDebug("channels[%d].drives[%d].controller.lba_mode = %d\n",
2515 channels[channel].drives[device].controller.lba_mode);
2516 PrintDebug("channels[%d].drives[%d].controller.features = %d\n",
2518 channels[channel].drives[device].controller.features);
2521 PrintDebug("channels[%d].drives[%d].model_no = %s\n",
2523 channels[channel].drives[device].model_no);
2524 PrintDebug("channels[%d].drives[%d].device_type = %d\n",
2526 channels[channel].drives[device].device_type);
2527 PrintDebug("channels[%d].drives[%d].cdrom.locked = %d\n",
2529 channels[channel].drives[device].cdrom.locked);
2530 PrintDebug("channels[%d].drives[%d].sense.sense_key = %d\n",
2532 channels[channel].drives[device].sense.sense_key);
2533 PrintDebug("channels[%d].drives[%d].sense.asc = %d\n",
2535 channels[channel].drives[device].sense.asc);
2536 PrintDebug("channels[%d].drives[%d].sense.ascq = %d\n",
2538 channels[channel].drives[device].sense.ascq);
2542 PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.c_d = %02x\n",
2544 channels[channel].drives[device].controller.interrupt_reason.c_d);
2546 PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.i_o = %02x\n",
2548 channels[channel].drives[device].controller.interrupt_reason.i_o);
2550 PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.rel = %02x\n",
2552 channels[channel].drives[device].controller.interrupt_reason.rel);
2554 PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.tag = %02x\n",
2556 channels[channel].drives[device].controller.interrupt_reason.tag);
2558 PrintDebug("channels[%d].drives[%d].cdrom.ready = %d\n",
2560 channels[channel].drives[device].cdrom.ready);