Separate ATA code from generic disk code.
[seabios.git] / src / cdrom.c
index f518a377636dc39c1e2b173fde03093bd04a989e..ca19da07a4562402d88ef37ba725bc61a6fabcc0 100644 (file)
@@ -1,6 +1,6 @@
 // 16bit code to access cdrom drives.
 //
-// Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
+// Copyright (C) 2008,2009  Kevin O'Connor <kevin@koconnor.net>
 // Copyright (C) 2002  MandrakeSoft S.A.
 //
 // This file may be distributed under the terms of the GNU LGPLv3 license.
@@ -9,7 +9,7 @@
 #include "util.h" // memset
 #include "bregs.h" // struct bregs
 #include "biosvar.h" // GET_EBDA
-#include "atabits.h" // ATA_TYPE_ATAPI
+#include "ata.h" // ATA_CMD_REQUEST_SENSE
 
 
 /****************************************************************
@@ -180,19 +180,31 @@ cdrom_13(struct bregs *regs, u8 device)
  * CD emulation
  ****************************************************************/
 
+static void
+cdemu_1302(struct bregs *regs, u8 device)
+{
+    cdemu_access(regs, device, CMD_READ);
+}
+
+static void
+cdemu_1304(struct bregs *regs, u8 device)
+{
+    cdemu_access(regs, device, CMD_VERIFY);
+}
+
 // read disk drive parameters
 static void
 cdemu_1308(struct bregs *regs, u8 device)
 {
     u16 ebda_seg = get_ebda_seg();
-    u16 nlc   = GET_EBDA2(ebda_seg, cdemu.cylinders) - 1;
-    u16 nlh   = GET_EBDA2(ebda_seg, cdemu.heads) - 1;
-    u16 nlspt = GET_EBDA2(ebda_seg, cdemu.spt);
+    u16 nlc   = GET_EBDA2(ebda_seg, cdemu.lchs.cylinders) - 1;
+    u16 nlh   = GET_EBDA2(ebda_seg, cdemu.lchs.heads) - 1;
+    u16 nlspt = GET_EBDA2(ebda_seg, cdemu.lchs.spt);
 
     regs->al = 0x00;
     regs->bl = 0x00;
     regs->ch = nlc & 0xff;
-    regs->cl = ((nlc >> 2) & 0xc0) | (nlspt  & 0x3f);
+    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
@@ -217,11 +229,9 @@ cdemu_13(struct bregs *regs)
     device += GET_EBDA2(ebda_seg, cdemu.device_spec);
 
     switch (regs->ah) {
-    // These functions are the same as for hard disks
-    case 0x02:
-    case 0x04:
-        disk_13(regs, device);
-        break;
+    case 0x02: cdemu_1302(regs, device); break;
+    case 0x04: cdemu_1304(regs, device); break;
+    case 0x08: cdemu_1308(regs, device); break;
 
     // These functions are the same as standard CDROM.
     case 0x00:
@@ -239,8 +249,6 @@ cdemu_13(struct bregs *regs)
         cdrom_13(regs, device);
         break;
 
-    case 0x08: cdemu_1308(regs, device); break;
-
     default:   disk_13XX(regs, device); break;
     }
 }
@@ -279,9 +287,9 @@ cdemu_134b(struct bregs *regs)
     SET_INT13ET(regs, buffer_segment, GET_EBDA2(ebda_seg, cdemu.buffer_segment));
     SET_INT13ET(regs, load_segment, GET_EBDA2(ebda_seg, cdemu.load_segment));
     SET_INT13ET(regs, sector_count, GET_EBDA2(ebda_seg, cdemu.sector_count));
-    SET_INT13ET(regs, cylinders, GET_EBDA2(ebda_seg, cdemu.cylinders));
-    SET_INT13ET(regs, sectors, GET_EBDA2(ebda_seg, cdemu.spt));
-    SET_INT13ET(regs, heads, GET_EBDA2(ebda_seg, cdemu.heads));
+    SET_INT13ET(regs, cylinders, GET_EBDA2(ebda_seg, cdemu.lchs.cylinders));
+    SET_INT13ET(regs, sectors, GET_EBDA2(ebda_seg, cdemu.lchs.spt));
+    SET_INT13ET(regs, heads, GET_EBDA2(ebda_seg, cdemu.lchs.heads));
 
     // If we have to terminate emulation
     if (regs->al == 0x00) {
@@ -379,7 +387,7 @@ atapi_is_ready(u16 device)
         }
     }
 
-    if (blksize != GET_GLOBAL(ATA.devices[device].blksize)) {
+    if (blksize != GET_GLOBAL(Drives.drives[device].blksize)) {
         printf("Unsupported sector size %u\n", blksize);
         return -1;
     }
@@ -389,30 +397,13 @@ atapi_is_ready(u16 device)
     return 0;
 }
 
