Change license from GPLv3 to LGPLv3.
[seabios.git] / src / disk.c
index ee2c983c953ae99d1987788104ee6ed37c85e914..d5c7df2783198e25f755e6185f8a9e65d9cb4f37 100644 (file)
@@ -3,16 +3,16 @@
 // 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 "util.h" // debug_enter
-#include "ata.h" // ATA_*
-
-#define DEBUGF1(fmt, args...) bprintf(0, fmt , ##args)
-#define DEBUGF(fmt, args...)
+#include "pic.h" // eoi_pic2
+#include "bregs.h" // struct bregs
+#include "pci.h" // pci_bdf_to_bus
+#include "atabits.h" // ATA_CB_STAT
 
 
 /****************************************************************
  ****************************************************************/
 
 void
-__disk_ret(const char *fname, struct bregs *regs, u8 code)
+__disk_ret(struct bregs *regs, u32 linecode, const char *fname)
 {
+    u8 code = linecode;
     SET_BDA(disk_last_status, code);
     if (code)
-        __set_code_fail(fname, regs, code);
+        __set_code_fail(regs, linecode, fname);
     else
         set_code_success(regs);
 }
 
 static void
-__disk_stub(const char *fname, struct bregs *regs)
+__disk_stub(struct bregs *regs, int lineno, const char *fname)
 {
-    __debug_stub(fname, regs);
-    __disk_ret(fname, regs, DISK_RET_SUCCESS);
+    __debug_stub(regs, lineno, fname);
+    __disk_ret(regs, DISK_RET_SUCCESS | (lineno << 8), fname);
 }
 
-#define DISK_STUB(regs) \
-    __disk_stub(__func__, (regs))
+#define DISK_STUB(regs)                         \
+    __disk_stub((regs), __LINE__, __func__)
+
+static int
+__send_disk_op(struct disk_op_s *op_p, u16 op_s)
+{
+    struct disk_op_s dop;
+    memcpy_far(MAKE_FARPTR(GET_SEG(SS), &dop)
+               , MAKE_FARPTR(op_s, op_p)
+               , 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.far_buffer
+            , dop.count, dop.command);
+
+    irq_enable();
+
+    int status;
+    if (dop.command == CMD_CDEMU_READ)
+        status = cdrom_read_512(&dop);
+    else if (dop.command == CMD_CDROM_READ)
+        status = cdrom_read(&dop);
+    else
+        status = ata_cmd_data(&dop);
+
+    irq_disable();
+
+    return status;
+}
+
+static int
+send_disk_op(struct disk_op_s *op)
+{
+    return stack_hop((u32)op, GET_SEG(SS), 0, __send_disk_op);
+}
 
 static void
 basic_access(struct bregs *regs, u8 device, u16 command)
 {
-    u8 type = GET_EBDA(ata.devices[device].type);
+    struct disk_op_s dop;
+    dop.lba = 0;
+    dop.driveid = device;
+    u8 type = GET_GLOBAL(ATA.devices[device].type);
     u16 nlc, nlh, nlspt;
     if (type == ATA_TYPE_ATA) {
-        nlc   = GET_EBDA(ata.devices[device].lchs.cylinders);
-        nlh   = GET_EBDA(ata.devices[device].lchs.heads);
-        nlspt = GET_EBDA(ata.devices[device].lchs.spt);
+        nlc   = GET_GLOBAL(ATA.devices[device].lchs.cylinders);
+        nlh   = GET_GLOBAL(ATA.devices[device].lchs.heads);
+        nlspt = GET_GLOBAL(ATA.devices[device].lchs.spt);
+        dop.command = command;
     } else {
         // Must be cd emulation.
-        nlc   = GET_EBDA(cdemu.vdevice.cylinders);
-        nlh   = GET_EBDA(cdemu.vdevice.heads);
-        nlspt = GET_EBDA(cdemu.vdevice.spt);
+        u16 ebda_seg = get_ebda_seg();
+        nlc   = GET_EBDA2(ebda_seg, cdemu.cylinders);
+        nlh   = GET_EBDA2(ebda_seg, cdemu.heads);
+        nlspt = GET_EBDA2(ebda_seg, cdemu.spt);
+        dop.lba = GET_EBDA2(ebda_seg, cdemu.ilba) * 4;
+        dop.command = CMD_CDEMU_READ;
     }
 
-    u16 count       = regs->al;
+    dop.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) {
+    if (dop.count > 128 || dop.count == 0 || sector == 0) {
         dprintf(1, "int13_harddisk: function %02x, parameter out of range!\n"
                 , regs->ah);
         disk_ret(regs, DISK_RET_EPARAM);
@@ -83,30 +124,23 @@ basic_access(struct bregs *regs, u8 device, u16 command)
     }
 
     // translate lchs to lba
-    u32 lba = (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nlspt)
-               + (u32)sector - 1);
+    dop.lba += (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nlspt)
+                + (u32)sector - 1);
 
     u16 segment = regs->es;
     u16 offset  = regs->bx;
