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