1 /* $OpenBSD: fdformat.c,v 1.18 2007/11/26 09:28:34 martynas Exp $ */ 2 3 /* 4 * Copyright (C) 1992-1994 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 EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, 20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /* 30 * FreeBSD: 31 * format a floppy disk 32 * 33 * Added FD_GTYPE ioctl, verifying, proportional indicators. 34 * Serge Vakulenko, vak@zebub.msk.su 35 * Sat Dec 18 17:45:47 MSK 1993 36 * 37 * Final adaptation, change format/verify logic, add separate 38 * format gap/interleave values 39 * Andrew A. Chernov, ache@astral.msk.su 40 * Thu Jan 27 00:47:24 MSK 1994 41 */ 42 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <unistd.h> 46 #include <fcntl.h> 47 #include <string.h> 48 #include <ctype.h> 49 #include <err.h> 50 #include <util.h> 51 52 #include <errno.h> 53 #include <sys/types.h> 54 #include <sys/ioctl.h> 55 #include <machine/ioctl_fd.h> 56 57 extern const char *__progname; 58 59 static void 60 format_track(int fd, int cyl, int secs, int head, int rate, int gaplen, 61 int secsize, int fill, int interleave) 62 { 63 struct fd_formb f; 64 int i,j; 65 int il[FD_MAX_NSEC + 1]; 66 67 memset(il,0,sizeof il); 68 for(j = 0, i = 1; i <= secs; i++) { 69 while(il[(j%secs)+1]) 70 j++; 71 il[(j%secs)+1] = i; 72 j += interleave; 73 } 74 75 f.format_version = FD_FORMAT_VERSION; 76 f.head = head; 77 f.cyl = cyl; 78 f.transfer_rate = rate; 79 80 f.fd_formb_secshift = secsize; 81 f.fd_formb_nsecs = secs; 82 f.fd_formb_gaplen = gaplen; 83 f.fd_formb_fillbyte = fill; 84 for(i = 0; i < secs; i++) { 85 f.fd_formb_cylno(i) = cyl; 86 f.fd_formb_headno(i) = head; 87 f.fd_formb_secno(i) = il[i+1]; 88 f.fd_formb_secsize(i) = secsize; 89 } 90 if (ioctl(fd, FD_FORM, (caddr_t)&f) < 0) 91 err(1, "FD_FORM"); 92 } 93 94 static int 95 verify_track(int fd, int track, int tracksize) 96 { 97 static char *buf = 0; 98 static int bufsz = 0; 99 int fdopts = -1, ofdopts, rv = 0; 100 101 if (ioctl(fd, FD_GOPTS, &fdopts) < 0) 102 warn("FD_GOPTS"); 103 else { 104 ofdopts = fdopts; 105 fdopts |= FDOPT_NORETRY; 106 (void)ioctl(fd, FD_SOPTS, &fdopts); 107 } 108 109 if (bufsz < tracksize) { 110 if (buf) 111 free (buf); 112 bufsz = tracksize; 113 buf = 0; 114 } 115 if (! buf) 116 buf = malloc (bufsz); 117 if (! buf) { 118 fprintf (stderr, "\nfdformat: out of memory\n"); 119 exit (2); 120 } 121 if (lseek (fd, (off_t) track*tracksize, SEEK_SET) < 0) 122 rv = -1; 123 /* try twice reading it, without using the normal retrier */ 124 else if (read (fd, buf, tracksize) != tracksize 125 && read (fd, buf, tracksize) != tracksize) 126 rv = -1; 127 if (fdopts != -1) 128 (void)ioctl(fd, FD_SOPTS, &ofdopts); 129 return (rv); 130 } 131 132 static void 133 usage(void) 134 { 135 printf("usage: %s [-nqv] [-c cyls] [-F fillbyte] [-g gap3len] ", 136 __progname); 137 printf("[-h heads]\n"); 138 printf("\t[-i intleave] [-r rate] [-S secshft] [-s secs]\n"); 139 printf("\t[-t steps_per_track] device_name\n"); 140 printf("Options:\n"); 141 printf("\t-n\tdon't verify floppy after formatting\n"); 142 printf("\t-q\tsuppress any normal output, don't ask for confirmation\n"); 143 printf("\t-v\tdon't format, verify only\n"); 144 printf("\tdevname\tthe full name of floppy device or in short form fd0, fd1\n"); 145 printf("Obscure options:\n"); 146 printf("\t-c #\tspecify number of cylinders, 40 or 80\n"); 147 printf("\t-F #\tspecify fill byte\n"); 148 printf("\t-g #\tspecify gap length\n"); 149 printf("\t-h #\tspecify number of floppy heads, 1 or 2\n"); 150 printf("\t-i #\tspecify interleave factor\n"); 151 printf("\t-r #\tspecify data rate, 250, 300 or 500 kbps\n"); 152 printf("\t-S #\tspecify sector size, 0=128, 1=256, 2=512 bytes\n"); 153 printf("\t-s #\tspecify number of sectors per track, 9, 10, 15 or 18\n"); 154 printf("\t-t #\tnumber of steps per track\n"); 155 exit(2); 156 } 157 158 static int 159 yes(void) 160 { 161 char reply[256], *p; 162 163 for (;;) { 164 fflush(stdout); 165 if (!fgets(reply, sizeof(reply), stdin)) 166 return (0); 167 for (p=reply; *p==' ' || *p=='\t'; ++p) 168 continue; 169 if (*p=='y' || *p=='Y') 170 return (1); 171 if (*p=='n' || *p=='N' || *p=='\n' || *p=='\r') 172 return (0); 173 printf("Answer `yes' or `no': "); 174 } 175 } 176 177 int 178 main(int argc, char *argv[]) 179 { 180 int cyls = -1, secs = -1, heads = -1, intleave = -1; 181 int rate = -1, gaplen = -1, secsize = -1, steps = -1; 182 int fill = 0xf6, quiet = 0, verify = 1, verify_only = 0; 183 int fd, c, track, error, tracks_per_dot, bytes_per_track, errs; 184 char *devname; 185 struct fd_type fdt; 186 187 while((c = getopt(argc, argv, "c:s:h:r:g:S:F:t:i:qvn")) != -1) 188 switch (c) { 189 case 'c': /* # of cyls */ 190 cyls = atoi(optarg); 191 break; 192 193 case 's': /* # of secs per track */ 194 secs = atoi(optarg); 195 break; 196 197 case 'h': /* # of heads */ 198 heads = atoi(optarg); 199 break; 200 201 case 'r': /* transfer rate, kilobyte/sec */ 202 rate = atoi(optarg); 203 break; 204 205 case 'g': /* length of GAP3 to format with */ 206 gaplen = atoi(optarg); 207 break; 208 209 case 'S': /* sector size shift factor (1 << S)*128 */ 210 secsize = atoi(optarg); 211 break; 212 213 case 'F': /* fill byte, C-like notation allowed */ 214 fill = (int)strtol(optarg, (char **)0, 0); 215 break; 216 217 case 't': /* steps per track */ 218 steps = atoi(optarg); 219 break; 220 221 case 'i': /* interleave factor */ 222 intleave = atoi(optarg); 223 break; 224 225 case 'q': 226 quiet = 1; 227 break; 228 229 case 'n': 230 verify = 0; 231 break; 232 233 case 'v': 234 verify = 1; 235 verify_only = 1; 236 break; 237 238 case '?': default: 239 usage(); 240 } 241 242 if (optind != argc - 1) 243 usage(); 244 245 if ((fd = opendev(argv[optind], O_RDWR, OPENDEV_PART, &devname)) < 0) 246 err(1, "%s", devname); 247 248 if (ioctl(fd, FD_GTYPE, &fdt) < 0) 249 errx(1, "not a floppy disk: %s", devname); 250 251 switch (rate) { 252 case -1: 253 break; 254 case 250: 255 fdt.rate = FDC_250KBPS; 256 break; 257 case 300: 258 fdt.rate = FDC_300KBPS; 259 break; 260 case 500: 261 fdt.rate = FDC_500KBPS; 262 break; 263 default: 264 errx(1, "invalid transfer rate: %d", rate); 265 } 266 267 if (cyls >= 0) 268 fdt.tracks = cyls; 269 if (secs >= 0) 270 fdt.sectrac = secs; 271 if (fdt.sectrac > FD_MAX_NSEC) 272 errx(1, "too many sectors per track, max value is %d", 273 FD_MAX_NSEC); 274 if (heads >= 0) 275 fdt.heads = heads; 276 if (gaplen >= 0) 277 fdt.gap2 = gaplen; 278 if (secsize >= 0) 279 fdt.secsize = secsize; 280 if (steps >= 0) 281 fdt.step = steps; 282 283 bytes_per_track = fdt.sectrac * (1<<fdt.secsize) * 128; 284 tracks_per_dot = fdt.tracks * fdt.heads / 40; 285 if (tracks_per_dot == 0) 286 tracks_per_dot++; 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 devname); 293 } 294 else if (!quiet) { 295 printf("Format %dK floppy `%s'? (y/n): ", 296 fdt.tracks * fdt.heads * bytes_per_track / 1024, 297 devname); 298 if (!yes()) { 299 printf("Not confirmed.\n"); 300 exit(0); 301 } 302 } 303 304 /* 305 * Formatting. 306 */ 307 if (!quiet) { 308 printf("Processing "); 309 for (track = 0; track < fdt.tracks * fdt.heads; track++) { 310 if (!((track + 1) % tracks_per_dot)) 311 putchar('-'); 312 } 313 putchar('\r'); 314 printf("Processing "); 315 fflush(stdout); 316 } 317 318 error = errs = 0; 319 320 for (track = 0; track < fdt.tracks * fdt.heads; track++) { 321 if (!verify_only) { 322 format_track(fd, track / fdt.heads, fdt.sectrac, 323 track % fdt.heads, fdt.rate, fdt.gap2, 324 fdt.secsize, fill, 325 intleave >= 0 ? intleave : 1); 326 if (!quiet && !((track + 1) % tracks_per_dot)) { 327 putchar('F'); 328 fflush(stdout); 329 } 330 } 331 if (verify) { 332 if (verify_track(fd, track, bytes_per_track) < 0) 333 error = errs = 1; 334 if (!quiet && !((track + 1) % tracks_per_dot)) { 335 if (!verify_only) 336 putchar('\b'); 337 if (error) { 338 putchar('E'); 339 error = 0; 340 } 341 else 342 putchar('V'); 343 fflush(stdout); 344 } 345 } 346 } 347 close(fd); 348 if (!quiet) 349 printf(" done.\n"); 350 351 exit(errs); 352 } 353