grml...
[seabios.git] / src / disk.c
index 0cd2aca47c170911cdcd2c7bbb71a0829cc09005..f2c662193aa435b2e47c4ecb095cbe436bd4aefd 100644 (file)
@@ -23,222 +23,124 @@ void
 __disk_ret(struct bregs *regs, u32 linecode, const char *fname)
 {
     u8 code = linecode;
-    SET_BDA(disk_last_status, code);
+    if (regs->dl < EXTSTART_HD)
+        SET_BDA(floppy_last_status, code);
+    else
+        SET_BDA(disk_last_status, code);
     if (code)
-        __set_code_fail(regs, linecode, fname);
+        __set_code_invalid(regs, linecode, fname);
     else
         set_code_success(regs);
 }
 
+void
+__disk_ret_unimplemented(struct bregs *regs, u32 linecode, const char *fname)
+{
+    u8 code = linecode;
+    if (regs->dl < EXTSTART_HD)
+        SET_BDA(floppy_last_status, code);
+    else
+        SET_BDA(disk_last_status, code);
+    __set_code_unimplemented(regs, linecode, fname);
+}
+
 static void
 __disk_stub(struct bregs *regs, int lineno, const char *fname)
 {
-    __debug_stub(regs, lineno, fname);
+    __warn_unimplemented(regs, lineno, fname);
     __disk_ret(regs, DISK_RET_SUCCESS | (lineno << 8), fname);
 }
 
 #define DISK_STUB(regs)                         \
     __disk_stub((regs), __LINE__, __func__)
 
-// Execute a "disk_op_s" request - this runs on a stack in the ebda.
-static int
-__send_disk_op(struct disk_op_s *op_far, u16 op_seg)
+// Get the cylinders/heads/sectors for the given drive.
+static void
+fillLCHS(struct drive_s *drive_g, u16 *nlc, u16 *nlh, u16 *nlspt)
 {
-    struct disk_op_s dop;
-    memcpy_far(GET_SEG(SS), &dop
-               , op_seg, op_far
-               , sizeof(dop));
-
-    dprintf(DEBUG_HDL_13, "disk_op d=%d lba=%d buf=%p count=%d cmd=%d\n"
-            , dop.driveid, (u32)dop.lba, dop.buf_fl
-            , dop.count, dop.command);
-
-    irq_enable();
-
-    int status = 0;
-    u8 type = GET_GLOBAL(Drives.drives[dop.driveid].type);
-    if (type == DTYPE_ATA)
-        status = process_ata_op(&dop);
-    else if (type == DTYPE_ATAPI)
-        status = process_atapi_op(&dop);
-
-    irq_disable();
-
-    // Update count with total sectors transferred.
-    SET_FARVAR(op_seg, op_far->count, dop.count);
-
-    if (status)
-        dprintf(1, "disk_op cmd %d error %d!\n", dop.command, status);
-
-    return status;
+    if (CONFIG_CDROM_EMU
+        && drive_g == GLOBALFLAT2GLOBAL(GET_GLOBAL(cdemu_drive_gf))) {
+        // Emulated drive - get info from ebda.  (It's not possible to
+        // populate the geometry directly in the driveid because the
+        // geometry is only known after the bios segment is made
+        // read-only).
+        u16 ebda_seg = get_ebda_seg();
+        *nlc = GET_EBDA2(ebda_seg, cdemu.lchs.cylinders);
+        *nlh = GET_EBDA2(ebda_seg, cdemu.lchs.heads);
+        *nlspt = GET_EBDA2(ebda_seg, cdemu.lchs.spt);
+        return;
+    }
+    *nlc = GET_GLOBAL(drive_g->lchs.cylinders);
+    *nlh = GET_GLOBAL(drive_g->lchs.heads);
+    *nlspt = GET_GLOBAL(drive_g->lchs.spt);
 }
 
-// Execute a "disk_op_s" request by jumping to a stack in the ebda.
-static int
-send_disk_op(struct disk_op_s *op)
+// Perform read/write/verify using old-style chs accesses
+static void
+basic_access(struct bregs *regs, struct drive_s *drive_g, u16 command)
 {
-    if (! CONFIG_DRIVES)
-        return -1;
-
-    return stack_hop((u32)op, GET_SEG(SS), 0, __send_disk_op);
-}
+    struct disk_op_s dop;
+    dop.drive_g = drive_g;
+    dop.command = command;
 
-// Obtain the requested disk lba from an old-style chs request.
-static int
-legacy_lba(struct bregs *regs, u16 lchs_seg, struct chs_s *lchs_far)
-{
     u8 count = regs->al;
     u16 cylinder = regs->ch | ((((u16)regs->cl) << 2) & 0x300);
     u16 sector = regs->cl & 0x3f;
     u16 head = regs->dh;
 
     if (count > 128 || count == 0 || sector == 0) {
-        dprintf(1, "int13_harddisk: function %02x, parameter out of range!\n"
-                , regs->ah);
+        warn_invalid(regs);
         disk_ret(regs, DISK_RET_EPARAM);
-        return -1;
+        return;
     }
+    dop.count = count;
 
-    u16 nlc = GET_FARVAR(lchs_seg, lchs_far->cylinders);
-    u16 nlh = GET_FARVAR(lchs_seg, lchs_far->heads);
-    u16 nlspt = GET_FARVAR(lchs_seg, lchs_far->spt);
+    u16 nlc, nlh, nlspt;
+    fillLCHS(drive_g, &nlc, &nlh, &nlspt);
 
     // sanity check on cyl heads, sec
     if (cylinder >= nlc || head >= nlh || sector > nlspt) {
-        dprintf(1, "int13_harddisk: function %02x, parameters out of"
-                " range %04x/%04x/%04x!\n"
-                , regs->ah, cylinder, head, sector);
+        warn_invalid(regs);
         disk_ret(regs, DISK_RET_EPARAM);
-        return -1;
+        return;
     }
 
     // translate lchs to lba
-    return (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nlspt)
-            + (u32)sector - 1);
-}
+    dop.lba = (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nlspt)
+               + (u32)sector - 1);
 
