Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stephen Warren | 5cf41dc | 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 | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <common.h> |
| 19 | #include <config.h> |
| 20 | #include <command.h> |
| 21 | #include <part.h> |
| 22 | #include <vsprintf.h> |
| 23 | |
Sam Protsenko | 64e6a49 | 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, |
| 27 | }; |
| 28 | |
Jeroen Hofstee | 0e350f8 | 2014-06-23 00:22:08 +0200 | [diff] [blame] | 29 | static int do_part_uuid(int argc, char * const argv[]) |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 30 | { |
| 31 | int part; |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 32 | struct blk_desc *dev_desc; |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 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 | |
Simon Glass | e35929e | 2016-02-29 15:25:44 -0700 | [diff] [blame] | 40 | part = blk_get_device_part_str(argv[0], argv[1], &dev_desc, &info, 0); |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 41 | if (part < 0) |
| 42 | return 1; |
| 43 | |
| 44 | if (argc > 2) |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 45 | env_set(argv[2], info.uuid); |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 46 | else |
| 47 | printf("%s\n", info.uuid); |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
Jeroen Hofstee | 0e350f8 | 2014-06-23 00:22:08 +0200 | [diff] [blame] | 52 | static int do_part_list(int argc, char * const argv[]) |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 53 | { |
| 54 | int ret; |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 55 | struct blk_desc *desc; |
Sjoerd Simons | 0798d6f | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 56 | char *var = NULL; |
| 57 | bool bootable = false; |
| 58 | int i; |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 59 | |
Sjoerd Simons | 0798d6f | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 60 | if (argc < 2) |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 61 | return CMD_RET_USAGE; |
| 62 | |
Sjoerd Simons | 0798d6f | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 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 | |
Simon Glass | ebac37c | 2016-02-29 15:25:43 -0700 | [diff] [blame] | 84 | ret = blk_get_device_by_str(argv[0], argv[1], &desc); |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 85 | if (ret < 0) |
| 86 | return 1; |
| 87 | |
Sjoerd Simons | 0798d6f | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 88 | if (var != NULL) { |
Sjoerd Simons | e86df6e | 2015-01-05 18:13:37 +0100 | [diff] [blame] | 89 | int p; |
Sjoerd Simons | 0798d6f | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 90 | char str[512] = { '\0', }; |
Paul Kocialkowski | a78f78e | 2015-06-15 21:35:04 +0200 | [diff] [blame] | 91 | disk_partition_t info; |
Sjoerd Simons | e86df6e | 2015-01-05 18:13:37 +0100 | [diff] [blame] | 92 | |
Kever Yang | 7e29511 | 2018-09-07 17:37:36 +0800 | [diff] [blame] | 93 | for (p = 1; p < MAX_SEARCH_PARTITIONS; p++) { |
Sjoerd Simons | 0798d6f | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 94 | char t[5]; |
Simon Glass | 3e8bd46 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 95 | int r = part_get_info(desc, p, &info); |
Sjoerd Simons | e86df6e | 2015-01-05 18:13:37 +0100 | [diff] [blame] | 96 | |
Sjoerd Simons | 0798d6f | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 97 | if (r != 0) |
| 98 | continue; |
| 99 | |
| 100 | if (bootable && !info.bootable) |
| 101 | continue; |
| 102 | |
Stephen Warren | 653447b | 2015-12-09 09:48:04 -0700 | [diff] [blame] | 103 | sprintf(t, "%s%x", str[0] ? " " : "", p); |
Sjoerd Simons | 0798d6f | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 104 | strcat(str, t); |
Sjoerd Simons | e86df6e | 2015-01-05 18:13:37 +0100 | [diff] [blame] | 105 | } |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 106 | env_set(var, str); |
Sjoerd Simons | e86df6e | 2015-01-05 18:13:37 +0100 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
Simon Glass | 3e8bd46 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 110 | part_print(desc); |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
Sam Protsenko | 64e6a49 | 2018-02-26 23:18:00 +0200 | [diff] [blame] | 115 | static int do_part_info(int argc, char * const argv[], enum cmd_part_info param) |
Paul Kocialkowski | 8607c4f | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 116 | { |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 117 | struct blk_desc *desc; |
Paul Kocialkowski | 8607c4f | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 118 | disk_partition_t info; |
| 119 | char buf[512] = { 0 }; |
Sam Protsenko | 36df616 | 2018-02-26 23:17:59 +0200 | [diff] [blame] | 120 | char *endp; |
Paul Kocialkowski | 8607c4f | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 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 | |
Simon Glass | ebac37c | 2016-02-29 15:25:43 -0700 | [diff] [blame] | 130 | ret = blk_get_device_by_str(argv[0], argv[1], &desc); |
Paul Kocialkowski | 8607c4f | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 131 | if (ret < 0) |
| 132 | return 1; |
| 133 | |
Sam Protsenko | 36df616 | 2018-02-26 23:17:59 +0200 | [diff] [blame] | 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 | } |
Paul Kocialkowski | 8607c4f | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 144 | |
Sam Protsenko | 64e6a49 | 2018-02-26 23:18:00 +0200 | [diff] [blame] | 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 | } |
Paul Kocialkowski | 8607c4f | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 156 | |
| 157 | if (argc > 3) |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 158 | env_set(argv[3], buf); |
Paul Kocialkowski | 8607c4f | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 159 | else |
| 160 | printf("%s\n", buf); |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
Sam Protsenko | 64e6a49 | 2018-02-26 23:18:00 +0200 | [diff] [blame] | 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 | |
Paul Kocialkowski | 8607c4f | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 170 | static int do_part_size(int argc, char * const argv[]) |
| 171 | { |
Sam Protsenko | 64e6a49 | 2018-02-26 23:18:00 +0200 | [diff] [blame] | 172 | return do_part_info(argc, argv, CMD_PART_INFO_SIZE); |
Paul Kocialkowski | 8607c4f | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 173 | } |
| 174 | |
Jeroen Hofstee | 0e350f8 | 2014-06-23 00:22:08 +0200 | [diff] [blame] | 175 | static int do_part(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 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); |
Paul Kocialkowski | 8607c4f | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 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); |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 188 | |
| 189 | return CMD_RET_USAGE; |
| 190 | } |
| 191 | |
| 192 | U_BOOT_CMD( |
Sjoerd Simons | 0798d6f | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 193 | part, CONFIG_SYS_MAXARGS, 1, do_part, |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 194 | "disk partition related commands", |
maxin.john@enea.com | 8ca584e | 2015-04-28 01:44:58 +0200 | [diff] [blame] | 195 | "uuid <interface> <dev>:<part>\n" |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 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" |
Sjoerd Simons | e86df6e | 2015-01-05 18:13:37 +0100 | [diff] [blame] | 200 | " - print a device's partition table\n" |
Sjoerd Simons | 0798d6f | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 201 | "part list <interface> <dev> [flags] <varname>\n" |
| 202 | " - set environment variable to the list of partitions\n" |
Paul Kocialkowski | 8607c4f | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 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" |
Sam Protsenko | 36df616 | 2018-02-26 23:17:59 +0200 | [diff] [blame] | 206 | " part can be either partition number or partition name\n" |
Paul Kocialkowski | 8607c4f | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 207 | "part size <interface> <dev> <part> <varname>\n" |
Sam Protsenko | 36df616 | 2018-02-26 23:17:59 +0200 | [diff] [blame] | 208 | " - set environment variable to the size of the partition (in blocks)\n" |
| 209 | " part can be either partition number or partition name" |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 210 | ); |