Skip to contentSkip to conversation

blk: sandbox: Support binding a device with a given logical block size

PATCH In review Bin Meng 6 participants 38 messages Sep 26, 2023 – Oct 11, 2023

Subsystems (3)
lore
Share & tools
lore

Bin Meng

author
#0 lore raw

cover blk: sandbox: Support binding a device with a given logical block size

At present on Sandbox when binding to a host backing file, the host block device is created with a hard-coded 512 bytes block size.

Such assumption works for most cases, but for situation that with a raw image file dump from a pre-formatted GPT partitioned disk image from a 4KiB block size device, when binding this file to a host device and mapping this device to a blkmap, "blkmap" command like "blkmap part" won't work correctly, due to block size mismatch during parsing the partition table.

This series updates Sandbox block driver, as well as the blkmap driver, to get rid of the hard-coded 512 bytes block size assumption.

This series is available at u-boot-x86/blk for testing.

Test log (512 block size):

=> host bind 0 test.img
=> host info

  dev       blocks  blksz label           path
    0       262144    512 0               test.img

=> blkmap create 0
Created "0"
=> blkmap map 0 0 40000 linear host 0 0
Block 0x0+0x40000 mapped to block 0x0 of "host 0" => blkmap info
Device 0: Vendor: U-Boot Rev: 1.0 Prod: blkmap
Type: Hard Disk
Capacity: 128.0 MB = 0.1 GB (262144 x 512)
=> blkmap part

Partition Map for BLKMAP device 0 -- Partition Type: EFI

Part Start LBA End LBA Name

          Attributes
          Type GUID
          Partition GUID
    1     0x00000022      0x000000bd      "u-boot-spl"
          attrs:  0x0000000000000000
          type:   5b193300-fc78-40cd-8002-e86c45580b47
                  (5b193300-fc78-40cd-8002-e86c45580b47)
          guid:   0bb6bb6e-4aac-4c27-be03-016b01e7b941
    2     0x00000822      0x00000c84      "u-boot"
          attrs:  0x0000000000000000
          type:   2e54b353-1271-4842-806f-e436d6af6985
                  (2e54b353-1271-4842-806f-e436d6af6985)
          guid:   91d50814-8e31-4cc0-97dc-779e1dc59056
    3     0x00000c85      0x0000cc84      "rootfs"
          attrs:  0x0000000000000004
          type:   0fc63daf-8483-4772-8e79-3d69d8477de4
                  (linux)
          guid:   42799722-6e55-46e6-afa9-529e7af3f03b

Test log (4096 block size):

=> host bind 0 test.img 4096
=> host info
dev blocks blksz label path

    0        32768   4096 0               test.img

=> blkmap create 0
Created "0"
=> blkmap map 0 0 8000 linear host 0 0
Block 0x0+0x8000 mapped to block 0x0 of "host 0" => blkmap info
Device 0: Vendor: U-Boot Rev: 1.0 Prod: blkmap
Type: Hard Disk
Capacity: 128.0 MB = 0.1 GB (32768 x 4096)
=> blkmap part

Partition Map for BLKMAP device 0 -- Partition Type: EFI

Part Start LBA End LBA Name

          Attributes
          Type GUID
          Partition GUID
    1     0x00000100      0x00001fff      "primary"
          attrs:  0x0000000000000000
          type:   0fc63daf-8483-4772-8e79-3d69d8477de4
                  (linux)
          guid:   eba904d7-72c1-4dbd-bb4e-36be49cba5e3
    2     0x00002000      0x00007ffa      "primary"
          attrs:  0x0000000000000000
          type:   0fc63daf-8483-4772-8e79-3d69d8477de4
                  (linux)
          guid:   c48c360e-db47-46da-ab87-26416fad3cd3

Bin Meng (15):
blk: Use a macro for the typical block size
cmd: host: Mandate the filename parameter in the 'bind' command
blk: sandbox: Support binding a device with a given logical block size
blk: host_dev: Make host_sb_detach_file() and host_sb_ops static
blk: host_dev: Sanity check on the size of host backing file
cmd: host: Print out the block size of the host device
blk: blkmap: Make bind/unbind routines static
cmd: blkmap: Make map_handlers[] and its .fn static
blk: blkmap: Support mapping to device of any block size
cmd: blk_common: Use macros for the return values
dm: blk: Rename get_desc() and make it externally visible
cmd: blk_common: Stop using hard-coded block size for Sandbox
operations
dm: blk: Drop blk_{read,write}_devnum()
disk: part: Print out the unknown device uclass id
disk: part: Handle blkmap device in print_part_header()

 cmd/blk_common.c            | 34 +++++++++++++++----------
 cmd/blkmap.c                |  7 ++---
 cmd/host.c                  | 25 +++++++++++++-----
 common/usb_storage.c        |  4 +--
 disk/part.c                 |  5 +++-
 drivers/ata/dwc_ahsata.c    |  3 ++-
 drivers/ata/fsl_sata.c      |  3 ++-
 drivers/ata/sata_mv.c       |  3 ++-
 drivers/ata/sata_sil.c      |  3 ++-
 drivers/block/blk-uclass.c  | 51 +++++--------------------------------
 drivers/block/blkmap.c      | 16 ++++++------
 drivers/block/host-uclass.c | 15 ++++++++---
 drivers/block/host_dev.c    | 11 +++++---
 drivers/mmc/mmc-uclass.c    |  2 +-
 drivers/nvme/nvme.c         |  2 +-
 include/blk.h               | 40 ++++++++++-------------------
 include/sandbox_host.h      |  7 +++--
 test/dm/blk.c               |  7 ++---
 test/dm/host.c              | 26 +++++++++----------
 19 files changed, 129 insertions(+), 135 deletions(-)
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 1/15 blk: Use a macro for the typical block size

Avoid using the magic number 512 directly.

---

 common/usb_storage.c     | 4 ++--
 drivers/ata/dwc_ahsata.c | 3 ++-
 drivers/ata/fsl_sata.c   | 3 ++-
 drivers/ata/sata_mv.c    | 3 ++-
 drivers/ata/sata_sil.c   | 3 ++-
 drivers/block/blkmap.c   | 2 +-
 drivers/block/host_dev.c | 2 +-
 drivers/mmc/mmc-uclass.c | 2 +-
 drivers/nvme/nvme.c      | 2 +-
 include/blk.h            | 2 ++
 10 files changed, 16 insertions(+), 10 deletions(-)
