blob: 98c4d2a099e972354114d6fcfb0efb158e33dbad [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 Glass40fd0502016-02-29 15:25:57 -07007#include <dm.h>
Simon Glassd3049312012-12-26 09:53:36 +00008#include <fs.h>
Henrik Nordströmf4d8de42013-11-10 10:26:56 -07009#include <part.h>
10#include <sandboxblockdev.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090011#include <linux/errno.h>
Simon Glassd3049312012-12-26 09:53:36 +000012
Sjoerd Simons9b97b6b2015-04-13 22:54:23 +020013static int host_curr_device = -1;
14
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +020015static int do_host_load(cmd_tbl_t *cmdtp, int flag, int argc,
Simon Glassd3049312012-12-26 09:53:36 +000016 char * const argv[])
17{
Tom Rini9ade4852013-10-10 10:46:22 -040018 return do_load(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
Simon Glassd3049312012-12-26 09:53:36 +000019}
20
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +020021static int do_host_ls(cmd_tbl_t *cmdtp, int flag, int argc,
Simon Glassd3049312012-12-26 09:53:36 +000022 char * const argv[])
23{
24 return do_ls(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
25}
26
Stefan Brüns49afb372016-08-11 22:52:04 +020027static int do_host_size(cmd_tbl_t *cmdtp, int flag, int argc,
28 char * const argv[])
29{
30 return do_size(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
31}
32
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +020033static int do_host_save(cmd_tbl_t *cmdtp, int flag, int argc,
Simon Glass7eb2c8d2013-04-20 08:42:51 +000034 char * const argv[])
35{
Tom Rini9ade4852013-10-10 10:46:22 -040036 return do_save(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
Simon Glass7eb2c8d2013-04-20 08:42:51 +000037}
38
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +020039static int do_host_bind(cmd_tbl_t *cmdtp, int flag, int argc,
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070040 char * const argv[])
41{
42 if (argc < 2 || argc > 3)
43 return CMD_RET_USAGE;
44 char *ep;
45 char *dev_str = argv[1];
46 char *file = argc >= 3 ? argv[2] : NULL;
47 int dev = simple_strtoul(dev_str, &ep, 16);
48 if (*ep) {
49 printf("** Bad device specification %s **\n", dev_str);
50 return CMD_RET_USAGE;
51 }
52 return host_dev_bind(dev, file);
53}
54
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +020055static int do_host_info(cmd_tbl_t *cmdtp, int flag, int argc,
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070056 char * const argv[])
57{
58 if (argc < 1 || argc > 2)
59 return CMD_RET_USAGE;
60 int min_dev = 0;
61 int max_dev = CONFIG_HOST_MAX_DEVICES - 1;
62 if (argc >= 2) {
63 char *ep;
64 char *dev_str = argv[1];
65 int dev = simple_strtoul(dev_str, &ep, 16);
66 if (*ep) {
67 printf("** Bad device specification %s **\n", dev_str);
68 return CMD_RET_USAGE;
69 }
70 min_dev = dev;
71 max_dev = dev;
72 }
73 int dev;
74 printf("%3s %12s %s\n", "dev", "blocks", "path");
75 for (dev = min_dev; dev <= max_dev; dev++) {
Simon Glass4101f682016-02-29 15:25:34 -070076 struct blk_desc *blk_dev;
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070077 int ret;
78
79 printf("%3d ", dev);
80 ret = host_get_dev_err(dev, &blk_dev);
81 if (ret) {
82 if (ret == -ENOENT)
83 puts("Not bound to a backing file\n");
84 else if (ret == -ENODEV)
85 puts("Invalid host device number\n");
86
87 continue;
88 }
Simon Glass40fd0502016-02-29 15:25:57 -070089 struct host_block_dev *host_dev;
90
91#ifdef CONFIG_BLK
AKASHI Takahiro5bac9c52019-08-22 16:47:39 +090092 host_dev = dev_get_platdata(blk_dev->bdev);
Simon Glass40fd0502016-02-29 15:25:57 -070093#else
94 host_dev = blk_dev->priv;
95#endif
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070096 printf("%12lu %s\n", (unsigned long)blk_dev->lba,
97 host_dev->filename);
98 }
99 return 0;
100}
101
Sjoerd Simons9b97b6b2015-04-13 22:54:23 +0200102static int do_host_dev(cmd_tbl_t *cmdtp, int flag, int argc,
103 char * const argv[])
104{
105 int dev;
106 char *ep;
Simon Glass4101f682016-02-29 15:25:34 -0700107 struct blk_desc *blk_dev;
Sjoerd Simons9b97b6b2015-04-13 22:54:23 +0200108 int ret;
109
110 if (argc < 1 || argc > 3)
111 return CMD_RET_USAGE;
112
113 if (argc == 1) {
114 if (host_curr_device < 0) {
115 printf("No current host device\n");
116 return 1;
117 }
118 printf("Current host device %d\n", host_curr_device);
119 return 0;
120 }
121
122 dev = simple_strtoul(argv[1], &ep, 16);
123 if (*ep) {
124 printf("** Bad device specification %s **\n", argv[2]);
125 return CMD_RET_USAGE;
126 }
127
128 ret = host_get_dev_err(dev, &blk_dev);
129 if (ret) {
130 if (ret == -ENOENT)
131 puts("Not bound to a backing file\n");
132 else if (ret == -ENODEV)
133 puts("Invalid host device number\n");
134
135 return 1;
136 }
137
138 host_curr_device = dev;
139 return 0;
140}
141
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200142static cmd_tbl_t cmd_host_sub[] = {
143 U_BOOT_CMD_MKENT(load, 7, 0, do_host_load, "", ""),
144 U_BOOT_CMD_MKENT(ls, 3, 0, do_host_ls, "", ""),
145 U_BOOT_CMD_MKENT(save, 6, 0, do_host_save, "", ""),
Stefan Brüns49afb372016-08-11 22:52:04 +0200146 U_BOOT_CMD_MKENT(size, 3, 0, do_host_size, "", ""),
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200147 U_BOOT_CMD_MKENT(bind, 3, 0, do_host_bind, "", ""),
148 U_BOOT_CMD_MKENT(info, 3, 0, do_host_info, "", ""),
Sjoerd Simons9b97b6b2015-04-13 22:54:23 +0200149 U_BOOT_CMD_MKENT(dev, 0, 1, do_host_dev, "", ""),
Simon Glassd3049312012-12-26 09:53:36 +0000150};
151
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200152static int do_host(cmd_tbl_t *cmdtp, int flag, int argc,
Simon Glassd3049312012-12-26 09:53:36 +0000153 char * const argv[])
154{
155 cmd_tbl_t *c;
156
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200157 /* Skip past 'host' */
Simon Glassd3049312012-12-26 09:53:36 +0000158 argc--;
159 argv++;
160
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200161 c = find_cmd_tbl(argv[0], cmd_host_sub,
162 ARRAY_SIZE(cmd_host_sub));
Simon Glassd3049312012-12-26 09:53:36 +0000163
164 if (c)
165 return c->cmd(cmdtp, flag, argc, argv);
166 else
167 return CMD_RET_USAGE;
168}
169
170U_BOOT_CMD(
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200171 host, 8, 1, do_host,
172 "Miscellaneous host commands",
Stephen Warren4d907022014-06-12 10:28:32 -0600173 "load hostfs - <addr> <filename> [<bytes> <offset>] - "
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000174 "load a file from host\n"
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200175 "host ls hostfs - <filename> - list files on host\n"
176 "host save hostfs - <addr> <filename> <bytes> [<offset>] - "
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000177 "save a file to host\n"
Sébastien Szymanski6baa6922017-01-19 17:49:13 +0100178 "host size hostfs - <filename> - determine size of file on host\n"
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200179 "host bind <dev> [<filename>] - bind \"host\" device to file\n"
180 "host info [<dev>] - show device binding & info\n"
Sjoerd Simons9b97b6b2015-04-13 22:54:23 +0200181 "host dev [<dev>] - Set or retrieve the current host device\n"
Sjoerd Simonsbacfb1d2015-04-13 22:54:22 +0200182 "host commands use the \"hostfs\" device. The \"host\" device is used\n"
Stephen Warren4d907022014-06-12 10:28:32 -0600183 "with standard IO commands such as fatls or ext2load"
Simon Glassd3049312012-12-26 09:53:36 +0000184);