blob: 98c788e75793f306dd615e52c8de9925c531b25e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Piotr Wilczekcc00ea82012-12-11 11:09:47 +01002/*
3 * cmd_gpt.c -- GPT (GUID Partition Table) handling command
4 *
Lukasz Majewski309691b2015-11-20 08:06:17 +01005 * Copyright (C) 2015
6 * Lukasz Majewski <l.majewski@majess.pl>
7 *
Piotr Wilczekcc00ea82012-12-11 11:09:47 +01008 * Copyright (C) 2012 Samsung Electronics
9 * author: Lukasz Majewski <l.majewski@samsung.com>
10 * author: Piotr Wilczek <p.wilczek@samsung.com>
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010011 */
12
13#include <common.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060014#include <env.h>
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010015#include <malloc.h>
16#include <command.h>
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010017#include <part_efi.h>
18#include <exports.h>
Simon Glass6f044982020-05-10 11:39:52 -060019#include <uuid.h>
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010020#include <linux/ctype.h>
Piotr Wilczek36c06b12013-01-27 22:59:25 +000021#include <div64.h>
Lukasz Majewski309691b2015-11-20 08:06:17 +010022#include <memalign.h>
Alison Chaikenf04d4b52017-07-04 11:18:50 -070023#include <linux/compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070024#include <linux/err.h>
Alison Chaiken8b3397f2017-07-04 11:19:18 -070025#include <linux/sizes.h>
26#include <stdlib.h>
Alison Chaikenf04d4b52017-07-04 11:18:50 -070027
28static LIST_HEAD(disk_partitions);
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010029
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010030/**
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 Marczak5ec2ed22014-04-02 10:20:06 +020039static int extract_env(const char *str, char **env)
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010040{
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +020041 int ret = -1;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010042 char *e, *s;
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +020043#ifdef CONFIG_RANDOM_UUID
44 char uuid_str[UUID_STR_LEN + 1];
45#endif
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010046
47 if (!str || strlen(str) < 4)
48 return -1;
49
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +020050 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 Glass64b723f2017-08-03 12:22:12 -060060 e = env_get(s);
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +020061 if (e == NULL) {
62#ifdef CONFIG_RANDOM_UUID
63 debug("%s unset. ", str);
Vincent Tinellibdfacea2017-02-27 16:11:15 +020064 gen_rand_uuid_str(uuid_str, UUID_STR_FORMAT_GUID);
Simon Glass6a38e412017-08-03 12:22:09 -060065 env_set(s, uuid_str);
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +020066
Simon Glass64b723f2017-08-03 12:22:12 -060067 e = env_get(s);
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +020068 if (e) {
69 debug("Set to random.\n");
70 ret = 0;
71 } else {
72 debug("Can't get random UUID.\n");
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010073 }
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +020074#else
75 debug("%s unset.\n", str);
76#endif
77 } else {
78 debug("%s get from environment.\n", str);
79 ret = 0;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010080 }
81
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +020082 *env = e;
83 free(s);
84
85 return ret;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010086}
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 */
98static 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 Delaunay7840c4a2015-11-17 11:36:52 +0100128 * 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 */
135static 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 Chaiken8dcee7c2017-07-04 11:19:46 -0700161static 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 Chaikenf04d4b52017-07-04 11:18:50 -0700180#ifdef CONFIG_CMD_GPT_RENAME
181static 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
192static struct disk_part *allocate_disk_part(disk_partition_t *info, int partnum)
193{
194 struct disk_part *newpart;
Heinrich Schuchardt30725f52017-09-21 19:03:06 +0200195 newpart = calloc(1, sizeof(struct disk_part));
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700196 if (!newpart)
197 return ERR_PTR(-ENOMEM);
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700198
199 newpart->gpt_part_info.start = info->start;
200 newpart->gpt_part_info.size = info->size;
201 newpart->gpt_part_info.blksz = info->blksz;
202 strncpy((char *)newpart->gpt_part_info.name, (const char *)info->name,
203 PART_NAME_LEN);
204 newpart->gpt_part_info.name[PART_NAME_LEN - 1] = '\0';
205 strncpy((char *)newpart->gpt_part_info.type, (const char *)info->type,
206 PART_TYPE_LEN);
207 newpart->gpt_part_info.type[PART_TYPE_LEN - 1] = '\0';
208 newpart->gpt_part_info.bootable = info->bootable;
209#ifdef CONFIG_PARTITION_UUIDS
210 strncpy(newpart->gpt_part_info.uuid, (const char *)info->uuid,
211 UUID_STR_LEN);
212 /* UUID_STR_LEN is correct, as uuid[]'s length is UUID_STR_LEN+1 chars */
213 newpart->gpt_part_info.uuid[UUID_STR_LEN] = '\0';
214#endif
215 newpart->partnum = partnum;
216
217 return newpart;
218}
219
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700220static void prettyprint_part_size(char *sizestr, lbaint_t partsize,
221 lbaint_t blksize)
222{
223 unsigned long long partbytes, partmegabytes;
224
225 partbytes = partsize * blksize;
226 partmegabytes = lldiv(partbytes, SZ_1M);
227 snprintf(sizestr, 16, "%lluMiB", partmegabytes);
228}
229
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700230static void print_gpt_info(void)
231{
232 struct list_head *pos;
233 struct disk_part *curr;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700234 char partstartstr[16];
235 char partsizestr[16];
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700236
237 list_for_each(pos, &disk_partitions) {
238 curr = list_entry(pos, struct disk_part, list);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700239 prettyprint_part_size(partstartstr, curr->gpt_part_info.start,
240 curr->gpt_part_info.blksz);
241 prettyprint_part_size(partsizestr, curr->gpt_part_info.size,
242 curr->gpt_part_info.blksz);
243
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700244 printf("Partition %d:\n", curr->partnum);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700245 printf("Start %s, size %s\n", partstartstr, partsizestr);
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700246 printf("Block size %lu, name %s\n", curr->gpt_part_info.blksz,
247 curr->gpt_part_info.name);
248 printf("Type %s, bootable %d\n", curr->gpt_part_info.type,
Heinrich Schuchardt59a860d2020-03-19 13:49:34 +0100249 curr->gpt_part_info.bootable & PART_BOOTABLE);
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700250#ifdef CONFIG_PARTITION_UUIDS
251 printf("UUID %s\n", curr->gpt_part_info.uuid);
252#endif
253 printf("\n");
254 }
255}
256
257/*
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700258 * create the string that upstream 'gpt write' command will accept as an
259 * argument
260 *
261 * From doc/README.gpt, Format of partitions layout:
262 * "uuid_disk=...;name=u-boot,size=60MiB,uuid=...;
263 * name=kernel,size=60MiB,uuid=...;"
264 * The fields 'name' and 'size' are mandatory for every partition.
265 * The field 'start' is optional. The fields 'uuid' and 'uuid_disk'
266 * are optional if CONFIG_RANDOM_UUID is enabled.
267 */
268static int create_gpt_partitions_list(int numparts, const char *guid,
269 char *partitions_list)
270{
271 struct list_head *pos;
272 struct disk_part *curr;
273 char partstr[PART_NAME_LEN + 1];
274
275 if (!partitions_list)
276 return -EINVAL;
277
278 strcpy(partitions_list, "uuid_disk=");
279 strncat(partitions_list, guid, UUID_STR_LEN + 1);
280 strcat(partitions_list, ";");
281
282 list_for_each(pos, &disk_partitions) {
283 curr = list_entry(pos, struct disk_part, list);
284 strcat(partitions_list, "name=");
285 strncat(partitions_list, (const char *)curr->gpt_part_info.name,
286 PART_NAME_LEN + 1);
Patrick Delaunay309a6fa2017-10-18 15:11:08 +0200287 sprintf(partstr, ",start=0x%llx",
288 (unsigned long long)curr->gpt_part_info.start *
289 curr->gpt_part_info.blksz);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700290 /* one extra byte for NULL */
291 strncat(partitions_list, partstr, PART_NAME_LEN + 1);
Patrick Delaunay309a6fa2017-10-18 15:11:08 +0200292 sprintf(partstr, ",size=0x%llx",
293 (unsigned long long)curr->gpt_part_info.size *
294 curr->gpt_part_info.blksz);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700295 strncat(partitions_list, partstr, PART_NAME_LEN + 1);
296
297 strcat(partitions_list, ",uuid=");
298 strncat(partitions_list, curr->gpt_part_info.uuid,
299 UUID_STR_LEN + 1);
300 strcat(partitions_list, ";");
301 }
302 return 0;
303}
304
305/*
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700306 * read partition info into disk_partitions list where
307 * it can be printed or modified
308 */
309static int get_gpt_info(struct blk_desc *dev_desc)
310{
311 /* start partition numbering at 1, as U-Boot does */
312 int valid_parts = 0, p, ret;
313 disk_partition_t info;
314 struct disk_part *new_disk_part;
315
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700316 /*
317 * Always re-read partition info from device, in case
318 * it has changed
319 */
320 INIT_LIST_HEAD(&disk_partitions);
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700321
322 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
323 ret = part_get_info(dev_desc, p, &info);
324 if (ret)
325 continue;
326
327 /* Add 1 here because counter is zero-based but p1 is
328 the first partition */
329 new_disk_part = allocate_disk_part(&info, valid_parts+1);
330 if (IS_ERR(new_disk_part))
331 goto out;
332
333 list_add_tail(&new_disk_part->list, &disk_partitions);
334 valid_parts++;
335 }
336 if (valid_parts == 0) {
337 printf("** No valid partitions found **\n");
338 goto out;
339 }
340 return valid_parts;
341 out:
342 if (valid_parts >= 1)
343 del_gpt_info();
344 return -ENODEV;
345}
346
347/* a wrapper to test get_gpt_info */
348static int do_get_gpt_info(struct blk_desc *dev_desc)
349{
350 int ret;
351
352 ret = get_gpt_info(dev_desc);
353 if (ret > 0) {
354 print_gpt_info();
355 del_gpt_info();
356 return 0;
357 }
358 return ret;
359}
360#endif
361
Patrick Delaunay7840c4a2015-11-17 11:36:52 +0100362/**
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100363 * set_gpt_info(): Fill partition information from string
364 * function allocates memory, remember to free!
365 *
366 * @param dev_desc - pointer block device descriptor
367 * @param str_part - pointer to string with partition information
368 * @param str_disk_guid - pointer to pointer to allocated string with disk guid
369 * @param partitions - pointer to pointer to allocated partitions array
370 * @param parts_count - number of partitions
371 *
372 * @return - zero on success, otherwise error
373 *
374 */
Simon Glasse3394752016-02-29 15:25:34 -0700375static int set_gpt_info(struct blk_desc *dev_desc,
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100376 const char *str_part,
377 char **str_disk_guid,
378 disk_partition_t **partitions,
379 u8 *parts_count)
380{
381 char *tok, *str, *s;
382 int i;
383 char *val, *p;
384 int p_count;
385 disk_partition_t *parts;
386 int errno = 0;
Piotr Wilczek36c06b12013-01-27 22:59:25 +0000387 uint64_t size_ll, start_ll;
Michael Trimarchi853445f2016-06-08 10:18:16 +0200388 lbaint_t offset = 0;
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700389 int max_str_part = calc_parts_list_len(MAX_SEARCH_PARTITIONS);
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100390
Egbert Eichbfd4a712013-10-04 18:53:04 +0200391 debug("%s: lba num: 0x%x %d\n", __func__,
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100392 (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba);
393
394 if (str_part == NULL)
395 return -1;
396
397 str = strdup(str_part);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700398 if (str == NULL)
399 return -ENOMEM;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100400
401 /* extract disk guid */
402 s = str;
Rob Herringd9761972015-01-26 09:44:18 -0600403 val = extract_val(str, "uuid_disk");
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100404 if (!val) {
Rob Herringd9761972015-01-26 09:44:18 -0600405#ifdef CONFIG_RANDOM_UUID
406 *str_disk_guid = malloc(UUID_STR_LEN + 1);
Tom Rinia29829e2017-10-07 11:27:59 -0400407 if (*str_disk_guid == NULL)
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700408 return -ENOMEM;
Rob Herringd9761972015-01-26 09:44:18 -0600409 gen_rand_uuid_str(*str_disk_guid, UUID_STR_FORMAT_STD);
410#else
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100411 free(str);
412 return -2;
Rob Herringd9761972015-01-26 09:44:18 -0600413#endif
414 } else {
415 val = strsep(&val, ";");
416 if (extract_env(val, &p))
417 p = val;
418 *str_disk_guid = strdup(p);
419 free(val);
420 /* Move s to first partition */
421 strsep(&s, ";");
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100422 }
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700423 if (s == NULL) {
424 printf("Error: is the partitions string NULL-terminated?\n");
425 return -EINVAL;
426 }
427 if (strnlen(s, max_str_part) == 0)
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100428 return -3;
429
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700430 i = strnlen(s, max_str_part) - 1;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100431 if (s[i] == ';')
432 s[i] = '\0';
433
434 /* calculate expected number of partitions */
435 p_count = 1;
436 p = s;
437 while (*p) {
438 if (*p++ == ';')
439 p_count++;
440 }
441
442 /* allocate memory for partitions */
443 parts = calloc(sizeof(disk_partition_t), p_count);
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700444 if (parts == NULL)
445 return -ENOMEM;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100446
Robert P. J. Daye3f2c2e2013-08-21 11:39:19 -0400447 /* retrieve partitions data from string */
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100448 for (i = 0; i < p_count; i++) {
449 tok = strsep(&s, ";");
450
451 if (tok == NULL)
452 break;
453
454 /* uuid */
455 val = extract_val(tok, "uuid");
Rob Herringd9761972015-01-26 09:44:18 -0600456 if (!val) {
457 /* 'uuid' is optional if random uuid's are enabled */
458#ifdef CONFIG_RANDOM_UUID
459 gen_rand_uuid_str(parts[i].uuid, UUID_STR_FORMAT_STD);
460#else
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100461 errno = -4;
462 goto err;
Rob Herringd9761972015-01-26 09:44:18 -0600463#endif
464 } else {
465 if (extract_env(val, &p))
466 p = val;
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700467 if (strnlen(p, max_str_part) >= sizeof(parts[i].uuid)) {
Rob Herringd9761972015-01-26 09:44:18 -0600468 printf("Wrong uuid format for partition %d\n", i);
469 errno = -4;
470 goto err;
471 }
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700472 strncpy((char *)parts[i].uuid, p, max_str_part);
Rob Herringd9761972015-01-26 09:44:18 -0600473 free(val);
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100474 }
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100475#ifdef CONFIG_PARTITION_TYPE_GUID
476 /* guid */
477 val = extract_val(tok, "type");
478 if (val) {
479 /* 'type' is optional */
480 if (extract_env(val, &p))
481 p = val;
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700482 if (strnlen(p, max_str_part) >= sizeof(parts[i].type_guid)) {
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100483 printf("Wrong type guid format for partition %d\n",
484 i);
485 errno = -4;
486 goto err;
487 }
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700488 strncpy((char *)parts[i].type_guid, p, max_str_part);
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100489 free(val);
490 }
491#endif
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100492 /* name */
493 val = extract_val(tok, "name");
494 if (!val) { /* name is mandatory */
495 errno = -4;
496 goto err;
497 }
498 if (extract_env(val, &p))
499 p = val;
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700500 if (strnlen(p, max_str_part) >= sizeof(parts[i].name)) {
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100501 errno = -4;
502 goto err;
503 }
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700504 strncpy((char *)parts[i].name, p, max_str_part);
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100505 free(val);
506
507 /* size */
508 val = extract_val(tok, "size");
509 if (!val) { /* 'size' is mandatory */
510 errno = -4;
511 goto err;
512 }
513 if (extract_env(val, &p))
514 p = val;
Michael Trimarchi853445f2016-06-08 10:18:16 +0200515 if ((strcmp(p, "-") == 0)) {
Kever Yangf1d070d2016-07-29 11:12:18 +0800516 /* Let part efi module to auto extend the size */
517 parts[i].size = 0;
Michael Trimarchi853445f2016-06-08 10:18:16 +0200518 } else {
519 size_ll = ustrtoull(p, &p, 0);
520 parts[i].size = lldiv(size_ll, dev_desc->blksz);
521 }
522
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100523 free(val);
524
525 /* start address */
526 val = extract_val(tok, "start");
527 if (val) { /* start address is optional */
528 if (extract_env(val, &p))
529 p = val;
Piotr Wilczek36c06b12013-01-27 22:59:25 +0000530 start_ll = ustrtoull(p, &p, 0);
531 parts[i].start = lldiv(start_ll, dev_desc->blksz);
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100532 free(val);
533 }
Patrick Delaunay7840c4a2015-11-17 11:36:52 +0100534
Michael Trimarchi853445f2016-06-08 10:18:16 +0200535 offset += parts[i].size + parts[i].start;
536
Patrick Delaunay7840c4a2015-11-17 11:36:52 +0100537 /* bootable */
538 if (found_key(tok, "bootable"))
Heinrich Schuchardt59a860d2020-03-19 13:49:34 +0100539 parts[i].bootable = PART_BOOTABLE;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100540 }
541
542 *parts_count = p_count;
543 *partitions = parts;
544 free(str);
545
546 return 0;
547err:
548 free(str);
549 free(*str_disk_guid);
550 free(parts);
551
552 return errno;
553}
554
Simon Glasse3394752016-02-29 15:25:34 -0700555static int gpt_default(struct blk_desc *blk_dev_desc, const char *str_part)
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100556{
557 int ret;
558 char *str_disk_guid;
559 u8 part_count = 0;
560 disk_partition_t *partitions = NULL;
561
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100562 /* fill partitions */
Egbert Eichbfd4a712013-10-04 18:53:04 +0200563 ret = set_gpt_info(blk_dev_desc, str_part,
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100564 &str_disk_guid, &partitions, &part_count);
565 if (ret) {
566 if (ret == -1)
567 printf("No partition list provided\n");
568 if (ret == -2)
569 printf("Missing disk guid\n");
570 if ((ret == -3) || (ret == -4))
571 printf("Partition list incomplete\n");
572 return -1;
573 }
574
575 /* save partitions layout to disk */
Rob Herring008ab492015-01-26 09:43:15 -0600576 ret = gpt_restore(blk_dev_desc, str_disk_guid, partitions, part_count);
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100577 free(str_disk_guid);
578 free(partitions);
579
Lukasz Majewski309691b2015-11-20 08:06:17 +0100580 return ret;
581}
582
Simon Glasse3394752016-02-29 15:25:34 -0700583static int gpt_verify(struct blk_desc *blk_dev_desc, const char *str_part)
Lukasz Majewski309691b2015-11-20 08:06:17 +0100584{
585 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1,
586 blk_dev_desc->blksz);
587 disk_partition_t *partitions = NULL;
588 gpt_entry *gpt_pte = NULL;
589 char *str_disk_guid;
590 u8 part_count = 0;
591 int ret = 0;
592
593 /* fill partitions */
594 ret = set_gpt_info(blk_dev_desc, str_part,
595 &str_disk_guid, &partitions, &part_count);
596 if (ret) {
597 if (ret == -1) {
598 printf("No partition list provided - only basic check\n");
599 ret = gpt_verify_headers(blk_dev_desc, gpt_head,
600 &gpt_pte);
601 goto out;
602 }
603 if (ret == -2)
604 printf("Missing disk guid\n");
605 if ((ret == -3) || (ret == -4))
606 printf("Partition list incomplete\n");
607 return -1;
608 }
609
610 /* Check partition layout with provided pattern */
611 ret = gpt_verify_partitions(blk_dev_desc, partitions, part_count,
612 gpt_head, &gpt_pte);
613 free(str_disk_guid);
614 free(partitions);
615 out:
616 free(gpt_pte);
Rob Herring008ab492015-01-26 09:43:15 -0600617 return ret;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100618}
619
Alison Chaikene4222582017-06-25 16:43:23 -0700620static int do_disk_guid(struct blk_desc *dev_desc, char * const namestr)
621{
622 int ret;
623 char disk_guid[UUID_STR_LEN + 1];
624
625 ret = get_disk_guid(dev_desc, disk_guid);
626 if (ret < 0)
627 return CMD_RET_FAILURE;
628
629 if (namestr)
Simon Glass6a38e412017-08-03 12:22:09 -0600630 env_set(namestr, disk_guid);
Alison Chaikene4222582017-06-25 16:43:23 -0700631 else
632 printf("%s\n", disk_guid);
633
634 return ret;
635}
636
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700637#ifdef CONFIG_CMD_GPT_RENAME
638static int do_rename_gpt_parts(struct blk_desc *dev_desc, char *subcomm,
639 char *name1, char *name2)
640{
641 struct list_head *pos;
642 struct disk_part *curr;
643 disk_partition_t *new_partitions = NULL;
644 char disk_guid[UUID_STR_LEN + 1];
Tom Rinib0d3c0b2020-01-21 11:53:38 -0500645 char *partitions_list, *str_disk_guid = NULL;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700646 u8 part_count = 0;
647 int partlistlen, ret, numparts = 0, partnum, i = 1, ctr1 = 0, ctr2 = 0;
648
649 if ((subcomm == NULL) || (name1 == NULL) || (name2 == NULL) ||
650 (strcmp(subcomm, "swap") && (strcmp(subcomm, "rename"))))
651 return -EINVAL;
652
653 ret = get_disk_guid(dev_desc, disk_guid);
654 if (ret < 0)
655 return ret;
Alison Chaiken7e805c02017-09-26 07:42:28 -0700656 /*
657 * Allocates disk_partitions, requiring matching call to del_gpt_info()
658 * if successful.
659 */
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700660 numparts = get_gpt_info(dev_desc);
661 if (numparts <= 0)
662 return numparts ? numparts : -ENODEV;
663
664 partlistlen = calc_parts_list_len(numparts);
665 partitions_list = malloc(partlistlen);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700666 if (!partitions_list) {
667 del_gpt_info();
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700668 return -ENOMEM;
Alison Chaiken7e805c02017-09-26 07:42:28 -0700669 }
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700670 memset(partitions_list, '\0', partlistlen);
671
672 ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700673 if (ret < 0) {
674 free(partitions_list);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700675 return ret;
Alison Chaiken7e805c02017-09-26 07:42:28 -0700676 }
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700677 /*
678 * Uncomment the following line to print a string that 'gpt write'
679 * or 'gpt verify' will accept as input.
680 */
681 debug("OLD partitions_list is %s with %u chars\n", partitions_list,
682 (unsigned)strlen(partitions_list));
683
Alison Chaiken7e805c02017-09-26 07:42:28 -0700684 /* set_gpt_info allocates new_partitions and str_disk_guid */
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700685 ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid,
686 &new_partitions, &part_count);
Tom Rinib0d3c0b2020-01-21 11:53:38 -0500687 if (ret < 0)
688 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700689
690 if (!strcmp(subcomm, "swap")) {
691 if ((strlen(name1) > PART_NAME_LEN) || (strlen(name2) > PART_NAME_LEN)) {
692 printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700693 ret = -EINVAL;
694 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700695 }
696 list_for_each(pos, &disk_partitions) {
697 curr = list_entry(pos, struct disk_part, list);
698 if (!strcmp((char *)curr->gpt_part_info.name, name1)) {
699 strcpy((char *)curr->gpt_part_info.name, name2);
700 ctr1++;
701 } else if (!strcmp((char *)curr->gpt_part_info.name, name2)) {
702 strcpy((char *)curr->gpt_part_info.name, name1);
703 ctr2++;
704 }
705 }
706 if ((ctr1 + ctr2 < 2) || (ctr1 != ctr2)) {
707 printf("Cannot swap partition names except in pairs.\n");
Alison Chaiken7e805c02017-09-26 07:42:28 -0700708 ret = -EINVAL;
709 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700710 }
711 } else { /* rename */
712 if (strlen(name2) > PART_NAME_LEN) {
713 printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700714 ret = -EINVAL;
715 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700716 }
717 partnum = (int)simple_strtol(name1, NULL, 10);
718 if ((partnum < 0) || (partnum > numparts)) {
719 printf("Illegal partition number %s\n", name1);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700720 ret = -EINVAL;
721 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700722 }
723 ret = part_get_info(dev_desc, partnum, new_partitions);
724 if (ret < 0)
Alison Chaiken7e805c02017-09-26 07:42:28 -0700725 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700726
727 /* U-Boot partition numbering starts at 1 */
728 list_for_each(pos, &disk_partitions) {
729 curr = list_entry(pos, struct disk_part, list);
730 if (i == partnum) {
731 strcpy((char *)curr->gpt_part_info.name, name2);
732 break;
733 }
734 i++;
735 }
736 }
737
738 ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list);
739 if (ret < 0)
Alison Chaiken7e805c02017-09-26 07:42:28 -0700740 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700741 debug("NEW partitions_list is %s with %u chars\n", partitions_list,
742 (unsigned)strlen(partitions_list));
743
744 ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid,
745 &new_partitions, &part_count);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700746 /*
747 * Even though valid pointers are here passed into set_gpt_info(),
748 * it mallocs again, and there's no way to tell which failed.
749 */
Tom Rinib0d3c0b2020-01-21 11:53:38 -0500750 if (ret < 0)
751 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700752
753 debug("Writing new partition table\n");
754 ret = gpt_restore(dev_desc, disk_guid, new_partitions, numparts);
755 if (ret < 0) {
756 printf("Writing new partition table failed\n");
Alison Chaiken7e805c02017-09-26 07:42:28 -0700757 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700758 }
759
760 debug("Reading back new partition table\n");
Alison Chaiken7e805c02017-09-26 07:42:28 -0700761 /*
762 * Empty the existing disk_partitions list, as otherwise the memory in
763 * the original list is unreachable.
764 */
765 del_gpt_info();
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700766 numparts = get_gpt_info(dev_desc);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700767 if (numparts <= 0) {
768 ret = numparts ? numparts : -ENODEV;
769 goto out;
770 }
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700771 printf("new partition table with %d partitions is:\n", numparts);
772 print_gpt_info();
Alison Chaiken7e805c02017-09-26 07:42:28 -0700773 out:
Tom Rinib0d3c0b2020-01-21 11:53:38 -0500774 del_gpt_info();
775#ifdef CONFIG_RANDOM_UUID
Heinrich Schuchardt443806d2020-04-28 21:40:13 +0200776 free(str_disk_guid);
Tom Rinib0d3c0b2020-01-21 11:53:38 -0500777#endif
Heinrich Schuchardt443806d2020-04-28 21:40:13 +0200778 free(new_partitions);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700779 free(partitions_list);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700780 return ret;
781}
782#endif
783
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100784/**
785 * do_gpt(): Perform GPT operations
786 *
787 * @param cmdtp - command name
788 * @param flag
789 * @param argc
790 * @param argv
791 *
792 * @return zero on success; otherwise error
793 */
794static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
795{
796 int ret = CMD_RET_SUCCESS;
797 int dev = 0;
Egbert Eichbfd4a712013-10-04 18:53:04 +0200798 char *ep;
Simon Glasse3394752016-02-29 15:25:34 -0700799 struct blk_desc *blk_dev_desc = NULL;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100800
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700801#ifndef CONFIG_CMD_GPT_RENAME
Lukasz Majewski309691b2015-11-20 08:06:17 +0100802 if (argc < 4 || argc > 5)
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700803#else
804 if (argc < 4 || argc > 6)
805#endif
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100806 return CMD_RET_USAGE;
807
Lukasz Majewski309691b2015-11-20 08:06:17 +0100808 dev = (int)simple_strtoul(argv[3], &ep, 10);
809 if (!ep || ep[0] != '\0') {
810 printf("'%s' is not a number\n", argv[3]);
811 return CMD_RET_USAGE;
812 }
Simon Glassbe1e9bb2016-02-29 15:25:42 -0700813 blk_dev_desc = blk_get_dev(argv[2], dev);
Lukasz Majewski309691b2015-11-20 08:06:17 +0100814 if (!blk_dev_desc) {
815 printf("%s: %s dev %d NOT available\n",
816 __func__, argv[2], dev);
817 return CMD_RET_FAILURE;
818 }
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +0200819
Lukasz Majewski309691b2015-11-20 08:06:17 +0100820 if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
821 printf("Writing GPT: ");
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +0200822 ret = gpt_default(blk_dev_desc, argv[4]);
Lukasz Majewski309691b2015-11-20 08:06:17 +0100823 } else if ((strcmp(argv[1], "verify") == 0)) {
824 ret = gpt_verify(blk_dev_desc, argv[4]);
825 printf("Verify GPT: ");
Alison Chaikene4222582017-06-25 16:43:23 -0700826 } else if (strcmp(argv[1], "guid") == 0) {
827 ret = do_disk_guid(blk_dev_desc, argv[4]);
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700828#ifdef CONFIG_CMD_GPT_RENAME
829 } else if (strcmp(argv[1], "read") == 0) {
830 ret = do_get_gpt_info(blk_dev_desc);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700831 } else if ((strcmp(argv[1], "swap") == 0) ||
832 (strcmp(argv[1], "rename") == 0)) {
833 ret = do_rename_gpt_parts(blk_dev_desc, argv[1], argv[4], argv[5]);
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700834#endif
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100835 } else {
836 return CMD_RET_USAGE;
837 }
Lukasz Majewski309691b2015-11-20 08:06:17 +0100838
839 if (ret) {
840 printf("error!\n");
841 return CMD_RET_FAILURE;
842 }
843
844 printf("success!\n");
845 return CMD_RET_SUCCESS;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100846}
847
848U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
849 "GUID Partition Table",
Robert P. J. Daye3f2c2e2013-08-21 11:39:19 -0400850 "<command> <interface> <dev> <partitions_list>\n"
Lukasz Majewskicc5f3d72015-11-13 07:42:10 +0100851 " - GUID partition table restoration and validity check\n"
852 " Restore or verify GPT information on a device connected\n"
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100853 " to interface\n"
Lukasz Majewskicc5f3d72015-11-13 07:42:10 +0100854 " Example usage:\n"
855 " gpt write mmc 0 $partitions\n"
856 " gpt verify mmc 0 $partitions\n"
Eugeniu Roscafa8acbc2019-04-30 04:53:46 +0200857 " gpt guid <interface> <dev>\n"
Alison Chaikene4222582017-06-25 16:43:23 -0700858 " - print disk GUID\n"
Eugeniu Roscafa8acbc2019-04-30 04:53:46 +0200859 " gpt guid <interface> <dev> <varname>\n"
Alison Chaikene4222582017-06-25 16:43:23 -0700860 " - set environment variable to disk GUID\n"
861 " Example usage:\n"
862 " gpt guid mmc 0\n"
863 " gpt guid mmc 0 varname\n"
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700864#ifdef CONFIG_CMD_GPT_RENAME
865 "gpt partition renaming commands:\n"
Eugeniu Roscafa8acbc2019-04-30 04:53:46 +0200866 " gpt read <interface> <dev>\n"
867 " - read GPT into a data structure for manipulation\n"
868 " gpt swap <interface> <dev> <name1> <name2>\n"
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700869 " - change all partitions named name1 to name2\n"
870 " and vice-versa\n"
Eugeniu Roscafa8acbc2019-04-30 04:53:46 +0200871 " gpt rename <interface> <dev> <part> <name>\n"
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700872 " - rename the specified partition\n"
873 " Example usage:\n"
874 " gpt swap mmc 0 foo bar\n"
875 " gpt rename mmc 0 3 foo\n"
876#endif
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100877);