Separate ATA code from generic disk code.
[seabios.git] / src / disk.c
index 832eee1c0043d2787a46534afc5996e923bae81e..263c0d9a802e2e18df621da9a3978d50e58660a2 100644 (file)
 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
 // Copyright (C) 2002  MandrakeSoft S.A.
 //
-// This file may be distributed under the terms of the GNU GPLv3 license.
+// This file may be distributed under the terms of the GNU LGPLv3 license.
 
 #include "disk.h" // floppy_13
-#include "biosvar.h" // struct bregs
+#include "biosvar.h" // SET_BDA
 #include "config.h" // CONFIG_*
-#include "cmos.h" // inb_cmos
 #include "util.h" // debug_enter
-#include "ata.h" // ATA_*
+#include "pic.h" // eoi_pic2
+#include "bregs.h" // struct bregs
+#include "pci.h" // pci_bdf_to_bus
+#include "ata.h" // ATA_CB_DC
 
 
 /****************************************************************
  * Helper functions
  ****************************************************************/
 
-static inline void
-disk_ret(struct bregs *regs, u8 code)
+void
+__disk_ret(struct bregs *regs, u32 linecode, const char *fname)
 {
-    regs->ah = code;
+    u8 code = linecode;
     SET_BDA(disk_last_status, code);
-    set_cf(regs, code);
+    if (code)
+        __set_code_fail(regs, linecode, fname);
+    else
+        set_code_success(regs);
 }
 
-#define DISK_STUB(regs) do {                    \
-        struct bregs *__regs = (regs);          \
-        debug_stub(__regs);                     \
-        disk_ret(__regs, DISK_RET_SUCCESS);     \
-    } while (0)
-
-static u8
-checksum_seg(u16 seg, u16 offset, u32 len)
+static void
+__disk_stub(struct bregs *regs, int lineno, const char *fname)
 {
-    u32 i;
-    u8 sum = 0;
-    for (i=0; i<len; i++)
-        sum += GET_FARVAR(seg, *(u8*)(offset+i));
-    return sum;
+    __debug_stub(regs, lineno, fname);
+    __disk_ret(regs, DISK_RET_SUCCESS | (lineno << 8), fname);
 }
 
-static void
-basic_access(struct bregs *regs, u8 device, u16 command)
+#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)
 {
-    u16 count       = regs->al;
-    u16 cylinder    = regs->ch | ((((u16) regs->cl) << 2) & 0x300);
-    u16 sector      = regs->cl & 0x3f;
-    u16 head        = regs->dh;
+    struct disk_op_s dop;
+    memcpy_far(GET_SEG(SS), &dop
+               , op_seg, op_far
+               , sizeof(dop));
 
-    if ((count > 128) || (count == 0) || (sector == 0)) {
-        BX_INFO("int13_harddisk: function %02x, parameter out of range!\n"
-                , regs->ah);
-        disk_ret(regs, DISK_RET_EPARAM);
-        return;
-    }
+    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);
 
-    u16 nlc   = GET_EBDA(ata.devices[device].lchs.cylinders);
-    u16 nlh   = GET_EBDA(ata.devices[device].lchs.heads);
-    u16 nlspt = GET_EBDA(ata.devices[device].lchs.spt);
-    u16 nph   = GET_EBDA(ata.devices[device].pchs.heads);
-    u16 npspt = GET_EBDA(ata.devices[device].pchs.spt);
+    irq_enable();
 
-    // sanity check on cyl heads, sec
-    if ( (cylinder >= nlc) || (head >= nlh) || (sector > nlspt )) {
-        BX_INFO("int13_harddisk: function %02x, parameters out of"
-                " range %04x/%04x/%04x!\n"
-                , regs->ah, cylinder, head, sector);
-        disk_ret(regs, DISK_RET_EPARAM);
-        return;
-    }
+    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);
 
-    u32 lba = 0;
-    // if needed, translate lchs to lba, and execute command
-    if ( (nph != nlh) || (npspt != nlspt)) {
-        lba = (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nlspt)
-               + (u32)sector - 1);
-        sector = 0; // this forces the command to be lba
-    }
+    irq_disable();
 
-    u16 segment = regs->es;
-    u16 offset  = regs->bx;
-
-    u8 status;
-    switch (command) {
-    case ATA_CMD_READ_SECTORS:
-        status = ata_cmd_data_in(device, ATA_CMD_READ_SECTORS
-                                 , count, cylinder, head, sector
-                                 , lba, segment, offset);
-        break;
-    case ATA_CMD_WRITE_SECTORS:
-        status = ata_cmd_data_out(device, ATA_CMD_WRITE_SECTORS
-                                  , count, cylinder, head, sector
-                                  , lba, segment, offset);
-        break;
-    default:
-        disk_ret(regs, DISK_RET_SUCCESS);
-        return;
-    }
+    // Update count with total sectors transferred.
+    SET_FARVAR(op_seg, op_far->count, dop.count);
 
