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