Reduce ide stack usage by reorganizing ata calls.
[seabios.git] / src / cdrom.c
index b2caaf612a4b06d5eb3cb2a17b39f4e53b67258b..e2e4fb03ab7cad80f418ab7dc944a8c3ad664c58 100644 (file)
@@ -112,9 +112,9 @@ cdrom_1346(struct bregs *regs, u8 device)
 static void
 cdrom_1349(struct bregs *regs, u8 device)
 {
+    set_fail(regs);
     // always send changed ??
     regs->ah = DISK_RET_ECHANGED;
-    set_cf(regs, 1);
 }
 
 static void
@@ -179,18 +179,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 +209,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 +223,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;
     }
 }
 
@@ -295,8 +310,8 @@ 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);
+    u16 ret = ata_cmd_packet(device, atacmd, sizeof(atacmd), sizeof(buffer)
+                             , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
     if (ret != 0)
         return 0x0002;
 
@@ -331,8 +346,8 @@ atapi_is_ready(u16 device)
             DEBUGF("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);
+        u16 ret = ata_cmd_packet(device, packet, sizeof(packet), sizeof(buf)
+                                 , MAKE_FARPTR(GET_SEG(SS), (u32)buf));
         if (ret == 0)
             break;
 
@@ -429,7 +444,8 @@ cdrom_boot()
 
     // 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 +459,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 +478,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 +487,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 +497,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 +555,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);
+    DEBUGF("cdemu media=%d\n", media);
 
     return 0;
 }