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