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