blob: 3831ab44e1474e89154275d8dd25e79c9f7d8dc0 [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 *
5 * Licensed under GPLv2, see file LICENSE in this tarball for details.
Eric Andersen01c3d402003-07-05 07:51:31 +00006 */
7
Denis Vlasenkob6adbf12007-05-26 19:00:18 +00008#include "libbb.h"
Eric Andersen01c3d402003-07-05 07:51:31 +00009
10
11/* Stuff extracted from linux/fd.h */
12struct 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};
35struct 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
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000044int fdformat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000045int fdformat_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen01c3d402003-07-05 07:51:31 +000046{
Glenn L McGrath348672d2004-01-20 12:57:18 +000047 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;
Eric Andersen01c3d402003-07-05 07:51:31 +000052
Denis Vlasenko1d426652008-03-17 09:09:09 +000053 opt_complementary = "=1"; /* must have 1 param */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000054 verify = !getopt32(argv, "n");
Glenn L McGrath348672d2004-01-20 12:57:18 +000055 argv += optind;
Eric Andersen01c3d402003-07-05 07:51:31 +000056
Denis Vlasenkob6332242006-10-03 19:57:50 +000057 xstat(*argv, &st);
Glenn L McGrath348672d2004-01-20 12:57:18 +000058 if (!S_ISBLK(st.st_mode)) {
Denis Vlasenko92758142006-10-03 19:56:34 +000059 bb_error_msg_and_die("%s: not a block device", *argv);
Glenn L McGrath348672d2004-01-20 12:57:18 +000060 /* do not test major - perhaps this was an USB floppy */
61 }
Eric Andersen01c3d402003-07-05 07:51:31 +000062
Glenn L McGrath348672d2004-01-20 12:57:18 +000063 /* O_RDWR for formatting and verifying */
Denis Vlasenko92758142006-10-03 19:56:34 +000064 fd = xopen(*argv, O_RDWR);
Glenn L McGrath348672d2004-01-20 12:57:18 +000065
Denis Vlasenkob6332242006-10-03 19:57:50 +000066 /* original message was: "Could not determine current format type" */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000067 xioctl(fd, FDGETPRM, &param);
Glenn L McGrath348672d2004-01-20 12:57:18 +000068
Denis Vlasenkob6332242006-10-03 19:57:50 +000069 printf("%s-sided, %d tracks, %d sec/track. Total capacity %d kB\n",
Glenn L McGrath348672d2004-01-20 12:57:18 +000070 (param.head == 2) ? "Double" : "Single",
71 param.track, param.sect, param.size >> 1);
72
73 /* FORMAT */
Denis Vlasenko92758142006-10-03 19:56:34 +000074 printf("Formatting... ");
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000075 xioctl(fd, FDFMTBEG, NULL);
Glenn L McGrath348672d2004-01-20 12:57:18 +000076
77 /* n == track */
Denis Vlasenko92758142006-10-03 19:56:34 +000078 for (n = 0; n < param.track; n++) {
79 descr.head = 0;
80 descr.track = n;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000081 xioctl(fd, FDFMTTRK, &descr);
Denis Vlasenko92758142006-10-03 19:56:34 +000082 printf("%3d\b\b\b", n);
83 if (param.head == 2) {
84 descr.head = 1;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000085 xioctl(fd, FDFMTTRK, &descr);
Denis Vlasenko92758142006-10-03 19:56:34 +000086 }
Glenn L McGrath348672d2004-01-20 12:57:18 +000087 }
Eric Andersen522a2f32004-04-03 12:36:03 +000088
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000089 xioctl(fd, FDFMTEND, NULL);
Rob Landley59fe8b92006-08-04 22:02:55 +000090 printf("done\n");
Glenn L McGrath348672d2004-01-20 12:57:18 +000091
92 /* VERIFY */
Denis Vlasenko92758142006-10-03 19:56:34 +000093 if (verify) {
Glenn L McGrath348672d2004-01-20 12:57:18 +000094 /* n == cyl_size */
95 n = param.sect*param.head*512;
96
97 data = xmalloc(n);
Denis Vlasenko92758142006-10-03 19:56:34 +000098 printf("Verifying... ");
Glenn L McGrath348672d2004-01-20 12:57:18 +000099 for (cyl = 0; cyl < param.track; cyl++) {
Rob Landley59fe8b92006-08-04 22:02:55 +0000100 printf("%3d\b\b\b", cyl);
Denis Vlasenko92758142006-10-03 19:56:34 +0000101 read_bytes = safe_read(fd, data, n);
102 if (read_bytes != n) {
103 if (read_bytes < 0) {
Bernhard Reutner-Fischer1b9d7c92006-06-03 22:45:37 +0000104 bb_perror_msg(bb_msg_read_error);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000105 }
Denis Vlasenkob6332242006-10-03 19:57:50 +0000106 bb_error_msg_and_die("problem reading cylinder %d, "
107 "expected %d, read %d", cyl, n, read_bytes);
Denis Vlasenko92758142006-10-03 19:56:34 +0000108 // FIXME: maybe better seek & continue??
Glenn L McGrath348672d2004-01-20 12:57:18 +0000109 }
110 /* Check backwards so we don't need a counter */
Denis Vlasenko92758142006-10-03 19:56:34 +0000111 while (--read_bytes >= 0) {
112 if (data[read_bytes] != FD_FILL_BYTE) {
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000113 printf("bad data in cyl %d\nContinuing... ", cyl);
Glenn L McGrath348672d2004-01-20 12:57:18 +0000114 }
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.*/
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000120
Rob Landley658d2cf2005-09-08 03:11:58 +0000121 if (ENABLE_FEATURE_CLEAN_UP) free(data);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000122
Rob Landley59fe8b92006-08-04 22:02:55 +0000123 printf("done\n");
Glenn L McGrath348672d2004-01-20 12:57:18 +0000124 }
Rob Landley658d2cf2005-09-08 03:11:58 +0000125
126 if (ENABLE_FEATURE_CLEAN_UP) close(fd);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000127
Glenn L McGrath348672d2004-01-20 12:57:18 +0000128 /* Don't bother closing. Exit does
129 * that, so we can save a few bytes */
130 return EXIT_SUCCESS;
Eric Andersen01c3d402003-07-05 07:51:31 +0000131}