1 /* 2 * Hard disk geometry utilities 3 * 4 * Copyright (C) 2012 Red Hat, Inc. 5 * 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. 8 * 9 * This file incorporates work covered by the following copyright and 10 * permission notice: 11 * 12 * Copyright (c) 2003 Fabrice Bellard 13 * 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: 20 * 21 * The above copyright notice and this permission notice shall be included in 22 * all copies or substantial portions of the Software. 23 * 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 30 * THE SOFTWARE. 31 */ 32 33 #include "qemu/osdep.h" 34 #include "system/block-backend.h" 35 #include "qapi/qapi-types-block.h" 36 #include "hw/block/block.h" 37 #include "trace.h" 38 39 struct partition { 40 uint8_t boot_ind; /* 0x80 - active */ 41 uint8_t head; /* starting head */ 42 uint8_t sector; /* starting sector */ 43 uint8_t cyl; /* starting cylinder */ 44 uint8_t sys_ind; /* What partition type */ 45 uint8_t end_head; /* end head */ 46 uint8_t end_sector; /* end sector */ 47 uint8_t end_cyl; /* end cylinder */ 48 uint32_t start_sect; /* starting sector counting from 0 */ 49 uint32_t nr_sects; /* nr of sectors in partition */ 50 } QEMU_PACKED; 51 52 /* try to guess the disk logical geometry from the MS-DOS partition table. 53 Return 0 if OK, -1 if could not guess */ 54 static int guess_disk_lchs(BlockBackend *blk, 55 int *pcylinders, int *pheads, int *psectors) 56 { 57 uint8_t buf[BDRV_SECTOR_SIZE]; 58 int i, heads, sectors, cylinders; 59 struct partition *p; 60 uint32_t nr_sects; 61 uint64_t nb_sectors; 62 63 blk_get_geometry(blk, &nb_sectors); 64 65 if (blk_pread(blk, 0, BDRV_SECTOR_SIZE, buf, 0) < 0) { 66 return -1; 67 } 68 /* test MS-DOS magic */ 69 if (buf[510] != 0x55 || buf[511] != 0xaa) { 70 return -1; 71 } 72 for (i = 0; i < 4; i++) { 73 p = ((struct partition *)(buf + 0x1be)) + i; 74 nr_sects = le32_to_cpu(p->nr_sects); 75 if (nr_sects && p->end_head) { 76 /* We make the assumption that the partition terminates on 77 a cylinder boundary */ 78 heads = p->end_head + 1; 79 sectors = p->end_sector & 63; 80 if (sectors == 0) { 81 continue; 82 } 83 cylinders = nb_sectors / (heads * sectors); 84 if (cylinders < 1 || cylinders > 16383) { 85 continue; 86 } 87 *pheads = heads; 88 *psectors = sectors; 89 *pcylinders = cylinders; 90 trace_hd_geometry_lchs_guess(blk, cylinders, heads, sectors); 91 return 0; 92 } 93 } 94 return -1; 95 } 96 97 static void guess_chs_for_size(BlockBackend *blk, 98 uint32_t *pcyls, uint32_t *pheads, uint32_t *psecs) 99 { 100 uint64_t nb_sectors; 101 int cylinders; 102 103 blk_get_geometry(blk, &nb_sectors); 104 105 cylinders = nb_sectors / (16 * 63); 106 if (cylinders > 16383) { 107 cylinders = 16383; 108 } else if (cylinders < 2) { 109 cylinders = 2; 110 } 111 *pcyls = cylinders; 112 *pheads = 16; 113 *psecs = 63; 114 } 115 116 void hd_geometry_guess(BlockBackend *blk, 117 uint32_t *pcyls, uint32_t *pheads, uint32_t *psecs, 118 int *ptrans) 119 { 120 int cylinders, heads, secs, translation; 121 HDGeometry geo; 122 123 /* Try to probe the backing device geometry, otherwise fallback 124 to the old logic. (as of 12/2014 probing only succeeds on DASDs) */ 125 if (blk_probe_geometry(blk, &geo) == 0) { 126 *pcyls = geo.cylinders; 127 *psecs = geo.sectors; 128 *pheads = geo.heads; 129 translation = BIOS_ATA_TRANSLATION_NONE; 130 } else if (guess_disk_lchs(blk, &cylinders, &heads, &secs) < 0) { 131 /* no LCHS guess: use a standard physical disk geometry */ 132 guess_chs_for_size(blk, pcyls, pheads, psecs); 133 translation = hd_bios_chs_auto_trans(*pcyls, *pheads, *psecs); 134 } else if (heads > 16) { 135 /* LCHS guess with heads > 16 means that a BIOS LBA 136 translation was active, so a standard physical disk 137 geometry is OK */ 138 guess_chs_for_size(blk, pcyls, pheads, psecs); 139 translation = *pcyls * *pheads <= 131072 140 ? BIOS_ATA_TRANSLATION_LARGE 141 : BIOS_ATA_TRANSLATION_LBA; 142 } else { 143 /* LCHS guess with heads <= 16: use as physical geometry */ 144 *pcyls = cylinders; 145 *pheads = heads; 146 *psecs = secs; 147 /* disable any translation to be in sync with 148 the logical geometry */ 149 translation = BIOS_ATA_TRANSLATION_NONE; 150 } 151 if (ptrans) { 152 if (*ptrans == BIOS_ATA_TRANSLATION_AUTO) { 153 *ptrans = translation; 154 } else { 155 /* Defer to the translation specified by the user. */ 156 translation = *ptrans; 157 } 158 } 159 trace_hd_geometry_guess(blk, *pcyls, *pheads, *psecs, translation); 160 } 161 162 int hd_bios_chs_auto_trans(uint32_t cyls, uint32_t heads, uint32_t secs) 163 { 164 return cyls <= 1024 && heads <= 16 && secs <= 63 165 ? BIOS_ATA_TRANSLATION_NONE 166 : BIOS_ATA_TRANSLATION_LBA; 167 } 168