1
/* usbms.c - USB Mass Storage Support. */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2008 Free Software Foundation, Inc.
6
* GRUB is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* GRUB is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
23
#include <grub/scsi.h>
24
#include <grub/scsicmd.h>
25
#include <grub/misc.h>
27
#define GRUB_USBMS_DIRECTION_BIT 7
29
/* The USB Mass Storage Command Block Wrapper. */
32
grub_uint32_t signature;
34
grub_uint32_t transfer_length;
38
grub_uint8_t cbwcb[16];
39
} __attribute__ ((packed));
43
grub_uint32_t signature;
45
grub_uint32_t residue;
47
} __attribute__ ((packed));
51
struct grub_usb_device *dev;
57
struct grub_usb_desc_endp *in;
58
struct grub_usb_desc_endp *out;
63
typedef struct grub_usbms_dev *grub_usbms_dev_t;
65
/* FIXME: remove limit. */
66
#define MAX_USBMS_DEVICES 128
67
static grub_usbms_dev_t grub_usbms_devices[MAX_USBMS_DEVICES];
68
static int first_available_slot = 0;
71
grub_usbms_reset (grub_usb_device_t dev, int interface)
73
return grub_usb_control_msg (dev, 0x21, 255, 0, interface, 0, 0);
77
grub_usbms_detach (grub_usb_device_t usbdev, int config, int interface)
80
for (i = 0; i < ARRAY_SIZE (grub_usbms_devices); i++)
81
if (grub_usbms_devices[i] && grub_usbms_devices[i]->dev == usbdev
82
&& grub_usbms_devices[i]->interface == interface
83
&& grub_usbms_devices[i]->config == config)
85
grub_free (grub_usbms_devices[i]);
86
grub_usbms_devices[i] = 0;
91
grub_usbms_attach (grub_usb_device_t usbdev, int configno, int interfno)
93
struct grub_usb_desc_if *interf
94
= usbdev->config[configno].interf[interfno].descif;
96
grub_uint8_t luns = 0;
100
if (first_available_slot == ARRAY_SIZE (grub_usbms_devices))
103
curnum = first_available_slot;
104
first_available_slot++;
106
interf = usbdev->config[configno].interf[interfno].descif;
108
if ((interf->subclass != GRUB_USBMS_SUBCLASS_BULK
109
/* Experimental support of RBC, MMC-2, UFI, SFF-8070i devices */
110
&& interf->subclass != GRUB_USBMS_SUBCLASS_RBC
111
&& interf->subclass != GRUB_USBMS_SUBCLASS_MMC2
112
&& interf->subclass != GRUB_USBMS_SUBCLASS_UFI
113
&& interf->subclass != GRUB_USBMS_SUBCLASS_SFF8070 )
114
|| interf->protocol != GRUB_USBMS_PROTOCOL_BULK)
117
grub_usbms_devices[curnum] = grub_zalloc (sizeof (struct grub_usbms_dev));
118
if (! grub_usbms_devices[curnum])
121
grub_usbms_devices[curnum]->dev = usbdev;
122
grub_usbms_devices[curnum]->interface = interfno;
124
grub_dprintf ("usbms", "alive\n");
126
/* Iterate over all endpoints of this interface, at least a
127
IN and OUT bulk endpoint are required. */
128
for (j = 0; j < interf->endpointcnt; j++)
130
struct grub_usb_desc_endp *endp;
131
endp = &usbdev->config[0].interf[interfno].descendp[j];
133
if ((endp->endp_addr & 128) && (endp->attrib & 3) == 2)
135
/* Bulk IN endpoint. */
136
grub_usbms_devices[curnum]->in = endp;
137
/* Clear Halt is not possible yet! */
138
/* grub_usb_clear_halt (usbdev, endp->endp_addr); */
139
grub_usbms_devices[curnum]->in_maxsz = endp->maxpacket;
141
else if (!(endp->endp_addr & 128) && (endp->attrib & 3) == 2)
143
/* Bulk OUT endpoint. */
144
grub_usbms_devices[curnum]->out = endp;
145
/* Clear Halt is not possible yet! */
146
/* grub_usb_clear_halt (usbdev, endp->endp_addr); */
147
grub_usbms_devices[curnum]->out_maxsz = endp->maxpacket;
151
if (!grub_usbms_devices[curnum]->in || !grub_usbms_devices[curnum]->out)
153
grub_free (grub_usbms_devices[curnum]);
154
grub_usbms_devices[curnum] = 0;
158
grub_dprintf ("usbms", "alive\n");
160
/* XXX: Activate the first configuration. */
161
grub_usb_set_configuration (usbdev, 1);
163
/* Query the amount of LUNs. */
164
err = grub_usb_control_msg (usbdev, 0xA1, 254, 0, interfno, 1, (char *) &luns);
168
/* In case of a stall, clear the stall. */
169
if (err == GRUB_USB_ERR_STALL)
171
grub_usb_clear_halt (usbdev, grub_usbms_devices[curnum]->in->endp_addr);
172
grub_usb_clear_halt (usbdev, grub_usbms_devices[curnum]->out->endp_addr);
174
/* Just set the amount of LUNs to one. */
175
grub_errno = GRUB_ERR_NONE;
176
grub_usbms_devices[curnum]->luns = 1;
179
/* luns = 0 means one LUN with ID 0 present ! */
180
/* We get from device not number of LUNs but highest
181
* LUN number. LUNs are numbered from 0,
182
* i.e. number of LUNs is luns+1 ! */
183
grub_usbms_devices[curnum]->luns = luns + 1;
185
grub_dprintf ("usbms", "alive\n");
187
usbdev->config[configno].interf[interfno].detach_hook = grub_usbms_detach;
189
#if 0 /* All this part should be probably deleted.
190
* This make trouble on some devices if they are not in
191
* Phase Error state - and there they should be not in such state...
192
* Bulk only mass storage reset procedure should be used only
193
* on place and in time when it is really necessary. */
194
/* Reset recovery procedure */
195
/* Bulk-Only Mass Storage Reset, after the reset commands
197
grub_usbms_reset (usbdev, i);
198
grub_usb_clear_halt (usbdev, usbms->in->endp_addr);
199
grub_usb_clear_halt (usbdev, usbms->out->endp_addr);
208
grub_usbms_iterate (int (*hook) (int bus, int luns))
212
grub_usb_poll_devices ();
214
for (i = 0; i < ARRAY_SIZE (grub_usbms_devices); i++)
215
if (grub_usbms_devices[i])
217
if (hook (i, grub_usbms_devices[i]->luns))
225
grub_usbms_transfer (struct grub_scsi *scsi, grub_size_t cmdsize, char *cmd,
226
grub_size_t size, char *buf, int read_write)
228
struct grub_usbms_cbw cbw;
229
grub_usbms_dev_t dev = (grub_usbms_dev_t) scsi->data;
230
struct grub_usbms_csw status;
231
static grub_uint32_t tag = 0;
232
grub_usb_err_t err = GRUB_USB_ERR_NONE;
233
grub_usb_err_t errCSW = GRUB_USB_ERR_NONE;
234
int retrycnt = 3 + 1;
239
return grub_error (GRUB_ERR_IO, "USB Mass Storage stalled");
241
/* Setup the request. */
242
grub_memset (&cbw, 0, sizeof (cbw));
243
cbw.signature = grub_cpu_to_le32 (0x43425355);
245
cbw.transfer_length = grub_cpu_to_le32 (size);
246
cbw.flags = (!read_write) << GRUB_USBMS_DIRECTION_BIT;
247
cbw.lun = scsi->lun; /* In USB MS CBW are LUN bits on another place than in SCSI CDB, both should be set correctly. */
248
cbw.length = cmdsize;
249
grub_memcpy (cbw.cbwcb, cmd, cmdsize);
251
/* Debug print of CBW content. */
252
grub_dprintf ("usb", "CBW: sign=0x%08x tag=0x%08x len=0x%08x\n",
253
cbw.signature, cbw.tag, cbw.transfer_length);
254
grub_dprintf ("usb", "CBW: flags=0x%02x lun=0x%02x CB_len=0x%02x\n",
255
cbw.flags, cbw.lun, cbw.length);
256
grub_dprintf ("usb", "CBW: cmd:\n %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
257
cbw.cbwcb[ 0], cbw.cbwcb[ 1], cbw.cbwcb[ 2], cbw.cbwcb[ 3],
258
cbw.cbwcb[ 4], cbw.cbwcb[ 5], cbw.cbwcb[ 6], cbw.cbwcb[ 7],
259
cbw.cbwcb[ 8], cbw.cbwcb[ 9], cbw.cbwcb[10], cbw.cbwcb[11],
260
cbw.cbwcb[12], cbw.cbwcb[13], cbw.cbwcb[14], cbw.cbwcb[15]);
262
/* Write the request.
263
* XXX: Error recovery is maybe still not fully correct. */
264
err = grub_usb_bulk_write (dev->dev, dev->out->endp_addr,
265
sizeof (cbw), (char *) &cbw);
268
if (err == GRUB_USB_ERR_STALL)
270
grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
273
return grub_error (GRUB_ERR_IO, "USB Mass Storage request failed");
276
/* Read/write the data, (maybe) according to specification. */
277
if (size && (read_write == 0))
279
err = grub_usb_bulk_read (dev->dev, dev->in->endp_addr, size, buf);
280
grub_dprintf ("usb", "read: %d %d\n", err, GRUB_USB_ERR_STALL);
283
if (err == GRUB_USB_ERR_STALL)
284
grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
287
/* Debug print of received data. */
288
grub_dprintf ("usb", "buf:\n");
292
for (i = 0; i < size; i++)
293
grub_dprintf ("usb", "0x%02x: 0x%02x\n", i, buf[i]);
296
grub_dprintf ("usb", "Too much data for debug print...\n");
300
err = grub_usb_bulk_write (dev->dev, dev->out->endp_addr, size, buf);
301
grub_dprintf ("usb", "write: %d %d\n", err, GRUB_USB_ERR_STALL);
302
grub_dprintf ("usb", "First 16 bytes of sent data:\n %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
303
buf[ 0], buf[ 1], buf[ 2], buf[ 3],
304
buf[ 4], buf[ 5], buf[ 6], buf[ 7],
305
buf[ 8], buf[ 9], buf[10], buf[11],
306
buf[12], buf[13], buf[14], buf[15]);
309
if (err == GRUB_USB_ERR_STALL)
310
grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
313
/* Debug print of sent data. */
317
for (i=0; i<size; i++)
318
grub_dprintf ("usb", "0x%02x: 0x%02x\n", i, buf[i]);
321
grub_dprintf ("usb", "Too much data for debug print...\n");
324
/* Read the status - (maybe) according to specification. */
326
errCSW = grub_usb_bulk_read (dev->dev, dev->in->endp_addr,
327
sizeof (status), (char *) &status);
330
grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
331
errCSW = grub_usb_bulk_read (dev->dev, dev->in->endp_addr,
332
sizeof (status), (char *) &status);
334
{ /* Bulk-only reset device. */
335
grub_dprintf ("usb", "Bulk-only reset device - errCSW\n");
336
grub_usbms_reset (dev->dev, dev->interface);
337
grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
338
grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
343
/* Debug print of CSW content. */
344
grub_dprintf ("usb", "CSW: sign=0x%08x tag=0x%08x resid=0x%08x\n",
345
status.signature, status.tag, status.residue);
346
grub_dprintf ("usb", "CSW: status=0x%02x\n", status.status);
348
/* If phase error or not valid signature, do bulk-only reset device. */
349
if ((status.status == 2) ||
350
(status.signature != grub_cpu_to_le32(0x53425355)))
351
{ /* Bulk-only reset device. */
352
grub_dprintf ("usb", "Bulk-only reset device - bad status\n");
353
grub_usbms_reset (dev->dev, dev->interface);
354
grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
355
grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
360
/* If "command failed" status or data transfer failed -> error */
361
if ((status.status || err) && !read_write)
362
return grub_error (GRUB_ERR_READ_ERROR,
363
"error communication with USB Mass Storage device");
364
else if ((status.status || err) && read_write)
365
return grub_error (GRUB_ERR_WRITE_ERROR,
366
"error communication with USB Mass Storage device");
368
return GRUB_ERR_NONE;
373
grub_usbms_read (struct grub_scsi *scsi, grub_size_t cmdsize, char *cmd,
374
grub_size_t size, char *buf)
376
return grub_usbms_transfer (scsi, cmdsize, cmd, size, buf, 0);
380
grub_usbms_write (struct grub_scsi *scsi, grub_size_t cmdsize, char *cmd,
381
grub_size_t size, char *buf)
383
return grub_usbms_transfer (scsi, cmdsize, cmd, size, buf, 1);
387
grub_usbms_open (int devnum, struct grub_scsi *scsi)
389
grub_usb_poll_devices ();
391
if (!grub_usbms_devices[devnum])
392
return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
393
"unknown USB Mass Storage device");
395
scsi->data = grub_usbms_devices[devnum];
396
scsi->luns = grub_usbms_devices[devnum]->luns;
398
return GRUB_ERR_NONE;
401
static struct grub_scsi_dev grub_usbms_dev =
404
.id = GRUB_SCSI_SUBSYSTEM_USBMS,
405
.iterate = grub_usbms_iterate,
406
.open = grub_usbms_open,
407
.read = grub_usbms_read,
408
.write = grub_usbms_write
411
struct grub_usb_attach_desc attach_hook =
413
.class = GRUB_USB_CLASS_MASS_STORAGE,
414
.hook = grub_usbms_attach
419
grub_usb_register_attach_hook_class (&attach_hook);
420
grub_scsi_dev_register (&grub_usbms_dev);
426
for (i = 0; i < ARRAY_SIZE (grub_usbms_devices); i++)
428
grub_usbms_devices[i]->dev->config[grub_usbms_devices[i]->config]
429
.interf[grub_usbms_devices[i]->interface].detach_hook = 0;
430
grub_usbms_devices[i]->dev->config[grub_usbms_devices[i]->config]
431
.interf[grub_usbms_devices[i]->interface].attached = 0;
433
grub_usb_unregister_attach_hook_class (&attach_hook);
434
grub_scsi_dev_unregister (&grub_usbms_dev);