]>
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+
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 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_STD
);
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 * set_gpt_info(): Fill partition information from string
122 * function allocates memory, remember to free!
124 * @param dev_desc - pointer block device descriptor
125 * @param str_part - pointer to string with partition information
126 * @param str_disk_guid - pointer to pointer to allocated string with disk guid
127 * @param partitions - pointer to pointer to allocated partitions array
128 * @param parts_count - number of partitions
130 * @return - zero on success, otherwise error
133 static int set_gpt_info(block_dev_desc_t
*dev_desc
,
134 const char *str_part
,
135 char **str_disk_guid
,
136 disk_partition_t
**partitions
,
143 disk_partition_t
*parts
;
145 uint64_t size_ll
, start_ll
;
147 debug("%s: lba num: 0x%x %d\n", __func__
,
148 (unsigned int)dev_desc
->lba
, (unsigned int)dev_desc
->lba
);
150 if (str_part
== NULL
)
153 str
= strdup(str_part
);
155 /* extract disk guid */
157 val
= extract_val(str
, "uuid_disk");
159 #ifdef CONFIG_RANDOM_UUID
160 *str_disk_guid
= malloc(UUID_STR_LEN
+ 1);
161 gen_rand_uuid_str(*str_disk_guid
, UUID_STR_FORMAT_STD
);
167 val
= strsep(&val
, ";");
168 if (extract_env(val
, &p
))
170 *str_disk_guid
= strdup(p
);
172 /* Move s to first partition */
182 /* calculate expected number of partitions */
190 /* allocate memory for partitions */
191 parts
= calloc(sizeof(disk_partition_t
), p_count
);
193 /* retrieve partitions data from string */
194 for (i
= 0; i
< p_count
; i
++) {
195 tok
= strsep(&s
, ";");
201 val
= extract_val(tok
, "uuid");
203 /* 'uuid' is optional if random uuid's are enabled */
204 #ifdef CONFIG_RANDOM_UUID
205 gen_rand_uuid_str(parts
[i
].uuid
, UUID_STR_FORMAT_STD
);
211 if (extract_env(val
, &p
))
213 if (strlen(p
) >= sizeof(parts
[i
].uuid
)) {
214 printf("Wrong uuid format for partition %d\n", i
);
218 strcpy((char *)parts
[i
].uuid
, p
);
221 #ifdef CONFIG_PARTITION_TYPE_GUID
223 val
= extract_val(tok
, "type");
225 /* 'type' is optional */
226 if (extract_env(val
, &p
))
228 if (strlen(p
) >= sizeof(parts
[i
].type_guid
)) {
229 printf("Wrong type guid format for partition %d\n",
234 strcpy((char *)parts
[i
].type_guid
, p
);
239 val
= extract_val(tok
, "name");
240 if (!val
) { /* name is mandatory */
244 if (extract_env(val
, &p
))
246 if (strlen(p
) >= sizeof(parts
[i
].name
)) {
250 strcpy((char *)parts
[i
].name
, p
);
254 val
= extract_val(tok
, "size");
255 if (!val
) { /* 'size' is mandatory */
259 if (extract_env(val
, &p
))
261 size_ll
= ustrtoull(p
, &p
, 0);
262 parts
[i
].size
= lldiv(size_ll
, dev_desc
->blksz
);
266 val
= extract_val(tok
, "start");
267 if (val
) { /* start address is optional */
268 if (extract_env(val
, &p
))
270 start_ll
= ustrtoull(p
, &p
, 0);
271 parts
[i
].start
= lldiv(start_ll
, dev_desc
->blksz
);
276 *parts_count
= p_count
;
283 free(*str_disk_guid
);
289 static int gpt_default(block_dev_desc_t
*blk_dev_desc
, const char *str_part
)
294 disk_partition_t
*partitions
= NULL
;
299 /* fill partitions */
300 ret
= set_gpt_info(blk_dev_desc
, str_part
,
301 &str_disk_guid
, &partitions
, &part_count
);
304 printf("No partition list provided\n");
306 printf("Missing disk guid\n");
307 if ((ret
== -3) || (ret
== -4))
308 printf("Partition list incomplete\n");
312 /* save partitions layout to disk */
313 ret
= gpt_restore(blk_dev_desc
, str_disk_guid
, partitions
, part_count
);
321 * do_gpt(): Perform GPT operations
323 * @param cmdtp - command name
328 * @return zero on success; otherwise error
330 static int do_gpt(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
332 int ret
= CMD_RET_SUCCESS
;
335 block_dev_desc_t
*blk_dev_desc
;
338 return CMD_RET_USAGE
;
340 /* command: 'write' */
341 if ((strcmp(argv
[1], "write") == 0) && (argc
== 5)) {
342 dev
= (int)simple_strtoul(argv
[3], &ep
, 10);
343 if (!ep
|| ep
[0] != '\0') {
344 printf("'%s' is not a number\n", argv
[3]);
345 return CMD_RET_USAGE
;
347 blk_dev_desc
= get_dev(argv
[2], dev
);
349 printf("%s: %s dev %d NOT available\n",
350 __func__
, argv
[2], dev
);
351 return CMD_RET_FAILURE
;
354 puts("Writing GPT: ");
356 ret
= gpt_default(blk_dev_desc
, argv
[4]);
359 return CMD_RET_SUCCESS
;
362 return CMD_RET_FAILURE
;
365 return CMD_RET_USAGE
;
370 U_BOOT_CMD(gpt
, CONFIG_SYS_MAXARGS
, 1, do_gpt
,
371 "GUID Partition Table",
372 "<command> <interface> <dev> <partitions_list>\n"
373 " - GUID partition table restoration\n"
374 " Restore GPT information on a device connected\n"