Home | History | Annotate | Line # | Download | only in fdformat
fdformat.c revision 1.15.8.1
      1  1.15.8.1       jym /*	$NetBSD: fdformat.c,v 1.15.8.1 2009/05/13 19:19:49 jym Exp $	*/
      2       1.5   thorpej 
      3       1.1       jtk /*-
      4       1.5   thorpej  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
      5       1.5   thorpej  * All rights reserved.
      6       1.5   thorpej  *
      7       1.5   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8       1.5   thorpej  * by John Kohl.
      9       1.1       jtk  *
     10       1.1       jtk  * Redistribution and use in source and binary forms, with or without
     11       1.1       jtk  * modification, are permitted provided that the following conditions
     12       1.1       jtk  * are met:
     13       1.1       jtk  * 1. Redistributions of source code must retain the above copyright
     14       1.1       jtk  *    notice, this list of conditions and the following disclaimer.
     15       1.1       jtk  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1       jtk  *    notice, this list of conditions and the following disclaimer in the
     17       1.1       jtk  *    documentation and/or other materials provided with the distribution.
     18       1.1       jtk  *
     19       1.5   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.5   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.5   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.5   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.5   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.5   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.5   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.5   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.5   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.5   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1       jtk  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1       jtk  */
     31       1.1       jtk 
     32       1.1       jtk /*
     33       1.1       jtk  * fdformat: format a floppy diskette, using interface provided in
     34       1.1       jtk  * <sys/fdio.h>
     35       1.1       jtk  */
     36      1.10       agc #include <sys/cdefs.h>
     37      1.10       agc 
     38      1.10       agc #ifndef lint
     39  1.15.8.1       jym __RCSID("$NetBSD: fdformat.c,v 1.15.8.1 2009/05/13 19:19:49 jym Exp $");
     40      1.10       agc #endif
     41      1.10       agc 
     42       1.4     lukem #include <sys/types.h>
     43       1.4     lukem #include <sys/fdio.h>
     44       1.4     lukem #include <sys/ioctl.h>
     45       1.1       jtk 
     46       1.4     lukem #include <err.h>
     47       1.4     lukem #include <errno.h>
     48       1.4     lukem #include <fcntl.h>
     49       1.4     lukem #include <limits.h>
     50       1.1       jtk #include <stdio.h>
     51       1.1       jtk #include <stdlib.h>
     52       1.1       jtk #include <unistd.h>
     53       1.1       jtk #include "pathnames.h"
     54       1.1       jtk 
     55      1.12  christos static const char *fdb_array[2] = {_PATH_FLOPPYTAB, 0};
     56       1.1       jtk 
     57      1.12  christos #define MASK_NBPS		0x0001
     58      1.12  christos #define MASK_NCYL		0x0002
     59      1.12  christos #define MASK_NSPT		0x0004
     60      1.12  christos #define MASK_NTRK		0x0008
     61       1.1       jtk #define MASK_STEPSPERCYL	0x0010
     62      1.12  christos #define MASK_GAPLEN		0x0020
     63      1.12  christos #define MASK_FILLBYTE		0x0040
     64      1.12  christos #define MASK_XFER_RATE		0x0080
     65      1.12  christos #define MASK_INTERLEAVE		0x0100
     66       1.1       jtk 
     67       1.1       jtk #define ALLPARMS (MASK_NBPS|MASK_NCYL|MASK_NSPT|MASK_NTRK|MASK_STEPSPERCYL|MASK_GAPLEN|MASK_FILLBYTE|MASK_XFER_RATE|MASK_INTERLEAVE)
     68       1.1       jtk 
     69      1.12  christos static int	confirm(int);
     70      1.14     perry static void	usage(void) __dead;
     71      1.12  christos static int	verify_track(int, int, int, struct fdformat_parms *, char *);
     72       1.1       jtk 
     73      1.12  christos int	main(int, char **);
     74      1.12  christos 
     75      1.12  christos static int
     76       1.1       jtk confirm(int def)
     77       1.1       jtk {
     78       1.1       jtk 	int ch;
     79       1.1       jtk 
     80      1.12  christos 	(void)printf(" Yes/no [%c]?", def ? 'y' : 'n');
     81       1.1       jtk 	ch = getchar();
     82       1.1       jtk 	switch (ch) {
     83       1.1       jtk 	case 'y':
     84       1.1       jtk 	case 'Y':
     85       1.1       jtk 		return 1;
     86       1.1       jtk 	case '\n':
     87       1.1       jtk 		return def;
     88       1.1       jtk 	case EOF:
     89       1.1       jtk 	case 'n':
     90       1.1       jtk 	case 'N':
     91       1.1       jtk 	default:
     92       1.1       jtk 		return 0;
     93       1.1       jtk 	}
     94       1.1       jtk }
     95       1.1       jtk 
     96      1.12  christos static int
     97       1.1       jtk verify_track(int fd, int cyl, int trk, struct fdformat_parms *parms, char *buf)
     98       1.1       jtk {
     99       1.1       jtk 	size_t tracksize;
    100       1.1       jtk 	off_t offset;
    101       1.1       jtk 
    102       1.1       jtk 	tracksize = parms->nbps * parms->nspt; /* bytes per track */
    103       1.1       jtk 	offset = tracksize * (cyl * parms->ntrk + trk); /* track offset */
    104       1.1       jtk 
    105       1.1       jtk 	if (lseek(fd, offset, SEEK_SET) == (off_t) -1) {
    106      1.13    mishka 		(void)printf("- SEEK ERROR\n");
    107       1.1       jtk 		return 1;
    108       1.1       jtk 	}
    109  1.15.8.1       jym 	if ((size_t)read(fd, buf, tracksize) != tracksize) {
    110      1.13    mishka 		(void)printf("- VERIFY ERROR\n");
    111       1.1       jtk 		return 1;
    112      1.13    mishka 	}
    113       1.1       jtk 	return 0;
    114       1.1       jtk }
    115       1.1       jtk 
    116      1.12  christos static void
    117       1.1       jtk usage(void)
    118       1.1       jtk {
    119      1.12  christos 	(void)fprintf(stderr,
    120      1.12  christos 	    "Usage: %s [-f device] [-t type] [-n] [-B nbps] [-S nspt]\n"
    121       1.9    simonb 	    "\t[-T ntrk] [-C ncyl] [-P stepspercyl] [-G gaplen]\n"
    122       1.9    simonb 	    "\t[-F fillbyte] [-X xfer_rate] [-I interleave]\n", getprogname());
    123       1.7  christos 	exit(1);
    124       1.1       jtk }
    125       1.1       jtk 
    126      1.12  christos #define numarg(which, maskn, op) 				\
    127      1.12  christos 	do {							\
    128      1.12  christos 		tmplong = strtol(optarg, &tmpcharp, 0); 	\
    129      1.12  christos 		if (*tmpcharp != '\0' || tmplong op 0) 		\
    130      1.12  christos 		    errx(1,					\
    131      1.12  christos 			"Invalid numerical argument `%s' for " 	\
    132      1.12  christos 			# which, optarg); 			\
    133      1.12  christos 		if (errno == ERANGE && (tmplong == LONG_MIN ||	\
    134      1.12  christos 		    tmplong == LONG_MAX)) 			\
    135      1.12  christos 		    err(1,					\
    136      1.12  christos 			"Bad numerical argument `%s' for " 	\
    137      1.12  christos 			# which, optarg); 			\
    138      1.12  christos 		parms.which = tmplong; 				\
    139      1.12  christos 		parmmask |= MASK_##maskn;			\
    140      1.12  christos 	} while (/* CONSTCOND */0)
    141      1.12  christos 
    142      1.12  christos #define getparm(structname, maskname) 				\
    143      1.12  christos 	do {							\
    144      1.12  christos 		if (cgetnum(fdbuf, # structname, &tmplong) == -1) \
    145      1.12  christos 			errx(1, "Parameter " # structname	\
    146      1.12  christos 			    " missing for type `%s'", optarg);	\
    147      1.12  christos 		parms.structname = tmplong; 			\
    148      1.12  christos 		parmmask |= MASK_ ## maskname;			\
    149      1.12  christos 	} while (/* CONSTCOND */0)
    150       1.1       jtk 
    151      1.12  christos 
    152      1.12  christos #define copyparm(which, mask) 					\
    153      1.12  christos 	if ((parmmask & MASK_##mask) == 0) 			\
    154       1.1       jtk 		parms.which = fetchparms.which
    155       1.1       jtk 
    156       1.1       jtk int
    157       1.1       jtk main(int argc, char *argv[])
    158       1.1       jtk {
    159       1.4     lukem 	char *fdbuf = NULL, *trackbuf = NULL;
    160       1.1       jtk 	int errcnt = 0;
    161       1.1       jtk 	int verify = 1;
    162       1.1       jtk 	int ch;
    163       1.1       jtk 	long tmplong;
    164       1.1       jtk 	int tmpint;
    165       1.1       jtk 	char *tmpcharp;
    166       1.1       jtk 	int parmmask = 0;
    167       1.1       jtk 	struct fdformat_parms parms, fetchparms;
    168       1.1       jtk 	struct fdformat_cmd cmd;
    169      1.12  christos 	const char *filename = _PATH_FLOPPY_DEV;
    170       1.1       jtk 	int fd;
    171       1.4     lukem 	int trk, cyl;
    172       1.1       jtk 
    173       1.1       jtk 	while ((ch = getopt(argc, argv, "f:t:nB:C:S:T:P:G:F:X:I:")) != -1)
    174       1.1       jtk 		switch (ch) {
    175       1.1       jtk 		case 't':		/* disk type */
    176       1.1       jtk 			switch (cgetent(&fdbuf, fdb_array, optarg)) {
    177       1.1       jtk 			case 0:
    178       1.1       jtk 				break;
    179       1.1       jtk 			case 1:
    180       1.1       jtk 			case -3:
    181       1.1       jtk 				errx(1, "tc= loop or missing entry entry in "
    182       1.1       jtk 				     _PATH_FLOPPYTAB " for type %s", optarg);
    183       1.1       jtk 				break;
    184       1.1       jtk 			case -1:
    185      1.12  christos 				errx(1, "Unknown floppy disk type %s", optarg);
    186       1.1       jtk 				break;
    187       1.1       jtk 			default:
    188      1.12  christos 				err(1, "Problem accessing " _PATH_FLOPPYTAB);
    189       1.1       jtk 				break;
    190       1.1       jtk 			}
    191       1.1       jtk 
    192       1.1       jtk 			getparm(nbps, NBPS);
    193       1.1       jtk 			getparm(ncyl, NCYL);
    194       1.1       jtk 			getparm(nspt, NSPT);
    195       1.1       jtk 			getparm(ntrk, NTRK);
    196       1.1       jtk 			getparm(stepspercyl, STEPSPERCYL);
    197       1.1       jtk 			getparm(gaplen, GAPLEN);
    198       1.1       jtk 			getparm(fillbyte, FILLBYTE);
    199       1.1       jtk 			getparm(xfer_rate, XFER_RATE);
    200       1.1       jtk 			getparm(interleave, INTERLEAVE);
    201       1.1       jtk 			break;
    202       1.1       jtk 		case 'f':		/* device name */
    203       1.1       jtk 			filename = optarg;
    204       1.1       jtk 			break;
    205       1.1       jtk 		case 'n':		/* no verify */
    206       1.1       jtk 			verify = 0;
    207       1.1       jtk 			break;
    208       1.1       jtk 		case 'B':
    209      1.12  christos 			numarg(nbps, NBPS, <=);
    210       1.1       jtk 			break;
    211       1.1       jtk 		case 'C':
    212      1.12  christos 			numarg(ncyl, NCYL, <=);
    213       1.1       jtk 			break;
    214       1.1       jtk 		case 'S':
    215      1.12  christos 			numarg(nspt, NSPT, <=);
    216       1.1       jtk 			break;
    217       1.1       jtk 		case 'T':
    218      1.12  christos 			numarg(ntrk, NTRK, <=);
    219       1.1       jtk 			break;
    220       1.1       jtk 		case 'P':
    221      1.12  christos 			numarg(stepspercyl, STEPSPERCYL, <=);
    222       1.1       jtk 			break;
    223       1.1       jtk 		case 'G':
    224      1.12  christos 			numarg(gaplen, GAPLEN, <=);
    225       1.1       jtk 			break;
    226      1.12  christos 		case 'F':
    227      1.12  christos 			numarg(fillbyte, FILLBYTE, <);
    228       1.1       jtk 			break;
    229       1.1       jtk 		case 'X':
    230      1.12  christos 			numarg(xfer_rate, XFER_RATE, <=);
    231       1.1       jtk 			break;
    232       1.1       jtk 		case 'I':
    233      1.12  christos 			numarg(interleave, INTERLEAVE, <=);
    234       1.1       jtk 			break;
    235       1.1       jtk 		case '?':
    236       1.1       jtk 		default:
    237       1.1       jtk 			usage();
    238       1.1       jtk 		}
    239      1.12  christos 
    240       1.3       jtk 	if (optind < argc)
    241       1.3       jtk 		usage();
    242       1.3       jtk 
    243       1.1       jtk 	fd = open(filename, O_RDWR);
    244       1.1       jtk 	if (fd == -1)
    245      1.12  christos 		err(1, "Cannot open %s", filename);
    246       1.1       jtk 	if (ioctl(fd, FDIOCGETFORMAT, &fetchparms) == -1) {
    247       1.1       jtk 		if (errno == ENOTTY)
    248      1.12  christos 			err(1, "Device `%s' does not support floppy formatting",
    249       1.1       jtk 			    filename);
    250       1.1       jtk 		else
    251      1.12  christos 			err(1, "Cannot fetch current floppy"
    252      1.12  christos 			    " formatting parameters");
    253       1.1       jtk 	}
    254       1.1       jtk 
    255       1.1       jtk 	copyparm(nbps, NBPS);
    256       1.1       jtk 	copyparm(ncyl, NCYL);
    257       1.1       jtk 	copyparm(nspt, NSPT);
    258       1.1       jtk 	copyparm(ntrk, NTRK);
    259       1.1       jtk 	copyparm(stepspercyl, STEPSPERCYL);
    260       1.1       jtk 	copyparm(gaplen, GAPLEN);
    261       1.1       jtk 	copyparm(fillbyte, FILLBYTE);
    262       1.1       jtk 	copyparm(xfer_rate, XFER_RATE);
    263       1.1       jtk 	copyparm(interleave, INTERLEAVE);
    264       1.1       jtk 
    265       1.1       jtk 	parms.fdformat_version = FDFORMAT_VERSION;
    266       1.1       jtk 
    267       1.1       jtk 	tmpint = FDOPT_NORETRY|FDOPT_SILENT;
    268       1.1       jtk 	if (ioctl(fd, FDIOCSETOPTS, &tmpint) == -1 ||
    269       1.1       jtk 	    ioctl(fd, FDIOCSETFORMAT, &parms) == -1) {
    270      1.12  christos 		errx(1, "Cannot set requested formatting parameters:"
    271      1.12  christos 		    " %d cylinders, %d tracks, %d sectors of %d bytes",
    272      1.12  christos 		    parms.ncyl, parms.ntrk, parms.nspt, parms.nbps);
    273       1.1       jtk 	}
    274       1.1       jtk 
    275      1.12  christos 	(void)printf("Ready to format %s with %d cylinders, %d tracks,"
    276      1.12  christos 	    " %d sectors of %d bytes\n(%d KB)",
    277      1.12  christos 	    filename, parms.ncyl, parms.ntrk, parms.nspt, parms.nbps,
    278      1.12  christos 	    parms.ncyl * parms.ntrk * parms.nspt * parms.nbps / 1024);
    279       1.1       jtk 	if (!confirm(1))
    280      1.12  christos 		errx(1,"Formatting abandoned -- not confirmed.");
    281       1.1       jtk 
    282      1.12  christos 	if (verify) {
    283       1.1       jtk 		trackbuf = malloc(parms.nbps * parms.nspt);
    284      1.12  christos 		if (trackbuf == NULL)
    285      1.12  christos 			warn("Cannot allocate verification buffer");
    286      1.12  christos 	}
    287       1.1       jtk 
    288       1.1       jtk 	cmd.formatcmd_version = FDFORMAT_VERSION;
    289  1.15.8.1       jym 	for (cyl = 0; (unsigned int)cyl < parms.ncyl; cyl++) {
    290       1.1       jtk 		cmd.cylinder = cyl;
    291  1.15.8.1       jym 		for (trk = 0; (unsigned int)trk < parms.ntrk; trk++) {
    292       1.1       jtk 			cmd.head = trk;
    293      1.13    mishka 			(void)printf("\rFormatting track %i / head %i ", cyl, trk);
    294      1.13    mishka 			(void)fflush(stdout);
    295       1.1       jtk 			if (ioctl(fd, FDIOCFORMAT_TRACK, &cmd) == 0) {
    296       1.1       jtk 				if (verify)
    297       1.1       jtk 					errcnt += verify_track(fd, cyl, trk,
    298      1.12  christos 					    &parms, trackbuf);
    299       1.1       jtk 			} else if (errno == EINVAL) {
    300      1.12  christos 				(void)putchar('\n');
    301      1.12  christos 				errx(1, "Formatting botch at <%d,%d>",
    302       1.1       jtk 				     cyl, trk);
    303       1.1       jtk 			} else if (errno == EIO) {
    304      1.13    mishka 				(void)printf("- IO ERROR\n");
    305       1.1       jtk 				errcnt++;
    306       1.1       jtk 			}
    307       1.1       jtk 		}
    308       1.1       jtk 	}
    309      1.13    mishka 	(void)printf("\rFormatting %i tracks total complete.\n",
    310      1.13    mishka 		parms.ncyl * parms.ntrk);
    311       1.1       jtk 	if (errcnt)
    312      1.12  christos 		errx(1, "%d track formatting error%s",
    313      1.12  christos 		    errcnt, errcnt == 1 ? "" : "s");
    314       1.1       jtk 	return 0;
    315       1.1       jtk }
    316