-    // Set nb of sector transferred
-    regs->al = GET_EBDA(ata.trsfsectors);
+    if (status)
+        dprintf(1, "disk_op cmd %d error %d!\n", dop.command, status);
 
-    if (status != 0) {
-        BX_INFO("int13_harddisk: function %02x, error %02x !\n",regs->ah,status);
-        disk_ret(regs, DISK_RET_EBADTRACK);
-    }
-    disk_ret(regs, DISK_RET_SUCCESS);
+    return status;
 }
 
-static void
-emu_access(struct bregs *regs, u8 device, u16 command)
+// Execute a "disk_op_s" request by jumping to a stack in the ebda.
+static int
+send_disk_op(struct disk_op_s *op)
 {
-    u16 nbsectors   = regs->al;
-    u16 cylinder    = regs->ch | ((((u16) regs->cl) << 2) & 0x300);
-    u16 sector      = regs->cl & 0x3f;
-    u16 head        = regs->dh;
+    if (! CONFIG_DRIVES)
+        return -1;
+
+    return stack_hop((u32)op, GET_SEG(SS), 0, __send_disk_op);
+}
 
-    if ((nbsectors > 128) || (nbsectors == 0) || (sector == 0)) {
-        BX_INFO("int13_harddisk: function %02x, parameter out of range!\n"
+// 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);
         disk_ret(regs, DISK_RET_EPARAM);
-        return;
+        return -1;
     }
 
-    u16 nlc   = GET_EBDA(cdemu.vdevice.cylinders);
-    u16 nlh   = GET_EBDA(cdemu.vdevice.heads);
-    u16 nlspt = GET_EBDA(cdemu.vdevice.spt);
+    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);
 
     // sanity check on cyl heads, sec
-    if ( (cylinder >= nlc) || (head >= nlh) || (sector > nlspt )) {
-        BX_INFO("int13_harddisk: function %02x, parameters out of"
+    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);
         disk_ret(regs, DISK_RET_EPARAM);
-        return;
+        return -1;
     }
 
