blob: 8d84415301f9ec6454676c334595b786d04a0af8 [file] [log] [blame]
Simon Glassbf6ce792012-12-26 09:53:36 +00001/*
2 * Copyright (c) 2012, Google Inc.
3 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Simon Glassbf6ce792012-12-26 09:53:36 +00005 */
6
7#include <common.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>
12#include <asm/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
Sjoerd Simons609d7fc2015-04-13 22:54:22 +020028static int do_host_save(cmd_tbl_t *cmdtp, int flag, int argc,
Simon Glassea307e82013-04-20 08:42:51 +000029 char * const argv[])
30{
Tom Rini089078a2013-10-10 10:46:22 -040031 return do_save(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
Simon Glassea307e82013-04-20 08:42:51 +000032}
33
Sjoerd Simons609d7fc2015-04-13 22:54:22 +020034static int do_host_bind(cmd_tbl_t *cmdtp, int flag, int argc,
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070035 char * const argv[])
36{
37 if (argc < 2 || argc > 3)
38 return CMD_RET_USAGE;
39 char *ep;
40 char *dev_str = argv[1];
41 char *file = argc >= 3 ? argv[2] : NULL;
42 int dev = simple_strtoul(dev_str, &ep, 16);
43 if (*ep) {
44 printf("** Bad device specification %s **\n", dev_str);
45 return CMD_RET_USAGE;
46 }
47 return host_dev_bind(dev, file);
48}
49
Sjoerd Simons609d7fc2015-04-13 22:54:22 +020050static int do_host_info(cmd_tbl_t *cmdtp, int flag, int argc,
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070051 char * const argv[])
52{
53 if (argc < 1 || argc > 2)
54 return CMD_RET_USAGE;
55 int min_dev = 0;
56 int max_dev = CONFIG_HOST_MAX_DEVICES - 1;
57 if (argc >= 2) {
58 char *ep;
59 char *dev_str = argv[1];
60 int dev = simple_strtoul(dev_str, &ep, 16);
61 if (*ep) {
62 printf("** Bad device specification %s **\n", dev_str);
63 return CMD_RET_USAGE;
64 }
65 min_dev = dev;
66 max_dev = dev;
67 }
68 int dev;
69 printf("%3s %12s %s\n", "dev", "blocks", "path");
70 for (dev = min_dev; dev <= max_dev; dev++) {
Simon Glasse3394752016-02-29 15:25:34 -070071 struct blk_desc *blk_dev;
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070072 int ret;
73
74 printf("%3d ", dev);
75 ret = host_get_dev_err(dev, &blk_dev);
76 if (ret) {
77 if (ret == -ENOENT)
78 puts("Not bound to a backing file\n");
79 else if (ret == -ENODEV)
80 puts("Invalid host device number\n");
81
82 continue;
83 }
Simon Glass2c2075a2016-02-29 15:25:57 -070084 struct host_block_dev *host_dev;
85
86#ifdef CONFIG_BLK
87 host_dev = dev_get_priv(blk_dev->bdev);
88#else
89 host_dev = blk_dev->priv;
90#endif
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070091 printf("%12lu %s\n", (unsigned long)blk_dev->lba,
92 host_dev->filename);
93 }
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +020094 return 0;
95}
96
97static int do_host_dev(cmd_tbl_t *cmdtp, int flag, int argc,
98 char * const argv[])
99{
100 int dev;
101 char *ep;
Simon Glasse3394752016-02-29 15:25:34 -0700102 struct blk_desc *blk_dev;
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200103 int ret;
104
105 if (argc < 1 || argc > 3)
106 return CMD_RET_USAGE;
107
108 if (argc == 1) {
109 if (host_curr_device < 0) {
110 printf("No current host device\n");
111 return 1;
112 }
113 printf("Current host device %d\n", host_curr_device);
114 return 0;
115 }
116
117 dev = simple_strtoul(argv[1], &ep, 16);
118 if (*ep) {
119 printf("** Bad device specification %s **\n", argv[2]);
120 return CMD_RET_USAGE;
121 }
122
123 ret = host_get_dev_err(dev, &blk_dev);
124 if (ret) {
125 if (ret == -ENOENT)
126 puts("Not bound to a backing file\n");
127 else if (ret == -ENODEV)
128 puts("Invalid host device number\n");
129
130 return 1;
131 }
132
133 host_curr_device = dev;
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700134 return 0;
135}
136
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200137static cmd_tbl_t cmd_host_sub[] = {
138 U_BOOT_CMD_MKENT(load, 7, 0, do_host_load, "", ""),
139 U_BOOT_CMD_MKENT(ls, 3, 0, do_host_ls, "", ""),
140 U_BOOT_CMD_MKENT(save, 6, 0, do_host_save, "", ""),
141 U_BOOT_CMD_MKENT(bind, 3, 0, do_host_bind, "", ""),
142 U_BOOT_CMD_MKENT(info, 3, 0, do_host_info, "", ""),
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200143 U_BOOT_CMD_MKENT(dev, 0, 1, do_host_dev, "", ""),
Simon Glassbf6ce792012-12-26 09:53:36 +0000144};
145
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200146static int do_host(cmd_tbl_t *cmdtp, int flag, int argc,
Simon Glassbf6ce792012-12-26 09:53:36 +0000147 char * const argv[])
148{
149 cmd_tbl_t *c;
150
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200151 /* Skip past 'host' */
Simon Glassbf6ce792012-12-26 09:53:36 +0000152 argc--;
153 argv++;
154
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200155 c = find_cmd_tbl(argv[0], cmd_host_sub,
156 ARRAY_SIZE(cmd_host_sub));
Simon Glassbf6ce792012-12-26 09:53:36 +0000157
158 if (c)
159 return c->cmd(cmdtp, flag, argc, argv);
160 else
161 return CMD_RET_USAGE;
162}
163
164U_BOOT_CMD(
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200165 sb, 8, 1, do_host,
166 "Deprecated: use 'host' command instead.", ""
167);
168
169U_BOOT_CMD(
170 host, 8, 1, do_host,
171 "Miscellaneous host commands",
Stephen Warrenbff6c312014-06-12 10:28:32 -0600172 "load hostfs - <addr> <filename> [<bytes> <offset>] - "
Simon Glassea307e82013-04-20 08:42:51 +0000173 "load a file from host\n"
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200174 "host ls hostfs - <filename> - list files on host\n"
175 "host save hostfs - <addr> <filename> <bytes> [<offset>] - "
Simon Glassea307e82013-04-20 08:42:51 +0000176 "save a file to host\n"
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200177 "host bind <dev> [<filename>] - bind \"host\" device to file\n"
178 "host info [<dev>] - show device binding & info\n"
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200179 "host dev [<dev>] - Set or retrieve the current host device\n"
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200180 "host commands use the \"hostfs\" device. The \"host\" device is used\n"
Stephen Warrenbff6c312014-06-12 10:28:32 -0600181 "with standard IO commands such as fatls or ext2load"
Simon Glassbf6ce792012-12-26 09:53:36 +0000182);