[U-Boot] [PATCH 3/6] sandbox: Implement host dev [device]

Sjoerd Simons sjoerd.simons at collabora.co.uk
Thu Feb 19 23:41:11 CET 2015


A common pattern to check if a certain device exists (e.g. in
config_distro_bootcmd) is to use: <interface> dev [device]

Implement host dev [device] so this pattern can be used for sandbox host
devices.

Signed-off-by: Sjoerd Simons <sjoerd.simons at collabora.co.uk>
---
 common/cmd_sandbox.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/common/cmd_sandbox.c b/common/cmd_sandbox.c
index 4286969..7447d43 100644
--- a/common/cmd_sandbox.c
+++ b/common/cmd_sandbox.c
@@ -10,6 +10,8 @@
 #include <sandboxblockdev.h>
 #include <asm/errno.h>
 
+static int sandbox_curr_device = -1;
+
 static int do_sandbox_load(cmd_tbl_t *cmdtp, int flag, int argc,
 			   char * const argv[])
 {
@@ -124,3 +126,49 @@ U_BOOT_CMD(
 	"sb commands use the \"hostfs\" device. The \"host\" device is used\n"
 	"with standard IO commands such as fatls or ext2load"
 );
+
+
+static int do_host_dev(cmd_tbl_t *cmdtp, int flag, int argc,
+		       char * const argv[])
+{
+	int dev;
+	char *ep;
+	block_dev_desc_t *blk_dev;
+	int ret;
+
+	if (argc < 2 || strcmp(argv[1], "dev"))
+		return CMD_RET_USAGE;
+
+	if (argc == 2) {
+		if (sandbox_curr_device < 0) {
+			printf("No current host device\n");
+			return 1;
+		}
+		printf ("Current host device %d\n", sandbox_curr_device);
+		return 0;
+	}
+
+	dev = simple_strtoul(argv[2], &ep, 16);
+	if (*ep) {
+		printf("** Bad device specification %s **\n", argv[2]);
+		return CMD_RET_USAGE;
+	}
+
+	ret = host_get_dev_err(dev, &blk_dev);
+	if (ret) {
+		if (ret == -ENOENT)
+			puts("Not bound to a backing file\n");
+		else if (ret == -ENODEV)
+			puts("Invalid host device number\n");
+
+		return 1;
+	}
+
+	sandbox_curr_device = dev;
+	return 0;
+}
+
+U_BOOT_CMD(
+	host, 3, 2, do_host_dev,
+	"Set or get current host device", "dev [dev] - Set or retrieve the current host device"
+);
-- 
2.1.4



More information about the U-Boot mailing list