blob: fb1cb1fdd1ad15194cec15bccb4ed727ca42a5b0 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassd3049312012-12-26 09:53:36 +00002/*
3 * Copyright (c) 2012, Google Inc.
Simon Glassd3049312012-12-26 09:53:36 +00004 */
5
6#include <common.h>
Simon Glass09140112020-05-10 11:40:03 -06007#include <command.h>
Simon Glass40fd0502016-02-29 15:25:57 -07008#include <dm.h>
Simon Glassd3049312012-12-26 09:53:36 +00009#include <fs.h>
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070010#include <part.h>
Simon Glass95201812022-10-29 19:47:17 -060011#include <sandbox_host.h>
Simon Glass336d4612020-02-03 07:36:16 -070012#include <dm/device_compat.h>
Simon Glass95201812022-10-29 19:47:17 -060013#include <dm/device-internal.h>
14#include <dm/uclass-internal.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090015#include <linux/errno.h>
Simon Glassd3049312012-12-26 09:53:36 +000016
Simon Glass09140112020-05-10 11:40:03 -060017static int do_host_load(struct cmd_tbl *cmdtp, int flag, int argc,
18 char *const argv[])
Simon Glassd3049312012-12-26 09:53:36 +000019{
Tom Rini9ade4852013-10-10 10:46:22 -040020 return do_load(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
Simon Glassd3049312012-12-26 09:53:36 +000021}
22
Simon Glass09140112020-05-10 11:40:03 -060023static int do_host_ls(struct cmd_tbl *cmdtp, int flag, int argc,
24 char *const argv[])
Simon Glassd3049312012-12-26 09:53:36 +000025{
26 return do_ls(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
27}
28
Simon Glass09140112020-05-10 11:40:03 -060029static int do_host_size(struct cmd_tbl *cmdtp, int flag, int argc,
30 char *const argv[])
Stefan Brüns49afb372016-08-11 22:52:04 +020031{
32 return do_size(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
33}
34
Simon Glass09140112020-05-10 11:40:03 -060035static int do_host_save(struct cmd_tbl *cmdtp, int flag, int argc,
36 char *const argv[])
Simon Glass7eb2c8d2013-04-20 08:42:51 +000037{
Tom Rini9ade4852013-10-10 10:46:22 -040038 return do_save(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
Simon Glass7eb2c8d2013-04-20 08:42:51 +000039}
40
Simon Glass09140112020-05-10 11:40:03 -060041static int do_host_bind(struct cmd_tbl *cmdtp, int flag, int argc,
42 char *const argv[])
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070043{
Simon Glass17585512021-03-15 18:11:11 +130044 bool removable = false;
Simon Glass95201812022-10-29 19:47:17 -060045 struct udevice *dev;
46 const char *label;
Simon Glass1503c2c2021-03-15 18:11:09 +130047 char *file;
Simon Glass95201812022-10-29 19:47:17 -060048 int ret;
Simon Glass1503c2c2021-03-15 18:11:09 +130049
Simon Glass803e9c12021-03-15 18:11:10 +130050 /* Skip 'bind' */
51 argc--;
52 argv++;
Simon Glass17585512021-03-15 18:11:11 +130053 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)
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070063 return CMD_RET_USAGE;
Simon Glass95201812022-10-29 19:47:17 -060064 label = argv[0];
Simon Glass803e9c12021-03-15 18:11:10 +130065 file = argc > 1 ? argv[1] : NULL;
Simon Glass1503c2c2021-03-15 18:11:09 +130066
Simon Glass95201812022-10-29 19:47:17 -060067 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 */
84static 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
104static 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
137static 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);
Henrik Nordströmf4d8de42013-11-10 10:26:56 -0700156}
157
Simon Glass09140112020-05-10 11:40:03 -0600158static int do_host_info(struct cmd_tbl *cmdtp, int flag, int argc,
159 char *const argv[])
Henrik Nordströmf4d8de42013-11-10 10:26:56 -0700160{
Simon Glass95201812022-10-29 19:47:17 -0600161 struct udevice *dev;
162
163 if (argc < 1)
Henrik Nordströmf4d8de42013-11-10 10:26:56 -0700164 return CMD_RET_USAGE;
Simon Glass95201812022-10-29 19:47:17 -0600165
166 dev = NULL;
Henrik Nordströmf4d8de42013-11-10 10:26:56 -0700167 if (argc >= 2) {
Simon Glass95201812022-10-29 19:47:17 -0600168 dev = parse_host_label(argv[1]);
169 if (!dev)
170 return CMD_RET_FAILURE;
Henrik Nordströmf4d8de42013-11-10 10:26:56 -0700171 }
Henrik Nordströmf4d8de42013-11-10 10:26:56 -0700172
Simon Glass95201812022-10-29 19:47:17 -0600173 printf("%3s %12s %-15s %s\n", "dev", "blocks", "label", "path");
174 if (dev) {
175 show_host_dev(dev);
176 } else {
177 struct uclass *uc;
Henrik Nordströmf4d8de42013-11-10 10:26:56 -0700178
Simon Glass95201812022-10-29 19:47:17 -0600179 uclass_id_foreach_dev(UCLASS_HOST, dev, uc)
180 show_host_dev(dev);
Henrik Nordströmf4d8de42013-11-10 10:26:56 -0700181 }
Simon Glass95201812022-10-29 19:47:17 -0600182
Henrik Nordströmf4d8de42013-11-10 10:26:56 -0700183 return 0;
184}
185
Simon Glass09140112020-05-10 11:40:03 -0600186static int do_host_dev(struct cmd_tbl *cmdtp, int flag, int argc,
187 char *const argv[])
Sjoerd Simons9b97b6b2015-04-13 22:54:23 +0200188{
Simon Glass95201812022-10-29 19:47:17 -0600189 struct udevice *dev;
190 const char *label;
Sjoerd Simons9b97b6b2015-04-13 22:54:23 +0200191
192 if (argc < 1 || argc > 3)
193 return CMD_RET_USAGE;
194
195 if (argc == 1) {
Simon Glass95201812022-10-29 19:47:17 -0600196 struct host_sb_plat *plat;
197
198 dev = host_get_cur_dev();
199 if (!dev) {
Sjoerd Simons9b97b6b2015-04-13 22:54:23 +0200200 printf("No current host device\n");
Simon Glass95201812022-10-29 19:47:17 -0600201 return CMD_RET_FAILURE;
Sjoerd Simons9b97b6b2015-04-13 22:54:23 +0200202 }
Simon Glass95201812022-10-29 19:47:17 -0600203 plat = dev_get_plat(dev);
204 printf("Current host device: %d: %s\n", dev_seq(dev),
205 plat->label);
Sjoerd Simons9b97b6b2015-04-13 22:54:23 +0200206 return 0;
207 }
208
Simon Glass95201812022-10-29 19:47:17 -0600209 label = argv[1];
210 dev = parse_host_label(argv[1]);
211 if (!dev)
212 return CMD_RET_FAILURE;
Sjoerd Simons9b97b6b2015-04-13 22:54:23 +0200213
Simon Glass95201812022-10-29 19:47:17 -0600214 host_set_cur_dev(dev);
Sjoerd Simons9b97b6b2015-04-13 22:54:23 +0200215
Sjoerd Simons9b97b6b2015-04-13 22:54:23 +0200216 return 0;
217}
218
Simon Glass09140112020-05-10 11:40:03 -0600219static struct cmd_tbl cmd_host_sub[] = {
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200220 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, "", ""),
Stefan Brüns49afb372016-08-11 22:52:04 +0200223 U_BOOT_CMD_MKENT(size, 3, 0, do_host_size, "", ""),
Simon Glass17585512021-03-15 18:11:11 +1300224 U_BOOT_CMD_MKENT(bind, 4, 0, do_host_bind, "", ""),
Simon Glass95201812022-10-29 19:47:17 -0600225 U_BOOT_CMD_MKENT(unbind, 4, 0, do_host_unbind, "", ""),
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200226 U_BOOT_CMD_MKENT(info, 3, 0, do_host_info, "", ""),
Sjoerd Simons9b97b6b2015-04-13 22:54:23 +0200227 U_BOOT_CMD_MKENT(dev, 0, 1, do_host_dev, "", ""),
Simon Glassd3049312012-12-26 09:53:36 +0000228};
229
Simon Glass09140112020-05-10 11:40:03 -0600230static int do_host(struct cmd_tbl *cmdtp, int flag, int argc,
231 char *const argv[])
Simon Glassd3049312012-12-26 09:53:36 +0000232{
Simon Glass09140112020-05-10 11:40:03 -0600233 struct cmd_tbl *c;
Simon Glassd3049312012-12-26 09:53:36 +0000234
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200235 /* Skip past 'host' */
Simon Glassd3049312012-12-26 09:53:36 +0000236 argc--;
237 argv++;
238
Simon Glass95201812022-10-29 19:47:17 -0600239 c = find_cmd_tbl(argv[0], cmd_host_sub, ARRAY_SIZE(cmd_host_sub));
Simon Glassd3049312012-12-26 09:53:36 +0000240
241 if (c)
242 return c->cmd(cmdtp, flag, argc, argv);
243 else
244 return CMD_RET_USAGE;
245}
246
247U_BOOT_CMD(
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200248 host, 8, 1, do_host,
249 "Miscellaneous host commands",
Stephen Warren4d907022014-06-12 10:28:32 -0600250 "load hostfs - <addr> <filename> [<bytes> <offset>] - "
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000251 "load a file from host\n"
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200252 "host ls hostfs - <filename> - list files on host\n"
253 "host save hostfs - <addr> <filename> <bytes> [<offset>] - "
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000254 "save a file to host\n"
Sébastien Szymanski6baa6922017-01-19 17:49:13 +0100255 "host size hostfs - <filename> - determine size of file on host\n"
Simon Glass95201812022-10-29 19:47:17 -0600256 "host bind [-r] <label> [<filename>] - bind \"host\" device to file\n"
Simon Glass17585512021-03-15 18:11:11 +1300257 " -r = mark as removable\n"
Simon Glass95201812022-10-29 19:47:17 -0600258 "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"
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200261 "host commands use the \"hostfs\" device. The \"host\" device is used\n"
Stephen Warren4d907022014-06-12 10:28:32 -0600262 "with standard IO commands such as fatls or ext2load"
Simon Glassd3049312012-12-26 09:53:36 +0000263);