blob: fb1cb1fdd1ad15194cec15bccb4ed727ca42a5b0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassbf6ce792012-12-26 09:53:36 +00002/*
3 * Copyright (c) 2012, Google Inc.
Simon Glassbf6ce792012-12-26 09:53:36 +00004 */
5
6#include <common.h>
Simon Glassed38aef2020-05-10 11:40:03 -06007#include <command.h>
Simon Glass2c2075a2016-02-29 15:25:57 -07008#include <dm.h>
Simon Glassbf6ce792012-12-26 09:53:36 +00009#include <fs.h>
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070010#include <part.h>
Simon Glasse57f8d42022-10-29 19:47:17 -060011#include <sandbox_host.h>
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <dm/device_compat.h>
Simon Glasse57f8d42022-10-29 19:47:17 -060013#include <dm/device-internal.h>
14#include <dm/uclass-internal.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090015#include <linux/errno.h>
Simon Glassbf6ce792012-12-26 09:53:36 +000016
Simon Glassed38aef2020-05-10 11:40:03 -060017static int do_host_load(struct cmd_tbl *cmdtp, int flag, int argc,
18 char *const argv[])
Simon Glassbf6ce792012-12-26 09:53:36 +000019{
Tom Rini089078a2013-10-10 10:46:22 -040020 return do_load(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
Simon Glassbf6ce792012-12-26 09:53:36 +000021}
22
Simon Glassed38aef2020-05-10 11:40:03 -060023static int do_host_ls(struct cmd_tbl *cmdtp, int flag, int argc,
24 char *const argv[])
Simon Glassbf6ce792012-12-26 09:53:36 +000025{
26 return do_ls(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
27}
28
Simon Glassed38aef2020-05-10 11:40:03 -060029static int do_host_size(struct cmd_tbl *cmdtp, int flag, int argc,
30 char *const argv[])
Stefan Brüns72acc652016-08-11 22:52:04 +020031{
32 return do_size(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
33}
34
Simon Glassed38aef2020-05-10 11:40:03 -060035static int do_host_save(struct cmd_tbl *cmdtp, int flag, int argc,
36 char *const argv[])
Simon Glassea307e82013-04-20 08:42:51 +000037{
Tom Rini089078a2013-10-10 10:46:22 -040038 return do_save(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
Simon Glassea307e82013-04-20 08:42:51 +000039}
40
Simon Glassed38aef2020-05-10 11:40:03 -060041static int do_host_bind(struct cmd_tbl *cmdtp, int flag, int argc,
42 char *const argv[])
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070043{
Simon Glassb4d00772021-03-15 18:11:11 +130044 bool removable = false;
Simon Glasse57f8d42022-10-29 19:47:17 -060045 struct udevice *dev;
46 const char *label;
Simon Glass8f5fdf32021-03-15 18:11:09 +130047 char *file;
Simon Glasse57f8d42022-10-29 19:47:17 -060048 int ret;
Simon Glass8f5fdf32021-03-15 18:11:09 +130049
Simon Glassf0a45382021-03-15 18:11:10 +130050 /* Skip 'bind' */
51 argc--;
52 argv++;
Simon Glassb4d00772021-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öm26f9a6c2013-11-10 10:26:56 -070063 return CMD_RET_USAGE;
Simon Glasse57f8d42022-10-29 19:47:17 -060064 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 */
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)
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700112 return CMD_RET_USAGE;
Simon Glasse57f8d42022-10-29 19:47:17 -0600113
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;
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700123 }
Simon Glasse57f8d42022-10-29 19:47:17 -0600124
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 }
Simon Glass8f5fdf32021-03-15 18:11:09 +1300133
Simon Glasse57f8d42022-10-29 19:47:17 -0600134 return 0;
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700135}
136
Simon Glasse57f8d42022-10-29 19:47:17 -0600137static 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
Simon Glassed38aef2020-05-10 11:40:03 -0600158static int do_host_info(struct cmd_tbl *cmdtp, int flag, int argc,
159 char *const argv[])
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700160{
Simon Glasse57f8d42022-10-29 19:47:17 -0600161 struct udevice *dev;
162
163 if (argc < 1)
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700164 return CMD_RET_USAGE;
Simon Glasse57f8d42022-10-29 19:47:17 -0600165
166 dev = NULL;
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700167 if (argc >= 2) {
Simon Glasse57f8d42022-10-29 19:47:17 -0600168 dev = parse_host_label(argv[1]);
169 if (!dev)
170 return CMD_RET_FAILURE;
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700171 }
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700172
Simon Glasse57f8d42022-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öm26f9a6c2013-11-10 10:26:56 -0700178
Simon Glasse57f8d42022-10-29 19:47:17 -0600179 uclass_id_foreach_dev(UCLASS_HOST, dev, uc)
180 show_host_dev(dev);
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700181 }
Simon Glasse57f8d42022-10-29 19:47:17 -0600182
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200183 return 0;
184}
185
Simon Glassed38aef2020-05-10 11:40:03 -0600186static int do_host_dev(struct cmd_tbl *cmdtp, int flag, int argc,
187 char *const argv[])
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200188{
Simon Glasse57f8d42022-10-29 19:47:17 -0600189 struct udevice *dev;
190 const char *label;
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200191
192 if (argc < 1 || argc > 3)
193 return CMD_RET_USAGE;
194
195 if (argc == 1) {
Simon Glasse57f8d42022-10-29 19:47:17 -0600196 struct host_sb_plat *plat;
197
198 dev = host_get_cur_dev();
199 if (!dev) {
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200200 printf("No current host device\n");
Simon Glasse57f8d42022-10-29 19:47:17 -0600201 return CMD_RET_FAILURE;
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200202 }
Simon Glasse57f8d42022-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 Simons3f6b8d82015-04-13 22:54:23 +0200206 return 0;
207 }
208
Simon Glasse57f8d42022-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 Simons3f6b8d82015-04-13 22:54:23 +0200213
Simon Glasse57f8d42022-10-29 19:47:17 -0600214 host_set_cur_dev(dev);
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200215
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700216 return 0;
217}
218
Simon Glassed38aef2020-05-10 11:40:03 -0600219static struct cmd_tbl cmd_host_sub[] = {
Sjoerd Simons609d7fc2015-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üns72acc652016-08-11 22:52:04 +0200223 U_BOOT_CMD_MKENT(size, 3, 0, do_host_size, "", ""),
Simon Glassb4d00772021-03-15 18:11:11 +1300224 U_BOOT_CMD_MKENT(bind, 4, 0, do_host_bind, "", ""),
Simon Glasse57f8d42022-10-29 19:47:17 -0600225 U_BOOT_CMD_MKENT(unbind, 4, 0, do_host_unbind, "", ""),
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200226 U_BOOT_CMD_MKENT(info, 3, 0, do_host_info, "", ""),
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200227 U_BOOT_CMD_MKENT(dev, 0, 1, do_host_dev, "", ""),
Simon Glassbf6ce792012-12-26 09:53:36 +0000228};
229
Simon Glassed38aef2020-05-10 11:40:03 -0600230static int do_host(struct cmd_tbl *cmdtp, int flag, int argc,
231 char *const argv[])
Simon Glassbf6ce792012-12-26 09:53:36 +0000232{
Simon Glassed38aef2020-05-10 11:40:03 -0600233 struct cmd_tbl *c;
Simon Glassbf6ce792012-12-26 09:53:36 +0000234
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200235 /* Skip past 'host' */
Simon Glassbf6ce792012-12-26 09:53:36 +0000236 argc--;
237 argv++;
238
Simon Glasse57f8d42022-10-29 19:47:17 -0600239 c = find_cmd_tbl(argv[0], cmd_host_sub, ARRAY_SIZE(cmd_host_sub));
Simon Glassbf6ce792012-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 Simons609d7fc2015-04-13 22:54:22 +0200248 host, 8, 1, do_host,
249 "Miscellaneous host commands",
Stephen Warrenbff6c312014-06-12 10:28:32 -0600250 "load hostfs - <addr> <filename> [<bytes> <offset>] - "
Simon Glassea307e82013-04-20 08:42:51 +0000251 "load a file from host\n"
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200252 "host ls hostfs - <filename> - list files on host\n"
253 "host save hostfs - <addr> <filename> <bytes> [<offset>] - "
Simon Glassea307e82013-04-20 08:42:51 +0000254 "save a file to host\n"
Sébastien Szymanskied6481e2017-01-19 17:49:13 +0100255 "host size hostfs - <filename> - determine size of file on host\n"
Simon Glasse57f8d42022-10-29 19:47:17 -0600256 "host bind [-r] <label> [<filename>] - bind \"host\" device to file\n"
Simon Glassb4d00772021-03-15 18:11:11 +1300257 " -r = mark as removable\n"
Simon Glasse57f8d42022-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 Simons609d7fc2015-04-13 22:54:22 +0200261 "host commands use the \"hostfs\" device. The \"host\" device is used\n"
Stephen Warrenbff6c312014-06-12 10:28:32 -0600262 "with standard IO commands such as fatls or ext2load"
Simon Glassbf6ce792012-12-26 09:53:36 +0000263);