fdformat.c revision 55541
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 55541 2000-01-07 09:02:47Z kato $
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{
1261022Sache	static char namebuff[20];	/* big enough for "/dev/rfd0a"... */
1271022Sache
1281022Sache	memset(namebuff, 0, 20);
1291022Sache	if(*arg == '\0') /* ??? */
1301022Sache		return arg;
1311022Sache	if(*arg == '/')  /* do not convert absolute pathnames */
1321022Sache		return arg;
1331022Sache	strcpy(namebuff, "/dev/r");
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",
14329531Scharnier	"usage: fdformat [-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;
1731022Sache	int fill = 0xf6, quiet = 0, verify = 1, verify_only = 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
1781022Sache	while((c = getopt(argc, argv, "f:c:s:h:r:g:S:F:t:i:qvn")) != -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
2241022Sache		case 'n':
2251022Sache			verify = 0;
2261022Sache			break;
2271022Sache
2281022Sache		case 'v':
2291022Sache			verify = 1;
2301022Sache			verify_only = 1;
2311022Sache			break;
2321022Sache
2331022Sache		case '?': default:
2341022Sache			usage();
2351022Sache		}
2361022Sache
2371022Sache	if(optind != argc - 1)
2381022Sache		usage();
2391022Sache
2401022Sache	switch(format) {
2411022Sache	default:
24229531Scharnier		errx(2, "bad floppy size: %dK", format);
2431022Sache	case -1:   suffix = "";      break;
2441022Sache	case 360:  suffix = ".360";  break;
24555541Skato	case 640:  suffix = ".640";  break;
2461022Sache	case 720:  suffix = ".720";  break;
2471022Sache	case 800:  suffix = ".800";  break;
2481022Sache	case 820:  suffix = ".820";  break;
2491022Sache	case 1200: suffix = ".1200"; break;
25055541Skato	case 1232: suffix = ".1232"; break;
2511022Sache	case 1440: suffix = ".1440"; break;
2521022Sache	case 1480: suffix = ".1480"; break;
2531022Sache	case 1720: suffix = ".1720"; break;
2541022Sache	}
2551022Sache
2561022Sache	devname = makename(argv[optind], suffix);
2571022Sache
25829531Scharnier	if((fd = open(devname, O_RDWR)) < 0)
25929531Scharnier		err(1, "%s", devname);
2601022Sache
26129531Scharnier	if(ioctl(fd, FD_GTYPE, &fdt) < 0)
26229531Scharnier		errx(1, "not a floppy disk: %s", devname);
2631022Sache
2641022Sache	switch(rate) {
2651022Sache	case -1:  break;
2661022Sache	case 250: fdt.trans = FDC_250KBPS; break;
2671022Sache	case 300: fdt.trans = FDC_300KBPS; break;
2681022Sache	case 500: fdt.trans = FDC_500KBPS; break;
2691022Sache	default:
27029531Scharnier		errx(2, "invalid transfer rate: %d", rate);
2711022Sache	}
2721022Sache
2731022Sache	if (cyls >= 0)    fdt.tracks = cyls;
2741022Sache	if (secs >= 0)    fdt.sectrac = secs;
27529531Scharnier	if (fdt.sectrac > FD_MAX_NSEC)
27629531Scharnier		errx(2, "too many sectors per track, max value is %d", FD_MAX_NSEC);
2771022Sache	if (heads >= 0)   fdt.heads = heads;
2781022Sache	if (gaplen >= 0)  fdt.f_gap = gaplen;
2791022Sache	if (secsize >= 0) fdt.secsize = secsize;
2801022Sache	if (steps >= 0)   fdt.steptrac = steps;
2811022Sache	if (intleave >= 0) fdt.f_inter = intleave;
2821022Sache
2831022Sache	bytes_per_track = fdt.sectrac * (1<<fdt.secsize) * 128;
2841022Sache
28555541Skato	/* XXX  20/40 = 0.5 */
28655541Skato	tracks_per_dot = (fdt.tracks * fdt.heads + 20) / 40;
28755541Skato
2881022Sache	if (verify_only) {
2891022Sache		if(!quiet)
2901022Sache			printf("Verify %dK floppy `%s'.\n",
2911022Sache				fdt.tracks * fdt.heads * bytes_per_track / 1024,
2921022Sache				devname);
2931022Sache	}
2941022Sache	else if(!quiet) {
2951022Sache		printf("Format %dK floppy `%s'? (y/n): ",
2961022Sache			fdt.tracks * fdt.heads * bytes_per_track / 1024,
2971022Sache			devname);
2981022Sache		if(! yes ()) {
2991022Sache			printf("Not confirmed.\n");
3001022Sache			return 0;
3011022Sache		}
3021022Sache	}
3031022Sache
3041022Sache	/*
3051022Sache	 * Formatting.
3061022Sache	 */
3071022Sache	if(!quiet) {
30855541Skato		int i;
30955541Skato
3101022Sache		printf("Processing ");
31155541Skato		for (i = 0; i < (fdt.tracks * fdt.heads) / tracks_per_dot; i++)
31255541Skato			putchar('-');
31355541Skato		printf("\rProcessing ");
3141022Sache		fflush(stdout);
3151022Sache	}
3161022Sache
3171022Sache	error = errs = 0;
3181022Sache
3191022Sache	for (track = 0; track < fdt.tracks * fdt.heads; track++) {
3201022Sache		if (!verify_only) {
3211183Srgrimes			format_track(fd, track / fdt.heads, fdt.sectrac,
3221183Srgrimes				track % fdt.heads, fdt.trans, fdt.f_gap,
3231183Srgrimes				fdt.secsize, fill, fdt.f_inter);
3241022Sache			if(!quiet && !((track + 1) % tracks_per_dot)) {
3251022Sache				putchar('F');
3261022Sache				fflush(stdout);
3271022Sache			}
3281022Sache		}
3291022Sache		if (verify) {
3301022Sache			if (verify_track(fd, track, bytes_per_track) < 0)
3311022Sache				error = errs = 1;
3321022Sache			if(!quiet && !((track + 1) % tracks_per_dot)) {
3331022Sache				if (!verify_only)
3341022Sache					putchar('\b');
3351022Sache				if (error) {
3361022Sache					putchar('E');
3371022Sache					error = 0;
3381022Sache				}
3391022Sache				else
3401022Sache					putchar('V');
3411022Sache				fflush(stdout);
3421022Sache			}
3431022Sache		}
3441022Sache	}
3451022Sache	if(!quiet)
3461022Sache		printf(" done.\n");
3471022Sache
3481022Sache	return errs;
3491022Sache}
3501533Sjoerg/*
3511533Sjoerg * Local Variables:
3521533Sjoerg *  c-indent-level:               8
3531533Sjoerg *  c-continued-statement-offset: 8
3541533Sjoerg *  c-continued-brace-offset:     0
3551533Sjoerg *  c-brace-offset:              -8
3561533Sjoerg *  c-brace-imaginary-offset:     0
3571533Sjoerg *  c-argdecl-indent:             8
3581533Sjoerg *  c-label-offset:              -8
3591533Sjoerg *  c++-hanging-braces:           1
3601533Sjoerg *  c++-access-specifier-offset: -8
3611533Sjoerg *  c++-empty-arglist-indent:     8
3621533Sjoerg *  c++-friend-offset:            0
3631533Sjoerg * End:
3641533Sjoerg */
365