1*d7259957Scheloha /* $OpenBSD: fdformat.c,v 1.25 2022/12/04 23:50:50 cheloha Exp $ */
211c870c2Sdownsj
311c870c2Sdownsj /*
411c870c2Sdownsj * Copyright (C) 1992-1994 by Joerg Wunsch, Dresden
511c870c2Sdownsj * All rights reserved.
611c870c2Sdownsj *
711c870c2Sdownsj * Redistribution and use in source and binary forms, with or without
811c870c2Sdownsj * modification, are permitted provided that the following conditions
911c870c2Sdownsj * are met:
1011c870c2Sdownsj * 1. Redistributions of source code must retain the above copyright
1111c870c2Sdownsj * notice, this list of conditions and the following disclaimer.
1211c870c2Sdownsj * 2. Redistributions in binary form must reproduce the above copyright
1311c870c2Sdownsj * notice, this list of conditions and the following disclaimer in the
1411c870c2Sdownsj * documentation and/or other materials provided with the distribution.
1511c870c2Sdownsj *
1611c870c2Sdownsj * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
1711c870c2Sdownsj * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1811c870c2Sdownsj * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1911c870c2Sdownsj * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
2011c870c2Sdownsj * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2111c870c2Sdownsj * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2211c870c2Sdownsj * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2311c870c2Sdownsj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2411c870c2Sdownsj * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
2511c870c2Sdownsj * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2611c870c2Sdownsj * POSSIBILITY OF SUCH DAMAGE.
2711c870c2Sdownsj */
2811c870c2Sdownsj
2911c870c2Sdownsj /*
3011c870c2Sdownsj * FreeBSD:
3111c870c2Sdownsj * format a floppy disk
3211c870c2Sdownsj *
3311c870c2Sdownsj * Added FD_GTYPE ioctl, verifying, proportional indicators.
3411c870c2Sdownsj * Serge Vakulenko, vak@zebub.msk.su
3511c870c2Sdownsj * Sat Dec 18 17:45:47 MSK 1993
3611c870c2Sdownsj *
3711c870c2Sdownsj * Final adaptation, change format/verify logic, add separate
3811c870c2Sdownsj * format gap/interleave values
3911c870c2Sdownsj * Andrew A. Chernov, ache@astral.msk.su
4011c870c2Sdownsj * Thu Jan 27 00:47:24 MSK 1994
4111c870c2Sdownsj */
4211c870c2Sdownsj
4311c870c2Sdownsj #include <stdio.h>
4411c870c2Sdownsj #include <stdlib.h>
4511c870c2Sdownsj #include <unistd.h>
4611c870c2Sdownsj #include <fcntl.h>
471d1da20fSmillert #include <string.h>
48a47b6461Sderaadt #include <limits.h>
4911c870c2Sdownsj #include <ctype.h>
50e3f41f19Sdownsj #include <err.h>
51930d1a1bSmillert #include <util.h>
5211c870c2Sdownsj
5311c870c2Sdownsj #include <errno.h>
5411c870c2Sdownsj #include <sys/types.h>
5511c870c2Sdownsj #include <sys/ioctl.h>
5611c870c2Sdownsj #include <machine/ioctl_fd.h>
5711c870c2Sdownsj
58e3f41f19Sdownsj extern const char *__progname;
59e3f41f19Sdownsj
6011c870c2Sdownsj static void
format_track(int fd,int cyl,int secs,int head,int rate,int gaplen,int secsize,int fill,int interleave)6100da5b9dSderaadt format_track(int fd, int cyl, int secs, int head, int rate, int gaplen,
6200da5b9dSderaadt int secsize, int fill, int interleave)
6311c870c2Sdownsj {
6411c870c2Sdownsj struct fd_formb f;
650ac0d02eSmpech int i,j;
6611c870c2Sdownsj int il[FD_MAX_NSEC + 1];
6711c870c2Sdownsj
6811c870c2Sdownsj memset(il,0,sizeof il);
6911c870c2Sdownsj for(j = 0, i = 1; i <= secs; i++) {
70d15b7c8aSderaadt while(il[(j%secs)+1])
71d15b7c8aSderaadt j++;
7211c870c2Sdownsj il[(j%secs)+1] = i;
7311c870c2Sdownsj j += interleave;
7411c870c2Sdownsj }
7511c870c2Sdownsj
7611c870c2Sdownsj f.format_version = FD_FORMAT_VERSION;
7711c870c2Sdownsj f.head = head;
7811c870c2Sdownsj f.cyl = cyl;
7911c870c2Sdownsj f.transfer_rate = rate;
8011c870c2Sdownsj
8111c870c2Sdownsj f.fd_formb_secshift = secsize;
8211c870c2Sdownsj f.fd_formb_nsecs = secs;
8311c870c2Sdownsj f.fd_formb_gaplen = gaplen;
8411c870c2Sdownsj f.fd_formb_fillbyte = fill;
8511c870c2Sdownsj for(i = 0; i < secs; i++) {
8611c870c2Sdownsj f.fd_formb_cylno(i) = cyl;
8711c870c2Sdownsj f.fd_formb_headno(i) = head;
8811c870c2Sdownsj f.fd_formb_secno(i) = il[i+1];
8911c870c2Sdownsj f.fd_formb_secsize(i) = secsize;
9011c870c2Sdownsj }
91df69c215Sderaadt if (ioctl(fd, FD_FORM, (caddr_t)&f) == -1)
92e3f41f19Sdownsj err(1, "FD_FORM");
9311c870c2Sdownsj }
9411c870c2Sdownsj
9511c870c2Sdownsj static int
verify_track(int fd,int track,int tracksize)9600da5b9dSderaadt verify_track(int fd, int track, int tracksize)
9711c870c2Sdownsj {
9811c870c2Sdownsj static char *buf = 0;
9911c870c2Sdownsj static int bufsz = 0;
10011c870c2Sdownsj int fdopts = -1, ofdopts, rv = 0;
10111c870c2Sdownsj
102df69c215Sderaadt if (ioctl(fd, FD_GOPTS, &fdopts) == -1)
103e3f41f19Sdownsj warn("FD_GOPTS");
10411c870c2Sdownsj else {
10511c870c2Sdownsj ofdopts = fdopts;
10611c870c2Sdownsj fdopts |= FDOPT_NORETRY;
10711c870c2Sdownsj (void)ioctl(fd, FD_SOPTS, &fdopts);
10811c870c2Sdownsj }
10911c870c2Sdownsj
11011c870c2Sdownsj if (bufsz < tracksize) {
11111c870c2Sdownsj free(buf);
11211c870c2Sdownsj bufsz = tracksize;
11311c870c2Sdownsj buf = 0;
11411c870c2Sdownsj }
11511c870c2Sdownsj if (! buf)
11611c870c2Sdownsj buf = malloc(bufsz);
11711c870c2Sdownsj if (! buf) {
11811c870c2Sdownsj fprintf (stderr, "\nfdformat: out of memory\n");
11911c870c2Sdownsj exit (2);
12011c870c2Sdownsj }
121df69c215Sderaadt if (lseek (fd, (off_t) track*tracksize, SEEK_SET) == -1)
12211c870c2Sdownsj rv = -1;
12311c870c2Sdownsj /* try twice reading it, without using the normal retrier */
12411c870c2Sdownsj else if (read (fd, buf, tracksize) != tracksize
12511c870c2Sdownsj && read (fd, buf, tracksize) != tracksize)
12611c870c2Sdownsj rv = -1;
12711c870c2Sdownsj if (fdopts != -1)
12811c870c2Sdownsj (void)ioctl(fd, FD_SOPTS, &ofdopts);
12911c870c2Sdownsj return (rv);
13011c870c2Sdownsj }
13111c870c2Sdownsj
13211c870c2Sdownsj static void
usage(void)13300da5b9dSderaadt usage(void)
13411c870c2Sdownsj {
135ca33c524Sjmc printf("usage: %s [-nqv] [-c cyls] [-F fillbyte] [-g gap3len] "
136ca33c524Sjmc "[-h heads]\n"
137ca33c524Sjmc " [-i intleave] [-r rate] [-S secshft] [-s secs]\n"
138ca33c524Sjmc " [-t steps_per_track] device_name\n", __progname);
1396feebd7eSderaadt exit(2);
14011c870c2Sdownsj }
14111c870c2Sdownsj
14211c870c2Sdownsj static int
yes(void)14300da5b9dSderaadt yes(void)
14411c870c2Sdownsj {
14511c870c2Sdownsj char reply[256], *p;
14611c870c2Sdownsj
14711c870c2Sdownsj for (;;) {
14811c870c2Sdownsj fflush(stdout);
149577f803eSray if (!fgets(reply, sizeof(reply), stdin))
15011c870c2Sdownsj return (0);
15111c870c2Sdownsj for (p=reply; *p==' ' || *p=='\t'; ++p)
15211c870c2Sdownsj continue;
15311c870c2Sdownsj if (*p=='y' || *p=='Y')
15411c870c2Sdownsj return (1);
15511c870c2Sdownsj if (*p=='n' || *p=='N' || *p=='\n' || *p=='\r')
15611c870c2Sdownsj return (0);
15711c870c2Sdownsj printf("Answer `yes' or `no': ");
15811c870c2Sdownsj }
15911c870c2Sdownsj }
16011c870c2Sdownsj
16111c870c2Sdownsj int
main(int argc,char * argv[])16200da5b9dSderaadt main(int argc, char *argv[])
16311c870c2Sdownsj {
16411c870c2Sdownsj int cyls = -1, secs = -1, heads = -1, intleave = -1;
16511c870c2Sdownsj int rate = -1, gaplen = -1, secsize = -1, steps = -1;
16611c870c2Sdownsj int fill = 0xf6, quiet = 0, verify = 1, verify_only = 0;
16711c870c2Sdownsj int fd, c, track, error, tracks_per_dot, bytes_per_track, errs;
168a47b6461Sderaadt const char *errstr;
169e3f41f19Sdownsj char *devname;
17011c870c2Sdownsj struct fd_type fdt;
17111c870c2Sdownsj
17211c870c2Sdownsj while((c = getopt(argc, argv, "c:s:h:r:g:S:F:t:i:qvn")) != -1)
17311c870c2Sdownsj switch (c) {
17411c870c2Sdownsj case 'c': /* # of cyls */
175a47b6461Sderaadt cyls = strtonum(optarg, 1, INT_MAX, &errstr);
176a47b6461Sderaadt if (errstr)
177a47b6461Sderaadt errx(1, "-c %s: %s", optarg, errstr);
17811c870c2Sdownsj break;
17911c870c2Sdownsj
18011c870c2Sdownsj case 's': /* # of secs per track */
181a47b6461Sderaadt secs = strtonum(optarg, 1, INT_MAX, &errstr);
182a47b6461Sderaadt if (errstr)
183a47b6461Sderaadt errx(1, "-s %s: %s", optarg, errstr);
18411c870c2Sdownsj break;
18511c870c2Sdownsj
18611c870c2Sdownsj case 'h': /* # of heads */
187a47b6461Sderaadt heads = strtonum(optarg, 1, INT_MAX, &errstr);
188a47b6461Sderaadt if (errstr)
189a47b6461Sderaadt errx(1, "-h %s: %s", optarg, errstr);
19011c870c2Sdownsj break;
19111c870c2Sdownsj
19211c870c2Sdownsj case 'r': /* transfer rate, kilobyte/sec */
193a47b6461Sderaadt rate = strtonum(optarg, 1, INT_MAX, &errstr);
194a47b6461Sderaadt if (errstr)
195a47b6461Sderaadt errx(1, "-r %s: %s", optarg, errstr);
19611c870c2Sdownsj break;
19711c870c2Sdownsj
19811c870c2Sdownsj case 'g': /* length of GAP3 to format with */
199a47b6461Sderaadt gaplen = strtonum(optarg, 1, INT_MAX, &errstr);
200a47b6461Sderaadt if (errstr)
201a47b6461Sderaadt errx(1, "-g %s: %s", optarg, errstr);
20211c870c2Sdownsj break;
20311c870c2Sdownsj
20411c870c2Sdownsj case 'S': /* sector size shift factor (1 << S)*128 */
205a47b6461Sderaadt secsize = strtonum(optarg, 0, INT_MAX, &errstr);
206a47b6461Sderaadt if (errstr)
207a47b6461Sderaadt errx(1, "-S %s: %s", optarg, errstr);
20811c870c2Sdownsj break;
20911c870c2Sdownsj
21011c870c2Sdownsj case 'F': /* fill byte, C-like notation allowed */
211c9899b11Skrw fill = (int)strtol(optarg, NULL, 0);
21211c870c2Sdownsj break;
21311c870c2Sdownsj
21411c870c2Sdownsj case 't': /* steps per track */
215a47b6461Sderaadt steps = strtonum(optarg, 1, INT_MAX, &errstr);
216a47b6461Sderaadt if (errstr)
217a47b6461Sderaadt errx(1, "-t %s: %s", optarg, errstr);
21811c870c2Sdownsj break;
21911c870c2Sdownsj
22011c870c2Sdownsj case 'i': /* interleave factor */
221a47b6461Sderaadt intleave = strtonum(optarg, 1, INT_MAX, &errstr);
222a47b6461Sderaadt if (errstr)
223a47b6461Sderaadt errx(1, "-i %s: %s", optarg, errstr);
22411c870c2Sdownsj break;
22511c870c2Sdownsj
22611c870c2Sdownsj case 'q':
22711c870c2Sdownsj quiet = 1;
22811c870c2Sdownsj break;
22911c870c2Sdownsj
23011c870c2Sdownsj case 'n':
23111c870c2Sdownsj verify = 0;
23211c870c2Sdownsj break;
23311c870c2Sdownsj
23411c870c2Sdownsj case 'v':
23511c870c2Sdownsj verify = 1;
23611c870c2Sdownsj verify_only = 1;
23711c870c2Sdownsj break;
23811c870c2Sdownsj
239*d7259957Scheloha default:
24011c870c2Sdownsj usage();
24111c870c2Sdownsj }
24211c870c2Sdownsj
24311c870c2Sdownsj if (optind != argc - 1)
24411c870c2Sdownsj usage();
24511c870c2Sdownsj
246df69c215Sderaadt if ((fd = opendev(argv[optind], O_RDWR, OPENDEV_PART, &devname)) == -1)
247b638aa94Smillert err(1, "%s", devname);
24811c870c2Sdownsj
249df69c215Sderaadt if (ioctl(fd, FD_GTYPE, &fdt) == -1)
250e3f41f19Sdownsj errx(1, "not a floppy disk: %s", devname);
25111c870c2Sdownsj
25211c870c2Sdownsj switch (rate) {
253d15b7c8aSderaadt case -1:
254d15b7c8aSderaadt break;
255d15b7c8aSderaadt case 250:
256d15b7c8aSderaadt fdt.rate = FDC_250KBPS;
257d15b7c8aSderaadt break;
258d15b7c8aSderaadt case 300:
259d15b7c8aSderaadt fdt.rate = FDC_300KBPS;
260d15b7c8aSderaadt break;
261d15b7c8aSderaadt case 500:
262d15b7c8aSderaadt fdt.rate = FDC_500KBPS;
263d15b7c8aSderaadt break;
26411c870c2Sdownsj default:
265e3f41f19Sdownsj errx(1, "invalid transfer rate: %d", rate);
26611c870c2Sdownsj }
26711c870c2Sdownsj
268d15b7c8aSderaadt if (cyls >= 0)
269d15b7c8aSderaadt fdt.tracks = cyls;
270d15b7c8aSderaadt if (secs >= 0)
271d15b7c8aSderaadt fdt.sectrac = secs;
272e3f41f19Sdownsj if (fdt.sectrac > FD_MAX_NSEC)
273e3f41f19Sdownsj errx(1, "too many sectors per track, max value is %d",
274e3f41f19Sdownsj FD_MAX_NSEC);
275d15b7c8aSderaadt if (heads >= 0)
276d15b7c8aSderaadt fdt.heads = heads;
277d15b7c8aSderaadt if (gaplen >= 0)
278d15b7c8aSderaadt fdt.gap2 = gaplen;
279d15b7c8aSderaadt if (secsize >= 0)
280d15b7c8aSderaadt fdt.secsize = secsize;
281d15b7c8aSderaadt if (steps >= 0)
282d15b7c8aSderaadt fdt.step = steps;
28311c870c2Sdownsj
28411c870c2Sdownsj bytes_per_track = fdt.sectrac * (1<<fdt.secsize) * 128;
28511c870c2Sdownsj tracks_per_dot = fdt.tracks * fdt.heads / 40;
286cc8b5a71Smiod if (tracks_per_dot == 0)
287cc8b5a71Smiod tracks_per_dot++;
28811c870c2Sdownsj
28911c870c2Sdownsj if (verify_only) {
29011c870c2Sdownsj if (!quiet)
29111c870c2Sdownsj printf("Verify %dK floppy `%s'.\n",
29211c870c2Sdownsj fdt.tracks * fdt.heads * bytes_per_track / 1024,
29311c870c2Sdownsj devname);
29411c870c2Sdownsj }
29511c870c2Sdownsj else if (!quiet) {
29611c870c2Sdownsj printf("Format %dK floppy `%s'? (y/n): ",
29711c870c2Sdownsj fdt.tracks * fdt.heads * bytes_per_track / 1024,
29811c870c2Sdownsj devname);
29911c870c2Sdownsj if (!yes()) {
30011c870c2Sdownsj printf("Not confirmed.\n");
301e3f41f19Sdownsj exit(0);
30211c870c2Sdownsj }
30311c870c2Sdownsj }
30411c870c2Sdownsj
30511c870c2Sdownsj /*
30611c870c2Sdownsj * Formatting.
30711c870c2Sdownsj */
30811c870c2Sdownsj if (!quiet) {
309d15b7c8aSderaadt printf("Processing ");
31078e9c359Sderaadt for (track = 0; track < fdt.tracks * fdt.heads; track++) {
311d15b7c8aSderaadt if (!((track + 1) % tracks_per_dot))
31278e9c359Sderaadt putchar('-');
31378e9c359Sderaadt }
314d15b7c8aSderaadt putchar('\r');
31511c870c2Sdownsj printf("Processing ");
31611c870c2Sdownsj fflush(stdout);
31711c870c2Sdownsj }
31811c870c2Sdownsj
31911c870c2Sdownsj error = errs = 0;
32011c870c2Sdownsj
32111c870c2Sdownsj for (track = 0; track < fdt.tracks * fdt.heads; track++) {
32211c870c2Sdownsj if (!verify_only) {
32311c870c2Sdownsj format_track(fd, track / fdt.heads, fdt.sectrac,
32411c870c2Sdownsj track % fdt.heads, fdt.rate, fdt.gap2,
32511c870c2Sdownsj fdt.secsize, fill,
32611c870c2Sdownsj intleave >= 0 ? intleave : 1);
32711c870c2Sdownsj if (!quiet && !((track + 1) % tracks_per_dot)) {
32811c870c2Sdownsj putchar('F');
32911c870c2Sdownsj fflush(stdout);
33011c870c2Sdownsj }
33111c870c2Sdownsj }
33211c870c2Sdownsj if (verify) {
33311c870c2Sdownsj if (verify_track(fd, track, bytes_per_track) < 0)
33411c870c2Sdownsj error = errs = 1;
33511c870c2Sdownsj if (!quiet && !((track + 1) % tracks_per_dot)) {
33611c870c2Sdownsj if (!verify_only)
33711c870c2Sdownsj putchar('\b');
33811c870c2Sdownsj if (error) {
33911c870c2Sdownsj putchar('E');
34011c870c2Sdownsj error = 0;
34111c870c2Sdownsj }
34211c870c2Sdownsj else
34311c870c2Sdownsj putchar('V');
34411c870c2Sdownsj fflush(stdout);
34511c870c2Sdownsj }
34611c870c2Sdownsj }
34711c870c2Sdownsj }
3483dff2522Sderaadt close(fd);
34911c870c2Sdownsj if (!quiet)
35011c870c2Sdownsj printf(" done.\n");
35111c870c2Sdownsj
352e3f41f19Sdownsj exit(errs);
35311c870c2Sdownsj }
354