]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_gpt.c
Merge branch 'tx6-devel' into uboot-merge
[karo-tx-uboot.git] / common / cmd_gpt.c
1 /*
2  * cmd_gpt.c -- GPT (GUID Partition Table) handling command
3  *
4  * Copyright (C) 2012 Samsung Electronics
5  * author: Lukasz Majewski <l.majewski@samsung.com>
6  * author: Piotr Wilczek <p.wilczek@samsung.com>
7  *
8  * SPDX-License-Identifier:     GPL-2.0+
9  */
10
11 #include <common.h>
12 #include <malloc.h>
13 #include <command.h>
14 #include <mmc.h>
15 #include <part_efi.h>
16 #include <exports.h>
17 #include <linux/ctype.h>
18 #include <div64.h>
19
20 #ifndef CONFIG_PARTITION_UUIDS
21 #error CONFIG_PARTITION_UUIDS must be enabled for CONFIG_CMD_GPT to be enabled
22 #endif
23
24 /**
25  * extract_env(): Expand env name from string format '&{env_name}'
26  *                and return pointer to the env (if the env is set)
27  *
28  * @param str - pointer to string
29  * @param env - pointer to pointer to extracted env
30  *
31  * @return - zero on successful expand and env is set
32  */
33 static char extract_env(const char *str, char **env)
34 {
35         char *e, *s;
36
37         if (!str || strlen(str) < 4)
38                 return -1;
39
40         if ((strncmp(str, "${", 2) == 0) && (str[strlen(str) - 1] == '}')) {
41                 s = strdup(str);
42                 if (s == NULL)
43                         return -1;
44                 memset(s + strlen(s) - 1, '\0', 1);
45                 memmove(s, s + 2, strlen(s) - 1);
46                 e = getenv(s);
47                 free(s);
48                 if (e == NULL) {
49                         printf("Environmental '%s' not set\n", str);
50                         return -1; /* env not set */
51                 }
52                 *env = e;
53                 return 0;
54         }
55
56         return -1;
57 }
58
59 /**
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!
63  *
64  * @param str - pointer to string with key=values pairs
65  * @param key - pointer to the key to search for
66  *
67  * @return - pointer to allocated string with the value
68  */
69 static char *extract_val(const char *str, const char *key)
70 {
71         char *v, *k;
72         char *s, *strcopy;
73         char *new = NULL;
74
75         strcopy = strdup(str);
76         if (strcopy == NULL)
77                 return NULL;
78
79         s = strcopy;
80         while (s) {
81                 v = strsep(&s, ",");
82                 if (!v)
83                         break;
84                 k = strsep(&v, "=");
85                 if (!k)
86                         break;
87                 if  (strcmp(k, key) == 0) {
88                         new = strdup(v);
89                         break;
90                 }
91         }
92
93         free(strcopy);
94
95         return new;
96 }
97
98 /**
99  * set_gpt_info(): Fill partition information from string
100  *              function allocates memory, remember to free!
101  *
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
107  *
108  * @return - zero on success, otherwise error
109  *
110  */
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,
115                         u8 *parts_count)
116 {
117         char *tok, *str, *s;
118         int i;
119         char *val, *p;
120         int p_count;
121         disk_partition_t *parts;
122         char *guid_str;
123         int errno = 0;
124         uint64_t size_ll, start_ll;
125
126         debug("%s: MMC lba num: 0x%x %d\n", __func__,
127               (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba);
128
129         if (str_part == NULL)
130                 return -1;
131
132         str = strdup(str_part);
133
134         /* extract disk guid */
135         s = str;
136         tok = strsep(&s, ";");
137         val = extract_val(tok, "uuid_disk");
138         if (!val) {
139                 errno = -2;
140                 goto free_str;
141         }
142         if (extract_env(val, &p) == 0)
143                 guid_str = strdup(p);
144         else
145                 guid_str = strdup(val);
146
147         free(val);
148
149         if (strlen(s) == 0) {
150                 errno = -3;
151                 goto free_guid;
152         }
153
154         i = strlen(s) - 1;
155         if (s[i] == ';')
156                 s[i] = '\0';
157
158         /* calculate expected number of partitions */
159         p_count = 1;
160         p = s;
161         while (*p) {
162                 if (*p++ == ';')
163                         p_count++;
164         }
165
166         /* allocate memory for partitions */
167         parts = calloc(sizeof(disk_partition_t), p_count);
168
169         /* retrive partions data from string */
170         for (i = 0; i < p_count; i++) {
171                 tok = strsep(&s, ";");
172
173                 if (tok == NULL)
174                         break;
175
176                 /* uuid */
177                 val = extract_val(tok, "uuid");
178                 if (!val) { /* 'uuid' is mandatory */
179                         errno = -4;
180                         goto err;
181                 }
182                 if (extract_env(val, &p))
183                         p = val;
184                 if (strlen(p) >= sizeof(parts[i].uuid)) {
185                         printf("Wrong uuid format for partition %d\n", i);
186                         errno = -4;
187                         goto err;
188                 }
189                 strcpy((char *)parts[i].uuid, p);
190                 free(val);
191
192                 /* name */
193                 val = extract_val(tok, "name");
194                 if (!val) { /* name is mandatory */
195                         errno = -4;
196                         goto err;
197                 }
198                 if (extract_env(val, &p))
199                         p = val;
200                 if (strlen(p) >= sizeof(parts[i].name)) {
201                         errno = -4;
202                         goto err;
203                 }
204                 strcpy((char *)parts[i].name, p);
205                 free(val);
206
207                 /* size */
208                 val = extract_val(tok, "size");
209                 if (!val) { /* 'size' is mandatory */
210                         errno = -4;
211                         goto err;
212                 }
213                 if (extract_env(val, &p))
214                         p = val;
215                 size_ll = ustrtoull(p, &p, 0);
216                 parts[i].size = lldiv(size_ll, dev_desc->blksz);
217                 free(val);
218
219                 /* start address */
220                 val = extract_val(tok, "start");
221                 if (val) { /* start address is optional */
222                         if (extract_env(val, &p))
223                                 p = val;
224                         start_ll = ustrtoull(p, &p, 0);
225                         parts[i].start = lldiv(start_ll, dev_desc->blksz);
226                         free(val);
227                 }
228         }
229
230         *str_disk_guid = guid_str;
231         *parts_count = p_count;
232         *partitions = parts;
233         free(str);
234
235         return 0;
236
237 err:
238         free(parts);
239 free_guid:
240         free(guid_str);
241 free_str:
242         free(str);
243
244         *str_disk_guid = NULL;
245         *parts_count = 0;
246         *partitions = NULL;
247         return errno;
248 }
249
250 static int gpt_mmc_default(int dev, const char *str_part)
251 {
252         int ret;
253         char *str_disk_guid;
254         u8 part_count = 0;
255         disk_partition_t *partitions = NULL;
256
257         struct mmc *mmc = find_mmc_device(dev);
258
259         if (mmc == NULL) {
260                 printf("%s: mmc dev %d NOT available\n", __func__, dev);
261                 return CMD_RET_FAILURE;
262         }
263
264         if (!str_part)
265                 return -1;
266
267         /* fill partitions */
268         ret = set_gpt_info(&mmc->block_dev, str_part,
269                         &str_disk_guid, &partitions, &part_count);
270         if (ret) {
271                 if (ret == -1)
272                         printf("No partition list provided\n");
273                 if (ret == -2)
274                         printf("Missing disk guid\n");
275                 if ((ret == -3) || (ret == -4))
276                         printf("Partition list incomplete\n");
277                 return -1;
278         }
279
280         /* save partitions layout to disk */
281         gpt_restore(&mmc->block_dev, str_disk_guid, partitions, part_count);
282         free(str_disk_guid);
283         free(partitions);
284
285         return 0;
286 }
287
288 /**
289  * do_gpt(): Perform GPT operations
290  *
291  * @param cmdtp - command name
292  * @param flag
293  * @param argc
294  * @param argv
295  *
296  * @return zero on success; otherwise error
297  */
298 static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
299 {
300         int ret = CMD_RET_SUCCESS;
301         int dev = 0;
302         char *pstr;
303
304         if (argc < 5)
305                 return CMD_RET_USAGE;
306
307         /* command: 'write' */
308         if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
309                 /* device: 'mmc' */
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",
315                                                 argv[3]);
316                                         return CMD_RET_USAGE;
317                                 }
318                         dev = (int)simple_strtoul(argv[3], NULL, 10);
319                         /* write to mmc */
320                         if (gpt_mmc_default(dev, argv[4]))
321                                 return CMD_RET_FAILURE;
322                 }
323         } else {
324                 return CMD_RET_USAGE;
325         }
326         return ret;
327 }
328
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"
334         " to interface\n"
335 );