blob: ba1460ea1c35c29a064d1a63c6c5868e5cfe555d [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>
8#include <fs.h>
Henrik Nordström26f9a6c2013-11-10 10:26:56 -07009#include <part.h>
10#include <sandboxblockdev.h>
11#include <asm/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
Sjoerd Simons609d7fc2015-04-13 22:54:22 +020027static int do_host_save(cmd_tbl_t *cmdtp, int flag, int argc,
Simon Glassea307e82013-04-20 08:42:51 +000028 char * const argv[])
29{
Tom Rini089078a2013-10-10 10:46:22 -040030 return do_save(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
Simon Glassea307e82013-04-20 08:42:51 +000031}
32
Sjoerd Simons609d7fc2015-04-13 22:54:22 +020033static int do_host_bind(cmd_tbl_t *cmdtp, int flag, int argc,
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070034 char * const argv[])
35{
36 if (argc < 2 || argc > 3)
37 return CMD_RET_USAGE;
38 char *ep;
39 char *dev_str = argv[1];
40 char *file = argc >= 3 ? argv[2] : NULL;
41 int dev = simple_strtoul(dev_str, &ep, 16);
42 if (*ep) {
43 printf("** Bad device specification %s **\n", dev_str);
44 return CMD_RET_USAGE;
45 }
46 return host_dev_bind(dev, file);
47}
48
Sjoerd Simons609d7fc2015-04-13 22:54:22 +020049static int do_host_info(cmd_tbl_t *cmdtp, int flag, int argc,
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070050 char * const argv[])
51{
52 if (argc < 1 || argc > 2)
53 return CMD_RET_USAGE;
54 int min_dev = 0;
55 int max_dev = CONFIG_HOST_MAX_DEVICES - 1;
56 if (argc >= 2) {
57 char *ep;
58 char *dev_str = argv[1];
59 int dev = simple_strtoul(dev_str, &ep, 16);
60 if (*ep) {
61 printf("** Bad device specification %s **\n", dev_str);
62 return CMD_RET_USAGE;
63 }
64 min_dev = dev;
65 max_dev = dev;
66 }
67 int dev;
68 printf("%3s %12s %s\n", "dev", "blocks", "path");
69 for (dev = min_dev; dev <= max_dev; dev++) {
70 block_dev_desc_t *blk_dev;
71 int ret;
72
73 printf("%3d ", dev);
74 ret = host_get_dev_err(dev, &blk_dev);
75 if (ret) {
76 if (ret == -ENOENT)
77 puts("Not bound to a backing file\n");
78 else if (ret == -ENODEV)
79 puts("Invalid host device number\n");
80
81 continue;
82 }
83 struct host_block_dev *host_dev = blk_dev->priv;
84 printf("%12lu %s\n", (unsigned long)blk_dev->lba,
85 host_dev->filename);
86 }
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +020087 return 0;
88}
89
90static int do_host_dev(cmd_tbl_t *cmdtp, int flag, int argc,
91 char * const argv[])
92{
93 int dev;
94 char *ep;
95 block_dev_desc_t *blk_dev;
96 int ret;
97
98 if (argc < 1 || argc > 3)
99 return CMD_RET_USAGE;
100
101 if (argc == 1) {
102 if (host_curr_device < 0) {
103 printf("No current host device\n");
104 return 1;
105 }
106 printf("Current host device %d\n", host_curr_device);
107 return 0;
108 }
109
110 dev = simple_strtoul(argv[1], &ep, 16);
111 if (*ep) {
112 printf("** Bad device specification %s **\n", argv[2]);
113 return CMD_RET_USAGE;
114 }
115
116 ret = host_get_dev_err(dev, &blk_dev);
117 if (ret) {
118 if (ret == -ENOENT)
119 puts("Not bound to a backing file\n");
120 else if (ret == -ENODEV)
121 puts("Invalid host device number\n");
122
123 return 1;
124 }
125
126 host_curr_device = dev;
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700127 return 0;
128}
129
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200130static cmd_tbl_t cmd_host_sub[] = {
131 U_BOOT_CMD_MKENT(load, 7, 0, do_host_load, "", ""),
132 U_BOOT_CMD_MKENT(ls, 3, 0, do_host_ls, "", ""),
133 U_BOOT_CMD_MKENT(save, 6, 0, do_host_save, "", ""),
134 U_BOOT_CMD_MKENT(bind, 3, 0, do_host_bind, "", ""),
135 U_BOOT_CMD_MKENT(info, 3, 0, do_host_info, "", ""),
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200136 U_BOOT_CMD_MKENT(dev, 0, 1, do_host_dev, "", ""),
Simon Glassbf6ce792012-12-26 09:53:36 +0000137};
138
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200139static int do_host(cmd_tbl_t *cmdtp, int flag, int argc,
Simon Glassbf6ce792012-12-26 09:53:36 +0000140 char * const argv[])
141{
142 cmd_tbl_t *c;
143
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200144 /* Skip past 'host' */
Simon Glassbf6ce792012-12-26 09:53:36 +0000145 argc--;
146 argv++;
147
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200148 c = find_cmd_tbl(argv[0], cmd_host_sub,
149 ARRAY_SIZE(cmd_host_sub));
Simon Glassbf6ce792012-12-26 09:53:36 +0000150
151 if (c)
152 return c->cmd(cmdtp, flag, argc, argv);
153 else
154 return CMD_RET_USAGE;
155}
156
157U_BOOT_CMD(
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200158 sb, 8, 1, do_host,
159 "Deprecated: use 'host' command instead.", ""
160);
161
162U_BOOT_CMD(
163 host, 8, 1, do_host,
164 "Miscellaneous host commands",
Stephen Warrenbff6c312014-06-12 10:28:32 -0600165 "load hostfs - <addr> <filename> [<bytes> <offset>] - "
Simon Glassea307e82013-04-20 08:42:51 +0000166 "load a file from host\n"
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200167 "host ls hostfs - <filename> - list files on host\n"
168 "host save hostfs - <addr> <filename> <bytes> [<offset>] - "
Simon Glassea307e82013-04-20 08:42:51 +0000169 "save a file to host\n"
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200170 "host bind <dev> [<filename>] - bind \"host\" device to file\n"
171 "host info [<dev>] - show device binding & info\n"
Sjoerd Simons3f6b8d82015-04-13 22:54:23 +0200172 "host dev [<dev>] - Set or retrieve the current host device\n"
Sjoerd Simons609d7fc2015-04-13 22:54:22 +0200173 "host commands use the \"hostfs\" device. The \"host\" device is used\n"
Stephen Warrenbff6c312014-06-12 10:28:32 -0600174 "with standard IO commands such as fatls or ext2load"
Simon Glassbf6ce792012-12-26 09:53:36 +0000175);