1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 2012, Google Inc.
4 */
5
6 #include <common.h>
7 #include <command.h>
8 #include <dm.h>
9 #include <fs.h>
10 #include <part.h>
11 #include <sandboxblockdev.h>
12 #include <dm/device_compat.h>
13 #include <linux/errno.h>
14
15 static int host_curr_device = -1;
16
do_host_load(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])17 static int do_host_load(struct cmd_tbl *cmdtp, int flag, int argc,
18 char *const argv[])
19 {
20 return do_load(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
21 }
22
do_host_ls(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])23 static int do_host_ls(struct cmd_tbl *cmdtp, int flag, int argc,
24 char *const argv[])
25 {
26 return do_ls(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
27 }
28
do_host_size(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])29 static int do_host_size(struct cmd_tbl *cmdtp, int flag, int argc,
30 char *const argv[])
31 {
32 return do_size(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
33 }
34
do_host_save(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])35 static int do_host_save(struct cmd_tbl *cmdtp, int flag, int argc,
36 char *const argv[])
37 {
38 return do_save(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
39 }
40
do_host_bind(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])41 static int do_host_bind(struct cmd_tbl *cmdtp, int flag, int argc,
42 char *const argv[])
43 {
44 if (argc < 2 || argc > 3)
45 return CMD_RET_USAGE;
46 char *ep;
47 char *dev_str = argv[1];
48 char *file = argc >= 3 ? argv[2] : NULL;
49 int dev = simple_strtoul(dev_str, &ep, 16);
50 if (*ep) {
51 printf("** Bad device specification %s **\n", dev_str);
52 return CMD_RET_USAGE;
53 }
54 return !!host_dev_bind(dev, file);
55 }
56
do_host_info(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])57 static int do_host_info(struct cmd_tbl *cmdtp, int flag, int argc,
58 char *const argv[])
59 {
60 if (argc < 1 || argc > 2)
61 return CMD_RET_USAGE;
62 int min_dev = 0;
63 int max_dev = CONFIG_HOST_MAX_DEVICES - 1;
64 if (argc >= 2) {
65 char *ep;
66 char *dev_str = argv[1];
67 int dev = simple_strtoul(dev_str, &ep, 16);
68 if (*ep) {
69 printf("** Bad device specification %s **\n", dev_str);
70 return CMD_RET_USAGE;
71 }
72 min_dev = dev;
73 max_dev = dev;
74 }
75 int dev;
76 printf("%3s %12s %s\n", "dev", "blocks", "path");
77 for (dev = min_dev; dev <= max_dev; dev++) {
78 struct blk_desc *blk_dev;
79 int ret;
80
81 printf("%3d ", dev);
82 ret = host_get_dev_err(dev, &blk_dev);
83 if (ret) {
84 if (ret == -ENOENT)
85 puts("Not bound to a backing file\n");
86 else if (ret == -ENODEV)
87 puts("Invalid host device number\n");
88
89 continue;
90 }
91 struct host_block_dev *host_dev;
92
93 #ifdef CONFIG_BLK
94 host_dev = dev_get_plat(blk_dev->bdev);
95 #else
96 host_dev = blk_dev->priv;
97 #endif
98 printf("%12lu %s\n", (unsigned long)blk_dev->lba,
99 host_dev->filename);
100 }
101 return 0;
102 }
103
do_host_dev(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])104 static int do_host_dev(struct cmd_tbl *cmdtp, int flag, int argc,
105 char *const argv[])
106 {
107 int dev;
108 char *ep;
109 struct blk_desc *blk_dev;
110 int ret;
111
112 if (argc < 1 || argc > 3)
113 return CMD_RET_USAGE;
114
115 if (argc == 1) {
116 if (host_curr_device < 0) {
117 printf("No current host device\n");
118 return 1;
119 }
120 printf("Current host device %d\n", host_curr_device);
121 return 0;
122 }
123
124 dev = simple_strtoul(argv[1], &ep, 16);
125 if (*ep) {
126 printf("** Bad device specification %s **\n", argv[2]);
127 return CMD_RET_USAGE;
128 }
129
130 ret = host_get_dev_err(dev, &blk_dev);
131 if (ret) {
132 if (ret == -ENOENT)
133 puts("Not bound to a backing file\n");
134 else if (ret == -ENODEV)
135 puts("Invalid host device number\n");
136
137 return 1;
138 }
139
140 host_curr_device = dev;
141 return 0;
142 }
143
144 static struct cmd_tbl cmd_host_sub[] = {
145 U_BOOT_CMD_MKENT(load, 7, 0, do_host_load, "", ""),
146 U_BOOT_CMD_MKENT(ls, 3, 0, do_host_ls, "", ""),
147 U_BOOT_CMD_MKENT(save, 6, 0, do_host_save, "", ""),
148 U_BOOT_CMD_MKENT(size, 3, 0, do_host_size, "", ""),
149 U_BOOT_CMD_MKENT(bind, 3, 0, do_host_bind, "", ""),
150 U_BOOT_CMD_MKENT(info, 3, 0, do_host_info, "", ""),
151 U_BOOT_CMD_MKENT(dev, 0, 1, do_host_dev, "", ""),
152 };
153
do_host(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])154 static int do_host(struct cmd_tbl *cmdtp, int flag, int argc,
155 char *const argv[])
156 {
157 struct cmd_tbl *c;
158
159 /* Skip past 'host' */
160 argc--;
161 argv++;
162
163 c = find_cmd_tbl(argv[0], cmd_host_sub,
164 ARRAY_SIZE(cmd_host_sub));
165
166 if (c)
167 return c->cmd(cmdtp, flag, argc, argv);
168 else
169 return CMD_RET_USAGE;
170 }
171
172 U_BOOT_CMD(
173 host, 8, 1, do_host,
174 "Miscellaneous host commands",
175 "load hostfs - <addr> <filename> [<bytes> <offset>] - "
176 "load a file from host\n"
177 "host ls hostfs - <filename> - list files on host\n"
178 "host save hostfs - <addr> <filename> <bytes> [<offset>] - "
179 "save a file to host\n"
180 "host size hostfs - <filename> - determine size of file on host\n"
181 "host bind <dev> [<filename>] - bind \"host\" device to file\n"
182 "host info [<dev>] - show device binding & info\n"
183 "host dev [<dev>] - Set or retrieve the current host device\n"
184 "host commands use the \"hostfs\" device. The \"host\" device is used\n"
185 "with standard IO commands such as fatls or ext2load"
186 );
187