blob: f0466fdbdcdc4f4acbb33aeaae654a82982d664e [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 Vlasenkob097a842018-12-28 03:20:17 +01009//config: bool "fdformat (4.4 kb)"
Denys Vlasenkodd898c92016-11-23 11:46:32 +010010//config: default y
Denys Vlasenkodd898c92016-11-23 11:46:32 +010011//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020012//config: fdformat is used to low-level format a floppy disk.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010013
14//applet:IF_FDFORMAT(APPLET(fdformat, BB_DIR_USR_SBIN, BB_SUID_DROP))
15
16//kbuild:lib-$(CONFIG_FDFORMAT) += fdformat.o
Eric Andersen01c3d402003-07-05 07:51:31 +000017
Pere Orga5bc8c002011-04-11 03:29:49 +020018//usage:#define fdformat_trivial_usage
19//usage: "[-n] DEVICE"
20//usage:#define fdformat_full_usage "\n\n"
21//usage: "Format floppy disk\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020022//usage: "\n -n Don't verify after format"
23
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000024#include "libbb.h"
Eric Andersen01c3d402003-07-05 07:51:31 +000025
26
27/* Stuff extracted from linux/fd.h */
28struct floppy_struct {
29 unsigned int size, /* nr of sectors total */
30 sect, /* sectors per track */
31 head, /* nr of heads */
32 track, /* nr of tracks */
33 stretch; /* !=0 means double track steps */
34#define FD_STRETCH 1
35#define FD_SWAPSIDES 2
36
37 unsigned char gap, /* gap1 size */
38
39 rate, /* data rate. |= 0x40 for perpendicular */
40#define FD_2M 0x4
41#define FD_SIZECODEMASK 0x38
42#define FD_SIZECODE(floppy) (((((floppy)->rate&FD_SIZECODEMASK)>> 3)+ 2) %8)
43#define FD_SECTSIZE(floppy) ( (floppy)->rate & FD_2M ? \
44 512 : 128 << FD_SIZECODE(floppy) )
45#define FD_PERP 0x40
46
47 spec1, /* stepping rate, head unload time */
48 fmt_gap; /* gap2 size */
49 const char * name; /* used only for predefined formats */
50};
51struct format_descr {
52 unsigned int device,head,track;
53};
54#define FDFMTBEG _IO(2,0x47)
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020055#define FDFMTTRK _IOW(2,0x48, struct format_descr)
Eric Andersen01c3d402003-07-05 07:51:31 +000056#define FDFMTEND _IO(2,0x49)
57#define FDGETPRM _IOR(2, 0x04, struct floppy_struct)
58#define FD_FILL_BYTE 0xF6 /* format fill byte. */
59
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000060int fdformat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000061int fdformat_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen01c3d402003-07-05 07:51:31 +000062{
Glenn L McGrath348672d2004-01-20 12:57:18 +000063 int fd, n, cyl, read_bytes, verify;
64 unsigned char *data;
65 struct stat st;
66 struct floppy_struct param;
67 struct format_descr descr;
Eric Andersen01c3d402003-07-05 07:51:31 +000068
Denys Vlasenko22542ec2017-08-08 21:55:02 +020069 verify = !getopt32(argv, "^" "n" "\0" "=1");
Glenn L McGrath348672d2004-01-20 12:57:18 +000070 argv += optind;
Eric Andersen01c3d402003-07-05 07:51:31 +000071
Denis Vlasenkob6332242006-10-03 19:57:50 +000072 xstat(*argv, &st);
Glenn L McGrath348672d2004-01-20 12:57:18 +000073 if (!S_ISBLK(st.st_mode)) {
Denis Vlasenko92758142006-10-03 19:56:34 +000074 bb_error_msg_and_die("%s: not a block device", *argv);
Glenn L McGrath348672d2004-01-20 12:57:18 +000075 /* do not test major - perhaps this was an USB floppy */
76 }
Eric Andersen01c3d402003-07-05 07:51:31 +000077
Glenn L McGrath348672d2004-01-20 12:57:18 +000078 /* O_RDWR for formatting and verifying */
Denis Vlasenko92758142006-10-03 19:56:34 +000079 fd = xopen(*argv, O_RDWR);
Glenn L McGrath348672d2004-01-20 12:57:18 +000080
Denis Vlasenkob6332242006-10-03 19:57:50 +000081 /* original message was: "Could not determine current format type" */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000082 xioctl(fd, FDGETPRM, &param);
Glenn L McGrath348672d2004-01-20 12:57:18 +000083
Denys Vlasenko327f5502013-11-29 16:45:45 +010084 printf("%s-sided, %u tracks, %u sec/track. Total capacity %d kB\n",
Glenn L McGrath348672d2004-01-20 12:57:18 +000085 (param.head == 2) ? "Double" : "Single",
86 param.track, param.sect, param.size >> 1);
87
88 /* FORMAT */
Denis Vlasenko92758142006-10-03 19:56:34 +000089 printf("Formatting... ");
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000090 xioctl(fd, FDFMTBEG, NULL);
Glenn L McGrath348672d2004-01-20 12:57:18 +000091
92 /* n == track */
Denis Vlasenko92758142006-10-03 19:56:34 +000093 for (n = 0; n < param.track; n++) {
94 descr.head = 0;
95 descr.track = n;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000096 xioctl(fd, FDFMTTRK, &descr);
Denis Vlasenko92758142006-10-03 19:56:34 +000097 printf("%3d\b\b\b", n);
98 if (param.head == 2) {
99 descr.head = 1;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000100 xioctl(fd, FDFMTTRK, &descr);
Denis Vlasenko92758142006-10-03 19:56:34 +0000101 }
Glenn L McGrath348672d2004-01-20 12:57:18 +0000102 }
Eric Andersen522a2f32004-04-03 12:36:03 +0000103
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000104 xioctl(fd, FDFMTEND, NULL);
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200105 puts("Done");
Glenn L McGrath348672d2004-01-20 12:57:18 +0000106
107 /* VERIFY */
Denis Vlasenko92758142006-10-03 19:56:34 +0000108 if (verify) {
Glenn L McGrath348672d2004-01-20 12:57:18 +0000109 /* n == cyl_size */
110 n = param.sect*param.head*512;
111
112 data = xmalloc(n);
Denis Vlasenko92758142006-10-03 19:56:34 +0000113 printf("Verifying... ");
Glenn L McGrath348672d2004-01-20 12:57:18 +0000114 for (cyl = 0; cyl < param.track; cyl++) {
Rob Landley59fe8b92006-08-04 22:02:55 +0000115 printf("%3d\b\b\b", cyl);
Denis Vlasenko92758142006-10-03 19:56:34 +0000116 read_bytes = safe_read(fd, data, n);
117 if (read_bytes != n) {
118 if (read_bytes < 0) {
James Byrne69374872019-07-02 11:35:03 +0200119 bb_simple_perror_msg(bb_msg_read_error);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000120 }
Denis Vlasenkob6332242006-10-03 19:57:50 +0000121 bb_error_msg_and_die("problem reading cylinder %d, "
122 "expected %d, read %d", cyl, n, read_bytes);
Denis Vlasenko92758142006-10-03 19:56:34 +0000123 // FIXME: maybe better seek & continue??
Glenn L McGrath348672d2004-01-20 12:57:18 +0000124 }
125 /* Check backwards so we don't need a counter */
Denis Vlasenko92758142006-10-03 19:56:34 +0000126 while (--read_bytes >= 0) {
127 if (data[read_bytes] != FD_FILL_BYTE) {
Denys Vlasenko69675782013-01-14 01:34:48 +0100128 printf("bad data in cyl %d\nContinuing... ", cyl);
Glenn L McGrath348672d2004-01-20 12:57:18 +0000129 }
130 }
131 }
132 /* There is no point in freeing blocks at the end of a program, because
133 all of the program's space is given back to the system when the process
134 terminates.*/
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000135
Rob Landley658d2cf2005-09-08 03:11:58 +0000136 if (ENABLE_FEATURE_CLEAN_UP) free(data);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000137
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200138 puts("Done");
Glenn L McGrath348672d2004-01-20 12:57:18 +0000139 }
Rob Landley658d2cf2005-09-08 03:11:58 +0000140
141 if (ENABLE_FEATURE_CLEAN_UP) close(fd);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000142
Glenn L McGrath348672d2004-01-20 12:57:18 +0000143 /* Don't bother closing. Exit does
144 * that, so we can save a few bytes */
145 return EXIT_SUCCESS;
Eric Andersen01c3d402003-07-05 07:51:31 +0000146}