blob: ec791fdc5d4cfb0f7017be9686055c24d07017c9 [file] [log] [blame]
Stephen Warren3a529102012-09-21 09:51:01 +00001/*
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 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020016 * SPDX-License-Identifier: GPL-2.0+
Stephen Warren3a529102012-09-21 09:51:01 +000017 */
18
19#include <common.h>
20#include <config.h>
21#include <command.h>
22#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,
28};
29
Jeroen Hofsteef384fbf2014-06-23 00:22:08 +020030static 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;
Stephen Warren3a529102012-09-21 09:51:01 +000034 disk_partition_t info;
35
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
Jeroen Hofsteef384fbf2014-06-23 00:22:08 +020053static 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;
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +010091 char str[512] = { '\0', };
Paul Kocialkowskib613cb32015-06-15 21:35:04 +020092 disk_partition_t info;
Sjoerd Simonsca8982b2015-01-05 18:13:37 +010093
94 for (p = 1; p < 128; 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
Sam Protsenko92bfb862018-02-26 23:18:00 +0200116static 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;
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200119 disk_partition_t info;
120 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);
142 if (part == -1)
143 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;
153 default:
154 printf("** Unknown cmd_part_info value: %d\n", param);
155 return 1;
156 }
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200157
158 if (argc > 3)
Simon Glass6a38e412017-08-03 12:22:09 -0600159 env_set(argv[3], buf);
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200160 else
161 printf("%s\n", buf);
162
163 return 0;
164}
165
Sam Protsenko92bfb862018-02-26 23:18:00 +0200166static int do_part_start(int argc, char * const argv[])
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200167{
Sam Protsenko92bfb862018-02-26 23:18:00 +0200168 return do_part_info(argc, argv, CMD_PART_INFO_START);
169}
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200170
Sam Protsenko92bfb862018-02-26 23:18:00 +0200171static int do_part_size(int argc, char * const argv[])
172{
173 return do_part_info(argc, argv, CMD_PART_INFO_SIZE);
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200174}
175
Jeroen Hofsteef384fbf2014-06-23 00:22:08 +0200176static int do_part(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Stephen Warren3a529102012-09-21 09:51:01 +0000177{
178 if (argc < 2)
179 return CMD_RET_USAGE;
180
181 if (!strcmp(argv[1], "uuid"))
182 return do_part_uuid(argc - 2, argv + 2);
183 else if (!strcmp(argv[1], "list"))
184 return do_part_list(argc - 2, argv + 2);
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200185 else if (!strcmp(argv[1], "start"))
186 return do_part_start(argc - 2, argv + 2);
187 else if (!strcmp(argv[1], "size"))
188 return do_part_size(argc - 2, argv + 2);
Stephen Warren3a529102012-09-21 09:51:01 +0000189
190 return CMD_RET_USAGE;
191}
192
193U_BOOT_CMD(
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +0100194 part, CONFIG_SYS_MAXARGS, 1, do_part,
Stephen Warren3a529102012-09-21 09:51:01 +0000195 "disk partition related commands",
maxin.john@enea.com9bc711d2015-04-28 01:44:58 +0200196 "uuid <interface> <dev>:<part>\n"
Stephen Warren3a529102012-09-21 09:51:01 +0000197 " - print partition UUID\n"
198 "part uuid <interface> <dev>:<part> <varname>\n"
199 " - set environment variable to partition UUID\n"
200 "part list <interface> <dev>\n"
Sjoerd Simonsca8982b2015-01-05 18:13:37 +0100201 " - print a device's partition table\n"
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +0100202 "part list <interface> <dev> [flags] <varname>\n"
203 " - set environment variable to the list of partitions\n"
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200204 " flags can be -bootable (list only bootable partitions)\n"
205 "part start <interface> <dev> <part> <varname>\n"
206 " - set environment variable to the start of the partition (in blocks)\n"
Sam Protsenko515a8632018-02-26 23:17:59 +0200207 " part can be either partition number or partition name\n"
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200208 "part size <interface> <dev> <part> <varname>\n"
Sam Protsenko515a8632018-02-26 23:17:59 +0200209 " - set environment variable to the size of the partition (in blocks)\n"
210 " part can be either partition number or partition name"
Stephen Warren3a529102012-09-21 09:51:01 +0000211);