blob: 5772e13090fcd1a5aec1fa25180de86a7012caef [file] [log] [blame]
Eric Andersen01c3d402003-07-05 07:51:31 +00001/* 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
Glenn L McGrath348672d2004-01-20 12:57:18 +000055static void print_and_flush(const char * __restrict format, ...)
Eric Andersen01c3d402003-07-05 07:51:31 +000056{
Glenn L McGrath348672d2004-01-20 12:57:18 +000057 va_list arg;
Eric Andersen01c3d402003-07-05 07:51:31 +000058
Glenn L McGrath348672d2004-01-20 12:57:18 +000059 va_start(arg, format);
60 bb_vfprintf(stdout, format, arg);
61 va_end(arg);
62 bb_xfflush_stdout();
Eric Andersen01c3d402003-07-05 07:51:31 +000063}
64
Glenn L McGrath348672d2004-01-20 12:57:18 +000065static void bb_xioctl(int fd, int request, void *argp, const char *string)
Eric Andersen01c3d402003-07-05 07:51:31 +000066{
Glenn L McGrath348672d2004-01-20 12:57:18 +000067 if (ioctl (fd, request, argp) < 0) {
68 bb_perror_msg_and_die(string);
Eric Andersen01c3d402003-07-05 07:51:31 +000069 }
Eric Andersen01c3d402003-07-05 07:51:31 +000070}
71
72int fdformat_main(int argc,char **argv)
73{
Glenn L McGrath348672d2004-01-20 12:57:18 +000074 int fd, n, cyl, read_bytes, verify;
75 unsigned char *data;
76 struct stat st;
77 struct floppy_struct param;
78 struct format_descr descr;
Eric Andersen01c3d402003-07-05 07:51:31 +000079
Glenn L McGrath348672d2004-01-20 12:57:18 +000080 if (argc < 2) {
81 bb_show_usage();
82 }
83 verify = !bb_getopt_ulflags(argc, argv, "n");
84 argv += optind;
Eric Andersen01c3d402003-07-05 07:51:31 +000085
Glenn L McGrath348672d2004-01-20 12:57:18 +000086 /* R_OK is needed for verifying */
87 if (stat(*argv,&st) < 0 || access(*argv,W_OK | R_OK ) < 0) {
Mike Frysinger948a09d2006-03-23 02:07:20 +000088 bb_perror_msg_and_die("%s",*argv);
Glenn L McGrath348672d2004-01-20 12:57:18 +000089 }
90 if (!S_ISBLK(st.st_mode)) {
91 bb_error_msg_and_die("%s: not a block device",*argv);
92 /* do not test major - perhaps this was an USB floppy */
93 }
Eric Andersen01c3d402003-07-05 07:51:31 +000094
Eric Andersen01c3d402003-07-05 07:51:31 +000095
Glenn L McGrath348672d2004-01-20 12:57:18 +000096 /* O_RDWR for formatting and verifying */
97 fd = bb_xopen(*argv,O_RDWR );
98
99 bb_xioctl(fd, FDGETPRM, &param, "FDGETPRM");/*original message was: "Could not determine current format type" */
100
101 print_and_flush("%s-sided, %d tracks, %d sec/track. Total capacity %d kB.\n",
102 (param.head == 2) ? "Double" : "Single",
103 param.track, param.sect, param.size >> 1);
104
105 /* FORMAT */
106 print_and_flush("Formatting ... ", NULL);
107 bb_xioctl(fd, FDFMTBEG,NULL,"FDFMTBEG");
108
109 /* n == track */
Eric Andersen522a2f32004-04-03 12:36:03 +0000110 for (n = 0; n < param.track; n++)
111 {
112 descr.head = 0;
113 descr.track = n;
114 bb_xioctl(fd, FDFMTTRK,&descr,"FDFMTTRK");
115 print_and_flush("%3d\b\b\b", n);
116 if (param.head == 2) {
117 descr.head = 1;
118 bb_xioctl(fd, FDFMTTRK,&descr,"FDFMTTRK");
119 }
Glenn L McGrath348672d2004-01-20 12:57:18 +0000120 }
Eric Andersen522a2f32004-04-03 12:36:03 +0000121
Glenn L McGrath348672d2004-01-20 12:57:18 +0000122 bb_xioctl(fd,FDFMTEND,NULL,"FDFMTEND");
123 print_and_flush("done\n", NULL);
124
125 /* VERIFY */
126 if(verify) {
127 /* n == cyl_size */
128 n = param.sect*param.head*512;
129
130 data = xmalloc(n);
131 print_and_flush("Verifying ... ", NULL);
132 for (cyl = 0; cyl < param.track; cyl++) {
133 print_and_flush("%3d\b\b\b", cyl);
134 if((read_bytes = safe_read(fd,data,n))!= n ) {
135 if(read_bytes < 0) {
136 bb_perror_msg("Read: ");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000137 }
Glenn L McGrath348672d2004-01-20 12:57:18 +0000138 bb_error_msg_and_die("Problem reading cylinder %d, expected %d, read %d", cyl, n, read_bytes);
139 }
140 /* Check backwards so we don't need a counter */
141 while(--read_bytes>=0) {
142 if( data[read_bytes] != FD_FILL_BYTE) {
143 print_and_flush("bad data in cyl %d\nContinuing ... ",cyl);
144 }
145 }
146 }
147 /* There is no point in freeing blocks at the end of a program, because
148 all of the program's space is given back to the system when the process
149 terminates.*/
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000150
Rob Landley658d2cf2005-09-08 03:11:58 +0000151 if (ENABLE_FEATURE_CLEAN_UP) free(data);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000152
Glenn L McGrath348672d2004-01-20 12:57:18 +0000153 print_and_flush("done\n", NULL);
154 }
Rob Landley658d2cf2005-09-08 03:11:58 +0000155
156 if (ENABLE_FEATURE_CLEAN_UP) close(fd);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000157
Glenn L McGrath348672d2004-01-20 12:57:18 +0000158 /* Don't bother closing. Exit does
159 * that, so we can save a few bytes */
160 return EXIT_SUCCESS;
Eric Andersen01c3d402003-07-05 07:51:31 +0000161}