-    void *far_buffer = MAKE_FARPTR(segment, offset);
-
-    irq_enable();
-
-    int status;
-    if (type == ATA_TYPE_ATA)
-        status = ata_cmd_data(device, command, lba, count, far_buffer);
-    else
-        status = cdrom_read_emu(device, lba, count, far_buffer);
+    dop.far_buffer = MAKE_FARPTR(segment, offset);
 
-    irq_disable();
+    int status = send_disk_op(&dop);
 
     // Set nb of sector transferred
-    regs->al = GET_EBDA(ata.trsfsectors);
+    regs->al = GET_EBDA(sector_count);
 
     if (status != 0) {
-        dprintf(1, "int13_harddisk: function %02x, error %02x !\n"
+        dprintf(1, "int13_harddisk: function %02x, error %d!\n"
                 , regs->ah, status);
         disk_ret(regs, DISK_RET_EBADTRACK);
+        return;
     }
     disk_ret(regs, DISK_RET_SUCCESS);
 }
@@ -114,15 +148,21 @@ basic_access(struct bregs *regs, u8 device, u16 command)
 static void
 extended_access(struct bregs *regs, u8 device, u16 command)
 {
+    struct disk_op_s dop;
     // Get lba and check.
-    u64 lba = GET_INT13EXT(regs, lba);
-    u8 type = GET_EBDA(ata.devices[device].type);
-    if (type == ATA_TYPE_ATA
-        && lba >= GET_EBDA(ata.devices[device].sectors)) {
-        dprintf(1, "int13_harddisk: function %02x. LBA out of range\n"
-                , regs->ah);
-        disk_ret(regs, DISK_RET_EPARAM);
-        return;
+    dop.lba = GET_INT13EXT(regs, lba);
+    dop.command = command;
+    dop.driveid = device;
+    u8 type = GET_GLOBAL(ATA.devices[device].type);
+    if (type == ATA_TYPE_ATA) {
+        if (dop.lba >= GET_GLOBAL(ATA.devices[device].sectors)) {
+            dprintf(1, "int13_harddisk: function %02x. LBA out of range\n"
+                    , regs->ah);
+            disk_ret(regs, DISK_RET_EPARAM);
+            return;
+        }
+    } else {
+        dop.command = CMD_CDROM_READ;
     }
 
     if (!command) {
@@ -133,23 +173,15 @@ extended_access(struct bregs *regs, u8 device, u16 command)
 
     u16 segment = GET_INT13EXT(regs, segment);
     u16 offset = GET_INT13EXT(regs, offset);
-    void *far_buffer = MAKE_FARPTR(segment, offset);
-    u16 count = GET_INT13EXT(regs, count);
-
-    irq_enable();
-
-    u8 status;
-    if (type == ATA_TYPE_ATA)
-        status = ata_cmd_data(device, command, lba, count, far_buffer);
-    else
-        status = cdrom_read(device, lba, count, far_buffer);
+    dop.far_buffer = MAKE_FARPTR(segment, offset);
+    dop.count = GET_INT13EXT(regs, count);
 
-    irq_disable();
+    int status = send_disk_op(&dop);
 
-    SET_INT13EXT(regs, count, GET_EBDA(ata.trsfsectors));
+    SET_INT13EXT(regs, count, GET_EBDA(sector_count));
 
     if (status != 0) {
-        dprintf(1, "int13_harddisk: function %02x, error %02x !\n"
+        dprintf(1, "int13_harddisk: function %02x, error %d!\n"
                 , regs->ah, status);
         disk_ret(regs, DISK_RET_EBADTRACK);
         return;
@@ -213,10 +245,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(ATA.devices[device].lchs.cylinders);
+    u16 nlh = GET_GLOBAL(ATA.devices[device].lchs.heads);
+    u16 nlspt = GET_GLOBAL(ATA.devices[device].lchs.spt);
+    u16 count = GET_BDA(hdcount);
 
     nlc = nlc - 2; /* 0 based , last sector not used */
     regs->al = 0;
@@ -257,7 +289,7 @@ 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);
+    u8 status = inb(GET_GLOBAL(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
@@ -283,9 +315,9 @@ 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(ATA.devices[device].lchs.cylinders);
+    u16 nlh   = GET_GLOBAL(ATA.devices[device].lchs.heads);
+    u16 nlspt = GET_GLOBAL(ATA.devices[device].lchs.spt);
 
     // Compute sector count seen by int13
     u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
@@ -357,21 +389,24 @@ 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);
-    u64 lba     = GET_EBDA(ata.devices[device].sectors);
-    u16 blksize = GET_EBDA(ata.devices[device].blksize);
+    u8  type    = GET_GLOBAL(ATA.devices[device].type);
+    u16 npc     = GET_GLOBAL(ATA.devices[device].pchs.cylinders);
+    u16 nph     = GET_GLOBAL(ATA.devices[device].pchs.heads);
+    u16 npspt   = GET_GLOBAL(ATA.devices[device].pchs.spt);
+    u64 lba     = GET_GLOBAL(ATA.devices[device].sectors);
+    u16 blksize = GET_GLOBAL(ATA.devices[device].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);
 
-    SET_INT13DPT(regs, size, 0x1a);
+    SET_INT13DPT(regs, size, 26);
     if (type == ATA_TYPE_ATA) {
         if (lba > (u64)npspt*nph*0x3fff) {
             SET_INT13DPT(regs, infos, 0x00); // geometry is invalid
@@ -394,103 +429,99 @@ disk_1348(struct bregs *regs, u8 device)
     }
     SET_INT13DPT(regs, blksize, blksize);
 
-    if (size < 0x1e) {
+    if (size < 30) {
         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, SEG_EBDA);
+    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);
+    u8 slave = device % 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);
+    u8 mode = GET_GLOBAL(ATA.devices[device].mode);
 
-    u16 options;
+    u16 options = 0;
     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 translation = GET_GLOBAL(ATA.devices[device].translation);
+        if (translation != ATA_TRANSLATION_NONE) {
+            options |= 1<<3; // CHS translation
+            if (translation == ATA_TRANSLATION_LBA)
+                options |= 1<<9;
+            if (translation == ATA_TRANSLATION_RECHS)
+                options |= 3<<9;
+        }
     } else {
         // ATAPI
-        options  = (1<<5); // removable device
-        options |= (1<<6); // atapi device
+        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 *p = MAKE_FARPTR(SEG_EBDA
-                        , offsetof(struct extended_bios_data_area_s, ata.dpte));
-    u8 sum = checksum(p, 15);
-    SET_EBDA(ata.dpte.checksum, ~sum);
-
-    if (size < 0x42) {
+    options |= 1<<4; // lba translation
+    if (mode == ATA_MODE_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 *p = MAKE_FARPTR(ebda_seg
+                        , offsetof(struct extended_bios_data_area_s, dpte));
+    SET_EBDA2(ebda_seg, dpte.checksum, -checksum(p, 15));
+
+    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);
+
+    SET_INT13DPT(regs, checksum, -checksum(MAKE_FARPTR(regs->ds, 30), 35));
 
-    sum = checksum(MAKE_FARPTR(regs->ds, 30), 34);
-    SET_INT13DPT(regs, checksum, ~sum);
+    disk_ret(regs, DISK_RET_SUCCESS);
 }
 
 // IBM/MS extended media change
@@ -602,7 +633,7 @@ get_device(struct bregs *regs, u8 iscd, u8 drive)
     }
 
     // Get the ata channel
-    u8 device = GET_EBDA(ata.idmap[iscd][drive]);
+    u8 device = GET_GLOBAL(ATA.idmap[iscd][drive]);
 
     // basic check : device has to be valid
     if (device >= CONFIG_MAX_ATA_DEVICES) {
@@ -644,7 +675,7 @@ handle_legacy_disk(struct bregs *regs, u8 drive)
 void VISIBLE16
 handle_40(struct bregs *regs)
 {
-    debug_enter(regs);
+    debug_enter(regs, DEBUG_HDL_40);
     handle_legacy_disk(regs, regs->dl);
 }
 
@@ -652,7 +683,7 @@ handle_40(struct bregs *regs)
 void VISIBLE16
 handle_13(struct bregs *regs)
 {
-    //debug_enter(regs);
+    debug_enter(regs, DEBUG_HDL_13);
     u8 drive = regs->dl;
 
     if (CONFIG_CDROM_EMU) {
@@ -660,8 +691,9 @@ handle_13(struct bregs *regs)
             cdemu_134b(regs);
             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)) {
+            if (drive == GET_EBDA2(ebda_seg, cdemu.emulated_drive)) {
                 cdemu_13(regs);
                 return;
             }
@@ -676,7 +708,7 @@ handle_13(struct bregs *regs)
 void VISIBLE16
 handle_76()
 {
-    debug_isr();
+    debug_isr(DEBUG_ISR_76);
     SET_BDA(disk_interrupt_flag, 0xff);
-    eoi_both_pics();
+    eoi_pic2();
 }