blob: df759416c882853b8d2cbee3be8bf2533a3fb2ae [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Piotr Wilczek8b096232012-12-11 11:09:47 +01002/*
3 * cmd_gpt.c -- GPT (GUID Partition Table) handling command
4 *
Lukasz Majewskibbb9ffa2015-11-20 08:06:17 +01005 * Copyright (C) 2015
6 * Lukasz Majewski <l.majewski@majess.pl>
7 *
Piotr Wilczek8b096232012-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 Wilczek8b096232012-12-11 11:09:47 +010011 */
12
13#include <common.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060014#include <blk.h>
Simon Glass9fb625c2019-08-01 09:46:51 -060015#include <env.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060016#include <log.h>
Piotr Wilczek8b096232012-12-11 11:09:47 +010017#include <malloc.h>
18#include <command.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060019#include <part.h>
Piotr Wilczek8b096232012-12-11 11:09:47 +010020#include <part_efi.h>
21#include <exports.h>
Simon Glassba06b3c2020-05-10 11:39:52 -060022#include <uuid.h>
Piotr Wilczek8b096232012-12-11 11:09:47 +010023#include <linux/ctype.h>
Piotr Wilczek3e34cf72013-01-27 22:59:25 +000024#include <div64.h>
Lukasz Majewskibbb9ffa2015-11-20 08:06:17 +010025#include <memalign.h>
Alison Chaiken09a49932017-07-04 11:18:50 -070026#include <linux/compat.h>
Simon Glass61b29b82020-02-03 07:36:15 -070027#include <linux/err.h>
Alison Chaiken203f9b42017-07-04 11:19:18 -070028#include <linux/sizes.h>
29#include <stdlib.h>
Alison Chaiken09a49932017-07-04 11:18:50 -070030
31static LIST_HEAD(disk_partitions);
Piotr Wilczek8b096232012-12-11 11:09:47 +010032
Piotr Wilczek8b096232012-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 *
40 * @return - zero on successful expand and env is set
41 */
Przemyslaw Marczak39206382014-04-02 10:20:06 +020042static int extract_env(const char *str, char **env)
Piotr Wilczek8b096232012-12-11 11:09:47 +010043{
Przemyslaw Marczak39206382014-04-02 10:20:06 +020044 int ret = -1;
Piotr Wilczek8b096232012-12-11 11:09:47 +010045 char *e, *s;
Przemyslaw Marczak39206382014-04-02 10:20:06 +020046#ifdef CONFIG_RANDOM_UUID
47 char uuid_str[UUID_STR_LEN + 1];
48#endif
Piotr Wilczek8b096232012-12-11 11:09:47 +010049
50 if (!str || strlen(str) < 4)
51 return -1;
52
Przemyslaw Marczak39206382014-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 Glass00caae62017-08-03 12:22:12 -060063 e = env_get(s);
Przemyslaw Marczak39206382014-04-02 10:20:06 +020064 if (e == NULL) {
65#ifdef CONFIG_RANDOM_UUID
66 debug("%s unset. ", str);
Vincent Tinelli9da52f82017-02-27 16:11:15 +020067 gen_rand_uuid_str(uuid_str, UUID_STR_FORMAT_GUID);
Simon Glass382bee52017-08-03 12:22:09 -060068 env_set(s, uuid_str);
Przemyslaw Marczak39206382014-04-02 10:20:06 +020069
Simon Glass00caae62017-08-03 12:22:12 -060070 e = env_get(s);
Przemyslaw Marczak39206382014-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 Wilczek8b096232012-12-11 11:09:47 +010076 }
Przemyslaw Marczak39206382014-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 Wilczek8b096232012-12-11 11:09:47 +010083 }
84
Przemyslaw Marczak39206382014-04-02 10:20:06 +020085 *env = e;
86 free(s);
87
88 return ret;
Piotr Wilczek8b096232012-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 *
99 * @return - pointer to allocated string with the value
100 */
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 Delaunaycfdaf4c2015-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 *
136 * @return - true on found key
137 */
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
Alison Chaiken2fcaa412017-07-04 11:19:46 -0700164static int calc_parts_list_len(int numparts)
165{
166 int partlistlen = UUID_STR_LEN + 1 + strlen("uuid_disk=");
167 /* for the comma */
168 partlistlen++;
169
170 /* per-partition additions; numparts starts at 1, so this should be correct */
171 partlistlen += numparts * (strlen("name=,") + PART_NAME_LEN + 1);
172 /* see part.h for definition of struct disk_partition */
173 partlistlen += numparts * (strlen("start=MiB,") + sizeof(lbaint_t) + 1);
174 partlistlen += numparts * (strlen("size=MiB,") + sizeof(lbaint_t) + 1);
175 partlistlen += numparts * (strlen("uuid=;") + UUID_STR_LEN + 1);
176 /* for the terminating null */
177 partlistlen++;
178 debug("Length of partitions_list is %d for %d partitions\n", partlistlen,
179 numparts);
180 return partlistlen;
181}
182
Alison Chaiken09a49932017-07-04 11:18:50 -0700183#ifdef CONFIG_CMD_GPT_RENAME
184static void del_gpt_info(void)
185{
186 struct list_head *pos = &disk_partitions;
187 struct disk_part *curr;
188 while (!list_empty(pos)) {
189 curr = list_entry(pos->next, struct disk_part, list);
190 list_del(pos->next);
191 free(curr);
192 }
193}
194
Simon Glass05289792020-05-10 11:39:57 -0600195static struct disk_part *allocate_disk_part(struct disk_partition *info,
196 int partnum)
Alison Chaiken09a49932017-07-04 11:18:50 -0700197{
198 struct disk_part *newpart;
Heinrich Schuchardtf66bc0e2017-09-21 19:03:06 +0200199 newpart = calloc(1, sizeof(struct disk_part));
Alison Chaiken09a49932017-07-04 11:18:50 -0700200 if (!newpart)
201 return ERR_PTR(-ENOMEM);
Alison Chaiken09a49932017-07-04 11:18:50 -0700202
203 newpart->gpt_part_info.start = info->start;
204 newpart->gpt_part_info.size = info->size;
205 newpart->gpt_part_info.blksz = info->blksz;
206 strncpy((char *)newpart->gpt_part_info.name, (const char *)info->name,
207 PART_NAME_LEN);
208 newpart->gpt_part_info.name[PART_NAME_LEN - 1] = '\0';
209 strncpy((char *)newpart->gpt_part_info.type, (const char *)info->type,
210 PART_TYPE_LEN);
211 newpart->gpt_part_info.type[PART_TYPE_LEN - 1] = '\0';
212 newpart->gpt_part_info.bootable = info->bootable;
213#ifdef CONFIG_PARTITION_UUIDS
214 strncpy(newpart->gpt_part_info.uuid, (const char *)info->uuid,
215 UUID_STR_LEN);
216 /* UUID_STR_LEN is correct, as uuid[]'s length is UUID_STR_LEN+1 chars */
217 newpart->gpt_part_info.uuid[UUID_STR_LEN] = '\0';
218#endif
219 newpart->partnum = partnum;
220
221 return newpart;
222}
223
Alison Chaiken203f9b42017-07-04 11:19:18 -0700224static void prettyprint_part_size(char *sizestr, lbaint_t partsize,
225 lbaint_t blksize)
226{
227 unsigned long long partbytes, partmegabytes;
228
229 partbytes = partsize * blksize;
230 partmegabytes = lldiv(partbytes, SZ_1M);
231 snprintf(sizestr, 16, "%lluMiB", partmegabytes);
232}
233
Alison Chaiken09a49932017-07-04 11:18:50 -0700234static void print_gpt_info(void)
235{
236 struct list_head *pos;
237 struct disk_part *curr;
Alison Chaiken203f9b42017-07-04 11:19:18 -0700238 char partstartstr[16];
239 char partsizestr[16];
Alison Chaiken09a49932017-07-04 11:18:50 -0700240
241 list_for_each(pos, &disk_partitions) {
242 curr = list_entry(pos, struct disk_part, list);
Alison Chaiken203f9b42017-07-04 11:19:18 -0700243 prettyprint_part_size(partstartstr, curr->gpt_part_info.start,
244 curr->gpt_part_info.blksz);
245 prettyprint_part_size(partsizestr, curr->gpt_part_info.size,
246 curr->gpt_part_info.blksz);
247
Alison Chaiken09a49932017-07-04 11:18:50 -0700248 printf("Partition %d:\n", curr->partnum);
Alison Chaiken203f9b42017-07-04 11:19:18 -0700249 printf("Start %s, size %s\n", partstartstr, partsizestr);
Alison Chaiken09a49932017-07-04 11:18:50 -0700250 printf("Block size %lu, name %s\n", curr->gpt_part_info.blksz,
251 curr->gpt_part_info.name);
252 printf("Type %s, bootable %d\n", curr->gpt_part_info.type,
Heinrich Schuchardt25801ac2020-03-19 13:49:34 +0100253 curr->gpt_part_info.bootable & PART_BOOTABLE);
Alison Chaiken09a49932017-07-04 11:18:50 -0700254#ifdef CONFIG_PARTITION_UUIDS
255 printf("UUID %s\n", curr->gpt_part_info.uuid);
256#endif
257 printf("\n");
258 }
259}
260
Alison Chaiken203f9b42017-07-04 11:19:18 -0700261/*
262 * create the string that upstream 'gpt write' command will accept as an
263 * argument
264 *
265 * From doc/README.gpt, Format of partitions layout:
266 * "uuid_disk=...;name=u-boot,size=60MiB,uuid=...;
267 * name=kernel,size=60MiB,uuid=...;"
268 * The fields 'name' and 'size' are mandatory for every partition.
269 * The field 'start' is optional. The fields 'uuid' and 'uuid_disk'
270 * are optional if CONFIG_RANDOM_UUID is enabled.
271 */
272static int create_gpt_partitions_list(int numparts, const char *guid,
273 char *partitions_list)
274{
275 struct list_head *pos;
276 struct disk_part *curr;
277 char partstr[PART_NAME_LEN + 1];
278
279 if (!partitions_list)
280 return -EINVAL;
281
282 strcpy(partitions_list, "uuid_disk=");
283 strncat(partitions_list, guid, UUID_STR_LEN + 1);
284 strcat(partitions_list, ";");
285
286 list_for_each(pos, &disk_partitions) {
287 curr = list_entry(pos, struct disk_part, list);
288 strcat(partitions_list, "name=");
289 strncat(partitions_list, (const char *)curr->gpt_part_info.name,
290 PART_NAME_LEN + 1);
Patrick Delaunay3a2605f2017-10-18 15:11:08 +0200291 sprintf(partstr, ",start=0x%llx",
292 (unsigned long long)curr->gpt_part_info.start *
293 curr->gpt_part_info.blksz);
Alison Chaiken203f9b42017-07-04 11:19:18 -0700294 /* one extra byte for NULL */
295 strncat(partitions_list, partstr, PART_NAME_LEN + 1);
Patrick Delaunay3a2605f2017-10-18 15:11:08 +0200296 sprintf(partstr, ",size=0x%llx",
297 (unsigned long long)curr->gpt_part_info.size *
298 curr->gpt_part_info.blksz);
Alison Chaiken203f9b42017-07-04 11:19:18 -0700299 strncat(partitions_list, partstr, PART_NAME_LEN + 1);
300
301 strcat(partitions_list, ",uuid=");
302 strncat(partitions_list, curr->gpt_part_info.uuid,
303 UUID_STR_LEN + 1);
304 strcat(partitions_list, ";");
305 }
306 return 0;
307}
308
Alison Chaiken09a49932017-07-04 11:18:50 -0700309/*
310 * read partition info into disk_partitions list where
311 * it can be printed or modified
312 */
313static int get_gpt_info(struct blk_desc *dev_desc)
314{
315 /* start partition numbering at 1, as U-Boot does */
316 int valid_parts = 0, p, ret;
Simon Glass05289792020-05-10 11:39:57 -0600317 struct disk_partition info;
Alison Chaiken09a49932017-07-04 11:18:50 -0700318 struct disk_part *new_disk_part;
319
Alison Chaiken203f9b42017-07-04 11:19:18 -0700320 /*
321 * Always re-read partition info from device, in case
322 * it has changed
323 */
324 INIT_LIST_HEAD(&disk_partitions);
Alison Chaiken09a49932017-07-04 11:18:50 -0700325
326 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
327 ret = part_get_info(dev_desc, p, &info);
328 if (ret)
329 continue;
330
331 /* Add 1 here because counter is zero-based but p1 is
332 the first partition */
333 new_disk_part = allocate_disk_part(&info, valid_parts+1);
334 if (IS_ERR(new_disk_part))
335 goto out;
336
337 list_add_tail(&new_disk_part->list, &disk_partitions);
338 valid_parts++;
339 }
340 if (valid_parts == 0) {
341 printf("** No valid partitions found **\n");
342 goto out;
343 }
344 return valid_parts;
345 out:
346 if (valid_parts >= 1)
347 del_gpt_info();
348 return -ENODEV;
349}
350
351/* a wrapper to test get_gpt_info */
352static int do_get_gpt_info(struct blk_desc *dev_desc)
353{
354 int ret;
355
356 ret = get_gpt_info(dev_desc);
357 if (ret > 0) {
358 print_gpt_info();
359 del_gpt_info();
360 return 0;
361 }
362 return ret;
363}
364#endif
365
Patrick Delaunaycfdaf4c2015-11-17 11:36:52 +0100366/**
Piotr Wilczek8b096232012-12-11 11:09:47 +0100367 * set_gpt_info(): Fill partition information from string
368 * function allocates memory, remember to free!
369 *
370 * @param dev_desc - pointer block device descriptor
371 * @param str_part - pointer to string with partition information
372 * @param str_disk_guid - pointer to pointer to allocated string with disk guid
373 * @param partitions - pointer to pointer to allocated partitions array
374 * @param parts_count - number of partitions
375 *
376 * @return - zero on success, otherwise error
377 *
378 */
Simon Glass4101f682016-02-29 15:25:34 -0700379static int set_gpt_info(struct blk_desc *dev_desc,
Piotr Wilczek8b096232012-12-11 11:09:47 +0100380 const char *str_part,
381 char **str_disk_guid,
Simon Glass05289792020-05-10 11:39:57 -0600382 struct disk_partition **partitions,
Piotr Wilczek8b096232012-12-11 11:09:47 +0100383 u8 *parts_count)
384{
385 char *tok, *str, *s;
386 int i;
387 char *val, *p;
388 int p_count;
Simon Glass05289792020-05-10 11:39:57 -0600389 struct disk_partition *parts;
Piotr Wilczek8b096232012-12-11 11:09:47 +0100390 int errno = 0;
Piotr Wilczek3e34cf72013-01-27 22:59:25 +0000391 uint64_t size_ll, start_ll;
Michael Trimarchi6663623562016-06-08 10:18:16 +0200392 lbaint_t offset = 0;
Alison Chaiken2fcaa412017-07-04 11:19:46 -0700393 int max_str_part = calc_parts_list_len(MAX_SEARCH_PARTITIONS);
Piotr Wilczek8b096232012-12-11 11:09:47 +0100394
Egbert Eich619f0fd2013-10-04 18:53:04 +0200395 debug("%s: lba num: 0x%x %d\n", __func__,
Piotr Wilczek8b096232012-12-11 11:09:47 +0100396 (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba);
397
398 if (str_part == NULL)
399 return -1;
400
401 str = strdup(str_part);
Alison Chaiken203f9b42017-07-04 11:19:18 -0700402 if (str == NULL)
403 return -ENOMEM;
Piotr Wilczek8b096232012-12-11 11:09:47 +0100404
405 /* extract disk guid */
406 s = str;
Rob Herring0c7e8d12015-01-26 09:44:18 -0600407 val = extract_val(str, "uuid_disk");
Piotr Wilczek8b096232012-12-11 11:09:47 +0100408 if (!val) {
Rob Herring0c7e8d12015-01-26 09:44:18 -0600409#ifdef CONFIG_RANDOM_UUID
410 *str_disk_guid = malloc(UUID_STR_LEN + 1);
Tom Rinibf52fcd2017-10-07 11:27:59 -0400411 if (*str_disk_guid == NULL)
Alison Chaiken2fcaa412017-07-04 11:19:46 -0700412 return -ENOMEM;
Rob Herring0c7e8d12015-01-26 09:44:18 -0600413 gen_rand_uuid_str(*str_disk_guid, UUID_STR_FORMAT_STD);
414#else
Piotr Wilczek8b096232012-12-11 11:09:47 +0100415 free(str);
416 return -2;
Rob Herring0c7e8d12015-01-26 09:44:18 -0600417#endif
418 } else {
419 val = strsep(&val, ";");
420 if (extract_env(val, &p))
421 p = val;
422 *str_disk_guid = strdup(p);
423 free(val);
424 /* Move s to first partition */
425 strsep(&s, ";");
Piotr Wilczek8b096232012-12-11 11:09:47 +0100426 }
Alison Chaiken2fcaa412017-07-04 11:19:46 -0700427 if (s == NULL) {
428 printf("Error: is the partitions string NULL-terminated?\n");
429 return -EINVAL;
430 }
431 if (strnlen(s, max_str_part) == 0)
Piotr Wilczek8b096232012-12-11 11:09:47 +0100432 return -3;
433
Alison Chaiken2fcaa412017-07-04 11:19:46 -0700434 i = strnlen(s, max_str_part) - 1;
Piotr Wilczek8b096232012-12-11 11:09:47 +0100435 if (s[i] == ';')
436 s[i] = '\0';
437
438 /* calculate expected number of partitions */
439 p_count = 1;
440 p = s;
441 while (*p) {
442 if (*p++ == ';')
443 p_count++;
444 }
445
446 /* allocate memory for partitions */
Simon Glass05289792020-05-10 11:39:57 -0600447 parts = calloc(sizeof(struct disk_partition), p_count);
Alison Chaiken2fcaa412017-07-04 11:19:46 -0700448 if (parts == NULL)
449 return -ENOMEM;
Piotr Wilczek8b096232012-12-11 11:09:47 +0100450
Robert P. J. Day1f8b5462013-08-21 11:39:19 -0400451 /* retrieve partitions data from string */
Piotr Wilczek8b096232012-12-11 11:09:47 +0100452 for (i = 0; i < p_count; i++) {
453 tok = strsep(&s, ";");
454
455 if (tok == NULL)
456 break;
457
458 /* uuid */
459 val = extract_val(tok, "uuid");
Rob Herring0c7e8d12015-01-26 09:44:18 -0600460 if (!val) {
461 /* 'uuid' is optional if random uuid's are enabled */
462#ifdef CONFIG_RANDOM_UUID
463 gen_rand_uuid_str(parts[i].uuid, UUID_STR_FORMAT_STD);
464#else
Piotr Wilczek8b096232012-12-11 11:09:47 +0100465 errno = -4;
466 goto err;
Rob Herring0c7e8d12015-01-26 09:44:18 -0600467#endif
468 } else {
469 if (extract_env(val, &p))
470 p = val;
Alison Chaiken2fcaa412017-07-04 11:19:46 -0700471 if (strnlen(p, max_str_part) >= sizeof(parts[i].uuid)) {
Rob Herring0c7e8d12015-01-26 09:44:18 -0600472 printf("Wrong uuid format for partition %d\n", i);
473 errno = -4;
474 goto err;
475 }
Alison Chaiken2fcaa412017-07-04 11:19:46 -0700476 strncpy((char *)parts[i].uuid, p, max_str_part);
Rob Herring0c7e8d12015-01-26 09:44:18 -0600477 free(val);
Piotr Wilczek8b096232012-12-11 11:09:47 +0100478 }
Patrick Delaunay7561b252015-10-27 11:00:27 +0100479#ifdef CONFIG_PARTITION_TYPE_GUID
480 /* guid */
481 val = extract_val(tok, "type");
482 if (val) {
483 /* 'type' is optional */
484 if (extract_env(val, &p))
485 p = val;
Alison Chaiken2fcaa412017-07-04 11:19:46 -0700486 if (strnlen(p, max_str_part) >= sizeof(parts[i].type_guid)) {
Patrick Delaunay7561b252015-10-27 11:00:27 +0100487 printf("Wrong type guid format for partition %d\n",
488 i);
489 errno = -4;
490 goto err;
491 }
Alison Chaiken2fcaa412017-07-04 11:19:46 -0700492 strncpy((char *)parts[i].type_guid, p, max_str_part);
Patrick Delaunay7561b252015-10-27 11:00:27 +0100493 free(val);
494 }
495#endif
Piotr Wilczek8b096232012-12-11 11:09:47 +0100496 /* name */
497 val = extract_val(tok, "name");
498 if (!val) { /* name is mandatory */
499 errno = -4;
500 goto err;
501 }
502 if (extract_env(val, &p))
503 p = val;
Alison Chaiken2fcaa412017-07-04 11:19:46 -0700504 if (strnlen(p, max_str_part) >= sizeof(parts[i].name)) {
Piotr Wilczek8b096232012-12-11 11:09:47 +0100505 errno = -4;
506 goto err;
507 }
Alison Chaiken2fcaa412017-07-04 11:19:46 -0700508 strncpy((char *)parts[i].name, p, max_str_part);
Piotr Wilczek8b096232012-12-11 11:09:47 +0100509 free(val);
510
511 /* size */
512 val = extract_val(tok, "size");
513 if (!val) { /* 'size' is mandatory */
514 errno = -4;
515 goto err;
516 }
517 if (extract_env(val, &p))
518 p = val;
Michael Trimarchi6663623562016-06-08 10:18:16 +0200519 if ((strcmp(p, "-") == 0)) {
Kever Yangc2fdd342016-07-29 11:12:18 +0800520 /* Let part efi module to auto extend the size */
521 parts[i].size = 0;
Michael Trimarchi6663623562016-06-08 10:18:16 +0200522 } else {
523 size_ll = ustrtoull(p, &p, 0);
524 parts[i].size = lldiv(size_ll, dev_desc->blksz);
525 }
526
Piotr Wilczek8b096232012-12-11 11:09:47 +0100527 free(val);
528
529 /* start address */
530 val = extract_val(tok, "start");
531 if (val) { /* start address is optional */
532 if (extract_env(val, &p))
533 p = val;
Piotr Wilczek3e34cf72013-01-27 22:59:25 +0000534 start_ll = ustrtoull(p, &p, 0);
535 parts[i].start = lldiv(start_ll, dev_desc->blksz);
Piotr Wilczek8b096232012-12-11 11:09:47 +0100536 free(val);
537 }
Patrick Delaunaycfdaf4c2015-11-17 11:36:52 +0100538
Michael Trimarchi6663623562016-06-08 10:18:16 +0200539 offset += parts[i].size + parts[i].start;
540
Patrick Delaunaycfdaf4c2015-11-17 11:36:52 +0100541 /* bootable */
542 if (found_key(tok, "bootable"))
Heinrich Schuchardt25801ac2020-03-19 13:49:34 +0100543 parts[i].bootable = PART_BOOTABLE;
Piotr Wilczek8b096232012-12-11 11:09:47 +0100544 }
545
546 *parts_count = p_count;
547 *partitions = parts;
548 free(str);
549
550 return 0;
551err:
552 free(str);
553 free(*str_disk_guid);
554 free(parts);
555
556 return errno;
557}
558
Simon Glass4101f682016-02-29 15:25:34 -0700559static int gpt_default(struct blk_desc *blk_dev_desc, const char *str_part)
Piotr Wilczek8b096232012-12-11 11:09:47 +0100560{
561 int ret;
562 char *str_disk_guid;
563 u8 part_count = 0;
Simon Glass05289792020-05-10 11:39:57 -0600564 struct disk_partition *partitions = NULL;
Piotr Wilczek8b096232012-12-11 11:09:47 +0100565
Piotr Wilczek8b096232012-12-11 11:09:47 +0100566 /* fill partitions */
Egbert Eich619f0fd2013-10-04 18:53:04 +0200567 ret = set_gpt_info(blk_dev_desc, str_part,
Piotr Wilczek8b096232012-12-11 11:09:47 +0100568 &str_disk_guid, &partitions, &part_count);
569 if (ret) {
570 if (ret == -1)
571 printf("No partition list provided\n");
572 if (ret == -2)
573 printf("Missing disk guid\n");
574 if ((ret == -3) || (ret == -4))
575 printf("Partition list incomplete\n");
576 return -1;
577 }
578
579 /* save partitions layout to disk */
Rob Herringa150e6c2015-01-26 09:43:15 -0600580 ret = gpt_restore(blk_dev_desc, str_disk_guid, partitions, part_count);
Piotr Wilczek8b096232012-12-11 11:09:47 +0100581 free(str_disk_guid);
582 free(partitions);
583
Rob Herringa150e6c2015-01-26 09:43:15 -0600584 return ret;
Piotr Wilczek8b096232012-12-11 11:09:47 +0100585}
586
Simon Glass4101f682016-02-29 15:25:34 -0700587static int gpt_verify(struct blk_desc *blk_dev_desc, const char *str_part)
Lukasz Majewskibbb9ffa2015-11-20 08:06:17 +0100588{
589 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1,
590 blk_dev_desc->blksz);
Simon Glass05289792020-05-10 11:39:57 -0600591 struct disk_partition *partitions = NULL;
Lukasz Majewskibbb9ffa2015-11-20 08:06:17 +0100592 gpt_entry *gpt_pte = NULL;
593 char *str_disk_guid;
594 u8 part_count = 0;
595 int ret = 0;
596
597 /* fill partitions */
598 ret = set_gpt_info(blk_dev_desc, str_part,
599 &str_disk_guid, &partitions, &part_count);
600 if (ret) {
601 if (ret == -1) {
602 printf("No partition list provided - only basic check\n");
603 ret = gpt_verify_headers(blk_dev_desc, gpt_head,
604 &gpt_pte);
605 goto out;
606 }
607 if (ret == -2)
608 printf("Missing disk guid\n");
609 if ((ret == -3) || (ret == -4))
610 printf("Partition list incomplete\n");
611 return -1;
612 }
613
614 /* Check partition layout with provided pattern */
615 ret = gpt_verify_partitions(blk_dev_desc, partitions, part_count,
616 gpt_head, &gpt_pte);
617 free(str_disk_guid);
618 free(partitions);
619 out:
620 free(gpt_pte);
621 return ret;
622}
623
Alison Chaiken73d6d182017-06-25 16:43:23 -0700624static int do_disk_guid(struct blk_desc *dev_desc, char * const namestr)
625{
626 int ret;
627 char disk_guid[UUID_STR_LEN + 1];
628
629 ret = get_disk_guid(dev_desc, disk_guid);
630 if (ret < 0)
631 return CMD_RET_FAILURE;
632
633 if (namestr)
Simon Glass382bee52017-08-03 12:22:09 -0600634 env_set(namestr, disk_guid);
Alison Chaiken73d6d182017-06-25 16:43:23 -0700635 else
636 printf("%s\n", disk_guid);
637
638 return ret;
639}
640
Alison Chaiken203f9b42017-07-04 11:19:18 -0700641#ifdef CONFIG_CMD_GPT_RENAME
642static int do_rename_gpt_parts(struct blk_desc *dev_desc, char *subcomm,
643 char *name1, char *name2)
644{
645 struct list_head *pos;
646 struct disk_part *curr;
Simon Glass05289792020-05-10 11:39:57 -0600647 struct disk_partition *new_partitions = NULL;
Alison Chaiken203f9b42017-07-04 11:19:18 -0700648 char disk_guid[UUID_STR_LEN + 1];
Tom Rini5749faa2020-01-21 11:53:38 -0500649 char *partitions_list, *str_disk_guid = NULL;
Alison Chaiken203f9b42017-07-04 11:19:18 -0700650 u8 part_count = 0;
651 int partlistlen, ret, numparts = 0, partnum, i = 1, ctr1 = 0, ctr2 = 0;
652
653 if ((subcomm == NULL) || (name1 == NULL) || (name2 == NULL) ||
654 (strcmp(subcomm, "swap") && (strcmp(subcomm, "rename"))))
655 return -EINVAL;
656
657 ret = get_disk_guid(dev_desc, disk_guid);
658 if (ret < 0)
659 return ret;
Alison Chaiken18030d02017-09-26 07:42:28 -0700660 /*
661 * Allocates disk_partitions, requiring matching call to del_gpt_info()
662 * if successful.
663 */
Alison Chaiken203f9b42017-07-04 11:19:18 -0700664 numparts = get_gpt_info(dev_desc);
665 if (numparts <= 0)
666 return numparts ? numparts : -ENODEV;
667
668 partlistlen = calc_parts_list_len(numparts);
669 partitions_list = malloc(partlistlen);
Alison Chaiken18030d02017-09-26 07:42:28 -0700670 if (!partitions_list) {
671 del_gpt_info();
Alison Chaiken203f9b42017-07-04 11:19:18 -0700672 return -ENOMEM;
Alison Chaiken18030d02017-09-26 07:42:28 -0700673 }
Alison Chaiken203f9b42017-07-04 11:19:18 -0700674 memset(partitions_list, '\0', partlistlen);
675
676 ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list);
Alison Chaiken18030d02017-09-26 07:42:28 -0700677 if (ret < 0) {
678 free(partitions_list);
Alison Chaiken203f9b42017-07-04 11:19:18 -0700679 return ret;
Alison Chaiken18030d02017-09-26 07:42:28 -0700680 }
Alison Chaiken203f9b42017-07-04 11:19:18 -0700681 /*
682 * Uncomment the following line to print a string that 'gpt write'
683 * or 'gpt verify' will accept as input.
684 */
685 debug("OLD partitions_list is %s with %u chars\n", partitions_list,
686 (unsigned)strlen(partitions_list));
687
Alison Chaiken18030d02017-09-26 07:42:28 -0700688 /* set_gpt_info allocates new_partitions and str_disk_guid */
Alison Chaiken203f9b42017-07-04 11:19:18 -0700689 ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid,
690 &new_partitions, &part_count);
Tom Rini5749faa2020-01-21 11:53:38 -0500691 if (ret < 0)
692 goto out;
Alison Chaiken203f9b42017-07-04 11:19:18 -0700693
694 if (!strcmp(subcomm, "swap")) {
695 if ((strlen(name1) > PART_NAME_LEN) || (strlen(name2) > PART_NAME_LEN)) {
696 printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN);
Alison Chaiken18030d02017-09-26 07:42:28 -0700697 ret = -EINVAL;
698 goto out;
Alison Chaiken203f9b42017-07-04 11:19:18 -0700699 }
700 list_for_each(pos, &disk_partitions) {
701 curr = list_entry(pos, struct disk_part, list);
702 if (!strcmp((char *)curr->gpt_part_info.name, name1)) {
703 strcpy((char *)curr->gpt_part_info.name, name2);
704 ctr1++;
705 } else if (!strcmp((char *)curr->gpt_part_info.name, name2)) {
706 strcpy((char *)curr->gpt_part_info.name, name1);
707 ctr2++;
708 }
709 }
710 if ((ctr1 + ctr2 < 2) || (ctr1 != ctr2)) {
711 printf("Cannot swap partition names except in pairs.\n");
Alison Chaiken18030d02017-09-26 07:42:28 -0700712 ret = -EINVAL;
713 goto out;
Alison Chaiken203f9b42017-07-04 11:19:18 -0700714 }
715 } else { /* rename */
716 if (strlen(name2) > PART_NAME_LEN) {
717 printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN);
Alison Chaiken18030d02017-09-26 07:42:28 -0700718 ret = -EINVAL;
719 goto out;
Alison Chaiken203f9b42017-07-04 11:19:18 -0700720 }
721 partnum = (int)simple_strtol(name1, NULL, 10);
722 if ((partnum < 0) || (partnum > numparts)) {
723 printf("Illegal partition number %s\n", name1);
Alison Chaiken18030d02017-09-26 07:42:28 -0700724 ret = -EINVAL;
725 goto out;
Alison Chaiken203f9b42017-07-04 11:19:18 -0700726 }
727 ret = part_get_info(dev_desc, partnum, new_partitions);
728 if (ret < 0)
Alison Chaiken18030d02017-09-26 07:42:28 -0700729 goto out;
Alison Chaiken203f9b42017-07-04 11:19:18 -0700730
731 /* U-Boot partition numbering starts at 1 */
732 list_for_each(pos, &disk_partitions) {
733 curr = list_entry(pos, struct disk_part, list);
734 if (i == partnum) {
735 strcpy((char *)curr->gpt_part_info.name, name2);
736 break;
737 }
738 i++;
739 }
740 }
741
742 ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list);
743 if (ret < 0)
Alison Chaiken18030d02017-09-26 07:42:28 -0700744 goto out;
Alison Chaiken203f9b42017-07-04 11:19:18 -0700745 debug("NEW partitions_list is %s with %u chars\n", partitions_list,
746 (unsigned)strlen(partitions_list));
747
748 ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid,
749 &new_partitions, &part_count);
Alison Chaiken18030d02017-09-26 07:42:28 -0700750 /*
751 * Even though valid pointers are here passed into set_gpt_info(),
752 * it mallocs again, and there's no way to tell which failed.
753 */
Tom Rini5749faa2020-01-21 11:53:38 -0500754 if (ret < 0)
755 goto out;
Alison Chaiken203f9b42017-07-04 11:19:18 -0700756
757 debug("Writing new partition table\n");
758 ret = gpt_restore(dev_desc, disk_guid, new_partitions, numparts);
759 if (ret < 0) {
760 printf("Writing new partition table failed\n");
Alison Chaiken18030d02017-09-26 07:42:28 -0700761 goto out;
Alison Chaiken203f9b42017-07-04 11:19:18 -0700762 }
763
764 debug("Reading back new partition table\n");
Alison Chaiken18030d02017-09-26 07:42:28 -0700765 /*
766 * Empty the existing disk_partitions list, as otherwise the memory in
767 * the original list is unreachable.
768 */
769 del_gpt_info();
Alison Chaiken203f9b42017-07-04 11:19:18 -0700770 numparts = get_gpt_info(dev_desc);
Alison Chaiken18030d02017-09-26 07:42:28 -0700771 if (numparts <= 0) {
772 ret = numparts ? numparts : -ENODEV;
773 goto out;
774 }
Alison Chaiken203f9b42017-07-04 11:19:18 -0700775 printf("new partition table with %d partitions is:\n", numparts);
776 print_gpt_info();
Alison Chaiken18030d02017-09-26 07:42:28 -0700777 out:
Tom Rini5749faa2020-01-21 11:53:38 -0500778 del_gpt_info();
779#ifdef CONFIG_RANDOM_UUID
Heinrich Schuchardtb142d0a2020-04-28 21:40:13 +0200780 free(str_disk_guid);
Tom Rini5749faa2020-01-21 11:53:38 -0500781#endif
Heinrich Schuchardtb142d0a2020-04-28 21:40:13 +0200782 free(new_partitions);
Alison Chaiken18030d02017-09-26 07:42:28 -0700783 free(partitions_list);
Alison Chaiken203f9b42017-07-04 11:19:18 -0700784 return ret;
785}
786#endif
787
Piotr Wilczek8b096232012-12-11 11:09:47 +0100788/**
789 * do_gpt(): Perform GPT operations
790 *
791 * @param cmdtp - command name
792 * @param flag
793 * @param argc
794 * @param argv
795 *
796 * @return zero on success; otherwise error
797 */
Simon Glass09140112020-05-10 11:40:03 -0600798static int do_gpt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Piotr Wilczek8b096232012-12-11 11:09:47 +0100799{
800 int ret = CMD_RET_SUCCESS;
801 int dev = 0;
Egbert Eich619f0fd2013-10-04 18:53:04 +0200802 char *ep;
Simon Glass4101f682016-02-29 15:25:34 -0700803 struct blk_desc *blk_dev_desc = NULL;
Piotr Wilczek8b096232012-12-11 11:09:47 +0100804
Alison Chaiken203f9b42017-07-04 11:19:18 -0700805#ifndef CONFIG_CMD_GPT_RENAME
Lukasz Majewskibbb9ffa2015-11-20 08:06:17 +0100806 if (argc < 4 || argc > 5)
Alison Chaiken203f9b42017-07-04 11:19:18 -0700807#else
808 if (argc < 4 || argc > 6)
809#endif
Piotr Wilczek8b096232012-12-11 11:09:47 +0100810 return CMD_RET_USAGE;
811
Lukasz Majewskibbb9ffa2015-11-20 08:06:17 +0100812 dev = (int)simple_strtoul(argv[3], &ep, 10);
813 if (!ep || ep[0] != '\0') {
814 printf("'%s' is not a number\n", argv[3]);
815 return CMD_RET_USAGE;
816 }
Simon Glassdb1d9e72016-02-29 15:25:42 -0700817 blk_dev_desc = blk_get_dev(argv[2], dev);
Lukasz Majewskibbb9ffa2015-11-20 08:06:17 +0100818 if (!blk_dev_desc) {
819 printf("%s: %s dev %d NOT available\n",
820 __func__, argv[2], dev);
821 return CMD_RET_FAILURE;
822 }
823
Piotr Wilczek8b096232012-12-11 11:09:47 +0100824 if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
Lukasz Majewskibbb9ffa2015-11-20 08:06:17 +0100825 printf("Writing GPT: ");
Przemyslaw Marczak39206382014-04-02 10:20:06 +0200826 ret = gpt_default(blk_dev_desc, argv[4]);
Lukasz Majewskibbb9ffa2015-11-20 08:06:17 +0100827 } else if ((strcmp(argv[1], "verify") == 0)) {
828 ret = gpt_verify(blk_dev_desc, argv[4]);
829 printf("Verify GPT: ");
Alison Chaiken73d6d182017-06-25 16:43:23 -0700830 } else if (strcmp(argv[1], "guid") == 0) {
831 ret = do_disk_guid(blk_dev_desc, argv[4]);
Alison Chaiken09a49932017-07-04 11:18:50 -0700832#ifdef CONFIG_CMD_GPT_RENAME
833 } else if (strcmp(argv[1], "read") == 0) {
834 ret = do_get_gpt_info(blk_dev_desc);
Alison Chaiken203f9b42017-07-04 11:19:18 -0700835 } else if ((strcmp(argv[1], "swap") == 0) ||
836 (strcmp(argv[1], "rename") == 0)) {
837 ret = do_rename_gpt_parts(blk_dev_desc, argv[1], argv[4], argv[5]);
Alison Chaiken09a49932017-07-04 11:18:50 -0700838#endif
Piotr Wilczek8b096232012-12-11 11:09:47 +0100839 } else {
840 return CMD_RET_USAGE;
841 }
Lukasz Majewskibbb9ffa2015-11-20 08:06:17 +0100842
843 if (ret) {
844 printf("error!\n");
845 return CMD_RET_FAILURE;
846 }
847
848 printf("success!\n");
849 return CMD_RET_SUCCESS;
Piotr Wilczek8b096232012-12-11 11:09:47 +0100850}
851
852U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
853 "GUID Partition Table",
Robert P. J. Day1f8b5462013-08-21 11:39:19 -0400854 "<command> <interface> <dev> <partitions_list>\n"
Lukasz Majewski74f889b2015-11-13 07:42:10 +0100855 " - GUID partition table restoration and validity check\n"
856 " Restore or verify GPT information on a device connected\n"
Piotr Wilczek8b096232012-12-11 11:09:47 +0100857 " to interface\n"
Lukasz Majewski74f889b2015-11-13 07:42:10 +0100858 " Example usage:\n"
859 " gpt write mmc 0 $partitions\n"
860 " gpt verify mmc 0 $partitions\n"
Eugeniu Roscad0266092019-04-30 04:53:46 +0200861 " gpt guid <interface> <dev>\n"
Alison Chaiken73d6d182017-06-25 16:43:23 -0700862 " - print disk GUID\n"
Eugeniu Roscad0266092019-04-30 04:53:46 +0200863 " gpt guid <interface> <dev> <varname>\n"
Alison Chaiken73d6d182017-06-25 16:43:23 -0700864 " - set environment variable to disk GUID\n"
865 " Example usage:\n"
866 " gpt guid mmc 0\n"
867 " gpt guid mmc 0 varname\n"
Alison Chaiken203f9b42017-07-04 11:19:18 -0700868#ifdef CONFIG_CMD_GPT_RENAME
869 "gpt partition renaming commands:\n"
Eugeniu Roscad0266092019-04-30 04:53:46 +0200870 " gpt read <interface> <dev>\n"
871 " - read GPT into a data structure for manipulation\n"
872 " gpt swap <interface> <dev> <name1> <name2>\n"
Alison Chaiken203f9b42017-07-04 11:19:18 -0700873 " - change all partitions named name1 to name2\n"
874 " and vice-versa\n"
Eugeniu Roscad0266092019-04-30 04:53:46 +0200875 " gpt rename <interface> <dev> <part> <name>\n"
Alison Chaiken203f9b42017-07-04 11:19:18 -0700876 " - rename the specified partition\n"
877 " Example usage:\n"
878 " gpt swap mmc 0 foo bar\n"
879 " gpt rename mmc 0 3 foo\n"
880#endif
Piotr Wilczek8b096232012-12-11 11:09:47 +0100881);