xref: /openbmc/u-boot/cmd/part.c (revision d1e15041)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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>
16  */
17 
18 #include <common.h>
19 #include <config.h>
20 #include <command.h>
21 #include <part.h>
22 #include <vsprintf.h>
23 
24 enum cmd_part_info {
25 	CMD_PART_INFO_START = 0,
26 	CMD_PART_INFO_SIZE,
27 };
28 
do_part_uuid(int argc,char * const argv[])29 static int do_part_uuid(int argc, char * const argv[])
30 {
31 	int part;
32 	struct blk_desc *dev_desc;
33 	disk_partition_t info;
34 
35 	if (argc < 2)
36 		return CMD_RET_USAGE;
37 	if (argc > 3)
38 		return CMD_RET_USAGE;
39 
40 	part = blk_get_device_part_str(argv[0], argv[1], &dev_desc, &info, 0);
41 	if (part < 0)
42 		return 1;
43 
44 	if (argc > 2)
45 		env_set(argv[2], info.uuid);
46 	else
47 		printf("%s\n", info.uuid);
48 
49 	return 0;
50 }
51 
do_part_list(int argc,char * const argv[])52 static int do_part_list(int argc, char * const argv[])
53 {
54 	int ret;
55 	struct blk_desc *desc;
56 	char *var = NULL;
57 	bool bootable = false;
58 	int i;
59 
60 	if (argc < 2)
61 		return CMD_RET_USAGE;
62 
63 	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 
84 	ret = blk_get_device_by_str(argv[0], argv[1], &desc);
85 	if (ret < 0)
86 		return 1;
87 
88 	if (var != NULL) {
89 		int p;
90 		char str[512] = { '\0', };
91 		disk_partition_t info;
92 
93 		for (p = 1; p < MAX_SEARCH_PARTITIONS; p++) {
94 			char t[5];
95 			int r = part_get_info(desc, p, &info);
96 
97 			if (r != 0)
98 				continue;
99 
100 			if (bootable && !info.bootable)
101 				continue;
102 
103 			sprintf(t, "%s%x", str[0] ? " " : "", p);
104 			strcat(str, t);
105 		}
106 		env_set(var, str);
107 		return 0;
108 	}
109 
110 	part_print(desc);
111 
112 	return 0;
113 }
114 
do_part_info(int argc,char * const argv[],enum cmd_part_info param)115 static int do_part_info(int argc, char * const argv[], enum cmd_part_info param)
116 {
117 	struct blk_desc *desc;
118 	disk_partition_t info;
119 	char buf[512] = { 0 };
120 	char *endp;
121 	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 
130 	ret = blk_get_device_by_str(argv[0], argv[1], &desc);
131 	if (ret < 0)
132 		return 1;
133 
134 	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 	}
144 
145 	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 	}
156 
157 	if (argc > 3)
158 		env_set(argv[3], buf);
159 	else
160 		printf("%s\n", buf);
161 
162 	return 0;
163 }
164 
do_part_start(int argc,char * const argv[])165 static int do_part_start(int argc, char * const argv[])
166 {
167 	return do_part_info(argc, argv, CMD_PART_INFO_START);
168 }
169 
do_part_size(int argc,char * const argv[])170 static int do_part_size(int argc, char * const argv[])
171 {
172 	return do_part_info(argc, argv, CMD_PART_INFO_SIZE);
173 }
174 
do_part(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])175 static int do_part(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
176 {
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);
184 	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);
188 
189 	return CMD_RET_USAGE;
190 }
191 
192 U_BOOT_CMD(
193 	part,	CONFIG_SYS_MAXARGS,	1,	do_part,
194 	"disk partition related commands",
195 	"uuid <interface> <dev>:<part>\n"
196 	"    - 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"
200 	"    - print a device's partition table\n"
201 	"part list <interface> <dev> [flags] <varname>\n"
202 	"    - set environment variable to the list of partitions\n"
203 	"      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"
206 	"      part can be either partition number or partition name\n"
207 	"part size <interface> <dev> <part> <varname>\n"
208 	"    - set environment variable to the size of the partition (in blocks)\n"
209 	"      part can be either partition number or partition name"
210 );
211