blob: bee204fff06dddd06a64f484182c220584e72444 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stephen Warren5cf41dc2012-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 Warren5cf41dc2012-09-21 09:51:01 +000016 */
17
18#include <common.h>
19#include <config.h>
20#include <command.h>
21#include <part.h>
22#include <vsprintf.h>
23
Sam Protsenko64e6a492018-02-26 23:18:00 +020024enum cmd_part_info {
25 CMD_PART_INFO_START = 0,
26 CMD_PART_INFO_SIZE,
27};
28
Jeroen Hofstee0e350f82014-06-23 00:22:08 +020029static int do_part_uuid(int argc, char * const argv[])
Stephen Warren5cf41dc2012-09-21 09:51:01 +000030{
31 int part;
Simon Glass4101f682016-02-29 15:25:34 -070032 struct blk_desc *dev_desc;
Stephen Warren5cf41dc2012-09-21 09:51:01 +000033 disk_partition_t info;
34
35 if (argc < 2)
36 return CMD_RET_USAGE;
37 if (argc > 3)
38 return CMD_RET_USAGE;
39
Simon Glasse35929e2016-02-29 15:25:44 -070040 part = blk_get_device_part_str(argv[0], argv[1], &dev_desc, &info, 0);
Stephen Warren5cf41dc2012-09-21 09:51:01 +000041 if (part < 0)
42 return 1;
43
44 if (argc > 2)
Simon Glass382bee52017-08-03 12:22:09 -060045 env_set(argv[2], info.uuid);
Stephen Warren5cf41dc2012-09-21 09:51:01 +000046 else
47 printf("%s\n", info.uuid);
48
49 return 0;
50}
51
Jeroen Hofstee0e350f82014-06-23 00:22:08 +020052static int do_part_list(int argc, char * const argv[])
Stephen Warren5cf41dc2012-09-21 09:51:01 +000053{
54 int ret;
Simon Glass4101f682016-02-29 15:25:34 -070055 struct blk_desc *desc;
Sjoerd Simons0798d6f2015-02-25 23:23:50 +010056 char *var = NULL;
57 bool bootable = false;
58 int i;
Stephen Warren5cf41dc2012-09-21 09:51:01 +000059
Sjoerd Simons0798d6f2015-02-25 23:23:50 +010060 if (argc < 2)
Stephen Warren5cf41dc2012-09-21 09:51:01 +000061 return CMD_RET_USAGE;
62
Sjoerd Simons0798d6f2015-02-25 23:23:50 +010063 if (argc > 2) {
64 for (i = 2; i < argc ; i++) {
65 if (argv[i][0] == '-') {
66 if (!strcmp(argv[i], "-bootable")) {
67 bootable = true;
68 } else {
69 printf("Unknown option %s\n", argv[i]);
70 return CMD_RET_USAGE;
71 }
72 } else {
73 var = argv[i];
74 break;
75 }
76 }
77
78 /* Loops should have been exited at the last argument, which
79 * as it contained the variable */
80 if (argc != i + 1)
81 return CMD_RET_USAGE;
82 }
83
Simon Glassebac37c2016-02-29 15:25:43 -070084 ret = blk_get_device_by_str(argv[0], argv[1], &desc);
Stephen Warren5cf41dc2012-09-21 09:51:01 +000085 if (ret < 0)
86 return 1;
87
Sjoerd Simons0798d6f2015-02-25 23:23:50 +010088 if (var != NULL) {
Sjoerd Simonse86df6e2015-01-05 18:13:37 +010089 int p;
Sjoerd Simons0798d6f2015-02-25 23:23:50 +010090 char str[512] = { '\0', };
Paul Kocialkowskia78f78e2015-06-15 21:35:04 +020091 disk_partition_t info;
Sjoerd Simonse86df6e2015-01-05 18:13:37 +010092
93 for (p = 1; p < 128; p++) {
Sjoerd Simons0798d6f2015-02-25 23:23:50 +010094 char t[5];
Simon Glass3e8bd462016-02-29 15:25:48 -070095 int r = part_get_info(desc, p, &info);
Sjoerd Simonse86df6e2015-01-05 18:13:37 +010096
Sjoerd Simons0798d6f2015-02-25 23:23:50 +010097 if (r != 0)
98 continue;
99
100 if (bootable && !info.bootable)
101 continue;
102
Stephen Warren653447b2015-12-09 09:48:04 -0700103 sprintf(t, "%s%x", str[0] ? " " : "", p);
Sjoerd Simons0798d6f2015-02-25 23:23:50 +0100104 strcat(str, t);
Sjoerd Simonse86df6e2015-01-05 18:13:37 +0100105 }
Simon Glass382bee52017-08-03 12:22:09 -0600106 env_set(var, str);
Sjoerd Simonse86df6e2015-01-05 18:13:37 +0100107 return 0;
108 }
109
Simon Glass3e8bd462016-02-29 15:25:48 -0700110 part_print(desc);
Stephen Warren5cf41dc2012-09-21 09:51:01 +0000111
112 return 0;
113}
114
Sam Protsenko64e6a492018-02-26 23:18:00 +0200115static int do_part_info(int argc, char * const argv[], enum cmd_part_info param)
Paul Kocialkowski8607c4f2015-06-15 21:35:05 +0200116{
Simon Glass4101f682016-02-29 15:25:34 -0700117 struct blk_desc *desc;
Paul Kocialkowski8607c4f2015-06-15 21:35:05 +0200118 disk_partition_t info;
119 char buf[512] = { 0 };
Sam Protsenko36df6162018-02-26 23:17:59 +0200120 char *endp;
Paul Kocialkowski8607c4f2015-06-15 21:35:05 +0200121 int part;
122 int err;
123 int ret;
124
125 if (argc < 3)
126 return CMD_RET_USAGE;
127 if (argc > 4)
128 return CMD_RET_USAGE;
129
Simon Glassebac37c2016-02-29 15:25:43 -0700130 ret = blk_get_device_by_str(argv[0], argv[1], &desc);
Paul Kocialkowski8607c4f2015-06-15 21:35:05 +0200131 if (ret < 0)
132 return 1;
133
Sam Protsenko36df6162018-02-26 23:17:59 +0200134 part = simple_strtoul(argv[2], &endp, 0);
135 if (*endp == '\0') {
136 err = part_get_info(desc, part, &info);
137 if (err)
138 return 1;
139 } else {
140 part = part_get_info_by_name(desc, argv[2], &info);
141 if (part == -1)
142 return 1;
143 }
Paul Kocialkowski8607c4f2015-06-15 21:35:05 +0200144
Sam Protsenko64e6a492018-02-26 23:18:00 +0200145 switch (param) {
146 case CMD_PART_INFO_START:
147 snprintf(buf, sizeof(buf), LBAF, info.start);
148 break;
149 case CMD_PART_INFO_SIZE:
150 snprintf(buf, sizeof(buf), LBAF, info.size);
151 break;
152 default:
153 printf("** Unknown cmd_part_info value: %d\n", param);
154 return 1;
155 }
Paul Kocialkowski8607c4f2015-06-15 21:35:05 +0200156
157 if (argc > 3)
Simon Glass382bee52017-08-03 12:22:09 -0600158 env_set(argv[3], buf);
Paul Kocialkowski8607c4f2015-06-15 21:35:05 +0200159 else
160 printf("%s\n", buf);
161
162 return 0;
163}
164
Sam Protsenko64e6a492018-02-26 23:18:00 +0200165static int do_part_start(int argc, char * const argv[])
166{
167 return do_part_info(argc, argv, CMD_PART_INFO_START);
168}
169
Paul Kocialkowski8607c4f2015-06-15 21:35:05 +0200170static int do_part_size(int argc, char * const argv[])
171{
Sam Protsenko64e6a492018-02-26 23:18:00 +0200172 return do_part_info(argc, argv, CMD_PART_INFO_SIZE);
Paul Kocialkowski8607c4f2015-06-15 21:35:05 +0200173}
174
Jeroen Hofstee0e350f82014-06-23 00:22:08 +0200175static int do_part(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Stephen Warren5cf41dc2012-09-21 09:51:01 +0000176{
177 if (argc < 2)
178 return CMD_RET_USAGE;
179
180 if (!strcmp(argv[1], "uuid"))
181 return do_part_uuid(argc - 2, argv + 2);
182 else if (!strcmp(argv[1], "list"))
183 return do_part_list(argc - 2, argv + 2);
Paul Kocialkowski8607c4f2015-06-15 21:35:05 +0200184 else if (!strcmp(argv[1], "start"))
185 return do_part_start(argc - 2, argv + 2);
186 else if (!strcmp(argv[1], "size"))
187 return do_part_size(argc - 2, argv + 2);
Stephen Warren5cf41dc2012-09-21 09:51:01 +0000188
189 return CMD_RET_USAGE;
190}
191
192U_BOOT_CMD(
Sjoerd Simons0798d6f2015-02-25 23:23:50 +0100193 part, CONFIG_SYS_MAXARGS, 1, do_part,
Stephen Warren5cf41dc2012-09-21 09:51:01 +0000194 "disk partition related commands",
maxin.john@enea.com8ca584e2015-04-28 01:44:58 +0200195 "uuid <interface> <dev>:<part>\n"
Stephen Warren5cf41dc2012-09-21 09:51:01 +0000196 " - print partition UUID\n"
197 "part uuid <interface> <dev>:<part> <varname>\n"
198 " - set environment variable to partition UUID\n"
199 "part list <interface> <dev>\n"
Sjoerd Simonse86df6e2015-01-05 18:13:37 +0100200 " - print a device's partition table\n"
Sjoerd Simons0798d6f2015-02-25 23:23:50 +0100201 "part list <interface> <dev> [flags] <varname>\n"
202 " - set environment variable to the list of partitions\n"
Paul Kocialkowski8607c4f2015-06-15 21:35:05 +0200203 " flags can be -bootable (list only bootable partitions)\n"
204 "part start <interface> <dev> <part> <varname>\n"
205 " - set environment variable to the start of the partition (in blocks)\n"
Sam Protsenko36df6162018-02-26 23:17:59 +0200206 " part can be either partition number or partition name\n"
Paul Kocialkowski8607c4f2015-06-15 21:35:05 +0200207 "part size <interface> <dev> <part> <varname>\n"
Sam Protsenko36df6162018-02-26 23:17:59 +0200208 " - set environment variable to the size of the partition (in blocks)\n"
209 " part can be either partition number or partition name"
Stephen Warren5cf41dc2012-09-21 09:51:01 +0000210);