blob: 746bf40b2d0a8ad50c78b7d3e49d96ab7a074990 [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
Jeroen Hofsteef384fbf2014-06-23 00:22:08 +020025static int do_part_uuid(int argc, char * const argv[])
Stephen Warren3a529102012-09-21 09:51:01 +000026{
27 int part;
Simon Glasse3394752016-02-29 15:25:34 -070028 struct blk_desc *dev_desc;
Stephen Warren3a529102012-09-21 09:51:01 +000029 disk_partition_t info;
30
31 if (argc < 2)
32 return CMD_RET_USAGE;
33 if (argc > 3)
34 return CMD_RET_USAGE;
35
Simon Glasse76ee972016-02-29 15:25:44 -070036 part = blk_get_device_part_str(argv[0], argv[1], &dev_desc, &info, 0);
Stephen Warren3a529102012-09-21 09:51:01 +000037 if (part < 0)
38 return 1;
39
40 if (argc > 2)
Simon Glass6a38e412017-08-03 12:22:09 -060041 env_set(argv[2], info.uuid);
Stephen Warren3a529102012-09-21 09:51:01 +000042 else
43 printf("%s\n", info.uuid);
44
45 return 0;
46}
47
Jeroen Hofsteef384fbf2014-06-23 00:22:08 +020048static int do_part_list(int argc, char * const argv[])
Stephen Warren3a529102012-09-21 09:51:01 +000049{
50 int ret;
Simon Glasse3394752016-02-29 15:25:34 -070051 struct blk_desc *desc;
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +010052 char *var = NULL;
53 bool bootable = false;
54 int i;
Stephen Warren3a529102012-09-21 09:51:01 +000055
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +010056 if (argc < 2)
Stephen Warren3a529102012-09-21 09:51:01 +000057 return CMD_RET_USAGE;
58
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +010059 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
Simon Glasse6649a62016-02-29 15:25:43 -070080 ret = blk_get_device_by_str(argv[0], argv[1], &desc);
Stephen Warren3a529102012-09-21 09:51:01 +000081 if (ret < 0)
82 return 1;
83
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +010084 if (var != NULL) {
Sjoerd Simonsca8982b2015-01-05 18:13:37 +010085 int p;
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +010086 char str[512] = { '\0', };
Paul Kocialkowskib613cb32015-06-15 21:35:04 +020087 disk_partition_t info;
Sjoerd Simonsca8982b2015-01-05 18:13:37 +010088
89 for (p = 1; p < 128; p++) {
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +010090 char t[5];
Simon Glassb89a8442016-02-29 15:25:48 -070091 int r = part_get_info(desc, p, &info);
Sjoerd Simonsca8982b2015-01-05 18:13:37 +010092
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +010093 if (r != 0)
94 continue;
95
96 if (bootable && !info.bootable)
97 continue;
98
Stephen Warren78bb0c12015-12-09 09:48:04 -070099 sprintf(t, "%s%x", str[0] ? " " : "", p);
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +0100100 strcat(str, t);
Sjoerd Simonsca8982b2015-01-05 18:13:37 +0100101 }
Simon Glass6a38e412017-08-03 12:22:09 -0600102 env_set(var, str);
Sjoerd Simonsca8982b2015-01-05 18:13:37 +0100103 return 0;
104 }
105
Simon Glassb89a8442016-02-29 15:25:48 -0700106 part_print(desc);
Stephen Warren3a529102012-09-21 09:51:01 +0000107
108 return 0;
109}
110
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200111static int do_part_start(int argc, char * const argv[])
112{
Simon Glasse3394752016-02-29 15:25:34 -0700113 struct blk_desc *desc;
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200114 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
Simon Glasse6649a62016-02-29 15:25:43 -0700127 ret = blk_get_device_by_str(argv[0], argv[1], &desc);
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200128 if (ret < 0)
129 return 1;
130
Simon Glassb89a8442016-02-29 15:25:48 -0700131 err = part_get_info(desc, part, &info);
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200132 if (err)
133 return 1;
134
135 snprintf(buf, sizeof(buf), LBAF, info.start);
136
137 if (argc > 3)
Simon Glass6a38e412017-08-03 12:22:09 -0600138 env_set(argv[3], buf);
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200139 else
140 printf("%s\n", buf);
141
142 return 0;
143}
144
145static int do_part_size(int argc, char * const argv[])
146{
Simon Glasse3394752016-02-29 15:25:34 -0700147 struct blk_desc *desc;
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200148 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
Simon Glasse6649a62016-02-29 15:25:43 -0700161 ret = blk_get_device_by_str(argv[0], argv[1], &desc);
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200162 if (ret < 0)
163 return 1;
164
Simon Glassb89a8442016-02-29 15:25:48 -0700165 err = part_get_info(desc, part, &info);
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200166 if (err)
167 return 1;
168
169 snprintf(buf, sizeof(buf), LBAF, info.size);
170
171 if (argc > 3)
Simon Glass6a38e412017-08-03 12:22:09 -0600172 env_set(argv[3], buf);
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200173 else
174 printf("%s\n", buf);
175
176 return 0;
177}
178
Jeroen Hofsteef384fbf2014-06-23 00:22:08 +0200179static int do_part(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Stephen Warren3a529102012-09-21 09:51:01 +0000180{
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);
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200188 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);
Stephen Warren3a529102012-09-21 09:51:01 +0000192
193 return CMD_RET_USAGE;
194}
195
196U_BOOT_CMD(
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +0100197 part, CONFIG_SYS_MAXARGS, 1, do_part,
Stephen Warren3a529102012-09-21 09:51:01 +0000198 "disk partition related commands",
maxin.john@enea.com9bc711d2015-04-28 01:44:58 +0200199 "uuid <interface> <dev>:<part>\n"
Stephen Warren3a529102012-09-21 09:51:01 +0000200 " - 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"
Sjoerd Simonsca8982b2015-01-05 18:13:37 +0100204 " - print a device's partition table\n"
Sjoerd Simons43cdb5d2015-02-25 23:23:50 +0100205 "part list <interface> <dev> [flags] <varname>\n"
206 " - set environment variable to the list of partitions\n"
Paul Kocialkowskicf7b3762015-06-15 21:35:05 +0200207 " 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)"
Stephen Warren3a529102012-09-21 09:51:01 +0000212);