blob: c75f85acd52d4dca58bebfab742cca99cc4f2641 [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
Tom Riniabb9a042024-05-18 20:20:43 -060018#include <common.h>
Stephen Warren3a529102012-09-21 09:51:01 +000019#include <config.h>
20#include <command.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060021#include <env.h>
Stephen Warren3a529102012-09-21 09:51:01 +000022#include <part.h>
23#include <vsprintf.h>
24
Sam Protsenko92bfb862018-02-26 23:18:00 +020025enum cmd_part_info {
26 CMD_PART_INFO_START = 0,
27 CMD_PART_INFO_SIZE,
Ruslan Trofymenkodb9bd6362019-06-14 17:01:26 +030028 CMD_PART_INFO_NUMBER
Sam Protsenko92bfb862018-02-26 23:18:00 +020029};
30
Simon Glassed38aef2020-05-10 11:40:03 -060031static int do_part_uuid(int argc, char *const argv[])
Stephen Warren3a529102012-09-21 09:51:01 +000032{
33 int part;
Simon Glasse3394752016-02-29 15:25:34 -070034 struct blk_desc *dev_desc;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060035 struct disk_partition info;
Stephen Warren3a529102012-09-21 09:51:01 +000036
37 if (argc < 2)
38 return CMD_RET_USAGE;
39 if (argc > 3)
40 return CMD_RET_USAGE;
41
Simon Glasse76ee972016-02-29 15:25:44 -070042 part = blk_get_device_part_str(argv[0], argv[1], &dev_desc, &info, 0);
Stephen Warren3a529102012-09-21 09:51:01 +000043 if (part < 0)
44 return 1;
45
46 if (argc > 2)
Simon Glass6a38e412017-08-03 12:22:09 -060047 env_set(argv[2], info.uuid);
Stephen Warren3a529102012-09-21 09:51:01 +000048 else
49 printf("%s\n", info.uuid);
50
51 return 0;
52}
53
Simon Glassed38aef2020-05-10 11:40:03 -060054static int do_part_list(int argc, char *const argv[])
Stephen Warren3a529102012-09-21 09:51:01 +000055{
56 int ret;
Simon Glasse3394752016-02-29 15:25:34 -070057 struct blk_desc *desc;
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +010058 char *var = NULL;
59 bool bootable = false;
60 int i;
Stephen Warren3a529102012-09-21 09:51:01 +000061
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +010062 if (argc < 2)
Stephen Warren3a529102012-09-21 09:51:01 +000063 return CMD_RET_USAGE;
64
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +010065 if (argc > 2) {
66 for (i = 2; i < argc ; i++) {
67 if (argv[i][0] == '-') {
68 if (!strcmp(argv[i], "-bootable")) {
69 bootable = true;
70 } else {
71 printf("Unknown option %s\n", argv[i]);
72 return CMD_RET_USAGE;
73 }
74 } else {
75 var = argv[i];
76 break;
77 }
78 }
79
80 /* Loops should have been exited at the last argument, which
81 * as it contained the variable */
82 if (argc != i + 1)
83 return CMD_RET_USAGE;
84 }
85
Simon Glasse6649a62016-02-29 15:25:43 -070086 ret = blk_get_device_by_str(argv[0], argv[1], &desc);
Stephen Warren3a529102012-09-21 09:51:01 +000087 if (ret < 0)
88 return 1;
89
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +010090 if (var != NULL) {
Sjoerd Simonsca8982b2015-01-05 18:13:37 +010091 int p;
Heinrich Schuchardt72d78832022-01-11 16:03:38 +010092 char str[3 * MAX_SEARCH_PARTITIONS] = { '\0', };
Simon Glassc1c4a8f2020-05-10 11:39:57 -060093 struct disk_partition info;
Sjoerd Simonsca8982b2015-01-05 18:13:37 +010094
Heinrich Schuchardt72d78832022-01-11 16:03:38 +010095 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +010096 char t[5];
Simon Glassb89a8442016-02-29 15:25:48 -070097 int r = part_get_info(desc, p, &info);
Sjoerd Simonsca8982b2015-01-05 18:13:37 +010098
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +010099 if (r != 0)
100 continue;
101
102 if (bootable && !info.bootable)
103 continue;
104
Stephen Warren78bb0c12015-12-09 09:48:04 -0700105 sprintf(t, "%s%x", str[0] ? " " : "", p);
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +0100106 strcat(str, t);
Sjoerd Simonsca8982b2015-01-05 18:13:37 +0100107 }
Simon Glass6a38e412017-08-03 12:22:09 -0600108 env_set(var, str);
Sjoerd Simonsca8982b2015-01-05 18:13:37 +0100109 return 0;
110 }
111
Simon Glassb89a8442016-02-29 15:25:48 -0700112 part_print(desc);
Stephen Warren3a529102012-09-21 09:51:01 +0000113
114 return 0;
115}
116
Simon Glassed38aef2020-05-10 11:40:03 -0600117static int do_part_info(int argc, char *const argv[], enum cmd_part_info param)
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200118{
Simon Glasse3394752016-02-29 15:25:34 -0700119 struct blk_desc *desc;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600120 struct disk_partition info;
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200121 char buf[512] = { 0 };
Sam Protsenko515a8632018-02-26 23:17:59 +0200122 char *endp;
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200123 int part;
124 int err;
125 int ret;
126
127 if (argc < 3)
128 return CMD_RET_USAGE;
129 if (argc > 4)
130 return CMD_RET_USAGE;
131
Simon Glasse6649a62016-02-29 15:25:43 -0700132 ret = blk_get_device_by_str(argv[0], argv[1], &desc);
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200133 if (ret < 0)
134 return 1;
135
Sam Protsenko515a8632018-02-26 23:17:59 +0200136 part = simple_strtoul(argv[2], &endp, 0);
137 if (*endp == '\0') {
138 err = part_get_info(desc, part, &info);
139 if (err)
140 return 1;
141 } else {
142 part = part_get_info_by_name(desc, argv[2], &info);
Anders Delliena7b261b2021-06-22 10:40:01 +0100143 if (part < 0)
Sam Protsenko515a8632018-02-26 23:17:59 +0200144 return 1;
145 }
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200146
Sam Protsenko92bfb862018-02-26 23:18:00 +0200147 switch (param) {
148 case CMD_PART_INFO_START:
149 snprintf(buf, sizeof(buf), LBAF, info.start);
150 break;
151 case CMD_PART_INFO_SIZE:
152 snprintf(buf, sizeof(buf), LBAF, info.size);
153 break;
Ruslan Trofymenkodb9bd6362019-06-14 17:01:26 +0300154 case CMD_PART_INFO_NUMBER:
Roman Stratiienko014a3812019-09-20 15:59:18 +0200155 snprintf(buf, sizeof(buf), "0x%x", part);
Ruslan Trofymenkodb9bd6362019-06-14 17:01:26 +0300156 break;
Sam Protsenko92bfb862018-02-26 23:18:00 +0200157 default:
158 printf("** Unknown cmd_part_info value: %d\n", param);
159 return 1;
160 }
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200161
162 if (argc > 3)
Simon Glass6a38e412017-08-03 12:22:09 -0600163 env_set(argv[3], buf);
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200164 else
165 printf("%s\n", buf);
166
167 return 0;
168}
169
Simon Glassed38aef2020-05-10 11:40:03 -0600170static int do_part_start(int argc, char *const argv[])
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200171{
Sam Protsenko92bfb862018-02-26 23:18:00 +0200172 return do_part_info(argc, argv, CMD_PART_INFO_START);
173}
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200174
Simon Glassed38aef2020-05-10 11:40:03 -0600175static int do_part_size(int argc, char *const argv[])
Sam Protsenko92bfb862018-02-26 23:18:00 +0200176{
177 return do_part_info(argc, argv, CMD_PART_INFO_SIZE);
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200178}
179
Simon Glassed38aef2020-05-10 11:40:03 -0600180static int do_part_number(int argc, char *const argv[])
Ruslan Trofymenkodb9bd6362019-06-14 17:01:26 +0300181{
182 return do_part_info(argc, argv, CMD_PART_INFO_NUMBER);
183}
184
Simon Glassed338b72023-07-15 21:38:47 -0600185static int do_part_set(int argc, char *const argv[])
186{
187 const char *devname, *partstr, *typestr;
188 struct blk_desc *desc;
189 int dev;
190
191 if (argc < 3)
192 return CMD_RET_USAGE;
193
194 /* Look up the device */
195 devname = argv[0];
196 partstr = argv[1];
197 typestr = argv[2];
198 dev = blk_get_device_by_str(devname, partstr, &desc);
199 if (dev < 0) {
200 printf("** Bad device specification %s %s **\n", devname,
201 partstr);
202 return CMD_RET_FAILURE;
203 }
204
205 desc->part_type = part_get_type_by_name(typestr);
206 if (!desc->part_type) {
207 printf("Unknown partition type '%s'\n", typestr);
208 return CMD_RET_FAILURE;
209 }
210 part_print(desc);
211
212 return 0;
213}
214
Enric Balletbo i Serra0359a092023-01-10 17:19:34 +0100215#ifdef CONFIG_PARTITION_TYPE_GUID
216static int do_part_type(int argc, char *const argv[])
217{
218 int part;
219 struct blk_desc *dev_desc;
220 struct disk_partition info;
221
222 if (argc < 2)
223 return CMD_RET_USAGE;
224 if (argc > 3)
225 return CMD_RET_USAGE;
226
227 part = blk_get_device_part_str(argv[0], argv[1], &dev_desc, &info, 0);
228 if (part < 0)
229 return 1;
230
231 if (argc > 2)
232 env_set(argv[2], info.type_guid);
233 else
234 printf("%s\n", info.type_guid);
235
236 return 0;
237}
238#endif
239
Niel Fourie40102bc2020-03-24 16:17:03 +0100240static int do_part_types(int argc, char * const argv[])
241{
242 struct part_driver *drv = ll_entry_start(struct part_driver,
243 part_driver);
244 const int n_ents = ll_entry_count(struct part_driver, part_driver);
245 struct part_driver *entry;
246 int i = 0;
247
248 puts("Supported partition tables");
249
250 for (entry = drv; entry != drv + n_ents; entry++) {
251 printf("%c %s", i ? ',' : ':', entry->name);
252 i++;
253 }
254 if (!i)
255 puts(": <none>");
256 puts("\n");
257 return CMD_RET_SUCCESS;
258}
259
Simon Glassed38aef2020-05-10 11:40:03 -0600260static int do_part(struct cmd_tbl *cmdtp, int flag, int argc,
261 char *const argv[])
Stephen Warren3a529102012-09-21 09:51:01 +0000262{
263 if (argc < 2)
264 return CMD_RET_USAGE;
265
266 if (!strcmp(argv[1], "uuid"))
267 return do_part_uuid(argc - 2, argv + 2);
268 else if (!strcmp(argv[1], "list"))
269 return do_part_list(argc - 2, argv + 2);
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200270 else if (!strcmp(argv[1], "start"))
271 return do_part_start(argc - 2, argv + 2);
272 else if (!strcmp(argv[1], "size"))
273 return do_part_size(argc - 2, argv + 2);
Ruslan Trofymenkodb9bd6362019-06-14 17:01:26 +0300274 else if (!strcmp(argv[1], "number"))
275 return do_part_number(argc - 2, argv + 2);
Niel Fourie40102bc2020-03-24 16:17:03 +0100276 else if (!strcmp(argv[1], "types"))
277 return do_part_types(argc - 2, argv + 2);
Simon Glassed338b72023-07-15 21:38:47 -0600278 else if (!strcmp(argv[1], "set"))
279 return do_part_set(argc - 2, argv + 2);
Enric Balletbo i Serra0359a092023-01-10 17:19:34 +0100280#ifdef CONFIG_PARTITION_TYPE_GUID
281 else if (!strcmp(argv[1], "type"))
282 return do_part_type(argc - 2, argv + 2);
283#endif
Stephen Warren3a529102012-09-21 09:51:01 +0000284 return CMD_RET_USAGE;
285}
286
287U_BOOT_CMD(
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +0100288 part, CONFIG_SYS_MAXARGS, 1, do_part,
Stephen Warren3a529102012-09-21 09:51:01 +0000289 "disk partition related commands",
maxin.john@enea.com9bc711d2015-04-28 01:44:58 +0200290 "uuid <interface> <dev>:<part>\n"
Stephen Warren3a529102012-09-21 09:51:01 +0000291 " - print partition UUID\n"
292 "part uuid <interface> <dev>:<part> <varname>\n"
293 " - set environment variable to partition UUID\n"
294 "part list <interface> <dev>\n"
Sjoerd Simonsca8982b2015-01-05 18:13:37 +0100295 " - print a device's partition table\n"
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +0100296 "part list <interface> <dev> [flags] <varname>\n"
297 " - set environment variable to the list of partitions\n"
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200298 " flags can be -bootable (list only bootable partitions)\n"
299 "part start <interface> <dev> <part> <varname>\n"
300 " - set environment variable to the start of the partition (in blocks)\n"
Sam Protsenko515a8632018-02-26 23:17:59 +0200301 " part can be either partition number or partition name\n"
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200302 "part size <interface> <dev> <part> <varname>\n"
Sam Protsenko515a8632018-02-26 23:17:59 +0200303 " - set environment variable to the size of the partition (in blocks)\n"
Ruslan Trofymenkodb9bd6362019-06-14 17:01:26 +0300304 " part can be either partition number or partition name\n"
305 "part number <interface> <dev> <part> <varname>\n"
306 " - set environment variable to the partition number using the partition name\n"
Niel Fourie40102bc2020-03-24 16:17:03 +0100307 " part must be specified as partition name\n"
Enric Balletbo i Serra0359a092023-01-10 17:19:34 +0100308#ifdef CONFIG_PARTITION_TYPE_GUID
309 "part type <interface> <dev>:<part>\n"
310 " - print partition type\n"
Enric Balletbo i Serra0359a092023-01-10 17:19:34 +0100311 "part type <interface> <dev>:<part> <varname>\n"
312 " - set environment variable to partition type\n"
Marek Vasutf5b02722023-12-10 22:25:07 +0100313#endif
Simon Glassed338b72023-07-15 21:38:47 -0600314 "part set <interface> <dev> type\n"
315 " - set partition type for a device\n"
Niel Fourie40102bc2020-03-24 16:17:03 +0100316 "part types\n"
317 " - list supported partition table types"
Stephen Warren3a529102012-09-21 09:51:01 +0000318);