Make sure to exit from cdrom_boot() if a cdrom is not found.
[seabios.git] / src / cdrom.c
index aa5dfd3becca9615b8f9bcbdf126224a39f0c6bc..3690f677e54058079e34cc7eefdcae64b827eea8 100644 (file)
@@ -8,9 +8,8 @@
 #include "disk.h" // cdrom_13
 #include "util.h" // memset
 #include "ata.h" // ATA_CMD_READ_SECTORS
-
-#define DEBUGF1(fmt, args...) bprintf(0, fmt , ##args)
-#define DEBUGF(fmt, args...)
+#include "bregs.h" // struct bregs
+#include "biosvar.h" // GET_EBDA
 
 
 /****************************************************************
@@ -179,18 +178,13 @@ cdrom_13(struct bregs *regs, u8 device)
  * CD emulation
  ****************************************************************/
 
-// read disk sectors
-static void
-cdemu_1302(struct bregs *regs, u8 device)
-{
-    emu_access(regs, device, ATA_CMD_READ_SECTORS);
-}
-
-// verify disk sectors
-static void
-cdemu_1304(struct bregs *regs, u8 device)
+// Read a series of 512 byte sectors from the cdrom starting at the
+// image offset.
+__always_inline int
+cdrom_read_emu(u16 biosid, u32 vlba, u32 count, void *far_buffer)
 {
-    emu_access(regs, device, 0);
+    u32 ilba = GET_EBDA(cdemu.ilba);
+    return cdrom_read_512(biosid, ilba * 4 + vlba, count, far_buffer);
 }
 
 // read disk drive parameters
@@ -214,7 +208,7 @@ cdemu_1308(struct bregs *regs, u8 device)
         regs->bl = media * 2;
 
     regs->es = SEG_BIOS;
-    regs->di = (u16)&diskette_param_table2;
+    regs->di = (u32)&diskette_param_table2;
 
     disk_ret(regs, DISK_RET_SUCCESS);
 }
@@ -228,11 +222,31 @@ cdemu_13(struct bregs *regs)
     device += GET_EBDA(cdemu.device_spec);
 
     switch (regs->ah) {
-    case 0x02: cdemu_1302(regs, device); break;
-    case 0x04: cdemu_1304(regs, device); break;
+    // These functions are the same as for hard disks
+    case 0x02:
+    case 0x04:
+        disk_13(regs, device);
+        break;
+
+    // These functions are the same as standard CDROM.
+    case 0x00:
+    case 0x01:
+    case 0x03:
+    case 0x05:
+    case 0x09:
+    case 0x0c:
+    case 0x0d:
+    case 0x10:
+    case 0x11:
+    case 0x14:
+    case 0x15:
+    case 0x16:
+        cdrom_13(regs, device);
+        break;
+
     case 0x08: cdemu_1308(regs, device); break;
-    // XXX - All other calls get passed to standard CDROM functions.
-    default: cdrom_13(regs, device); break;
+
+    default:   disk_13XX(regs, device); break;
     }
 }
 
@@ -287,7 +301,7 @@ cdemu_134b(struct bregs *regs)
  ****************************************************************/
 
 // Request SENSE
