blob: aeabd19dd766845dae2e21154fb92833133bcc5c [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
Simon Glass655306c2020-05-10 11:39:58 -060013#include <blk.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060014#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010016#include <malloc.h>
17#include <command.h>
Simon Glass655306c2020-05-10 11:39:58 -060018#include <part.h>
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010019#include <part_efi.h>
Corneliu Doban0c6776c2020-11-09 11:49:16 +053020#include <part.h>
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010021#include <exports.h>
Simon Glass6f044982020-05-10 11:39:52 -060022#include <uuid.h>
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010023#include <linux/ctype.h>
Piotr Wilczek36c06b12013-01-27 22:59:25 +000024#include <div64.h>
Lukasz Majewski309691b2015-11-20 08:06:17 +010025#include <memalign.h>
Alison Chaikenf04d4b52017-07-04 11:18:50 -070026#include <linux/compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070027#include <linux/err.h>
Alison Chaiken8b3397f2017-07-04 11:19:18 -070028#include <linux/sizes.h>
29#include <stdlib.h>
Alison Chaikenf04d4b52017-07-04 11:18:50 -070030
31static LIST_HEAD(disk_partitions);
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010032
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010033/**
34 * extract_env(): Expand env name from string format '&{env_name}'
35 * and return pointer to the env (if the env is set)
36 *
37 * @param str - pointer to string
38 * @param env - pointer to pointer to extracted env
39 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010040 * Return: - zero on successful expand and env is set
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010041 */
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +020042static int extract_env(const char *str, char **env)
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010043{
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +020044 int ret = -1;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010045 char *e, *s;
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +020046#ifdef CONFIG_RANDOM_UUID
47 char uuid_str[UUID_STR_LEN + 1];
48#endif
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010049
50 if (!str || strlen(str) < 4)
51 return -1;
52
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +020053 if (!((strncmp(str, "${", 2) == 0) && (str[strlen(str) - 1] == '}')))
54 return -1;
55
56 s = strdup(str);
57 if (s == NULL)
58 return -1;
59
60 memset(s + strlen(s) - 1, '\0', 1);
61 memmove(s, s + 2, strlen(s) - 1);
62
Simon Glass64b723f2017-08-03 12:22:12 -060063 e = env_get(s);
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +020064 if (e == NULL) {
65#ifdef CONFIG_RANDOM_UUID
66 debug("%s unset. ", str);
Vincent Tinellibdfacea2017-02-27 16:11:15 +020067 gen_rand_uuid_str(uuid_str, UUID_STR_FORMAT_GUID);
Simon Glass6a38e412017-08-03 12:22:09 -060068 env_set(s, uuid_str);
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +020069
Simon Glass64b723f2017-08-03 12:22:12 -060070 e = env_get(s);
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +020071 if (e) {
72 debug("Set to random.\n");
73 ret = 0;
74 } else {
75 debug("Can't get random UUID.\n");
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010076 }
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +020077#else
78 debug("%s unset.\n", str);
79#endif
80 } else {
81 debug("%s get from environment.\n", str);
82 ret = 0;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010083 }
84
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +020085 *env = e;
86 free(s);
87
88 return ret;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +010089}
90
91/**
92 * extract_val(): Extract value from a key=value pair list (comma separated).
93 * Only value for the given key is returend.
94 * Function allocates memory for the value, remember to free!
95 *
96 * @param str - pointer to string with key=values pairs
97 * @param key - pointer to the key to search for
98 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010099 * Return: - pointer to allocated string with the value
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100100 */
101static char *extract_val(const char *str, const char *key)
102{
103 char *v, *k;
104 char *s, *strcopy;
105 char *new = NULL;
106
107 strcopy = strdup(str);
108 if (strcopy == NULL)
109 return NULL;
110
111 s = strcopy;
112 while (s) {
113 v = strsep(&s, ",");
114 if (!v)
115 break;
116 k = strsep(&v, "=");
117 if (!k)
118 break;
119 if (strcmp(k, key) == 0) {
120 new = strdup(v);
121 break;
122 }
123 }
124
125 free(strcopy);
126
127 return new;
128}
129
130/**
Patrick Delaunay7840c4a2015-11-17 11:36:52 +0100131 * found_key(): Found key without value in parameter list (comma separated).
132 *
133 * @param str - pointer to string with key
134 * @param key - pointer to the key to search for
135 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100136 * Return: - true on found key
Patrick Delaunay7840c4a2015-11-17 11:36:52 +0100137 */
138static bool found_key(const char *str, const char *key)
139{
140 char *k;
141 char *s, *strcopy;
142 bool result = false;
143
144 strcopy = strdup(str);
145 if (!strcopy)
146 return NULL;
147
148 s = strcopy;
149 while (s) {
150 k = strsep(&s, ",");
151 if (!k)
152 break;
153 if (strcmp(k, key) == 0) {
154 result = true;
155 break;
156 }
157 }
158
159 free(strcopy);
160
161 return result;
162}
163
Heinrich Schuchardtd4bb6532023-09-02 09:35:23 +0200164/**
165 * calc_parts_list_len() - get size of partition table description
166 *
167 * @numparts: number of partitions
168 * Return: string size including terminating NUL
169 */
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700170static int calc_parts_list_len(int numparts)
171{
Heinrich Schuchardtd4bb6532023-09-02 09:35:23 +0200172 /* number of hexadecimal digits of the lbaint_t representation */
173 const int lbaint_size = 2 * sizeof(lbaint_t);
174 int partlistlen;
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700175
Heinrich Schuchardtd4bb6532023-09-02 09:35:23 +0200176 /* media description including terminating NUL */
177 partlistlen = strlen("uuid_disk=;") + UUID_STR_LEN + 1;
178 /* per-partition descriptions; numparts */
179 partlistlen += numparts * (strlen("name=,") + PART_NAME_LEN);
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700180 /* see part.h for definition of struct disk_partition */
Heinrich Schuchardtd4bb6532023-09-02 09:35:23 +0200181 partlistlen += numparts * (strlen("start=0x,") + lbaint_size);
182 partlistlen += numparts * (strlen("size=0x,") + lbaint_size);
Heinrich Schuchardtd4bb6532023-09-02 09:35:23 +0200183 if (IS_ENABLED(CONFIG_PARTITION_UUIDS))
Heinrich Schuchardtc97dbc42023-09-02 09:35:24 +0200184 partlistlen += numparts * (strlen("uuid=,") + UUID_STR_LEN);
185 if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID))
186 partlistlen += numparts * (strlen("type=;") + UUID_STR_LEN);
Heinrich Schuchardtd4bb6532023-09-02 09:35:23 +0200187 debug("Length of partitions_list is %d for %d partitions\n",
188 partlistlen, numparts);
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700189 return partlistlen;
190}
191
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700192#ifdef CONFIG_CMD_GPT_RENAME
193static void del_gpt_info(void)
194{
195 struct list_head *pos = &disk_partitions;
196 struct disk_part *curr;
197 while (!list_empty(pos)) {
198 curr = list_entry(pos->next, struct disk_part, list);
199 list_del(pos->next);
200 free(curr);
201 }
202}
203
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600204static struct disk_part *allocate_disk_part(struct disk_partition *info,
205 int partnum)
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700206{
207 struct disk_part *newpart;
Heinrich Schuchardt30725f52017-09-21 19:03:06 +0200208 newpart = calloc(1, sizeof(struct disk_part));
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700209 if (!newpart)
210 return ERR_PTR(-ENOMEM);
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700211
212 newpart->gpt_part_info.start = info->start;
213 newpart->gpt_part_info.size = info->size;
214 newpart->gpt_part_info.blksz = info->blksz;
215 strncpy((char *)newpart->gpt_part_info.name, (const char *)info->name,
216 PART_NAME_LEN);
217 newpart->gpt_part_info.name[PART_NAME_LEN - 1] = '\0';
218 strncpy((char *)newpart->gpt_part_info.type, (const char *)info->type,
219 PART_TYPE_LEN);
220 newpart->gpt_part_info.type[PART_TYPE_LEN - 1] = '\0';
221 newpart->gpt_part_info.bootable = info->bootable;
Heinrich Schuchardt2d07fb42023-09-02 09:35:22 +0200222 if (IS_ENABLED(CONFIG_PARTITION_UUIDS))
223 disk_partition_set_uuid(&newpart->gpt_part_info,
224 disk_partition_uuid(info));
Heinrich Schuchardtc97dbc42023-09-02 09:35:24 +0200225 if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID))
226 disk_partition_set_type_guid(&newpart->gpt_part_info,
227 disk_partition_type_guid(info));
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700228 newpart->partnum = partnum;
229
230 return newpart;
231}
232
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700233static void prettyprint_part_size(char *sizestr, lbaint_t partsize,
234 lbaint_t blksize)
235{
236 unsigned long long partbytes, partmegabytes;
237
238 partbytes = partsize * blksize;
239 partmegabytes = lldiv(partbytes, SZ_1M);
240 snprintf(sizestr, 16, "%lluMiB", partmegabytes);
241}
242
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700243static void print_gpt_info(void)
244{
245 struct list_head *pos;
246 struct disk_part *curr;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700247 char partstartstr[16];
248 char partsizestr[16];
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700249
250 list_for_each(pos, &disk_partitions) {
251 curr = list_entry(pos, struct disk_part, list);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700252 prettyprint_part_size(partstartstr, curr->gpt_part_info.start,
253 curr->gpt_part_info.blksz);
254 prettyprint_part_size(partsizestr, curr->gpt_part_info.size,
255 curr->gpt_part_info.blksz);
256
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700257 printf("Partition %d:\n", curr->partnum);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700258 printf("Start %s, size %s\n", partstartstr, partsizestr);
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700259 printf("Block size %lu, name %s\n", curr->gpt_part_info.blksz,
260 curr->gpt_part_info.name);
261 printf("Type %s, bootable %d\n", curr->gpt_part_info.type,
Heinrich Schuchardt59a860d2020-03-19 13:49:34 +0100262 curr->gpt_part_info.bootable & PART_BOOTABLE);
Heinrich Schuchardt2d07fb42023-09-02 09:35:22 +0200263 if (CONFIG_IS_ENABLED(PARTITION_UUIDS))
264 printf("UUID %s\n",
265 disk_partition_uuid(&curr->gpt_part_info));
Heinrich Schuchardtc97dbc42023-09-02 09:35:24 +0200266 if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID))
267 printf("Type GUID %s\n",
268 disk_partition_type_guid(&curr->gpt_part_info));
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700269 printf("\n");
270 }
271}
272
273/*
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700274 * create the string that upstream 'gpt write' command will accept as an
275 * argument
276 *
277 * From doc/README.gpt, Format of partitions layout:
278 * "uuid_disk=...;name=u-boot,size=60MiB,uuid=...;
279 * name=kernel,size=60MiB,uuid=...;"
280 * The fields 'name' and 'size' are mandatory for every partition.
281 * The field 'start' is optional. The fields 'uuid' and 'uuid_disk'
282 * are optional if CONFIG_RANDOM_UUID is enabled.
283 */
284static int create_gpt_partitions_list(int numparts, const char *guid,
285 char *partitions_list)
286{
287 struct list_head *pos;
288 struct disk_part *curr;
289 char partstr[PART_NAME_LEN + 1];
290
291 if (!partitions_list)
292 return -EINVAL;
293
294 strcpy(partitions_list, "uuid_disk=");
295 strncat(partitions_list, guid, UUID_STR_LEN + 1);
296 strcat(partitions_list, ";");
297
298 list_for_each(pos, &disk_partitions) {
299 curr = list_entry(pos, struct disk_part, list);
300 strcat(partitions_list, "name=");
301 strncat(partitions_list, (const char *)curr->gpt_part_info.name,
302 PART_NAME_LEN + 1);
Patrick Delaunay309a6fa2017-10-18 15:11:08 +0200303 sprintf(partstr, ",start=0x%llx",
304 (unsigned long long)curr->gpt_part_info.start *
305 curr->gpt_part_info.blksz);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700306 /* one extra byte for NULL */
307 strncat(partitions_list, partstr, PART_NAME_LEN + 1);
Patrick Delaunay309a6fa2017-10-18 15:11:08 +0200308 sprintf(partstr, ",size=0x%llx",
309 (unsigned long long)curr->gpt_part_info.size *
310 curr->gpt_part_info.blksz);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700311 strncat(partitions_list, partstr, PART_NAME_LEN + 1);
312
Heinrich Schuchardtc97dbc42023-09-02 09:35:24 +0200313 if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID)) {
314 strcat(partitions_list, ",type=");
315 strncat(partitions_list,
316 disk_partition_type_guid(&curr->gpt_part_info),
317 UUID_STR_LEN + 1);
318 }
Heinrich Schuchardt2d07fb42023-09-02 09:35:22 +0200319 if (CONFIG_IS_ENABLED(PARTITION_UUIDS)) {
320 strcat(partitions_list, ",uuid=");
321 strncat(partitions_list,
322 disk_partition_uuid(&curr->gpt_part_info),
323 UUID_STR_LEN + 1);
324 }
Joshua Watt9e58adf2023-08-31 10:51:40 -0600325 if (curr->gpt_part_info.bootable & PART_BOOTABLE)
326 strcat(partitions_list, ",bootable");
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700327 strcat(partitions_list, ";");
328 }
329 return 0;
330}
331
332/*
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700333 * read partition info into disk_partitions list where
334 * it can be printed or modified
335 */
336static int get_gpt_info(struct blk_desc *dev_desc)
337{
338 /* start partition numbering at 1, as U-Boot does */
339 int valid_parts = 0, p, ret;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600340 struct disk_partition info;
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700341 struct disk_part *new_disk_part;
342
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700343 /*
344 * Always re-read partition info from device, in case
345 * it has changed
346 */
347 INIT_LIST_HEAD(&disk_partitions);
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700348
349 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
350 ret = part_get_info(dev_desc, p, &info);
351 if (ret)
352 continue;
353
354 /* Add 1 here because counter is zero-based but p1 is
355 the first partition */
356 new_disk_part = allocate_disk_part(&info, valid_parts+1);
357 if (IS_ERR(new_disk_part))
358 goto out;
359
360 list_add_tail(&new_disk_part->list, &disk_partitions);
361 valid_parts++;
362 }
363 if (valid_parts == 0) {
364 printf("** No valid partitions found **\n");
365 goto out;
366 }
367 return valid_parts;
368 out:
369 if (valid_parts >= 1)
370 del_gpt_info();
371 return -ENODEV;
372}
373
374/* a wrapper to test get_gpt_info */
Farhan Ali078d0222021-02-26 10:17:33 -0800375static int do_get_gpt_info(struct blk_desc *dev_desc, char * const namestr)
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700376{
Farhan Ali078d0222021-02-26 10:17:33 -0800377 int numparts;
378
379 numparts = get_gpt_info(dev_desc);
380
381 if (numparts > 0) {
382 if (namestr) {
383 char disk_guid[UUID_STR_LEN + 1];
384 char *partitions_list;
385 int partlistlen;
386 int ret = -1;
387
388 ret = get_disk_guid(dev_desc, disk_guid);
389 if (ret < 0)
390 return ret;
391
392 partlistlen = calc_parts_list_len(numparts);
393 partitions_list = malloc(partlistlen);
394 if (!partitions_list) {
395 del_gpt_info();
396 return -ENOMEM;
397 }
398 memset(partitions_list, '\0', partlistlen);
399
400 ret = create_gpt_partitions_list(numparts, disk_guid,
401 partitions_list);
402 if (ret < 0)
403 printf("Error: Could not create partition list string!\n");
404 else
405 env_set(namestr, partitions_list);
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700406
Farhan Ali078d0222021-02-26 10:17:33 -0800407 free(partitions_list);
408 } else {
409 print_gpt_info();
410 }
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700411 del_gpt_info();
412 return 0;
413 }
Farhan Ali078d0222021-02-26 10:17:33 -0800414 return numparts;
Alison Chaikenf04d4b52017-07-04 11:18:50 -0700415}
416#endif
417
Patrick Delaunay7840c4a2015-11-17 11:36:52 +0100418/**
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100419 * set_gpt_info(): Fill partition information from string
420 * function allocates memory, remember to free!
421 *
422 * @param dev_desc - pointer block device descriptor
423 * @param str_part - pointer to string with partition information
424 * @param str_disk_guid - pointer to pointer to allocated string with disk guid
425 * @param partitions - pointer to pointer to allocated partitions array
426 * @param parts_count - number of partitions
427 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100428 * Return: - zero on success, otherwise error
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100429 *
430 */
Simon Glasse3394752016-02-29 15:25:34 -0700431static int set_gpt_info(struct blk_desc *dev_desc,
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100432 const char *str_part,
433 char **str_disk_guid,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600434 struct disk_partition **partitions,
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100435 u8 *parts_count)
436{
437 char *tok, *str, *s;
438 int i;
439 char *val, *p;
440 int p_count;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600441 struct disk_partition *parts;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100442 int errno = 0;
Piotr Wilczek36c06b12013-01-27 22:59:25 +0000443 uint64_t size_ll, start_ll;
Michael Trimarchi853445f2016-06-08 10:18:16 +0200444 lbaint_t offset = 0;
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700445 int max_str_part = calc_parts_list_len(MAX_SEARCH_PARTITIONS);
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100446
Egbert Eichbfd4a712013-10-04 18:53:04 +0200447 debug("%s: lba num: 0x%x %d\n", __func__,
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100448 (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba);
449
450 if (str_part == NULL)
451 return -1;
452
453 str = strdup(str_part);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700454 if (str == NULL)
455 return -ENOMEM;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100456
457 /* extract disk guid */
458 s = str;
Rob Herringd9761972015-01-26 09:44:18 -0600459 val = extract_val(str, "uuid_disk");
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100460 if (!val) {
Rob Herringd9761972015-01-26 09:44:18 -0600461#ifdef CONFIG_RANDOM_UUID
462 *str_disk_guid = malloc(UUID_STR_LEN + 1);
Tom Rinia29829e2017-10-07 11:27:59 -0400463 if (*str_disk_guid == NULL)
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700464 return -ENOMEM;
Rob Herringd9761972015-01-26 09:44:18 -0600465 gen_rand_uuid_str(*str_disk_guid, UUID_STR_FORMAT_STD);
466#else
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100467 free(str);
468 return -2;
Rob Herringd9761972015-01-26 09:44:18 -0600469#endif
470 } else {
471 val = strsep(&val, ";");
472 if (extract_env(val, &p))
473 p = val;
474 *str_disk_guid = strdup(p);
475 free(val);
476 /* Move s to first partition */
477 strsep(&s, ";");
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100478 }
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700479 if (s == NULL) {
480 printf("Error: is the partitions string NULL-terminated?\n");
481 return -EINVAL;
482 }
483 if (strnlen(s, max_str_part) == 0)
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100484 return -3;
485
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700486 i = strnlen(s, max_str_part) - 1;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100487 if (s[i] == ';')
488 s[i] = '\0';
489
490 /* calculate expected number of partitions */
491 p_count = 1;
492 p = s;
493 while (*p) {
494 if (*p++ == ';')
495 p_count++;
496 }
497
498 /* allocate memory for partitions */
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600499 parts = calloc(sizeof(struct disk_partition), p_count);
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700500 if (parts == NULL)
501 return -ENOMEM;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100502
Robert P. J. Daye3f2c2e2013-08-21 11:39:19 -0400503 /* retrieve partitions data from string */
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100504 for (i = 0; i < p_count; i++) {
505 tok = strsep(&s, ";");
506
507 if (tok == NULL)
508 break;
509
510 /* uuid */
511 val = extract_val(tok, "uuid");
Rob Herringd9761972015-01-26 09:44:18 -0600512 if (!val) {
513 /* 'uuid' is optional if random uuid's are enabled */
514#ifdef CONFIG_RANDOM_UUID
515 gen_rand_uuid_str(parts[i].uuid, UUID_STR_FORMAT_STD);
516#else
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100517 errno = -4;
518 goto err;
Rob Herringd9761972015-01-26 09:44:18 -0600519#endif
520 } else {
521 if (extract_env(val, &p))
522 p = val;
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700523 if (strnlen(p, max_str_part) >= sizeof(parts[i].uuid)) {
Rob Herringd9761972015-01-26 09:44:18 -0600524 printf("Wrong uuid format for partition %d\n", i);
525 errno = -4;
526 goto err;
527 }
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700528 strncpy((char *)parts[i].uuid, p, max_str_part);
Rob Herringd9761972015-01-26 09:44:18 -0600529 free(val);
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100530 }
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100531#ifdef CONFIG_PARTITION_TYPE_GUID
532 /* guid */
533 val = extract_val(tok, "type");
534 if (val) {
535 /* 'type' is optional */
536 if (extract_env(val, &p))
537 p = val;
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700538 if (strnlen(p, max_str_part) >= sizeof(parts[i].type_guid)) {
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100539 printf("Wrong type guid format for partition %d\n",
540 i);
541 errno = -4;
542 goto err;
543 }
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700544 strncpy((char *)parts[i].type_guid, p, max_str_part);
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100545 free(val);
546 }
547#endif
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100548 /* name */
549 val = extract_val(tok, "name");
550 if (!val) { /* name is mandatory */
551 errno = -4;
552 goto err;
553 }
554 if (extract_env(val, &p))
555 p = val;
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700556 if (strnlen(p, max_str_part) >= sizeof(parts[i].name)) {
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100557 errno = -4;
558 goto err;
559 }
Alison Chaiken8dcee7c2017-07-04 11:19:46 -0700560 strncpy((char *)parts[i].name, p, max_str_part);
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100561 free(val);
562
563 /* size */
564 val = extract_val(tok, "size");
565 if (!val) { /* 'size' is mandatory */
566 errno = -4;
567 goto err;
568 }
569 if (extract_env(val, &p))
570 p = val;
Michael Trimarchi853445f2016-06-08 10:18:16 +0200571 if ((strcmp(p, "-") == 0)) {
Kever Yangf1d070d2016-07-29 11:12:18 +0800572 /* Let part efi module to auto extend the size */
573 parts[i].size = 0;
Michael Trimarchi853445f2016-06-08 10:18:16 +0200574 } else {
575 size_ll = ustrtoull(p, &p, 0);
576 parts[i].size = lldiv(size_ll, dev_desc->blksz);
577 }
578
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100579 free(val);
580
581 /* start address */
582 val = extract_val(tok, "start");
583 if (val) { /* start address is optional */
584 if (extract_env(val, &p))
585 p = val;
Piotr Wilczek36c06b12013-01-27 22:59:25 +0000586 start_ll = ustrtoull(p, &p, 0);
587 parts[i].start = lldiv(start_ll, dev_desc->blksz);
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100588 free(val);
589 }
Patrick Delaunay7840c4a2015-11-17 11:36:52 +0100590
Michael Trimarchi853445f2016-06-08 10:18:16 +0200591 offset += parts[i].size + parts[i].start;
592
Patrick Delaunay7840c4a2015-11-17 11:36:52 +0100593 /* bootable */
594 if (found_key(tok, "bootable"))
Heinrich Schuchardt59a860d2020-03-19 13:49:34 +0100595 parts[i].bootable = PART_BOOTABLE;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100596 }
597
598 *parts_count = p_count;
599 *partitions = parts;
600 free(str);
601
602 return 0;
603err:
604 free(str);
605 free(*str_disk_guid);
606 free(parts);
607
608 return errno;
609}
610
Philippe Reynes51215292022-04-22 17:46:49 +0200611static int gpt_repair(struct blk_desc *blk_dev_desc)
612{
613 int ret = 0;
614
615 ret = gpt_repair_headers(blk_dev_desc);
616
617 return ret;
618}
619
Simon Glasse3394752016-02-29 15:25:34 -0700620static int gpt_default(struct blk_desc *blk_dev_desc, const char *str_part)
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100621{
622 int ret;
623 char *str_disk_guid;
624 u8 part_count = 0;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600625 struct disk_partition *partitions = NULL;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100626
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100627 /* fill partitions */
Egbert Eichbfd4a712013-10-04 18:53:04 +0200628 ret = set_gpt_info(blk_dev_desc, str_part,
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100629 &str_disk_guid, &partitions, &part_count);
630 if (ret) {
631 if (ret == -1)
632 printf("No partition list provided\n");
633 if (ret == -2)
634 printf("Missing disk guid\n");
635 if ((ret == -3) || (ret == -4))
636 printf("Partition list incomplete\n");
637 return -1;
638 }
639
640 /* save partitions layout to disk */
Rob Herring008ab492015-01-26 09:43:15 -0600641 ret = gpt_restore(blk_dev_desc, str_disk_guid, partitions, part_count);
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100642 free(str_disk_guid);
643 free(partitions);
644
Kishan Dudhatracdea43b2024-04-22 23:47:33 +0530645 /* initialize partition table */
646 if (blk_enabled())
647 part_init(blk_dev_desc);
648
Lukasz Majewski309691b2015-11-20 08:06:17 +0100649 return ret;
650}
651
Simon Glasse3394752016-02-29 15:25:34 -0700652static int gpt_verify(struct blk_desc *blk_dev_desc, const char *str_part)
Lukasz Majewski309691b2015-11-20 08:06:17 +0100653{
654 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1,
655 blk_dev_desc->blksz);
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600656 struct disk_partition *partitions = NULL;
Lukasz Majewski309691b2015-11-20 08:06:17 +0100657 gpt_entry *gpt_pte = NULL;
658 char *str_disk_guid;
659 u8 part_count = 0;
660 int ret = 0;
661
662 /* fill partitions */
663 ret = set_gpt_info(blk_dev_desc, str_part,
664 &str_disk_guid, &partitions, &part_count);
665 if (ret) {
666 if (ret == -1) {
667 printf("No partition list provided - only basic check\n");
668 ret = gpt_verify_headers(blk_dev_desc, gpt_head,
669 &gpt_pte);
670 goto out;
671 }
672 if (ret == -2)
673 printf("Missing disk guid\n");
674 if ((ret == -3) || (ret == -4))
675 printf("Partition list incomplete\n");
676 return -1;
677 }
678
679 /* Check partition layout with provided pattern */
680 ret = gpt_verify_partitions(blk_dev_desc, partitions, part_count,
681 gpt_head, &gpt_pte);
682 free(str_disk_guid);
683 free(partitions);
684 out:
Sam Protsenkodef1e372024-06-19 16:23:30 -0500685 if (!ret)
686 free(gpt_pte);
Rob Herring008ab492015-01-26 09:43:15 -0600687 return ret;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100688}
689
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530690/**
691 * gpt_enumerate() - Enumerate partition names into environment variable.
692 *
693 * Enumerate partition names. Partition names are stored in gpt_partition_list
694 * environment variable. Each partition name is delimited by space.
695 *
696 * @desc: block device descriptor
697 *
698 * @Return: '0' on success and -ve error on failure
699 */
700static int gpt_enumerate(struct blk_desc *desc)
701{
702 struct part_driver *first_drv, *part_drv;
703 int str_len = 0, tmp_len;
704 char part_list[2048];
705 int n_drvs;
706 char *ptr;
707
708 part_list[0] = 0;
709 n_drvs = part_driver_get_count();
710 if (!n_drvs) {
711 printf("Failed to get partition driver count\n");
712 return -ENOENT;
713 }
714
715 first_drv = part_driver_get_first();
716 for (part_drv = first_drv; part_drv != first_drv + n_drvs; part_drv++) {
717 struct disk_partition pinfo;
718 int ret;
719 int i;
720
Heinrich Schuchardt16e48de2023-08-26 03:53:42 +0200721 if (part_drv->test(desc))
722 continue;
723
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530724 for (i = 1; i < part_drv->max_entries; i++) {
725 ret = part_drv->get_info(desc, i, &pinfo);
Heinrich Schuchardt16e48de2023-08-26 03:53:42 +0200726 if (ret)
727 continue;
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530728
729 ptr = &part_list[str_len];
730 tmp_len = strlen((const char *)pinfo.name);
731 str_len += tmp_len;
732 /* +1 for space */
733 str_len++;
734 if (str_len > sizeof(part_list)) {
735 printf("Error insufficient memory\n");
736 return -ENOMEM;
737 }
738 strcpy(ptr, (const char *)pinfo.name);
739 /* One byte for space(" ") delimiter */
740 ptr[tmp_len] = ' ';
741 }
Heinrich Schuchardt16e48de2023-08-26 03:53:42 +0200742 if (*part_list)
743 part_list[strlen(part_list) - 1] = 0;
744 break;
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530745 }
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530746 debug("setenv gpt_partition_list %s\n", part_list);
747
748 return env_set("gpt_partition_list", part_list);
749}
750
751/**
752 * gpt_setenv_part_variables() - setup partition environmental variables
753 *
754 * Setup the gpt_partition_name, gpt_partition_entry, gpt_partition_addr
Joshua Wattfd1134e2023-08-31 10:51:37 -0600755 * and gpt_partition_size, gpt_partition_bootable environment variables.
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530756 *
757 * @pinfo: pointer to disk partition
758 * @i: partition entry
759 *
760 * @Return: '0' on success and -ENOENT on failure
761 */
762static int gpt_setenv_part_variables(struct disk_partition *pinfo, int i)
763{
764 int ret;
765
766 ret = env_set_hex("gpt_partition_addr", pinfo->start);
767 if (ret)
768 goto fail;
769
770 ret = env_set_hex("gpt_partition_size", pinfo->size);
771 if (ret)
772 goto fail;
773
Heinrich Schuchardt7e879da2023-08-26 03:53:43 +0200774 ret = env_set_hex("gpt_partition_entry", i);
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530775 if (ret)
776 goto fail;
777
778 ret = env_set("gpt_partition_name", (const char *)pinfo->name);
779 if (ret)
780 goto fail;
781
Joshua Wattfd1134e2023-08-31 10:51:37 -0600782 ret = env_set_ulong("gpt_partition_bootable", !!(pinfo->bootable & PART_BOOTABLE));
783 if (ret)
784 goto fail;
785
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530786 return 0;
787
788fail:
789 return -ENOENT;
790}
791
792/**
793 * gpt_setenv() - Dynamically setup environment variables.
794 *
795 * Dynamically setup environment variables for name, index, offset and size
796 * for partition in GPT table after running "gpt setenv" for a partition name.
797 *
798 * @desc: block device descriptor
799 * @name: partition name
800 *
801 * @Return: '0' on success and -ve err on failure
802 */
803static int gpt_setenv(struct blk_desc *desc, const char *name)
804{
805 struct part_driver *first_drv, *part_drv;
806 int n_drvs;
807 int ret = -1;
808
809 n_drvs = part_driver_get_count();
810 if (!n_drvs) {
811 printf("Failed to get partition driver count\n");
812 goto fail;
813 }
814
815 first_drv = part_driver_get_first();
816 for (part_drv = first_drv; part_drv != first_drv + n_drvs; part_drv++) {
817 struct disk_partition pinfo;
818 int i;
819
820 for (i = 1; i < part_drv->max_entries; i++) {
821 ret = part_drv->get_info(desc, i, &pinfo);
Heinrich Schuchardt441dc542023-08-26 03:53:41 +0200822 if (ret)
823 continue;
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530824
825 if (!strcmp(name, (const char *)pinfo.name)) {
826 /* match found, setup environment variables */
827 ret = gpt_setenv_part_variables(&pinfo, i);
828 if (ret)
829 goto fail;
830
831 return 0;
832 }
833 }
834 }
835
836fail:
837 return ret;
838}
839
Alison Chaikene4222582017-06-25 16:43:23 -0700840static int do_disk_guid(struct blk_desc *dev_desc, char * const namestr)
841{
842 int ret;
843 char disk_guid[UUID_STR_LEN + 1];
844
845 ret = get_disk_guid(dev_desc, disk_guid);
846 if (ret < 0)
847 return CMD_RET_FAILURE;
848
849 if (namestr)
Simon Glass6a38e412017-08-03 12:22:09 -0600850 env_set(namestr, disk_guid);
Alison Chaikene4222582017-06-25 16:43:23 -0700851 else
852 printf("%s\n", disk_guid);
853
854 return ret;
855}
856
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700857#ifdef CONFIG_CMD_GPT_RENAME
858static int do_rename_gpt_parts(struct blk_desc *dev_desc, char *subcomm,
859 char *name1, char *name2)
860{
861 struct list_head *pos;
862 struct disk_part *curr;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600863 struct disk_partition *new_partitions = NULL;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700864 char disk_guid[UUID_STR_LEN + 1];
Tom Rinib0d3c0b2020-01-21 11:53:38 -0500865 char *partitions_list, *str_disk_guid = NULL;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700866 u8 part_count = 0;
867 int partlistlen, ret, numparts = 0, partnum, i = 1, ctr1 = 0, ctr2 = 0;
868
Joshua Wattd88ba5f2023-08-31 10:51:41 -0600869 if (!subcomm || !name1 || !name2 ||
870 (strcmp(subcomm, "swap") && strcmp(subcomm, "rename") &&
871 strcmp(subcomm, "transpose")))
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700872 return -EINVAL;
873
874 ret = get_disk_guid(dev_desc, disk_guid);
875 if (ret < 0)
876 return ret;
Alison Chaiken7e805c02017-09-26 07:42:28 -0700877 /*
878 * Allocates disk_partitions, requiring matching call to del_gpt_info()
879 * if successful.
880 */
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700881 numparts = get_gpt_info(dev_desc);
882 if (numparts <= 0)
883 return numparts ? numparts : -ENODEV;
884
885 partlistlen = calc_parts_list_len(numparts);
886 partitions_list = malloc(partlistlen);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700887 if (!partitions_list) {
888 del_gpt_info();
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700889 return -ENOMEM;
Alison Chaiken7e805c02017-09-26 07:42:28 -0700890 }
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700891 memset(partitions_list, '\0', partlistlen);
892
893 ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700894 if (ret < 0) {
895 free(partitions_list);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700896 return ret;
Alison Chaiken7e805c02017-09-26 07:42:28 -0700897 }
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700898 /*
899 * Uncomment the following line to print a string that 'gpt write'
900 * or 'gpt verify' will accept as input.
901 */
902 debug("OLD partitions_list is %s with %u chars\n", partitions_list,
903 (unsigned)strlen(partitions_list));
904
Alison Chaiken7e805c02017-09-26 07:42:28 -0700905 /* set_gpt_info allocates new_partitions and str_disk_guid */
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700906 ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid,
907 &new_partitions, &part_count);
Tom Rinib0d3c0b2020-01-21 11:53:38 -0500908 if (ret < 0)
909 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700910
911 if (!strcmp(subcomm, "swap")) {
912 if ((strlen(name1) > PART_NAME_LEN) || (strlen(name2) > PART_NAME_LEN)) {
913 printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700914 ret = -EINVAL;
915 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700916 }
917 list_for_each(pos, &disk_partitions) {
918 curr = list_entry(pos, struct disk_part, list);
919 if (!strcmp((char *)curr->gpt_part_info.name, name1)) {
920 strcpy((char *)curr->gpt_part_info.name, name2);
921 ctr1++;
922 } else if (!strcmp((char *)curr->gpt_part_info.name, name2)) {
923 strcpy((char *)curr->gpt_part_info.name, name1);
924 ctr2++;
925 }
926 }
927 if ((ctr1 + ctr2 < 2) || (ctr1 != ctr2)) {
928 printf("Cannot swap partition names except in pairs.\n");
Alison Chaiken7e805c02017-09-26 07:42:28 -0700929 ret = -EINVAL;
930 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700931 }
Joshua Wattd88ba5f2023-08-31 10:51:41 -0600932 } else if (!strcmp(subcomm, "transpose")) {
933 int idx1, idx2;
934 struct disk_partition* first = NULL;
935 struct disk_partition* second= NULL;
936 struct disk_partition tmp_part;
937
938 idx1 = simple_strtoul(name1, NULL, 10);
939 idx2 = simple_strtoul(name2, NULL, 10);
940 if (idx1 == idx2) {
941 printf("Cannot swap partition with itself\n");
942 ret = -EINVAL;
943 goto out;
944 }
945
946 list_for_each(pos, &disk_partitions) {
947 curr = list_entry(pos, struct disk_part, list);
948 if (curr->partnum == idx1)
949 first = &curr->gpt_part_info;
950 else if (curr->partnum == idx2)
951 second = &curr->gpt_part_info;
952 }
953 if (!first) {
954 printf("Illegal partition number %s\n", name1);
955 ret = -EINVAL;
956 goto out;
957 }
958 if (!second) {
959 printf("Illegal partition number %s\n", name2);
960 ret = -EINVAL;
961 goto out;
962 }
963
964 tmp_part = *first;
965 *first = *second;
966 *second = tmp_part;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700967 } else { /* rename */
968 if (strlen(name2) > PART_NAME_LEN) {
969 printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700970 ret = -EINVAL;
971 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700972 }
973 partnum = (int)simple_strtol(name1, NULL, 10);
974 if ((partnum < 0) || (partnum > numparts)) {
975 printf("Illegal partition number %s\n", name1);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700976 ret = -EINVAL;
977 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700978 }
979 ret = part_get_info(dev_desc, partnum, new_partitions);
980 if (ret < 0)
Alison Chaiken7e805c02017-09-26 07:42:28 -0700981 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700982
983 /* U-Boot partition numbering starts at 1 */
984 list_for_each(pos, &disk_partitions) {
985 curr = list_entry(pos, struct disk_part, list);
986 if (i == partnum) {
987 strcpy((char *)curr->gpt_part_info.name, name2);
988 break;
989 }
990 i++;
991 }
992 }
993
994 ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list);
995 if (ret < 0)
Alison Chaiken7e805c02017-09-26 07:42:28 -0700996 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700997 debug("NEW partitions_list is %s with %u chars\n", partitions_list,
998 (unsigned)strlen(partitions_list));
999
1000 ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid,
1001 &new_partitions, &part_count);
Alison Chaiken7e805c02017-09-26 07:42:28 -07001002 /*
1003 * Even though valid pointers are here passed into set_gpt_info(),
1004 * it mallocs again, and there's no way to tell which failed.
1005 */
Tom Rinib0d3c0b2020-01-21 11:53:38 -05001006 if (ret < 0)
1007 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001008
1009 debug("Writing new partition table\n");
1010 ret = gpt_restore(dev_desc, disk_guid, new_partitions, numparts);
1011 if (ret < 0) {
1012 printf("Writing new partition table failed\n");
Alison Chaiken7e805c02017-09-26 07:42:28 -07001013 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001014 }
1015
1016 debug("Reading back new partition table\n");
Alison Chaiken7e805c02017-09-26 07:42:28 -07001017 /*
1018 * Empty the existing disk_partitions list, as otherwise the memory in
1019 * the original list is unreachable.
1020 */
1021 del_gpt_info();
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001022 numparts = get_gpt_info(dev_desc);
Alison Chaiken7e805c02017-09-26 07:42:28 -07001023 if (numparts <= 0) {
1024 ret = numparts ? numparts : -ENODEV;
1025 goto out;
1026 }
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001027 printf("new partition table with %d partitions is:\n", numparts);
1028 print_gpt_info();
Alison Chaiken7e805c02017-09-26 07:42:28 -07001029 out:
Tom Rinib0d3c0b2020-01-21 11:53:38 -05001030 del_gpt_info();
1031#ifdef CONFIG_RANDOM_UUID
Heinrich Schuchardt443806d2020-04-28 21:40:13 +02001032 free(str_disk_guid);
Tom Rinib0d3c0b2020-01-21 11:53:38 -05001033#endif
Heinrich Schuchardt443806d2020-04-28 21:40:13 +02001034 free(new_partitions);
Alison Chaiken7e805c02017-09-26 07:42:28 -07001035 free(partitions_list);
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001036 return ret;
1037}
Joshua Watt44d62332023-08-31 10:51:38 -06001038
1039/**
1040 * gpt_set_bootable() - Set bootable flags for partitions
1041 *
1042 * Sets the bootable flag for any partition names in the comma separated list of
1043 * partition names. Any partitions not in the list have their bootable flag
1044 * cleared
1045 *
1046 * @desc: block device descriptor
1047 * @name: Comma separated list of partition names
1048 *
1049 * @Return: '0' on success and -ve error on failure
1050 */
1051static int gpt_set_bootable(struct blk_desc *blk_dev_desc, char *const part_list)
1052{
1053 char *name;
1054 char disk_guid[UUID_STR_LEN + 1];
1055 struct list_head *pos;
1056 struct disk_part *curr;
1057 struct disk_partition *partitions = NULL;
1058 int part_count = 0;
1059 int ret = get_disk_guid(blk_dev_desc, disk_guid);
1060
1061 if (ret < 0)
1062 return ret;
1063
1064 ret = get_gpt_info(blk_dev_desc);
1065 if (ret <= 0)
1066 goto out;
1067
1068 part_count = ret;
1069 partitions = malloc(sizeof(*partitions) * part_count);
1070 if (!partitions) {
1071 ret = -ENOMEM;
1072 goto out;
1073 }
1074
1075 /* Copy partitions and clear bootable flag */
1076 part_count = 0;
1077 list_for_each(pos, &disk_partitions) {
1078 curr = list_entry(pos, struct disk_part, list);
1079 partitions[part_count] = curr->gpt_part_info;
1080 partitions[part_count].bootable &= ~PART_BOOTABLE;
1081 part_count++;
1082 }
1083
1084 name = strtok(part_list, ",");
1085 while (name) {
1086 bool found = false;
1087
1088 for (int i = 0; i < part_count; i++) {
1089 if (strcmp((char *)partitions[i].name, name) == 0) {
1090 partitions[i].bootable |= PART_BOOTABLE;
1091 found = true;
1092 }
1093 }
1094
1095 if (!found) {
1096 printf("Warning: No partition matching '%s' found\n",
1097 name);
1098 }
1099
1100 name = strtok(NULL, ",");
1101 }
1102
1103 ret = gpt_restore(blk_dev_desc, disk_guid, partitions, part_count);
1104
1105out:
1106 del_gpt_info();
1107
1108 if (partitions)
1109 free(partitions);
1110
1111 return ret;
1112}
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001113#endif
1114
Piotr Wilczekcc00ea82012-12-11 11:09:47 +01001115/**
1116 * do_gpt(): Perform GPT operations
1117 *
1118 * @param cmdtp - command name
1119 * @param flag
1120 * @param argc
1121 * @param argv
1122 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001123 * Return: zero on success; otherwise error
Piotr Wilczekcc00ea82012-12-11 11:09:47 +01001124 */
Simon Glassed38aef2020-05-10 11:40:03 -06001125static int do_gpt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Piotr Wilczekcc00ea82012-12-11 11:09:47 +01001126{
1127 int ret = CMD_RET_SUCCESS;
1128 int dev = 0;
Egbert Eichbfd4a712013-10-04 18:53:04 +02001129 char *ep;
Simon Glasse3394752016-02-29 15:25:34 -07001130 struct blk_desc *blk_dev_desc = NULL;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +01001131
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001132#ifndef CONFIG_CMD_GPT_RENAME
Lukasz Majewski309691b2015-11-20 08:06:17 +01001133 if (argc < 4 || argc > 5)
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001134#else
1135 if (argc < 4 || argc > 6)
1136#endif
Piotr Wilczekcc00ea82012-12-11 11:09:47 +01001137 return CMD_RET_USAGE;
1138
Simon Glassff9b9032021-07-24 09:03:30 -06001139 dev = (int)dectoul(argv[3], &ep);
Lukasz Majewski309691b2015-11-20 08:06:17 +01001140 if (!ep || ep[0] != '\0') {
1141 printf("'%s' is not a number\n", argv[3]);
1142 return CMD_RET_USAGE;
1143 }
Simon Glassbe1e9bb2016-02-29 15:25:42 -07001144 blk_dev_desc = blk_get_dev(argv[2], dev);
Lukasz Majewski309691b2015-11-20 08:06:17 +01001145 if (!blk_dev_desc) {
1146 printf("%s: %s dev %d NOT available\n",
1147 __func__, argv[2], dev);
1148 return CMD_RET_FAILURE;
1149 }
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +02001150
Philippe Reynes51215292022-04-22 17:46:49 +02001151 if (strcmp(argv[1], "repair") == 0) {
1152 printf("Repairing GPT: ");
1153 ret = gpt_repair(blk_dev_desc);
1154 } else if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
Lukasz Majewski309691b2015-11-20 08:06:17 +01001155 printf("Writing GPT: ");
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +02001156 ret = gpt_default(blk_dev_desc, argv[4]);
Lukasz Majewski309691b2015-11-20 08:06:17 +01001157 } else if ((strcmp(argv[1], "verify") == 0)) {
1158 ret = gpt_verify(blk_dev_desc, argv[4]);
1159 printf("Verify GPT: ");
Corneliu Doban0c6776c2020-11-09 11:49:16 +05301160 } else if ((strcmp(argv[1], "setenv") == 0)) {
1161 ret = gpt_setenv(blk_dev_desc, argv[4]);
1162 } else if ((strcmp(argv[1], "enumerate") == 0)) {
1163 ret = gpt_enumerate(blk_dev_desc);
Alison Chaikene4222582017-06-25 16:43:23 -07001164 } else if (strcmp(argv[1], "guid") == 0) {
1165 ret = do_disk_guid(blk_dev_desc, argv[4]);
Alison Chaikenf04d4b52017-07-04 11:18:50 -07001166#ifdef CONFIG_CMD_GPT_RENAME
1167 } else if (strcmp(argv[1], "read") == 0) {
Farhan Ali078d0222021-02-26 10:17:33 -08001168 ret = do_get_gpt_info(blk_dev_desc, (argc == 5) ? argv[4] : NULL);
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001169 } else if ((strcmp(argv[1], "swap") == 0) ||
Joshua Wattd88ba5f2023-08-31 10:51:41 -06001170 (strcmp(argv[1], "rename") == 0) ||
1171 (strcmp(argv[1], "transpose") == 0)) {
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001172 ret = do_rename_gpt_parts(blk_dev_desc, argv[1], argv[4], argv[5]);
Joshua Watt44d62332023-08-31 10:51:38 -06001173 } else if ((strcmp(argv[1], "set-bootable") == 0)) {
1174 ret = gpt_set_bootable(blk_dev_desc, argv[4]);
Alison Chaikenf04d4b52017-07-04 11:18:50 -07001175#endif
Piotr Wilczekcc00ea82012-12-11 11:09:47 +01001176 } else {
1177 return CMD_RET_USAGE;
1178 }
Lukasz Majewski309691b2015-11-20 08:06:17 +01001179
1180 if (ret) {
1181 printf("error!\n");
1182 return CMD_RET_FAILURE;
1183 }
1184
1185 printf("success!\n");
1186 return CMD_RET_SUCCESS;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +01001187}
1188
1189U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
1190 "GUID Partition Table",
Robert P. J. Daye3f2c2e2013-08-21 11:39:19 -04001191 "<command> <interface> <dev> <partitions_list>\n"
Lukasz Majewskicc5f3d72015-11-13 07:42:10 +01001192 " - GUID partition table restoration and validity check\n"
1193 " Restore or verify GPT information on a device connected\n"
Piotr Wilczekcc00ea82012-12-11 11:09:47 +01001194 " to interface\n"
Lukasz Majewskicc5f3d72015-11-13 07:42:10 +01001195 " Example usage:\n"
Philippe Reynes51215292022-04-22 17:46:49 +02001196 " gpt repair mmc 0\n"
1197 " - repair the GPT on the device\n"
Lukasz Majewskicc5f3d72015-11-13 07:42:10 +01001198 " gpt write mmc 0 $partitions\n"
Corneliu Doban0c6776c2020-11-09 11:49:16 +05301199 " - write the GPT to device\n"
Lukasz Majewskicc5f3d72015-11-13 07:42:10 +01001200 " gpt verify mmc 0 $partitions\n"
Corneliu Doban0c6776c2020-11-09 11:49:16 +05301201 " - verify the GPT on device against $partitions\n"
1202 " gpt setenv mmc 0 $name\n"
1203 " - setup environment variables for partition $name:\n"
1204 " gpt_partition_addr, gpt_partition_size,\n"
Joshua Wattfd1134e2023-08-31 10:51:37 -06001205 " gpt_partition_name, gpt_partition_entry,\n"
1206 " gpt_partition_bootable\n"
Corneliu Doban0c6776c2020-11-09 11:49:16 +05301207 " gpt enumerate mmc 0\n"
1208 " - store list of partitions to gpt_partition_list environment variable\n"
Eugeniu Roscafa8acbc2019-04-30 04:53:46 +02001209 " gpt guid <interface> <dev>\n"
Alison Chaikene4222582017-06-25 16:43:23 -07001210 " - print disk GUID\n"
Eugeniu Roscafa8acbc2019-04-30 04:53:46 +02001211 " gpt guid <interface> <dev> <varname>\n"
Alison Chaikene4222582017-06-25 16:43:23 -07001212 " - set environment variable to disk GUID\n"
1213 " Example usage:\n"
1214 " gpt guid mmc 0\n"
1215 " gpt guid mmc 0 varname\n"
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001216#ifdef CONFIG_CMD_GPT_RENAME
1217 "gpt partition renaming commands:\n"
Farhan Ali078d0222021-02-26 10:17:33 -08001218 " gpt read <interface> <dev> [<varname>]\n"
Eugeniu Roscafa8acbc2019-04-30 04:53:46 +02001219 " - read GPT into a data structure for manipulation\n"
Farhan Ali078d0222021-02-26 10:17:33 -08001220 " - read GPT partitions into environment variable\n"
Eugeniu Roscafa8acbc2019-04-30 04:53:46 +02001221 " gpt swap <interface> <dev> <name1> <name2>\n"
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001222 " - change all partitions named name1 to name2\n"
1223 " and vice-versa\n"
Joshua Wattd88ba5f2023-08-31 10:51:41 -06001224 " gpt transpose <interface> <dev> <part1> <part2>\n"
1225 " - Swap the order of the entries for part1 and part2 in the partition table\n"
Eugeniu Roscafa8acbc2019-04-30 04:53:46 +02001226 " gpt rename <interface> <dev> <part> <name>\n"
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001227 " - rename the specified partition\n"
Joshua Watt44d62332023-08-31 10:51:38 -06001228 " gpt set-bootable <interface> <dev> <list>\n"
1229 " - make partition names in list bootable\n"
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001230 " Example usage:\n"
1231 " gpt swap mmc 0 foo bar\n"
1232 " gpt rename mmc 0 3 foo\n"
Joshua Watt44d62332023-08-31 10:51:38 -06001233 " gpt set-bootable mmc 0 boot_a,boot_b\n"
Joshua Wattd88ba5f2023-08-31 10:51:41 -06001234 " gpt transpose mmc 0 1 2\n"
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001235#endif
Piotr Wilczekcc00ea82012-12-11 11:09:47 +01001236);