Change license from GPLv3 to LGPLv3.
[seabios.git] / src / floppy.c
index c27ee87a5c4435f8a21a40541819393eafa36b49..6214acba21ea2d083bb646b65fd476e7342915b3 100644 (file)
@@ -3,17 +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 "types.h" // u8
 #include "disk.h" // DISK_RET_SUCCESS
 #include "config.h" // CONFIG_FLOPPY_SUPPORT
-#include "biosvar.h" // struct bregs
+#include "biosvar.h" // SET_BDA
 #include "util.h" // irq_disable
 #include "cmos.h" // inb_cmos
-
-#define DEBUGF1(fmt, args...) bprintf(0, fmt , ##args)
-#define DEBUGF(fmt, args...)
+#include "pic.h" // eoi_pic1
+#include "bregs.h" // struct bregs
 
 #define BX_FLOPPY_ON_CNT 37   /* 2 seconds */
 
@@ -21,7 +20,7 @@
 // Since no provisions are made for multiple drive types, most
 // values in this table are ignored.  I set parameters for 1.44M
 // floppy here
-struct floppy_ext_dbt_s diskette_param_table2 VISIBLE16 = {
+struct floppy_ext_dbt_s diskette_param_table2 VAR16_32 = {
     .dbt = {
         .specify1       = 0xAF,
         .specify2       = 0x02, // head load time 0000001, DMA used
@@ -40,13 +39,41 @@ struct floppy_ext_dbt_s diskette_param_table2 VISIBLE16 = {
     .drive_type     = 4,    // drive type in cmos
 };
 
-// Oddities:
-//   Return codes vary greatly - AL not cleared consistenlty, BDA return
-//      status not set consistently, sometimes panics.
-//   Extra outb(0x000a, 0x02) in read?
-//   Does not disable interrupts on failure paths.
-//   numfloppies used before set in int_1308
-//   int_1305 verifies track but doesn't use it?
+void
+floppy_drive_setup()
+{
+    if (! CONFIG_FLOPPY_SUPPORT)
+        return;
+    dprintf(3, "init floppy drives\n");
+    if (CONFIG_COREBOOT)
+        // XXX - disable floppies on coreboot for now.
+        outb_cmos(0, CMOS_FLOPPY_DRIVE_TYPE);
+    u8 type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
+    u8 out = 0;
+    u8 num_floppies = 0;
+
+    if (type & 0xf0) {
+        out |= 0x07;
+        num_floppies++;
+    }
+    if (type & 0x0f) {
+        out |= 0x70;
+        num_floppies++;
+    }
+    SET_BDA(floppy_harddisk_info, out);
+
+    // Update equipment word bits for floppy
+    if (num_floppies == 1)
+        // 1 drive, ready for boot
+        SETBITS_BDA(equipment_list_flags, 0x01);
+    else if (num_floppies == 2)
+        // 2 drives, ready for boot
+        SETBITS_BDA(equipment_list_flags, 0x41);
+
+    outb(0x02, PORT_DMA1_MASK_REG);
+
+    enable_hwirq(6, entry_0e);
+}
 
 static inline void
 set_diskette_current_cyl(u8 drive, u8 cyl)
@@ -130,7 +157,7 @@ floppy_prepare_controller(u8 drive)
     }
 }
 
-static u8
+static int
 floppy_pio(u8 *cmd, u8 cmdlen)
 {
     floppy_prepare_controller(cmd[1] & 1);
@@ -146,7 +173,7 @@ floppy_pio(u8 *cmd, u8 cmdlen)
         if (!GET_BDA(floppy_motor_counter)) {
             irq_disable();
             floppy_reset_controller();
-            return DISK_RET_ETIMEOUT;
+            return -1;
         }
         v = GET_BDA(floppy_recalibration_status);
         if (v & FRS_TIMEOUT)
@@ -161,24 +188,32 @@ floppy_pio(u8 *cmd, u8 cmdlen)
     return 0;
 }
 
-static u8
+#define floppy_ret(regs, code)                                  \
+    __floppy_ret((regs), (code) | (__LINE__ << 8), __func__)
+
+void
+__floppy_ret(struct bregs *regs, u32 linecode, const char *fname)
+{
+    u8 code = linecode;
+    SET_BDA(floppy_last_status, code);
+    if (code)
+        __set_code_fail(regs, linecode, fname);
+    else
+        set_code_success(regs);
+}
+
+static int
 floppy_cmd(struct bregs *regs, u16 count, u8 *cmd, u8 cmdlen)
 {
     // es:bx = pointer to where to place information from diskette
-    // port 04: DMA-1 base and current address, channel 2
-    // port 05: DMA-1 base and current count, channel 2
-    u16 page = regs->es >> 12;   // upper 4 bits
-    u16 base_es = regs->es << 4; // lower 16bits contributed by ES
-    u16 base_address = base_es + regs->bx; // lower 16 bits of address
-    // contributed by ES:BX
-    if (base_address < base_es)
-        // in case of carry, adjust page by 1
-        page++;
+    u32 addr = (u32)MAKE_FARPTR(regs->es, regs->bx);
 
     // check for 64K boundary overrun
-    u16 last_addr = base_address + count;
-    if (last_addr < base_address)
-        return DISK_RET_EBOUNDARY;
+    u32 last_addr = addr + count;
+    if ((addr >> 16) != (last_addr >> 16)) {
+        floppy_ret(regs, DISK_RET_EBOUNDARY);
+        return -1;
+    }
 
     u8 mode_register = 0x4a; // single mode, increment, autoinit disable,
     if (cmd[0] == 0xe6)
@@ -188,8 +223,8 @@ floppy_cmd(struct bregs *regs, u16 count, u8 *cmd, u8 cmdlen)
     //DEBUGF("floppy dma c2\n");
     outb(0x06, PORT_DMA1_MASK_REG);
     outb(0x00, PORT_DMA1_CLEAR_FF_REG); // clear flip-flop
-    outb(base_address, PORT_DMA_ADDR_2);
-    outb(base_address>>8, PORT_DMA_ADDR_2);
+    outb(addr, PORT_DMA_ADDR_2);
+    outb(addr>>8, PORT_DMA_ADDR_2);
     outb(0x00, PORT_DMA1_CLEAR_FF_REG); // clear flip-flop
     outb(count, PORT_DMA_CNT_2);
     outb(count>>8, PORT_DMA_CNT_2);
@@ -199,13 +234,15 @@ floppy_cmd(struct bregs *regs, u16 count, u8 *cmd, u8 cmdlen)
     outb(mode_register, PORT_DMA1_MODE_REG);
 
     // port 81: DMA-1 Page Register, channel 2
-    outb(page, PORT_DMA_PAGE_2);
+    outb(addr>>16, PORT_DMA_PAGE_2);
 
     outb(0x02, PORT_DMA1_MASK_REG); // unmask channel 2
 
-    u8 ret = floppy_pio(cmd, cmdlen);
-    if (ret)
-        return ret;
+    int ret = floppy_pio(cmd, cmdlen);
+    if (ret) {
+        floppy_ret(regs, DISK_RET_ETIMEOUT);
+        return -1;
+    }
 
     // check port 3f4 for accessibility to status bytes
     if ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0)
@@ -337,37 +374,20 @@ floppy_media_sense(u8 drive)
     return rv;
 }
 
