blob: d694bd631eb82ec131ef9964b9378a8daac6bf56 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Eric Andersen01c3d402003-07-05 07:51:31 +00002/* fdformat.c - Low-level formats a floppy disk - Werner Almesberger */
3
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
8 *
9 * 5 July 2003 -- modified for Busybox by Erik Andersen
10 */
11
Eric Andersen01c3d402003-07-05 07:51:31 +000012#include "busybox.h"
13
14
15/* Stuff extracted from linux/fd.h */
16struct 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 */
22#define FD_STRETCH 1
23#define FD_SWAPSIDES 2
24
25 unsigned char gap, /* gap1 size */
26
27 rate, /* data rate. |= 0x40 for perpendicular */
28#define FD_2M 0x4
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) )
33#define FD_PERP 0x40
34
35 spec1, /* stepping rate, head unload time */
36 fmt_gap; /* gap2 size */
37 const char * name; /* used only for predefined formats */
38};
39struct format_descr {
40 unsigned int device,head,track;
41};
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. */
47
Rob Landleyd921b2e2006-08-03 15:41:12 +000048static void xioctl(int fd, int request, void *argp, const char *string)
Eric Andersen01c3d402003-07-05 07:51:31 +000049{
Denis Vlasenko92758142006-10-03 19:56:34 +000050 if (ioctl(fd, request, argp) < 0) {
Glenn L McGrath348672d2004-01-20 12:57:18 +000051 bb_perror_msg_and_die(string);
Eric Andersen01c3d402003-07-05 07:51:31 +000052 }
Eric Andersen01c3d402003-07-05 07:51:31 +000053}
54
Denis Vlasenko06af2162007-02-03 17:28:39 +000055int fdformat_main(int argc,char **argv);
Eric Andersen01c3d402003-07-05 07:51:31 +000056int fdformat_main(int argc,char **argv)
57{
Glenn L McGrath348672d2004-01-20 12:57:18 +000058 int fd, n, cyl, read_bytes, verify;
59 unsigned char *data;
60 struct stat st;
61 struct floppy_struct param;
62 struct format_descr descr;
Eric Andersen01c3d402003-07-05 07:51:31 +000063
Glenn L McGrath348672d2004-01-20 12:57:18 +000064 if (argc < 2) {
65 bb_show_usage();
66 }
Denis Vlasenko67b23e62006-10-03 21:00:06 +000067 verify = !getopt32(argc, argv, "n");
Glenn L McGrath348672d2004-01-20 12:57:18 +000068 argv += optind;
Eric Andersen01c3d402003-07-05 07:51:31 +000069
Denis Vlasenkob6332242006-10-03 19:57:50 +000070 xstat(*argv, &st);
Glenn L McGrath348672d2004-01-20 12:57:18 +000071 if (!S_ISBLK(st.st_mode)) {
Denis Vlasenko92758142006-10-03 19:56:34 +000072 bb_error_msg_and_die("%s: not a block device", *argv);
Glenn L McGrath348672d2004-01-20 12:57:18 +000073 /* do not test major - perhaps this was an USB floppy */
74 }
Eric Andersen01c3d402003-07-05 07:51:31 +000075
Glenn L McGrath348672d2004-01-20 12:57:18 +000076 /* O_RDWR for formatting and verifying */
Denis Vlasenko92758142006-10-03 19:56:34 +000077 fd = xopen(*argv, O_RDWR);
Glenn L McGrath348672d2004-01-20 12:57:18 +000078
Denis Vlasenkob6332242006-10-03 19:57:50 +000079 /* original message was: "Could not determine current format type" */
80 xioctl(fd, FDGETPRM, &param, "FDGETPRM");
Glenn L McGrath348672d2004-01-20 12:57:18 +000081
Denis Vlasenkob6332242006-10-03 19:57:50 +000082 printf("%s-sided, %d tracks, %d sec/track. Total capacity %d kB\n",
Glenn L McGrath348672d2004-01-20 12:57:18 +000083 (param.head == 2) ? "Double" : "Single",
84 param.track, param.sect, param.size >> 1);
85
86 /* FORMAT */
Denis Vlasenko92758142006-10-03 19:56:34 +000087 printf("Formatting... ");
88 xioctl(fd, FDFMTBEG, NULL, "FDFMTBEG");
Glenn L McGrath348672d2004-01-20 12:57:18 +000089
90 /* n == track */
Denis Vlasenko92758142006-10-03 19:56:34 +000091 for (n = 0; n < param.track; n++) {
92 descr.head = 0;
93 descr.track = n;
94 xioctl(fd, FDFMTTRK, &descr, "FDFMTTRK");
95 printf("%3d\b\b\b", n);
96 if (param.head == 2) {
97 descr.head = 1;
98 xioctl(fd, FDFMTTRK, &descr, "FDFMTTRK");
99 }
Glenn L McGrath348672d2004-01-20 12:57:18 +0000100 }
Eric Andersen522a2f32004-04-03 12:36:03 +0000101
Denis Vlasenko92758142006-10-03 19:56:34 +0000102 xioctl(fd, FDFMTEND, NULL, "FDFMTEND");
Rob Landley59fe8b92006-08-04 22:02:55 +0000103 printf("done\n");
Glenn L McGrath348672d2004-01-20 12:57:18 +0000104
105 /* VERIFY */
Denis Vlasenko92758142006-10-03 19:56:34 +0000106 if (verify) {
Glenn L McGrath348672d2004-01-20 12:57:18 +0000107 /* n == cyl_size */
108 n = param.sect*param.head*512;
109
110 data = xmalloc(n);
Denis Vlasenko92758142006-10-03 19:56:34 +0000111 printf("Verifying... ");
Glenn L McGrath348672d2004-01-20 12:57:18 +0000112 for (cyl = 0; cyl < param.track; cyl++) {
Rob Landley59fe8b92006-08-04 22:02:55 +0000113 printf("%3d\b\b\b", cyl);
Denis Vlasenko92758142006-10-03 19:56:34 +0000114 read_bytes = safe_read(fd, data, n);
115 if (read_bytes != n) {
116 if (read_bytes < 0) {
Bernhard Reutner-Fischer1b9d7c92006-06-03 22:45:37 +0000117 bb_perror_msg(bb_msg_read_error);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000118 }
Denis Vlasenkob6332242006-10-03 19:57:50 +0000119 bb_error_msg_and_die("problem reading cylinder %d, "
120 "expected %d, read %d", cyl, n, read_bytes);
Denis Vlasenko92758142006-10-03 19:56:34 +0000121 // FIXME: maybe better seek & continue??
Glenn L McGrath348672d2004-01-20 12:57:18 +0000122 }
123 /* Check backwards so we don't need a counter */
Denis Vlasenko92758142006-10-03 19:56:34 +0000124 while (--read_bytes >= 0) {
125 if (data[read_bytes] != FD_FILL_BYTE) {
126 printf("bad data in cyl %d\nContinuing... ",cyl);
Glenn L McGrath348672d2004-01-20 12:57:18 +0000127 }
128 }
129 }
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
132 terminates.*/
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000133
Rob Landley658d2cf2005-09-08 03:11:58 +0000134 if (ENABLE_FEATURE_CLEAN_UP) free(data);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000135
Rob Landley59fe8b92006-08-04 22:02:55 +0000136 printf("done\n");
Glenn L McGrath348672d2004-01-20 12:57:18 +0000137 }
Rob Landley658d2cf2005-09-08 03:11:58 +0000138
139 if (ENABLE_FEATURE_CLEAN_UP) close(fd);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000140
Glenn L McGrath348672d2004-01-20 12:57:18 +0000141 /* Don't bother closing. Exit does
142 * that, so we can save a few bytes */
143 return EXIT_SUCCESS;
Eric Andersen01c3d402003-07-05 07:51:31 +0000144}