Skip to contentSkip to conversation

Workaround for zero error blocks

PATCH Pete Zaitcev 1 participant 1 message Dec 7, 2004 SCSI

lore
Share & tools
lore
#0 lore raw

Doug & others:

I'd like to get comments on attached patch. Without it, doing "dd if=/dev/scd0" produces a truncated image. Does it look sane?

Thanks,
-- Pete

P.S. Here's a device fingerprint in case it's a firmware bug and we want to shame the vendor.

dmesg (INQUIRY):
Vendor: CDWRITER Model: IDE2410 Rev: A.23
Type: CD-ROM ANSI SCSI revision: 00

/proc/bus/usb/devices:

P:  Vendor=0c0b ProdID=b001 Rev=11.10
S:  Manufacturer=DMI
S:  Product=USB 2.0 Storage Adaptor
S:  SerialNumber=0B020142050D2A72
drivers/scsi/sr.c +31 -0
diff -urp -X dontdiff linux-2.6.10-rc2-bk8/drivers/scsi/sr.c linux-2.6.10-rc2-bk8-usb/drivers/scsi/sr.c
@@ -212,6 +212,35 @@ int sr_media_change(struct cdrom_device_
 	}
 	return retval;
 }
+
+/*
+ * This function allows to do "dd if=/dev/scd0 of=/tmp/image.bin" and get
+ * a non-truncated result which matches the burned ISO. The problem happens
+ * with a broken USB CD-ROM which does two things:
+ *  - reports the media size a few blocks bigger than recorded size
+ *  - reports sense with zero error block if we read across the end of media
+ */
+static void correct_zero_error_sector(long *error_sector, struct scsi_cmnd *SCpnt)
+{
+	unsigned int cmd_block;
+	unsigned int cmd_len, resid_len;
+
+	if (SCpnt->cmnd[0] != READ_6 && SCpnt->cmnd[0] != READ_10)
+		return;
+
+	/* Do not touch anything that is not a CD-ROM */
+	if (SCpnt->device->sector_size != 2048)
+		return;
+
+	cmd_block = SCpnt->request->sector >> 2;
+	cmd_len = SCpnt->request_bufflen >> 11;
+
+	resid_len = SCpnt->resid >> 11;
+	if (resid_len == 0 || resid_len >= cmd_len)
+		return;
+
+	*error_sector = cmd_block + (cmd_len - resid_len);
+}
  
 /*
  * rw_intr is the interrupt routine for the device driver.
@@ -252,6 +281,8 @@ static void rw_intr(struct scsi_cmnd * S
 				(SCpnt->sense_buffer[4] << 16) |
 				(SCpnt->sense_buffer[5] << 8) |
 				SCpnt->sense_buffer[6];
+			if (error_sector == 0)
+				correct_zero_error_sector(&error_sector, SCpnt);
 			if (SCpnt->request->bio != NULL)
 				block_sectors =
 					bio_sectors(SCpnt->request->bio);