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