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