blob: 080b7cfb1d8243dcfbcb091c704486723a160602 [file] [log] [blame]
Simon Glassd3049312012-12-26 09:53:36 +00001/*
2 * Copyright (c) 2012, Google Inc.
3 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Simon Glassd3049312012-12-26 09:53:36 +00005 */
6
7#include <common.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>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090012#include <linux/errno.h>
Simon Glassd3049312012-12-26 09:53:36 +000013
Sjoerd Simons9b97b6b2015-04-13 22:54:23 +020014static int host_curr_device = -1;
15
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +020016static int do_host_load(cmd_tbl_t *cmdtp, int flag, int argc,
Simon Glassd3049312012-12-26 09:53:36 +000017 char * const argv[])
18{
Tom Rini9ade4852013-10-10 10:46:22 -040019 return do_load(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
Simon Glassd3049312012-12-26 09:53:36 +000020}
21
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +020022static int do_host_ls(cmd_tbl_t *cmdtp, int flag, int argc,
Simon Glassd3049312012-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üns49afb372016-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 Simonsbacfb1d2015-04-13 22:54:22 +020034static int do_host_save(cmd_tbl_t *cmdtp, int flag, int argc,
Simon Glass7eb2c8d2013-04-20 08:42:51 +000035 char * const argv[])
36{
Tom Rini9ade4852013-10-10 10:46:22 -040037 return do_save(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
Simon Glass7eb2c8d2013-04-20 08:42:51 +000038}
39
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +020040static int do_host_bind(cmd_tbl_t *cmdtp, int flag, int argc,
Henrik Nordströmf4d8de42013-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 Simonsbacfb1d2015-04-13 22:54:22 +020056static int do_host_info(cmd_tbl_t *cmdtp, int flag, int argc,
Henrik Nordströmf4d8de42013-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 Glass4101f682016-02-29 15:25:34 -070077 struct blk_desc *blk_dev;
Henrik Nordströmf4d8de42013-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 Glass40fd0502016-02-29 15:25:57 -070090 struct host_block_dev *host_dev;
91
92#ifdef CONFIG_BLK
93 host_dev = dev_get_priv(blk_dev->bdev);
94#else
95 host_dev = blk_dev->priv;
96#endif
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070097 printf("%12lu %s\n", (unsigned long)blk_dev->lba,
98 host_dev->filename);
99 }
100 return 0;
101}
102
Sjoerd Simons9b97b6b2015-04-13 22:54:23 +0200103static int do_host_dev(cmd_tbl_t *cmdtp, int flag, int argc,
104 char * const argv[])
105{
106 int dev;
107 char *ep;
Simon Glass4101f682016-02-29 15:25:34 -0700108 struct blk_desc *blk_dev;
Sjoerd Simons9b97b6b2015-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;
140 return 0;
141}
142
Sjoerd Simonsbacfb1d2015-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üns49afb372016-08-11 22:52:04 +0200147 U_BOOT_CMD_MKENT(size, 3, 0, do_host_size, "", ""),
Sjoerd Simonsbacfb1d2015-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 Simons9b97b6b2015-04-13 22:54:23 +0200150 U_BOOT_CMD_MKENT(dev, 0, 1, do_host_dev, "", ""),
Simon Glassd3049312012-12-26 09:53:36 +0000151};
152
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200153static int do_host(cmd_tbl_t *cmdtp, int flag, int argc,
Simon Glassd3049312012-12-26 09:53:36 +0000154 char * const argv[])
155{
156 cmd_tbl_t *c;
157
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200158 /* Skip past 'host' */
Simon Glassd3049312012-12-26 09:53:36 +0000159 argc--;
160 argv++;
161
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200162 c = find_cmd_tbl(argv[0], cmd_host_sub,
163 ARRAY_SIZE(cmd_host_sub));
Simon Glassd3049312012-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 Simonsbacfb1d2015-04-13 22:54:22 +0200172 sb, 8, 1, do_host,
173 "Deprecated: use 'host' command instead.", ""
174);
175
176U_BOOT_CMD(
177 host, 8, 1, do_host,
178 "Miscellaneous host commands",
Stephen Warren4d907022014-06-12 10:28:32 -0600179 "load hostfs - <addr> <filename> [<bytes> <offset>] - "
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000180 "load a file from host\n"
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200181 "host ls hostfs - <filename> - list files on host\n"
182 "host save hostfs - <addr> <filename> <bytes> [<offset>] - "
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000183 "save a file to host\n"
Sébastien Szymanski6baa6922017-01-19 17:49:13 +0100184 "host size hostfs - <filename> - determine size of file on host\n"
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200185 "host bind <dev> [<filename>] - bind \"host\" device to file\n"
186 "host info [<dev>] - show device binding & info\n"
Sjoerd Simons9b97b6b2015-04-13 22:54:23 +0200187 "host dev [<dev>] - Set or retrieve the current host device\n"
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200188 "host commands use the \"hostfs\" device. The \"host\" device is used\n"
Stephen Warren4d907022014-06-12 10:28:32 -0600189 "with standard IO commands such as fatls or ext2load"
Simon Glassd3049312012-12-26 09:53:36 +0000190);