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