Unify cd emulation access and main disk access code.
[seabios.git] / src / ata.c
index c82e8a3e308fb17292823d4de6c880f094b5cf1f..f5a1c98c76038fd033f5b21a042436fd403dd54b 100644 (file)
--- a/src/ata.c
+++ b/src/ata.c
 #include "cmos.h" // inb_cmos
 #include "pic.h" // enable_hwirq
 #include "biosvar.h" // GET_EBDA
-#include "pci.h" // pci_find_class
+#include "pci.h" // foreachpci
 #include "pci_ids.h" // PCI_CLASS_STORAGE_OTHER
 #include "pci_regs.h" // PCI_INTERRUPT_LINE
 #include "boot.h" // add_bcv_hd
 #include "disk.h" // struct ata_s
 #include "ata.h" // ATA_CB_STAT
 
-#define IDE_SECTOR_SIZE 512
-#define CDROM_SECTOR_SIZE 2048
-
 #define IDE_TIMEOUT 32000 //32 seconds max for IDE ops
 
-struct ata_channel_s ATA_channels[CONFIG_MAX_ATA_INTERFACES] VAR16_32;
+struct ata_channel_s ATA_channels[CONFIG_MAX_ATA_INTERFACES] VAR16VISIBLE;
 
 
 /****************************************************************
@@ -352,7 +349,7 @@ ata_cmd_data(struct disk_op_s *op, int iswrite, int command)
     int ret = send_cmd(op->driveid, &cmd);
     if (ret)
         return ret;
-    return ata_transfer(op, iswrite, IDE_SECTOR_SIZE);
+    return ata_transfer(op, iswrite, DISK_SECTOR_SIZE);
 }
 
 int
@@ -527,13 +524,27 @@ extract_identify(int driveid, u16 *buffer)
 
     // Common flags.
     SET_GLOBAL(Drives.drives[driveid].removable, (buffer[0] & 0x80) ? 1 : 0);
+    SET_GLOBAL(Drives.drives[driveid].cntl_info, extract_version(buffer));
+}
+
+void
+describe_atapi(int driveid)
+{
+    u8 ataid = Drives.drives[driveid].cntl_id;
+    u8 channel = ataid / 2;
+    u8 slave = ataid % 2;
+    u8 version = Drives.drives[driveid].cntl_info;
+    int iscd = Drives.drives[driveid].floppy_type;
+    printf("ata%d-%d: %s ATAPI-%d %s", channel, slave
+           , Drives.drives[driveid].model, version
+           , (iscd ? "CD-Rom/DVD-Rom" : "Device"));
 }
 
 static int
 init_drive_atapi(int driveid, u16 *buffer)
 {
     // Send an IDENTIFY_DEVICE_PACKET command to device
-    memset(buffer, 0, IDE_SECTOR_SIZE);
+    memset(buffer, 0, DISK_SECTOR_SIZE);
     struct disk_op_s dop;
     memset(&dop, 0, sizeof(dop));
     dop.driveid = driveid;
@@ -550,14 +561,7 @@ init_drive_atapi(int driveid, u16 *buffer)
     SET_GLOBAL(Drives.drives[driveid].blksize, CDROM_SECTOR_SIZE);
     SET_GLOBAL(Drives.drives[driveid].sectors, (u64)-1);
     u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
-
-    // Report drive info to user.
-    u8 ataid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
-    u8 channel = ataid / 2;
-    u8 slave = ataid % 2;
-    printf("ata%d-%d: %s ATAPI-%d %s\n", channel, slave
-           , Drives.drives[driveid].model, extract_version(buffer)
-           , (iscd ? "CD-Rom/DVD-Rom" : "Device"));
+    SET_GLOBAL(Drives.drives[driveid].floppy_type, iscd);
 
     // fill cdidmap
     if (iscd)
@@ -566,11 +570,28 @@ init_drive_atapi(int driveid, u16 *buffer)
     return 0;
 }
 
+void
+describe_ata(int driveid)
+{
+    u8 ataid = Drives.drives[driveid].cntl_id;
+    u8 channel = ataid / 2;
+    u8 slave = ataid % 2;
+    u64 sectors = Drives.drives[driveid].sectors;
+    u8 version = Drives.drives[driveid].cntl_info;
+    char *model = Drives.drives[driveid].model;
+    printf("ata%d-%d: %s ATA-%d Hard-Disk", channel, slave, model, version);
+    u64 sizeinmb = sectors >> 11;
+    if (sizeinmb < (1 << 16))
+        printf(" (%u MiBytes)", (u32)sizeinmb);
+    else
+        printf(" (%u GiBytes)", (u32)(sizeinmb >> 10));
+}
+
 static int
 init_drive_ata(int driveid, u16 *buffer)
 {
     // Send an IDENTIFY_DEVICE command to device
-    memset(buffer, 0, IDE_SECTOR_SIZE);
+    memset(buffer, 0, DISK_SECTOR_SIZE);
     struct disk_op_s dop;
     memset(&dop, 0, sizeof(dop));
     dop.driveid = driveid;
@@ -584,7 +605,7 @@ init_drive_ata(int driveid, u16 *buffer)
     // Success - setup as ATA.
     extract_identify(driveid, buffer);
     SET_GLOBAL(Drives.drives[driveid].type, DTYPE_ATA);
-    SET_GLOBAL(Drives.drives[driveid].blksize, IDE_SECTOR_SIZE);
+    SET_GLOBAL(Drives.drives[driveid].blksize, DISK_SECTOR_SIZE);
 
     SET_GLOBAL(Drives.drives[driveid].pchs.cylinders, buffer[1]);
     SET_GLOBAL(Drives.drives[driveid].pchs.heads, buffer[3]);
@@ -600,21 +621,8 @@ init_drive_ata(int driveid, u16 *buffer)
     // Setup disk geometry translation.
     setup_translation(driveid);
 
-    // Report drive info to user.
-    u8 ataid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
-    u8 channel = ataid / 2;
-    u8 slave = ataid % 2;
-    char *model = Drives.drives[driveid].model;
-    printf("ata%d-%d: %s ATA-%d Hard-Disk ", channel, slave, model
-           , extract_version(buffer));
-    u64 sizeinmb = sectors >> 11;
-    if (sizeinmb < (1 << 16))
-        printf("(%u MiBytes)\n", (u32)sizeinmb);
-    else
-        printf("(%u GiBytes)\n", (u32)(sizeinmb >> 10));
-
     // Register with bcv system.
-    add_bcv_hd(driveid, model);
+    add_bcv_internal(driveid);
 
     return 0;
 }
@@ -697,9 +705,8 @@ ata_detect()
         // check for ATAPI
         u16 buffer[256];
         int ret = init_drive_atapi(driveid, buffer);
-        if (!ret) {
-            // Found an ATAPI drive.
-        } else {
+        if (ret) {
+            // Didn't find an ATAPI drive - look for ATA drive.
             u8 st = inb(iobase1+ATA_CB_STAT);
             if (!st)
                 // Status not set - can't be a valid drive.
@@ -718,6 +725,10 @@ ata_detect()
         }
         SET_GLOBAL(Drives.drivecount, driveid+1);
 
+        // Report drive info to user.
+        describe_drive(driveid);
+        printf("\n");
+
         u16 resetresult = buffer[93];
         dprintf(6, "ata_detect resetresult=%04x\n", resetresult);
         if (!slave && (resetresult & 0xdf61) == 0x4041)