blob: cd9c9677f0f0011f459e440ac7b6576341f95b55 [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>
11#include <sandboxblockdev.h>
Simon Glass336d4612020-02-03 07:36:16 -070012#include <dm/device_compat.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090013#include <linux/errno.h>
Simon Glassd3049312012-12-26 09:53:36 +000014
Sjoerd Simons9b97b6b2015-04-13 22:54:23 +020015static int host_curr_device = -1;
16
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{
44 if (argc < 2 || argc > 3)
45 return CMD_RET_USAGE;
46 char *ep;
47 char *dev_str = argv[1];
48 char *file = argc >= 3 ? argv[2] : NULL;
49 int dev = simple_strtoul(dev_str, &ep, 16);
50 if (*ep) {
51 printf("** Bad device specification %s **\n", dev_str);
52 return CMD_RET_USAGE;
53 }
54 return host_dev_bind(dev, file);
55}
56
Simon Glass09140112020-05-10 11:40:03 -060057static int do_host_info(struct cmd_tbl *cmdtp, int flag, int argc,
58 char *const argv[])
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070059{
60 if (argc < 1 || argc > 2)
61 return CMD_RET_USAGE;
62 int min_dev = 0;
63 int max_dev = CONFIG_HOST_MAX_DEVICES - 1;
64 if (argc >= 2) {
65 char *ep;
66 char *dev_str = argv[1];
67 int dev = simple_strtoul(dev_str, &ep, 16);
68 if (*ep) {
69 printf("** Bad device specification %s **\n", dev_str);
70 return CMD_RET_USAGE;
71 }
72 min_dev = dev;
73 max_dev = dev;
74 }
75 int dev;
76 printf("%3s %12s %s\n", "dev", "blocks", "path");
77 for (dev = min_dev; dev <= max_dev; dev++) {
Simon Glass4101f682016-02-29 15:25:34 -070078 struct blk_desc *blk_dev;
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070079 int ret;
80
81 printf("%3d ", dev);
82 ret = host_get_dev_err(dev, &blk_dev);
83 if (ret) {
84 if (ret == -ENOENT)
85 puts("Not bound to a backing file\n");
86 else if (ret == -ENODEV)
87 puts("Invalid host device number\n");
88
89 continue;
90 }
Simon Glass40fd0502016-02-29 15:25:57 -070091 struct host_block_dev *host_dev;
92
93#ifdef CONFIG_BLK
AKASHI Takahiro5bac9c52019-08-22 16:47:39 +090094 host_dev = dev_get_platdata(blk_dev->bdev);
Simon Glass40fd0502016-02-29 15:25:57 -070095#else
96 host_dev = blk_dev->priv;
97#endif
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070098 printf("%12lu %s\n", (unsigned long)blk_dev->lba,
99 host_dev->filename);
100 }
101 return 0;
102}
103
Simon Glass09140112020-05-10 11:40:03 -0600104static int do_host_dev(struct cmd_tbl *cmdtp, int flag, int argc,
105 char *const argv[])
Sjoerd Simons9b97b6b2015-04-13 22:54:23 +0200106{
107 int dev;
108 char *ep;
Simon Glass4101f682016-02-29 15:25:34 -0700109 struct blk_desc *blk_dev;
Sjoerd Simons9b97b6b2015-04-13 22:54:23 +0200110 int ret;
111
112 if (argc < 1 || argc > 3)
113 return CMD_RET_USAGE;
114
115 if (argc == 1) {
116 if (host_curr_device < 0) {
117 printf("No current host device\n");
118 return 1;
119 }
120 printf("Current host device %d\n", host_curr_device);
121 return 0;
122 }
123
124 dev = simple_strtoul(argv[1], &ep, 16);
125 if (*ep) {
126 printf("** Bad device specification %s **\n", argv[2]);
127 return CMD_RET_USAGE;
128 }
129
130 ret = host_get_dev_err(dev, &blk_dev);
131 if (ret) {
132 if (ret == -ENOENT)
133 puts("Not bound to a backing file\n");
134 else if (ret == -ENODEV)
135 puts("Invalid host device number\n");
136
137 return 1;
138 }
139
140 host_curr_device = dev;
141 return 0;
142}
143
Simon Glass09140112020-05-10 11:40:03 -0600144static struct cmd_tbl cmd_host_sub[] = {
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200145 U_BOOT_CMD_MKENT(load, 7, 0, do_host_load, "", ""),
146 U_BOOT_CMD_MKENT(ls, 3, 0, do_host_ls, "", ""),
147 U_BOOT_CMD_MKENT(save, 6, 0, do_host_save, "", ""),
Stefan Brüns49afb372016-08-11 22:52:04 +0200148 U_BOOT_CMD_MKENT(size, 3, 0, do_host_size, "", ""),
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200149 U_BOOT_CMD_MKENT(bind, 3, 0, do_host_bind, "", ""),
150 U_BOOT_CMD_MKENT(info, 3, 0, do_host_info, "", ""),
Sjoerd Simons9b97b6b2015-04-13 22:54:23 +0200151 U_BOOT_CMD_MKENT(dev, 0, 1, do_host_dev, "", ""),
Simon Glassd3049312012-12-26 09:53:36 +0000152};
153
Simon Glass09140112020-05-10 11:40:03 -0600154static int do_host(struct cmd_tbl *cmdtp, int flag, int argc,
155 char *const argv[])
Simon Glassd3049312012-12-26 09:53:36 +0000156{
Simon Glass09140112020-05-10 11:40:03 -0600157 struct cmd_tbl *c;
Simon Glassd3049312012-12-26 09:53:36 +0000158
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200159 /* Skip past 'host' */
Simon Glassd3049312012-12-26 09:53:36 +0000160 argc--;
161 argv++;
162
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200163 c = find_cmd_tbl(argv[0], cmd_host_sub,
164 ARRAY_SIZE(cmd_host_sub));
Simon Glassd3049312012-12-26 09:53:36 +0000165
166 if (c)
167 return c->cmd(cmdtp, flag, argc, argv);
168 else
169 return CMD_RET_USAGE;
170}
171
172U_BOOT_CMD(
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200173 host, 8, 1, do_host,
174 "Miscellaneous host commands",
Stephen Warren4d907022014-06-12 10:28:32 -0600175 "load hostfs - <addr> <filename> [<bytes> <offset>] - "
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000176 "load a file from host\n"
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200177 "host ls hostfs - <filename> - list files on host\n"
178 "host save hostfs - <addr> <filename> <bytes> [<offset>] - "
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000179 "save a file to host\n"
Sébastien Szymanski6baa6922017-01-19 17:49:13 +0100180 "host size hostfs - <filename> - determine size of file on host\n"
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200181 "host bind <dev> [<filename>] - bind \"host\" device to file\n"
182 "host info [<dev>] - show device binding & info\n"
Sjoerd Simons9b97b6b2015-04-13 22:54:23 +0200183 "host dev [<dev>] - Set or retrieve the current host device\n"
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200184 "host commands use the \"hostfs\" device. The \"host\" device is used\n"
Stephen Warren4d907022014-06-12 10:28:32 -0600185 "with standard IO commands such as fatls or ext2load"
Simon Glassd3049312012-12-26 09:53:36 +0000186);