Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


fixed toc_length initialization bug
[palacios.git] / palacios / src / devices / ramdisk.c
1 /* 
2  *
3  *   Copyright (C) 2002  MandrakeSoft S.A.
4  *
5  *     MandrakeSoft S.A.
6  *     43, rue d'Aboukir
7  *     75002 Paris - France
8  *     http://www.linux-mandrake.com/
9  *     http://www.mandrakesoft.com/
10  *
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.
15  *
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.
20  *
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
24  *
25  * Major modifications made for the V3VEE project
26  * 
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
30  * 
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
35  * 
36  */
37
38
39 #include <devices/ramdisk.h>
40 #include <palacios/vmm.h>
41 #include <devices/cdrom.h>
42 #include <devices/ide.h>
43
44
45 #ifndef DEBUG_RAMDISK
46 #undef PrintDebug
47 #define PrintDebug(_f, _a...)
48 #endif
49
50
51
52
53
54 /*
55  * Data type definitions
56  *
57  */
58 #define INDEX_PULSE_CYCLE 10
59
60
61
62
63 #define INTR_REASON_BIT_ERR           0x01
64 #define UNABLE_FIND_TAT_CHANNEL_ERR   0x02
65 #define DRQ_ERR                       0x03
66 #define READ_BUF_GT_512               0x04
67
68
69
70 #define PRI_DATA_PORT         0x1f0
71 #define PRI_FEATURES_PORT     0x1f1
72 #define PRI_SECT_CNT_PORT     0x1f2
73 #define PRI_SECT_ADDR1_PORT   0x1f3
74 #define PRI_SECT_ADDR2_PORT   0x1f4
75 #define PRI_SECT_ADDR3_PORT   0x1f5
76 #define PRI_DRV_SEL_PORT      0x1f6
77 #define PRI_CMD_PORT          0x1f7
78 #define PRI_CTRL_PORT         0x3f6
79 #define PRI_ADDR_REG_PORT     0x3f7
80
81 #define SEC_DATA_PORT         0x170
82 #define SEC_FEATURES_PORT     0x171
83 #define SEC_SECT_CNT_PORT     0x172
84 #define SEC_SECT_ADDR1_PORT   0x173
85 #define SEC_SECT_ADDR2_PORT   0x174
86 #define SEC_SECT_ADDR3_PORT   0x175
87 #define SEC_DRV_SEL_PORT      0x176
88 #define SEC_CMD_PORT          0x177
89 #define SEC_CTRL_PORT         0x376
90 #define SEC_ADDR_REG_PORT     0x377
91
92
93 #define PACKET_SIZE 12
94
95
96
97 static const char cdrom_str[] = "CD-ROM";
98 static const char harddisk_str[] = "HARDDISK";
99 static const char none_str[] = "NONE";
100
101
102 static inline const char * device_type_to_str(device_type_t type) {
103   switch (type) {
104   case IDE_DISK:
105     return harddisk_str;
106   case IDE_CDROM:
107     return cdrom_str;
108   case IDE_NONE:
109     return none_str;
110   default:
111     return NULL;
112   }
113 }
114
115
116 static inline void write_features(struct channel_t * channel, uchar_t value) {
117   channel->drives[0].controller.features = value;
118   channel->drives[1].controller.features = value;
119 }
120
121
122 static inline void write_sector_count(struct channel_t * channel, uchar_t value) {
123   channel->drives[0].controller.sector_count = value;
124   channel->drives[1].controller.sector_count = value;
125 }
126
127 static inline void write_sector_number(struct channel_t * channel, uchar_t value) {
128   channel->drives[0].controller.sector_no = value;
129   channel->drives[1].controller.sector_no = value;
130 }
131
132
133 static inline void write_cylinder_low(struct channel_t * channel, uchar_t value) {
134   channel->drives[0].controller.cylinder_no &= 0xff00;
135   channel->drives[0].controller.cylinder_no |= value;
136   channel->drives[1].controller.cylinder_no &= 0xff00;
137   channel->drives[1].controller.cylinder_no |= value;
138 }
139
140 static inline void write_cylinder_high(struct channel_t * channel, uchar_t value) {
141   ushort_t val2 = value;
142   val2 = val2 << 8;
143   channel->drives[0].controller.cylinder_no &= 0x00ff;
144   channel->drives[0].controller.cylinder_no |= (val2 & 0xff00);
145
146   channel->drives[1].controller.cylinder_no &= 0x00ff;
147   channel->drives[1].controller.cylinder_no |= (val2 & 0xff00);
148 }
149
150 static inline void write_head_no(struct channel_t * channel, uchar_t value) {
151   channel->drives[0].controller.head_no = value;
152   channel->drives[1].controller.head_no = value;
153 }
154
155 static inline void write_lba_mode(struct channel_t * channel, uchar_t value) {
156   channel->drives[0].controller.lba_mode = value;
157   channel->drives[1].controller.lba_mode = value;
158 }
159
160
161 static inline uint_t get_channel_no(struct ramdisk_t * ramdisk, struct channel_t * channel) {
162   return (((uchar_t *)channel - (uchar_t *)(ramdisk->channels)) / sizeof(struct channel_t));
163 }
164
165 static inline uint_t get_drive_no(struct channel_t * channel, struct drive_t * drive) {
166   return (((uchar_t *)drive - (uchar_t*)(channel->drives)) /  sizeof(struct drive_t));
167 }
168
169 static inline struct drive_t * get_selected_drive(struct channel_t * channel) {
170   return &(channel->drives[channel->drive_select]);
171 }
172
173
174 static inline int is_primary_port(struct ramdisk_t * ramdisk, ushort_t port) {
175   switch(port) 
176     {
177     case PRI_DATA_PORT:
178     case PRI_FEATURES_PORT:
179     case PRI_SECT_CNT_PORT:
180     case PRI_SECT_ADDR1_PORT:
181     case PRI_SECT_ADDR2_PORT:
182     case PRI_SECT_ADDR3_PORT:
183     case PRI_DRV_SEL_PORT:
184     case PRI_CMD_PORT:
185     case PRI_CTRL_PORT:
186       return 1;
187     default:
188       return 0;
189     }
190 }
191
192
193
194 static inline int is_secondary_port(struct ramdisk_t * ramdisk, ushort_t port) {
195   switch(port) 
196     {
197     case SEC_DATA_PORT:
198     case SEC_FEATURES_PORT:
199     case SEC_SECT_CNT_PORT:
200     case SEC_SECT_ADDR1_PORT:
201     case SEC_SECT_ADDR2_PORT:
202     case SEC_SECT_ADDR3_PORT:
203     case SEC_DRV_SEL_PORT:
204     case SEC_CMD_PORT:
205     case SEC_CTRL_PORT:
206       return 1;
207     default:
208       return 0;
209     }
210 }
211
212 static inline int num_drives_on_channel(struct channel_t * channel) {
213   if ((channel->drives[0].device_type == IDE_NONE) &&
214       (channel->drives[1].device_type == IDE_NONE)) {
215     return 0;
216   } else if ((channel->drives[0].device_type != IDE_NONE) &&
217              (channel->drives[1].device_type != IDE_NONE)) {
218     return 2;
219   } else {
220     return 1;
221   }
222 }
223
224
225
226 static inline uchar_t extract_bits(uchar_t * buf, uint_t buf_offset, uint_t bit_offset, uint_t num_bits) {
227   uchar_t val = buf[buf_offset];
228   val = val >> bit_offset;
229   val &= ((1 << num_bits) -1);
230   return val;
231 }
232
233
234 static inline uchar_t get_packet_field(struct channel_t * channel, uint_t packet_offset, uint_t bit_offset, uint_t num_bits) {
235   struct drive_t * drive = get_selected_drive(channel);
236   return extract_bits(drive->controller.buffer, packet_offset, bit_offset, num_bits);
237 }
238
239
240 static inline uchar_t get_packet_byte(struct channel_t * channel, uint_t offset) {
241   struct drive_t * drive = get_selected_drive(channel);
242   return drive->controller.buffer[offset];
243 }
244
245 static inline uint16_t get_packet_word(struct channel_t * channel, uint_t offset) {
246   struct drive_t * drive = get_selected_drive(channel);
247   uint16_t val = drive->controller.buffer[offset];
248   val = val << 8;
249   val |= drive->controller.buffer[offset + 1];
250   return val;
251 }
252
253
254 static inline uint16_t rd_read_16bit(const uint8_t* buf) {
255   return (buf[0] << 8) | buf[1];
256 }
257
258
259
260 static inline uint32_t rd_read_32bit(const uint8_t* buf) {
261   return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
262 }
263
264 ////////////////////////////////////////////////////////////////////////////
265
266
267 /*
268  * ATAPI routines
269  */
270
271
272 static void rd_init_mode_sense_single(struct vm_device * dev, struct channel_t * channel, const void * src, int size);
273
274 static void rd_command_aborted(struct vm_device * dev, struct channel_t * channel, unsigned value);
275
276
277
278
279 static int handle_atapi_packet_command(struct vm_device * dev, 
280                                        struct channel_t * channel, 
281                                        ushort_t val);
282
283 static int rd_init_send_atapi_command(struct vm_device * dev, 
284                                       struct channel_t * channel, 
285                                       Bit8u command, int req_length, 
286                                       int alloc_length, bool lazy);
287
288 static void rd_ready_to_send_atapi(struct vm_device * dev, 
289                                    struct channel_t * channel);
290
291 static void rd_atapi_cmd_error(struct vm_device * dev, 
292                                struct channel_t * channel, 
293                                sense_t sense_key, asc_t asc);
294
295 static void rd_atapi_cmd_nop(struct vm_device * dev, struct channel_t * channel);
296 static void rd_identify_ATAPI_drive(struct vm_device * dev, struct channel_t * channel);
297
298
299
300 /*
301  * Interrupt handling
302  */
303 static void rd_raise_interrupt(struct vm_device * dev, struct channel_t * channel);
304 static void rd_lower_irq(struct vm_device *dev, Bit32u irq);
305
306
307
308 /*
309  * Helper routines
310  */
311
312
313
314 #ifdef DEBUG_RAMDISK
315 static void rd_print_state(struct ramdisk_t *ramdisk);
316 static int check_bit_fields(struct controller_t * controller);
317 #endif
318 ////////////////////////////////////////////////////////////////////
319
320
321
322
323
324 int v3_ramdisk_register_cdrom(struct vm_device * dev, uint_t busID, uint_t driveID, struct cdrom_ops* cd, void * private_data) {
325   struct ramdisk_t * ramdisk  = (struct ramdisk_t *)(dev->private_data);
326   struct channel_t * channel = &(ramdisk->channels[busID]);
327   struct drive_t * drive = &(channel->drives[driveID]);
328   struct controller_t * controller = &(drive->controller);
329
330
331   
332   if (drive->device_type != IDE_NONE) {
333     PrintError("Device already registered at this location\n");
334     return -1;
335   }
336
337
338   channel->irq =  15;
339
340   // Make model string
341   strncpy((char*)(drive->model_no), "V3VEE Ramdisk", 40);
342
343   while (strlen((char *)(drive->model_no)) < 40) {
344     strcat ((char*)(drive->model_no), " ");
345   }
346   
347   PrintDebug("CDROM on target %d/%d\n", busID, driveID);
348   
349   drive->device_type = IDE_CDROM;
350   drive->cdrom.locked = 0;
351   drive->sense.sense_key = SENSE_NONE;
352   drive->sense.asc = 0;
353   drive->sense.ascq = 0;
354   
355   drive->private_data = private_data;
356
357 #ifdef DEBUG_RAMDISK
358   if (check_bit_fields(controller) == INTR_REASON_BIT_ERR) {
359     PrintDebug("interrupt reason: bit field error\n");
360     return INTR_REASON_BIT_ERR;
361   }
362 #endif
363   
364   controller->sector_count = 0;
365
366   drive->cdrom.cd = cd;
367   
368   PrintDebug("\t\tCD on ata%d-%d: '%s'\n", 
369              busID, 
370              driveID, "");
371   
372   if(drive->cdrom.cd->insert_cdrom(drive->private_data)) {
373     PrintDebug("\t\tMedia present in CD-ROM drive\n");
374     drive->cdrom.ready = 1;
375     drive->cdrom.capacity = drive->cdrom.cd->capacity(drive->private_data);
376     PrintDebug("\t\tCDROM capacity is %d\n", drive->cdrom.capacity);
377   } else {                  
378     PrintDebug("\t\tCould not locate CD-ROM, continuing with media not present\n");
379     drive->cdrom.ready = 0;
380   }
381   
382   return 0;
383 }
384
385
386 static Bit32u rd_init_hardware(struct ramdisk_t *ramdisk) {
387   uint_t channel_num; 
388   uint_t device;
389   struct channel_t *channels = (struct channel_t *)(&(ramdisk->channels));
390
391   PrintDebug("[rd_init_harddrive]\n");
392
393   for (channel_num = 0; channel_num < MAX_ATA_CHANNEL; channel_num++) {
394     memset((char *)(channels + channel_num), 0, sizeof(struct channel_t));
395   }
396
397   for (channel_num = 0; channel_num < MAX_ATA_CHANNEL; channel_num++){
398     struct channel_t * channel = &(channels[channel_num]);
399
400     channel->ioaddr1 = 0x0;
401     channel->ioaddr2 = 0x0;
402     channel->irq = 0;
403
404     for (device = 0; device < 2; device++){
405       struct drive_t * drive = &(channel->drives[device]);
406       struct controller_t * controller = &(drive->controller);
407
408       controller->status.busy = 0;
409       controller->status.drive_ready = 1;
410       controller->status.write_fault = 0;
411       controller->status.seek_complete = 1;
412       controller->status.drq = 0;
413       controller->status.corrected_data = 0;
414       controller->status.index_pulse = 0;
415       controller->status.index_pulse_count = 0;
416       controller->status.err = 0;
417
418       controller->error_register = 0x01; // diagnostic code: no error
419       controller->head_no = 0;
420       controller->sector_count = 1;
421       controller->sector_no = 1;
422       controller->cylinder_no = 0;
423       controller->current_command = 0x00;
424       controller->buffer_index = 0;
425
426       controller->control.reset = 0;
427       controller->control.disable_irq = 0;
428       controller->reset_in_progress = 0;
429
430       controller->sectors_per_block = 0x80;
431       controller->lba_mode = 0;
432       
433       
434       controller->features = 0;
435         
436       // If not present
437       drive->device_type = IDE_NONE;
438
439       // Make model string
440       strncpy((char*)(drive->model_no), "", 40);
441       while(strlen((char *)(drive->model_no)) < 40) {
442         strcat ((char*)(drive->model_no), " ");
443       }
444
445     }//for device
446   }//for channel
447
448 #ifdef DEBUG_RAMDISK
449   rd_print_state(ramdisk);
450 #endif
451   return 0;
452 }
453
454
455 /*
456   static void rd_reset_harddrive(struct ramdisk_t *ramdisk, unsigned type) {
457   return;
458   }
459
460 */
461 static void rd_close_harddrive(struct ramdisk_t *ramdisk) {
462   return;
463 }
464
465
466 ////////////////////////////////////////////////////////////////////
467
468
469
470 static int read_data_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
471   struct ramdisk_t * ramdisk  = (struct ramdisk_t *)(dev->private_data);
472   struct channel_t * channel = NULL;
473   struct drive_t * drive = NULL;
474   struct controller_t * controller = NULL;
475
476
477
478   if (is_primary_port(ramdisk, port)) {
479     channel = &(ramdisk->channels[0]);
480   } else if (is_secondary_port(ramdisk, port)) {
481     channel = &(ramdisk->channels[1]);
482   } else {
483     PrintError("Invalid Port: %d\n", port);
484     return -1;
485   }
486   
487   drive = get_selected_drive(channel);
488   controller = &(drive->controller);
489
490
491   PrintDebug("[read_data_handler] IO Read at 0x%x, on drive %d/%d (current_cmd = 0x%02x)\n", 
492              port, 
493              get_channel_no(ramdisk, channel),
494              get_drive_no(channel, drive), 
495              controller->current_command);
496
497   switch (controller->current_command) {
498   case 0xec:    // IDENTIFY DEVICE
499   case 0xa1:
500     {
501
502
503       controller->status.busy = 0;
504       controller->status.drive_ready = 1;
505       controller->status.write_fault = 0;
506       controller->status.seek_complete = 1;
507       controller->status.corrected_data = 0;
508       controller->status.err = 0;
509       
510       /*
511         value32 = controller->buffer[index];
512         index++;
513         
514         if (io_len >= 2) {
515         value32 |= (controller->buffer[index] << 8);
516         index++;
517         }
518         
519         if (io_len == 4) {
520         value32 |= (controller->buffer[index] << 16);
521         value32 |= (controller->buffer[index+1] << 24);
522         index += 2;
523         }
524         
525         controller->buffer_index = index;
526       */
527       /* JRL */
528       memcpy(dst, controller->buffer + controller->buffer_index, length);
529       controller->buffer_index += length;
530       
531       if (controller->buffer_index >= 512) {
532         controller->status.drq = 0;
533       }
534       
535       return length;
536     }
537   case 0xa0: //send packet cmd 
538     {
539       uint_t index = controller->buffer_index;
540
541       
542       PrintDebug("\t\tatapi.command(%02x), index(%d), cdrom.remaining_blocks(%d)\n", 
543                     drive->atapi.command, 
544                     index, 
545                     drive->cdrom.remaining_blocks);
546       
547       // Load block if necessary
548       if (index >= 2048) {
549         
550         if (index > 2048) {
551           PrintError("\t\tindex > 2048 : 0x%x\n", index);
552           return -1;
553         }
554         
555         switch (drive->atapi.command) {
556         case 0x28: // read (10)
557         case 0xa8: // read (12)
558           {
559     
560             if (!(drive->cdrom.ready)) {
561               PrintError("\t\tRead with CDROM not ready\n");
562               return -1;
563             } 
564             
565             drive->cdrom.cd->read_block(drive->private_data, controller->buffer,
566                                         drive->cdrom.next_lba);
567             drive->cdrom.next_lba++;
568             drive->cdrom.remaining_blocks--;
569             
570             
571             if (!(drive->cdrom.remaining_blocks)) {
572               PrintDebug("\t\tLast READ block loaded {CDROM}\n");
573             } else {
574               PrintDebug("\t\tREAD block loaded (%d remaining) {CDROM}\n",
575                          drive->cdrom.remaining_blocks);
576             }
577             
578             // one block transfered, start at beginning
579             index = 0;
580             break;
581           }
582         default: // no need to load a new block
583           break;
584         }
585       }
586     
587
588       /*
589         increment = 0;
590         value32 = controller->buffer[index + increment];
591         increment++;
592         
593         if (io_len >= 2) {
594         value32 |= (controller->buffer[index + increment] << 8);
595         increment++;
596         }
597         
598         if (io_len == 4) {
599         value32 |= (controller->buffer[index + increment] << 16);
600         value32 |= (controller->buffer[index + increment + 1] << 24);
601         increment += 2;
602         }
603
604         controller->buffer_index = index + increment;
605         controller->drq_index += increment;
606
607       */
608       /* JRL: CHECK THAT there is enough data in the buffer to copy.... */
609       {      
610         memcpy(dst, controller->buffer + index, length);
611         
612         controller->buffer_index  = index + length;
613         controller->drq_index += length;
614       }
615       
616       /* *** */
617       
618       if (controller->drq_index >= (unsigned)drive->atapi.drq_bytes) {
619         controller->status.drq = 0;
620         controller->drq_index = 0;
621         
622         drive->atapi.total_bytes_remaining -= drive->atapi.drq_bytes;
623         
624         if (drive->atapi.total_bytes_remaining > 0) {
625           // one or more blocks remaining (works only for single block commands)
626           
627           PrintDebug("\t\tPACKET drq bytes read\n");
628           controller->interrupt_reason.i_o = 1;
629           controller->status.busy = 0;
630           controller->status.drq = 1;
631           controller->interrupt_reason.c_d = 0;
632           
633           // set new byte count if last block
634           if (drive->atapi.total_bytes_remaining < controller->byte_count) {
635             controller->byte_count = drive->atapi.total_bytes_remaining;
636           }
637           drive->atapi.drq_bytes = controller->byte_count;
638           
639           rd_raise_interrupt(dev, channel);
640         } else {
641           // all bytes read
642           PrintDebug("\t\tPACKET all bytes read\n");
643           
644           controller->interrupt_reason.i_o = 1;
645           controller->interrupt_reason.c_d = 1;
646           controller->status.drive_ready = 1;
647           controller->interrupt_reason.rel = 0;
648           controller->status.busy = 0;
649           controller->status.drq = 0;
650           controller->status.err = 0;
651           
652           rd_raise_interrupt(dev, channel);
653         }
654       }
655       return length;
656       break;
657     }
658
659   default:
660     PrintDebug("\t\tread need support more command: %02x\n", controller->current_command);
661     break;
662   }
663
664   return -1;
665 }
666
667
668
669
670 static int write_data_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
671   struct ramdisk_t * ramdisk  = (struct ramdisk_t *)(dev->private_data);
672   struct channel_t * channel = NULL;
673   struct drive_t * drive = NULL;
674   struct controller_t * controller = NULL;
675
676   if (is_primary_port(ramdisk, port)) {
677     channel = &(ramdisk->channels[0]);
678   } else if (is_secondary_port(ramdisk, port)) {
679     channel = &(ramdisk->channels[1]);
680   } else {
681     PrintError("Invalid Port: %d\n", port);
682     return -1;
683   }
684   
685   drive = get_selected_drive(channel);
686   controller = &(drive->controller);
687
688
689   PrintDebug("[write_data_handler] IO write at 0x%x, current_cmd = 0x%02x\n", 
690              port, controller->current_command);
691
692  
693
694   //PrintDebug("[write_data_handler]\n");
695   switch (controller->current_command) {
696   case 0x30: // WRITE SECTORS
697     PrintError("\t\tneed to implement 0x30(write sector) to port 0x%x\n", port);
698     return -1;
699     
700   case 0xa0: // PACKET
701     
702     if (handle_atapi_packet_command(dev, channel, *(ushort_t *)src) == -1) {
703       return -1;
704     }
705
706     return length;
707     
708   default:
709     PrintError("\t\tIO write(0x%x): current command is %02xh\n", 
710                port, controller->current_command);
711
712     return -1;
713   }
714
715
716   return -1;
717 }
718
719
720
721
722
723
724
725 static int read_status_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
726   struct ramdisk_t * ramdisk  = (struct ramdisk_t *)(dev->private_data);
727   struct channel_t * channel = NULL;
728   struct drive_t * drive = NULL;
729   struct controller_t * controller = NULL;
730
731
732
733
734   if (is_primary_port(ramdisk, port)) {
735     channel = &(ramdisk->channels[0]);
736   } else if (is_secondary_port(ramdisk, port)) {
737     channel = &(ramdisk->channels[1]);
738   } else {
739     PrintError("Invalid Port: %d\n", port);
740     return -1;
741   }
742   
743   drive = get_selected_drive(channel);
744   controller = &(drive->controller);
745
746  
747   PrintDebug("[read_status_handler] IO read at 0x%x, on drive %d/%d\n", 
748              port, get_channel_no(ramdisk, channel), 
749              channel->drive_select);
750
751
752   if (num_drives_on_channel(channel) == 0) {
753     // (mch) Just return zero for these registers
754     memset(dst, 0, length);
755
756   } else {
757     uchar_t val = (
758                    (controller->status.busy << 7)            |
759                    (controller->status.drive_ready << 6)     |
760                    (controller->status.write_fault << 5)     |
761                    (controller->status.seek_complete << 4)   |
762                    (controller->status.drq << 3)             |
763                    (controller->status.corrected_data << 2)  |
764                    (controller->status.index_pulse << 1)     |
765                    (controller->status.err) );
766
767
768     memcpy(dst, &val, length);
769
770     controller->status.index_pulse_count++;
771     controller->status.index_pulse = 0;
772     
773     if (controller->status.index_pulse_count >= INDEX_PULSE_CYCLE) {
774       controller->status.index_pulse = 1;
775       controller->status.index_pulse_count = 0;
776     }
777   }
778   
779   if ((port == SEC_CMD_PORT) || (port == PRI_CMD_PORT)) {
780     rd_lower_irq(dev, channel->irq);
781   }
782   
783   PrintDebug("\t\tRead STATUS = 0x%x\n", *(uchar_t *)dst);
784
785   return length;
786   
787 }
788
789
790 static int write_cmd_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
791   struct ramdisk_t * ramdisk  = (struct ramdisk_t *)(dev->private_data);
792   struct channel_t * channel = NULL;
793   struct drive_t * drive = NULL;
794   struct controller_t * controller = NULL;
795   uchar_t value = *(uchar_t *)src;
796
797   if (length != 1) {
798     PrintError("Invalid Command port write length: %d (port=%d)\n", length, port);
799     return -1;
800   }
801
802   if (is_primary_port(ramdisk, port)) {
803     channel = &(ramdisk->channels[0]);
804   } else if (is_secondary_port(ramdisk, port)) {
805     channel = &(ramdisk->channels[1]);
806   } else {
807     PrintError("Invalid Port: %d\n", port);
808     return -1;
809   }
810   
811   drive = get_selected_drive(channel);
812   controller = &(drive->controller);
813
814
815   PrintDebug("[write_command_handler] IO write at 0x%x, on drive %d/%d (val = 0x%x)\n", 
816              port, get_channel_no(ramdisk, channel), 
817              channel->drive_select, 
818              value);
819
820   switch (value) {
821     // ATAPI commands
822   case 0xa1: // IDENTIFY PACKET DEVICE
823     {
824       if (drive->device_type == IDE_CDROM) {
825         controller->current_command = value;
826         controller->error_register = 0;
827         
828         controller->status.busy = 0;
829         controller->status.drive_ready = 1;
830         controller->status.write_fault = 0;
831         controller->status.drq   = 1;
832         controller->status.err   = 0;
833         
834         controller->status.seek_complete = 1;
835         controller->status.corrected_data = 0;
836         
837         controller->buffer_index = 0;
838         rd_raise_interrupt(dev, channel);
839         rd_identify_ATAPI_drive(dev, channel);
840       } else {
841         rd_command_aborted(dev, channel, 0xa1);
842       }
843       break;
844     }
845   case 0xa0: // SEND PACKET (atapi)
846     {
847       if (drive->device_type == IDE_CDROM) {
848         // PACKET
849         
850         if (controller->features & (1 << 0)) {
851           PrintError("\t\tPACKET-DMA not supported");
852           return -1;
853         }
854         
855         if (controller->features & (1 << 1)) {
856           PrintError("\t\tPACKET-overlapped not supported");
857           return -1;
858         }
859         
860         // We're already ready!
861         controller->sector_count = 1;
862         controller->status.busy = 0;
863         controller->status.write_fault = 0;
864
865         // serv bit??
866         controller->status.drq = 1;
867         controller->status.err = 0;
868         
869         // NOTE: no interrupt here
870         controller->current_command = value;
871         controller->buffer_index = 0;
872       } else {
873         rd_command_aborted (dev, channel, 0xa0);
874       }
875       break;
876     }
877   default:
878     PrintError("\t\tneed translate command %2x\n", value);
879     //return -1;
880     /* JRL THIS NEEDS TO CHANGE */
881     return length;
882
883   }
884   return length;
885 }
886
887
888 static int write_ctrl_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
889   struct ramdisk_t * ramdisk  = (struct ramdisk_t *)(dev->private_data);
890   struct channel_t * channel = NULL;
891   struct drive_t * master_drive = NULL;
892   struct drive_t * slave_drive = NULL;
893   struct controller_t * controller = NULL;
894   uchar_t value = *(uchar_t *)src;
895   rd_bool prev_control_reset;
896
897   if (length != 1) {
898     PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
899     return -1;
900   }
901
902   if (is_primary_port(ramdisk, port)) {
903     channel = &(ramdisk->channels[0]);
904   } else if (is_secondary_port(ramdisk, port)) {
905     channel = &(ramdisk->channels[1]);
906   } else {
907     PrintError("Invalid Port: %d\n", port);
908     return -1;
909   }
910
911   master_drive = &(channel->drives[0]);
912   slave_drive = &(channel->drives[1]);
913
914   controller = &(get_selected_drive(channel)->controller);
915
916
917   PrintDebug("[write_control_handler] IO write at 0x%x, on drive %d/%d (val = 0x%x)\n", 
918              port, get_channel_no(ramdisk, channel), 
919              channel->drive_select, 
920              value);
921
922   // (mch) Even if device 1 was selected, a write to this register
923   // goes to device 0 (if device 1 is absent)
924   
925   prev_control_reset = controller->control.reset;
926
927
928   if (value & 0x04) {
929     PrintDebug("RESET Signaled\n");
930   }
931
932   master_drive->controller.control.reset         = value & 0x04;
933   slave_drive->controller.control.reset         = value & 0x04;
934
935   // CGS: was: SELECTED_CONTROLLER(channel).control.disable_irq    = value & 0x02;
936   master_drive->controller.control.disable_irq = value & 0x02;
937   slave_drive->controller.control.disable_irq = value & 0x02;
938   
939   PrintDebug("\t\tadpater control reg: reset controller = %d\n",
940                 (unsigned) (controller->control.reset) ? 1 : 0);
941   PrintDebug("\t\tadpater control reg: disable_irq(X) = %d\n",
942                 (unsigned) (controller->control.disable_irq) ? 1 : 0);
943   
944   if ((!prev_control_reset) && (controller->control.reset)) {
945     uint_t id = 0;
946
947     // transition from 0 to 1 causes all drives to reset
948     PrintDebug("\t\thard drive: RESET\n");
949     
950     // (mch) Set BSY, drive not ready
951     for (id = 0; id < 2; id++) {
952       struct controller_t * ctrl = NULL;
953
954       if (id == 0) {
955         ctrl = &(master_drive->controller);
956       } else if (id == 1) {
957         ctrl = &(slave_drive->controller);
958       }
959
960       ctrl->status.busy           = 1;
961       ctrl->status.drive_ready    = 0;
962       ctrl->reset_in_progress     = 1;
963       
964       ctrl->status.write_fault    = 0;
965       ctrl->status.seek_complete  = 1;
966       ctrl->status.drq            = 0;
967       ctrl->status.corrected_data = 0;
968       ctrl->status.err            = 0;
969       
970       ctrl->error_register = 0x01; // diagnostic code: no error
971       
972       ctrl->current_command = 0x00;
973       ctrl->buffer_index = 0;
974       
975       ctrl->sectors_per_block = 0x80;
976       ctrl->lba_mode          = 0;
977       
978       ctrl->control.disable_irq = 0;
979     }
980
981     rd_lower_irq(dev, channel->irq);
982
983   } else if ((controller->reset_in_progress) &&
984              (!controller->control.reset)) {
985     uint_t id;
986     // Clear BSY and DRDY
987     PrintDebug("\t\tReset complete {%s}\n", device_type_to_str(get_selected_drive(channel)->device_type));
988
989     for (id = 0; id < 2; id++) {
990       struct controller_t * ctrl = NULL;
991       struct drive_t * drv = NULL;
992
993       if (id == 0) {
994         ctrl = &(master_drive->controller);
995         drv = master_drive;
996       } else if (id == 1) {
997         ctrl = &(slave_drive->controller);
998         drv = slave_drive;
999       }
1000
1001       ctrl->status.busy           = 0;
1002       ctrl->status.drive_ready    = 1;
1003       ctrl->reset_in_progress     = 0;
1004       
1005       // Device signature
1006       if (drv->device_type == IDE_DISK) {
1007         PrintDebug("\t\tdrive %d/%d is harddrive\n", get_channel_no(ramdisk, channel), id);
1008         ctrl->head_no        = 0;
1009         ctrl->sector_count   = 1;
1010         ctrl->sector_no      = 1;
1011         ctrl->cylinder_no    = 0;
1012       } else {
1013         ctrl->head_no        = 0;
1014         ctrl->sector_count   = 1;
1015         ctrl->sector_no      = 1;
1016         ctrl->cylinder_no    = 0xeb14;
1017       }
1018     }
1019   }
1020
1021   PrintDebug("\t\ts[0].controller.control.disable_irq = %02x\n", 
1022              master_drive->controller.control.disable_irq);
1023   PrintDebug("\t\ts[1].controller.control.disable_irq = %02x\n", 
1024              slave_drive->controller.control.disable_irq);
1025   return length;
1026 }
1027
1028
1029 static int read_general_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
1030   struct ramdisk_t * ramdisk  = (struct ramdisk_t *)(dev->private_data);
1031   struct channel_t * channel = NULL;
1032   struct drive_t * drive = NULL;
1033   struct controller_t * controller = NULL;
1034
1035
1036   if (length != 1) {
1037     PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
1038     return -1;
1039   }
1040
1041   if (is_primary_port(ramdisk, port)) {
1042     channel = &(ramdisk->channels[0]);
1043   } else if (is_secondary_port(ramdisk, port)) {
1044     channel = &(ramdisk->channels[1]);
1045   } else {
1046     PrintError("Invalid Port: %d\n", port);
1047     return -1;
1048   }
1049   
1050   drive = get_selected_drive(channel);
1051   controller = &(drive->controller);
1052
1053
1054   PrintDebug("[read_general_handler] IO read addr at %x, on drive %d/%d, curcmd = %02x\n", 
1055              port, get_channel_no(ramdisk, channel), 
1056              channel->drive_select, 
1057              controller->current_command);
1058   
1059
1060   switch (port) {
1061   case PRI_FEATURES_PORT:
1062   case SEC_FEATURES_PORT: // hard disk error register 0x1f1
1063     {    
1064       uchar_t val = (drive->device_type == IDE_NONE) ? 0 : controller->error_register;
1065       
1066       controller->status.err = 0;
1067       
1068       PrintDebug("\t\tRead FEATURES = 0x%x\n", val);
1069
1070       *(uchar_t *)dst = val;
1071       return length;
1072       
1073       break;
1074     }
1075
1076   case PRI_SECT_CNT_PORT:
1077   case SEC_SECT_CNT_PORT:  // hard disk sector count / interrupt reason 0x1f2
1078     {
1079       uchar_t val = (drive->device_type == IDE_NONE) ? 0 : controller->sector_count;
1080       PrintDebug("\t\tRead SECTOR COUNT = 0x%x\n", val);
1081       *(uchar_t *)dst = val;
1082       return length;
1083
1084       break;
1085     }
1086   case PRI_SECT_ADDR1_PORT:
1087   case SEC_SECT_ADDR1_PORT: // sector number 0x1f3
1088     { 
1089       uchar_t val = (drive->device_type == IDE_NONE) ? 0 : controller->sector_no;
1090
1091       PrintDebug("\t\tRead SECTOR ADDR1 = 0x%x\n", val);
1092
1093       *(uchar_t *)dst = val;
1094       return length;
1095
1096       break;
1097     }
1098
1099   case PRI_SECT_ADDR2_PORT:
1100   case SEC_SECT_ADDR2_PORT:  // cylinder low 0x1f4  
1101     {
1102       // -- WARNING : On real hardware the controller registers are shared between drives. 
1103       // So we must respond even if the select device is not present. Some OS uses this fact 
1104       // to detect the disks.... minix2 for example
1105       uchar_t val = (num_drives_on_channel(channel) == 0) ? 0 : (controller->cylinder_no & 0x00ff);
1106
1107       PrintDebug("\t\tRead SECTOR ADDR2 = 0x%x\n", val);
1108
1109       *(uchar_t *)dst = val;
1110       return length;
1111
1112       break;      
1113   }
1114
1115   case PRI_SECT_ADDR3_PORT:
1116   case SEC_SECT_ADDR3_PORT: // cylinder high 0x1f5
1117     {
1118       // -- WARNING : On real hardware the controller registers are shared between drives. 
1119       // So we must respond even if the select device is not present. Some OS uses this fact 
1120       // to detect the disks.... minix2 for example
1121       uchar_t val = (num_drives_on_channel(channel) == 0) ? 0 : (controller->cylinder_no >> 8);
1122
1123       PrintDebug("\t\tRead SECTOR ADDR3 = 0x%x\n", val);
1124
1125       *(uchar_t *)dst = val;
1126       return length;
1127
1128       break;    
1129     }
1130   case PRI_DRV_SEL_PORT:
1131   case SEC_DRV_SEL_PORT:  // hard disk drive and head register 0x1f6
1132     {
1133       // b7 Extended data field for ECC
1134       // b6/b5: Used to be sector size.  00=256,01=512,10=1024,11=128
1135       //   Since 512 was always used, bit 6 was taken to mean LBA mode:
1136       //     b6 1=LBA mode, 0=CHS mode
1137       //     b5 1
1138       // b4: DRV
1139       // b3..0 HD3..HD0
1140       uchar_t val = ((1 << 7)                          |
1141                      ((controller->lba_mode > 0) << 6) |
1142                      (1 << 5)                          |            // 01b = 512 sector size
1143                      (channel->drive_select << 4)      |
1144                      (controller->head_no << 0));
1145       
1146       PrintDebug("\t\tRead DRIVE SELECT = 0x%x\n", val);
1147       *(uchar_t *)dst = val;
1148       return length;
1149
1150       break;
1151     }
1152  case PRI_ADDR_REG_PORT:
1153  case SEC_ADDR_REG_PORT: // Hard Disk Address Register 0x3f7
1154    {
1155      // Obsolete and unsupported register.  Not driven by hard
1156      // disk controller.  Report all 1's.  If floppy controller
1157      // is handling this address, it will call this function
1158      // set/clear D7 (the only bit it handles), then return
1159      // the combined value
1160      *(uchar_t *)dst = 0xff;
1161      return length;
1162     }
1163
1164   default:
1165     PrintError("Invalid Port: %d\n", port);
1166     return -1;
1167   }
1168 }
1169
1170
1171
1172
1173 static int write_general_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
1174   struct ramdisk_t * ramdisk  = (struct ramdisk_t *)(dev->private_data);
1175   struct channel_t * channel = NULL;
1176   struct drive_t * drive = NULL;
1177   struct controller_t * controller = NULL;
1178   uchar_t value = *(uchar_t *)src;
1179
1180   if (length != 1) {
1181     PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
1182     return -1;
1183   }
1184
1185   if (is_primary_port(ramdisk, port)) {
1186     channel = &(ramdisk->channels[0]);
1187   } else if (is_secondary_port(ramdisk, port)) {
1188     channel = &(ramdisk->channels[1]);
1189   } else {
1190     PrintError("Invalid Port: %d\n", port);
1191     return -1;
1192   }
1193   
1194   drive = get_selected_drive(channel);
1195   controller = &(drive->controller);
1196
1197
1198   PrintDebug("[write_general_handler] IO write to port %x (val=0x%02x), channel = %d\n", 
1199              port, value, get_channel_no(ramdisk, channel));
1200
1201   switch (port) {
1202
1203   case PRI_FEATURES_PORT:
1204   case SEC_FEATURES_PORT: // hard disk write precompensation 0x1f1
1205     {
1206       write_features(channel, value);
1207       break;
1208     }
1209   case PRI_SECT_CNT_PORT:
1210   case SEC_SECT_CNT_PORT: // hard disk sector count 0x1f2
1211     {
1212       write_sector_count(channel, value);
1213       break;
1214     }
1215   case PRI_SECT_ADDR1_PORT:
1216   case SEC_SECT_ADDR1_PORT: // hard disk sector number 0x1f3
1217     {
1218       write_sector_number(channel, value);
1219       break;
1220     }
1221   case PRI_SECT_ADDR2_PORT:
1222   case SEC_SECT_ADDR2_PORT: // hard disk cylinder low 0x1f4
1223     {
1224       write_cylinder_low(channel, value);
1225       break;
1226     }
1227   case PRI_SECT_ADDR3_PORT:
1228   case SEC_SECT_ADDR3_PORT: // hard disk cylinder high 0x1f5
1229     {
1230       write_cylinder_high(channel, value);
1231       break;
1232     }
1233   case PRI_DRV_SEL_PORT:
1234   case SEC_DRV_SEL_PORT: // hard disk drive and head register 0x1f6
1235     {
1236       // b7 Extended data field for ECC
1237       // b6/b5: Used to be sector size.  00=256,01=512,10=1024,11=128
1238       //   Since 512 was always used, bit 6 was taken to mean LBA mode:
1239       //     b6 1=LBA mode, 0=CHS mode
1240       //     b5 1
1241       // b4: DRV
1242       // b3..0 HD3..HD0
1243
1244       // 1x1xxxxx
1245
1246       PrintDebug("\tDrive Select value=%x\n", value);
1247
1248       if ((value & 0xa0) != 0xa0) { 
1249         PrintDebug("\t\tIO write 0x%x (%02x): not 1x1xxxxxb\n", port, (unsigned) value);
1250       }
1251       
1252       write_head_no(channel, value & 0xf);
1253       if ((controller->lba_mode == 0) && (((value >> 6) & 1) == 1)) {
1254         PrintDebug("\t\tenabling LBA mode\n");
1255       }
1256
1257       write_lba_mode(channel, (value >> 6) & 1);
1258
1259
1260
1261       if (drive->cdrom.cd) {
1262         PrintDebug("\t\tSetting LBA on CDROM: %d\n", (value >> 6) & 1);
1263         drive->cdrom.cd->set_LBA(drive->private_data, (value >> 6) & 1);
1264       }
1265       
1266
1267       channel->drive_select = (value >> 4) & 0x01;
1268       drive = get_selected_drive(channel);
1269
1270       if (drive->device_type == IDE_NONE) {
1271         PrintDebug("\t\tError: device set to %d which does not exist! channel = 0x%x\n",
1272                    channel->drive_select, get_channel_no(ramdisk, channel));
1273
1274         controller->error_register = 0x04; // aborted
1275         controller->status.err = 1;
1276       }
1277       
1278       break;
1279     }
1280   default:
1281     PrintError("\t\thard drive: io write to unhandled port 0x%x  (value = %c)\n", port, value);
1282     //return -1;
1283   }
1284
1285   return length;
1286 }
1287
1288
1289  
1290
1291
1292 static void rd_raise_interrupt(struct vm_device * dev, struct channel_t * channel) {
1293   Bit32u irq;
1294   //  struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1295   struct drive_t * drive = get_selected_drive(channel);
1296   struct controller_t * controller = &(drive->controller);
1297
1298   PrintDebug("[raise_interrupt] disable_irq = %02x\n", controller->control.disable_irq);
1299
1300   if (!(controller->control.disable_irq)) {
1301     irq = channel->irq; 
1302  
1303     PrintDebug("\t\tRaising interrupt %d {%s}\n\n", irq, device_type_to_str(drive->device_type));
1304
1305     dev->vm->vm_ops.raise_irq(dev->vm, irq);
1306   } else {
1307     PrintDebug("\t\tirq is disabled\n");
1308   }
1309   
1310   return;
1311 }
1312
1313 static void rd_lower_irq(struct vm_device *dev, Bit32u irq)  // __attribute__(regparm(1))
1314 {
1315   PrintDebug("[lower_irq] irq = %d\n", irq);
1316   dev->vm->vm_ops.lower_irq(dev->vm, irq);
1317 }
1318
1319
1320
1321
1322
1323
1324
1325 //////////////////////////////////////////////////////////////////////////
1326
1327 /*
1328  * ATAPI subroutines
1329  */
1330
1331
1332
1333 int handle_atapi_packet_command(struct vm_device * dev, struct channel_t * channel, ushort_t value) {
1334   struct ramdisk_t * ramdisk  = (struct ramdisk_t *)(dev->private_data);
1335   struct drive_t * drive = get_selected_drive(channel);
1336   struct controller_t * controller = &(drive->controller);
1337
1338   if (controller->buffer_index >= PACKET_SIZE) {
1339     PrintError("ATAPI packet exceeded maximum length: buffer_index (%d) >= PACKET_SIZE\n", 
1340                controller->buffer_index);
1341     return -1;
1342   }
1343
1344   controller->buffer[controller->buffer_index] = value;
1345   controller->buffer[controller->buffer_index + 1] = (value >> 8);
1346   controller->buffer_index += 2;
1347   
1348   
1349   /* if packet completely writtten */
1350   if (controller->buffer_index >= PACKET_SIZE) {
1351     // complete command received
1352     Bit8u atapi_command = controller->buffer[0];
1353     
1354     PrintDebug("\t\tcdrom: ATAPI command 0x%x started\n", atapi_command);
1355     
1356     switch (atapi_command) {
1357     case 0x00: // test unit ready
1358       {
1359         if (drive->cdrom.ready) {
1360           rd_atapi_cmd_nop(dev, channel);
1361         } else {
1362           rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1363         }
1364         
1365         rd_raise_interrupt(dev, channel);
1366         
1367         break;
1368       }
1369     case 0x03:  // request sense
1370       {
1371         int alloc_length = controller->buffer[4];
1372
1373         if (rd_init_send_atapi_command(dev, channel, atapi_command, 18, alloc_length, false) == -1) {
1374           return -1;
1375         }
1376         
1377         // sense data
1378         controller->buffer[0] = 0x70 | (1 << 7);
1379         controller->buffer[1] = 0;
1380         controller->buffer[2] = drive->sense.sense_key;
1381         controller->buffer[3] = drive->sense.information.arr[0];
1382         controller->buffer[4] = drive->sense.information.arr[1];
1383         controller->buffer[5] = drive->sense.information.arr[2];
1384         controller->buffer[6] = drive->sense.information.arr[3];
1385         controller->buffer[7] = 17 - 7;
1386         controller->buffer[8] = drive->sense.specific_inf.arr[0];
1387         controller->buffer[9] = drive->sense.specific_inf.arr[1];
1388         controller->buffer[10] = drive->sense.specific_inf.arr[2];
1389         controller->buffer[11] = drive->sense.specific_inf.arr[3];
1390         controller->buffer[12] = drive->sense.asc;
1391         controller->buffer[13] = drive->sense.ascq;
1392         controller->buffer[14] = drive->sense.fruc;
1393         controller->buffer[15] = drive->sense.key_spec.arr[0];
1394         controller->buffer[16] = drive->sense.key_spec.arr[1];
1395         controller->buffer[17] = drive->sense.key_spec.arr[2];
1396         
1397         rd_ready_to_send_atapi(dev, channel);
1398         break;
1399       }
1400     case 0x1b:  // start stop unit
1401       {
1402         //bx_bool Immed = (controller->buffer[1] >> 0) & 1;
1403         rd_bool LoEj = (controller->buffer[4] >> 1) & 1;
1404         rd_bool Start = (controller->buffer[4] >> 0) & 1;
1405
1406         // stop the disc
1407         if ((!LoEj) && (!Start)) { 
1408           PrintError("FIXME: Stop disc not implemented\n");
1409
1410           rd_atapi_cmd_nop(dev, channel);
1411           rd_raise_interrupt(dev, channel);
1412         } else if (!LoEj && Start) { // start (spin up) the disc
1413           
1414           drive->cdrom.cd->start_cdrom(drive->private_data);
1415           
1416           PrintError("FIXME: ATAPI start disc not reading TOC\n");
1417           rd_atapi_cmd_nop(dev, channel);
1418           rd_raise_interrupt(dev, channel);
1419         } else if (LoEj && !Start) { // Eject the disc
1420           rd_atapi_cmd_nop(dev, channel);
1421           
1422           if (drive->cdrom.ready) {
1423             
1424             drive->cdrom.cd->eject_cdrom(drive->private_data);
1425             
1426             drive->cdrom.ready = 0;
1427             //bx_options.atadevice[channel][SLAVE_SELECTED(channel)].Ostatus->set(EJECTED);
1428             //bx_gui->update_drive_status_buttons();
1429           }
1430           rd_raise_interrupt(dev, channel);
1431         } else { // Load the disc
1432           // My guess is that this command only closes the tray, that's a no-op for us
1433           rd_atapi_cmd_nop(dev, channel);
1434           rd_raise_interrupt(dev, channel);
1435         }
1436         break;
1437       }
1438     case 0xbd: // mechanism status
1439       {
1440         uint16_t alloc_length = rd_read_16bit(controller->buffer + 8);
1441         
1442         if (alloc_length == 0) {
1443           PrintError("Zero allocation length to MECHANISM STATUS not impl.\n");
1444           return -1;
1445         }
1446         
1447         if (rd_init_send_atapi_command(dev, channel, atapi_command, 8, alloc_length, false) == -1) {
1448           return -1;
1449         }
1450         
1451         controller->buffer[0] = 0; // reserved for non changers
1452         controller->buffer[1] = 0; // reserved for non changers
1453         
1454         controller->buffer[2] = 0; // Current LBA (TODO!)
1455         controller->buffer[3] = 0; // Current LBA (TODO!)
1456         controller->buffer[4] = 0; // Current LBA (TODO!)
1457         
1458         controller->buffer[5] = 1; // one slot
1459         
1460         controller->buffer[6] = 0; // slot table length
1461         controller->buffer[7] = 0; // slot table length
1462         
1463         rd_ready_to_send_atapi(dev, channel);
1464         break;
1465       }
1466     case 0x5a:  // mode sense
1467       {
1468         uint16_t alloc_length = rd_read_16bit(controller->buffer + 7);
1469         
1470         Bit8u PC = controller->buffer[2] >> 6;
1471         Bit8u PageCode = controller->buffer[2] & 0x3f;
1472         
1473         switch (PC) {
1474         case 0x0: // current values
1475           {
1476             switch (PageCode) {
1477             case 0x01: // error recovery
1478               {
1479                 
1480                 if (rd_init_send_atapi_command(dev, channel, atapi_command, sizeof(struct error_recovery_t) + 8, alloc_length, false) == -1) {
1481                   return -1;
1482                 }
1483                 
1484                 rd_init_mode_sense_single(dev, channel, &(drive->cdrom.current.error_recovery),
1485                                           sizeof(struct error_recovery_t));
1486                 rd_ready_to_send_atapi(dev, channel);
1487                 break;
1488               }
1489             case 0x2a: // CD-ROM capabilities & mech. status
1490               {
1491
1492                 if (rd_init_send_atapi_command(dev, channel, atapi_command, 28, alloc_length, false) == -1) {
1493                   return -1;
1494                 }
1495
1496                 rd_init_mode_sense_single(dev, channel, &(controller->buffer[8]), 28);
1497                 
1498                 controller->buffer[8] = 0x2a;
1499                 controller->buffer[9] = 0x12;
1500                 controller->buffer[10] = 0x00;
1501                 controller->buffer[11] = 0x00;
1502                 // Multisession, Mode 2 Form 2, Mode 2 Form 1
1503                 controller->buffer[12] = 0x70; 
1504                 controller->buffer[13] = (3 << 5);
1505                 controller->buffer[14] = (unsigned char) (1 |
1506                                                           (drive->cdrom.locked ? (1 << 1) : 0) |
1507                                                           (1 << 3) |
1508                                                           (1 << 5));
1509                 controller->buffer[15] = 0x00;
1510                 controller->buffer[16] = (706 >> 8) & 0xff;
1511                 controller->buffer[17] = 706 & 0xff;
1512                 controller->buffer[18] = 0;
1513                 controller->buffer[19] = 2;
1514                 controller->buffer[20] = (512 >> 8) & 0xff;
1515                 controller->buffer[21] = 512 & 0xff;
1516                 controller->buffer[22] = (706 >> 8) & 0xff;
1517                 controller->buffer[23] = 706 & 0xff;
1518                 controller->buffer[24] = 0;
1519                 controller->buffer[25] = 0;
1520                 controller->buffer[26] = 0;
1521                 controller->buffer[27] = 0;
1522                 rd_ready_to_send_atapi(dev, channel);
1523                 break;
1524               }
1525             case 0x0d: // CD-ROM
1526             case 0x0e: // CD-ROM audio control
1527             case 0x3f: // all
1528               {
1529                 PrintError("Ramdisk: cdrom: MODE SENSE (curr), code=%x not implemented yet\n",
1530                          PageCode);
1531                 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1532                                    ASC_INV_FIELD_IN_CMD_PACKET);
1533                 rd_raise_interrupt(dev, channel);
1534                 break;
1535               }
1536             default:
1537               {
1538                 // not implemeted by this device
1539                 PrintDebug("\t\tcdrom: MODE SENSE PC=%x, PageCode=%x, not implemented by device\n",
1540                               PC, PageCode);
1541                 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1542                                    ASC_INV_FIELD_IN_CMD_PACKET);
1543                 rd_raise_interrupt(dev, channel);
1544                 break;
1545               }
1546             }
1547             break;
1548           }
1549         case 0x1: // changeable values
1550           {
1551             switch (PageCode) {
1552             case 0x01: // error recovery
1553             case 0x0d: // CD-ROM
1554             case 0x0e: // CD-ROM audio control
1555             case 0x2a: // CD-ROM capabilities & mech. status
1556             case 0x3f: // all
1557               {
1558                 PrintError("cdrom: MODE SENSE (chg), code=%x not implemented yet\n",
1559                            PageCode);
1560                 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1561                                    ASC_INV_FIELD_IN_CMD_PACKET);
1562                 rd_raise_interrupt(dev, channel);
1563                 break;
1564               }
1565             default:
1566               {
1567                 // not implemeted by this device
1568                 PrintDebug("\t\tcdrom: MODE SENSE PC=%x, PageCode=%x, not implemented by device\n",
1569                            PC, PageCode);
1570                 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1571                                    ASC_INV_FIELD_IN_CMD_PACKET);
1572                 rd_raise_interrupt(dev, channel);
1573                 break;
1574               }
1575             }
1576             break;
1577           }
1578         case 0x2: // default values
1579           {
1580             switch (PageCode) {
1581             case 0x01: // error recovery
1582             case 0x0d: // CD-ROM
1583             case 0x0e: // CD-ROM audio control
1584             case 0x2a: // CD-ROM capabilities & mech. status
1585             case 0x3f: // all
1586               PrintError("cdrom: MODE SENSE (dflt), code=%x\n",
1587                        PageCode);
1588               return -1;
1589               
1590             default:
1591               {
1592                 // not implemeted by this device
1593                 PrintDebug("\t\tcdrom: MODE SENSE PC=%x, PageCode=%x, not implemented by device\n",
1594                               PC, PageCode);
1595                 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1596                                    ASC_INV_FIELD_IN_CMD_PACKET);
1597                 rd_raise_interrupt(dev, channel);
1598                 break;
1599               }
1600             }
1601             break;
1602           }
1603         case 0x3: // saved values not implemented
1604           {
1605             rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
1606             rd_raise_interrupt(dev, channel);
1607             break;
1608           }
1609         default:
1610           {
1611             PrintError("Should not get here!\n");
1612             return -1;
1613             break;
1614           }
1615         }
1616         break;
1617       }
1618     case 0x12: // inquiry
1619       { 
1620         uint8_t alloc_length = controller->buffer[4];
1621         
1622         if (rd_init_send_atapi_command(dev, channel, atapi_command, 36, alloc_length, false) == -1) {
1623           return -1;
1624         }
1625         
1626         controller->buffer[0] = 0x05; // CD-ROM
1627         controller->buffer[1] = 0x80; // Removable
1628         controller->buffer[2] = 0x00; // ISO, ECMA, ANSI version
1629         controller->buffer[3] = 0x21; // ATAPI-2, as specified
1630         controller->buffer[4] = 31; // additional length (total 36)
1631         controller->buffer[5] = 0x00; // reserved
1632         controller->buffer[6] = 0x00; // reserved
1633         controller->buffer[7] = 0x00; // reserved
1634         
1635         // Vendor ID
1636         const char* vendor_id = "VTAB    ";
1637         int i;
1638         for (i = 0; i < 8; i++) {
1639           controller->buffer[8+i] = vendor_id[i];
1640         }
1641
1642         // Product ID
1643         const char* product_id = "Turbo CD-ROM    ";
1644         for (i = 0; i < 16; i++) {
1645           controller->buffer[16+i] = product_id[i];
1646         }
1647
1648         // Product Revision level
1649         const char* rev_level = "1.0 "; 
1650         for (i = 0; i < 4; i++) {
1651           controller->buffer[32 + i] = rev_level[i];
1652         }
1653
1654         rd_ready_to_send_atapi(dev, channel);
1655         break;
1656       }
1657     case 0x25:  // read cd-rom capacity
1658       {
1659         // no allocation length???
1660         if (rd_init_send_atapi_command(dev, channel, atapi_command, 8, 8, false) == -1) {
1661           return -1;
1662         }
1663         
1664         if (drive->cdrom.ready) {
1665           uint32_t capacity = drive->cdrom.capacity;
1666
1667           PrintDebug("\t\tCapacity is %d sectors (%d bytes)\n", capacity, capacity * 2048);
1668
1669           controller->buffer[0] = (capacity >> 24) & 0xff;
1670           controller->buffer[1] = (capacity >> 16) & 0xff;
1671           controller->buffer[2] = (capacity >> 8) & 0xff;
1672           controller->buffer[3] = (capacity >> 0) & 0xff;
1673           controller->buffer[4] = (2048 >> 24) & 0xff;
1674           controller->buffer[5] = (2048 >> 16) & 0xff;
1675           controller->buffer[6] = (2048 >> 8) & 0xff;
1676           controller->buffer[7] = (2048 >> 0) & 0xff;
1677
1678           rd_ready_to_send_atapi(dev, channel);
1679         } else {
1680           rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1681           rd_raise_interrupt(dev, channel);
1682         }
1683         break;
1684       }
1685       
1686       
1687     case 0xbe:  // read cd
1688       {
1689         if (drive->cdrom.ready) {
1690           PrintError("Read CD with CD present not implemented\n");
1691           rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1692           rd_raise_interrupt(dev, channel);
1693         } else {
1694           rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1695           rd_raise_interrupt(dev, channel);
1696         }
1697         break;
1698       }
1699     case 0x43: // read toc
1700       { 
1701         if (drive->cdrom.ready) {
1702           int toc_length = 0;  
1703           bool msf = (controller->buffer[1] >> 1) & 1;
1704           uint8_t starting_track = controller->buffer[6];
1705           
1706           uint16_t alloc_length = rd_read_16bit(controller->buffer + 7);
1707           
1708           uint8_t format = (controller->buffer[9] >> 6);
1709           int i;
1710
1711           PrintDebug("Reading CDROM TOC: Format=%d (byte count=%d) (toc length:%d)\n", 
1712                      format, controller->byte_count, toc_length);
1713
1714           switch (format) {
1715           case 0:
1716             {
1717               if (!(drive->cdrom.cd->read_toc(drive->private_data, controller->buffer,
1718                                               &toc_length, msf, starting_track))) {
1719                 PrintError("CDROM: Reading Table of Contents Failed\n");
1720                 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1721                                    ASC_INV_FIELD_IN_CMD_PACKET);
1722                 rd_raise_interrupt(dev, channel);
1723                 break;
1724               }
1725
1726               if (rd_init_send_atapi_command(dev, channel, atapi_command, toc_length, alloc_length, false) == -1) {
1727                 return -1;
1728               }
1729
1730               rd_ready_to_send_atapi(dev, channel);    
1731
1732               break;
1733             }
1734           case 1:
1735             // multi session stuff. we ignore this and emulate a single session only
1736
1737             if (rd_init_send_atapi_command(dev, channel, atapi_command, 12, alloc_length, false) == -1) {
1738               return -1;
1739             }
1740             
1741             controller->buffer[0] = 0;
1742             controller->buffer[1] = 0x0a;
1743             controller->buffer[2] = 1;
1744             controller->buffer[3] = 1;
1745
1746             for (i = 0; i < 8; i++) {
1747               controller->buffer[4 + i] = 0;
1748             }
1749
1750             rd_ready_to_send_atapi(dev, channel);
1751             break;
1752             
1753           case 2:
1754           default:
1755             PrintError("(READ TOC) Format %d not supported\n", format);
1756             return -1;
1757           }
1758         } else {
1759           rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1760           rd_raise_interrupt(dev, channel);
1761         }
1762         break;
1763       }
1764     case 0x28: // read (10)
1765     case 0xa8: // read (12)
1766       { 
1767         
1768         uint32_t transfer_length;
1769         if (atapi_command == 0x28) {
1770           transfer_length = rd_read_16bit(controller->buffer + 7);
1771         } else {
1772           transfer_length = rd_read_32bit(controller->buffer + 6);
1773         }
1774
1775         uint32_t lba = rd_read_32bit(controller->buffer + 2);
1776         
1777         if (!(drive->cdrom.ready)) {
1778           PrintError("CDROM Error: Not Ready (ATA%d/%d)\n", 
1779                      get_channel_no(ramdisk, channel), get_drive_no(channel, drive));
1780           rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1781           rd_raise_interrupt(dev, channel);
1782           break;
1783         }
1784         
1785         if (transfer_length == 0) {
1786           rd_atapi_cmd_nop(dev, channel);
1787           rd_raise_interrupt(dev, channel);
1788           PrintError("READ(%d) with transfer length 0, ok\n", 
1789                      (atapi_command == 0x28) ? 10 : 12);
1790           break;
1791         }
1792         
1793         if (lba + transfer_length > drive->cdrom.capacity) {
1794           PrintError("CDROM Error: Capacity exceeded [capacity=%d] (ATA%d/%d)\n",
1795                      drive->cdrom.capacity,
1796                      get_channel_no(ramdisk, channel), get_drive_no(channel, drive));
1797           rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
1798           rd_raise_interrupt(dev, channel);
1799           break;
1800         }
1801         
1802         PrintDebug("\t\tcdrom: READ (%d) LBA=%d LEN=%d\n", 
1803                    (atapi_command == 0x28) ? 10 : 12, 
1804                    lba, transfer_length);
1805         
1806         // handle command
1807         if (rd_init_send_atapi_command(dev, channel, atapi_command, transfer_length * 2048,
1808                                        transfer_length * 2048, true) == -1) {
1809           PrintError("CDROM Error: Atapi command send error\n");
1810           return -1;
1811         }
1812
1813         drive->cdrom.remaining_blocks = transfer_length;
1814         drive->cdrom.next_lba = lba;
1815         rd_ready_to_send_atapi(dev, channel);
1816         break;
1817       }
1818     case 0x2b:  // seek
1819       {
1820         uint32_t lba = rd_read_32bit(controller->buffer + 2);
1821
1822         if (!(drive->cdrom.ready)) {
1823           rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1824           rd_raise_interrupt(dev, channel);
1825           break;
1826         }
1827         
1828         if (lba > drive->cdrom.capacity) {
1829           rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
1830           rd_raise_interrupt(dev, channel);
1831           break;
1832         }
1833         
1834         PrintDebug("\t\tcdrom: SEEK (ignored)\n");
1835
1836         rd_atapi_cmd_nop(dev, channel);
1837         rd_raise_interrupt(dev, channel);
1838
1839         break;
1840       }
1841     case 0x1e:  // prevent/allow medium removal
1842       {
1843
1844         if (drive->cdrom.ready) {
1845           drive->cdrom.locked = controller->buffer[4] & 1;
1846           rd_atapi_cmd_nop(dev, channel);
1847         } else {
1848           rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1849         }
1850
1851         rd_raise_interrupt(dev, channel);
1852
1853         break;
1854       }
1855     case 0x42:  // read sub-channel
1856       {
1857         //bool msf = get_packet_field(channel,1, 1, 1);
1858         bool sub_q = get_packet_field(channel,2, 6, 1);
1859         //uint8_t data_format = get_packet_byte(channel,3);
1860         //uint8_t track_number = get_packet_byte(channel,6);
1861         uint16_t alloc_length = get_packet_word(channel,7);
1862         
1863
1864         /*
1865           UNUSED(msf);
1866           UNUSED(data_format);
1867           UNUSED(track_number);
1868         */
1869         if (!(drive->cdrom.ready)) {
1870           rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1871           rd_raise_interrupt(dev, channel);
1872         } else {
1873           controller->buffer[0] = 0;
1874           controller->buffer[1] = 0; // audio not supported
1875           controller->buffer[2] = 0;
1876           controller->buffer[3] = 0;
1877           
1878           int ret_len = 4; // header size
1879           
1880           if (sub_q) { // !sub_q == header only
1881             PrintError("Read sub-channel with SubQ not implemented\n");
1882             rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1883                                ASC_INV_FIELD_IN_CMD_PACKET);
1884             rd_raise_interrupt(dev, channel);
1885           }
1886           
1887           if (rd_init_send_atapi_command(dev, channel, atapi_command, ret_len, alloc_length, false) == -1) {
1888             return -1;
1889           }
1890           rd_ready_to_send_atapi(dev, channel);
1891         }
1892         break;
1893       }
1894     case 0x51:  // read disc info
1895       {
1896         // no-op to keep the Linux CD-ROM driver happy
1897         rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1898         rd_raise_interrupt(dev, channel);
1899         break;
1900       }
1901     case 0x55: // mode select
1902     case 0xa6: // load/unload cd
1903     case 0x4b: // pause/resume
1904     case 0x45: // play audio
1905     case 0x47: // play audio msf
1906     case 0xbc: // play cd
1907     case 0xb9: // read cd msf
1908     case 0x44: // read header
1909     case 0xba: // scan
1910     case 0xbb: // set cd speed
1911     case 0x4e: // stop play/scan
1912     case 0x46: // ???
1913     case 0x4a: // ???
1914       PrintError("ATAPI command 0x%x not implemented yet\n",
1915                atapi_command);
1916       rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1917       rd_raise_interrupt(dev, channel);
1918       break;
1919     default:
1920       PrintError("Unknown ATAPI command 0x%x (%d)\n",
1921                  atapi_command, atapi_command);
1922       // We'd better signal the error if the user chose to continue
1923       rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1924       rd_raise_interrupt(dev, channel);
1925       break;
1926     }
1927   }
1928        
1929               
1930   return 0;
1931 }
1932
1933
1934
1935
1936 int rd_init_send_atapi_command(struct vm_device * dev, struct channel_t * channel, Bit8u command, int req_length, int alloc_length, bool lazy)
1937 {
1938   struct drive_t * drive = &(channel->drives[channel->drive_select]);
1939   struct controller_t * controller = &(drive->controller);
1940
1941   // controller->byte_count is a union of controller->cylinder_no;
1942   // lazy is used to force a data read in the buffer at the next read.
1943   
1944   PrintDebug("[rd_init_send_atapi_cmd]\n");
1945
1946   if (controller->byte_count == 0xffff) {
1947     controller->byte_count = 0xfffe;
1948   }
1949
1950   if ((controller->byte_count & 1) && 
1951       !(alloc_length <= controller->byte_count)) {
1952       
1953     PrintDebug("\t\tOdd byte count (0x%04x) to ATAPI command 0x%02x, using 0x%x\n", 
1954                controller->byte_count, 
1955                command, 
1956                controller->byte_count - 1);
1957     
1958     controller->byte_count -= 1;
1959   }
1960   
1961   if (controller->byte_count == 0) {
1962     PrintError("\t\tATAPI command with zero byte count\n");
1963     return -1;
1964   }
1965
1966   if (alloc_length < 0) {
1967     PrintError("\t\tAllocation length < 0\n");
1968     return -1;
1969   }
1970
1971   if (alloc_length == 0) {
1972     alloc_length = controller->byte_count;
1973   }
1974   
1975   controller->interrupt_reason.i_o = 1;
1976   controller->interrupt_reason.c_d = 0;
1977   controller->status.busy = 0;
1978   controller->status.drq = 1;
1979   controller->status.err = 0;
1980   
1981   // no bytes transfered yet
1982   if (lazy) {
1983     controller->buffer_index = 2048;
1984   } else {
1985     controller->buffer_index = 0;
1986   }
1987
1988   controller->drq_index = 0;
1989   
1990   if (controller->byte_count > req_length) {
1991     controller->byte_count = req_length;
1992   }
1993
1994   if (controller->byte_count > alloc_length) {
1995     controller->byte_count = alloc_length;
1996   }  
1997
1998   drive->atapi.command = command;
1999   drive->atapi.drq_bytes = controller->byte_count;
2000   drive->atapi.total_bytes_remaining = (req_length < alloc_length) ? req_length : alloc_length;
2001   
2002   // if (lazy) {
2003   // // bias drq_bytes and total_bytes_remaining
2004   // SELECTED_DRIVE(channel).atapi.drq_bytes += 2048;
2005   // SELECTED_DRIVE(channel).atapi.total_bytes_remaining += 2048;
2006   // }
2007
2008   return 0;
2009 }
2010
2011
2012
2013 void rd_ready_to_send_atapi(struct vm_device * dev, struct channel_t * channel) {
2014   PrintDebug("[rd_ready_to_send_atapi]\n");
2015   
2016   rd_raise_interrupt(dev, channel);
2017 }
2018
2019
2020
2021
2022
2023 void rd_atapi_cmd_error(struct vm_device * dev, struct channel_t * channel, sense_t sense_key, asc_t asc)
2024 {
2025   struct drive_t * drive = &(channel->drives[channel->drive_select]);
2026   struct controller_t * controller = &(drive->controller);
2027
2028 #ifdef DEBUG_RAMDISK
2029   {
2030     struct ramdisk_t *ramdisk = (struct ramdisk_t *)(dev->private_data);
2031     PrintDebug("[rd_atapi_cmd_error]\n");
2032     PrintDebug("Error: atapi_cmd_error channel=%02x key=%02x asc=%02x\n", 
2033                get_channel_no(ramdisk, channel), sense_key, asc);
2034   }
2035 #endif
2036
2037   controller->error_register = sense_key << 4;
2038   controller->interrupt_reason.i_o = 1;
2039   controller->interrupt_reason.c_d = 1;
2040   controller->interrupt_reason.rel = 0;
2041   controller->status.busy = 0;
2042   controller->status.drive_ready = 1;
2043   controller->status.write_fault = 0;
2044   controller->status.drq = 0;
2045   controller->status.err = 1;
2046   
2047   drive->sense.sense_key = sense_key;
2048   drive->sense.asc = asc;
2049   drive->sense.ascq = 0;
2050 }
2051
2052
2053
2054 void rd_atapi_cmd_nop(struct vm_device * dev, struct channel_t * channel)
2055 {
2056   struct drive_t * drive = &(channel->drives[channel->drive_select]);
2057   struct controller_t * controller = &(drive->controller);
2058
2059   PrintDebug("[rd_atapi_cmd_nop]\n");
2060   controller->interrupt_reason.i_o = 1;
2061   controller->interrupt_reason.c_d = 1;
2062   controller->interrupt_reason.rel = 0;
2063   controller->status.busy = 0;
2064   controller->status.drive_ready = 1;
2065   controller->status.drq = 0;
2066   controller->status.err = 0;
2067 }
2068
2069
2070
2071
2072 void rd_identify_ATAPI_drive(struct vm_device * dev, struct channel_t * channel)
2073 {
2074   struct drive_t * drive = &(channel->drives[channel->drive_select]);
2075   struct controller_t * controller = &(drive->controller);
2076
2077
2078   uint_t i;
2079   const char* serial_number = " VT00001\0\0\0\0\0\0\0\0\0\0\0\0";
2080   const char* firmware = "ALPHA1  ";
2081
2082   drive->id_drive[0] = (2 << 14) | (5 << 8) | (1 << 7) | (2 << 5) | (0 << 0); // Removable CDROM, 50us response, 12 byte packets
2083
2084   for (i = 1; i <= 9; i++) {
2085     drive->id_drive[i] = 0;
2086   }
2087
2088   for (i = 0; i < 10; i++) {
2089     drive->id_drive[10 + i] = ((serial_number[i * 2] << 8) |
2090                                (serial_number[(i * 2) + 1]));
2091   }
2092
2093   for (i = 20; i <= 22; i++) {
2094     drive->id_drive[i] = 0;
2095   }
2096
2097   for (i = 0; i < strlen(firmware)/2; i++) {
2098     drive->id_drive[23 + i] = ((firmware[i * 2] << 8) |
2099                                (firmware[(i * 2) + 1]));
2100   }
2101   V3_ASSERT((23 + i) == 27);
2102   
2103   for (i = 0; i < strlen((char *)(drive->model_no)) / 2; i++) {
2104     drive->id_drive[27 + i] = ((drive->model_no[i * 2] << 8) |
2105                                (drive->model_no[(i * 2) + 1]));
2106   }
2107   V3_ASSERT((27 + i) == 47);
2108
2109   drive->id_drive[47] = 0;
2110   drive->id_drive[48] = 1; // 32 bits access
2111
2112   drive->id_drive[49] = (1 << 9); // LBA supported
2113
2114   drive->id_drive[50] = 0;
2115   drive->id_drive[51] = 0;
2116   drive->id_drive[52] = 0;
2117
2118   drive->id_drive[53] = 3; // words 64-70, 54-58 valid
2119
2120   for (i = 54; i <= 62; i++) {
2121     drive->id_drive[i] = 0;
2122   }
2123
2124   // copied from CFA540A
2125   drive->id_drive[63] = 0x0103; // variable (DMA stuff)
2126   drive->id_drive[64] = 0x0001; // PIO
2127   drive->id_drive[65] = 0x00b4;
2128   drive->id_drive[66] = 0x00b4;
2129   drive->id_drive[67] = 0x012c;
2130   drive->id_drive[68] = 0x00b4;
2131
2132   drive->id_drive[69] = 0;
2133   drive->id_drive[70] = 0;
2134   drive->id_drive[71] = 30; // faked
2135   drive->id_drive[72] = 30; // faked
2136   drive->id_drive[73] = 0;
2137   drive->id_drive[74] = 0;
2138
2139   drive->id_drive[75] = 0;
2140
2141   for (i = 76; i <= 79; i++) {
2142     drive->id_drive[i] = 0;
2143   }
2144
2145   drive->id_drive[80] = 0x1e; // supports up to ATA/ATAPI-4
2146   drive->id_drive[81] = 0;
2147   drive->id_drive[82] = 0;
2148   drive->id_drive[83] = 0;
2149   drive->id_drive[84] = 0;
2150   drive->id_drive[85] = 0;
2151   drive->id_drive[86] = 0;
2152   drive->id_drive[87] = 0;
2153   drive->id_drive[88] = 0;
2154
2155   for (i = 89; i <= 126; i++) {
2156     drive->id_drive[i] = 0;
2157   }
2158
2159   drive->id_drive[127] = 0;
2160   drive->id_drive[128] = 0;
2161
2162   for (i = 129; i <= 159; i++) {
2163     drive->id_drive[i] = 0;
2164   }
2165
2166   for (i = 160; i <= 255; i++) {
2167     drive->id_drive[i] = 0;
2168   }
2169
2170   // now convert the id_drive array (native 256 word format) to
2171   // the controller buffer (512 bytes)
2172   Bit16u temp16;
2173   for (i = 0; i <= 255; i++) {
2174     temp16 = drive->id_drive[i];
2175     controller->buffer[i * 2] = temp16 & 0x00ff;
2176     controller->buffer[i * 2 + 1] = temp16 >> 8;
2177   }
2178
2179   return;
2180 }
2181
2182
2183
2184
2185
2186
2187
2188 static 
2189 void rd_init_mode_sense_single(struct vm_device * dev, 
2190                                struct channel_t * channel, const void* src, int size)
2191 {
2192   struct drive_t * drive = &(channel->drives[channel->drive_select]);
2193   struct controller_t * controller = &(drive->controller);
2194
2195   PrintDebug("[rd_init_mode_sense_single]\n");
2196
2197   // Header
2198   controller->buffer[0] = (size + 6) >> 8;
2199   controller->buffer[1] = (size + 6) & 0xff;
2200   controller->buffer[2] = 0x70; // no media present
2201   controller->buffer[3] = 0; // reserved
2202   controller->buffer[4] = 0; // reserved
2203   controller->buffer[5] = 0; // reserved
2204   controller->buffer[6] = 0; // reserved
2205   controller->buffer[7] = 0; // reserved
2206   
2207   // Data
2208   memcpy(controller->buffer + 8, src, size);
2209 }
2210
2211
2212
2213 static void rd_command_aborted(struct vm_device * dev, 
2214                                struct channel_t * channel, unsigned value) {
2215   struct drive_t * drive = &(channel->drives[channel->drive_select]);
2216   struct controller_t * controller = &(drive->controller);
2217
2218   PrintDebug("[rd_command_aborted]\n");
2219   PrintDebug("\t\taborting on command 0x%02x {%s}\n", value, device_type_to_str(drive->device_type));
2220
2221   controller->current_command = 0;
2222   controller->status.busy = 0;
2223   controller->status.drive_ready = 1;
2224   controller->status.err = 1;
2225   controller->error_register = 0x04; // command ABORTED
2226   controller->status.drq = 0;
2227   controller->status.seek_complete = 0;
2228   controller->status.corrected_data = 0;
2229   controller->buffer_index = 0;
2230
2231   rd_raise_interrupt(dev, channel);
2232 }
2233
2234
2235 static int ramdisk_init_device(struct vm_device *dev) {
2236   struct ramdisk_t *ramdisk= (struct ramdisk_t *)dev->private_data;
2237
2238   PrintDebug("Initializing Ramdisk\n");
2239
2240
2241   rd_init_hardware(ramdisk);
2242
2243
2244   dev_hook_io(dev, PRI_CTRL_PORT, 
2245               &read_status_port, &write_ctrl_port);
2246
2247   dev_hook_io(dev, PRI_DATA_PORT, 
2248               &read_data_port, &write_data_port);
2249   dev_hook_io(dev, PRI_FEATURES_PORT, 
2250               &read_general_port, &write_general_port);
2251   dev_hook_io(dev, PRI_SECT_CNT_PORT, 
2252               &read_general_port, &write_general_port);
2253   dev_hook_io(dev, PRI_SECT_ADDR1_PORT, 
2254               &read_general_port, &write_general_port);
2255   dev_hook_io(dev, PRI_SECT_ADDR2_PORT, 
2256               &read_general_port, &write_general_port);
2257   dev_hook_io(dev, PRI_SECT_ADDR3_PORT, 
2258               &read_general_port, &write_general_port);
2259   dev_hook_io(dev, PRI_DRV_SEL_PORT, 
2260               &read_general_port, &write_general_port);
2261   dev_hook_io(dev, PRI_CMD_PORT, 
2262               &read_status_port, &write_cmd_port);
2263
2264
2265   dev_hook_io(dev, SEC_CTRL_PORT, 
2266               &read_status_port, &write_ctrl_port);
2267
2268   dev_hook_io(dev, SEC_DATA_PORT, 
2269               &read_data_port, &write_data_port);
2270   dev_hook_io(dev, SEC_FEATURES_PORT, 
2271               &read_general_port, &write_general_port);
2272   dev_hook_io(dev, SEC_SECT_CNT_PORT, 
2273               &read_general_port, &write_general_port);
2274   dev_hook_io(dev, SEC_SECT_ADDR1_PORT, 
2275               &read_general_port, &write_general_port);
2276   dev_hook_io(dev, SEC_SECT_ADDR2_PORT, 
2277               &read_general_port, &write_general_port);
2278   dev_hook_io(dev, SEC_SECT_ADDR3_PORT, 
2279               &read_general_port, &write_general_port);
2280   dev_hook_io(dev, SEC_DRV_SEL_PORT, 
2281               &read_general_port, &write_general_port);
2282   dev_hook_io(dev, SEC_CMD_PORT, 
2283               &read_status_port, &write_cmd_port);
2284
2285   
2286
2287   dev_hook_io(dev, SEC_ADDR_REG_PORT, 
2288               &read_general_port, &write_general_port);
2289
2290   dev_hook_io(dev, PRI_ADDR_REG_PORT, 
2291               &read_general_port, &write_general_port);
2292
2293
2294
2295   return 0;
2296
2297 }
2298
2299
2300 static int ramdisk_deinit_device(struct vm_device *dev) {
2301   struct ramdisk_t *ramdisk = (struct ramdisk_t *)(dev->private_data);
2302   rd_close_harddrive(ramdisk);
2303   return 0;
2304 }
2305
2306 static struct vm_device_ops dev_ops = {
2307   .init = ramdisk_init_device,
2308   .deinit = ramdisk_deinit_device,
2309   .reset = NULL,
2310   .start = NULL,
2311   .stop = NULL,
2312 };
2313
2314
2315
2316
2317 struct vm_device *create_ramdisk()
2318 {
2319
2320   struct ramdisk_t *ramdisk;
2321   ramdisk = (struct ramdisk_t *)V3_Malloc(sizeof(struct ramdisk_t));  
2322   V3_ASSERT(ramdisk != NULL);  
2323
2324   PrintDebug("[create_ramdisk]\n");
2325
2326   struct vm_device *device = create_device("RAMDISK", &dev_ops, ramdisk);
2327
2328   return device;
2329 }
2330
2331
2332
2333
2334 #ifdef DEBUG_RAMDISK
2335
2336 static void rd_print_state(struct ramdisk_t * ramdisk) {
2337   uchar_t channel; 
2338   uchar_t device;
2339   struct channel_t * channels = (struct channel_t *)(&(ramdisk->channels));
2340
2341   /*
2342   for (channel = 0; channel < MAX_ATA_CHANNEL; channel++) {
2343     memset((char *)(channels + channel), 0, sizeof(struct channel_t));
2344   }
2345   */
2346   PrintDebug("sizeof(*channels) = %d\n", sizeof(*channels));
2347   PrintDebug("sizeof(channles->drives[0].controller) = %d\n", sizeof((channels->drives[0].controller)));
2348   PrintDebug("sizeof(channles->drives[0].cdrom) = %d\n", sizeof((channels->drives[0].cdrom)));
2349   PrintDebug("sizeof(channles->drives[0].sense) = %d\n", sizeof((channels->drives[0].sense)));
2350   PrintDebug("sizeof(channles->drives[0].atapi) = %d\n", sizeof((channels->drives[0].atapi)));
2351
2352
2353   PrintDebug("sizeof(channles->drives[0].controller.status) = %d\n", 
2354                 sizeof((channels->drives[0].controller.status)));
2355   PrintDebug("sizeof(channles->drives[0].controller.sector_count) = %d\n", 
2356                 sizeof((channels->drives[0].controller.sector_count)));
2357   PrintDebug("sizeof(channles->drives[0].controller.interrupt_reason) = %d\n", 
2358                 sizeof((channels->drives[0].controller.interrupt_reason)));
2359
2360   PrintDebug("sizeof(channles->drives[0].controller.cylinder_no) = %d\n", 
2361                 sizeof((channels->drives[0].controller.cylinder_no)));
2362   PrintDebug("sizeof(channles->drives[0].controller.byte_count) = %d\n", 
2363                 sizeof((channels->drives[0].controller.byte_count)));
2364
2365
2366   PrintDebug("sizeof(channles->drives[0].controller.control) = %d\n", 
2367                 sizeof((channels->drives[0].controller.control)));
2368
2369
2370   for (channel = 0; channel < MAX_ATA_CHANNEL; channel++){
2371   
2372     for (device = 0; device < 2; device++){
2373                   
2374       // Initialize controller state, even if device is not present
2375       PrintDebug("channels[%d].drives[%d].controller.status.busy = %d\n",
2376                     channel, device, 
2377                     channels[channel].drives[device].controller.status.busy);
2378       PrintDebug("channels[%d].drives[%d].controller.status.drive_ready = %d\n", 
2379                     channel, device, 
2380                     channels[channel].drives[device].controller.status.drive_ready);
2381       PrintDebug("channels[%d].drives[%d].controller.status.write_fault = %d\n", 
2382                     channel, device, 
2383                     channels[channel].drives[device].controller.status.write_fault);
2384       PrintDebug("channels[%d].drives[%d].controller.status.seek_complete = %d\n", 
2385                     channel, device, 
2386                     channels[channel].drives[device].controller.status.seek_complete);
2387       PrintDebug("channels[%d].drives[%d].controller.status.drq = %d\n", 
2388                     channel, device, 
2389                     channels[channel].drives[device].controller.status.drq);
2390       PrintDebug("channels[%d].drives[%d].controller.status.corrected_data = %d\n", 
2391                     channel, device, 
2392                     channels[channel].drives[device].controller.status.corrected_data);
2393       PrintDebug("channels[%d].drives[%d].controller.status.index_pulse = %d\n", 
2394                     channel, device, 
2395                     channels[channel].drives[device].controller.status.index_pulse);
2396       PrintDebug("channels[%d].drives[%d].controller.status.index_pulse_count = %d\n", 
2397                     channel, device, 
2398                     channels[channel].drives[device].controller.status.index_pulse_count);
2399       PrintDebug("channels[%d].drives[%d].controller.status.err = %d\n", 
2400                     channel, device, 
2401                     channels[channel].drives[device].controller.status.err);
2402
2403
2404       PrintDebug("channels[%d].drives[%d].controller.error_register = %d\n", 
2405                     channel, device, 
2406                     channels[channel].drives[device].controller.error_register);
2407       PrintDebug("channels[%d].drives[%d].controller.head_no = %d\n", 
2408                     channel, device, 
2409                     channels[channel].drives[device].controller.head_no);
2410       PrintDebug("channels[%d].drives[%d].controller.sector_count = %d\n", 
2411                     channel, device, 
2412                     channels[channel].drives[device].controller.sector_count);
2413       PrintDebug("channels[%d].drives[%d].controller.sector_no = %d\n", 
2414                     channel, device, 
2415                     channels[channel].drives[device].controller.sector_no);
2416       PrintDebug("channels[%d].drives[%d].controller.cylinder_no = %d\n", 
2417                     channel, device, 
2418                     channels[channel].drives[device].controller.cylinder_no);
2419       PrintDebug("channels[%d].drives[%d].controller.current_command = %02x\n", 
2420                     channel, device, 
2421                     channels[channel].drives[device].controller.current_command);
2422       PrintDebug("channels[%d].drives[%d].controller.buffer_index = %d\n", 
2423                     channel, device, 
2424                     channels[channel].drives[device].controller.buffer_index);
2425
2426
2427       PrintDebug("channels[%d].drives[%d].controller.control.reset = %d\n", 
2428                     channel, device, 
2429                     channels[channel].drives[device].controller.control.reset);
2430       PrintDebug("channels[%d].drives[%d].controller.control.disable_irq = %d\n", 
2431                     channel, device, 
2432                     channels[channel].drives[device].controller.control.disable_irq);
2433
2434
2435       PrintDebug("channels[%d].drives[%d].controller.reset_in_progress = %d\n", 
2436                     channel, device, 
2437                     channels[channel].drives[device].controller.reset_in_progress);
2438       PrintDebug("channels[%d].drives[%d].controller.sectors_per_block = %02x\n", 
2439                     channel, device, 
2440                     channels[channel].drives[device].controller.sectors_per_block); 
2441       PrintDebug("channels[%d].drives[%d].controller.lba_mode = %d\n", 
2442                     channel, device, 
2443                     channels[channel].drives[device].controller.lba_mode); 
2444       PrintDebug("channels[%d].drives[%d].controller.features = %d\n", 
2445                     channel, device, 
2446                     channels[channel].drives[device].controller.features); 
2447
2448
2449       PrintDebug("channels[%d].drives[%d].model_no = %s\n", 
2450                     channel, device, 
2451                     channels[channel].drives[device].model_no); 
2452       PrintDebug("channels[%d].drives[%d].device_type = %d\n", 
2453                     channel, device, 
2454                     channels[channel].drives[device].device_type); 
2455       PrintDebug("channels[%d].drives[%d].cdrom.locked = %d\n", 
2456                     channel, device, 
2457                     channels[channel].drives[device].cdrom.locked); 
2458       PrintDebug("channels[%d].drives[%d].sense.sense_key = %d\n", 
2459                     channel, device, 
2460                     channels[channel].drives[device].sense.sense_key); 
2461       PrintDebug("channels[%d].drives[%d].sense.asc = %d\n", 
2462                     channel, device, 
2463                     channels[channel].drives[device].sense.asc); 
2464       PrintDebug("channels[%d].drives[%d].sense.ascq = %d\n", 
2465                     channel, device, 
2466                     channels[channel].drives[device].sense.ascq); 
2467
2468
2469
2470       PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.c_d = %02x\n", 
2471                     channel, device, 
2472                     channels[channel].drives[device].controller.interrupt_reason.c_d);
2473
2474       PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.i_o = %02x\n", 
2475                     channel, device, 
2476                     channels[channel].drives[device].controller.interrupt_reason.i_o);
2477
2478       PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.rel = %02x\n", 
2479                     channel, device, 
2480                     channels[channel].drives[device].controller.interrupt_reason.rel);
2481
2482       PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.tag = %02x\n", 
2483                     channel, device, 
2484                     channels[channel].drives[device].controller.interrupt_reason.tag);
2485
2486       PrintDebug("channels[%d].drives[%d].cdrom.ready = %d\n", 
2487                     channel, device, 
2488                     channels[channel].drives[device].cdrom.ready);
2489       
2490     }  //for device
2491   }  //for channel
2492   
2493   return;
2494 }
2495
2496
2497
2498 static int check_bit_fields(struct controller_t * controller) {
2499   //Check bit fields
2500   controller->sector_count = 0;
2501   controller->interrupt_reason.c_d = 1;
2502   if (controller->sector_count != 0x01) {
2503     return INTR_REASON_BIT_ERR;
2504   }
2505   
2506   controller->sector_count = 0;
2507   controller->interrupt_reason.i_o = 1;
2508   if (controller->sector_count != 0x02) {
2509     return INTR_REASON_BIT_ERR;
2510   }
2511   
2512   controller->sector_count = 0;
2513   controller->interrupt_reason.rel = 1;
2514   if (controller->sector_count != 0x04) {
2515     return INTR_REASON_BIT_ERR;
2516   }
2517   
2518   controller->sector_count = 0;
2519   controller->interrupt_reason.tag = 3;
2520   if (controller->sector_count != 0x18) {
2521     return INTR_REASON_BIT_ERR;
2522   }
2523   
2524   return 0;
2525 }
2526 #endif