-static inline void
-floppy_ret(struct bregs *regs, u8 code)
-{
-    SET_BDA(floppy_last_status, code);
-    if (code)
-        set_code_fail(regs, code);
-    else
-        set_code_success(regs);
-}
-
-static inline void
-floppy_fail(struct bregs *regs, u8 code)
-{
-    floppy_ret(regs, code);
-    regs->al = 0; // no sectors read
-}
-
-static u16
+static int
 check_drive(struct bregs *regs, u8 drive)
 {
     // see if drive exists
     if (drive > 1 || !get_drive_type(drive)) {
         // XXX - return type doesn't match
-        floppy_fail(regs, DISK_RET_ETIMEOUT);
-        return 1;
+        floppy_ret(regs, DISK_RET_ETIMEOUT);
+        return -1;
     }
 
     // see if media in drive, and type is known
     if (floppy_media_known(drive) == 0 && floppy_media_sense(drive) == 0) {
-        floppy_fail(regs, DISK_RET_EMEDIA);
-        return 1;
+        floppy_ret(regs, DISK_RET_EMEDIA);
+        return -1;
     }
     return 0;
 }
@@ -402,7 +422,7 @@ static void
 floppy_1302(struct bregs *regs, u8 drive)
 {
     if (check_drive(regs, drive))
-        return;
+        goto fail;
 
     u8 num_sectors = regs->al;
     u8 track       = regs->ch;
@@ -411,9 +431,8 @@ floppy_1302(struct bregs *regs, u8 drive)
 
     if (head > 1 || sector == 0 || num_sectors == 0
         || track > 79 || num_sectors > 72) {
-        BX_INFO("int13_diskette: read/write/verify: parameter out of range\n");
-        floppy_fail(regs, DISK_RET_EPARAM);
-        return;
+        floppy_ret(regs, DISK_RET_EPARAM);
+        goto fail;
     }
 
     // send read-normal-data command (9 bytes) to controller
@@ -428,21 +447,22 @@ floppy_1302(struct bregs *regs, u8 drive)
     data[7] = 0; // Gap length
     data[8] = 0xff; // Gap length
 
-    u16 ret = floppy_cmd(regs, (num_sectors * 512) - 1, data, 9);
-    if (ret) {
-        floppy_fail(regs, ret);
-        return;
-    }
+    int ret = floppy_cmd(regs, (num_sectors * 512) - 1, data, 9);
+    if (ret)
+        goto fail;
 
     if (data[0] & 0xc0) {
-        floppy_fail(regs, DISK_RET_ECONTROLLER);
-        return;
+        floppy_ret(regs, DISK_RET_ECONTROLLER);
+        goto fail;
     }
 
     // ??? should track be new val from return_status[3] ?
     set_diskette_current_cyl(drive, track);
     // AL = number of sectors read (same value as passed)
     floppy_ret(regs, DISK_RET_SUCCESS);
+    return;
+fail:
+    regs->al = 0; // no sectors read
 }
 
 // Write Diskette Sectors
