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);
322 static int check_bit_fields(struct controller_t * controller);
326 ////////////////////////////////////////////////////////////////////
332 int v3_ramdisk_register_cdrom(struct vm_device * dev, uint_t busID, uint_t driveID, struct cdrom_ops* cd, void * private_data) {
333 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
334 struct channel_t * channel = &(ramdisk->channels[busID]);
335 struct drive_t * drive = &(channel->drives[driveID]);
336 struct controller_t * controller = &(drive->controller);
340 if (drive->device_type != IDE_NONE) {
341 PrintError("Device already registered at this location\n");
349 strncpy((char*)(drive->model_no), "V3VEE Ramdisk", 40);
351 while (strlen((char *)(drive->model_no)) < 40) {
352 strcat ((char*)(drive->model_no), " ");
355 PrintDebug("CDROM on target %d/%d\n", busID, driveID);
357 drive->device_type = IDE_CDROM;
358 drive->cdrom.locked = 0;
359 drive->sense.sense_key = SENSE_NONE;
360 drive->sense.asc = 0;
361 drive->sense.ascq = 0;
363 drive->private_data = private_data;
367 if (check_bit_fields(controller) == INTR_REASON_BIT_ERR) {
368 PrintError("interrupt reason: bit field error\n");
369 return INTR_REASON_BIT_ERR;
373 controller->sector_count = 0;
375 drive->cdrom.cd = cd;
377 PrintDebug("\t\tCD on ata%d-%d: '%s'\n",
381 if(drive->cdrom.cd->insert_cdrom(drive->private_data)) {
382 PrintDebug("\t\tMedia present in CD-ROM drive\n");
383 drive->cdrom.ready = 1;
384 drive->cdrom.capacity = drive->cdrom.cd->capacity(drive->private_data);
385 PrintDebug("\t\tCDROM capacity is %d\n", drive->cdrom.capacity);
387 PrintDebug("\t\tCould not locate CD-ROM, continuing with media not present\n");
388 drive->cdrom.ready = 0;
395 static Bit32u rd_init_hardware(struct ramdisk_t *ramdisk) {
398 struct channel_t *channels = (struct channel_t *)(&(ramdisk->channels));
400 PrintDebug("[rd_init_harddrive]\n");
402 for (channel_num = 0; channel_num < MAX_ATA_CHANNEL; channel_num++) {
403 memset((char *)(channels + channel_num), 0, sizeof(struct channel_t));
406 for (channel_num = 0; channel_num < MAX_ATA_CHANNEL; channel_num++){
407 struct channel_t * channel = &(channels[channel_num]);
409 channel->ioaddr1 = 0x0;
410 channel->ioaddr2 = 0x0;
413 for (device = 0; device < 2; device++){
414 struct drive_t * drive = &(channel->drives[device]);
415 struct controller_t * controller = &(drive->controller);
417 controller->status.busy = 0;
418 controller->status.drive_ready = 1;
419 controller->status.write_fault = 0;
420 controller->status.seek_complete = 1;
421 controller->status.drq = 0;
422 controller->status.corrected_data = 0;
423 controller->status.index_pulse = 0;
424 controller->status.index_pulse_count = 0;
425 controller->status.err = 0;
427 controller->error_register = 0x01; // diagnostic code: no error
428 controller->head_no = 0;
429 controller->sector_count = 1;
430 controller->sector_no = 1;
431 controller->cylinder_no = 0;
432 controller->current_command = 0x00;
433 controller->buffer_index = 0;
435 controller->control.reset = 0;
436 controller->control.disable_irq = 0;
437 controller->reset_in_progress = 0;
439 controller->sectors_per_block = 0x80;
440 controller->lba_mode = 0;
443 controller->features = 0;
446 drive->device_type = IDE_NONE;
449 strncpy((char*)(drive->model_no), "", 40);
450 while(strlen((char *)(drive->model_no)) < 40) {
451 strcat ((char*)(drive->model_no), " ");
458 rd_print_state(ramdisk);
465 static void rd_reset_harddrive(struct ramdisk_t *ramdisk, unsigned type) {
470 static void rd_close_harddrive(struct ramdisk_t *ramdisk) {
475 ////////////////////////////////////////////////////////////////////
479 static int read_data_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
480 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
481 struct channel_t * channel = NULL;
482 struct drive_t * drive = NULL;
483 struct controller_t * controller = NULL;
487 if (is_primary_port(ramdisk, port)) {
488 channel = &(ramdisk->channels[0]);
489 } else if (is_secondary_port(ramdisk, port)) {
490 channel = &(ramdisk->channels[1]);
492 PrintError("Invalid Port: %d\n", port);
496 drive = get_selected_drive(channel);
497 controller = &(drive->controller);
500 PrintTrace("[read_data_handler] IO Read at 0x%x, on drive %d/%d current cmd=0x%x\n",
502 get_channel_no(ramdisk, channel),
503 get_drive_no(channel, drive),
504 controller->current_command);
506 switch (controller->current_command) {
507 case 0xec: // IDENTIFY DEVICE
512 controller->status.busy = 0;
513 controller->status.drive_ready = 1;
514 controller->status.write_fault = 0;
515 controller->status.seek_complete = 1;
516 controller->status.corrected_data = 0;
517 controller->status.err = 0;
520 value32 = controller->buffer[index];
524 value32 |= (controller->buffer[index] << 8);
529 value32 |= (controller->buffer[index] << 16);
530 value32 |= (controller->buffer[index+1] << 24);
534 controller->buffer_index = index;
537 memcpy(dst, controller->buffer + controller->buffer_index, length);
538 controller->buffer_index += length;
540 if (controller->buffer_index >= 512) {
541 controller->status.drq = 0;
546 case 0xa0: //send packet cmd
548 uint_t index = controller->buffer_index;
551 PrintTrace("\t\tatapi.command(%02x), index(%d), cdrom.remaining_blocks(%d)\n",
552 drive->atapi.command,
554 drive->cdrom.remaining_blocks);
556 // Load block if necessary
560 PrintError("\t\tindex > 2048 : 0x%x\n", index);
564 switch (drive->atapi.command) {
565 case 0x28: // read (10)
566 case 0xa8: // read (12)
569 if (!(drive->cdrom.ready)) {
570 PrintError("\t\tRead with CDROM not ready\n");
574 drive->cdrom.cd->read_block(drive->private_data, controller->buffer,
575 drive->cdrom.next_lba);
576 drive->cdrom.next_lba++;
577 drive->cdrom.remaining_blocks--;
580 if (!(drive->cdrom.remaining_blocks)) {
581 PrintDebug("\t\tLast READ block loaded {CDROM}\n");
583 PrintDebug("\t\tREAD block loaded (%d remaining) {CDROM}\n",
584 drive->cdrom.remaining_blocks);
587 // one block transfered, start at beginning
591 default: // no need to load a new block
599 value32 = controller->buffer[index + increment];
603 value32 |= (controller->buffer[index + increment] << 8);
608 value32 |= (controller->buffer[index + increment] << 16);
609 value32 |= (controller->buffer[index + increment + 1] << 24);
613 controller->buffer_index = index + increment;
614 controller->drq_index += increment;
617 /* JRL: CHECK THAT there is enough data in the buffer to copy.... */
619 memcpy(dst, controller->buffer + index, length);
621 controller->buffer_index = index + length;
622 controller->drq_index += length;
627 if (controller->drq_index >= (unsigned)drive->atapi.drq_bytes) {
628 controller->status.drq = 0;
629 controller->drq_index = 0;
631 drive->atapi.total_bytes_remaining -= drive->atapi.drq_bytes;
633 if (drive->atapi.total_bytes_remaining > 0) {
634 // one or more blocks remaining (works only for single block commands)
636 PrintDebug("\t\tPACKET drq bytes read\n");
637 controller->interrupt_reason.i_o = 1;
638 controller->status.busy = 0;
639 controller->status.drq = 1;
640 controller->interrupt_reason.c_d = 0;
642 // set new byte count if last block
643 if (drive->atapi.total_bytes_remaining < controller->byte_count) {
644 controller->byte_count = drive->atapi.total_bytes_remaining;
646 drive->atapi.drq_bytes = controller->byte_count;
648 rd_raise_interrupt(dev, channel);
651 PrintDebug("\t\tPACKET all bytes read\n");
653 controller->interrupt_reason.i_o = 1;
654 controller->interrupt_reason.c_d = 1;
655 controller->status.drive_ready = 1;
656 controller->interrupt_reason.rel = 0;
657 controller->status.busy = 0;
658 controller->status.drq = 0;
659 controller->status.err = 0;
661 rd_raise_interrupt(dev, channel);
669 PrintError("\t\tunsupported command: %02x\n", controller->current_command);
679 static int write_data_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
680 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
681 struct channel_t * channel = NULL;
682 struct drive_t * drive = NULL;
683 struct controller_t * controller = NULL;
685 if (is_primary_port(ramdisk, port)) {
686 channel = &(ramdisk->channels[0]);
687 } else if (is_secondary_port(ramdisk, port)) {
688 channel = &(ramdisk->channels[1]);
690 PrintError("Invalid Port: %d\n", port);
694 drive = get_selected_drive(channel);
695 controller = &(drive->controller);
698 PrintDebug("[write_data_handler] IO write at 0x%x, current_cmd = 0x%02x\n",
699 port, controller->current_command);
703 //PrintDebug("[write_data_handler]\n");
704 switch (controller->current_command) {
705 case 0x30: // WRITE SECTORS
706 PrintError("\t\tneed to implement 0x30(write sector) to port 0x%x\n", port);
711 if (handle_atapi_packet_command(dev, channel, *(ushort_t *)src) == -1) {
712 PrintError("Error sending atapi packet command in PACKET write to data port\n");
719 PrintError("\t\tIO write(0x%x): current command is %02xh\n",
720 port, controller->current_command);
735 static int read_status_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
736 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
737 struct channel_t * channel = NULL;
738 struct drive_t * drive = NULL;
739 struct controller_t * controller = NULL;
744 if (is_primary_port(ramdisk, port)) {
745 channel = &(ramdisk->channels[0]);
746 } else if (is_secondary_port(ramdisk, port)) {
747 channel = &(ramdisk->channels[1]);
749 PrintError("Invalid Port: %d\n", port);
753 drive = get_selected_drive(channel);
754 controller = &(drive->controller);
757 PrintDebug("[read_status_handler] IO read at 0x%x, on drive %d/%d\n",
758 port, get_channel_no(ramdisk, channel),
759 channel->drive_select);
762 if (num_drives_on_channel(channel) == 0) {
763 PrintDebug("Setting value to zero because 0 devices on channel\n");
764 // (mch) Just return zero for these registers
765 memset(dst, 0, length);
769 (controller->status.busy << 7) |
770 (controller->status.drive_ready << 6) |
771 (controller->status.write_fault << 5) |
772 (controller->status.seek_complete << 4) |
773 (controller->status.drq << 3) |
774 (controller->status.corrected_data << 2) |
775 (controller->status.index_pulse << 1) |
776 (controller->status.err) );
779 memcpy(dst, &val, length);
781 controller->status.index_pulse_count++;
782 controller->status.index_pulse = 0;
784 if (controller->status.index_pulse_count >= INDEX_PULSE_CYCLE) {
785 controller->status.index_pulse = 1;
786 controller->status.index_pulse_count = 0;
790 if ((port == SEC_CMD_PORT) || (port == PRI_CMD_PORT)) {
791 rd_lower_irq(dev, channel);
794 PrintDebug("\t\tRead STATUS = 0x%x\n", *(uchar_t *)dst);
801 static int write_cmd_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
802 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
803 struct channel_t * channel = NULL;
804 struct drive_t * drive = NULL;
805 struct controller_t * controller = NULL;
806 uchar_t value = *(uchar_t *)src;
809 PrintError("Invalid Command port write length: %d (port=%d)\n", length, port);
813 if (is_primary_port(ramdisk, port)) {
814 channel = &(ramdisk->channels[0]);
815 } else if (is_secondary_port(ramdisk, port)) {
816 channel = &(ramdisk->channels[1]);
818 PrintError("Invalid Port: %d\n", port);
822 drive = get_selected_drive(channel);
823 controller = &(drive->controller);
826 PrintDebug("[write_command_handler] IO write at 0x%x, on drive %d/%d (val = 0x%x)\n",
827 port, get_channel_no(ramdisk, channel),
828 get_drive_no(channel, drive),
833 case 0xec: // IDENTIFY DEVICE
836 if (drive->device_type == IDE_NONE) {
837 PrintError("\t\tError: disk ata%d-%d not present, aborting\n",
838 get_channel_no(ramdisk, channel),
839 get_drive_no(channel, drive));
840 rd_command_aborted(dev, channel, value);
842 } else if (drive->device_type == IDE_CDROM) {
843 PrintDebug("Identifying CDROM...Going to abort????\n");
844 controller->head_no = 0;
845 controller->sector_count = 1;
846 controller->sector_no = 1;
847 controller->cylinder_no = 0xeb14;
848 rd_command_aborted(dev, channel, 0xec);
850 PrintError("\t\tError: Want to identify HDD!!\n");
852 SELECTED_CONTROLLER(channel).current_command = value;
853 SELECTED_CONTROLLER(channel).error_register = 0;
855 // See ATA/ATAPI-4, 8.12
856 SELECTED_CONTROLLER(channel).status.busy = 0;
857 SELECTED_CONTROLLER(channel).status.drive_ready = 1;
858 SELECTED_CONTROLLER(channel).status.write_fault = 0;
859 SELECTED_CONTROLLER(channel).status.drq = 1;
860 SELECTED_CONTROLLER(channel).status.err = 0;
862 SELECTED_CONTROLLER(channel).status.seek_complete = 1;
863 SELECTED_CONTROLLER(channel).status.corrected_data = 0;
865 SELECTED_CONTROLLER(channel).buffer_index = 0;
866 raise_interrupt(channel);
867 identify_drive(channel);
875 case 0xa1: // IDENTIFY PACKET DEVICE
877 if (drive->device_type == IDE_CDROM) {
878 controller->current_command = value;
879 controller->error_register = 0;
881 controller->status.busy = 0;
882 controller->status.drive_ready = 1;
883 controller->status.write_fault = 0;
884 controller->status.drq = 1;
885 controller->status.err = 0;
887 controller->status.seek_complete = 1;
888 controller->status.corrected_data = 0;
890 controller->buffer_index = 0;
891 rd_raise_interrupt(dev, channel);
892 rd_identify_ATAPI_drive(dev, channel);
894 PrintError("Identifying non cdrom device not supported - ata %d/%d\n",
895 get_channel_no(ramdisk, channel),
896 get_drive_no(channel, drive));
897 rd_command_aborted(dev, channel, 0xa1);
901 case 0xa0: // SEND PACKET (atapi)
903 if (drive->device_type == IDE_CDROM) {
906 if (controller->features & (1 << 0)) {
907 PrintError("\t\tPACKET-DMA not supported");
911 if (controller->features & (1 << 1)) {
912 PrintError("\t\tPACKET-overlapped not supported");
916 // We're already ready!
917 controller->sector_count = 1;
918 controller->status.busy = 0;
919 controller->status.write_fault = 0;
922 controller->status.drq = 1;
923 controller->status.err = 0;
925 // NOTE: no interrupt here
926 controller->current_command = value;
927 controller->buffer_index = 0;
929 PrintError("Sending packet to non cdrom device not supported\n");
930 rd_command_aborted (dev, channel, 0xa0);
935 PrintError("\t\tneed translate command %2x - ata %d\%d\n", value,
936 get_channel_no(ramdisk, channel),
937 get_drive_no(channel, drive));
939 /* JRL THIS NEEDS TO CHANGE */
947 static int write_ctrl_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
948 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
949 struct channel_t * channel = NULL;
950 struct drive_t * master_drive = NULL;
951 struct drive_t * slave_drive = NULL;
952 struct controller_t * controller = NULL;
953 uchar_t value = *(uchar_t *)src;
954 rd_bool prev_control_reset;
957 PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
961 if (is_primary_port(ramdisk, port)) {
962 channel = &(ramdisk->channels[0]);
963 } else if (is_secondary_port(ramdisk, port)) {
964 channel = &(ramdisk->channels[1]);
966 PrintError("Invalid Port: %d\n", port);
970 master_drive = &(channel->drives[0]);
971 slave_drive = &(channel->drives[1]);
973 controller = &(get_selected_drive(channel)->controller);
976 PrintDebug("[write_control_handler] IO write at 0x%x, on drive %d/%d (val = 0x%x)\n",
977 port, get_channel_no(ramdisk, channel),
978 channel->drive_select,
981 // (mch) Even if device 1 was selected, a write to this register
982 // goes to device 0 (if device 1 is absent)
984 prev_control_reset = controller->control.reset;
988 PrintDebug("RESET Signaled\n");
991 master_drive->controller.control.reset = value & 0x04;
992 slave_drive->controller.control.reset = value & 0x04;
994 // CGS: was: SELECTED_CONTROLLER(channel).control.disable_irq = value & 0x02;
995 master_drive->controller.control.disable_irq = value & 0x02;
996 slave_drive->controller.control.disable_irq = value & 0x02;
998 PrintDebug("\t\tadpater control reg: reset controller = %d\n",
999 (unsigned) (controller->control.reset) ? 1 : 0);
1000 PrintDebug("\t\tadpater control reg: disable_irq(X) = %d\n",
1001 (unsigned) (controller->control.disable_irq) ? 1 : 0);
1003 if ((!prev_control_reset) && (controller->control.reset)) {
1006 // transition from 0 to 1 causes all drives to reset
1007 PrintDebug("\t\thard drive: RESET\n");
1009 // (mch) Set BSY, drive not ready
1010 for (id = 0; id < 2; id++) {
1011 struct controller_t * ctrl = NULL;
1014 ctrl = &(master_drive->controller);
1015 } else if (id == 1) {
1016 ctrl = &(slave_drive->controller);
1019 ctrl->status.busy = 1;
1020 ctrl->status.drive_ready = 0;
1021 ctrl->reset_in_progress = 1;
1023 ctrl->status.write_fault = 0;
1024 ctrl->status.seek_complete = 1;
1025 ctrl->status.drq = 0;
1026 ctrl->status.corrected_data = 0;
1027 ctrl->status.err = 0;
1029 ctrl->error_register = 0x01; // diagnostic code: no error
1031 ctrl->current_command = 0x00;
1032 ctrl->buffer_index = 0;
1034 ctrl->sectors_per_block = 0x80;
1037 ctrl->control.disable_irq = 0;
1040 rd_lower_irq(dev, channel);
1042 } else if ((controller->reset_in_progress) &&
1043 (!controller->control.reset)) {
1045 // Clear BSY and DRDY
1046 PrintDebug("\t\tReset complete {%s}\n", device_type_to_str(get_selected_drive(channel)->device_type));
1048 for (id = 0; id < 2; id++) {
1049 struct controller_t * ctrl = NULL;
1050 struct drive_t * drv = NULL;
1053 ctrl = &(master_drive->controller);
1055 } else if (id == 1) {
1056 ctrl = &(slave_drive->controller);
1060 ctrl->status.busy = 0;
1061 ctrl->status.drive_ready = 1;
1062 ctrl->reset_in_progress = 0;
1065 if (drv->device_type == IDE_DISK) {
1066 PrintDebug("\t\tdrive %d/%d is harddrive\n", get_channel_no(ramdisk, channel), id);
1068 ctrl->sector_count = 1;
1069 ctrl->sector_no = 1;
1070 ctrl->cylinder_no = 0;
1073 ctrl->sector_count = 1;
1074 ctrl->sector_no = 1;
1075 ctrl->cylinder_no = 0xeb14;
1080 PrintDebug("\t\ts[0].controller.control.disable_irq = %02x\n",
1081 master_drive->controller.control.disable_irq);
1082 PrintDebug("\t\ts[1].controller.control.disable_irq = %02x\n",
1083 slave_drive->controller.control.disable_irq);
1088 static int read_general_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
1089 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1090 struct channel_t * channel = NULL;
1091 struct drive_t * drive = NULL;
1092 struct controller_t * controller = NULL;
1096 PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
1100 if (is_primary_port(ramdisk, port)) {
1101 channel = &(ramdisk->channels[0]);
1102 } else if (is_secondary_port(ramdisk, port)) {
1103 channel = &(ramdisk->channels[1]);
1105 PrintError("Invalid Port: %d\n", port);
1109 drive = get_selected_drive(channel);
1110 controller = &(drive->controller);
1113 PrintDebug("[read_general_handler] IO read addr at %x, on drive %d/%d, curcmd = %02x\n",
1114 port, get_channel_no(ramdisk, channel),
1115 channel->drive_select,
1116 controller->current_command);
1120 case PRI_FEATURES_PORT:
1121 case SEC_FEATURES_PORT: // hard disk error register 0x1f1
1123 uchar_t val = (drive->device_type == IDE_NONE) ? 0 : controller->error_register;
1125 controller->status.err = 0;
1127 PrintDebug("\t\tRead FEATURES = 0x%x\n", val);
1129 *(uchar_t *)dst = val;
1135 case PRI_SECT_CNT_PORT:
1136 case SEC_SECT_CNT_PORT: // hard disk sector count / interrupt reason 0x1f2
1138 uchar_t val = (drive->device_type == IDE_NONE) ? 0 : controller->sector_count;
1139 PrintDebug("\t\tRead SECTOR COUNT = 0x%x\n", val);
1140 *(uchar_t *)dst = val;
1145 case PRI_SECT_ADDR1_PORT:
1146 case SEC_SECT_ADDR1_PORT: // sector number 0x1f3
1148 uchar_t val = (drive->device_type == IDE_NONE) ? 0 : controller->sector_no;
1150 PrintDebug("\t\tRead SECTOR ADDR1 = 0x%x\n", val);
1152 *(uchar_t *)dst = val;
1158 case PRI_SECT_ADDR2_PORT:
1159 case SEC_SECT_ADDR2_PORT: // cylinder low 0x1f4
1161 // -- WARNING : On real hardware the controller registers are shared between drives.
1162 // So we must respond even if the select device is not present. Some OS uses this fact
1163 // to detect the disks.... minix2 for example
1164 uchar_t val = (num_drives_on_channel(channel) == 0) ? 0 : (controller->cylinder_no & 0x00ff);
1166 PrintDebug("\t\tRead SECTOR ADDR2 = 0x%x\n", val);
1168 *(uchar_t *)dst = val;
1174 case PRI_SECT_ADDR3_PORT:
1175 case SEC_SECT_ADDR3_PORT: // cylinder high 0x1f5
1177 // -- WARNING : On real hardware the controller registers are shared between drives.
1178 // So we must respond even if the select device is not present. Some OS uses this fact
1179 // to detect the disks.... minix2 for example
1180 uchar_t val = (num_drives_on_channel(channel) == 0) ? 0 : (controller->cylinder_no >> 8);
1182 PrintDebug("\t\tRead SECTOR ADDR3 = 0x%x\n", val);
1184 *(uchar_t *)dst = val;
1189 case PRI_DRV_SEL_PORT:
1190 case SEC_DRV_SEL_PORT: // hard disk drive and head register 0x1f6
1192 // b7 Extended data field for ECC
1193 // b6/b5: Used to be sector size. 00=256,01=512,10=1024,11=128
1194 // Since 512 was always used, bit 6 was taken to mean LBA mode:
1195 // b6 1=LBA mode, 0=CHS mode
1199 uchar_t val = ((1 << 7) |
1200 ((controller->lba_mode > 0) << 6) |
1201 (1 << 5) | // 01b = 512 sector size
1202 (channel->drive_select << 4) |
1203 (controller->head_no << 0));
1205 PrintDebug("\t\tRead DRIVE SELECT = 0x%x\n", val);
1206 *(uchar_t *)dst = val;
1211 case PRI_ADDR_REG_PORT:
1212 case SEC_ADDR_REG_PORT: // Hard Disk Address Register 0x3f7
1214 // Obsolete and unsupported register. Not driven by hard
1215 // disk controller. Report all 1's. If floppy controller
1216 // is handling this address, it will call this function
1217 // set/clear D7 (the only bit it handles), then return
1218 // the combined value
1219 *(uchar_t *)dst = 0xff;
1224 PrintError("Invalid Port: %d\n", port);
1232 static int write_general_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
1233 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1234 struct channel_t * channel = NULL;
1235 struct drive_t * drive = NULL;
1236 struct controller_t * controller = NULL;
1237 uchar_t value = *(uchar_t *)src;
1240 PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
1244 if (is_primary_port(ramdisk, port)) {
1245 channel = &(ramdisk->channels[0]);
1246 } else if (is_secondary_port(ramdisk, port)) {
1247 channel = &(ramdisk->channels[1]);
1249 PrintError("Invalid Port: %d\n", port);
1253 drive = get_selected_drive(channel);
1254 controller = &(drive->controller);
1257 PrintDebug("[write_general_handler] IO write to port %x (val=0x%02x), channel = %d\n",
1258 port, value, get_channel_no(ramdisk, channel));
1262 case PRI_FEATURES_PORT:
1263 case SEC_FEATURES_PORT: // hard disk write precompensation 0x1f1
1265 write_features(channel, value);
1268 case PRI_SECT_CNT_PORT:
1269 case SEC_SECT_CNT_PORT: // hard disk sector count 0x1f2
1271 write_sector_count(channel, value);
1274 case PRI_SECT_ADDR1_PORT:
1275 case SEC_SECT_ADDR1_PORT: // hard disk sector number 0x1f3
1277 write_sector_number(channel, value);
1280 case PRI_SECT_ADDR2_PORT:
1281 case SEC_SECT_ADDR2_PORT: // hard disk cylinder low 0x1f4
1283 write_cylinder_low(channel, value);
1286 case PRI_SECT_ADDR3_PORT:
1287 case SEC_SECT_ADDR3_PORT: // hard disk cylinder high 0x1f5
1289 write_cylinder_high(channel, value);
1292 case PRI_DRV_SEL_PORT:
1293 case SEC_DRV_SEL_PORT: // hard disk drive and head register 0x1f6
1295 // b7 Extended data field for ECC
1296 // b6/b5: Used to be sector size. 00=256,01=512,10=1024,11=128
1297 // Since 512 was always used, bit 6 was taken to mean LBA mode:
1298 // b6 1=LBA mode, 0=CHS mode
1305 PrintDebug("\tDrive Select value=%x\n", value);
1307 if ((value & 0xa0) != 0xa0) {
1308 PrintDebug("\t\tIO write 0x%x (%02x): not 1x1xxxxxb\n", port, (unsigned) value);
1311 write_head_no(channel, value & 0xf);
1312 if ((controller->lba_mode == 0) && (((value >> 6) & 1) == 1)) {
1313 PrintDebug("\t\tenabling LBA mode\n");
1316 write_lba_mode(channel, (value >> 6) & 1);
1320 if (drive->cdrom.cd) {
1321 PrintDebug("\t\tSetting LBA on CDROM: %d\n", (value >> 6) & 1);
1322 drive->cdrom.cd->set_LBA(drive->private_data, (value >> 6) & 1);
1326 channel->drive_select = (value >> 4) & 0x01;
1327 drive = get_selected_drive(channel);
1329 if (drive->device_type == IDE_NONE) {
1330 PrintError("\t\tError: device set to %d which does not exist! channel = 0x%x\n",
1331 channel->drive_select, get_channel_no(ramdisk, channel));
1333 controller->error_register = 0x04; // aborted
1334 controller->status.err = 1;
1340 PrintError("\t\thard drive: io write to unhandled port 0x%x (value = %c)\n", port, value);
1351 static void rd_raise_interrupt(struct vm_device * dev, struct channel_t * channel) {
1352 // struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1353 struct drive_t * drive = get_selected_drive(channel);
1354 struct controller_t * controller = &(drive->controller);
1356 PrintDebug("[raise_interrupt] disable_irq = 0x%02x\n", controller->control.disable_irq);
1358 if (!(controller->control.disable_irq)) {
1360 PrintDebug("\t\tRaising interrupt %d {%s}\n\n", channel->irq, device_type_to_str(drive->device_type));
1362 v3_raise_irq(dev->vm, channel->irq);
1364 PrintDebug("\t\tRaising irq but irq is disabled\n");
1370 static void rd_lower_irq(struct vm_device *dev, struct channel_t * channel) {
1371 PrintDebug("[lower_irq] irq = %d\n", channel->irq);
1372 v3_lower_irq(dev->vm, channel->irq);
1381 //////////////////////////////////////////////////////////////////////////
1389 int handle_atapi_packet_command(struct vm_device * dev, struct channel_t * channel, ushort_t value) {
1390 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1391 struct drive_t * drive = get_selected_drive(channel);
1392 struct controller_t * controller = &(drive->controller);
1394 if (controller->buffer_index >= PACKET_SIZE) {
1395 PrintError("ATAPI packet exceeded maximum length: buffer_index (%d) >= PACKET_SIZE\n",
1396 controller->buffer_index);
1400 controller->buffer[controller->buffer_index] = value;
1401 controller->buffer[controller->buffer_index + 1] = (value >> 8);
1402 controller->buffer_index += 2;
1405 /* if packet completely writtten */
1406 if (controller->buffer_index >= PACKET_SIZE) {
1407 // complete command received
1408 Bit8u atapi_command = controller->buffer[0];
1410 PrintDebug("\t\tcdrom: ATAPI command 0x%x started\n", atapi_command);
1412 switch (atapi_command) {
1413 case 0x00: // test unit ready
1415 PrintDebug("Testing unit ready\n");
1416 if (drive->cdrom.ready) {
1417 rd_atapi_cmd_nop(dev, channel);
1419 PrintError("CDROM not ready in test unit ready\n");
1420 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1423 rd_raise_interrupt(dev, channel);
1427 case 0x03: // request sense
1429 int alloc_length = controller->buffer[4];
1431 if (rd_init_send_atapi_command(dev, channel, atapi_command, 18, alloc_length, false) == -1) {
1432 PrintError("Error sending atapi command in Request Sense\n");
1437 controller->buffer[0] = 0x70 | (1 << 7);
1438 controller->buffer[1] = 0;
1439 controller->buffer[2] = drive->sense.sense_key;
1440 controller->buffer[3] = drive->sense.information.arr[0];
1441 controller->buffer[4] = drive->sense.information.arr[1];
1442 controller->buffer[5] = drive->sense.information.arr[2];
1443 controller->buffer[6] = drive->sense.information.arr[3];
1444 controller->buffer[7] = 17 - 7;
1445 controller->buffer[8] = drive->sense.specific_inf.arr[0];
1446 controller->buffer[9] = drive->sense.specific_inf.arr[1];
1447 controller->buffer[10] = drive->sense.specific_inf.arr[2];
1448 controller->buffer[11] = drive->sense.specific_inf.arr[3];
1449 controller->buffer[12] = drive->sense.asc;
1450 controller->buffer[13] = drive->sense.ascq;
1451 controller->buffer[14] = drive->sense.fruc;
1452 controller->buffer[15] = drive->sense.key_spec.arr[0];
1453 controller->buffer[16] = drive->sense.key_spec.arr[1];
1454 controller->buffer[17] = drive->sense.key_spec.arr[2];
1456 rd_ready_to_send_atapi(dev, channel);
1459 case 0x1b: // start stop unit
1461 //bx_bool Immed = (controller->buffer[1] >> 0) & 1;
1462 rd_bool LoEj = (controller->buffer[4] >> 1) & 1;
1463 rd_bool Start = (controller->buffer[4] >> 0) & 1;
1466 if ((!LoEj) && (!Start)) {
1467 PrintError("FIXME: Stop disc not implemented\n");
1469 rd_atapi_cmd_nop(dev, channel);
1470 rd_raise_interrupt(dev, channel);
1472 } else if (!LoEj && Start) { // start (spin up) the disc
1474 drive->cdrom.cd->start_cdrom(drive->private_data);
1476 PrintError("FIXME: ATAPI start disc not reading TOC\n");
1477 rd_atapi_cmd_nop(dev, channel);
1478 rd_raise_interrupt(dev, channel);
1480 } else if (LoEj && !Start) { // Eject the disc
1481 rd_atapi_cmd_nop(dev, channel);
1482 PrintDebug("Ejecting Disk\n");
1483 if (drive->cdrom.ready) {
1485 drive->cdrom.cd->eject_cdrom(drive->private_data);
1487 drive->cdrom.ready = 0;
1488 //bx_options.atadevice[channel][SLAVE_SELECTED(channel)].Ostatus->set(EJECTED);
1489 //bx_gui->update_drive_status_buttons();
1491 rd_raise_interrupt(dev, channel);
1493 } else { // Load the disc
1494 // My guess is that this command only closes the tray, that's a no-op for us
1495 rd_atapi_cmd_nop(dev, channel);
1496 rd_raise_interrupt(dev, channel);
1500 case 0xbd: // mechanism status
1502 uint16_t alloc_length = rd_read_16bit(controller->buffer + 8);
1504 if (alloc_length == 0) {
1505 PrintError("Zero allocation length to MECHANISM STATUS not impl.\n");
1509 if (rd_init_send_atapi_command(dev, channel, atapi_command, 8, alloc_length, false) == -1) {
1510 PrintError("Error sending atapi command in mechanism status\n");
1514 controller->buffer[0] = 0; // reserved for non changers
1515 controller->buffer[1] = 0; // reserved for non changers
1517 controller->buffer[2] = 0; // Current LBA (TODO!)
1518 controller->buffer[3] = 0; // Current LBA (TODO!)
1519 controller->buffer[4] = 0; // Current LBA (TODO!)
1521 controller->buffer[5] = 1; // one slot
1523 controller->buffer[6] = 0; // slot table length
1524 controller->buffer[7] = 0; // slot table length
1526 rd_ready_to_send_atapi(dev, channel);
1529 case 0x5a: // mode sense
1531 uint16_t alloc_length = rd_read_16bit(controller->buffer + 7);
1533 Bit8u PC = controller->buffer[2] >> 6;
1534 Bit8u PageCode = controller->buffer[2] & 0x3f;
1537 case 0x0: // current values
1540 case 0x01: // error recovery
1543 if (rd_init_send_atapi_command(dev, channel, atapi_command, sizeof(struct error_recovery_t) + 8, alloc_length, false) == -1) {
1544 PrintError("Error sending atapi command in mode sense error recovery\n");
1548 rd_init_mode_sense_single(dev, channel, &(drive->cdrom.current.error_recovery),
1549 sizeof(struct error_recovery_t));
1550 rd_ready_to_send_atapi(dev, channel);
1553 case 0x2a: // CD-ROM capabilities & mech. status
1556 if (rd_init_send_atapi_command(dev, channel, atapi_command, 28, alloc_length, false) == -1) {
1557 PrintError("Error sending atapi command in CDROM caps/mech mode-sense\n");
1561 rd_init_mode_sense_single(dev, channel, &(controller->buffer[8]), 28);
1563 controller->buffer[8] = 0x2a;
1564 controller->buffer[9] = 0x12;
1565 controller->buffer[10] = 0x00;
1566 controller->buffer[11] = 0x00;
1567 // Multisession, Mode 2 Form 2, Mode 2 Form 1
1568 controller->buffer[12] = 0x70;
1569 controller->buffer[13] = (3 << 5);
1570 controller->buffer[14] = (unsigned char) (1 |
1571 (drive->cdrom.locked ? (1 << 1) : 0) |
1574 controller->buffer[15] = 0x00;
1575 controller->buffer[16] = (706 >> 8) & 0xff;
1576 controller->buffer[17] = 706 & 0xff;
1577 controller->buffer[18] = 0;
1578 controller->buffer[19] = 2;
1579 controller->buffer[20] = (512 >> 8) & 0xff;
1580 controller->buffer[21] = 512 & 0xff;
1581 controller->buffer[22] = (706 >> 8) & 0xff;
1582 controller->buffer[23] = 706 & 0xff;
1583 controller->buffer[24] = 0;
1584 controller->buffer[25] = 0;
1585 controller->buffer[26] = 0;
1586 controller->buffer[27] = 0;
1587 rd_ready_to_send_atapi(dev, channel);
1590 case 0x0d: // CD-ROM
1591 case 0x0e: // CD-ROM audio control
1594 PrintError("Ramdisk: cdrom: MODE SENSE (curr), code=%x not implemented yet\n",
1596 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1597 ASC_INV_FIELD_IN_CMD_PACKET);
1598 rd_raise_interrupt(dev, channel);
1603 // not implemeted by this device
1604 PrintError("\t\tcdrom: MODE SENSE PC=%x, PageCode=%x, not implemented by device\n",
1606 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1607 ASC_INV_FIELD_IN_CMD_PACKET);
1608 rd_raise_interrupt(dev, channel);
1614 case 0x1: // changeable values
1617 case 0x01: // error recovery
1618 case 0x0d: // CD-ROM
1619 case 0x0e: // CD-ROM audio control
1620 case 0x2a: // CD-ROM capabilities & mech. status
1623 PrintError("cdrom: MODE SENSE (chg), code=%x not implemented yet\n",
1625 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1626 ASC_INV_FIELD_IN_CMD_PACKET);
1627 rd_raise_interrupt(dev, channel);
1632 // not implemeted by this device
1633 PrintError("Changeable values of mode sense not supported by cdrom\n");
1634 PrintDebug("\t\tcdrom: MODE SENSE PC=%x, PageCode=%x, not implemented by device\n",
1636 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1637 ASC_INV_FIELD_IN_CMD_PACKET);
1638 rd_raise_interrupt(dev, channel);
1644 case 0x2: // default values
1647 case 0x01: // error recovery
1648 case 0x0d: // CD-ROM
1649 case 0x0e: // CD-ROM audio control
1650 case 0x2a: // CD-ROM capabilities & mech. status
1652 PrintError("Default values of mode sense not supported by cdrom\n");
1653 PrintDebug("cdrom: MODE SENSE (dflt), code=%x\n",
1659 PrintError("Default values of mode sense not implemented in cdrom\n");
1660 // not implemeted by this device
1661 PrintDebug("cdrom: MODE SENSE PC=%x, PageCode=%x, not implemented by device\n",
1663 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1664 ASC_INV_FIELD_IN_CMD_PACKET);
1665 rd_raise_interrupt(dev, channel);
1671 case 0x3: // saved values not implemented
1673 PrintError("\t\tSaved values not implemented in mode sense\n");
1674 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
1675 rd_raise_interrupt(dev, channel);
1680 PrintError("Unsupported Mode sense value\n");
1687 case 0x12: // inquiry
1689 uint8_t alloc_length = controller->buffer[4];
1691 if (rd_init_send_atapi_command(dev, channel, atapi_command, 36, alloc_length, false) == -1) {
1692 PrintError("Error sending atapi command in inquiry\n");
1696 controller->buffer[0] = 0x05; // CD-ROM
1697 controller->buffer[1] = 0x80; // Removable
1698 controller->buffer[2] = 0x00; // ISO, ECMA, ANSI version
1699 controller->buffer[3] = 0x21; // ATAPI-2, as specified
1700 controller->buffer[4] = 31; // additional length (total 36)
1701 controller->buffer[5] = 0x00; // reserved
1702 controller->buffer[6] = 0x00; // reserved
1703 controller->buffer[7] = 0x00; // reserved
1706 const char* vendor_id = "VTAB ";
1708 for (i = 0; i < 8; i++) {
1709 controller->buffer[8+i] = vendor_id[i];
1713 const char* product_id = "Turbo CD-ROM ";
1714 for (i = 0; i < 16; i++) {
1715 controller->buffer[16+i] = product_id[i];
1718 // Product Revision level
1719 const char* rev_level = "1.0 ";
1720 for (i = 0; i < 4; i++) {
1721 controller->buffer[32 + i] = rev_level[i];
1724 rd_ready_to_send_atapi(dev, channel);
1727 case 0x25: // read cd-rom capacity
1729 // no allocation length???
1730 if (rd_init_send_atapi_command(dev, channel, atapi_command, 8, 8, false) == -1) {
1731 PrintError("Error sending atapi command in read cdrom capacity\n");
1735 if (drive->cdrom.ready) {
1736 uint32_t capacity = drive->cdrom.capacity;
1738 PrintDebug("\t\tCapacity is %d sectors (%d bytes)\n", capacity, capacity * 2048);
1740 controller->buffer[0] = (capacity >> 24) & 0xff;
1741 controller->buffer[1] = (capacity >> 16) & 0xff;
1742 controller->buffer[2] = (capacity >> 8) & 0xff;
1743 controller->buffer[3] = (capacity >> 0) & 0xff;
1744 controller->buffer[4] = (2048 >> 24) & 0xff;
1745 controller->buffer[5] = (2048 >> 16) & 0xff;
1746 controller->buffer[6] = (2048 >> 8) & 0xff;
1747 controller->buffer[7] = (2048 >> 0) & 0xff;
1749 rd_ready_to_send_atapi(dev, channel);
1751 PrintError("CDROM not ready in read cdrom capacity\n");
1752 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1753 rd_raise_interrupt(dev, channel);
1759 case 0xbe: // read cd
1761 if (drive->cdrom.ready) {
1762 PrintError("Read CD with CD present not implemented\n");
1763 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1764 rd_raise_interrupt(dev, channel);
1766 PrintError("Drive not ready in read cd with CD present\n");
1767 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1768 rd_raise_interrupt(dev, channel);
1772 case 0x43: // read toc
1774 if (drive->cdrom.ready) {
1776 bool msf = (controller->buffer[1] >> 1) & 1;
1777 uint8_t starting_track = controller->buffer[6];
1779 uint16_t alloc_length = rd_read_16bit(controller->buffer + 7);
1781 uint8_t format = (controller->buffer[9] >> 6);
1784 PrintDebug("Reading CDROM TOC: Format=%d (byte count=%d) (toc length:%d)\n",
1785 format, controller->byte_count, toc_length);
1790 if (!(drive->cdrom.cd->read_toc(drive->private_data, controller->buffer,
1791 &toc_length, msf, starting_track))) {
1792 PrintError("CDROM: Reading Table of Contents Failed\n");
1793 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1794 ASC_INV_FIELD_IN_CMD_PACKET);
1795 rd_raise_interrupt(dev, channel);
1800 if (rd_init_send_atapi_command(dev, channel, atapi_command, toc_length, alloc_length, false) == -1) {
1801 PrintError("Failed to init send atapi command in read toc (fmt=%d)\n", format);
1805 rd_ready_to_send_atapi(dev, channel);
1810 // multi session stuff. we ignore this and emulate a single session only
1812 if (rd_init_send_atapi_command(dev, channel, atapi_command, 12, alloc_length, false) == -1) {
1813 PrintError("Failed to init send atapi command in read toc (fmt=%d)\n", format);
1817 controller->buffer[0] = 0;
1818 controller->buffer[1] = 0x0a;
1819 controller->buffer[2] = 1;
1820 controller->buffer[3] = 1;
1822 for (i = 0; i < 8; i++) {
1823 controller->buffer[4 + i] = 0;
1826 rd_ready_to_send_atapi(dev, channel);
1831 PrintError("(READ TOC) Format %d not supported\n", format);
1835 PrintError("CDROM not ready in read toc\n");
1836 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1837 rd_raise_interrupt(dev, channel);
1841 case 0x28: // read (10)
1842 case 0xa8: // read (12)
1845 uint32_t transfer_length;
1846 if (atapi_command == 0x28) {
1847 transfer_length = rd_read_16bit(controller->buffer + 7);
1849 transfer_length = rd_read_32bit(controller->buffer + 6);
1852 uint32_t lba = rd_read_32bit(controller->buffer + 2);
1854 if (!(drive->cdrom.ready)) {
1855 PrintError("CDROM Error: Not Ready (ATA%d/%d)\n",
1856 get_channel_no(ramdisk, channel), get_drive_no(channel, drive));
1857 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1858 rd_raise_interrupt(dev, channel);
1862 if (transfer_length == 0) {
1863 PrintError("READ(%d) with transfer length 0, ok\n",
1864 (atapi_command == 0x28) ? 10 : 12);
1865 rd_atapi_cmd_nop(dev, channel);
1866 rd_raise_interrupt(dev, channel);
1870 if (lba + transfer_length > drive->cdrom.capacity) {
1871 PrintError("CDROM Error: Capacity exceeded [capacity=%d] (ATA%d/%d)\n",
1872 drive->cdrom.capacity,
1873 get_channel_no(ramdisk, channel), get_drive_no(channel, drive));
1874 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
1875 rd_raise_interrupt(dev, channel);
1879 PrintDebug("\t\tcdrom: READ (%d) LBA=%d LEN=%d\n",
1880 (atapi_command == 0x28) ? 10 : 12,
1881 lba, transfer_length);
1884 if (rd_init_send_atapi_command(dev, channel, atapi_command, transfer_length * 2048,
1885 transfer_length * 2048, true) == -1) {
1886 PrintError("CDROM Error: Atapi command send error\n");
1890 drive->cdrom.remaining_blocks = transfer_length;
1891 drive->cdrom.next_lba = lba;
1892 rd_ready_to_send_atapi(dev, channel);
1897 uint32_t lba = rd_read_32bit(controller->buffer + 2);
1899 if (!(drive->cdrom.ready)) {
1900 PrintError("CDROM not ready in seek\n");
1901 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1902 rd_raise_interrupt(dev, channel);
1906 if (lba > drive->cdrom.capacity) {
1907 PrintError("LBA is greater than CDROM capacity in seek\n");
1908 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
1909 rd_raise_interrupt(dev, channel);
1913 PrintError("\t\tcdrom: SEEK (ignored)\n");
1915 rd_atapi_cmd_nop(dev, channel);
1916 rd_raise_interrupt(dev, channel);
1920 case 0x1e: // prevent/allow medium removal
1923 if (drive->cdrom.ready) {
1924 drive->cdrom.locked = controller->buffer[4] & 1;
1925 rd_atapi_cmd_nop(dev, channel);
1927 PrintError("CD not ready in prevent/allow medium removal\n");
1928 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1931 rd_raise_interrupt(dev, channel);
1935 case 0x42: // read sub-channel
1937 //bool msf = get_packet_field(channel,1, 1, 1);
1938 bool sub_q = get_packet_field(channel,2, 6, 1);
1939 //uint8_t data_format = get_packet_byte(channel,3);
1940 //uint8_t track_number = get_packet_byte(channel,6);
1941 uint16_t alloc_length = get_packet_word(channel,7);
1946 UNUSED(data_format);
1947 UNUSED(track_number);
1949 if (!(drive->cdrom.ready)) {
1950 PrintError("CDROM not ready in read sub-channel\n");
1951 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1952 rd_raise_interrupt(dev, channel);
1954 controller->buffer[0] = 0;
1955 controller->buffer[1] = 0; // audio not supported
1956 controller->buffer[2] = 0;
1957 controller->buffer[3] = 0;
1959 int ret_len = 4; // header size
1961 if (sub_q) { // !sub_q == header only
1962 PrintError("Read sub-channel with SubQ not implemented\n");
1963 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1964 ASC_INV_FIELD_IN_CMD_PACKET);
1965 rd_raise_interrupt(dev, channel);
1968 if (rd_init_send_atapi_command(dev, channel, atapi_command, ret_len, alloc_length, false) == -1) {
1969 PrintError("Error sending atapi command in read sub-channel\n");
1972 rd_ready_to_send_atapi(dev, channel);
1976 case 0x51: // read disc info
1978 // no-op to keep the Linux CD-ROM driver happy
1979 PrintError("Error: Read disk info no-op to keep the Linux CD-ROM driver happy\n");
1980 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1981 rd_raise_interrupt(dev, channel);
1984 case 0x55: // mode select
1985 case 0xa6: // load/unload cd
1986 case 0x4b: // pause/resume
1987 case 0x45: // play audio
1988 case 0x47: // play audio msf
1989 case 0xbc: // play cd
1990 case 0xb9: // read cd msf
1991 case 0x44: // read header
1993 case 0xbb: // set cd speed
1994 case 0x4e: // stop play/scan
1997 PrintError("ATAPI command 0x%x not implemented yet\n",
1999 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
2000 rd_raise_interrupt(dev, channel);
2003 PrintError("Unknown ATAPI command 0x%x (%d)\n",
2004 atapi_command, atapi_command);
2005 // We'd better signal the error if the user chose to continue
2006 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
2007 rd_raise_interrupt(dev, channel);
2019 int rd_init_send_atapi_command(struct vm_device * dev, struct channel_t * channel, Bit8u command, int req_length, int alloc_length, bool lazy)
2021 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2022 struct controller_t * controller = &(drive->controller);
2024 // controller->byte_count is a union of controller->cylinder_no;
2025 // lazy is used to force a data read in the buffer at the next read.
2027 PrintDebug("[rd_init_send_atapi_cmd]\n");
2029 if (controller->byte_count == 0xffff) {
2030 controller->byte_count = 0xfffe;
2033 if ((controller->byte_count & 1) &&
2034 !(alloc_length <= controller->byte_count)) {
2036 PrintDebug("\t\tOdd byte count (0x%04x) to ATAPI command 0x%02x, using 0x%x\n",
2037 controller->byte_count,
2039 controller->byte_count - 1);
2041 controller->byte_count -= 1;
2044 if (controller->byte_count == 0) {
2045 PrintError("\t\tATAPI command with zero byte count\n");
2049 if (alloc_length < 0) {
2050 PrintError("\t\tAllocation length < 0\n");
2054 if (alloc_length == 0) {
2055 alloc_length = controller->byte_count;
2058 controller->interrupt_reason.i_o = 1;
2059 controller->interrupt_reason.c_d = 0;
2060 controller->status.busy = 0;
2061 controller->status.drq = 1;
2062 controller->status.err = 0;
2064 // no bytes transfered yet
2066 controller->buffer_index = 2048;
2068 controller->buffer_index = 0;
2071 controller->drq_index = 0;
2073 if (controller->byte_count > req_length) {
2074 controller->byte_count = req_length;
2077 if (controller->byte_count > alloc_length) {
2078 controller->byte_count = alloc_length;
2081 drive->atapi.command = command;
2082 drive->atapi.drq_bytes = controller->byte_count;
2083 drive->atapi.total_bytes_remaining = (req_length < alloc_length) ? req_length : alloc_length;
2086 // // bias drq_bytes and total_bytes_remaining
2087 // SELECTED_DRIVE(channel).atapi.drq_bytes += 2048;
2088 // SELECTED_DRIVE(channel).atapi.total_bytes_remaining += 2048;
2096 void rd_ready_to_send_atapi(struct vm_device * dev, struct channel_t * channel) {
2097 PrintDebug("[rd_ready_to_send_atapi]\n");
2099 rd_raise_interrupt(dev, channel);
2106 void rd_atapi_cmd_error(struct vm_device * dev, struct channel_t * channel, sense_t sense_key, asc_t asc)
2108 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2109 struct controller_t * controller = &(drive->controller);
2112 struct ramdisk_t *ramdisk = (struct ramdisk_t *)(dev->private_data);
2113 PrintError("[rd_atapi_cmd_error]\n");
2114 PrintError("Error: atapi_cmd_error channel=%02x key=%02x asc=%02x\n",
2115 get_channel_no(ramdisk, channel), sense_key, asc);
2118 controller->error_register = sense_key << 4;
2119 controller->interrupt_reason.i_o = 1;
2120 controller->interrupt_reason.c_d = 1;
2121 controller->interrupt_reason.rel = 0;
2122 controller->status.busy = 0;
2123 controller->status.drive_ready = 1;
2124 controller->status.write_fault = 0;
2125 controller->status.drq = 0;
2126 controller->status.err = 1;
2128 drive->sense.sense_key = sense_key;
2129 drive->sense.asc = asc;
2130 drive->sense.ascq = 0;
2135 void rd_atapi_cmd_nop(struct vm_device * dev, struct channel_t * channel)
2137 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2138 struct controller_t * controller = &(drive->controller);
2140 PrintDebug("[rd_atapi_cmd_nop]\n");
2141 controller->interrupt_reason.i_o = 1;
2142 controller->interrupt_reason.c_d = 1;
2143 controller->interrupt_reason.rel = 0;
2144 controller->status.busy = 0;
2145 controller->status.drive_ready = 1;
2146 controller->status.drq = 0;
2147 controller->status.err = 0;
2153 void rd_identify_ATAPI_drive(struct vm_device * dev, struct channel_t * channel)
2155 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2156 struct controller_t * controller = &(drive->controller);
2160 const char* serial_number = " VT00001\0\0\0\0\0\0\0\0\0\0\0\0";
2161 const char* firmware = "ALPHA1 ";
2163 drive->id_drive[0] = (2 << 14) | (5 << 8) | (1 << 7) | (2 << 5) | (0 << 0); // Removable CDROM, 50us response, 12 byte packets
2165 for (i = 1; i <= 9; i++) {
2166 drive->id_drive[i] = 0;
2169 for (i = 0; i < 10; i++) {
2170 drive->id_drive[10 + i] = ((serial_number[i * 2] << 8) |
2171 (serial_number[(i * 2) + 1]));
2174 for (i = 20; i <= 22; i++) {
2175 drive->id_drive[i] = 0;
2178 for (i = 0; i < strlen(firmware)/2; i++) {
2179 drive->id_drive[23 + i] = ((firmware[i * 2] << 8) |
2180 (firmware[(i * 2) + 1]));
2182 V3_ASSERT((23 + i) == 27);
2184 for (i = 0; i < strlen((char *)(drive->model_no)) / 2; i++) {
2185 drive->id_drive[27 + i] = ((drive->model_no[i * 2] << 8) |
2186 (drive->model_no[(i * 2) + 1]));
2188 V3_ASSERT((27 + i) == 47);
2190 drive->id_drive[47] = 0;
2191 drive->id_drive[48] = 1; // 32 bits access
2193 drive->id_drive[49] = (1 << 9); // LBA supported
2195 drive->id_drive[50] = 0;
2196 drive->id_drive[51] = 0;
2197 drive->id_drive[52] = 0;
2199 drive->id_drive[53] = 3; // words 64-70, 54-58 valid
2201 for (i = 54; i <= 62; i++) {
2202 drive->id_drive[i] = 0;
2205 // copied from CFA540A
2206 drive->id_drive[63] = 0x0103; // variable (DMA stuff)
2207 drive->id_drive[64] = 0x0001; // PIO
2208 drive->id_drive[65] = 0x00b4;
2209 drive->id_drive[66] = 0x00b4;
2210 drive->id_drive[67] = 0x012c;
2211 drive->id_drive[68] = 0x00b4;
2213 drive->id_drive[69] = 0;
2214 drive->id_drive[70] = 0;
2215 drive->id_drive[71] = 30; // faked
2216 drive->id_drive[72] = 30; // faked
2217 drive->id_drive[73] = 0;
2218 drive->id_drive[74] = 0;
2220 drive->id_drive[75] = 0;
2222 for (i = 76; i <= 79; i++) {
2223 drive->id_drive[i] = 0;
2226 drive->id_drive[80] = 0x1e; // supports up to ATA/ATAPI-4
2227 drive->id_drive[81] = 0;
2228 drive->id_drive[82] = 0;
2229 drive->id_drive[83] = 0;
2230 drive->id_drive[84] = 0;
2231 drive->id_drive[85] = 0;
2232 drive->id_drive[86] = 0;
2233 drive->id_drive[87] = 0;
2234 drive->id_drive[88] = 0;
2236 for (i = 89; i <= 126; i++) {
2237 drive->id_drive[i] = 0;
2240 drive->id_drive[127] = 0;
2241 drive->id_drive[128] = 0;
2243 for (i = 129; i <= 159; i++) {
2244 drive->id_drive[i] = 0;
2247 for (i = 160; i <= 255; i++) {
2248 drive->id_drive[i] = 0;
2251 // now convert the id_drive array (native 256 word format) to
2252 // the controller buffer (512 bytes)
2254 for (i = 0; i <= 255; i++) {
2255 temp16 = drive->id_drive[i];
2256 controller->buffer[i * 2] = temp16 & 0x00ff;
2257 controller->buffer[i * 2 + 1] = temp16 >> 8;
2270 void rd_init_mode_sense_single(struct vm_device * dev,
2271 struct channel_t * channel, const void* src, int size)
2273 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2274 struct controller_t * controller = &(drive->controller);
2276 PrintDebug("[rd_init_mode_sense_single]\n");
2279 controller->buffer[0] = (size + 6) >> 8;
2280 controller->buffer[1] = (size + 6) & 0xff;
2281 controller->buffer[2] = 0x70; // no media present
2282 controller->buffer[3] = 0; // reserved
2283 controller->buffer[4] = 0; // reserved
2284 controller->buffer[5] = 0; // reserved
2285 controller->buffer[6] = 0; // reserved
2286 controller->buffer[7] = 0; // reserved
2289 memcpy(controller->buffer + 8, src, size);
2294 static void rd_command_aborted(struct vm_device * dev,
2295 struct channel_t * channel, unsigned value) {
2296 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2297 struct controller_t * controller = &(drive->controller);
2299 PrintError("[rd_command_aborted]\n");
2300 PrintError("\t\taborting on command 0x%02x {%s}\n", value, device_type_to_str(drive->device_type));
2302 controller->current_command = 0;
2303 controller->status.busy = 0;
2304 controller->status.drive_ready = 1;
2305 controller->status.err = 1;
2306 controller->error_register = 0x04; // command ABORTED
2307 controller->status.drq = 0;
2308 controller->status.seek_complete = 0;
2309 controller->status.corrected_data = 0;
2310 controller->buffer_index = 0;
2312 rd_raise_interrupt(dev, channel);
2316 static int ramdisk_init_device(struct vm_device *dev) {
2317 struct ramdisk_t *ramdisk= (struct ramdisk_t *)dev->private_data;
2319 PrintDebug("Initializing Ramdisk\n");
2322 rd_init_hardware(ramdisk);
2325 v3_dev_hook_io(dev, PRI_CTRL_PORT,
2326 &read_status_port, &write_ctrl_port);
2328 v3_dev_hook_io(dev, PRI_DATA_PORT,
2329 &read_data_port, &write_data_port);
2330 v3_dev_hook_io(dev, PRI_FEATURES_PORT,
2331 &read_general_port, &write_general_port);
2332 v3_dev_hook_io(dev, PRI_SECT_CNT_PORT,
2333 &read_general_port, &write_general_port);
2334 v3_dev_hook_io(dev, PRI_SECT_ADDR1_PORT,
2335 &read_general_port, &write_general_port);
2336 v3_dev_hook_io(dev, PRI_SECT_ADDR2_PORT,
2337 &read_general_port, &write_general_port);
2338 v3_dev_hook_io(dev, PRI_SECT_ADDR3_PORT,
2339 &read_general_port, &write_general_port);
2340 v3_dev_hook_io(dev, PRI_DRV_SEL_PORT,
2341 &read_general_port, &write_general_port);
2342 v3_dev_hook_io(dev, PRI_CMD_PORT,
2343 &read_status_port, &write_cmd_port);
2346 v3_dev_hook_io(dev, SEC_CTRL_PORT,
2347 &read_status_port, &write_ctrl_port);
2349 v3_dev_hook_io(dev, SEC_DATA_PORT,
2350 &read_data_port, &write_data_port);
2351 v3_dev_hook_io(dev, SEC_FEATURES_PORT,
2352 &read_general_port, &write_general_port);
2353 v3_dev_hook_io(dev, SEC_SECT_CNT_PORT,
2354 &read_general_port, &write_general_port);
2355 v3_dev_hook_io(dev, SEC_SECT_ADDR1_PORT,
2356 &read_general_port, &write_general_port);
2357 v3_dev_hook_io(dev, SEC_SECT_ADDR2_PORT,
2358 &read_general_port, &write_general_port);
2359 v3_dev_hook_io(dev, SEC_SECT_ADDR3_PORT,
2360 &read_general_port, &write_general_port);
2361 v3_dev_hook_io(dev, SEC_DRV_SEL_PORT,
2362 &read_general_port, &write_general_port);
2363 v3_dev_hook_io(dev, SEC_CMD_PORT,
2364 &read_status_port, &write_cmd_port);
2368 v3_dev_hook_io(dev, SEC_ADDR_REG_PORT,
2369 &read_general_port, &write_general_port);
2371 v3_dev_hook_io(dev, PRI_ADDR_REG_PORT,
2372 &read_general_port, &write_general_port);
2381 static int ramdisk_deinit_device(struct vm_device *dev) {
2382 struct ramdisk_t *ramdisk = (struct ramdisk_t *)(dev->private_data);
2383 rd_close_harddrive(ramdisk);
2387 static struct vm_device_ops dev_ops = {
2388 .init = ramdisk_init_device,
2389 .deinit = ramdisk_deinit_device,
2398 struct vm_device * v3_create_ramdisk()
2401 struct ramdisk_t *ramdisk;
2402 ramdisk = (struct ramdisk_t *)V3_Malloc(sizeof(struct ramdisk_t));
2403 V3_ASSERT(ramdisk != NULL);
2405 PrintDebug("[create_ramdisk]\n");
2407 struct vm_device * device = v3_create_device("RAMDISK", &dev_ops, ramdisk);
2415 #ifdef DEBUG_RAMDISK
2417 static void rd_print_state(struct ramdisk_t * ramdisk) {
2420 struct channel_t * channels = (struct channel_t *)(&(ramdisk->channels));
2423 for (channel = 0; channel < MAX_ATA_CHANNEL; channel++) {
2424 memset((char *)(channels + channel), 0, sizeof(struct channel_t));
2427 PrintDebug("sizeof(*channels) = %d\n", sizeof(*channels));
2428 PrintDebug("sizeof(channles->drives[0].controller) = %d\n", sizeof((channels->drives[0].controller)));
2429 PrintDebug("sizeof(channles->drives[0].cdrom) = %d\n", sizeof((channels->drives[0].cdrom)));
2430 PrintDebug("sizeof(channles->drives[0].sense) = %d\n", sizeof((channels->drives[0].sense)));
2431 PrintDebug("sizeof(channles->drives[0].atapi) = %d\n", sizeof((channels->drives[0].atapi)));
2434 PrintDebug("sizeof(channles->drives[0].controller.status) = %d\n",
2435 sizeof((channels->drives[0].controller.status)));
2436 PrintDebug("sizeof(channles->drives[0].controller.sector_count) = %d\n",
2437 sizeof((channels->drives[0].controller.sector_count)));
2438 PrintDebug("sizeof(channles->drives[0].controller.interrupt_reason) = %d\n",
2439 sizeof((channels->drives[0].controller.interrupt_reason)));
2441 PrintDebug("sizeof(channles->drives[0].controller.cylinder_no) = %d\n",
2442 sizeof((channels->drives[0].controller.cylinder_no)));
2443 PrintDebug("sizeof(channles->drives[0].controller.byte_count) = %d\n",
2444 sizeof((channels->drives[0].controller.byte_count)));
2447 PrintDebug("sizeof(channles->drives[0].controller.control) = %d\n",
2448 sizeof((channels->drives[0].controller.control)));
2451 for (channel = 0; channel < MAX_ATA_CHANNEL; channel++){
2453 for (device = 0; device < 2; device++){
2455 // Initialize controller state, even if device is not present
2456 PrintDebug("channels[%d].drives[%d].controller.status.busy = %d\n",
2458 channels[channel].drives[device].controller.status.busy);
2459 PrintDebug("channels[%d].drives[%d].controller.status.drive_ready = %d\n",
2461 channels[channel].drives[device].controller.status.drive_ready);
2462 PrintDebug("channels[%d].drives[%d].controller.status.write_fault = %d\n",
2464 channels[channel].drives[device].controller.status.write_fault);
2465 PrintDebug("channels[%d].drives[%d].controller.status.seek_complete = %d\n",
2467 channels[channel].drives[device].controller.status.seek_complete);
2468 PrintDebug("channels[%d].drives[%d].controller.status.drq = %d\n",
2470 channels[channel].drives[device].controller.status.drq);
2471 PrintDebug("channels[%d].drives[%d].controller.status.corrected_data = %d\n",
2473 channels[channel].drives[device].controller.status.corrected_data);
2474 PrintDebug("channels[%d].drives[%d].controller.status.index_pulse = %d\n",
2476 channels[channel].drives[device].controller.status.index_pulse);
2477 PrintDebug("channels[%d].drives[%d].controller.status.index_pulse_count = %d\n",
2479 channels[channel].drives[device].controller.status.index_pulse_count);
2480 PrintDebug("channels[%d].drives[%d].controller.status.err = %d\n",
2482 channels[channel].drives[device].controller.status.err);
2485 PrintDebug("channels[%d].drives[%d].controller.error_register = %d\n",
2487 channels[channel].drives[device].controller.error_register);
2488 PrintDebug("channels[%d].drives[%d].controller.head_no = %d\n",
2490 channels[channel].drives[device].controller.head_no);
2491 PrintDebug("channels[%d].drives[%d].controller.sector_count = %d\n",
2493 channels[channel].drives[device].controller.sector_count);
2494 PrintDebug("channels[%d].drives[%d].controller.sector_no = %d\n",
2496 channels[channel].drives[device].controller.sector_no);
2497 PrintDebug("channels[%d].drives[%d].controller.cylinder_no = %d\n",
2499 channels[channel].drives[device].controller.cylinder_no);
2500 PrintDebug("channels[%d].drives[%d].controller.current_command = %02x\n",
2502 channels[channel].drives[device].controller.current_command);
2503 PrintDebug("channels[%d].drives[%d].controller.buffer_index = %d\n",
2505 channels[channel].drives[device].controller.buffer_index);
2508 PrintDebug("channels[%d].drives[%d].controller.control.reset = %d\n",
2510 channels[channel].drives[device].controller.control.reset);
2511 PrintDebug("channels[%d].drives[%d].controller.control.disable_irq = %d\n",
2513 channels[channel].drives[device].controller.control.disable_irq);
2516 PrintDebug("channels[%d].drives[%d].controller.reset_in_progress = %d\n",
2518 channels[channel].drives[device].controller.reset_in_progress);
2519 PrintDebug("channels[%d].drives[%d].controller.sectors_per_block = %02x\n",
2521 channels[channel].drives[device].controller.sectors_per_block);
2522 PrintDebug("channels[%d].drives[%d].controller.lba_mode = %d\n",
2524 channels[channel].drives[device].controller.lba_mode);
2525 PrintDebug("channels[%d].drives[%d].controller.features = %d\n",
2527 channels[channel].drives[device].controller.features);
2530 PrintDebug("channels[%d].drives[%d].model_no = %s\n",
2532 channels[channel].drives[device].model_no);
2533 PrintDebug("channels[%d].drives[%d].device_type = %d\n",
2535 channels[channel].drives[device].device_type);
2536 PrintDebug("channels[%d].drives[%d].cdrom.locked = %d\n",
2538 channels[channel].drives[device].cdrom.locked);
2539 PrintDebug("channels[%d].drives[%d].sense.sense_key = %d\n",
2541 channels[channel].drives[device].sense.sense_key);
2542 PrintDebug("channels[%d].drives[%d].sense.asc = %d\n",
2544 channels[channel].drives[device].sense.asc);
2545 PrintDebug("channels[%d].drives[%d].sense.ascq = %d\n",
2547 channels[channel].drives[device].sense.ascq);
2551 PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.c_d = %02x\n",
2553 channels[channel].drives[device].controller.interrupt_reason.c_d);
2555 PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.i_o = %02x\n",
2557 channels[channel].drives[device].controller.interrupt_reason.i_o);
2559 PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.rel = %02x\n",
2561 channels[channel].drives[device].controller.interrupt_reason.rel);
2563 PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.tag = %02x\n",
2565 channels[channel].drives[device].controller.interrupt_reason.tag);
2567 PrintDebug("channels[%d].drives[%d].cdrom.ready = %d\n",
2569 channels[channel].drives[device].cdrom.ready);
2580 static int check_bit_fields(struct controller_t * controller) {
2582 controller->sector_count = 0;
2583 controller->interrupt_reason.c_d = 1;
2584 if (controller->sector_count != 0x01) {
2585 return INTR_REASON_BIT_ERR;
2588 controller->sector_count = 0;
2589 controller->interrupt_reason.i_o = 1;
2590 if (controller->sector_count != 0x02) {
2591 return INTR_REASON_BIT_ERR;
2594 controller->sector_count = 0;
2595 controller->interrupt_reason.rel = 1;
2596 if (controller->sector_count != 0x04) {
2597 return INTR_REASON_BIT_ERR;
2600 controller->sector_count = 0;
2601 controller->interrupt_reason.tag = 3;
2602 if (controller->sector_count != 0x18) {
2603 return INTR_REASON_BIT_ERR;