xref: /openbmc/u-boot/cmd/host.c (revision dffceb4b)
1 /*
2  * Copyright (c) 2012, Google Inc.
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <fs.h>
9 #include <part.h>
10 #include <sandboxblockdev.h>
11 #include <asm/errno.h>
12 
13 static int host_curr_device = -1;
14 
15 static int do_host_load(cmd_tbl_t *cmdtp, int flag, int argc,
16 			   char * const argv[])
17 {
18 	return do_load(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
19 }
20 
21 static int do_host_ls(cmd_tbl_t *cmdtp, int flag, int argc,
22 			   char * const argv[])
23 {
24 	return do_ls(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
25 }
26 
27 static int do_host_save(cmd_tbl_t *cmdtp, int flag, int argc,
28 			   char * const argv[])
29 {
30 	return do_save(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
31 }
32 
33 static int do_host_bind(cmd_tbl_t *cmdtp, int flag, int argc,
34 			   char * const argv[])
35 {
36 	if (argc < 2 || argc > 3)
37 		return CMD_RET_USAGE;
38 	char *ep;
39 	char *dev_str = argv[1];
40 	char *file = argc >= 3 ? argv[2] : NULL;
41 	int dev = simple_strtoul(dev_str, &ep, 16);
42 	if (*ep) {
43 		printf("** Bad device specification %s **\n", dev_str);
44 		return CMD_RET_USAGE;
45 	}
46 	return host_dev_bind(dev, file);
47 }
48 
49 static int do_host_info(cmd_tbl_t *cmdtp, int flag, int argc,
50 			   char * const argv[])
51 {
52 	if (argc < 1 || argc > 2)
53 		return CMD_RET_USAGE;
54 	int min_dev = 0;
55 	int max_dev = CONFIG_HOST_MAX_DEVICES - 1;
56 	if (argc >= 2) {
57 		char *ep;
58 		char *dev_str = argv[1];
59 		int dev = simple_strtoul(dev_str, &ep, 16);
60 		if (*ep) {
61 			printf("** Bad device specification %s **\n", dev_str);
62 			return CMD_RET_USAGE;
63 		}
64 		min_dev = dev;
65 		max_dev = dev;
66 	}
67 	int dev;
68 	printf("%3s %12s %s\n", "dev", "blocks", "path");
69 	for (dev = min_dev; dev <= max_dev; dev++) {
70 		block_dev_desc_t *blk_dev;
71 		int ret;
72 
73 		printf("%3d ", dev);
74 		ret = host_get_dev_err(dev, &blk_dev);
75 		if (ret) {
76 			if (ret == -ENOENT)
77 				puts("Not bound to a backing file\n");
78 			else if (ret == -ENODEV)
79 				puts("Invalid host device number\n");
80 
81 			continue;
82 		}
83 		struct host_block_dev *host_dev = blk_dev->priv;
84 		printf("%12lu %s\n", (unsigned long)blk_dev->lba,
85 		       host_dev->filename);
86 	}
87 	return 0;
88 }
89 
90 static int do_host_dev(cmd_tbl_t *cmdtp, int flag, int argc,
91 		       char * const argv[])
92 {
93 	int dev;
94 	char *ep;
95 	block_dev_desc_t *blk_dev;
96 	int ret;
97 
98 	if (argc < 1 || argc > 3)
99 		return CMD_RET_USAGE;
100 
101 	if (argc == 1) {
102 		if (host_curr_device < 0) {
103 			printf("No current host device\n");
104 			return 1;
105 		}
106 		printf("Current host device %d\n", host_curr_device);
107 		return 0;
108 	}
109 
110 	dev = simple_strtoul(argv[1], &ep, 16);
111 	if (*ep) {
112 		printf("** Bad device specification %s **\n", argv[2]);
113 		return CMD_RET_USAGE;
114 	}
115 
116 	ret = host_get_dev_err(dev, &blk_dev);
117 	if (ret) {
118 		if (ret == -ENOENT)
119 			puts("Not bound to a backing file\n");
120 		else if (ret == -ENODEV)
121 			puts("Invalid host device number\n");
122 
123 		return 1;
124 	}
125 
126 	host_curr_device = dev;
127 	return 0;
128 }
129 
130 static cmd_tbl_t cmd_host_sub[] = {
131 	U_BOOT_CMD_MKENT(load, 7, 0, do_host_load, "", ""),
132 	U_BOOT_CMD_MKENT(ls, 3, 0, do_host_ls, "", ""),
133 	U_BOOT_CMD_MKENT(save, 6, 0, do_host_save, "", ""),
134 	U_BOOT_CMD_MKENT(bind, 3, 0, do_host_bind, "", ""),
135 	U_BOOT_CMD_MKENT(info, 3, 0, do_host_info, "", ""),
136 	U_BOOT_CMD_MKENT(dev, 0, 1, do_host_dev, "", ""),
137 };
138 
139 static int do_host(cmd_tbl_t *cmdtp, int flag, int argc,
140 		      char * const argv[])
141 {
142 	cmd_tbl_t *c;
143 
144 	/* Skip past 'host' */
145 	argc--;
146 	argv++;
147 
148 	c = find_cmd_tbl(argv[0], cmd_host_sub,
149 			 ARRAY_SIZE(cmd_host_sub));
150 
151 	if (c)
152 		return c->cmd(cmdtp, flag, argc, argv);
153 	else
154 		return CMD_RET_USAGE;
155 }
156 
157 U_BOOT_CMD(
158 	sb,	8,	1,	do_host,
159 	"Deprecated: use 'host' command instead.", ""
160 );
161 
162 U_BOOT_CMD(
163 	host, 8, 1, do_host,
164 	"Miscellaneous host commands",
165 	"load hostfs - <addr> <filename> [<bytes> <offset>]  - "
166 		"load a file from host\n"
167 	"host ls hostfs - <filename>                    - list files on host\n"
168 	"host save hostfs - <addr> <filename> <bytes> [<offset>] - "
169 		"save a file to host\n"
170 	"host bind <dev> [<filename>] - bind \"host\" device to file\n"
171 	"host info [<dev>]            - show device binding & info\n"
172 	"host dev [<dev>] - Set or retrieve the current host device\n"
173 	"host commands use the \"hostfs\" device. The \"host\" device is used\n"
174 	"with standard IO commands such as fatls or ext2load"
175 );
176