blob: 6aa3d9167a49a73528d3e1dddc963170bc718058 [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{
Simon Glass17585512021-03-15 18:11:11 +130044 bool removable = false;
Simon Glass1503c2c2021-03-15 18:11:09 +130045 const char *dev_str;
46 char *file;
47 char *ep;
48 int dev;
49
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 Glass803e9c12021-03-15 18:11:10 +130064 dev_str = argv[0];
Simon Glass1503c2c2021-03-15 18:11:09 +130065 dev = simple_strtoul(dev_str, &ep, 16);
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070066 if (*ep) {
67 printf("** Bad device specification %s **\n", dev_str);
68 return CMD_RET_USAGE;
69 }
Simon Glass803e9c12021-03-15 18:11:10 +130070 file = argc > 1 ? argv[1] : NULL;
Simon Glass1503c2c2021-03-15 18:11:09 +130071
Simon Glass17585512021-03-15 18:11:11 +130072 return !!host_dev_bind(dev, file, removable);
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070073}
74
Simon Glass09140112020-05-10 11:40:03 -060075static int do_host_info(struct cmd_tbl *cmdtp, int flag, int argc,
76 char *const argv[])
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070077{
78 if (argc < 1 || argc > 2)
79 return CMD_RET_USAGE;
80 int min_dev = 0;
81 int max_dev = CONFIG_HOST_MAX_DEVICES - 1;
82 if (argc >= 2) {
83 char *ep;
84 char *dev_str = argv[1];
85 int dev = simple_strtoul(dev_str, &ep, 16);
86 if (*ep) {
87 printf("** Bad device specification %s **\n", dev_str);
88 return CMD_RET_USAGE;
89 }
90 min_dev = dev;
91 max_dev = dev;
92 }
93 int dev;
94 printf("%3s %12s %s\n", "dev", "blocks", "path");
95 for (dev = min_dev; dev <= max_dev; dev++) {
Simon Glass4101f682016-02-29 15:25:34 -070096 struct blk_desc *blk_dev;
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070097 int ret;
98
99 printf("%3d ", dev);
100 ret = host_get_dev_err(dev, &blk_dev);
101 if (ret) {
102 if (ret == -ENOENT)
103 puts("Not bound to a backing file\n");
104 else if (ret == -ENODEV)
105 puts("Invalid host device number\n");
106
107 continue;
108 }
Simon Glass40fd0502016-02-29 15:25:57 -0700109 struct host_block_dev *host_dev;
110
111#ifdef CONFIG_BLK
Simon Glassc69cda22020-12-03 16:55:20 -0700112 host_dev = dev_get_plat(blk_dev->bdev);
Simon Glass40fd0502016-02-29 15:25:57 -0700113#else
114 host_dev = blk_dev->priv;
115#endif
Henrik Nordströmf4d8de42013-11-10 10:26:56 -0700116 printf("%12lu %s\n", (unsigned long)blk_dev->lba,
117 host_dev->filename);
118 }
119 return 0;
120}
121
Simon Glass09140112020-05-10 11:40:03 -0600122static int do_host_dev(struct cmd_tbl *cmdtp, int flag, int argc,
123 char *const argv[])
Sjoerd Simons9b97b6b2015-04-13 22:54:23 +0200124{
125 int dev;
126 char *ep;
Simon Glass4101f682016-02-29 15:25:34 -0700127 struct blk_desc *blk_dev;
Sjoerd Simons9b97b6b2015-04-13 22:54:23 +0200128 int ret;
129
130 if (argc < 1 || argc > 3)
131 return CMD_RET_USAGE;
132
133 if (argc == 1) {
134 if (host_curr_device < 0) {
135 printf("No current host device\n");
136 return 1;
137 }
138 printf("Current host device %d\n", host_curr_device);
139 return 0;
140 }
141
142 dev = simple_strtoul(argv[1], &ep, 16);
143 if (*ep) {
144 printf("** Bad device specification %s **\n", argv[2]);
145 return CMD_RET_USAGE;
146 }
147
148 ret = host_get_dev_err(dev, &blk_dev);
149 if (ret) {
150 if (ret == -ENOENT)
151 puts("Not bound to a backing file\n");
152 else if (ret == -ENODEV)
153 puts("Invalid host device number\n");
154
155 return 1;
156 }
157
158 host_curr_device = dev;
159 return 0;
160}
161
Simon Glass09140112020-05-10 11:40:03 -0600162static struct cmd_tbl cmd_host_sub[] = {
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200163 U_BOOT_CMD_MKENT(load, 7, 0, do_host_load, "", ""),
164 U_BOOT_CMD_MKENT(ls, 3, 0, do_host_ls, "", ""),
165 U_BOOT_CMD_MKENT(save, 6, 0, do_host_save, "", ""),
Stefan Brüns49afb372016-08-11 22:52:04 +0200166 U_BOOT_CMD_MKENT(size, 3, 0, do_host_size, "", ""),
Simon Glass17585512021-03-15 18:11:11 +1300167 U_BOOT_CMD_MKENT(bind, 4, 0, do_host_bind, "", ""),
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200168 U_BOOT_CMD_MKENT(info, 3, 0, do_host_info, "", ""),
Sjoerd Simons9b97b6b2015-04-13 22:54:23 +0200169 U_BOOT_CMD_MKENT(dev, 0, 1, do_host_dev, "", ""),
Simon Glassd3049312012-12-26 09:53:36 +0000170};
171
Simon Glass09140112020-05-10 11:40:03 -0600172static int do_host(struct cmd_tbl *cmdtp, int flag, int argc,
173 char *const argv[])
Simon Glassd3049312012-12-26 09:53:36 +0000174{
Simon Glass09140112020-05-10 11:40:03 -0600175 struct cmd_tbl *c;
Simon Glassd3049312012-12-26 09:53:36 +0000176
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200177 /* Skip past 'host' */
Simon Glassd3049312012-12-26 09:53:36 +0000178 argc--;
179 argv++;
180
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200181 c = find_cmd_tbl(argv[0], cmd_host_sub,
182 ARRAY_SIZE(cmd_host_sub));
Simon Glassd3049312012-12-26 09:53:36 +0000183
184 if (c)
185 return c->cmd(cmdtp, flag, argc, argv);
186 else
187 return CMD_RET_USAGE;
188}
189
190U_BOOT_CMD(
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200191 host, 8, 1, do_host,
192 "Miscellaneous host commands",
Stephen Warren4d907022014-06-12 10:28:32 -0600193 "load hostfs - <addr> <filename> [<bytes> <offset>] - "
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000194 "load a file from host\n"
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200195 "host ls hostfs - <filename> - list files on host\n"
196 "host save hostfs - <addr> <filename> <bytes> [<offset>] - "
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000197 "save a file to host\n"
Sébastien Szymanski6baa6922017-01-19 17:49:13 +0100198 "host size hostfs - <filename> - determine size of file on host\n"
Simon Glass17585512021-03-15 18:11:11 +1300199 "host bind [-r] <dev> [<filename>] - bind \"host\" device to file\n"
200 " -r = mark as removable\n"
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200201 "host info [<dev>] - show device binding & info\n"
Sjoerd Simons9b97b6b2015-04-13 22:54:23 +0200202 "host dev [<dev>] - Set or retrieve the current host device\n"
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200203 "host commands use the \"hostfs\" device. The \"host\" device is used\n"
Stephen Warren4d907022014-06-12 10:28:32 -0600204 "with standard IO commands such as fatls or ext2load"
Simon Glassd3049312012-12-26 09:53:36 +0000205);