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>
23 #ifndef CONFIG_PARTITION_UUIDS
24 #error CONFIG_PARTITION_UUIDS must be enabled for CONFIG_CMD_GPT to be enabled
28 * extract_env(): Expand env name from string format '&{env_name}'
29 * and return pointer to the env (if the env is set)
31 * @param str - pointer to string
32 * @param env - pointer to pointer to extracted env
34 * @return - zero on successful expand and env is set
36 static int extract_env(const char *str, char **env)
40 #ifdef CONFIG_RANDOM_UUID
41 char uuid_str[UUID_STR_LEN + 1];
44 if (!str || strlen(str) < 4)
47 if (!((strncmp(str, "${", 2) == 0) && (str[strlen(str) - 1] == '}')))
54 memset(s + strlen(s) - 1, '\0', 1);
55 memmove(s, s + 2, strlen(s) - 1);
59 #ifdef CONFIG_RANDOM_UUID
60 debug("%s unset. ", str);
61 gen_rand_uuid_str(uuid_str, UUID_STR_FORMAT_STD);
66 debug("Set to random.\n");
69 debug("Can't get random UUID.\n");
72 debug("%s unset.\n", str);
75 debug("%s get from environment.\n", str);
86 * extract_val(): Extract value from a key=value pair list (comma separated).
87 * Only value for the given key is returend.
88 * Function allocates memory for the value, remember to free!
90 * @param str - pointer to string with key=values pairs
91 * @param key - pointer to the key to search for
93 * @return - pointer to allocated string with the value
95 static char *extract_val(const char *str, const char *key)
101 strcopy = strdup(str);
113 if (strcmp(k, key) == 0) {
125 * found_key(): Found key without value in parameter list (comma separated).
127 * @param str - pointer to string with key
128 * @param key - pointer to the key to search for
130 * @return - true on found key
132 static bool found_key(const char *str, const char *key)
138 strcopy = strdup(str);
147 if (strcmp(k, key) == 0) {
159 * set_gpt_info(): Fill partition information from string
160 * function allocates memory, remember to free!
162 * @param dev_desc - pointer block device descriptor
163 * @param str_part - pointer to string with partition information
164 * @param str_disk_guid - pointer to pointer to allocated string with disk guid
165 * @param partitions - pointer to pointer to allocated partitions array
166 * @param parts_count - number of partitions
168 * @return - zero on success, otherwise error
171 static int set_gpt_info(block_dev_desc_t *dev_desc,
172 const char *str_part,
173 char **str_disk_guid,
174 disk_partition_t **partitions,
181 disk_partition_t *parts;
183 uint64_t size_ll, start_ll;
185 debug("%s: lba num: 0x%x %d\n", __func__,
186 (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba);
188 if (str_part == NULL)
191 str = strdup(str_part);
193 /* extract disk guid */
195 val = extract_val(str, "uuid_disk");
197 #ifdef CONFIG_RANDOM_UUID
198 *str_disk_guid = malloc(UUID_STR_LEN + 1);
199 gen_rand_uuid_str(*str_disk_guid, UUID_STR_FORMAT_STD);
205 val = strsep(&val, ";");
206 if (extract_env(val, &p))
208 *str_disk_guid = strdup(p);
210 /* Move s to first partition */
220 /* calculate expected number of partitions */
228 /* allocate memory for partitions */
229 parts = calloc(sizeof(disk_partition_t), p_count);
231 /* retrieve partitions data from string */
232 for (i = 0; i < p_count; i++) {
233 tok = strsep(&s, ";");
239 val = extract_val(tok, "uuid");
241 /* 'uuid' is optional if random uuid's are enabled */
242 #ifdef CONFIG_RANDOM_UUID
243 gen_rand_uuid_str(parts[i].uuid, UUID_STR_FORMAT_STD);
249 if (extract_env(val, &p))
251 if (strlen(p) >= sizeof(parts[i].uuid)) {
252 printf("Wrong uuid format for partition %d\n", i);
256 strcpy((char *)parts[i].uuid, p);
259 #ifdef CONFIG_PARTITION_TYPE_GUID
261 val = extract_val(tok, "type");
263 /* 'type' is optional */
264 if (extract_env(val, &p))
266 if (strlen(p) >= sizeof(parts[i].type_guid)) {
267 printf("Wrong type guid format for partition %d\n",
272 strcpy((char *)parts[i].type_guid, p);
277 val = extract_val(tok, "name");
278 if (!val) { /* name is mandatory */
282 if (extract_env(val, &p))
284 if (strlen(p) >= sizeof(parts[i].name)) {
288 strcpy((char *)parts[i].name, p);
292 val = extract_val(tok, "size");
293 if (!val) { /* 'size' is mandatory */
297 if (extract_env(val, &p))
299 size_ll = ustrtoull(p, &p, 0);
300 parts[i].size = lldiv(size_ll, dev_desc->blksz);
304 val = extract_val(tok, "start");
305 if (val) { /* start address is optional */
306 if (extract_env(val, &p))
308 start_ll = ustrtoull(p, &p, 0);
309 parts[i].start = lldiv(start_ll, dev_desc->blksz);
314 if (found_key(tok, "bootable"))
315 parts[i].bootable = 1;
318 *parts_count = p_count;
325 free(*str_disk_guid);
331 static int gpt_default(block_dev_desc_t *blk_dev_desc, const char *str_part)
336 disk_partition_t *partitions = NULL;
338 /* fill partitions */
339 ret = set_gpt_info(blk_dev_desc, str_part,
340 &str_disk_guid, &partitions, &part_count);
343 printf("No partition list provided\n");
345 printf("Missing disk guid\n");
346 if ((ret == -3) || (ret == -4))
347 printf("Partition list incomplete\n");
351 /* save partitions layout to disk */
352 ret = gpt_restore(blk_dev_desc, str_disk_guid, partitions, part_count);
359 static int gpt_verify(block_dev_desc_t *blk_dev_desc, const char *str_part)
361 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1,
362 blk_dev_desc->blksz);
363 disk_partition_t *partitions = NULL;
364 gpt_entry *gpt_pte = NULL;
369 /* fill partitions */
370 ret = set_gpt_info(blk_dev_desc, str_part,
371 &str_disk_guid, &partitions, &part_count);
374 printf("No partition list provided - only basic check\n");
375 ret = gpt_verify_headers(blk_dev_desc, gpt_head,
380 printf("Missing disk guid\n");
381 if ((ret == -3) || (ret == -4))
382 printf("Partition list incomplete\n");
386 /* Check partition layout with provided pattern */
387 ret = gpt_verify_partitions(blk_dev_desc, partitions, part_count,
397 * do_gpt(): Perform GPT operations
399 * @param cmdtp - command name
404 * @return zero on success; otherwise error
406 static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
408 int ret = CMD_RET_SUCCESS;
411 block_dev_desc_t *blk_dev_desc = NULL;
413 if (argc < 4 || argc > 5)
414 return CMD_RET_USAGE;
416 dev = (int)simple_strtoul(argv[3], &ep, 10);
417 if (!ep || ep[0] != '\0') {
418 printf("'%s' is not a number\n", argv[3]);
419 return CMD_RET_USAGE;
421 blk_dev_desc = get_dev(argv[2], dev);
423 printf("%s: %s dev %d NOT available\n",
424 __func__, argv[2], dev);
425 return CMD_RET_FAILURE;
428 if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
429 printf("Writing GPT: ");
430 ret = gpt_default(blk_dev_desc, argv[4]);
431 } else if ((strcmp(argv[1], "verify") == 0)) {
432 ret = gpt_verify(blk_dev_desc, argv[4]);
433 printf("Verify GPT: ");
435 return CMD_RET_USAGE;
440 return CMD_RET_FAILURE;
443 printf("success!\n");
444 return CMD_RET_SUCCESS;
447 U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
448 "GUID Partition Table",
449 "<command> <interface> <dev> <partitions_list>\n"
450 " - GUID partition table restoration and validity check\n"
451 " Restore or verify GPT information on a device connected\n"
454 " gpt write mmc 0 $partitions\n"
455 " gpt verify mmc 0 $partitions\n"