Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 2 | /* |
| 3 | * cmd_gpt.c -- GPT (GUID Partition Table) handling command |
| 4 | * |
Lukasz Majewski | 309691b | 2015-11-20 08:06:17 +0100 | [diff] [blame] | 5 | * Copyright (C) 2015 |
| 6 | * Lukasz Majewski <l.majewski@majess.pl> |
| 7 | * |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 8 | * Copyright (C) 2012 Samsung Electronics |
| 9 | * author: Lukasz Majewski <l.majewski@samsung.com> |
| 10 | * author: Piotr Wilczek <p.wilczek@samsung.com> |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <common.h> |
| 14 | #include <malloc.h> |
| 15 | #include <command.h> |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 16 | #include <part_efi.h> |
| 17 | #include <exports.h> |
| 18 | #include <linux/ctype.h> |
Piotr Wilczek | 36c06b1 | 2013-01-27 22:59:25 +0000 | [diff] [blame] | 19 | #include <div64.h> |
Lukasz Majewski | 309691b | 2015-11-20 08:06:17 +0100 | [diff] [blame] | 20 | #include <memalign.h> |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 21 | #include <linux/compat.h> |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 22 | #include <linux/sizes.h> |
| 23 | #include <stdlib.h> |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 24 | |
| 25 | static LIST_HEAD(disk_partitions); |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 26 | |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 27 | /** |
| 28 | * extract_env(): Expand env name from string format '&{env_name}' |
| 29 | * and return pointer to the env (if the env is set) |
| 30 | * |
| 31 | * @param str - pointer to string |
| 32 | * @param env - pointer to pointer to extracted env |
| 33 | * |
| 34 | * @return - zero on successful expand and env is set |
| 35 | */ |
Przemyslaw Marczak | 5ec2ed2 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 36 | static int extract_env(const char *str, char **env) |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 37 | { |
Przemyslaw Marczak | 5ec2ed2 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 38 | int ret = -1; |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 39 | char *e, *s; |
Przemyslaw Marczak | 5ec2ed2 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 40 | #ifdef CONFIG_RANDOM_UUID |
| 41 | char uuid_str[UUID_STR_LEN + 1]; |
| 42 | #endif |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 43 | |
| 44 | if (!str || strlen(str) < 4) |
| 45 | return -1; |
| 46 | |
Przemyslaw Marczak | 5ec2ed2 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 47 | if (!((strncmp(str, "${", 2) == 0) && (str[strlen(str) - 1] == '}'))) |
| 48 | return -1; |
| 49 | |
| 50 | s = strdup(str); |
| 51 | if (s == NULL) |
| 52 | return -1; |
| 53 | |
| 54 | memset(s + strlen(s) - 1, '\0', 1); |
| 55 | memmove(s, s + 2, strlen(s) - 1); |
| 56 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 57 | e = env_get(s); |
Przemyslaw Marczak | 5ec2ed2 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 58 | if (e == NULL) { |
| 59 | #ifdef CONFIG_RANDOM_UUID |
| 60 | debug("%s unset. ", str); |
Vincent Tinelli | bdfacea | 2017-02-27 16:11:15 +0200 | [diff] [blame] | 61 | gen_rand_uuid_str(uuid_str, UUID_STR_FORMAT_GUID); |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 62 | env_set(s, uuid_str); |
Przemyslaw Marczak | 5ec2ed2 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 63 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 64 | e = env_get(s); |
Przemyslaw Marczak | 5ec2ed2 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 65 | if (e) { |
| 66 | debug("Set to random.\n"); |
| 67 | ret = 0; |
| 68 | } else { |
| 69 | debug("Can't get random UUID.\n"); |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 70 | } |
Przemyslaw Marczak | 5ec2ed2 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 71 | #else |
| 72 | debug("%s unset.\n", str); |
| 73 | #endif |
| 74 | } else { |
| 75 | debug("%s get from environment.\n", str); |
| 76 | ret = 0; |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Przemyslaw Marczak | 5ec2ed2 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 79 | *env = e; |
| 80 | free(s); |
| 81 | |
| 82 | return ret; |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
| 86 | * extract_val(): Extract value from a key=value pair list (comma separated). |
| 87 | * Only value for the given key is returend. |
| 88 | * Function allocates memory for the value, remember to free! |
| 89 | * |
| 90 | * @param str - pointer to string with key=values pairs |
| 91 | * @param key - pointer to the key to search for |
| 92 | * |
| 93 | * @return - pointer to allocated string with the value |
| 94 | */ |
| 95 | static char *extract_val(const char *str, const char *key) |
| 96 | { |
| 97 | char *v, *k; |
| 98 | char *s, *strcopy; |
| 99 | char *new = NULL; |
| 100 | |
| 101 | strcopy = strdup(str); |
| 102 | if (strcopy == NULL) |
| 103 | return NULL; |
| 104 | |
| 105 | s = strcopy; |
| 106 | while (s) { |
| 107 | v = strsep(&s, ","); |
| 108 | if (!v) |
| 109 | break; |
| 110 | k = strsep(&v, "="); |
| 111 | if (!k) |
| 112 | break; |
| 113 | if (strcmp(k, key) == 0) { |
| 114 | new = strdup(v); |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | free(strcopy); |
| 120 | |
| 121 | return new; |
| 122 | } |
| 123 | |
| 124 | /** |
Patrick Delaunay | 7840c4a | 2015-11-17 11:36:52 +0100 | [diff] [blame] | 125 | * found_key(): Found key without value in parameter list (comma separated). |
| 126 | * |
| 127 | * @param str - pointer to string with key |
| 128 | * @param key - pointer to the key to search for |
| 129 | * |
| 130 | * @return - true on found key |
| 131 | */ |
| 132 | static bool found_key(const char *str, const char *key) |
| 133 | { |
| 134 | char *k; |
| 135 | char *s, *strcopy; |
| 136 | bool result = false; |
| 137 | |
| 138 | strcopy = strdup(str); |
| 139 | if (!strcopy) |
| 140 | return NULL; |
| 141 | |
| 142 | s = strcopy; |
| 143 | while (s) { |
| 144 | k = strsep(&s, ","); |
| 145 | if (!k) |
| 146 | break; |
| 147 | if (strcmp(k, key) == 0) { |
| 148 | result = true; |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | free(strcopy); |
| 154 | |
| 155 | return result; |
| 156 | } |
| 157 | |
Alison Chaiken | 8dcee7c | 2017-07-04 11:19:46 -0700 | [diff] [blame] | 158 | static int calc_parts_list_len(int numparts) |
| 159 | { |
| 160 | int partlistlen = UUID_STR_LEN + 1 + strlen("uuid_disk="); |
| 161 | /* for the comma */ |
| 162 | partlistlen++; |
| 163 | |
| 164 | /* per-partition additions; numparts starts at 1, so this should be correct */ |
| 165 | partlistlen += numparts * (strlen("name=,") + PART_NAME_LEN + 1); |
| 166 | /* see part.h for definition of struct disk_partition */ |
| 167 | partlistlen += numparts * (strlen("start=MiB,") + sizeof(lbaint_t) + 1); |
| 168 | partlistlen += numparts * (strlen("size=MiB,") + sizeof(lbaint_t) + 1); |
| 169 | partlistlen += numparts * (strlen("uuid=;") + UUID_STR_LEN + 1); |
| 170 | /* for the terminating null */ |
| 171 | partlistlen++; |
| 172 | debug("Length of partitions_list is %d for %d partitions\n", partlistlen, |
| 173 | numparts); |
| 174 | return partlistlen; |
| 175 | } |
| 176 | |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 177 | #ifdef CONFIG_CMD_GPT_RENAME |
| 178 | static void del_gpt_info(void) |
| 179 | { |
| 180 | struct list_head *pos = &disk_partitions; |
| 181 | struct disk_part *curr; |
| 182 | while (!list_empty(pos)) { |
| 183 | curr = list_entry(pos->next, struct disk_part, list); |
| 184 | list_del(pos->next); |
| 185 | free(curr); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | static struct disk_part *allocate_disk_part(disk_partition_t *info, int partnum) |
| 190 | { |
| 191 | struct disk_part *newpart; |
Heinrich Schuchardt | 30725f5 | 2017-09-21 19:03:06 +0200 | [diff] [blame] | 192 | newpart = calloc(1, sizeof(struct disk_part)); |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 193 | if (!newpart) |
| 194 | return ERR_PTR(-ENOMEM); |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 195 | |
| 196 | newpart->gpt_part_info.start = info->start; |
| 197 | newpart->gpt_part_info.size = info->size; |
| 198 | newpart->gpt_part_info.blksz = info->blksz; |
| 199 | strncpy((char *)newpart->gpt_part_info.name, (const char *)info->name, |
| 200 | PART_NAME_LEN); |
| 201 | newpart->gpt_part_info.name[PART_NAME_LEN - 1] = '\0'; |
| 202 | strncpy((char *)newpart->gpt_part_info.type, (const char *)info->type, |
| 203 | PART_TYPE_LEN); |
| 204 | newpart->gpt_part_info.type[PART_TYPE_LEN - 1] = '\0'; |
| 205 | newpart->gpt_part_info.bootable = info->bootable; |
| 206 | #ifdef CONFIG_PARTITION_UUIDS |
| 207 | strncpy(newpart->gpt_part_info.uuid, (const char *)info->uuid, |
| 208 | UUID_STR_LEN); |
| 209 | /* UUID_STR_LEN is correct, as uuid[]'s length is UUID_STR_LEN+1 chars */ |
| 210 | newpart->gpt_part_info.uuid[UUID_STR_LEN] = '\0'; |
| 211 | #endif |
| 212 | newpart->partnum = partnum; |
| 213 | |
| 214 | return newpart; |
| 215 | } |
| 216 | |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 217 | static void prettyprint_part_size(char *sizestr, lbaint_t partsize, |
| 218 | lbaint_t blksize) |
| 219 | { |
| 220 | unsigned long long partbytes, partmegabytes; |
| 221 | |
| 222 | partbytes = partsize * blksize; |
| 223 | partmegabytes = lldiv(partbytes, SZ_1M); |
| 224 | snprintf(sizestr, 16, "%lluMiB", partmegabytes); |
| 225 | } |
| 226 | |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 227 | static void print_gpt_info(void) |
| 228 | { |
| 229 | struct list_head *pos; |
| 230 | struct disk_part *curr; |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 231 | char partstartstr[16]; |
| 232 | char partsizestr[16]; |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 233 | |
| 234 | list_for_each(pos, &disk_partitions) { |
| 235 | curr = list_entry(pos, struct disk_part, list); |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 236 | prettyprint_part_size(partstartstr, curr->gpt_part_info.start, |
| 237 | curr->gpt_part_info.blksz); |
| 238 | prettyprint_part_size(partsizestr, curr->gpt_part_info.size, |
| 239 | curr->gpt_part_info.blksz); |
| 240 | |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 241 | printf("Partition %d:\n", curr->partnum); |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 242 | printf("Start %s, size %s\n", partstartstr, partsizestr); |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 243 | printf("Block size %lu, name %s\n", curr->gpt_part_info.blksz, |
| 244 | curr->gpt_part_info.name); |
| 245 | printf("Type %s, bootable %d\n", curr->gpt_part_info.type, |
| 246 | curr->gpt_part_info.bootable); |
| 247 | #ifdef CONFIG_PARTITION_UUIDS |
| 248 | printf("UUID %s\n", curr->gpt_part_info.uuid); |
| 249 | #endif |
| 250 | printf("\n"); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /* |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 255 | * create the string that upstream 'gpt write' command will accept as an |
| 256 | * argument |
| 257 | * |
| 258 | * From doc/README.gpt, Format of partitions layout: |
| 259 | * "uuid_disk=...;name=u-boot,size=60MiB,uuid=...; |
| 260 | * name=kernel,size=60MiB,uuid=...;" |
| 261 | * The fields 'name' and 'size' are mandatory for every partition. |
| 262 | * The field 'start' is optional. The fields 'uuid' and 'uuid_disk' |
| 263 | * are optional if CONFIG_RANDOM_UUID is enabled. |
| 264 | */ |
| 265 | static int create_gpt_partitions_list(int numparts, const char *guid, |
| 266 | char *partitions_list) |
| 267 | { |
| 268 | struct list_head *pos; |
| 269 | struct disk_part *curr; |
| 270 | char partstr[PART_NAME_LEN + 1]; |
| 271 | |
| 272 | if (!partitions_list) |
| 273 | return -EINVAL; |
| 274 | |
| 275 | strcpy(partitions_list, "uuid_disk="); |
| 276 | strncat(partitions_list, guid, UUID_STR_LEN + 1); |
| 277 | strcat(partitions_list, ";"); |
| 278 | |
| 279 | list_for_each(pos, &disk_partitions) { |
| 280 | curr = list_entry(pos, struct disk_part, list); |
| 281 | strcat(partitions_list, "name="); |
| 282 | strncat(partitions_list, (const char *)curr->gpt_part_info.name, |
| 283 | PART_NAME_LEN + 1); |
Patrick Delaunay | 309a6fa | 2017-10-18 15:11:08 +0200 | [diff] [blame] | 284 | sprintf(partstr, ",start=0x%llx", |
| 285 | (unsigned long long)curr->gpt_part_info.start * |
| 286 | curr->gpt_part_info.blksz); |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 287 | /* one extra byte for NULL */ |
| 288 | strncat(partitions_list, partstr, PART_NAME_LEN + 1); |
Patrick Delaunay | 309a6fa | 2017-10-18 15:11:08 +0200 | [diff] [blame] | 289 | sprintf(partstr, ",size=0x%llx", |
| 290 | (unsigned long long)curr->gpt_part_info.size * |
| 291 | curr->gpt_part_info.blksz); |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 292 | strncat(partitions_list, partstr, PART_NAME_LEN + 1); |
| 293 | |
| 294 | strcat(partitions_list, ",uuid="); |
| 295 | strncat(partitions_list, curr->gpt_part_info.uuid, |
| 296 | UUID_STR_LEN + 1); |
| 297 | strcat(partitions_list, ";"); |
| 298 | } |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | /* |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 303 | * read partition info into disk_partitions list where |
| 304 | * it can be printed or modified |
| 305 | */ |
| 306 | static int get_gpt_info(struct blk_desc *dev_desc) |
| 307 | { |
| 308 | /* start partition numbering at 1, as U-Boot does */ |
| 309 | int valid_parts = 0, p, ret; |
| 310 | disk_partition_t info; |
| 311 | struct disk_part *new_disk_part; |
| 312 | |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 313 | /* |
| 314 | * Always re-read partition info from device, in case |
| 315 | * it has changed |
| 316 | */ |
| 317 | INIT_LIST_HEAD(&disk_partitions); |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 318 | |
| 319 | for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) { |
| 320 | ret = part_get_info(dev_desc, p, &info); |
| 321 | if (ret) |
| 322 | continue; |
| 323 | |
| 324 | /* Add 1 here because counter is zero-based but p1 is |
| 325 | the first partition */ |
| 326 | new_disk_part = allocate_disk_part(&info, valid_parts+1); |
| 327 | if (IS_ERR(new_disk_part)) |
| 328 | goto out; |
| 329 | |
| 330 | list_add_tail(&new_disk_part->list, &disk_partitions); |
| 331 | valid_parts++; |
| 332 | } |
| 333 | if (valid_parts == 0) { |
| 334 | printf("** No valid partitions found **\n"); |
| 335 | goto out; |
| 336 | } |
| 337 | return valid_parts; |
| 338 | out: |
| 339 | if (valid_parts >= 1) |
| 340 | del_gpt_info(); |
| 341 | return -ENODEV; |
| 342 | } |
| 343 | |
| 344 | /* a wrapper to test get_gpt_info */ |
| 345 | static int do_get_gpt_info(struct blk_desc *dev_desc) |
| 346 | { |
| 347 | int ret; |
| 348 | |
| 349 | ret = get_gpt_info(dev_desc); |
| 350 | if (ret > 0) { |
| 351 | print_gpt_info(); |
| 352 | del_gpt_info(); |
| 353 | return 0; |
| 354 | } |
| 355 | return ret; |
| 356 | } |
| 357 | #endif |
| 358 | |
Patrick Delaunay | 7840c4a | 2015-11-17 11:36:52 +0100 | [diff] [blame] | 359 | /** |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 360 | * set_gpt_info(): Fill partition information from string |
| 361 | * function allocates memory, remember to free! |
| 362 | * |
| 363 | * @param dev_desc - pointer block device descriptor |
| 364 | * @param str_part - pointer to string with partition information |
| 365 | * @param str_disk_guid - pointer to pointer to allocated string with disk guid |
| 366 | * @param partitions - pointer to pointer to allocated partitions array |
| 367 | * @param parts_count - number of partitions |
| 368 | * |
| 369 | * @return - zero on success, otherwise error |
| 370 | * |
| 371 | */ |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 372 | static int set_gpt_info(struct blk_desc *dev_desc, |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 373 | const char *str_part, |
| 374 | char **str_disk_guid, |
| 375 | disk_partition_t **partitions, |
| 376 | u8 *parts_count) |
| 377 | { |
| 378 | char *tok, *str, *s; |
| 379 | int i; |
| 380 | char *val, *p; |
| 381 | int p_count; |
| 382 | disk_partition_t *parts; |
| 383 | int errno = 0; |
Piotr Wilczek | 36c06b1 | 2013-01-27 22:59:25 +0000 | [diff] [blame] | 384 | uint64_t size_ll, start_ll; |
Michael Trimarchi | 853445f | 2016-06-08 10:18:16 +0200 | [diff] [blame] | 385 | lbaint_t offset = 0; |
Alison Chaiken | 8dcee7c | 2017-07-04 11:19:46 -0700 | [diff] [blame] | 386 | int max_str_part = calc_parts_list_len(MAX_SEARCH_PARTITIONS); |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 387 | |
Egbert Eich | bfd4a71 | 2013-10-04 18:53:04 +0200 | [diff] [blame] | 388 | debug("%s: lba num: 0x%x %d\n", __func__, |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 389 | (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba); |
| 390 | |
| 391 | if (str_part == NULL) |
| 392 | return -1; |
| 393 | |
| 394 | str = strdup(str_part); |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 395 | if (str == NULL) |
| 396 | return -ENOMEM; |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 397 | |
| 398 | /* extract disk guid */ |
| 399 | s = str; |
Rob Herring | d976197 | 2015-01-26 09:44:18 -0600 | [diff] [blame] | 400 | val = extract_val(str, "uuid_disk"); |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 401 | if (!val) { |
Rob Herring | d976197 | 2015-01-26 09:44:18 -0600 | [diff] [blame] | 402 | #ifdef CONFIG_RANDOM_UUID |
| 403 | *str_disk_guid = malloc(UUID_STR_LEN + 1); |
Tom Rini | a29829e | 2017-10-07 11:27:59 -0400 | [diff] [blame] | 404 | if (*str_disk_guid == NULL) |
Alison Chaiken | 8dcee7c | 2017-07-04 11:19:46 -0700 | [diff] [blame] | 405 | return -ENOMEM; |
Rob Herring | d976197 | 2015-01-26 09:44:18 -0600 | [diff] [blame] | 406 | gen_rand_uuid_str(*str_disk_guid, UUID_STR_FORMAT_STD); |
| 407 | #else |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 408 | free(str); |
| 409 | return -2; |
Rob Herring | d976197 | 2015-01-26 09:44:18 -0600 | [diff] [blame] | 410 | #endif |
| 411 | } else { |
| 412 | val = strsep(&val, ";"); |
| 413 | if (extract_env(val, &p)) |
| 414 | p = val; |
| 415 | *str_disk_guid = strdup(p); |
| 416 | free(val); |
| 417 | /* Move s to first partition */ |
| 418 | strsep(&s, ";"); |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 419 | } |
Alison Chaiken | 8dcee7c | 2017-07-04 11:19:46 -0700 | [diff] [blame] | 420 | if (s == NULL) { |
| 421 | printf("Error: is the partitions string NULL-terminated?\n"); |
| 422 | return -EINVAL; |
| 423 | } |
| 424 | if (strnlen(s, max_str_part) == 0) |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 425 | return -3; |
| 426 | |
Alison Chaiken | 8dcee7c | 2017-07-04 11:19:46 -0700 | [diff] [blame] | 427 | i = strnlen(s, max_str_part) - 1; |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 428 | if (s[i] == ';') |
| 429 | s[i] = '\0'; |
| 430 | |
| 431 | /* calculate expected number of partitions */ |
| 432 | p_count = 1; |
| 433 | p = s; |
| 434 | while (*p) { |
| 435 | if (*p++ == ';') |
| 436 | p_count++; |
| 437 | } |
| 438 | |
| 439 | /* allocate memory for partitions */ |
| 440 | parts = calloc(sizeof(disk_partition_t), p_count); |
Alison Chaiken | 8dcee7c | 2017-07-04 11:19:46 -0700 | [diff] [blame] | 441 | if (parts == NULL) |
| 442 | return -ENOMEM; |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 443 | |
Robert P. J. Day | e3f2c2e | 2013-08-21 11:39:19 -0400 | [diff] [blame] | 444 | /* retrieve partitions data from string */ |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 445 | for (i = 0; i < p_count; i++) { |
| 446 | tok = strsep(&s, ";"); |
| 447 | |
| 448 | if (tok == NULL) |
| 449 | break; |
| 450 | |
| 451 | /* uuid */ |
| 452 | val = extract_val(tok, "uuid"); |
Rob Herring | d976197 | 2015-01-26 09:44:18 -0600 | [diff] [blame] | 453 | if (!val) { |
| 454 | /* 'uuid' is optional if random uuid's are enabled */ |
| 455 | #ifdef CONFIG_RANDOM_UUID |
| 456 | gen_rand_uuid_str(parts[i].uuid, UUID_STR_FORMAT_STD); |
| 457 | #else |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 458 | errno = -4; |
| 459 | goto err; |
Rob Herring | d976197 | 2015-01-26 09:44:18 -0600 | [diff] [blame] | 460 | #endif |
| 461 | } else { |
| 462 | if (extract_env(val, &p)) |
| 463 | p = val; |
Alison Chaiken | 8dcee7c | 2017-07-04 11:19:46 -0700 | [diff] [blame] | 464 | if (strnlen(p, max_str_part) >= sizeof(parts[i].uuid)) { |
Rob Herring | d976197 | 2015-01-26 09:44:18 -0600 | [diff] [blame] | 465 | printf("Wrong uuid format for partition %d\n", i); |
| 466 | errno = -4; |
| 467 | goto err; |
| 468 | } |
Alison Chaiken | 8dcee7c | 2017-07-04 11:19:46 -0700 | [diff] [blame] | 469 | strncpy((char *)parts[i].uuid, p, max_str_part); |
Rob Herring | d976197 | 2015-01-26 09:44:18 -0600 | [diff] [blame] | 470 | free(val); |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 471 | } |
Patrick Delaunay | 8248c8d | 2015-10-27 11:00:27 +0100 | [diff] [blame] | 472 | #ifdef CONFIG_PARTITION_TYPE_GUID |
| 473 | /* guid */ |
| 474 | val = extract_val(tok, "type"); |
| 475 | if (val) { |
| 476 | /* 'type' is optional */ |
| 477 | if (extract_env(val, &p)) |
| 478 | p = val; |
Alison Chaiken | 8dcee7c | 2017-07-04 11:19:46 -0700 | [diff] [blame] | 479 | if (strnlen(p, max_str_part) >= sizeof(parts[i].type_guid)) { |
Patrick Delaunay | 8248c8d | 2015-10-27 11:00:27 +0100 | [diff] [blame] | 480 | printf("Wrong type guid format for partition %d\n", |
| 481 | i); |
| 482 | errno = -4; |
| 483 | goto err; |
| 484 | } |
Alison Chaiken | 8dcee7c | 2017-07-04 11:19:46 -0700 | [diff] [blame] | 485 | strncpy((char *)parts[i].type_guid, p, max_str_part); |
Patrick Delaunay | 8248c8d | 2015-10-27 11:00:27 +0100 | [diff] [blame] | 486 | free(val); |
| 487 | } |
| 488 | #endif |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 489 | /* name */ |
| 490 | val = extract_val(tok, "name"); |
| 491 | if (!val) { /* name is mandatory */ |
| 492 | errno = -4; |
| 493 | goto err; |
| 494 | } |
| 495 | if (extract_env(val, &p)) |
| 496 | p = val; |
Alison Chaiken | 8dcee7c | 2017-07-04 11:19:46 -0700 | [diff] [blame] | 497 | if (strnlen(p, max_str_part) >= sizeof(parts[i].name)) { |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 498 | errno = -4; |
| 499 | goto err; |
| 500 | } |
Alison Chaiken | 8dcee7c | 2017-07-04 11:19:46 -0700 | [diff] [blame] | 501 | strncpy((char *)parts[i].name, p, max_str_part); |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 502 | free(val); |
| 503 | |
| 504 | /* size */ |
| 505 | val = extract_val(tok, "size"); |
| 506 | if (!val) { /* 'size' is mandatory */ |
| 507 | errno = -4; |
| 508 | goto err; |
| 509 | } |
| 510 | if (extract_env(val, &p)) |
| 511 | p = val; |
Michael Trimarchi | 853445f | 2016-06-08 10:18:16 +0200 | [diff] [blame] | 512 | if ((strcmp(p, "-") == 0)) { |
Kever Yang | f1d070d | 2016-07-29 11:12:18 +0800 | [diff] [blame] | 513 | /* Let part efi module to auto extend the size */ |
| 514 | parts[i].size = 0; |
Michael Trimarchi | 853445f | 2016-06-08 10:18:16 +0200 | [diff] [blame] | 515 | } else { |
| 516 | size_ll = ustrtoull(p, &p, 0); |
| 517 | parts[i].size = lldiv(size_ll, dev_desc->blksz); |
| 518 | } |
| 519 | |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 520 | free(val); |
| 521 | |
| 522 | /* start address */ |
| 523 | val = extract_val(tok, "start"); |
| 524 | if (val) { /* start address is optional */ |
| 525 | if (extract_env(val, &p)) |
| 526 | p = val; |
Piotr Wilczek | 36c06b1 | 2013-01-27 22:59:25 +0000 | [diff] [blame] | 527 | start_ll = ustrtoull(p, &p, 0); |
| 528 | parts[i].start = lldiv(start_ll, dev_desc->blksz); |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 529 | free(val); |
| 530 | } |
Patrick Delaunay | 7840c4a | 2015-11-17 11:36:52 +0100 | [diff] [blame] | 531 | |
Michael Trimarchi | 853445f | 2016-06-08 10:18:16 +0200 | [diff] [blame] | 532 | offset += parts[i].size + parts[i].start; |
| 533 | |
Patrick Delaunay | 7840c4a | 2015-11-17 11:36:52 +0100 | [diff] [blame] | 534 | /* bootable */ |
| 535 | if (found_key(tok, "bootable")) |
| 536 | parts[i].bootable = 1; |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | *parts_count = p_count; |
| 540 | *partitions = parts; |
| 541 | free(str); |
| 542 | |
| 543 | return 0; |
| 544 | err: |
| 545 | free(str); |
| 546 | free(*str_disk_guid); |
| 547 | free(parts); |
| 548 | |
| 549 | return errno; |
| 550 | } |
| 551 | |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 552 | static int gpt_default(struct blk_desc *blk_dev_desc, const char *str_part) |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 553 | { |
| 554 | int ret; |
| 555 | char *str_disk_guid; |
| 556 | u8 part_count = 0; |
| 557 | disk_partition_t *partitions = NULL; |
| 558 | |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 559 | /* fill partitions */ |
Egbert Eich | bfd4a71 | 2013-10-04 18:53:04 +0200 | [diff] [blame] | 560 | ret = set_gpt_info(blk_dev_desc, str_part, |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 561 | &str_disk_guid, &partitions, &part_count); |
| 562 | if (ret) { |
| 563 | if (ret == -1) |
| 564 | printf("No partition list provided\n"); |
| 565 | if (ret == -2) |
| 566 | printf("Missing disk guid\n"); |
| 567 | if ((ret == -3) || (ret == -4)) |
| 568 | printf("Partition list incomplete\n"); |
| 569 | return -1; |
| 570 | } |
| 571 | |
| 572 | /* save partitions layout to disk */ |
Rob Herring | 008ab49 | 2015-01-26 09:43:15 -0600 | [diff] [blame] | 573 | ret = gpt_restore(blk_dev_desc, str_disk_guid, partitions, part_count); |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 574 | free(str_disk_guid); |
| 575 | free(partitions); |
| 576 | |
Lukasz Majewski | 309691b | 2015-11-20 08:06:17 +0100 | [diff] [blame] | 577 | return ret; |
| 578 | } |
| 579 | |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 580 | static int gpt_verify(struct blk_desc *blk_dev_desc, const char *str_part) |
Lukasz Majewski | 309691b | 2015-11-20 08:06:17 +0100 | [diff] [blame] | 581 | { |
| 582 | ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, |
| 583 | blk_dev_desc->blksz); |
| 584 | disk_partition_t *partitions = NULL; |
| 585 | gpt_entry *gpt_pte = NULL; |
| 586 | char *str_disk_guid; |
| 587 | u8 part_count = 0; |
| 588 | int ret = 0; |
| 589 | |
| 590 | /* fill partitions */ |
| 591 | ret = set_gpt_info(blk_dev_desc, str_part, |
| 592 | &str_disk_guid, &partitions, &part_count); |
| 593 | if (ret) { |
| 594 | if (ret == -1) { |
| 595 | printf("No partition list provided - only basic check\n"); |
| 596 | ret = gpt_verify_headers(blk_dev_desc, gpt_head, |
| 597 | &gpt_pte); |
| 598 | goto out; |
| 599 | } |
| 600 | if (ret == -2) |
| 601 | printf("Missing disk guid\n"); |
| 602 | if ((ret == -3) || (ret == -4)) |
| 603 | printf("Partition list incomplete\n"); |
| 604 | return -1; |
| 605 | } |
| 606 | |
| 607 | /* Check partition layout with provided pattern */ |
| 608 | ret = gpt_verify_partitions(blk_dev_desc, partitions, part_count, |
| 609 | gpt_head, &gpt_pte); |
| 610 | free(str_disk_guid); |
| 611 | free(partitions); |
| 612 | out: |
| 613 | free(gpt_pte); |
Rob Herring | 008ab49 | 2015-01-26 09:43:15 -0600 | [diff] [blame] | 614 | return ret; |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 615 | } |
| 616 | |
Alison Chaiken | e422258 | 2017-06-25 16:43:23 -0700 | [diff] [blame] | 617 | static int do_disk_guid(struct blk_desc *dev_desc, char * const namestr) |
| 618 | { |
| 619 | int ret; |
| 620 | char disk_guid[UUID_STR_LEN + 1]; |
| 621 | |
| 622 | ret = get_disk_guid(dev_desc, disk_guid); |
| 623 | if (ret < 0) |
| 624 | return CMD_RET_FAILURE; |
| 625 | |
| 626 | if (namestr) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 627 | env_set(namestr, disk_guid); |
Alison Chaiken | e422258 | 2017-06-25 16:43:23 -0700 | [diff] [blame] | 628 | else |
| 629 | printf("%s\n", disk_guid); |
| 630 | |
| 631 | return ret; |
| 632 | } |
| 633 | |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 634 | #ifdef CONFIG_CMD_GPT_RENAME |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 635 | /* |
| 636 | * There are 3 malloc() calls in set_gpt_info() and there is no info about which |
| 637 | * failed. |
| 638 | */ |
| 639 | static void set_gpt_cleanup(char **str_disk_guid, |
| 640 | disk_partition_t **partitions) |
| 641 | { |
| 642 | #ifdef CONFIG_RANDOM_UUID |
| 643 | if (str_disk_guid) |
| 644 | free(str_disk_guid); |
| 645 | #endif |
| 646 | if (partitions) |
| 647 | free(partitions); |
| 648 | } |
| 649 | |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 650 | static int do_rename_gpt_parts(struct blk_desc *dev_desc, char *subcomm, |
| 651 | char *name1, char *name2) |
| 652 | { |
| 653 | struct list_head *pos; |
| 654 | struct disk_part *curr; |
| 655 | disk_partition_t *new_partitions = NULL; |
| 656 | char disk_guid[UUID_STR_LEN + 1]; |
| 657 | char *partitions_list, *str_disk_guid; |
| 658 | u8 part_count = 0; |
| 659 | int partlistlen, ret, numparts = 0, partnum, i = 1, ctr1 = 0, ctr2 = 0; |
| 660 | |
| 661 | if ((subcomm == NULL) || (name1 == NULL) || (name2 == NULL) || |
| 662 | (strcmp(subcomm, "swap") && (strcmp(subcomm, "rename")))) |
| 663 | return -EINVAL; |
| 664 | |
| 665 | ret = get_disk_guid(dev_desc, disk_guid); |
| 666 | if (ret < 0) |
| 667 | return ret; |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 668 | /* |
| 669 | * Allocates disk_partitions, requiring matching call to del_gpt_info() |
| 670 | * if successful. |
| 671 | */ |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 672 | numparts = get_gpt_info(dev_desc); |
| 673 | if (numparts <= 0) |
| 674 | return numparts ? numparts : -ENODEV; |
| 675 | |
| 676 | partlistlen = calc_parts_list_len(numparts); |
| 677 | partitions_list = malloc(partlistlen); |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 678 | if (!partitions_list) { |
| 679 | del_gpt_info(); |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 680 | return -ENOMEM; |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 681 | } |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 682 | memset(partitions_list, '\0', partlistlen); |
| 683 | |
| 684 | ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list); |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 685 | if (ret < 0) { |
| 686 | free(partitions_list); |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 687 | return ret; |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 688 | } |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 689 | /* |
| 690 | * Uncomment the following line to print a string that 'gpt write' |
| 691 | * or 'gpt verify' will accept as input. |
| 692 | */ |
| 693 | debug("OLD partitions_list is %s with %u chars\n", partitions_list, |
| 694 | (unsigned)strlen(partitions_list)); |
| 695 | |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 696 | /* set_gpt_info allocates new_partitions and str_disk_guid */ |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 697 | ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid, |
| 698 | &new_partitions, &part_count); |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 699 | if (ret < 0) { |
| 700 | del_gpt_info(); |
| 701 | free(partitions_list); |
| 702 | if (ret == -ENOMEM) |
| 703 | set_gpt_cleanup(&str_disk_guid, &new_partitions); |
| 704 | else |
| 705 | goto out; |
| 706 | } |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 707 | |
| 708 | if (!strcmp(subcomm, "swap")) { |
| 709 | if ((strlen(name1) > PART_NAME_LEN) || (strlen(name2) > PART_NAME_LEN)) { |
| 710 | printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN); |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 711 | ret = -EINVAL; |
| 712 | goto out; |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 713 | } |
| 714 | list_for_each(pos, &disk_partitions) { |
| 715 | curr = list_entry(pos, struct disk_part, list); |
| 716 | if (!strcmp((char *)curr->gpt_part_info.name, name1)) { |
| 717 | strcpy((char *)curr->gpt_part_info.name, name2); |
| 718 | ctr1++; |
| 719 | } else if (!strcmp((char *)curr->gpt_part_info.name, name2)) { |
| 720 | strcpy((char *)curr->gpt_part_info.name, name1); |
| 721 | ctr2++; |
| 722 | } |
| 723 | } |
| 724 | if ((ctr1 + ctr2 < 2) || (ctr1 != ctr2)) { |
| 725 | printf("Cannot swap partition names except in pairs.\n"); |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 726 | ret = -EINVAL; |
| 727 | goto out; |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 728 | } |
| 729 | } else { /* rename */ |
| 730 | if (strlen(name2) > PART_NAME_LEN) { |
| 731 | printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN); |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 732 | ret = -EINVAL; |
| 733 | goto out; |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 734 | } |
| 735 | partnum = (int)simple_strtol(name1, NULL, 10); |
| 736 | if ((partnum < 0) || (partnum > numparts)) { |
| 737 | printf("Illegal partition number %s\n", name1); |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 738 | ret = -EINVAL; |
| 739 | goto out; |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 740 | } |
| 741 | ret = part_get_info(dev_desc, partnum, new_partitions); |
| 742 | if (ret < 0) |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 743 | goto out; |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 744 | |
| 745 | /* U-Boot partition numbering starts at 1 */ |
| 746 | list_for_each(pos, &disk_partitions) { |
| 747 | curr = list_entry(pos, struct disk_part, list); |
| 748 | if (i == partnum) { |
| 749 | strcpy((char *)curr->gpt_part_info.name, name2); |
| 750 | break; |
| 751 | } |
| 752 | i++; |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list); |
| 757 | if (ret < 0) |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 758 | goto out; |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 759 | debug("NEW partitions_list is %s with %u chars\n", partitions_list, |
| 760 | (unsigned)strlen(partitions_list)); |
| 761 | |
| 762 | ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid, |
| 763 | &new_partitions, &part_count); |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 764 | /* |
| 765 | * Even though valid pointers are here passed into set_gpt_info(), |
| 766 | * it mallocs again, and there's no way to tell which failed. |
| 767 | */ |
| 768 | if (ret < 0) { |
| 769 | del_gpt_info(); |
| 770 | free(partitions_list); |
| 771 | if (ret == -ENOMEM) |
| 772 | set_gpt_cleanup(&str_disk_guid, &new_partitions); |
| 773 | else |
| 774 | goto out; |
| 775 | } |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 776 | |
| 777 | debug("Writing new partition table\n"); |
| 778 | ret = gpt_restore(dev_desc, disk_guid, new_partitions, numparts); |
| 779 | if (ret < 0) { |
| 780 | printf("Writing new partition table failed\n"); |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 781 | goto out; |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | debug("Reading back new partition table\n"); |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 785 | /* |
| 786 | * Empty the existing disk_partitions list, as otherwise the memory in |
| 787 | * the original list is unreachable. |
| 788 | */ |
| 789 | del_gpt_info(); |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 790 | numparts = get_gpt_info(dev_desc); |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 791 | if (numparts <= 0) { |
| 792 | ret = numparts ? numparts : -ENODEV; |
| 793 | goto out; |
| 794 | } |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 795 | printf("new partition table with %d partitions is:\n", numparts); |
| 796 | print_gpt_info(); |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 797 | del_gpt_info(); |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 798 | out: |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 799 | free(new_partitions); |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 800 | free(str_disk_guid); |
| 801 | free(partitions_list); |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 802 | return ret; |
| 803 | } |
| 804 | #endif |
| 805 | |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 806 | /** |
| 807 | * do_gpt(): Perform GPT operations |
| 808 | * |
| 809 | * @param cmdtp - command name |
| 810 | * @param flag |
| 811 | * @param argc |
| 812 | * @param argv |
| 813 | * |
| 814 | * @return zero on success; otherwise error |
| 815 | */ |
| 816 | static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 817 | { |
| 818 | int ret = CMD_RET_SUCCESS; |
| 819 | int dev = 0; |
Egbert Eich | bfd4a71 | 2013-10-04 18:53:04 +0200 | [diff] [blame] | 820 | char *ep; |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 821 | struct blk_desc *blk_dev_desc = NULL; |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 822 | |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 823 | #ifndef CONFIG_CMD_GPT_RENAME |
Lukasz Majewski | 309691b | 2015-11-20 08:06:17 +0100 | [diff] [blame] | 824 | if (argc < 4 || argc > 5) |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 825 | #else |
| 826 | if (argc < 4 || argc > 6) |
| 827 | #endif |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 828 | return CMD_RET_USAGE; |
| 829 | |
Lukasz Majewski | 309691b | 2015-11-20 08:06:17 +0100 | [diff] [blame] | 830 | dev = (int)simple_strtoul(argv[3], &ep, 10); |
| 831 | if (!ep || ep[0] != '\0') { |
| 832 | printf("'%s' is not a number\n", argv[3]); |
| 833 | return CMD_RET_USAGE; |
| 834 | } |
Simon Glass | be1e9bb | 2016-02-29 15:25:42 -0700 | [diff] [blame] | 835 | blk_dev_desc = blk_get_dev(argv[2], dev); |
Lukasz Majewski | 309691b | 2015-11-20 08:06:17 +0100 | [diff] [blame] | 836 | if (!blk_dev_desc) { |
| 837 | printf("%s: %s dev %d NOT available\n", |
| 838 | __func__, argv[2], dev); |
| 839 | return CMD_RET_FAILURE; |
| 840 | } |
Przemyslaw Marczak | 5ec2ed2 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 841 | |
Lukasz Majewski | 309691b | 2015-11-20 08:06:17 +0100 | [diff] [blame] | 842 | if ((strcmp(argv[1], "write") == 0) && (argc == 5)) { |
| 843 | printf("Writing GPT: "); |
Przemyslaw Marczak | 5ec2ed2 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 844 | ret = gpt_default(blk_dev_desc, argv[4]); |
Lukasz Majewski | 309691b | 2015-11-20 08:06:17 +0100 | [diff] [blame] | 845 | } else if ((strcmp(argv[1], "verify") == 0)) { |
| 846 | ret = gpt_verify(blk_dev_desc, argv[4]); |
| 847 | printf("Verify GPT: "); |
Alison Chaiken | e422258 | 2017-06-25 16:43:23 -0700 | [diff] [blame] | 848 | } else if (strcmp(argv[1], "guid") == 0) { |
| 849 | ret = do_disk_guid(blk_dev_desc, argv[4]); |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 850 | #ifdef CONFIG_CMD_GPT_RENAME |
| 851 | } else if (strcmp(argv[1], "read") == 0) { |
| 852 | ret = do_get_gpt_info(blk_dev_desc); |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 853 | } else if ((strcmp(argv[1], "swap") == 0) || |
| 854 | (strcmp(argv[1], "rename") == 0)) { |
| 855 | ret = do_rename_gpt_parts(blk_dev_desc, argv[1], argv[4], argv[5]); |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 856 | #endif |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 857 | } else { |
| 858 | return CMD_RET_USAGE; |
| 859 | } |
Lukasz Majewski | 309691b | 2015-11-20 08:06:17 +0100 | [diff] [blame] | 860 | |
| 861 | if (ret) { |
| 862 | printf("error!\n"); |
| 863 | return CMD_RET_FAILURE; |
| 864 | } |
| 865 | |
| 866 | printf("success!\n"); |
| 867 | return CMD_RET_SUCCESS; |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt, |
| 871 | "GUID Partition Table", |
Robert P. J. Day | e3f2c2e | 2013-08-21 11:39:19 -0400 | [diff] [blame] | 872 | "<command> <interface> <dev> <partitions_list>\n" |
Lukasz Majewski | cc5f3d7 | 2015-11-13 07:42:10 +0100 | [diff] [blame] | 873 | " - GUID partition table restoration and validity check\n" |
| 874 | " Restore or verify GPT information on a device connected\n" |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 875 | " to interface\n" |
Lukasz Majewski | cc5f3d7 | 2015-11-13 07:42:10 +0100 | [diff] [blame] | 876 | " Example usage:\n" |
| 877 | " gpt write mmc 0 $partitions\n" |
| 878 | " gpt verify mmc 0 $partitions\n" |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 879 | " read <interface> <dev>\n" |
| 880 | " - read GPT into a data structure for manipulation\n" |
Alison Chaiken | e422258 | 2017-06-25 16:43:23 -0700 | [diff] [blame] | 881 | " guid <interface> <dev>\n" |
| 882 | " - print disk GUID\n" |
| 883 | " guid <interface> <dev> <varname>\n" |
| 884 | " - set environment variable to disk GUID\n" |
| 885 | " Example usage:\n" |
| 886 | " gpt guid mmc 0\n" |
| 887 | " gpt guid mmc 0 varname\n" |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 888 | #ifdef CONFIG_CMD_GPT_RENAME |
| 889 | "gpt partition renaming commands:\n" |
| 890 | "gpt swap <interface> <dev> <name1> <name2>\n" |
| 891 | " - change all partitions named name1 to name2\n" |
| 892 | " and vice-versa\n" |
| 893 | "gpt rename <interface> <dev> <part> <name>\n" |
| 894 | " - rename the specified partition\n" |
| 895 | " Example usage:\n" |
| 896 | " gpt swap mmc 0 foo bar\n" |
| 897 | " gpt rename mmc 0 3 foo\n" |
| 898 | #endif |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 899 | ); |