]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_gpt.c
buildman: Permit branch names with an embedded '/'
[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         int errno = 0;
145         uint64_t size_ll, start_ll;
146
147         debug("%s:  lba num: 0x%x %d\n", __func__,
148               (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba);
149
150         if (str_part == NULL)
151                 return -1;
152
153         str = strdup(str_part);
154
155         /* extract disk guid */
156         s = str;
157         tok = strsep(&s, ";");
158         val = extract_val(tok, "uuid_disk");
159         if (!val) {
160                 free(str);
161                 return -2;
162         }
163         if (extract_env(val, &p))
164                 p = val;
165         *str_disk_guid = strdup(p);
166         free(val);
167
168         if (strlen(s) == 0)
169                 return -3;
170
171         i = strlen(s) - 1;
172         if (s[i] == ';')
173                 s[i] = '\0';
174
175         /* calculate expected number of partitions */
176         p_count = 1;
177         p = s;
178         while (*p) {
179                 if (*p++ == ';')
180                         p_count++;
181         }
182
183         /* allocate memory for partitions */
184         parts = calloc(sizeof(disk_partition_t), p_count);
185
186         /* retrieve partitions data from string */
187         for (i = 0; i < p_count; i++) {
188                 tok = strsep(&s, ";");
189
190                 if (tok == NULL)
191                         break;
192
193                 /* uuid */
194                 val = extract_val(tok, "uuid");
195                 if (!val) { /* 'uuid' is mandatory */
196                         errno = -4;
197                         goto err;
198                 }
199                 if (extract_env(val, &p))
200                         p = val;
201                 if (strlen(p) >= sizeof(parts[i].uuid)) {
202                         printf("Wrong uuid format for partition %d\n", i);
203                         errno = -4;
204                         goto err;
205                 }
206                 strcpy((char *)parts[i].uuid, p);
207                 free(val);
208
209                 /* name */
210                 val = extract_val(tok, "name");
211                 if (!val) { /* name is mandatory */
212                         errno = -4;
213                         goto err;
214                 }
215                 if (extract_env(val, &p))
216                         p = val;
217                 if (strlen(p) >= sizeof(parts[i].name)) {
218                         errno = -4;
219                         goto err;
220                 }
221                 strcpy((char *)parts[i].name, p);
222                 free(val);
223
224                 /* size */
225                 val = extract_val(tok, "size");
226                 if (!val) { /* 'size' is mandatory */
227                         errno = -4;
228                         goto err;
229                 }
230                 if (extract_env(val, &p))
231                         p = val;
232                 size_ll = ustrtoull(p, &p, 0);
233                 parts[i].size = lldiv(size_ll, dev_desc->blksz);
234                 free(val);
235
236                 /* start address */
237                 val = extract_val(tok, "start");
238                 if (val) { /* start address is optional */
239                         if (extract_env(val, &p))
240                                 p = val;
241                         start_ll = ustrtoull(p, &p, 0);
242                         parts[i].start = lldiv(start_ll, dev_desc->blksz);
243                         free(val);
244                 }
245         }
246
247         *parts_count = p_count;
248         *partitions = parts;
249         free(str);
250
251         return 0;
252 err:
253         free(str);
254         free(*str_disk_guid);
255         free(parts);
256
257         return errno;
258 }
259
260 static int gpt_default(block_dev_desc_t *blk_dev_desc, const char *str_part)
261 {
262         int ret;
263         char *str_disk_guid;
264         u8 part_count = 0;
265         disk_partition_t *partitions = NULL;
266
267         if (!str_part)
268                 return -1;
269
270         /* fill partitions */
271         ret = set_gpt_info(blk_dev_desc, str_part,
272                         &str_disk_guid, &partitions, &part_count);
273         if (ret) {
274                 if (ret == -1)
275                         printf("No partition list provided\n");
276                 if (ret == -2)
277                         printf("Missing disk guid\n");
278                 if ((ret == -3) || (ret == -4))
279                         printf("Partition list incomplete\n");
280                 return -1;
281         }
282
283         /* save partitions layout to disk */
284         gpt_restore(blk_dev_desc, str_disk_guid, partitions, part_count);
285         free(str_disk_guid);
286         free(partitions);
287
288         return 0;
289 }
290
291 /**
292  * do_gpt(): Perform GPT operations
293  *
294  * @param cmdtp - command name
295  * @param flag
296  * @param argc
297  * @param argv
298  *
299  * @return zero on success; otherwise error
300  */
301 static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
302 {
303         int ret = CMD_RET_SUCCESS;
304         int dev = 0;
305         char *ep;
306         block_dev_desc_t *blk_dev_desc;
307
308         if (argc < 5)
309                 return CMD_RET_USAGE;
310
311         /* command: 'write' */
312         if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
313                 dev = (int)simple_strtoul(argv[3], &ep, 10);
314                 if (!ep || ep[0] != '\0') {
315                         printf("'%s' is not a number\n", argv[3]);
316                         return CMD_RET_USAGE;
317                 }
318                 blk_dev_desc = get_dev(argv[2], dev);
319                 if (!blk_dev_desc) {
320                         printf("%s: %s dev %d NOT available\n",
321                                __func__, argv[2], dev);
322                         return CMD_RET_FAILURE;
323                 }
324
325                 puts("Writing GPT: ");
326
327                 ret = gpt_default(blk_dev_desc, argv[4]);
328                 if (!ret) {
329                         puts("success!\n");
330                         return CMD_RET_SUCCESS;
331                 } else {
332                         puts("error!\n");
333                         return CMD_RET_FAILURE;
334                 }
335         } else {
336                 return CMD_RET_USAGE;
337         }
338         return ret;
339 }
340
341 U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
342         "GUID Partition Table",
343         "<command> <interface> <dev> <partitions_list>\n"
344         " - GUID partition table restoration\n"
345         " Restore GPT information on a device connected\n"
346         " to interface\n"
347 );