Introduce MODESEGMENT define; rename VISIBLE32 to VISIBLE32FLAT.
[seabios.git] / src / block.c
index 3550dcdd28a01025b922536a5d849c54165bf58b..3fb5e91da9ecb446d6c394f0920339b0d67e0da6 100644 (file)
 
 struct drives_s Drives VAR16VISIBLE;
 
+struct drive_s *
+getDrive(u8 exttype, u8 extdriveoffset)
+{
+    // basic check : device has to be defined
+    if (extdriveoffset >= ARRAY_SIZE(Drives.idmap[0]))
+        return NULL;
+
+    // Get the ata channel
+    u8 driveid = GET_GLOBAL(Drives.idmap[exttype][extdriveoffset]);
+
+    // basic check : device has to be valid
+    if (driveid >= ARRAY_SIZE(Drives.drives))
+        return NULL;
+
+    return &Drives.drives[driveid];
+}
+
+struct drive_s *
+allocDrive()
+{
+    int driveid = Drives.drivecount;
+    if (driveid >= ARRAY_SIZE(Drives.drives))
+        return NULL;
+    Drives.drivecount++;
+    struct drive_s *drive_g = &Drives.drives[driveid];
+    memset(drive_g, 0, sizeof(*drive_g));
+    return drive_g;
+}
+
 
 /****************************************************************
  * Disk geometry translation
  ****************************************************************/
 
 static u8
-get_translation(int driveid)
+get_translation(struct drive_s *drive_g)
 {
-    u8 type = GET_GLOBAL(Drives.drives[driveid].type);
+    u8 type = GET_GLOBAL(drive_g->type);
     if (! CONFIG_COREBOOT && type == DTYPE_ATA) {
         // Emulators pass in the translation info via nvram.
-        u8 ataid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
+        u8 ataid = GET_GLOBAL(drive_g->cntl_id);
         u8 channel = ataid / 2;
         u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
         translation >>= 2 * (ataid % 4);
@@ -33,9 +62,9 @@ get_translation(int driveid)
     }
 
     // On COREBOOT, use a heuristic to determine translation type.
-    u16 heads = GET_GLOBAL(Drives.drives[driveid].pchs.heads);
-    u16 cylinders = GET_GLOBAL(Drives.drives[driveid].pchs.cylinders);
-    u16 spt = GET_GLOBAL(Drives.drives[driveid].pchs.spt);
+    u16 heads = GET_GLOBAL(drive_g->pchs.heads);
+    u16 cylinders = GET_GLOBAL(drive_g->pchs.cylinders);
+    u16 spt = GET_GLOBAL(drive_g->pchs.spt);
 
     if (cylinders <= 1024 && heads <= 16 && spt <= 63)
         return TRANSLATION_NONE;
@@ -45,27 +74,27 @@ get_translation(int driveid)
 }
 
 void
