blob: c72da8b895b119b071b0b80c0b4a11b0cb619e16 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Denys Vlasenko2ab94032017-10-05 15:33:28 +02002/*
3 * fdformat.c - Low-level formats a floppy disk - Werner Almesberger
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00004 * 5 July 2003 -- modified for Busybox by Erik Andersen
5 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02006 * Licensed under GPLv2, see file LICENSE in this source tree.
Eric Andersen01c3d402003-07-05 07:51:31 +00007 */
Denys Vlasenkodd898c92016-11-23 11:46:32 +01008//config:config FDFORMAT
Denys Vlasenko4eed2c62017-07-18 22:01:24 +02009//config: bool "fdformat (4.5 kb)"
Denys Vlasenkodd898c92016-11-23 11:46:32 +010010//config: default y
11//config: select PLATFORM_LINUX
12//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: fdformat is used to low-level format a floppy disk.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010014
15//applet:IF_FDFORMAT(APPLET(fdformat, BB_DIR_USR_SBIN, BB_SUID_DROP))
16
17//kbuild:lib-$(CONFIG_FDFORMAT) += fdformat.o
Eric Andersen01c3d402003-07-05 07:51:31 +000018
Pere Orga5bc8c002011-04-11 03:29:49 +020019//usage:#define fdformat_trivial_usage
20//usage: "[-n] DEVICE"
21//usage:#define fdformat_full_usage "\n\n"
22//usage: "Format floppy disk\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020023//usage: "\n -n Don't verify after format"
24
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000025#include "libbb.h"
Eric Andersen01c3d402003-07-05 07:51:31 +000026
27
28/* Stuff extracted from linux/fd.h */
29struct floppy_struct {
30 unsigned int size, /* nr of sectors total */
31 sect, /* sectors per track */
32 head, /* nr of heads */
33 track, /* nr of tracks */
34 stretch; /* !=0 means double track steps */
35#define FD_STRETCH 1
36#define FD_SWAPSIDES 2
37
38 unsigned char gap, /* gap1 size */
39
40 rate, /* data rate. |= 0x40 for perpendicular */
41#define FD_2M 0x4
42#define FD_SIZECODEMASK 0x38
43#define FD_SIZECODE(floppy) (((((floppy)->rate&FD_SIZECODEMASK)>> 3)+ 2) %8)
44#define FD_SECTSIZE(floppy) ( (floppy)->rate & FD_2M ? \
45 512 : 128 << FD_SIZECODE(floppy) )
46#define FD_PERP 0x40
47
48 spec1, /* stepping rate, head unload time */
49 fmt_gap; /* gap2 size */
50 const char * name; /* used only for predefined formats */
51};
52struct format_descr {
53 unsigned int device,head,track;
54};
55#define FDFMTBEG _IO(2,0x47)
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020056#define FDFMTTRK _IOW(2,0x48, struct format_descr)
Eric Andersen01c3d402003-07-05 07:51:31 +000057#define FDFMTEND _IO(2,0x49)
58#define FDGETPRM _IOR(2, 0x04, struct floppy_struct)
59#define FD_FILL_BYTE 0xF6 /* format fill byte. */
60
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000061int fdformat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000062int fdformat_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen01c3d402003-07-05 07:51:31 +000063{
Glenn L McGrath348672d2004-01-20 12:57:18 +000064 int fd, n, cyl, read_bytes, verify;
65 unsigned char *data;
66 struct stat st;
67 struct floppy_struct param;
68 struct format_descr descr;
Eric Andersen01c3d402003-07-05 07:51:31 +000069
Denys Vlasenko22542ec2017-08-08 21:55:02 +020070 verify = !getopt32(argv, "^" "n" "\0" "=1");
Glenn L McGrath348672d2004-01-20 12:57:18 +000071 argv += optind;
Eric Andersen01c3d402003-07-05 07:51:31 +000072
Denis Vlasenkob6332242006-10-03 19:57:50 +000073 xstat(*argv, &st);
Glenn L McGrath348672d2004-01-20 12:57:18 +000074 if (!S_ISBLK(st.st_mode)) {
Denis Vlasenko92758142006-10-03 19:56:34 +000075 bb_error_msg_and_die("%s: not a block device", *argv);
Glenn L McGrath348672d2004-01-20 12:57:18 +000076 /* do not test major - perhaps this was an USB floppy */
77 }
Eric Andersen01c3d402003-07-05 07:51:31 +000078
Glenn L McGrath348672d2004-01-20 12:57:18 +000079 /* O_RDWR for formatting and verifying */
Denis Vlasenko92758142006-10-03 19:56:34 +000080 fd = xopen(*argv, O_RDWR);
Glenn L McGrath348672d2004-01-20 12:57:18 +000081
Denis Vlasenkob6332242006-10-03 19:57:50 +000082 /* original message was: "Could not determine current format type" */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000083 xioctl(fd, FDGETPRM, &param);
Glenn L McGrath348672d2004-01-20 12:57:18 +000084
Denys Vlasenko327f5502013-11-29 16:45:45 +010085 printf("%s-sided, %u tracks, %u sec/track. Total capacity %d kB\n",
Glenn L McGrath348672d2004-01-20 12:57:18 +000086 (param.head == 2) ? "Double" : "Single",
87 param.track, param.sect, param.size >> 1);
88
89 /* FORMAT */
Denis Vlasenko92758142006-10-03 19:56:34 +000090 printf("Formatting... ");
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000091 xioctl(fd, FDFMTBEG, NULL);
Glenn L McGrath348672d2004-01-20 12:57:18 +000092
93 /* n == track */
Denis Vlasenko92758142006-10-03 19:56:34 +000094 for (n = 0; n < param.track; n++) {
95 descr.head = 0;
96 descr.track = n;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000097 xioctl(fd, FDFMTTRK, &descr);
Denis Vlasenko92758142006-10-03 19:56:34 +000098 printf("%3d\b\b\b", n);
99 if (param.head == 2) {
100 descr.head = 1;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000101 xioctl(fd, FDFMTTRK, &descr);
Denis Vlasenko92758142006-10-03 19:56:34 +0000102 }
Glenn L McGrath348672d2004-01-20 12:57:18 +0000103 }
Eric Andersen522a2f32004-04-03 12:36:03 +0000104
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000105 xioctl(fd, FDFMTEND, NULL);
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200106 puts("Done");
Glenn L McGrath348672d2004-01-20 12:57:18 +0000107
108 /* VERIFY */
Denis Vlasenko92758142006-10-03 19:56:34 +0000109 if (verify) {
Glenn L McGrath348672d2004-01-20 12:57:18 +0000110 /* n == cyl_size */
111 n = param.sect*param.head*512;
112
113 data = xmalloc(n);
Denis Vlasenko92758142006-10-03 19:56:34 +0000114 printf("Verifying... ");
Glenn L McGrath348672d2004-01-20 12:57:18 +0000115 for (cyl = 0; cyl < param.track; cyl++) {
Rob Landley59fe8b92006-08-04 22:02:55 +0000116 printf("%3d\b\b\b", cyl);
Denis Vlasenko92758142006-10-03 19:56:34 +0000117 read_bytes = safe_read(fd, data, n);
118 if (read_bytes != n) {
119 if (read_bytes < 0) {
Bernhard Reutner-Fischer1b9d7c92006-06-03 22:45:37 +0000120 bb_perror_msg(bb_msg_read_error);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000121 }
Denis Vlasenkob6332242006-10-03 19:57:50 +0000122 bb_error_msg_and_die("problem reading cylinder %d, "
123 "expected %d, read %d", cyl, n, read_bytes);
Denis Vlasenko92758142006-10-03 19:56:34 +0000124 // FIXME: maybe better seek & continue??
Glenn L McGrath348672d2004-01-20 12:57:18 +0000125 }
126 /* Check backwards so we don't need a counter */
Denis Vlasenko92758142006-10-03 19:56:34 +0000127 while (--read_bytes >= 0) {
128 if (data[read_bytes] != FD_FILL_BYTE) {
Denys Vlasenko69675782013-01-14 01:34:48 +0100129 printf("bad data in cyl %d\nContinuing... ", cyl);
Glenn L McGrath348672d2004-01-20 12:57:18 +0000130 }
131 }
132 }
133 /* There is no point in freeing blocks at the end of a program, because
134 all of the program's space is given back to the system when the process
135 terminates.*/
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000136
Rob Landley658d2cf2005-09-08 03:11:58 +0000137 if (ENABLE_FEATURE_CLEAN_UP) free(data);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000138
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200139 puts("Done");
Glenn L McGrath348672d2004-01-20 12:57:18 +0000140 }
Rob Landley658d2cf2005-09-08 03:11:58 +0000141
142 if (ENABLE_FEATURE_CLEAN_UP) close(fd);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000143
Glenn L McGrath348672d2004-01-20 12:57:18 +0000144 /* Don't bother closing. Exit does
145 * that, so we can save a few bytes */
146 return EXIT_SUCCESS;
Eric Andersen01c3d402003-07-05 07:51:31 +0000147}