]>
git.ipfire.org Git - people/ms/u-boot.git/blob - common/cmd_gpt.c
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+
17 #include <linux/ctype.h>
20 #ifndef CONFIG_PARTITION_UUIDS
21 #error CONFIG_PARTITION_UUIDS must be enabled for CONFIG_CMD_GPT to be enabled
25 * extract_env(): Expand env name from string format '&{env_name}'
26 * and return pointer to the env (if the env is set)
28 * @param str - pointer to string
29 * @param env - pointer to pointer to extracted env
31 * @return - zero on successful expand and env is set
33 static char extract_env(const char *str
, char **env
)
37 if (!str
|| strlen(str
) < 4)
40 if ((strncmp(str
, "${", 2) == 0) && (str
[strlen(str
) - 1] == '}')) {
44 memset(s
+ strlen(s
) - 1, '\0', 1);
45 memmove(s
, s
+ 2, strlen(s
) - 1);
49 printf("Environmental '%s' not set\n", str
);
50 return -1; /* env not set */
60 * extract_val(): Extract value from a key=value pair list (comma separated).
61 * Only value for the given key is returend.
62 * Function allocates memory for the value, remember to free!
64 * @param str - pointer to string with key=values pairs
65 * @param key - pointer to the key to search for
67 * @return - pointer to allocated string with the value
69 static char *extract_val(const char *str
, const char *key
)
75 strcopy
= strdup(str
);
87 if (strcmp(k
, key
) == 0) {
99 * set_gpt_info(): Fill partition information from string
100 * function allocates memory, remember to free!
102 * @param dev_desc - pointer block device descriptor
103 * @param str_part - pointer to string with partition information
104 * @param str_disk_guid - pointer to pointer to allocated string with disk guid
105 * @param partitions - pointer to pointer to allocated partitions array
106 * @param parts_count - number of partitions
108 * @return - zero on success, otherwise error
111 static int set_gpt_info(block_dev_desc_t
*dev_desc
,
112 const char *str_part
,
113 char **str_disk_guid
,
114 disk_partition_t
**partitions
,
121 disk_partition_t
*parts
;
123 uint64_t size_ll
, start_ll
;
125 debug("%s: MMC lba num: 0x%x %d\n", __func__
,
126 (unsigned int)dev_desc
->lba
, (unsigned int)dev_desc
->lba
);
128 if (str_part
== NULL
)
131 str
= strdup(str_part
);
133 /* extract disk guid */
135 tok
= strsep(&s
, ";");
136 val
= extract_val(tok
, "uuid_disk");
141 if (extract_env(val
, &p
))
143 *str_disk_guid
= strdup(p
);
153 /* calculate expected number of partitions */
161 /* allocate memory for partitions */
162 parts
= calloc(sizeof(disk_partition_t
), p_count
);
164 /* retrieve partitions data from string */
165 for (i
= 0; i
< p_count
; i
++) {
166 tok
= strsep(&s
, ";");
172 val
= extract_val(tok
, "uuid");
173 if (!val
) { /* 'uuid' is mandatory */
177 if (extract_env(val
, &p
))
179 if (strlen(p
) >= sizeof(parts
[i
].uuid
)) {
180 printf("Wrong uuid format for partition %d\n", i
);
184 strcpy((char *)parts
[i
].uuid
, p
);
188 val
= extract_val(tok
, "name");
189 if (!val
) { /* name is mandatory */
193 if (extract_env(val
, &p
))
195 if (strlen(p
) >= sizeof(parts
[i
].name
)) {
199 strcpy((char *)parts
[i
].name
, p
);
203 val
= extract_val(tok
, "size");
204 if (!val
) { /* 'size' is mandatory */
208 if (extract_env(val
, &p
))
210 size_ll
= ustrtoull(p
, &p
, 0);
211 parts
[i
].size
= lldiv(size_ll
, dev_desc
->blksz
);
215 val
= extract_val(tok
, "start");
216 if (val
) { /* start address is optional */
217 if (extract_env(val
, &p
))
219 start_ll
= ustrtoull(p
, &p
, 0);
220 parts
[i
].start
= lldiv(start_ll
, dev_desc
->blksz
);
225 *parts_count
= p_count
;
232 free(*str_disk_guid
);
238 static int gpt_mmc_default(int dev
, const char *str_part
)
243 disk_partition_t
*partitions
= NULL
;
245 struct mmc
*mmc
= find_mmc_device(dev
);
248 printf("%s: mmc dev %d NOT available\n", __func__
, dev
);
249 return CMD_RET_FAILURE
;
255 /* fill partitions */
256 ret
= set_gpt_info(&mmc
->block_dev
, str_part
,
257 &str_disk_guid
, &partitions
, &part_count
);
260 printf("No partition list provided\n");
262 printf("Missing disk guid\n");
263 if ((ret
== -3) || (ret
== -4))
264 printf("Partition list incomplete\n");
268 /* save partitions layout to disk */
269 gpt_restore(&mmc
->block_dev
, str_disk_guid
, partitions
, part_count
);
277 * do_gpt(): Perform GPT operations
279 * @param cmdtp - command name
284 * @return zero on success; otherwise error
286 static int do_gpt(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
288 int ret
= CMD_RET_SUCCESS
;
293 return CMD_RET_USAGE
;
295 /* command: 'write' */
296 if ((strcmp(argv
[1], "write") == 0) && (argc
== 5)) {
298 if (strcmp(argv
[2], "mmc") == 0) {
299 /* check if 'dev' is a number */
300 for (pstr
= argv
[3]; *pstr
!= '\0'; pstr
++)
301 if (!isdigit(*pstr
)) {
302 printf("'%s' is not a number\n",
304 return CMD_RET_USAGE
;
306 dev
= (int)simple_strtoul(argv
[3], NULL
, 10);
308 if (gpt_mmc_default(dev
, argv
[4]))
309 return CMD_RET_FAILURE
;
312 return CMD_RET_USAGE
;
317 U_BOOT_CMD(gpt
, CONFIG_SYS_MAXARGS
, 1, do_gpt
,
318 "GUID Partition Table",
319 "<command> <interface> <dev> <partitions_list>\n"
320 " - GUID partition table restoration\n"
321 " Restore GPT information on a device connected\n"