aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2003-07-05 07:51:31 +0000
committerEric Andersen <andersen@codepoet.org>2003-07-05 07:51:31 +0000
commit01c3d40f3c022abc08c14b96fe586a33fce081e8 (patch)
tree44343baf5a55f3f623770a1097a54eaad112c7f4
parenta21f4e018a39c08e09d69fae63383000a327716a (diff)
downloadbusybox-w32-01c3d40f3c022abc08c14b96fe586a33fce081e8.tar.gz
busybox-w32-01c3d40f3c022abc08c14b96fe586a33fce081e8.tar.bz2
busybox-w32-01c3d40f3c022abc08c14b96fe586a33fce081e8.zip
Add the fdformat utility, based on a patch from Kent Robotti,
but I then completely reworked the fdformat utility to comply with the current busybox way of doing things.
-rw-r--r--include/applets.h3
-rw-r--r--include/usage.h7
-rw-r--r--util-linux/Config.in6
-rw-r--r--util-linux/Makefile.in1
-rw-r--r--util-linux/fdformat.c158
5 files changed, 175 insertions, 0 deletions
diff --git a/include/applets.h b/include/applets.h
index cfb278123..5fe01de5f 100644
--- a/include/applets.h
+++ b/include/applets.h
@@ -193,6 +193,9 @@
193#ifdef CONFIG_FDFLUSH 193#ifdef CONFIG_FDFLUSH
194 APPLET(fdflush, fdflush_main, _BB_DIR_BIN, _BB_SUID_NEVER) 194 APPLET(fdflush, fdflush_main, _BB_DIR_BIN, _BB_SUID_NEVER)
195#endif 195#endif
196#ifdef CONFIG_FDFORMAT
197 APPLET(fdformat, fdformat_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
198#endif
196#ifdef CONFIG_FDISK 199#ifdef CONFIG_FDISK
197 APPLET(fdisk, fdisk_main, _BB_DIR_SBIN, _BB_SUID_NEVER) 200 APPLET(fdisk, fdisk_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
198#endif 201#endif
diff --git a/include/usage.h b/include/usage.h
index a570d3519..14f2e2859 100644
--- a/include/usage.h
+++ b/include/usage.h
@@ -571,6 +571,13 @@
571#define fdflush_full_usage \ 571#define fdflush_full_usage \
572 "Forces floppy disk drive to detect disk change" 572 "Forces floppy disk drive to detect disk change"
573 573
574#define fdformat_trivial_usage \
575 "[-n] DEVICE"
576#define fdformat_full_usage \
577 "Low-level formats a floppy disk\n\n" \
578 "Options:\n" \
579 "\t-n\tDon't verify after format"
580
574#define fdisk_trivial_usage \ 581#define fdisk_trivial_usage \
575 "[-l] [-v] [-b SSZ] [-u] DISK" 582 "[-l] [-v] [-b SSZ] [-u] DISK"
576#define fdisk_full_usage \ 583#define fdisk_full_usage \
diff --git a/util-linux/Config.in b/util-linux/Config.in
index d2e827213..ae17fe31b 100644
--- a/util-linux/Config.in
+++ b/util-linux/Config.in
@@ -59,6 +59,12 @@ config CONFIG_FDFLUSH
59 you change a disk. Most people have working hardware and can safely 59 you change a disk. Most people have working hardware and can safely
60 say leave this disabled. 60 say leave this disabled.
61 61
62config CONFIG_FDFORMAT
63 bool "fdformat"
64 default n
65 help
66 Fdformat is used to low-level format a floppy disk.
67
62config CONFIG_FDISK 68config CONFIG_FDISK
63 bool "fdisk" 69 bool "fdisk"
64 default n 70 default n
diff --git a/util-linux/Makefile.in b/util-linux/Makefile.in
index 4e1871425..28757a336 100644
--- a/util-linux/Makefile.in
+++ b/util-linux/Makefile.in
@@ -26,6 +26,7 @@ UTILLINUX-:=
26UTILLINUX-$(CONFIG_DMESG) += dmesg.o 26UTILLINUX-$(CONFIG_DMESG) += dmesg.o
27UTILLINUX-$(CONFIG_FBSET) += fbset.o 27UTILLINUX-$(CONFIG_FBSET) += fbset.o
28UTILLINUX-$(CONFIG_FDFLUSH) += fdflush.o 28UTILLINUX-$(CONFIG_FDFLUSH) += fdflush.o
29UTILLINUX-$(CONFIG_FDFORMAT) += fdformat.o
29UTILLINUX-$(CONFIG_FDISK) += fdisk.o 30UTILLINUX-$(CONFIG_FDISK) += fdisk.o
30UTILLINUX-$(CONFIG_FREERAMDISK) += freeramdisk.o 31UTILLINUX-$(CONFIG_FREERAMDISK) += freeramdisk.o
31UTILLINUX-$(CONFIG_FSCK_MINIX) += fsck_minix.o 32UTILLINUX-$(CONFIG_FSCK_MINIX) += fsck_minix.o
diff --git a/util-linux/fdformat.c b/util-linux/fdformat.c
new file mode 100644
index 000000000..5b926467f
--- /dev/null
+++ b/util-linux/fdformat.c
@@ -0,0 +1,158 @@
1/* fdformat.c - Low-level formats a floppy disk - Werner Almesberger */
2
3/* 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
4 * - added Native Language Support
5 * 1999-03-20 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
6 * - more i18n/nls translatable strings marked
7 *
8 * 5 July 2003 -- modified for Busybox by Erik Andersen
9 */
10
11#include <stdio.h>
12#include <string.h>
13#include <fcntl.h>
14#include <errno.h>
15#include <unistd.h>
16#include <stdlib.h>
17#include <sys/stat.h>
18#include <sys/ioctl.h>
19#include "busybox.h"
20
21
22/* Stuff extracted from linux/fd.h */
23struct floppy_struct {
24 unsigned int size, /* nr of sectors total */
25 sect, /* sectors per track */
26 head, /* nr of heads */
27 track, /* nr of tracks */
28 stretch; /* !=0 means double track steps */
29#define FD_STRETCH 1
30#define FD_SWAPSIDES 2
31
32 unsigned char gap, /* gap1 size */
33
34 rate, /* data rate. |= 0x40 for perpendicular */
35#define FD_2M 0x4
36#define FD_SIZECODEMASK 0x38
37#define FD_SIZECODE(floppy) (((((floppy)->rate&FD_SIZECODEMASK)>> 3)+ 2) %8)
38#define FD_SECTSIZE(floppy) ( (floppy)->rate & FD_2M ? \
39 512 : 128 << FD_SIZECODE(floppy) )
40#define FD_PERP 0x40
41
42 spec1, /* stepping rate, head unload time */
43 fmt_gap; /* gap2 size */
44 const char * name; /* used only for predefined formats */
45};
46struct format_descr {
47 unsigned int device,head,track;
48};
49#define FDFMTBEG _IO(2,0x47)
50#define FDFMTTRK _IOW(2,0x48, struct format_descr)
51#define FDFMTEND _IO(2,0x49)
52#define FDGETPRM _IOR(2, 0x04, struct floppy_struct)
53#define FD_FILL_BYTE 0xF6 /* format fill byte. */
54
55
56
57static void format_disk(int ctrl, char *name, struct floppy_struct *param)
58{
59 struct format_descr descr;
60 int track;
61
62 printf("Formatting ... ");
63 fflush(stdout);
64 if (ioctl(ctrl,FDFMTBEG,NULL) < 0) {
65 bb_perror_msg_and_die("FDFMTBEG");
66 }
67 for (track = 0; track < param->track; track++)
68 {
69 descr.track = track;
70 descr.head = 0;
71 if (ioctl(ctrl,FDFMTTRK,(long) &descr) < 0) {
72 bb_perror_msg_and_die("FDFMTTRK");
73 }
74
75 printf("%3d\b\b\b",track);
76 fflush(stdout);
77 if (param->head == 2) {
78 descr.head = 1;
79 if (ioctl(ctrl,FDFMTTRK,(long) &descr) < 0) {
80 bb_perror_msg_and_die("FDFMTTRK");
81 }
82 }
83 }
84 if (ioctl(ctrl,FDFMTEND,NULL) < 0) {
85 bb_perror_msg_and_die("FDFMTEND");
86 }
87 printf("done\n");
88}
89
90static void verify_disk(char *name, struct floppy_struct *param)
91{
92 unsigned char *data;
93 int fd,cyl_size,cyl,count,read_bytes;
94
95 cyl_size = param->sect*param->head*512;
96 data = xmalloc(cyl_size);
97 printf("Verifying ... ");
98 fflush(stdout);
99 fd = bb_xopen(name,O_RDONLY);
100 for (cyl = 0; cyl < param->track; cyl++)
101 {
102 printf("%3d\b\b\b",cyl);
103 fflush(stdout);
104 read_bytes = safe_read(fd,data,cyl_size);
105 if(read_bytes != cyl_size) {
106 if(read_bytes < 0) {
107 bb_perror_msg("Read: ");
108 }
109 bb_error_msg_and_die("Problem reading cylinder %d, "
110 "expected %d, read %d", cyl, cyl_size, read_bytes);
111 }
112 for (count = 0; count < cyl_size; count++)
113 if (data[count] != FD_FILL_BYTE) {
114 printf("bad data in cyl %d\nContinuing ... ",cyl);
115 fflush(stdout);
116 break;
117 }
118 }
119 printf("done\n");
120 close(fd);
121}
122
123int fdformat_main(int argc,char **argv)
124{
125 int ctrl;
126 int verify;
127 struct stat st;
128 struct floppy_struct param;
129
130 if (argc < 2) {
131 bb_show_usage();
132 }
133 verify != bb_getopt_ulflags(argc, argv, "n");
134 argv += optind;
135
136 if (stat(*argv,&st) < 0 || access(*argv,W_OK) < 0) {
137 bb_perror_msg_and_die(*argv);
138 }
139 if (!S_ISBLK(st.st_mode)) {
140 bb_error_msg_and_die("%s: not a block device",*argv);
141 /* do not test major - perhaps this was an USB floppy */
142 }
143
144 ctrl = bb_xopen(*argv,O_WRONLY);
145 if (ioctl(ctrl,FDGETPRM,(long) &param) < 0) {
146 bb_perror_msg_and_die("Could not determine current format type");
147 }
148 printf("%s-sided, %d tracks, %d sec/track. Total capacity %d kB.\n",
149 (param.head == 2) ? "Double" : "Single",
150 param.track, param.sect,param.size >> 1);
151 format_disk(ctrl, *argv, &param);
152 close(ctrl);
153
154 if (verify) {
155 verify_disk(*argv, &param);
156 }
157 return EXIT_SUCCESS;
158}