Minor cleanups.
[seabios.git] / src / disk.c
index 74c1acad31094a05fac6149adf416ff1d8408b82..1f172406420c1ce875039ab9eb486ff0885307bf 100644 (file)
 #include "util.h" // debug_enter
 #include "ata.h" // ATA_*
 
+#define DEBUGF1(fmt, args...) bprintf(0, fmt , ##args)
+#define DEBUGF(fmt, args...)
+
 
 /****************************************************************
  * Helper functions
  ****************************************************************/
 
-static inline void
-disk_ret(struct bregs *regs, u8 code)
-{
-    regs->ah = code;
-    SET_BDA(disk_last_status, code);
-    set_cf(regs, code);
-}
-
 #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)
-{
-    u32 i;
-    u8 sum = 0;
-    for (i=0; i<len; i++)
-        sum += GET_FARVAR(seg, *(u8*)(offset+i));
-    return sum;
-}
-
 static void
 basic_access(struct bregs *regs, u8 device, u16 command)
 {
+    u8 type = GET_EBDA(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);
+    } else {
+        // Must be cd emulation.
+        nlc   = GET_EBDA(cdemu.vdevice.cylinders);
+        nlh   = GET_EBDA(cdemu.vdevice.heads);
+        nlspt = GET_EBDA(cdemu.vdevice.spt);
+    }
+
     u16 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 (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;
     }
 
-    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);
-
     // sanity check on cyl heads, sec
-    if ( (cylinder >= nlc) || (head >= nlh) || (sector > nlspt )) {
+    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);
@@ -71,39 +63,36 @@ basic_access(struct bregs *regs, u8 device, u16 command)
         return;
     }
 
-    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
+    if (!command) {
+        // If verify or seek
+        disk_ret(regs, DISK_RET_SUCCESS);
+        return;
     }
 
+    // translate lchs to lba
+    u32 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);
 
-    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;
-    }
+    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);
+
+    irq_disable();
 
     // Set nb of sector transferred
     regs->al = GET_EBDA(ata.trsfsectors);
 
     if (status != 0) {
-        BX_INFO("int13_harddisk: function %02x, error %02x !\n",regs->ah,status);
+        BX_INFO("int13_harddisk: function %02x, error %02x !\n"
+                , regs->ah, status);
         disk_ret(regs, DISK_RET_EBADTRACK);
     }
     disk_ret(regs, DISK_RET_SUCCESS);
@@ -113,8 +102,6 @@ static void
 extended_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);
@@ -136,40 +123,26 @@ extended_access(struct bregs *regs, u8 device, u16 command)
         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 (!command) {
         // If verify or seek
         disk_ret(regs, DISK_RET_SUCCESS);
         return;
     }
 
+    u16 segment = GET_INT13EXT(regs, segment);
+    u16 offset = GET_INT13EXT(regs, offset);
+    void *far_buffer = MAKE_FARPTR(segment, offset);
+
+    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);
+
+    irq_disable();
+
     SET_INT13EXT(regs, count, GET_EBDA(ata.trsfsectors));
 
     if (status != 0) {
@@ -197,8 +170,10 @@ disk_1300(struct bregs *regs, u8 device)
 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
@@ -314,7 +289,7 @@ disk_1315(struct bregs *regs, u8 device)
     regs->cx = lba >> 16;
     regs->dx = lba & 0xffff;
 
-    disk_ret(regs, 0);
+    disk_ret(regs, DISK_RET_SUCCESS);
     regs->ah = 3; // hard disk accessible
 }
 
@@ -427,7 +402,7 @@ disk_1348(struct bregs *regs, u8 device)
 
     SET_INT13DPT(regs, size, 0x1e);
 
-    SET_INT13DPT(regs, dpte_segment, EBDA_SEG);
+    SET_INT13DPT(regs, dpte_segment, SEG_EBDA);
     SET_INT13DPT(regs, dpte_offset
                  , offsetof(struct extended_bios_data_area_s, ata.dpte));
 
@@ -467,9 +442,9 @@ disk_1348(struct bregs *regs, u8 device)
     else
         SET_EBDA(ata.dpte.revision, 0x10);
 
-    u8 sum = checksum_seg(EBDA_SEG
-                          , offsetof(struct extended_bios_data_area_s, ata.dpte)
-                          , 15);
+    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) {
@@ -513,7 +488,7 @@ disk_1348(struct bregs *regs, u8 device)
     SET_INT13DPT(regs, device_path[2], 0);
     SET_INT13DPT(regs, device_path[4], 0L);
 
-    sum = checksum_seg(regs->ds, 30, 34);
+    sum = checksum(MAKE_FARPTR(regs->ds, 30), 34);
     SET_INT13DPT(regs, checksum, ~sum);
 }
 
@@ -552,7 +527,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);
 }
 
@@ -569,14 +543,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);
@@ -614,168 +587,12 @@ 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);
-}
-
-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;
-    }
-}
-
-
 /****************************************************************
  * Entry points
  ****************************************************************/
 
 static u8
-get_device(struct bregs *regs, u8 drive)
+get_device(struct bregs *regs, u8 iscd, u8 drive)
 {
     // basic check : device has to be defined
     if (drive >= CONFIG_MAX_ATA_DEVICES) {
@@ -784,7 +601,7 @@ get_device(struct bregs *regs, u8 drive)
     }
 
     // Get the ata channel
-    u8 device = GET_EBDA(ata.hdidmap[drive]);
+    u8 device = GET_EBDA(ata.idmap[iscd][drive]);
 
     // basic check : device has to be valid
     if (device >= CONFIG_MAX_ATA_DEVICES) {
@@ -810,50 +627,55 @@ handle_legacy_disk(struct bregs *regs, u8 drive)
     }
 
     if (drive >= 0xe0) {
-        u8 device = get_device(regs, drive - 0xe0);
+        u8 device = get_device(regs, 1, drive - 0xe0);
         if (device >= CONFIG_MAX_ATA_DEVICES)
             return;
         cdrom_13(regs, device);
         return;
     }
 
-    u8 device = get_device(regs, drive - 0x80);
+    u8 device = get_device(regs, 0, drive - 0x80);
     if (device >= CONFIG_MAX_ATA_DEVICES)
         return;
     disk_13(regs, device);
 }
 
-void VISIBLE
+void VISIBLE16
 handle_40(struct bregs *regs)
 {
     debug_enter(regs);
     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);
     u8 drive = regs->dl;
-    // XXX
-#if BX_ELTORITO_BOOT
-    if (regs->ah >= 0x4a || regs->ah <= 0x4d) {
-        int13_eltorito(regs);
-    } else if (cdemu_isactive() && cdrom_emulated_drive()) {
-        int13_cdemu(regs);
-    } else
-#endif
-        handle_legacy_disk(regs, drive);
-    debug_exit(regs);
+
+    if (CONFIG_CDROM_EMU) {
+        if (regs->ah == 0x4b) {
+            cdemu_134b(regs);
+            return;
+        }
+        if (GET_EBDA(cdemu.active)) {
+            if (drive == GET_EBDA(cdemu.emulated_drive)) {
+                cdemu_13(regs);
+                return;
+            }
+            if (drive < 0xe0)
+                drive--;
+        }
+    }
+    handle_legacy_disk(regs, drive);
 }
 
 // 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);
+    debug_isr();
+    SET_BDA(disk_interrupt_flag, 0xff);
     eoi_both_pics();
 }