common/usb_storage.c +2 -2
index 85774220ef..35c656db0d 100644
@@ -219,8 +219,8 @@ static int usb_stor_probe_device(struct usb_device *udev)
 
 		snprintf(str, sizeof(str), "lun%d", lun);
 		ret = blk_create_devicef(udev->dev, "usb_storage_blk", str,
-					 UCLASS_USB, usb_max_devs, 512, 0,
-					 &dev);
+					 UCLASS_USB, usb_max_devs,
+					 DEFAULT_BLKSZ, 0, &dev);
 		if (ret) {
 			debug("Cannot bind driver\n");
 			return ret;
drivers/ata/dwc_ahsata.c +2 -1
index 6a4d861bf1..b4d4e39c9b 100644
@@ -880,7 +880,8 @@ int dwc_ahsata_scan(struct udevice *dev)
 	device_find_first_child(dev, &blk);
 	if (!blk) {
 		ret = blk_create_devicef(dev, "dwc_ahsata_blk", "blk",
-					 UCLASS_AHCI, -1, 512, 0, &blk);
+					 UCLASS_AHCI, -1, DEFAULT_BLKSZ,
+					 0, &blk);
 		if (ret) {
 			debug("Can't create device\n");
 			return ret;
drivers/ata/fsl_sata.c +2 -1
index 972101b29c..969bc191f8 100644
@@ -888,7 +888,8 @@ static int fsl_ata_probe(struct udevice *dev)
 	for (i = 0; i < nr_ports; i++) {
 		snprintf(sata_name, sizeof(sata_name), "fsl_sata%d", i);
 		ret = blk_create_devicef(dev, "sata_fsl_blk", sata_name,
-					 UCLASS_AHCI, -1, 512, 0, &blk);
+					 UCLASS_AHCI, -1, DEFAULT_BLKSZ,
+					 0, &blk);
 		if (ret) {
 			debug("Can't create device\n");
 			return ret;
drivers/ata/sata_mv.c +2 -1
index 18c7a66db1..1abea0b309 100644
@@ -1076,7 +1076,8 @@ static int sata_mv_probe(struct udevice *dev)
 
 	for (i = 0; i < nr_ports; i++) {
 		ret = blk_create_devicef(dev, "sata_mv_blk", "blk",
-					 UCLASS_AHCI, -1, 512, 0, &blk);
+					 UCLASS_AHCI, -1, DEFAULT_BLKSZ,
+					 0, &blk);
 		if (ret) {
 			debug("Can't create device\n");
 			continue;
drivers/ata/sata_sil.c +2 -1
index b5e150d568..43a91a7912 100644
@@ -730,7 +730,8 @@ static int sil_pci_probe(struct udevice *dev)
 	for (i = sata_info.portbase; i < sata_info.maxport; i++) {
 		snprintf(sata_name, sizeof(sata_name), "sil_sata%d", i);
 		ret = blk_create_devicef(dev, "sata_sil_blk", sata_name,
-					 UCLASS_AHCI, -1, 512, 0, &blk);
+					 UCLASS_AHCI, -1, DEFAULT_BLKSZ,
+					 0, &blk);
 		if (ret) {
 			debug("Can't create device\n");
 			return ret;
drivers/block/blkmap.c +1 -1
index 2bb0acc20f..409aa46de2 100644
@@ -390,7 +390,7 @@ int blkmap_dev_bind(struct udevice *dev)
 	int err;
 
 	err = blk_create_devicef(dev, "blkmap_blk", "blk", UCLASS_BLKMAP,
-				 dev_seq(dev), 512, 0, &bm->blk);
+				 dev_seq(dev), DEFAULT_BLKSZ, 0, &bm->blk);
 	if (err)
 		return log_msg_ret("blk", err);
 
drivers/block/host_dev.c +1 -1
index 64422417b7..31c7814054 100644
@@ -105,7 +105,7 @@ static int host_sb_bind(struct udevice *dev)
 	int ret;
 
 	ret = blk_create_devicef(dev, "sandbox_host_blk", "blk", UCLASS_HOST,
-				 dev_seq(dev), 512, 0, &blk);
+				 dev_seq(dev), DEFAULT_BLKSZ, 0, &blk);
 	if (ret)
 		return log_msg_ret("blk", ret);
 
drivers/mmc/mmc-uclass.c +1 -1
index 0e157672ea..328456831d 100644
@@ -412,7 +412,7 @@ int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg)
 	debug("%s: alias devnum=%d\n", __func__, dev_seq(dev));
 
 	ret = blk_create_devicef(dev, "mmc_blk", "blk", UCLASS_MMC,
-				 dev_seq(dev), 512, 0, &bdev);
+				 dev_seq(dev), DEFAULT_BLKSZ, 0, &bdev);
 	if (ret) {
 		debug("Cannot create block device\n");
 		return ret;
drivers/nvme/nvme.c +1 -1
index 20dc910d8a..c39cd41aa3 100644
@@ -906,7 +906,7 @@ int nvme_init(struct udevice *udev)
 
 		/* The real blksz and size will be set by nvme_blk_probe() */
 		ret = blk_create_devicef(udev, "nvme-blk", name, UCLASS_NVME,
-					 -1, 512, 0, &ns_udev);
+					 -1, DEFAULT_BLKSZ, 0, &ns_udev);
 		if (ret)
 			goto free_id;
 
include/blk.h +2 -0
index 2c9c7985a8..4a4365fbbf 100644
@@ -20,6 +20,8 @@ typedef ulong lbaint_t;
 #define LBAF "%" LBAFlength "x"
 #define LBAFU "%" LBAFlength "u"
 
+#define DEFAULT_BLKSZ		512
+
 struct udevice;
 
 static inline bool blk_enabled(void)
Signature · 1 line
2.25.1
Reviewed-by on v1 1/15
#2 lore raw

Re: [PATCH 01/15] blk: Use a macro for the typical block size

Quoted 6 lines · Bin Meng

Avoid using the magic number 512 directly.

---

Quoted 11 lines
 common/usb_storage.c     | 4 ++--
 drivers/ata/dwc_ahsata.c | 3 ++-
 drivers/ata/fsl_sata.c   | 3 ++-
 drivers/ata/sata_mv.c    | 3 ++-
 drivers/ata/sata_sil.c   | 3 ++-
 drivers/block/blkmap.c   | 2 +-
 drivers/block/host_dev.c | 2 +-
 drivers/mmc/mmc-uclass.c | 2 +-
 drivers/nvme/nvme.c      | 2 +-
 include/blk.h            | 2 ++
 10 files changed, 16 insertions(+), 10 deletions(-)
author
#3 lore raw

PATCH 2/15 cmd: host: Mandate the filename parameter in the 'bind' command

In reply to Bin Meng

At present the host bind command does not require filename to be provided. When it is not given NULL is passed to the host device driver, which ends up failure afterwards.

Change to mandate the filename so that it is useful.

---

 cmd/host.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
cmd/host.c +3 -3
index fb1cb1fdd1..b924940ffb 100644
@@ -59,10 +59,10 @@ static int do_host_bind(struct cmd_tbl *cmdtp, int flag, int argc,
 		argv++;
 	}
 
-	if (argc > 2)
+	if (argc != 2)
 		return CMD_RET_USAGE;
 	label = argv[0];
-	file = argc > 1 ? argv[1] : NULL;
+	file = argv[1];
 
 	ret = host_create_attach_file(label, file, removable, &dev);
 	if (ret) {
@@ -253,7 +253,7 @@ U_BOOT_CMD(
 	"host save hostfs - <addr> <filename> <bytes> [<offset>] - "
 		"save a file to host\n"
 	"host size hostfs - <filename> - determine size of file on host\n"
-	"host bind [-r] <label> [<filename>] - bind \"host\" device to file\n"
+	"host bind [-r] <label> <filename> - bind \"host\" device to file\n"
 	"     -r = mark as removable\n"
 	"host unbind <label>     - unbind file from \"host\" device\n"
 	"host info [<label>]     - show device binding & info\n"
Signature · 1 line
2.25.1
Reviewed-by on v1 2/15
#4 lore raw

Re: [PATCH 02/15] cmd: host: Mandate the filename parameter in the 'bind' command

Quoted 5 lines · Bin Meng

At present the host bind command does not require filename to be provided. When it is not given NULL is passed to the host device driver, which ends up failure afterwards.

Quoted 8 lines

Change to mandate the filename so that it is useful.

---

 cmd/host.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
author
#5 lore raw

PATCH 3/15 blk: sandbox: Support binding a device with a given logical block size

In reply to Bin Meng

Allow optionally set the logical block size of the host device to bind in the "host bind" command. If not given, defaults to 512.

---

 cmd/host.c                  | 16 +++++++++++++---
 drivers/block/host-uclass.c | 15 ++++++++++++---
 include/sandbox_host.h      |  7 +++++--
 test/dm/blk.c               |  7 ++++---
 test/dm/host.c              |  6 +++---
 5 files changed, 37 insertions(+), 14 deletions(-)
cmd/host.c +13 -3
index b924940ffb..2334ccd9bc 100644
@@ -13,6 +13,7 @@
 #include <dm/device-internal.h>
 #include <dm/uclass-internal.h>
 #include <linux/errno.h>
+#include <linux/log2.h>
 
 static int do_host_load(struct cmd_tbl *cmdtp, int flag, int argc,
 			char *const argv[])
@@ -45,6 +46,7 @@ static int do_host_bind(struct cmd_tbl *cmdtp, int flag, int argc,
 	struct udevice *dev;
 	const char *label;
 	char *file;
+	unsigned long blksz = DEFAULT_BLKSZ;
 	int ret;
 
 	/* Skip 'bind' */
@@ -59,12 +61,19 @@ static int do_host_bind(struct cmd_tbl *cmdtp, int flag, int argc,
 		argv++;
 	}
 
-	if (argc != 2)
+	if (argc < 2 || argc > 3)
 		return CMD_RET_USAGE;
 	label = argv[0];
 	file = argv[1];
+	if (argc > 2) {
+		blksz = dectoul(argv[2], NULL);
+		if (blksz < DEFAULT_BLKSZ || !is_power_of_2(blksz)) {
+			printf("blksz must be >= 512 and power of 2\n");
+			return CMD_RET_FAILURE;
+		}
+	}
 
-	ret = host_create_attach_file(label, file, removable, &dev);
+	ret = host_create_attach_file(label, file, removable, blksz, &dev);
 	if (ret) {
 		printf("Cannot create device / bind file\n");
 		return CMD_RET_FAILURE;
@@ -253,7 +262,8 @@ U_BOOT_CMD(
 	"host save hostfs - <addr> <filename> <bytes> [<offset>] - "
 		"save a file to host\n"
 	"host size hostfs - <filename> - determine size of file on host\n"
-	"host bind [-r] <label> <filename> - bind \"host\" device to file\n"
+	"host bind [-r] <label> <filename> [<blksz>] - bind \"host\" device to file,\n"
+	"     and optionally set the device's logical block size\n"
 	"     -r = mark as removable\n"
 	"host unbind <label>     - unbind file from \"host\" device\n"
 	"host info [<label>]     - show device binding & info\n"
drivers/block/host-uclass.c +12 -3
index 6460d968c2..b3647e3ce3 100644
@@ -13,6 +13,7 @@
 #include <blk.h>
 #include <dm.h>
 #include <malloc.h>
+#include <part.h>
 #include <sandbox_host.h>
 #include <dm/device-internal.h>
 #include <dm/lists.h>
@@ -29,7 +30,8 @@ struct host_priv {
 	struct udevice *cur_dev;
 };
 
-int host_create_device(const char *label, bool removable, struct udevice **devp)
+int host_create_device(const char *label, bool removable, unsigned long blksz,
+		       struct udevice **devp)
 {
 	char dev_name[30], *str, *label_new;
 	struct host_sb_plat *plat;
@@ -68,6 +70,12 @@ int host_create_device(const char *label, bool removable, struct udevice **devp)
 		struct blk_desc *desc = dev_get_uclass_plat(blk);
 
 		desc->removable = removable;
+
+		/* update blk device's block size with the provided one */
+		if (blksz != desc->blksz) {
+			desc->blksz = blksz;
+			desc->log2blksz = LOG2(desc->blksz);
+		}
 	}
 
 	plat = dev_get_plat(dev);
@@ -95,12 +103,13 @@ int host_attach_file(struct udevice *dev, const char *filename)
 }
 
 int host_create_attach_file(const char *label, const char *filename,
-			    bool removable, struct udevice **devp)
+			    bool removable, unsigned long blksz,
+			    struct udevice **devp)
 {
 	struct udevice *dev;
 	int ret;
 
-	ret = host_create_device(label, removable, &dev);
+	ret = host_create_device(label, removable, blksz, &dev);
 	if (ret)
 		return log_msg_ret("cre", ret);
 
include/sandbox_host.h +5 -2
index ebd7d99b47..f7a5fc6723 100644
@@ -74,10 +74,11 @@ int host_detach_file(struct udevice *dev);
  * @label: Label of the attachment, e.g. "test1"
  * @removable: true if the device should be marked as removable, false
  *	if it is fixed. See enum blk_flag_t
+ * @blksz: logical block size of the device
  * @devp: Returns the device created, on success
  * Returns: 0 if OK, -ve on error
  */
-int host_create_device(const char *label, bool removable,
+int host_create_device(const char *label, bool removable, unsigned long blksz,
 		       struct udevice **devp);
 
 /**
@@ -87,11 +88,13 @@ int host_create_device(const char *label, bool removable,
  * @filename: Name of the file, e.g. "/path/to/disk.img"
  * @removable: true if the device should be marked as removable, false
  *	if it is fixed. See enum blk_flag_t
+ * @blksz: logical block size of the device
  * @devp: Returns the device created, on success
  * Returns: 0 if OK, -ve on error
  */
 int host_create_attach_file(const char *label, const char *filename,
-			    bool removable, struct udevice **devp);
+			    bool removable, unsigned long blksz,
+			    struct udevice **devp);
 
 /**
  * host_find_by_label() - Find a host by label
test/dm/blk.c +4 -3
index 446c4423e6..799f1e4dc7 100644
@@ -4,6 +4,7 @@
  */
 
 #include <common.h>
+#include <blk.h>
 #include <dm.h>
 #include <part.h>
 #include <sandbox_host.h>
@@ -22,8 +23,8 @@ static int dm_test_blk_base(struct unit_test_state *uts)
 	struct udevice *blk0, *blk1, *dev0, *dev1, *dev, *chk0, *chk1;
 
 	/* Create two, one the parent of the other */
-	ut_assertok(host_create_device("test0", false, &dev0));
-	ut_assertok(host_create_device("test1", false, &dev1));
+	ut_assertok(host_create_device("test0", false, DEFAULT_BLKSZ, &dev0));
+	ut_assertok(host_create_device("test1", false, DEFAULT_BLKSZ, &dev1));
 
 	/* Check we can find them */
 	ut_assertok(blk_get_device(UCLASS_HOST, 0, &blk0));
@@ -99,7 +100,7 @@ static int dm_test_blk_find(struct unit_test_state *uts)
 {
 	struct udevice *blk, *chk, *dev;
 
-	ut_assertok(host_create_device("test0", false, &dev));
+	ut_assertok(host_create_device("test0", false, DEFAULT_BLKSZ, &dev));
 
 	ut_assertok(blk_find_device(UCLASS_HOST, 0, &chk));
 	ut_assertok(device_find_first_child_by_uclass(dev, UCLASS_BLK, &blk));
test/dm/host.c +3 -3
index 355ba7770a..19f6c67462 100644
@@ -32,7 +32,7 @@ static int dm_test_host(struct unit_test_state *uts)
 	ut_asserteq(-ENODEV, uclass_first_device_err(UCLASS_PARTITION, &part));
 
 	mem_start = ut_check_delta(0);
-	ut_assertok(host_create_device(label, true, &dev));
+	ut_assertok(host_create_device(label, true, DEFAULT_BLKSZ, &dev));
 
 	/* Check that the plat data has been allocated */
 	plat = dev_get_plat(dev);
@@ -81,7 +81,7 @@ static int dm_test_host_dup(struct unit_test_state *uts)
 	struct udevice *dev, *chk;
 
 	ut_asserteq(0, uclass_id_count(UCLASS_HOST));
-	ut_assertok(host_create_device(label, true, &dev));
+	ut_assertok(host_create_device(label, true, DEFAULT_BLKSZ, &dev));
 
 	/* Attach a file created in test_host.py */
 	ut_assertok(host_attach_file(dev, filename));
@@ -90,7 +90,7 @@ static int dm_test_host_dup(struct unit_test_state *uts)
 	ut_asserteq(1, uclass_id_count(UCLASS_HOST));
 
 	/* Create another device with the same label (should remove old one) */
-	ut_assertok(host_create_device(label, true, &dev));
+	ut_assertok(host_create_device(label, true, DEFAULT_BLKSZ, &dev));
 
 	/* Attach a different file created in test_host.py */
 	ut_assertok(host_attach_file(dev, filename2));
Signature · 1 line
2.25.1
author
#6 lore raw

PATCH 4/15 blk: host_dev: Make host_sb_detach_file() and host_sb_ops static

In reply to Bin Meng

They are only used in drivers/block/host_dev.c.

---

 drivers/block/host_dev.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/block/host_dev.c +2 -2
index 31c7814054..0b43f80a86 100644
@@ -73,7 +73,7 @@ err_fname:
 	return ret;
 }
 
-int host_sb_detach_file(struct udevice *dev)
+static int host_sb_detach_file(struct udevice *dev)
 {
 	struct host_sb_plat *plat = dev_get_plat(dev);
 	int ret;
@@ -123,7 +123,7 @@ static int host_sb_bind(struct udevice *dev)
 	return 0;
 }
 
-struct host_ops host_sb_ops = {
+static struct host_ops host_sb_ops = {
 	.attach_file	= host_sb_attach_file,
 	.detach_file	= host_sb_detach_file,
 };
Signature · 1 line
2.25.1
author
#8 lore raw

PATCH 5/15 blk: host_dev: Sanity check on the size of host backing file

In reply to Bin Meng

Since we are emulating a block device, its size should be multiple of the configured block size.

---

 drivers/block/host_dev.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/block/host_dev.c +5 -0
index 0b43f80a86..30c7415793 100644
@@ -58,6 +58,11 @@ static int host_sb_attach_file(struct udevice *dev, const char *filename)
 
 	size = os_filesize(fd);
 	desc = dev_get_uclass_plat(blk);
+	if (size % desc->blksz) {
+		printf("The size of host backing file '%s' is not multiple of "
+		       "the device block size\n", filename);
+		goto err_fname;
+	}
 	desc->lba = size / desc->blksz;
 
 	/* write this in last, when nothing can go wrong */
Signature · 1 line
2.25.1
Reviewed-by on v1 5/15
#9 lore raw

Re: [PATCH 05/15] blk: host_dev: Sanity check on the size of host backing file

Quoted 10 lines · Bin Meng

Since we are emulating a block device, its size should be multiple of the configured block size.

---

 drivers/block/host_dev.c | 5 +++++
 1 file changed, 5 insertions(+)
author
#10 lore raw

PATCH 6/15 cmd: host: Print out the block size of the host device

In reply to Bin Meng

It's useful if we can print out the block size of the host device in the "host info" command.

---

 cmd/host.c     |  7 ++++---
 test/dm/host.c | 20 ++++++++++----------
 2 files changed, 14 insertions(+), 13 deletions(-)
cmd/host.c +4 -3
index 2334ccd9bc..c33c2a9787 100644
@@ -160,8 +160,8 @@ static void show_host_dev(struct udevice *dev)
 		return;
 
 	desc = dev_get_uclass_plat(blk);
-	printf("%12lu %-15s %s\n", (unsigned long)desc->lba, plat->label,
-	       plat->filename);
+	printf("%12lu %6lu %-15s %s\n", (unsigned long)desc->lba, desc->blksz,
+	       plat->label, plat->filename);
 }
 
 static int do_host_info(struct cmd_tbl *cmdtp, int flag, int argc,
@@ -179,7 +179,8 @@ static int do_host_info(struct cmd_tbl *cmdtp, int flag, int argc,
 			return CMD_RET_FAILURE;
 	}
 
-	printf("%3s %12s %-15s %s\n", "dev", "blocks", "label", "path");
+	printf("%3s %12s %6s %-15s %s\n",
+	       "dev", "blocks", "blksz", "label", "path");
 	if (dev) {
 		show_host_dev(dev);
 	} else {
test/dm/host.c +10 -10
index 19f6c67462..580b14cf79 100644
@@ -114,7 +114,7 @@ static int dm_test_cmd_host(struct unit_test_state *uts)
 
 	/* first check 'host info' with binding */
 	ut_assertok(run_command("host info", 0));
-	ut_assert_nextline("dev       blocks label           path");
+	ut_assert_nextline("dev       blocks  blksz label           path");
 	ut_assert_console_end();
 
 	ut_assertok(run_commandf("host bind -r test2 %s", filename));
@@ -126,8 +126,8 @@ static int dm_test_cmd_host(struct unit_test_state *uts)
 	ut_asserteq(true, desc->removable);
 
 	ut_assertok(run_command("host info", 0));
-	ut_assert_nextline("dev       blocks label           path");
-	ut_assert_nextline("  0         4096 test2           2MB.ext2.img");
+	ut_assert_nextline("dev       blocks  blksz label           path");
+	ut_assert_nextline("  0         4096    512 test2           2MB.ext2.img");
 	ut_assert_console_end();
 
 	ut_assertok(run_commandf("host bind fat %s", filename2));
@@ -139,9 +139,9 @@ static int dm_test_cmd_host(struct unit_test_state *uts)
 	ut_asserteq(false, desc->removable);
 
 	ut_assertok(run_command("host info", 0));
-	ut_assert_nextline("dev       blocks label           path");
-	ut_assert_nextline("  0         4096 test2           2MB.ext2.img");
-	ut_assert_nextline("  1         2048 fat             1MB.fat32.img");
+	ut_assert_nextline("dev       blocks  blksz label           path");
+	ut_assert_nextline("  0         4096    512 test2           2MB.ext2.img");
+	ut_assert_nextline("  1         2048    512 fat             1MB.fat32.img");
 	ut_assert_console_end();
 
 	ut_asserteq(1, run_command("host info test", 0));
@@ -149,8 +149,8 @@ static int dm_test_cmd_host(struct unit_test_state *uts)
 	ut_assert_console_end();
 
 	ut_assertok(run_command("host info fat", 0));
-	ut_assert_nextline("dev       blocks label           path");
-	ut_assert_nextline("  1         2048 fat             1MB.fat32.img");
+	ut_assert_nextline("dev       blocks  blksz label           path");
+	ut_assert_nextline("  1         2048    512 fat             1MB.fat32.img");
 	ut_assert_console_end();
 
 	/* check 'host dev' */
@@ -186,8 +186,8 @@ static int dm_test_cmd_host(struct unit_test_state *uts)
 	ut_assert_console_end();
 
 	ut_assertok(run_command("host info", 0));
-	ut_assert_nextline("dev       blocks label           path");
-	ut_assert_nextline("  1         2048 fat             1MB.fat32.img");
+	ut_assert_nextline("dev       blocks  blksz label           path");
+	ut_assert_nextline("  1         2048    512 fat             1MB.fat32.img");
 	ut_assert_console_end();
 
 	return 0;
Signature · 1 line
2.25.1
Reviewed-by on v1 6/15
#11 lore raw

Re: [PATCH 06/15] cmd: host: Print out the block size of the host device

Quoted 11 lines · Bin Meng

It's useful if we can print out the block size of the host device in the "host info" command.

---

 cmd/host.c     |  7 ++++---
 test/dm/host.c | 20 ++++++++++----------
 2 files changed, 14 insertions(+), 13 deletions(-)
author
#12 lore raw

PATCH 7/15 blk: blkmap: Make bind/unbind routines static

In reply to Bin Meng

These 2 are only used in drivers/block/blkmap.c.

---

 drivers/block/blkmap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/block/blkmap.c +2 -2
index 409aa46de2..f6acfa8927 100644
@@ -383,7 +383,7 @@ U_BOOT_DRIVER(blkmap_blk) = {
 	.ops		= &blkmap_blk_ops,
 };
 
-int blkmap_dev_bind(struct udevice *dev)
+static int blkmap_dev_bind(struct udevice *dev)
 {
 	struct blkmap *bm = dev_get_plat(dev);
 	struct blk_desc *bd;
@@ -410,7 +410,7 @@ int blkmap_dev_bind(struct udevice *dev)
 	return 0;
 }
 
-int blkmap_dev_unbind(struct udevice *dev)
+static int blkmap_dev_unbind(struct udevice *dev)
 {
 	struct blkmap *bm = dev_get_plat(dev);
 	struct blkmap_slice *bms, *tmp;
Signature · 1 line
2.25.1
author
#14 lore raw

PATCH 8/15 cmd: blkmap: Make map_handlers[] and its .fn static

In reply to Bin Meng

These are only used in cmd/blkmap.c.

---

 cmd/blkmap.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
cmd/blkmap.c +4 -3
index b34c013072..ef74ebc003 100644
@@ -25,7 +25,8 @@ struct map_handler {
 	map_parser_fn fn;
 };
 
-int do_blkmap_map_linear(struct map_ctx *ctx, int argc, char *const argv[])
+static int do_blkmap_map_linear(struct map_ctx *ctx, int argc,
+				char *const argv[])
 {
 	struct blk_desc *lbd;
 	int err, ldevnum;
@@ -58,7 +59,7 @@ int do_blkmap_map_linear(struct map_ctx *ctx, int argc, char *const argv[])
 	return CMD_RET_SUCCESS;
 }
 
-int do_blkmap_map_mem(struct map_ctx *ctx, int argc, char *const argv[])
+static int do_blkmap_map_mem(struct map_ctx *ctx, int argc, char *const argv[])
 {
 	phys_addr_t addr;
 	int err;
@@ -80,7 +81,7 @@ int do_blkmap_map_mem(struct map_ctx *ctx, int argc, char *const argv[])
 	return CMD_RET_SUCCESS;
 }
 
-struct map_handler map_handlers[] = {
+static struct map_handler map_handlers[] = {
 	{ .name = "linear", .fn = do_blkmap_map_linear },
 	{ .name = "mem", .fn = do_blkmap_map_mem },
 
Signature · 1 line
2.25.1
author
#16 lore raw

PATCH 9/15 blk: blkmap: Support mapping to device of any block size

In reply to Bin Meng

At present if a device to map has a block size other than 512, the blkmap map process just fails. There is no reason why we can't just use the block size of the mapped device.

---

 drivers/block/blkmap.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/block/blkmap.c +5 -5
index f6acfa8927..149a4cac3e 100644
@@ -171,11 +171,11 @@ int blkmap_map_linear(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
 
 	bd = dev_get_uclass_plat(bm->blk);
 	lbd = dev_get_uclass_plat(lblk);
-	if (lbd->blksz != bd->blksz)
-		/* We could support block size translation, but we
-		 * don't yet.
-		 */
-		return -EINVAL;
+	if (lbd->blksz != bd->blksz) {
+		/* update to match the mapped device */
+		bd->blksz = lbd->blksz;
+		bd->log2blksz = LOG2(bd->blksz);
+	}
 
 	linear = malloc(sizeof(*linear));
 	if (!linear)
Signature · 1 line
2.25.1
#17 lore raw

Re: [PATCH 09/15] blk: blkmap: Support mapping to device of any block size

Quoted 3 lines · Bin Meng

At present if a device to map has a block size other than 512, the blkmap map process just fails. There is no reason why we can't just use the block size of the mapped device.

Won't this be very confusing to the user?

The blkmap device uses a fixed block size of 512:

https://source.denx.de/u-boot/u-boot/-/blob/master/drivers/block/blkmap.c?ref_type=heads#L393

So if I map a slice of a 4k device into a blkmap, then

    blkmap read 0x80000000 0 1

would copy 4k instead of 512 bytes from the lower device to 0x80000000, even though the blkmap reports a block size of 512.

It seems to me that the expected behavior would be that only the first 512 bytes would be copied in the command above.

Quoted 12 lines

---

 drivers/block/blkmap.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/block/blkmap.c +0 -0
index f6acfa8927..149a4cac3e 100644

drivers/block/blkmap.c int blkmap_map_linear(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, · line 177

Quoted 7 lines
drivers/block/blkmap.c +0 -4
@@ -171,11 +171,11 @@ int blkmap_map_linear(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
 
 	bd = dev_get_uclass_plat(bm->blk);
 	lbd = dev_get_uclass_plat(lblk);
-	if (lbd->blksz != bd->blksz)
-		/* We could support block size translation, but we
-		 * don't yet.
-		 */

Hence this comment ^

Quoted 11 lines
drivers/block/blkmap.c +5 -1
-		return -EINVAL;
+	if (lbd->blksz != bd->blksz) {
+		/* update to match the mapped device */
+		bd->blksz = lbd->blksz;
+		bd->log2blksz = LOG2(bd->blksz);
+	}
 
 	linear = malloc(sizeof(*linear));
 	if (!linear)
Signature · 1 line
2.25.1
#18 lore raw

Re: [PATCH 09/15] blk: blkmap: Support mapping to device of any block size

Hi Tobias,

Quoted 7 lines · Tobias Waldekranz
Quoted 3 lines · Bin Meng

At present if a device to map has a block size other than 512, the blkmap map process just fails. There is no reason why we can't just use the block size of the mapped device.

Won't this be very confusing to the user?

I don't see any confusion.

Quoted 4 lines

The blkmap device uses a fixed block size of 512:

https://source.denx.de/u-boot/u-boot/-/blob/master/drivers/block/blkmap.c?ref_type=heads#L393

Yes, the blkmap device was originally created with a fixed block size of 512, and that's fine.

Quoted 10 lines

So if I map a slice of a 4k device into a blkmap, then

    blkmap read 0x80000000 0 1

would copy 4k instead of 512 bytes from the lower device to 0x80000000, even though the blkmap reports a block size of 512.

It seems to me that the expected behavior would be that only the first 512 bytes would be copied in the command above.

No, the blkmap block size was later updated to match the real underlying device parameter during the map process. So it will copy all 4k to 0x80000000.

Quoted 14 lines
Quoted 14 lines

---

 drivers/block/blkmap.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/block/blkmap.c +0 -0
index f6acfa8927..149a4cac3e 100644
@@ -171,11 +171,11 @@ int blkmap_map_linear(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
 
Quoted 8 lines

drivers/block/blkmap.c int blkmap_map_linear(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, · line 177

Quoted 6 lines
drivers/block/blkmap.c +0 -4
      bd = dev_get_uclass_plat(bm->blk);
      lbd = dev_get_uclass_plat(lblk);
-     if (lbd->blksz != bd->blksz)
-             /* We could support block size translation, but we
-              * don't yet.
-              */

Hence this comment ^

This comment was completely removed with the new updates. There is no need to do any block size translation. We could just use whatever block size the lower device is using, hence this patch.

Quoted 11 lines

drivers/block/blkmap.c int blkmap_map_linear(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,

Quoted 11 lines
drivers/block/blkmap.c +5 -2
-             return -EINVAL;
+     if (lbd->blksz != bd->blksz) {
+             /* update to match the mapped device */
+             bd->blksz = lbd->blksz;
+             bd->log2blksz = LOG2(bd->blksz);
+     }
 
      linear = malloc(sizeof(*linear));
      if (!linear)
--

Regards,
Bin

Reviewed-by on v1 9/15
#19 lore raw

Re: [PATCH 09/15] blk: blkmap: Support mapping to device of any block size

In reply to Bin Meng
Quoted 10 lines · Bin Meng

At present if a device to map has a block size other than 512, the blkmap map process just fails. There is no reason why we can't just use the block size of the mapped device.

---

 drivers/block/blkmap.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
author
#20 lore raw

PATCH 10/15 cmd: blk_common: Use macros for the return values

In reply to Bin Meng

Avoid using magic number 0/1 for the command result.

---

 cmd/blk_common.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
cmd/blk_common.c +7 -7
index 9f9d4327a9..ad9b16dc09 100644
@@ -25,18 +25,18 @@ int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
 	case 2:
 		if (strncmp(argv[1], "inf", 3) == 0) {
 			blk_list_devices(uclass_id);
-			return 0;
+			return CMD_RET_SUCCESS;
 		} else if (strncmp(argv[1], "dev", 3) == 0) {
 			if (blk_print_device_num(uclass_id, *cur_devnump)) {
 				printf("\nno %s devices available\n", if_name);
 				return CMD_RET_FAILURE;
 			}
-			return 0;
+			return CMD_RET_SUCCESS;
 		} else if (strncmp(argv[1], "part", 4) == 0) {
 			if (blk_list_part(uclass_id))
 				printf("\nno %s partition table available\n",
 				       if_name);
-			return 0;
+			return CMD_RET_SUCCESS;
 		}
 		return CMD_RET_USAGE;
 	case 3:
@@ -49,7 +49,7 @@ int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
 			} else {
 				return CMD_RET_FAILURE;
 			}
-			return 0;
+			return CMD_RET_SUCCESS;
 		} else if (strncmp(argv[1], "part", 4) == 0) {
 			int dev = (int)dectoul(argv[2], NULL);
 
@@ -58,7 +58,7 @@ int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
 				       if_name, dev);
 				return CMD_RET_FAILURE;
 			}
-			return 0;
+			return CMD_RET_SUCCESS;
 		}
 		return CMD_RET_USAGE;
 
@@ -80,7 +80,7 @@ int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
 
 			printf("%ld blocks read: %s\n", n,
 			       n == cnt ? "OK" : "ERROR");
-			return n == cnt ? 0 : 1;
+			return n == cnt ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
 		} else if (strcmp(argv[1], "write") == 0) {
 			phys_addr_t paddr = hextoul(argv[2], NULL);
 			lbaint_t blk = hextoul(argv[3], NULL);
@@ -98,7 +98,7 @@ int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
 
 			printf("%ld blocks written: %s\n", n,
 			       n == cnt ? "OK" : "ERROR");
-			return n == cnt ? 0 : 1;
+			return n == cnt ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
 		} else {
 			return CMD_RET_USAGE;
 		}
Signature · 1 line
2.25.1
#21 lore raw

Re: [PATCH 10/15] cmd: blk_common: Use macros for the return values

Hi Bin,

Quoted 14 lines · Bin Meng

Avoid using magic number 0/1 for the command result.

---

 cmd/blk_common.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
cmd/blk_common.c +0 -0
index 9f9d4327a9..ad9b16dc09 100644

cmd/blk_common.c int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id, · line 28

Quoted 5 lines
cmd/blk_common.c +1 -1
@@ -25,18 +25,18 @@ int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
        case 2:
                if (strncmp(argv[1], "inf", 3) == 0) {
                        blk_list_devices(uclass_id);
-                       return 0;
+                       return CMD_RET_SUCCESS;

I really don't like this...0 is success.

Quoted 35 lines
cmd/blk_common.c +4 -4
                } else if (strncmp(argv[1], "dev", 3) == 0) {
                        if (blk_print_device_num(uclass_id, *cur_devnump)) {
                                printf("\nno %s devices available\n", if_name);
                                return CMD_RET_FAILURE;
                        }
-                       return 0;
+                       return CMD_RET_SUCCESS;
                } else if (strncmp(argv[1], "part", 4) == 0) {
                        if (blk_list_part(uclass_id))
                                printf("\nno %s partition table available\n",
                                       if_name);
-                       return 0;
+                       return CMD_RET_SUCCESS;
                }
                return CMD_RET_USAGE;
        case 3:
@@ -49,7 +49,7 @@ int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
                        } else {
                                return CMD_RET_FAILURE;
                        }
-                       return 0;
+                       return CMD_RET_SUCCESS;
                } else if (strncmp(argv[1], "part", 4) == 0) {
                        int dev = (int)dectoul(argv[2], NULL);
 
@@ -58,7 +58,7 @@ int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
                                       if_name, dev);
                                return CMD_RET_FAILURE;
                        }
-                       return 0;
+                       return CMD_RET_SUCCESS;
                }
                return CMD_RET_USAGE;
 

cmd/blk_common.c int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id, · line 83

Quoted 5 lines
cmd/blk_common.c +1 -1
@@ -80,7 +80,7 @@ int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
 
                        printf("%ld blocks read: %s\n", n,
                               n == cnt ? "OK" : "ERROR");
-                       return n == cnt ? 0 : 1;
+                       return n == cnt ? CMD_RET_SUCCESS : CMD_RET_FAILURE;

CMD_RET_FAILURE is OK, but I would prefer not to use CMD_RET_SUCCESS. It is 0 and always will be.

It encourages people to do things like:

if (ret == CMD_RET_SUCCESS)

instead of

if (!ret)

It would eventually creep into everything, including our clean error handling.

Quoted 13 lines
cmd/blk_common.c +1 -2
                } else if (strcmp(argv[1], "write") == 0) {
                        phys_addr_t paddr = hextoul(argv[2], NULL);
                        lbaint_t blk = hextoul(argv[3], NULL);
@@ -98,7 +98,7 @@ int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
 
                        printf("%ld blocks written: %s\n", n,
                               n == cnt ? "OK" : "ERROR");
-                       return n == cnt ? 0 : 1;
+                       return n == cnt ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
                } else {
                        return CMD_RET_USAGE;
                }
--
Quoted 2 lines

2.25.1

Regards,
Simon

#22 lore raw

Re: [PATCH 10/15] cmd: blk_common: Use macros for the return values

Hi Simon,

Quoted 70 lines · Simon Glass

Hi Bin,

Quoted 14 lines · Bin Meng

Avoid using magic number 0/1 for the command result.

---

 cmd/blk_common.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
cmd/blk_common.c +0 -0
index 9f9d4327a9..ad9b16dc09 100644

cmd/blk_common.c int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id, · line 28

Quoted 5 lines
cmd/blk_common.c +1 -1
@@ -25,18 +25,18 @@ int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
        case 2:
                if (strncmp(argv[1], "inf", 3) == 0) {
                        blk_list_devices(uclass_id);
-                       return 0;
+                       return CMD_RET_SUCCESS;

I really don't like this...0 is success.

Quoted 35 lines
cmd/blk_common.c +4 -4
                } else if (strncmp(argv[1], "dev", 3) == 0) {
                        if (blk_print_device_num(uclass_id, *cur_devnump)) {
                                printf("\nno %s devices available\n", if_name);
                                return CMD_RET_FAILURE;
                        }
-                       return 0;
+                       return CMD_RET_SUCCESS;
                } else if (strncmp(argv[1], "part", 4) == 0) {
                        if (blk_list_part(uclass_id))
                                printf("\nno %s partition table available\n",
                                       if_name);
-                       return 0;
+                       return CMD_RET_SUCCESS;
                }
                return CMD_RET_USAGE;
        case 3:
@@ -49,7 +49,7 @@ int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
                        } else {
                                return CMD_RET_FAILURE;
                        }
-                       return 0;
+                       return CMD_RET_SUCCESS;
                } else if (strncmp(argv[1], "part", 4) == 0) {
                        int dev = (int)dectoul(argv[2], NULL);
 
@@ -58,7 +58,7 @@ int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
                                       if_name, dev);
                                return CMD_RET_FAILURE;
                        }
-                       return 0;
+                       return CMD_RET_SUCCESS;
                }
                return CMD_RET_USAGE;
 

cmd/blk_common.c int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id, · line 83

Quoted 5 lines
cmd/blk_common.c +1 -1
@@ -80,7 +80,7 @@ int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
 
                        printf("%ld blocks read: %s\n", n,
                               n == cnt ? "OK" : "ERROR");
-                       return n == cnt ? 0 : 1;
+                       return n == cnt ? CMD_RET_SUCCESS : CMD_RET_FAILURE;

CMD_RET_FAILURE is OK, but I would prefer not to use CMD_RET_SUCCESS. It is 0 and always will be.

Quoted 7 lines

It encourages people to do things like:

if (ret == CMD_RET_SUCCESS)

instead of

if (!ret)

I see your concern. However we don't change the return value type to enum, so people can still use

    if (!ret)

I would still defend that we should use CMD_RET_SUCCESS.

This is like EXIT_XXX defined in stdlib.h:

#define   EXIT_FAILURE   1    /* Failing exit status.  */
#define   EXIT_SUCCESS   0    /* Successful exit status.  */

One should use predefined macros whenever possible.

Quoted 8 lines

It would eventually creep into everything, including our clean error handling.

Quoted 5 lines
cmd/blk_common.c +0 -0
                } else if (strcmp(argv[1], "write") == 0) {
                        phys_addr_t paddr = hextoul(argv[2], NULL);
                        lbaint_t blk = hextoul(argv[3], NULL);
@@ -98,7 +98,7 @@ int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
 
Quoted 8 lines

cmd/blk_common.c int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,

Quoted 8 lines
cmd/blk_common.c +1 -2
                        printf("%ld blocks written: %s\n", n,
                               n == cnt ? "OK" : "ERROR");
-                       return n == cnt ? 0 : 1;
+                       return n == cnt ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
                } else {
                        return CMD_RET_USAGE;
                }
--

Regards,
Bin

#23 lore raw

Re: [PATCH 10/15] cmd: blk_common: Use macros for the return values

Hi Bin,

Quoted 14 lines · Bin Meng

Hi Simon,

Quoted 10 lines · Simon Glass

Hi Bin,

Quoted 6 lines · Bin Meng

Avoid using magic number 0/1 for the command result.

---

Quoted 8 lines
Quoted 8 lines

cmd/blk_common.c int blk_common_cmd(int argc, char *const argv[],

Quoted 8 lines
 cmd/blk_common.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
cmd/blk_common.c +0 -0
index 9f9d4327a9..ad9b16dc09 100644
@@ -25,18 +25,18 @@ int blk_common_cmd(int argc, char *const argv[],

enum uclass_id uclass_id,

Quoted 10 lines
Quoted 10 lines

cmd/blk_common.c int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id, · line 28

Quoted 5 lines
cmd/blk_common.c +1 -1
        case 2:
                if (strncmp(argv[1], "inf", 3) == 0) {
                        blk_list_devices(uclass_id);
-                       return 0;
+                       return CMD_RET_SUCCESS;

I really don't like this...0 is success.

Quoted 2 lines
cmd/blk_common.c +0 -0
                } else if (strncmp(argv[1], "dev", 3) == 0) {
                        if (blk_print_device_num(uclass_id,

*cur_devnump)) {

Quoted 1 line
Quoted 1 line

cmd/blk_common.c int blk_common_cmd(int argc, char *const argv[], · line 31

Quoted 1 line
cmd/blk_common.c +0 -0
                                printf("\nno %s devices available\n",

if_name);

Quoted 7 lines
Quoted 7 lines

cmd/blk_common.c int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,

Quoted 7 lines
cmd/blk_common.c +1 -1
                                return CMD_RET_FAILURE;
                        }
-                       return 0;
+                       return CMD_RET_SUCCESS;
                } else if (strncmp(argv[1], "part", 4) == 0) {
                        if (blk_list_part(uclass_id))
                                printf("\nno %s partition table

available\n",

Quoted 7 lines
Quoted 7 lines

cmd/blk_common.c int blk_common_cmd(int argc, char *const argv[],

Quoted 7 lines
cmd/blk_common.c +1 -1
                                       if_name);
-                       return 0;
+                       return CMD_RET_SUCCESS;
                }
                return CMD_RET_USAGE;
        case 3:
@@ -49,7 +49,7 @@ int blk_common_cmd(int argc, char *const argv[],

enum uclass_id uclass_id,

Quoted 9 lines
Quoted 9 lines

cmd/blk_common.c int blk_common_cmd(int argc, char *const argv[],

Quoted 9 lines
cmd/blk_common.c +1 -1
                        } else {
                                return CMD_RET_FAILURE;
                        }
-                       return 0;
+                       return CMD_RET_SUCCESS;
                } else if (strncmp(argv[1], "part", 4) == 0) {
                        int dev = (int)dectoul(argv[2], NULL);
 
@@ -58,7 +58,7 @@ int blk_common_cmd(int argc, char *const argv[],

enum uclass_id uclass_id,

Quoted 9 lines
Quoted 9 lines

cmd/blk_common.c int blk_common_cmd(int argc, char *const argv[],

Quoted 9 lines
cmd/blk_common.c +1 -1
                                       if_name, dev);
                                return CMD_RET_FAILURE;
                        }
-                       return 0;
+                       return CMD_RET_SUCCESS;
                }
                return CMD_RET_USAGE;
 
@@ -80,7 +80,7 @@ int blk_common_cmd(int argc, char *const argv[],

enum uclass_id uclass_id,

Quoted 5 lines
Quoted 5 lines

cmd/blk_common.c int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,

Quoted 5 lines
cmd/blk_common.c +1 -1
                        printf("%ld blocks read: %s\n", n,
                               n == cnt ? "OK" : "ERROR");
-                       return n == cnt ? 0 : 1;
+                       return n == cnt ? CMD_RET_SUCCESS :

CMD_RET_FAILURE;

Quoted 17 lines
Quoted 11 lines

CMD_RET_FAILURE is OK, but I would prefer not to use CMD_RET_SUCCESS. It is 0 and always will be.

It encourages people to do things like:

if (ret == CMD_RET_SUCCESS)

instead of

if (!ret)

I see your concern. However we don't change the return value type to enum, so people can still use

    if (!ret)
Quoted 8 lines

I would still defend that we should use CMD_RET_SUCCESS.

This is like EXIT_XXX defined in stdlib.h:

#define   EXIT_FAILURE   1    /* Failing exit status.  */
#define   EXIT_SUCCESS   0    /* Successful exit status.  */

One should use predefined macros whenever possible.

I agree except for success, where I don't, sorry. It should always be 0 in U-Boot.

People then have to look up the value and also we get things like

if (ret != CMD_RET_SUCCESS)

It is a slippery and I would rather not start down it.

Quoted 3 lines
Quoted 3 lines

It would eventually creep into everything, including our clean error

handling.

Quoted 5 lines
Quoted 5 lines

cmd/blk_common.c int blk_common_cmd(int argc, char *const argv[],

Quoted 5 lines
cmd/blk_common.c +0 -0
                } else if (strcmp(argv[1], "write") == 0) {
                        phys_addr_t paddr = hextoul(argv[2], NULL);
                        lbaint_t blk = hextoul(argv[3], NULL);
@@ -98,7 +98,7 @@ int blk_common_cmd(int argc, char *const argv[],

enum uclass_id uclass_id,

Quoted 5 lines
Quoted 5 lines

cmd/blk_common.c int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,

Quoted 5 lines
cmd/blk_common.c +1 -1
                        printf("%ld blocks written: %s\n", n,
                               n == cnt ? "OK" : "ERROR");
-                       return n == cnt ? 0 : 1;
+                       return n == cnt ? CMD_RET_SUCCESS :

CMD_RET_FAILURE;

Quoted 4 lines
Quoted 4 lines

cmd/blk_common.c int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,

Quoted 4 lines
cmd/blk_common.c +0 -1
                } else {
                        return CMD_RET_USAGE;
                }
--

Regards,

Reviewed-by
#24 lore raw

Re: [PATCH 10/15] cmd: blk_common: Use macros for the return values

Quoted 107 lines · Simon Glass

Hi Bin,

Quoted 14 lines · Bin Meng

Hi Simon,

Quoted 10 lines · Simon Glass

Hi Bin,

Quoted 6 lines · Bin Meng

Avoid using magic number 0/1 for the command result.

---

Quoted 8 lines
Quoted 8 lines

cmd/blk_common.c int blk_common_cmd(int argc, char *const argv[],

Quoted 8 lines
 cmd/blk_common.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
cmd/blk_common.c +0 -0
index 9f9d4327a9..ad9b16dc09 100644
@@ -25,18 +25,18 @@ int blk_common_cmd(int argc, char *const argv[],

enum uclass_id uclass_id,

Quoted 10 lines
Quoted 10 lines

cmd/blk_common.c int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id, · line 28

Quoted 5 lines
cmd/blk_common.c +1 -1
        case 2:
                if (strncmp(argv[1], "inf", 3) == 0) {
                        blk_list_devices(uclass_id);
-                       return 0;
+                       return CMD_RET_SUCCESS;

I really don't like this...0 is success.

Quoted 2 lines
cmd/blk_common.c +0 -0
                } else if (strncmp(argv[1], "dev", 3) == 0) {
                        if (blk_print_device_num(uclass_id,

*cur_devnump)) {

Quoted 1 line
Quoted 1 line

cmd/blk_common.c int blk_common_cmd(int argc, char *const argv[], · line 31

Quoted 1 line
cmd/blk_common.c +0 -0
                                printf("\nno %s devices available\n",

if_name);

Quoted 7 lines
Quoted 7 lines

cmd/blk_common.c int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,

Quoted 7 lines
cmd/blk_common.c +1 -1
                                return CMD_RET_FAILURE;
                        }
-                       return 0;
+                       return CMD_RET_SUCCESS;
                } else if (strncmp(argv[1], "part", 4) == 0) {
                        if (blk_list_part(uclass_id))
                                printf("\nno %s partition table

available\n",

Quoted 7 lines
Quoted 7 lines

cmd/blk_common.c int blk_common_cmd(int argc, char *const argv[],

Quoted 7 lines
cmd/blk_common.c +1 -1
                                       if_name);
-                       return 0;
+                       return CMD_RET_SUCCESS;
                }
                return CMD_RET_USAGE;
        case 3:
@@ -49,7 +49,7 @@ int blk_common_cmd(int argc, char *const argv[],

enum uclass_id uclass_id,

Quoted 9 lines
Quoted 9 lines

cmd/blk_common.c int blk_common_cmd(int argc, char *const argv[],

Quoted 9 lines
cmd/blk_common.c +1 -1
                        } else {
                                return CMD_RET_FAILURE;
                        }
-                       return 0;
+                       return CMD_RET_SUCCESS;
                } else if (strncmp(argv[1], "part", 4) == 0) {
                        int dev = (int)dectoul(argv[2], NULL);
 
@@ -58,7 +58,7 @@ int blk_common_cmd(int argc, char *const argv[],

enum uclass_id uclass_id,

Quoted 9 lines
Quoted 9 lines

cmd/blk_common.c int blk_common_cmd(int argc, char *const argv[],

Quoted 9 lines
cmd/blk_common.c +1 -1
                                       if_name, dev);
                                return CMD_RET_FAILURE;
                        }
-                       return 0;
+                       return CMD_RET_SUCCESS;
                }
                return CMD_RET_USAGE;
 
@@ -80,7 +80,7 @@ int blk_common_cmd(int argc, char *const argv[],

enum uclass_id uclass_id,

Quoted 5 lines
Quoted 5 lines

cmd/blk_common.c int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,

Quoted 5 lines
cmd/blk_common.c +1 -1
                        printf("%ld blocks read: %s\n", n,
                               n == cnt ? "OK" : "ERROR");
-                       return n == cnt ? 0 : 1;
+                       return n == cnt ? CMD_RET_SUCCESS :

CMD_RET_FAILURE;

Quoted 17 lines
Quoted 11 lines

CMD_RET_FAILURE is OK, but I would prefer not to use CMD_RET_SUCCESS. It is 0 and always will be.

It encourages people to do things like:

if (ret == CMD_RET_SUCCESS)

instead of

if (!ret)

I see your concern. However we don't change the return value type to enum, so people can still use

    if (!ret)
Quoted 8 lines

I would still defend that we should use CMD_RET_SUCCESS.

This is like EXIT_XXX defined in stdlib.h:

#define   EXIT_FAILURE   1    /* Failing exit status.  */
#define   EXIT_SUCCESS   0    /* Successful exit status.  */

One should use predefined macros whenever possible.

Quoted 8 lines

I agree except for success, where I don't, sorry. It should always be 0 in U-Boot.

People then have to look up the value and also we get things like

if (ret != CMD_RET_SUCCESS)

It is a slippery and I would rather not start down it.

To be clear, we have a large amount of code today that uses CMD_RET_SUCCESS in the way this patch converts to. We do have a few cases even of the kind of code you worry about seeing, but I think in context clearer (it's in security or OTP fuse contexts) and the compiler will do the right thing regardless.

Signature · 1 line
Tom
author
#25 lore raw

PATCH 11/15 dm: blk: Rename get_desc() and make it externally visible

In reply to Bin Meng

get_desc() can be useful outside blk-uclass.c. Let's change it to an API and make it externally visible.

---

 drivers/block/blk-uclass.c | 26 ++++++++------------------
 include/blk.h              | 12 ++++++++++++
 2 files changed, 20 insertions(+), 18 deletions(-)
drivers/block/blk-uclass.c +8 -18
index 614b975e25..9407621fb2 100644
@@ -176,17 +176,7 @@ struct blk_desc *blk_get_by_device(struct udevice *dev)
 	return NULL;
 }
 
-/**
- * get_desc() - Get the block device descriptor for the given device number
- *
- * @uclass_id:	Interface type
- * @devnum:	Device number (0 = first)
- * @descp:	Returns block device descriptor on success
- * Return: 0 on success, -ENODEV if there is no such device and no device
- * with a higher device number, -ENOENT if there is no such device but there
- * is one with a higher number, or other -ve on other error.
- */
-static int get_desc(enum uclass_id uclass_id, int devnum, struct blk_desc **descp)
+int blk_get_desc(enum uclass_id uclass_id, int devnum, struct blk_desc **descp)
 {
 	bool found_more = false;
 	struct udevice *dev;
@@ -238,7 +228,7 @@ int blk_list_part(enum uclass_id uclass_id)
 	int ret;
 
 	for (ok = 0, devnum = 0;; ++devnum) {
-		ret = get_desc(uclass_id, devnum, &desc);
+		ret = blk_get_desc(uclass_id, devnum, &desc);
 		if (ret == -ENODEV)
 			break;
 		else if (ret)
@@ -261,7 +251,7 @@ int blk_print_part_devnum(enum uclass_id uclass_id, int devnum)
 	struct blk_desc *desc;
 	int ret;
 
-	ret = get_desc(uclass_id, devnum, &desc);
+	ret = blk_get_desc(uclass_id, devnum, &desc);
 	if (ret)
 		return ret;
 	if (desc->type == DEV_TYPE_UNKNOWN)
@@ -278,7 +268,7 @@ void blk_list_devices(enum uclass_id uclass_id)
 	int i;
 
 	for (i = 0;; ++i) {
-		ret = get_desc(uclass_id, i, &desc);
+		ret = blk_get_desc(uclass_id, i, &desc);
 		if (ret == -ENODEV)
 			break;
 		else if (ret)
@@ -295,7 +285,7 @@ int blk_print_device_num(enum uclass_id uclass_id, int devnum)
 	struct blk_desc *desc;
 	int ret;
 
-	ret = get_desc(uclass_id, devnum, &desc);
+	ret = blk_get_desc(uclass_id, devnum, &desc);
 	if (ret)
 		return ret;
 	printf("\nIDE device %d: ", devnum);
@@ -310,7 +300,7 @@ int blk_show_device(enum uclass_id uclass_id, int devnum)
 	int ret;
 
 	printf("\nDevice %d: ", devnum);
-	ret = get_desc(uclass_id, devnum, &desc);
+	ret = blk_get_desc(uclass_id, devnum, &desc);
 	if (ret == -ENODEV || ret == -ENOENT) {
 		printf("unknown device\n");
 		return -ENODEV;
@@ -332,7 +322,7 @@ ulong blk_read_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
 	ulong n;
 	int ret;
 
-	ret = get_desc(uclass_id, devnum, &desc);
+	ret = blk_get_desc(uclass_id, devnum, &desc);
 	if (ret)
 		return ret;
 	n = blk_dread(desc, start, blkcnt, buffer);
@@ -348,7 +338,7 @@ ulong blk_write_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
 	struct blk_desc *desc;
 	int ret;
 
-	ret = get_desc(uclass_id, devnum, &desc);
+	ret = blk_get_desc(uclass_id, devnum, &desc);
 	if (ret)
 		return ret;
 	return blk_dwrite(desc, start, blkcnt, buffer);
include/blk.h +12 -0
index 4a4365fbbf..0cd758d6f7 100644
@@ -504,6 +504,18 @@ const char *blk_get_devtype(struct udevice *dev);
  */
 struct blk_desc *blk_get_by_device(struct udevice *dev);
 
+/**
+ * blk_get_desc() - Get the block device descriptor for the given device number
+ *
+ * @uclass_id:	Interface type
+ * @devnum:	Device number (0 = first)
+ * @descp:	Returns block device descriptor on success
+ * Return: 0 on success, -ENODEV if there is no such device and no device
+ * with a higher device number, -ENOENT if there is no such device but there
+ * is one with a higher number, or other -ve on other error.
+ */
+int blk_get_desc(enum uclass_id uclass_id, int devnum, struct blk_desc **descp);
+
 #else
 #include <errno.h>
 /*
Signature · 1 line
2.25.1
Reviewed-by on v1 11/15
#26 lore raw

Re: [PATCH 11/15] dm: blk: Rename get_desc() and make it externally visible

Quoted 11 lines · Bin Meng

get_desc() can be useful outside blk-uclass.c. Let's change it to an API and make it externally visible.

---

 drivers/block/blk-uclass.c | 26 ++++++++------------------
 include/blk.h              | 12 ++++++++++++
 2 files changed, 20 insertions(+), 18 deletions(-)
#27 lore raw
In reply to Bin Meng
Quoted 9 lines · Bin Meng

At present on Sandbox when binding to a host backing file, the host block device is created with a hard-coded 512 bytes block size.

Such assumption works for most cases, but for situation that with a raw image file dump from a pre-formatted GPT partitioned disk image from a 4KiB block size device, when binding this file to a host device and mapping this device to a blkmap, "blkmap" command like "blkmap part" won't work correctly, due to block size mismatch during parsing the partition table.

Quoted 4 lines

This series updates Sandbox block driver, as well as the blkmap driver, to get rid of the hard-coded 512 bytes block size assumption.

This series is available at u-boot-x86/blk for testing.

It is really good to have an easy way to test other block sizes.

We can also use QEMU for alternative block sizes:

$ qemu-system-riscv64 -device nvme,help
logical_block_size=<size>
physical_block_size=<size>

In the commit messages, please, make it clear that you refer to logical block sizes and not to the physical block size.

Best regards

Heinrich

Quoted 112 lines

Test log (512 block size):

   => host bind 0 test.img
   => host info
   dev       blocks  blksz label           path
     0       262144    512 0               test.img
   => blkmap create 0
   Created "0"
   => blkmap map 0 0 40000 linear host 0 0
   Block 0x0+0x40000 mapped to block 0x0 of "host 0"
   => blkmap info
   Device 0: Vendor: U-Boot Rev: 1.0 Prod: blkmap
               Type: Hard Disk
               Capacity: 128.0 MB = 0.1 GB (262144 x 512)
   => blkmap part

   Partition Map for BLKMAP device 0  --   Partition Type: EFI

   Part    Start LBA       End LBA         Name
           Attributes
           Type GUID
           Partition GUID
     1     0x00000022      0x000000bd      "u-boot-spl"
           attrs:  0x0000000000000000
           type:   5b193300-fc78-40cd-8002-e86c45580b47
                   (5b193300-fc78-40cd-8002-e86c45580b47)
           guid:   0bb6bb6e-4aac-4c27-be03-016b01e7b941
     2     0x00000822      0x00000c84      "u-boot"
           attrs:  0x0000000000000000
           type:   2e54b353-1271-4842-806f-e436d6af6985
                   (2e54b353-1271-4842-806f-e436d6af6985)
           guid:   91d50814-8e31-4cc0-97dc-779e1dc59056
     3     0x00000c85      0x0000cc84      "rootfs"
           attrs:  0x0000000000000004
           type:   0fc63daf-8483-4772-8e79-3d69d8477de4
                   (linux)
           guid:   42799722-6e55-46e6-afa9-529e7af3f03b

Test log (4096 block size):

   => host bind 0 test.img 4096
   => host info
   dev       blocks  blksz label           path
     0        32768   4096 0               test.img
   => blkmap create 0
   Created "0"
   => blkmap map 0 0 8000 linear host 0 0
   Block 0x0+0x8000 mapped to block 0x0 of "host 0"
   => blkmap info
   Device 0: Vendor: U-Boot Rev: 1.0 Prod: blkmap
               Type: Hard Disk
               Capacity: 128.0 MB = 0.1 GB (32768 x 4096)
   => blkmap part

   Partition Map for BLKMAP device 0  --   Partition Type: EFI

   Part    Start LBA       End LBA         Name
           Attributes
           Type GUID
           Partition GUID
     1     0x00000100      0x00001fff      "primary"
           attrs:  0x0000000000000000
           type:   0fc63daf-8483-4772-8e79-3d69d8477de4
                   (linux)
           guid:   eba904d7-72c1-4dbd-bb4e-36be49cba5e3
     2     0x00002000      0x00007ffa      "primary"
           attrs:  0x0000000000000000
           type:   0fc63daf-8483-4772-8e79-3d69d8477de4
                   (linux)
           guid:   c48c360e-db47-46da-ab87-26416fad3cd3

Bin Meng (15):
blk: Use a macro for the typical block size
cmd: host: Mandate the filename parameter in the 'bind' command
blk: sandbox: Support binding a device with a given logical block size
blk: host_dev: Make host_sb_detach_file() and host_sb_ops static
blk: host_dev: Sanity check on the size of host backing file
cmd: host: Print out the block size of the host device
blk: blkmap: Make bind/unbind routines static
cmd: blkmap: Make map_handlers[] and its .fn static
blk: blkmap: Support mapping to device of any block size
cmd: blk_common: Use macros for the return values
dm: blk: Rename get_desc() and make it externally visible
cmd: blk_common: Stop using hard-coded block size for Sandbox
operations
dm: blk: Drop blk_{read,write}_devnum()
disk: part: Print out the unknown device uclass id
disk: part: Handle blkmap device in print_part_header()

  cmd/blk_common.c            | 34 +++++++++++++++----------
  cmd/blkmap.c                |  7 ++---
  cmd/host.c                  | 25 +++++++++++++-----
  common/usb_storage.c        |  4 +--
  disk/part.c                 |  5 +++-
  drivers/ata/dwc_ahsata.c    |  3 ++-
  drivers/ata/fsl_sata.c      |  3 ++-
  drivers/ata/sata_mv.c       |  3 ++-
  drivers/ata/sata_sil.c      |  3 ++-
  drivers/block/blk-uclass.c  | 51 +++++--------------------------------
  drivers/block/blkmap.c      | 16 ++++++------
  drivers/block/host-uclass.c | 15 ++++++++---
  drivers/block/host_dev.c    | 11 +++++---
  drivers/mmc/mmc-uclass.c    |  2 +-
  drivers/nvme/nvme.c         |  2 +-
  include/blk.h               | 40 ++++++++++-------------------
  include/sandbox_host.h      |  7 +++--
  test/dm/blk.c               |  7 ++---
  test/dm/host.c              | 26 +++++++++----------
  19 files changed, 129 insertions(+), 135 deletions(-)
#28 lore raw

Hi Heinrich,

Quoted 16 lines · Heinrich Schuchardt
Quoted 9 lines · Bin Meng

At present on Sandbox when binding to a host backing file, the host block device is created with a hard-coded 512 bytes block size.

Such assumption works for most cases, but for situation that with a raw image file dump from a pre-formatted GPT partitioned disk image from a 4KiB block size device, when binding this file to a host device and mapping this device to a blkmap, "blkmap" command like "blkmap part" won't work correctly, due to block size mismatch during parsing the partition table.

Quoted 4 lines

This series updates Sandbox block driver, as well as the blkmap driver, to get rid of the hard-coded 512 bytes block size assumption.

This series is available at u-boot-x86/blk for testing.

Quoted 7 lines

It is really good to have an easy way to test other block sizes.

We can also use QEMU for alternative block sizes:

$ qemu-system-riscv64 -device nvme,help
logical_block_size=<size>
physical_block_size=<size>

Yeah, please report any issue you find.

Quoted 3 lines

In the commit messages, please, make it clear that you refer to logical block sizes and not to the physical block size.

For drivers we rarely care about physical_block_size as they always operate on LBA. But I can write it down clearly that logical block size is used.

Regards,
Bin

author
#29 lore raw

PATCH 12/15 cmd: blk_common: Stop using hard-coded block size for Sandbox operations

In reply to Bin Meng

commit 3d2fc7971454 ("cmd: blk: Allow generic read/write operations to work in sandbox") used the hard-coded block size (512) for accessing the sandbox host device. Now that we have added support for non-512 block size for both Sandbox host device and blkmap driver, let's stop using the hard-coded block size.

---

 cmd/blk_common.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
cmd/blk_common.c +14 -6
index ad9b16dc09..02ac92837b 100644
@@ -67,15 +67,19 @@ int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
 			phys_addr_t paddr = hextoul(argv[2], NULL);
 			lbaint_t blk = hextoul(argv[3], NULL);
 			ulong cnt = hextoul(argv[4], NULL);
+			struct blk_desc *desc;
 			void *vaddr;
 			ulong n;
+			int ret;
 
 			printf("\n%s read: device %d block # "LBAFU", count %lu ... ",
 			       if_name, *cur_devnump, blk, cnt);
 
-			vaddr = map_sysmem(paddr, 512 * cnt);
-			n = blk_read_devnum(uclass_id, *cur_devnump, blk, cnt,
-					    vaddr);
+			ret = blk_get_desc(uclass_id, *cur_devnump, &desc);
+			if (ret)
+				return CMD_RET_FAILURE;
+			vaddr = map_sysmem(paddr, desc->blksz * cnt);
+			n = blk_dread(desc, blk, cnt, vaddr);
 			unmap_sysmem(vaddr);
 
 			printf("%ld blocks read: %s\n", n,
@@ -85,15 +89,19 @@ int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
 			phys_addr_t paddr = hextoul(argv[2], NULL);
 			lbaint_t blk = hextoul(argv[3], NULL);
 			ulong cnt = hextoul(argv[4], NULL);
+			struct blk_desc *desc;
 			void *vaddr;
 			ulong n;
+			int ret;
 
 			printf("\n%s write: device %d block # "LBAFU", count %lu ... ",
 			       if_name, *cur_devnump, blk, cnt);
 
-			vaddr = map_sysmem(paddr, 512 * cnt);
-			n = blk_write_devnum(uclass_id, *cur_devnump, blk, cnt,
-					     vaddr);
+			ret = blk_get_desc(uclass_id, *cur_devnump, &desc);
+			if (ret)
+				return CMD_RET_FAILURE;
+			vaddr = map_sysmem(paddr, desc->blksz * cnt);
+			n = blk_dwrite(desc, blk, cnt, vaddr);
 			unmap_sysmem(vaddr);
 
 			printf("%ld blocks written: %s\n", n,
Signature · 1 line
2.25.1
Reviewed-by on v1 12/15
#30 lore raw

Re: [PATCH 12/15] cmd: blk_common: Stop using hard-coded block size for Sandbox operations

Quoted 7 lines · Bin Meng

commit 3d2fc7971454 ("cmd: blk: Allow generic read/write operations to work in sandbox") used the hard-coded block size (512) for accessing the sandbox host device. Now that we have added support for non-512 block size for both Sandbox host device and blkmap driver, let's stop using the hard-coded block size.

Quoted 6 lines

---

 cmd/blk_common.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
author
#31 lore raw

PATCH 13/15 dm: blk: Drop blk_{read,write}_devnum()

In reply to Bin Meng

blk_{read,write}_devnum() are no longer used by anywhere in the source tree. Drop them.

---

 drivers/block/blk-uclass.c | 29 -----------------------------
 include/blk.h              | 26 --------------------------
 2 files changed, 55 deletions(-)
drivers/block/blk-uclass.c +0 -29
index 9407621fb2..a4c6cf194c 100644
@@ -315,35 +315,6 @@ int blk_show_device(enum uclass_id uclass_id, int devnum)
 	return 0;
 }
 
-ulong blk_read_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
-		      lbaint_t blkcnt, void *buffer)
-{
-	struct blk_desc *desc;
-	ulong n;
-	int ret;
-
-	ret = blk_get_desc(uclass_id, devnum, &desc);
-	if (ret)
-		return ret;
-	n = blk_dread(desc, start, blkcnt, buffer);
-	if (IS_ERR_VALUE(n))
-		return n;
-
-	return n;
-}
-
-ulong blk_write_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
-		       lbaint_t blkcnt, const void *buffer)
-{
-	struct blk_desc *desc;
-	int ret;
-
-	ret = blk_get_desc(uclass_id, devnum, &desc);
-	if (ret)
-		return ret;
-	return blk_dwrite(desc, start, blkcnt, buffer);
-}
-
 int blk_select_hwpart(struct udevice *dev, int hwpart)
 {
 	const struct blk_ops *ops = blk_get_ops(dev);
include/blk.h +0 -26
index 0cd758d6f7..e1477df3b6 100644
@@ -718,32 +718,6 @@ int blk_print_device_num(enum uclass_id uclass_id, int devnum);
  */
 int blk_print_part_devnum(enum uclass_id uclass_id, int devnum);
 
-/**
- * blk_read_devnum() - read blocks from a device
- *
- * @uclass_id:	Block device type
- * @devnum:	Device number
- * @start:	Start block number to read (0=first)
- * @blkcnt:	Number of blocks to read
- * @buffer:	Address to write data to
- * Return: number of blocks read, or -ve error number on error
- */
-ulong blk_read_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
-		      lbaint_t blkcnt, void *buffer);
-
-/**
- * blk_write_devnum() - write blocks to a device
- *
- * @uclass_id:	Block device type
- * @devnum:	Device number
- * @start:	Start block number to write (0=first)
- * @blkcnt:	Number of blocks to write
- * @buffer:	Address to read data from
- * Return: number of blocks written, or -ve error number on error
- */
-ulong blk_write_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
-		       lbaint_t blkcnt, const void *buffer);
-
 /**
  * blk_select_hwpart_devnum() - select a hardware partition
  *
Signature · 1 line
2.25.1
Reviewed-by on v1 13/15
#32 lore raw

Re: [PATCH 13/15] dm: blk: Drop blk_{read,write}_devnum()

Quoted 11 lines · Bin Meng

blk_{read,write}_devnum() are no longer used by anywhere in the source tree. Drop them.

---

 drivers/block/blk-uclass.c | 29 -----------------------------
 include/blk.h              | 26 --------------------------
 2 files changed, 55 deletions(-)
author
#33 lore raw

PATCH 14/15 disk: part: Print out the unknown device uclass id

In reply to Bin Meng

It's helpful to output the device uclass id for unknown devices during the debugging process.

---

 disk/part.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
disk/part.c +1 -1
index eec02f5898..493e04943b 100644
@@ -310,7 +310,7 @@ static void print_part_header(const char *type, struct blk_desc *dev_desc)
 		puts("EFI");
 		break;
 	default:
-		puts("UNKNOWN");
+		printf("UNKNOWN(%d)", dev_desc->uclass_id);
 		break;
 	}
 	printf (" device %d  --   Partition Type: %s\n\n",
Signature · 1 line
2.25.1
Reviewed-by on v1 14/15
#34 lore raw

Re: [PATCH 14/15] disk: part: Print out the unknown device uclass id

Quoted 9 lines · Bin Meng

It's helpful to output the device uclass id for unknown devices during the debugging process.

---

 disk/part.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
author
#35 lore raw

PATCH 15/15 disk: part: Handle blkmap device in print_part_header()

In reply to Bin Meng

Print out the blkmap device type when showing partition header for a blkmap device.

---

 disk/part.c | 3 +++
 1 file changed, 3 insertions(+)
disk/part.c +3 -0
index 493e04943b..6997a89775 100644
@@ -309,6 +309,9 @@ static void print_part_header(const char *type, struct blk_desc *dev_desc)
 	case UCLASS_EFI_MEDIA:
 		puts("EFI");
 		break;
+	case UCLASS_BLKMAP:
+		puts("BLKMAP");
+		break;
 	default:
 		printf("UNKNOWN(%d)", dev_desc->uclass_id);
 		break;
Signature · 1 line
2.25.1
#37 lore raw
In reply to Bin Meng
Quoted 71 lines · Bin Meng

At present on Sandbox when binding to a host backing file, the host block device is created with a hard-coded 512 bytes block size.

Such assumption works for most cases, but for situation that with a raw image file dump from a pre-formatted GPT partitioned disk image from a 4KiB block size device, when binding this file to a host device and mapping this device to a blkmap, "blkmap" command like "blkmap part" won't work correctly, due to block size mismatch during parsing the partition table.

This series updates Sandbox block driver, as well as the blkmap driver, to get rid of the hard-coded 512 bytes block size assumption.

This series is available at u-boot-x86/blk for testing.

Test log (512 block size):

=> host bind 0 test.img
=> host info

  dev       blocks  blksz label           path
    0       262144    512 0               test.img

=> blkmap create 0
Created "0"
=> blkmap map 0 0 40000 linear host 0 0
Block 0x0+0x40000 mapped to block 0x0 of "host 0" => blkmap info
Device 0: Vendor: U-Boot Rev: 1.0 Prod: blkmap
Type: Hard Disk
Capacity: 128.0 MB = 0.1 GB (262144 x 512)
=> blkmap part

Partition Map for BLKMAP device 0 -- Partition Type: EFI

Part Start LBA End LBA Name

          Attributes
          Type GUID
          Partition GUID
    1     0x00000022      0x000000bd      "u-boot-spl"
          attrs:  0x0000000000000000
          type:   5b193300-fc78-40cd-8002-e86c45580b47
                  (5b193300-fc78-40cd-8002-e86c45580b47)
          guid:   0bb6bb6e-4aac-4c27-be03-016b01e7b941
    2     0x00000822      0x00000c84      "u-boot"
          attrs:  0x0000000000000000
          type:   2e54b353-1271-4842-806f-e436d6af6985
                  (2e54b353-1271-4842-806f-e436d6af6985)
          guid:   91d50814-8e31-4cc0-97dc-779e1dc59056
    3     0x00000c85      0x0000cc84      "rootfs"
          attrs:  0x0000000000000004
          type:   0fc63daf-8483-4772-8e79-3d69d8477de4
                  (linux)
          guid:   42799722-6e55-46e6-afa9-529e7af3f03b

Test log (4096 block size):

=> host bind 0 test.img 4096
=> host info
dev blocks blksz label path

    0        32768   4096 0               test.img

=> blkmap create 0
Created "0"
=> blkmap map 0 0 8000 linear host 0 0
Block 0x0+0x8000 mapped to block 0x0 of "host 0" => blkmap info
Device 0: Vendor: U-Boot Rev: 1.0 Prod: blkmap
Type: Hard Disk
Capacity: 128.0 MB = 0.1 GB (32768 x 4096)
=> blkmap part

Partition Map for BLKMAP device 0 -- Partition Type: EFI

Part Start LBA End LBA Name

Quoted 13 lines
          Attributes
          Type GUID
          Partition GUID
    1     0x00000100      0x00001fff      "primary"
          attrs:  0x0000000000000000
          type:   0fc63daf-8483-4772-8e79-3d69d8477de4
                  (linux)
          guid:   eba904d7-72c1-4dbd-bb4e-36be49cba5e3
    2     0x00002000      0x00007ffa      "primary"
          attrs:  0x0000000000000000
          type:   0fc63daf-8483-4772-8e79-3d69d8477de4
                  (linux)
          guid:   c48c360e-db47-46da-ab87-26416fad3cd3

With minor changes to apply again, applied to u-boot/master, thanks!

Signature · 1 line
Tom