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