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