blob: 36b112d59784060766b0ffe8d24f0ac93df11e23 [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:
685 free(gpt_pte);
Rob Herring008ab492015-01-26 09:43:15 -0600686 return ret;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +0100687}
688
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530689/**
690 * gpt_enumerate() - Enumerate partition names into environment variable.
691 *
692 * Enumerate partition names. Partition names are stored in gpt_partition_list
693 * environment variable. Each partition name is delimited by space.
694 *
695 * @desc: block device descriptor
696 *
697 * @Return: '0' on success and -ve error on failure
698 */
699static int gpt_enumerate(struct blk_desc *desc)
700{
701 struct part_driver *first_drv, *part_drv;
702 int str_len = 0, tmp_len;
703 char part_list[2048];
704 int n_drvs;
705 char *ptr;
706
707 part_list[0] = 0;
708 n_drvs = part_driver_get_count();
709 if (!n_drvs) {
710 printf("Failed to get partition driver count\n");
711 return -ENOENT;
712 }
713
714 first_drv = part_driver_get_first();
715 for (part_drv = first_drv; part_drv != first_drv + n_drvs; part_drv++) {
716 struct disk_partition pinfo;
717 int ret;
718 int i;
719
Heinrich Schuchardt16e48de2023-08-26 03:53:42 +0200720 if (part_drv->test(desc))
721 continue;
722
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530723 for (i = 1; i < part_drv->max_entries; i++) {
724 ret = part_drv->get_info(desc, i, &pinfo);
Heinrich Schuchardt16e48de2023-08-26 03:53:42 +0200725 if (ret)
726 continue;
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530727
728 ptr = &part_list[str_len];
729 tmp_len = strlen((const char *)pinfo.name);
730 str_len += tmp_len;
731 /* +1 for space */
732 str_len++;
733 if (str_len > sizeof(part_list)) {
734 printf("Error insufficient memory\n");
735 return -ENOMEM;
736 }
737 strcpy(ptr, (const char *)pinfo.name);
738 /* One byte for space(" ") delimiter */
739 ptr[tmp_len] = ' ';
740 }
Heinrich Schuchardt16e48de2023-08-26 03:53:42 +0200741 if (*part_list)
742 part_list[strlen(part_list) - 1] = 0;
743 break;
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530744 }
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530745 debug("setenv gpt_partition_list %s\n", part_list);
746
747 return env_set("gpt_partition_list", part_list);
748}
749
750/**
751 * gpt_setenv_part_variables() - setup partition environmental variables
752 *
753 * Setup the gpt_partition_name, gpt_partition_entry, gpt_partition_addr
Joshua Wattfd1134e2023-08-31 10:51:37 -0600754 * and gpt_partition_size, gpt_partition_bootable environment variables.
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530755 *
756 * @pinfo: pointer to disk partition
757 * @i: partition entry
758 *
759 * @Return: '0' on success and -ENOENT on failure
760 */
761static int gpt_setenv_part_variables(struct disk_partition *pinfo, int i)
762{
763 int ret;
764
765 ret = env_set_hex("gpt_partition_addr", pinfo->start);
766 if (ret)
767 goto fail;
768
769 ret = env_set_hex("gpt_partition_size", pinfo->size);
770 if (ret)
771 goto fail;
772
Heinrich Schuchardt7e879da2023-08-26 03:53:43 +0200773 ret = env_set_hex("gpt_partition_entry", i);
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530774 if (ret)
775 goto fail;
776
777 ret = env_set("gpt_partition_name", (const char *)pinfo->name);
778 if (ret)
779 goto fail;
780
Joshua Wattfd1134e2023-08-31 10:51:37 -0600781 ret = env_set_ulong("gpt_partition_bootable", !!(pinfo->bootable & PART_BOOTABLE));
782 if (ret)
783 goto fail;
784
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530785 return 0;
786
787fail:
788 return -ENOENT;
789}
790
791/**
792 * gpt_setenv() - Dynamically setup environment variables.
793 *
794 * Dynamically setup environment variables for name, index, offset and size
795 * for partition in GPT table after running "gpt setenv" for a partition name.
796 *
797 * @desc: block device descriptor
798 * @name: partition name
799 *
800 * @Return: '0' on success and -ve err on failure
801 */
802static int gpt_setenv(struct blk_desc *desc, const char *name)
803{
804 struct part_driver *first_drv, *part_drv;
805 int n_drvs;
806 int ret = -1;
807
808 n_drvs = part_driver_get_count();
809 if (!n_drvs) {
810 printf("Failed to get partition driver count\n");
811 goto fail;
812 }
813
814 first_drv = part_driver_get_first();
815 for (part_drv = first_drv; part_drv != first_drv + n_drvs; part_drv++) {
816 struct disk_partition pinfo;
817 int i;
818
819 for (i = 1; i < part_drv->max_entries; i++) {
820 ret = part_drv->get_info(desc, i, &pinfo);
Heinrich Schuchardt441dc542023-08-26 03:53:41 +0200821 if (ret)
822 continue;
Corneliu Doban0c6776c2020-11-09 11:49:16 +0530823
824 if (!strcmp(name, (const char *)pinfo.name)) {
825 /* match found, setup environment variables */
826 ret = gpt_setenv_part_variables(&pinfo, i);
827 if (ret)
828 goto fail;
829
830 return 0;
831 }
832 }
833 }
834
835fail:
836 return ret;
837}
838
Alison Chaikene4222582017-06-25 16:43:23 -0700839static int do_disk_guid(struct blk_desc *dev_desc, char * const namestr)
840{
841 int ret;
842 char disk_guid[UUID_STR_LEN + 1];
843
844 ret = get_disk_guid(dev_desc, disk_guid);
845 if (ret < 0)
846 return CMD_RET_FAILURE;
847
848 if (namestr)
Simon Glass6a38e412017-08-03 12:22:09 -0600849 env_set(namestr, disk_guid);
Alison Chaikene4222582017-06-25 16:43:23 -0700850 else
851 printf("%s\n", disk_guid);
852
853 return ret;
854}
855
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700856#ifdef CONFIG_CMD_GPT_RENAME
857static int do_rename_gpt_parts(struct blk_desc *dev_desc, char *subcomm,
858 char *name1, char *name2)
859{
860 struct list_head *pos;
861 struct disk_part *curr;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600862 struct disk_partition *new_partitions = NULL;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700863 char disk_guid[UUID_STR_LEN + 1];
Tom Rinib0d3c0b2020-01-21 11:53:38 -0500864 char *partitions_list, *str_disk_guid = NULL;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700865 u8 part_count = 0;
866 int partlistlen, ret, numparts = 0, partnum, i = 1, ctr1 = 0, ctr2 = 0;
867
Joshua Wattd88ba5f2023-08-31 10:51:41 -0600868 if (!subcomm || !name1 || !name2 ||
869 (strcmp(subcomm, "swap") && strcmp(subcomm, "rename") &&
870 strcmp(subcomm, "transpose")))
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700871 return -EINVAL;
872
873 ret = get_disk_guid(dev_desc, disk_guid);
874 if (ret < 0)
875 return ret;
Alison Chaiken7e805c02017-09-26 07:42:28 -0700876 /*
877 * Allocates disk_partitions, requiring matching call to del_gpt_info()
878 * if successful.
879 */
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700880 numparts = get_gpt_info(dev_desc);
881 if (numparts <= 0)
882 return numparts ? numparts : -ENODEV;
883
884 partlistlen = calc_parts_list_len(numparts);
885 partitions_list = malloc(partlistlen);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700886 if (!partitions_list) {
887 del_gpt_info();
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700888 return -ENOMEM;
Alison Chaiken7e805c02017-09-26 07:42:28 -0700889 }
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700890 memset(partitions_list, '\0', partlistlen);
891
892 ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700893 if (ret < 0) {
894 free(partitions_list);
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700895 return ret;
Alison Chaiken7e805c02017-09-26 07:42:28 -0700896 }
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700897 /*
898 * Uncomment the following line to print a string that 'gpt write'
899 * or 'gpt verify' will accept as input.
900 */
901 debug("OLD partitions_list is %s with %u chars\n", partitions_list,
902 (unsigned)strlen(partitions_list));
903
Alison Chaiken7e805c02017-09-26 07:42:28 -0700904 /* set_gpt_info allocates new_partitions and str_disk_guid */
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700905 ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid,
906 &new_partitions, &part_count);
Tom Rinib0d3c0b2020-01-21 11:53:38 -0500907 if (ret < 0)
908 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700909
910 if (!strcmp(subcomm, "swap")) {
911 if ((strlen(name1) > PART_NAME_LEN) || (strlen(name2) > PART_NAME_LEN)) {
912 printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700913 ret = -EINVAL;
914 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700915 }
916 list_for_each(pos, &disk_partitions) {
917 curr = list_entry(pos, struct disk_part, list);
918 if (!strcmp((char *)curr->gpt_part_info.name, name1)) {
919 strcpy((char *)curr->gpt_part_info.name, name2);
920 ctr1++;
921 } else if (!strcmp((char *)curr->gpt_part_info.name, name2)) {
922 strcpy((char *)curr->gpt_part_info.name, name1);
923 ctr2++;
924 }
925 }
926 if ((ctr1 + ctr2 < 2) || (ctr1 != ctr2)) {
927 printf("Cannot swap partition names except in pairs.\n");
Alison Chaiken7e805c02017-09-26 07:42:28 -0700928 ret = -EINVAL;
929 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700930 }
Joshua Wattd88ba5f2023-08-31 10:51:41 -0600931 } else if (!strcmp(subcomm, "transpose")) {
932 int idx1, idx2;
933 struct disk_partition* first = NULL;
934 struct disk_partition* second= NULL;
935 struct disk_partition tmp_part;
936
937 idx1 = simple_strtoul(name1, NULL, 10);
938 idx2 = simple_strtoul(name2, NULL, 10);
939 if (idx1 == idx2) {
940 printf("Cannot swap partition with itself\n");
941 ret = -EINVAL;
942 goto out;
943 }
944
945 list_for_each(pos, &disk_partitions) {
946 curr = list_entry(pos, struct disk_part, list);
947 if (curr->partnum == idx1)
948 first = &curr->gpt_part_info;
949 else if (curr->partnum == idx2)
950 second = &curr->gpt_part_info;
951 }
952 if (!first) {
953 printf("Illegal partition number %s\n", name1);
954 ret = -EINVAL;
955 goto out;
956 }
957 if (!second) {
958 printf("Illegal partition number %s\n", name2);
959 ret = -EINVAL;
960 goto out;
961 }
962
963 tmp_part = *first;
964 *first = *second;
965 *second = tmp_part;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700966 } else { /* rename */
967 if (strlen(name2) > PART_NAME_LEN) {
968 printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700969 ret = -EINVAL;
970 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700971 }
972 partnum = (int)simple_strtol(name1, NULL, 10);
973 if ((partnum < 0) || (partnum > numparts)) {
974 printf("Illegal partition number %s\n", name1);
Alison Chaiken7e805c02017-09-26 07:42:28 -0700975 ret = -EINVAL;
976 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700977 }
978 ret = part_get_info(dev_desc, partnum, new_partitions);
979 if (ret < 0)
Alison Chaiken7e805c02017-09-26 07:42:28 -0700980 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700981
982 /* U-Boot partition numbering starts at 1 */
983 list_for_each(pos, &disk_partitions) {
984 curr = list_entry(pos, struct disk_part, list);
985 if (i == partnum) {
986 strcpy((char *)curr->gpt_part_info.name, name2);
987 break;
988 }
989 i++;
990 }
991 }
992
993 ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list);
994 if (ret < 0)
Alison Chaiken7e805c02017-09-26 07:42:28 -0700995 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -0700996 debug("NEW partitions_list is %s with %u chars\n", partitions_list,
997 (unsigned)strlen(partitions_list));
998
999 ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid,
1000 &new_partitions, &part_count);
Alison Chaiken7e805c02017-09-26 07:42:28 -07001001 /*
1002 * Even though valid pointers are here passed into set_gpt_info(),
1003 * it mallocs again, and there's no way to tell which failed.
1004 */
Tom Rinib0d3c0b2020-01-21 11:53:38 -05001005 if (ret < 0)
1006 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001007
1008 debug("Writing new partition table\n");
1009 ret = gpt_restore(dev_desc, disk_guid, new_partitions, numparts);
1010 if (ret < 0) {
1011 printf("Writing new partition table failed\n");
Alison Chaiken7e805c02017-09-26 07:42:28 -07001012 goto out;
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001013 }
1014
1015 debug("Reading back new partition table\n");
Alison Chaiken7e805c02017-09-26 07:42:28 -07001016 /*
1017 * Empty the existing disk_partitions list, as otherwise the memory in
1018 * the original list is unreachable.
1019 */
1020 del_gpt_info();
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001021 numparts = get_gpt_info(dev_desc);
Alison Chaiken7e805c02017-09-26 07:42:28 -07001022 if (numparts <= 0) {
1023 ret = numparts ? numparts : -ENODEV;
1024 goto out;
1025 }
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001026 printf("new partition table with %d partitions is:\n", numparts);
1027 print_gpt_info();
Alison Chaiken7e805c02017-09-26 07:42:28 -07001028 out:
Tom Rinib0d3c0b2020-01-21 11:53:38 -05001029 del_gpt_info();
1030#ifdef CONFIG_RANDOM_UUID
Heinrich Schuchardt443806d2020-04-28 21:40:13 +02001031 free(str_disk_guid);
Tom Rinib0d3c0b2020-01-21 11:53:38 -05001032#endif
Heinrich Schuchardt443806d2020-04-28 21:40:13 +02001033 free(new_partitions);
Alison Chaiken7e805c02017-09-26 07:42:28 -07001034 free(partitions_list);
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001035 return ret;
1036}
Joshua Watt44d62332023-08-31 10:51:38 -06001037
1038/**
1039 * gpt_set_bootable() - Set bootable flags for partitions
1040 *
1041 * Sets the bootable flag for any partition names in the comma separated list of
1042 * partition names. Any partitions not in the list have their bootable flag
1043 * cleared
1044 *
1045 * @desc: block device descriptor
1046 * @name: Comma separated list of partition names
1047 *
1048 * @Return: '0' on success and -ve error on failure
1049 */
1050static int gpt_set_bootable(struct blk_desc *blk_dev_desc, char *const part_list)
1051{
1052 char *name;
1053 char disk_guid[UUID_STR_LEN + 1];
1054 struct list_head *pos;
1055 struct disk_part *curr;
1056 struct disk_partition *partitions = NULL;
1057 int part_count = 0;
1058 int ret = get_disk_guid(blk_dev_desc, disk_guid);
1059
1060 if (ret < 0)
1061 return ret;
1062
1063 ret = get_gpt_info(blk_dev_desc);
1064 if (ret <= 0)
1065 goto out;
1066
1067 part_count = ret;
1068 partitions = malloc(sizeof(*partitions) * part_count);
1069 if (!partitions) {
1070 ret = -ENOMEM;
1071 goto out;
1072 }
1073
1074 /* Copy partitions and clear bootable flag */
1075 part_count = 0;
1076 list_for_each(pos, &disk_partitions) {
1077 curr = list_entry(pos, struct disk_part, list);
1078 partitions[part_count] = curr->gpt_part_info;
1079 partitions[part_count].bootable &= ~PART_BOOTABLE;
1080 part_count++;
1081 }
1082
1083 name = strtok(part_list, ",");
1084 while (name) {
1085 bool found = false;
1086
1087 for (int i = 0; i < part_count; i++) {
1088 if (strcmp((char *)partitions[i].name, name) == 0) {
1089 partitions[i].bootable |= PART_BOOTABLE;
1090 found = true;
1091 }
1092 }
1093
1094 if (!found) {
1095 printf("Warning: No partition matching '%s' found\n",
1096 name);
1097 }
1098
1099 name = strtok(NULL, ",");
1100 }
1101
1102 ret = gpt_restore(blk_dev_desc, disk_guid, partitions, part_count);
1103
1104out:
1105 del_gpt_info();
1106
1107 if (partitions)
1108 free(partitions);
1109
1110 return ret;
1111}
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001112#endif
1113
Piotr Wilczekcc00ea82012-12-11 11:09:47 +01001114/**
1115 * do_gpt(): Perform GPT operations
1116 *
1117 * @param cmdtp - command name
1118 * @param flag
1119 * @param argc
1120 * @param argv
1121 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001122 * Return: zero on success; otherwise error
Piotr Wilczekcc00ea82012-12-11 11:09:47 +01001123 */
Simon Glassed38aef2020-05-10 11:40:03 -06001124static int do_gpt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Piotr Wilczekcc00ea82012-12-11 11:09:47 +01001125{
1126 int ret = CMD_RET_SUCCESS;
1127 int dev = 0;
Egbert Eichbfd4a712013-10-04 18:53:04 +02001128 char *ep;
Simon Glasse3394752016-02-29 15:25:34 -07001129 struct blk_desc *blk_dev_desc = NULL;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +01001130
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001131#ifndef CONFIG_CMD_GPT_RENAME
Lukasz Majewski309691b2015-11-20 08:06:17 +01001132 if (argc < 4 || argc > 5)
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001133#else
1134 if (argc < 4 || argc > 6)
1135#endif
Piotr Wilczekcc00ea82012-12-11 11:09:47 +01001136 return CMD_RET_USAGE;
1137
Simon Glassff9b9032021-07-24 09:03:30 -06001138 dev = (int)dectoul(argv[3], &ep);
Lukasz Majewski309691b2015-11-20 08:06:17 +01001139 if (!ep || ep[0] != '\0') {
1140 printf("'%s' is not a number\n", argv[3]);
1141 return CMD_RET_USAGE;
1142 }
Simon Glassbe1e9bb2016-02-29 15:25:42 -07001143 blk_dev_desc = blk_get_dev(argv[2], dev);
Lukasz Majewski309691b2015-11-20 08:06:17 +01001144 if (!blk_dev_desc) {
1145 printf("%s: %s dev %d NOT available\n",
1146 __func__, argv[2], dev);
1147 return CMD_RET_FAILURE;
1148 }
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +02001149
Philippe Reynes51215292022-04-22 17:46:49 +02001150 if (strcmp(argv[1], "repair") == 0) {
1151 printf("Repairing GPT: ");
1152 ret = gpt_repair(blk_dev_desc);
1153 } else if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
Lukasz Majewski309691b2015-11-20 08:06:17 +01001154 printf("Writing GPT: ");
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +02001155 ret = gpt_default(blk_dev_desc, argv[4]);
Lukasz Majewski309691b2015-11-20 08:06:17 +01001156 } else if ((strcmp(argv[1], "verify") == 0)) {
1157 ret = gpt_verify(blk_dev_desc, argv[4]);
1158 printf("Verify GPT: ");
Corneliu Doban0c6776c2020-11-09 11:49:16 +05301159 } else if ((strcmp(argv[1], "setenv") == 0)) {
1160 ret = gpt_setenv(blk_dev_desc, argv[4]);
1161 } else if ((strcmp(argv[1], "enumerate") == 0)) {
1162 ret = gpt_enumerate(blk_dev_desc);
Alison Chaikene4222582017-06-25 16:43:23 -07001163 } else if (strcmp(argv[1], "guid") == 0) {
1164 ret = do_disk_guid(blk_dev_desc, argv[4]);
Alison Chaikenf04d4b52017-07-04 11:18:50 -07001165#ifdef CONFIG_CMD_GPT_RENAME
1166 } else if (strcmp(argv[1], "read") == 0) {
Farhan Ali078d0222021-02-26 10:17:33 -08001167 ret = do_get_gpt_info(blk_dev_desc, (argc == 5) ? argv[4] : NULL);
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001168 } else if ((strcmp(argv[1], "swap") == 0) ||
Joshua Wattd88ba5f2023-08-31 10:51:41 -06001169 (strcmp(argv[1], "rename") == 0) ||
1170 (strcmp(argv[1], "transpose") == 0)) {
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001171 ret = do_rename_gpt_parts(blk_dev_desc, argv[1], argv[4], argv[5]);
Joshua Watt44d62332023-08-31 10:51:38 -06001172 } else if ((strcmp(argv[1], "set-bootable") == 0)) {
1173 ret = gpt_set_bootable(blk_dev_desc, argv[4]);
Alison Chaikenf04d4b52017-07-04 11:18:50 -07001174#endif
Piotr Wilczekcc00ea82012-12-11 11:09:47 +01001175 } else {
1176 return CMD_RET_USAGE;
1177 }
Lukasz Majewski309691b2015-11-20 08:06:17 +01001178
1179 if (ret) {
1180 printf("error!\n");
1181 return CMD_RET_FAILURE;
1182 }
1183
1184 printf("success!\n");
1185 return CMD_RET_SUCCESS;
Piotr Wilczekcc00ea82012-12-11 11:09:47 +01001186}
1187
1188U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
1189 "GUID Partition Table",
Robert P. J. Daye3f2c2e2013-08-21 11:39:19 -04001190 "<command> <interface> <dev> <partitions_list>\n"
Lukasz Majewskicc5f3d72015-11-13 07:42:10 +01001191 " - GUID partition table restoration and validity check\n"
1192 " Restore or verify GPT information on a device connected\n"
Piotr Wilczekcc00ea82012-12-11 11:09:47 +01001193 " to interface\n"
Lukasz Majewskicc5f3d72015-11-13 07:42:10 +01001194 " Example usage:\n"
Philippe Reynes51215292022-04-22 17:46:49 +02001195 " gpt repair mmc 0\n"
1196 " - repair the GPT on the device\n"
Lukasz Majewskicc5f3d72015-11-13 07:42:10 +01001197 " gpt write mmc 0 $partitions\n"
Corneliu Doban0c6776c2020-11-09 11:49:16 +05301198 " - write the GPT to device\n"
Lukasz Majewskicc5f3d72015-11-13 07:42:10 +01001199 " gpt verify mmc 0 $partitions\n"
Corneliu Doban0c6776c2020-11-09 11:49:16 +05301200 " - verify the GPT on device against $partitions\n"
1201 " gpt setenv mmc 0 $name\n"
1202 " - setup environment variables for partition $name:\n"
1203 " gpt_partition_addr, gpt_partition_size,\n"
Joshua Wattfd1134e2023-08-31 10:51:37 -06001204 " gpt_partition_name, gpt_partition_entry,\n"
1205 " gpt_partition_bootable\n"
Corneliu Doban0c6776c2020-11-09 11:49:16 +05301206 " gpt enumerate mmc 0\n"
1207 " - store list of partitions to gpt_partition_list environment variable\n"
Eugeniu Roscafa8acbc2019-04-30 04:53:46 +02001208 " gpt guid <interface> <dev>\n"
Alison Chaikene4222582017-06-25 16:43:23 -07001209 " - print disk GUID\n"
Eugeniu Roscafa8acbc2019-04-30 04:53:46 +02001210 " gpt guid <interface> <dev> <varname>\n"
Alison Chaikene4222582017-06-25 16:43:23 -07001211 " - set environment variable to disk GUID\n"
1212 " Example usage:\n"
1213 " gpt guid mmc 0\n"
1214 " gpt guid mmc 0 varname\n"
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001215#ifdef CONFIG_CMD_GPT_RENAME
1216 "gpt partition renaming commands:\n"
Farhan Ali078d0222021-02-26 10:17:33 -08001217 " gpt read <interface> <dev> [<varname>]\n"
Eugeniu Roscafa8acbc2019-04-30 04:53:46 +02001218 " - read GPT into a data structure for manipulation\n"
Farhan Ali078d0222021-02-26 10:17:33 -08001219 " - read GPT partitions into environment variable\n"
Eugeniu Roscafa8acbc2019-04-30 04:53:46 +02001220 " gpt swap <interface> <dev> <name1> <name2>\n"
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001221 " - change all partitions named name1 to name2\n"
1222 " and vice-versa\n"
Joshua Wattd88ba5f2023-08-31 10:51:41 -06001223 " gpt transpose <interface> <dev> <part1> <part2>\n"
1224 " - Swap the order of the entries for part1 and part2 in the partition table\n"
Eugeniu Roscafa8acbc2019-04-30 04:53:46 +02001225 " gpt rename <interface> <dev> <part> <name>\n"
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001226 " - rename the specified partition\n"
Joshua Watt44d62332023-08-31 10:51:38 -06001227 " gpt set-bootable <interface> <dev> <list>\n"
1228 " - make partition names in list bootable\n"
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001229 " Example usage:\n"
1230 " gpt swap mmc 0 foo bar\n"
1231 " gpt rename mmc 0 3 foo\n"
Joshua Watt44d62332023-08-31 10:51:38 -06001232 " gpt set-bootable mmc 0 boot_a,boot_b\n"
Joshua Wattd88ba5f2023-08-31 10:51:41 -06001233 " gpt transpose mmc 0 1 2\n"
Alison Chaiken8b3397f2017-07-04 11:19:18 -07001234#endif
Piotr Wilczekcc00ea82012-12-11 11:09:47 +01001235);