blob: 4b2e06e615af9a5feded873b91284b0d28934ce0 [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>
Simon Glass9fb625c2019-08-01 09:46:51 -060021#include <env.h>
Stephen Warren5cf41dc2012-09-21 09:51:01 +000022#include <part.h>
23#include <vsprintf.h>
24
Sam Protsenko64e6a492018-02-26 23:18:00 +020025enum cmd_part_info {
26 CMD_PART_INFO_START = 0,
27 CMD_PART_INFO_SIZE,
Ruslan Trofymenkobe683752019-06-14 17:01:26 +030028 CMD_PART_INFO_NUMBER
Sam Protsenko64e6a492018-02-26 23:18:00 +020029};
30
Jeroen Hofstee0e350f82014-06-23 00:22:08 +020031static int do_part_uuid(int argc, char * const argv[])
Stephen Warren5cf41dc2012-09-21 09:51:01 +000032{
33 int part;
Simon Glass4101f682016-02-29 15:25:34 -070034 struct blk_desc *dev_desc;
Simon Glass05289792020-05-10 11:39:57 -060035 struct disk_partition info;
Stephen Warren5cf41dc2012-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 Glasse35929e2016-02-29 15:25:44 -070042 part = blk_get_device_part_str(argv[0], argv[1], &dev_desc, &info, 0);
Stephen Warren5cf41dc2012-09-21 09:51:01 +000043 if (part < 0)
44 return 1;
45
46 if (argc > 2)
Simon Glass382bee52017-08-03 12:22:09 -060047 env_set(argv[2], info.uuid);
Stephen Warren5cf41dc2012-09-21 09:51:01 +000048 else
49 printf("%s\n", info.uuid);
50
51 return 0;
52}
53
Jeroen Hofstee0e350f82014-06-23 00:22:08 +020054static int do_part_list(int argc, char * const argv[])
Stephen Warren5cf41dc2012-09-21 09:51:01 +000055{
56 int ret;
Simon Glass4101f682016-02-29 15:25:34 -070057 struct blk_desc *desc;
Sjoerd Simons0798d6f2015-02-25 23:23:50 +010058 char *var = NULL;
59 bool bootable = false;
60 int i;
Stephen Warren5cf41dc2012-09-21 09:51:01 +000061
Sjoerd Simons0798d6f2015-02-25 23:23:50 +010062 if (argc < 2)
Stephen Warren5cf41dc2012-09-21 09:51:01 +000063 return CMD_RET_USAGE;
64
Sjoerd Simons0798d6f2015-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 Glassebac37c2016-02-29 15:25:43 -070086 ret = blk_get_device_by_str(argv[0], argv[1], &desc);
Stephen Warren5cf41dc2012-09-21 09:51:01 +000087 if (ret < 0)
88 return 1;
89
Sjoerd Simons0798d6f2015-02-25 23:23:50 +010090 if (var != NULL) {
Sjoerd Simonse86df6e2015-01-05 18:13:37 +010091 int p;
Sjoerd Simons0798d6f2015-02-25 23:23:50 +010092 char str[512] = { '\0', };
Simon Glass05289792020-05-10 11:39:57 -060093 struct disk_partition info;
Sjoerd Simonse86df6e2015-01-05 18:13:37 +010094
Kever Yang7e295112018-09-07 17:37:36 +080095 for (p = 1; p < MAX_SEARCH_PARTITIONS; p++) {
Sjoerd Simons0798d6f2015-02-25 23:23:50 +010096 char t[5];
Simon Glass3e8bd462016-02-29 15:25:48 -070097 int r = part_get_info(desc, p, &info);
Sjoerd Simonse86df6e2015-01-05 18:13:37 +010098
Sjoerd Simons0798d6f2015-02-25 23:23:50 +010099 if (r != 0)
100 continue;
101
102 if (bootable && !info.bootable)
103 continue;
104
Stephen Warren653447b2015-12-09 09:48:04 -0700105 sprintf(t, "%s%x", str[0] ? " " : "", p);
Sjoerd Simons0798d6f2015-02-25 23:23:50 +0100106 strcat(str, t);
Sjoerd Simonse86df6e2015-01-05 18:13:37 +0100107 }
Simon Glass382bee52017-08-03 12:22:09 -0600108 env_set(var, str);
Sjoerd Simonse86df6e2015-01-05 18:13:37 +0100109 return 0;
110 }
111
Simon Glass3e8bd462016-02-29 15:25:48 -0700112 part_print(desc);
Stephen Warren5cf41dc2012-09-21 09:51:01 +0000113
114 return 0;
115}
116
Sam Protsenko64e6a492018-02-26 23:18:00 +0200117static int do_part_info(int argc, char * const argv[], enum cmd_part_info param)
Paul Kocialkowski8607c4f2015-06-15 21:35:05 +0200118{
Simon Glass4101f682016-02-29 15:25:34 -0700119 struct blk_desc *desc;
Simon Glass05289792020-05-10 11:39:57 -0600120 struct disk_partition info;
Paul Kocialkowski8607c4f2015-06-15 21:35:05 +0200121 char buf[512] = { 0 };
Sam Protsenko36df6162018-02-26 23:17:59 +0200122 char *endp;
Paul Kocialkowski8607c4f2015-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 Glassebac37c2016-02-29 15:25:43 -0700132 ret = blk_get_device_by_str(argv[0], argv[1], &desc);
Paul Kocialkowski8607c4f2015-06-15 21:35:05 +0200133 if (ret < 0)
134 return 1;
135
Sam Protsenko36df6162018-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);
143 if (part == -1)
144 return 1;
145 }
Paul Kocialkowski8607c4f2015-06-15 21:35:05 +0200146
Sam Protsenko64e6a492018-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 Trofymenkobe683752019-06-14 17:01:26 +0300154 case CMD_PART_INFO_NUMBER:
Roman Stratiienko400b9552019-09-20 15:59:18 +0200155 snprintf(buf, sizeof(buf), "0x%x", part);
Ruslan Trofymenkobe683752019-06-14 17:01:26 +0300156 break;
Sam Protsenko64e6a492018-02-26 23:18:00 +0200157 default:
158 printf("** Unknown cmd_part_info value: %d\n", param);
159 return 1;
160 }
Paul Kocialkowski8607c4f2015-06-15 21:35:05 +0200161
162 if (argc > 3)
Simon Glass382bee52017-08-03 12:22:09 -0600163 env_set(argv[3], buf);
Paul Kocialkowski8607c4f2015-06-15 21:35:05 +0200164 else
165 printf("%s\n", buf);
166
167 return 0;
168}
169
Sam Protsenko64e6a492018-02-26 23:18:00 +0200170static int do_part_start(int argc, char * const argv[])
171{
172 return do_part_info(argc, argv, CMD_PART_INFO_START);
173}
174
Paul Kocialkowski8607c4f2015-06-15 21:35:05 +0200175static int do_part_size(int argc, char * const argv[])
176{
Sam Protsenko64e6a492018-02-26 23:18:00 +0200177 return do_part_info(argc, argv, CMD_PART_INFO_SIZE);
Paul Kocialkowski8607c4f2015-06-15 21:35:05 +0200178}
179
Ruslan Trofymenkobe683752019-06-14 17:01:26 +0300180static int do_part_number(int argc, char * const argv[])
181{
182 return do_part_info(argc, argv, CMD_PART_INFO_NUMBER);
183}
184
Jeroen Hofstee0e350f82014-06-23 00:22:08 +0200185static int do_part(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Stephen Warren5cf41dc2012-09-21 09:51:01 +0000186{
187 if (argc < 2)
188 return CMD_RET_USAGE;
189
190 if (!strcmp(argv[1], "uuid"))
191 return do_part_uuid(argc - 2, argv + 2);
192 else if (!strcmp(argv[1], "list"))
193 return do_part_list(argc - 2, argv + 2);
Paul Kocialkowski8607c4f2015-06-15 21:35:05 +0200194 else if (!strcmp(argv[1], "start"))
195 return do_part_start(argc - 2, argv + 2);
196 else if (!strcmp(argv[1], "size"))
197 return do_part_size(argc - 2, argv + 2);
Ruslan Trofymenkobe683752019-06-14 17:01:26 +0300198 else if (!strcmp(argv[1], "number"))
199 return do_part_number(argc - 2, argv + 2);
Stephen Warren5cf41dc2012-09-21 09:51:01 +0000200
201 return CMD_RET_USAGE;
202}
203
204U_BOOT_CMD(
Sjoerd Simons0798d6f2015-02-25 23:23:50 +0100205 part, CONFIG_SYS_MAXARGS, 1, do_part,
Stephen Warren5cf41dc2012-09-21 09:51:01 +0000206 "disk partition related commands",
maxin.john@enea.com8ca584e2015-04-28 01:44:58 +0200207 "uuid <interface> <dev>:<part>\n"
Stephen Warren5cf41dc2012-09-21 09:51:01 +0000208 " - print partition UUID\n"
209 "part uuid <interface> <dev>:<part> <varname>\n"
210 " - set environment variable to partition UUID\n"
211 "part list <interface> <dev>\n"
Sjoerd Simonse86df6e2015-01-05 18:13:37 +0100212 " - print a device's partition table\n"
Sjoerd Simons0798d6f2015-02-25 23:23:50 +0100213 "part list <interface> <dev> [flags] <varname>\n"
214 " - set environment variable to the list of partitions\n"
Paul Kocialkowski8607c4f2015-06-15 21:35:05 +0200215 " flags can be -bootable (list only bootable partitions)\n"
216 "part start <interface> <dev> <part> <varname>\n"
217 " - set environment variable to the start of the partition (in blocks)\n"
Sam Protsenko36df6162018-02-26 23:17:59 +0200218 " part can be either partition number or partition name\n"
Paul Kocialkowski8607c4f2015-06-15 21:35:05 +0200219 "part size <interface> <dev> <part> <varname>\n"
Sam Protsenko36df6162018-02-26 23:17:59 +0200220 " - set environment variable to the size of the partition (in blocks)\n"
Ruslan Trofymenkobe683752019-06-14 17:01:26 +0300221 " part can be either partition number or partition name\n"
222 "part number <interface> <dev> <part> <varname>\n"
223 " - set environment variable to the partition number using the partition name\n"
224 " part must be specified as partition name"
Stephen Warren5cf41dc2012-09-21 09:51:01 +0000225);