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.


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