]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/fdcontrol/fdcontrol.c
genl: add a monitor subcommand
[FreeBSD/FreeBSD.git] / usr.sbin / fdcontrol / fdcontrol.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (C) 1994, 2001 by Joerg Wunsch, Dresden
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
22  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
26  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
27  * DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 #include <sys/fdcio.h>
32 #include <sys/file.h>
33
34 #include <err.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sysexits.h>
39 #include <unistd.h>
40
41 #include "fdutil.h"
42
43
44 static  int format, verbose, show = 1, showfmt;
45 static  char *fmtstring;
46
47 static void showdev(enum fd_drivetype, const char *);
48 static void usage(void) __dead2;
49
50 static void
51 usage(void)
52 {
53         errx(EX_USAGE,
54              "usage: fdcontrol [-F] [-d dbg] [-f fmt] [-s fmtstr] [-v] device");
55 }
56
57 void
58 showdev(enum fd_drivetype type, const char *fname)
59 {
60         const char *name, *descr;
61
62         getname(type, &name, &descr);
63         if (verbose)
64                 printf("%s: %s drive (%s)\n", fname, name, descr);
65         else
66                 printf("%s\n", name);
67 }
68
69 int
70 main(int argc, char **argv)
71 {
72         enum fd_drivetype type;
73         struct fd_type ft, newft, *fdtp;
74         const char *name, *descr;
75         int fd, i, autofmt;
76
77         autofmt = 0;
78         while((i = getopt(argc, argv, "aFf:s:v")) != -1)
79                 switch(i) {
80
81                 case 'a':
82                         autofmt = 1;
83                         /*FALLTHROUGH*/
84                 case 'F':
85                         showfmt = 1;
86                         show = 0;
87                         break;
88
89                 case 'f':
90                         if (!strcmp(optarg, "auto")) {
91                                 format = -1;
92                         } else if (getnum(optarg, &format)) {
93                                 fprintf(stderr,
94                         "Bad argument %s to -f option; must be numeric\n",
95                                         optarg);
96                                 usage();
97                         }
98                         show = 0;
99                         break;
100
101                 case 's':
102                         fmtstring = optarg;
103                         show = 0;
104                         break;
105
106                 case 'v':
107                         verbose++;
108                         break;
109
110                 default:
111                         usage();
112                 }
113
114         argc -= optind;
115         argv += optind;
116
117         if(argc != 1)
118                 usage();
119
120         if((fd = open(argv[0], O_RDONLY | O_NONBLOCK)) < 0)
121                 err(EX_UNAVAILABLE, "open(%s)", argv[0]);
122
123         if (ioctl(fd, FD_GDTYPE, &type) == -1)
124                 err(EX_OSERR, "ioctl(FD_GDTYPE)");
125         if (ioctl(fd, FD_GTYPE, &ft) == -1)
126                 err(EX_OSERR, "ioctl(FD_GTYPE)");
127
128         if (show) {
129                 showdev(type, argv[0]);
130                 return (0);
131         }
132
133         if (autofmt) {
134                 memset(&newft, 0, sizeof newft);
135                 ft = newft;
136         }
137
138         if (format) {
139                 getname(type, &name, &descr);
140                 fdtp = get_fmt(format, type);
141                 if (fdtp == 0)
142                         errx(EX_USAGE,
143                             "unknown format %d KB for drive type %s",
144                             format, name);
145                 ft = *fdtp;
146         }
147
148         if (fmtstring) {
149                 parse_fmt(fmtstring, type, ft, &newft);
150                 ft = newft;
151         }
152
153         if (showfmt) {
154                 if (verbose) {
155                         const char *s;
156
157                         printf("%s: %d KB media type\n", argv[0],
158                             (128 << ft.secsize) * ft.size / 1024);
159                         printf("\tFormat:\t\t");
160                         print_fmt(ft);
161                         if (ft.datalen != 0xff &&
162                             ft.datalen != (128 << ft.secsize))
163                                 printf("\tData length:\t%d\n", ft.datalen);
164                         printf("\tSector size:\t%d\n", 128 << ft.secsize);
165                         printf("\tSectors/track:\t%d\n", ft.sectrac);
166                         printf("\tHeads/cylinder:\t%d\n", ft.heads);
167                         printf("\tCylinders/disk:\t%d\n", ft.tracks);
168                         switch (ft.trans) {
169                         case 0: printf("\tTransfer rate:\t500 kbps\n"); break;
170                         case 1: printf("\tTransfer rate:\t300 kbps\n"); break;
171                         case 2: printf("\tTransfer rate:\t250 kbps\n"); break;
172                         case 3: printf("\tTransfer rate:\t1 Mbps\n"); break;
173                         }
174                         printf("\tSector gap:\t%d\n", ft.gap);
175                         printf("\tFormat gap:\t%d\n", ft.f_gap);
176                         printf("\tInterleave:\t%d\n", ft.f_inter);
177                         printf("\tSide offset:\t%d\n", ft.offset_side2);
178                         printf("\tFlags\t\t<");
179                         s = "";
180                         if (ft.flags & FL_MFM) {
181                                 printf("%sMFM", s);
182                                 s = ",";
183                         }
184                         if (ft.flags & FL_2STEP) {
185                                 printf("%s2STEP", s);
186                                 s = ",";
187                         }
188                         if (ft.flags & FL_PERPND) {
189                                 printf("%sPERPENDICULAR", s);
190                                 s = ",";
191                         }
192                         if (ft.flags & FL_AUTO) {
193                                 printf("%sAUTO", s);
194                                 s = ",";
195                         }
196                         printf(">\n");
197                 } else {
198                         print_fmt(ft);
199                 }
200                 return (0);
201         }
202
203         if (format || fmtstring) {
204                 if (ioctl(fd, FD_STYPE, &ft) == -1)
205                         err(EX_OSERR, "ioctl(FD_STYPE)");
206                 return (0);
207         }
208
209         return 0;
210 }