[U-Boot] [PATCH 10/11] dm: scsi: fix divide-by-0 error in scsi_scan()

Jean-Jacques Hiblot jjhiblot at ti.com
Fri Apr 14 11:08:08 UTC 2017


With DM_SCSI enabled, blk_create_devicef() is called with blkz = 0, leading
to a divide-by-0 exception.
scsi_detect_dev() can be used to get the required parameters (block size
and number of blocks) from the drive before calling blk_create_devicef().

Signed-off-by: Jean-Jacques Hiblot <jjhiblot at ti.com>
Reviewed-by: Tom Rini <trini at konsulko.com>
Reviewed-by: Simon Glass <sjg at chromium.org>
---
 common/scsi.c | 85 ++++++++++++++++++++++++++++++++++-------------------------
 1 file changed, 49 insertions(+), 36 deletions(-)

diff --git a/common/scsi.c b/common/scsi.c
index 972ef338..c456f5a 100644
--- a/common/scsi.c
+++ b/common/scsi.c
@@ -549,6 +549,52 @@ removable:
  * to the user if mode = 1
  */
 #if defined(CONFIG_DM_SCSI)
+static int do_scsi_scan_one(struct udevice *dev, int id, int lun, int mode)
+{
+	int ret;
+	struct udevice *bdev;
+	struct blk_desc bd;
+	struct blk_desc *bdesc;
+	char str[10];
+
+	/*
+	 * detect the scsi driver to get information about its geometry (block
+	 * size, number of blocks) and other parameters (ids, type, ...)
+	 */
+	scsi_init_dev_desc_priv(&bd);
+	if (scsi_detect_dev(id, lun, &bd))
+		return -ENODEV;
+
+	/*
+	* Create only one block device and do detection
+	* to make sure that there won't be a lot of
+	* block devices created
+	*/
+	snprintf(str, sizeof(str), "id%dlun%d", id, lun);
+	ret = blk_create_devicef(dev, "scsi_blk", str, IF_TYPE_SCSI, -1,
+			bd.blksz, bd.blksz * bd.lba, &bdev);
+	if (ret) {
+		debug("Can't create device\n");
+		return ret;
+	}
+
+	bdesc = dev_get_uclass_platdata(bdev);
+	bdesc->target = id;
+	bdesc->lun = lun;
+	bdesc->removable = bd.removable;
+	bdesc->type = bd.type;
+	memcpy(&bdesc->vendor, &bd.vendor, sizeof(bd.vendor));
+	memcpy(&bdesc->product, &bd.product, sizeof(bd.product));
+	memcpy(&bdesc->revision, &bd.revision,	sizeof(bd.revision));
+	part_init(bdesc);
+
+	if (mode == 1) {
+		printf("  Device %d: ", 0);
+		dev_print(bdesc);
+	}
+	return 0;
+}
+
 int scsi_scan(int mode)
 {
 	unsigned char i, lun;
@@ -576,42 +622,9 @@ int scsi_scan(int mode)
 		/* Get controller platdata */
 		plat = dev_get_platdata(dev);
 
-		for (i = 0; i < plat->max_id; i++) {
-			for (lun = 0; lun < plat->max_lun; lun++) {
-				struct udevice *bdev; /* block device */
-				/* block device description */
-				struct blk_desc *bdesc;
-				char str[10];
-
-				/*
-				 * Create only one block device and do detection
-				 * to make sure that there won't be a lot of
-				 * block devices created
-				 */
-				snprintf(str, sizeof(str), "id%dlun%d", i, lun);
-				ret = blk_create_devicef(dev, "scsi_blk",
-							  str, IF_TYPE_SCSI,
-							  -1, 0, 0, &bdev);
-				if (ret) {
-					debug("Can't create device\n");
-					return ret;
-				}
-				bdesc = dev_get_uclass_platdata(bdev);
-
-				scsi_init_dev_desc_priv(bdesc);
-				ret = scsi_detect_dev(i, lun, bdesc);
-				if (ret) {
-					device_unbind(bdev);
-					continue;
-				}
-				part_init(bdesc);
-
-				if (mode == 1) {
-					printf("  Device %d: ", 0);
-					dev_print(bdesc);
-				} /* if mode */
-			} /* next LUN */
-		}
+		for (i = 0; i < plat->max_id; i++)
+			for (lun = 0; lun < plat->max_lun; lun++)
+				do_scsi_scan_one(dev, i, lun, mode);
 	}
 
 	return 0;
-- 
1.9.1



More information about the U-Boot mailing list