-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdumpids.c
206 lines (167 loc) · 4.31 KB
/
dumpids.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* dumpids
*
* $Id: dumpids.c,v 1.3 2002/10/19 23:36:47 eric Exp $
*
* Copyright 2002 Eric Smith.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. Note that permission is
* not granted to redistribute this program under the terms of any
* other version of the General Public License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fd.h>
#include <linux/fdreg.h>
typedef int bool;
typedef unsigned char u8;
int double_step = 0;
int data_rate;
/* rate codes are unfortunately NOT defined in fdreg.h */
#define FD_RATE_250_KBPS 2
#define FD_RATE_300_KBPS 1
#define FD_RATE_500_KBPS 0
bool recalibrate (int dev)
{
struct floppy_raw_cmd cmd;
int i = 0;
cmd.data = NULL;
cmd.length = 0;
cmd.rate = data_rate;
cmd.flags = FD_RAW_INTR;
cmd.cmd [i++] = FD_RECALIBRATE;
cmd.cmd [i++] = 0;
cmd.cmd_count = i;
return (0 <= ioctl (dev, FDRAWCMD, & cmd));
// return (0 <= ioctl (dev, FDRAWCMD, & cmd));
}
bool seek (int dev, int cylinder)
{
struct floppy_raw_cmd cmd;
int i = 0;
cmd.data = NULL;
cmd.length = 0;
cmd.rate = data_rate;
cmd.flags = FD_RAW_INTR;
cmd.cmd[i++] = FD_SEEK;
cmd.cmd[i++] = 0;
cmd.cmd[i++] = cylinder << double_step;
cmd.cmd_count = i;
return (0 <= ioctl (dev, FDRAWCMD, & cmd));
}
bool read_id (int dev, int fm, int seek_head, int *cylinder, int *head, int *sector, int *sector_size)
{
struct floppy_raw_cmd cmd;
u8 mask = 0x5f;
int i = 0;
if (fm)
mask &= ~0x40;
cmd.data = NULL; /* No data to transfer */
cmd.length = 0;
cmd.rate = data_rate;
cmd.flags = FD_RAW_INTR;
cmd.cmd[i++] = FD_READID & mask;
cmd.cmd[i++] = seek_head ? 4 : 0;
cmd.cmd_count = i;
if (0 > ioctl (dev, FDRAWCMD, & cmd))
return (0);
if (cmd.reply [0] & 0x40)
return (0);
*cylinder = cmd.reply [3];
*head = cmd.reply [4];
*sector = cmd.reply [5];
*sector_size = 128 << cmd.reply [6];
return (1);
}
int main (int argc, char *argv[])
{
int dev;
struct floppy_drive_params fdp;
int seek_cylinder, seek_head;
int cylinder, head, sector, sector_size;
int fm, rate;
int reset_now;
if (argc != 6)
{
fprintf (stderr, "usage:\n"
"%s device cylinder head fm rate\n", argv [0]);
exit (1);
}
dev = open (argv [1], O_RDONLY | O_NDELAY, 0);
if (0 > dev)
{
fprintf (stderr, "error opening drive\n");
exit (2);
}
seek_cylinder = atoi (argv [2]);
seek_head = atoi (argv [3]);
fm = atoi (argv [4]);
rate = atoi (argv [5]);
if (0 > ioctl (dev, FDRESET, & reset_now))
{
fprintf (stderr, "can't reset drive\n");
exit (2);
}
if (0 > ioctl (dev, FDGETDRVPRM, & fdp))
{
fprintf (stderr, "can't get drive parameters\n");
exit (2);
}
printf ("cmos: %d\n", fdp.cmos);
printf ("tracks: %d\n", fdp.tracks);
printf ("rpm: %d\n", fdp.rps * 60);
switch (rate)
{
case 250:
data_rate = FD_RATE_250_KBPS;
break;
case 300:
data_rate = FD_RATE_300_KBPS;
break;
case 500:
data_rate = FD_RATE_500_KBPS;
break;
default:
fprintf (stderr, "unrecognized data rate, only 250, 300 and 500 supported\n");
exit (2);
}
if (! recalibrate (dev))
{
fprintf (stderr, "error recalibrating drive\n");
exit (2);
}
if (! seek (dev, seek_cylinder))
{
fprintf (stderr, "error seeking\n");
exit (2);
}
while (1)
{
if (read_id (dev, fm, seek_head, & cylinder, & head, & sector, & sector_size))
{
printf ("cyl %02d, head %d, sector %02d, size %d\n",
cylinder, head, sector, sector_size);
}
else
{
fprintf (stderr, "error reading ID\n");
exit (2);
}
}
close (dev);
exit (0);
}