blob: d140a1eddb92e1a4d9ff3ffa0442cb66d99cecd3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stephen Warren3a529102012-09-21 09:51:01 +00002/*
3 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
4 *
5 * made from cmd_ext2, which was:
6 *
7 * (C) Copyright 2004
8 * esd gmbh <www.esd-electronics.com>
9 * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
10 *
11 * made from cmd_reiserfs by
12 *
13 * (C) Copyright 2003 - 2004
14 * Sysgo Real-Time Solutions, AG <www.elinos.com>
15 * Pavel Bartusek <pba@sysgo.com>
Stephen Warren3a529102012-09-21 09:51:01 +000016 */
17
Stephen Warren3a529102012-09-21 09:51:01 +000018#include <config.h>
19#include <command.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060020#include <env.h>
Stephen Warren3a529102012-09-21 09:51:01 +000021#include <part.h>
22#include <vsprintf.h>
23
Sam Protsenko92bfb862018-02-26 23:18:00 +020024enum cmd_part_info {
25 CMD_PART_INFO_START = 0,
26 CMD_PART_INFO_SIZE,
Ruslan Trofymenkodb9bd6362019-06-14 17:01:26 +030027 CMD_PART_INFO_NUMBER
Sam Protsenko92bfb862018-02-26 23:18:00 +020028};
29
Simon Glassed38aef2020-05-10 11:40:03 -060030static int do_part_uuid(int argc, char *const argv[])
Stephen Warren3a529102012-09-21 09:51:01 +000031{
32 int part;
Simon Glasse3394752016-02-29 15:25:34 -070033 struct blk_desc *dev_desc;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060034 struct disk_partition info;
Stephen Warren3a529102012-09-21 09:51:01 +000035
36 if (argc < 2)
37 return CMD_RET_USAGE;
38 if (argc > 3)
39 return CMD_RET_USAGE;
40
Simon Glasse76ee972016-02-29 15:25:44 -070041 part = blk_get_device_part_str(argv[0], argv[1], &dev_desc, &info, 0);
Stephen Warren3a529102012-09-21 09:51:01 +000042 if (part < 0)
43 return 1;
44
45 if (argc > 2)
Simon Glass6a38e412017-08-03 12:22:09 -060046 env_set(argv[2], info.uuid);
Stephen Warren3a529102012-09-21 09:51:01 +000047 else
48 printf("%s\n", info.uuid);
49
50 return 0;
51}
52
Simon Glassed38aef2020-05-10 11:40:03 -060053static int do_part_list(int argc, char *const argv[])
Stephen Warren3a529102012-09-21 09:51:01 +000054{
55 int ret;
Simon Glasse3394752016-02-29 15:25:34 -070056 struct blk_desc *desc;
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +010057 char *var = NULL;
58 bool bootable = false;
59 int i;
Stephen Warren3a529102012-09-21 09:51:01 +000060
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +010061 if (argc < 2)
Stephen Warren3a529102012-09-21 09:51:01 +000062 return CMD_RET_USAGE;
63
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +010064 if (argc > 2) {
65 for (i = 2; i < argc ; i++) {
66 if (argv[i][0] == '-') {
67 if (!strcmp(argv[i], "-bootable")) {
68 bootable = true;
69 } else {
70 printf("Unknown option %s\n", argv[i]);
71 return CMD_RET_USAGE;
72 }
73 } else {
74 var = argv[i];
75 break;
76 }
77 }
78
79 /* Loops should have been exited at the last argument, which
80 * as it contained the variable */
81 if (argc != i + 1)
82 return CMD_RET_USAGE;
83 }
84
Simon Glasse6649a62016-02-29 15:25:43 -070085 ret = blk_get_device_by_str(argv[0], argv[1], &desc);
Stephen Warren3a529102012-09-21 09:51:01 +000086 if (ret < 0)
87 return 1;
88
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +010089 if (var != NULL) {
Sjoerd Simonsca8982b2015-01-05 18:13:37 +010090 int p;
Heinrich Schuchardt72d78832022-01-11 16:03:38 +010091 char str[3 * MAX_SEARCH_PARTITIONS] = { '\0', };
Simon Glassc1c4a8f2020-05-10 11:39:57 -060092 struct disk_partition info;
Sjoerd Simonsca8982b2015-01-05 18:13:37 +010093
Heinrich Schuchardt72d78832022-01-11 16:03:38 +010094 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +010095 char t[5];
Simon Glassb89a8442016-02-29 15:25:48 -070096 int r = part_get_info(desc, p, &info);
Sjoerd Simonsca8982b2015-01-05 18:13:37 +010097
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +010098 if (r != 0)
99 continue;
100
101 if (bootable && !info.bootable)
102 continue;
103
Stephen Warren78bb0c12015-12-09 09:48:04 -0700104 sprintf(t, "%s%x", str[0] ? " " : "", p);
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +0100105 strcat(str, t);
Sjoerd Simonsca8982b2015-01-05 18:13:37 +0100106 }
Simon Glass6a38e412017-08-03 12:22:09 -0600107 env_set(var, str);
Sjoerd Simonsca8982b2015-01-05 18:13:37 +0100108 return 0;
109 }
110
Simon Glassb89a8442016-02-29 15:25:48 -0700111 part_print(desc);
Stephen Warren3a529102012-09-21 09:51:01 +0000112
113 return 0;
114}
115
Simon Glassed38aef2020-05-10 11:40:03 -0600116static int do_part_info(int argc, char *const argv[], enum cmd_part_info param)
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200117{
Simon Glasse3394752016-02-29 15:25:34 -0700118 struct blk_desc *desc;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600119 struct disk_partition info;
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200120 char buf[512] = { 0 };
Sam Protsenko515a8632018-02-26 23:17:59 +0200121 char *endp;
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200122 int part;
123 int err;
124 int ret;
125
126 if (argc < 3)
127 return CMD_RET_USAGE;
128 if (argc > 4)
129 return CMD_RET_USAGE;
130
Simon Glasse6649a62016-02-29 15:25:43 -0700131 ret = blk_get_device_by_str(argv[0], argv[1], &desc);
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200132 if (ret < 0)
133 return 1;
134
Sam Protsenko515a8632018-02-26 23:17:59 +0200135 part = simple_strtoul(argv[2], &endp, 0);
136 if (*endp == '\0') {
137 err = part_get_info(desc, part, &info);
138 if (err)
139 return 1;
140 } else {
141 part = part_get_info_by_name(desc, argv[2], &info);
Anders Delliena7b261b2021-06-22 10:40:01 +0100142 if (part < 0)
Sam Protsenko515a8632018-02-26 23:17:59 +0200143 return 1;
144 }
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200145
Sam Protsenko92bfb862018-02-26 23:18:00 +0200146 switch (param) {
147 case CMD_PART_INFO_START:
148 snprintf(buf, sizeof(buf), LBAF, info.start);
149 break;
150 case CMD_PART_INFO_SIZE:
151 snprintf(buf, sizeof(buf), LBAF, info.size);
152 break;
Ruslan Trofymenkodb9bd6362019-06-14 17:01:26 +0300153 case CMD_PART_INFO_NUMBER:
Roman Stratiienko014a3812019-09-20 15:59:18 +0200154 snprintf(buf, sizeof(buf), "0x%x", part);
Ruslan Trofymenkodb9bd6362019-06-14 17:01:26 +0300155 break;
Sam Protsenko92bfb862018-02-26 23:18:00 +0200156 default:
157 printf("** Unknown cmd_part_info value: %d\n", param);
158 return 1;
159 }
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200160
161 if (argc > 3)
Simon Glass6a38e412017-08-03 12:22:09 -0600162 env_set(argv[3], buf);
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200163 else
164 printf("%s\n", buf);
165
166 return 0;
167}
168
Simon Glassed38aef2020-05-10 11:40:03 -0600169static int do_part_start(int argc, char *const argv[])
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200170{
Sam Protsenko92bfb862018-02-26 23:18:00 +0200171 return do_part_info(argc, argv, CMD_PART_INFO_START);
172}
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200173
Simon Glassed38aef2020-05-10 11:40:03 -0600174static int do_part_size(int argc, char *const argv[])
Sam Protsenko92bfb862018-02-26 23:18:00 +0200175{
176 return do_part_info(argc, argv, CMD_PART_INFO_SIZE);
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200177}
178
Simon Glassed38aef2020-05-10 11:40:03 -0600179static int do_part_number(int argc, char *const argv[])
Ruslan Trofymenkodb9bd6362019-06-14 17:01:26 +0300180{
181 return do_part_info(argc, argv, CMD_PART_INFO_NUMBER);
182}
183
Simon Glassed338b72023-07-15 21:38:47 -0600184static int do_part_set(int argc, char *const argv[])
185{
186 const char *devname, *partstr, *typestr;
187 struct blk_desc *desc;
188 int dev;
189
190 if (argc < 3)
191 return CMD_RET_USAGE;
192
193 /* Look up the device */
194 devname = argv[0];
195 partstr = argv[1];
196 typestr = argv[2];
197 dev = blk_get_device_by_str(devname, partstr, &desc);
198 if (dev < 0) {
199 printf("** Bad device specification %s %s **\n", devname,
200 partstr);
201 return CMD_RET_FAILURE;
202 }
203
204 desc->part_type = part_get_type_by_name(typestr);
205 if (!desc->part_type) {
206 printf("Unknown partition type '%s'\n", typestr);
207 return CMD_RET_FAILURE;
208 }
209 part_print(desc);
210
211 return 0;
212}
213
Enric Balletbo i Serra0359a092023-01-10 17:19:34 +0100214#ifdef CONFIG_PARTITION_TYPE_GUID
215static int do_part_type(int argc, char *const argv[])
216{
217 int part;
218 struct blk_desc *dev_desc;
219 struct disk_partition info;
220
221 if (argc < 2)
222 return CMD_RET_USAGE;
223 if (argc > 3)
224 return CMD_RET_USAGE;
225
226 part = blk_get_device_part_str(argv[0], argv[1], &dev_desc, &info, 0);
227 if (part < 0)
228 return 1;
229
230 if (argc > 2)
231 env_set(argv[2], info.type_guid);
232 else
233 printf("%s\n", info.type_guid);
234
235 return 0;
236}
237#endif
238
Niel Fourie40102bc2020-03-24 16:17:03 +0100239static int do_part_types(int argc, char * const argv[])
240{
241 struct part_driver *drv = ll_entry_start(struct part_driver,
242 part_driver);
243 const int n_ents = ll_entry_count(struct part_driver, part_driver);
244 struct part_driver *entry;
245 int i = 0;
246
247 puts("Supported partition tables");
248
249 for (entry = drv; entry != drv + n_ents; entry++) {
250 printf("%c %s", i ? ',' : ':', entry->name);
251 i++;
252 }
253 if (!i)
254 puts(": <none>");
255 puts("\n");
256 return CMD_RET_SUCCESS;
257}
258
Simon Glassed38aef2020-05-10 11:40:03 -0600259static int do_part(struct cmd_tbl *cmdtp, int flag, int argc,
260 char *const argv[])
Stephen Warren3a529102012-09-21 09:51:01 +0000261{
262 if (argc < 2)
263 return CMD_RET_USAGE;
264
265 if (!strcmp(argv[1], "uuid"))
266 return do_part_uuid(argc - 2, argv + 2);
267 else if (!strcmp(argv[1], "list"))
268 return do_part_list(argc - 2, argv + 2);
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200269 else if (!strcmp(argv[1], "start"))
270 return do_part_start(argc - 2, argv + 2);
271 else if (!strcmp(argv[1], "size"))
272 return do_part_size(argc - 2, argv + 2);
Ruslan Trofymenkodb9bd6362019-06-14 17:01:26 +0300273 else if (!strcmp(argv[1], "number"))
274 return do_part_number(argc - 2, argv + 2);
Niel Fourie40102bc2020-03-24 16:17:03 +0100275 else if (!strcmp(argv[1], "types"))
276 return do_part_types(argc - 2, argv + 2);
Simon Glassed338b72023-07-15 21:38:47 -0600277 else if (!strcmp(argv[1], "set"))
278 return do_part_set(argc - 2, argv + 2);
Enric Balletbo i Serra0359a092023-01-10 17:19:34 +0100279#ifdef CONFIG_PARTITION_TYPE_GUID
280 else if (!strcmp(argv[1], "type"))
281 return do_part_type(argc - 2, argv + 2);
282#endif
Stephen Warren3a529102012-09-21 09:51:01 +0000283 return CMD_RET_USAGE;
284}
285
286U_BOOT_CMD(
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +0100287 part, CONFIG_SYS_MAXARGS, 1, do_part,
Stephen Warren3a529102012-09-21 09:51:01 +0000288 "disk partition related commands",
maxin.john@enea.com9bc711d2015-04-28 01:44:58 +0200289 "uuid <interface> <dev>:<part>\n"
Stephen Warren3a529102012-09-21 09:51:01 +0000290 " - print partition UUID\n"
291 "part uuid <interface> <dev>:<part> <varname>\n"
292 " - set environment variable to partition UUID\n"
293 "part list <interface> <dev>\n"
Sjoerd Simonsca8982b2015-01-05 18:13:37 +0100294 " - print a device's partition table\n"
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +0100295 "part list <interface> <dev> [flags] <varname>\n"
296 " - set environment variable to the list of partitions\n"
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200297 " flags can be -bootable (list only bootable partitions)\n"
298 "part start <interface> <dev> <part> <varname>\n"
299 " - set environment variable to the start of the partition (in blocks)\n"
Sam Protsenko515a8632018-02-26 23:17:59 +0200300 " part can be either partition number or partition name\n"
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200301 "part size <interface> <dev> <part> <varname>\n"
Sam Protsenko515a8632018-02-26 23:17:59 +0200302 " - set environment variable to the size of the partition (in blocks)\n"
Ruslan Trofymenkodb9bd6362019-06-14 17:01:26 +0300303 " part can be either partition number or partition name\n"
304 "part number <interface> <dev> <part> <varname>\n"
305 " - set environment variable to the partition number using the partition name\n"
Niel Fourie40102bc2020-03-24 16:17:03 +0100306 " part must be specified as partition name\n"
Enric Balletbo i Serra0359a092023-01-10 17:19:34 +0100307#ifdef CONFIG_PARTITION_TYPE_GUID
308 "part type <interface> <dev>:<part>\n"
309 " - print partition type\n"
Enric Balletbo i Serra0359a092023-01-10 17:19:34 +0100310 "part type <interface> <dev>:<part> <varname>\n"
311 " - set environment variable to partition type\n"
Marek Vasutf5b02722023-12-10 22:25:07 +0100312#endif
Simon Glassed338b72023-07-15 21:38:47 -0600313 "part set <interface> <dev> type\n"
314 " - set partition type for a device\n"
Niel Fourie40102bc2020-03-24 16:17:03 +0100315 "part types\n"
316 " - list supported partition table types"
Stephen Warren3a529102012-09-21 09:51:01 +0000317);