2 * Hard disk geometry utilities
4 * Copyright (C) 2012 Red Hat, Inc.
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
9 * This file incorporates work covered by the following copyright and
12 * Copyright (c) 2003 Fabrice Bellard
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to deal
16 * in the Software without restriction, including without limitation the rights
17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 * copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33 #include "sysemu/block-backend.h"
34 #include "hw/block/block.h"
38 uint8_t boot_ind
; /* 0x80 - active */
39 uint8_t head
; /* starting head */
40 uint8_t sector
; /* starting sector */
41 uint8_t cyl
; /* starting cylinder */
42 uint8_t sys_ind
; /* What partition type */
43 uint8_t end_head
; /* end head */
44 uint8_t end_sector
; /* end sector */
45 uint8_t end_cyl
; /* end cylinder */
46 uint32_t start_sect
; /* starting sector counting from 0 */
47 uint32_t nr_sects
; /* nr of sectors in partition */
50 /* try to guess the disk logical geometry from the MSDOS partition table.
51 Return 0 if OK, -1 if could not guess */
52 static int guess_disk_lchs(BlockBackend
*blk
,
53 int *pcylinders
, int *pheads
, int *psectors
)
55 uint8_t buf
[BDRV_SECTOR_SIZE
];
56 int i
, heads
, sectors
, cylinders
;
61 blk_get_geometry(blk
, &nb_sectors
);
64 * The function will be invoked during startup not only in sync I/O mode,
65 * but also in async I/O mode. So the I/O throttling function has to
66 * be disabled temporarily here, not permanently.
68 if (blk_read_unthrottled(blk
, 0, buf
, 1) < 0) {
71 /* test msdos magic */
72 if (buf
[510] != 0x55 || buf
[511] != 0xaa) {
75 for (i
= 0; i
< 4; i
++) {
76 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
77 nr_sects
= le32_to_cpu(p
->nr_sects
);
78 if (nr_sects
&& p
->end_head
) {
79 /* We make the assumption that the partition terminates on
80 a cylinder boundary */
81 heads
= p
->end_head
+ 1;
82 sectors
= p
->end_sector
& 63;
86 cylinders
= nb_sectors
/ (heads
* sectors
);
87 if (cylinders
< 1 || cylinders
> 16383) {
92 *pcylinders
= cylinders
;
93 trace_hd_geometry_lchs_guess(blk
, cylinders
, heads
, sectors
);
100 static void guess_chs_for_size(BlockBackend
*blk
,
101 uint32_t *pcyls
, uint32_t *pheads
, uint32_t *psecs
)
106 blk_get_geometry(blk
, &nb_sectors
);
108 cylinders
= nb_sectors
/ (16 * 63);
109 if (cylinders
> 16383) {
111 } else if (cylinders
< 2) {
119 void hd_geometry_guess(BlockBackend
*blk
,
120 uint32_t *pcyls
, uint32_t *pheads
, uint32_t *psecs
,
123 int cylinders
, heads
, secs
, translation
;
125 if (guess_disk_lchs(blk
, &cylinders
, &heads
, &secs
) < 0) {
126 /* no LCHS guess: use a standard physical disk geometry */
127 guess_chs_for_size(blk
, pcyls
, pheads
, psecs
);
128 translation
= hd_bios_chs_auto_trans(*pcyls
, *pheads
, *psecs
);
129 } else if (heads
> 16) {
130 /* LCHS guess with heads > 16 means that a BIOS LBA
131 translation was active, so a standard physical disk
133 guess_chs_for_size(blk
, pcyls
, pheads
, psecs
);
134 translation
= *pcyls
* *pheads
<= 131072
135 ? BIOS_ATA_TRANSLATION_LARGE
136 : BIOS_ATA_TRANSLATION_LBA
;
138 /* LCHS guess with heads <= 16: use as physical geometry */
142 /* disable any translation to be in sync with
143 the logical geometry */
144 translation
= BIOS_ATA_TRANSLATION_NONE
;
147 *ptrans
= translation
;
149 trace_hd_geometry_guess(blk
, *pcyls
, *pheads
, *psecs
, translation
);
152 int hd_bios_chs_auto_trans(uint32_t cyls
, uint32_t heads
, uint32_t secs
)
154 return cyls
<= 1024 && heads
<= 16 && secs
<= 63
155 ? BIOS_ATA_TRANSLATION_NONE
156 : BIOS_ATA_TRANSLATION_LBA
;