1 /* 2 * Copyright (C) 1992-1994,2001 by Joerg Wunsch, Dresden 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, 18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 23 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 * POSSIBILITY OF SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 #include <sys/types.h> 30 #include <sys/fdcio.h> 31 #include <sys/stat.h> 32 33 #include <ctype.h> 34 #include <err.h> 35 #include <errno.h> 36 #include <fcntl.h> 37 #include <paths.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <strings.h> 41 #include <sysexits.h> 42 #include <unistd.h> 43 44 #include "fdutil.h" 45 46 static void 47 format_track(int fd, int cyl, int secs, int head, int rate, 48 int gaplen, int secsize, int fill, int interleave, 49 int offset) 50 { 51 struct fd_formb f; 52 int i, j, il[FD_MAX_NSEC + 1]; 53 54 memset(il, 0, sizeof il); 55 for(j = 0, i = 1 + offset; i <= secs + offset; i++) { 56 while(il[(j % secs) + 1]) 57 j++; 58 il[(j % secs) + 1] = i; 59 j += interleave; 60 } 61 62 f.format_version = FD_FORMAT_VERSION; 63 f.head = head; 64 f.cyl = cyl; 65 f.transfer_rate = rate; 66 67 f.fd_formb_secshift = secsize; 68 f.fd_formb_nsecs = secs; 69 f.fd_formb_gaplen = gaplen; 70 f.fd_formb_fillbyte = fill; 71 for(i = 0; i < secs; i++) { 72 f.fd_formb_cylno(i) = cyl; 73 f.fd_formb_headno(i) = head; 74 f.fd_formb_secno(i) = il[i+1]; 75 f.fd_formb_secsize(i) = secsize; 76 } 77 if(ioctl(fd, FD_FORM, (caddr_t)&f) < 0) 78 err(EX_OSERR, "ioctl(FD_FORM)"); 79 } 80 81 static int 82 verify_track(int fd, int track, int tracksize) 83 { 84 static char *buf; 85 static int bufsz; 86 int fdopts = -1, ofdopts, rv = 0; 87 88 if (ioctl(fd, FD_GOPTS, &fdopts) < 0) 89 warn("warning: ioctl(FD_GOPTS)"); 90 else { 91 ofdopts = fdopts; 92 fdopts |= FDOPT_NORETRY; 93 (void)ioctl(fd, FD_SOPTS, &fdopts); 94 } 95 96 if (bufsz < tracksize) 97 buf = realloc(buf, bufsz = tracksize); 98 if (buf == 0) 99 errx(EX_UNAVAILABLE, "out of memory"); 100 if (lseek (fd, (long) track * tracksize, 0) < 0) 101 rv = -1; 102 /* try twice reading it, without using the normal retrier */ 103 else if (read (fd, buf, tracksize) != tracksize 104 && read (fd, buf, tracksize) != tracksize) 105 rv = -1; 106 if (fdopts != -1) 107 (void)ioctl(fd, FD_SOPTS, &ofdopts); 108 return (rv); 109 } 110 111 static void 112 usage (void) 113 { 114 errx(EX_USAGE, 115 "usage: fdformat [-F fill] [-f fmt] [-s fmtstr] [-nqvy] device"); 116 } 117 118 static int 119 yes (void) 120 { 121 char reply[256], *p; 122 123 reply[sizeof(reply) - 1] = 0; 124 for (;;) { 125 fflush(stdout); 126 if (!fgets (reply, sizeof(reply) - 1, stdin)) 127 return (0); 128 for (p=reply; *p==' ' || *p=='\t'; ++p) 129 continue; 130 if (*p=='y' || *p=='Y') 131 return (1); 132 if (*p=='n' || *p=='N' || *p=='\n' || *p=='\r') 133 return (0); 134 printf("Answer `yes' or `no': "); 135 } 136 } 137 138 int 139 main(int argc, char **argv) 140 { 141 enum fd_drivetype type; 142 struct fd_type fdt, newft, *fdtp; 143 struct stat sb; 144 #define MAXPRINTERRS 10 145 struct fdc_status fdcs[MAXPRINTERRS]; 146 int format, fill, quiet, verify, verify_only, confirm; 147 int fd, c, i, track, error, tracks_per_dot, bytes_per_track, errs; 148 int fdopts, flags; 149 char *fmtstring, *device; 150 const char *name, *descr; 151 152 format = quiet = verify_only = confirm = 0; 153 verify = 1; 154 fill = 0xf6; 155 fmtstring = 0; 156 157 while((c = getopt(argc, argv, "F:f:nqs:vy")) != -1) 158 switch(c) { 159 case 'F': /* fill byte */ 160 if (getnum(optarg, &fill)) { 161 fprintf(stderr, 162 "Bad argument %s to -F option; must be numeric\n", 163 optarg); 164 usage(); 165 } 166 break; 167 168 case 'f': /* format in kilobytes */ 169 if (getnum(optarg, &format)) { 170 fprintf(stderr, 171 "Bad argument %s to -f option; must be numeric\n", 172 optarg); 173 usage(); 174 } 175 break; 176 177 case 'n': /* don't verify */ 178 verify = 0; 179 break; 180 181 case 'q': /* quiet */ 182 quiet = 1; 183 break; 184 185 case 's': /* format string with detailed options */ 186 fmtstring = optarg; 187 break; 188 189 case 'v': /* verify only */ 190 verify = 1; 191 verify_only = 1; 192 break; 193 194 case 'y': /* confirm */ 195 confirm = 1; 196 break; 197 198 default: 199 usage(); 200 } 201 202 if(optind != argc - 1) 203 usage(); 204 205 if (stat(argv[optind], &sb) == -1 && errno == ENOENT) { 206 /* try prepending _PATH_DEV */ 207 device = malloc(strlen(argv[optind] + sizeof _PATH_DEV + 1)); 208 if (device == 0) 209 errx(EX_UNAVAILABLE, "out of memory"); 210 strcpy(device, _PATH_DEV); 211 strcat(device, argv[optind]); 212 if (stat(device, &sb) == -1) { 213 free(device); 214 device = argv[optind]; /* let it fail below */ 215 } 216 } else { 217 device = argv[optind]; 218 } 219 220 if ((fd = open(device, O_RDWR | O_NONBLOCK)) < 0) 221 err(EX_OSERR, "open(%s)", device); 222 223 /* 224 * Device initialization. 225 * 226 * First, get the device type descriptor. This tells us about 227 * the media geometry data we need to format a medium. It also 228 * lets us know quickly whether the device name actually points 229 * to a floppy disk drive. 230 * 231 * Then, obtain any drive options. We're mainly interested to 232 * see whether we're currently working on a device with media 233 * density autoselection (FDOPT_AUTOSEL). Then, we add the 234 * device option to tell the kernel not to log media errors, 235 * since we can handle them ourselves. If the device does 236 * media density autoselection, we then need to set the device 237 * type appropriately, since by opening with O_NONBLOCK we 238 * told the driver to bypass media autoselection (otherwise we 239 * wouldn't stand a chance to format an unformatted or damaged 240 * medium). We do not attempt to set the media type on any 241 * other devices since this is a privileged operation. For the 242 * same reason, specifying -f and -s options is only possible 243 * for autoselecting devices. 244 * 245 * Finally, we are ready to turn off O_NONBLOCK, and start to 246 * actually format something. 247 */ 248 if(ioctl(fd, FD_GTYPE, &fdt) < 0) 249 errx(EX_OSERR, "not a floppy disk: %s", device); 250 if (ioctl(fd, FD_GDTYPE, &type) == -1) 251 err(EX_OSERR, "ioctl(FD_GDTYPE)"); 252 if (ioctl(fd, FD_GOPTS, &fdopts) == -1) 253 err(EX_OSERR, "ioctl(FD_GOPTS)"); 254 fdopts |= FDOPT_NOERRLOG; 255 if (ioctl(fd, FD_SOPTS, &fdopts) == -1) 256 err(EX_OSERR, "ioctl(FD_SOPTS, FDOPT_NOERRLOG)"); 257 if (format) { 258 getname(type, &name, &descr); 259 fdtp = get_fmt(format, type); 260 if (fdtp == 0) 261 errx(EX_USAGE, 262 "unknown format %d KB for drive type %s", 263 format, name); 264 fdt = *fdtp; 265 } 266 if (fmtstring) { 267 parse_fmt(fmtstring, type, fdt, &newft); 268 fdt = newft; 269 } 270 if (fdopts & FDOPT_AUTOSEL) { 271 if (ioctl(fd, FD_STYPE, &fdt) < 0) 272 err(EX_OSERR, "ioctl(FD_STYPE)"); 273 } else if (fmtstring || format) { 274 errx(EX_USAGE, 275 "-f fmt or -s fmtstr is only allowed for autoselecting devices"); 276 } 277 if ((flags = fcntl(fd, F_GETFL, 0)) == -1) 278 err(EX_OSERR, "fcntl(F_GETFL)"); 279 flags &= ~O_NONBLOCK; 280 if (fcntl(fd, F_SETFL, flags) == -1) 281 err(EX_OSERR, "fcntl(F_SETFL)"); 282 283 bytes_per_track = fdt.sectrac * (128 << fdt.secsize); 284 285 /* XXX 20/40 = 0.5 */ 286 tracks_per_dot = (fdt.tracks * fdt.heads + 20) / 40; 287 288 if (verify_only) { 289 if(!quiet) 290 printf("Verify %dK floppy `%s'.\n", 291 fdt.tracks * fdt.heads * bytes_per_track / 1024, 292 device); 293 } 294 else if(!quiet && !confirm) { 295 printf("Format %dK floppy `%s'? (y/n): ", 296 fdt.tracks * fdt.heads * bytes_per_track / 1024, 297 device); 298 if(!yes()) { 299 printf("Not confirmed.\n"); 300 return (EX_UNAVAILABLE); 301 } 302 } 303 304 /* 305 * Formatting. 306 */ 307 if(!quiet) { 308 printf("Processing "); 309 for (i = 0; i < (fdt.tracks * fdt.heads) / tracks_per_dot; i++) 310 putchar('-'); 311 printf("\rProcessing "); 312 fflush(stdout); 313 } 314 315 error = errs = 0; 316 317 for (track = 0; track < fdt.tracks * fdt.heads; track++) { 318 if (!verify_only) { 319 format_track(fd, track / fdt.heads, fdt.sectrac, 320 track % fdt.heads, fdt.trans, fdt.f_gap, 321 fdt.secsize, fill, fdt.f_inter, 322 track % fdt.heads? fdt.offset_side2: 0); 323 if(!quiet && !((track + 1) % tracks_per_dot)) { 324 putchar('F'); 325 fflush(stdout); 326 } 327 } 328 if (verify) { 329 if (verify_track(fd, track, bytes_per_track) < 0) { 330 error = 1; 331 if (errs < MAXPRINTERRS && errno == EIO) { 332 if (ioctl(fd, FD_GSTAT, fdcs + errs) == 333 -1) 334 errx(EX_IOERR, 335 "floppy IO error, but no FDC status"); 336 errs++; 337 } 338 } 339 if(!quiet && !((track + 1) % tracks_per_dot)) { 340 if (!verify_only) 341 putchar('\b'); 342 if (error) { 343 putchar('E'); 344 error = 0; 345 } 346 else 347 putchar('V'); 348 fflush(stdout); 349 } 350 } 351 } 352 if(!quiet) 353 printf(" done.\n"); 354 355 if (!quiet && errs) { 356 fflush(stdout); 357 fprintf(stderr, "Errors encountered:\nCyl Head Sect Error\n"); 358 for (i = 0; i < errs && i < MAXPRINTERRS; i++) { 359 fprintf(stderr, " %2d %2d %2d ", 360 fdcs[i].status[3], fdcs[i].status[4], 361 fdcs[i].status[5]); 362 printstatus(fdcs + i, 1); 363 putc('\n', stderr); 364 } 365 if (errs >= MAXPRINTERRS) 366 fprintf(stderr, "(Further errors not printed.)\n"); 367 } 368 369 return errs != 0; 370 } 371