fdformat.c revision 8857
11022Sache/* 21533Sjoerg * Copyright (C) 1992-1994 by Joerg Wunsch, Dresden 31022Sache * All rights reserved. 41022Sache * 51022Sache * Redistribution and use in source and binary forms, with or without 61022Sache * modification, are permitted provided that the following conditions 71022Sache * are met: 81022Sache * 1. Redistributions of source code must retain the above copyright 91022Sache * notice, this list of conditions and the following disclaimer. 101022Sache * 2. Redistributions in binary form must reproduce the above copyright 111022Sache * notice, this list of conditions and the following disclaimer in the 121022Sache * documentation and/or other materials provided with the distribution. 131022Sache * 141533Sjoerg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 151533Sjoerg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 161533Sjoerg * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 171533Sjoerg * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, 181533Sjoerg * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 191533Sjoerg * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 201533Sjoerg * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 211533Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 221533Sjoerg * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 231533Sjoerg * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 241533Sjoerg * POSSIBILITY OF SUCH DAMAGE. 251022Sache */ 261022Sache 271022Sache/* 281022Sache * FreeBSD: 291022Sache * format a floppy disk 308857Srgrimes * 311022Sache * Added FD_GTYPE ioctl, verifying, proportional indicators. 321022Sache * Serge Vakulenko, vak@zebub.msk.su 331022Sache * Sat Dec 18 17:45:47 MSK 1993 341022Sache * 351022Sache * Final adaptation, change format/verify logic, add separate 361022Sache * format gap/interleave values 371022Sache * Andrew A. Chernov, ache@astral.msk.su 381022Sache * Thu Jan 27 00:47:24 MSK 1994 391022Sache */ 401022Sache 411022Sache#include <stdio.h> 421022Sache#include <stdlib.h> 431022Sache#include <unistd.h> 441022Sache#include <fcntl.h> 451022Sache#include <strings.h> 461022Sache#include <ctype.h> 471022Sache 481022Sache#include <errno.h> 491022Sache#include <machine/ioctl_fd.h> 501022Sache 511022Sachestatic void 521022Sacheformat_track(int fd, int cyl, int secs, int head, int rate, 531137Sache int gaplen, int secsize, int fill,int interleave) 541022Sache{ 551022Sache struct fd_formb f; 561137Sache register int i,j; 571138Sache int il[FD_MAX_NSEC + 1]; 581022Sache 591137Sache memset(il,0,sizeof il); 601137Sache for(j = 0, i = 1; i <= secs; i++) { 611137Sache while(il[(j%secs)+1]) j++; 621137Sache il[(j%secs)+1] = i; 631137Sache j += interleave; 641137Sache } 651137Sache 661022Sache f.format_version = FD_FORMAT_VERSION; 671022Sache f.head = head; 681022Sache f.cyl = cyl; 691022Sache f.transfer_rate = rate; 701022Sache 711022Sache f.fd_formb_secshift = secsize; 721022Sache f.fd_formb_nsecs = secs; 731022Sache f.fd_formb_gaplen = gaplen; 741022Sache f.fd_formb_fillbyte = fill; 751022Sache for(i = 0; i < secs; i++) { 761022Sache f.fd_formb_cylno(i) = cyl; 771022Sache f.fd_formb_headno(i) = head; 781137Sache f.fd_formb_secno(i) = il[i+1]; 791022Sache f.fd_formb_secsize(i) = secsize; 801022Sache } 811022Sache if(ioctl(fd, FD_FORM, (caddr_t)&f) < 0) { 821022Sache perror("\nfdformat: ioctl(FD_FORM)"); 831022Sache exit(1); 841022Sache } 851022Sache} 861022Sache 871022Sachestatic int 881022Sacheverify_track(int fd, int track, int tracksize) 891022Sache{ 901022Sache static char *buf = 0; 911022Sache static int bufsz = 0; 921533Sjoerg int fdopts = -1, ofdopts, rv = 0; 931022Sache 941533Sjoerg if (ioctl(fd, FD_GOPTS, &fdopts) < 0) 951533Sjoerg perror("warning: ioctl(FD_GOPTS)"); 961533Sjoerg else { 971533Sjoerg ofdopts = fdopts; 981533Sjoerg fdopts |= FDOPT_NORETRY; 991533Sjoerg (void)ioctl(fd, FD_SOPTS, &fdopts); 1001533Sjoerg } 1018857Srgrimes 1021022Sache if (bufsz < tracksize) { 1031022Sache if (buf) 1041022Sache free (buf); 1051022Sache bufsz = tracksize; 1061022Sache buf = 0; 1071022Sache } 1081022Sache if (! buf) 1091022Sache buf = malloc (bufsz); 1101022Sache if (! buf) { 1111022Sache fprintf (stderr, "\nfdformat: out of memory\n"); 1121022Sache exit (2); 1131022Sache } 1141022Sache if (lseek (fd, (long) track*tracksize, 0) < 0) 1151533Sjoerg rv = -1; 1161533Sjoerg /* try twice reading it, without using the normal retrier */ 1171533Sjoerg else if (read (fd, buf, tracksize) != tracksize 1181533Sjoerg && read (fd, buf, tracksize) != tracksize) 1191533Sjoerg rv = -1; 1201533Sjoerg if(fdopts != -1) 1211533Sjoerg (void)ioctl(fd, FD_SOPTS, &ofdopts); 1221533Sjoerg return (rv); 1231022Sache} 1241022Sache 1251022Sachestatic const char * 1261022Sachemakename(const char *arg, const char *suffix) 1271022Sache{ 1281022Sache static char namebuff[20]; /* big enough for "/dev/rfd0a"... */ 1291022Sache 1301022Sache memset(namebuff, 0, 20); 1311022Sache if(*arg == '\0') /* ??? */ 1321022Sache return arg; 1331022Sache if(*arg == '/') /* do not convert absolute pathnames */ 1341022Sache return arg; 1351022Sache strcpy(namebuff, "/dev/r"); 1361022Sache strncat(namebuff, arg, 3); 1371022Sache strcat(namebuff, suffix); 1381022Sache return namebuff; 1391022Sache} 1401022Sache 1411022Sachestatic void 1421533Sjoergusage (void) 1431022Sache{ 1441022Sache printf("Usage:\n\tfdformat [-q] [-n | -v] [-f #] [-c #] [-s #] [-h #]\n"); 1451022Sache printf("\t\t [-r #] [-g #] [-i #] [-S #] [-F #] [-t #] devname\n"); 1461022Sache printf("Options:\n"); 1471022Sache printf("\t-q\tsupress any normal output, don't ask for confirmation\n"); 1481022Sache printf("\t-n\tdon't verify floppy after formatting\n"); 1491022Sache printf("\t-v\tdon't format, verify only\n"); 1501022Sache printf("\t-f #\tspecify desired floppy capacity, in kilobytes;\n"); 1511022Sache printf("\t\tvalid choices are 360, 720, 800, 820, 1200, 1440, 1480, 1720\n"); 1521022Sache printf("\tdevname\tthe full name of floppy device or in short form fd0, fd1\n"); 1531022Sache printf("Obscure options:\n"); 1541022Sache printf("\t-c #\tspecify number of cylinders, 40 or 80\n"); 1551022Sache printf("\t-s #\tspecify number of sectors per track, 9, 10, 15 or 18\n"); 1561022Sache printf("\t-h #\tspecify number of floppy heads, 1 or 2\n"); 1571022Sache printf("\t-r #\tspecify data rate, 250, 300 or 500 kbps\n"); 1581022Sache printf("\t-g #\tspecify gap length\n"); 1591022Sache printf("\t-i #\tspecify interleave factor\n"); 1601022Sache printf("\t-S #\tspecify sector size, 0=128, 1=256, 2=512 bytes\n"); 1611022Sache printf("\t-F #\tspecify fill byte\n"); 1621022Sache printf("\t-t #\tnumber of steps per track\n"); 1631022Sache exit(2); 1641022Sache} 1651022Sache 1661022Sachestatic int 1671533Sjoergyes (void) 1681022Sache{ 1691022Sache char reply [256], *p; 1701022Sache 1711022Sache reply[sizeof(reply)-1] = 0; 1721022Sache for (;;) { 1731022Sache fflush(stdout); 1741022Sache if (! fgets (reply, sizeof(reply)-1, stdin)) 1751022Sache return (0); 1761022Sache for (p=reply; *p==' ' || *p=='\t'; ++p) 1771022Sache continue; 1781022Sache if (*p=='y' || *p=='Y') 1791022Sache return (1); 1801022Sache if (*p=='n' || *p=='N' || *p=='\n' || *p=='\r') 1811022Sache return (0); 1821022Sache printf("Answer `yes' or `no': "); 1831022Sache } 1841022Sache} 1851022Sache 1861022Sacheint 1871022Sachemain(int argc, char **argv) 1881022Sache{ 1891022Sache int format = -1, cyls = -1, secs = -1, heads = -1, intleave = -1; 1901022Sache int rate = -1, gaplen = -1, secsize = -1, steps = -1; 1911022Sache int fill = 0xf6, quiet = 0, verify = 1, verify_only = 0; 1921022Sache int fd, c, track, error, tracks_per_dot, bytes_per_track, errs; 1931022Sache const char *devname, *suffix; 1941022Sache struct fd_type fdt; 1951022Sache 1961022Sache while((c = getopt(argc, argv, "f:c:s:h:r:g:S:F:t:i:qvn")) != -1) 1971022Sache switch(c) { 1981022Sache case 'f': /* format in kilobytes */ 1991022Sache format = atoi(optarg); 2001022Sache break; 2011022Sache 2021022Sache case 'c': /* # of cyls */ 2031022Sache cyls = atoi(optarg); 2041022Sache break; 2051022Sache 2061022Sache case 's': /* # of secs per track */ 2071022Sache secs = atoi(optarg); 2081022Sache break; 2091022Sache 2101022Sache case 'h': /* # of heads */ 2111022Sache heads = atoi(optarg); 2121022Sache break; 2131022Sache 2141022Sache case 'r': /* transfer rate, kilobyte/sec */ 2151022Sache rate = atoi(optarg); 2161022Sache break; 2171022Sache 2181022Sache case 'g': /* length of GAP3 to format with */ 2191022Sache gaplen = atoi(optarg); 2201022Sache break; 2211022Sache 2221022Sache case 'S': /* sector size shift factor (1 << S)*128 */ 2231022Sache secsize = atoi(optarg); 2241022Sache break; 2251022Sache 2261022Sache case 'F': /* fill byte, C-like notation allowed */ 2271022Sache fill = (int)strtol(optarg, (char **)0, 0); 2281022Sache break; 2291022Sache 2301022Sache case 't': /* steps per track */ 2311022Sache steps = atoi(optarg); 2321022Sache break; 2331022Sache 2341022Sache case 'i': /* interleave factor */ 2351022Sache intleave = atoi(optarg); 2361022Sache break; 2371022Sache 2381022Sache case 'q': 2391022Sache quiet = 1; 2401022Sache break; 2411022Sache 2421022Sache case 'n': 2431022Sache verify = 0; 2441022Sache break; 2451022Sache 2461022Sache case 'v': 2471022Sache verify = 1; 2481022Sache verify_only = 1; 2491022Sache break; 2501022Sache 2511022Sache case '?': default: 2521022Sache usage(); 2531022Sache } 2541022Sache 2551022Sache if(optind != argc - 1) 2561022Sache usage(); 2571022Sache 2581022Sache switch(format) { 2591022Sache default: 2601022Sache fprintf(stderr, "fdformat: bad floppy size: %dK\n", format); 2611022Sache exit(2); 2621022Sache case -1: suffix = ""; break; 2631022Sache case 360: suffix = ".360"; break; 2641022Sache case 720: suffix = ".720"; break; 2651022Sache case 800: suffix = ".800"; break; 2661022Sache case 820: suffix = ".820"; break; 2671022Sache case 1200: suffix = ".1200"; break; 2681022Sache case 1440: suffix = ".1440"; break; 2691022Sache case 1480: suffix = ".1480"; break; 2701022Sache case 1720: suffix = ".1720"; break; 2711022Sache } 2721022Sache 2731022Sache devname = makename(argv[optind], suffix); 2741022Sache 2751022Sache if((fd = open(devname, O_RDWR)) < 0) { 2761022Sache perror(devname); 2771022Sache exit(1); 2781022Sache } 2791022Sache 2801022Sache if(ioctl(fd, FD_GTYPE, &fdt) < 0) { 2811022Sache fprintf(stderr, "fdformat: not a floppy disk: %s\n", devname); 2821022Sache exit(1); 2831022Sache } 2841022Sache 2851022Sache switch(rate) { 2861022Sache case -1: break; 2871022Sache case 250: fdt.trans = FDC_250KBPS; break; 2881022Sache case 300: fdt.trans = FDC_300KBPS; break; 2891022Sache case 500: fdt.trans = FDC_500KBPS; break; 2901022Sache default: 2911022Sache fprintf(stderr, "fdformat: invalid transfer rate: %d\n", rate); 2921022Sache exit(2); 2931022Sache } 2941022Sache 2951022Sache if (cyls >= 0) fdt.tracks = cyls; 2961022Sache if (secs >= 0) fdt.sectrac = secs; 2971137Sache if (fdt.sectrac > FD_MAX_NSEC) { 2981137Sache fprintf(stderr, "fdformat: too many sectors per track, max value is %d\n", FD_MAX_NSEC); 2991137Sache exit(2); 3001137Sache } 3011022Sache if (heads >= 0) fdt.heads = heads; 3021022Sache if (gaplen >= 0) fdt.f_gap = gaplen; 3031022Sache if (secsize >= 0) fdt.secsize = secsize; 3041022Sache if (steps >= 0) fdt.steptrac = steps; 3051022Sache if (intleave >= 0) fdt.f_inter = intleave; 3061022Sache 3071022Sache bytes_per_track = fdt.sectrac * (1<<fdt.secsize) * 128; 3081022Sache tracks_per_dot = fdt.tracks * fdt.heads / 40; 3091022Sache 3101022Sache if (verify_only) { 3111022Sache if(!quiet) 3121022Sache printf("Verify %dK floppy `%s'.\n", 3131022Sache fdt.tracks * fdt.heads * bytes_per_track / 1024, 3141022Sache devname); 3151022Sache } 3161022Sache else if(!quiet) { 3171022Sache printf("Format %dK floppy `%s'? (y/n): ", 3181022Sache fdt.tracks * fdt.heads * bytes_per_track / 1024, 3191022Sache devname); 3201022Sache if(! yes ()) { 3211022Sache printf("Not confirmed.\n"); 3221022Sache return 0; 3231022Sache } 3241022Sache } 3251022Sache 3261022Sache /* 3271022Sache * Formatting. 3281022Sache */ 3291022Sache if(!quiet) { 3301022Sache printf("Processing ----------------------------------------\r"); 3311022Sache printf("Processing "); 3321022Sache fflush(stdout); 3331022Sache } 3341022Sache 3351022Sache error = errs = 0; 3361022Sache 3371022Sache for (track = 0; track < fdt.tracks * fdt.heads; track++) { 3381022Sache if (!verify_only) { 3391183Srgrimes format_track(fd, track / fdt.heads, fdt.sectrac, 3401183Srgrimes track % fdt.heads, fdt.trans, fdt.f_gap, 3411183Srgrimes fdt.secsize, fill, fdt.f_inter); 3421022Sache if(!quiet && !((track + 1) % tracks_per_dot)) { 3431022Sache putchar('F'); 3441022Sache fflush(stdout); 3451022Sache } 3461022Sache } 3471022Sache if (verify) { 3481022Sache if (verify_track(fd, track, bytes_per_track) < 0) 3491022Sache error = errs = 1; 3501022Sache if(!quiet && !((track + 1) % tracks_per_dot)) { 3511022Sache if (!verify_only) 3521022Sache putchar('\b'); 3531022Sache if (error) { 3541022Sache putchar('E'); 3551022Sache error = 0; 3561022Sache } 3571022Sache else 3581022Sache putchar('V'); 3591022Sache fflush(stdout); 3601022Sache } 3611022Sache } 3621022Sache } 3631022Sache if(!quiet) 3641022Sache printf(" done.\n"); 3651022Sache 3661022Sache return errs; 3671022Sache} 3681533Sjoerg/* 3691533Sjoerg * Local Variables: 3701533Sjoerg * c-indent-level: 8 3711533Sjoerg * c-continued-statement-offset: 8 3721533Sjoerg * c-continued-brace-offset: 0 3731533Sjoerg * c-brace-offset: -8 3741533Sjoerg * c-brace-imaginary-offset: 0 3751533Sjoerg * c-argdecl-indent: 8 3761533Sjoerg * c-label-offset: -8 3771533Sjoerg * c++-hanging-braces: 1 3781533Sjoerg * c++-access-specifier-offset: -8 3791533Sjoerg * c++-empty-arglist-indent: 8 3801533Sjoerg * c++-friend-offset: 0 3811533Sjoerg * End: 3821533Sjoerg */ 383