@@ -450,7 +470,7 @@ static void
 floppy_1303(struct bregs *regs, u8 drive)
 {
     if (check_drive(regs, drive))
-        return;
+        goto fail;
 
     u8 num_sectors = regs->al;
     u8 track       = regs->ch;
@@ -459,9 +479,8 @@ floppy_1303(struct bregs *regs, u8 drive)
 
     if (head > 1 || sector == 0 || num_sectors == 0
         || track > 79 || num_sectors > 72) {
-        BX_INFO("int13_diskette: read/write/verify: parameter out of range\n");
-        floppy_fail(regs, DISK_RET_EPARAM);
-        return;
+        floppy_ret(regs, DISK_RET_EPARAM);
+        goto fail;
     }
 
     // send write-normal-data command (9 bytes) to controller
@@ -476,17 +495,14 @@ floppy_1303(struct bregs *regs, u8 drive)
     data[7] = 0; // Gap length
     data[8] = 0xff; // Gap length
 
-    u8 ret = floppy_cmd(regs, (num_sectors * 512) - 1, data, 9);
-    if (ret) {
-        floppy_fail(regs, ret);
-        return;
-    }
+    int ret = floppy_cmd(regs, (num_sectors * 512) - 1, data, 9);
+    if (ret)
+        goto fail;
 
     if (data[0] & 0xc0) {
         if (data[1] & 0x02) {
-            set_fail(regs);
-            regs->ax = 0x0300;
-            return;
+            floppy_ret(regs, DISK_RET_EWRITEPROTECT);
+            goto fail;
         }
         BX_PANIC("int13_diskette_function: read error\n");
     }
@@ -495,6 +511,9 @@ floppy_1303(struct bregs *regs, u8 drive)
     set_diskette_current_cyl(drive, track);
     // AL = number of sectors read (same value as passed)
     floppy_ret(regs, DISK_RET_SUCCESS);
+    return;
+fail:
+    regs->al = 0; // no sectors read
 }
 
 // Verify Diskette Sectors
