2 * cmd_gpt.c -- GPT (GUID Partition Table) handling command
4 * Copyright (C) 2012 Samsung Electronics
5 * author: Lukasz Majewski <l.majewski@samsung.com>
6 * author: Piotr Wilczek <p.wilczek@samsung.com>
8 * SPDX-License-Identifier: GPL-2.0+
16 #include <linux/ctype.h>
19 #ifndef CONFIG_PARTITION_UUIDS
20 #error CONFIG_PARTITION_UUIDS must be enabled for CONFIG_CMD_GPT to be enabled
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 char extract_env(const char *str, char **env)
36 if (!str || strlen(str) < 4)
39 if ((strncmp(str, "${", 2) == 0) && (str[strlen(str) - 1] == '}')) {
43 memset(s + strlen(s) - 1, '\0', 1);
44 memmove(s, s + 2, strlen(s) - 1);
48 printf("Environmental '%s' not set\n", str);
49 return -1; /* env not set */
59 * extract_val(): Extract value from a key=value pair list (comma separated).
60 * Only value for the given key is returend.
61 * Function allocates memory for the value, remember to free!
63 * @param str - pointer to string with key=values pairs
64 * @param key - pointer to the key to search for
66 * @return - pointer to allocated string with the value
68 static char *extract_val(const char *str, const char *key)
74 strcopy = strdup(str);
86 if (strcmp(k, key) == 0) {
98 * set_gpt_info(): Fill partition information from string
99 * function allocates memory, remember to free!
101 * @param dev_desc - pointer block device descriptor
102 * @param str_part - pointer to string with partition information
103 * @param str_disk_guid - pointer to pointer to allocated string with disk guid
104 * @param partitions - pointer to pointer to allocated partitions array
105 * @param parts_count - number of partitions
107 * @return - zero on success, otherwise error
110 static int set_gpt_info(block_dev_desc_t *dev_desc,
111 const char *str_part,
112 char **str_disk_guid,
113 disk_partition_t **partitions,
120 disk_partition_t *parts;
122 uint64_t size_ll, start_ll;
124 debug("%s: lba num: 0x%x %d\n", __func__,
125 (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba);
127 if (str_part == NULL)
130 str = strdup(str_part);
132 /* extract disk guid */
134 tok = strsep(&s, ";");
135 val = extract_val(tok, "uuid_disk");
140 if (extract_env(val, &p))
142 *str_disk_guid = strdup(p);
152 /* calculate expected number of partitions */
160 /* allocate memory for partitions */
161 parts = calloc(sizeof(disk_partition_t), p_count);
163 /* retrieve partitions data from string */
164 for (i = 0; i < p_count; i++) {
165 tok = strsep(&s, ";");
171 val = extract_val(tok, "uuid");
172 if (!val) { /* 'uuid' is mandatory */
176 if (extract_env(val, &p))
178 if (strlen(p) >= sizeof(parts[i].uuid)) {
179 printf("Wrong uuid format for partition %d\n", i);
183 strcpy((char *)parts[i].uuid, p);
187 val = extract_val(tok, "name");
188 if (!val) { /* name is mandatory */
192 if (extract_env(val, &p))
194 if (strlen(p) >= sizeof(parts[i].name)) {
198 strcpy((char *)parts[i].name, p);
202 val = extract_val(tok, "size");
203 if (!val) { /* 'size' is mandatory */
207 if (extract_env(val, &p))
209 size_ll = ustrtoull(p, &p, 0);
210 parts[i].size = lldiv(size_ll, dev_desc->blksz);
214 val = extract_val(tok, "start");
215 if (val) { /* start address is optional */
216 if (extract_env(val, &p))
218 start_ll = ustrtoull(p, &p, 0);
219 parts[i].start = lldiv(start_ll, dev_desc->blksz);
224 *parts_count = p_count;
231 free(*str_disk_guid);
237 static int gpt_default(block_dev_desc_t *blk_dev_desc, const char *str_part)
242 disk_partition_t *partitions = NULL;
247 /* fill partitions */
248 ret = set_gpt_info(blk_dev_desc, str_part,
249 &str_disk_guid, &partitions, &part_count);
252 printf("No partition list provided\n");
254 printf("Missing disk guid\n");
255 if ((ret == -3) || (ret == -4))
256 printf("Partition list incomplete\n");
260 /* save partitions layout to disk */
261 gpt_restore(blk_dev_desc, str_disk_guid, partitions, part_count);
269 * do_gpt(): Perform GPT operations
271 * @param cmdtp - command name
276 * @return zero on success; otherwise error
278 static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
280 int ret = CMD_RET_SUCCESS;
283 block_dev_desc_t *blk_dev_desc;
286 return CMD_RET_USAGE;
288 /* command: 'write' */
289 if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
290 dev = (int)simple_strtoul(argv[3], &ep, 10);
291 if (!ep || ep[0] != '\0') {
292 printf("'%s' is not a number\n", argv[3]);
293 return CMD_RET_USAGE;
295 blk_dev_desc = get_dev(argv[2], dev);
297 printf("%s: %s dev %d NOT available\n",
298 __func__, argv[2], dev);
299 return CMD_RET_FAILURE;
302 if (gpt_default(blk_dev_desc, argv[4]))
303 return CMD_RET_FAILURE;
305 return CMD_RET_USAGE;
310 U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
311 "GUID Partition Table",
312 "<command> <interface> <dev> <partitions_list>\n"
313 " - GUID partition table restoration\n"
314 " Restore GPT information on a device connected\n"