-// Perform read/write/verify using old-style chs accesses
-static void
-basic_access(struct bregs *regs, u8 driveid, u16 command)
-{
-    struct disk_op_s dop;
-    dop.driveid = driveid;
-    dop.command = command;
-    int lba = legacy_lba(regs, get_global_seg(), &Drives.drives[driveid].lchs);
-    if (lba < 0)
-        return;
-    dop.lba = lba;
-    dop.count = regs->al;
     dop.buf_fl = MAKE_FLATPTR(regs->es, regs->bx);
 
     int status = send_disk_op(&dop);
 
     regs->al = dop.count;
 
-    if (status) {
-        disk_ret(regs, DISK_RET_EBADTRACK);
-        return;
-    }
-    disk_ret(regs, DISK_RET_SUCCESS);
-}
-
-// Perform cdemu read/verify
-void
-cdemu_access(struct bregs *regs, u8 driveid, u16 command)
-{
-    struct disk_op_s dop;
-    dop.driveid = driveid;
-    dop.command = command;
-    u16 ebda_seg = get_ebda_seg();
-    int vlba = legacy_lba(
-        regs, ebda_seg
-        , (void*)offsetof(struct extended_bios_data_area_s, cdemu.lchs));
-    if (vlba < 0)
-        return;
-    dop.lba = GET_EBDA2(ebda_seg, cdemu.ilba) + vlba / 4;
-    u8 count = regs->al;
-    u8 *cdbuf_far = (void*)offsetof(struct extended_bios_data_area_s, cdemu_buf);
-    u8 *dest_far = (void*)(regs->bx+0);
-    regs->al = 0;
-
-    if (vlba & 3) {
-        dop.count = 1;
-        dop.buf_fl = MAKE_FLATPTR(ebda_seg, cdbuf_far);
-        int status = send_disk_op(&dop);
-        if (status)
-            goto fail;
-        u8 thiscount = 4 - (vlba & 3);
-        if (thiscount > count)
-            thiscount = count;
-        count -= thiscount;
-        memcpy_far(regs->es, dest_far
-                   , ebda_seg, cdbuf_far + (vlba & 3) * 512
-                   , thiscount * 512);
-        dest_far += thiscount * 512;
-        regs->al += thiscount;
-        dop.lba++;
-    }
-
-    if (count > 3) {
-        dop.count = count / 4;
-        dop.buf_fl = MAKE_FLATPTR(regs->es, dest_far);
-        int status = send_disk_op(&dop);
-        regs->al += dop.count * 4;
-        if (status)
-            goto fail;
-        u8 thiscount = count & ~3;
-        count &= 3;
-        dest_far += thiscount * 512;
-        dop.lba += thiscount / 4;
-    }
-
-    if (count) {
-        dop.count = 1;
-        dop.buf_fl = MAKE_FLATPTR(ebda_seg, cdbuf_far);
-        int status = send_disk_op(&dop);
-        if (status)
-            goto fail;
-        u8 thiscount = count;
-        memcpy_far(regs->es, dest_far, ebda_seg, cdbuf_far, thiscount * 512);
-        regs->al += thiscount;
-    }
-
-    disk_ret(regs, DISK_RET_SUCCESS);
-    return;
-fail:
-    disk_ret(regs, DISK_RET_EBADTRACK);
+    disk_ret(regs, status);
 }
 
 // Perform read/write/verify using new-style "int13ext" accesses.
 static void
