1 /* vi: set sw=4 ts=4: */
2 /* fdformat.c - Low-level formats a floppy disk - Werner Almesberger */
4 /* 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
5 * - added Native Language Support
6 * 1999-03-20 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
7 * - more i18n/nls translatable strings marked
9 * 5 July 2003 -- modified for Busybox by Erik Andersen
15 /* Stuff extracted from linux/fd.h */
16 struct floppy_struct {
17 unsigned int size, /* nr of sectors total */
18 sect, /* sectors per track */
19 head, /* nr of heads */
20 track, /* nr of tracks */
21 stretch; /* !=0 means double track steps */
23 #define FD_SWAPSIDES 2
25 unsigned char gap, /* gap1 size */
27 rate, /* data rate. |= 0x40 for perpendicular */
29 #define FD_SIZECODEMASK 0x38
30 #define FD_SIZECODE(floppy) (((((floppy)->rate&FD_SIZECODEMASK)>> 3)+ 2) %8)
31 #define FD_SECTSIZE(floppy) ( (floppy)->rate & FD_2M ? \
32 512 : 128 << FD_SIZECODE(floppy) )
35 spec1, /* stepping rate, head unload time */
36 fmt_gap; /* gap2 size */
37 const char * name; /* used only for predefined formats */
40 unsigned int device,head,track;
42 #define FDFMTBEG _IO(2,0x47)
43 #define FDFMTTRK _IOW(2,0x48, struct format_descr)
44 #define FDFMTEND _IO(2,0x49)
45 #define FDGETPRM _IOR(2, 0x04, struct floppy_struct)
46 #define FD_FILL_BYTE 0xF6 /* format fill byte. */
48 static void xioctl(int fd, int request, void *argp, const char *string)
50 if (ioctl(fd, request, argp) < 0) {
51 bb_perror_msg_and_die(string);
55 int fdformat_main(int argc,char **argv);
56 int fdformat_main(int argc,char **argv)
58 int fd, n, cyl, read_bytes, verify;
61 struct floppy_struct param;
62 struct format_descr descr;
67 verify = !getopt32(argc, argv, "n");
71 if (!S_ISBLK(st.st_mode)) {
72 bb_error_msg_and_die("%s: not a block device", *argv);
73 /* do not test major - perhaps this was an USB floppy */
76 /* O_RDWR for formatting and verifying */
77 fd = xopen(*argv, O_RDWR);
79 /* original message was: "Could not determine current format type" */
80 xioctl(fd, FDGETPRM, ¶m, "FDGETPRM");
82 printf("%s-sided, %d tracks, %d sec/track. Total capacity %d kB\n",
83 (param.head == 2) ? "Double" : "Single",
84 param.track, param.sect, param.size >> 1);
87 printf("Formatting... ");
88 xioctl(fd, FDFMTBEG, NULL, "FDFMTBEG");
91 for (n = 0; n < param.track; n++) {
94 xioctl(fd, FDFMTTRK, &descr, "FDFMTTRK");
95 printf("%3d\b\b\b", n);
96 if (param.head == 2) {
98 xioctl(fd, FDFMTTRK, &descr, "FDFMTTRK");
102 xioctl(fd, FDFMTEND, NULL, "FDFMTEND");
108 n = param.sect*param.head*512;
111 printf("Verifying... ");
112 for (cyl = 0; cyl < param.track; cyl++) {
113 printf("%3d\b\b\b", cyl);
114 read_bytes = safe_read(fd, data, n);
115 if (read_bytes != n) {
116 if (read_bytes < 0) {
117 bb_perror_msg(bb_msg_read_error);
119 bb_error_msg_and_die("problem reading cylinder %d, "
120 "expected %d, read %d", cyl, n, read_bytes);
121 // FIXME: maybe better seek & continue??
123 /* Check backwards so we don't need a counter */
124 while (--read_bytes >= 0) {
125 if (data[read_bytes] != FD_FILL_BYTE) {
126 printf("bad data in cyl %d\nContinuing... ",cyl);
130 /* There is no point in freeing blocks at the end of a program, because
131 all of the program's space is given back to the system when the process
134 if (ENABLE_FEATURE_CLEAN_UP) free(data);
139 if (ENABLE_FEATURE_CLEAN_UP) close(fd);
141 /* Don't bother closing. Exit does
142 * that, so we can save a few bytes */