1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
5 * made from cmd_ext2, which was:
8 * esd gmbh <www.esd-electronics.com>
9 * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
11 * made from cmd_reiserfs by
13 * (C) Copyright 2003 - 2004
14 * Sysgo Real-Time Solutions, AG <www.elinos.com>
15 * Pavel Bartusek <pba@sysgo.com>
26 CMD_PART_INFO_START = 0,
31 static int do_part_uuid(int argc, char *const argv[])
34 struct blk_desc *dev_desc;
35 struct disk_partition info;
42 part = blk_get_device_part_str(argv[0], argv[1], &dev_desc, &info, 0);
47 env_set(argv[2], info.uuid);
49 printf("%s\n", info.uuid);
54 static int do_part_list(int argc, char *const argv[])
57 struct blk_desc *desc;
59 bool bootable = false;
66 for (i = 2; i < argc ; i++) {
67 if (argv[i][0] == '-') {
68 if (!strcmp(argv[i], "-bootable")) {
71 printf("Unknown option %s\n", argv[i]);
80 /* Loops should have been exited at the last argument, which
81 * as it contained the variable */
86 ret = blk_get_device_by_str(argv[0], argv[1], &desc);
92 char str[3 * MAX_SEARCH_PARTITIONS] = { '\0', };
93 struct disk_partition info;
95 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
97 int r = part_get_info(desc, p, &info);
102 if (bootable && !info.bootable)
105 sprintf(t, "%s%x", str[0] ? " " : "", p);
117 static int do_part_info(int argc, char *const argv[], enum cmd_part_info param)
119 struct blk_desc *desc;
120 struct disk_partition info;
121 char buf[512] = { 0 };
128 return CMD_RET_USAGE;
130 return CMD_RET_USAGE;
132 ret = blk_get_device_by_str(argv[0], argv[1], &desc);
136 part = simple_strtoul(argv[2], &endp, 0);
138 err = part_get_info(desc, part, &info);
142 part = part_get_info_by_name(desc, argv[2], &info);
148 case CMD_PART_INFO_START:
149 snprintf(buf, sizeof(buf), LBAF, info.start);
151 case CMD_PART_INFO_SIZE:
152 snprintf(buf, sizeof(buf), LBAF, info.size);
154 case CMD_PART_INFO_NUMBER:
155 snprintf(buf, sizeof(buf), "0x%x", part);
158 printf("** Unknown cmd_part_info value: %d\n", param);
163 env_set(argv[3], buf);
170 static int do_part_start(int argc, char *const argv[])
172 return do_part_info(argc, argv, CMD_PART_INFO_START);
175 static int do_part_size(int argc, char *const argv[])
177 return do_part_info(argc, argv, CMD_PART_INFO_SIZE);
180 static int do_part_number(int argc, char *const argv[])
182 return do_part_info(argc, argv, CMD_PART_INFO_NUMBER);
185 #ifdef CONFIG_PARTITION_TYPE_GUID
186 static int do_part_type(int argc, char *const argv[])
189 struct blk_desc *dev_desc;
190 struct disk_partition info;
193 return CMD_RET_USAGE;
195 return CMD_RET_USAGE;
197 part = blk_get_device_part_str(argv[0], argv[1], &dev_desc, &info, 0);
202 env_set(argv[2], info.type_guid);
204 printf("%s\n", info.type_guid);
210 static int do_part_types(int argc, char * const argv[])
212 struct part_driver *drv = ll_entry_start(struct part_driver,
214 const int n_ents = ll_entry_count(struct part_driver, part_driver);
215 struct part_driver *entry;
218 puts("Supported partition tables");
220 for (entry = drv; entry != drv + n_ents; entry++) {
221 printf("%c %s", i ? ',' : ':', entry->name);
227 return CMD_RET_SUCCESS;
230 static int do_part(struct cmd_tbl *cmdtp, int flag, int argc,
234 return CMD_RET_USAGE;
236 if (!strcmp(argv[1], "uuid"))
237 return do_part_uuid(argc - 2, argv + 2);
238 else if (!strcmp(argv[1], "list"))
239 return do_part_list(argc - 2, argv + 2);
240 else if (!strcmp(argv[1], "start"))
241 return do_part_start(argc - 2, argv + 2);
242 else if (!strcmp(argv[1], "size"))
243 return do_part_size(argc - 2, argv + 2);
244 else if (!strcmp(argv[1], "number"))
245 return do_part_number(argc - 2, argv + 2);
246 else if (!strcmp(argv[1], "types"))
247 return do_part_types(argc - 2, argv + 2);
248 #ifdef CONFIG_PARTITION_TYPE_GUID
249 else if (!strcmp(argv[1], "type"))
250 return do_part_type(argc - 2, argv + 2);
252 return CMD_RET_USAGE;
256 part, CONFIG_SYS_MAXARGS, 1, do_part,
257 "disk partition related commands",
258 "uuid <interface> <dev>:<part>\n"
259 " - print partition UUID\n"
260 "part uuid <interface> <dev>:<part> <varname>\n"
261 " - set environment variable to partition UUID\n"
262 "part list <interface> <dev>\n"
263 " - print a device's partition table\n"
264 "part list <interface> <dev> [flags] <varname>\n"
265 " - set environment variable to the list of partitions\n"
266 " flags can be -bootable (list only bootable partitions)\n"
267 "part start <interface> <dev> <part> <varname>\n"
268 " - set environment variable to the start of the partition (in blocks)\n"
269 " part can be either partition number or partition name\n"
270 "part size <interface> <dev> <part> <varname>\n"
271 " - set environment variable to the size of the partition (in blocks)\n"
272 " part can be either partition number or partition name\n"
273 "part number <interface> <dev> <part> <varname>\n"
274 " - set environment variable to the partition number using the partition name\n"
275 " part must be specified as partition name\n"
276 #ifdef CONFIG_PARTITION_TYPE_GUID
277 "part type <interface> <dev>:<part>\n"
278 " - print partition type\n"
280 "part type <interface> <dev>:<part> <varname>\n"
281 " - set environment variable to partition type\n"
283 " - list supported partition table types"