1 /* $NetBSD: vnconfig.c,v 1.14 1997/09/29 06:43:14 enami Exp $ */ 2 3 /*- 4 * Copyright (c) 1997 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe. 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 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * Copyright (c) 1993 University of Utah. 41 * Copyright (c) 1990, 1993 42 * The Regents of the University of California. All rights reserved. 43 * 44 * This code is derived from software contributed to Berkeley by 45 * the Systems Programming Group of the University of Utah Computer 46 * Science Department. 47 * 48 * Redistribution and use in source and binary forms, with or without 49 * modification, are permitted provided that the following conditions 50 * are met: 51 * 1. Redistributions of source code must retain the above copyright 52 * notice, this list of conditions and the following disclaimer. 53 * 2. Redistributions in binary form must reproduce the above copyright 54 * notice, this list of conditions and the following disclaimer in the 55 * documentation and/or other materials provided with the distribution. 56 * 3. All advertising materials mentioning features or use of this software 57 * must display the following acknowledgement: 58 * This product includes software developed by the University of 59 * California, Berkeley and its contributors. 60 * 4. Neither the name of the University nor the names of its contributors 61 * may be used to endorse or promote products derived from this software 62 * without specific prior written permission. 63 * 64 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 65 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 66 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 67 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 68 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 69 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 70 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 71 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 72 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 73 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 74 * SUCH DAMAGE. 75 * 76 * from: Utah $Hdr: vnconfig.c 1.1 93/12/15$ 77 * 78 * @(#)vnconfig.c 8.1 (Berkeley) 12/15/93 79 */ 80 81 #include <sys/param.h> 82 #include <sys/ioctl.h> 83 #include <sys/mount.h> 84 #include <sys/stat.h> 85 #include <sys/buf.h> 86 #include <sys/disklabel.h> 87 #include <sys/disk.h> 88 89 #include <dev/vndvar.h> 90 91 #include <err.h> 92 #include <errno.h> 93 #include <fcntl.h> 94 #include <stdio.h> 95 #include <stdlib.h> 96 #include <string.h> 97 #include <unistd.h> 98 #include <util.h> 99 100 #define VND_CONFIG 1 101 #define VND_UNCONFIG 2 102 103 int verbose = 0; 104 char *tabname; 105 106 int config __P((char *, char *, char *, int)); 107 char *rawdevice __P((char *)); 108 int getgeom __P((struct vndgeom *, char *)); 109 void usage __P((void)); 110 111 int 112 main(argc, argv) 113 int argc; 114 char *argv[]; 115 { 116 int ch, rv, action = VND_CONFIG; 117 118 while ((ch = getopt(argc, argv, "ct:uv")) != -1) { 119 switch (ch) { 120 case 'c': 121 action = VND_CONFIG; 122 break; 123 case 't': 124 tabname = optarg; 125 break; 126 case 'u': 127 action = VND_UNCONFIG; 128 break; 129 case 'v': 130 verbose = 1; 131 break; 132 default: 133 case '?': 134 usage(); 135 /* NOTREACHED */ 136 } 137 } 138 argc -= optind; 139 argv += optind; 140 141 if (action == VND_CONFIG) { 142 if ((argc < 2 || argc > 3) || 143 (argc == 3 && tabname != NULL)) 144 usage(); 145 rv = config(argv[0], argv[1], (argc == 3) ? argv[2] : NULL, 146 action); 147 } else { 148 if (argc != 1 || tabname != NULL) 149 usage(); 150 rv = config(argv[0], NULL, NULL, action); 151 } 152 exit(rv); 153 } 154 155 int 156 config(dev, file, geom, action) 157 char *dev, *file, *geom; 158 int action; 159 { 160 struct vnd_ioctl vndio; 161 struct disklabel *lp; 162 char rdev[MAXPATHLEN + 1]; 163 int fd, rv; 164 165 fd = opendisk(dev, O_RDWR, rdev, sizeof(rdev), 0); 166 if (fd < 0) { 167 warn("%s: opendisk", rdev); 168 return (1); 169 } 170 171 memset(&vndio, 0, sizeof(vndio)); 172 #ifdef __GNUC__ 173 rv = 0; /* XXX */ 174 #endif 175 176 vndio.vnd_file = file; 177 if (geom != NULL) { 178 rv = getgeom(&vndio.vnd_geom, geom); 179 if (rv != 0) 180 errx(1, "invalid geometry: %s", geom); 181 vndio.vnd_flags = VNDIOF_HASGEOM; 182 } else if (tabname != NULL) { 183 lp = getdiskbyname(tabname); 184 if (lp == NULL) 185 errx(1, "unknown disk type: %s", tabname); 186 vndio.vnd_geom.vng_secsize = lp->d_secsize; 187 vndio.vnd_geom.vng_nsectors = lp->d_nsectors; 188 vndio.vnd_geom.vng_ntracks = lp->d_ntracks; 189 vndio.vnd_geom.vng_ncylinders = lp->d_ncylinders; 190 vndio.vnd_flags = VNDIOF_HASGEOM; 191 } 192 193 /* 194 * Clear (un-configure) the device 195 */ 196 if (action == VND_UNCONFIG) { 197 rv = ioctl(fd, VNDIOCCLR, &vndio); 198 if (rv) 199 warn("%s: VNDIOCCLR", rdev); 200 else if (verbose) 201 printf("%s: cleared\n", rdev); 202 } 203 /* 204 * Configure the device 205 */ 206 if (action == VND_CONFIG) { 207 rv = ioctl(fd, VNDIOCSET, &vndio); 208 if (rv) 209 warn("%s: VNDIOCSET", rdev); 210 else if (verbose) { 211 printf("%s: %d bytes on %s", rdev, vndio.vnd_size, 212 file); 213 if (vndio.vnd_flags & VNDIOF_HASGEOM) 214 printf(" using geometry %d/%d/%d/%d", 215 vndio.vnd_geom.vng_secsize, 216 vndio.vnd_geom.vng_nsectors, 217 vndio.vnd_geom.vng_ntracks, 218 vndio.vnd_geom.vng_ncylinders); 219 printf("\n"); 220 } 221 222 } 223 224 (void) close(fd); 225 fflush(stdout); 226 return (rv < 0); 227 } 228 229 int 230 getgeom(vng, cp) 231 struct vndgeom *vng; 232 char *cp; 233 { 234 char *secsize, *nsectors, *ntracks, *ncylinders; 235 236 #define GETARG(arg) \ 237 do { \ 238 if (cp == NULL || *cp == '\0') \ 239 return (1); \ 240 arg = strsep(&cp, "/"); \ 241 if (arg == NULL) \ 242 return (1); \ 243 } while (0) 244 245 GETARG(secsize); 246 GETARG(nsectors); 247 GETARG(ntracks); 248 GETARG(ncylinders); 249 250 #undef GETARG 251 252 /* Too many? */ 253 if (cp != NULL) 254 return (1); 255 256 #define CVTARG(str, num) \ 257 do { \ 258 num = strtol(str, &cp, 10); \ 259 if (*cp != '\0') \ 260 return (1); \ 261 } while (0) 262 263 CVTARG(secsize, vng->vng_secsize); 264 CVTARG(nsectors, vng->vng_nsectors); 265 CVTARG(ntracks, vng->vng_ntracks); 266 CVTARG(ncylinders, vng->vng_ncylinders); 267 268 #undef CVTARG 269 270 return (0); 271 } 272 273 void 274 usage() 275 { 276 277 (void)fprintf(stderr, "%s%s", 278 "usage: vnconfig [-c] [-t typename] [-v] special-file" 279 " regular-file [geomspec]\n", 280 " vnconfig -u [-v] special-file\n"); 281 exit(1); 282 } 283