blob: 6aa3d9167a49a73528d3e1dddc963170bc718058 [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>
11#include <sandboxblockdev.h>
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <dm/device_compat.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090013#include <linux/errno.h>
Simon Glassbf6ce792012-12-26 09:53:36 +000014
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +020015static int host_curr_device = -1;
16
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 Glass8f5fdf32021-03-15 18:11:09 +130045 const char *dev_str;
46 char *file;
47 char *ep;
48 int dev;
49
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 Glassf0a45382021-03-15 18:11:10 +130064 dev_str = argv[0];
Simon Glass8f5fdf32021-03-15 18:11:09 +130065 dev = simple_strtoul(dev_str, &ep, 16);
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070066 if (*ep) {
67 printf("** Bad device specification %s **\n", dev_str);
68 return CMD_RET_USAGE;
69 }
Simon Glassf0a45382021-03-15 18:11:10 +130070 file = argc > 1 ? argv[1] : NULL;
Simon Glass8f5fdf32021-03-15 18:11:09 +130071
Simon Glassb4d00772021-03-15 18:11:11 +130072 return !!host_dev_bind(dev, file, removable);
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070073}
74
Simon Glassed38aef2020-05-10 11:40:03 -060075static int do_host_info(struct cmd_tbl *cmdtp, int flag, int argc,
76 char *const argv[])
Henrik Nordström26f9a6c2013-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 Glasse3394752016-02-29 15:25:34 -070096 struct blk_desc *blk_dev;
Henrik Nordström26f9a6c2013-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 Glass2c2075a2016-02-29 15:25:57 -0700109 struct host_block_dev *host_dev;
110
111#ifdef CONFIG_BLK
Simon Glassfa20e932020-12-03 16:55:20 -0700112 host_dev = dev_get_plat(blk_dev->bdev);
Simon Glass2c2075a2016-02-29 15:25:57 -0700113#else
114 host_dev = blk_dev->priv;
115#endif
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700116 printf("%12lu %s\n", (unsigned long)blk_dev->lba,
117 host_dev->filename);
118 }
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200119 return 0;
120}
121
Simon Glassed38aef2020-05-10 11:40:03 -0600122static int do_host_dev(struct cmd_tbl *cmdtp, int flag, int argc,
123 char *const argv[])
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200124{
125 int dev;
126 char *ep;
Simon Glasse3394752016-02-29 15:25:34 -0700127 struct blk_desc *blk_dev;
Sjoerd Simons3f6b8d82015-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;
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700159 return 0;
160}
161
Simon Glassed38aef2020-05-10 11:40:03 -0600162static struct cmd_tbl cmd_host_sub[] = {
Sjoerd Simons609d7fc2015-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üns72acc652016-08-11 22:52:04 +0200166 U_BOOT_CMD_MKENT(size, 3, 0, do_host_size, "", ""),
Simon Glassb4d00772021-03-15 18:11:11 +1300167 U_BOOT_CMD_MKENT(bind, 4, 0, do_host_bind, "", ""),
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200168 U_BOOT_CMD_MKENT(info, 3, 0, do_host_info, "", ""),
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200169 U_BOOT_CMD_MKENT(dev, 0, 1, do_host_dev, "", ""),
Simon Glassbf6ce792012-12-26 09:53:36 +0000170};
171
Simon Glassed38aef2020-05-10 11:40:03 -0600172static int do_host(struct cmd_tbl *cmdtp, int flag, int argc,
173 char *const argv[])
Simon Glassbf6ce792012-12-26 09:53:36 +0000174{
Simon Glassed38aef2020-05-10 11:40:03 -0600175 struct cmd_tbl *c;
Simon Glassbf6ce792012-12-26 09:53:36 +0000176
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200177 /* Skip past 'host' */
Simon Glassbf6ce792012-12-26 09:53:36 +0000178 argc--;
179 argv++;
180
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200181 c = find_cmd_tbl(argv[0], cmd_host_sub,
182 ARRAY_SIZE(cmd_host_sub));
Simon Glassbf6ce792012-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 Simons609d7fc2015-04-13 22:54:22 +0200191 host, 8, 1, do_host,
192 "Miscellaneous host commands",
Stephen Warrenbff6c312014-06-12 10:28:32 -0600193 "load hostfs - <addr> <filename> [<bytes> <offset>] - "
Simon Glassea307e82013-04-20 08:42:51 +0000194 "load a file from host\n"
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200195 "host ls hostfs - <filename> - list files on host\n"
196 "host save hostfs - <addr> <filename> <bytes> [<offset>] - "
Simon Glassea307e82013-04-20 08:42:51 +0000197 "save a file to host\n"
Sébastien Szymanskied6481e2017-01-19 17:49:13 +0100198 "host size hostfs - <filename> - determine size of file on host\n"
Simon Glassb4d00772021-03-15 18:11:11 +1300199 "host bind [-r] <dev> [<filename>] - bind \"host\" device to file\n"
200 " -r = mark as removable\n"
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200201 "host info [<dev>] - show device binding & info\n"
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200202 "host dev [<dev>] - Set or retrieve the current host device\n"
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200203 "host commands use the \"hostfs\" device. The \"host\" device is used\n"
Stephen Warrenbff6c312014-06-12 10:28:32 -0600204 "with standard IO commands such as fatls or ext2load"
Simon Glassbf6ce792012-12-26 09:53:36 +0000205);