blob: 3cc6436b28b07aeab2c046d1f97b80c9297dba60 [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 Glass655306c2020-05-10 11:39:58 -060014#include <blk.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060015#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010017#include <malloc.h>
18#include <command.h>
Simon Glass655306c2020-05-10 11:39:58 -060019#include <part.h>
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010020#include <part_efi.h>
Corneliu Doban0c6776c2020-11-09 11:49:16 +053021#include <part.h>
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010022#include <exports.h>
Simon Glass6f044982020-05-10 11:39:52 -060023#include <uuid.h>
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010024#include <linux/ctype.h>
Piotr Wilczek36c06b12013-01-27 22:59:25 +000025#include <div64.h>
Lukasz Majewski309691b2015-11-20 08:06:17 +010026#include <memalign.h>
Alison Chaikenf04d4b52017-07-04 11:18:50 -070027#include <linux/compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070028#include <linux/err.h>
Alison Chaiken8b3397f2017-07-04 11:19:18 -070029#include <linux/sizes.h>
30#include <stdlib.h>
Alison Chaikenf04d4b52017-07-04 11:18:50 -070031
32static LIST_HEAD(disk_partitions);
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010033
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010034/**
35 * extract_env(): Expand env name from string format '&{env_name}'
36 * and return pointer to the env (if the env is set)
37 *
38 * @param str - pointer to string
39 * @param env - pointer to pointer to extracted env
40 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010041 * Return: - zero on successful expand and env is set
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010042 */
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +020043static int extract_env(const char *str, char **env)
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010044{
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +020045 int ret = -1;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010046 char *e, *s;
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +020047#ifdef CONFIG_RANDOM_UUID
48 char uuid_str[UUID_STR_LEN + 1];
49#endif
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010050
51 if (!str || strlen(str) < 4)
52 return -1;
53
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +020054 if (!((strncmp(str, "${", 2) == 0) && (str[strlen(str) - 1] == '}')))
55 return -1;
56
57 s = strdup(str);
58 if (s == NULL)
59 return -1;
60
61 memset(s + strlen(s) - 1, '\0', 1);
62 memmove(s, s + 2, strlen(s) - 1);
63
Simon Glass64b723f2017-08-03 12:22:12 -060064 e = env_get(s);
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +020065 if (e == NULL) {
66#ifdef CONFIG_RANDOM_UUID
67 debug("%s unset. ", str);
Vincent Tinellibdfacea2017-02-27 16:11:15 +020068 gen_rand_uuid_str(uuid_str, UUID_STR_FORMAT_GUID);
Simon Glass6a38e412017-08-03 12:22:09 -060069 env_set(s, uuid_str);
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +020070
Simon Glass64b723f2017-08-03 12:22:12 -060071 e = env_get(s);
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +020072 if (e) {
73 debug("Set to random.\n");
74 ret = 0;
75 } else {
76 debug("Can't get random UUID.\n");
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010077 }
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +020078#else
79 debug("%s unset.\n", str);
80#endif
81 } else {
82 debug("%s get from environment.\n", str);
83 ret = 0;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010084 }
85
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +020086 *env = e;
87 free(s);
88
89 return ret;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010090}
91
92/**
93 * extract_val(): Extract value from a key=value pair list (comma separated).
94 * Only value for the given key is returend.
95 * Function allocates memory for the value, remember to free!
96 *
97 * @param str - pointer to string with key=values pairs
98 * @param key - pointer to the key to search for
99 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100100 * Return: - pointer to allocated string with the value
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100101 */
102static char *extract_val(const char *str, const char *key)
103{
104 char *v, *k;
105 char *s, *strcopy;
106 char *new = NULL;
107
108 strcopy = strdup(str);
109 if (strcopy == NULL)
110 return NULL;
111
112 s = strcopy;
113 while (s) {
114 v = strsep(&s, ",");
115 if (!v)
116 break;
117 k = strsep(&v, "=");
118 if (!k)
119 break;
120 if (strcmp(k, key) == 0) {
121 new = strdup(v);
122 break;
123 }
124 }
125
126 free(strcopy);
127
128 return new;
129}
130
131/**
Patrick Delaunay7840c4a2015-11-17 11:36:52 +0100132 * found_key(): Found key without value in parameter list (comma separated).
133 *
134 * @param str - pointer to string with key
135 * @param key - pointer to the key to search for
136 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100137 * Return: - true on found key
Patrick Delaunay7840c4a2015-11-17 11:36:52 +0100138 */
139static bool found_key(const char *str, const char *key)
140{
141 char *k;
142 char *s, *strcopy;
143 bool result = false;
144
145 strcopy = strdup(str);
146 if (!strcopy)
147 return NULL;
148
149 s = strcopy;
150 while (s) {
151 k = strsep(&s, ",");
152 if (!k)
153 break;
154 if (strcmp(k, key) == 0) {
155 result = true;
156 break;
157 }
158 }
159
160 free(strcopy);
161
162 return result;
163}
164
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700165static int calc_parts_list_len(int numparts)
166{
167 int partlistlen = UUID_STR_LEN + 1 + strlen("uuid_disk=");
168 /* for the comma */
169 partlistlen++;
170
171 /* per-partition additions; numparts starts at 1, so this should be correct */
172 partlistlen += numparts * (strlen("name=,") + PART_NAME_LEN + 1);
173 /* see part.h for definition of struct disk_partition */
174 partlistlen += numparts * (strlen("start=MiB,") + sizeof(lbaint_t) + 1);
175 partlistlen += numparts * (strlen("size=MiB,") + sizeof(lbaint_t) + 1);
176 partlistlen += numparts * (strlen("uuid=;") + UUID_STR_LEN + 1);
177 /* for the terminating null */
178 partlistlen++;
179 debug("Length of partitions_list is %d for %d partitions\n", partlistlen,
180 numparts);
181 return partlistlen;
182}
183
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700184#ifdef CONFIG_CMD_GPT_RENAME
185static void del_gpt_info(void)
186{
187 struct list_head *pos = &disk_partitions;
188 struct disk_part *curr;
189 while (!list_empty(pos)) {
190 curr = list_entry(pos->next, struct disk_part, list);
191 list_del(pos->next);
192 free(curr);
193 }
194}
195
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600196static struct disk_part *allocate_disk_part(struct disk_partition *info,
197 int partnum)
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700198{
199 struct disk_part *newpart;
Heinrich Schuchardt30725f52017-09-21 19:03:06 +0200200 newpart = calloc(1, sizeof(struct disk_part));
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700201 if (!newpart)
202 return ERR_PTR(-ENOMEM);
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700203
204 newpart->gpt_part_info.start = info->start;
205 newpart->gpt_part_info.size = info->size;
206 newpart->gpt_part_info.blksz = info->blksz;
207 strncpy((char *)newpart->gpt_part_info.name, (const char *)info->name,
208 PART_NAME_LEN);
209 newpart->gpt_part_info.name[PART_NAME_LEN - 1] = '\0';
210 strncpy((char *)newpart->gpt_part_info.type, (const char *)info->type,
211 PART_TYPE_LEN);
212 newpart->gpt_part_info.type[PART_TYPE_LEN - 1] = '\0';
213 newpart->gpt_part_info.bootable = info->bootable;
Simon Glass23e34532023-08-24 13:55:31 -0600214 if (IS_ENABLED(CONFIG_PARTITION_UUIDS)) {
215 strlcpy(newpart->gpt_part_info.uuid, disk_partition_uuid(info),
216 UUID_STR_LEN + 1);
217 }
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700218 newpart->partnum = partnum;
219
220 return newpart;
221}
222
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700223static void prettyprint_part_size(char *sizestr, lbaint_t partsize,
224 lbaint_t blksize)
225{
226 unsigned long long partbytes, partmegabytes;
227
228 partbytes = partsize * blksize;
229 partmegabytes = lldiv(partbytes, SZ_1M);
230 snprintf(sizestr, 16, "%lluMiB", partmegabytes);
231}
232
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700233static void print_gpt_info(void)
234{
235 struct list_head *pos;
236 struct disk_part *curr;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700237 char partstartstr[16];
238 char partsizestr[16];
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700239
240 list_for_each(pos, &disk_partitions) {
241 curr = list_entry(pos, struct disk_part, list);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700242 prettyprint_part_size(partstartstr, curr->gpt_part_info.start,
243 curr->gpt_part_info.blksz);
244 prettyprint_part_size(partsizestr, curr->gpt_part_info.size,
245 curr->gpt_part_info.blksz);
246
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700247 printf("Partition %d:\n", curr->partnum);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700248 printf("Start %s, size %s\n", partstartstr, partsizestr);
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700249 printf("Block size %lu, name %s\n", curr->gpt_part_info.blksz,
250 curr->gpt_part_info.name);
251 printf("Type %s, bootable %d\n", curr->gpt_part_info.type,
Heinrich Schuchardt59a860d2020-03-19 13:49:34 +0100252 curr->gpt_part_info.bootable & PART_BOOTABLE);
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700253#ifdef CONFIG_PARTITION_UUIDS
254 printf("UUID %s\n", curr->gpt_part_info.uuid);
255#endif
256 printf("\n");
257 }
258}
259
260/*
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700261 * create the string that upstream 'gpt write' command will accept as an
262 * argument
263 *
264 * From doc/README.gpt, Format of partitions layout:
265 * "uuid_disk=...;name=u-boot,size=60MiB,uuid=...;
266 * name=kernel,size=60MiB,uuid=...;"
267 * The fields 'name' and 'size' are mandatory for every partition.
268 * The field 'start' is optional. The fields 'uuid' and 'uuid_disk'
269 * are optional if CONFIG_RANDOM_UUID is enabled.
270 */
271static int create_gpt_partitions_list(int numparts, const char *guid,
272 char *partitions_list)
273{
274 struct list_head *pos;
275 struct disk_part *curr;
276 char partstr[PART_NAME_LEN + 1];
277
278 if (!partitions_list)
279 return -EINVAL;
280
281 strcpy(partitions_list, "uuid_disk=");
282 strncat(partitions_list, guid, UUID_STR_LEN + 1);
283 strcat(partitions_list, ";");
284
285 list_for_each(pos, &disk_partitions) {
286 curr = list_entry(pos, struct disk_part, list);
287 strcat(partitions_list, "name=");
288 strncat(partitions_list, (const char *)curr->gpt_part_info.name,
289 PART_NAME_LEN + 1);
Patrick Delaunay309a6fa2017-10-18 15:11:08 +0200290 sprintf(partstr, ",start=0x%llx",
291 (unsigned long long)curr->gpt_part_info.start *
292 curr->gpt_part_info.blksz);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700293 /* one extra byte for NULL */
294 strncat(partitions_list, partstr, PART_NAME_LEN + 1);
Patrick Delaunay309a6fa2017-10-18 15:11:08 +0200295 sprintf(partstr, ",size=0x%llx",
296 (unsigned long long)curr->gpt_part_info.size *
297 curr->gpt_part_info.blksz);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700298 strncat(partitions_list, partstr, PART_NAME_LEN + 1);
299
300 strcat(partitions_list, ",uuid=");
301 strncat(partitions_list, curr->gpt_part_info.uuid,
302 UUID_STR_LEN + 1);
303 strcat(partitions_list, ";");
304 }
305 return 0;
306}
307
308/*
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700309 * read partition info into disk_partitions list where
310 * it can be printed or modified
311 */
312static int get_gpt_info(struct blk_desc *dev_desc)
313{
314 /* start partition numbering at 1, as U-Boot does */
315 int valid_parts = 0, p, ret;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600316 struct disk_partition info;
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700317 struct disk_part *new_disk_part;
318
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700319 /*
320 * Always re-read partition info from device, in case
321 * it has changed
322 */
323 INIT_LIST_HEAD(&disk_partitions);
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700324
325 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
326 ret = part_get_info(dev_desc, p, &info);
327 if (ret)
328 continue;
329
330 /* Add 1 here because counter is zero-based but p1 is
331 the first partition */
332 new_disk_part = allocate_disk_part(&info, valid_parts+1);
333 if (IS_ERR(new_disk_part))
334 goto out;
335
336 list_add_tail(&new_disk_part->list, &disk_partitions);
337 valid_parts++;
338 }
339 if (valid_parts == 0) {
340 printf("** No valid partitions found **\n");
341 goto out;
342 }
343 return valid_parts;
344 out:
345 if (valid_parts >= 1)
346 del_gpt_info();
347 return -ENODEV;
348}
349
350/* a wrapper to test get_gpt_info */
Farhan Ali078d0222021-02-26 10:17:33 -0800351static int do_get_gpt_info(struct blk_desc *dev_desc, char * const namestr)
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700352{
Farhan Ali078d0222021-02-26 10:17:33 -0800353 int numparts;
354
355 numparts = get_gpt_info(dev_desc);
356
357 if (numparts > 0) {
358 if (namestr) {
359 char disk_guid[UUID_STR_LEN + 1];
360 char *partitions_list;
361 int partlistlen;
362 int ret = -1;
363
364 ret = get_disk_guid(dev_desc, disk_guid);
365 if (ret < 0)
366 return ret;
367
368 partlistlen = calc_parts_list_len(numparts);
369 partitions_list = malloc(partlistlen);
370 if (!partitions_list) {
371 del_gpt_info();
372 return -ENOMEM;
373 }
374 memset(partitions_list, '\0', partlistlen);
375
376 ret = create_gpt_partitions_list(numparts, disk_guid,
377 partitions_list);
378 if (ret < 0)
379 printf("Error: Could not create partition list string!\n");
380 else
381 env_set(namestr, partitions_list);
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700382
Farhan Ali078d0222021-02-26 10:17:33 -0800383 free(partitions_list);
384 } else {
385 print_gpt_info();
386 }
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700387 del_gpt_info();
388 return 0;
389 }
Farhan Ali078d0222021-02-26 10:17:33 -0800390 return numparts;
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700391}
392#endif
393
Patrick Delaunay7840c4a2015-11-17 11:36:52 +0100394/**
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100395 * set_gpt_info(): Fill partition information from string
396 * function allocates memory, remember to free!
397 *
398 * @param dev_desc - pointer block device descriptor
399 * @param str_part - pointer to string with partition information
400 * @param str_disk_guid - pointer to pointer to allocated string with disk guid
401 * @param partitions - pointer to pointer to allocated partitions array
402 * @param parts_count - number of partitions
403 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100404 * Return: - zero on success, otherwise error
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100405 *
406 */
Simon Glasse3394752016-02-29 15:25:34 -0700407static int set_gpt_info(struct blk_desc *dev_desc,
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100408 const char *str_part,
409 char **str_disk_guid,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600410 struct disk_partition **partitions,
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100411 u8 *parts_count)
412{
413 char *tok, *str, *s;
414 int i;
415 char *val, *p;
416 int p_count;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600417 struct disk_partition *parts;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100418 int errno = 0;
Piotr Wilczek36c06b12013-01-27 22:59:25 +0000419 uint64_t size_ll, start_ll;
Michael Trimarchi853445f2016-06-08 10:18:16 +0200420 lbaint_t offset = 0;
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700421 int max_str_part = calc_parts_list_len(MAX_SEARCH_PARTITIONS);
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100422
Egbert Eichbfd4a712013-10-04 18:53:04 +0200423 debug("%s: lba num: 0x%x %d\n", __func__,
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100424 (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba);
425
426 if (str_part == NULL)
427 return -1;
428
429 str = strdup(str_part);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700430 if (str == NULL)
431 return -ENOMEM;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100432
433 /* extract disk guid */
434 s = str;
Rob Herringd9761972015-01-26 09:44:18 -0600435 val = extract_val(str, "uuid_disk");
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100436 if (!val) {
Rob Herringd9761972015-01-26 09:44:18 -0600437#ifdef CONFIG_RANDOM_UUID
438 *str_disk_guid = malloc(UUID_STR_LEN + 1);
Tom Rinia29829e2017-10-07 11:27:59 -0400439 if (*str_disk_guid == NULL)
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700440 return -ENOMEM;
Rob Herringd9761972015-01-26 09:44:18 -0600441 gen_rand_uuid_str(*str_disk_guid, UUID_STR_FORMAT_STD);
442#else
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100443 free(str);
444 return -2;
Rob Herringd9761972015-01-26 09:44:18 -0600445#endif
446 } else {
447 val = strsep(&val, ";");
448 if (extract_env(val, &p))
449 p = val;
450 *str_disk_guid = strdup(p);
451 free(val);
452 /* Move s to first partition */
453 strsep(&s, ";");
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100454 }
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700455 if (s == NULL) {
456 printf("Error: is the partitions string NULL-terminated?\n");
457 return -EINVAL;
458 }
459 if (strnlen(s, max_str_part) == 0)
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100460 return -3;
461
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700462 i = strnlen(s, max_str_part) - 1;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100463 if (s[i] == ';')
464 s[i] = '\0';
465
466 /* calculate expected number of partitions */
467 p_count = 1;
468 p = s;
469 while (*p) {
470 if (*p++ == ';')
471 p_count++;
472 }
473
474 /* allocate memory for partitions */
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600475 parts = calloc(sizeof(struct disk_partition), p_count);
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700476 if (parts == NULL)
477 return -ENOMEM;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100478
Robert P. J. Daye3f2c2e2013-08-21 11:39:19 -0400479 /* retrieve partitions data from string */
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100480 for (i = 0; i < p_count; i++) {
481 tok = strsep(&s, ";");
482
483 if (tok == NULL)
484 break;
485
486 /* uuid */
487 val = extract_val(tok, "uuid");
Rob Herringd9761972015-01-26 09:44:18 -0600488 if (!val) {
489 /* 'uuid' is optional if random uuid's are enabled */
490#ifdef CONFIG_RANDOM_UUID
491 gen_rand_uuid_str(parts[i].uuid, UUID_STR_FORMAT_STD);
492#else
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100493 errno = -4;
494 goto err;
Rob Herringd9761972015-01-26 09:44:18 -0600495#endif
496 } else {
497 if (extract_env(val, &p))
498 p = val;
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700499 if (strnlen(p, max_str_part) >= sizeof(parts[i].uuid)) {
Rob Herringd9761972015-01-26 09:44:18 -0600500 printf("Wrong uuid format for partition %d\n", i);
501 errno = -4;
502 goto err;
503 }
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700504 strncpy((char *)parts[i].uuid, p, max_str_part);
Rob Herringd9761972015-01-26 09:44:18 -0600505 free(val);
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100506 }
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100507#ifdef CONFIG_PARTITION_TYPE_GUID
508 /* guid */
509 val = extract_val(tok, "type");
510 if (val) {
511 /* 'type' is optional */
512 if (extract_env(val, &p))
513 p = val;
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700514 if (strnlen(p, max_str_part) >= sizeof(parts[i].type_guid)) {
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100515 printf("Wrong type guid format for partition %d\n",
516 i);
517 errno = -4;
518 goto err;
519 }
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700520 strncpy((char *)parts[i].type_guid, p, max_str_part);
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100521 free(val);
522 }
523#endif
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100524 /* name */
525 val = extract_val(tok, "name");
526 if (!val) { /* name is mandatory */
527 errno = -4;
528 goto err;
529 }
530 if (extract_env(val, &p))
531 p = val;
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700532 if (strnlen(p, max_str_part) >= sizeof(parts[i].name)) {
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100533 errno = -4;
534 goto err;
535 }
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700536 strncpy((char *)parts[i].name, p, max_str_part);
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100537 free(val);
538
539 /* size */
540 val = extract_val(tok, "size");
541 if (!val) { /* 'size' is mandatory */
542 errno = -4;
543 goto err;
544 }
545 if (extract_env(val, &p))
546 p = val;
Michael Trimarchi853445f2016-06-08 10:18:16 +0200547 if ((strcmp(p, "-") == 0)) {
Kever Yangf1d070d2016-07-29 11:12:18 +0800548 /* Let part efi module to auto extend the size */
549 parts[i].size = 0;
Michael Trimarchi853445f2016-06-08 10:18:16 +0200550 } else {
551 size_ll = ustrtoull(p, &p, 0);
552 parts[i].size = lldiv(size_ll, dev_desc->blksz);
553 }
554
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100555 free(val);
556
557 /* start address */
558 val = extract_val(tok, "start");
559 if (val) { /* start address is optional */
560 if (extract_env(val, &p))
561 p = val;
Piotr Wilczek36c06b12013-01-27 22:59:25 +0000562 start_ll = ustrtoull(p, &p, 0);
563 parts[i].start = lldiv(start_ll, dev_desc->blksz);
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100564 free(val);
565 }
Patrick Delaunay7840c4a2015-11-17 11:36:52 +0100566
Michael Trimarchi853445f2016-06-08 10:18:16 +0200567 offset += parts[i].size + parts[i].start;
568
Patrick Delaunay7840c4a2015-11-17 11:36:52 +0100569 /* bootable */
570 if (found_key(tok, "bootable"))
Heinrich Schuchardt59a860d2020-03-19 13:49:34 +0100571 parts[i].bootable = PART_BOOTABLE;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100572 }
573
574 *parts_count = p_count;
575 *partitions = parts;
576 free(str);
577
578 return 0;
579err:
580 free(str);
581 free(*str_disk_guid);
582 free(parts);
583
584 return errno;
585}
586
Philippe Reynes51215292022-04-22 17:46:49 +0200587static int gpt_repair(struct blk_desc *blk_dev_desc)
588{
589 int ret = 0;
590
591 ret = gpt_repair_headers(blk_dev_desc);
592
593 return ret;
594}
595
Simon Glasse3394752016-02-29 15:25:34 -0700596static int gpt_default(struct blk_desc *blk_dev_desc, const char *str_part)
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100597{
598 int ret;
599 char *str_disk_guid;
600 u8 part_count = 0;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600601 struct disk_partition *partitions = NULL;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100602
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100603 /* fill partitions */
Egbert Eichbfd4a712013-10-04 18:53:04 +0200604 ret = set_gpt_info(blk_dev_desc, str_part,
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100605 &str_disk_guid, &partitions, &part_count);
606 if (ret) {
607 if (ret == -1)
608 printf("No partition list provided\n");
609 if (ret == -2)
610 printf("Missing disk guid\n");
611 if ((ret == -3) || (ret == -4))
612 printf("Partition list incomplete\n");
613 return -1;
614 }
615
616 /* save partitions layout to disk */
Rob Herring008ab492015-01-26 09:43:15 -0600617 ret = gpt_restore(blk_dev_desc, str_disk_guid, partitions, part_count);
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100618 free(str_disk_guid);
619 free(partitions);
620
Lukasz Majewski309691b2015-11-20 08:06:17 +0100621 return ret;
622}
623
Simon Glasse3394752016-02-29 15:25:34 -0700624static int gpt_verify(struct blk_desc *blk_dev_desc, const char *str_part)
Lukasz Majewski309691b2015-11-20 08:06:17 +0100625{
626 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1,
627 blk_dev_desc->blksz);
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600628 struct disk_partition *partitions = NULL;
Lukasz Majewski309691b2015-11-20 08:06:17 +0100629 gpt_entry *gpt_pte = NULL;
630 char *str_disk_guid;
631 u8 part_count = 0;
632 int ret = 0;
633
634 /* fill partitions */
635 ret = set_gpt_info(blk_dev_desc, str_part,
636 &str_disk_guid, &partitions, &part_count);
637 if (ret) {
638 if (ret == -1) {
639 printf("No partition list provided - only basic check\n");
640 ret = gpt_verify_headers(blk_dev_desc, gpt_head,
641 &gpt_pte);
642 goto out;
643 }
644 if (ret == -2)
645 printf("Missing disk guid\n");
646 if ((ret == -3) || (ret == -4))
647 printf("Partition list incomplete\n");
648 return -1;
649 }
650
651 /* Check partition layout with provided pattern */
652 ret = gpt_verify_partitions(blk_dev_desc, partitions, part_count,
653 gpt_head, &gpt_pte);
654 free(str_disk_guid);
655 free(partitions);
656 out:
657 free(gpt_pte);
Rob Herring008ab492015-01-26 09:43:15 -0600658 return ret;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100659}
660
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530661/**
662 * gpt_enumerate() - Enumerate partition names into environment variable.
663 *
664 * Enumerate partition names. Partition names are stored in gpt_partition_list
665 * environment variable. Each partition name is delimited by space.
666 *
667 * @desc: block device descriptor
668 *
669 * @Return: '0' on success and -ve error on failure
670 */
671static int gpt_enumerate(struct blk_desc *desc)
672{
673 struct part_driver *first_drv, *part_drv;
674 int str_len = 0, tmp_len;
675 char part_list[2048];
676 int n_drvs;
677 char *ptr;
678
679 part_list[0] = 0;
680 n_drvs = part_driver_get_count();
681 if (!n_drvs) {
682 printf("Failed to get partition driver count\n");
683 return -ENOENT;
684 }
685
686 first_drv = part_driver_get_first();
687 for (part_drv = first_drv; part_drv != first_drv + n_drvs; part_drv++) {
688 struct disk_partition pinfo;
689 int ret;
690 int i;
691
Heinrich Schuchardt16e48de2023-08-26 03:53:42 +0200692 if (part_drv->test(desc))
693 continue;
694
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530695 for (i = 1; i < part_drv->max_entries; i++) {
696 ret = part_drv->get_info(desc, i, &pinfo);
Heinrich Schuchardt16e48de2023-08-26 03:53:42 +0200697 if (ret)
698 continue;
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530699
700 ptr = &part_list[str_len];
701 tmp_len = strlen((const char *)pinfo.name);
702 str_len += tmp_len;
703 /* +1 for space */
704 str_len++;
705 if (str_len > sizeof(part_list)) {
706 printf("Error insufficient memory\n");
707 return -ENOMEM;
708 }
709 strcpy(ptr, (const char *)pinfo.name);
710 /* One byte for space(" ") delimiter */
711 ptr[tmp_len] = ' ';
712 }
Heinrich Schuchardt16e48de2023-08-26 03:53:42 +0200713 if (*part_list)
714 part_list[strlen(part_list) - 1] = 0;
715 break;
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530716 }
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530717 debug("setenv gpt_partition_list %s\n", part_list);
718
719 return env_set("gpt_partition_list", part_list);
720}
721
722/**
723 * gpt_setenv_part_variables() - setup partition environmental variables
724 *
725 * Setup the gpt_partition_name, gpt_partition_entry, gpt_partition_addr
726 * and gpt_partition_size environment variables.
727 *
728 * @pinfo: pointer to disk partition
729 * @i: partition entry
730 *
731 * @Return: '0' on success and -ENOENT on failure
732 */
733static int gpt_setenv_part_variables(struct disk_partition *pinfo, int i)
734{
735 int ret;
736
737 ret = env_set_hex("gpt_partition_addr", pinfo->start);
738 if (ret)
739 goto fail;
740
741 ret = env_set_hex("gpt_partition_size", pinfo->size);
742 if (ret)
743 goto fail;
744
Heinrich Schuchardt7e879da2023-08-26 03:53:43 +0200745 ret = env_set_hex("gpt_partition_entry", i);
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530746 if (ret)
747 goto fail;
748
749 ret = env_set("gpt_partition_name", (const char *)pinfo->name);
750 if (ret)
751 goto fail;
752
753 return 0;
754
755fail:
756 return -ENOENT;
757}
758
759/**
760 * gpt_setenv() - Dynamically setup environment variables.
761 *
762 * Dynamically setup environment variables for name, index, offset and size
763 * for partition in GPT table after running "gpt setenv" for a partition name.
764 *
765 * @desc: block device descriptor
766 * @name: partition name
767 *
768 * @Return: '0' on success and -ve err on failure
769 */
770static int gpt_setenv(struct blk_desc *desc, const char *name)
771{
772 struct part_driver *first_drv, *part_drv;
773 int n_drvs;
774 int ret = -1;
775
776 n_drvs = part_driver_get_count();
777 if (!n_drvs) {
778 printf("Failed to get partition driver count\n");
779 goto fail;
780 }
781
782 first_drv = part_driver_get_first();
783 for (part_drv = first_drv; part_drv != first_drv + n_drvs; part_drv++) {
784 struct disk_partition pinfo;
785 int i;
786
787 for (i = 1; i < part_drv->max_entries; i++) {
788 ret = part_drv->get_info(desc, i, &pinfo);
Heinrich Schuchardt441dc542023-08-26 03:53:41 +0200789 if (ret)
790 continue;
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530791
792 if (!strcmp(name, (const char *)pinfo.name)) {
793 /* match found, setup environment variables */
794 ret = gpt_setenv_part_variables(&pinfo, i);
795 if (ret)
796 goto fail;
797
798 return 0;
799 }
800 }
801 }
802
803fail:
804 return ret;
805}
806
Alison Chaikene4222582017-06-25 16:43:23 -0700807static int do_disk_guid(struct blk_desc *dev_desc, char * const namestr)
808{
809 int ret;
810 char disk_guid[UUID_STR_LEN + 1];
811
812 ret = get_disk_guid(dev_desc, disk_guid);
813 if (ret < 0)
814 return CMD_RET_FAILURE;
815
816 if (namestr)
Simon Glass6a38e412017-08-03 12:22:09 -0600817 env_set(namestr, disk_guid);
Alison Chaikene4222582017-06-25 16:43:23 -0700818 else
819 printf("%s\n", disk_guid);
820
821 return ret;
822}
823
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700824#ifdef CONFIG_CMD_GPT_RENAME
825static int do_rename_gpt_parts(struct blk_desc *dev_desc, char *subcomm,
826 char *name1, char *name2)
827{
828 struct list_head *pos;
829 struct disk_part *curr;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600830 struct disk_partition *new_partitions = NULL;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700831 char disk_guid[UUID_STR_LEN + 1];
Tom Rinib0d3c0b2020-01-21 11:53:38 -0500832 char *partitions_list, *str_disk_guid = NULL;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700833 u8 part_count = 0;
834 int partlistlen, ret, numparts = 0, partnum, i = 1, ctr1 = 0, ctr2 = 0;
835
836 if ((subcomm == NULL) || (name1 == NULL) || (name2 == NULL) ||
837 (strcmp(subcomm, "swap") && (strcmp(subcomm, "rename"))))
838 return -EINVAL;
839
840 ret = get_disk_guid(dev_desc, disk_guid);
841 if (ret < 0)
842 return ret;
Alison Chaiken7e805c02017-09-26 07:42:28 -0700843 /*
844 * Allocates disk_partitions, requiring matching call to del_gpt_info()
845 * if successful.
846 */
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700847 numparts = get_gpt_info(dev_desc);
848 if (numparts <= 0)
849 return numparts ? numparts : -ENODEV;
850
851 partlistlen = calc_parts_list_len(numparts);
852 partitions_list = malloc(partlistlen);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700853 if (!partitions_list) {
854 del_gpt_info();
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700855 return -ENOMEM;
Alison Chaiken7e805c02017-09-26 07:42:28 -0700856 }
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700857 memset(partitions_list, '\0', partlistlen);
858
859 ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700860 if (ret < 0) {
861 free(partitions_list);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700862 return ret;
Alison Chaiken7e805c02017-09-26 07:42:28 -0700863 }
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700864 /*
865 * Uncomment the following line to print a string that 'gpt write'
866 * or 'gpt verify' will accept as input.
867 */
868 debug("OLD partitions_list is %s with %u chars\n", partitions_list,
869 (unsigned)strlen(partitions_list));
870
Alison Chaiken7e805c02017-09-26 07:42:28 -0700871 /* set_gpt_info allocates new_partitions and str_disk_guid */
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700872 ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid,
873 &new_partitions, &part_count);
Tom Rinib0d3c0b2020-01-21 11:53:38 -0500874 if (ret < 0)
875 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700876
877 if (!strcmp(subcomm, "swap")) {
878 if ((strlen(name1) > PART_NAME_LEN) || (strlen(name2) > PART_NAME_LEN)) {
879 printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700880 ret = -EINVAL;
881 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700882 }
883 list_for_each(pos, &disk_partitions) {
884 curr = list_entry(pos, struct disk_part, list);
885 if (!strcmp((char *)curr->gpt_part_info.name, name1)) {
886 strcpy((char *)curr->gpt_part_info.name, name2);
887 ctr1++;
888 } else if (!strcmp((char *)curr->gpt_part_info.name, name2)) {
889 strcpy((char *)curr->gpt_part_info.name, name1);
890 ctr2++;
891 }
892 }
893 if ((ctr1 + ctr2 < 2) || (ctr1 != ctr2)) {
894 printf("Cannot swap partition names except in pairs.\n");
Alison Chaiken7e805c02017-09-26 07:42:28 -0700895 ret = -EINVAL;
896 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700897 }
898 } else { /* rename */
899 if (strlen(name2) > PART_NAME_LEN) {
900 printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700901 ret = -EINVAL;
902 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700903 }
904 partnum = (int)simple_strtol(name1, NULL, 10);
905 if ((partnum < 0) || (partnum > numparts)) {
906 printf("Illegal partition number %s\n", name1);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700907 ret = -EINVAL;
908 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700909 }
910 ret = part_get_info(dev_desc, partnum, new_partitions);
911 if (ret < 0)
Alison Chaiken7e805c02017-09-26 07:42:28 -0700912 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700913
914 /* U-Boot partition numbering starts at 1 */
915 list_for_each(pos, &disk_partitions) {
916 curr = list_entry(pos, struct disk_part, list);
917 if (i == partnum) {
918 strcpy((char *)curr->gpt_part_info.name, name2);
919 break;
920 }
921 i++;
922 }
923 }
924
925 ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list);
926 if (ret < 0)
Alison Chaiken7e805c02017-09-26 07:42:28 -0700927 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700928 debug("NEW partitions_list is %s with %u chars\n", partitions_list,
929 (unsigned)strlen(partitions_list));
930
931 ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid,
932 &new_partitions, &part_count);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700933 /*
934 * Even though valid pointers are here passed into set_gpt_info(),
935 * it mallocs again, and there's no way to tell which failed.
936 */
Tom Rinib0d3c0b2020-01-21 11:53:38 -0500937 if (ret < 0)
938 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700939
940 debug("Writing new partition table\n");
941 ret = gpt_restore(dev_desc, disk_guid, new_partitions, numparts);
942 if (ret < 0) {
943 printf("Writing new partition table failed\n");
Alison Chaiken7e805c02017-09-26 07:42:28 -0700944 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700945 }
946
947 debug("Reading back new partition table\n");
Alison Chaiken7e805c02017-09-26 07:42:28 -0700948 /*
949 * Empty the existing disk_partitions list, as otherwise the memory in
950 * the original list is unreachable.
951 */
952 del_gpt_info();
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700953 numparts = get_gpt_info(dev_desc);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700954 if (numparts <= 0) {
955 ret = numparts ? numparts : -ENODEV;
956 goto out;
957 }
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700958 printf("new partition table with %d partitions is:\n", numparts);
959 print_gpt_info();
Alison Chaiken7e805c02017-09-26 07:42:28 -0700960 out:
Tom Rinib0d3c0b2020-01-21 11:53:38 -0500961 del_gpt_info();
962#ifdef CONFIG_RANDOM_UUID
Heinrich Schuchardt443806d2020-04-28 21:40:13 +0200963 free(str_disk_guid);
Tom Rinib0d3c0b2020-01-21 11:53:38 -0500964#endif
Heinrich Schuchardt443806d2020-04-28 21:40:13 +0200965 free(new_partitions);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700966 free(partitions_list);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700967 return ret;
968}
969#endif
970
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100971/**
972 * do_gpt(): Perform GPT operations
973 *
974 * @param cmdtp - command name
975 * @param flag
976 * @param argc
977 * @param argv
978 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100979 * Return: zero on success; otherwise error
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100980 */
Simon Glassed38aef2020-05-10 11:40:03 -0600981static int do_gpt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100982{
983 int ret = CMD_RET_SUCCESS;
984 int dev = 0;
Egbert Eichbfd4a712013-10-04 18:53:04 +0200985 char *ep;
Simon Glasse3394752016-02-29 15:25:34 -0700986 struct blk_desc *blk_dev_desc = NULL;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100987
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700988#ifndef CONFIG_CMD_GPT_RENAME
Lukasz Majewski309691b2015-11-20 08:06:17 +0100989 if (argc < 4 || argc > 5)
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700990#else
991 if (argc < 4 || argc > 6)
992#endif
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100993 return CMD_RET_USAGE;
994
Simon Glassff9b9032021-07-24 09:03:30 -0600995 dev = (int)dectoul(argv[3], &ep);
Lukasz Majewski309691b2015-11-20 08:06:17 +0100996 if (!ep || ep[0] != '\0') {
997 printf("'%s' is not a number\n", argv[3]);
998 return CMD_RET_USAGE;
999 }
Simon Glassbe1e9bb2016-02-29 15:25:42 -07001000 blk_dev_desc = blk_get_dev(argv[2], dev);
Lukasz Majewski309691b2015-11-20 08:06:17 +01001001 if (!blk_dev_desc) {
1002 printf("%s: %s dev %d NOT available\n",
1003 __func__, argv[2], dev);
1004 return CMD_RET_FAILURE;
1005 }
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +02001006
Philippe Reynes51215292022-04-22 17:46:49 +02001007 if (strcmp(argv[1], "repair") == 0) {
1008 printf("Repairing GPT: ");
1009 ret = gpt_repair(blk_dev_desc);
1010 } else if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
Lukasz Majewski309691b2015-11-20 08:06:17 +01001011 printf("Writing GPT: ");
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +02001012 ret = gpt_default(blk_dev_desc, argv[4]);
Lukasz Majewski309691b2015-11-20 08:06:17 +01001013 } else if ((strcmp(argv[1], "verify") == 0)) {
1014 ret = gpt_verify(blk_dev_desc, argv[4]);
1015 printf("Verify GPT: ");
Corneliu Doban0c6776c2020-11-09 11:49:16 +05301016 } else if ((strcmp(argv[1], "setenv") == 0)) {
1017 ret = gpt_setenv(blk_dev_desc, argv[4]);
1018 } else if ((strcmp(argv[1], "enumerate") == 0)) {
1019 ret = gpt_enumerate(blk_dev_desc);
Alison Chaikene4222582017-06-25 16:43:23 -07001020 } else if (strcmp(argv[1], "guid") == 0) {
1021 ret = do_disk_guid(blk_dev_desc, argv[4]);
Alison Chaikenf04d4b52017-07-04 11:18:50 -07001022#ifdef CONFIG_CMD_GPT_RENAME
1023 } else if (strcmp(argv[1], "read") == 0) {
Farhan Ali078d0222021-02-26 10:17:33 -08001024 ret = do_get_gpt_info(blk_dev_desc, (argc == 5) ? argv[4] : NULL);
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001025 } else if ((strcmp(argv[1], "swap") == 0) ||
1026 (strcmp(argv[1], "rename") == 0)) {
1027 ret = do_rename_gpt_parts(blk_dev_desc, argv[1], argv[4], argv[5]);
Alison Chaikenf04d4b52017-07-04 11:18:50 -07001028#endif
Piotr Wilczekcc00ea82012-12-11 11:09:47 +01001029 } else {
1030 return CMD_RET_USAGE;
1031 }
Lukasz Majewski309691b2015-11-20 08:06:17 +01001032
1033 if (ret) {
1034 printf("error!\n");
1035 return CMD_RET_FAILURE;
1036 }
1037
1038 printf("success!\n");
1039 return CMD_RET_SUCCESS;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +01001040}
1041
1042U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
1043 "GUID Partition Table",
Robert P. J. Daye3f2c2e2013-08-21 11:39:19 -04001044 "<command> <interface> <dev> <partitions_list>\n"
Lukasz Majewskicc5f3d72015-11-13 07:42:10 +01001045 " - GUID partition table restoration and validity check\n"
1046 " Restore or verify GPT information on a device connected\n"
Piotr Wilczekcc00ea82012-12-11 11:09:47 +01001047 " to interface\n"
Lukasz Majewskicc5f3d72015-11-13 07:42:10 +01001048 " Example usage:\n"
Philippe Reynes51215292022-04-22 17:46:49 +02001049 " gpt repair mmc 0\n"
1050 " - repair the GPT on the device\n"
Lukasz Majewskicc5f3d72015-11-13 07:42:10 +01001051 " gpt write mmc 0 $partitions\n"
Corneliu Doban0c6776c2020-11-09 11:49:16 +05301052 " - write the GPT to device\n"
Lukasz Majewskicc5f3d72015-11-13 07:42:10 +01001053 " gpt verify mmc 0 $partitions\n"
Corneliu Doban0c6776c2020-11-09 11:49:16 +05301054 " - verify the GPT on device against $partitions\n"
1055 " gpt setenv mmc 0 $name\n"
1056 " - setup environment variables for partition $name:\n"
1057 " gpt_partition_addr, gpt_partition_size,\n"
1058 " gpt_partition_name, gpt_partition_entry\n"
1059 " gpt enumerate mmc 0\n"
1060 " - store list of partitions to gpt_partition_list environment variable\n"
Eugeniu Roscafa8acbc2019-04-30 04:53:46 +02001061 " gpt guid <interface> <dev>\n"
Alison Chaikene4222582017-06-25 16:43:23 -07001062 " - print disk GUID\n"
Eugeniu Roscafa8acbc2019-04-30 04:53:46 +02001063 " gpt guid <interface> <dev> <varname>\n"
Alison Chaikene4222582017-06-25 16:43:23 -07001064 " - set environment variable to disk GUID\n"
1065 " Example usage:\n"
1066 " gpt guid mmc 0\n"
1067 " gpt guid mmc 0 varname\n"
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001068#ifdef CONFIG_CMD_GPT_RENAME
1069 "gpt partition renaming commands:\n"
Farhan Ali078d0222021-02-26 10:17:33 -08001070 " gpt read <interface> <dev> [<varname>]\n"
Eugeniu Roscafa8acbc2019-04-30 04:53:46 +02001071 " - read GPT into a data structure for manipulation\n"
Farhan Ali078d0222021-02-26 10:17:33 -08001072 " - read GPT partitions into environment variable\n"
Eugeniu Roscafa8acbc2019-04-30 04:53:46 +02001073 " gpt swap <interface> <dev> <name1> <name2>\n"
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001074 " - change all partitions named name1 to name2\n"
1075 " and vice-versa\n"
Eugeniu Roscafa8acbc2019-04-30 04:53:46 +02001076 " gpt rename <interface> <dev> <part> <name>\n"
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001077 " - rename the specified partition\n"
1078 " Example usage:\n"
1079 " gpt swap mmc 0 foo bar\n"
1080 " gpt rename mmc 0 3 foo\n"
1081#endif
Piotr Wilczekcc00ea82012-12-11 11:09:47 +01001082);