blob: 6f49cec8fbf73519831d044a71942ee34e67d389 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00002/* fdformat.c - Low-level formats a floppy disk - Werner Almesberger
3 * 5 July 2003 -- modified for Busybox by Erik Andersen
4 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02005 * Licensed under GPLv2, see file LICENSE in this source tree.
Eric Andersen01c3d402003-07-05 07:51:31 +00006 */
7
Pere Orga5bc8c002011-04-11 03:29:49 +02008//usage:#define fdformat_trivial_usage
9//usage: "[-n] DEVICE"
10//usage:#define fdformat_full_usage "\n\n"
11//usage: "Format floppy disk\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020012//usage: "\n -n Don't verify after format"
13
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000014#include "libbb.h"
Eric Andersen01c3d402003-07-05 07:51:31 +000015
16
17/* Stuff extracted from linux/fd.h */
18struct floppy_struct {
19 unsigned int size, /* nr of sectors total */
20 sect, /* sectors per track */
21 head, /* nr of heads */
22 track, /* nr of tracks */
23 stretch; /* !=0 means double track steps */
24#define FD_STRETCH 1
25#define FD_SWAPSIDES 2
26
27 unsigned char gap, /* gap1 size */
28
29 rate, /* data rate. |= 0x40 for perpendicular */
30#define FD_2M 0x4
31#define FD_SIZECODEMASK 0x38
32#define FD_SIZECODE(floppy) (((((floppy)->rate&FD_SIZECODEMASK)>> 3)+ 2) %8)
33#define FD_SECTSIZE(floppy) ( (floppy)->rate & FD_2M ? \
34 512 : 128 << FD_SIZECODE(floppy) )
35#define FD_PERP 0x40
36
37 spec1, /* stepping rate, head unload time */
38 fmt_gap; /* gap2 size */
39 const char * name; /* used only for predefined formats */
40};
41struct format_descr {
42 unsigned int device,head,track;
43};
44#define FDFMTBEG _IO(2,0x47)
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020045#define FDFMTTRK _IOW(2,0x48, struct format_descr)
Eric Andersen01c3d402003-07-05 07:51:31 +000046#define FDFMTEND _IO(2,0x49)
47#define FDGETPRM _IOR(2, 0x04, struct floppy_struct)
48#define FD_FILL_BYTE 0xF6 /* format fill byte. */
49
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000050int fdformat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000051int fdformat_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen01c3d402003-07-05 07:51:31 +000052{
Glenn L McGrath348672d2004-01-20 12:57:18 +000053 int fd, n, cyl, read_bytes, verify;
54 unsigned char *data;
55 struct stat st;
56 struct floppy_struct param;
57 struct format_descr descr;
Eric Andersen01c3d402003-07-05 07:51:31 +000058
Denis Vlasenko1d426652008-03-17 09:09:09 +000059 opt_complementary = "=1"; /* must have 1 param */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000060 verify = !getopt32(argv, "n");
Glenn L McGrath348672d2004-01-20 12:57:18 +000061 argv += optind;
Eric Andersen01c3d402003-07-05 07:51:31 +000062
Denis Vlasenkob6332242006-10-03 19:57:50 +000063 xstat(*argv, &st);
Glenn L McGrath348672d2004-01-20 12:57:18 +000064 if (!S_ISBLK(st.st_mode)) {
Denis Vlasenko92758142006-10-03 19:56:34 +000065 bb_error_msg_and_die("%s: not a block device", *argv);
Glenn L McGrath348672d2004-01-20 12:57:18 +000066 /* do not test major - perhaps this was an USB floppy */
67 }
Eric Andersen01c3d402003-07-05 07:51:31 +000068
Glenn L McGrath348672d2004-01-20 12:57:18 +000069 /* O_RDWR for formatting and verifying */
Denis Vlasenko92758142006-10-03 19:56:34 +000070 fd = xopen(*argv, O_RDWR);
Glenn L McGrath348672d2004-01-20 12:57:18 +000071
Denis Vlasenkob6332242006-10-03 19:57:50 +000072 /* original message was: "Could not determine current format type" */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000073 xioctl(fd, FDGETPRM, &param);
Glenn L McGrath348672d2004-01-20 12:57:18 +000074
Denys Vlasenko327f5502013-11-29 16:45:45 +010075 printf("%s-sided, %u tracks, %u sec/track. Total capacity %d kB\n",
Glenn L McGrath348672d2004-01-20 12:57:18 +000076 (param.head == 2) ? "Double" : "Single",
77 param.track, param.sect, param.size >> 1);
78
79 /* FORMAT */
Denis Vlasenko92758142006-10-03 19:56:34 +000080 printf("Formatting... ");
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000081 xioctl(fd, FDFMTBEG, NULL);
Glenn L McGrath348672d2004-01-20 12:57:18 +000082
83 /* n == track */
Denis Vlasenko92758142006-10-03 19:56:34 +000084 for (n = 0; n < param.track; n++) {
85 descr.head = 0;
86 descr.track = n;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000087 xioctl(fd, FDFMTTRK, &descr);
Denis Vlasenko92758142006-10-03 19:56:34 +000088 printf("%3d\b\b\b", n);
89 if (param.head == 2) {
90 descr.head = 1;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000091 xioctl(fd, FDFMTTRK, &descr);
Denis Vlasenko92758142006-10-03 19:56:34 +000092 }
Glenn L McGrath348672d2004-01-20 12:57:18 +000093 }
Eric Andersen522a2f32004-04-03 12:36:03 +000094
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000095 xioctl(fd, FDFMTEND, NULL);
Rob Landley59fe8b92006-08-04 22:02:55 +000096 printf("done\n");
Glenn L McGrath348672d2004-01-20 12:57:18 +000097
98 /* VERIFY */
Denis Vlasenko92758142006-10-03 19:56:34 +000099 if (verify) {
Glenn L McGrath348672d2004-01-20 12:57:18 +0000100 /* n == cyl_size */
101 n = param.sect*param.head*512;
102
103 data = xmalloc(n);
Denis Vlasenko92758142006-10-03 19:56:34 +0000104 printf("Verifying... ");
Glenn L McGrath348672d2004-01-20 12:57:18 +0000105 for (cyl = 0; cyl < param.track; cyl++) {
Rob Landley59fe8b92006-08-04 22:02:55 +0000106 printf("%3d\b\b\b", cyl);
Denis Vlasenko92758142006-10-03 19:56:34 +0000107 read_bytes = safe_read(fd, data, n);
108 if (read_bytes != n) {
109 if (read_bytes < 0) {
Bernhard Reutner-Fischer1b9d7c92006-06-03 22:45:37 +0000110 bb_perror_msg(bb_msg_read_error);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000111 }
Denis Vlasenkob6332242006-10-03 19:57:50 +0000112 bb_error_msg_and_die("problem reading cylinder %d, "
113 "expected %d, read %d", cyl, n, read_bytes);
Denis Vlasenko92758142006-10-03 19:56:34 +0000114 // FIXME: maybe better seek & continue??
Glenn L McGrath348672d2004-01-20 12:57:18 +0000115 }
116 /* Check backwards so we don't need a counter */
Denis Vlasenko92758142006-10-03 19:56:34 +0000117 while (--read_bytes >= 0) {
118 if (data[read_bytes] != FD_FILL_BYTE) {
Denys Vlasenko69675782013-01-14 01:34:48 +0100119 printf("bad data in cyl %d\nContinuing... ", cyl);
Glenn L McGrath348672d2004-01-20 12:57:18 +0000120 }
121 }
122 }
123 /* There is no point in freeing blocks at the end of a program, because
124 all of the program's space is given back to the system when the process
125 terminates.*/
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000126
Rob Landley658d2cf2005-09-08 03:11:58 +0000127 if (ENABLE_FEATURE_CLEAN_UP) free(data);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000128
Rob Landley59fe8b92006-08-04 22:02:55 +0000129 printf("done\n");
Glenn L McGrath348672d2004-01-20 12:57:18 +0000130 }
Rob Landley658d2cf2005-09-08 03:11:58 +0000131
132 if (ENABLE_FEATURE_CLEAN_UP) close(fd);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000133
Glenn L McGrath348672d2004-01-20 12:57:18 +0000134 /* Don't bother closing. Exit does
135 * that, so we can save a few bytes */
136 return EXIT_SUCCESS;
Eric Andersen01c3d402003-07-05 07:51:31 +0000137}