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