move remaining help text from include/usage.src.h
[oweals/busybox.git] / util-linux / fdformat.c
1 /* vi: set sw=4 ts=4: */
2 /* fdformat.c  -  Low-level formats a floppy disk - Werner Almesberger
3  * 5 July 2003 -- modified for Busybox by Erik Andersen
4  *
5  * Licensed under GPLv2, see file LICENSE in this source tree.
6  */
7
8 //usage:#define fdformat_trivial_usage
9 //usage:       "[-n] DEVICE"
10 //usage:#define fdformat_full_usage "\n\n"
11 //usage:       "Format floppy disk\n"
12 //usage:     "\nOptions:"
13 //usage:     "\n        -n      Don't verify after format"
14
15 #include "libbb.h"
16
17
18 /* Stuff extracted from linux/fd.h */
19 struct floppy_struct {
20         unsigned int    size,           /* nr of sectors total */
21                         sect,           /* sectors per track */
22                         head,           /* nr of heads */
23                         track,          /* nr of tracks */
24                         stretch;        /* !=0 means double track steps */
25 #define FD_STRETCH 1
26 #define FD_SWAPSIDES 2
27
28         unsigned char   gap,            /* gap1 size */
29
30                         rate,           /* data rate. |= 0x40 for perpendicular */
31 #define FD_2M 0x4
32 #define FD_SIZECODEMASK 0x38
33 #define FD_SIZECODE(floppy) (((((floppy)->rate&FD_SIZECODEMASK)>> 3)+ 2) %8)
34 #define FD_SECTSIZE(floppy) ( (floppy)->rate & FD_2M ? \
35                              512 : 128 << FD_SIZECODE(floppy) )
36 #define FD_PERP 0x40
37
38                         spec1,          /* stepping rate, head unload time */
39                         fmt_gap;        /* gap2 size */
40         const char      * name; /* used only for predefined formats */
41 };
42 struct format_descr {
43         unsigned int device,head,track;
44 };
45 #define FDFMTBEG _IO(2,0x47)
46 #define FDFMTTRK _IOW(2,0x48, struct format_descr)
47 #define FDFMTEND _IO(2,0x49)
48 #define FDGETPRM _IOR(2, 0x04, struct floppy_struct)
49 #define FD_FILL_BYTE 0xF6 /* format fill byte. */
50
51 int fdformat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
52 int fdformat_main(int argc UNUSED_PARAM, char **argv)
53 {
54         int fd, n, cyl, read_bytes, verify;
55         unsigned char *data;
56         struct stat st;
57         struct floppy_struct param;
58         struct format_descr descr;
59
60         opt_complementary = "=1"; /* must have 1 param */
61         verify = !getopt32(argv, "n");
62         argv += optind;
63
64         xstat(*argv, &st);
65         if (!S_ISBLK(st.st_mode)) {
66                 bb_error_msg_and_die("%s: not a block device", *argv);
67                 /* do not test major - perhaps this was an USB floppy */
68         }
69
70         /* O_RDWR for formatting and verifying */
71         fd = xopen(*argv, O_RDWR);
72
73         /* original message was: "Could not determine current format type" */
74         xioctl(fd, FDGETPRM, &param);
75
76         printf("%s-sided, %d tracks, %d sec/track. Total capacity %d kB\n",
77                 (param.head == 2) ? "Double" : "Single",
78                 param.track, param.sect, param.size >> 1);
79
80         /* FORMAT */
81         printf("Formatting... ");
82         xioctl(fd, FDFMTBEG, NULL);
83
84         /* n == track */
85         for (n = 0; n < param.track; n++) {
86                 descr.head = 0;
87                 descr.track = n;
88                 xioctl(fd, FDFMTTRK, &descr);
89                 printf("%3d\b\b\b", n);
90                 if (param.head == 2) {
91                         descr.head = 1;
92                         xioctl(fd, FDFMTTRK, &descr);
93                 }
94         }
95
96         xioctl(fd, FDFMTEND, NULL);
97         printf("done\n");
98
99         /* VERIFY */
100         if (verify) {
101                 /* n == cyl_size */
102                 n = param.sect*param.head*512;
103
104                 data = xmalloc(n);
105                 printf("Verifying... ");
106                 for (cyl = 0; cyl < param.track; cyl++) {
107                         printf("%3d\b\b\b", cyl);
108                         read_bytes = safe_read(fd, data, n);
109                         if (read_bytes != n) {
110                                 if (read_bytes < 0) {
111                                         bb_perror_msg(bb_msg_read_error);
112                                 }
113                                 bb_error_msg_and_die("problem reading cylinder %d, "
114                                         "expected %d, read %d", cyl, n, read_bytes);
115                                 // FIXME: maybe better seek & continue??
116                         }
117                         /* Check backwards so we don't need a counter */
118                         while (--read_bytes >= 0) {
119                                 if (data[read_bytes] != FD_FILL_BYTE) {
120                                          printf("bad data in cyl %d\nContinuing... ", cyl);
121                                 }
122                         }
123                 }
124                 /* There is no point in freeing blocks at the end of a program, because
125                 all of the program's space is given back to the system when the process
126                 terminates.*/
127
128                 if (ENABLE_FEATURE_CLEAN_UP) free(data);
129
130                 printf("done\n");
131         }
132
133         if (ENABLE_FEATURE_CLEAN_UP) close(fd);
134
135         /* Don't bother closing.  Exit does
136          * that, so we can save a few bytes */
137         return EXIT_SUCCESS;
138 }