Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved. |
| 3 | * |
| 4 | * made from cmd_ext2, which was: |
| 5 | * |
| 6 | * (C) Copyright 2004 |
| 7 | * esd gmbh <www.esd-electronics.com> |
| 8 | * Reinhard Arlt <reinhard.arlt@esd-electronics.com> |
| 9 | * |
| 10 | * made from cmd_reiserfs by |
| 11 | * |
| 12 | * (C) Copyright 2003 - 2004 |
| 13 | * Sysgo Real-Time Solutions, AG <www.elinos.com> |
| 14 | * Pavel Bartusek <pba@sysgo.com> |
| 15 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 16 | * SPDX-License-Identifier: GPL-2.0+ |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <common.h> |
| 20 | #include <config.h> |
| 21 | #include <command.h> |
| 22 | #include <part.h> |
| 23 | #include <vsprintf.h> |
| 24 | |
Jeroen Hofstee | 0e350f8 | 2014-06-23 00:22:08 +0200 | [diff] [blame] | 25 | static int do_part_uuid(int argc, char * const argv[]) |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 26 | { |
| 27 | int part; |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 28 | struct blk_desc *dev_desc; |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 29 | disk_partition_t info; |
| 30 | |
| 31 | if (argc < 2) |
| 32 | return CMD_RET_USAGE; |
| 33 | if (argc > 3) |
| 34 | return CMD_RET_USAGE; |
| 35 | |
Simon Glass | e35929e | 2016-02-29 15:25:44 -0700 | [diff] [blame] | 36 | 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] | 37 | if (part < 0) |
| 38 | return 1; |
| 39 | |
| 40 | if (argc > 2) |
| 41 | setenv(argv[2], info.uuid); |
| 42 | else |
| 43 | printf("%s\n", info.uuid); |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
Jeroen Hofstee | 0e350f8 | 2014-06-23 00:22:08 +0200 | [diff] [blame] | 48 | static int do_part_list(int argc, char * const argv[]) |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 49 | { |
| 50 | int ret; |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 51 | struct blk_desc *desc; |
Sjoerd Simons | 0798d6f | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 52 | char *var = NULL; |
| 53 | bool bootable = false; |
| 54 | int i; |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 55 | |
Sjoerd Simons | 0798d6f | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 56 | if (argc < 2) |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 57 | return CMD_RET_USAGE; |
| 58 | |
Sjoerd Simons | 0798d6f | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 59 | if (argc > 2) { |
| 60 | for (i = 2; i < argc ; i++) { |
| 61 | if (argv[i][0] == '-') { |
| 62 | if (!strcmp(argv[i], "-bootable")) { |
| 63 | bootable = true; |
| 64 | } else { |
| 65 | printf("Unknown option %s\n", argv[i]); |
| 66 | return CMD_RET_USAGE; |
| 67 | } |
| 68 | } else { |
| 69 | var = argv[i]; |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /* Loops should have been exited at the last argument, which |
| 75 | * as it contained the variable */ |
| 76 | if (argc != i + 1) |
| 77 | return CMD_RET_USAGE; |
| 78 | } |
| 79 | |
Simon Glass | ebac37c | 2016-02-29 15:25:43 -0700 | [diff] [blame] | 80 | ret = blk_get_device_by_str(argv[0], argv[1], &desc); |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 81 | if (ret < 0) |
| 82 | return 1; |
| 83 | |
Sjoerd Simons | 0798d6f | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 84 | if (var != NULL) { |
Sjoerd Simons | e86df6e | 2015-01-05 18:13:37 +0100 | [diff] [blame] | 85 | int p; |
Sjoerd Simons | 0798d6f | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 86 | char str[512] = { '\0', }; |
Paul Kocialkowski | a78f78e | 2015-06-15 21:35:04 +0200 | [diff] [blame] | 87 | disk_partition_t info; |
Sjoerd Simons | e86df6e | 2015-01-05 18:13:37 +0100 | [diff] [blame] | 88 | |
| 89 | for (p = 1; p < 128; p++) { |
Sjoerd Simons | 0798d6f | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 90 | char t[5]; |
Simon Glass | 3e8bd46 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 91 | int r = part_get_info(desc, p, &info); |
Sjoerd Simons | e86df6e | 2015-01-05 18:13:37 +0100 | [diff] [blame] | 92 | |
Sjoerd Simons | 0798d6f | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 93 | if (r != 0) |
| 94 | continue; |
| 95 | |
| 96 | if (bootable && !info.bootable) |
| 97 | continue; |
| 98 | |
Stephen Warren | 653447b | 2015-12-09 09:48:04 -0700 | [diff] [blame] | 99 | sprintf(t, "%s%x", str[0] ? " " : "", p); |
Sjoerd Simons | 0798d6f | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 100 | strcat(str, t); |
Sjoerd Simons | e86df6e | 2015-01-05 18:13:37 +0100 | [diff] [blame] | 101 | } |
Sjoerd Simons | 0798d6f | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 102 | setenv(var, str); |
Sjoerd Simons | e86df6e | 2015-01-05 18:13:37 +0100 | [diff] [blame] | 103 | return 0; |
| 104 | } |
| 105 | |
Simon Glass | 3e8bd46 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 106 | part_print(desc); |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
Paul Kocialkowski | 8607c4f | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 111 | static int do_part_start(int argc, char * const argv[]) |
| 112 | { |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 113 | struct blk_desc *desc; |
Paul Kocialkowski | 8607c4f | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 114 | disk_partition_t info; |
| 115 | char buf[512] = { 0 }; |
| 116 | int part; |
| 117 | int err; |
| 118 | int ret; |
| 119 | |
| 120 | if (argc < 3) |
| 121 | return CMD_RET_USAGE; |
| 122 | if (argc > 4) |
| 123 | return CMD_RET_USAGE; |
| 124 | |
| 125 | part = simple_strtoul(argv[2], NULL, 0); |
| 126 | |
Simon Glass | ebac37c | 2016-02-29 15:25:43 -0700 | [diff] [blame] | 127 | ret = blk_get_device_by_str(argv[0], argv[1], &desc); |
Paul Kocialkowski | 8607c4f | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 128 | if (ret < 0) |
| 129 | return 1; |
| 130 | |
Simon Glass | 3e8bd46 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 131 | err = part_get_info(desc, part, &info); |
Paul Kocialkowski | 8607c4f | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 132 | if (err) |
| 133 | return 1; |
| 134 | |
| 135 | snprintf(buf, sizeof(buf), LBAF, info.start); |
| 136 | |
| 137 | if (argc > 3) |
| 138 | setenv(argv[3], buf); |
| 139 | else |
| 140 | printf("%s\n", buf); |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int do_part_size(int argc, char * const argv[]) |
| 146 | { |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 147 | struct blk_desc *desc; |
Paul Kocialkowski | 8607c4f | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 148 | disk_partition_t info; |
| 149 | char buf[512] = { 0 }; |
| 150 | int part; |
| 151 | int err; |
| 152 | int ret; |
| 153 | |
| 154 | if (argc < 3) |
| 155 | return CMD_RET_USAGE; |
| 156 | if (argc > 4) |
| 157 | return CMD_RET_USAGE; |
| 158 | |
| 159 | part = simple_strtoul(argv[2], NULL, 0); |
| 160 | |
Simon Glass | ebac37c | 2016-02-29 15:25:43 -0700 | [diff] [blame] | 161 | ret = blk_get_device_by_str(argv[0], argv[1], &desc); |
Paul Kocialkowski | 8607c4f | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 162 | if (ret < 0) |
| 163 | return 1; |
| 164 | |
Simon Glass | 3e8bd46 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 165 | err = part_get_info(desc, part, &info); |
Paul Kocialkowski | 8607c4f | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 166 | if (err) |
| 167 | return 1; |
| 168 | |
| 169 | snprintf(buf, sizeof(buf), LBAF, info.size); |
| 170 | |
| 171 | if (argc > 3) |
| 172 | setenv(argv[3], buf); |
| 173 | else |
| 174 | printf("%s\n", buf); |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
Jeroen Hofstee | 0e350f8 | 2014-06-23 00:22:08 +0200 | [diff] [blame] | 179 | 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] | 180 | { |
| 181 | if (argc < 2) |
| 182 | return CMD_RET_USAGE; |
| 183 | |
| 184 | if (!strcmp(argv[1], "uuid")) |
| 185 | return do_part_uuid(argc - 2, argv + 2); |
| 186 | else if (!strcmp(argv[1], "list")) |
| 187 | return do_part_list(argc - 2, argv + 2); |
Paul Kocialkowski | 8607c4f | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 188 | else if (!strcmp(argv[1], "start")) |
| 189 | return do_part_start(argc - 2, argv + 2); |
| 190 | else if (!strcmp(argv[1], "size")) |
| 191 | return do_part_size(argc - 2, argv + 2); |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 192 | |
| 193 | return CMD_RET_USAGE; |
| 194 | } |
| 195 | |
| 196 | U_BOOT_CMD( |
Sjoerd Simons | 0798d6f | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 197 | part, CONFIG_SYS_MAXARGS, 1, do_part, |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 198 | "disk partition related commands", |
maxin.john@enea.com | 8ca584e | 2015-04-28 01:44:58 +0200 | [diff] [blame] | 199 | "uuid <interface> <dev>:<part>\n" |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 200 | " - print partition UUID\n" |
| 201 | "part uuid <interface> <dev>:<part> <varname>\n" |
| 202 | " - set environment variable to partition UUID\n" |
| 203 | "part list <interface> <dev>\n" |
Sjoerd Simons | e86df6e | 2015-01-05 18:13:37 +0100 | [diff] [blame] | 204 | " - print a device's partition table\n" |
Sjoerd Simons | 0798d6f | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 205 | "part list <interface> <dev> [flags] <varname>\n" |
| 206 | " - set environment variable to the list of partitions\n" |
Paul Kocialkowski | 8607c4f | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 207 | " flags can be -bootable (list only bootable partitions)\n" |
| 208 | "part start <interface> <dev> <part> <varname>\n" |
| 209 | " - set environment variable to the start of the partition (in blocks)\n" |
| 210 | "part size <interface> <dev> <part> <varname>\n" |
| 211 | " - set environment variable to the size of the partition (in blocks)" |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 212 | ); |