Initial support for booting from USB drives.
[seabios.git] / src / usb-msc.c
1 // Code for handling USB Mass Storage Controller devices.
2 //
3 // Copyright (C) 2010  Kevin O'Connor <kevin@koconnor.net>
4 //
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
6
7 #include "util.h" // dprintf
8 #include "config.h" // CONFIG_USB_MSC
9 #include "usb-msc.h" // usb_msc_init
10 #include "usb.h" // struct usb_s
11 #include "biosvar.h" // GET_GLOBAL
12 #include "blockcmd.h" // cdb_read
13 #include "disk.h" // DTYPE_USB
14 #include "boot.h" // add_bcv_internal
15
16 #define DESCSIZE 80
17
18 struct usbdrive_s {
19     struct drive_s drive;
20     struct usb_pipe *bulkin, *bulkout;
21     char *desc;
22 };
23
24
25 /****************************************************************
26  * Bulk-only drive command processing
27  ****************************************************************/
28
29 #define USB_CDB_SIZE 12
30
31 #define CBW_SIGNATURE 0x43425355 // USBC
32
33 struct cbw_s {
34     u32 dCBWSignature;
35     u32 dCBWTag;
36     u32 dCBWDataTransferLength;
37     u8 bmCBWFlags;
38     u8 bCBWLUN;
39     u8 bCBWCBLength;
40     u8 CBWCB[16];
41 } PACKED;
42
43 #define CSW_SIGNATURE 0x53425355 // USBS
44
45 struct csw_s {
46     u32 dCSWSignature;
47     u32 dCSWTag;
48     u32 dCSWDataResidue;
49     u8 bCSWStatus;
50 } PACKED;
51
52 // Low-level usb command transmit function.
53 int
54 usb_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
55 {
56     dprintf(1, "usb_cmd_data id=%p write=%d count=%d bs=%d buf=%p\n"
57             , op->drive_g, 0, op->count, blocksize, op->buf_fl);
58     struct usbdrive_s *udrive_g = container_of(
59         op->drive_g, struct usbdrive_s, drive);
60     struct usb_pipe *bulkin = GET_GLOBAL(udrive_g->bulkin);
61     struct usb_pipe *bulkout = GET_GLOBAL(udrive_g->bulkout);
62
63     // Setup command block wrapper.
64     u32 bytes = blocksize * op->count;
65     struct cbw_s cbw;
66     memset(&cbw, 0, sizeof(cbw));
67     cbw.dCBWSignature = CBW_SIGNATURE;
68     cbw.dCBWTag = 999; // XXX
69     cbw.dCBWDataTransferLength = bytes;
70     cbw.bmCBWFlags = USB_DIR_IN; // XXX
71     cbw.bCBWLUN = 0; // XXX
72     cbw.bCBWCBLength = USB_CDB_SIZE;
73     memcpy(cbw.CBWCB, cdbcmd, USB_CDB_SIZE);
74
75     // Transfer cbw to device.
76     int ret = usb_send_bulk(bulkout, USB_DIR_OUT
77                             , MAKE_FLATPTR(GET_SEG(SS), &cbw), sizeof(cbw));
78     if (ret)
79         goto fail;
80
81     // Transfer data from device.
82     ret = usb_send_bulk(bulkin, USB_DIR_IN, op->buf_fl, bytes);
83     if (ret)
84         goto fail;
85
86     // Transfer csw info.
87     struct csw_s csw;
88     ret = usb_send_bulk(bulkin, USB_DIR_IN
89                         , MAKE_FLATPTR(GET_SEG(SS), &csw), sizeof(csw));
90     if (ret)
91         goto fail;
92
93     if (!csw.bCSWStatus)
94         return DISK_RET_SUCCESS;
95     if (csw.bCSWStatus == 2)
96         goto fail;
97
98     op->count -= csw.dCSWDataResidue / blocksize;
99     return DISK_RET_EBADTRACK;
100
101 fail:
102     // XXX - reset connection
103     dprintf(1, "USB transmission failed\n");
104     op->count = 0;
105     return DISK_RET_EBADTRACK;
106 }
107
108
109 /****************************************************************
110  * Drive ops
111  ****************************************************************/
112
113 // 16bit command demuxer for ATAPI cdroms.
114 int
115 process_usb_op(struct disk_op_s *op)
116 {
117     switch (op->command) {
118     case CMD_READ:
119         return cdb_read(op);
120     case CMD_FORMAT:
121     case CMD_WRITE:
122         return DISK_RET_EWRITEPROTECT;
123     case CMD_RESET:
124     case CMD_ISREADY:
125     case CMD_VERIFY:
126     case CMD_SEEK:
127         return DISK_RET_SUCCESS;
128     default:
129         op->count = 0;
130         return DISK_RET_EPARAM;
131     }
132 }
133
134 void
135 describe_usb(struct drive_s *drive_g)
136 {
137     struct usbdrive_s *udrive_g = container_of(
138         drive_g, struct usbdrive_s, drive);
139     printf("%s", udrive_g->desc);
140 }
141
142
143 /****************************************************************
144  * Setup
145  ****************************************************************/
146
147 static int
148 setup_drive_cdrom(struct disk_op_s *op)
149 {
150     op->drive_g->blksize = CDROM_SECTOR_SIZE;
151     op->drive_g->sectors = (u64)-1;
152     map_cd_drive(op->drive_g);
153     return 0;
154 }
155
156 static int
157 setup_drive_hd(struct disk_op_s *op)
158 {
159     struct cdbres_read_capacity info;
160     int ret = cdb_read_capacity(op, &info);
161     if (ret)
162         return ret;
163     // XXX - retry for some timeout?
164
165     u32 blksize = ntohl(info.blksize), sectors = ntohl(info.sectors);
166     if (blksize != DISK_SECTOR_SIZE) {
167         if (blksize == CDROM_SECTOR_SIZE)
168             return setup_drive_cdrom(op);
169         dprintf(1, "Unsupported USB MSC block size %d\n", blksize);
170         return -1;
171     }
172     op->drive_g->blksize = blksize;
173     op->drive_g->sectors = sectors;
174     dprintf(1, "USB MSC blksize=%d sectors=%d\n", blksize, sectors);
175
176     // Setup disk geometry translation.
177     setup_translation(op->drive_g);
178
179     // Register with bcv system.
180     add_bcv_internal(op->drive_g);
181
182     return 0;
183 }
184
185 // Configure a usb msc device.
186 int
187 usb_msc_init(u32 endp, struct usb_interface_descriptor *iface, int imax)
188 {
189     if (!CONFIG_USB_MSC)
190         return -1;
191
192     // Verify right kind of device
193     if (iface->bInterfaceSubClass != US_SC_SCSI
194         || iface->bInterfaceProtocol != US_PR_BULK) {
195         dprintf(1, "Unsupported MSC USB device (subclass=%02x proto=%02x)\n"
196                 , iface->bInterfaceSubClass, iface->bInterfaceProtocol);
197         return -1;
198     }
199
200     // Find bulk in and bulk out endpoints.
201     struct usb_endpoint_descriptor *indesc = findEndPointDesc(
202         iface, imax, USB_ENDPOINT_XFER_BULK, USB_DIR_IN);
203     struct usb_endpoint_descriptor *outdesc = findEndPointDesc(
204         iface, imax, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT);
205     if (!indesc || !outdesc)
206         goto fail;
207     u32 inendp = mkendpFromDesc(endp, indesc);
208     struct usb_pipe *bulkin = alloc_bulk_pipe(inendp);
209     u32 outendp = mkendpFromDesc(endp, outdesc);
210     struct usb_pipe *bulkout = alloc_bulk_pipe(outendp);
211     if (!bulkin || !bulkout)
212         goto fail;
213
214     // Allocate drive structure.
215     char *desc = malloc_tmphigh(DESCSIZE);
216     struct usbdrive_s *udrive_g = malloc_fseg(sizeof(*udrive_g));
217     if (!udrive_g || !desc) {
218         warn_noalloc();
219         goto fail;
220     }
221     memset(udrive_g, 0, sizeof(*udrive_g));
222     udrive_g->drive.type = DTYPE_USB;
223     udrive_g->bulkin = bulkin;
224     udrive_g->bulkout = bulkout;
225
226     // Validate drive and find block size and sector count.
227     struct disk_op_s dop;
228     memset(&dop, 0, sizeof(dop));
229     dop.drive_g = &udrive_g->drive;
230     struct cdbres_inquiry data;
231     int ret = cdb_get_inquiry(&dop, &data);
232     if (ret)
233         goto fail;
234     char vendor[sizeof(data.vendor)+1], product[sizeof(data.product)+1];
235     char rev[sizeof(data.rev)+1];
236     int pdt = data.pdt & 0x1f;
237     int removable = !!(data.removable & 0x80);
238     dprintf(1, "USB MSC vendor='%s' product='%s' rev='%s'"
239             " type=%d removable=%d\n"
240             , strtcpy(vendor, data.vendor, sizeof(vendor))
241             , strtcpy(product, data.product, sizeof(product))
242             , strtcpy(rev, data.rev, sizeof(rev))
243             , pdt, removable);
244
245     if (pdt == USB_MSC_TYPE_CDROM)
246         ret = setup_drive_cdrom(&dop);
247     else
248         ret = setup_drive_hd(&dop);
249     if (ret)
250         goto fail;
251
252     snprintf(desc, DESCSIZE, "USB Drive %s %s %s", vendor, product, rev);
253     udrive_g->desc = desc;
254
255     return 0;
256 fail:
257     dprintf(1, "Unable to configure USB MSC device.\n");
258     free(desc);
259     free(udrive_g);
260     return -1;
261 }