Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 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> |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 18 | #include <config.h> |
| 19 | #include <command.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 20 | #include <env.h> |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 21 | #include <part.h> |
| 22 | #include <vsprintf.h> |
| 23 | |
Sam Protsenko | 92bfb86 | 2018-02-26 23:18:00 +0200 | [diff] [blame] | 24 | enum cmd_part_info { |
| 25 | CMD_PART_INFO_START = 0, |
| 26 | CMD_PART_INFO_SIZE, |
Ruslan Trofymenko | db9bd636 | 2019-06-14 17:01:26 +0300 | [diff] [blame] | 27 | CMD_PART_INFO_NUMBER |
Sam Protsenko | 92bfb86 | 2018-02-26 23:18:00 +0200 | [diff] [blame] | 28 | }; |
| 29 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 30 | static int do_part_uuid(int argc, char *const argv[]) |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 31 | { |
| 32 | int part; |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 33 | struct blk_desc *dev_desc; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 34 | struct disk_partition info; |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 35 | |
| 36 | if (argc < 2) |
| 37 | return CMD_RET_USAGE; |
| 38 | if (argc > 3) |
| 39 | return CMD_RET_USAGE; |
| 40 | |
Simon Glass | e76ee97 | 2016-02-29 15:25:44 -0700 | [diff] [blame] | 41 | part = blk_get_device_part_str(argv[0], argv[1], &dev_desc, &info, 0); |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 42 | if (part < 0) |
| 43 | return 1; |
| 44 | |
| 45 | if (argc > 2) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 46 | env_set(argv[2], info.uuid); |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 47 | else |
| 48 | printf("%s\n", info.uuid); |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 53 | static int do_part_list(int argc, char *const argv[]) |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 54 | { |
| 55 | int ret; |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 56 | struct blk_desc *desc; |
Sjoerd Simons | 43cdb5d | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 57 | char *var = NULL; |
| 58 | bool bootable = false; |
| 59 | int i; |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 60 | |
Sjoerd Simons | 43cdb5d | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 61 | if (argc < 2) |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 62 | return CMD_RET_USAGE; |
| 63 | |
Sjoerd Simons | 43cdb5d | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 64 | if (argc > 2) { |
| 65 | for (i = 2; i < argc ; i++) { |
| 66 | if (argv[i][0] == '-') { |
| 67 | if (!strcmp(argv[i], "-bootable")) { |
| 68 | bootable = true; |
| 69 | } else { |
| 70 | printf("Unknown option %s\n", argv[i]); |
| 71 | return CMD_RET_USAGE; |
| 72 | } |
| 73 | } else { |
| 74 | var = argv[i]; |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /* Loops should have been exited at the last argument, which |
| 80 | * as it contained the variable */ |
| 81 | if (argc != i + 1) |
| 82 | return CMD_RET_USAGE; |
| 83 | } |
| 84 | |
Simon Glass | e6649a6 | 2016-02-29 15:25:43 -0700 | [diff] [blame] | 85 | ret = blk_get_device_by_str(argv[0], argv[1], &desc); |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 86 | if (ret < 0) |
| 87 | return 1; |
| 88 | |
Sjoerd Simons | 43cdb5d | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 89 | if (var != NULL) { |
Sjoerd Simons | ca8982b | 2015-01-05 18:13:37 +0100 | [diff] [blame] | 90 | int p; |
Heinrich Schuchardt | 72d7883 | 2022-01-11 16:03:38 +0100 | [diff] [blame] | 91 | char str[3 * MAX_SEARCH_PARTITIONS] = { '\0', }; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 92 | struct disk_partition info; |
Sjoerd Simons | ca8982b | 2015-01-05 18:13:37 +0100 | [diff] [blame] | 93 | |
Heinrich Schuchardt | 72d7883 | 2022-01-11 16:03:38 +0100 | [diff] [blame] | 94 | for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) { |
Sjoerd Simons | 43cdb5d | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 95 | char t[5]; |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 96 | int r = part_get_info(desc, p, &info); |
Sjoerd Simons | ca8982b | 2015-01-05 18:13:37 +0100 | [diff] [blame] | 97 | |
Sjoerd Simons | 43cdb5d | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 98 | if (r != 0) |
| 99 | continue; |
| 100 | |
| 101 | if (bootable && !info.bootable) |
| 102 | continue; |
| 103 | |
Stephen Warren | 78bb0c1 | 2015-12-09 09:48:04 -0700 | [diff] [blame] | 104 | sprintf(t, "%s%x", str[0] ? " " : "", p); |
Sjoerd Simons | 43cdb5d | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 105 | strcat(str, t); |
Sjoerd Simons | ca8982b | 2015-01-05 18:13:37 +0100 | [diff] [blame] | 106 | } |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 107 | env_set(var, str); |
Sjoerd Simons | ca8982b | 2015-01-05 18:13:37 +0100 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 111 | part_print(desc); |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 116 | static int do_part_info(int argc, char *const argv[], enum cmd_part_info param) |
Paul Kocialkowski | cf7b376 | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 117 | { |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 118 | struct blk_desc *desc; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 119 | struct disk_partition info; |
Paul Kocialkowski | cf7b376 | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 120 | char buf[512] = { 0 }; |
Sam Protsenko | 515a863 | 2018-02-26 23:17:59 +0200 | [diff] [blame] | 121 | char *endp; |
Paul Kocialkowski | cf7b376 | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 122 | int part; |
| 123 | int err; |
| 124 | int ret; |
| 125 | |
| 126 | if (argc < 3) |
| 127 | return CMD_RET_USAGE; |
| 128 | if (argc > 4) |
| 129 | return CMD_RET_USAGE; |
| 130 | |
Simon Glass | e6649a6 | 2016-02-29 15:25:43 -0700 | [diff] [blame] | 131 | ret = blk_get_device_by_str(argv[0], argv[1], &desc); |
Paul Kocialkowski | cf7b376 | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 132 | if (ret < 0) |
| 133 | return 1; |
| 134 | |
Sam Protsenko | 515a863 | 2018-02-26 23:17:59 +0200 | [diff] [blame] | 135 | part = simple_strtoul(argv[2], &endp, 0); |
| 136 | if (*endp == '\0') { |
| 137 | err = part_get_info(desc, part, &info); |
| 138 | if (err) |
| 139 | return 1; |
| 140 | } else { |
| 141 | part = part_get_info_by_name(desc, argv[2], &info); |
Anders Dellien | a7b261b | 2021-06-22 10:40:01 +0100 | [diff] [blame] | 142 | if (part < 0) |
Sam Protsenko | 515a863 | 2018-02-26 23:17:59 +0200 | [diff] [blame] | 143 | return 1; |
| 144 | } |
Paul Kocialkowski | cf7b376 | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 145 | |
Sam Protsenko | 92bfb86 | 2018-02-26 23:18:00 +0200 | [diff] [blame] | 146 | switch (param) { |
| 147 | case CMD_PART_INFO_START: |
| 148 | snprintf(buf, sizeof(buf), LBAF, info.start); |
| 149 | break; |
| 150 | case CMD_PART_INFO_SIZE: |
| 151 | snprintf(buf, sizeof(buf), LBAF, info.size); |
| 152 | break; |
Ruslan Trofymenko | db9bd636 | 2019-06-14 17:01:26 +0300 | [diff] [blame] | 153 | case CMD_PART_INFO_NUMBER: |
Roman Stratiienko | 014a381 | 2019-09-20 15:59:18 +0200 | [diff] [blame] | 154 | snprintf(buf, sizeof(buf), "0x%x", part); |
Ruslan Trofymenko | db9bd636 | 2019-06-14 17:01:26 +0300 | [diff] [blame] | 155 | break; |
Sam Protsenko | 92bfb86 | 2018-02-26 23:18:00 +0200 | [diff] [blame] | 156 | default: |
| 157 | printf("** Unknown cmd_part_info value: %d\n", param); |
| 158 | return 1; |
| 159 | } |
Paul Kocialkowski | cf7b376 | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 160 | |
| 161 | if (argc > 3) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 162 | env_set(argv[3], buf); |
Paul Kocialkowski | cf7b376 | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 163 | else |
| 164 | printf("%s\n", buf); |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 169 | static int do_part_start(int argc, char *const argv[]) |
Paul Kocialkowski | cf7b376 | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 170 | { |
Sam Protsenko | 92bfb86 | 2018-02-26 23:18:00 +0200 | [diff] [blame] | 171 | return do_part_info(argc, argv, CMD_PART_INFO_START); |
| 172 | } |
Paul Kocialkowski | cf7b376 | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 173 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 174 | static int do_part_size(int argc, char *const argv[]) |
Sam Protsenko | 92bfb86 | 2018-02-26 23:18:00 +0200 | [diff] [blame] | 175 | { |
| 176 | return do_part_info(argc, argv, CMD_PART_INFO_SIZE); |
Paul Kocialkowski | cf7b376 | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 179 | static int do_part_number(int argc, char *const argv[]) |
Ruslan Trofymenko | db9bd636 | 2019-06-14 17:01:26 +0300 | [diff] [blame] | 180 | { |
| 181 | return do_part_info(argc, argv, CMD_PART_INFO_NUMBER); |
| 182 | } |
| 183 | |
Simon Glass | ed338b7 | 2023-07-15 21:38:47 -0600 | [diff] [blame] | 184 | static int do_part_set(int argc, char *const argv[]) |
| 185 | { |
| 186 | const char *devname, *partstr, *typestr; |
| 187 | struct blk_desc *desc; |
| 188 | int dev; |
| 189 | |
| 190 | if (argc < 3) |
| 191 | return CMD_RET_USAGE; |
| 192 | |
| 193 | /* Look up the device */ |
| 194 | devname = argv[0]; |
| 195 | partstr = argv[1]; |
| 196 | typestr = argv[2]; |
| 197 | dev = blk_get_device_by_str(devname, partstr, &desc); |
| 198 | if (dev < 0) { |
| 199 | printf("** Bad device specification %s %s **\n", devname, |
| 200 | partstr); |
| 201 | return CMD_RET_FAILURE; |
| 202 | } |
| 203 | |
| 204 | desc->part_type = part_get_type_by_name(typestr); |
| 205 | if (!desc->part_type) { |
| 206 | printf("Unknown partition type '%s'\n", typestr); |
| 207 | return CMD_RET_FAILURE; |
| 208 | } |
| 209 | part_print(desc); |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
Enric Balletbo i Serra | 0359a09 | 2023-01-10 17:19:34 +0100 | [diff] [blame] | 214 | #ifdef CONFIG_PARTITION_TYPE_GUID |
| 215 | static int do_part_type(int argc, char *const argv[]) |
| 216 | { |
| 217 | int part; |
| 218 | struct blk_desc *dev_desc; |
| 219 | struct disk_partition info; |
| 220 | |
| 221 | if (argc < 2) |
| 222 | return CMD_RET_USAGE; |
| 223 | if (argc > 3) |
| 224 | return CMD_RET_USAGE; |
| 225 | |
| 226 | part = blk_get_device_part_str(argv[0], argv[1], &dev_desc, &info, 0); |
| 227 | if (part < 0) |
| 228 | return 1; |
| 229 | |
| 230 | if (argc > 2) |
| 231 | env_set(argv[2], info.type_guid); |
| 232 | else |
| 233 | printf("%s\n", info.type_guid); |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | #endif |
| 238 | |
Niel Fourie | 40102bc | 2020-03-24 16:17:03 +0100 | [diff] [blame] | 239 | static int do_part_types(int argc, char * const argv[]) |
| 240 | { |
| 241 | struct part_driver *drv = ll_entry_start(struct part_driver, |
| 242 | part_driver); |
| 243 | const int n_ents = ll_entry_count(struct part_driver, part_driver); |
| 244 | struct part_driver *entry; |
| 245 | int i = 0; |
| 246 | |
| 247 | puts("Supported partition tables"); |
| 248 | |
| 249 | for (entry = drv; entry != drv + n_ents; entry++) { |
| 250 | printf("%c %s", i ? ',' : ':', entry->name); |
| 251 | i++; |
| 252 | } |
| 253 | if (!i) |
| 254 | puts(": <none>"); |
| 255 | puts("\n"); |
| 256 | return CMD_RET_SUCCESS; |
| 257 | } |
| 258 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 259 | static int do_part(struct cmd_tbl *cmdtp, int flag, int argc, |
| 260 | char *const argv[]) |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 261 | { |
| 262 | if (argc < 2) |
| 263 | return CMD_RET_USAGE; |
| 264 | |
| 265 | if (!strcmp(argv[1], "uuid")) |
| 266 | return do_part_uuid(argc - 2, argv + 2); |
| 267 | else if (!strcmp(argv[1], "list")) |
| 268 | return do_part_list(argc - 2, argv + 2); |
Paul Kocialkowski | cf7b376 | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 269 | else if (!strcmp(argv[1], "start")) |
| 270 | return do_part_start(argc - 2, argv + 2); |
| 271 | else if (!strcmp(argv[1], "size")) |
| 272 | return do_part_size(argc - 2, argv + 2); |
Ruslan Trofymenko | db9bd636 | 2019-06-14 17:01:26 +0300 | [diff] [blame] | 273 | else if (!strcmp(argv[1], "number")) |
| 274 | return do_part_number(argc - 2, argv + 2); |
Niel Fourie | 40102bc | 2020-03-24 16:17:03 +0100 | [diff] [blame] | 275 | else if (!strcmp(argv[1], "types")) |
| 276 | return do_part_types(argc - 2, argv + 2); |
Simon Glass | ed338b7 | 2023-07-15 21:38:47 -0600 | [diff] [blame] | 277 | else if (!strcmp(argv[1], "set")) |
| 278 | return do_part_set(argc - 2, argv + 2); |
Enric Balletbo i Serra | 0359a09 | 2023-01-10 17:19:34 +0100 | [diff] [blame] | 279 | #ifdef CONFIG_PARTITION_TYPE_GUID |
| 280 | else if (!strcmp(argv[1], "type")) |
| 281 | return do_part_type(argc - 2, argv + 2); |
| 282 | #endif |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 283 | return CMD_RET_USAGE; |
| 284 | } |
| 285 | |
| 286 | U_BOOT_CMD( |
Sjoerd Simons | 43cdb5d | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 287 | part, CONFIG_SYS_MAXARGS, 1, do_part, |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 288 | "disk partition related commands", |
maxin.john@enea.com | 9bc711d | 2015-04-28 01:44:58 +0200 | [diff] [blame] | 289 | "uuid <interface> <dev>:<part>\n" |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 290 | " - print partition UUID\n" |
| 291 | "part uuid <interface> <dev>:<part> <varname>\n" |
| 292 | " - set environment variable to partition UUID\n" |
| 293 | "part list <interface> <dev>\n" |
Sjoerd Simons | ca8982b | 2015-01-05 18:13:37 +0100 | [diff] [blame] | 294 | " - print a device's partition table\n" |
Sjoerd Simons | 43cdb5d | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 295 | "part list <interface> <dev> [flags] <varname>\n" |
| 296 | " - set environment variable to the list of partitions\n" |
Paul Kocialkowski | cf7b376 | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 297 | " flags can be -bootable (list only bootable partitions)\n" |
| 298 | "part start <interface> <dev> <part> <varname>\n" |
| 299 | " - set environment variable to the start of the partition (in blocks)\n" |
Sam Protsenko | 515a863 | 2018-02-26 23:17:59 +0200 | [diff] [blame] | 300 | " part can be either partition number or partition name\n" |
Paul Kocialkowski | cf7b376 | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 301 | "part size <interface> <dev> <part> <varname>\n" |
Sam Protsenko | 515a863 | 2018-02-26 23:17:59 +0200 | [diff] [blame] | 302 | " - set environment variable to the size of the partition (in blocks)\n" |
Ruslan Trofymenko | db9bd636 | 2019-06-14 17:01:26 +0300 | [diff] [blame] | 303 | " part can be either partition number or partition name\n" |
| 304 | "part number <interface> <dev> <part> <varname>\n" |
| 305 | " - set environment variable to the partition number using the partition name\n" |
Niel Fourie | 40102bc | 2020-03-24 16:17:03 +0100 | [diff] [blame] | 306 | " part must be specified as partition name\n" |
Enric Balletbo i Serra | 0359a09 | 2023-01-10 17:19:34 +0100 | [diff] [blame] | 307 | #ifdef CONFIG_PARTITION_TYPE_GUID |
| 308 | "part type <interface> <dev>:<part>\n" |
| 309 | " - print partition type\n" |
Enric Balletbo i Serra | 0359a09 | 2023-01-10 17:19:34 +0100 | [diff] [blame] | 310 | "part type <interface> <dev>:<part> <varname>\n" |
| 311 | " - set environment variable to partition type\n" |
Marek Vasut | f5b0272 | 2023-12-10 22:25:07 +0100 | [diff] [blame] | 312 | #endif |
Simon Glass | ed338b7 | 2023-07-15 21:38:47 -0600 | [diff] [blame] | 313 | "part set <interface> <dev> type\n" |
| 314 | " - set partition type for a device\n" |
Niel Fourie | 40102bc | 2020-03-24 16:17:03 +0100 | [diff] [blame] | 315 | "part types\n" |
| 316 | " - list supported partition table types" |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 317 | ); |