1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2012, Google Inc.
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 #include <linux/log2.h>
18 static int do_host_load(struct cmd_tbl *cmdtp, int flag, int argc,
21 return do_load(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
24 static int do_host_ls(struct cmd_tbl *cmdtp, int flag, int argc,
27 return do_ls(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
30 static int do_host_size(struct cmd_tbl *cmdtp, int flag, int argc,
33 return do_size(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
36 static int do_host_save(struct cmd_tbl *cmdtp, int flag, int argc,
39 return do_save(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
42 static int do_host_bind(struct cmd_tbl *cmdtp, int flag, int argc,
45 bool removable = false;
49 unsigned long blksz = DEFAULT_BLKSZ;
58 if (!strcmp(argv[0], "-r")) {
64 if (argc < 2 || argc > 3)
69 blksz = dectoul(argv[2], NULL);
70 if (blksz < DEFAULT_BLKSZ || !is_power_of_2(blksz)) {
71 printf("blksz must be >= 512 and power of 2\n");
72 return CMD_RET_FAILURE;
76 ret = host_create_attach_file(label, file, removable, blksz, &dev);
78 printf("Cannot create device / bind file\n");
79 return CMD_RET_FAILURE;
86 * parse_host_label() - Parse a device label or sequence number
88 * This shows an error if it returns NULL
90 * @label: String containing the label or sequence number
91 * Returns: Associated device, or NULL if not found
93 static struct udevice *parse_host_label(const char *label)
97 dev = host_find_by_label(label);
102 devnum = hextoul(label, &ep);
104 uclass_find_device_by_seq(UCLASS_HOST, devnum, &dev)) {
105 printf("No such device '%s'\n", label);
113 static int do_host_unbind(struct cmd_tbl *cmdtp, int flag, int argc,
121 return CMD_RET_USAGE;
124 dev = parse_host_label(label);
126 return CMD_RET_FAILURE;
128 ret = host_detach_file(dev);
130 printf("Cannot detach file (err=%d)\n", ret);
131 return CMD_RET_FAILURE;
134 ret = device_unbind(dev);
136 printf("Cannot attach file\n");
137 ret = device_unbind(dev);
139 printf("Cannot unbind device '%s'\n", dev->name);
140 return CMD_RET_FAILURE;
146 static void show_host_dev(struct udevice *dev)
148 struct host_sb_plat *plat = dev_get_plat(dev);
149 struct blk_desc *desc;
153 printf("%3d ", dev_seq(dev));
155 printf("Not bound to a backing file\n");
158 ret = blk_get_from_parent(dev, &blk);
159 if (ret) /* cannot happen */
162 desc = dev_get_uclass_plat(blk);
163 printf("%12lu %6lu %-15s %s\n", (unsigned long)desc->lba, desc->blksz,
164 plat->label, plat->filename);
167 static int do_host_info(struct cmd_tbl *cmdtp, int flag, int argc,
173 return CMD_RET_USAGE;
177 dev = parse_host_label(argv[1]);
179 return CMD_RET_FAILURE;
182 printf("%3s %12s %6s %-15s %s\n",
183 "dev", "blocks", "blksz", "label", "path");
189 uclass_id_foreach_dev(UCLASS_HOST, dev, uc)
196 static int do_host_dev(struct cmd_tbl *cmdtp, int flag, int argc,
202 if (argc < 1 || argc > 3)
203 return CMD_RET_USAGE;
206 struct host_sb_plat *plat;
208 dev = host_get_cur_dev();
210 printf("No current host device\n");
211 return CMD_RET_FAILURE;
213 plat = dev_get_plat(dev);
214 printf("Current host device: %d: %s\n", dev_seq(dev),
220 dev = parse_host_label(argv[1]);
222 return CMD_RET_FAILURE;
224 host_set_cur_dev(dev);
229 static struct cmd_tbl cmd_host_sub[] = {
230 U_BOOT_CMD_MKENT(load, 7, 0, do_host_load, "", ""),
231 U_BOOT_CMD_MKENT(ls, 3, 0, do_host_ls, "", ""),
232 U_BOOT_CMD_MKENT(save, 6, 0, do_host_save, "", ""),
233 U_BOOT_CMD_MKENT(size, 3, 0, do_host_size, "", ""),
234 U_BOOT_CMD_MKENT(bind, 4, 0, do_host_bind, "", ""),
235 U_BOOT_CMD_MKENT(unbind, 4, 0, do_host_unbind, "", ""),
236 U_BOOT_CMD_MKENT(info, 3, 0, do_host_info, "", ""),
237 U_BOOT_CMD_MKENT(dev, 0, 1, do_host_dev, "", ""),
240 static int do_host(struct cmd_tbl *cmdtp, int flag, int argc,
245 /* Skip past 'host' */
249 c = find_cmd_tbl(argv[0], cmd_host_sub, ARRAY_SIZE(cmd_host_sub));
252 return c->cmd(cmdtp, flag, argc, argv);
254 return CMD_RET_USAGE;
259 "Miscellaneous host commands",
260 "load hostfs - <addr> <filename> [<bytes> <offset>] - "
261 "load a file from host\n"
262 "host ls hostfs - <filename> - list files on host\n"
263 "host save hostfs - <addr> <filename> <bytes> [<offset>] - "
264 "save a file to host\n"
265 "host size hostfs - <filename> - determine size of file on host\n"
266 "host bind [-r] <label> <filename> [<blksz>] - bind \"host\" device to file,\n"
267 " and optionally set the device's logical block size\n"
268 " -r = mark as removable\n"
269 "host unbind <label> - unbind file from \"host\" device\n"
270 "host info [<label>] - show device binding & info\n"
271 "host dev [<label>] - set or retrieve the current host device\n"
272 "host commands use the \"hostfs\" device. The \"host\" device is used\n"
273 "with standard IO commands such as fatls or ext2load"