2 * IDE Emulation Layer for retro-style PIO interfaces
4 * (c) Copyright Alan Cox, 2015-2019
6 * IDE-emu is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * IDE-emu is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with IDE-emu. If not, see <http://www.gnu.org/licenses/>.
27 #include <arpa/inet.h>
34 #define IDE_DATA_OUT 3
63 #define IDE_CMD_CALIB 0x10
64 #define IDE_CMD_READ 0x20
65 #define IDE_CMD_READ_NR 0x21
66 #define IDE_CMD_WRITE 0x30
67 #define IDE_CMD_WRITE_NR 0x31
68 #define IDE_CMD_VERIFY 0x40
69 #define IDE_CMD_VERIFY_NR 0x41
70 #define IDE_CMD_SEEK 0x70
71 #define IDE_CMD_EDD 0x90
72 #define IDE_CMD_INTPARAMS 0x91
73 #define IDE_CMD_IDENTIFY 0xEC
74 #define IDE_CMD_SETFEATURES 0xEF
76 const uint8_t ide_magic[8] = {
77 '1','D','E','D','1','5','C','0'
80 static char *charmap(uint8_t v)
84 sprintf(cbuf, "^%c", '@'+v);
86 sprintf(cbuf, " %c", v);
90 sprintf(cbuf, ":%c", '@' + v - 128);
92 sprintf(cbuf, "~%c", v - 128);
98 static void hexdump(uint8_t *bp)
101 for (i = 0; i < 512; i+= 16) {
102 for(j = 0; j < 16; j++)
103 fprintf(stderr, "%02X ", bp[i+j]);
104 fprintf(stderr, "|");
105 for(j = 0; j < 16; j++)
106 fprintf(stderr, "%2s", charmap(bp[i+j]));
107 fprintf(stderr, "\n");
111 /* FIXME: use proper endian convertors! */
112 static uint16_t le16(uint16_t v)
114 uint8_t *p = (uint8_t *)&v;
115 return p[0] | (p[1] << 8);
118 static void ide_xlate_errno(struct ide_taskfile *t, int len)
130 static void ide_fault(struct ide_drive *d, const char *p)
132 fprintf(stderr, "ide: %s: %d: %s\n", d->controller->name,
133 (int)(d - d->controller->drive), p);
136 /* Disk translation */
137 static off_t xlate_block(struct ide_taskfile *t)
139 struct ide_drive *d = t->drive;
142 if (t->lba4 & DEVH_LBA) {
143 /* fprintf(stderr, "XLATE LBA %02X:%02X:%02X:%02X\n",
144 t->lba4, t->lba3, t->lba2, t->lba1);*/
146 return 2 + (((t->lba4 & DEVH_HEAD) << 24) | (t->lba3 << 16) | (t->lba2 << 8) | t->lba1);
147 ide_fault(d, "LBA on non LBA drive");
150 /* Some well known software asks for 0/0/0 when it means 0/0/1. Drives appear
151 to interpret sector 0 as sector 1 */
153 fprintf(stderr, "[Bug: request for sector offset 0].\n");
156 cyl = (t->lba3 << 8) | t->lba2;
157 /* fprintf(stderr, "(H %d C %d S %d)\n", t->lba4 & DEVH_HEAD, cyl, t->lba1); */
158 if (t->lba1 == 0 || t->lba1 > d->sectors || t->lba4 >= d->heads || cyl >= d->cylinders) {
161 /* Sector 1 is first */
162 /* Images generally go cylinder/head/sector. This also matters if we ever
163 implement more advanced geometry setting */
164 return 1 + ((cyl * d->heads) + (t->lba4 & DEVH_HEAD)) * d->sectors + t->lba1;
167 /* Indicate the drive is ready */
168 static void ready(struct ide_taskfile *tf)
170 tf->status &= ~(ST_BSY|ST_DRQ);
171 tf->status |= ST_DRDY;
172 tf->drive->state = IDE_IDLE;
175 /* Return to idle state, completing a command */
176 static void completed(struct ide_taskfile *tf)
179 tf->drive->intrq = 1;
182 static void drive_failed(struct ide_taskfile *tf)
184 tf->status |= ST_ERR;
185 tf->error = ERR_IDNF;
189 static void data_in_state(struct ide_taskfile *tf)
191 struct ide_drive *d = tf->drive;
192 d->state = IDE_DATA_IN;
193 d->dptr = d->data + 512;
194 /* We don't clear DRDY here, drives may well accept a command at this
195 point and at least one firmware for RC2014 assumes this */
196 tf->status &= ~ST_BSY;
197 tf->status |= ST_DRQ;
198 d->intrq = 1; /* Double check */
201 static void data_out_state(struct ide_taskfile *tf)
203 struct ide_drive *d = tf->drive;
204 d->state = IDE_DATA_OUT;
206 tf->status &= ~ (ST_BSY|ST_DRDY);
207 tf->status |= ST_DRQ;
208 d->intrq = 1; /* Double check */
211 static void edd_setup(struct ide_taskfile *tf)
213 tf->error = 0x01; /* All good */
214 tf->lba1 = 0x01; /* EDD always updates drive 0 */
222 void ide_reset(struct ide_controller *c)
224 if (c->drive[0].present) {
225 edd_setup(&c->drive[0].taskfile);
226 /* A drive could clear busy then set DRDY up to 2 minutes later if its
227 mindnumbingly slow to start up ! We don't emulate any of that */
228 c->drive[0].taskfile.status = ST_DRDY;
229 c->drive[0].eightbit = 0;
231 if (c->drive[1].present) {
232 edd_setup(&c->drive[1].taskfile);
233 c->drive[1].taskfile.status = ST_DRDY;
234 c->drive[1].eightbit = 0;
239 void ide_reset_begin(struct ide_controller *c)
241 if (c->drive[0].present)
242 c->drive[0].taskfile.status |= ST_BSY;
243 if (c->drive[1].present)
244 c->drive[1].taskfile.status |= ST_BSY;
245 /* Ought to be a time delay relative to reset or power on */
249 static void ide_srst_begin(struct ide_controller *c)
252 if (c->drive[0].present)
253 c->drive[0].taskfile.status |= ST_BSY;
254 if (c->drive[1].present)
255 c->drive[1].taskfile.status |= ST_BSY;
258 static void ide_srst_end(struct ide_controller *c)
260 /* Could be time delays here */
261 ready(&c->drive[0].taskfile);
262 ready(&c->drive[1].taskfile);
265 static void cmd_edd_complete(struct ide_taskfile *tf)
267 struct ide_controller *c = tf->drive->controller;
268 if (c->drive[0].present)
269 edd_setup(&c->drive[0].taskfile);
270 if (c->drive[1].present)
271 edd_setup(&c->drive[1].taskfile);
275 static void cmd_identify_complete(struct ide_taskfile *tf)
277 struct ide_drive *d = tf->drive;
278 memcpy(d->data, d->identify, 512);
280 /* Arrange to copy just the identify buffer */
285 static void cmd_initparam_complete(struct ide_taskfile *tf)
287 struct ide_drive *d = tf->drive;
288 /* We only support the current mapping */
289 if (tf->count != d->sectors || (tf->lba4 & DEVH_HEAD) + 1 != d->heads) {
290 tf->status |= ST_ERR;
291 tf->error |= ERR_ABRT;
292 tf->drive->failed = 1; /* Report ID NF until fixed */
293 /* fprintf(stderr, "geo is %d %d, asked for %d %d\n",
294 d->sectors, d->heads, tf->count, (tf->lba4 & DEVH_HEAD) + 1); */
295 ide_fault(d, "invalid geometry");
296 } else if (tf->drive->failed == 1)
297 tf->drive->failed = 0; /* Valid translation */
301 static void cmd_readsectors_complete(struct ide_taskfile *tf)
303 struct ide_drive *d = tf->drive;
304 /* Move to data xfer */
309 d->offset = xlate_block(tf);
310 /* DRDY is not guaranteed here but at least one buggy RC2014 firmware
312 tf->status |= ST_DRQ | ST_DSC | ST_DRDY;
313 tf->status &= ~ST_BSY;
314 /* 0 = 256 sectors */
315 d->length = tf->count ? tf->count : 256;
316 /* fprintf(stderr, "READ %d SECTORS @ %ld\n", d->length, d->offset); */
317 if (d->offset == -1 || lseek(d->fd, 512 * d->offset, SEEK_SET) == -1) {
318 tf->status |= ST_ERR;
319 tf->status &= ~ST_DSC;
320 tf->error |= ERR_IDNF;
321 /* return null data */
329 static void cmd_verifysectors_complete(struct ide_taskfile *tf)
331 struct ide_drive *d = tf->drive;
332 /* Move to data xfer */
337 d->offset = xlate_block(tf);
338 /* 0 = 256 sectors */
339 d->length = tf->count ? tf->count : 256;
340 if (d->offset == -1 || lseek(d->fd, 512 * (d->offset + d->length - 1), SEEK_SET) == -1) {
341 tf->status &= ~ST_DSC;
342 tf->status |= ST_ERR;
343 tf->error |= ERR_IDNF;
345 tf->status |= ST_DSC;
349 static void cmd_recalibrate_complete(struct ide_taskfile *tf)
351 struct ide_drive *d = tf->drive;
354 if (d->offset == -1 || xlate_block(tf) != 0L) {
355 tf->status &= ~ST_DSC;
356 tf->status |= ST_ERR;
357 tf->error |= ERR_ABRT;
359 tf->status |= ST_DSC;
363 static void cmd_seek_complete(struct ide_taskfile *tf)
365 struct ide_drive *d = tf->drive;
368 d->offset = xlate_block(tf);
369 if (d->offset == -1 || lseek(d->fd, 512 * d->offset, SEEK_SET) == -1) {
370 tf->status &= ~ST_DSC;
371 tf->status |= ST_ERR;
372 tf->error |= ERR_IDNF;
374 tf->status |= ST_DSC;
378 static void cmd_setfeatures_complete(struct ide_taskfile *tf)
380 struct ide_drive *d = tf->drive;
381 switch(tf->feature) {
386 if ((tf->count & 0xF0) >= 0x20) {
387 tf->status |= ST_ERR;
388 tf->error |= ERR_ABRT;
390 /* Silently accept PIO mode settings */
396 tf->status |= ST_ERR;
397 tf->error |= ERR_ABRT;
402 static void cmd_writesectors_complete(struct ide_taskfile *tf)
404 struct ide_drive *d = tf->drive;
405 /* Move to data xfer */
410 d->offset = xlate_block(tf);
411 tf->status |= ST_DRQ;
412 /* 0 = 256 sectors */
413 d->length = tf->count ? tf->count : 256;
414 /* fprintf(stderr, "WRITE %d SECTORS @ %ld\n", d->length, d->offset); */
415 if (d->offset == -1 || lseek(d->fd, 512 * d->offset, SEEK_SET) == -1) {
416 tf->status |= ST_ERR;
417 tf->error |= ERR_IDNF;
418 tf->status &= ~ST_DSC;
419 /* return null data */
427 static void ide_set_error(struct ide_drive *d)
429 d->taskfile.lba4 &= ~DEVH_HEAD;
431 if (d->taskfile.lba4 & DEVH_LBA) {
432 d->taskfile.lba1 = d->offset & 0xFF;
433 d->taskfile.lba2 = (d->offset >> 8) & 0xFF;
434 d->taskfile.lba3 = (d->offset >> 16) & 0xFF;
435 d->taskfile.lba4 |= (d->offset >> 24) & DEVH_HEAD;
437 d->taskfile.lba1 = d->offset % d->sectors + 1;
438 d->offset /= d->sectors;
439 d->taskfile.lba4 |= d->offset / (d->cylinders * d->sectors);
440 d->offset %= (d->cylinders * d->sectors);
441 d->taskfile.lba2 = d->offset & 0xFF;
442 d->taskfile.lba3 = (d->offset >> 8) & 0xFF;
444 d->taskfile.count = d->length;
445 d->taskfile.status |= ST_ERR;
447 completed(&d->taskfile);
450 static int ide_read_sector(struct ide_drive *d)
455 if ((len = read(d->fd, d->data, 512)) != 512) {
456 perror("ide_read_sector");
457 d->taskfile.status |= ST_ERR;
458 d->taskfile.status &= ~ST_DSC;
459 ide_xlate_errno(&d->taskfile, len);
467 static int ide_write_sector(struct ide_drive *d)
472 if ((len = write(d->fd, d->data, 512)) != 512) {
473 d->taskfile.status |= ST_ERR;
474 d->taskfile.status &= ~ST_DSC;
475 ide_xlate_errno(&d->taskfile, len);
483 static uint16_t ide_data_in(struct ide_drive *d, int len)
486 if (d->state == IDE_DATA_IN) {
487 if (d->dptr == d->data + 512) {
488 if (ide_read_sector(d) < 0) {
489 ide_set_error(d); /* Set the LBA or CHS etc */
490 return 0xFFFF; /* and error bits set by read_sector */
496 v |= (d->dptr[1] << 8);
500 d->taskfile.data = v;
501 if (d->dptr == d->data + 512) {
503 d->intrq = 1; /* we don't yet emulate multimode */
504 if (d->length == 0) {
506 completed(&d->taskfile);
510 ide_fault(d, "bad data read");
513 return d->taskfile.data & 0xFF;
514 return d->taskfile.data;
517 static void ide_data_out(struct ide_drive *d, uint16_t v, int len)
519 if (d->state != IDE_DATA_OUT) {
520 ide_fault(d, "bad data write");
521 d->taskfile.data = v;
526 d->taskfile.data = v;
529 d->taskfile.data = v >> 8;
531 if (d->dptr == d->data + 512) {
532 if (ide_write_sector(d) < 0) {
538 if (d->length == 0) {
540 d->taskfile.status |= ST_DSC;
541 completed(&d->taskfile);
547 static void ide_issue_command(struct ide_taskfile *t)
549 t->status &= ~(ST_ERR|ST_DRDY);
552 t->drive->state = IDE_CMD;
554 /* We could complete with delays but don't do so yet */
556 case IDE_CMD_EDD: /* 0x90 */
559 case IDE_CMD_IDENTIFY: /* 0xEC */
560 cmd_identify_complete(t);
562 case IDE_CMD_INTPARAMS: /* 0x91 */
563 cmd_initparam_complete(t);
565 case IDE_CMD_READ: /* 0x20 */
566 case IDE_CMD_READ_NR: /* 0x21 */
567 cmd_readsectors_complete(t);
569 case IDE_CMD_SETFEATURES: /* 0xEF */
570 cmd_setfeatures_complete(t);
572 case IDE_CMD_VERIFY: /* 0x40 */
573 case IDE_CMD_VERIFY_NR: /* 0x41 */
574 cmd_verifysectors_complete(t);
576 case IDE_CMD_WRITE: /* 0x30 */
577 case IDE_CMD_WRITE_NR: /* 0x31 */
578 cmd_writesectors_complete(t);
581 if ((t->command & 0xF0) == IDE_CMD_CALIB) /* 1x */
582 cmd_recalibrate_complete(t);
583 else if ((t->command & 0xF0) == IDE_CMD_SEEK) /* 7x */
584 cmd_seek_complete(t);
588 t->error |= ERR_ABRT;
595 * 8bit IDE controller emulation
598 uint8_t ide_read8(struct ide_controller *c, uint8_t r)
600 struct ide_drive *d = &c->drive[c->selected];
601 struct ide_taskfile *t = &d->taskfile;
604 return ide_data_in(d, 1);
618 d->intrq = 0; /* Acked */
622 ide_fault(d, "bogus register");
627 void ide_write8(struct ide_controller *c, uint8_t r, uint8_t v)
629 struct ide_drive *d = &c->drive[c->selected];
630 struct ide_taskfile *t = &d->taskfile;
632 if (r != ide_devctrl_w) {
633 if (t->status & ST_BSY) {
634 ide_fault(d, "command written while busy");
637 /* Not clear this is the right emulation */
638 if (d->present == 0 && r != ide_lba_top) {
639 ide_fault(d, "not present");
646 ide_data_out(d, v, 1);
664 c->selected = (v & DEVH_DEV) ? 1 : 0;
665 c->drive[c->selected].taskfile.lba4 = v & (DEVH_HEAD|DEVH_DEV|DEVH_LBA);
669 ide_issue_command(t);
672 /* ATA: "When the Device Control register is written, both devices
673 respond to the write regardless of which device is selected" */
674 if ((v ^ t->devctrl) & DCL_SRST) {
680 c->drive[0].taskfile.devctrl = v; /* Check versus real h/w does this end up cleared */
681 c->drive[1].taskfile.devctrl = v;
687 * 16bit IDE controller emulation
690 uint16_t ide_read16(struct ide_controller *c, uint8_t r)
692 struct ide_drive *d = &c->drive[c->selected];
694 return htons(ide_data_in(d,2));
695 return ide_read8(c, r);
698 void ide_write16(struct ide_controller *c, uint8_t r, uint16_t v)
700 struct ide_drive *d = &c->drive[c->selected];
701 struct ide_taskfile *t = &d->taskfile;
703 if (r != ide_devctrl_w && (t->status & ST_BSY)) {
704 ide_fault(d, "command written while busy");
708 ide_data_out(d, ntohs(v), 2);
714 * Allocate a new IDE controller emulation
716 struct ide_controller *ide_allocate(const char *name)
718 struct ide_controller *c = calloc(1, sizeof(*c));
721 c->name = strdup(name);
722 if (c->name == NULL) {
726 c->drive[0].controller = c;
727 c->drive[1].controller = c;
728 c->drive[0].taskfile.drive = &c->drive[0];
729 c->drive[1].taskfile.drive = &c->drive[1];
734 * Attach a file to a device on the controller
736 int ide_attach(struct ide_controller *c, int drive, int fd)
738 struct ide_drive *d = &c->drive[drive];
740 ide_fault(d, "double attach");
744 if (read(d->fd, d->data, 512) != 512 ||
745 read(d->fd, d->identify, 512) != 512) {
746 ide_fault(d, "i/o error on attach");
749 if (memcmp(d->data, ide_magic, 8)) {
750 ide_fault(d, "bad magic");
755 d->heads = d->identify[3];
756 d->sectors = d->identify[6];
757 d->cylinders = le16(d->identify[1]);
758 if (d->identify[49] & le16(1 << 9))
766 * Detach an IDE device from the interface (not hot pluggable)
768 void ide_detach(struct ide_drive *d)
776 * Free up and release and IDE controller
778 void ide_free(struct ide_controller *c)
780 if (c->drive[0].present)
781 ide_detach(&c->drive[0]);
782 if (c->drive[1].present)
783 ide_detach(&c->drive[1]);
784 free((void *)c->name);
789 * Emulation interface for an 8bit controller using latches on the
792 uint8_t ide_read_latched(struct ide_controller *c, uint8_t reg)
795 if (reg == ide_data_latch)
796 return c->data_latch;
797 v = ide_read16(c, reg);
798 if (reg == ide_data) {
799 c->data_latch = v >> 8;
805 void ide_write_latched(struct ide_controller *c, uint8_t reg, uint8_t v)
809 if (reg == ide_data_latch) {
814 d |= (c->data_latch << 8);
815 ide_write16(c, reg, d);
818 static void make_ascii(uint16_t *p, const char *t, int len)
824 for (i = 0; i < len; i += 2) {
832 static void make_serial(uint16_t *p)
835 srand(getpid()^time(NULL));
836 snprintf(buf, 21, "%08d%08d%04d", rand(), rand(), rand());
837 make_ascii(p, buf, 20);
840 int ide_make_drive(uint8_t type, int fd)
847 if (type < 1 || type > MAX_DRIVE_TYPE)
850 memset(ident, 0, 512);
851 memcpy(ident, ide_magic, 8);
852 if (write(fd, ident, 512) != 512)
856 ident[0] = le16((1 << 15) | (1 << 6)); /* Non removable */
857 make_serial(ident + 10);
858 ident[47] = 0; /* no read multi for now */
859 ident[51] = le16(240 /* PIO2 */ << 8); /* PIO cycle time */
860 ident[53] = le16(1); /* Geometry words are valid */
863 case ACME_ROADRUNNER:
864 /* 504MB drive with LBA support */
868 make_ascii(ident + 23, "A001.001", 8);
869 make_ascii(ident + 27, "ACME ROADRUNNER v0.1", 40);
870 ident[49] = le16(1 << 9); /* LBA */
872 case ACME_ULTRASONICUS:
873 /* 40MB drive with LBA support */
877 ident[49] = le16(1 << 9); /* LBA */
878 make_ascii(ident + 23, "A001.001", 8);
879 make_ascii(ident + 27, "ACME ULTRASONICUS AD INFINITUM v0.1", 40);
882 /* 20MB drive with LBA support */
886 ident[49] = le16(1 << 9); /* LBA */
887 make_ascii(ident + 23, "A001.001", 8);
888 make_ascii(ident + 27, "ACME NEMESIS RIDICULII v0.1", 40);
891 /* 20MB drive without LBA support */
895 make_ascii(ident + 23, "A001.001", 8);
896 make_ascii(ident + 27, "ACME COYOTE v0.1", 40);
898 case ACME_ACCELLERATTI:
902 ident[49] = le16(1 << 9); /* LBA */
903 make_ascii(ident + 23, "A001.001", 8);
904 make_ascii(ident + 27, "ACME ACCELLERATTI INCREDIBILUS v0.1", 40);
910 ident[49] = le16(1 << 9); /* LBA */
911 make_ascii(ident + 23, "A001.001", 8);
912 make_ascii(ident + 27, "ACME ZIPPIBUS v0.1", 40);
918 ident[54] = ident[1];
919 ident[55] = ident[3];
920 ident[56] = ident[6];
922 ident[57] = le16(sectors & 0xFFFF);
923 ident[58] = le16(sectors >> 16);
924 ident[60] = ident[57];
925 ident[61] = ident[58];
926 if (write(fd, ident, 512) != 512)
929 memset(ident, 0xE5, 512);
931 if (write(fd, ident, 512) != 512)