2 * Copyright (C) 1992-1994 by Joerg Wunsch, Dresden
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 * format a floppy disk
31 * Added FD_GTYPE ioctl, verifying, proportional indicators.
32 * Serge Vakulenko, vak@zebub.msk.su
33 * Sat Dec 18 17:45:47 MSK 1993
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
49 #include <machine/ioctl_fd.h>
52 format_track(int fd, int cyl, int secs, int head, int rate,
53 int gaplen, int secsize, int fill,int interleave)
57 int il[FD_MAX_NSEC + 1];
59 memset(il,0,sizeof il);
60 for(j = 0, i = 1; i <= secs; i++) {
61 while(il[(j%secs)+1]) j++;
66 f.format_version = FD_FORMAT_VERSION;
69 f.transfer_rate = rate;
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;
81 if(ioctl(fd, FD_FORM, (caddr_t)&f) < 0)
82 err(1, "ioctl(FD_FORM)");
86 verify_track(int fd, int track, int tracksize)
90 int fdopts = -1, ofdopts, rv = 0;
92 if (ioctl(fd, FD_GOPTS, &fdopts) < 0)
93 warn("warning: ioctl(FD_GOPTS)");
96 fdopts |= FDOPT_NORETRY;
97 (void)ioctl(fd, FD_SOPTS, &fdopts);
100 if (bufsz < tracksize) {
107 buf = malloc (bufsz);
109 errx(2, "out of memory");
110 if (lseek (fd, (long) track*tracksize, 0) < 0)
112 /* try twice reading it, without using the normal retrier */
113 else if (read (fd, buf, tracksize) != tracksize
114 && read (fd, buf, tracksize) != tracksize)
117 (void)ioctl(fd, FD_SOPTS, &ofdopts);
122 makename(const char *arg, const char *suffix)
124 static char namebuff[20]; /* big enough for "/dev/rfd0a"... */
126 memset(namebuff, 0, 20);
127 if(*arg == '\0') /* ??? */
129 if(*arg == '/') /* do not convert absolute pathnames */
131 strcpy(namebuff, "/dev/r");
132 strncat(namebuff, arg, 3);
133 strcat(namebuff, suffix);
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");
149 char reply [256], *p;
151 reply[sizeof(reply)-1] = 0;
154 if (! fgets (reply, sizeof(reply)-1, stdin))
156 for (p=reply; *p==' ' || *p=='\t'; ++p)
158 if (*p=='y' || *p=='Y')
160 if (*p=='n' || *p=='N' || *p=='\n' || *p=='\r')
162 printf("Answer `yes' or `no': ");
167 main(int argc, char **argv)
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;
176 while((c = getopt(argc, argv, "f:c:s:h:r:g:S:F:t:i:qvn")) != -1)
178 case 'f': /* format in kilobytes */
179 format = atoi(optarg);
182 case 'c': /* # of cyls */
186 case 's': /* # of secs per track */
190 case 'h': /* # of heads */
191 heads = atoi(optarg);
194 case 'r': /* transfer rate, kilobyte/sec */
198 case 'g': /* length of GAP3 to format with */
199 gaplen = atoi(optarg);
202 case 'S': /* sector size shift factor (1 << S)*128 */
203 secsize = atoi(optarg);
206 case 'F': /* fill byte, C-like notation allowed */
207 fill = (int)strtol(optarg, (char **)0, 0);
210 case 't': /* steps per track */
211 steps = atoi(optarg);
214 case 'i': /* interleave factor */
215 intleave = atoi(optarg);
235 if(optind != argc - 1)
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;
252 devname = makename(argv[optind], suffix);
254 if((fd = open(devname, O_RDWR)) < 0)
255 err(1, "%s", devname);
257 if(ioctl(fd, FD_GTYPE, &fdt) < 0)
258 errx(1, "not a floppy disk: %s", devname);
262 case 250: fdt.trans = FDC_250KBPS; break;
263 case 300: fdt.trans = FDC_300KBPS; break;
264 case 500: fdt.trans = FDC_500KBPS; break;
266 errx(2, "invalid transfer rate: %d", rate);
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;
279 bytes_per_track = fdt.sectrac * (1<<fdt.secsize) * 128;
280 tracks_per_dot = fdt.tracks * fdt.heads / 40;
284 printf("Verify %dK floppy `%s'.\n",
285 fdt.tracks * fdt.heads * bytes_per_track / 1024,
289 printf("Format %dK floppy `%s'? (y/n): ",
290 fdt.tracks * fdt.heads * bytes_per_track / 1024,
293 printf("Not confirmed.\n");
302 printf("Processing ----------------------------------------\r");
303 printf("Processing ");
309 for (track = 0; track < fdt.tracks * fdt.heads; track++) {
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)) {
320 if (verify_track(fd, track, bytes_per_track) < 0)
322 if(!quiet && !((track + 1) % tracks_per_dot)) {
343 * c-continued-statement-offset: 8
344 * c-continued-brace-offset: 0
346 * c-brace-imaginary-offset: 0
347 * c-argdecl-indent: 8
349 * c++-hanging-braces: 1
350 * c++-access-specifier-offset: -8
351 * c++-empty-arglist-indent: 8
352 * c++-friend-offset: 0