1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (C) 1994, 2001 by Joerg Wunsch, Dresden 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY 17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 22 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 26 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 27 * DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/fdcio.h> 34 #include <sys/file.h> 35 36 #include <err.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <sysexits.h> 41 #include <unistd.h> 42 43 #include "fdutil.h" 44 45 46 static int format, verbose, show = 1, showfmt; 47 static char *fmtstring; 48 49 static void showdev(enum fd_drivetype, const char *); 50 static void usage(void); 51 52 static void 53 usage(void) 54 { 55 errx(EX_USAGE, 56 "usage: fdcontrol [-F] [-d dbg] [-f fmt] [-s fmtstr] [-v] device"); 57 } 58 59 void 60 showdev(enum fd_drivetype type, const char *fname) 61 { 62 const char *name, *descr; 63 64 getname(type, &name, &descr); 65 if (verbose) 66 printf("%s: %s drive (%s)\n", fname, name, descr); 67 else 68 printf("%s\n", name); 69 } 70 71 int 72 main(int argc, char **argv) 73 { 74 enum fd_drivetype type; 75 struct fd_type ft, newft, *fdtp; 76 const char *name, *descr; 77 int fd, i, autofmt; 78 79 autofmt = 0; 80 while((i = getopt(argc, argv, "aFf:s:v")) != -1) 81 switch(i) { 82 83 case 'a': 84 autofmt = 1; 85 case 'F': 86 showfmt = 1; 87 show = 0; 88 break; 89 90 case 'f': 91 if (!strcmp(optarg, "auto")) { 92 format = -1; 93 } else if (getnum(optarg, &format)) { 94 fprintf(stderr, 95 "Bad argument %s to -f option; must be numeric\n", 96 optarg); 97 usage(); 98 } 99 show = 0; 100 break; 101 102 case 's': 103 fmtstring = optarg; 104 show = 0; 105 break; 106 107 case 'v': 108 verbose++; 109 break; 110 111 default: 112 usage(); 113 } 114 115 argc -= optind; 116 argv += optind; 117 118 if(argc != 1) 119 usage(); 120 121 if((fd = open(argv[0], O_RDONLY | O_NONBLOCK)) < 0) 122 err(EX_UNAVAILABLE, "open(%s)", argv[0]); 123 124 if (ioctl(fd, FD_GDTYPE, &type) == -1) 125 err(EX_OSERR, "ioctl(FD_GDTYPE)"); 126 if (ioctl(fd, FD_GTYPE, &ft) == -1) 127 err(EX_OSERR, "ioctl(FD_GTYPE)"); 128 129 if (show) { 130 showdev(type, argv[0]); 131 return (0); 132 } 133 134 if (autofmt) { 135 memset(&newft, 0, sizeof newft); 136 ft = newft; 137 } 138 139 if (format) { 140 getname(type, &name, &descr); 141 fdtp = get_fmt(format, type); 142 if (fdtp == 0) 143 errx(EX_USAGE, 144 "unknown format %d KB for drive type %s", 145 format, name); 146 ft = *fdtp; 147 } 148 149 if (fmtstring) { 150 parse_fmt(fmtstring, type, ft, &newft); 151 ft = newft; 152 } 153 154 if (showfmt) { 155 if (verbose) { 156 const char *s; 157 158 printf("%s: %d KB media type\n", argv[0], 159 (128 << ft.secsize) * ft.size / 1024); 160 printf("\tFormat:\t\t"); 161 print_fmt(ft); 162 if (ft.datalen != 0xff && 163 ft.datalen != (128 << ft.secsize)) 164 printf("\tData length:\t%d\n", ft.datalen); 165 printf("\tSector size:\t%d\n", 128 << ft.secsize); 166 printf("\tSectors/track:\t%d\n", ft.sectrac); 167 printf("\tHeads/cylinder:\t%d\n", ft.heads); 168 printf("\tCylinders/disk:\t%d\n", ft.tracks); 169 switch (ft.trans) { 170 case 0: printf("\tTransfer rate:\t500 kbps\n"); break; 171 case 1: printf("\tTransfer rate:\t300 kbps\n"); break; 172 case 2: printf("\tTransfer rate:\t250 kbps\n"); break; 173 case 3: printf("\tTransfer rate:\t1 Mbps\n"); break; 174 } 175 printf("\tSector gap:\t%d\n", ft.gap); 176 printf("\tFormat gap:\t%d\n", ft.f_gap); 177 printf("\tInterleave:\t%d\n", ft.f_inter); 178 printf("\tSide offset:\t%d\n", ft.offset_side2); 179 printf("\tFlags\t\t<"); 180 s = ""; 181 if (ft.flags & FL_MFM) { 182 printf("%sMFM", s); 183 s = ","; 184 } 185 if (ft.flags & FL_2STEP) { 186 printf("%s2STEP", s); 187 s = ","; 188 } 189 if (ft.flags & FL_PERPND) { 190 printf("%sPERPENDICULAR", s); 191 s = ","; 192 } 193 if (ft.flags & FL_AUTO) { 194 printf("%sAUTO", s); 195 s = ","; 196 } 197 printf(">\n"); 198 } else { 199 print_fmt(ft); 200 } 201 return (0); 202 } 203 204 if (format || fmtstring) { 205 if (ioctl(fd, FD_STYPE, &ft) == -1) 206 err(EX_OSERR, "ioctl(FD_STYPE)"); 207 return (0); 208 } 209 210 return 0; 211 } 212