1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2012, Google Inc.
4  */
5 
6 #include <common.h>
7 #include <command.h>
8 #include <dm.h>
9 #include <fs.h>
10 #include <part.h>
11 #include <sandbox_host.h>
12 #include <dm/device_compat.h>
13 #include <dm/device-internal.h>
14 #include <dm/uclass-internal.h>
15 #include <linux/errno.h>
16 
do_host_load(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])17 static int do_host_load(struct cmd_tbl *cmdtp, int flag, int argc,
18 			char *const argv[])
19 {
20 	return do_load(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
21 }
22 
do_host_ls(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])23 static int do_host_ls(struct cmd_tbl *cmdtp, int flag, int argc,
24 		      char *const argv[])
25 {
26 	return do_ls(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
27 }
28 
do_host_size(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])29 static int do_host_size(struct cmd_tbl *cmdtp, int flag, int argc,
30 			char *const argv[])
31 {
32 	return do_size(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
33 }
34 
do_host_save(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])35 static int do_host_save(struct cmd_tbl *cmdtp, int flag, int argc,
36 			char *const argv[])
37 {
38 	return do_save(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
39 }
40 
do_host_bind(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])41 static int do_host_bind(struct cmd_tbl *cmdtp, int flag, int argc,
42 			char *const argv[])
43 {
44 	bool removable = false;
45 	struct udevice *dev;
46 	const char *label;
47 	char *file;
48 	int ret;
49 
50 	/* Skip 'bind' */
51 	argc--;
52 	argv++;
53 	if (argc < 2)
54 		return CMD_RET_USAGE;
55 
56 	if (!strcmp(argv[0], "-r")) {
57 		removable = true;
58 		argc--;
59 		argv++;
60 	}
61 
62 	if (argc > 2)
63 		return CMD_RET_USAGE;
64 	label = argv[0];
65 	file = argc > 1 ? argv[1] : NULL;
66 
67 	ret = host_create_attach_file(label, file, removable, &dev);
68 	if (ret) {
69 		printf("Cannot create device / bind file\n");
70 		return CMD_RET_FAILURE;
71 	}
72 
73 	return 0;
74 }
75 
76 /**
77  * parse_host_label() - Parse a device label or sequence number
78  *
79  * This shows an error if it returns NULL
80  *
81  * @label: String containing the label or sequence number
82  * Returns: Associated device, or NULL if not found
83  */
parse_host_label(const char * label)84 static struct udevice *parse_host_label(const char *label)
85 {
86 	struct udevice *dev;
87 
88 	dev = host_find_by_label(label);
89 	if (!dev) {
90 		int devnum;
91 		char *ep;
92 
93 		devnum = hextoul(label, &ep);
94 		if (*ep ||
95 		    uclass_find_device_by_seq(UCLASS_HOST, devnum, &dev)) {
96 			printf("No such device '%s'\n", label);
97 			return NULL;
98 		}
99 	}
100 
101 	return dev;
102 }
103 
do_host_unbind(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])104 static int do_host_unbind(struct cmd_tbl *cmdtp, int flag, int argc,
105 			  char *const argv[])
106 {
107 	struct udevice *dev;
108 	const char *label;
109 	int ret;
110 
111 	if (argc < 2)
112 		return CMD_RET_USAGE;
113 
114 	label = argv[1];
115 	dev = parse_host_label(label);
116 	if (!dev)
117 		return CMD_RET_FAILURE;
118 
119 	ret = host_detach_file(dev);
120 	if (ret) {
121 		printf("Cannot detach file (err=%d)\n", ret);
122 		return CMD_RET_FAILURE;
123 	}
124 
125 	ret = device_unbind(dev);
126 	if (ret) {
127 		printf("Cannot attach file\n");
128 		ret = device_unbind(dev);
129 		if (ret)
130 			printf("Cannot unbind device '%s'\n", dev->name);
131 		return CMD_RET_FAILURE;
132 	}
133 
134 	return 0;
135 }
136 
show_host_dev(struct udevice * dev)137 static void show_host_dev(struct udevice *dev)
138 {
139 	struct host_sb_plat *plat = dev_get_plat(dev);
140 	struct blk_desc *desc;
141 	struct udevice *blk;
142 	int ret;
143 
144 	printf("%3d ", dev_seq(dev));
145 	if (!plat->fd) {
146 		printf("Not bound to a backing file\n");
147 		return;
148 	}
149 	ret = blk_get_from_parent(dev, &blk);
150 	if (ret)  /* cannot happen */
151 		return;
152 
153 	desc = dev_get_uclass_plat(blk);
154 	printf("%12lu %-15s %s\n", (unsigned long)desc->lba, plat->label,
155 	       plat->filename);
156 }
157 
do_host_info(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])158 static int do_host_info(struct cmd_tbl *cmdtp, int flag, int argc,
159 			char *const argv[])
160 {
161 	struct udevice *dev;
162 
163 	if (argc < 1)
164 		return CMD_RET_USAGE;
165 
166 	dev = NULL;
167 	if (argc >= 2) {
168 		dev = parse_host_label(argv[1]);
169 		if (!dev)
170 			return CMD_RET_FAILURE;
171 	}
172 
173 	printf("%3s %12s %-15s %s\n", "dev", "blocks", "label", "path");
174 	if (dev) {
175 		show_host_dev(dev);
176 	} else {
177 		struct uclass *uc;
178 
179 		uclass_id_foreach_dev(UCLASS_HOST, dev, uc)
180 			show_host_dev(dev);
181 	}
182 
183 	return 0;
184 }
185 
do_host_dev(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])186 static int do_host_dev(struct cmd_tbl *cmdtp, int flag, int argc,
187 		       char *const argv[])
188 {
189 	struct udevice *dev;
190 	const char *label;
191 
192 	if (argc < 1 || argc > 3)
193 		return CMD_RET_USAGE;
194 
195 	if (argc == 1) {
196 		struct host_sb_plat *plat;
197 
198 		dev = host_get_cur_dev();
199 		if (!dev) {
200 			printf("No current host device\n");
201 			return CMD_RET_FAILURE;
202 		}
203 		plat = dev_get_plat(dev);
204 		printf("Current host device: %d: %s\n", dev_seq(dev),
205 		       plat->label);
206 		return 0;
207 	}
208 
209 	label = argv[1];
210 	dev = parse_host_label(argv[1]);
211 	if (!dev)
212 		return CMD_RET_FAILURE;
213 
214 	host_set_cur_dev(dev);
215 
216 	return 0;
217 }
218 
219 static struct cmd_tbl cmd_host_sub[] = {
220 	U_BOOT_CMD_MKENT(load, 7, 0, do_host_load, "", ""),
221 	U_BOOT_CMD_MKENT(ls, 3, 0, do_host_ls, "", ""),
222 	U_BOOT_CMD_MKENT(save, 6, 0, do_host_save, "", ""),
223 	U_BOOT_CMD_MKENT(size, 3, 0, do_host_size, "", ""),
224 	U_BOOT_CMD_MKENT(bind, 4, 0, do_host_bind, "", ""),
225 	U_BOOT_CMD_MKENT(unbind, 4, 0, do_host_unbind, "", ""),
226 	U_BOOT_CMD_MKENT(info, 3, 0, do_host_info, "", ""),
227 	U_BOOT_CMD_MKENT(dev, 0, 1, do_host_dev, "", ""),
228 };
229 
do_host(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])230 static int do_host(struct cmd_tbl *cmdtp, int flag, int argc,
231 		   char *const argv[])
232 {
233 	struct cmd_tbl *c;
234 
235 	/* Skip past 'host' */
236 	argc--;
237 	argv++;
238 
239 	c = find_cmd_tbl(argv[0], cmd_host_sub, ARRAY_SIZE(cmd_host_sub));
240 
241 	if (c)
242 		return c->cmd(cmdtp, flag, argc, argv);
243 	else
244 		return CMD_RET_USAGE;
245 }
246 
247 U_BOOT_CMD(
248 	host, 8, 1, do_host,
249 	"Miscellaneous host commands",
250 	"load hostfs - <addr> <filename> [<bytes> <offset>]  - "
251 		"load a file from host\n"
252 	"host ls hostfs - <filename>                    - list files on host\n"
253 	"host save hostfs - <addr> <filename> <bytes> [<offset>] - "
254 		"save a file to host\n"
255 	"host size hostfs - <filename> - determine size of file on host\n"
256 	"host bind [-r] <label> [<filename>] - bind \"host\" device to file\n"
257 	"     -r = mark as removable\n"
258 	"host unbind <label>     - unbind file from \"host\" device\n"
259 	"host info [<label>]     - show device binding & info\n"
260 	"host dev [<label>]      - set or retrieve the current host device\n"
261 	"host commands use the \"hostfs\" device. The \"host\" device is used\n"
262 	"with standard IO commands such as fatls or ext2load"
263 );
264