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 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <linux/ctype.h>
32 #ifndef CONFIG_PARTITION_UUIDS
33 #error CONFIG_PARTITION_UUIDS must be enabled for CONFIG_CMD_GPT to be enabled
37 * extract_env(): Expand env name from string format '&{env_name}'
38 * and return pointer to the env (if the env is set)
40 * @param str - pointer to string
41 * @param env - pointer to pointer to extracted env
43 * @return - zero on successful expand and env is set
45 static char extract_env(const char *str, char **env)
49 if (!str || strlen(str) < 4)
52 if ((strncmp(str, "${", 2) == 0) && (str[strlen(str) - 1] == '}')) {
56 memset(s + strlen(s) - 1, '\0', 1);
57 memmove(s, s + 2, strlen(s) - 1);
61 printf("Environmental '%s' not set\n", str);
62 return -1; /* env not set */
72 * extract_val(): Extract value from a key=value pair list (comma separated).
73 * Only value for the given key is returend.
74 * Function allocates memory for the value, remember to free!
76 * @param str - pointer to string with key=values pairs
77 * @param key - pointer to the key to search for
79 * @return - pointer to allocated string with the value
81 static char *extract_val(const char *str, const char *key)
87 strcopy = strdup(str);
99 if (strcmp(k, key) == 0) {
111 * set_gpt_info(): Fill partition information from string
112 * function allocates memory, remember to free!
114 * @param dev_desc - pointer block device descriptor
115 * @param str_part - pointer to string with partition information
116 * @param str_disk_guid - pointer to pointer to allocated string with disk guid
117 * @param partitions - pointer to pointer to allocated partitions array
118 * @param parts_count - number of partitions
120 * @return - zero on success, otherwise error
123 static int set_gpt_info(block_dev_desc_t *dev_desc,
124 const char *str_part,
125 char **str_disk_guid,
126 disk_partition_t **partitions,
133 disk_partition_t *parts;
135 uint64_t size_ll, start_ll;
137 debug("%s: MMC lba num: 0x%x %d\n", __func__,
138 (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba);
140 if (str_part == NULL)
143 str = strdup(str_part);
145 /* extract disk guid */
147 tok = strsep(&s, ";");
148 val = extract_val(tok, "uuid_disk");
153 if (extract_env(val, &p))
155 *str_disk_guid = strdup(p);
165 /* calculate expected number of partitions */
173 /* allocate memory for partitions */
174 parts = calloc(sizeof(disk_partition_t), p_count);
176 /* retrive partions data from string */
177 for (i = 0; i < p_count; i++) {
178 tok = strsep(&s, ";");
184 val = extract_val(tok, "uuid");
185 if (!val) { /* 'uuid' is mandatory */
189 if (extract_env(val, &p))
191 if (strlen(p) >= sizeof(parts[i].uuid)) {
192 printf("Wrong uuid format for partition %d\n", i);
196 strcpy((char *)parts[i].uuid, p);
200 val = extract_val(tok, "name");
201 if (!val) { /* name is mandatory */
205 if (extract_env(val, &p))
207 if (strlen(p) >= sizeof(parts[i].name)) {
211 strcpy((char *)parts[i].name, p);
215 val = extract_val(tok, "size");
216 if (!val) { /* 'size' is mandatory */
220 if (extract_env(val, &p))
222 size_ll = ustrtoull(p, &p, 0);
223 parts[i].size = lldiv(size_ll, dev_desc->blksz);
227 val = extract_val(tok, "start");
228 if (val) { /* start address is optional */
229 if (extract_env(val, &p))
231 start_ll = ustrtoull(p, &p, 0);
232 parts[i].start = lldiv(start_ll, dev_desc->blksz);
237 *parts_count = p_count;
244 free(*str_disk_guid);
250 static int gpt_mmc_default(int dev, const char *str_part)
255 disk_partition_t *partitions = NULL;
257 struct mmc *mmc = find_mmc_device(dev);
260 printf("%s: mmc dev %d NOT available\n", __func__, dev);
261 return CMD_RET_FAILURE;
267 /* fill partitions */
268 ret = set_gpt_info(&mmc->block_dev, str_part,
269 &str_disk_guid, &partitions, &part_count);
272 printf("No partition list provided\n");
274 printf("Missing disk guid\n");
275 if ((ret == -3) || (ret == -4))
276 printf("Partition list incomplete\n");
280 /* save partitions layout to disk */
281 gpt_restore(&mmc->block_dev, str_disk_guid, partitions, part_count);
289 * do_gpt(): Perform GPT operations
291 * @param cmdtp - command name
296 * @return zero on success; otherwise error
298 static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
300 int ret = CMD_RET_SUCCESS;
305 return CMD_RET_USAGE;
307 /* command: 'write' */
308 if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
310 if (strcmp(argv[2], "mmc") == 0) {
311 /* check if 'dev' is a number */
312 for (pstr = argv[3]; *pstr != '\0'; pstr++)
313 if (!isdigit(*pstr)) {
314 printf("'%s' is not a number\n",
316 return CMD_RET_USAGE;
318 dev = (int)simple_strtoul(argv[3], NULL, 10);
320 if (gpt_mmc_default(dev, argv[4]))
321 return CMD_RET_FAILURE;
324 return CMD_RET_USAGE;
329 U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
330 "GUID Partition Table",
331 "<command> <interface> <dev> <partions_list>\n"
332 " - GUID partition table restoration\n"
333 " Restore GPT information on a device connected\n"