-extended_access(struct bregs *regs, u8 driveid, u16 command)
+extended_access(struct bregs *regs, struct drive_s *drive_g, u16 command)
 {
     struct disk_op_s dop;
     // Get lba and check.
     dop.lba = GET_INT13EXT(regs, lba);
     dop.command = command;
-    dop.driveid = driveid;
-    if (dop.lba >= GET_GLOBAL(Drives.drives[driveid].sectors)) {
-        dprintf(1, "int13_harddisk: function %02x. LBA out of range\n"
-                , regs->ah);
+    dop.drive_g = drive_g;
+    if (dop.lba >= GET_GLOBAL(drive_g->sectors)) {
+        warn_invalid(regs);
         disk_ret(regs, DISK_RET_EPARAM);
         return;
     }
 
-    u16 segment = GET_INT13EXT(regs, segment);
-    u16 offset = GET_INT13EXT(regs, offset);
-    dop.buf_fl = MAKE_FLATPTR(segment, offset);
+    dop.buf_fl = SEGOFF_TO_FLATPTR(GET_INT13EXT(regs, data));
     dop.count = GET_INT13EXT(regs, count);
 
     int status = send_disk_op(&dop);
 
     SET_INT13EXT(regs, count, dop.count);
 
-    if (status) {
-        disk_ret(regs, DISK_RET_EBADTRACK);
-        return;
-    }
-    disk_ret(regs, DISK_RET_SUCCESS);
+    disk_ret(regs, status);
 }
 
 
@@ -248,19 +150,25 @@ extended_access(struct bregs *regs, u8 driveid, u16 command)
 
 // disk controller reset
 static void
-disk_1300(struct bregs *regs, u8 driveid)
+disk_1300(struct bregs *regs, struct drive_s *drive_g)
 {
     struct disk_op_s dop;
-    dop.driveid = driveid;
+    dop.drive_g = drive_g;
     dop.command = CMD_RESET;
-    send_disk_op(&dop);
+    int status = send_disk_op(&dop);
+    disk_ret(regs, status);
 }
 
 // read disk status
 static void
-disk_1301(struct bregs *regs, u8 driveid)
+disk_1301(struct bregs *regs, struct drive_s *drive_g)
 {
-    u8 v = GET_BDA(disk_last_status);
+    u8 v;
+    if (regs->dl < EXTSTART_HD)
+        // Floppy
+        v = GET_BDA(floppy_last_status);
+    else
+        v = GET_BDA(disk_last_status);
     regs->ah = v;
     set_cf(regs, v);
     // XXX - clear disk_last_status?
@@ -268,126 +176,190 @@ disk_1301(struct bregs *regs, u8 driveid)
 
 // read disk sectors
 static void
-disk_1302(struct bregs *regs, u8 driveid)
+disk_1302(struct bregs *regs, struct drive_s *drive_g)
 {
-    basic_access(regs, driveid, CMD_READ);
+    basic_access(regs, drive_g, CMD_READ);
 }
 
 // write disk sectors
 static void
-disk_1303(struct bregs *regs, u8 driveid)
+disk_1303(struct bregs *regs, struct drive_s *drive_g)
 {
-    basic_access(regs, driveid, CMD_WRITE);
+    basic_access(regs, drive_g, CMD_WRITE);
 }
 
 // verify disk sectors
 static void
-disk_1304(struct bregs *regs, u8 driveid)
+disk_1304(struct bregs *regs, struct drive_s *drive_g)
 {
-    basic_access(regs, driveid, CMD_VERIFY);
-    // FIXME verify
+    basic_access(regs, drive_g, CMD_VERIFY);
 }
 
 // format disk track
 static void
-disk_1305(struct bregs *regs, u8 driveid)
+disk_1305(struct bregs *regs, struct drive_s *drive_g)
 {
-    DISK_STUB(regs);
+    debug_stub(regs);
+
+    u16 nlc, nlh, nlspt;
+    fillLCHS(drive_g, &nlc, &nlh, &nlspt);
+
+    u8 num_sectors = regs->al;
+    u8 head        = regs->dh;
+
+    if (head >= nlh || num_sectors == 0 || num_sectors > nlspt) {
+        disk_ret(regs, DISK_RET_EPARAM);
+        return;
+    }
+
+    struct disk_op_s dop;
+    dop.drive_g = drive_g;
+    dop.command = CMD_FORMAT;
+    dop.lba = head;
+    dop.count = num_sectors;
+    dop.buf_fl = MAKE_FLATPTR(regs->es, regs->bx);
+    int status = send_disk_op(&dop);
+    disk_ret(regs, status);
 }
 
 // read disk drive parameters
 static void
-disk_1308(struct bregs *regs, u8 driveid)
+disk_1308(struct bregs *regs, struct drive_s *drive_g)
 {
+    u16 ebda_seg = get_ebda_seg();
     // Get logical geometry from table
-    u16 nlc = GET_GLOBAL(Drives.drives[driveid].lchs.cylinders);
-    u16 nlh = GET_GLOBAL(Drives.drives[driveid].lchs.heads);
-    u16 nlspt = GET_GLOBAL(Drives.drives[driveid].lchs.spt);
-    u16 count = GET_BDA(hdcount);
+    u16 nlc, nlh, nlspt;
+    fillLCHS(drive_g, &nlc, &nlh, &nlspt);
+    nlc--;
+    nlh--;
+    u8 count;
+    if (regs->dl < EXTSTART_HD) {
+        // Floppy
+        count = GET_GLOBAL(FloppyCount);
+
+        if (CONFIG_CDROM_EMU
+            && drive_g == GLOBALFLAT2GLOBAL(GET_GLOBAL(cdemu_drive_gf)))
+            regs->bx = GET_EBDA2(ebda_seg, cdemu.media) * 2;
+        else
+            regs->bx = GET_GLOBAL(drive_g->floppy_type);
+
+        // set es & di to point to 11 byte diskette param table in ROM
+        regs->es = SEG_BIOS;
+        regs->di = (u32)&diskette_param_table2;
+    } else if (regs->dl < EXTSTART_CD) {
+        // Hard drive
+        count = GET_BDA(hdcount);
+        nlc--;  // last sector reserved
+    } else {
+        // Not supported on CDROM
+        disk_ret(regs, DISK_RET_EPARAM);
+        return;
+    }
+
+    if (CONFIG_CDROM_EMU && GET_EBDA2(ebda_seg, cdemu.active)) {
+        u8 emudrive = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
+        if (((emudrive ^ regs->dl) & 0x80) == 0)
+            // Note extra drive due to emulation.
+            count++;
+        if (regs->dl < EXTSTART_HD && count > 2)
+            // Max of two floppy drives.
+            count = 2;
+    }
 
-    nlc = nlc - 2; /* 0 based , last sector not used */
     regs->al = 0;
     regs->ch = nlc & 0xff;
     regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
-    regs->dh = nlh - 1;
-    regs->dl = count; /* FIXME returns 0, 1, or n hard drives */
+    regs->dh = nlh;
 
-    // FIXME should set ES & DI
     disk_ret(regs, DISK_RET_SUCCESS);
+    regs->dl = count;
 }
 
 // initialize drive parameters
 static void
-disk_1309(struct bregs *regs, u8 driveid)
+disk_1309(struct bregs *regs, struct drive_s *drive_g)
 {
     DISK_STUB(regs);
 }
 
 // seek to specified cylinder
 static void
-disk_130c(struct bregs *regs, u8 driveid)
+disk_130c(struct bregs *regs, struct drive_s *drive_g)
 {
     DISK_STUB(regs);
 }
 
 // alternate disk reset
 static void
-disk_130d(struct bregs *regs, u8 driveid)
+disk_130d(struct bregs *regs, struct drive_s *drive_g)
 {
     DISK_STUB(regs);
 }
 
 // check drive ready
 static void
-disk_1310(struct bregs *regs, u8 driveid)
+disk_1310(struct bregs *regs, struct drive_s *drive_g)
 {
     // should look at 40:8E also???
 
     struct disk_op_s dop;
-    dop.driveid = driveid;
+    dop.drive_g = drive_g;
     dop.command = CMD_ISREADY;
     int status = send_disk_op(&dop);
-    if (status)
-        disk_ret(regs, DISK_RET_ENOTREADY);
-    else
-        disk_ret(regs, DISK_RET_SUCCESS);
+    disk_ret(regs, status);
 }
 
 // recalibrate
 static void
-disk_1311(struct bregs *regs, u8 driveid)
+disk_1311(struct bregs *regs, struct drive_s *drive_g)
 {
     DISK_STUB(regs);
 }
 
 // controller internal diagnostic
 static void
-disk_1314(struct bregs *regs, u8 driveid)
+disk_1314(struct bregs *regs, struct drive_s *drive_g)
 {
     DISK_STUB(regs);
 }
 
 // read disk drive size
 static void
-disk_1315(struct bregs *regs, u8 driveid)
+disk_1315(struct bregs *regs, struct drive_s *drive_g)
 {
+    disk_ret(regs, DISK_RET_SUCCESS);
+    if (regs->dl < EXTSTART_HD || regs->dl >= EXTSTART_CD) {
+        // Floppy or cdrom
+        regs->ah = 1;
+        return;
+    }
+    // Hard drive
+
     // Get logical geometry from table
-    u16 nlc   = GET_GLOBAL(Drives.drives[driveid].lchs.cylinders);
-    u16 nlh   = GET_GLOBAL(Drives.drives[driveid].lchs.heads);
-    u16 nlspt = GET_GLOBAL(Drives.drives[driveid].lchs.spt);
+    u16 nlc, nlh, nlspt;
+    fillLCHS(drive_g, &nlc, &nlh, &nlspt);
 
     // Compute sector count seen by int13
     u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
     regs->cx = lba >> 16;
     regs->dx = lba & 0xffff;
-
-    disk_ret(regs, DISK_RET_SUCCESS);
     regs->ah = 3; // hard disk accessible
 }
 
+static void
+disk_1316(struct bregs *regs, struct drive_s *drive_g)
+{
+    if (regs->dl >= EXTSTART_HD) {
+        // Hard drive
+        disk_ret(regs, DISK_RET_EPARAM);
+        return;
+    }
+    disk_ret(regs, DISK_RET_ECHANGED);
+}
+
 // IBM/MS installation check
 static void
-disk_1341(struct bregs *regs, u8 driveid)
+disk_1341(struct bregs *regs, struct drive_s *drive_g)
 {
     regs->bx = 0xaa55;  // install check
     regs->cx = 0x0007;  // ext disk access and edd, removable supported
@@ -397,53 +369,141 @@ disk_1341(struct bregs *regs, u8 driveid)
 
 // IBM/MS extended read
 static void
-disk_1342(struct bregs *regs, u8 driveid)
+disk_1342(struct bregs *regs, struct drive_s *drive_g)
 {
-    extended_access(regs, driveid, CMD_READ);
+    extended_access(regs, drive_g, CMD_READ);
 }
 
 // IBM/MS extended write
 static void
-disk_1343(struct bregs *regs, u8 driveid)
+disk_1343(struct bregs *regs, struct drive_s *drive_g)
 {
-    extended_access(regs, driveid, CMD_WRITE);
+    extended_access(regs, drive_g, CMD_WRITE);
 }
 
 // IBM/MS verify
 static void
-disk_1344(struct bregs *regs, u8 driveid)
+disk_1344(struct bregs *regs, struct drive_s *drive_g)
 {
-    extended_access(regs, driveid, CMD_VERIFY);
+    extended_access(regs, drive_g, CMD_VERIFY);
 }
 
-// IBM/MS lock/unlock drive
+// lock
+static void
+disk_134500(struct bregs *regs, struct drive_s *drive_g)
+{
+    u16 ebda_seg = get_ebda_seg();
+    int cdid = regs->dl - EXTSTART_CD;
+    u8 locks = GET_EBDA2(ebda_seg, cdrom_locks[cdid]);
+    if (locks == 0xff) {
+        regs->al = 1;
+        disk_ret(regs, DISK_RET_ETOOMANYLOCKS);
+        return;
+    }
+    SET_EBDA2(ebda_seg, cdrom_locks[cdid], locks + 1);
+    regs->al = 1;
+    disk_ret(regs, DISK_RET_SUCCESS);
+}
+
+// unlock
 static void
-disk_1345(struct bregs *regs, u8 driveid)
+disk_134501(struct bregs *regs, struct drive_s *drive_g)
 {
-    // Always success for HD
+    u16 ebda_seg = get_ebda_seg();
+    int cdid = regs->dl - EXTSTART_CD;
+    u8 locks = GET_EBDA2(ebda_seg, cdrom_locks[cdid]);
+    if (locks == 0x00) {
+        regs->al = 0;
+        disk_ret(regs, DISK_RET_ENOTLOCKED);
+        return;
+    }
+    locks--;
+    SET_EBDA2(ebda_seg, cdrom_locks[cdid], locks);
+    regs->al = (locks ? 1 : 0);
+    disk_ret(regs, DISK_RET_SUCCESS);
+}
+
+// status
+static void
+disk_134502(struct bregs *regs, struct drive_s *drive_g)
+{
+    int cdid = regs->dl - EXTSTART_CD;
+    u8 locks = GET_EBDA(cdrom_locks[cdid]);
+    regs->al = (locks ? 1 : 0);
     disk_ret(regs, DISK_RET_SUCCESS);
 }
 
+static void
+disk_1345XX(struct bregs *regs, struct drive_s *drive_g)
+{
+    disk_ret_unimplemented(regs, DISK_RET_EPARAM);
+}
+
+// IBM/MS lock/unlock drive
+static void
+disk_1345(struct bregs *regs, struct drive_s *drive_g)
+{
+    if (regs->dl < EXTSTART_CD) {
+        // Always success for HD
+        disk_ret(regs, DISK_RET_SUCCESS);
+        return;
+    }
+
+    switch (regs->al) {
+    case 0x00: disk_134500(regs, drive_g); break;
+    case 0x01: disk_134501(regs, drive_g); break;
+    case 0x02: disk_134502(regs, drive_g); break;
+    default:   disk_1345XX(regs, drive_g); break;
+    }
+}
+
 // IBM/MS eject media
 static void
-disk_1346(struct bregs *regs, u8 driveid)
+disk_1346(struct bregs *regs, struct drive_s *drive_g)
 {
-    // Volume Not Removable
-    disk_ret(regs, DISK_RET_ENOTREMOVABLE);
+    if (regs->dl < EXTSTART_CD) {
+        // Volume Not Removable
+        disk_ret(regs, DISK_RET_ENOTREMOVABLE);
+        return;
+    }
+
+    int cdid = regs->dl - EXTSTART_CD;
+    u8 locks = GET_EBDA(cdrom_locks[cdid]);
+    if (locks != 0) {
+        disk_ret(regs, DISK_RET_ELOCKED);
+        return;
+    }
+
+    // FIXME should handle 0x31 no media in device
+    // FIXME should handle 0xb5 valid request failed
+
+    // Call removable media eject
+    struct bregs br;
+    memset(&br, 0, sizeof(br));
+    br.ah = 0x52;
+    br.dl = regs->dl;
+    call16_int(0x15, &br);
+
+    if (br.ah || br.flags & F_CF) {
+        disk_ret(regs, DISK_RET_ELOCKED);
+        return;
+    }
+    disk_ret(regs, DISK_RET_SUCCESS);
 }
 
 // IBM/MS extended seek
 static void
-disk_1347(struct bregs *regs, u8 driveid)
+disk_1347(struct bregs *regs, struct drive_s *drive_g)
 {
-    extended_access(regs, driveid, CMD_SEEK);
+    extended_access(regs, drive_g, CMD_SEEK);
 }
 
 // IBM/MS get drive parameters
 static void
-disk_1348(struct bregs *regs, u8 driveid)
+disk_1348(struct bregs *regs, struct drive_s *drive_g)
 {
     u16 size = GET_INT13DPT(regs, size);
+    u16 t13 = size == 74;
 
     // Buffer is too small
     if (size < 26) {
@@ -453,12 +513,12 @@ disk_1348(struct bregs *regs, u8 driveid)
 
     // EDD 1.x
 
-    u8  type    = GET_GLOBAL(Drives.drives[driveid].type);
-    u16 npc     = GET_GLOBAL(Drives.drives[driveid].pchs.cylinders);
-    u16 nph     = GET_GLOBAL(Drives.drives[driveid].pchs.heads);
-    u16 npspt   = GET_GLOBAL(Drives.drives[driveid].pchs.spt);
-    u64 lba     = GET_GLOBAL(Drives.drives[driveid].sectors);
-    u16 blksize = GET_GLOBAL(Drives.drives[driveid].blksize);
+    u8  type    = GET_GLOBAL(drive_g->type);
+    u16 npc     = GET_GLOBAL(drive_g->pchs.cylinders);
+    u16 nph     = GET_GLOBAL(drive_g->pchs.heads);
+    u16 npspt   = GET_GLOBAL(drive_g->pchs.spt);
+    u64 lba     = GET_GLOBAL(drive_g->sectors);
+    u16 blksize = GET_GLOBAL(drive_g->blksize);
 
     dprintf(DEBUG_HDL_13, "disk_1348 size=%d t=%d chs=%d,%d,%d lba=%d bs=%d\n"
             , size, type, npc, nph, npspt, (u32)lba, blksize);
@@ -485,63 +545,78 @@ disk_1348(struct bregs *regs, u8 driveid)
     }
     SET_INT13DPT(regs, blksize, blksize);
 
-    if (size < 30 || (type != DTYPE_ATA && type != DTYPE_ATAPI)) {
+    if (size < 30 ||
+        (type != DTYPE_ATA && type != DTYPE_ATAPI && type != DTYPE_VIRTIO_BLK)) {
         disk_ret(regs, DISK_RET_SUCCESS);
         return;
     }
 
     // EDD 2.x
 
-    u16 ebda_seg = get_ebda_seg();
+    int bdf;
+    u16 iobase1 = 0;
+    u64 device_path = 0;
+    u8 channel = 0;
     SET_INT13DPT(regs, size, 30);
+    if (type == DTYPE_ATA || type == DTYPE_ATAPI) {
+        u16 ebda_seg = get_ebda_seg();
 
-    SET_INT13DPT(regs, dpte_segment, ebda_seg);
-    SET_INT13DPT(regs, dpte_offset
-                 , offsetof(struct extended_bios_data_area_s, dpte));
-
-    // Fill in dpte
-    u8 ataid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
-    u8 channel = ataid / 2;
-    u8 slave = ataid % 2;
-    u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
-    u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
-    u8 irq = GET_GLOBAL(ATA_channels[channel].irq);
-
-    u16 options = 0;
-    if (type == DTYPE_ATA) {
-        u8 translation = GET_GLOBAL(Drives.drives[driveid].translation);
-        if (translation != TRANSLATION_NONE) {
-            options |= 1<<3; // CHS translation
-            if (translation == TRANSLATION_LBA)
-                options |= 1<<9;
-            if (translation == TRANSLATION_RECHS)
-                options |= 3<<9;
+        SET_INT13DPT(regs, dpte_segment, ebda_seg);
+        SET_INT13DPT(regs, dpte_offset
+                     , offsetof(struct extended_bios_data_area_s, dpte));
+
+        // Fill in dpte
+        struct atadrive_s *adrive_g = container_of(
+            drive_g, struct atadrive_s, drive);
+        struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
+        u8 slave = GET_GLOBAL(adrive_g->slave);
+        u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
+        u8 irq = GET_GLOBALFLAT(chan_gf->irq);
+        iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
+        bdf = GET_GLOBALFLAT(chan_gf->pci_bdf);
+        device_path = slave;
+        channel = GET_GLOBALFLAT(chan_gf->chanid);
+
+        u16 options = 0;
+        if (type == DTYPE_ATA) {
+            u8 translation = GET_GLOBAL(drive_g->translation);
+            if (translation != TRANSLATION_NONE) {
+                options |= 1<<3; // CHS translation
+                if (translation == TRANSLATION_LBA)
+                    options |= 1<<9;
+                if (translation == TRANSLATION_RECHS)
+                    options |= 3<<9;
+            }
+        } else {
+            // ATAPI
+            options |= 1<<5; // removable device
+            options |= 1<<6; // atapi device
         }
+        options |= 1<<4; // lba translation
+        if (CONFIG_ATA_PIO32)
+            options |= 1<<7;
+
+        SET_EBDA2(ebda_seg, dpte.iobase1, iobase1);
+        SET_EBDA2(ebda_seg, dpte.iobase2, iobase2 + ATA_CB_DC);
+        SET_EBDA2(ebda_seg, dpte.prefix, ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
+                                          | ATA_CB_DH_LBA));
+        SET_EBDA2(ebda_seg, dpte.unused, 0xcb);
+        SET_EBDA2(ebda_seg, dpte.irq, irq);
+        SET_EBDA2(ebda_seg, dpte.blkcount, 1);
+        SET_EBDA2(ebda_seg, dpte.dma, 0);
+        SET_EBDA2(ebda_seg, dpte.pio, 0);
+        SET_EBDA2(ebda_seg, dpte.options, options);
+        SET_EBDA2(ebda_seg, dpte.reserved, 0);
+        SET_EBDA2(ebda_seg, dpte.revision, 0x11);
+
+        u8 sum = checksum_far(
+            ebda_seg, (void*)offsetof(struct extended_bios_data_area_s, dpte), 15);
+        SET_EBDA2(ebda_seg, dpte.checksum, -sum);
     } else {
-        // ATAPI
-        options |= 1<<5; // removable device
-        options |= 1<<6; // atapi device
+        SET_INT13DPT(regs, dpte_segment, 0);
+        SET_INT13DPT(regs, dpte_offset, 0);
+        bdf = GET_GLOBAL(drive_g->cntl_id);
     }
-    options |= 1<<4; // lba translation
-    if (CONFIG_ATA_PIO32)
-        options |= 1<<7;
-
-    SET_EBDA2(ebda_seg, dpte.iobase1, iobase1);
-    SET_EBDA2(ebda_seg, dpte.iobase2, iobase2 + ATA_CB_DC);
-    SET_EBDA2(ebda_seg, dpte.prefix, ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
-                                      | ATA_CB_DH_LBA));
-    SET_EBDA2(ebda_seg, dpte.unused, 0xcb);
-    SET_EBDA2(ebda_seg, dpte.irq, irq);
-    SET_EBDA2(ebda_seg, dpte.blkcount, 1);
-    SET_EBDA2(ebda_seg, dpte.dma, 0);
-    SET_EBDA2(ebda_seg, dpte.pio, 0);
-    SET_EBDA2(ebda_seg, dpte.options, options);
-    SET_EBDA2(ebda_seg, dpte.reserved, 0);
-    SET_EBDA2(ebda_seg, dpte.revision, 0x11);
-
-    u8 sum = checksum_far(
-        ebda_seg, (void*)offsetof(struct extended_bios_data_area_s, dpte), 15);
-    SET_EBDA2(ebda_seg, dpte.checksum, -sum);
 
     if (size < 66) {
         disk_ret(regs, DISK_RET_SUCCESS);
@@ -550,96 +625,129 @@ disk_1348(struct bregs *regs, u8 driveid)
 
     // EDD 3.x
     SET_INT13DPT(regs, key, 0xbedd);
-    SET_INT13DPT(regs, dpi_length, 36);
+    SET_INT13DPT(regs, dpi_length, t13 ? 44 : 36);
     SET_INT13DPT(regs, reserved1, 0);
     SET_INT13DPT(regs, reserved2, 0);
 
-    SET_INT13DPT(regs, host_bus[0], 'P');
-    SET_INT13DPT(regs, host_bus[1], 'C');
-    SET_INT13DPT(regs, host_bus[2], 'I');
-    SET_INT13DPT(regs, host_bus[3], 0);
+    if (bdf != -1) {
+        SET_INT13DPT(regs, host_bus[0], 'P');
+        SET_INT13DPT(regs, host_bus[1], 'C');
+        SET_INT13DPT(regs, host_bus[2], 'I');
+        SET_INT13DPT(regs, host_bus[3], ' ');
+
+        u32 path = (pci_bdf_to_bus(bdf) | (pci_bdf_to_dev(bdf) << 8)
+                    | (pci_bdf_to_fn(bdf) << 16));
+        if (t13)
+            path |= channel << 24;
+
+        SET_INT13DPT(regs, iface_path, path);
+    } else {
+        // ISA
+        SET_INT13DPT(regs, host_bus[0], 'I');
+        SET_INT13DPT(regs, host_bus[1], 'S');
+        SET_INT13DPT(regs, host_bus[2], 'A');
+        SET_INT13DPT(regs, host_bus[3], ' ');
+
+        SET_INT13DPT(regs, iface_path, iobase1);
+    }
 
-    u32 bdf = GET_GLOBAL(ATA_channels[channel].pci_bdf);
-    u32 path = (pci_bdf_to_bus(bdf) | (pci_bdf_to_dev(bdf) << 8)
-                | (pci_bdf_to_fn(bdf) << 16));
-    SET_INT13DPT(regs, iface_path, path);
+    if (type != DTYPE_VIRTIO_BLK) {
+        SET_INT13DPT(regs, iface_type[0], 'A');
+        SET_INT13DPT(regs, iface_type[1], 'T');
+        SET_INT13DPT(regs, iface_type[2], 'A');
+        SET_INT13DPT(regs, iface_type[3], ' ');
+    } else {
+        SET_INT13DPT(regs, iface_type[0], 'S');
+        SET_INT13DPT(regs, iface_type[1], 'C');
+        SET_INT13DPT(regs, iface_type[2], 'S');
+        SET_INT13DPT(regs, iface_type[3], 'I');
+    }
+    SET_INT13DPT(regs, iface_type[4], ' ');
+    SET_INT13DPT(regs, iface_type[5], ' ');
+    SET_INT13DPT(regs, iface_type[6], ' ');
+    SET_INT13DPT(regs, iface_type[7], ' ');
 
-    SET_INT13DPT(regs, iface_type[0], 'A');
-    SET_INT13DPT(regs, iface_type[1], 'T');
-    SET_INT13DPT(regs, iface_type[2], 'A');
-    SET_INT13DPT(regs, iface_type[3], 0);
-    SET_INT13DPT(regs, iface_type[4], 0);
-    SET_INT13DPT(regs, iface_type[5], 0);
-    SET_INT13DPT(regs, iface_type[6], 0);
-    SET_INT13DPT(regs, iface_type[7], 0);
+    if (t13) {
+        SET_INT13DPT(regs, t13.device_path[0], device_path);
+        SET_INT13DPT(regs, t13.device_path[1], 0);
 
-    SET_INT13DPT(regs, device_path, slave);
+        SET_INT13DPT(regs, t13.checksum
+                     , -checksum_far(regs->ds, (void*)(regs->si+30), 43));
+    } else {
+        SET_INT13DPT(regs, phoenix.device_path, device_path);
 
-    SET_INT13DPT(regs, checksum
-                 , -checksum_far(regs->ds, (void*)(regs->si+30), 35));
+        SET_INT13DPT(regs, phoenix.checksum
+                     , -checksum_far(regs->ds, (void*)(regs->si+30), 35));
+    }
 
     disk_ret(regs, DISK_RET_SUCCESS);
 }
 
 // IBM/MS extended media change
 static void
-disk_1349(struct bregs *regs, u8 driveid)
+disk_1349(struct bregs *regs, struct drive_s *drive_g)
 {
-    // Always success for HD
-    disk_ret(regs, DISK_RET_SUCCESS);
+    if (regs->dl < EXTSTART_CD) {
+        // Always success for HD
+        disk_ret(regs, DISK_RET_SUCCESS);
+        return;
+    }
+    set_invalid(regs);
+    // always send changed ??
+    regs->ah = DISK_RET_ECHANGED;
 }
 
 static void
-disk_134e01(struct bregs *regs, u8 driveid)
+disk_134e01(struct bregs *regs, struct drive_s *drive_g)
 {
     disk_ret(regs, DISK_RET_SUCCESS);
 }
 
 static void
-disk_134e03(struct bregs *regs, u8 driveid)
+disk_134e03(struct bregs *regs, struct drive_s *drive_g)
 {
     disk_ret(regs, DISK_RET_SUCCESS);
 }
 
 static void
-disk_134e04(struct bregs *regs, u8 driveid)
+disk_134e04(struct bregs *regs, struct drive_s *drive_g)
 {
     disk_ret(regs, DISK_RET_SUCCESS);
 }
 
 static void
-disk_134e06(struct bregs *regs, u8 driveid)
+disk_134e06(struct bregs *regs, struct drive_s *drive_g)
 {
     disk_ret(regs, DISK_RET_SUCCESS);
 }
 
 static void
-disk_134eXX(struct bregs *regs, u8 driveid)
+disk_134eXX(struct bregs *regs, struct drive_s *drive_g)
 {
     disk_ret(regs, DISK_RET_EPARAM);
 }
 
 // IBM/MS set hardware configuration
 static void
-disk_134e(struct bregs *regs, u8 driveid)
+disk_134e(struct bregs *regs, struct drive_s *drive_g)
 {
     switch (regs->al) {
-    case 0x01: disk_134e01(regs, driveid); break;
-    case 0x03: disk_134e03(regs, driveid); break;
-    case 0x04: disk_134e04(regs, driveid); break;
-    case 0x06: disk_134e06(regs, driveid); break;
-    default:   disk_134eXX(regs, driveid); break;
+    case 0x01: disk_134e01(regs, drive_g); break;
+    case 0x03: disk_134e03(regs, drive_g); break;
+    case 0x04: disk_134e04(regs, drive_g); break;
+    case 0x06: disk_134e06(regs, drive_g); break;
+    default:   disk_134eXX(regs, drive_g); break;
     }
 }
 
-void
-disk_13XX(struct bregs *regs, u8 driveid)
+static void
+disk_13XX(struct bregs *regs, struct drive_s *drive_g)
 {
-    disk_ret(regs, DISK_RET_EPARAM);
+    disk_ret_unimplemented(regs, DISK_RET_EPARAM);
 }
 
-void
-disk_13(struct bregs *regs, u8 driveid)
+static void
+disk_13(struct bregs *regs, struct drive_s *drive_g)
 {
     //debug_stub(regs);
 
@@ -647,86 +755,90 @@ disk_13(struct bregs *regs, u8 driveid)
     SET_BDA(disk_interrupt_flag, 0);
 
     switch (regs->ah) {
-    case 0x00: disk_1300(regs, driveid); break;
-    case 0x01: disk_1301(regs, driveid); break;
-    case 0x02: disk_1302(regs, driveid); break;
-    case 0x03: disk_1303(regs, driveid); break;
-    case 0x04: disk_1304(regs, driveid); break;
-    case 0x05: disk_1305(regs, driveid); break;
-    case 0x08: disk_1308(regs, driveid); break;
-    case 0x09: disk_1309(regs, driveid); break;
-    case 0x0c: disk_130c(regs, driveid); break;
-    case 0x0d: disk_130d(regs, driveid); break;
-    case 0x10: disk_1310(regs, driveid); break;
-    case 0x11: disk_1311(regs, driveid); break;
-    case 0x14: disk_1314(regs, driveid); break;
-    case 0x15: disk_1315(regs, driveid); break;
-    case 0x41: disk_1341(regs, driveid); break;
-    case 0x42: disk_1342(regs, driveid); break;
-    case 0x43: disk_1343(regs, driveid); break;
-    case 0x44: disk_1344(regs, driveid); break;
-    case 0x45: disk_1345(regs, driveid); break;
-    case 0x46: disk_1346(regs, driveid); break;
-    case 0x47: disk_1347(regs, driveid); break;
-    case 0x48: disk_1348(regs, driveid); break;
-    case 0x49: disk_1349(regs, driveid); break;
-    case 0x4e: disk_134e(regs, driveid); break;
-    default:   disk_13XX(regs, driveid); break;
+    case 0x00: disk_1300(regs, drive_g); break;
+    case 0x01: disk_1301(regs, drive_g); break;
+    case 0x02: disk_1302(regs, drive_g); break;
+    case 0x03: disk_1303(regs, drive_g); break;
+    case 0x04: disk_1304(regs, drive_g); break;
+    case 0x05: disk_1305(regs, drive_g); break;
+    case 0x08: disk_1308(regs, drive_g); break;
+    case 0x09: disk_1309(regs, drive_g); break;
+    case 0x0c: disk_130c(regs, drive_g); break;
+    case 0x0d: disk_130d(regs, drive_g); break;
+    case 0x10: disk_1310(regs, drive_g); break;
+    case 0x11: disk_1311(regs, drive_g); break;
+    case 0x14: disk_1314(regs, drive_g); break;
+    case 0x15: disk_1315(regs, drive_g); break;
+    case 0x16: disk_1316(regs, drive_g); break;
+    case 0x41: disk_1341(regs, drive_g); break;
+    case 0x42: disk_1342(regs, drive_g); break;
+    case 0x43: disk_1343(regs, drive_g); break;
+    case 0x44: disk_1344(regs, drive_g); break;
+    case 0x45: disk_1345(regs, drive_g); break;
+    case 0x46: disk_1346(regs, drive_g); break;
+    case 0x47: disk_1347(regs, drive_g); break;
+    case 0x48: disk_1348(regs, drive_g); break;
+    case 0x49: disk_1349(regs, drive_g); break;
+    case 0x4e: disk_134e(regs, drive_g); break;
+    default:   disk_13XX(regs, drive_g); break;
     }
 }
 
-
-/****************************************************************
- * Entry points
- ****************************************************************/
-
-static int
-get_driveid(struct bregs *regs, u8 iscd, u8 drive)
+static void
+floppy_13(struct bregs *regs, struct drive_s *drive_g)
 {
-    // basic check : device has to be defined
-    if (drive >= ARRAY_SIZE(Drives.idmap[0])) {
-        disk_ret(regs, DISK_RET_EPARAM);
-        return -1;
+    // Only limited commands are supported on floppies.
+    switch (regs->ah) {
+    case 0x00:
+    case 0x01:
+    case 0x02:
+    case 0x03:
+    case 0x04:
+    case 0x05:
+    case 0x08:
+    case 0x15:
+    case 0x16:
+        disk_13(regs, drive_g);
+        break;
+    default:   disk_13XX(regs, drive_g); break;
     }
+}
 
-    // Get the ata channel
-    u8 driveid = GET_GLOBAL(Drives.idmap[iscd][drive]);
-
-    // basic check : device has to be valid
-    if (driveid >= ARRAY_SIZE(Drives.drives)) {
-        disk_ret(regs, DISK_RET_EPARAM);
-        return -1;
-    }
 
-    return driveid;
-}
+/****************************************************************
+ * Entry points
+ ****************************************************************/
 
 static void
 handle_legacy_disk(struct bregs *regs, u8 extdrive)
 {
-    if (extdrive < 0x80) {
-        floppy_13(regs, extdrive);
-        return;
-    }
-
     if (! CONFIG_DRIVES) {
-        // XXX - old code had other disk access method.
+        // XXX - support handle_1301 anyway?
         disk_ret(regs, DISK_RET_EPARAM);
         return;
     }
 
-    if (extdrive >= 0xe0) {
-        int driveid = get_driveid(regs, 1, extdrive - 0xe0);
-        if (driveid < 0)
-            return;
-        cdrom_13(regs, driveid);
+    if (extdrive < EXTSTART_HD) {
+        struct drive_s *drive_g = getDrive(EXTTYPE_FLOPPY, extdrive);
+        if (!drive_g)
+            goto fail;
+        floppy_13(regs, drive_g);
         return;
     }
 
-    int driveid = get_driveid(regs, 0, extdrive - 0x80);
-    if (driveid < 0)
-        return;
-    disk_13(regs, driveid);
+    struct drive_s *drive_g;
+    if (extdrive >= EXTSTART_CD)
+        drive_g = getDrive(EXTTYPE_CD, extdrive - EXTSTART_CD);
+    else
+        drive_g = getDrive(EXTTYPE_HD, extdrive - EXTSTART_HD);
+    if (!drive_g)
+        goto fail;
+    disk_13(regs, drive_g);
+    return;
+
+fail:
+    // XXX - support 1301/1308/1315 anyway?
+    disk_ret(regs, DISK_RET_EPARAM);
 }
 
 void VISIBLE16
@@ -752,10 +864,19 @@ handle_13(struct bregs *regs)
         if (GET_EBDA2(ebda_seg, cdemu.active)) {
             u8 emudrive = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
             if (extdrive == emudrive) {
-                cdemu_13(regs);
+                // Access to an emulated drive.
+                struct drive_s *cdemu_g;
+                cdemu_g = GLOBALFLAT2GLOBAL(GET_GLOBAL(cdemu_drive_gf));
+                if (regs->ah > 0x16) {
+                    // Only old-style commands supported.
+                    disk_13XX(regs, cdemu_g);
+                    return;
+                }
+                disk_13(regs, cdemu_g);
                 return;
             }
-            if (extdrive < 0xe0 && ((emudrive ^ extdrive) & 0x80) == 0)
+            if (extdrive < EXTSTART_CD && ((emudrive ^ extdrive) & 0x80) == 0)
+                // Adjust id to make room for emulated drive.
                 extdrive--;
         }
     }
@@ -764,7 +885,7 @@ handle_13(struct bregs *regs)
 
 // record completion in BIOS task complete flag
 void VISIBLE16
-handle_76()
+handle_76(void)
 {
     debug_isr(DEBUG_ISR_76);
     SET_BDA(disk_interrupt_flag, 0xff);