blob: c33c2a9787eb36562c66d9267d1d9eea22ba5d93 [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>
Bin Meng881e4752023-09-26 16:43:33 +080016#include <linux/log2.h>
Simon Glassbf6ce792012-12-26 09:53:36 +000017
Simon Glassed38aef2020-05-10 11:40:03 -060018static int do_host_load(struct cmd_tbl *cmdtp, int flag, int argc,
19 char *const argv[])
Simon Glassbf6ce792012-12-26 09:53:36 +000020{
Tom Rini089078a2013-10-10 10:46:22 -040021 return do_load(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
Simon Glassbf6ce792012-12-26 09:53:36 +000022}
23
Simon Glassed38aef2020-05-10 11:40:03 -060024static int do_host_ls(struct cmd_tbl *cmdtp, int flag, int argc,
25 char *const argv[])
Simon Glassbf6ce792012-12-26 09:53:36 +000026{
27 return do_ls(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
28}
29
Simon Glassed38aef2020-05-10 11:40:03 -060030static int do_host_size(struct cmd_tbl *cmdtp, int flag, int argc,
31 char *const argv[])
Stefan Brüns72acc652016-08-11 22:52:04 +020032{
33 return do_size(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
34}
35
Simon Glassed38aef2020-05-10 11:40:03 -060036static int do_host_save(struct cmd_tbl *cmdtp, int flag, int argc,
37 char *const argv[])
Simon Glassea307e82013-04-20 08:42:51 +000038{
Tom Rini089078a2013-10-10 10:46:22 -040039 return do_save(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
Simon Glassea307e82013-04-20 08:42:51 +000040}
41
Simon Glassed38aef2020-05-10 11:40:03 -060042static int do_host_bind(struct cmd_tbl *cmdtp, int flag, int argc,
43 char *const argv[])
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070044{
Simon Glassb4d00772021-03-15 18:11:11 +130045 bool removable = false;
Simon Glasse57f8d42022-10-29 19:47:17 -060046 struct udevice *dev;
47 const char *label;
Simon Glass8f5fdf32021-03-15 18:11:09 +130048 char *file;
Bin Meng881e4752023-09-26 16:43:33 +080049 unsigned long blksz = DEFAULT_BLKSZ;
Simon Glasse57f8d42022-10-29 19:47:17 -060050 int ret;
Simon Glass8f5fdf32021-03-15 18:11:09 +130051
Simon Glassf0a45382021-03-15 18:11:10 +130052 /* Skip 'bind' */
53 argc--;
54 argv++;
Simon Glassb4d00772021-03-15 18:11:11 +130055 if (argc < 2)
56 return CMD_RET_USAGE;
57
58 if (!strcmp(argv[0], "-r")) {
59 removable = true;
60 argc--;
61 argv++;
62 }
63
Bin Meng881e4752023-09-26 16:43:33 +080064 if (argc < 2 || argc > 3)
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070065 return CMD_RET_USAGE;
Simon Glasse57f8d42022-10-29 19:47:17 -060066 label = argv[0];
Bin Mengc3e784c2023-09-26 16:43:32 +080067 file = argv[1];
Bin Meng881e4752023-09-26 16:43:33 +080068 if (argc > 2) {
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;
73 }
74 }
Simon Glasse57f8d42022-10-29 19:47:17 -060075
Bin Meng881e4752023-09-26 16:43:33 +080076 ret = host_create_attach_file(label, file, removable, blksz, &dev);
Simon Glasse57f8d42022-10-29 19:47:17 -060077 if (ret) {
78 printf("Cannot create device / bind file\n");
79 return CMD_RET_FAILURE;
80 }
81
82 return 0;
83}
84
85/**
86 * parse_host_label() - Parse a device label or sequence number
87 *
88 * This shows an error if it returns NULL
89 *
90 * @label: String containing the label or sequence number
91 * Returns: Associated device, or NULL if not found
92 */
93static struct udevice *parse_host_label(const char *label)
94{
95 struct udevice *dev;
96
97 dev = host_find_by_label(label);
98 if (!dev) {
99 int devnum;
100 char *ep;
101
102 devnum = hextoul(label, &ep);
103 if (*ep ||
104 uclass_find_device_by_seq(UCLASS_HOST, devnum, &dev)) {
105 printf("No such device '%s'\n", label);
106 return NULL;
107 }
108 }
109
110 return dev;
111}
112
113static int do_host_unbind(struct cmd_tbl *cmdtp, int flag, int argc,
114 char *const argv[])
115{
116 struct udevice *dev;
117 const char *label;
118 int ret;
119
120 if (argc < 2)
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700121 return CMD_RET_USAGE;
Simon Glasse57f8d42022-10-29 19:47:17 -0600122
123 label = argv[1];
124 dev = parse_host_label(label);
125 if (!dev)
126 return CMD_RET_FAILURE;
127
128 ret = host_detach_file(dev);
129 if (ret) {
130 printf("Cannot detach file (err=%d)\n", ret);
131 return CMD_RET_FAILURE;
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700132 }
Simon Glasse57f8d42022-10-29 19:47:17 -0600133
134 ret = device_unbind(dev);
135 if (ret) {
136 printf("Cannot attach file\n");
137 ret = device_unbind(dev);
138 if (ret)
139 printf("Cannot unbind device '%s'\n", dev->name);
140 return CMD_RET_FAILURE;
141 }
Simon Glass8f5fdf32021-03-15 18:11:09 +1300142
Simon Glasse57f8d42022-10-29 19:47:17 -0600143 return 0;
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700144}
145
Simon Glasse57f8d42022-10-29 19:47:17 -0600146static void show_host_dev(struct udevice *dev)
147{
148 struct host_sb_plat *plat = dev_get_plat(dev);
149 struct blk_desc *desc;
150 struct udevice *blk;
151 int ret;
152
153 printf("%3d ", dev_seq(dev));
154 if (!plat->fd) {
155 printf("Not bound to a backing file\n");
156 return;
157 }
158 ret = blk_get_from_parent(dev, &blk);
159 if (ret) /* cannot happen */
160 return;
161
162 desc = dev_get_uclass_plat(blk);
Bin Mengc4373782023-09-26 16:43:36 +0800163 printf("%12lu %6lu %-15s %s\n", (unsigned long)desc->lba, desc->blksz,
164 plat->label, plat->filename);
Simon Glasse57f8d42022-10-29 19:47:17 -0600165}
166
Simon Glassed38aef2020-05-10 11:40:03 -0600167static int do_host_info(struct cmd_tbl *cmdtp, int flag, int argc,
168 char *const argv[])
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700169{
Simon Glasse57f8d42022-10-29 19:47:17 -0600170 struct udevice *dev;
171
172 if (argc < 1)
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700173 return CMD_RET_USAGE;
Simon Glasse57f8d42022-10-29 19:47:17 -0600174
175 dev = NULL;
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700176 if (argc >= 2) {
Simon Glasse57f8d42022-10-29 19:47:17 -0600177 dev = parse_host_label(argv[1]);
178 if (!dev)
179 return CMD_RET_FAILURE;
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700180 }
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700181
Bin Mengc4373782023-09-26 16:43:36 +0800182 printf("%3s %12s %6s %-15s %s\n",
183 "dev", "blocks", "blksz", "label", "path");
Simon Glasse57f8d42022-10-29 19:47:17 -0600184 if (dev) {
185 show_host_dev(dev);
186 } else {
187 struct uclass *uc;
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700188
Simon Glasse57f8d42022-10-29 19:47:17 -0600189 uclass_id_foreach_dev(UCLASS_HOST, dev, uc)
190 show_host_dev(dev);
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700191 }
Simon Glasse57f8d42022-10-29 19:47:17 -0600192
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200193 return 0;
194}
195
Simon Glassed38aef2020-05-10 11:40:03 -0600196static int do_host_dev(struct cmd_tbl *cmdtp, int flag, int argc,
197 char *const argv[])
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200198{
Simon Glasse57f8d42022-10-29 19:47:17 -0600199 struct udevice *dev;
200 const char *label;
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200201
202 if (argc < 1 || argc > 3)
203 return CMD_RET_USAGE;
204
205 if (argc == 1) {
Simon Glasse57f8d42022-10-29 19:47:17 -0600206 struct host_sb_plat *plat;
207
208 dev = host_get_cur_dev();
209 if (!dev) {
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200210 printf("No current host device\n");
Simon Glasse57f8d42022-10-29 19:47:17 -0600211 return CMD_RET_FAILURE;
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200212 }
Simon Glasse57f8d42022-10-29 19:47:17 -0600213 plat = dev_get_plat(dev);
214 printf("Current host device: %d: %s\n", dev_seq(dev),
215 plat->label);
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200216 return 0;
217 }
218
Simon Glasse57f8d42022-10-29 19:47:17 -0600219 label = argv[1];
220 dev = parse_host_label(argv[1]);
221 if (!dev)
222 return CMD_RET_FAILURE;
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200223
Simon Glasse57f8d42022-10-29 19:47:17 -0600224 host_set_cur_dev(dev);
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200225
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700226 return 0;
227}
228
Simon Glassed38aef2020-05-10 11:40:03 -0600229static struct cmd_tbl cmd_host_sub[] = {
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200230 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, "", ""),
Stefan Brüns72acc652016-08-11 22:52:04 +0200233 U_BOOT_CMD_MKENT(size, 3, 0, do_host_size, "", ""),
Simon Glassb4d00772021-03-15 18:11:11 +1300234 U_BOOT_CMD_MKENT(bind, 4, 0, do_host_bind, "", ""),
Simon Glasse57f8d42022-10-29 19:47:17 -0600235 U_BOOT_CMD_MKENT(unbind, 4, 0, do_host_unbind, "", ""),
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200236 U_BOOT_CMD_MKENT(info, 3, 0, do_host_info, "", ""),
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200237 U_BOOT_CMD_MKENT(dev, 0, 1, do_host_dev, "", ""),
Simon Glassbf6ce792012-12-26 09:53:36 +0000238};
239
Simon Glassed38aef2020-05-10 11:40:03 -0600240static int do_host(struct cmd_tbl *cmdtp, int flag, int argc,
241 char *const argv[])
Simon Glassbf6ce792012-12-26 09:53:36 +0000242{
Simon Glassed38aef2020-05-10 11:40:03 -0600243 struct cmd_tbl *c;
Simon Glassbf6ce792012-12-26 09:53:36 +0000244
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200245 /* Skip past 'host' */
Simon Glassbf6ce792012-12-26 09:53:36 +0000246 argc--;
247 argv++;
248
Simon Glasse57f8d42022-10-29 19:47:17 -0600249 c = find_cmd_tbl(argv[0], cmd_host_sub, ARRAY_SIZE(cmd_host_sub));
Simon Glassbf6ce792012-12-26 09:53:36 +0000250
251 if (c)
252 return c->cmd(cmdtp, flag, argc, argv);
253 else
254 return CMD_RET_USAGE;
255}
256
257U_BOOT_CMD(
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200258 host, 8, 1, do_host,
259 "Miscellaneous host commands",
Stephen Warrenbff6c312014-06-12 10:28:32 -0600260 "load hostfs - <addr> <filename> [<bytes> <offset>] - "
Simon Glassea307e82013-04-20 08:42:51 +0000261 "load a file from host\n"
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200262 "host ls hostfs - <filename> - list files on host\n"
263 "host save hostfs - <addr> <filename> <bytes> [<offset>] - "
Simon Glassea307e82013-04-20 08:42:51 +0000264 "save a file to host\n"
Sébastien Szymanskied6481e2017-01-19 17:49:13 +0100265 "host size hostfs - <filename> - determine size of file on host\n"
Bin Meng881e4752023-09-26 16:43:33 +0800266 "host bind [-r] <label> <filename> [<blksz>] - bind \"host\" device to file,\n"
267 " and optionally set the device's logical block size\n"
Simon Glassb4d00772021-03-15 18:11:11 +1300268 " -r = mark as removable\n"
Simon Glasse57f8d42022-10-29 19:47:17 -0600269 "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"
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200272 "host commands use the \"hostfs\" device. The \"host\" device is used\n"
Stephen Warrenbff6c312014-06-12 10:28:32 -0600273 "with standard IO commands such as fatls or ext2load"
Simon Glassbf6ce792012-12-26 09:53:36 +0000274);