hd-geometry: Compute BIOS CHS translation in one place
authorMarkus Armbruster <armbru@redhat.com>
Tue, 10 Jul 2012 09:12:53 +0000 (11:12 +0200)
committerKevin Wolf <kwolf@redhat.com>
Tue, 17 Jul 2012 14:48:32 +0000 (16:48 +0200)
Currently, it is split between hd_geometry_guess() and
pc_cmos_init_late().  Confusing.  info qtree shows the result of the
former.  Also confusing.

Fold the part done in pc_cmos_init_late() into hd_geometry_guess().

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
hw/block-common.h
hw/hd-geometry.c
hw/ide/core.c
hw/ide/qdev.c
hw/pc.c

index ec7810d7d4c3aaa65af19b4adc571d84db3ad1da..31e12bacedb801ff0da5e1a6cfdd0aa1d9b459b8 100644 (file)
@@ -24,5 +24,6 @@
 void hd_geometry_guess(BlockDriverState *bs,
                        uint32_t *pcyls, uint32_t *pheads, uint32_t *psecs,
                        int *ptrans);
+int hd_bios_chs_auto_trans(uint32_t cyls, uint32_t heads, uint32_t secs);
 
 #endif
index 74678a656b51a2629108917b08792f1afff7bb39..1cdb9fb75370b6270c6cd1a13d5a1a355988cc00 100644 (file)
@@ -125,7 +125,7 @@ void hd_geometry_guess(BlockDriverState *bs,
     if (guess_disk_lchs(bs, &cylinders, &heads, &secs) < 0) {
         /* no LCHS guess: use a standard physical disk geometry  */
         guess_chs_for_size(bs, pcyls, pheads, psecs);
-        translation = BIOS_ATA_TRANSLATION_AUTO;
+        translation = hd_bios_chs_auto_trans(*pcyls, *pheads, *psecs);
     } else if (heads > 16) {
         /* LCHS guess with heads > 16 means that a BIOS LBA
            translation was active, so a standard physical disk
@@ -148,3 +148,10 @@ void hd_geometry_guess(BlockDriverState *bs,
     }
     trace_hd_geometry_guess(bs, *pcyls, *pheads, *psecs, translation);
 }
+
+int hd_bios_chs_auto_trans(uint32_t cyls, uint32_t heads, uint32_t secs)
+{
+    return cyls <= 1024 && heads <= 16 && secs <= 63
+        ? BIOS_ATA_TRANSLATION_NONE
+        : BIOS_ATA_TRANSLATION_LBA;
+}
index bf1ce8968a664b98f3a0fdd873b625d679116b69..1ca7cdf7d02d71b6da6c8cb3f927b87c6d0aff91 100644 (file)
@@ -2091,6 +2091,8 @@ void ide_init2_with_non_qdev_drives(IDEBus *bus, DriveInfo *hd0,
             trans = dinfo->trans;
             if (!cyls && !heads && !secs) {
                 hd_geometry_guess(dinfo->bdrv, &cyls, &heads, &secs, &trans);
+            } else if (trans == BIOS_ATA_TRANSLATION_AUTO) {
+                trans = hd_bios_chs_auto_trans(cyls, heads, secs);
             }
             if (ide_init_drive(&bus->ifs[i], dinfo->bdrv,
                                dinfo->media_cd ? IDE_CD : IDE_HD, NULL,
index 84097fd4ece38337d5eff1f1ff085c197bedd030..de9db3bf9f37f2e184392a8fe12c4a5af3009a60 100644 (file)
@@ -171,6 +171,9 @@ static int ide_dev_initfn(IDEDevice *dev, IDEDriveKind kind)
         hd_geometry_guess(dev->conf.bs,
                           &dev->conf.cyls, &dev->conf.heads, &dev->conf.secs,
                           &dev->chs_trans);
+    } else if (dev->chs_trans == BIOS_ATA_TRANSLATION_AUTO) {
+        dev->chs_trans = hd_bios_chs_auto_trans(dev->conf.cyls,
+                                        dev->conf.heads, dev->conf.secs);
     }
 
     if (ide_init_drive(s, dev->conf.bs, kind,
diff --git a/hw/pc.c b/hw/pc.c
index 77b12b4c11807a20ff9b0f4e162b3d880cf25d80..598267af894e5f46b7486c994bc634a58c677490 100644 (file)
--- a/hw/pc.c
+++ b/hw/pc.c
@@ -290,7 +290,7 @@ static void pc_cmos_init_late(void *opaque)
     int16_t cylinders;
     int8_t heads, sectors;
     int val;
-    int i;
+    int i, trans;
 
     val = 0;
     if (ide_get_geometry(arg->idebus[0], 0,
@@ -313,20 +313,9 @@ static void pc_cmos_init_late(void *opaque)
            geometry can be different if a translation is done. */
         if (ide_get_geometry(arg->idebus[i / 2], i % 2,
                              &cylinders, &heads, &sectors) >= 0) {
-            int translation = ide_get_bios_chs_trans(arg->idebus[i / 2],
-                                                     i % 2);
-            if (translation == BIOS_ATA_TRANSLATION_AUTO) {
-                if (cylinders <= 1024 && heads <= 16 && sectors <= 63) {
-                    /* No translation. */
-                    translation = 0;
-                } else {
-                    /* LBA translation. */
-                    translation = 1;
-                }
-            } else {
-                translation--;
-            }
-            val |= translation << (i * 2);
+            trans = ide_get_bios_chs_trans(arg->idebus[i / 2], i % 2) - 1;
+            assert((trans & ~3) == 0);
+            val |= trans << (i * 2);
         }
     }
     rtc_set_memory(s, 0x39, val);