Introduce MODESEGMENT define; rename VISIBLE32 to VISIBLE32FLAT.
[seabios.git] / src / block.c
index a62536caa8707b343852ec4e9c4bccbf07780ffb..3fb5e91da9ecb446d6c394f0920339b0d67e0da6 100644 (file)
@@ -86,15 +86,15 @@ setup_translation(struct drive_s *drive_g)
     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;
@@ -116,7 +116,7 @@ setup_translation(struct drive_s *drive_g)
         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)
@@ -127,7 +127,7 @@ setup_translation(struct drive_s *drive_g)
         // 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;
@@ -141,7 +141,11 @@ setup_translation(struct drive_s *drive_g)
     // clip to 1024 cylinders in lchs
     if (cylinders > 1024)
         cylinders = 1024;
-    dprintf(1, " LCHS=%d/%d/%d\n", cylinders, heads, 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);
@@ -216,18 +220,42 @@ map_hd_drive(struct drive_s *drive_g)
     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(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 drive %p to %d\n", drive_g, cdcount);
-    int driveid = drive_g - Drives.drives;
-    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
@@ -235,21 +263,16 @@ void
 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 drive %p to %d\n", drive_g, floppycount);
-    int driveid = drive_g - Drives.drives;
-    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);
@@ -260,7 +283,7 @@ map_floppy_drive(struct drive_s *drive_g)
 void
 describe_drive(struct drive_s *drive_g)
 {
-    ASSERT32();
+    ASSERT32FLAT();
     u8 type = GET_GLOBAL(drive_g->type);
     switch (type) {
     case DTYPE_FLOPPY: