blob: fe9e06681ce2948de6ee3771437f82b6c4705cfa [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;
214#ifdef CONFIG_PARTITION_UUIDS
215 strncpy(newpart->gpt_part_info.uuid, (const char *)info->uuid,
216 UUID_STR_LEN);
217 /* UUID_STR_LEN is correct, as uuid[]'s length is UUID_STR_LEN+1 chars */
218 newpart->gpt_part_info.uuid[UUID_STR_LEN] = '\0';
219#endif
220 newpart->partnum = partnum;
221
222 return newpart;
223}
224
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700225static void prettyprint_part_size(char *sizestr, lbaint_t partsize,
226 lbaint_t blksize)
227{
228 unsigned long long partbytes, partmegabytes;
229
230 partbytes = partsize * blksize;
231 partmegabytes = lldiv(partbytes, SZ_1M);
232 snprintf(sizestr, 16, "%lluMiB", partmegabytes);
233}
234
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700235static void print_gpt_info(void)
236{
237 struct list_head *pos;
238 struct disk_part *curr;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700239 char partstartstr[16];
240 char partsizestr[16];
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700241
242 list_for_each(pos, &disk_partitions) {
243 curr = list_entry(pos, struct disk_part, list);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700244 prettyprint_part_size(partstartstr, curr->gpt_part_info.start,
245 curr->gpt_part_info.blksz);
246 prettyprint_part_size(partsizestr, curr->gpt_part_info.size,
247 curr->gpt_part_info.blksz);
248
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700249 printf("Partition %d:\n", curr->partnum);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700250 printf("Start %s, size %s\n", partstartstr, partsizestr);
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700251 printf("Block size %lu, name %s\n", curr->gpt_part_info.blksz,
252 curr->gpt_part_info.name);
253 printf("Type %s, bootable %d\n", curr->gpt_part_info.type,
Heinrich Schuchardt59a860d2020-03-19 13:49:34 +0100254 curr->gpt_part_info.bootable & PART_BOOTABLE);
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700255#ifdef CONFIG_PARTITION_UUIDS
256 printf("UUID %s\n", curr->gpt_part_info.uuid);
257#endif
258 printf("\n");
259 }
260}
261
262/*
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700263 * create the string that upstream 'gpt write' command will accept as an
264 * argument
265 *
266 * From doc/README.gpt, Format of partitions layout:
267 * "uuid_disk=...;name=u-boot,size=60MiB,uuid=...;
268 * name=kernel,size=60MiB,uuid=...;"
269 * The fields 'name' and 'size' are mandatory for every partition.
270 * The field 'start' is optional. The fields 'uuid' and 'uuid_disk'
271 * are optional if CONFIG_RANDOM_UUID is enabled.
272 */
273static int create_gpt_partitions_list(int numparts, const char *guid,
274 char *partitions_list)
275{
276 struct list_head *pos;
277 struct disk_part *curr;
278 char partstr[PART_NAME_LEN + 1];
279
280 if (!partitions_list)
281 return -EINVAL;
282
283 strcpy(partitions_list, "uuid_disk=");
284 strncat(partitions_list, guid, UUID_STR_LEN + 1);
285 strcat(partitions_list, ";");
286
287 list_for_each(pos, &disk_partitions) {
288 curr = list_entry(pos, struct disk_part, list);
289 strcat(partitions_list, "name=");
290 strncat(partitions_list, (const char *)curr->gpt_part_info.name,
291 PART_NAME_LEN + 1);
Patrick Delaunay309a6fa2017-10-18 15:11:08 +0200292 sprintf(partstr, ",start=0x%llx",
293 (unsigned long long)curr->gpt_part_info.start *
294 curr->gpt_part_info.blksz);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700295 /* one extra byte for NULL */
296 strncat(partitions_list, partstr, PART_NAME_LEN + 1);
Patrick Delaunay309a6fa2017-10-18 15:11:08 +0200297 sprintf(partstr, ",size=0x%llx",
298 (unsigned long long)curr->gpt_part_info.size *
299 curr->gpt_part_info.blksz);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700300 strncat(partitions_list, partstr, PART_NAME_LEN + 1);
301
302 strcat(partitions_list, ",uuid=");
303 strncat(partitions_list, curr->gpt_part_info.uuid,
304 UUID_STR_LEN + 1);
305 strcat(partitions_list, ";");
306 }
307 return 0;
308}
309
310/*
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700311 * read partition info into disk_partitions list where
312 * it can be printed or modified
313 */
314static int get_gpt_info(struct blk_desc *dev_desc)
315{
316 /* start partition numbering at 1, as U-Boot does */
317 int valid_parts = 0, p, ret;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600318 struct disk_partition info;
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700319 struct disk_part *new_disk_part;
320
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700321 /*
322 * Always re-read partition info from device, in case
323 * it has changed
324 */
325 INIT_LIST_HEAD(&disk_partitions);
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700326
327 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
328 ret = part_get_info(dev_desc, p, &info);
329 if (ret)
330 continue;
331
332 /* Add 1 here because counter is zero-based but p1 is
333 the first partition */
334 new_disk_part = allocate_disk_part(&info, valid_parts+1);
335 if (IS_ERR(new_disk_part))
336 goto out;
337
338 list_add_tail(&new_disk_part->list, &disk_partitions);
339 valid_parts++;
340 }
341 if (valid_parts == 0) {
342 printf("** No valid partitions found **\n");
343 goto out;
344 }
345 return valid_parts;
346 out:
347 if (valid_parts >= 1)
348 del_gpt_info();
349 return -ENODEV;
350}
351
352/* a wrapper to test get_gpt_info */
Farhan Ali078d0222021-02-26 10:17:33 -0800353static int do_get_gpt_info(struct blk_desc *dev_desc, char * const namestr)
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700354{
Farhan Ali078d0222021-02-26 10:17:33 -0800355 int numparts;
356
357 numparts = get_gpt_info(dev_desc);
358
359 if (numparts > 0) {
360 if (namestr) {
361 char disk_guid[UUID_STR_LEN + 1];
362 char *partitions_list;
363 int partlistlen;
364 int ret = -1;
365
366 ret = get_disk_guid(dev_desc, disk_guid);
367 if (ret < 0)
368 return ret;
369
370 partlistlen = calc_parts_list_len(numparts);
371 partitions_list = malloc(partlistlen);
372 if (!partitions_list) {
373 del_gpt_info();
374 return -ENOMEM;
375 }
376 memset(partitions_list, '\0', partlistlen);
377
378 ret = create_gpt_partitions_list(numparts, disk_guid,
379 partitions_list);
380 if (ret < 0)
381 printf("Error: Could not create partition list string!\n");
382 else
383 env_set(namestr, partitions_list);
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700384
Farhan Ali078d0222021-02-26 10:17:33 -0800385 free(partitions_list);
386 } else {
387 print_gpt_info();
388 }
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700389 del_gpt_info();
390 return 0;
391 }
Farhan Ali078d0222021-02-26 10:17:33 -0800392 return numparts;
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700393}
394#endif
395
Patrick Delaunay7840c4a2015-11-17 11:36:52 +0100396/**
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100397 * set_gpt_info(): Fill partition information from string
398 * function allocates memory, remember to free!
399 *
400 * @param dev_desc - pointer block device descriptor
401 * @param str_part - pointer to string with partition information
402 * @param str_disk_guid - pointer to pointer to allocated string with disk guid
403 * @param partitions - pointer to pointer to allocated partitions array
404 * @param parts_count - number of partitions
405 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100406 * Return: - zero on success, otherwise error
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100407 *
408 */
Simon Glasse3394752016-02-29 15:25:34 -0700409static int set_gpt_info(struct blk_desc *dev_desc,
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100410 const char *str_part,
411 char **str_disk_guid,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600412 struct disk_partition **partitions,
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100413 u8 *parts_count)
414{
415 char *tok, *str, *s;
416 int i;
417 char *val, *p;
418 int p_count;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600419 struct disk_partition *parts;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100420 int errno = 0;
Piotr Wilczek36c06b12013-01-27 22:59:25 +0000421 uint64_t size_ll, start_ll;
Michael Trimarchi853445f2016-06-08 10:18:16 +0200422 lbaint_t offset = 0;
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700423 int max_str_part = calc_parts_list_len(MAX_SEARCH_PARTITIONS);
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100424
Egbert Eichbfd4a712013-10-04 18:53:04 +0200425 debug("%s: lba num: 0x%x %d\n", __func__,
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100426 (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba);
427
428 if (str_part == NULL)
429 return -1;
430
431 str = strdup(str_part);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700432 if (str == NULL)
433 return -ENOMEM;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100434
435 /* extract disk guid */
436 s = str;
Rob Herringd9761972015-01-26 09:44:18 -0600437 val = extract_val(str, "uuid_disk");
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100438 if (!val) {
Rob Herringd9761972015-01-26 09:44:18 -0600439#ifdef CONFIG_RANDOM_UUID
440 *str_disk_guid = malloc(UUID_STR_LEN + 1);
Tom Rinia29829e2017-10-07 11:27:59 -0400441 if (*str_disk_guid == NULL)
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700442 return -ENOMEM;
Rob Herringd9761972015-01-26 09:44:18 -0600443 gen_rand_uuid_str(*str_disk_guid, UUID_STR_FORMAT_STD);
444#else
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100445 free(str);
446 return -2;
Rob Herringd9761972015-01-26 09:44:18 -0600447#endif
448 } else {
449 val = strsep(&val, ";");
450 if (extract_env(val, &p))
451 p = val;
452 *str_disk_guid = strdup(p);
453 free(val);
454 /* Move s to first partition */
455 strsep(&s, ";");
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100456 }
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700457 if (s == NULL) {
458 printf("Error: is the partitions string NULL-terminated?\n");
459 return -EINVAL;
460 }
461 if (strnlen(s, max_str_part) == 0)
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100462 return -3;
463
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700464 i = strnlen(s, max_str_part) - 1;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100465 if (s[i] == ';')
466 s[i] = '\0';
467
468 /* calculate expected number of partitions */
469 p_count = 1;
470 p = s;
471 while (*p) {
472 if (*p++ == ';')
473 p_count++;
474 }
475
476 /* allocate memory for partitions */
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600477 parts = calloc(sizeof(struct disk_partition), p_count);
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700478 if (parts == NULL)
479 return -ENOMEM;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100480
Robert P. J. Daye3f2c2e2013-08-21 11:39:19 -0400481 /* retrieve partitions data from string */
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100482 for (i = 0; i < p_count; i++) {
483 tok = strsep(&s, ";");
484
485 if (tok == NULL)
486 break;
487
488 /* uuid */
489 val = extract_val(tok, "uuid");
Rob Herringd9761972015-01-26 09:44:18 -0600490 if (!val) {
491 /* 'uuid' is optional if random uuid's are enabled */
492#ifdef CONFIG_RANDOM_UUID
493 gen_rand_uuid_str(parts[i].uuid, UUID_STR_FORMAT_STD);
494#else
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100495 errno = -4;
496 goto err;
Rob Herringd9761972015-01-26 09:44:18 -0600497#endif
498 } else {
499 if (extract_env(val, &p))
500 p = val;
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700501 if (strnlen(p, max_str_part) >= sizeof(parts[i].uuid)) {
Rob Herringd9761972015-01-26 09:44:18 -0600502 printf("Wrong uuid format for partition %d\n", i);
503 errno = -4;
504 goto err;
505 }
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700506 strncpy((char *)parts[i].uuid, p, max_str_part);
Rob Herringd9761972015-01-26 09:44:18 -0600507 free(val);
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100508 }
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100509#ifdef CONFIG_PARTITION_TYPE_GUID
510 /* guid */
511 val = extract_val(tok, "type");
512 if (val) {
513 /* 'type' is optional */
514 if (extract_env(val, &p))
515 p = val;
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700516 if (strnlen(p, max_str_part) >= sizeof(parts[i].type_guid)) {
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100517 printf("Wrong type guid format for partition %d\n",
518 i);
519 errno = -4;
520 goto err;
521 }
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700522 strncpy((char *)parts[i].type_guid, p, max_str_part);
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100523 free(val);
524 }
525#endif
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100526 /* name */
527 val = extract_val(tok, "name");
528 if (!val) { /* name is mandatory */
529 errno = -4;
530 goto err;
531 }
532 if (extract_env(val, &p))
533 p = val;
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700534 if (strnlen(p, max_str_part) >= sizeof(parts[i].name)) {
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100535 errno = -4;
536 goto err;
537 }
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700538 strncpy((char *)parts[i].name, p, max_str_part);
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100539 free(val);
540
541 /* size */
542 val = extract_val(tok, "size");
543 if (!val) { /* 'size' is mandatory */
544 errno = -4;
545 goto err;
546 }
547 if (extract_env(val, &p))
548 p = val;
Michael Trimarchi853445f2016-06-08 10:18:16 +0200549 if ((strcmp(p, "-") == 0)) {
Kever Yangf1d070d2016-07-29 11:12:18 +0800550 /* Let part efi module to auto extend the size */
551 parts[i].size = 0;
Michael Trimarchi853445f2016-06-08 10:18:16 +0200552 } else {
553 size_ll = ustrtoull(p, &p, 0);
554 parts[i].size = lldiv(size_ll, dev_desc->blksz);
555 }
556
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100557 free(val);
558
559 /* start address */
560 val = extract_val(tok, "start");
561 if (val) { /* start address is optional */
562 if (extract_env(val, &p))
563 p = val;
Piotr Wilczek36c06b12013-01-27 22:59:25 +0000564 start_ll = ustrtoull(p, &p, 0);
565 parts[i].start = lldiv(start_ll, dev_desc->blksz);
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100566 free(val);
567 }
Patrick Delaunay7840c4a2015-11-17 11:36:52 +0100568
Michael Trimarchi853445f2016-06-08 10:18:16 +0200569 offset += parts[i].size + parts[i].start;
570
Patrick Delaunay7840c4a2015-11-17 11:36:52 +0100571 /* bootable */
572 if (found_key(tok, "bootable"))
Heinrich Schuchardt59a860d2020-03-19 13:49:34 +0100573 parts[i].bootable = PART_BOOTABLE;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100574 }
575
576 *parts_count = p_count;
577 *partitions = parts;
578 free(str);
579
580 return 0;
581err:
582 free(str);
583 free(*str_disk_guid);
584 free(parts);
585
586 return errno;
587}
588
Philippe Reynes51215292022-04-22 17:46:49 +0200589static int gpt_repair(struct blk_desc *blk_dev_desc)
590{
591 int ret = 0;
592
593 ret = gpt_repair_headers(blk_dev_desc);
594
595 return ret;
596}
597
Simon Glasse3394752016-02-29 15:25:34 -0700598static int gpt_default(struct blk_desc *blk_dev_desc, const char *str_part)
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100599{
600 int ret;
601 char *str_disk_guid;
602 u8 part_count = 0;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600603 struct disk_partition *partitions = NULL;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100604
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100605 /* fill partitions */
Egbert Eichbfd4a712013-10-04 18:53:04 +0200606 ret = set_gpt_info(blk_dev_desc, str_part,
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100607 &str_disk_guid, &partitions, &part_count);
608 if (ret) {
609 if (ret == -1)
610 printf("No partition list provided\n");
611 if (ret == -2)
612 printf("Missing disk guid\n");
613 if ((ret == -3) || (ret == -4))
614 printf("Partition list incomplete\n");
615 return -1;
616 }
617
618 /* save partitions layout to disk */
Rob Herring008ab492015-01-26 09:43:15 -0600619 ret = gpt_restore(blk_dev_desc, str_disk_guid, partitions, part_count);
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100620 free(str_disk_guid);
621 free(partitions);
622
Lukasz Majewski309691b2015-11-20 08:06:17 +0100623 return ret;
624}
625
Simon Glasse3394752016-02-29 15:25:34 -0700626static int gpt_verify(struct blk_desc *blk_dev_desc, const char *str_part)
Lukasz Majewski309691b2015-11-20 08:06:17 +0100627{
628 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1,
629 blk_dev_desc->blksz);
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600630 struct disk_partition *partitions = NULL;
Lukasz Majewski309691b2015-11-20 08:06:17 +0100631 gpt_entry *gpt_pte = NULL;
632 char *str_disk_guid;
633 u8 part_count = 0;
634 int ret = 0;
635
636 /* fill partitions */
637 ret = set_gpt_info(blk_dev_desc, str_part,
638 &str_disk_guid, &partitions, &part_count);
639 if (ret) {
640 if (ret == -1) {
641 printf("No partition list provided - only basic check\n");
642 ret = gpt_verify_headers(blk_dev_desc, gpt_head,
643 &gpt_pte);
644 goto out;
645 }
646 if (ret == -2)
647 printf("Missing disk guid\n");
648 if ((ret == -3) || (ret == -4))
649 printf("Partition list incomplete\n");
650 return -1;
651 }
652
653 /* Check partition layout with provided pattern */
654 ret = gpt_verify_partitions(blk_dev_desc, partitions, part_count,
655 gpt_head, &gpt_pte);
656 free(str_disk_guid);
657 free(partitions);
658 out:
659 free(gpt_pte);
Rob Herring008ab492015-01-26 09:43:15 -0600660 return ret;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100661}
662
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530663/**
664 * gpt_enumerate() - Enumerate partition names into environment variable.
665 *
666 * Enumerate partition names. Partition names are stored in gpt_partition_list
667 * environment variable. Each partition name is delimited by space.
668 *
669 * @desc: block device descriptor
670 *
671 * @Return: '0' on success and -ve error on failure
672 */
673static int gpt_enumerate(struct blk_desc *desc)
674{
675 struct part_driver *first_drv, *part_drv;
676 int str_len = 0, tmp_len;
677 char part_list[2048];
678 int n_drvs;
679 char *ptr;
680
681 part_list[0] = 0;
682 n_drvs = part_driver_get_count();
683 if (!n_drvs) {
684 printf("Failed to get partition driver count\n");
685 return -ENOENT;
686 }
687
688 first_drv = part_driver_get_first();
689 for (part_drv = first_drv; part_drv != first_drv + n_drvs; part_drv++) {
690 struct disk_partition pinfo;
691 int ret;
692 int i;
693
Heinrich Schuchardt16e48de2023-08-26 03:53:42 +0200694 if (part_drv->test(desc))
695 continue;
696
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530697 for (i = 1; i < part_drv->max_entries; i++) {
698 ret = part_drv->get_info(desc, i, &pinfo);
Heinrich Schuchardt16e48de2023-08-26 03:53:42 +0200699 if (ret)
700 continue;
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530701
702 ptr = &part_list[str_len];
703 tmp_len = strlen((const char *)pinfo.name);
704 str_len += tmp_len;
705 /* +1 for space */
706 str_len++;
707 if (str_len > sizeof(part_list)) {
708 printf("Error insufficient memory\n");
709 return -ENOMEM;
710 }
711 strcpy(ptr, (const char *)pinfo.name);
712 /* One byte for space(" ") delimiter */
713 ptr[tmp_len] = ' ';
714 }
Heinrich Schuchardt16e48de2023-08-26 03:53:42 +0200715 if (*part_list)
716 part_list[strlen(part_list) - 1] = 0;
717 break;
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530718 }
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530719 debug("setenv gpt_partition_list %s\n", part_list);
720
721 return env_set("gpt_partition_list", part_list);
722}
723
724/**
725 * gpt_setenv_part_variables() - setup partition environmental variables
726 *
727 * Setup the gpt_partition_name, gpt_partition_entry, gpt_partition_addr
728 * and gpt_partition_size environment variables.
729 *
730 * @pinfo: pointer to disk partition
731 * @i: partition entry
732 *
733 * @Return: '0' on success and -ENOENT on failure
734 */
735static int gpt_setenv_part_variables(struct disk_partition *pinfo, int i)
736{
737 int ret;
738
739 ret = env_set_hex("gpt_partition_addr", pinfo->start);
740 if (ret)
741 goto fail;
742
743 ret = env_set_hex("gpt_partition_size", pinfo->size);
744 if (ret)
745 goto fail;
746
Heinrich Schuchardt7e879da2023-08-26 03:53:43 +0200747 ret = env_set_hex("gpt_partition_entry", i);
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530748 if (ret)
749 goto fail;
750
751 ret = env_set("gpt_partition_name", (const char *)pinfo->name);
752 if (ret)
753 goto fail;
754
755 return 0;
756
757fail:
758 return -ENOENT;
759}
760
761/**
762 * gpt_setenv() - Dynamically setup environment variables.
763 *
764 * Dynamically setup environment variables for name, index, offset and size
765 * for partition in GPT table after running "gpt setenv" for a partition name.
766 *
767 * @desc: block device descriptor
768 * @name: partition name
769 *
770 * @Return: '0' on success and -ve err on failure
771 */
772static int gpt_setenv(struct blk_desc *desc, const char *name)
773{
774 struct part_driver *first_drv, *part_drv;
775 int n_drvs;
776 int ret = -1;
777
778 n_drvs = part_driver_get_count();
779 if (!n_drvs) {
780 printf("Failed to get partition driver count\n");
781 goto fail;
782 }
783
784 first_drv = part_driver_get_first();
785 for (part_drv = first_drv; part_drv != first_drv + n_drvs; part_drv++) {
786 struct disk_partition pinfo;
787 int i;
788
789 for (i = 1; i < part_drv->max_entries; i++) {
790 ret = part_drv->get_info(desc, i, &pinfo);
Heinrich Schuchardt441dc542023-08-26 03:53:41 +0200791 if (ret)
792 continue;
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530793
794 if (!strcmp(name, (const char *)pinfo.name)) {
795 /* match found, setup environment variables */
796 ret = gpt_setenv_part_variables(&pinfo, i);
797 if (ret)
798 goto fail;
799
800 return 0;
801 }
802 }
803 }
804
805fail:
806 return ret;
807}
808
Alison Chaikene4222582017-06-25 16:43:23 -0700809static int do_disk_guid(struct blk_desc *dev_desc, char * const namestr)
810{
811 int ret;
812 char disk_guid[UUID_STR_LEN + 1];
813
814 ret = get_disk_guid(dev_desc, disk_guid);
815 if (ret < 0)
816 return CMD_RET_FAILURE;
817
818 if (namestr)
Simon Glass6a38e412017-08-03 12:22:09 -0600819 env_set(namestr, disk_guid);
Alison Chaikene4222582017-06-25 16:43:23 -0700820 else
821 printf("%s\n", disk_guid);
822
823 return ret;
824}
825
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700826#ifdef CONFIG_CMD_GPT_RENAME
827static int do_rename_gpt_parts(struct blk_desc *dev_desc, char *subcomm,
828 char *name1, char *name2)
829{
830 struct list_head *pos;
831 struct disk_part *curr;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600832 struct disk_partition *new_partitions = NULL;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700833 char disk_guid[UUID_STR_LEN + 1];
Tom Rinib0d3c0b2020-01-21 11:53:38 -0500834 char *partitions_list, *str_disk_guid = NULL;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700835 u8 part_count = 0;
836 int partlistlen, ret, numparts = 0, partnum, i = 1, ctr1 = 0, ctr2 = 0;
837
838 if ((subcomm == NULL) || (name1 == NULL) || (name2 == NULL) ||
839 (strcmp(subcomm, "swap") && (strcmp(subcomm, "rename"))))
840 return -EINVAL;
841
842 ret = get_disk_guid(dev_desc, disk_guid);
843 if (ret < 0)
844 return ret;
Alison Chaiken7e805c02017-09-26 07:42:28 -0700845 /*
846 * Allocates disk_partitions, requiring matching call to del_gpt_info()
847 * if successful.
848 */
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700849 numparts = get_gpt_info(dev_desc);
850 if (numparts <= 0)
851 return numparts ? numparts : -ENODEV;
852
853 partlistlen = calc_parts_list_len(numparts);
854 partitions_list = malloc(partlistlen);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700855 if (!partitions_list) {
856 del_gpt_info();
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700857 return -ENOMEM;
Alison Chaiken7e805c02017-09-26 07:42:28 -0700858 }
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700859 memset(partitions_list, '\0', partlistlen);
860
861 ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700862 if (ret < 0) {
863 free(partitions_list);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700864 return ret;
Alison Chaiken7e805c02017-09-26 07:42:28 -0700865 }
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700866 /*
867 * Uncomment the following line to print a string that 'gpt write'
868 * or 'gpt verify' will accept as input.
869 */
870 debug("OLD partitions_list is %s with %u chars\n", partitions_list,
871 (unsigned)strlen(partitions_list));
872
Alison Chaiken7e805c02017-09-26 07:42:28 -0700873 /* set_gpt_info allocates new_partitions and str_disk_guid */
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700874 ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid,
875 &new_partitions, &part_count);
Tom Rinib0d3c0b2020-01-21 11:53:38 -0500876 if (ret < 0)
877 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700878
879 if (!strcmp(subcomm, "swap")) {
880 if ((strlen(name1) > PART_NAME_LEN) || (strlen(name2) > PART_NAME_LEN)) {
881 printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700882 ret = -EINVAL;
883 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700884 }
885 list_for_each(pos, &disk_partitions) {
886 curr = list_entry(pos, struct disk_part, list);
887 if (!strcmp((char *)curr->gpt_part_info.name, name1)) {
888 strcpy((char *)curr->gpt_part_info.name, name2);
889 ctr1++;
890 } else if (!strcmp((char *)curr->gpt_part_info.name, name2)) {
891 strcpy((char *)curr->gpt_part_info.name, name1);
892 ctr2++;
893 }
894 }
895 if ((ctr1 + ctr2 < 2) || (ctr1 != ctr2)) {
896 printf("Cannot swap partition names except in pairs.\n");
Alison Chaiken7e805c02017-09-26 07:42:28 -0700897 ret = -EINVAL;
898 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700899 }
900 } else { /* rename */
901 if (strlen(name2) > PART_NAME_LEN) {
902 printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700903 ret = -EINVAL;
904 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700905 }
906 partnum = (int)simple_strtol(name1, NULL, 10);
907 if ((partnum < 0) || (partnum > numparts)) {
908 printf("Illegal partition number %s\n", name1);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700909 ret = -EINVAL;
910 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700911 }
912 ret = part_get_info(dev_desc, partnum, new_partitions);
913 if (ret < 0)
Alison Chaiken7e805c02017-09-26 07:42:28 -0700914 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700915
916 /* U-Boot partition numbering starts at 1 */
917 list_for_each(pos, &disk_partitions) {
918 curr = list_entry(pos, struct disk_part, list);
919 if (i == partnum) {
920 strcpy((char *)curr->gpt_part_info.name, name2);
921 break;
922 }
923 i++;
924 }
925 }
926
927 ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list);
928 if (ret < 0)
Alison Chaiken7e805c02017-09-26 07:42:28 -0700929 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700930 debug("NEW partitions_list is %s with %u chars\n", partitions_list,
931 (unsigned)strlen(partitions_list));
932
933 ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid,
934 &new_partitions, &part_count);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700935 /*
936 * Even though valid pointers are here passed into set_gpt_info(),
937 * it mallocs again, and there's no way to tell which failed.
938 */
Tom Rinib0d3c0b2020-01-21 11:53:38 -0500939 if (ret < 0)
940 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700941
942 debug("Writing new partition table\n");
943 ret = gpt_restore(dev_desc, disk_guid, new_partitions, numparts);
944 if (ret < 0) {
945 printf("Writing new partition table failed\n");
Alison Chaiken7e805c02017-09-26 07:42:28 -0700946 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700947 }
948
949 debug("Reading back new partition table\n");
Alison Chaiken7e805c02017-09-26 07:42:28 -0700950 /*
951 * Empty the existing disk_partitions list, as otherwise the memory in
952 * the original list is unreachable.
953 */
954 del_gpt_info();
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700955 numparts = get_gpt_info(dev_desc);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700956 if (numparts <= 0) {
957 ret = numparts ? numparts : -ENODEV;
958 goto out;
959 }
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700960 printf("new partition table with %d partitions is:\n", numparts);
961 print_gpt_info();
Alison Chaiken7e805c02017-09-26 07:42:28 -0700962 out:
Tom Rinib0d3c0b2020-01-21 11:53:38 -0500963 del_gpt_info();
964#ifdef CONFIG_RANDOM_UUID
Heinrich Schuchardt443806d2020-04-28 21:40:13 +0200965 free(str_disk_guid);
Tom Rinib0d3c0b2020-01-21 11:53:38 -0500966#endif
Heinrich Schuchardt443806d2020-04-28 21:40:13 +0200967 free(new_partitions);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700968 free(partitions_list);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700969 return ret;
970}
971#endif
972
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100973/**
974 * do_gpt(): Perform GPT operations
975 *
976 * @param cmdtp - command name
977 * @param flag
978 * @param argc
979 * @param argv
980 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100981 * Return: zero on success; otherwise error
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100982 */
Simon Glassed38aef2020-05-10 11:40:03 -0600983static int do_gpt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100984{
985 int ret = CMD_RET_SUCCESS;
986 int dev = 0;
Egbert Eichbfd4a712013-10-04 18:53:04 +0200987 char *ep;
Simon Glasse3394752016-02-29 15:25:34 -0700988 struct blk_desc *blk_dev_desc = NULL;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100989
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700990#ifndef CONFIG_CMD_GPT_RENAME
Lukasz Majewski309691b2015-11-20 08:06:17 +0100991 if (argc < 4 || argc > 5)
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700992#else
993 if (argc < 4 || argc > 6)
994#endif
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100995 return CMD_RET_USAGE;
996
Simon Glassff9b9032021-07-24 09:03:30 -0600997 dev = (int)dectoul(argv[3], &ep);
Lukasz Majewski309691b2015-11-20 08:06:17 +0100998 if (!ep || ep[0] != '\0') {
999 printf("'%s' is not a number\n", argv[3]);
1000 return CMD_RET_USAGE;
1001 }
Simon Glassbe1e9bb2016-02-29 15:25:42 -07001002 blk_dev_desc = blk_get_dev(argv[2], dev);
Lukasz Majewski309691b2015-11-20 08:06:17 +01001003 if (!blk_dev_desc) {
1004 printf("%s: %s dev %d NOT available\n",
1005 __func__, argv[2], dev);
1006 return CMD_RET_FAILURE;
1007 }
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +02001008
Philippe Reynes51215292022-04-22 17:46:49 +02001009 if (strcmp(argv[1], "repair") == 0) {
1010 printf("Repairing GPT: ");
1011 ret = gpt_repair(blk_dev_desc);
1012 } else if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
Lukasz Majewski309691b2015-11-20 08:06:17 +01001013 printf("Writing GPT: ");
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +02001014 ret = gpt_default(blk_dev_desc, argv[4]);
Lukasz Majewski309691b2015-11-20 08:06:17 +01001015 } else if ((strcmp(argv[1], "verify") == 0)) {
1016 ret = gpt_verify(blk_dev_desc, argv[4]);
1017 printf("Verify GPT: ");
Corneliu Doban0c6776c2020-11-09 11:49:16 +05301018 } else if ((strcmp(argv[1], "setenv") == 0)) {
1019 ret = gpt_setenv(blk_dev_desc, argv[4]);
1020 } else if ((strcmp(argv[1], "enumerate") == 0)) {
1021 ret = gpt_enumerate(blk_dev_desc);
Alison Chaikene4222582017-06-25 16:43:23 -07001022 } else if (strcmp(argv[1], "guid") == 0) {
1023 ret = do_disk_guid(blk_dev_desc, argv[4]);
Alison Chaikenf04d4b52017-07-04 11:18:50 -07001024#ifdef CONFIG_CMD_GPT_RENAME
1025 } else if (strcmp(argv[1], "read") == 0) {
Farhan Ali078d0222021-02-26 10:17:33 -08001026 ret = do_get_gpt_info(blk_dev_desc, (argc == 5) ? argv[4] : NULL);
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001027 } else if ((strcmp(argv[1], "swap") == 0) ||
1028 (strcmp(argv[1], "rename") == 0)) {
1029 ret = do_rename_gpt_parts(blk_dev_desc, argv[1], argv[4], argv[5]);
Alison Chaikenf04d4b52017-07-04 11:18:50 -07001030#endif
Piotr Wilczekcc00ea82012-12-11 11:09:47 +01001031 } else {
1032 return CMD_RET_USAGE;
1033 }
Lukasz Majewski309691b2015-11-20 08:06:17 +01001034
1035 if (ret) {
1036 printf("error!\n");
1037 return CMD_RET_FAILURE;
1038 }
1039
1040 printf("success!\n");
1041 return CMD_RET_SUCCESS;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +01001042}
1043
1044U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
1045 "GUID Partition Table",
Robert P. J. Daye3f2c2e2013-08-21 11:39:19 -04001046 "<command> <interface> <dev> <partitions_list>\n"
Lukasz Majewskicc5f3d72015-11-13 07:42:10 +01001047 " - GUID partition table restoration and validity check\n"
1048 " Restore or verify GPT information on a device connected\n"
Piotr Wilczekcc00ea82012-12-11 11:09:47 +01001049 " to interface\n"
Lukasz Majewskicc5f3d72015-11-13 07:42:10 +01001050 " Example usage:\n"
Philippe Reynes51215292022-04-22 17:46:49 +02001051 " gpt repair mmc 0\n"
1052 " - repair the GPT on the device\n"
Lukasz Majewskicc5f3d72015-11-13 07:42:10 +01001053 " gpt write mmc 0 $partitions\n"
Corneliu Doban0c6776c2020-11-09 11:49:16 +05301054 " - write the GPT to device\n"
Lukasz Majewskicc5f3d72015-11-13 07:42:10 +01001055 " gpt verify mmc 0 $partitions\n"
Corneliu Doban0c6776c2020-11-09 11:49:16 +05301056 " - verify the GPT on device against $partitions\n"
1057 " gpt setenv mmc 0 $name\n"
1058 " - setup environment variables for partition $name:\n"
1059 " gpt_partition_addr, gpt_partition_size,\n"
1060 " gpt_partition_name, gpt_partition_entry\n"
1061 " gpt enumerate mmc 0\n"
1062 " - store list of partitions to gpt_partition_list environment variable\n"
Eugeniu Roscafa8acbc2019-04-30 04:53:46 +02001063 " gpt guid <interface> <dev>\n"
Alison Chaikene4222582017-06-25 16:43:23 -07001064 " - print disk GUID\n"
Eugeniu Roscafa8acbc2019-04-30 04:53:46 +02001065 " gpt guid <interface> <dev> <varname>\n"
Alison Chaikene4222582017-06-25 16:43:23 -07001066 " - set environment variable to disk GUID\n"
1067 " Example usage:\n"
1068 " gpt guid mmc 0\n"
1069 " gpt guid mmc 0 varname\n"
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001070#ifdef CONFIG_CMD_GPT_RENAME
1071 "gpt partition renaming commands:\n"
Farhan Ali078d0222021-02-26 10:17:33 -08001072 " gpt read <interface> <dev> [<varname>]\n"
Eugeniu Roscafa8acbc2019-04-30 04:53:46 +02001073 " - read GPT into a data structure for manipulation\n"
Farhan Ali078d0222021-02-26 10:17:33 -08001074 " - read GPT partitions into environment variable\n"
Eugeniu Roscafa8acbc2019-04-30 04:53:46 +02001075 " gpt swap <interface> <dev> <name1> <name2>\n"
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001076 " - change all partitions named name1 to name2\n"
1077 " and vice-versa\n"
Eugeniu Roscafa8acbc2019-04-30 04:53:46 +02001078 " gpt rename <interface> <dev> <part> <name>\n"
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001079 " - rename the specified partition\n"
1080 " Example usage:\n"
1081 " gpt swap mmc 0 foo bar\n"
1082 " gpt rename mmc 0 3 foo\n"
1083#endif
Piotr Wilczekcc00ea82012-12-11 11:09:47 +01001084);