// 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.
#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
/****************************************************************
* 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
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:
cdrom_13(regs, device);
break;
- case 0x08: cdemu_1308(regs, device); break;
-
default: disk_13XX(regs, device); break;
}
}
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) {
}
}
- 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;
}
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)
// 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;
// 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
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;
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 {
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