-// Compare a string on the stack to one in the code segment.
-static int
-streq_cs(u8 *s1, char *cs_s2)
-{
-    u8 *s2 = (u8*)cs_s2;
-    for (;;) {
-        if (*s1 != GET_GLOBAL(*s2))
-            return 0;
-        if (! *s1)
-            return 1;
-        s1++;
-        s2++;
-    }
-}
-
 int
 cdrom_boot(int cdid)
 {
     // Verify device is a cdrom.
-    if (cdid >= ATA.cdcount)
+    if (cdid >= Drives.cdcount)
         return 1;
-    int driveid = GET_GLOBAL(ATA.idmap[1][cdid]);
-    if (GET_GLOBAL(ATA.devices[driveid].device) != ATA_DEVICE_CDROM)
-        return 2;
+    int driveid = GET_GLOBAL(Drives.idmap[1][cdid]);
 
     int ret = atapi_is_ready(driveid);
     if (ret)
@@ -421,6 +412,7 @@ cdrom_boot(int cdid)
     // Read the Boot Record Volume Descriptor
     u8 buffer[2048];
     struct disk_op_s dop;
+    memset(&dop, 0, sizeof(dop));
     dop.driveid = driveid;
     dop.lba = 0x11;
     dop.count = 1;
@@ -432,7 +424,7 @@ cdrom_boot(int cdid)
     // Validity checks
     if (buffer[0])
         return 4;
-    if (!streq_cs(&buffer[1], "CD001\001EL TORITO SPECIFICATION"))
+    if (strcmp((char*)&buffer[1], "CD001\001EL TORITO SPECIFICATION") != 0)
         return 5;
 
     // ok, now we calculate the Boot catalog address
@@ -478,10 +470,10 @@ cdrom_boot(int cdid)
     SET_EBDA2(ebda_seg, cdemu.ilba, lba);
 
     // And we read the image in memory
-    dop.lba = lba * 4;
-    dop.count = nbsectors;
+    dop.lba = lba;
+    dop.count = DIV_ROUND_UP(nbsectors, 4);
     dop.buf_fl = MAKE_FLATPTR(boot_segment, 0);
-    ret = cdrom_read_512(&dop);
+    ret = cdrom_read(&dop);
     if (ret)
         return 12;
 
@@ -504,19 +496,19 @@ cdrom_boot(int cdid)
 
         switch (media) {
         case 0x01:  // 1.2M floppy
-            SET_EBDA2(ebda_seg, cdemu.spt, 15);
-            SET_EBDA2(ebda_seg, cdemu.cylinders, 80);
-            SET_EBDA2(ebda_seg, cdemu.heads, 2);
+            SET_EBDA2(ebda_seg, cdemu.lchs.spt, 15);
+            SET_EBDA2(ebda_seg, cdemu.lchs.cylinders, 80);
+            SET_EBDA2(ebda_seg, cdemu.lchs.heads, 2);
             break;
         case 0x02:  // 1.44M floppy
-            SET_EBDA2(ebda_seg, cdemu.spt, 18);
-            SET_EBDA2(ebda_seg, cdemu.cylinders, 80);
-            SET_EBDA2(ebda_seg, cdemu.heads, 2);
+            SET_EBDA2(ebda_seg, cdemu.lchs.spt, 18);
+            SET_EBDA2(ebda_seg, cdemu.lchs.cylinders, 80);
+            SET_EBDA2(ebda_seg, cdemu.lchs.heads, 2);
             break;
         case 0x03:  // 2.88M floppy
-            SET_EBDA2(ebda_seg, cdemu.spt, 36);
-            SET_EBDA2(ebda_seg, cdemu.cylinders, 80);
-            SET_EBDA2(ebda_seg, cdemu.heads, 2);
+            SET_EBDA2(ebda_seg, cdemu.lchs.spt, 36);
+            SET_EBDA2(ebda_seg, cdemu.lchs.cylinders, 80);
+            SET_EBDA2(ebda_seg, cdemu.lchs.heads, 2);
             break;
         }
     } else {
@@ -530,9 +522,10 @@ cdrom_boot(int cdid)
         u8 cyllow = GET_FARVAR(boot_segment, mbr->partitions[0].last.cyllow);
         u8 heads = GET_FARVAR(boot_segment, mbr->partitions[0].last.heads);
 
-        SET_EBDA2(ebda_seg, cdemu.spt, sptcyl & 0x3f);
-        SET_EBDA2(ebda_seg, cdemu.cylinders, ((sptcyl<<2)&0x300) + cyllow + 1);
-        SET_EBDA2(ebda_seg, cdemu.heads, heads + 1);
+        SET_EBDA2(ebda_seg, cdemu.lchs.spt, sptcyl & 0x3f);
+        SET_EBDA2(ebda_seg, cdemu.lchs.cylinders
+                  , ((sptcyl<<2)&0x300) + cyllow + 1);
+        SET_EBDA2(ebda_seg, cdemu.lchs.heads, heads + 1);
     }
 
     // everything is ok, so from now on, the emulation is active