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...)
58 * Data type definitions
61 #define INDEX_PULSE_CYCLE 10
66 #define INTR_REASON_BIT_ERR 0x01
67 #define UNABLE_FIND_TAT_CHANNEL_ERR 0x02
69 #define READ_BUF_GT_512 0x04
73 #define PRI_DATA_PORT 0x1f0
74 #define PRI_FEATURES_PORT 0x1f1
75 #define PRI_SECT_CNT_PORT 0x1f2
76 #define PRI_SECT_ADDR1_PORT 0x1f3
77 #define PRI_SECT_ADDR2_PORT 0x1f4
78 #define PRI_SECT_ADDR3_PORT 0x1f5
79 #define PRI_DRV_SEL_PORT 0x1f6
80 #define PRI_CMD_PORT 0x1f7
81 #define PRI_CTRL_PORT 0x3f6
82 #define PRI_ADDR_REG_PORT 0x3f7
84 #define SEC_DATA_PORT 0x170
85 #define SEC_FEATURES_PORT 0x171
86 #define SEC_SECT_CNT_PORT 0x172
87 #define SEC_SECT_ADDR1_PORT 0x173
88 #define SEC_SECT_ADDR2_PORT 0x174
89 #define SEC_SECT_ADDR3_PORT 0x175
90 #define SEC_DRV_SEL_PORT 0x176
91 #define SEC_CMD_PORT 0x177
92 #define SEC_CTRL_PORT 0x376
93 #define SEC_ADDR_REG_PORT 0x377
99 #define PACKET_SIZE 12
106 #define ATA_DETECT 0xf0 //0X3E8
107 #define ATA_RESET 0xf1 //0X3E9
108 #define ATA_CMD_DATA_IN 0xf2 //0X3EA
109 #define ATA_CMD_DATA_OUT 0xf3 //0X3EB
110 #define ATA_CMD_PACKET 0xf4 //0X3EC
111 #define ATAPI_GET_SENSE 0xf5 //0X3ED
112 #define ATAPI_IS_READY 0xf6 //0X3EE
113 #define ATAPI_IS_CDROM 0xf7 //0X3EF
115 #define CDEMU_INIT 0xf8 //0X2E8
116 #define CDEMU_ISACTIVE 0xf9 //0X2E9
117 #define CDEMU_EMULATED_DRIVE 0xfa //0X2EA
118 #define CDROM_BOOT 0xfb //0X2EB
121 #define HARD_DRIVE_POST 0xfc //0X2EC
124 #define ATA_DEVICE_NO 0xfd //0X2ED
125 #define ATA_DEVICE_TYPE 0xfe //0X2ED
127 #define INT13_HARDDISK 0xff //0x2ef
128 #define INT13_CDROM 0xe0 //0x2f8
129 #define INT13_CDEMU 0xe1 //0x2f9
130 #define INT13_ELTORITO 0xe2 //0x2fa
131 #define INT13_DISKETTE_FUNCTION 0xe3 //0x2fb
136 static const char cdrom_str[] = "CD-ROM";
137 static const char harddisk_str[] = "HARDDISK";
138 static const char none_str[] = "NONE";
141 static inline const char * device_type_to_str(device_type_t type) {
155 static inline void write_features(struct channel_t * channel, uchar_t value) {
156 channel->drives[0].controller.features = value;
157 channel->drives[1].controller.features = value;
161 static inline void write_sector_count(struct channel_t * channel, uchar_t value) {
162 channel->drives[0].controller.sector_count = value;
163 channel->drives[1].controller.sector_count = value;
166 static inline void write_sector_number(struct channel_t * channel, uchar_t value) {
167 channel->drives[0].controller.sector_no = value;
168 channel->drives[1].controller.sector_no = value;
172 static inline void write_cylinder_low(struct channel_t * channel, uchar_t value) {
173 channel->drives[0].controller.cylinder_no &= 0xff00;
174 channel->drives[0].controller.cylinder_no |= value;
175 channel->drives[1].controller.cylinder_no &= 0xff00;
176 channel->drives[1].controller.cylinder_no |= value;
179 static inline void write_cylinder_high(struct channel_t * channel, uchar_t value) {
180 ushort_t val2 = value;
182 channel->drives[0].controller.cylinder_no &= 0x00ff;
183 channel->drives[0].controller.cylinder_no |= (val2 & 0xff00);
185 channel->drives[1].controller.cylinder_no &= 0x00ff;
186 channel->drives[1].controller.cylinder_no |= (val2 & 0xff00);
189 static inline void write_head_no(struct channel_t * channel, uchar_t value) {
190 channel->drives[0].controller.head_no = value;
191 channel->drives[1].controller.head_no = value;
194 static inline void write_lba_mode(struct channel_t * channel, uchar_t value) {
195 channel->drives[0].controller.lba_mode = value;
196 channel->drives[1].controller.lba_mode = value;
200 static inline uint_t get_channel_no(struct ramdisk_t * ramdisk, struct channel_t * channel) {
201 return (((uchar_t *)channel - (uchar_t *)(ramdisk->channels)) / sizeof(struct channel_t));
204 static inline uint_t get_drive_no(struct channel_t * channel, struct drive_t * drive) {
205 return (((uchar_t *)drive - (uchar_t*)(channel->drives)) / sizeof(struct drive_t));
208 static inline struct drive_t * get_selected_drive(struct channel_t * channel) {
209 return &(channel->drives[channel->drive_select]);
213 static inline int is_primary_port(struct ramdisk_t * ramdisk, ushort_t port) {
217 case PRI_FEATURES_PORT:
218 case PRI_SECT_CNT_PORT:
219 case PRI_SECT_ADDR1_PORT:
220 case PRI_SECT_ADDR2_PORT:
221 case PRI_SECT_ADDR3_PORT:
222 case PRI_DRV_SEL_PORT:
233 static inline int is_secondary_port(struct ramdisk_t * ramdisk, ushort_t port) {
237 case SEC_FEATURES_PORT:
238 case SEC_SECT_CNT_PORT:
239 case SEC_SECT_ADDR1_PORT:
240 case SEC_SECT_ADDR2_PORT:
241 case SEC_SECT_ADDR3_PORT:
242 case SEC_DRV_SEL_PORT:
251 static inline int num_drives_on_channel(struct channel_t * channel) {
252 if ((channel->drives[0].device_type == IDE_NONE) &&
253 (channel->drives[1].device_type == IDE_NONE)) {
255 } else if ((channel->drives[0].device_type != IDE_NONE) &&
256 (channel->drives[1].device_type != IDE_NONE)) {
265 static inline uchar_t extract_bits(uchar_t * buf, uint_t buf_offset, uint_t bit_offset, uint_t num_bits) {
266 uchar_t val = buf[buf_offset];
267 val = val >> bit_offset;
268 val &= ((1 << num_bits) -1);
273 static inline uchar_t get_packet_field(struct channel_t * channel, uint_t packet_offset, uint_t bit_offset, uint_t num_bits) {
274 struct drive_t * drive = get_selected_drive(channel);
275 return extract_bits(drive->controller.buffer, packet_offset, bit_offset, num_bits);
279 static inline uchar_t get_packet_byte(struct channel_t * channel, uint_t offset) {
280 struct drive_t * drive = get_selected_drive(channel);
281 return drive->controller.buffer[offset];
284 static inline uint16_t get_packet_word(struct channel_t * channel, uint_t offset) {
285 struct drive_t * drive = get_selected_drive(channel);
286 uint16_t val = drive->controller.buffer[offset];
288 val |= drive->controller.buffer[offset + 1];
293 static inline uint16_t rd_read_16bit(const uint8_t* buf) {
294 return (buf[0] << 8) | buf[1];
299 static inline uint32_t rd_read_32bit(const uint8_t* buf) {
300 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
303 ////////////////////////////////////////////////////////////////////////////
311 static void rd_init_mode_sense_single(struct vm_device * dev, struct channel_t * channel, const void * src, int size);
313 static void rd_command_aborted(struct vm_device * dev, struct channel_t * channel, unsigned value);
318 static int handle_atapi_packet_command(struct vm_device * dev,
319 struct channel_t * channel,
322 static int rd_init_send_atapi_command(struct vm_device * dev,
323 struct channel_t * channel,
324 Bit8u command, int req_length,
325 int alloc_length, bool lazy);
327 static void rd_ready_to_send_atapi(struct vm_device * dev,
328 struct channel_t * channel);
330 static void rd_atapi_cmd_error(struct vm_device * dev,
331 struct channel_t * channel,
332 sense_t sense_key, asc_t asc);
334 static void rd_atapi_cmd_nop(struct vm_device * dev, struct channel_t * channel);
335 static void rd_identify_ATAPI_drive(struct vm_device * dev, struct channel_t * channel);
342 static void rd_raise_interrupt(struct vm_device * dev, struct channel_t * channel);
343 static void rd_lower_irq(struct vm_device *dev, struct channel_t * channel);
354 static void rd_print_state(struct ramdisk_t *ramdisk);
355 static int check_bit_fields(struct controller_t * controller);
359 ////////////////////////////////////////////////////////////////////
365 int v3_ramdisk_register_cdrom(struct vm_device * dev, uint_t busID, uint_t driveID, struct cdrom_ops* cd, void * private_data) {
366 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
367 struct channel_t * channel = &(ramdisk->channels[busID]);
368 struct drive_t * drive = &(channel->drives[driveID]);
369 struct controller_t * controller = &(drive->controller);
373 if (drive->device_type != IDE_NONE) {
374 PrintError("Device already registered at this location\n");
382 strncpy((char*)(drive->model_no), "V3VEE Ramdisk", 40);
384 while (strlen((char *)(drive->model_no)) < 40) {
385 strcat ((char*)(drive->model_no), " ");
388 PrintDebug("CDROM on target %d/%d\n", busID, driveID);
390 drive->device_type = IDE_CDROM;
391 drive->cdrom.locked = 0;
392 drive->sense.sense_key = SENSE_NONE;
393 drive->sense.asc = 0;
394 drive->sense.ascq = 0;
396 drive->private_data = private_data;
400 if (check_bit_fields(controller) == INTR_REASON_BIT_ERR) {
401 PrintError("interrupt reason: bit field error\n");
402 return INTR_REASON_BIT_ERR;
406 controller->sector_count = 0;
408 drive->cdrom.cd = cd;
410 PrintDebug("\t\tCD on ata%d-%d: '%s'\n",
414 if(drive->cdrom.cd->insert_cdrom(drive->private_data)) {
415 PrintDebug("\t\tMedia present in CD-ROM drive\n");
416 drive->cdrom.ready = 1;
417 drive->cdrom.capacity = drive->cdrom.cd->capacity(drive->private_data);
418 PrintDebug("\t\tCDROM capacity is %d\n", drive->cdrom.capacity);
420 PrintDebug("\t\tCould not locate CD-ROM, continuing with media not present\n");
421 drive->cdrom.ready = 0;
428 static Bit32u rd_init_hardware(struct ramdisk_t *ramdisk) {
431 struct channel_t *channels = (struct channel_t *)(&(ramdisk->channels));
433 PrintDebug("[rd_init_harddrive]\n");
435 for (channel_num = 0; channel_num < MAX_ATA_CHANNEL; channel_num++) {
436 memset((char *)(channels + channel_num), 0, sizeof(struct channel_t));
439 for (channel_num = 0; channel_num < MAX_ATA_CHANNEL; channel_num++){
440 struct channel_t * channel = &(channels[channel_num]);
442 channel->ioaddr1 = 0x0;
443 channel->ioaddr2 = 0x0;
446 for (device = 0; device < 2; device++){
447 struct drive_t * drive = &(channel->drives[device]);
448 struct controller_t * controller = &(drive->controller);
450 controller->status.busy = 0;
451 controller->status.drive_ready = 1;
452 controller->status.write_fault = 0;
453 controller->status.seek_complete = 1;
454 controller->status.drq = 0;
455 controller->status.corrected_data = 0;
456 controller->status.index_pulse = 0;
457 controller->status.index_pulse_count = 0;
458 controller->status.err = 0;
460 controller->error_register = 0x01; // diagnostic code: no error
461 controller->head_no = 0;
462 controller->sector_count = 1;
463 controller->sector_no = 1;
464 controller->cylinder_no = 0;
465 controller->current_command = 0x00;
466 controller->buffer_index = 0;
468 controller->control.reset = 0;
469 controller->control.disable_irq = 0;
470 controller->reset_in_progress = 0;
472 controller->sectors_per_block = 0x80;
473 controller->lba_mode = 0;
476 controller->features = 0;
479 drive->device_type = IDE_NONE;
482 strncpy((char*)(drive->model_no), "", 40);
483 while(strlen((char *)(drive->model_no)) < 40) {
484 strcat ((char*)(drive->model_no), " ");
491 rd_print_state(ramdisk);
498 static void rd_reset_harddrive(struct ramdisk_t *ramdisk, unsigned type) {
503 static void rd_close_harddrive(struct ramdisk_t *ramdisk) {
508 ////////////////////////////////////////////////////////////////////
512 static int read_data_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
513 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
514 struct channel_t * channel = NULL;
515 struct drive_t * drive = NULL;
516 struct controller_t * controller = NULL;
520 if (is_primary_port(ramdisk, port)) {
521 channel = &(ramdisk->channels[0]);
522 } else if (is_secondary_port(ramdisk, port)) {
523 channel = &(ramdisk->channels[1]);
525 PrintError("Invalid Port: %d\n", port);
529 drive = get_selected_drive(channel);
530 controller = &(drive->controller);
533 PrintTrace("[read_data_handler] IO Read at 0x%x, on drive %d/%d (current_cmd = 0x%02x)\n",
535 get_channel_no(ramdisk, channel),
536 get_drive_no(channel, drive),
537 controller->current_command);
539 switch (controller->current_command) {
540 case 0xec: // IDENTIFY DEVICE
545 controller->status.busy = 0;
546 controller->status.drive_ready = 1;
547 controller->status.write_fault = 0;
548 controller->status.seek_complete = 1;
549 controller->status.corrected_data = 0;
550 controller->status.err = 0;
553 value32 = controller->buffer[index];
557 value32 |= (controller->buffer[index] << 8);
562 value32 |= (controller->buffer[index] << 16);
563 value32 |= (controller->buffer[index+1] << 24);
567 controller->buffer_index = index;
570 memcpy(dst, controller->buffer + controller->buffer_index, length);
571 controller->buffer_index += length;
573 if (controller->buffer_index >= 512) {
574 controller->status.drq = 0;
579 case 0xa0: //send packet cmd
581 uint_t index = controller->buffer_index;
584 PrintTrace("\t\tatapi.command(%02x), index(%d), cdrom.remaining_blocks(%d)\n",
585 drive->atapi.command,
587 drive->cdrom.remaining_blocks);
589 // Load block if necessary
593 PrintError("\t\tindex > 2048 : 0x%x\n", index);
597 switch (drive->atapi.command) {
598 case 0x28: // read (10)
599 case 0xa8: // read (12)
602 if (!(drive->cdrom.ready)) {
603 PrintError("\t\tRead with CDROM not ready\n");
607 drive->cdrom.cd->read_block(drive->private_data, controller->buffer,
608 drive->cdrom.next_lba);
609 drive->cdrom.next_lba++;
610 drive->cdrom.remaining_blocks--;
613 if (!(drive->cdrom.remaining_blocks)) {
614 PrintDebug("\t\tLast READ block loaded {CDROM}\n");
616 PrintDebug("\t\tREAD block loaded (%d remaining) {CDROM}\n",
617 drive->cdrom.remaining_blocks);
620 // one block transfered, start at beginning
624 default: // no need to load a new block
632 value32 = controller->buffer[index + increment];
636 value32 |= (controller->buffer[index + increment] << 8);
641 value32 |= (controller->buffer[index + increment] << 16);
642 value32 |= (controller->buffer[index + increment + 1] << 24);
646 controller->buffer_index = index + increment;
647 controller->drq_index += increment;
650 /* JRL: CHECK THAT there is enough data in the buffer to copy.... */
652 memcpy(dst, controller->buffer + index, length);
654 controller->buffer_index = index + length;
655 controller->drq_index += length;
660 if (controller->drq_index >= (unsigned)drive->atapi.drq_bytes) {
661 controller->status.drq = 0;
662 controller->drq_index = 0;
664 drive->atapi.total_bytes_remaining -= drive->atapi.drq_bytes;
666 if (drive->atapi.total_bytes_remaining > 0) {
667 // one or more blocks remaining (works only for single block commands)
669 PrintDebug("\t\tPACKET drq bytes read\n");
670 controller->interrupt_reason.i_o = 1;
671 controller->status.busy = 0;
672 controller->status.drq = 1;
673 controller->interrupt_reason.c_d = 0;
675 // set new byte count if last block
676 if (drive->atapi.total_bytes_remaining < controller->byte_count) {
677 controller->byte_count = drive->atapi.total_bytes_remaining;
679 drive->atapi.drq_bytes = controller->byte_count;
681 rd_raise_interrupt(dev, channel);
684 PrintDebug("\t\tPACKET all bytes read\n");
686 controller->interrupt_reason.i_o = 1;
687 controller->interrupt_reason.c_d = 1;
688 controller->status.drive_ready = 1;
689 controller->interrupt_reason.rel = 0;
690 controller->status.busy = 0;
691 controller->status.drq = 0;
692 controller->status.err = 0;
694 rd_raise_interrupt(dev, channel);
702 PrintError("\t\tunsupported command: %02x\n", controller->current_command);
712 static int write_data_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
713 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
714 struct channel_t * channel = NULL;
715 struct drive_t * drive = NULL;
716 struct controller_t * controller = NULL;
718 if (is_primary_port(ramdisk, port)) {
719 channel = &(ramdisk->channels[0]);
720 } else if (is_secondary_port(ramdisk, port)) {
721 channel = &(ramdisk->channels[1]);
723 PrintError("Invalid Port: %d\n", port);
727 drive = get_selected_drive(channel);
728 controller = &(drive->controller);
730 PrintDebug("[write_data_handler] IO write at 0x%x, current_cmd = 0x%02x\n",
731 port, controller->current_command);
733 switch (controller->current_command) {
734 case 0x30: // WRITE SECTORS
735 PrintError("\t\tneed to implement 0x30(write sector) to port 0x%x\n", port);
740 if (handle_atapi_packet_command(dev, channel, *(ushort_t *)src) == -1) {
741 PrintError("Error sending atapi packet command in PACKET write to data port\n");
748 PrintError("\t\tIO write(0x%x): current command is %02xh\n",
749 port, controller->current_command);
761 static int read_status_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
762 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
763 struct channel_t * channel = NULL;
764 struct drive_t * drive = NULL;
765 struct controller_t * controller = NULL;
769 PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
773 if (is_primary_port(ramdisk, port)) {
774 channel = &(ramdisk->channels[0]);
775 } else if (is_secondary_port(ramdisk, port)) {
776 channel = &(ramdisk->channels[1]);
778 PrintError("Invalid Port: %d\n", port);
782 drive = get_selected_drive(channel);
783 controller = &(drive->controller);
785 PrintDebug("[read_status_handler] IO read at 0x%x, on drive %d/%d\n",
786 port, get_channel_no(ramdisk, channel),
787 channel->drive_select);
790 if (num_drives_on_channel(channel) == 0) {
791 PrintDebug("Setting value to zero because 0 devices on channel\n");
792 // (mch) Just return zero for these registers
793 memset(dst, 0, length);
797 (controller->status.busy << 7) |
798 (controller->status.drive_ready << 6) |
799 (controller->status.write_fault << 5) |
800 (controller->status.seek_complete << 4) |
801 (controller->status.drq << 3) |
802 (controller->status.corrected_data << 2) |
803 (controller->status.index_pulse << 1) |
804 (controller->status.err) );
806 memcpy(dst, &val, length);
808 controller->status.index_pulse_count++;
809 controller->status.index_pulse = 0;
811 if (controller->status.index_pulse_count >= INDEX_PULSE_CYCLE) {
812 controller->status.index_pulse = 1;
813 controller->status.index_pulse_count = 0;
817 if ((port == SEC_CMD_PORT) || (port == PRI_CMD_PORT)) {
818 rd_lower_irq(dev, channel);
826 static int write_cmd_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
827 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
828 struct channel_t * channel = NULL;
829 struct drive_t * drive = NULL;
830 struct controller_t * controller = NULL;
831 uchar_t value = *(uchar_t *)src;
834 PrintError("Invalid Command port write length: %d (port=%d)\n", length, port);
838 if (is_primary_port(ramdisk, port)) {
839 channel = &(ramdisk->channels[0]);
840 } else if (is_secondary_port(ramdisk, port)) {
841 channel = &(ramdisk->channels[1]);
843 PrintError("Invalid Port: %d\n", port);
847 drive = get_selected_drive(channel);
848 controller = &(drive->controller);
851 PrintDebug("[write_command_handler] IO write at 0x%x, on drive %d/%d (val = 0x%x)\n",
852 port, get_channel_no(ramdisk, channel),
853 get_drive_no(channel, drive),
858 case 0xec: // IDENTIFY DEVICE
861 if (drive->device_type == IDE_NONE) {
862 PrintError("\t\tError: disk ata%d-%d not present, aborting\n",
863 get_channel_no(ramdisk, channel),
864 get_drive_no(channel, drive));
865 rd_command_aborted(dev, channel, value);
867 } else if (drive->device_type == IDE_CDROM) {
868 PrintDebug("Identifying CDROM...Going to abort????\n");
869 controller->head_no = 0;
870 controller->sector_count = 1;
871 controller->sector_no = 1;
872 controller->cylinder_no = 0xeb14;
873 rd_command_aborted(dev, channel, 0xec);
875 PrintError("\t\tError: Want to identify HDD!!\n");
877 SELECTED_CONTROLLER(channel).current_command = value;
878 SELECTED_CONTROLLER(channel).error_register = 0;
880 // See ATA/ATAPI-4, 8.12
881 SELECTED_CONTROLLER(channel).status.busy = 0;
882 SELECTED_CONTROLLER(channel).status.drive_ready = 1;
883 SELECTED_CONTROLLER(channel).status.write_fault = 0;
884 SELECTED_CONTROLLER(channel).status.drq = 1;
885 SELECTED_CONTROLLER(channel).status.err = 0;
887 SELECTED_CONTROLLER(channel).status.seek_complete = 1;
888 SELECTED_CONTROLLER(channel).status.corrected_data = 0;
890 SELECTED_CONTROLLER(channel).buffer_index = 0;
891 raise_interrupt(channel);
892 identify_drive(channel);
900 case 0xa1: // IDENTIFY PACKET DEVICE
902 if (drive->device_type == IDE_CDROM) {
903 controller->current_command = value;
904 controller->error_register = 0;
906 controller->status.busy = 0;
907 controller->status.drive_ready = 1;
908 controller->status.write_fault = 0;
909 controller->status.drq = 1;
910 controller->status.err = 0;
912 controller->status.seek_complete = 1;
913 controller->status.corrected_data = 0;
915 controller->buffer_index = 0;
916 rd_raise_interrupt(dev, channel);
917 rd_identify_ATAPI_drive(dev, channel);
919 PrintError("Identifying non cdrom device not supported - ata %d/%d\n",
920 get_channel_no(ramdisk, channel),
921 get_drive_no(channel, drive));
922 rd_command_aborted(dev, channel, 0xa1);
926 case 0xa0: // SEND PACKET (atapi)
928 if (drive->device_type == IDE_CDROM) {
931 if (controller->features & (1 << 0)) {
932 PrintError("\t\tPACKET-DMA not supported");
936 if (controller->features & (1 << 1)) {
937 PrintError("\t\tPACKET-overlapped not supported");
941 // We're already ready!
942 controller->sector_count = 1;
943 controller->status.busy = 0;
944 controller->status.write_fault = 0;
947 controller->status.drq = 1;
948 controller->status.err = 0;
950 // NOTE: no interrupt here
951 controller->current_command = value;
952 controller->buffer_index = 0;
954 PrintError("Sending packet to non cdrom device not supported\n");
955 rd_command_aborted (dev, channel, 0xa0);
960 PrintError("\t\tneed translate command %2x - ata %d\%d\n", value,
961 get_channel_no(ramdisk, channel),
962 get_drive_no(channel, drive));
964 /* JRL THIS NEEDS TO CHANGE */
972 static int write_ctrl_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
973 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
974 struct channel_t * channel = NULL;
975 struct drive_t * master_drive = NULL;
976 struct drive_t * slave_drive = NULL;
977 struct controller_t * controller = NULL;
978 uchar_t value = *(uchar_t *)src;
979 rd_bool prev_control_reset;
982 PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
986 if (is_primary_port(ramdisk, port)) {
987 channel = &(ramdisk->channels[0]);
988 } else if (is_secondary_port(ramdisk, port)) {
989 channel = &(ramdisk->channels[1]);
991 PrintError("Invalid Port: %d\n", port);
995 master_drive = &(channel->drives[0]);
996 slave_drive = &(channel->drives[1]);
998 controller = &(get_selected_drive(channel)->controller);
1001 PrintDebug("[write_control_handler] IO write at 0x%x, on drive %d/%d (val = 0x%x)\n",
1002 port, get_channel_no(ramdisk, channel),
1003 channel->drive_select,
1006 // (mch) Even if device 1 was selected, a write to this register
1007 // goes to device 0 (if device 1 is absent)
1009 prev_control_reset = controller->control.reset;
1011 master_drive->controller.control.reset = value & 0x04;
1012 slave_drive->controller.control.reset = value & 0x04;
1014 // CGS: was: SELECTED_CONTROLLER(channel).control.disable_irq = value & 0x02;
1015 master_drive->controller.control.disable_irq = value & 0x02;
1016 slave_drive->controller.control.disable_irq = value & 0x02;
1018 PrintDebug("\t\tadpater control reg: reset controller = %d\n",
1019 (unsigned) (controller->control.reset) ? 1 : 0);
1020 PrintDebug("\t\tadpater control reg: disable_irq(X) = %d\n",
1021 (unsigned) (controller->control.disable_irq) ? 1 : 0);
1023 if ((!prev_control_reset) && (controller->control.reset)) {
1026 // transition from 0 to 1 causes all drives to reset
1027 PrintDebug("\t\thard drive: RESET\n");
1029 // (mch) Set BSY, drive not ready
1030 for (id = 0; id < 2; id++) {
1031 struct controller_t * ctrl = NULL;
1034 ctrl = &(master_drive->controller);
1035 } else if (id == 1) {
1036 ctrl = &(slave_drive->controller);
1039 ctrl->status.busy = 1;
1040 ctrl->status.drive_ready = 0;
1041 ctrl->reset_in_progress = 1;
1043 ctrl->status.write_fault = 0;
1044 ctrl->status.seek_complete = 1;
1045 ctrl->status.drq = 0;
1046 ctrl->status.corrected_data = 0;
1047 ctrl->status.err = 0;
1049 ctrl->error_register = 0x01; // diagnostic code: no error
1051 ctrl->current_command = 0x00;
1052 ctrl->buffer_index = 0;
1054 ctrl->sectors_per_block = 0x80;
1057 ctrl->control.disable_irq = 0;
1060 rd_lower_irq(dev, channel);
1062 } else if ((controller->reset_in_progress) &&
1063 (!controller->control.reset)) {
1065 // Clear BSY and DRDY
1066 PrintDebug("\t\tReset complete {%s}\n", device_type_to_str(get_selected_drive(channel)->device_type));
1068 for (id = 0; id < 2; id++) {
1069 struct controller_t * ctrl = NULL;
1070 struct drive_t * drv = NULL;
1073 ctrl = &(master_drive->controller);
1075 } else if (id == 1) {
1076 ctrl = &(slave_drive->controller);
1080 ctrl->status.busy = 0;
1081 ctrl->status.drive_ready = 1;
1082 ctrl->reset_in_progress = 0;
1085 if (drv->device_type == IDE_DISK) {
1086 PrintDebug("\t\tdrive %d/%d is harddrive\n", get_channel_no(ramdisk, channel), id);
1088 ctrl->sector_count = 1;
1089 ctrl->sector_no = 1;
1090 ctrl->cylinder_no = 0;
1093 ctrl->sector_count = 1;
1094 ctrl->sector_no = 1;
1095 ctrl->cylinder_no = 0xeb14;
1100 PrintDebug("\t\ts[0].controller.control.disable_irq = %02x\n",
1101 master_drive->controller.control.disable_irq);
1102 PrintDebug("\t\ts[1].controller.control.disable_irq = %02x\n",
1103 slave_drive->controller.control.disable_irq);
1108 static int read_general_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
1109 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1110 struct channel_t * channel = NULL;
1111 struct drive_t * drive = NULL;
1112 struct controller_t * controller = NULL;
1116 PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
1120 if (is_primary_port(ramdisk, port)) {
1121 channel = &(ramdisk->channels[0]);
1122 } else if (is_secondary_port(ramdisk, port)) {
1123 channel = &(ramdisk->channels[1]);
1125 PrintError("Invalid Port: %d\n", port);
1129 drive = get_selected_drive(channel);
1130 controller = &(drive->controller);
1133 PrintDebug("[read_general_handler] IO read addr at %x, on drive %d/%d, curcmd = %02x\n",
1134 port, get_channel_no(ramdisk, channel),
1135 channel->drive_select,
1136 controller->current_command);
1140 case PRI_FEATURES_PORT:
1141 case SEC_FEATURES_PORT: // hard disk error register 0x1f1
1143 uchar_t val = (drive->device_type == IDE_NONE) ? 0 : controller->error_register;
1145 controller->status.err = 0;
1147 *(uchar_t *)dst = val;
1153 case PRI_SECT_CNT_PORT:
1154 case SEC_SECT_CNT_PORT: // hard disk sector count / interrupt reason 0x1f2
1156 uchar_t val = (drive->device_type == IDE_NONE) ? 0 : controller->sector_count;
1158 *(uchar_t *)dst = val;
1163 case PRI_SECT_ADDR1_PORT:
1164 case SEC_SECT_ADDR1_PORT: // sector number 0x1f3
1166 uchar_t val = (drive->device_type == IDE_NONE) ? 0 : controller->sector_no;
1168 *(uchar_t *)dst = val;
1174 case PRI_SECT_ADDR2_PORT:
1175 case SEC_SECT_ADDR2_PORT: // cylinder low 0x1f4
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 & 0x00ff);
1182 *(uchar_t *)dst = val;
1188 case PRI_SECT_ADDR3_PORT:
1189 case SEC_SECT_ADDR3_PORT: // cylinder high 0x1f5
1191 // -- WARNING : On real hardware the controller registers are shared between drives.
1192 // So we must respond even if the select device is not present. Some OS uses this fact
1193 // to detect the disks.... minix2 for example
1194 uchar_t val = (num_drives_on_channel(channel) == 0) ? 0 : (controller->cylinder_no >> 8);
1196 *(uchar_t *)dst = val;
1201 case PRI_DRV_SEL_PORT:
1202 case SEC_DRV_SEL_PORT: // hard disk drive and head register 0x1f6
1204 // b7 Extended data field for ECC
1205 // b6/b5: Used to be sector size. 00=256,01=512,10=1024,11=128
1206 // Since 512 was always used, bit 6 was taken to mean LBA mode:
1207 // b6 1=LBA mode, 0=CHS mode
1211 uchar_t val = ((1 << 7) |
1212 ((controller->lba_mode > 0) << 6) |
1213 (1 << 5) | // 01b = 512 sector size
1214 (channel->drive_select << 4) |
1215 (controller->head_no << 0));
1217 *(uchar_t *)dst = val;
1222 case PRI_ADDR_REG_PORT:
1223 case SEC_ADDR_REG_PORT: // Hard Disk Address Register 0x3f7
1225 // Obsolete and unsupported register. Not driven by hard
1226 // disk controller. Report all 1's. If floppy controller
1227 // is handling this address, it will call this function
1228 // set/clear D7 (the only bit it handles), then return
1229 // the combined value
1230 *(uchar_t *)dst = 0xff;
1235 PrintError("Invalid Port: %d\n", port);
1243 static int write_general_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
1244 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1245 struct channel_t * channel = NULL;
1246 struct drive_t * drive = NULL;
1247 struct controller_t * controller = NULL;
1248 uchar_t value = *(uchar_t *)src;
1251 PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
1255 if (is_primary_port(ramdisk, port)) {
1256 channel = &(ramdisk->channels[0]);
1257 } else if (is_secondary_port(ramdisk, port)) {
1258 channel = &(ramdisk->channels[1]);
1260 PrintError("Invalid Port: %d\n", port);
1264 drive = get_selected_drive(channel);
1265 controller = &(drive->controller);
1268 PrintDebug("[write_general_handler] IO write to port %x (val=0x%02x), channel = %d\n",
1269 port, value, get_channel_no(ramdisk, channel));
1273 case PRI_FEATURES_PORT:
1274 case SEC_FEATURES_PORT: // hard disk write precompensation 0x1f1
1276 write_features(channel, value);
1279 case PRI_SECT_CNT_PORT:
1280 case SEC_SECT_CNT_PORT: // hard disk sector count 0x1f2
1282 write_sector_count(channel, value);
1285 case PRI_SECT_ADDR1_PORT:
1286 case SEC_SECT_ADDR1_PORT: // hard disk sector number 0x1f3
1288 write_sector_number(channel, value);
1291 case PRI_SECT_ADDR2_PORT:
1292 case SEC_SECT_ADDR2_PORT: // hard disk cylinder low 0x1f4
1294 write_cylinder_low(channel, value);
1297 case PRI_SECT_ADDR3_PORT:
1298 case SEC_SECT_ADDR3_PORT: // hard disk cylinder high 0x1f5
1300 write_cylinder_high(channel, value);
1303 case PRI_DRV_SEL_PORT:
1304 case SEC_DRV_SEL_PORT: // hard disk drive and head register 0x1f6
1306 // b7 Extended data field for ECC
1307 // b6/b5: Used to be sector size. 00=256,01=512,10=1024,11=128
1308 // Since 512 was always used, bit 6 was taken to mean LBA mode:
1309 // b6 1=LBA mode, 0=CHS mode
1316 PrintDebug("\tDrive Select value=%x\n", value);
1318 if ((value & 0xa0) != 0xa0) {
1319 PrintDebug("\t\tIO write 0x%x (%02x): not 1x1xxxxxb\n", port, (unsigned) value);
1322 write_head_no(channel, value & 0xf);
1323 if ((controller->lba_mode == 0) && (((value >> 6) & 1) == 1)) {
1324 PrintDebug("\t\tenabling LBA mode\n");
1327 write_lba_mode(channel, (value >> 6) & 1);
1331 if (drive->cdrom.cd) {
1332 PrintDebug("\t\tSetting LBA on CDROM: %d\n", (value >> 6) & 1);
1333 drive->cdrom.cd->set_LBA(drive->private_data, (value >> 6) & 1);
1337 channel->drive_select = (value >> 4) & 0x01;
1338 drive = get_selected_drive(channel);
1340 if (drive->device_type == IDE_NONE) {
1341 PrintError("\t\tError: device set to %d which does not exist! channel = 0x%x\n",
1342 channel->drive_select, get_channel_no(ramdisk, channel));
1344 controller->error_register = 0x04; // aborted
1345 controller->status.err = 1;
1351 PrintError("\t\thard drive: io write to unhandled port 0x%x (value = %c)\n", port, value);
1362 static void rd_raise_interrupt(struct vm_device * dev, struct channel_t * channel) {
1363 // struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1364 struct drive_t * drive = get_selected_drive(channel);
1365 struct controller_t * controller = &(drive->controller);
1367 PrintDebug("[raise_interrupt] disable_irq = 0x%02x\n", controller->control.disable_irq);
1369 if (!(controller->control.disable_irq)) {
1371 PrintDebug("\t\tRaising interrupt %d {%s}\n\n", channel->irq, device_type_to_str(drive->device_type));
1373 v3_raise_irq(dev->vm, channel->irq);
1375 PrintError("\t\tRaising irq but irq is disabled\n");
1381 static void rd_lower_irq(struct vm_device *dev, struct channel_t * channel) {
1382 PrintDebug("[lower_irq] irq = %d\n", channel->irq);
1383 v3_lower_irq(dev->vm, channel->irq);
1392 //////////////////////////////////////////////////////////////////////////
1400 int handle_atapi_packet_command(struct vm_device * dev, struct channel_t * channel, ushort_t value) {
1401 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1402 struct drive_t * drive = get_selected_drive(channel);
1403 struct controller_t * controller = &(drive->controller);
1405 if (controller->buffer_index >= PACKET_SIZE) {
1406 PrintError("ATAPI packet exceeded maximum length: buffer_index (%d) >= PACKET_SIZE\n",
1407 controller->buffer_index);
1411 controller->buffer[controller->buffer_index] = value;
1412 controller->buffer[controller->buffer_index + 1] = (value >> 8);
1413 controller->buffer_index += 2;
1416 /* if packet completely writtten */
1417 if (controller->buffer_index >= PACKET_SIZE) {
1418 // complete command received
1419 Bit8u atapi_command = controller->buffer[0];
1421 PrintDebug("\t\tcdrom: ATAPI command 0x%x started\n", atapi_command);
1423 switch (atapi_command) {
1424 case 0x00: // test unit ready
1426 PrintDebug("Testing unit ready\n");
1427 if (drive->cdrom.ready) {
1428 rd_atapi_cmd_nop(dev, channel);
1430 PrintError("CDROM not ready in test unit ready\n");
1431 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1434 rd_raise_interrupt(dev, channel);
1438 case 0x03: // request sense
1440 int alloc_length = controller->buffer[4];
1442 if (rd_init_send_atapi_command(dev, channel, atapi_command, 18, alloc_length, false) == -1) {
1443 PrintError("Error sending atapi command in Request Sense\n");
1448 controller->buffer[0] = 0x70 | (1 << 7);
1449 controller->buffer[1] = 0;
1450 controller->buffer[2] = drive->sense.sense_key;
1451 controller->buffer[3] = drive->sense.information.arr[0];
1452 controller->buffer[4] = drive->sense.information.arr[1];
1453 controller->buffer[5] = drive->sense.information.arr[2];
1454 controller->buffer[6] = drive->sense.information.arr[3];
1455 controller->buffer[7] = 17 - 7;
1456 controller->buffer[8] = drive->sense.specific_inf.arr[0];
1457 controller->buffer[9] = drive->sense.specific_inf.arr[1];
1458 controller->buffer[10] = drive->sense.specific_inf.arr[2];
1459 controller->buffer[11] = drive->sense.specific_inf.arr[3];
1460 controller->buffer[12] = drive->sense.asc;
1461 controller->buffer[13] = drive->sense.ascq;
1462 controller->buffer[14] = drive->sense.fruc;
1463 controller->buffer[15] = drive->sense.key_spec.arr[0];
1464 controller->buffer[16] = drive->sense.key_spec.arr[1];
1465 controller->buffer[17] = drive->sense.key_spec.arr[2];
1467 rd_ready_to_send_atapi(dev, channel);
1470 case 0x1b: // start stop unit
1472 //bx_bool Immed = (controller->buffer[1] >> 0) & 1;
1473 rd_bool LoEj = (controller->buffer[4] >> 1) & 1;
1474 rd_bool Start = (controller->buffer[4] >> 0) & 1;
1477 if ((!LoEj) && (!Start)) {
1478 PrintError("FIXME: Stop disc not implemented\n");
1480 rd_atapi_cmd_nop(dev, channel);
1481 rd_raise_interrupt(dev, channel);
1483 } else if (!LoEj && Start) { // start (spin up) the disc
1485 drive->cdrom.cd->start_cdrom(drive->private_data);
1487 PrintError("FIXME: ATAPI start disc not reading TOC\n");
1488 rd_atapi_cmd_nop(dev, channel);
1489 rd_raise_interrupt(dev, channel);
1491 } else if (LoEj && !Start) { // Eject the disc
1492 rd_atapi_cmd_nop(dev, channel);
1493 PrintDebug("Ejecting Disk\n");
1494 if (drive->cdrom.ready) {
1496 drive->cdrom.cd->eject_cdrom(drive->private_data);
1498 drive->cdrom.ready = 0;
1499 //bx_options.atadevice[channel][SLAVE_SELECTED(channel)].Ostatus->set(EJECTED);
1500 //bx_gui->update_drive_status_buttons();
1502 rd_raise_interrupt(dev, channel);
1504 } else { // Load the disc
1505 // My guess is that this command only closes the tray, that's a no-op for us
1506 rd_atapi_cmd_nop(dev, channel);
1507 rd_raise_interrupt(dev, channel);
1511 case 0xbd: // mechanism status
1513 uint16_t alloc_length = rd_read_16bit(controller->buffer + 8);
1515 if (alloc_length == 0) {
1516 PrintError("Zero allocation length to MECHANISM STATUS not impl.\n");
1520 if (rd_init_send_atapi_command(dev, channel, atapi_command, 8, alloc_length, false) == -1) {
1521 PrintError("Error sending atapi command in mechanism status\n");
1525 controller->buffer[0] = 0; // reserved for non changers
1526 controller->buffer[1] = 0; // reserved for non changers
1528 controller->buffer[2] = 0; // Current LBA (TODO!)
1529 controller->buffer[3] = 0; // Current LBA (TODO!)
1530 controller->buffer[4] = 0; // Current LBA (TODO!)
1532 controller->buffer[5] = 1; // one slot
1534 controller->buffer[6] = 0; // slot table length
1535 controller->buffer[7] = 0; // slot table length
1537 rd_ready_to_send_atapi(dev, channel);
1540 case 0x5a: // mode sense
1542 uint16_t alloc_length = rd_read_16bit(controller->buffer + 7);
1544 Bit8u PC = controller->buffer[2] >> 6;
1545 Bit8u PageCode = controller->buffer[2] & 0x3f;
1548 case 0x0: // current values
1551 case 0x01: // error recovery
1554 if (rd_init_send_atapi_command(dev, channel, atapi_command, sizeof(struct error_recovery_t) + 8, alloc_length, false) == -1) {
1555 PrintError("Error sending atapi command in mode sense error recovery\n");
1559 rd_init_mode_sense_single(dev, channel, &(drive->cdrom.current.error_recovery),
1560 sizeof(struct error_recovery_t));
1561 rd_ready_to_send_atapi(dev, channel);
1564 case 0x2a: // CD-ROM capabilities & mech. status
1567 if (rd_init_send_atapi_command(dev, channel, atapi_command, 28, alloc_length, false) == -1) {
1568 PrintError("Error sending atapi command in CDROM caps/mech mode-sense\n");
1572 rd_init_mode_sense_single(dev, channel, &(controller->buffer[8]), 28);
1574 controller->buffer[8] = 0x2a;
1575 controller->buffer[9] = 0x12;
1576 controller->buffer[10] = 0x00;
1577 controller->buffer[11] = 0x00;
1578 // Multisession, Mode 2 Form 2, Mode 2 Form 1
1579 controller->buffer[12] = 0x70;
1580 controller->buffer[13] = (3 << 5);
1581 controller->buffer[14] = (unsigned char) (1 |
1582 (drive->cdrom.locked ? (1 << 1) : 0) |
1585 controller->buffer[15] = 0x00;
1586 controller->buffer[16] = (706 >> 8) & 0xff;
1587 controller->buffer[17] = 706 & 0xff;
1588 controller->buffer[18] = 0;
1589 controller->buffer[19] = 2;
1590 controller->buffer[20] = (512 >> 8) & 0xff;
1591 controller->buffer[21] = 512 & 0xff;
1592 controller->buffer[22] = (706 >> 8) & 0xff;
1593 controller->buffer[23] = 706 & 0xff;
1594 controller->buffer[24] = 0;
1595 controller->buffer[25] = 0;
1596 controller->buffer[26] = 0;
1597 controller->buffer[27] = 0;
1598 rd_ready_to_send_atapi(dev, channel);
1601 case 0x0d: // CD-ROM
1602 case 0x0e: // CD-ROM audio control
1605 PrintError("Ramdisk: cdrom: MODE SENSE (curr), code=%x not implemented yet\n",
1607 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1608 ASC_INV_FIELD_IN_CMD_PACKET);
1609 rd_raise_interrupt(dev, channel);
1614 // not implemeted by this device
1615 PrintError("\t\tcdrom: MODE SENSE PC=%x, PageCode=%x, not implemented by device\n",
1617 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1618 ASC_INV_FIELD_IN_CMD_PACKET);
1619 rd_raise_interrupt(dev, channel);
1625 case 0x1: // changeable values
1628 case 0x01: // error recovery
1629 case 0x0d: // CD-ROM
1630 case 0x0e: // CD-ROM audio control
1631 case 0x2a: // CD-ROM capabilities & mech. status
1634 PrintError("cdrom: MODE SENSE (chg), code=%x not implemented yet\n",
1636 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1637 ASC_INV_FIELD_IN_CMD_PACKET);
1638 rd_raise_interrupt(dev, channel);
1643 // not implemeted by this device
1644 PrintError("Changeable values of mode sense not supported by cdrom\n");
1645 PrintDebug("\t\tcdrom: MODE SENSE PC=%x, PageCode=%x, not implemented by device\n",
1647 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1648 ASC_INV_FIELD_IN_CMD_PACKET);
1649 rd_raise_interrupt(dev, channel);
1655 case 0x2: // default values
1658 case 0x01: // error recovery
1659 case 0x0d: // CD-ROM
1660 case 0x0e: // CD-ROM audio control
1661 case 0x2a: // CD-ROM capabilities & mech. status
1663 PrintError("Default values of mode sense not supported by cdrom\n");
1664 PrintDebug("cdrom: MODE SENSE (dflt), code=%x\n",
1670 PrintError("Default values of mode sense not implemented in cdrom\n");
1671 // not implemeted by this device
1672 PrintDebug("cdrom: MODE SENSE PC=%x, PageCode=%x, not implemented by device\n",
1674 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1675 ASC_INV_FIELD_IN_CMD_PACKET);
1676 rd_raise_interrupt(dev, channel);
1682 case 0x3: // saved values not implemented
1684 PrintError("\t\tSaved values not implemented in mode sense\n");
1685 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
1686 rd_raise_interrupt(dev, channel);
1691 PrintError("Unsupported Mode sense value\n");
1698 case 0x12: // inquiry
1700 uint8_t alloc_length = controller->buffer[4];
1702 if (rd_init_send_atapi_command(dev, channel, atapi_command, 36, alloc_length, false) == -1) {
1703 PrintError("Error sending atapi command in inquiry\n");
1707 controller->buffer[0] = 0x05; // CD-ROM
1708 controller->buffer[1] = 0x80; // Removable
1709 controller->buffer[2] = 0x00; // ISO, ECMA, ANSI version
1710 controller->buffer[3] = 0x21; // ATAPI-2, as specified
1711 controller->buffer[4] = 31; // additional length (total 36)
1712 controller->buffer[5] = 0x00; // reserved
1713 controller->buffer[6] = 0x00; // reserved
1714 controller->buffer[7] = 0x00; // reserved
1717 const char* vendor_id = "VTAB ";
1719 for (i = 0; i < 8; i++) {
1720 controller->buffer[8+i] = vendor_id[i];
1724 const char* product_id = "Turbo CD-ROM ";
1725 for (i = 0; i < 16; i++) {
1726 controller->buffer[16+i] = product_id[i];
1729 // Product Revision level
1730 const char* rev_level = "1.0 ";
1731 for (i = 0; i < 4; i++) {
1732 controller->buffer[32 + i] = rev_level[i];
1735 rd_ready_to_send_atapi(dev, channel);
1738 case 0x25: // read cd-rom capacity
1740 // no allocation length???
1741 if (rd_init_send_atapi_command(dev, channel, atapi_command, 8, 8, false) == -1) {
1742 PrintError("Error sending atapi command in read cdrom capacity\n");
1746 if (drive->cdrom.ready) {
1747 uint32_t capacity = drive->cdrom.capacity;
1749 PrintDebug("\t\tCapacity is %d sectors (%d bytes)\n", capacity, capacity * 2048);
1751 controller->buffer[0] = (capacity >> 24) & 0xff;
1752 controller->buffer[1] = (capacity >> 16) & 0xff;
1753 controller->buffer[2] = (capacity >> 8) & 0xff;
1754 controller->buffer[3] = (capacity >> 0) & 0xff;
1755 controller->buffer[4] = (2048 >> 24) & 0xff;
1756 controller->buffer[5] = (2048 >> 16) & 0xff;
1757 controller->buffer[6] = (2048 >> 8) & 0xff;
1758 controller->buffer[7] = (2048 >> 0) & 0xff;
1760 rd_ready_to_send_atapi(dev, channel);
1762 PrintError("CDROM not ready in read cdrom capacity\n");
1763 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1764 rd_raise_interrupt(dev, channel);
1770 case 0xbe: // read cd
1772 if (drive->cdrom.ready) {
1773 PrintError("Read CD with CD present not implemented\n");
1774 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1775 rd_raise_interrupt(dev, channel);
1777 PrintError("Drive not ready in read cd with CD present\n");
1778 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1779 rd_raise_interrupt(dev, channel);
1783 case 0x43: // read toc
1785 if (drive->cdrom.ready) {
1787 bool msf = (controller->buffer[1] >> 1) & 1;
1788 uint8_t starting_track = controller->buffer[6];
1790 uint16_t alloc_length = rd_read_16bit(controller->buffer + 7);
1792 uint8_t format = (controller->buffer[9] >> 6);
1797 if (!(drive->cdrom.cd->read_toc(drive->private_data, controller->buffer,
1798 &toc_length, msf, starting_track))) {
1799 PrintError("CDROM: Reading Table of Contents Failed\n");
1800 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1801 ASC_INV_FIELD_IN_CMD_PACKET);
1802 rd_raise_interrupt(dev, channel);
1806 if (rd_init_send_atapi_command(dev, channel, atapi_command, toc_length, alloc_length, false) == -1) {
1807 PrintError("Failed to init send atapi command in read toc (fmt=%d)\n", format);
1811 rd_ready_to_send_atapi(dev, channel);
1816 // multi session stuff. we ignore this and emulate a single session only
1817 if (rd_init_send_atapi_command(dev, channel, atapi_command, 12, alloc_length, false) == -1) {
1818 PrintError("Failed to init send atapi command in read toc (fmt=%d)\n", format);
1822 controller->buffer[0] = 0;
1823 controller->buffer[1] = 0x0a;
1824 controller->buffer[2] = 1;
1825 controller->buffer[3] = 1;
1827 for (i = 0; i < 8; i++) {
1828 controller->buffer[4 + i] = 0;
1831 rd_ready_to_send_atapi(dev, channel);
1836 PrintError("(READ TOC) Format %d not supported\n", format);
1840 PrintError("CDROM not ready in read toc\n");
1841 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1842 rd_raise_interrupt(dev, channel);
1846 case 0x28: // read (10)
1847 case 0xa8: // read (12)
1850 uint32_t transfer_length;
1851 if (atapi_command == 0x28) {
1852 transfer_length = rd_read_16bit(controller->buffer + 7);
1854 transfer_length = rd_read_32bit(controller->buffer + 6);
1857 uint32_t lba = rd_read_32bit(controller->buffer + 2);
1859 if (!(drive->cdrom.ready)) {
1860 PrintError("CDROM Error: Not Ready (ATA%d/%d)\n",
1861 get_channel_no(ramdisk, channel), get_drive_no(channel, drive));
1862 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1863 rd_raise_interrupt(dev, channel);
1867 if (transfer_length == 0) {
1868 PrintError("READ(%d) with transfer length 0, ok\n",
1869 (atapi_command == 0x28) ? 10 : 12);
1870 rd_atapi_cmd_nop(dev, channel);
1871 rd_raise_interrupt(dev, channel);
1875 if (lba + transfer_length > drive->cdrom.capacity) {
1876 PrintError("CDROM Error: Capacity exceeded [capacity=%d] (ATA%d/%d)\n",
1877 drive->cdrom.capacity,
1878 get_channel_no(ramdisk, channel), get_drive_no(channel, drive));
1879 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
1880 rd_raise_interrupt(dev, channel);
1884 PrintDebug("\t\tcdrom: READ (%d) LBA=%d LEN=%d\n",
1885 (atapi_command == 0x28) ? 10 : 12,
1886 lba, transfer_length);
1889 if (rd_init_send_atapi_command(dev, channel, atapi_command, transfer_length * 2048,
1890 transfer_length * 2048, true) == -1) {
1891 PrintError("CDROM Error: Atapi command send error\n");
1895 drive->cdrom.remaining_blocks = transfer_length;
1896 drive->cdrom.next_lba = lba;
1897 rd_ready_to_send_atapi(dev, channel);
1902 uint32_t lba = rd_read_32bit(controller->buffer + 2);
1904 if (!(drive->cdrom.ready)) {
1905 PrintError("CDROM not ready in seek\n");
1906 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1907 rd_raise_interrupt(dev, channel);
1911 if (lba > drive->cdrom.capacity) {
1912 PrintError("LBA is greater than CDROM capacity in seek\n");
1913 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
1914 rd_raise_interrupt(dev, channel);
1918 PrintError("\t\tcdrom: SEEK (ignored)\n");
1920 rd_atapi_cmd_nop(dev, channel);
1921 rd_raise_interrupt(dev, channel);
1925 case 0x1e: // prevent/allow medium removal
1928 if (drive->cdrom.ready) {
1929 drive->cdrom.locked = controller->buffer[4] & 1;
1930 rd_atapi_cmd_nop(dev, channel);
1932 PrintError("CD not ready in prevent/allow medium removal\n");
1933 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1936 rd_raise_interrupt(dev, channel);
1940 case 0x42: // read sub-channel
1942 //bool msf = get_packet_field(channel,1, 1, 1);
1943 bool sub_q = get_packet_field(channel,2, 6, 1);
1944 //uint8_t data_format = get_packet_byte(channel,3);
1945 //uint8_t track_number = get_packet_byte(channel,6);
1946 uint16_t alloc_length = get_packet_word(channel,7);
1951 UNUSED(data_format);
1952 UNUSED(track_number);
1954 if (!(drive->cdrom.ready)) {
1955 PrintError("CDROM not ready in read sub-channel\n");
1956 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1957 rd_raise_interrupt(dev, channel);
1959 controller->buffer[0] = 0;
1960 controller->buffer[1] = 0; // audio not supported
1961 controller->buffer[2] = 0;
1962 controller->buffer[3] = 0;
1964 int ret_len = 4; // header size
1966 if (sub_q) { // !sub_q == header only
1967 PrintError("Read sub-channel with SubQ not implemented\n");
1968 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1969 ASC_INV_FIELD_IN_CMD_PACKET);
1970 rd_raise_interrupt(dev, channel);
1973 if (rd_init_send_atapi_command(dev, channel, atapi_command, ret_len, alloc_length, false) == -1) {
1974 PrintError("Error sending atapi command in read sub-channel\n");
1977 rd_ready_to_send_atapi(dev, channel);
1981 case 0x51: // read disc info
1983 // no-op to keep the Linux CD-ROM driver happy
1984 PrintError("Error: Read disk info no-op to keep the Linux CD-ROM driver happy\n");
1985 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1986 rd_raise_interrupt(dev, channel);
1989 case 0x55: // mode select
1990 case 0xa6: // load/unload cd
1991 case 0x4b: // pause/resume
1992 case 0x45: // play audio
1993 case 0x47: // play audio msf
1994 case 0xbc: // play cd
1995 case 0xb9: // read cd msf
1996 case 0x44: // read header
1998 case 0xbb: // set cd speed
1999 case 0x4e: // stop play/scan
2002 PrintError("ATAPI command 0x%x not implemented yet\n",
2004 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
2005 rd_raise_interrupt(dev, channel);
2008 PrintError("Unknown ATAPI command 0x%x (%d)\n",
2009 atapi_command, atapi_command);
2010 // We'd better signal the error if the user chose to continue
2011 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
2012 rd_raise_interrupt(dev, channel);
2022 int rd_init_send_atapi_command(struct vm_device * dev, struct channel_t * channel, Bit8u command, int req_length, int alloc_length, bool lazy)
2024 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2025 struct controller_t * controller = &(drive->controller);
2027 // controller->byte_count is a union of controller->cylinder_no;
2028 // lazy is used to force a data read in the buffer at the next read.
2030 PrintDebug("[rd_init_send_atapi_cmd]\n");
2032 if (controller->byte_count == 0xffff) {
2033 controller->byte_count = 0xfffe;
2036 if ((controller->byte_count & 1) &&
2037 !(alloc_length <= controller->byte_count)) {
2039 PrintDebug("\t\tOdd byte count (0x%04x) to ATAPI command 0x%02x, using 0x%x\n",
2040 controller->byte_count,
2042 controller->byte_count - 1);
2044 controller->byte_count -= 1;
2047 if (controller->byte_count == 0) {
2048 PrintError("\t\tATAPI command with zero byte count\n");
2052 if (alloc_length < 0) {
2053 PrintError("\t\tAllocation length < 0\n");
2057 if (alloc_length == 0) {
2058 alloc_length = controller->byte_count;
2061 controller->interrupt_reason.i_o = 1;
2062 controller->interrupt_reason.c_d = 0;
2063 controller->status.busy = 0;
2064 controller->status.drq = 1;
2065 controller->status.err = 0;
2067 // no bytes transfered yet
2069 controller->buffer_index = 2048;
2071 controller->buffer_index = 0;
2074 controller->drq_index = 0;
2076 if (controller->byte_count > req_length) {
2077 controller->byte_count = req_length;
2080 if (controller->byte_count > alloc_length) {
2081 controller->byte_count = alloc_length;
2084 drive->atapi.command = command;
2085 drive->atapi.drq_bytes = controller->byte_count;
2086 drive->atapi.total_bytes_remaining = (req_length < alloc_length) ? req_length : alloc_length;
2089 // // bias drq_bytes and total_bytes_remaining
2090 // SELECTED_DRIVE(channel).atapi.drq_bytes += 2048;
2091 // SELECTED_DRIVE(channel).atapi.total_bytes_remaining += 2048;
2099 void rd_ready_to_send_atapi(struct vm_device * dev, struct channel_t * channel) {
2100 PrintDebug("[rd_ready_to_send_atapi]\n");
2102 rd_raise_interrupt(dev, channel);
2109 void rd_atapi_cmd_error(struct vm_device * dev, struct channel_t * channel, sense_t sense_key, asc_t asc)
2111 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2112 struct controller_t * controller = &(drive->controller);
2115 struct ramdisk_t *ramdisk = (struct ramdisk_t *)(dev->private_data);
2116 PrintError("[rd_atapi_cmd_error]\n");
2117 PrintError("Error: atapi_cmd_error channel=%02x key=%02x asc=%02x\n",
2118 get_channel_no(ramdisk, channel), sense_key, asc);
2121 controller->error_register = sense_key << 4;
2122 controller->interrupt_reason.i_o = 1;
2123 controller->interrupt_reason.c_d = 1;
2124 controller->interrupt_reason.rel = 0;
2125 controller->status.busy = 0;
2126 controller->status.drive_ready = 1;
2127 controller->status.write_fault = 0;
2128 controller->status.drq = 0;
2129 controller->status.err = 1;
2131 drive->sense.sense_key = sense_key;
2132 drive->sense.asc = asc;
2133 drive->sense.ascq = 0;
2138 void rd_atapi_cmd_nop(struct vm_device * dev, struct channel_t * channel)
2140 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2141 struct controller_t * controller = &(drive->controller);
2143 PrintDebug("[rd_atapi_cmd_nop]\n");
2144 controller->interrupt_reason.i_o = 1;
2145 controller->interrupt_reason.c_d = 1;
2146 controller->interrupt_reason.rel = 0;
2147 controller->status.busy = 0;
2148 controller->status.drive_ready = 1;
2149 controller->status.drq = 0;
2150 controller->status.err = 0;
2156 void rd_identify_ATAPI_drive(struct vm_device * dev, struct channel_t * channel)
2158 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2159 struct controller_t * controller = &(drive->controller);
2163 const char* serial_number = " VT00001\0\0\0\0\0\0\0\0\0\0\0\0";
2164 const char* firmware = "ALPHA1 ";
2166 drive->id_drive[0] = (2 << 14) | (5 << 8) | (1 << 7) | (2 << 5) | (0 << 0); // Removable CDROM, 50us response, 12 byte packets
2168 for (i = 1; i <= 9; i++) {
2169 drive->id_drive[i] = 0;
2172 for (i = 0; i < 10; i++) {
2173 drive->id_drive[10 + i] = ((serial_number[i * 2] << 8) |
2174 (serial_number[(i * 2) + 1]));
2177 for (i = 20; i <= 22; i++) {
2178 drive->id_drive[i] = 0;
2181 for (i = 0; i < strlen(firmware)/2; i++) {
2182 drive->id_drive[23 + i] = ((firmware[i * 2] << 8) |
2183 (firmware[(i * 2) + 1]));
2185 V3_ASSERT((23 + i) == 27);
2187 for (i = 0; i < strlen((char *)(drive->model_no)) / 2; i++) {
2188 drive->id_drive[27 + i] = ((drive->model_no[i * 2] << 8) |
2189 (drive->model_no[(i * 2) + 1]));
2191 V3_ASSERT((27 + i) == 47);
2193 drive->id_drive[47] = 0;
2194 drive->id_drive[48] = 1; // 32 bits access
2196 drive->id_drive[49] = (1 << 9); // LBA supported
2198 drive->id_drive[50] = 0;
2199 drive->id_drive[51] = 0;
2200 drive->id_drive[52] = 0;
2202 drive->id_drive[53] = 3; // words 64-70, 54-58 valid
2204 for (i = 54; i <= 62; i++) {
2205 drive->id_drive[i] = 0;
2208 // copied from CFA540A
2209 drive->id_drive[63] = 0x0103; // variable (DMA stuff)
2210 drive->id_drive[64] = 0x0001; // PIO
2211 drive->id_drive[65] = 0x00b4;
2212 drive->id_drive[66] = 0x00b4;
2213 drive->id_drive[67] = 0x012c;
2214 drive->id_drive[68] = 0x00b4;
2216 drive->id_drive[69] = 0;
2217 drive->id_drive[70] = 0;
2218 drive->id_drive[71] = 30; // faked
2219 drive->id_drive[72] = 30; // faked
2220 drive->id_drive[73] = 0;
2221 drive->id_drive[74] = 0;
2223 drive->id_drive[75] = 0;
2225 for (i = 76; i <= 79; i++) {
2226 drive->id_drive[i] = 0;
2229 drive->id_drive[80] = 0x1e; // supports up to ATA/ATAPI-4
2230 drive->id_drive[81] = 0;
2231 drive->id_drive[82] = 0;
2232 drive->id_drive[83] = 0;
2233 drive->id_drive[84] = 0;
2234 drive->id_drive[85] = 0;
2235 drive->id_drive[86] = 0;
2236 drive->id_drive[87] = 0;
2237 drive->id_drive[88] = 0;
2239 for (i = 89; i <= 126; i++) {
2240 drive->id_drive[i] = 0;
2243 drive->id_drive[127] = 0;
2244 drive->id_drive[128] = 0;
2246 for (i = 129; i <= 159; i++) {
2247 drive->id_drive[i] = 0;
2250 for (i = 160; i <= 255; i++) {
2251 drive->id_drive[i] = 0;
2254 // now convert the id_drive array (native 256 word format) to
2255 // the controller buffer (512 bytes)
2257 for (i = 0; i <= 255; i++) {
2258 temp16 = drive->id_drive[i];
2259 controller->buffer[i * 2] = temp16 & 0x00ff;
2260 controller->buffer[i * 2 + 1] = temp16 >> 8;
2273 void rd_init_mode_sense_single(struct vm_device * dev,
2274 struct channel_t * channel, const void* src, int size)
2276 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2277 struct controller_t * controller = &(drive->controller);
2279 PrintDebug("[rd_init_mode_sense_single]\n");
2282 controller->buffer[0] = (size + 6) >> 8;
2283 controller->buffer[1] = (size + 6) & 0xff;
2284 controller->buffer[2] = 0x70; // no media present
2285 controller->buffer[3] = 0; // reserved
2286 controller->buffer[4] = 0; // reserved
2287 controller->buffer[5] = 0; // reserved
2288 controller->buffer[6] = 0; // reserved
2289 controller->buffer[7] = 0; // reserved
2292 memcpy(controller->buffer + 8, src, size);
2297 static void rd_command_aborted(struct vm_device * dev,
2298 struct channel_t * channel, unsigned value) {
2299 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2300 struct controller_t * controller = &(drive->controller);
2302 PrintError("[rd_command_aborted]\n");
2303 PrintError("\t\taborting on command 0x%02x {%s}\n", value, device_type_to_str(drive->device_type));
2305 controller->current_command = 0;
2306 controller->status.busy = 0;
2307 controller->status.drive_ready = 1;
2308 controller->status.err = 1;
2309 controller->error_register = 0x04; // command ABORTED
2310 controller->status.drq = 0;
2311 controller->status.seek_complete = 0;
2312 controller->status.corrected_data = 0;
2313 controller->buffer_index = 0;
2315 rd_raise_interrupt(dev, channel);
2319 static int ramdisk_init_device(struct vm_device *dev) {
2320 struct ramdisk_t *ramdisk= (struct ramdisk_t *)dev->private_data;
2322 PrintDebug("Initializing Ramdisk\n");
2325 rd_init_hardware(ramdisk);
2328 dev_hook_io(dev, PRI_CTRL_PORT,
2329 &read_status_port, &write_ctrl_port);
2331 dev_hook_io(dev, PRI_DATA_PORT,
2332 &read_data_port, &write_data_port);
2333 dev_hook_io(dev, PRI_FEATURES_PORT,
2334 &read_general_port, &write_general_port);
2335 dev_hook_io(dev, PRI_SECT_CNT_PORT,
2336 &read_general_port, &write_general_port);
2337 dev_hook_io(dev, PRI_SECT_ADDR1_PORT,
2338 &read_general_port, &write_general_port);
2339 dev_hook_io(dev, PRI_SECT_ADDR2_PORT,
2340 &read_general_port, &write_general_port);
2341 dev_hook_io(dev, PRI_SECT_ADDR3_PORT,
2342 &read_general_port, &write_general_port);
2343 dev_hook_io(dev, PRI_DRV_SEL_PORT,
2344 &read_general_port, &write_general_port);
2345 dev_hook_io(dev, PRI_CMD_PORT,
2346 &read_status_port, &write_cmd_port);
2349 dev_hook_io(dev, SEC_CTRL_PORT,
2350 &read_status_port, &write_ctrl_port);
2352 dev_hook_io(dev, SEC_DATA_PORT,
2353 &read_data_port, &write_data_port);
2354 dev_hook_io(dev, SEC_FEATURES_PORT,
2355 &read_general_port, &write_general_port);
2356 dev_hook_io(dev, SEC_SECT_CNT_PORT,
2357 &read_general_port, &write_general_port);
2358 dev_hook_io(dev, SEC_SECT_ADDR1_PORT,
2359 &read_general_port, &write_general_port);
2360 dev_hook_io(dev, SEC_SECT_ADDR2_PORT,
2361 &read_general_port, &write_general_port);
2362 dev_hook_io(dev, SEC_SECT_ADDR3_PORT,
2363 &read_general_port, &write_general_port);
2364 dev_hook_io(dev, SEC_DRV_SEL_PORT,
2365 &read_general_port, &write_general_port);
2366 dev_hook_io(dev, SEC_CMD_PORT,
2367 &read_status_port, &write_cmd_port);
2371 dev_hook_io(dev, SEC_ADDR_REG_PORT,
2372 &read_general_port, &write_general_port);
2374 dev_hook_io(dev, PRI_ADDR_REG_PORT,
2375 &read_general_port, &write_general_port);
2384 static int ramdisk_deinit_device(struct vm_device *dev) {
2385 struct ramdisk_t *ramdisk = (struct ramdisk_t *)(dev->private_data);
2386 rd_close_harddrive(ramdisk);
2390 static struct vm_device_ops dev_ops = {
2391 .init = ramdisk_init_device,
2392 .deinit = ramdisk_deinit_device,
2401 struct vm_device *create_ramdisk()
2404 struct ramdisk_t *ramdisk;
2405 ramdisk = (struct ramdisk_t *)V3_Malloc(sizeof(struct ramdisk_t));
2406 V3_ASSERT(ramdisk != NULL);
2408 PrintDebug("[create_ramdisk]\n");
2410 struct vm_device *device = create_device("RAMDISK", &dev_ops, ramdisk);
2418 #ifdef DEBUG_RAMDISK
2420 static void rd_print_state(struct ramdisk_t * ramdisk) {
2423 struct channel_t * channels = (struct channel_t *)(&(ramdisk->channels));
2426 for (channel = 0; channel < MAX_ATA_CHANNEL; channel++) {
2427 memset((char *)(channels + channel), 0, sizeof(struct channel_t));
2430 PrintDebug("sizeof(*channels) = %d\n", sizeof(*channels));
2431 PrintDebug("sizeof(channles->drives[0].controller) = %d\n", sizeof((channels->drives[0].controller)));
2432 PrintDebug("sizeof(channles->drives[0].cdrom) = %d\n", sizeof((channels->drives[0].cdrom)));
2433 PrintDebug("sizeof(channles->drives[0].sense) = %d\n", sizeof((channels->drives[0].sense)));
2434 PrintDebug("sizeof(channles->drives[0].atapi) = %d\n", sizeof((channels->drives[0].atapi)));
2437 PrintDebug("sizeof(channles->drives[0].controller.status) = %d\n",
2438 sizeof((channels->drives[0].controller.status)));
2439 PrintDebug("sizeof(channles->drives[0].controller.sector_count) = %d\n",
2440 sizeof((channels->drives[0].controller.sector_count)));
2441 PrintDebug("sizeof(channles->drives[0].controller.interrupt_reason) = %d\n",
2442 sizeof((channels->drives[0].controller.interrupt_reason)));
2444 PrintDebug("sizeof(channles->drives[0].controller.cylinder_no) = %d\n",
2445 sizeof((channels->drives[0].controller.cylinder_no)));
2446 PrintDebug("sizeof(channles->drives[0].controller.byte_count) = %d\n",
2447 sizeof((channels->drives[0].controller.byte_count)));
2450 PrintDebug("sizeof(channles->drives[0].controller.control) = %d\n",
2451 sizeof((channels->drives[0].controller.control)));
2454 for (channel = 0; channel < MAX_ATA_CHANNEL; channel++){
2456 for (device = 0; device < 2; device++){
2458 // Initialize controller state, even if device is not present
2459 PrintDebug("channels[%d].drives[%d].controller.status.busy = %d\n",
2461 channels[channel].drives[device].controller.status.busy);
2462 PrintDebug("channels[%d].drives[%d].controller.status.drive_ready = %d\n",
2464 channels[channel].drives[device].controller.status.drive_ready);
2465 PrintDebug("channels[%d].drives[%d].controller.status.write_fault = %d\n",
2467 channels[channel].drives[device].controller.status.write_fault);
2468 PrintDebug("channels[%d].drives[%d].controller.status.seek_complete = %d\n",
2470 channels[channel].drives[device].controller.status.seek_complete);
2471 PrintDebug("channels[%d].drives[%d].controller.status.drq = %d\n",
2473 channels[channel].drives[device].controller.status.drq);
2474 PrintDebug("channels[%d].drives[%d].controller.status.corrected_data = %d\n",
2476 channels[channel].drives[device].controller.status.corrected_data);
2477 PrintDebug("channels[%d].drives[%d].controller.status.index_pulse = %d\n",
2479 channels[channel].drives[device].controller.status.index_pulse);
2480 PrintDebug("channels[%d].drives[%d].controller.status.index_pulse_count = %d\n",
2482 channels[channel].drives[device].controller.status.index_pulse_count);
2483 PrintDebug("channels[%d].drives[%d].controller.status.err = %d\n",
2485 channels[channel].drives[device].controller.status.err);
2488 PrintDebug("channels[%d].drives[%d].controller.error_register = %d\n",
2490 channels[channel].drives[device].controller.error_register);
2491 PrintDebug("channels[%d].drives[%d].controller.head_no = %d\n",
2493 channels[channel].drives[device].controller.head_no);
2494 PrintDebug("channels[%d].drives[%d].controller.sector_count = %d\n",
2496 channels[channel].drives[device].controller.sector_count);
2497 PrintDebug("channels[%d].drives[%d].controller.sector_no = %d\n",
2499 channels[channel].drives[device].controller.sector_no);
2500 PrintDebug("channels[%d].drives[%d].controller.cylinder_no = %d\n",
2502 channels[channel].drives[device].controller.cylinder_no);
2503 PrintDebug("channels[%d].drives[%d].controller.current_command = %02x\n",
2505 channels[channel].drives[device].controller.current_command);
2506 PrintDebug("channels[%d].drives[%d].controller.buffer_index = %d\n",
2508 channels[channel].drives[device].controller.buffer_index);
2511 PrintDebug("channels[%d].drives[%d].controller.control.reset = %d\n",
2513 channels[channel].drives[device].controller.control.reset);
2514 PrintDebug("channels[%d].drives[%d].controller.control.disable_irq = %d\n",
2516 channels[channel].drives[device].controller.control.disable_irq);
2519 PrintDebug("channels[%d].drives[%d].controller.reset_in_progress = %d\n",
2521 channels[channel].drives[device].controller.reset_in_progress);
2522 PrintDebug("channels[%d].drives[%d].controller.sectors_per_block = %02x\n",
2524 channels[channel].drives[device].controller.sectors_per_block);
2525 PrintDebug("channels[%d].drives[%d].controller.lba_mode = %d\n",
2527 channels[channel].drives[device].controller.lba_mode);
2528 PrintDebug("channels[%d].drives[%d].controller.features = %d\n",
2530 channels[channel].drives[device].controller.features);
2533 PrintDebug("channels[%d].drives[%d].model_no = %s\n",
2535 channels[channel].drives[device].model_no);
2536 PrintDebug("channels[%d].drives[%d].device_type = %d\n",
2538 channels[channel].drives[device].device_type);
2539 PrintDebug("channels[%d].drives[%d].cdrom.locked = %d\n",
2541 channels[channel].drives[device].cdrom.locked);
2542 PrintDebug("channels[%d].drives[%d].sense.sense_key = %d\n",
2544 channels[channel].drives[device].sense.sense_key);
2545 PrintDebug("channels[%d].drives[%d].sense.asc = %d\n",
2547 channels[channel].drives[device].sense.asc);
2548 PrintDebug("channels[%d].drives[%d].sense.ascq = %d\n",
2550 channels[channel].drives[device].sense.ascq);
2554 PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.c_d = %02x\n",
2556 channels[channel].drives[device].controller.interrupt_reason.c_d);
2558 PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.i_o = %02x\n",
2560 channels[channel].drives[device].controller.interrupt_reason.i_o);
2562 PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.rel = %02x\n",
2564 channels[channel].drives[device].controller.interrupt_reason.rel);
2566 PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.tag = %02x\n",
2568 channels[channel].drives[device].controller.interrupt_reason.tag);
2570 PrintDebug("channels[%d].drives[%d].cdrom.ready = %d\n",
2572 channels[channel].drives[device].cdrom.ready);
2581 static void trace_info(ushort_t port, void *src, uint_t length) {
2586 if (length == 1 && *((uchar_t*) src) == ATA_DETECT)
2587 PrintDebug("ata_detect()\n");
2591 if (length == 1 && *((uchar_t*) src) == ATA_RESET)
2592 PrintDebug("ata_reset()\n");
2596 if (length == 1 && *((uchar_t*) src) == ATA_CMD_DATA_IN)
2597 PrintDebug("ata_cmd_data_in()\n");
2601 if (length == 1 && *((uchar_t*) src) == ATA_CMD_DATA_OUT)
2602 PrintDebug("ata_cmd_data_out()\n");
2606 if (length == 1 && *((uchar_t*) src) == ATA_CMD_PACKET)
2607 PrintDebug("ata_cmd_packet()\n");
2611 if (length == 1 && *((uchar_t*) src) == ATAPI_GET_SENSE)
2612 PrintDebug("atapi_get_sense()\n");
2616 if (length == 1 && *((uchar_t*) src) == ATAPI_IS_READY)
2617 PrintDebug("atapi_is_ready()\n");
2621 if (length == 1 && *((uchar_t*) src) == ATAPI_IS_CDROM)
2622 PrintDebug("atapi_is_cdrom()\n");
2627 if (length == 1 && *((uchar_t*) src) == CDEMU_INIT)
2628 PrintDebug("cdemu_init()\n");
2632 if (length == 1 && *((uchar_t*) src) == CDEMU_ISACTIVE)
2633 PrintDebug("cdemu_isactive()\n");
2637 if (length == 1 && *((uchar_t*) src) == CDEMU_EMULATED_DRIVE)
2638 PrintDebug("cdemu_emulated_drive()\n");
2642 if (length == 1 && *((uchar_t*) src) == CDROM_BOOT)
2643 PrintDebug("cdrom_boot()\n");
2647 if (length == 1 && *((uchar_t*) src) == HARD_DRIVE_POST)
2648 PrintDebug("ata_hard_drive_post()\n");
2653 PrintDebug("ata_device_no(%d)\n", *((uchar_t*) src));
2658 PrintDebug("ata_device_type(%d)\n", *((uchar_t*) src));
2662 if (length == 1 && *((uchar_t*) src) == INT13_HARDDISK)
2663 PrintDebug("int13_harddrive()\n");
2667 if (length == 1 && *((uchar_t*) src) == INT13_CDROM)
2668 PrintDebug("int13_cdrom()\n");
2672 if (length == 1 && *((uchar_t*) src) == INT13_CDEMU)
2673 PrintDebug("int13_cdemu()\n");
2677 if (length == 1 && *((uchar_t*) src) == INT13_ELTORITO)
2678 PrintDebug("int13_eltorito()\n");
2682 if (length == 1 && *((uchar_t*) src) == INT13_DISKETTE_FUNCTION)
2683 PrintDebug("int13_diskette_function()\n");
2695 static int check_bit_fields(struct controller_t * controller) {
2697 controller->sector_count = 0;
2698 controller->interrupt_reason.c_d = 1;
2699 if (controller->sector_count != 0x01) {
2700 return INTR_REASON_BIT_ERR;
2703 controller->sector_count = 0;
2704 controller->interrupt_reason.i_o = 1;
2705 if (controller->sector_count != 0x02) {
2706 return INTR_REASON_BIT_ERR;
2709 controller->sector_count = 0;
2710 controller->interrupt_reason.rel = 1;
2711 if (controller->sector_count != 0x04) {
2712 return INTR_REASON_BIT_ERR;
2715 controller->sector_count = 0;
2716 controller->interrupt_reason.tag = 3;
2717 if (controller->sector_count != 0x18) {
2718 return INTR_REASON_BIT_ERR;