usb-msc: move READ CAPACITY to usb_msc_init, fix off-by-one
[seabios.git] / src / usb-msc.c
index a57e4d2c35c50584411c41c8a20b04690e472565..3298ca394f444bbab4eec61dafee621f7d692376 100644 (file)
@@ -12,7 +12,6 @@
 #include "blockcmd.h" // cdb_read
 #include "disk.h" // DTYPE_USB
 #include "boot.h" // boot_add_hd
-#include "pci.h" // struct pci_device
 
 struct usbdrive_s {
     struct drive_s drive;
@@ -47,6 +46,17 @@ struct csw_s {
     u8 bCSWStatus;
 } PACKED;
 
+static int
+usb_msc_send(struct usbdrive_s *udrive_g, int dir, void *buf, u32 bytes)
+{
+    struct usb_pipe *pipe;
+    if (dir == USB_DIR_OUT)
+        pipe = GET_GLOBAL(udrive_g->bulkout);
+    else
+        pipe = GET_GLOBAL(udrive_g->bulkin);
+    return usb_send_bulk(pipe, dir, buf, bytes);
+}
+
 // Low-level usb command transmit function.
 int
 usb_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
@@ -58,35 +68,35 @@ usb_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
             , op->drive_g, 0, op->count, blocksize, op->buf_fl);
     struct usbdrive_s *udrive_g = container_of(
         op->drive_g, struct usbdrive_s, drive);
-    struct usb_pipe *bulkin = GET_GLOBAL(udrive_g->bulkin);
-    struct usb_pipe *bulkout = GET_GLOBAL(udrive_g->bulkout);
 
     // Setup command block wrapper.
     u32 bytes = blocksize * op->count;
     struct cbw_s cbw;
     memset(&cbw, 0, sizeof(cbw));
+    memcpy(cbw.CBWCB, cdbcmd, USB_CDB_SIZE);
     cbw.dCBWSignature = CBW_SIGNATURE;
     cbw.dCBWTag = 999; // XXX
     cbw.dCBWDataTransferLength = bytes;
-    cbw.bmCBWFlags = USB_DIR_IN; // XXX
+    cbw.bmCBWFlags = (cbw.CBWCB[0] == CDB_CMD_WRITE_10) ? USB_DIR_OUT : USB_DIR_IN;
     cbw.bCBWLUN = 0; // XXX
     cbw.bCBWCBLength = USB_CDB_SIZE;
-    memcpy(cbw.CBWCB, cdbcmd, USB_CDB_SIZE);
 
     // Transfer cbw to device.
-    int ret = usb_send_bulk(bulkout, USB_DIR_OUT
+    int ret = usb_msc_send(udrive_g, USB_DIR_OUT
                             , MAKE_FLATPTR(GET_SEG(SS), &cbw), sizeof(cbw));
     if (ret)
         goto fail;
 
-    // Transfer data from device.
-    ret = usb_send_bulk(bulkin, USB_DIR_IN, op->buf_fl, bytes);
-    if (ret)
-        goto fail;
+    // Transfer data to/from device.
+    if (bytes) {
+        ret = usb_msc_send(udrive_g, cbw.bmCBWFlags, op->buf_fl, bytes);
+        if (ret)
+            goto fail;
+    }
 
     // Transfer csw info.
     struct csw_s csw;
-    ret = usb_send_bulk(bulkin, USB_DIR_IN
+    ret = usb_msc_send(udrive_g, USB_DIR_IN
                         , MAKE_FLATPTR(GET_SEG(SS), &csw), sizeof(csw));
     if (ret)
         goto fail;
@@ -96,7 +106,8 @@ usb_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
     if (csw.bCSWStatus == 2)
         goto fail;
 
-    op->count -= csw.dCSWDataResidue / blocksize;
+    if (blocksize)
+        op->count -= csw.dCSWDataResidue / blocksize;
     return DISK_RET_EBADTRACK;
 
 fail:
@@ -120,9 +131,9 @@ process_usb_op(struct disk_op_s *op)
     switch (op->command) {
     case CMD_READ:
         return cdb_read(op);
-    case CMD_FORMAT:
     case CMD_WRITE:
-        return DISK_RET_EWRITEPROTECT;
+        return cdb_write(op);
+    case CMD_FORMAT:
     case CMD_RESET:
     case CMD_ISREADY:
     case CMD_VERIFY:
@@ -142,11 +153,10 @@ process_usb_op(struct disk_op_s *op)
 static int
 setup_drive_cdrom(struct disk_op_s *op, char *desc)
 {
-    op->drive_g->blksize = CDROM_SECTOR_SIZE;
     op->drive_g->sectors = (u64)-1;
     struct usb_pipe *pipe = container_of(
         op->drive_g, struct usbdrive_s, drive)->bulkout;
-    int prio = bootprio_find_usb(pipe->cntl->pci->bdf, pipe->path);
+    int prio = bootprio_find_usb(pipe->cntl->pci, pipe->path);
     boot_add_cd(op->drive_g, desc, prio);
     return 0;
 }
@@ -154,29 +164,16 @@ setup_drive_cdrom(struct disk_op_s *op, char *desc)
 static int
 setup_drive_hd(struct disk_op_s *op, char *desc)
 {
-    struct cdbres_read_capacity info;
-    int ret = cdb_read_capacity(op, &info);
-    if (ret)
-        return ret;
-    // XXX - retry for some timeout?
-
-    u32 blksize = ntohl(info.blksize), sectors = ntohl(info.sectors);
-    if (blksize != DISK_SECTOR_SIZE) {
-        if (blksize == CDROM_SECTOR_SIZE)
-            return setup_drive_cdrom(op, desc);
-        dprintf(1, "Unsupported USB MSC block size %d\n", blksize);
+    if (op->drive_g->blksize != DISK_SECTOR_SIZE) {
+        dprintf(1, "Unsupported USB MSC block size %d\n", op->drive_g->blksize);
         return -1;
     }
-    op->drive_g->blksize = blksize;
-    op->drive_g->sectors = sectors;
-    dprintf(1, "USB MSC blksize=%d sectors=%d\n", blksize, sectors);
 
     // Register with bcv system.
     struct usb_pipe *pipe = container_of(
         op->drive_g, struct usbdrive_s, drive)->bulkout;
-    int prio = bootprio_find_usb(pipe->cntl->pci->bdf, pipe->path);
+    int prio = bootprio_find_usb(pipe->cntl->pci, pipe->path);
     boot_add_hd(op->drive_g, desc, prio);
-
     return 0;
 }
 
@@ -246,6 +243,18 @@ usb_msc_init(struct usb_pipe *pipe
                               , vendor, product, rev);
         ret = setup_drive_cdrom(&dop, desc);
     } else {
+        struct cdbres_read_capacity capdata;
+        ret = cdb_read_capacity(&dop, &capdata);
+        if (ret)
+            return ret;
+        // XXX - retry for some timeout?
+
+        // READ CAPACITY returns the address of the last block
+        udrive_g->drive.blksize = ntohl(capdata.blksize);
+        udrive_g->drive.sectors = ntohl(capdata.sectors) + 1;
+        dprintf(1, "USB MSC blksize=%d sectors=%d\n",
+                udrive_g->drive.blksize, (int)udrive_g->drive.sectors);
+
         char *desc = znprintf(MAXDESCSIZE, "USB Drive %s %s %s"
                               , vendor, product, rev);
         ret = setup_drive_hd(&dop, desc);