xref: /freebsd/usr.sbin/fdcontrol/fdcontrol.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
11de7b4b8SPedro F. Giffuni /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
41a6bed68SJoerg Wunsch  * Copyright (C) 1994, 2001 by Joerg Wunsch, Dresden
5e9e8f7b7SJoerg Wunsch  * All rights reserved.
6e9e8f7b7SJoerg Wunsch  *
7e9e8f7b7SJoerg Wunsch  * Redistribution and use in source and binary forms, with or without
8e9e8f7b7SJoerg Wunsch  * modification, are permitted provided that the following conditions
9e9e8f7b7SJoerg Wunsch  * are met:
10e9e8f7b7SJoerg Wunsch  * 1. Redistributions of source code must retain the above copyright
11e9e8f7b7SJoerg Wunsch  *    notice, this list of conditions and the following disclaimer.
12e9e8f7b7SJoerg Wunsch  * 2. Redistributions in binary form must reproduce the above copyright
13e9e8f7b7SJoerg Wunsch  *    notice, this list of conditions and the following disclaimer in the
14e9e8f7b7SJoerg Wunsch  *    documentation and/or other materials provided with the distribution.
15e9e8f7b7SJoerg Wunsch  *
16e9e8f7b7SJoerg Wunsch  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY
17e9e8f7b7SJoerg Wunsch  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18e9e8f7b7SJoerg Wunsch  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19e9e8f7b7SJoerg Wunsch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE
20e9e8f7b7SJoerg Wunsch  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21e9e8f7b7SJoerg Wunsch  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
22e9e8f7b7SJoerg Wunsch  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23e9e8f7b7SJoerg Wunsch  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24e9e8f7b7SJoerg Wunsch  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25e9e8f7b7SJoerg Wunsch  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
26e9e8f7b7SJoerg Wunsch  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
27e9e8f7b7SJoerg Wunsch  * DAMAGE.
28e9e8f7b7SJoerg Wunsch  */
29e9e8f7b7SJoerg Wunsch 
30b728350eSDavid E. O'Brien #include <sys/cdefs.h>
31015fadf9SDima Dorfman #include <sys/fdcio.h>
32015fadf9SDima Dorfman #include <sys/file.h>
33015fadf9SDima Dorfman 
348268bea7SPhilippe Charnier #include <err.h>
35e9e8f7b7SJoerg Wunsch #include <stdio.h>
36e9e8f7b7SJoerg Wunsch #include <stdlib.h>
371a6bed68SJoerg Wunsch #include <string.h>
381a6bed68SJoerg Wunsch #include <sysexits.h>
399136d119SPeter Wemm #include <unistd.h>
40e9e8f7b7SJoerg Wunsch 
411a6bed68SJoerg Wunsch #include "fdutil.h"
421a6bed68SJoerg Wunsch 
431a6bed68SJoerg Wunsch 
441b67be7bSPoul-Henning Kamp static	int format, verbose, show = 1, showfmt;
451a6bed68SJoerg Wunsch static	char *fmtstring;
461a6bed68SJoerg Wunsch 
471a6bed68SJoerg Wunsch static void showdev(enum fd_drivetype, const char *);
48*a9cce232SAlfonso Gregory static void usage(void) __dead2;
49015fadf9SDima Dorfman 
50015fadf9SDima Dorfman static void
usage(void)51e9e8f7b7SJoerg Wunsch usage(void)
52e9e8f7b7SJoerg Wunsch {
531a6bed68SJoerg Wunsch 	errx(EX_USAGE,
541a6bed68SJoerg Wunsch 	     "usage: fdcontrol [-F] [-d dbg] [-f fmt] [-s fmtstr] [-v] device");
55e9e8f7b7SJoerg Wunsch }
56e9e8f7b7SJoerg Wunsch 
571a6bed68SJoerg Wunsch void
showdev(enum fd_drivetype type,const char * fname)581a6bed68SJoerg Wunsch showdev(enum fd_drivetype type, const char *fname)
591a6bed68SJoerg Wunsch {
601a6bed68SJoerg Wunsch 	const char *name, *descr;
61e9e8f7b7SJoerg Wunsch 
621a6bed68SJoerg Wunsch 	getname(type, &name, &descr);
631a6bed68SJoerg Wunsch 	if (verbose)
641a6bed68SJoerg Wunsch 		printf("%s: %s drive (%s)\n", fname, name, descr);
651a6bed68SJoerg Wunsch 	else
661a6bed68SJoerg Wunsch 		printf("%s\n", name);
671a6bed68SJoerg Wunsch }
68e9e8f7b7SJoerg Wunsch 
69e9e8f7b7SJoerg Wunsch int
main(int argc,char ** argv)70e9e8f7b7SJoerg Wunsch main(int argc, char **argv)
71e9e8f7b7SJoerg Wunsch {
721a6bed68SJoerg Wunsch 	enum fd_drivetype type;
731a6bed68SJoerg Wunsch 	struct fd_type ft, newft, *fdtp;
741a6bed68SJoerg Wunsch 	const char *name, *descr;
7581f5cd99SJoerg Wunsch 	int fd, i, autofmt;
76e9e8f7b7SJoerg Wunsch 
771b67be7bSPoul-Henning Kamp 	autofmt = 0;
781b67be7bSPoul-Henning Kamp 	while((i = getopt(argc, argv, "aFf:s:v")) != -1)
791a6bed68SJoerg Wunsch 		switch(i) {
801a6bed68SJoerg Wunsch 
811b67be7bSPoul-Henning Kamp 		case 'a':
821b67be7bSPoul-Henning Kamp 			autofmt = 1;
836dc01766SWarner Losh 			/*FALLTHROUGH*/
841a6bed68SJoerg Wunsch 		case 'F':
851a6bed68SJoerg Wunsch 			showfmt = 1;
861a6bed68SJoerg Wunsch 			show = 0;
871a6bed68SJoerg Wunsch 			break;
881a6bed68SJoerg Wunsch 
891a6bed68SJoerg Wunsch 		case 'f':
909b28b5e6SPoul-Henning Kamp 			if (!strcmp(optarg, "auto")) {
919b28b5e6SPoul-Henning Kamp 				format = -1;
929b28b5e6SPoul-Henning Kamp 			} else if (getnum(optarg, &format)) {
931a6bed68SJoerg Wunsch 				fprintf(stderr,
941a6bed68SJoerg Wunsch 			"Bad argument %s to -f option; must be numeric\n",
951a6bed68SJoerg Wunsch 					optarg);
961a6bed68SJoerg Wunsch 				usage();
971a6bed68SJoerg Wunsch 			}
981a6bed68SJoerg Wunsch 			show = 0;
99e9e8f7b7SJoerg Wunsch 			break;
100e9e8f7b7SJoerg Wunsch 
101e9e8f7b7SJoerg Wunsch 		case 's':
1021a6bed68SJoerg Wunsch 			fmtstring = optarg;
1031a6bed68SJoerg Wunsch 			show = 0;
104e9e8f7b7SJoerg Wunsch 			break;
105e9e8f7b7SJoerg Wunsch 
1061a6bed68SJoerg Wunsch 		case 'v':
1071a6bed68SJoerg Wunsch 			verbose++;
1081a6bed68SJoerg Wunsch 			break;
1091a6bed68SJoerg Wunsch 
110e9e8f7b7SJoerg Wunsch 		default:
111e9e8f7b7SJoerg Wunsch 			usage();
112e9e8f7b7SJoerg Wunsch 		}
113e9e8f7b7SJoerg Wunsch 
114e9e8f7b7SJoerg Wunsch 	argc -= optind;
115e9e8f7b7SJoerg Wunsch 	argv += optind;
116e9e8f7b7SJoerg Wunsch 
117e9e8f7b7SJoerg Wunsch 	if(argc != 1)
118e9e8f7b7SJoerg Wunsch 		usage();
119e9e8f7b7SJoerg Wunsch 
12081f5cd99SJoerg Wunsch 	if((fd = open(argv[0], O_RDONLY | O_NONBLOCK)) < 0)
1211a6bed68SJoerg Wunsch 		err(EX_UNAVAILABLE, "open(%s)", argv[0]);
1221a6bed68SJoerg Wunsch 
1231a6bed68SJoerg Wunsch 	if (ioctl(fd, FD_GDTYPE, &type) == -1)
1241a6bed68SJoerg Wunsch 		err(EX_OSERR, "ioctl(FD_GDTYPE)");
1251a6bed68SJoerg Wunsch 	if (ioctl(fd, FD_GTYPE, &ft) == -1)
1261a6bed68SJoerg Wunsch 		err(EX_OSERR, "ioctl(FD_GTYPE)");
1271a6bed68SJoerg Wunsch 
1281a6bed68SJoerg Wunsch 	if (show) {
1291a6bed68SJoerg Wunsch 		showdev(type, argv[0]);
1301a6bed68SJoerg Wunsch 		return (0);
131e9e8f7b7SJoerg Wunsch 	}
132e9e8f7b7SJoerg Wunsch 
1331b67be7bSPoul-Henning Kamp 	if (autofmt) {
1341b67be7bSPoul-Henning Kamp 		memset(&newft, 0, sizeof newft);
1351b67be7bSPoul-Henning Kamp 		ft = newft;
1361b67be7bSPoul-Henning Kamp 	}
1371b67be7bSPoul-Henning Kamp 
1381a6bed68SJoerg Wunsch 	if (format) {
1391a6bed68SJoerg Wunsch 		getname(type, &name, &descr);
1401a6bed68SJoerg Wunsch 		fdtp = get_fmt(format, type);
1411a6bed68SJoerg Wunsch 		if (fdtp == 0)
1421a6bed68SJoerg Wunsch 			errx(EX_USAGE,
1431a6bed68SJoerg Wunsch 			    "unknown format %d KB for drive type %s",
1441a6bed68SJoerg Wunsch 			    format, name);
1451a6bed68SJoerg Wunsch 		ft = *fdtp;
146e9e8f7b7SJoerg Wunsch 	}
147e9e8f7b7SJoerg Wunsch 
1481a6bed68SJoerg Wunsch 	if (fmtstring) {
1491a6bed68SJoerg Wunsch 		parse_fmt(fmtstring, type, ft, &newft);
1501a6bed68SJoerg Wunsch 		ft = newft;
151e9e8f7b7SJoerg Wunsch 	}
152e9e8f7b7SJoerg Wunsch 
1531a6bed68SJoerg Wunsch 	if (showfmt) {
1549b28b5e6SPoul-Henning Kamp 		if (verbose) {
1554c1f1c62SXin LI 			const char *s;
1569b28b5e6SPoul-Henning Kamp 
1579b28b5e6SPoul-Henning Kamp 			printf("%s: %d KB media type\n", argv[0],
1589b28b5e6SPoul-Henning Kamp 			    (128 << ft.secsize) * ft.size / 1024);
1599b28b5e6SPoul-Henning Kamp 			printf("\tFormat:\t\t");
1601a6bed68SJoerg Wunsch 			print_fmt(ft);
1619b28b5e6SPoul-Henning Kamp 			if (ft.datalen != 0xff &&
1629b28b5e6SPoul-Henning Kamp 			    ft.datalen != (128 << ft.secsize))
1639b28b5e6SPoul-Henning Kamp 				printf("\tData length:\t%d\n", ft.datalen);
1649b28b5e6SPoul-Henning Kamp 			printf("\tSector size:\t%d\n", 128 << ft.secsize);
1659b28b5e6SPoul-Henning Kamp 			printf("\tSectors/track:\t%d\n", ft.sectrac);
1669b28b5e6SPoul-Henning Kamp 			printf("\tHeads/cylinder:\t%d\n", ft.heads);
1679b28b5e6SPoul-Henning Kamp 			printf("\tCylinders/disk:\t%d\n", ft.tracks);
1689b28b5e6SPoul-Henning Kamp 			switch (ft.trans) {
1699b28b5e6SPoul-Henning Kamp 			case 0: printf("\tTransfer rate:\t500 kbps\n"); break;
1709b28b5e6SPoul-Henning Kamp 			case 1: printf("\tTransfer rate:\t300 kbps\n"); break;
1719b28b5e6SPoul-Henning Kamp 			case 2: printf("\tTransfer rate:\t250 kbps\n"); break;
1729b28b5e6SPoul-Henning Kamp 			case 3: printf("\tTransfer rate:\t1 Mbps\n"); break;
1739b28b5e6SPoul-Henning Kamp 			}
1749b28b5e6SPoul-Henning Kamp 			printf("\tSector gap:\t%d\n", ft.gap);
1759b28b5e6SPoul-Henning Kamp 			printf("\tFormat gap:\t%d\n", ft.f_gap);
1769b28b5e6SPoul-Henning Kamp 			printf("\tInterleave:\t%d\n", ft.f_inter);
1779b28b5e6SPoul-Henning Kamp 			printf("\tSide offset:\t%d\n", ft.offset_side2);
1789b28b5e6SPoul-Henning Kamp 			printf("\tFlags\t\t<");
1799b28b5e6SPoul-Henning Kamp 			s = "";
1809b28b5e6SPoul-Henning Kamp 			if (ft.flags & FL_MFM) {
1819b28b5e6SPoul-Henning Kamp 				printf("%sMFM", s);
1829b28b5e6SPoul-Henning Kamp 				s = ",";
1839b28b5e6SPoul-Henning Kamp 			}
1849b28b5e6SPoul-Henning Kamp 			if (ft.flags & FL_2STEP) {
1859b28b5e6SPoul-Henning Kamp 				printf("%s2STEP", s);
1869b28b5e6SPoul-Henning Kamp 				s = ",";
1879b28b5e6SPoul-Henning Kamp 			}
1889b28b5e6SPoul-Henning Kamp 			if (ft.flags & FL_PERPND) {
1899b28b5e6SPoul-Henning Kamp 				printf("%sPERPENDICULAR", s);
1909b28b5e6SPoul-Henning Kamp 				s = ",";
1919b28b5e6SPoul-Henning Kamp 			}
1921b67be7bSPoul-Henning Kamp 			if (ft.flags & FL_AUTO) {
1931b67be7bSPoul-Henning Kamp 				printf("%sAUTO", s);
1941b67be7bSPoul-Henning Kamp 				s = ",";
1951b67be7bSPoul-Henning Kamp 			}
1969b28b5e6SPoul-Henning Kamp 			printf(">\n");
1979b28b5e6SPoul-Henning Kamp 		} else {
1989b28b5e6SPoul-Henning Kamp 			print_fmt(ft);
1999b28b5e6SPoul-Henning Kamp 		}
2001a6bed68SJoerg Wunsch 		return (0);
201e9e8f7b7SJoerg Wunsch 	}
2021a6bed68SJoerg Wunsch 
2031a6bed68SJoerg Wunsch 	if (format || fmtstring) {
2041a6bed68SJoerg Wunsch 		if (ioctl(fd, FD_STYPE, &ft) == -1)
2051a6bed68SJoerg Wunsch 			err(EX_OSERR, "ioctl(FD_STYPE)");
2061a6bed68SJoerg Wunsch 		return (0);
2071a6bed68SJoerg Wunsch 	}
2081a6bed68SJoerg Wunsch 
2098268bea7SPhilippe Charnier 	return 0;
210e9e8f7b7SJoerg Wunsch }
211