]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_gpt.c
mmc: omap_hsmmc: enable 8bit interface for eMMC for AM43xx
[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 <part_efi.h>
15 #include <exports.h>
16 #include <linux/ctype.h>
17 #include <div64.h>
18
19 #ifndef CONFIG_PARTITION_UUIDS
20 #error CONFIG_PARTITION_UUIDS must be enabled for CONFIG_CMD_GPT to be enabled
21 #endif
22
23 /**
24  * extract_env(): Expand env name from string format '&{env_name}'
25  *                and return pointer to the env (if the env is set)
26  *
27  * @param str - pointer to string
28  * @param env - pointer to pointer to extracted env
29  *
30  * @return - zero on successful expand and env is set
31  */
32 static int extract_env(const char *str, char **env)
33 {
34         int ret = -1;
35         char *e, *s;
36 #ifdef CONFIG_RANDOM_UUID
37         char uuid_str[UUID_STR_LEN + 1];
38 #endif
39
40         if (!str || strlen(str) < 4)
41                 return -1;
42
43         if (!((strncmp(str, "${", 2) == 0) && (str[strlen(str) - 1] == '}')))
44                 return -1;
45
46         s = strdup(str);
47         if (s == NULL)
48                 return -1;
49
50         memset(s + strlen(s) - 1, '\0', 1);
51         memmove(s, s + 2, strlen(s) - 1);
52
53         e = getenv(s);
54         if (e == NULL) {
55 #ifdef CONFIG_RANDOM_UUID
56                 debug("%s unset. ", str);
57                 gen_rand_uuid_str(uuid_str, UUID_STR_FORMAT_STD);
58                 setenv(s, uuid_str);
59
60                 e = getenv(s);
61                 if (e) {
62                         debug("Set to random.\n");
63                         ret = 0;
64                 } else {
65                         debug("Can't get random UUID.\n");
66                 }
67 #else
68                 debug("%s unset.\n", str);
69 #endif
70         } else {
71                 debug("%s get from environment.\n", str);
72                 ret = 0;
73         }
74
75         *env = e;
76         free(s);
77
78         return ret;
79 }
80
81 /**
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!
85  *
86  * @param str - pointer to string with key=values pairs
87  * @param key - pointer to the key to search for
88  *
89  * @return - pointer to allocated string with the value
90  */
91 static char *extract_val(const char *str, const char *key)
92 {
93         char *v, *k;
94         char *s, *strcopy;
95         char *new = NULL;
96
97         strcopy = strdup(str);
98         if (strcopy == NULL)
99                 return NULL;
100
101         s = strcopy;
102         while (s) {
103                 v = strsep(&s, ",");
104                 if (!v)
105                         break;
106                 k = strsep(&v, "=");
107                 if (!k)
108                         break;
109                 if  (strcmp(k, key) == 0) {
110                         new = strdup(v);
111                         break;
112                 }
113         }
114
115         free(strcopy);
116
117         return new;
118 }
119
120 /**
121  * set_gpt_info(): Fill partition information from string
122  *              function allocates memory, remember to free!
123  *
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
129  *
130  * @return - zero on success, otherwise error
131  *
132  */
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,
137                         u8 *parts_count)
138 {
139         char *tok, *str, *s;
140         int i;
141         char *val, *p;
142         int p_count;
143         disk_partition_t *parts;
144         char *guid_str;
145         int errno = 0;
146         uint64_t size_ll, start_ll;
147
148         debug("%s:  lba num: 0x%x %d\n", __func__,
149               (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba);
150
151         if (str_part == NULL)
152                 return -1;
153
154         str = strdup(str_part);
155
156         /* extract disk guid */
157         s = str;
158         val = extract_val(str, "uuid_disk");
159         if (!val) {
160 #ifdef CONFIG_RANDOM_UUID
161                 *str_disk_guid = malloc(UUID_STR_LEN + 1);
162                 gen_rand_uuid_str(*str_disk_guid, UUID_STR_FORMAT_STD);
163 #else
164                 free(str);
165                 return -2;
166 #endif
167         } else {
168                 char *v = val;
169
170                 v = strsep(&v, ";");
171                 if (extract_env(v, &p))
172                         p = v;
173                 *str_disk_guid = strdup(p);
174                 free(val);
175                 /* Move s to first partition */
176                 strsep(&s, ";");
177         }
178         if (strlen(s) == 0)
179                 return -3;
180
181         i = strlen(s) - 1;
182         if (s[i] == ';')
183                 s[i] = '\0';
184
185         /* calculate expected number of partitions */
186         p_count = 1;
187         p = s;
188         while (*p) {
189                 if (*p++ == ';')
190                         p_count++;
191         }
192
193         /* allocate memory for partitions */
194         parts = calloc(sizeof(disk_partition_t), p_count);
195
196         /* retrieve partitions data from string */
197         for (i = 0; i < p_count; i++) {
198                 tok = strsep(&s, ";");
199
200                 if (tok == NULL)
201                         break;
202
203                 /* uuid */
204                 val = extract_val(tok, "uuid");
205                 if (!val) {
206                         /* 'uuid' is optional if random uuid's are enabled */
207 #ifdef CONFIG_RANDOM_UUID
208                         gen_rand_uuid_str(parts[i].uuid, UUID_STR_FORMAT_STD);
209 #else
210                         errno = -4;
211                         goto err;
212 #endif
213                 } else {
214                         if (extract_env(val, &p))
215                                 p = val;
216                         if (strlen(p) >= sizeof(parts[i].uuid)) {
217                                 printf("Wrong uuid format for partition %d\n", i);
218                                 errno = -4;
219                                 goto err;
220                         }
221                         strcpy((char *)parts[i].uuid, p);
222                         free(val);
223                 }
224                 /* name */
225                 val = extract_val(tok, "name");
226                 if (!val) { /* name is mandatory */
227                         errno = -4;
228                         goto err;
229                 }
230                 if (extract_env(val, &p))
231                         p = val;
232                 if (strlen(p) >= sizeof(parts[i].name)) {
233                         errno = -4;
234                         goto err;
235                 }
236                 strcpy((char *)parts[i].name, p);
237                 free(val);
238
239                 /* size */
240                 val = extract_val(tok, "size");
241                 if (!val) { /* 'size' is mandatory */
242                         errno = -4;
243                         goto err;
244                 }
245                 if (extract_env(val, &p))
246                         p = val;
247                 size_ll = ustrtoull(p, &p, 0);
248                 parts[i].size = lldiv(size_ll, dev_desc->blksz);
249                 free(val);
250
251                 /* start address */
252                 val = extract_val(tok, "start");
253                 if (val) { /* start address is optional */
254                         if (extract_env(val, &p))
255                                 p = val;
256                         start_ll = ustrtoull(p, &p, 0);
257                         parts[i].start = lldiv(start_ll, dev_desc->blksz);
258                         free(val);
259                 }
260         }
261
262         *str_disk_guid = guid_str;
263         *parts_count = p_count;
264         *partitions = parts;
265         free(str);
266
267         return 0;
268
269 err:
270         free(parts);
271 free_guid:
272         free(guid_str);
273 free_str:
274         free(str);
275
276         *str_disk_guid = NULL;
277         *parts_count = 0;
278         *partitions = NULL;
279         return errno;
280 }
281
282 static int gpt_default(block_dev_desc_t *blk_dev_desc, const char *str_part)
283 {
284         int ret;
285         char *str_disk_guid;
286         u8 part_count = 0;
287         disk_partition_t *partitions = NULL;
288
289         if (!str_part)
290                 return -1;
291
292         /* fill partitions */
293         ret = set_gpt_info(blk_dev_desc, str_part,
294                         &str_disk_guid, &partitions, &part_count);
295         if (ret) {
296                 if (ret == -1)
297                         printf("No partition list provided\n");
298                 if (ret == -2)
299                         printf("Missing disk guid\n");
300                 if ((ret == -3) || (ret == -4))
301                         printf("Partition list incomplete\n");
302                 return -1;
303         }
304
305         /* save partitions layout to disk */
306         ret = gpt_restore(blk_dev_desc, str_disk_guid, partitions, part_count);
307         free(str_disk_guid);
308         free(partitions);
309
310         return ret;
311 }
312
313 /**
314  * do_gpt(): Perform GPT operations
315  *
316  * @param cmdtp - command name
317  * @param flag
318  * @param argc
319  * @param argv
320  *
321  * @return zero on success; otherwise error
322  */
323 static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
324 {
325         int ret = CMD_RET_SUCCESS;
326         int dev = 0;
327         char *ep;
328         block_dev_desc_t *blk_dev_desc;
329
330         if (argc < 5)
331                 return CMD_RET_USAGE;
332
333         /* command: 'write' */
334         if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
335                 dev = (int)simple_strtoul(argv[3], &ep, 10);
336                 if (!ep || ep[0] != '\0') {
337                         printf("'%s' is not a number\n", argv[3]);
338                         return CMD_RET_USAGE;
339                 }
340                 blk_dev_desc = get_dev(argv[2], dev);
341                 if (!blk_dev_desc) {
342                         printf("%s: %s dev %d NOT available\n",
343                                __func__, argv[2], dev);
344                         return CMD_RET_FAILURE;
345                 }
346
347                 puts("Writing GPT: ");
348
349                 ret = gpt_default(blk_dev_desc, argv[4]);
350                 if (!ret) {
351                         puts("success!\n");
352                         return CMD_RET_SUCCESS;
353                 } else {
354                         puts("error!\n");
355                         return CMD_RET_FAILURE;
356                 }
357         } else {
358                 return CMD_RET_USAGE;
359         }
360         return ret;
361 }
362
363 U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
364         "GUID Partition Table",
365         "<command> <interface> <dev> <partitions_list>\n"
366         " - GUID partition table restoration\n"
367         " Restore GPT information on a device connected\n"
368         " to interface\n"
369 );