-setup_translation(int driveid)
+setup_translation(struct drive_s *drive_g)
 {
-    u8 translation = get_translation(driveid);
-    SET_GLOBAL(Drives.drives[driveid].translation, translation);
+    u8 translation = get_translation(drive_g);
+    SET_GLOBAL(drive_g->translation, translation);
 
-    u8 ataid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
+    u8 ataid = GET_GLOBAL(drive_g->cntl_id);
     u8 channel = ataid / 2;
     u8 slave = ataid % 2;
-    u16 heads = GET_GLOBAL(Drives.drives[driveid].pchs.heads);
-    u16 cylinders = GET_GLOBAL(Drives.drives[driveid].pchs.cylinders);
-    u16 spt = GET_GLOBAL(Drives.drives[driveid].pchs.spt);
-    u64 sectors = GET_GLOBAL(Drives.drives[driveid].sectors);
+    u16 heads = GET_GLOBAL(drive_g->pchs.heads);
+    u16 cylinders = GET_GLOBAL(drive_g->pchs.cylinders);
+    u16 spt = GET_GLOBAL(drive_g->pchs.spt);
+    u64 sectors = GET_GLOBAL(drive_g->sectors);
+    const char *desc = NULL;
 
-    dprintf(1, "ata%d-%d: PCHS=%u/%d/%d translation="
-            , channel, slave, cylinders, heads, spt);
     switch (translation) {
+    default:
     case TRANSLATION_NONE:
-        dprintf(1, "none");
+        desc = "none";
         break;
     case TRANSLATION_LBA:
-        dprintf(1, "lba");
+        desc = "lba";
         spt = 63;
         if (sectors > 63*255*1024) {
             heads = 255;
@@ -87,7 +116,7 @@ setup_translation(int driveid)
         cylinders = sect / heads;
         break;
     case TRANSLATION_RECHS:
-        dprintf(1, "r-echs");
+        desc = "r-echs";
         // Take care not to overflow
         if (heads==16) {
             if (cylinders>61439)
@@ -98,7 +127,7 @@ setup_translation(int driveid)
         // then go through the large bitshift process
     case TRANSLATION_LARGE:
         if (translation == TRANSLATION_LARGE)
-            dprintf(1, "large");
+            desc = "large";
         while (cylinders > 1024) {
             cylinders >>= 1;
             heads <<= 1;
@@ -112,11 +141,15 @@ setup_translation(int driveid)
     // clip to 1024 cylinders in lchs
     if (cylinders > 1024)
         cylinders = 1024;
-    dprintf(1, " LCHS=%d/%d/%d\n", cylinders, heads, spt);
-
-    SET_GLOBAL(Drives.drives[driveid].lchs.heads, heads);
-    SET_GLOBAL(Drives.drives[driveid].lchs.cylinders, cylinders);
-    SET_GLOBAL(Drives.drives[driveid].lchs.spt, spt);
+    dprintf(1, "ata%d-%d: PCHS=%u/%d/%d translation=%s LCHS=%d/%d/%d\n"
+            , channel, slave
+            , drive_g->pchs.cylinders, drive_g->pchs.heads, drive_g->pchs.spt
+            , desc
+            , cylinders, heads, spt);
+
+    SET_GLOBAL(drive_g->lchs.heads, heads);
+    SET_GLOBAL(drive_g->lchs.cylinders, cylinders);
+    SET_GLOBAL(drive_g->lchs.spt, spt);
 }
 
 
@@ -126,20 +159,20 @@ setup_translation(int driveid)
 
 // Fill in Fixed Disk Parameter Table (located in ebda).
 static void
-fill_fdpt(int driveid)
+fill_fdpt(struct drive_s *drive_g, int hdid)
 {
-    if (driveid > 1)
+    if (hdid > 1)
         return;
 
-    u16 nlc   = GET_GLOBAL(Drives.drives[driveid].lchs.cylinders);
-    u16 nlh   = GET_GLOBAL(Drives.drives[driveid].lchs.heads);
-    u16 nlspt = GET_GLOBAL(Drives.drives[driveid].lchs.spt);
+    u16 nlc   = GET_GLOBAL(drive_g->lchs.cylinders);
+    u16 nlh   = GET_GLOBAL(drive_g->lchs.heads);
+    u16 nlspt = GET_GLOBAL(drive_g->lchs.spt);
 
-    u16 npc   = GET_GLOBAL(Drives.drives[driveid].pchs.cylinders);
-    u16 nph   = GET_GLOBAL(Drives.drives[driveid].pchs.heads);
-    u16 npspt = GET_GLOBAL(Drives.drives[driveid].pchs.spt);
+    u16 npc   = GET_GLOBAL(drive_g->pchs.cylinders);
+    u16 nph   = GET_GLOBAL(drive_g->pchs.heads);
+    u16 npspt = GET_GLOBAL(drive_g->pchs.spt);
 
-    struct fdpt_s *fdpt = &get_ebda_ptr()->fdpt[driveid];
+    struct fdpt_s *fdpt = &get_ebda_ptr()->fdpt[hdid];
     fdpt->precompensation = 0xffff;
     fdpt->drive_control_byte = 0xc0 | ((nph > 8) << 3);
     fdpt->landing_zone = npc;
@@ -162,7 +195,7 @@ fill_fdpt(int driveid)
     // Checksum structure.
     fdpt->checksum -= checksum(fdpt, sizeof(*fdpt));
 
-    if (driveid == 0)
+    if (hdid == 0)
         SET_IVT(0x41, SEGOFF(get_ebda_seg(), offsetof(
                                  struct extended_bios_data_area_s, fdpt[0])));
     else
@@ -172,68 +205,115 @@ fill_fdpt(int driveid)
 
 // Map a drive (that was registered via add_bcv_hd)
 void
-map_hd_drive(int driveid)
+map_hd_drive(struct drive_s *drive_g)
 {
     // fill hdidmap
     u8 hdcount = GET_BDA(hdcount);
     if (hdcount >= ARRAY_SIZE(Drives.idmap[0]))
         return;
-    dprintf(3, "Mapping hd driveid %d to %d\n", driveid, hdcount);
+    dprintf(3, "Mapping hd drive %p to %d\n", drive_g, hdcount);
+    int driveid = drive_g - Drives.drives;
     SET_GLOBAL(Drives.idmap[EXTTYPE_HD][hdcount], driveid);
     SET_BDA(hdcount, hdcount + 1);
 
     // Fill "fdpt" structure.
-    fill_fdpt(hdcount);
+    fill_fdpt(drive_g, hdcount);
+}
+
+// Find spot to add a drive
+static void
+add_ordered_drive(u8 *idmap, u8 *count, struct drive_s *drive_g)
+{
+    if (*count >= ARRAY_SIZE(Drives.idmap[0])) {
+        dprintf(1, "No room to map drive %p\n", drive_g);
+        return;
+    }
+    u8 *pos = &idmap[*count];
+    *count = *count + 1;
+    if (CONFIG_THREADS) {
+        // Add to idmap with assured drive order.
+        u8 *end = pos;
+        for (;;) {
+            u8 *prev = pos - 1;
+            if (prev < idmap)
+                break;
+            struct drive_s *prevdrive = &Drives.drives[*prev];
+            if (prevdrive->type < drive_g->type
+                || (prevdrive->type == drive_g->type
+                    && prevdrive->cntl_id < drive_g->cntl_id))
+                break;
+            pos--;
+        }
+        if (pos != end)
+            memmove(pos+1, pos, (void*)end-(void*)pos);
+    }
+    *pos = drive_g - Drives.drives;
 }
 
 // Map a cd
 void
-map_cd_drive(int driveid)
+map_cd_drive(struct drive_s *drive_g)
 {
-    // fill cdidmap
-    u8 cdcount = GET_GLOBAL(Drives.cdcount);
-    if (cdcount >= ARRAY_SIZE(Drives.idmap[0]))
-        return;
-    dprintf(3, "Mapping cd driveid %d to %d\n", driveid, cdcount);
-    SET_GLOBAL(Drives.idmap[EXTTYPE_CD][cdcount], driveid);
-    SET_GLOBAL(Drives.cdcount, cdcount+1);
+    dprintf(3, "Mapping cd drive %p\n", drive_g);
+    add_ordered_drive(Drives.idmap[EXTTYPE_CD], &Drives.cdcount, drive_g);
 }
 
 // Map a floppy
 void
-map_floppy_drive(int driveid)
+map_floppy_drive(struct drive_s *drive_g)
 {
     // fill idmap
-    u8 floppycount = GET_GLOBAL(Drives.floppycount);
-    if (floppycount >= ARRAY_SIZE(Drives.idmap[0]))
-        return;
-    dprintf(3, "Mapping floppy driveid %d to %d\n", driveid, floppycount);
-    SET_GLOBAL(Drives.idmap[EXTTYPE_FLOPPY][floppycount], driveid);
-    floppycount++;
-    SET_GLOBAL(Drives.floppycount, floppycount);
+    dprintf(3, "Mapping floppy drive %p\n", drive_g);
+    add_ordered_drive(Drives.idmap[EXTTYPE_FLOPPY], &Drives.floppycount
+                      , drive_g);
 
     // Update equipment word bits for floppy
-    if (floppycount == 1) {
+    if (Drives.floppycount == 1) {
         // 1 drive, ready for boot
         SETBITS_BDA(equipment_list_flags, 0x01);
         SET_BDA(floppy_harddisk_info, 0x07);
-    } else {
+    } else if (Drives.floppycount >= 2) {
         // 2 drives, ready for boot
         SETBITS_BDA(equipment_list_flags, 0x41);
         SET_BDA(floppy_harddisk_info, 0x77);
     }
 }
 
+// Show a one line description (without trailing newline) of a drive.
+void
+describe_drive(struct drive_s *drive_g)
+{
+    ASSERT32FLAT();
+    u8 type = GET_GLOBAL(drive_g->type);
+    switch (type) {
+    case DTYPE_FLOPPY:
+        describe_floppy(drive_g);
+        break;
+    case DTYPE_ATA:
+        describe_ata(drive_g);
+        break;
+    case DTYPE_ATAPI:
+        describe_atapi(drive_g);
+        break;
+    case DTYPE_RAMDISK:
+        describe_ramdisk(drive_g);
+        break;
+    default:
+        printf("Unknown");
+        break;
+    }
+}
+
 
 /****************************************************************
  * 16bit calling interface
  ****************************************************************/
 
 // Execute a disk_op request.
-static int
+int
 process_op(struct disk_op_s *op)
 {
-    u8 type = GET_GLOBAL(Drives.drives[op->driveid].type);
+    u8 type = GET_GLOBAL(op->drive_g->type);
     switch (type) {
     case DTYPE_FLOPPY:
         return process_floppy_op(op);
@@ -243,6 +323,8 @@ process_op(struct disk_op_s *op)
         return process_atapi_op(op);
     case DTYPE_RAMDISK:
         return process_ramdisk_op(op);
+    case DTYPE_CDEMU:
+        return process_cdemu_op(op);
     default:
         op->count = 0;
         return DISK_RET_EPARAM;
@@ -258,16 +340,12 @@ __send_disk_op(struct disk_op_s *op_far, u16 op_seg)
                , op_seg, op_far
                , sizeof(dop));
 
-    dprintf(DEBUG_HDL_13, "disk_op d=%d lba=%d buf=%p count=%d cmd=%d\n"
-            , dop.driveid, (u32)dop.lba, dop.buf_fl
+    dprintf(DEBUG_HDL_13, "disk_op d=%p lba=%d buf=%p count=%d cmd=%d\n"
+            , dop.drive_g, (u32)dop.lba, dop.buf_fl
             , dop.count, dop.command);
 
-    irq_enable();
-
     int status = process_op(&dop);
 
-    irq_disable();
-
     // Update count with total sectors transferred.
     SET_FARVAR(op_seg, op_far->count, dop.count);