blob: eefc4f255ef2fcfe3b69f57cc4ce789ca033bd08 [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 Glass2c2075a2016-02-29 15:25:57 -07007#include <dm.h>
Simon Glassbf6ce792012-12-26 09:53:36 +00008#include <fs.h>
Henrik Nordström26f9a6c2013-11-10 10:26:56 -07009#include <part.h>
10#include <sandboxblockdev.h>
Simon Glass9bc15642020-02-03 07:36:16 -070011#include <dm/device_compat.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090012#include <linux/errno.h>
Simon Glassbf6ce792012-12-26 09:53:36 +000013
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +020014static int host_curr_device = -1;
15
Sjoerd Simons609d7fc2015-04-13 22:54:22 +020016static int do_host_load(cmd_tbl_t *cmdtp, int flag, int argc,
Simon Glassbf6ce792012-12-26 09:53:36 +000017 char * const argv[])
18{
Tom Rini089078a2013-10-10 10:46:22 -040019 return do_load(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
Simon Glassbf6ce792012-12-26 09:53:36 +000020}
21
Sjoerd Simons609d7fc2015-04-13 22:54:22 +020022static int do_host_ls(cmd_tbl_t *cmdtp, int flag, int argc,
Simon Glassbf6ce792012-12-26 09:53:36 +000023 char * const argv[])
24{
25 return do_ls(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
26}
27
Stefan Brüns72acc652016-08-11 22:52:04 +020028static int do_host_size(cmd_tbl_t *cmdtp, int flag, int argc,
29 char * const argv[])
30{
31 return do_size(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
32}
33
Sjoerd Simons609d7fc2015-04-13 22:54:22 +020034static int do_host_save(cmd_tbl_t *cmdtp, int flag, int argc,
Simon Glassea307e82013-04-20 08:42:51 +000035 char * const argv[])
36{
Tom Rini089078a2013-10-10 10:46:22 -040037 return do_save(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
Simon Glassea307e82013-04-20 08:42:51 +000038}
39
Sjoerd Simons609d7fc2015-04-13 22:54:22 +020040static int do_host_bind(cmd_tbl_t *cmdtp, int flag, int argc,
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070041 char * const argv[])
42{
43 if (argc < 2 || argc > 3)
44 return CMD_RET_USAGE;
45 char *ep;
46 char *dev_str = argv[1];
47 char *file = argc >= 3 ? argv[2] : NULL;
48 int dev = simple_strtoul(dev_str, &ep, 16);
49 if (*ep) {
50 printf("** Bad device specification %s **\n", dev_str);
51 return CMD_RET_USAGE;
52 }
53 return host_dev_bind(dev, file);
54}
55
Sjoerd Simons609d7fc2015-04-13 22:54:22 +020056static int do_host_info(cmd_tbl_t *cmdtp, int flag, int argc,
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070057 char * const argv[])
58{
59 if (argc < 1 || argc > 2)
60 return CMD_RET_USAGE;
61 int min_dev = 0;
62 int max_dev = CONFIG_HOST_MAX_DEVICES - 1;
63 if (argc >= 2) {
64 char *ep;
65 char *dev_str = argv[1];
66 int dev = simple_strtoul(dev_str, &ep, 16);
67 if (*ep) {
68 printf("** Bad device specification %s **\n", dev_str);
69 return CMD_RET_USAGE;
70 }
71 min_dev = dev;
72 max_dev = dev;
73 }
74 int dev;
75 printf("%3s %12s %s\n", "dev", "blocks", "path");
76 for (dev = min_dev; dev <= max_dev; dev++) {
Simon Glasse3394752016-02-29 15:25:34 -070077 struct blk_desc *blk_dev;
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070078 int ret;
79
80 printf("%3d ", dev);
81 ret = host_get_dev_err(dev, &blk_dev);
82 if (ret) {
83 if (ret == -ENOENT)
84 puts("Not bound to a backing file\n");
85 else if (ret == -ENODEV)
86 puts("Invalid host device number\n");
87
88 continue;
89 }
Simon Glass2c2075a2016-02-29 15:25:57 -070090 struct host_block_dev *host_dev;
91
92#ifdef CONFIG_BLK
AKASHI Takahiro23b13492019-08-22 16:47:39 +090093 host_dev = dev_get_platdata(blk_dev->bdev);
Simon Glass2c2075a2016-02-29 15:25:57 -070094#else
95 host_dev = blk_dev->priv;
96#endif
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070097 printf("%12lu %s\n", (unsigned long)blk_dev->lba,
98 host_dev->filename);
99 }
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200100 return 0;
101}
102
103static int do_host_dev(cmd_tbl_t *cmdtp, int flag, int argc,
104 char * const argv[])
105{
106 int dev;
107 char *ep;
Simon Glasse3394752016-02-29 15:25:34 -0700108 struct blk_desc *blk_dev;
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200109 int ret;
110
111 if (argc < 1 || argc > 3)
112 return CMD_RET_USAGE;
113
114 if (argc == 1) {
115 if (host_curr_device < 0) {
116 printf("No current host device\n");
117 return 1;
118 }
119 printf("Current host device %d\n", host_curr_device);
120 return 0;
121 }
122
123 dev = simple_strtoul(argv[1], &ep, 16);
124 if (*ep) {
125 printf("** Bad device specification %s **\n", argv[2]);
126 return CMD_RET_USAGE;
127 }
128
129 ret = host_get_dev_err(dev, &blk_dev);
130 if (ret) {
131 if (ret == -ENOENT)
132 puts("Not bound to a backing file\n");
133 else if (ret == -ENODEV)
134 puts("Invalid host device number\n");
135
136 return 1;
137 }
138
139 host_curr_device = dev;
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700140 return 0;
141}
142
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200143static cmd_tbl_t cmd_host_sub[] = {
144 U_BOOT_CMD_MKENT(load, 7, 0, do_host_load, "", ""),
145 U_BOOT_CMD_MKENT(ls, 3, 0, do_host_ls, "", ""),
146 U_BOOT_CMD_MKENT(save, 6, 0, do_host_save, "", ""),
Stefan Brüns72acc652016-08-11 22:52:04 +0200147 U_BOOT_CMD_MKENT(size, 3, 0, do_host_size, "", ""),
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200148 U_BOOT_CMD_MKENT(bind, 3, 0, do_host_bind, "", ""),
149 U_BOOT_CMD_MKENT(info, 3, 0, do_host_info, "", ""),
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200150 U_BOOT_CMD_MKENT(dev, 0, 1, do_host_dev, "", ""),
Simon Glassbf6ce792012-12-26 09:53:36 +0000151};
152
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200153static int do_host(cmd_tbl_t *cmdtp, int flag, int argc,
Simon Glassbf6ce792012-12-26 09:53:36 +0000154 char * const argv[])
155{
156 cmd_tbl_t *c;
157
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200158 /* Skip past 'host' */
Simon Glassbf6ce792012-12-26 09:53:36 +0000159 argc--;
160 argv++;
161
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200162 c = find_cmd_tbl(argv[0], cmd_host_sub,
163 ARRAY_SIZE(cmd_host_sub));
Simon Glassbf6ce792012-12-26 09:53:36 +0000164
165 if (c)
166 return c->cmd(cmdtp, flag, argc, argv);
167 else
168 return CMD_RET_USAGE;
169}
170
171U_BOOT_CMD(
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200172 host, 8, 1, do_host,
173 "Miscellaneous host commands",
Stephen Warrenbff6c312014-06-12 10:28:32 -0600174 "load hostfs - <addr> <filename> [<bytes> <offset>] - "
Simon Glassea307e82013-04-20 08:42:51 +0000175 "load a file from host\n"
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200176 "host ls hostfs - <filename> - list files on host\n"
177 "host save hostfs - <addr> <filename> <bytes> [<offset>] - "
Simon Glassea307e82013-04-20 08:42:51 +0000178 "save a file to host\n"
Sébastien Szymanskied6481e2017-01-19 17:49:13 +0100179 "host size hostfs - <filename> - determine size of file on host\n"
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200180 "host bind <dev> [<filename>] - bind \"host\" device to file\n"
181 "host info [<dev>] - show device binding & info\n"
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200182 "host dev [<dev>] - Set or retrieve the current host device\n"
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200183 "host commands use the \"hostfs\" device. The \"host\" device is used\n"
Stephen Warrenbff6c312014-06-12 10:28:32 -0600184 "with standard IO commands such as fatls or ext2load"
Simon Glassbf6ce792012-12-26 09:53:36 +0000185);