]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/fdformat/fdformat.c
This commit was generated by cvs2svn to compensate for changes in r42468,
[FreeBSD/FreeBSD.git] / usr.sbin / fdformat / fdformat.c
1 /*
2  * Copyright (C) 1992-1994 by Joerg Wunsch, Dresden
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
18  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
23  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 /*
28  * FreeBSD:
29  * format a floppy disk
30  *
31  * Added FD_GTYPE ioctl, verifying, proportional indicators.
32  * Serge Vakulenko, vak@zebub.msk.su
33  * Sat Dec 18 17:45:47 MSK 1993
34  *
35  * Final adaptation, change format/verify logic, add separate
36  * format gap/interleave values
37  * Andrew A. Chernov, ache@astral.msk.su
38  * Thu Jan 27 00:47:24 MSK 1994
39  */
40
41 #include <ctype.h>
42 #include <err.h>
43 #include <fcntl.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <strings.h>
47 #include <unistd.h>
48
49 #include <machine/ioctl_fd.h>
50
51 static void
52 format_track(int fd, int cyl, int secs, int head, int rate,
53              int gaplen, int secsize, int fill,int interleave)
54 {
55         struct fd_formb f;
56         register int i,j;
57         int il[FD_MAX_NSEC + 1];
58
59         memset(il,0,sizeof il);
60         for(j = 0, i = 1; i <= secs; i++) {
61             while(il[(j%secs)+1]) j++;
62             il[(j%secs)+1] = i;
63             j += interleave;
64         }
65
66         f.format_version = FD_FORMAT_VERSION;
67         f.head = head;
68         f.cyl = cyl;
69         f.transfer_rate = rate;
70
71         f.fd_formb_secshift = secsize;
72         f.fd_formb_nsecs = secs;
73         f.fd_formb_gaplen = gaplen;
74         f.fd_formb_fillbyte = fill;
75         for(i = 0; i < secs; i++) {
76                 f.fd_formb_cylno(i) = cyl;
77                 f.fd_formb_headno(i) = head;
78                 f.fd_formb_secno(i) = il[i+1];
79                 f.fd_formb_secsize(i) = secsize;
80         }
81         if(ioctl(fd, FD_FORM, (caddr_t)&f) < 0)
82                 err(1, "ioctl(FD_FORM)");
83 }
84
85 static int
86 verify_track(int fd, int track, int tracksize)
87 {
88         static char *buf = 0;
89         static int bufsz = 0;
90         int fdopts = -1, ofdopts, rv = 0;
91
92         if (ioctl(fd, FD_GOPTS, &fdopts) < 0)
93                 warn("warning: ioctl(FD_GOPTS)");
94         else {
95                 ofdopts = fdopts;
96                 fdopts |= FDOPT_NORETRY;
97                 (void)ioctl(fd, FD_SOPTS, &fdopts);
98         }
99
100         if (bufsz < tracksize) {
101                 if (buf)
102                         free (buf);
103                 bufsz = tracksize;
104                 buf = 0;
105         }
106         if (! buf)
107                 buf = malloc (bufsz);
108         if (! buf)
109                 errx(2, "out of memory");
110         if (lseek (fd, (long) track*tracksize, 0) < 0)
111                 rv = -1;
112         /* try twice reading it, without using the normal retrier */
113         else if (read (fd, buf, tracksize) != tracksize
114                  && read (fd, buf, tracksize) != tracksize)
115                 rv = -1;
116         if(fdopts != -1)
117                 (void)ioctl(fd, FD_SOPTS, &ofdopts);
118         return (rv);
119 }
120
121 static const char *
122 makename(const char *arg, const char *suffix)
123 {
124         static char namebuff[20];       /* big enough for "/dev/rfd0a"... */
125
126         memset(namebuff, 0, 20);
127         if(*arg == '\0') /* ??? */
128                 return arg;
129         if(*arg == '/')  /* do not convert absolute pathnames */
130                 return arg;
131         strcpy(namebuff, "/dev/r");
132         strncat(namebuff, arg, 3);
133         strcat(namebuff, suffix);
134         return namebuff;
135 }
136
137 static void
138 usage (void)
139 {
140         fprintf(stderr, "%s\n%s\n",
141         "usage: fdformat [-q] [-n | -v] [-f #] [-c #] [-s #] [-h #]",
142         "                [-r #] [-g #] [-i #] [-S #] [-F #] [-t #] devname");
143         exit(2);
144 }
145
146 static int
147 yes (void)
148 {
149         char reply [256], *p;
150
151         reply[sizeof(reply)-1] = 0;
152         for (;;) {
153                 fflush(stdout);
154                 if (! fgets (reply, sizeof(reply)-1, stdin))
155                         return (0);
156                 for (p=reply; *p==' ' || *p=='\t'; ++p)
157                         continue;
158                 if (*p=='y' || *p=='Y')
159                         return (1);
160                 if (*p=='n' || *p=='N' || *p=='\n' || *p=='\r')
161                         return (0);
162                 printf("Answer `yes' or `no': ");
163         }
164 }
165
166 int
167 main(int argc, char **argv)
168 {
169         int format = -1, cyls = -1, secs = -1, heads = -1, intleave = -1;
170         int rate = -1, gaplen = -1, secsize = -1, steps = -1;
171         int fill = 0xf6, quiet = 0, verify = 1, verify_only = 0;
172         int fd, c, track, error, tracks_per_dot, bytes_per_track, errs;
173         const char *devname, *suffix;
174         struct fd_type fdt;
175
176         while((c = getopt(argc, argv, "f:c:s:h:r:g:S:F:t:i:qvn")) != -1)
177                 switch(c) {
178                 case 'f':       /* format in kilobytes */
179                         format = atoi(optarg);
180                         break;
181
182                 case 'c':       /* # of cyls */
183                         cyls = atoi(optarg);
184                         break;
185
186                 case 's':       /* # of secs per track */
187                         secs = atoi(optarg);
188                         break;
189
190                 case 'h':       /* # of heads */
191                         heads = atoi(optarg);
192                         break;
193
194                 case 'r':       /* transfer rate, kilobyte/sec */
195                         rate = atoi(optarg);
196                         break;
197
198                 case 'g':       /* length of GAP3 to format with */
199                         gaplen = atoi(optarg);
200                         break;
201
202                 case 'S':       /* sector size shift factor (1 << S)*128 */
203                         secsize = atoi(optarg);
204                         break;
205
206                 case 'F':       /* fill byte, C-like notation allowed */
207                         fill = (int)strtol(optarg, (char **)0, 0);
208                         break;
209
210                 case 't':       /* steps per track */
211                         steps = atoi(optarg);
212                         break;
213
214                 case 'i':       /* interleave factor */
215                         intleave = atoi(optarg);
216                         break;
217
218                 case 'q':
219                         quiet = 1;
220                         break;
221
222                 case 'n':
223                         verify = 0;
224                         break;
225
226                 case 'v':
227                         verify = 1;
228                         verify_only = 1;
229                         break;
230
231                 case '?': default:
232                         usage();
233                 }
234
235         if(optind != argc - 1)
236                 usage();
237
238         switch(format) {
239         default:
240                 errx(2, "bad floppy size: %dK", format);
241         case -1:   suffix = "";      break;
242         case 360:  suffix = ".360";  break;
243         case 720:  suffix = ".720";  break;
244         case 800:  suffix = ".800";  break;
245         case 820:  suffix = ".820";  break;
246         case 1200: suffix = ".1200"; break;
247         case 1440: suffix = ".1440"; break;
248         case 1480: suffix = ".1480"; break;
249         case 1720: suffix = ".1720"; break;
250         }
251
252         devname = makename(argv[optind], suffix);
253
254         if((fd = open(devname, O_RDWR)) < 0)
255                 err(1, "%s", devname);
256
257         if(ioctl(fd, FD_GTYPE, &fdt) < 0)
258                 errx(1, "not a floppy disk: %s", devname);
259
260         switch(rate) {
261         case -1:  break;
262         case 250: fdt.trans = FDC_250KBPS; break;
263         case 300: fdt.trans = FDC_300KBPS; break;
264         case 500: fdt.trans = FDC_500KBPS; break;
265         default:
266                 errx(2, "invalid transfer rate: %d", rate);
267         }
268
269         if (cyls >= 0)    fdt.tracks = cyls;
270         if (secs >= 0)    fdt.sectrac = secs;
271         if (fdt.sectrac > FD_MAX_NSEC)
272                 errx(2, "too many sectors per track, max value is %d", FD_MAX_NSEC);
273         if (heads >= 0)   fdt.heads = heads;
274         if (gaplen >= 0)  fdt.f_gap = gaplen;
275         if (secsize >= 0) fdt.secsize = secsize;
276         if (steps >= 0)   fdt.steptrac = steps;
277         if (intleave >= 0) fdt.f_inter = intleave;
278
279         bytes_per_track = fdt.sectrac * (1<<fdt.secsize) * 128;
280         tracks_per_dot = fdt.tracks * fdt.heads / 40;
281
282         if (verify_only) {
283                 if(!quiet)
284                         printf("Verify %dK floppy `%s'.\n",
285                                 fdt.tracks * fdt.heads * bytes_per_track / 1024,
286                                 devname);
287         }
288         else if(!quiet) {
289                 printf("Format %dK floppy `%s'? (y/n): ",
290                         fdt.tracks * fdt.heads * bytes_per_track / 1024,
291                         devname);
292                 if(! yes ()) {
293                         printf("Not confirmed.\n");
294                         return 0;
295                 }
296         }
297
298         /*
299          * Formatting.
300          */
301         if(!quiet) {
302                 printf("Processing ----------------------------------------\r");
303                 printf("Processing ");
304                 fflush(stdout);
305         }
306
307         error = errs = 0;
308
309         for (track = 0; track < fdt.tracks * fdt.heads; track++) {
310                 if (!verify_only) {
311                         format_track(fd, track / fdt.heads, fdt.sectrac,
312                                 track % fdt.heads, fdt.trans, fdt.f_gap,
313                                 fdt.secsize, fill, fdt.f_inter);
314                         if(!quiet && !((track + 1) % tracks_per_dot)) {
315                                 putchar('F');
316                                 fflush(stdout);
317                         }
318                 }
319                 if (verify) {
320                         if (verify_track(fd, track, bytes_per_track) < 0)
321                                 error = errs = 1;
322                         if(!quiet && !((track + 1) % tracks_per_dot)) {
323                                 if (!verify_only)
324                                         putchar('\b');
325                                 if (error) {
326                                         putchar('E');
327                                         error = 0;
328                                 }
329                                 else
330                                         putchar('V');
331                                 fflush(stdout);
332                         }
333                 }
334         }
335         if(!quiet)
336                 printf(" done.\n");
337
338         return errs;
339 }
340 /*
341  * Local Variables:
342  *  c-indent-level:               8
343  *  c-continued-statement-offset: 8
344  *  c-continued-brace-offset:     0
345  *  c-brace-offset:              -8
346  *  c-brace-imaginary-offset:     0
347  *  c-argdecl-indent:             8
348  *  c-label-offset:              -8
349  *  c++-hanging-braces:           1
350  *  c++-access-specifier-offset: -8
351  *  c++-empty-arglist-indent:     8
352  *  c++-friend-offset:            0
353  * End:
354  */