1 /* $NetBSD: partutil.c,v 1.3 2008/04/28 20:23:08 martin 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.3 2008/04/28 20:23:08 martin 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 #include <disktab.h> 42 #include <fcntl.h> 43 #include <util.h> 44 #include <unistd.h> 45 #include <err.h> 46 #include <string.h> 47 #include <errno.h> 48 49 #include "partutil.h" 50 51 static void 52 label2geom(struct disk_geom *geo, const struct disklabel *lp) 53 { 54 geo->dg_secperunit = lp->d_secperunit; 55 geo->dg_secsize = lp->d_secsize; 56 geo->dg_nsectors = lp->d_nsectors; 57 geo->dg_ntracks = lp->d_ntracks; 58 geo->dg_ncylinders = lp->d_ncylinders; 59 geo->dg_secpercyl = lp->d_secpercyl; 60 geo->dg_pcylinders = lp->d_ncylinders; 61 geo->dg_sparespertrack = lp->d_sparespertrack; 62 geo->dg_sparespercyl = lp->d_sparespercyl; 63 geo->dg_acylinders = lp->d_acylinders; 64 } 65 66 static void 67 part2wedge(struct dkwedge_info *dkw, const struct disklabel *lp, const char *s) 68 { 69 struct stat sb; 70 const struct partition *pp; 71 int ptn; 72 73 (void)memset(dkw, 0, sizeof(*dkw)); 74 if (stat(s, &sb) == -1) 75 return; 76 77 ptn = strchr(s, '\0')[-1] - 'a'; 78 if (ptn >= lp->d_npartitions || ptn != DISKPART(sb.st_rdev)) 79 return; 80 81 pp = &lp->d_partitions[ptn]; 82 dkw->dkw_offset = pp->p_offset; 83 dkw->dkw_size = pp->p_size; 84 dkw->dkw_parent[0] = '*'; 85 switch (pp->p_fstype) { 86 default: 87 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_UNKNOWN); 88 break; 89 case FS_UNUSED: 90 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_UNUSED); 91 break; 92 case FS_SWAP: 93 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_SWAP); 94 break; 95 case FS_BSDFFS: 96 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_FFS); 97 break; 98 case FS_BSDLFS: 99 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_LFS); 100 break; 101 case FS_EX2FS: 102 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_EXT2FS); 103 break; 104 case FS_ISO9660: 105 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_ISO9660); 106 break; 107 case FS_ADOS: 108 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_AMIGADOS); 109 break; 110 case FS_HFS: 111 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_APPLEHFS); 112 break; 113 case FS_MSDOS: 114 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_FAT); 115 break; 116 case FS_FILECORE: 117 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_FILECORE); 118 break; 119 case FS_APPLEUFS: 120 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_APPLEUFS); 121 break; 122 case FS_NTFS: 123 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_NTFS); 124 break; 125 } 126 } 127 128 int 129 getdiskinfo(const char *s, int fd, const char *dt, struct disk_geom *geo, 130 struct dkwedge_info *dkw) 131 { 132 struct disklabel lab; 133 struct disklabel *lp = &lab; 134 char parent[1024]; 135 136 if (dt) { 137 lp = getdiskbyname(dt); 138 if (lp == NULL) 139 errx(1, "%s: unknown disk type", dt); 140 goto part; 141 } 142 143 if (ioctl(fd, DIOCGDINFO, lp) == -1) { 144 if (errno == ENOTTY) { 145 int pfd; 146 if (ioctl(fd, DIOCGWEDGEINFO, dkw) == -1) { 147 warn("ioctl (DIOCGWEDGEINFO)"); 148 goto bad; 149 } 150 pfd = opendisk(dkw->dkw_parent, O_RDONLY, 151 parent, sizeof(parent), 0); 152 if (pfd == -1) { 153 warn("Cannot open `%s'", dkw->dkw_parent); 154 goto bad; 155 } 156 if (ioctl(pfd, DIOCGDINFO, lp) != -1) { 157 (void)close(pfd); 158 goto label; 159 } else { 160 int serrno = errno; 161 (void)close(pfd); 162 errno = serrno; 163 } 164 } 165 warn("ioctl (DIOCGDINFO)"); 166 goto bad; 167 } 168 part: 169 part2wedge(dkw, lp, s); 170 label: 171 label2geom(geo, lp); 172 return 0; 173 bad: 174 return -1; 175 } 176