-
Notifications
You must be signed in to change notification settings - Fork 141
/
udisksata.c
372 lines (340 loc) · 12.5 KB
/
udisksata.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
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
365
366
367
368
369
370
371
372
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2008 David Zeuthen <zeuthen@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "config.h"
#include <glib/gi18n-lib.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/bsg.h>
#include <scsi/scsi.h>
#include <scsi/sg.h>
#include <scsi/scsi_ioctl.h>
#include <linux/cdrom.h>
#include <stdint.h>
#include <glib.h>
#include <glib-object.h>
#include "udisksata.h"
#include "udisksprivate.h"
#include "udiskslogging.h"
#include "udisksdaemonutil.h"
#define UDISKS_ATA_DEFAULT_COMMAND_TIMEOUT_MSEC (5 * 1000)
/**
* SECTION:udisksata
* @title: ATA commands
* @short_description: Helper routines for ATA commands
*
* Helper routines for sending ATA commands to a device.
*/
/**
* sata_protocol:
* @cdb: The CDB byte array to be updated (bytes 1 & 2)
* @protocol: The specified protocol
*
* Set the CDB bytes 1 & 2 for correct SATA protocol.
*/
static void
sata_protocol (guint8 cdb[], UDisksAtaCommandProtocol protocol)
{
switch (protocol)
{
case UDISKS_ATA_COMMAND_PROTOCOL_NONE:
cdb[1] = 3 << 1; /* PROTOCOL: Non-data */
cdb[2] = 0x20; /* OFF_LINE=0, CK_COND=1, T_DIR=0, BYT_BLOK=0, T_LENGTH=0 */
break;
case UDISKS_ATA_COMMAND_PROTOCOL_DRIVE_TO_HOST:
cdb[1] = 4 << 1; /* PROTOCOL: PIO Data-In */
cdb[2] = 0x2e; /* OFF_LINE=0, CK_COND=1, T_DIR=1, BYT_BLOK=1, T_LENGTH=2 */
break;
case UDISKS_ATA_COMMAND_PROTOCOL_HOST_TO_DRIVE:
cdb[1] = 5 << 1; /* PROTOCOL: PIO Data-Out */
cdb[2] = 0x26; /* OFF_LINE=0, CK_COND=1, T_DIR=0, BYT_BLOK=1, T_LENGTH=2 */
break;
default:
g_assert_not_reached ();
break;
}
}
/**
* udisks_ata_send_command_sync:
* @fd: A file descriptor for a ATA device.
* @timeout_msec: Timeout in milli-seconds for the command. Use -1 for the default (5 seconds) timeout and %G_MAXINT for no timeout.
* @protocol: The direction of the command.
* @input: The input for the command.
* @output: The output for the command.
* @error: Return location for error or %NULL.
*
* Sends a command to an ATA device. Blocks the calling thread while the command is pending.
*
* Returns: %TRUE if the command succeeded, %FALSE if @error is set.
*/
gboolean
udisks_ata_send_command_sync (gint fd,
gint timeout_msec,
UDisksAtaCommandProtocol protocol,
UDisksAtaCommandInput *input,
UDisksAtaCommandOutput *output,
GError **error)
{
struct sg_io_v4 io_v4;
uint8_t cdb[16];
gint cdb_len = 16;
uint8_t sense[32];
uint8_t *desc = sense+8;
gboolean use_ata12 = FALSE;
gboolean ret = FALSE;
gint rc;
g_return_val_if_fail (fd != -1, FALSE);
g_return_val_if_fail (timeout_msec == -1 || timeout_msec > 0, FALSE);
g_return_val_if_fail (/* protocol >= 0 && */ protocol <= 2, FALSE);
g_return_val_if_fail (input != NULL, FALSE);
g_return_val_if_fail (input->buffer_size == 0 || input->buffer != NULL, FALSE);
g_return_val_if_fail (output != NULL, FALSE);
g_return_val_if_fail (output->buffer_size == 0 || output->buffer != NULL, FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
if (timeout_msec == -1)
timeout_msec = UDISKS_ATA_DEFAULT_COMMAND_TIMEOUT_MSEC;
/* zero outputs, even if returning an error */
output->error = 0;
output->count = 0;
output->device = 0;
output->status = 0;
output->lba = 0;
if (output->buffer != NULL)
memset (output->buffer, 0, output->buffer_size);
memset (cdb, 0, sizeof (cdb));
/* Prefer ATA PASS-THROUGH (16) to ATA PASS-THROUGH (12) since the op-code
* for the latter clashes with the MMC blank command.
*
* TODO: this is hard-coded to FALSE for now - should retry with the 12-byte
* version only if the 16-byte version fails. But we don't do that
* right now
*/
use_ata12 = FALSE;
if (use_ata12)
{
/* Do not confuse optical drive firmware with ATA commands
* some drives are reported to blank CD-RWs because the op-code
* for ATA PASS-THROUGH (12) clashes with the MMC blank command.
*
* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=556635
*/
if (ioctl (fd, CDROM_GET_CAPABILITY, NULL) >= 0)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Refusing to send ATA PASS-THROUGH (12) to optical drive");
goto out;
}
/*
* ATA Pass-Through 12 byte command, as described in
*
* T10 04-262r8 ATA Command Pass-Through
*
* from http://www.t10.org/ftp/t10/document.04/04-262r8.pdf
*/
cdb[0] = 0xa1; /* OPERATION CODE: 12 byte pass through */
sata_protocol (cdb, protocol); /* Set the protocol bytes */
cdb[3] = input->feature; /* FEATURES */
cdb[4] = input->count; /* SECTORS */
cdb[5] = (input->lba >> 0) & 0xff; /* LBA LOW */
cdb[6] = (input->lba >> 8) & 0xff; /* LBA MID */
cdb[7] = (input->lba >> 16) & 0xff; /* LBA HIGH */
cdb[8] = input->device; /* SELECT */
cdb[9] = input->command; /* ATA COMMAND */
cdb_len = 12;
}
else
{
/*
* ATA Pass-Through 16 byte command, as described in
*
* T10 04-262r8 ATA Command Pass-Through
*
* from http://www.t10.org/ftp/t10/document.04/04-262r8.pdf
*/
cdb[0] = 0x85; /* OPERATION CODE: 16 byte pass through */
sata_protocol (cdb, protocol); /* Set the protocol bytes */
cdb[ 3] = (input->feature >> 8) & 0xff; /* FEATURES */
cdb[ 4] = (input->feature >> 0) & 0xff; /* FEATURES */
cdb[ 5] = (input->count >> 8) & 0xff; /* SECTORS */
cdb[ 6] = (input->count >> 0) & 0xff; /* SECTORS */
cdb[ 8] = (input->lba >> 16) & 0xff; /* LBA HIGH */
cdb[10] = (input->lba >> 8) & 0xff; /* LBA MID */
cdb[12] = (input->lba >> 0) & 0xff; /* LBA LOW */
cdb[13] = input->device; /* SELECT */
cdb[14] = input->command; /* ATA COMMAND */
cdb_len = 16;
}
/* See http://sg.danny.cz/sg/sg_io.html and http://www.tldp.org/HOWTO/SCSI-Generic-HOWTO/index.html
* for detailed information about how the SG_IO ioctl work
*/
memset (sense, 0, sizeof (sense));
memset (&io_v4, 0, sizeof (io_v4));
io_v4.guard = 'Q';
io_v4.protocol = BSG_PROTOCOL_SCSI;
io_v4.subprotocol = BSG_SUB_PROTOCOL_SCSI_CMD;
io_v4.request_len = cdb_len;
io_v4.request = (uintptr_t) cdb;
io_v4.max_response_len = sizeof (sense);
io_v4.response = (uintptr_t) sense;
io_v4.din_xfer_len = output->buffer_size;
io_v4.din_xferp = (uintptr_t) output->buffer;
io_v4.dout_xfer_len = input->buffer_size;
io_v4.dout_xferp = (uintptr_t) input->buffer;
if (timeout_msec == G_MAXINT)
io_v4.timeout = G_MAXUINT;
else
io_v4.timeout = timeout_msec;
rc = ioctl (fd, SG_IO, &io_v4);
if (rc != 0)
{
/* could be that the driver doesn't do version 4, try version 3 */
if (errno == EINVAL)
{
struct sg_io_hdr io_hdr;
memset(&io_hdr, 0, sizeof (struct sg_io_hdr));
io_hdr.interface_id = 'S';
io_hdr.cmdp = (unsigned char*) cdb;
io_hdr.cmd_len = cdb_len;
switch (protocol)
{
case UDISKS_ATA_COMMAND_PROTOCOL_NONE:
io_hdr.dxfer_direction = SG_DXFER_NONE;
break;
case UDISKS_ATA_COMMAND_PROTOCOL_DRIVE_TO_HOST:
io_hdr.dxferp = output->buffer;
io_hdr.dxfer_len = output->buffer_size;
io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
break;
case UDISKS_ATA_COMMAND_PROTOCOL_HOST_TO_DRIVE:
io_hdr.dxferp = input->buffer;
io_hdr.dxfer_len = input->buffer_size;
io_hdr.dxfer_direction = SG_DXFER_TO_DEV;
break;
}
io_hdr.sbp = sense;
io_hdr.mx_sb_len = sizeof (sense);
if (timeout_msec == G_MAXINT)
io_hdr.timeout = G_MAXUINT;
else
io_hdr.timeout = timeout_msec;
rc = ioctl (fd, SG_IO, &io_hdr);
if (rc != 0)
{
g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno),
"SGIO v3 ioctl failed (v4 not supported): %m");
goto out;
}
}
else
{
g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno),
"SGIO v4 ioctl failed: %m");
goto out;
}
}
if (!(sense[0] == 0x72 && desc[0] == 0x9 && desc[1] == 0x0c))
{
gchar *s = udisks_daemon_util_hexdump (sense, 32);
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Unexpected sense data returned:\n%s", s);
g_free (s);
goto out;
}
output->error = desc[3];
output->count = desc[5];
output->device = desc[12];
output->status = desc[13];
output->lba = (desc[11] << 16) | (desc[9] << 8) | desc[7];
/* TODO: be more exact with the error code perhaps? */
if (output->error != 0 || output->status & 0x01)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"ATA command failed: error=0x%02x count=0x%02x status=0x%02x",
(guint) output->error, (guint) output->count, (guint) output->status);
goto out;
}
ret = TRUE;
out:
return ret;
}
/**
* udisks_ata_get_pm_state:
* @device: ATA drive block device path.
* @error: Return location for error.
* @pm_state: Return location for the current power state value.
*
* Get the current power mode state.
*
* The format of @pm_state is the result obtained from sending the
* ATA command `CHECK POWER MODE` to the drive.
*
* Known values include:
* - `0x00`: Device is in PM2: Standby state.
* - `0x40`: Device is in the PM0: Active state, the NV Cache power mode is enabled, and the spindle is spun down or spinning down.
* - `0x41`: Device is in the PM0: Active state, the NV Cache power mode is enabled, and the spindle is spun up or spinning up.
* - `0x80`: Device is in PM1: Idle state.
* - `0xff`: Device is in the PM0: Active state or PM1: Idle State.
*
* Typically user interfaces will report "Drive is spun down" if @pm_state is
* 0x00 and "Drive is spun up" otherwise.
*
* Returns: %TRUE if the operation succeeded, %FALSE if @error is set.
*/
gboolean
udisks_ata_get_pm_state (const gchar *device, GError **error, guchar *count)
{
int fd;
gboolean rc = FALSE;
/* ATA8: 7.8 CHECK POWER MODE - E5h, Non-Data */
UDisksAtaCommandInput input = {.command = 0xe5};
UDisksAtaCommandOutput output = {0};
g_warn_if_fail (device != NULL);
fd = open (device, O_RDONLY|O_NONBLOCK);
if (fd == -1)
{
g_set_error (error, UDISKS_ERROR, UDISKS_ERROR_FAILED,
"Error opening device file %s while getting PM state: %m",
device);
goto out;
}
if (!udisks_ata_send_command_sync (fd,
-1,
UDISKS_ATA_COMMAND_PROTOCOL_NONE,
&input,
&output,
error))
{
g_prefix_error (error, "Error sending ATA command CHECK POWER MODE: ");
goto out;
}
/* count field is used for the state, see ATA8: table 102 */
*count = output.count;
rc = TRUE;
out:
if (fd != -1)
close (fd);
return rc;
}