2012-07-10 13:12:31 +04:00
|
|
|
/*
|
|
|
|
* Hard disk geometry utilities
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following copyright and
|
|
|
|
* permission notice:
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2016-01-18 21:01:42 +03:00
|
|
|
#include "qemu/osdep.h"
|
2014-10-07 15:59:18 +04:00
|
|
|
#include "sysemu/block-backend.h"
|
2018-02-11 12:36:01 +03:00
|
|
|
#include "qapi/qapi-types-block.h"
|
2016-03-15 19:22:36 +03:00
|
|
|
#include "qemu/bswap.h"
|
2013-02-05 20:06:20 +04:00
|
|
|
#include "hw/block/block.h"
|
2012-07-10 13:12:32 +04:00
|
|
|
#include "trace.h"
|
2012-07-10 13:12:31 +04:00
|
|
|
|
|
|
|
struct partition {
|
|
|
|
uint8_t boot_ind; /* 0x80 - active */
|
|
|
|
uint8_t head; /* starting head */
|
|
|
|
uint8_t sector; /* starting sector */
|
|
|
|
uint8_t cyl; /* starting cylinder */
|
|
|
|
uint8_t sys_ind; /* What partition type */
|
|
|
|
uint8_t end_head; /* end head */
|
|
|
|
uint8_t end_sector; /* end sector */
|
|
|
|
uint8_t end_cyl; /* end cylinder */
|
|
|
|
uint32_t start_sect; /* starting sector counting from 0 */
|
|
|
|
uint32_t nr_sects; /* nr of sectors in partition */
|
|
|
|
} QEMU_PACKED;
|
|
|
|
|
2023-07-14 14:32:24 +03:00
|
|
|
/* try to guess the disk logical geometry from the MS-DOS partition table.
|
2012-07-10 13:12:31 +04:00
|
|
|
Return 0 if OK, -1 if could not guess */
|
2014-10-07 15:59:18 +04:00
|
|
|
static int guess_disk_lchs(BlockBackend *blk,
|
2012-07-10 13:12:31 +04:00
|
|
|
int *pcylinders, int *pheads, int *psectors)
|
|
|
|
{
|
|
|
|
uint8_t buf[BDRV_SECTOR_SIZE];
|
|
|
|
int i, heads, sectors, cylinders;
|
|
|
|
struct partition *p;
|
|
|
|
uint32_t nr_sects;
|
|
|
|
uint64_t nb_sectors;
|
|
|
|
|
2014-10-07 15:59:18 +04:00
|
|
|
blk_get_geometry(blk, &nb_sectors);
|
2012-07-10 13:12:31 +04:00
|
|
|
|
block: Change blk_{pread,pwrite}() param order
Swap 'buf' and 'bytes' around for consistency with
blk_co_{pread,pwrite}(), and in preparation to implement these functions
using generated_co_wrapper.
Callers were updated using this Coccinelle script:
@@ expression blk, offset, buf, bytes, flags; @@
- blk_pread(blk, offset, buf, bytes, flags)
+ blk_pread(blk, offset, bytes, buf, flags)
@@ expression blk, offset, buf, bytes, flags; @@
- blk_pwrite(blk, offset, buf, bytes, flags)
+ blk_pwrite(blk, offset, bytes, buf, flags)
It had no effect on hw/block/nand.c, presumably due to the #if, so that
file was updated manually.
Overly-long lines were then fixed by hand.
Signed-off-by: Alberto Faria <afaria@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Hanna Reitz <hreitz@redhat.com>
Message-Id: <20220705161527.1054072-4-afaria@redhat.com>
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
2022-07-05 19:15:11 +03:00
|
|
|
if (blk_pread(blk, 0, BDRV_SECTOR_SIZE, buf, 0) < 0) {
|
2012-07-10 13:12:31 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2023-07-14 14:32:24 +03:00
|
|
|
/* test MS-DOS magic */
|
2012-07-10 13:12:31 +04:00
|
|
|
if (buf[510] != 0x55 || buf[511] != 0xaa) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
p = ((struct partition *)(buf + 0x1be)) + i;
|
|
|
|
nr_sects = le32_to_cpu(p->nr_sects);
|
|
|
|
if (nr_sects && p->end_head) {
|
|
|
|
/* We make the assumption that the partition terminates on
|
|
|
|
a cylinder boundary */
|
|
|
|
heads = p->end_head + 1;
|
|
|
|
sectors = p->end_sector & 63;
|
|
|
|
if (sectors == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cylinders = nb_sectors / (heads * sectors);
|
|
|
|
if (cylinders < 1 || cylinders > 16383) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*pheads = heads;
|
|
|
|
*psectors = sectors;
|
|
|
|
*pcylinders = cylinders;
|
2014-10-07 15:59:18 +04:00
|
|
|
trace_hd_geometry_lchs_guess(blk, cylinders, heads, sectors);
|
2012-07-10 13:12:31 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-10-07 15:59:18 +04:00
|
|
|
static void guess_chs_for_size(BlockBackend *blk,
|
2012-07-10 13:12:41 +04:00
|
|
|
uint32_t *pcyls, uint32_t *pheads, uint32_t *psecs)
|
2012-07-10 13:12:34 +04:00
|
|
|
{
|
|
|
|
uint64_t nb_sectors;
|
|
|
|
int cylinders;
|
|
|
|
|
2014-10-07 15:59:18 +04:00
|
|
|
blk_get_geometry(blk, &nb_sectors);
|
2012-07-10 13:12:34 +04:00
|
|
|
|
|
|
|
cylinders = nb_sectors / (16 * 63);
|
|
|
|
if (cylinders > 16383) {
|
|
|
|
cylinders = 16383;
|
|
|
|
} else if (cylinders < 2) {
|
|
|
|
cylinders = 2;
|
|
|
|
}
|
|
|
|
*pcyls = cylinders;
|
|
|
|
*pheads = 16;
|
|
|
|
*psecs = 63;
|
|
|
|
}
|
|
|
|
|
2014-10-07 15:59:18 +04:00
|
|
|
void hd_geometry_guess(BlockBackend *blk,
|
2012-07-10 13:12:41 +04:00
|
|
|
uint32_t *pcyls, uint32_t *pheads, uint32_t *psecs,
|
2012-07-10 13:12:37 +04:00
|
|
|
int *ptrans)
|
2012-07-10 13:12:31 +04:00
|
|
|
{
|
2012-07-10 13:12:35 +04:00
|
|
|
int cylinders, heads, secs, translation;
|
2015-02-16 14:47:58 +03:00
|
|
|
HDGeometry geo;
|
2012-07-10 13:12:31 +04:00
|
|
|
|
2015-02-16 14:47:58 +03:00
|
|
|
/* Try to probe the backing device geometry, otherwise fallback
|
|
|
|
to the old logic. (as of 12/2014 probing only succeeds on DASDs) */
|
|
|
|
if (blk_probe_geometry(blk, &geo) == 0) {
|
|
|
|
*pcyls = geo.cylinders;
|
|
|
|
*psecs = geo.sectors;
|
|
|
|
*pheads = geo.heads;
|
|
|
|
translation = BIOS_ATA_TRANSLATION_NONE;
|
|
|
|
} else if (guess_disk_lchs(blk, &cylinders, &heads, &secs) < 0) {
|
2012-07-10 13:12:33 +04:00
|
|
|
/* no LCHS guess: use a standard physical disk geometry */
|
2014-10-07 15:59:18 +04:00
|
|
|
guess_chs_for_size(blk, pcyls, pheads, psecs);
|
2012-07-10 13:12:53 +04:00
|
|
|
translation = hd_bios_chs_auto_trans(*pcyls, *pheads, *psecs);
|
2012-07-10 13:12:33 +04:00
|
|
|
} else if (heads > 16) {
|
|
|
|
/* LCHS guess with heads > 16 means that a BIOS LBA
|
|
|
|
translation was active, so a standard physical disk
|
|
|
|
geometry is OK */
|
2014-10-07 15:59:18 +04:00
|
|
|
guess_chs_for_size(blk, pcyls, pheads, psecs);
|
2012-07-10 13:12:37 +04:00
|
|
|
translation = *pcyls * *pheads <= 131072
|
|
|
|
? BIOS_ATA_TRANSLATION_LARGE
|
|
|
|
: BIOS_ATA_TRANSLATION_LBA;
|
2012-07-10 13:12:33 +04:00
|
|
|
} else {
|
|
|
|
/* LCHS guess with heads <= 16: use as physical geometry */
|
|
|
|
*pcyls = cylinders;
|
|
|
|
*pheads = heads;
|
|
|
|
*psecs = secs;
|
|
|
|
/* disable any translation to be in sync with
|
|
|
|
the logical geometry */
|
2012-07-10 13:12:37 +04:00
|
|
|
translation = BIOS_ATA_TRANSLATION_NONE;
|
|
|
|
}
|
|
|
|
if (ptrans) {
|
hw/block/hd-geometry: Do not override specified bios-chs-trans
For small disk images (<4 GiB), QEMU and SeaBIOS default to the
LARGE/ECHS disk translation method, but it is not uncommon for other
BIOS software to use LBA in these cases as well. Some operating
system boot loaders (e.g., NT 4) do not handle LARGE translations
outside of fixed configurations. See, e.g., Q154052:
"When starting an x86 based computer, Ntdetect.com retrieves and
stores Interrupt 13 information. . . If the disk controller is using a
32 sector/64 head translation scheme, this boundary will be 1 GB. If
the controller uses 63 sector/255 head translation [AUTHOR: i.e.,
LBA], the limit will be 4 GB."
To accommodate these situations, hd_geometry_guess() now follows the
disk translation specified by the user even when the ATA disk geometry
is guessed.
hd_geometry_guess():
* Only set the disk translation when translation is AUTO.
* Show the soon-to-be active translation (*ptrans) in the trace rather
than what was guessed.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/56
Buglink: https://bugs.launchpad.net/qemu/+bug/1745312
Signed-off-by: Lev Kujawski <lkujaw@member.fsf.org>
Message-Id: <20220707204045.999544-1-lkujaw@member.fsf.org>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2022-07-07 23:40:45 +03:00
|
|
|
if (*ptrans == BIOS_ATA_TRANSLATION_AUTO) {
|
|
|
|
*ptrans = translation;
|
|
|
|
} else {
|
|
|
|
/* Defer to the translation specified by the user. */
|
|
|
|
translation = *ptrans;
|
|
|
|
}
|
2012-07-10 13:12:31 +04:00
|
|
|
}
|
2014-10-07 15:59:18 +04:00
|
|
|
trace_hd_geometry_guess(blk, *pcyls, *pheads, *psecs, translation);
|
2012-07-10 13:12:31 +04:00
|
|
|
}
|
2012-07-10 13:12:53 +04:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|