@@ -502,7 +521,7 @@ static void
 floppy_1304(struct bregs *regs, u8 drive)
 {
     if (check_drive(regs, drive))
-        return;
+        goto fail;
 
     u8 num_sectors = regs->al;
     u8 track       = regs->ch;
@@ -511,22 +530,24 @@ floppy_1304(struct bregs *regs, u8 drive)
 
     if (head > 1 || sector == 0 || num_sectors == 0
         || track > 79 || num_sectors > 72) {
-        BX_INFO("int13_diskette: read/write/verify: parameter out of range\n");
-        floppy_fail(regs, DISK_RET_EPARAM);
-        return;
+        floppy_ret(regs, DISK_RET_EPARAM);
+        goto fail;
     }
 
     // ??? should track be new val from return_status[3] ?
     set_diskette_current_cyl(drive, track);
     // AL = number of sectors verified (same value as passed)
     floppy_ret(regs, DISK_RET_SUCCESS);
+    return;
+fail:
+    regs->al = 0; // no sectors read
 }
 
 // format diskette track
 static void
 floppy_1305(struct bregs *regs, u8 drive)
 {
-    DEBUGF("floppy f05\n");
+    dprintf(3, "floppy f05\n");
 
     if (check_drive(regs, drive))
         return;
@@ -535,8 +556,7 @@ floppy_1305(struct bregs *regs, u8 drive)
     u8 head        = regs->dh;
 
     if (head > 1 || num_sectors == 0 || num_sectors > 18) {
-        BX_INFO("int13_diskette: read/write/verify: parameter out of range\n");
-        floppy_fail(regs, DISK_RET_EPARAM);
+        floppy_ret(regs, DISK_RET_EPARAM);
         return;
     }
 
@@ -549,16 +569,13 @@ floppy_1305(struct bregs *regs, u8 drive)
     data[4] = 0; // Gap length
     data[5] = 0xf6; // Fill byte
 
-    u8 ret = floppy_cmd(regs, (num_sectors * 4) - 1, data, 6);
-    if (ret) {
-        floppy_fail(regs, ret);
+    int ret = floppy_cmd(regs, (num_sectors * 4) - 1, data, 6);
+    if (ret)
         return;
-    }
 
     if (data[0] & 0xc0) {
         if (data[1] & 0x02) {
-            set_fail(regs);
-            regs->ax = 0x0300;
+            floppy_ret(regs, DISK_RET_EWRITEPROTECT);
             return;
         }
         BX_PANIC("int13_diskette_function: read error\n");
@@ -572,7 +589,7 @@ floppy_1305(struct bregs *regs, u8 drive)
 static void
 floppy_1308(struct bregs *regs, u8 drive)
 {
-    DEBUGF("floppy f08\n");
+    dprintf(3, "floppy f08\n");
 
     u8 drive_type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
     u8 num_floppies = 0;
@@ -589,7 +606,7 @@ floppy_1308(struct bregs *regs, u8 drive)
         regs->es = 0;
         regs->di = 0;
         regs->dl = num_floppies;
-        set_success(regs);
+        set_fail(regs);
         return;
     }
 
@@ -656,15 +673,16 @@ floppy_1308(struct bregs *regs, u8 drive)
 
     /* set es & di to point to 11 byte diskette param table in ROM */
     regs->es = SEG_BIOS;
-    regs->di = (u16)&diskette_param_table2;
+    regs->di = (u32)&diskette_param_table2;
     /* disk status not changed upon success */
+    set_success(regs);
 }
 
 // read diskette drive type
 static void
 floppy_1315(struct bregs *regs, u8 drive)
 {
-    DEBUGF("floppy f15\n");
+    dprintf(6, "floppy f15\n");
     if (drive > 1) {
         set_fail(regs);
         regs->ah = 0; // only 2 drives supported
@@ -681,7 +699,6 @@ floppy_1315(struct bregs *regs, u8 drive)
 static void
 floppy_1316(struct bregs *regs, u8 drive)
 {
-    DEBUGF("floppy f16\n");
     if (drive > 1) {
         floppy_ret(regs, DISK_RET_EPARAM);
         return;
@@ -692,7 +709,6 @@ floppy_1316(struct bregs *regs, u8 drive)
 static void
 floppy_13XX(struct bregs *regs, u8 drive)
 {
-    BX_INFO("int13_diskette: unsupported AH=%02x\n", regs->ah);
     floppy_ret(regs, DISK_RET_EPARAM);
 }
 
@@ -725,7 +741,10 @@ floppy_13(struct bregs *regs, u8 drive)
 void VISIBLE16
 handle_0e()
 {
-    //debug_isr();
+    debug_isr(DEBUG_ISR_0e);
+    if (! CONFIG_FLOPPY_SUPPORT)
+        goto done;
+
     if ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0) {
         outb(0x08, PORT_FD_DATA); // sense interrupt status
         while ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0)
@@ -734,15 +753,20 @@ handle_0e()
             inb(PORT_FD_DATA);
         } while ((inb(PORT_FD_STATUS) & 0xc0) == 0xc0);
     }
-    eoi_master_pic();
     // diskette interrupt has occurred
     SETBITS_BDA(floppy_recalibration_status, FRS_TIMEOUT);
+
+done:
+    eoi_pic1();
 }
 
 // Called from int08 handler.
 void
 floppy_tick()
 {
+    if (! CONFIG_FLOPPY_SUPPORT)
+        return;
+
     // time to turn off drive(s)?
     u8 fcount = GET_BDA(floppy_motor_counter);
     if (fcount) {