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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
/**** vi:set ts=8 sts=8 sw=8:************************************************
*
* Copyright (C) 2002 Marcin Dalecki <martin@dalecki.de>
*
* 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.
*
* 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.
*/
/*
* Generic ioctl handling for all ATA/ATAPI device drivers.
*/
#include <linux/types.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/errno.h>
#include <linux/blkpg.h>
#include <linux/pci.h>
#include <linux/delay.h>
#include <linux/cdrom.h>
#include <linux/device.h>
#include <linux/hdreg.h>
#include <linux/ide.h>
#include <asm/uaccess.h>
#include "ioctl.h"
/* BIG GEOMETRY - dying, used only by HDIO_GETGEO_BIG_RAW */
struct hd_big_geometry {
u8 heads;
u8 sectors;
u32 cylinders;
unsigned long start;
};
/*
* Implement generic ioctls invoked from userspace to imlpement specific
* functionality.
*
* Unfortunately every single low level programm out there is using this
* interface.
*/
static int do_cmd_ioctl(struct ata_device *drive, unsigned long arg)
{
int err = 0;
u8 vals[4];
u8 *argbuf = vals;
int argsize = 4;
struct ata_taskfile args;
/* Second phase.
*/
if (copy_from_user(vals, (void *)arg, 4))
return -EFAULT;
memset(&args, 0, sizeof(args));
args.taskfile.feature = vals[2];
args.taskfile.sector_count = vals[1];
args.taskfile.sector_number = vals[3];
if (vals[0] == WIN_SMART) {
args.taskfile.low_cylinder = 0x4f;
args.taskfile.high_cylinder = 0xc2;
} else {
args.taskfile.low_cylinder = 0x00;
args.taskfile.high_cylinder = 0x00;
}
args.taskfile.device_head = 0x00;
args.cmd = vals[0];
if (vals[3]) {
argsize = 4 + (SECTOR_WORDS * 4 * vals[3]);
argbuf = kmalloc(argsize, GFP_KERNEL);
if (argbuf == NULL)
return -ENOMEM;
memcpy(argbuf, vals, 4);
memset(argbuf + 4, 0, argsize - 4);
}
/* Issue ATA command and wait for completion.
*/
err = ide_raw_taskfile(drive, &args, argbuf + 4);
argbuf[0] = drive->status;
argbuf[1] = args.taskfile.feature;
argbuf[2] = args.taskfile.sector_count;
if (copy_to_user((void *)arg, argbuf, argsize))
err = -EFAULT;
if (argsize > 4)
kfree(argbuf);
return err;
}
/*
* NOTE: Due to ridiculous coding habbits in the hdparm utility we have to
* always return unsigned long in case we are returning simple values.
*/
int ata_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
unsigned int major, minor;
struct ata_device *drive;
kdev_t dev;
dev = inode->i_rdev;
major = major(dev);
minor = minor(dev);
if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
return -ENODEV;
/* Contrary to popular beleve we disallow even the reading of the ioctl
* values for users which don't have permission too. We do this becouse
* such information could be used by an attacker to deply a simple-user
* attack, which triggers bugs present only on a particular
* configuration.
*/
switch (cmd) {
case HDIO_GET_32BIT: {
unsigned long val = drive->channel->io_32bit;
if (put_user(val, (unsigned long *) arg))
return -EFAULT;
return 0;
}
case HDIO_SET_32BIT:
if (!capable(CAP_SYS_ADMIN))
return -EACCES;
if (arg < 0 || arg > 1)
return -EINVAL;
if (drive->channel->no_io_32bit)
return -EIO;
if (ide_spin_wait_hwgroup(drive))
return -EBUSY;
drive->channel->io_32bit = arg;
spin_unlock_irq(drive->channel->lock);
return 0;
case HDIO_SET_PIO_MODE:
if (!capable(CAP_SYS_ADMIN))
return -EACCES;
if (arg < 0 || arg > 255)
return -EINVAL;
if (!drive->channel->tuneproc)
return -ENOSYS;
/* FIXME: we can see that tuneproc whould do the
* locking!.
*/
if (ide_spin_wait_hwgroup(drive))
return -EBUSY;
drive->channel->tuneproc(drive, (u8) arg);
spin_unlock_irq(drive->channel->lock);
return 0;
case HDIO_GET_UNMASKINTR: {
unsigned long val = drive->channel->unmask;
if (put_user(val, (unsigned long *) arg))
return -EFAULT;
return 0;
}
case HDIO_SET_UNMASKINTR:
if (!capable(CAP_SYS_ADMIN))
return -EACCES;
if (arg < 0 || arg > 1)
return -EINVAL;
if (drive->channel->no_unmask)
return -EIO;
if (ide_spin_wait_hwgroup(drive))
return -EBUSY;
drive->channel->unmask = arg;
spin_unlock_irq(drive->channel->lock);
return 0;
case HDIO_GET_DMA: {
unsigned long val = drive->using_dma;
if (put_user(val, (unsigned long *) arg))
return -EFAULT;
return 0;
}
case HDIO_SET_DMA:
if (!capable(CAP_SYS_ADMIN))
return -EACCES;
if (arg < 0 || arg > 1)
return -EINVAL;
if (!drive->driver)
return -EPERM;
if (!drive->id || !(drive->id->capability & 1) || !drive->channel->udma_setup)
return -EPERM;
if (ide_spin_wait_hwgroup(drive))
return -EBUSY;
udma_enable(drive, arg, 1);
spin_unlock_irq(drive->channel->lock);
return 0;
case HDIO_GETGEO: {
struct hd_geometry *loc = (struct hd_geometry *) arg;
unsigned short bios_cyl = drive->bios_cyl; /* truncate */
if (!loc || (drive->type != ATA_DISK && drive->type != ATA_FLOPPY))
return -EINVAL;
if (put_user(drive->bios_head, (u8 *) &loc->heads))
return -EFAULT;
if (put_user(drive->bios_sect, (u8 *) &loc->sectors))
return -EFAULT;
if (put_user(bios_cyl, (u16 *) &loc->cylinders))
return -EFAULT;
if (put_user((unsigned)drive->part[minor(inode->i_rdev)&PARTN_MASK].start_sect,
(unsigned long *) &loc->start))
return -EFAULT;
return 0;
}
case HDIO_GETGEO_BIG_RAW: {
struct hd_big_geometry *loc = (struct hd_big_geometry *) arg;
if (!loc || (drive->type != ATA_DISK && drive->type != ATA_FLOPPY))
return -EINVAL;
if (put_user(drive->head, (u8 *) &loc->heads))
return -EFAULT;
if (put_user(drive->sect, (u8 *) &loc->sectors))
return -EFAULT;
if (put_user(drive->cyl, (unsigned int *) &loc->cylinders))
return -EFAULT;
if (put_user((unsigned)drive->part[minor(inode->i_rdev)&PARTN_MASK].start_sect,
(unsigned long *) &loc->start))
return -EFAULT;
return 0;
}
case HDIO_GET_IDENTITY:
if (minor(inode->i_rdev) & PARTN_MASK)
return -EINVAL;
if (drive->id == NULL)
return -ENOMSG;
if (copy_to_user((char *)arg, (char *)drive->id, sizeof(*drive->id)))
return -EFAULT;
return 0;
case HDIO_GET_NICE:
return put_user(drive->dsc_overlap | drive->atapi_overlap << 1,
(long *) arg);
case HDIO_SET_NICE:
if (!capable(CAP_SYS_ADMIN))
return -EACCES;
if (arg != (arg & 1))
return -EPERM;
drive->dsc_overlap = arg & 1;
/* Only CD-ROM's and tapes support DSC overlap. */
if (drive->dsc_overlap && !(drive->type == ATA_ROM || drive->type == ATA_TAPE)) {
drive->dsc_overlap = 0;
return -EPERM;
}
return 0;
case HDIO_GET_BUSSTATE:
if (put_user(drive->channel->bus_state, (long *)arg))
return -EFAULT;
return 0;
case HDIO_SET_BUSSTATE:
if (!capable(CAP_SYS_ADMIN))
return -EACCES;
if (drive->channel->busproc)
drive->channel->busproc(drive, (int)arg);
return 0;
case HDIO_DRIVE_CMD:
if (!capable(CAP_SYS_ADMIN))
return -EACCES;
if (!arg) {
if (ide_spin_wait_hwgroup(drive))
return -EBUSY;
/* Do nothing, just unlock */
spin_unlock_irq(drive->channel->lock);
return 0;
}
return do_cmd_ioctl(drive, arg);
/*
* uniform packet command handling
*/
case CDROMEJECT:
case CDROMCLOSETRAY:
return block_ioctl(inode->i_bdev, cmd, arg);
case BLKRRPART: /* Re-read partition tables */
return ata_revalidate(inode->i_rdev);
/* Now check whatever this particular ioctl has a device type
* specific implementation.
*/
default:
if (ata_ops(drive) && ata_ops(drive)->ioctl)
return ata_ops(drive)->ioctl(drive, inode, file, cmd, arg);
return -EINVAL;
}
}
|