-static u16
+static int
 atapi_get_sense(u16 device, u8 *asc, u8 *ascq)
 {
     u8 buffer[18];
@@ -295,10 +309,10 @@ atapi_get_sense(u16 device, u8 *asc, u8 *ascq)
     memset(atacmd, 0, sizeof(atacmd));
     atacmd[0] = ATA_CMD_REQUEST_SENSE;
     atacmd[4] = sizeof(buffer);
-    u16 ret = ata_cmd_packet(device, atacmd, sizeof(atacmd)
-                             , 18L, 0, ATA_DATA_IN, GET_SEG(SS), (u32)buffer);
+    int ret = ata_cmd_packet(device, atacmd, sizeof(atacmd), sizeof(buffer)
+                             , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
     if (ret != 0)
-        return 0x0002;
+        return ret;
 
     *asc = buffer[12];
     *ascq = buffer[13];
@@ -306,7 +320,7 @@ atapi_get_sense(u16 device, u8 *asc, u8 *ascq)
     return 0;
 }
 
-static u16
+static int
 atapi_is_ready(u16 device)
 {
     if (GET_EBDA(ata.devices[device].type) != ATA_TYPE_ATAPI) {
@@ -314,7 +328,7 @@ atapi_is_ready(u16 device)
         return -1;
     }
 
-    DEBUGF("ata_detect_medium: begin\n");
+    dprintf(6, "ata_detect_medium: begin\n");
     u8 packet[12];
     memset(packet, 0, sizeof(packet));
     packet[0] = 0x25; /* READ CAPACITY */
@@ -328,11 +342,11 @@ atapi_is_ready(u16 device)
     u8 in_progress = 0;
     for (;; time+=100) {
         if (time >= timeout) {
-            DEBUGF("read capacity failed\n");
+            dprintf(1, "read capacity failed\n");
             return -1;
         }
-        u16 ret = ata_cmd_packet(device, packet, sizeof(packet)
-                                 , 0, 8L, ATA_DATA_IN, GET_SEG(SS), (u32)buf);
+        int ret = ata_cmd_packet(device, packet, sizeof(packet), sizeof(buf)
+                                 , MAKE_FARPTR(GET_SEG(SS), (u32)buf));
         if (ret == 0)
             break;
 
@@ -342,7 +356,7 @@ atapi_is_ready(u16 device)
             continue;
 
         if (asc == 0x3a) { /* MEDIUM NOT PRESENT */
-            DEBUGF("Device reports MEDIUM NOT PRESENT\n");
+            dprintf(1, "Device reports MEDIUM NOT PRESENT\n");
             return -1;
         }
 
@@ -371,7 +385,7 @@ atapi_is_ready(u16 device)
         | (u32) buf[2] << 8
         | (u32) buf[3] << 0;
 
-    DEBUGF("sectors=%u\n", sectors);
+    dprintf(6, "sectors=%u\n", sectors);
     if (block_len == 2048)
         sectors <<= 2; /* # of sectors in 512-byte "soft" sector */
     if (sectors != GET_EBDA(ata.devices[device].sectors))
@@ -380,7 +394,7 @@ atapi_is_ready(u16 device)
     return 0;
 }
 
-static u16
+static int
 atapi_is_cdrom(u8 device)
 {
     if (device >= CONFIG_MAX_ATA_DEVICES)
@@ -410,7 +424,7 @@ streq_cs(u8 *s1, char *cs_s2)
     }
 }
 
-u16
+int
 cdrom_boot()
 {
     // Find out the first cdrom
@@ -418,18 +432,18 @@ cdrom_boot()
     for (device=0; device<CONFIG_MAX_ATA_DEVICES; device++)
         if (atapi_is_cdrom(device))
             break;
-
-    u16 ret = atapi_is_ready(device);
-    if (ret)
-        BX_INFO("ata_is_ready returned %d\n", ret);
-
-    // if not found
     if (device >= CONFIG_MAX_ATA_DEVICES)
+        // cdrom not found
         return 2;
 
+    int ret = atapi_is_ready(device);
+    if (ret)
+        dprintf(1, "atapi_is_ready returned %d\n", ret);
+
     // Read the Boot Record Volume Descriptor
     u8 buffer[2048];
-    ret = cdrom_read(device, 0x11, 2048, GET_SEG(SS), (u32)buffer, 0);
+    ret = cdrom_read(device, 0x11, 1
+                     , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
     if (ret)
         return 3;
 
@@ -443,7 +457,8 @@ cdrom_boot()
     u32 lba = *(u32*)&buffer[0x47];
 
     // And we read the Boot Catalog
-    ret = cdrom_read(device, lba, 2048, GET_SEG(SS), (u32)buffer, 0);
+    ret = cdrom_read(device, lba, 1
+                     , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
     if (ret)
         return 7;
 
@@ -461,15 +476,8 @@ cdrom_boot()
     if (buffer[0x20] != 0x88)
         return 11; // Bootable
 
-    SET_EBDA(cdemu.media,buffer[0x21]);
-    if (buffer[0x21] == 0)
-        // FIXME ElTorito Hardcoded. cdrom is hardcoded as device 0xE0.
-        // Win2000 cd boot needs to know it booted from cd
-        SET_EBDA(cdemu.emulated_drive, 0xE0);
-    else if (buffer[0x21] < 4)
-        SET_EBDA(cdemu.emulated_drive, 0x00);
-    else
-        SET_EBDA(cdemu.emulated_drive, 0x80);
+    u8 media = buffer[0x21];
+    SET_EBDA(cdemu.media, media);
 
     SET_EBDA(cdemu.controller_index, device/2);
     SET_EBDA(cdemu.device_spec, device%2);
@@ -477,9 +485,8 @@ cdrom_boot()
     u16 boot_segment = *(u16*)&buffer[0x22];
     if (!boot_segment)
         boot_segment = 0x07C0;
-
-    SET_EBDA(cdemu.load_segment,boot_segment);
-    SET_EBDA(cdemu.buffer_segment,0x0000);
+    SET_EBDA(cdemu.load_segment, boot_segment);
+    SET_EBDA(cdemu.buffer_segment, 0x0000);
 
     u16 nbsectors = *(u16*)&buffer[0x26];
     SET_EBDA(cdemu.sector_count, nbsectors);
@@ -488,13 +495,38 @@ cdrom_boot()
     SET_EBDA(cdemu.ilba, lba);
 
     // And we read the image in memory
-    ret = cdrom_read(device, lba, nbsectors*512
-                     , boot_segment, 0, 0);
+    ret = cdrom_read_emu(device, 0, nbsectors, MAKE_FARPTR(boot_segment, 0));
     if (ret)
         return 12;
 
+    if (media == 0) {
+        // No emulation requested - return success.
+
+        // FIXME ElTorito Hardcoded. cdrom is hardcoded as device 0xE0.
+        // Win2000 cd boot needs to know it booted from cd
+        SET_EBDA(cdemu.emulated_drive, 0xE0);
+
+        return 0;
+    }
+
+    // Emulation of a floppy/harddisk requested
+    if (! CONFIG_CDROM_EMU)
+        return 13;
+
+    // Set emulated drive id and increase bios installed hardware
+    // number of devices
+    if (media < 4) {
+        // Floppy emulation
+        SET_EBDA(cdemu.emulated_drive, 0x00);
+        SETBITS_BDA(equipment_list_flags, 0x41);
+    } else {
+        // Harddrive emulation
+        SET_EBDA(cdemu.emulated_drive, 0x80);
+        SET_EBDA(ata.hdcount, GET_EBDA(ata.hdcount) + 1);
+    }
+
     // Remember the media type
-    switch (GET_EBDA(cdemu.media)) {
+    switch (media) {
     case 0x01:  // 1.2M floppy
         SET_EBDA(cdemu.vdevice.spt, 15);
         SET_EBDA(cdemu.vdevice.cylinders, 80);
@@ -521,17 +553,9 @@ cdrom_boot()
     }
     }
 
-    if (GET_EBDA(cdemu.media) != 0) {
-        // Increase bios installed hardware number of devices
-        if (GET_EBDA(cdemu.emulated_drive) == 0x00)
-            SETBITS_BDA(equipment_list_flags, 0x41);
-        else
-            SET_EBDA(ata.hdcount, GET_EBDA(ata.hdcount) + 1);
-    }
-
     // everything is ok, so from now on, the emulation is active
-    if (GET_EBDA(cdemu.media))
-        SET_EBDA(cdemu.active, 0x01);
+    SET_EBDA(cdemu.active, 0x01);
+    dprintf(6, "cdemu media=%d\n", media);
 
     return 0;
 }