-    if (!command) {
-        // If verify or seek
-        disk_ret(regs, DISK_RET_SUCCESS);
+    // translate lchs to lba
+    return (((((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 device, u16 command)
+{
+    struct disk_op_s dop;
+    dop.driveid = device;
+    dop.command = command;
+    int lba = legacy_lba(regs, get_global_seg(), &Drives.drives[device].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);
 
-    u32 ilba = GET_EBDA(cdemu.ilba);
-    // calculate the virtual lba inside the image
-    u32 vlba= (((((u32)cylinder*(u32)nlh)+(u32)head)*(u32)nlspt)
-               +((u32)(sector-1)));
-    // start lba on cd
-    u32 slba  = (u32)vlba/4;
-    u16 before= (u16)vlba%4;
-    // end lba on cd
-    u32 elba = (u32)(vlba+nbsectors-1)/4;
-    u32 count = elba-slba+1;
-    u32 lba = ilba+slba;
-
-    u16 segment = regs->es;
-    u16 offset  = regs->bx;
-
-    u8 atacmd[12];
-    memset(atacmd, 0, sizeof(atacmd));
-    atacmd[0]=0x28;                      // READ command
-    atacmd[7]=(count & 0xff00) >> 8;     // Sectors
-    atacmd[8]=(count & 0x00ff);          // Sectors
-    atacmd[2]=(lba & 0xff000000) >> 24;  // LBA
-    atacmd[3]=(lba & 0x00ff0000) >> 16;
-    atacmd[4]=(lba & 0x0000ff00) >> 8;
-    atacmd[5]=(lba & 0x000000ff);
-
-    u8 status = ata_cmd_packet(device, (u32)atacmd, sizeof(atacmd)
-                               , before*512, count*2048L
-                               , ATA_DATA_IN, segment, offset);
-
-    if (status != 0) {
-        BX_INFO("int13_harddisk: function %02x, error %02x !\n",regs->ah,status);
-        regs->al = 0;
+    regs->al = dop.count;
+
+    if (status) {
         disk_ret(regs, DISK_RET_EBADTRACK);
+        return;
     }
-    regs->al = nbsectors;
     disk_ret(regs, DISK_RET_SUCCESS);
 }
 
-static void
-extended_access(struct bregs *regs, u8 device, u16 command)
+// Perform cdemu read/verify
+void
+cdemu_access(struct bregs *regs, u8 device, u16 command)
 {
-    u16 count = GET_INT13EXT(regs, count);
-    u16 segment = GET_INT13EXT(regs, segment);
-    u16 offset = GET_INT13EXT(regs, offset);
-
-    // Can't use 64 bits lba
-    u32 lba = GET_INT13EXT(regs, lba2);
-    if (lba != 0L) {
-        BX_PANIC("int13_harddisk: function %02x. Can't use 64bits lba\n"
-                 , regs->ah);
-        disk_ret(regs, DISK_RET_EPARAM);
+    struct disk_op_s dop;
+    dop.driveid = device;
+    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;
     }
 
-    u8 type = GET_EBDA(ata.devices[device].type);
+    disk_ret(regs, DISK_RET_SUCCESS);
+    return;
+fail:
+    disk_ret(regs, DISK_RET_EBADTRACK);
+}
 
-    // Get 32 bits lba and check
-    lba = GET_INT13EXT(regs, lba1);
-    if (type == ATA_TYPE_ATA
-        && lba >= GET_EBDA(ata.devices[device].sectors)) {
-        BX_INFO("int13_harddisk: function %02x. LBA out of range\n", regs->ah);
+// Perform read/write/verify using new-style "int13ext" accesses.
+static void
+extended_access(struct bregs *regs, u8 device, u16 command)
+{
+    struct disk_op_s dop;
+    // Get lba and check.
+    dop.lba = GET_INT13EXT(regs, lba);
+    dop.command = command;
+    dop.driveid = device;
+    if (dop.lba >= GET_GLOBAL(Drives.drives[device].sectors)) {
+        dprintf(1, "int13_harddisk: function %02x. LBA out of range\n"
+                , regs->ah);
         disk_ret(regs, DISK_RET_EPARAM);
         return;
     }
 
-    u8 status;
-    switch (command) {
-    case ATA_CMD_READ_SECTORS:
-        if (type == ATA_TYPE_ATA) {
-            status = ata_cmd_data_in(device, ATA_CMD_READ_SECTORS
-                                     , count, 0, 0, 0
-                                     , lba, segment, offset);
-        } else {
-            u8 atacmd[12];
-            memset(atacmd, 0, sizeof(atacmd));
-            atacmd[0]=0x28;                      // READ command
-            atacmd[7]=(count & 0xff00) >> 8;     // Sectors
-            atacmd[8]=(count & 0x00ff);          // Sectors
-            atacmd[2]=(lba & 0xff000000) >> 24;  // LBA
-            atacmd[3]=(lba & 0x00ff0000) >> 16;
-            atacmd[4]=(lba & 0x0000ff00) >> 8;
-            atacmd[5]=(lba & 0x000000ff);
-
-            status = ata_cmd_packet(device, (u32)atacmd, sizeof(atacmd)
-                                    , 0, count*2048L
-                                    , ATA_DATA_IN, segment, offset);
-        }
-        break;
-    case ATA_CMD_WRITE_SECTORS:
-        status = ata_cmd_data_out(device, ATA_CMD_WRITE_SECTORS
-                                  , count, 0, 0, 0
-                                  , lba, segment, offset);
-        break;
-    default:
-        // If verify or seek
-        disk_ret(regs, DISK_RET_SUCCESS);
-        return;
-    }
+    u16 segment = GET_INT13EXT(regs, segment);
+    u16 offset = GET_INT13EXT(regs, offset);
+    dop.buf_fl = MAKE_FLATPTR(segment, offset);
+    dop.count = GET_INT13EXT(regs, count);
 
-    SET_INT13EXT(regs, count, GET_EBDA(ata.trsfsectors));
+    int status = send_disk_op(&dop);
 
-    if (status != 0) {
-        BX_INFO("int13_harddisk: function %02x, error %02x !\n"
-                , regs->ah, status);
+    SET_INT13EXT(regs, count, dop.count);
+
+    if (status) {
         disk_ret(regs, DISK_RET_EBADTRACK);
         return;
     }
@@ -262,36 +250,41 @@ extended_access(struct bregs *regs, u8 device, u16 command)
 static void
 disk_1300(struct bregs *regs, u8 device)
 {
-    ata_reset(device);
+    struct disk_op_s dop;
+    dop.driveid = device;
+    dop.command = CMD_RESET;
+    send_disk_op(&dop);
 }
 
 // read disk status
 static void
 disk_1301(struct bregs *regs, u8 device)
 {
-    regs->ah = GET_BDA(disk_last_status);
-    disk_ret(regs, DISK_RET_SUCCESS);
+    u8 v = GET_BDA(disk_last_status);
+    regs->ah = v;
+    set_cf(regs, v);
+    // XXX - clear disk_last_status?
 }
 
 // read disk sectors
 static void
 disk_1302(struct bregs *regs, u8 device)
 {
-    basic_access(regs, device, ATA_CMD_READ_SECTORS);
+    basic_access(regs, device, CMD_READ);
 }
 
 // write disk sectors
 static void
 disk_1303(struct bregs *regs, u8 device)
 {
-    basic_access(regs, device, ATA_CMD_WRITE_SECTORS);
+    basic_access(regs, device, CMD_WRITE);
 }
 
 // verify disk sectors
 static void
 disk_1304(struct bregs *regs, u8 device)
 {
-    basic_access(regs, device, 0);
+    basic_access(regs, device, CMD_VERIFY);
     // FIXME verify
 }
 
@@ -307,10 +300,10 @@ static void
 disk_1308(struct bregs *regs, u8 device)
 {
     // Get logical geometry from table
-    u16 nlc = GET_EBDA(ata.devices[device].lchs.cylinders);
-    u16 nlh = GET_EBDA(ata.devices[device].lchs.heads);
-    u16 nlspt = GET_EBDA(ata.devices[device].lchs.spt);
-    u16 count = GET_EBDA(ata.hdcount);
+    u16 nlc = GET_GLOBAL(Drives.drives[device].lchs.cylinders);
+    u16 nlh = GET_GLOBAL(Drives.drives[device].lchs.heads);
+    u16 nlspt = GET_GLOBAL(Drives.drives[device].lchs.spt);
+    u16 count = GET_BDA(hdcount);
 
     nlc = nlc - 2; /* 0 based , last sector not used */
     regs->al = 0;
@@ -350,12 +343,14 @@ disk_1310(struct bregs *regs, u8 device)
 {
     // should look at 40:8E also???
 
-    // Read the status from controller
-    u8 status = inb(GET_EBDA(ata.channels[device/2].iobase1) + ATA_CB_STAT);
-    if ( (status & ( ATA_CB_STAT_BSY | ATA_CB_STAT_RDY )) == ATA_CB_STAT_RDY )
-        disk_ret(regs, DISK_RET_SUCCESS);
-    else
+    struct disk_op_s dop;
+    dop.driveid = device;
+    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);
 }
 
 // recalibrate
@@ -377,16 +372,16 @@ static void
 disk_1315(struct bregs *regs, u8 device)
 {
     // Get logical geometry from table
-    u16 nlc   = GET_EBDA(ata.devices[device].lchs.cylinders);
-    u16 nlh   = GET_EBDA(ata.devices[device].lchs.heads);
-    u16 nlspt = GET_EBDA(ata.devices[device].lchs.spt);
+    u16 nlc   = GET_GLOBAL(Drives.drives[device].lchs.cylinders);
+    u16 nlh   = GET_GLOBAL(Drives.drives[device].lchs.heads);
+    u16 nlspt = GET_GLOBAL(Drives.drives[device].lchs.spt);
 
     // 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, 0);
+    disk_ret(regs, DISK_RET_SUCCESS);
     regs->ah = 3; // hard disk accessible
 }
 
@@ -404,21 +399,21 @@ disk_1341(struct bregs *regs, u8 device)
 static void
 disk_1342(struct bregs *regs, u8 device)
 {
-    extended_access(regs, device, ATA_CMD_READ_SECTORS);
+    extended_access(regs, device, CMD_READ);
 }
 
 // IBM/MS extended write
 static void
 disk_1343(struct bregs *regs, u8 device)
 {
-    extended_access(regs, device, ATA_CMD_WRITE_SECTORS);
+    extended_access(regs, device, CMD_WRITE);
 }
 
 // IBM/MS verify
 static void
 disk_1344(struct bregs *regs, u8 device)
 {
-    extended_access(regs, device, 0);
+    extended_access(regs, device, CMD_VERIFY);
 }
 
 // IBM/MS lock/unlock drive
@@ -441,7 +436,7 @@ disk_1346(struct bregs *regs, u8 device)
 static void
 disk_1347(struct bregs *regs, u8 device)
 {
-    extended_access(regs, device, 0);
+    extended_access(regs, device, CMD_SEEK);
 }
 
 // IBM/MS get drive parameters
@@ -451,23 +446,33 @@ disk_1348(struct bregs *regs, u8 device)
     u16 size = GET_INT13DPT(regs, size);
 
     // Buffer is too small
-    if (size < 0x1a) {
+    if (size < 26) {
         disk_ret(regs, DISK_RET_EPARAM);
         return;
     }
 
     // EDD 1.x
 
-    u8  type    = GET_EBDA(ata.devices[device].type);
-    u16 npc     = GET_EBDA(ata.devices[device].pchs.cylinders);
-    u16 nph     = GET_EBDA(ata.devices[device].pchs.heads);
-    u16 npspt   = GET_EBDA(ata.devices[device].pchs.spt);
-    u32 lba     = GET_EBDA(ata.devices[device].sectors);
-    u16 blksize = GET_EBDA(ata.devices[device].blksize);
+    u8  type    = GET_GLOBAL(Drives.drives[device].type);
+    u16 npc     = GET_GLOBAL(Drives.drives[device].pchs.cylinders);
+    u16 nph     = GET_GLOBAL(Drives.drives[device].pchs.heads);
+    u16 npspt   = GET_GLOBAL(Drives.drives[device].pchs.spt);
+    u64 lba     = GET_GLOBAL(Drives.drives[device].sectors);
+    u16 blksize = GET_GLOBAL(Drives.drives[device].blksize);
 
-    SET_INT13DPT(regs, size, 0x1a);
-    if (type == ATA_TYPE_ATA) {
-        if ((lba/npspt)/nph > 0x3fff) {
+    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);
+
+    SET_INT13DPT(regs, size, 26);
+    if (type == DTYPE_ATAPI) {
+        // 0x74 = removable, media change, lockable, max values
+        SET_INT13DPT(regs, infos, 0x74);
+        SET_INT13DPT(regs, cylinders, 0xffffffff);
+        SET_INT13DPT(regs, heads, 0xffffffff);
+        SET_INT13DPT(regs, spt, 0xffffffff);
+        SET_INT13DPT(regs, sector_count, (u64)-1);
+    } else {
+        if (lba > (u64)npspt*nph*0x3fff) {
             SET_INT13DPT(regs, infos, 0x00); // geometry is invalid
             SET_INT13DPT(regs, cylinders, 0x3fff);
         } else {
@@ -476,117 +481,104 @@ disk_1348(struct bregs *regs, u8 device)
         }
         SET_INT13DPT(regs, heads, (u32)nph);
         SET_INT13DPT(regs, spt, (u32)npspt);
-        SET_INT13DPT(regs, sector_count1, lba);  // FIXME should be Bit64
-        SET_INT13DPT(regs, sector_count2, 0L);
-    } else {
-        // ATAPI
-        // 0x74 = removable, media change, lockable, max values
-        SET_INT13DPT(regs, infos, 0x74);
-        SET_INT13DPT(regs, cylinders, 0xffffffff);
-        SET_INT13DPT(regs, heads, 0xffffffff);
-        SET_INT13DPT(regs, spt, 0xffffffff);
-        SET_INT13DPT(regs, sector_count1, 0xffffffff);  // FIXME should be Bit64
-        SET_INT13DPT(regs, sector_count2, 0xffffffff);
+        SET_INT13DPT(regs, sector_count, lba);
     }
     SET_INT13DPT(regs, blksize, blksize);
 
-    if (size < 0x1e) {
+    if (size < 30 || (type != DTYPE_ATA && type != DTYPE_ATAPI)) {
         disk_ret(regs, DISK_RET_SUCCESS);
         return;
     }
 
     // EDD 2.x
 
-    SET_INT13DPT(regs, size, 0x1e);
+    u16 ebda_seg = get_ebda_seg();
+    SET_INT13DPT(regs, size, 30);
 
-    SET_INT13DPT(regs, dpte_segment, EBDA_SEG);
+    SET_INT13DPT(regs, dpte_segment, ebda_seg);
     SET_INT13DPT(regs, dpte_offset
-                 , offsetof(struct extended_bios_data_area_s, ata.dpte));
+                 , offsetof(struct extended_bios_data_area_s, dpte));
 
     // Fill in dpte
-    u8 channel = device / 2;
-    u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
-    u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
-    u8 irq = GET_EBDA(ata.channels[channel].irq);
-    u8 mode = GET_EBDA(ata.devices[device].mode);
-
-    u16 options;
-    if (type == ATA_TYPE_ATA) {
-        u8 translation = GET_EBDA(ata.devices[device].translation);
-        options  = (translation==ATA_TRANSLATION_NONE?0:1)<<3; // chs translation
-        options |= (translation==ATA_TRANSLATION_LBA?1:0)<<9;
-        options |= (translation==ATA_TRANSLATION_RECHS?3:0)<<9;
+    u8 ataid = GET_GLOBAL(Drives.drives[device].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[device].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
-    options |= (mode==ATA_MODE_PIO32?1:0)<<7;
-
-    SET_EBDA(ata.dpte.iobase1, iobase1);
-    SET_EBDA(ata.dpte.iobase2, iobase2 + ATA_CB_DC);
-    SET_EBDA(ata.dpte.prefix, (0xe | (device % 2))<<4 );
-    SET_EBDA(ata.dpte.unused, 0xcb );
-    SET_EBDA(ata.dpte.irq, irq );
-    SET_EBDA(ata.dpte.blkcount, 1 );
-    SET_EBDA(ata.dpte.dma, 0 );
-    SET_EBDA(ata.dpte.pio, 0 );
-    SET_EBDA(ata.dpte.options, options);
-    SET_EBDA(ata.dpte.reserved, 0);
-    if (size >= 0x42)
-        SET_EBDA(ata.dpte.revision, 0x11);
-    else
-        SET_EBDA(ata.dpte.revision, 0x10);
-
-    u8 sum = checksum_seg(EBDA_SEG
-                          , offsetof(struct extended_bios_data_area_s, ata.dpte)
-                          , 15);
-    SET_EBDA(ata.dpte.checksum, ~sum);
-
-    if (size < 0x42) {
+        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);
+
+    if (size < 66) {
         disk_ret(regs, DISK_RET_SUCCESS);
         return;
     }
 
     // EDD 3.x
-    channel = device / 2;
-    u8 iface = GET_EBDA(ata.channels[channel].iface);
-    iobase1 = GET_EBDA(ata.channels[channel].iobase1);
-
-    SET_INT13DPT(regs, size, 0x42);
     SET_INT13DPT(regs, key, 0xbedd);
-    SET_INT13DPT(regs, dpi_length, 0x24);
+    SET_INT13DPT(regs, dpi_length, 36);
     SET_INT13DPT(regs, reserved1, 0);
     SET_INT13DPT(regs, reserved2, 0);
 
-    if (iface==ATA_IFACE_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], 0);
-    } else {
-        // FIXME PCI
-    }
+    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);
+
+    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);
+
     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 (iface==ATA_IFACE_ISA) {
-        SET_INT13DPT(regs, iface_path[0], iobase1);
-        SET_INT13DPT(regs, iface_path[2], 0);
-        SET_INT13DPT(regs, iface_path[4], 0L);
-    } else {
-        // FIXME PCI
-    }
-    SET_INT13DPT(regs, device_path[0], device%2);
-    SET_INT13DPT(regs, device_path[1], 0);
-    SET_INT13DPT(regs, device_path[2], 0);
-    SET_INT13DPT(regs, device_path[4], 0L);
+    SET_INT13DPT(regs, device_path, slave);
 
-    sum = checksum_seg(regs->ds, 30, 34);
-    SET_INT13DPT(regs, checksum, ~sum);
+    SET_INT13DPT(regs, checksum
+                 , -checksum_far(regs->ds, (void*)(regs->si+30), 35));
+
+    disk_ret(regs, DISK_RET_SUCCESS);
 }
 
 // IBM/MS extended media change
@@ -624,7 +616,6 @@ disk_134e06(struct bregs *regs, u8 device)
 static void
 disk_134eXX(struct bregs *regs, u8 device)
 {
-    debug_stub(regs);
     disk_ret(regs, DISK_RET_EPARAM);
 }
 
@@ -641,14 +632,13 @@ disk_134e(struct bregs *regs, u8 device)
     }
 }
 
-static void
+void
 disk_13XX(struct bregs *regs, u8 device)
 {
-    debug_stub(regs);
     disk_ret(regs, DISK_RET_EPARAM);
 }
 
-static void
+void
 disk_13(struct bregs *regs, u8 device)
 {
     //debug_stub(regs);
@@ -686,292 +676,29 @@ disk_13(struct bregs *regs, u8 device)
 }
 
 
-/****************************************************************
- * CDROM functions
- ****************************************************************/
-
-// read disk drive size
-static void
-cdrom_1315(struct bregs *regs, u8 device)
-{
-    disk_ret(regs, DISK_RET_EADDRNOTFOUND);
-}
-
-// lock
-static void
-cdrom_134500(struct bregs *regs, u8 device)
-{
-    u8 locks = GET_EBDA(ata.devices[device].lock);
-    if (locks == 0xff) {
-        regs->al = 1;
-        disk_ret(regs, DISK_RET_ETOOMANYLOCKS);
-        return;
-    }
-    SET_EBDA(ata.devices[device].lock, locks + 1);
-    regs->al = 1;
-    disk_ret(regs, DISK_RET_SUCCESS);
-}
-
-// unlock
-static void
-cdrom_134501(struct bregs *regs, u8 device)
-{
-    u8 locks = GET_EBDA(ata.devices[device].lock);
-    if (locks == 0x00) {
-        regs->al = 0;
-        disk_ret(regs, DISK_RET_ENOTLOCKED);
-        return;
-    }
-    locks--;
-    SET_EBDA(ata.devices[device].lock, locks);
-    regs->al = (locks ? 1 : 0);
-    disk_ret(regs, DISK_RET_SUCCESS);
-}
-
-// status
-static void
-cdrom_134502(struct bregs *regs, u8 device)
-{
-    u8 locks = GET_EBDA(ata.devices[device].lock);
-    regs->al = (locks ? 1 : 0);
-    disk_ret(regs, DISK_RET_SUCCESS);
-}
-
-static void
-cdrom_1345XX(struct bregs *regs, u8 device)
-{
-    disk_ret(regs, DISK_RET_EPARAM);
-}
-
-// IBM/MS lock/unlock drive
-static void
-cdrom_1345(struct bregs *regs, u8 device)
-{
-    switch (regs->al) {
-    case 0x00: cdrom_134500(regs, device); break;
-    case 0x01: cdrom_134501(regs, device); break;
-    case 0x02: cdrom_134502(regs, device); break;
-    default:   cdrom_1345XX(regs, device); break;
-    }
-}
-
-// IBM/MS eject media
-static void
-cdrom_1346(struct bregs *regs, u8 device)
-{
-    u8 locks = GET_EBDA(ata.devices[device].lock);
-    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;
-    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 media change
-static void
-cdrom_1349(struct bregs *regs, u8 device)
-{
-    // always send changed ??
-    regs->ah = DISK_RET_ECHANGED;
-    set_cf(regs, 1);
-}
-
-static void
-cdrom_ok(struct bregs *regs, u8 device)
-{
-    disk_ret(regs, DISK_RET_SUCCESS);
-}
-
-static void
-cdrom_wp(struct bregs *regs, u8 device)
-{
-    disk_ret(regs, DISK_RET_EWRITEPROTECT);
-}
-
-static void
-cdrom_13(struct bregs *regs, u8 device)
-{
-    //debug_stub(regs);
-
-    switch (regs->ah) {
-    case 0x15: cdrom_1315(regs, device); break;
-    case 0x45: cdrom_1345(regs, device); break;
-    case 0x46: cdrom_1346(regs, device); break;
-    case 0x49: cdrom_1349(regs, device); break;
-
-    // These functions are the same as for hard disks
-    case 0x01: disk_1301(regs, device); break;
-    case 0x41: disk_1341(regs, device); break;
-    case 0x42: disk_1342(regs, device); break;
-    case 0x44: disk_1344(regs, device); break;
-    case 0x47: disk_1347(regs, device); break;
-    case 0x48: disk_1348(regs, device); break;
-    case 0x4e: disk_134e(regs, device); break;
-
-    // all these functions return SUCCESS
-    case 0x00: cdrom_ok(regs, device); break; // disk controller reset
-    case 0x09: cdrom_ok(regs, device); break; // initialize drive parameters
-    case 0x0c: cdrom_ok(regs, device); break; // seek to specified cylinder
-    case 0x0d: cdrom_ok(regs, device); break; // alternate disk reset
-    case 0x10: cdrom_ok(regs, device); break; // check drive ready
-    case 0x11: cdrom_ok(regs, device); break; // recalibrate
-    case 0x14: cdrom_ok(regs, device); break; // controller internal diagnostic
-    case 0x16: cdrom_ok(regs, device); break; // detect disk change
-
-    // all these functions return disk write-protected
-    case 0x03: cdrom_wp(regs, device); break; // write disk sectors
-    case 0x05: cdrom_wp(regs, device); break; // format disk track
-    case 0x43: cdrom_wp(regs, device); break; // IBM/MS extended write
-
-    default:   disk_13XX(regs, device); break;
-    }
-}
-
-
-/****************************************************************
- * CD emulation
- ****************************************************************/
-
-// read disk sectors
-static void
-cdemu_1302(struct bregs *regs, u8 device)
-{
-    emu_access(regs, device, ATA_CMD_READ_SECTORS);
-}
-
-// verify disk sectors
-static void
-cdemu_1304(struct bregs *regs, u8 device)
-{
-    emu_access(regs, device, 0);
-}
-
-// read disk drive parameters
-static void
-cdemu_1308(struct bregs *regs, u8 device)
-{
-    u16 nlc   = GET_EBDA(cdemu.vdevice.cylinders) - 1;
-    u16 nlh   = GET_EBDA(cdemu.vdevice.heads) - 1;
-    u16 nlspt = GET_EBDA(cdemu.vdevice.spt);
-
-    regs->al = 0x00;
-    regs->bl = 0x00;
-    regs->ch = nlc & 0xff;
-    regs->cl = ((nlc >> 2) & 0xc0) | (nlspt  & 0x3f);
-    regs->dh = nlh;
-    // FIXME ElTorito Various. should send the real count of drives 1 or 2
-    // FIXME ElTorito Harddisk. should send the HD count
-    regs->dl = 0x02;
-    u8 media = GET_EBDA(cdemu.media);
-    if (media <= 3)
-        regs->bl = media * 2;
-
-    regs->es = SEG_BIOS;
-    regs->di = (u16)&diskette_param_table2;
-
-    disk_ret(regs, DISK_RET_SUCCESS);
-}
-
-static void
-cdemu_13(struct bregs *regs)
-{
-    //debug_stub(regs);
-
-    u8 device  = GET_EBDA(cdemu.controller_index) * 2;
-    device += GET_EBDA(cdemu.device_spec);
-
-    switch (regs->ah) {
-    case 0x02: cdemu_1302(regs, device); break;
-    case 0x04: cdemu_1304(regs, device); break;
-    case 0x08: cdemu_1308(regs, device); break;
-    // XXX - All other calls get passed to standard CDROM functions.
-    default: cdrom_13(regs, device); break;
-    }
-}
-
-struct eltorito_s {
-    u8 size;
-    u8 media;
-    u8 emulated_drive;
-    u8 controller_index;
-    u32 ilba;
-    u16 device_spec;
-    u16 buffer_segment;
-    u16 load_segment;
-    u16 sector_count;
-    u8 cylinders;
-    u8 sectors;
-    u8 heads;
-};
-
-#define SET_INT13ET(regs,var,val)                                      \
-    SET_FARVAR((regs)->ds, ((struct eltorito_s*)((regs)->si+0))->var, (val))
-
-// ElTorito - Terminate disk emu
-static void
-cdemu_134b(struct bregs *regs)
-{
-    // FIXME ElTorito Hardcoded
-    SET_INT13ET(regs, size, 0x13);
-    SET_INT13ET(regs, media, GET_EBDA(cdemu.media));
-    SET_INT13ET(regs, emulated_drive, GET_EBDA(cdemu.emulated_drive));
-    SET_INT13ET(regs, controller_index, GET_EBDA(cdemu.controller_index));
-    SET_INT13ET(regs, ilba, GET_EBDA(cdemu.ilba));
-    SET_INT13ET(regs, device_spec, GET_EBDA(cdemu.device_spec));
-    SET_INT13ET(regs, buffer_segment, GET_EBDA(cdemu.buffer_segment));
-    SET_INT13ET(regs, load_segment, GET_EBDA(cdemu.load_segment));
-    SET_INT13ET(regs, sector_count, GET_EBDA(cdemu.sector_count));
-    SET_INT13ET(regs, cylinders, GET_EBDA(cdemu.vdevice.cylinders));
-    SET_INT13ET(regs, sectors, GET_EBDA(cdemu.vdevice.spt));
-    SET_INT13ET(regs, heads, GET_EBDA(cdemu.vdevice.heads));
-
-    // If we have to terminate emulation
-    if (regs->al == 0x00) {
-        // FIXME ElTorito Various. Should be handled accordingly to spec
-        SET_EBDA(cdemu.active, 0x00); // bye bye
-    }
-
-    disk_ret(regs, DISK_RET_SUCCESS);
-}
-
-
 /****************************************************************
  * Entry points
  ****************************************************************/
 
-static u8
-get_device(struct bregs *regs, u8 drive)
+static int
+get_device(struct bregs *regs, u8 iscd, u8 drive)
 {
     // basic check : device has to be defined
-    if (drive >= CONFIG_MAX_ATA_DEVICES) {
+    if (drive >= ARRAY_SIZE(Drives.idmap[0])) {
         disk_ret(regs, DISK_RET_EPARAM);
-        return CONFIG_MAX_ATA_DEVICES;
+        return -1;
     }
 
     // Get the ata channel
-    u8 device = GET_EBDA(ata.hdidmap[drive]);
+    u8 driveid = GET_GLOBAL(Drives.idmap[iscd][drive]);
 
     // basic check : device has to be valid
-    if (device >= CONFIG_MAX_ATA_DEVICES) {
+    if (driveid >= ARRAY_SIZE(Drives.drives)) {
         disk_ret(regs, DISK_RET_EPARAM);
-        return CONFIG_MAX_ATA_DEVICES;
+        return -1;
     }
 
-    return device;
+    return driveid;
 }
 
 static void
@@ -982,64 +709,67 @@ handle_legacy_disk(struct bregs *regs, u8 drive)
         return;
     }
 
-    if (! CONFIG_ATA) {
+    if (! CONFIG_DRIVES) {
         // XXX - old code had other disk access method.
         disk_ret(regs, DISK_RET_EPARAM);
         return;
     }
 
     if (drive >= 0xe0) {
-        u8 device = get_device(regs, drive - 0xe0);
-        if (device >= CONFIG_MAX_ATA_DEVICES)
+        int driveid = get_device(regs, 1, drive - 0xe0);
+        if (driveid < 0)
             return;
-        cdrom_13(regs, device);
+        cdrom_13(regs, driveid);
         return;
     }
 
-    u8 device = get_device(regs, drive - 0x80);
-    if (device >= CONFIG_MAX_ATA_DEVICES)
+    int driveid = get_device(regs, 0, drive - 0x80);
+    if (driveid < 0)
         return;
-    disk_13(regs, device);
+    disk_13(regs, driveid);
 }
 
-void VISIBLE
+void VISIBLE16
 handle_40(struct bregs *regs)
 {
-    debug_enter(regs);
+    debug_enter(regs, DEBUG_HDL_40);
     handle_legacy_disk(regs, regs->dl);
-    debug_exit(regs);
 }
 
 // INT 13h Fixed Disk Services Entry Point
-void VISIBLE
+void VISIBLE16
 handle_13(struct bregs *regs)
 {
-    debug_enter(regs);
+    debug_enter(regs, DEBUG_HDL_13);
     u8 drive = regs->dl;
 
-    if (CONFIG_ELTORITO_BOOT) {
+    if (CONFIG_CDROM_EMU) {
         if (regs->ah == 0x4b) {
             cdemu_134b(regs);
-            goto done;
+            return;
         }
-        if (GET_EBDA(cdemu.active)) {
-            if (drive == GET_EBDA(cdemu.emulated_drive)) {
+        u16 ebda_seg = get_ebda_seg();
+        if (GET_EBDA2(ebda_seg, cdemu.active)) {
+            u8 emudrive = GET_EBDA2(ebda_seg, cdemu.emulated_drive);
+            if (drive == emudrive) {
                 cdemu_13(regs);
-                goto done;
+                return;
             }
-            drive--;
+            if (drive < 0xe0 && ((emudrive ^ drive) & 0x80) == 0)
+                drive--;
         }
     }
     handle_legacy_disk(regs, drive);
-done:
-    debug_exit(regs);
 }
 
 // record completion in BIOS task complete flag
-void VISIBLE
-handle_76(struct bregs *regs)
+void VISIBLE16
+handle_76()
 {
-    debug_isr(regs);
-    SET_BDA(floppy_harddisk_info, 0xff);
-    eoi_both_pics();
+    debug_isr(DEBUG_ISR_76);
+    SET_BDA(disk_interrupt_flag, 0xff);
+    eoi_pic2();
 }
+
+// Old Fixed Disk Parameter Table (newer tables are in the ebda).
+struct fdpt_s OldFDPT VAR16FIXED(0xe401);