1 /* $NetBSD: partutil.c,v 1.9 2009/07/16 23:50:32 dyoung 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.9 2009/07/16 23:50:32 dyoung Exp $"); 34 35 #include <sys/types.h> 36 #include <sys/disklabel.h> 37 #include <sys/disk.h> 38 #include <sys/ioctl.h> 39 #include <sys/stat.h> 40 41 42 #include <disktab.h> 43 #include <err.h> 44 #include <errno.h> 45 #include <fcntl.h> 46 #include <util.h> 47 #include <unistd.h> 48 #include <stdlib.h> 49 #include <string.h> 50 51 #include <prop/proplib.h> 52 53 #include "partutil.h" 54 55 /* 56 * Convert disklabel geometry info to disk_geom. 57 */ 58 static void 59 label2geom(struct disk_geom *geo, const struct disklabel *lp) 60 { 61 geo->dg_secperunit = lp->d_secperunit; 62 geo->dg_secsize = lp->d_secsize; 63 geo->dg_nsectors = lp->d_nsectors; 64 geo->dg_ntracks = lp->d_ntracks; 65 geo->dg_ncylinders = lp->d_ncylinders; 66 geo->dg_secpercyl = lp->d_secpercyl; 67 geo->dg_pcylinders = lp->d_ncylinders; 68 geo->dg_sparespertrack = lp->d_sparespertrack; 69 geo->dg_sparespercyl = lp->d_sparespercyl; 70 geo->dg_acylinders = lp->d_acylinders; 71 } 72 73 /* 74 * Set what we need to know about disk geometry. 75 */ 76 static void 77 dict2geom(struct disk_geom *geo, prop_dictionary_t dict) 78 { 79 memset(geo, 0, sizeof(struct disk_geom)); 80 prop_dictionary_get_int64(dict, "sectors-per-unit", &geo->dg_secperunit); 81 prop_dictionary_get_uint32(dict, "sector-size", &geo->dg_secsize); 82 prop_dictionary_get_uint32(dict, "sectors-per-track", &geo->dg_nsectors); 83 prop_dictionary_get_uint32(dict, "tracks-per-cylinder", &geo->dg_ntracks); 84 prop_dictionary_get_uint32(dict, "cylinders-per-unit", &geo->dg_ncylinders); 85 } 86 87 88 static void 89 part2wedge(struct dkwedge_info *dkw, const struct disklabel *lp, const char *s) 90 { 91 struct stat sb; 92 const struct partition *pp; 93 int ptn; 94 95 (void)memset(dkw, 0, sizeof(*dkw)); 96 if (stat(s, &sb) == -1) 97 return; 98 99 ptn = strchr(s, '\0')[-1] - 'a'; 100 if ((unsigned)ptn >= lp->d_npartitions || 101 (devminor_t)ptn != DISKPART(sb.st_rdev)) 102 return; 103 104 pp = &lp->d_partitions[ptn]; 105 dkw->dkw_offset = pp->p_offset; 106 dkw->dkw_size = pp->p_size; 107 dkw->dkw_parent[0] = '*'; 108 switch (pp->p_fstype) { 109 default: 110 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_UNKNOWN); 111 break; 112 case FS_UNUSED: 113 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_UNUSED); 114 break; 115 case FS_SWAP: 116 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_SWAP); 117 break; 118 case FS_BSDFFS: 119 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_FFS); 120 break; 121 case FS_BSDLFS: 122 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_LFS); 123 break; 124 case FS_EX2FS: 125 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_EXT2FS); 126 break; 127 case FS_ISO9660: 128 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_ISO9660); 129 break; 130 case FS_ADOS: 131 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_AMIGADOS); 132 break; 133 case FS_HFS: 134 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_APPLEHFS); 135 break; 136 case FS_MSDOS: 137 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_FAT); 138 break; 139 case FS_FILECORE: 140 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_FILECORE); 141 break; 142 case FS_APPLEUFS: 143 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_APPLEUFS); 144 break; 145 case FS_NTFS: 146 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_NTFS); 147 break; 148 } 149 } 150 151 int 152 getdiskinfo(const char *s, int fd, const char *dt, struct disk_geom *geo, 153 struct dkwedge_info *dkw) 154 { 155 struct disklabel lab; 156 struct disklabel *lp = &lab; 157 prop_dictionary_t disk_dict, geom_dict; 158 159 if (dt) { 160 lp = getdiskbyname(dt); 161 if (lp == NULL) 162 errx(1, "%s: unknown disk type", dt); 163 } 164 165 /* Get disk description dictionary */ 166 if (prop_dictionary_recv_ioctl(fd, DIOCGDISKINFO, &disk_dict)) { 167 /* 168 * Ask for disklabel if DIOCGDISKINFO failed. This is 169 * compatibility call and can be removed when all devices 170 * will support DIOCGDISKINFO. 171 * cgd, ccd pseudo disk drives doesn't support DIOCGDDISKINFO 172 */ 173 if (ioctl(fd, DIOCGDINFO, lp) == -1) { 174 printf("DIOCGDINFO on %s failed\n", s); 175 return -1; 176 } 177 label2geom(geo, lp); 178 } else { 179 geom_dict = prop_dictionary_get(disk_dict, "geometry"); 180 dict2geom(geo, geom_dict); 181 } 182 183 /* Get info about partition/wedge */ 184 if (ioctl(fd, DIOCGWEDGEINFO, dkw) == -1) { 185 if (ioctl(fd, DIOCGDINFO, lp) == -1) 186 errx(errno, "Please implement DIOCGWEDGEINFO or " 187 "DIOCGDINFO for disk device %s\n", s); 188 189 part2wedge(dkw, lp, s); 190 } 191 192 return 0; 193 } 194