]> Git Repo - J-u-boot.git/blame - cmd/host.c
Merge tag 'u-boot-amlogic-20221122' of https://source.denx.de/u-boot/custodians/u...
[J-u-boot.git] / cmd / host.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
d304931f
SG
2/*
3 * Copyright (c) 2012, Google Inc.
d304931f
SG
4 */
5
6#include <common.h>
09140113 7#include <command.h>
40fd0508 8#include <dm.h>
d304931f 9#include <fs.h>
f4d8de48 10#include <part.h>
95201811 11#include <sandbox_host.h>
336d4615 12#include <dm/device_compat.h>
95201811
SG
13#include <dm/device-internal.h>
14#include <dm/uclass-internal.h>
1221ce45 15#include <linux/errno.h>
d304931f 16
09140113
SG
17static int do_host_load(struct cmd_tbl *cmdtp, int flag, int argc,
18 char *const argv[])
d304931f 19{
9ade4857 20 return do_load(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
d304931f
SG
21}
22
09140113
SG
23static int do_host_ls(struct cmd_tbl *cmdtp, int flag, int argc,
24 char *const argv[])
d304931f
SG
25{
26 return do_ls(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
27}
28
09140113
SG
29static int do_host_size(struct cmd_tbl *cmdtp, int flag, int argc,
30 char *const argv[])
49afb379
SB
31{
32 return do_size(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
33}
34
09140113
SG
35static int do_host_save(struct cmd_tbl *cmdtp, int flag, int argc,
36 char *const argv[])
7eb2c8d5 37{
9ade4857 38 return do_save(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
7eb2c8d5
SG
39}
40
09140113
SG
41static int do_host_bind(struct cmd_tbl *cmdtp, int flag, int argc,
42 char *const argv[])
f4d8de48 43{
1758551e 44 bool removable = false;
95201811
SG
45 struct udevice *dev;
46 const char *label;
1503c2c7 47 char *file;
95201811 48 int ret;
1503c2c7 49
803e9c1c
SG
50 /* Skip 'bind' */
51 argc--;
52 argv++;
1758551e
SG
53 if (argc < 2)
54 return CMD_RET_USAGE;
55
56 if (!strcmp(argv[0], "-r")) {
57 removable = true;
58 argc--;
59 argv++;
60 }
61
62 if (argc > 2)
f4d8de48 63 return CMD_RET_USAGE;
95201811
SG
64 label = argv[0];
65 file = argc > 1 ? argv[1] : NULL;
66
67 ret = host_create_attach_file(label, file, removable, &dev);
68 if (ret) {
69 printf("Cannot create device / bind file\n");
70 return CMD_RET_FAILURE;
71 }
72
73 return 0;
74}
75
76/**
77 * parse_host_label() - Parse a device label or sequence number
78 *
79 * This shows an error if it returns NULL
80 *
81 * @label: String containing the label or sequence number
82 * Returns: Associated device, or NULL if not found
83 */
84static struct udevice *parse_host_label(const char *label)
85{
86 struct udevice *dev;
87
88 dev = host_find_by_label(label);
89 if (!dev) {
90 int devnum;
91 char *ep;
92
93 devnum = hextoul(label, &ep);
94 if (*ep ||
95 uclass_find_device_by_seq(UCLASS_HOST, devnum, &dev)) {
96 printf("No such device '%s'\n", label);
97 return NULL;
98 }
99 }
100
101 return dev;
102}
103
104static int do_host_unbind(struct cmd_tbl *cmdtp, int flag, int argc,
105 char *const argv[])
106{
107 struct udevice *dev;
108 const char *label;
109 int ret;
110
111 if (argc < 2)
f4d8de48 112 return CMD_RET_USAGE;
95201811
SG
113
114 label = argv[1];
115 dev = parse_host_label(label);
116 if (!dev)
117 return CMD_RET_FAILURE;
118
119 ret = host_detach_file(dev);
120 if (ret) {
121 printf("Cannot detach file (err=%d)\n", ret);
122 return CMD_RET_FAILURE;
123 }
124
125 ret = device_unbind(dev);
126 if (ret) {
127 printf("Cannot attach file\n");
128 ret = device_unbind(dev);
129 if (ret)
130 printf("Cannot unbind device '%s'\n", dev->name);
131 return CMD_RET_FAILURE;
f4d8de48 132 }
1503c2c7 133
95201811
SG
134 return 0;
135}
136
137static void show_host_dev(struct udevice *dev)
138{
139 struct host_sb_plat *plat = dev_get_plat(dev);
140 struct blk_desc *desc;
141 struct udevice *blk;
142 int ret;
143
144 printf("%3d ", dev_seq(dev));
145 if (!plat->fd) {
146 printf("Not bound to a backing file\n");
147 return;
148 }
149 ret = blk_get_from_parent(dev, &blk);
150 if (ret) /* cannot happen */
151 return;
152
153 desc = dev_get_uclass_plat(blk);
154 printf("%12lu %-15s %s\n", (unsigned long)desc->lba, plat->label,
155 plat->filename);
f4d8de48
HN
156}
157
09140113
SG
158static int do_host_info(struct cmd_tbl *cmdtp, int flag, int argc,
159 char *const argv[])
f4d8de48 160{
95201811
SG
161 struct udevice *dev;
162
163 if (argc < 1)
f4d8de48 164 return CMD_RET_USAGE;
95201811
SG
165
166 dev = NULL;
f4d8de48 167 if (argc >= 2) {
95201811
SG
168 dev = parse_host_label(argv[1]);
169 if (!dev)
170 return CMD_RET_FAILURE;
f4d8de48 171 }
40fd0508 172
95201811
SG
173 printf("%3s %12s %-15s %s\n", "dev", "blocks", "label", "path");
174 if (dev) {
175 show_host_dev(dev);
176 } else {
177 struct uclass *uc;
178
179 uclass_id_foreach_dev(UCLASS_HOST, dev, uc)
180 show_host_dev(dev);
f4d8de48 181 }
95201811 182
f4d8de48
HN
183 return 0;
184}
185
09140113
SG
186static int do_host_dev(struct cmd_tbl *cmdtp, int flag, int argc,
187 char *const argv[])
9b97b6ba 188{
95201811
SG
189 struct udevice *dev;
190 const char *label;
9b97b6ba
SS
191
192 if (argc < 1 || argc > 3)
193 return CMD_RET_USAGE;
194
195 if (argc == 1) {
95201811
SG
196 struct host_sb_plat *plat;
197
198 dev = host_get_cur_dev();
199 if (!dev) {
9b97b6ba 200 printf("No current host device\n");
95201811 201 return CMD_RET_FAILURE;
9b97b6ba 202 }
95201811
SG
203 plat = dev_get_plat(dev);
204 printf("Current host device: %d: %s\n", dev_seq(dev),
205 plat->label);
9b97b6ba
SS
206 return 0;
207 }
208
95201811
SG
209 label = argv[1];
210 dev = parse_host_label(argv[1]);
211 if (!dev)
212 return CMD_RET_FAILURE;
9b97b6ba 213
95201811 214 host_set_cur_dev(dev);
9b97b6ba 215
9b97b6ba
SS
216 return 0;
217}
218
09140113 219static struct cmd_tbl cmd_host_sub[] = {
bacfb1df
SS
220 U_BOOT_CMD_MKENT(load, 7, 0, do_host_load, "", ""),
221 U_BOOT_CMD_MKENT(ls, 3, 0, do_host_ls, "", ""),
222 U_BOOT_CMD_MKENT(save, 6, 0, do_host_save, "", ""),
49afb379 223 U_BOOT_CMD_MKENT(size, 3, 0, do_host_size, "", ""),
1758551e 224 U_BOOT_CMD_MKENT(bind, 4, 0, do_host_bind, "", ""),
95201811 225 U_BOOT_CMD_MKENT(unbind, 4, 0, do_host_unbind, "", ""),
bacfb1df 226 U_BOOT_CMD_MKENT(info, 3, 0, do_host_info, "", ""),
9b97b6ba 227 U_BOOT_CMD_MKENT(dev, 0, 1, do_host_dev, "", ""),
d304931f
SG
228};
229
09140113
SG
230static int do_host(struct cmd_tbl *cmdtp, int flag, int argc,
231 char *const argv[])
d304931f 232{
09140113 233 struct cmd_tbl *c;
d304931f 234
bacfb1df 235 /* Skip past 'host' */
d304931f
SG
236 argc--;
237 argv++;
238
95201811 239 c = find_cmd_tbl(argv[0], cmd_host_sub, ARRAY_SIZE(cmd_host_sub));
d304931f
SG
240
241 if (c)
242 return c->cmd(cmdtp, flag, argc, argv);
243 else
244 return CMD_RET_USAGE;
245}
246
bacfb1df
SS
247U_BOOT_CMD(
248 host, 8, 1, do_host,
249 "Miscellaneous host commands",
4d907025 250 "load hostfs - <addr> <filename> [<bytes> <offset>] - "
7eb2c8d5 251 "load a file from host\n"
bacfb1df
SS
252 "host ls hostfs - <filename> - list files on host\n"
253 "host save hostfs - <addr> <filename> <bytes> [<offset>] - "
7eb2c8d5 254 "save a file to host\n"
6baa692f 255 "host size hostfs - <filename> - determine size of file on host\n"
95201811 256 "host bind [-r] <label> [<filename>] - bind \"host\" device to file\n"
1758551e 257 " -r = mark as removable\n"
95201811
SG
258 "host unbind <label> - unbind file from \"host\" device\n"
259 "host info [<label>] - show device binding & info\n"
260 "host dev [<label>] - set or retrieve the current host device\n"
bacfb1df 261 "host commands use the \"hostfs\" device. The \"host\" device is used\n"
4d907025 262 "with standard IO commands such as fatls or ext2load"
d304931f 263);
This page took 0.433464 seconds and 4 git commands to generate.