blk: dm: make blk_create_device() take a number of block instead of a size
authorJean-Jacques Hiblot <jjhiblot@ti.com>
Fri, 9 Jun 2017 14:45:18 +0000 (16:45 +0200)
committerSimon Glass <sjg@chromium.org>
Tue, 12 Sep 2017 03:43:58 +0000 (21:43 -0600)
There is an overflow problem when taking the size instead of the number
of blocks in blk_create_device(). This results in a wrong device size: the
device apparent size is its real size  modulo 4GB.
Using the number of blocks instead of the device size fixes the problem and
is more coherent with the internals of the block layer.

Signed-off-by: Jean-Jacques Hiblot <jjhiblot@ti.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
drivers/block/blk-uclass.c
drivers/block/sandbox.c
drivers/scsi/scsi.c
include/blk.h
test/dm/blk.c

index 3c5a87b60a8f385cee652254ddb983f177aa9db4..aee2a50d6269248609ef3a6cef0aae2a07127797 100644 (file)
@@ -546,7 +546,7 @@ static int blk_claim_devnum(enum if_type if_type, int devnum)
 
 int blk_create_device(struct udevice *parent, const char *drv_name,
                      const char *name, int if_type, int devnum, int blksz,
-                     lbaint_t size, struct udevice **devp)
+                     lbaint_t lba, struct udevice **devp)
 {
        struct blk_desc *desc;
        struct udevice *dev;
@@ -567,7 +567,7 @@ int blk_create_device(struct udevice *parent, const char *drv_name,
        desc = dev_get_uclass_platdata(dev);
        desc->if_type = if_type;
        desc->blksz = blksz;
-       desc->lba = size / blksz;
+       desc->lba = lba;
        desc->part_type = PART_TYPE_UNKNOWN;
        desc->bdev = dev;
        desc->devnum = devnum;
@@ -578,7 +578,7 @@ int blk_create_device(struct udevice *parent, const char *drv_name,
 
 int blk_create_devicef(struct udevice *parent, const char *drv_name,
                       const char *name, int if_type, int devnum, int blksz,
-                      lbaint_t size, struct udevice **devp)
+                      lbaint_t lba, struct udevice **devp)
 {
        char dev_name[30], *str;
        int ret;
@@ -589,7 +589,7 @@ int blk_create_devicef(struct udevice *parent, const char *drv_name,
                return -ENOMEM;
 
        ret = blk_create_device(parent, drv_name, str, if_type, devnum,
-                               blksz, size, devp);
+                               blksz, lba, devp);
        if (ret) {
                free(str);
                return ret;
index 34d1c638bc7788755f10b76154e513ae1ec3c806..98df6b33b6e51a2486cee5dd0f00da547540dc2d 100644 (file)
@@ -129,7 +129,7 @@ int host_dev_bind(int devnum, char *filename)
        }
        ret = blk_create_device(gd->dm_root, "sandbox_host_blk", str,
                                IF_TYPE_HOST, devnum, 512,
-                               os_lseek(fd, 0, OS_SEEK_END), &dev);
+                               os_lseek(fd, 0, OS_SEEK_END) / 512, &dev);
        if (ret)
                goto err_file;
        ret = device_probe(dev);
index 1a65a3f9b943b58667da497b5b81526ced1c94e4..df998921f564b1c7885d30dc1e943640b342bf7b 100644 (file)
@@ -580,7 +580,7 @@ static int do_scsi_scan_one(struct udevice *dev, int id, int lun, bool verbose)
        */
        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);
+                       bd.blksz, bd.lba, &bdev);
        if (ret) {
                debug("Can't create device\n");
                return ret;
index 27abfddb94f7ba2ddf9ffd4f1a35a62c7ec1d6e0..1965812a9d5a39f0148ce133a895a69b4058d07b 100644 (file)
@@ -315,12 +315,12 @@ int blk_next_device(struct udevice **devp);
  * @devnum:    Device number, specific to the interface type, or -1 to
  *             allocate the next available number
  * @blksz:     Block size of the device in bytes (typically 512)
- * @size:      Total size of the device in bytes
+ * @lba:       Total number of blocks of the device
  * @devp:      the new device (which has not been probed)
  */
 int blk_create_device(struct udevice *parent, const char *drv_name,
                      const char *name, int if_type, int devnum, int blksz,
-                     lbaint_t size, struct udevice **devp);
+                     lbaint_t lba, struct udevice **devp);
 
 /**
  * blk_create_devicef() - Create a new named block device
@@ -332,12 +332,12 @@ int blk_create_device(struct udevice *parent, const char *drv_name,
  * @devnum:    Device number, specific to the interface type, or -1 to
  *             allocate the next available number
  * @blksz:     Block size of the device in bytes (typically 512)
- * @size:      Total size of the device in bytes
+ * @lba:       Total number of blocks of the device
  * @devp:      the new device (which has not been probed)
  */
 int blk_create_devicef(struct udevice *parent, const char *drv_name,
                       const char *name, int if_type, int devnum, int blksz,
-                      lbaint_t size, struct udevice **devp);
+                      lbaint_t lba, struct udevice **devp);
 
 /**
  * blk_prepare_device() - Prepare a block device for use
index 923e8d95f06f8a4f89cfc63ef1e7178d31d49e94..30d1e6184df1fbbd39af922393076b1041ff04b0 100644 (file)
@@ -23,9 +23,9 @@ static int dm_test_blk_base(struct unit_test_state *uts)
 
        /* Create two, one the parent of the other */
        ut_assertok(blk_create_device(gd->dm_root, "sandbox_host_blk", "test",
-                                     IF_TYPE_HOST, 1, 512, 1024, &blk));
+                                     IF_TYPE_HOST, 1, 512, 2, &blk));
        ut_assertok(blk_create_device(blk, "usb_storage_blk", "test",
-                                     IF_TYPE_USB, 3, 512, 1024, &usb_blk));
+                                     IF_TYPE_USB, 3, 512, 2, &usb_blk));
 
        /* Check we can find them */
        ut_asserteq(-ENODEV, blk_get_device(IF_TYPE_HOST, 0, &dev));
@@ -101,7 +101,7 @@ static int dm_test_blk_find(struct unit_test_state *uts)
        struct udevice *blk, *dev;
 
        ut_assertok(blk_create_device(gd->dm_root, "sandbox_host_blk", "test",
-                                     IF_TYPE_HOST, 1, 512, 1024, &blk));
+                                     IF_TYPE_HOST, 1, 512, 2, &blk));
        ut_asserteq(-ENODEV, blk_find_device(IF_TYPE_HOST, 0, &dev));
        ut_assertok(blk_find_device(IF_TYPE_HOST, 1, &dev));
        ut_asserteq_ptr(blk, dev);