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