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 | |
| 18 | #include <common.h> |
| 19 | #include <config.h> |
| 20 | #include <command.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 21 | #include <env.h> |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 22 | #include <part.h> |
| 23 | #include <vsprintf.h> |
| 24 | |
Sam Protsenko | 92bfb86 | 2018-02-26 23:18:00 +0200 | [diff] [blame] | 25 | enum cmd_part_info { |
| 26 | CMD_PART_INFO_START = 0, |
| 27 | CMD_PART_INFO_SIZE, |
Ruslan Trofymenko | db9bd636 | 2019-06-14 17:01:26 +0300 | [diff] [blame] | 28 | CMD_PART_INFO_NUMBER |
Sam Protsenko | 92bfb86 | 2018-02-26 23:18:00 +0200 | [diff] [blame] | 29 | }; |
| 30 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 31 | static int do_part_uuid(int argc, char *const argv[]) |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 32 | { |
| 33 | int part; |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 34 | struct blk_desc *dev_desc; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 35 | struct disk_partition info; |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 36 | |
| 37 | if (argc < 2) |
| 38 | return CMD_RET_USAGE; |
| 39 | if (argc > 3) |
| 40 | return CMD_RET_USAGE; |
| 41 | |
Simon Glass | e76ee97 | 2016-02-29 15:25:44 -0700 | [diff] [blame] | 42 | 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] | 43 | if (part < 0) |
| 44 | return 1; |
| 45 | |
| 46 | if (argc > 2) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 47 | env_set(argv[2], info.uuid); |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 48 | else |
| 49 | printf("%s\n", info.uuid); |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 54 | static int do_part_list(int argc, char *const argv[]) |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 55 | { |
| 56 | int ret; |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 57 | struct blk_desc *desc; |
Sjoerd Simons | 43cdb5d | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 58 | char *var = NULL; |
| 59 | bool bootable = false; |
| 60 | int i; |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 61 | |
Sjoerd Simons | 43cdb5d | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 62 | if (argc < 2) |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 63 | return CMD_RET_USAGE; |
| 64 | |
Sjoerd Simons | 43cdb5d | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 65 | 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 Glass | e6649a6 | 2016-02-29 15:25:43 -0700 | [diff] [blame] | 86 | ret = blk_get_device_by_str(argv[0], argv[1], &desc); |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 87 | if (ret < 0) |
| 88 | return 1; |
| 89 | |
Sjoerd Simons | 43cdb5d | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 90 | if (var != NULL) { |
Sjoerd Simons | ca8982b | 2015-01-05 18:13:37 +0100 | [diff] [blame] | 91 | int p; |
Sjoerd Simons | 43cdb5d | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 92 | char str[512] = { '\0', }; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 93 | struct disk_partition info; |
Sjoerd Simons | ca8982b | 2015-01-05 18:13:37 +0100 | [diff] [blame] | 94 | |
Kever Yang | 8416da1 | 2018-09-07 17:37:36 +0800 | [diff] [blame] | 95 | for (p = 1; p < MAX_SEARCH_PARTITIONS; p++) { |
Sjoerd Simons | 43cdb5d | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 96 | char t[5]; |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 97 | int r = part_get_info(desc, p, &info); |
Sjoerd Simons | ca8982b | 2015-01-05 18:13:37 +0100 | [diff] [blame] | 98 | |
Sjoerd Simons | 43cdb5d | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 99 | if (r != 0) |
| 100 | continue; |
| 101 | |
| 102 | if (bootable && !info.bootable) |
| 103 | continue; |
| 104 | |
Stephen Warren | 78bb0c1 | 2015-12-09 09:48:04 -0700 | [diff] [blame] | 105 | sprintf(t, "%s%x", str[0] ? " " : "", p); |
Sjoerd Simons | 43cdb5d | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 106 | strcat(str, t); |
Sjoerd Simons | ca8982b | 2015-01-05 18:13:37 +0100 | [diff] [blame] | 107 | } |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 108 | env_set(var, str); |
Sjoerd Simons | ca8982b | 2015-01-05 18:13:37 +0100 | [diff] [blame] | 109 | return 0; |
| 110 | } |
| 111 | |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 112 | part_print(desc); |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 117 | 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] | 118 | { |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 119 | struct blk_desc *desc; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 120 | struct disk_partition info; |
Paul Kocialkowski | cf7b376 | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 121 | char buf[512] = { 0 }; |
Sam Protsenko | 515a863 | 2018-02-26 23:17:59 +0200 | [diff] [blame] | 122 | char *endp; |
Paul Kocialkowski | cf7b376 | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 123 | 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 Glass | e6649a6 | 2016-02-29 15:25:43 -0700 | [diff] [blame] | 132 | ret = blk_get_device_by_str(argv[0], argv[1], &desc); |
Paul Kocialkowski | cf7b376 | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 133 | if (ret < 0) |
| 134 | return 1; |
| 135 | |
Sam Protsenko | 515a863 | 2018-02-26 23:17:59 +0200 | [diff] [blame] | 136 | 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 Kocialkowski | cf7b376 | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 146 | |
Sam Protsenko | 92bfb86 | 2018-02-26 23:18:00 +0200 | [diff] [blame] | 147 | 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 Trofymenko | db9bd636 | 2019-06-14 17:01:26 +0300 | [diff] [blame] | 154 | case CMD_PART_INFO_NUMBER: |
Roman Stratiienko | 014a381 | 2019-09-20 15:59:18 +0200 | [diff] [blame] | 155 | snprintf(buf, sizeof(buf), "0x%x", part); |
Ruslan Trofymenko | db9bd636 | 2019-06-14 17:01:26 +0300 | [diff] [blame] | 156 | break; |
Sam Protsenko | 92bfb86 | 2018-02-26 23:18:00 +0200 | [diff] [blame] | 157 | default: |
| 158 | printf("** Unknown cmd_part_info value: %d\n", param); |
| 159 | return 1; |
| 160 | } |
Paul Kocialkowski | cf7b376 | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 161 | |
| 162 | if (argc > 3) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 163 | env_set(argv[3], buf); |
Paul Kocialkowski | cf7b376 | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 164 | else |
| 165 | printf("%s\n", buf); |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 170 | static int do_part_start(int argc, char *const argv[]) |
Paul Kocialkowski | cf7b376 | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 171 | { |
Sam Protsenko | 92bfb86 | 2018-02-26 23:18:00 +0200 | [diff] [blame] | 172 | return do_part_info(argc, argv, CMD_PART_INFO_START); |
| 173 | } |
Paul Kocialkowski | cf7b376 | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 174 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 175 | static int do_part_size(int argc, char *const argv[]) |
Sam Protsenko | 92bfb86 | 2018-02-26 23:18:00 +0200 | [diff] [blame] | 176 | { |
| 177 | return do_part_info(argc, argv, CMD_PART_INFO_SIZE); |
Paul Kocialkowski | cf7b376 | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 180 | static int do_part_number(int argc, char *const argv[]) |
Ruslan Trofymenko | db9bd636 | 2019-06-14 17:01:26 +0300 | [diff] [blame] | 181 | { |
| 182 | return do_part_info(argc, argv, CMD_PART_INFO_NUMBER); |
| 183 | } |
| 184 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 185 | static int do_part(struct cmd_tbl *cmdtp, int flag, int argc, |
| 186 | char *const argv[]) |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 187 | { |
| 188 | if (argc < 2) |
| 189 | return CMD_RET_USAGE; |
| 190 | |
| 191 | if (!strcmp(argv[1], "uuid")) |
| 192 | return do_part_uuid(argc - 2, argv + 2); |
| 193 | else if (!strcmp(argv[1], "list")) |
| 194 | return do_part_list(argc - 2, argv + 2); |
Paul Kocialkowski | cf7b376 | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 195 | else if (!strcmp(argv[1], "start")) |
| 196 | return do_part_start(argc - 2, argv + 2); |
| 197 | else if (!strcmp(argv[1], "size")) |
| 198 | return do_part_size(argc - 2, argv + 2); |
Ruslan Trofymenko | db9bd636 | 2019-06-14 17:01:26 +0300 | [diff] [blame] | 199 | else if (!strcmp(argv[1], "number")) |
| 200 | return do_part_number(argc - 2, argv + 2); |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 201 | |
| 202 | return CMD_RET_USAGE; |
| 203 | } |
| 204 | |
| 205 | U_BOOT_CMD( |
Sjoerd Simons | 43cdb5d | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 206 | part, CONFIG_SYS_MAXARGS, 1, do_part, |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 207 | "disk partition related commands", |
maxin.john@enea.com | 9bc711d | 2015-04-28 01:44:58 +0200 | [diff] [blame] | 208 | "uuid <interface> <dev>:<part>\n" |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 209 | " - print partition UUID\n" |
| 210 | "part uuid <interface> <dev>:<part> <varname>\n" |
| 211 | " - set environment variable to partition UUID\n" |
| 212 | "part list <interface> <dev>\n" |
Sjoerd Simons | ca8982b | 2015-01-05 18:13:37 +0100 | [diff] [blame] | 213 | " - print a device's partition table\n" |
Sjoerd Simons | 43cdb5d | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 214 | "part list <interface> <dev> [flags] <varname>\n" |
| 215 | " - set environment variable to the list of partitions\n" |
Paul Kocialkowski | cf7b376 | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 216 | " flags can be -bootable (list only bootable partitions)\n" |
| 217 | "part start <interface> <dev> <part> <varname>\n" |
| 218 | " - set environment variable to the start of the partition (in blocks)\n" |
Sam Protsenko | 515a863 | 2018-02-26 23:17:59 +0200 | [diff] [blame] | 219 | " part can be either partition number or partition name\n" |
Paul Kocialkowski | cf7b376 | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 220 | "part size <interface> <dev> <part> <varname>\n" |
Sam Protsenko | 515a863 | 2018-02-26 23:17:59 +0200 | [diff] [blame] | 221 | " - set environment variable to the size of the partition (in blocks)\n" |
Ruslan Trofymenko | db9bd636 | 2019-06-14 17:01:26 +0300 | [diff] [blame] | 222 | " part can be either partition number or partition name\n" |
| 223 | "part number <interface> <dev> <part> <varname>\n" |
| 224 | " - set environment variable to the partition number using the partition name\n" |
| 225 | " part must be specified as partition name" |
Stephen Warren | 3a52910 | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 226 | ); |