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>
31 #ifndef CONFIG_PARTITION_UUIDS
32 #error CONFIG_PARTITION_UUIDS must be enabled for CONFIG_CMD_GPT to be enabled
36 * extract_env(): Expand env name from string format '&{env_name}'
37 * and return pointer to the env (if the env is set)
39 * @param str - pointer to string
40 * @param env - pointer to pointer to extracted env
42 * @return - zero on successful expand and env is set
44 static char extract_env(const char *str, char **env)
48 if (!str || strlen(str) < 4)
51 if ((strncmp(str, "${", 2) == 0) && (str[strlen(str) - 1] == '}')) {
55 memset(s + strlen(s) - 1, '\0', 1);
56 memmove(s, s + 2, strlen(s) - 1);
60 printf("Environmental '%s' not set\n", str);
61 return -1; /* env not set */
71 * extract_val(): Extract value from a key=value pair list (comma separated).
72 * Only value for the given key is returend.
73 * Function allocates memory for the value, remember to free!
75 * @param str - pointer to string with key=values pairs
76 * @param key - pointer to the key to search for
78 * @return - pointer to allocated string with the value
80 static char *extract_val(const char *str, const char *key)
86 strcopy = strdup(str);
98 if (strcmp(k, key) == 0) {
110 * set_gpt_info(): Fill partition information from string
111 * function allocates memory, remember to free!
113 * @param dev_desc - pointer block device descriptor
114 * @param str_part - pointer to string with partition information
115 * @param str_disk_guid - pointer to pointer to allocated string with disk guid
116 * @param partitions - pointer to pointer to allocated partitions array
117 * @param parts_count - number of partitions
119 * @return - zero on success, otherwise error
122 static int set_gpt_info(block_dev_desc_t *dev_desc,
123 const char *str_part,
124 char **str_disk_guid,
125 disk_partition_t **partitions,
132 disk_partition_t *parts;
135 debug("%s: MMC lba num: 0x%x %d\n", __func__,
136 (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba);
138 if (str_part == NULL)
141 str = strdup(str_part);
143 /* extract disk guid */
145 tok = strsep(&s, ";");
146 val = extract_val(tok, "uuid_disk");
151 if (extract_env(val, &p))
153 *str_disk_guid = strdup(p);
163 /* calculate expected number of partitions */
171 /* allocate memory for partitions */
172 parts = calloc(sizeof(disk_partition_t), p_count);
174 /* retrive partions data from string */
175 for (i = 0; i < p_count; i++) {
176 tok = strsep(&s, ";");
182 val = extract_val(tok, "uuid");
183 if (!val) { /* 'uuid' is mandatory */
187 if (extract_env(val, &p))
189 if (strlen(p) >= sizeof(parts[i].uuid)) {
190 printf("Wrong uuid format for partition %d\n", i);
194 strcpy((char *)parts[i].uuid, p);
198 val = extract_val(tok, "name");
199 if (!val) { /* name is mandatory */
203 if (extract_env(val, &p))
205 if (strlen(p) >= sizeof(parts[i].name)) {
209 strcpy((char *)parts[i].name, p);
213 val = extract_val(tok, "size");
214 if (!val) { /* 'size' is mandatory */
218 if (extract_env(val, &p))
220 parts[i].size = ustrtoul(p, &p, 0);
221 parts[i].size /= dev_desc->blksz;
225 val = extract_val(tok, "start");
226 if (val) { /* start address is optional */
227 if (extract_env(val, &p))
229 parts[i].start = ustrtoul(p, &p, 0);
230 parts[i].start /= dev_desc->blksz;
235 *parts_count = p_count;
242 free(*str_disk_guid);
248 static int gpt_mmc_default(int dev, const char *str_part)
253 disk_partition_t *partitions = NULL;
255 struct mmc *mmc = find_mmc_device(dev);
258 printf("%s: mmc dev %d NOT available\n", __func__, dev);
259 return CMD_RET_FAILURE;
265 /* fill partitions */
266 ret = set_gpt_info(&mmc->block_dev, str_part,
267 &str_disk_guid, &partitions, &part_count);
270 printf("No partition list provided\n");
272 printf("Missing disk guid\n");
273 if ((ret == -3) || (ret == -4))
274 printf("Partition list incomplete\n");
278 /* save partitions layout to disk */
279 gpt_restore(&mmc->block_dev, str_disk_guid, partitions, part_count);
287 * do_gpt(): Perform GPT operations
289 * @param cmdtp - command name
294 * @return zero on success; otherwise error
296 static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
298 int ret = CMD_RET_SUCCESS;
303 return CMD_RET_USAGE;
305 /* command: 'write' */
306 if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
308 if (strcmp(argv[2], "mmc") == 0) {
309 /* check if 'dev' is a number */
310 for (pstr = argv[3]; *pstr != '\0'; pstr++)
311 if (!isdigit(*pstr)) {
312 printf("'%s' is not a number\n",
314 return CMD_RET_USAGE;
316 dev = (int)simple_strtoul(argv[3], NULL, 10);
318 if (gpt_mmc_default(dev, argv[4]))
319 return CMD_RET_FAILURE;
322 return CMD_RET_USAGE;
327 U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
328 "GUID Partition Table",
329 "<command> <interface> <dev> <partions_list>\n"
330 " - GUID partition table restoration\n"
331 " Restore GPT information on a device connected\n"