2 * cmd_gpt.c -- GPT (GUID Partition Table) handling command
5 * Lukasz Majewski <l.majewski@majess.pl>
7 * Copyright (C) 2012 Samsung Electronics
8 * author: Lukasz Majewski <l.majewski@samsung.com>
9 * author: Piotr Wilczek <p.wilczek@samsung.com>
11 * SPDX-License-Identifier: GPL-2.0+
19 #include <linux/ctype.h>
24 * extract_env(): Expand env name from string format '&{env_name}'
25 * and return pointer to the env (if the env is set)
27 * @param str - pointer to string
28 * @param env - pointer to pointer to extracted env
30 * @return - zero on successful expand and env is set
32 static int extract_env(const char *str, char **env)
36 #ifdef CONFIG_RANDOM_UUID
37 char uuid_str[UUID_STR_LEN + 1];
40 if (!str || strlen(str) < 4)
43 if (!((strncmp(str, "${", 2) == 0) && (str[strlen(str) - 1] == '}')))
50 memset(s + strlen(s) - 1, '\0', 1);
51 memmove(s, s + 2, strlen(s) - 1);
55 #ifdef CONFIG_RANDOM_UUID
56 debug("%s unset. ", str);
57 gen_rand_uuid_str(uuid_str, UUID_STR_FORMAT_GUID);
62 debug("Set to random.\n");
65 debug("Can't get random UUID.\n");
68 debug("%s unset.\n", str);
71 debug("%s get from environment.\n", str);
82 * extract_val(): Extract value from a key=value pair list (comma separated).
83 * Only value for the given key is returend.
84 * Function allocates memory for the value, remember to free!
86 * @param str - pointer to string with key=values pairs
87 * @param key - pointer to the key to search for
89 * @return - pointer to allocated string with the value
91 static char *extract_val(const char *str, const char *key)
97 strcopy = strdup(str);
109 if (strcmp(k, key) == 0) {
121 * found_key(): Found key without value in parameter list (comma separated).
123 * @param str - pointer to string with key
124 * @param key - pointer to the key to search for
126 * @return - true on found key
128 static bool found_key(const char *str, const char *key)
134 strcopy = strdup(str);
143 if (strcmp(k, key) == 0) {
155 * set_gpt_info(): Fill partition information from string
156 * function allocates memory, remember to free!
158 * @param dev_desc - pointer block device descriptor
159 * @param str_part - pointer to string with partition information
160 * @param str_disk_guid - pointer to pointer to allocated string with disk guid
161 * @param partitions - pointer to pointer to allocated partitions array
162 * @param parts_count - number of partitions
164 * @return - zero on success, otherwise error
167 static int set_gpt_info(struct blk_desc *dev_desc,
168 const char *str_part,
169 char **str_disk_guid,
170 disk_partition_t **partitions,
177 disk_partition_t *parts;
179 uint64_t size_ll, start_ll;
182 debug("%s: lba num: 0x%x %d\n", __func__,
183 (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba);
185 if (str_part == NULL)
188 str = strdup(str_part);
190 /* extract disk guid */
192 val = extract_val(str, "uuid_disk");
194 #ifdef CONFIG_RANDOM_UUID
195 *str_disk_guid = malloc(UUID_STR_LEN + 1);
196 gen_rand_uuid_str(*str_disk_guid, UUID_STR_FORMAT_STD);
202 val = strsep(&val, ";");
203 if (extract_env(val, &p))
205 *str_disk_guid = strdup(p);
207 /* Move s to first partition */
217 /* calculate expected number of partitions */
225 /* allocate memory for partitions */
226 parts = calloc(sizeof(disk_partition_t), p_count);
228 /* retrieve partitions data from string */
229 for (i = 0; i < p_count; i++) {
230 tok = strsep(&s, ";");
236 val = extract_val(tok, "uuid");
238 /* 'uuid' is optional if random uuid's are enabled */
239 #ifdef CONFIG_RANDOM_UUID
240 gen_rand_uuid_str(parts[i].uuid, UUID_STR_FORMAT_STD);
246 if (extract_env(val, &p))
248 if (strlen(p) >= sizeof(parts[i].uuid)) {
249 printf("Wrong uuid format for partition %d\n", i);
253 strcpy((char *)parts[i].uuid, p);
256 #ifdef CONFIG_PARTITION_TYPE_GUID
258 val = extract_val(tok, "type");
260 /* 'type' is optional */
261 if (extract_env(val, &p))
263 if (strlen(p) >= sizeof(parts[i].type_guid)) {
264 printf("Wrong type guid format for partition %d\n",
269 strcpy((char *)parts[i].type_guid, p);
274 val = extract_val(tok, "name");
275 if (!val) { /* name is mandatory */
279 if (extract_env(val, &p))
281 if (strlen(p) >= sizeof(parts[i].name)) {
285 strcpy((char *)parts[i].name, p);
289 val = extract_val(tok, "size");
290 if (!val) { /* 'size' is mandatory */
294 if (extract_env(val, &p))
296 if ((strcmp(p, "-") == 0)) {
297 /* Let part efi module to auto extend the size */
300 size_ll = ustrtoull(p, &p, 0);
301 parts[i].size = lldiv(size_ll, dev_desc->blksz);
307 val = extract_val(tok, "start");
308 if (val) { /* start address is optional */
309 if (extract_env(val, &p))
311 start_ll = ustrtoull(p, &p, 0);
312 parts[i].start = lldiv(start_ll, dev_desc->blksz);
316 offset += parts[i].size + parts[i].start;
319 if (found_key(tok, "bootable"))
320 parts[i].bootable = 1;
323 *parts_count = p_count;
330 free(*str_disk_guid);
336 static int gpt_default(struct blk_desc *blk_dev_desc, const char *str_part)
341 disk_partition_t *partitions = NULL;
343 /* fill partitions */
344 ret = set_gpt_info(blk_dev_desc, str_part,
345 &str_disk_guid, &partitions, &part_count);
348 printf("No partition list provided\n");
350 printf("Missing disk guid\n");
351 if ((ret == -3) || (ret == -4))
352 printf("Partition list incomplete\n");
356 /* save partitions layout to disk */
357 ret = gpt_restore(blk_dev_desc, str_disk_guid, partitions, part_count);
364 static int gpt_verify(struct blk_desc *blk_dev_desc, const char *str_part)
366 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1,
367 blk_dev_desc->blksz);
368 disk_partition_t *partitions = NULL;
369 gpt_entry *gpt_pte = NULL;
374 /* fill partitions */
375 ret = set_gpt_info(blk_dev_desc, str_part,
376 &str_disk_guid, &partitions, &part_count);
379 printf("No partition list provided - only basic check\n");
380 ret = gpt_verify_headers(blk_dev_desc, gpt_head,
385 printf("Missing disk guid\n");
386 if ((ret == -3) || (ret == -4))
387 printf("Partition list incomplete\n");
391 /* Check partition layout with provided pattern */
392 ret = gpt_verify_partitions(blk_dev_desc, partitions, part_count,
402 * do_gpt(): Perform GPT operations
404 * @param cmdtp - command name
409 * @return zero on success; otherwise error
411 static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
413 int ret = CMD_RET_SUCCESS;
416 struct blk_desc *blk_dev_desc = NULL;
418 if (argc < 4 || argc > 5)
419 return CMD_RET_USAGE;
421 dev = (int)simple_strtoul(argv[3], &ep, 10);
422 if (!ep || ep[0] != '\0') {
423 printf("'%s' is not a number\n", argv[3]);
424 return CMD_RET_USAGE;
426 blk_dev_desc = blk_get_dev(argv[2], dev);
428 printf("%s: %s dev %d NOT available\n",
429 __func__, argv[2], dev);
430 return CMD_RET_FAILURE;
433 if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
434 printf("Writing GPT: ");
435 ret = gpt_default(blk_dev_desc, argv[4]);
436 } else if ((strcmp(argv[1], "verify") == 0)) {
437 ret = gpt_verify(blk_dev_desc, argv[4]);
438 printf("Verify GPT: ");
440 return CMD_RET_USAGE;
445 return CMD_RET_FAILURE;
448 printf("success!\n");
449 return CMD_RET_SUCCESS;
452 U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
453 "GUID Partition Table",
454 "<command> <interface> <dev> <partitions_list>\n"
455 " - GUID partition table restoration and validity check\n"
456 " Restore or verify GPT information on a device connected\n"
459 " gpt write mmc 0 $partitions\n"
460 " gpt verify mmc 0 $partitions\n"