fdformat.c revision 61154
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. 2555541Skato * 2655541Skato * $FreeBSD: head/usr.sbin/fdformat/fdformat.c 61154 2000-06-01 22:27:30Z phk $ 271022Sache */ 281022Sache 291022Sache/* 301022Sache * FreeBSD: 311022Sache * format a floppy disk 328857Srgrimes * 331022Sache * Added FD_GTYPE ioctl, verifying, proportional indicators. 341022Sache * Serge Vakulenko, vak@zebub.msk.su 351022Sache * Sat Dec 18 17:45:47 MSK 1993 361022Sache * 371022Sache * Final adaptation, change format/verify logic, add separate 381022Sache * format gap/interleave values 391022Sache * Andrew A. Chernov, ache@astral.msk.su 401022Sache * Thu Jan 27 00:47:24 MSK 1994 411022Sache */ 421022Sache 4329531Scharnier#include <ctype.h> 4429531Scharnier#include <err.h> 4529531Scharnier#include <fcntl.h> 461022Sache#include <stdio.h> 471022Sache#include <stdlib.h> 4829531Scharnier#include <strings.h> 491022Sache#include <unistd.h> 501022Sache 511022Sache#include <machine/ioctl_fd.h> 521022Sache 531022Sachestatic void 541022Sacheformat_track(int fd, int cyl, int secs, int head, int rate, 551137Sache int gaplen, int secsize, int fill,int interleave) 561022Sache{ 571022Sache struct fd_formb f; 581137Sache register int i,j; 591138Sache int il[FD_MAX_NSEC + 1]; 601022Sache 611137Sache memset(il,0,sizeof il); 621137Sache for(j = 0, i = 1; i <= secs; i++) { 631137Sache while(il[(j%secs)+1]) j++; 641137Sache il[(j%secs)+1] = i; 651137Sache j += interleave; 661137Sache } 671137Sache 681022Sache f.format_version = FD_FORMAT_VERSION; 691022Sache f.head = head; 701022Sache f.cyl = cyl; 711022Sache f.transfer_rate = rate; 721022Sache 731022Sache f.fd_formb_secshift = secsize; 741022Sache f.fd_formb_nsecs = secs; 751022Sache f.fd_formb_gaplen = gaplen; 761022Sache f.fd_formb_fillbyte = fill; 771022Sache for(i = 0; i < secs; i++) { 781022Sache f.fd_formb_cylno(i) = cyl; 791022Sache f.fd_formb_headno(i) = head; 801137Sache f.fd_formb_secno(i) = il[i+1]; 811022Sache f.fd_formb_secsize(i) = secsize; 821022Sache } 8329531Scharnier if(ioctl(fd, FD_FORM, (caddr_t)&f) < 0) 8429531Scharnier err(1, "ioctl(FD_FORM)"); 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) 9529531Scharnier warn("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); 11029531Scharnier if (! buf) 11129531Scharnier errx(2, "out of memory"); 1121022Sache if (lseek (fd, (long) track*tracksize, 0) < 0) 1131533Sjoerg rv = -1; 1141533Sjoerg /* try twice reading it, without using the normal retrier */ 1151533Sjoerg else if (read (fd, buf, tracksize) != tracksize 1161533Sjoerg && read (fd, buf, tracksize) != tracksize) 1171533Sjoerg rv = -1; 1181533Sjoerg if(fdopts != -1) 1191533Sjoerg (void)ioctl(fd, FD_SOPTS, &ofdopts); 1201533Sjoerg return (rv); 1211022Sache} 1221022Sache 1231022Sachestatic const char * 1241022Sachemakename(const char *arg, const char *suffix) 1251022Sache{ 12656447Sasmodai static char namebuff[20]; /* big enough for "/dev/fd0a"... */ 1271022Sache 1281022Sache memset(namebuff, 0, 20); 1291022Sache if(*arg == '\0') /* ??? */ 1301022Sache return arg; 1311022Sache if(*arg == '/') /* do not convert absolute pathnames */ 1321022Sache return arg; 13356447Sasmodai strcpy(namebuff, "/dev/"); 1341022Sache strncat(namebuff, arg, 3); 1351022Sache strcat(namebuff, suffix); 1361022Sache return namebuff; 1371022Sache} 1381022Sache 1391022Sachestatic void 1401533Sjoergusage (void) 1411022Sache{ 14229531Scharnier fprintf(stderr, "%s\n%s\n", 14361154Sphk "usage: fdformat [-y] [-q] [-n | -v] [-f #] [-c #] [-s #] [-h #]", 14429531Scharnier " [-r #] [-g #] [-i #] [-S #] [-F #] [-t #] devname"); 1451022Sache exit(2); 1461022Sache} 1471022Sache 1481022Sachestatic int 1491533Sjoergyes (void) 1501022Sache{ 1511022Sache char reply [256], *p; 1521022Sache 1531022Sache reply[sizeof(reply)-1] = 0; 1541022Sache for (;;) { 1551022Sache fflush(stdout); 1561022Sache if (! fgets (reply, sizeof(reply)-1, stdin)) 1571022Sache return (0); 1581022Sache for (p=reply; *p==' ' || *p=='\t'; ++p) 1591022Sache continue; 1601022Sache if (*p=='y' || *p=='Y') 1611022Sache return (1); 1621022Sache if (*p=='n' || *p=='N' || *p=='\n' || *p=='\r') 1631022Sache return (0); 1641022Sache printf("Answer `yes' or `no': "); 1651022Sache } 1661022Sache} 1671022Sache 1681022Sacheint 1691022Sachemain(int argc, char **argv) 1701022Sache{ 1711022Sache int format = -1, cyls = -1, secs = -1, heads = -1, intleave = -1; 1721022Sache int rate = -1, gaplen = -1, secsize = -1, steps = -1; 17361154Sphk int fill = 0xf6, quiet = 0, verify = 1, verify_only = 0, confirm = 0; 1741022Sache int fd, c, track, error, tracks_per_dot, bytes_per_track, errs; 1751022Sache const char *devname, *suffix; 1761022Sache struct fd_type fdt; 1771022Sache 17861154Sphk while((c = getopt(argc, argv, "f:c:s:h:r:g:S:F:t:i:qyvn")) != -1) 1791022Sache switch(c) { 1801022Sache case 'f': /* format in kilobytes */ 1811022Sache format = atoi(optarg); 1821022Sache break; 1831022Sache 1841022Sache case 'c': /* # of cyls */ 1851022Sache cyls = atoi(optarg); 1861022Sache break; 1871022Sache 1881022Sache case 's': /* # of secs per track */ 1891022Sache secs = atoi(optarg); 1901022Sache break; 1911022Sache 1921022Sache case 'h': /* # of heads */ 1931022Sache heads = atoi(optarg); 1941022Sache break; 1951022Sache 1961022Sache case 'r': /* transfer rate, kilobyte/sec */ 1971022Sache rate = atoi(optarg); 1981022Sache break; 1991022Sache 2001022Sache case 'g': /* length of GAP3 to format with */ 2011022Sache gaplen = atoi(optarg); 2021022Sache break; 2031022Sache 2041022Sache case 'S': /* sector size shift factor (1 << S)*128 */ 2051022Sache secsize = atoi(optarg); 2061022Sache break; 2071022Sache 2081022Sache case 'F': /* fill byte, C-like notation allowed */ 2091022Sache fill = (int)strtol(optarg, (char **)0, 0); 2101022Sache break; 2111022Sache 2121022Sache case 't': /* steps per track */ 2131022Sache steps = atoi(optarg); 2141022Sache break; 2151022Sache 2161022Sache case 'i': /* interleave factor */ 2171022Sache intleave = atoi(optarg); 2181022Sache break; 2191022Sache 2201022Sache case 'q': 2211022Sache quiet = 1; 2221022Sache break; 2231022Sache 22461154Sphk case 'y': 22561154Sphk confirm = 1; 22661154Sphk break; 22761154Sphk 2281022Sache case 'n': 2291022Sache verify = 0; 2301022Sache break; 2311022Sache 2321022Sache case 'v': 2331022Sache verify = 1; 2341022Sache verify_only = 1; 2351022Sache break; 2361022Sache 2371022Sache case '?': default: 2381022Sache usage(); 2391022Sache } 2401022Sache 2411022Sache if(optind != argc - 1) 2421022Sache usage(); 2431022Sache 2441022Sache switch(format) { 2451022Sache default: 24629531Scharnier errx(2, "bad floppy size: %dK", format); 2471022Sache case -1: suffix = ""; break; 2481022Sache case 360: suffix = ".360"; break; 24955541Skato case 640: suffix = ".640"; break; 2501022Sache case 720: suffix = ".720"; break; 2511022Sache case 800: suffix = ".800"; break; 2521022Sache case 820: suffix = ".820"; break; 2531022Sache case 1200: suffix = ".1200"; break; 25455541Skato case 1232: suffix = ".1232"; break; 2551022Sache case 1440: suffix = ".1440"; break; 2561022Sache case 1480: suffix = ".1480"; break; 2571022Sache case 1720: suffix = ".1720"; break; 2581022Sache } 2591022Sache 2601022Sache devname = makename(argv[optind], suffix); 2611022Sache 26229531Scharnier if((fd = open(devname, O_RDWR)) < 0) 26329531Scharnier err(1, "%s", devname); 2641022Sache 26529531Scharnier if(ioctl(fd, FD_GTYPE, &fdt) < 0) 26629531Scharnier errx(1, "not a floppy disk: %s", devname); 2671022Sache 2681022Sache switch(rate) { 2691022Sache case -1: break; 2701022Sache case 250: fdt.trans = FDC_250KBPS; break; 2711022Sache case 300: fdt.trans = FDC_300KBPS; break; 2721022Sache case 500: fdt.trans = FDC_500KBPS; break; 2731022Sache default: 27429531Scharnier errx(2, "invalid transfer rate: %d", rate); 2751022Sache } 2761022Sache 2771022Sache if (cyls >= 0) fdt.tracks = cyls; 2781022Sache if (secs >= 0) fdt.sectrac = secs; 27929531Scharnier if (fdt.sectrac > FD_MAX_NSEC) 28029531Scharnier errx(2, "too many sectors per track, max value is %d", FD_MAX_NSEC); 2811022Sache if (heads >= 0) fdt.heads = heads; 2821022Sache if (gaplen >= 0) fdt.f_gap = gaplen; 2831022Sache if (secsize >= 0) fdt.secsize = secsize; 2841022Sache if (steps >= 0) fdt.steptrac = steps; 2851022Sache if (intleave >= 0) fdt.f_inter = intleave; 2861022Sache 2871022Sache bytes_per_track = fdt.sectrac * (1<<fdt.secsize) * 128; 2881022Sache 28955541Skato /* XXX 20/40 = 0.5 */ 29055541Skato tracks_per_dot = (fdt.tracks * fdt.heads + 20) / 40; 29155541Skato 2921022Sache if (verify_only) { 2931022Sache if(!quiet) 2941022Sache printf("Verify %dK floppy `%s'.\n", 2951022Sache fdt.tracks * fdt.heads * bytes_per_track / 1024, 2961022Sache devname); 2971022Sache } 29861154Sphk else if(!quiet && !confirm) { 2991022Sache printf("Format %dK floppy `%s'? (y/n): ", 3001022Sache fdt.tracks * fdt.heads * bytes_per_track / 1024, 3011022Sache devname); 3021022Sache if(! yes ()) { 3031022Sache printf("Not confirmed.\n"); 3041022Sache return 0; 3051022Sache } 3061022Sache } 3071022Sache 3081022Sache /* 3091022Sache * Formatting. 3101022Sache */ 3111022Sache if(!quiet) { 31255541Skato int i; 31355541Skato 3141022Sache printf("Processing "); 31555541Skato for (i = 0; i < (fdt.tracks * fdt.heads) / tracks_per_dot; i++) 31655541Skato putchar('-'); 31755541Skato printf("\rProcessing "); 3181022Sache fflush(stdout); 3191022Sache } 3201022Sache 3211022Sache error = errs = 0; 3221022Sache 3231022Sache for (track = 0; track < fdt.tracks * fdt.heads; track++) { 3241022Sache if (!verify_only) { 3251183Srgrimes format_track(fd, track / fdt.heads, fdt.sectrac, 3261183Srgrimes track % fdt.heads, fdt.trans, fdt.f_gap, 3271183Srgrimes fdt.secsize, fill, fdt.f_inter); 3281022Sache if(!quiet && !((track + 1) % tracks_per_dot)) { 3291022Sache putchar('F'); 3301022Sache fflush(stdout); 3311022Sache } 3321022Sache } 3331022Sache if (verify) { 3341022Sache if (verify_track(fd, track, bytes_per_track) < 0) 3351022Sache error = errs = 1; 3361022Sache if(!quiet && !((track + 1) % tracks_per_dot)) { 3371022Sache if (!verify_only) 3381022Sache putchar('\b'); 3391022Sache if (error) { 3401022Sache putchar('E'); 3411022Sache error = 0; 3421022Sache } 3431022Sache else 3441022Sache putchar('V'); 3451022Sache fflush(stdout); 3461022Sache } 3471022Sache } 3481022Sache } 3491022Sache if(!quiet) 3501022Sache printf(" done.\n"); 3511022Sache 3521022Sache return errs; 3531022Sache} 3541533Sjoerg/* 3551533Sjoerg * Local Variables: 3561533Sjoerg * c-indent-level: 8 3571533Sjoerg * c-continued-statement-offset: 8 3581533Sjoerg * c-continued-brace-offset: 0 3591533Sjoerg * c-brace-offset: -8 3601533Sjoerg * c-brace-imaginary-offset: 0 3611533Sjoerg * c-argdecl-indent: 8 3621533Sjoerg * c-label-offset: -8 3631533Sjoerg * c++-hanging-braces: 1 3641533Sjoerg * c++-access-specifier-offset: -8 3651533Sjoerg * c++-empty-arglist-indent: 8 3661533Sjoerg * c++-friend-offset: 0 3671533Sjoerg * End: 3681533Sjoerg */ 369