xref: /openbmc/u-boot/cmd/part.c (revision d9b23e26)
1 /*
2  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
3  *
4  * made from cmd_ext2, which was:
5  *
6  * (C) Copyright 2004
7  * esd gmbh <www.esd-electronics.com>
8  * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
9  *
10  * made from cmd_reiserfs by
11  *
12  * (C) Copyright 2003 - 2004
13  * Sysgo Real-Time Solutions, AG <www.elinos.com>
14  * Pavel Bartusek <pba@sysgo.com>
15  *
16  * SPDX-License-Identifier:	GPL-2.0+
17  */
18 
19 #include <common.h>
20 #include <config.h>
21 #include <command.h>
22 #include <part.h>
23 #include <vsprintf.h>
24 
25 static int do_part_uuid(int argc, char * const argv[])
26 {
27 	int part;
28 	struct blk_desc *dev_desc;
29 	disk_partition_t info;
30 
31 	if (argc < 2)
32 		return CMD_RET_USAGE;
33 	if (argc > 3)
34 		return CMD_RET_USAGE;
35 
36 	part = blk_get_device_part_str(argv[0], argv[1], &dev_desc, &info, 0);
37 	if (part < 0)
38 		return 1;
39 
40 	if (argc > 2)
41 		env_set(argv[2], info.uuid);
42 	else
43 		printf("%s\n", info.uuid);
44 
45 	return 0;
46 }
47 
48 static int do_part_list(int argc, char * const argv[])
49 {
50 	int ret;
51 	struct blk_desc *desc;
52 	char *var = NULL;
53 	bool bootable = false;
54 	int i;
55 
56 	if (argc < 2)
57 		return CMD_RET_USAGE;
58 
59 	if (argc > 2) {
60 		for (i = 2; i < argc ; i++) {
61 			if (argv[i][0] == '-') {
62 				if (!strcmp(argv[i], "-bootable")) {
63 					bootable = true;
64 				} else {
65 					printf("Unknown option %s\n", argv[i]);
66 					return CMD_RET_USAGE;
67 				}
68 			} else {
69 				var = argv[i];
70 				break;
71 			}
72 		}
73 
74 		/* Loops should have been exited at the last argument, which
75 		 * as it contained the variable */
76 		if (argc != i + 1)
77 			return CMD_RET_USAGE;
78 	}
79 
80 	ret = blk_get_device_by_str(argv[0], argv[1], &desc);
81 	if (ret < 0)
82 		return 1;
83 
84 	if (var != NULL) {
85 		int p;
86 		char str[512] = { '\0', };
87 		disk_partition_t info;
88 
89 		for (p = 1; p < 128; p++) {
90 			char t[5];
91 			int r = part_get_info(desc, p, &info);
92 
93 			if (r != 0)
94 				continue;
95 
96 			if (bootable && !info.bootable)
97 				continue;
98 
99 			sprintf(t, "%s%x", str[0] ? " " : "", p);
100 			strcat(str, t);
101 		}
102 		env_set(var, str);
103 		return 0;
104 	}
105 
106 	part_print(desc);
107 
108 	return 0;
109 }
110 
111 static int do_part_start(int argc, char * const argv[])
112 {
113 	struct blk_desc *desc;
114 	disk_partition_t info;
115 	char buf[512] = { 0 };
116 	int part;
117 	int err;
118 	int ret;
119 
120 	if (argc < 3)
121 		return CMD_RET_USAGE;
122 	if (argc > 4)
123 		return CMD_RET_USAGE;
124 
125 	part = simple_strtoul(argv[2], NULL, 0);
126 
127 	ret = blk_get_device_by_str(argv[0], argv[1], &desc);
128 	if (ret < 0)
129 		return 1;
130 
131 	err = part_get_info(desc, part, &info);
132 	if (err)
133 		return 1;
134 
135 	snprintf(buf, sizeof(buf), LBAF, info.start);
136 
137 	if (argc > 3)
138 		env_set(argv[3], buf);
139 	else
140 		printf("%s\n", buf);
141 
142 	return 0;
143 }
144 
145 static int do_part_size(int argc, char * const argv[])
146 {
147 	struct blk_desc *desc;
148 	disk_partition_t info;
149 	char buf[512] = { 0 };
150 	int part;
151 	int err;
152 	int ret;
153 
154 	if (argc < 3)
155 		return CMD_RET_USAGE;
156 	if (argc > 4)
157 		return CMD_RET_USAGE;
158 
159 	part = simple_strtoul(argv[2], NULL, 0);
160 
161 	ret = blk_get_device_by_str(argv[0], argv[1], &desc);
162 	if (ret < 0)
163 		return 1;
164 
165 	err = part_get_info(desc, part, &info);
166 	if (err)
167 		return 1;
168 
169 	snprintf(buf, sizeof(buf), LBAF, info.size);
170 
171 	if (argc > 3)
172 		env_set(argv[3], buf);
173 	else
174 		printf("%s\n", buf);
175 
176 	return 0;
177 }
178 
179 static int do_part(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
180 {
181 	if (argc < 2)
182 		return CMD_RET_USAGE;
183 
184 	if (!strcmp(argv[1], "uuid"))
185 		return do_part_uuid(argc - 2, argv + 2);
186 	else if (!strcmp(argv[1], "list"))
187 		return do_part_list(argc - 2, argv + 2);
188 	else if (!strcmp(argv[1], "start"))
189 		return do_part_start(argc - 2, argv + 2);
190 	else if (!strcmp(argv[1], "size"))
191 		return do_part_size(argc - 2, argv + 2);
192 
193 	return CMD_RET_USAGE;
194 }
195 
196 U_BOOT_CMD(
197 	part,	CONFIG_SYS_MAXARGS,	1,	do_part,
198 	"disk partition related commands",
199 	"uuid <interface> <dev>:<part>\n"
200 	"    - print partition UUID\n"
201 	"part uuid <interface> <dev>:<part> <varname>\n"
202 	"    - set environment variable to partition UUID\n"
203 	"part list <interface> <dev>\n"
204 	"    - print a device's partition table\n"
205 	"part list <interface> <dev> [flags] <varname>\n"
206 	"    - set environment variable to the list of partitions\n"
207 	"      flags can be -bootable (list only bootable partitions)\n"
208 	"part start <interface> <dev> <part> <varname>\n"
209 	"    - set environment variable to the start of the partition (in blocks)\n"
210 	"part size <interface> <dev> <part> <varname>\n"
211 	"    - set environment variable to the size of the partition (in blocks)"
212 );
213