blob: 98c4d2a099e972354114d6fcfb0efb158e33dbad [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>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090011#include <linux/errno.h>
Simon Glassbf6ce792012-12-26 09:53:36 +000012
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +020013static int host_curr_device = -1;
14
Sjoerd Simons609d7fc2015-04-13 22:54:22 +020015static int do_host_load(cmd_tbl_t *cmdtp, int flag, int argc,
Simon Glassbf6ce792012-12-26 09:53:36 +000016 char * const argv[])
17{
Tom Rini089078a2013-10-10 10:46:22 -040018 return do_load(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
Simon Glassbf6ce792012-12-26 09:53:36 +000019}
20
Sjoerd Simons609d7fc2015-04-13 22:54:22 +020021static int do_host_ls(cmd_tbl_t *cmdtp, int flag, int argc,
Simon Glassbf6ce792012-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üns72acc652016-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 Simons609d7fc2015-04-13 22:54:22 +020033static int do_host_save(cmd_tbl_t *cmdtp, int flag, int argc,
Simon Glassea307e82013-04-20 08:42:51 +000034 char * const argv[])
35{
Tom Rini089078a2013-10-10 10:46:22 -040036 return do_save(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
Simon Glassea307e82013-04-20 08:42:51 +000037}
38
Sjoerd Simons609d7fc2015-04-13 22:54:22 +020039static int do_host_bind(cmd_tbl_t *cmdtp, int flag, int argc,
Henrik Nordström26f9a6c2013-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 Simons609d7fc2015-04-13 22:54:22 +020055static int do_host_info(cmd_tbl_t *cmdtp, int flag, int argc,
Henrik Nordström26f9a6c2013-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 Glasse3394752016-02-29 15:25:34 -070076 struct blk_desc *blk_dev;
Henrik Nordström26f9a6c2013-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 Glass2c2075a2016-02-29 15:25:57 -070089 struct host_block_dev *host_dev;
90
91#ifdef CONFIG_BLK
AKASHI Takahiro23b13492019-08-22 16:47:39 +090092 host_dev = dev_get_platdata(blk_dev->bdev);
Simon Glass2c2075a2016-02-29 15:25:57 -070093#else
94 host_dev = blk_dev->priv;
95#endif
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070096 printf("%12lu %s\n", (unsigned long)blk_dev->lba,
97 host_dev->filename);
98 }
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +020099 return 0;
100}
101
102static int do_host_dev(cmd_tbl_t *cmdtp, int flag, int argc,
103 char * const argv[])
104{
105 int dev;
106 char *ep;
Simon Glasse3394752016-02-29 15:25:34 -0700107 struct blk_desc *blk_dev;
Sjoerd Simons3f6b8d82015-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;
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700139 return 0;
140}
141
Sjoerd Simons609d7fc2015-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üns72acc652016-08-11 22:52:04 +0200146 U_BOOT_CMD_MKENT(size, 3, 0, do_host_size, "", ""),
Sjoerd Simons609d7fc2015-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 Simons3f6b8d82015-04-13 22:54:23 +0200149 U_BOOT_CMD_MKENT(dev, 0, 1, do_host_dev, "", ""),
Simon Glassbf6ce792012-12-26 09:53:36 +0000150};
151
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200152static int do_host(cmd_tbl_t *cmdtp, int flag, int argc,
Simon Glassbf6ce792012-12-26 09:53:36 +0000153 char * const argv[])
154{
155 cmd_tbl_t *c;
156
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200157 /* Skip past 'host' */
Simon Glassbf6ce792012-12-26 09:53:36 +0000158 argc--;
159 argv++;
160
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200161 c = find_cmd_tbl(argv[0], cmd_host_sub,
162 ARRAY_SIZE(cmd_host_sub));
Simon Glassbf6ce792012-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 Simons609d7fc2015-04-13 22:54:22 +0200171 host, 8, 1, do_host,
172 "Miscellaneous host commands",
Stephen Warrenbff6c312014-06-12 10:28:32 -0600173 "load hostfs - <addr> <filename> [<bytes> <offset>] - "
Simon Glassea307e82013-04-20 08:42:51 +0000174 "load a file from host\n"
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200175 "host ls hostfs - <filename> - list files on host\n"
176 "host save hostfs - <addr> <filename> <bytes> [<offset>] - "
Simon Glassea307e82013-04-20 08:42:51 +0000177 "save a file to host\n"
Sébastien Szymanskied6481e2017-01-19 17:49:13 +0100178 "host size hostfs - <filename> - determine size of file on host\n"
Sjoerd Simons609d7fc2015-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 Simons3f6b8d82015-04-13 22:54:23 +0200181 "host dev [<dev>] - Set or retrieve the current host device\n"
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200182 "host commands use the \"hostfs\" device. The \"host\" device is used\n"
Stephen Warrenbff6c312014-06-12 10:28:32 -0600183 "with standard IO commands such as fatls or ext2load"
Simon Glassbf6ce792012-12-26 09:53:36 +0000184);