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 | |
| 25 | #ifndef CONFIG_PARTITION_UUIDS |
| 26 | #error CONFIG_PARTITION_UUIDS must be enabled for CONFIG_CMD_PART to be enabled |
| 27 | #endif |
| 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; |
| 32 | block_dev_desc_t *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 = get_device_and_partition(argv[0], argv[1], &dev_desc, &info, 0); |
| 41 | if (part < 0) |
| 42 | return 1; |
| 43 | |
| 44 | if (argc > 2) |
| 45 | setenv(argv[2], info.uuid); |
| 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; |
| 55 | block_dev_desc_t *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 | |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 84 | ret = get_device(argv[0], argv[1], &desc); |
| 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 | |
| 93 | for (p = 1; p < 128; p++) { |
Sjoerd Simons | 0798d6f | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 94 | char t[5]; |
Sjoerd Simons | e86df6e | 2015-01-05 18:13:37 +0100 | [diff] [blame] | 95 | int r = get_partition_info(desc, p, &info); |
| 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 | } |
Sjoerd Simons | 0798d6f | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 106 | setenv(var, str); |
Sjoerd Simons | e86df6e | 2015-01-05 18:13:37 +0100 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 110 | print_part(desc); |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
Paul Kocialkowski | 8607c4f | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 115 | static int do_part_start(int argc, char * const argv[]) |
| 116 | { |
| 117 | block_dev_desc_t *desc; |
| 118 | disk_partition_t info; |
| 119 | char buf[512] = { 0 }; |
| 120 | int part; |
| 121 | int err; |
| 122 | int ret; |
| 123 | |
| 124 | if (argc < 3) |
| 125 | return CMD_RET_USAGE; |
| 126 | if (argc > 4) |
| 127 | return CMD_RET_USAGE; |
| 128 | |
| 129 | part = simple_strtoul(argv[2], NULL, 0); |
| 130 | |
| 131 | ret = get_device(argv[0], argv[1], &desc); |
| 132 | if (ret < 0) |
| 133 | return 1; |
| 134 | |
| 135 | err = get_partition_info(desc, part, &info); |
| 136 | if (err) |
| 137 | return 1; |
| 138 | |
| 139 | snprintf(buf, sizeof(buf), LBAF, info.start); |
| 140 | |
| 141 | if (argc > 3) |
| 142 | setenv(argv[3], buf); |
| 143 | else |
| 144 | printf("%s\n", buf); |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | static int do_part_size(int argc, char * const argv[]) |
| 150 | { |
| 151 | block_dev_desc_t *desc; |
| 152 | disk_partition_t info; |
| 153 | char buf[512] = { 0 }; |
| 154 | int part; |
| 155 | int err; |
| 156 | int ret; |
| 157 | |
| 158 | if (argc < 3) |
| 159 | return CMD_RET_USAGE; |
| 160 | if (argc > 4) |
| 161 | return CMD_RET_USAGE; |
| 162 | |
| 163 | part = simple_strtoul(argv[2], NULL, 0); |
| 164 | |
| 165 | ret = get_device(argv[0], argv[1], &desc); |
| 166 | if (ret < 0) |
| 167 | return 1; |
| 168 | |
| 169 | err = get_partition_info(desc, part, &info); |
| 170 | if (err) |
| 171 | return 1; |
| 172 | |
| 173 | snprintf(buf, sizeof(buf), LBAF, info.size); |
| 174 | |
| 175 | if (argc > 3) |
| 176 | setenv(argv[3], buf); |
| 177 | else |
| 178 | printf("%s\n", buf); |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
Jeroen Hofstee | 0e350f8 | 2014-06-23 00:22:08 +0200 | [diff] [blame] | 183 | 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] | 184 | { |
| 185 | if (argc < 2) |
| 186 | return CMD_RET_USAGE; |
| 187 | |
| 188 | if (!strcmp(argv[1], "uuid")) |
| 189 | return do_part_uuid(argc - 2, argv + 2); |
| 190 | else if (!strcmp(argv[1], "list")) |
| 191 | return do_part_list(argc - 2, argv + 2); |
Paul Kocialkowski | 8607c4f | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 192 | else if (!strcmp(argv[1], "start")) |
| 193 | return do_part_start(argc - 2, argv + 2); |
| 194 | else if (!strcmp(argv[1], "size")) |
| 195 | return do_part_size(argc - 2, argv + 2); |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 196 | |
| 197 | return CMD_RET_USAGE; |
| 198 | } |
| 199 | |
| 200 | U_BOOT_CMD( |
Sjoerd Simons | 0798d6f | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 201 | part, CONFIG_SYS_MAXARGS, 1, do_part, |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 202 | "disk partition related commands", |
maxin.john@enea.com | 8ca584e | 2015-04-28 01:44:58 +0200 | [diff] [blame] | 203 | "uuid <interface> <dev>:<part>\n" |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 204 | " - print partition UUID\n" |
| 205 | "part uuid <interface> <dev>:<part> <varname>\n" |
| 206 | " - set environment variable to partition UUID\n" |
| 207 | "part list <interface> <dev>\n" |
Sjoerd Simons | e86df6e | 2015-01-05 18:13:37 +0100 | [diff] [blame] | 208 | " - print a device's partition table\n" |
Sjoerd Simons | 0798d6f | 2015-02-25 23:23:50 +0100 | [diff] [blame] | 209 | "part list <interface> <dev> [flags] <varname>\n" |
| 210 | " - set environment variable to the list of partitions\n" |
Paul Kocialkowski | 8607c4f | 2015-06-15 21:35:05 +0200 | [diff] [blame] | 211 | " flags can be -bootable (list only bootable partitions)\n" |
| 212 | "part start <interface> <dev> <part> <varname>\n" |
| 213 | " - set environment variable to the start of the partition (in blocks)\n" |
| 214 | "part size <interface> <dev> <part> <varname>\n" |
| 215 | " - set environment variable to the size of the partition (in blocks)" |
Stephen Warren | 5cf41dc | 2012-09-21 09:51:01 +0000 | [diff] [blame] | 216 | ); |