1 /* $NetBSD: partutil.c,v 1.19 2025/04/13 14:00:59 jakllsch Exp $ */ 2 3 /*- 4 * Copyright (c) 2006 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __RCSID("$NetBSD: partutil.c,v 1.19 2025/04/13 14:00:59 jakllsch Exp $"); 34 35 #include <sys/types.h> 36 #include <sys/param.h> 37 #include <sys/disklabel.h> 38 #include <sys/disk.h> 39 #include <sys/ioctl.h> 40 #include <sys/stat.h> 41 42 43 #include <disktab.h> 44 #include <err.h> 45 #include <errno.h> 46 #include <fcntl.h> 47 #include <util.h> 48 #include <unistd.h> 49 #include <stdlib.h> 50 #include <string.h> 51 52 #include <prop/proplib.h> 53 54 #include "partutil.h" 55 56 /* 57 * Convert disklabel geometry info to disk_geom. 58 */ 59 static void 60 label2geom(struct disk_geom *geo, const struct disklabel *lp) 61 { 62 geo->dg_secperunit = lp->d_secperunit; 63 geo->dg_secsize = lp->d_secsize; 64 geo->dg_nsectors = lp->d_nsectors; 65 geo->dg_ntracks = lp->d_ntracks; 66 geo->dg_ncylinders = lp->d_ncylinders; 67 geo->dg_secpercyl = lp->d_secpercyl; 68 geo->dg_pcylinders = lp->d_ncylinders; 69 geo->dg_sparespertrack = lp->d_sparespertrack; 70 geo->dg_sparespercyl = lp->d_sparespercyl; 71 geo->dg_acylinders = lp->d_acylinders; 72 } 73 74 /* 75 * Set what we need to know about disk geometry. 76 */ 77 static void 78 dict2geom(struct disk_geom *geo, prop_dictionary_t dict) 79 { 80 (void)memset(geo, 0, sizeof(struct disk_geom)); 81 prop_dictionary_get_int64(dict, "sectors-per-unit", 82 &geo->dg_secperunit); 83 prop_dictionary_get_uint32(dict, "sector-size", &geo->dg_secsize); 84 prop_dictionary_get_uint32(dict, "sectors-per-track", 85 &geo->dg_nsectors); 86 prop_dictionary_get_uint32(dict, "tracks-per-cylinder", 87 &geo->dg_ntracks); 88 prop_dictionary_get_uint32(dict, "cylinders-per-unit", 89 &geo->dg_ncylinders); 90 geo->dg_physsecsize = geo->dg_secsize; 91 prop_dictionary_get_uint32(dict, "physical-sector-size", 92 &geo->dg_physsecsize); 93 prop_dictionary_get_uint32(dict, "aligned-sector", 94 &geo->dg_alignedsec); 95 } 96 97 98 int 99 getdiskinfo(const char *s, int fd, const char *dt, struct disk_geom *geo, 100 struct dkwedge_info *dkw) 101 { 102 struct disklabel lab; 103 struct disklabel *lp = &lab; 104 prop_dictionary_t disk_dict, geom_dict; 105 struct stat sb; 106 const struct partition *pp; 107 int ptn, error; 108 109 if (dt) { 110 lp = getdiskbyname(dt); 111 if (lp == NULL) 112 errx(1, "unknown disk type `%s'", dt); 113 } 114 115 /* Get disk description dictionary */ 116 disk_dict = NULL; 117 error = prop_dictionary_recv_ioctl(fd, DIOCGDISKINFO, &disk_dict); 118 119 /* fail quickly if the device does not exist at all */ 120 if (error == ENXIO) 121 return -1; 122 123 if (error) { 124 /* 125 * Ask for disklabel if DIOCGDISKINFO failed. This is 126 * compatibility call and can be removed when all devices 127 * will support DIOCGDISKINFO. 128 * cgd, ccd pseudo disk drives doesn't support DIOCGDDISKINFO 129 */ 130 if (ioctl(fd, DIOCGDINFO, lp) == -1) { 131 if (errno != ENXIO) 132 warn("DIOCGDINFO on %s failed", s); 133 return -1; 134 } 135 label2geom(geo, lp); 136 } else { 137 geom_dict = prop_dictionary_get(disk_dict, "geometry"); 138 dict2geom(geo, geom_dict); 139 } 140 if (disk_dict != NULL) 141 prop_object_release(disk_dict); 142 143 if (dkw == NULL) 144 return 0; 145 146 /* Get info about partition/wedge */ 147 if (ioctl(fd, DIOCGWEDGEINFO, dkw) != -1) { 148 /* DIOCGWEDGEINFO didn't fail, we're done */ 149 return 0; 150 } 151 152 if (ioctl(fd, DIOCGDINFO, lp) == -1) { 153 err(1, "Please implement DIOCGWEDGEINFO or " 154 "DIOCGDINFO for disk device %s", s); 155 } 156 157 /* DIOCGDINFO didn't fail */ 158 (void)memset(dkw, 0, sizeof(*dkw)); 159 160 if (stat(s, &sb) == -1) 161 return 0; 162 163 ptn = DISKPART(sb.st_rdev); 164 if (ptn < 0 || ptn >= lp->d_npartitions) 165 return 0; 166 167 pp = &lp->d_partitions[ptn]; 168 if (ptn != getrawpartition()) { 169 dkw->dkw_offset = pp->p_offset; 170 dkw->dkw_size = pp->p_size; 171 } else { 172 dkw->dkw_offset = 0; 173 dkw->dkw_size = geo->dg_secperunit; 174 } 175 dkw->dkw_parent[0] = '*'; 176 strlcpy(dkw->dkw_ptype, getfstypename(pp->p_fstype), 177 sizeof(dkw->dkw_ptype)); 178 179 return 0; 180 } 181 182 int 183 getdisksize(const char *name, u_int *secsize, off_t *mediasize) 184 { 185 char buf[MAXPATHLEN]; 186 struct disk_geom geo; 187 int fd, error; 188 189 if ((fd = opendisk(name, O_RDONLY, buf, sizeof(buf), 0)) == -1) 190 return -1; 191 192 error = getdiskinfo(name, fd, NULL, &geo, NULL); 193 close(fd); 194 if (error) 195 return error; 196 197 *secsize = geo.dg_secsize; 198 *mediasize = geo.dg_secsize * geo.dg_secperunit; 199 return 0; 200 } 201