1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <common.h>
24#include <malloc.h>
25#include <command.h>
26#include <mmc.h>
27#include <part_efi.h>
28#include <exports.h>
29#include <linux/ctype.h>
30
31#ifndef CONFIG_PARTITION_UUIDS
32#error CONFIG_PARTITION_UUIDS must be enabled for CONFIG_CMD_GPT to be enabled
33#endif
34
35
36
37
38
39
40
41
42
43
44static char extract_env(const char *str, char **env)
45{
46 char *e, *s;
47
48 if (!str || strlen(str) < 4)
49 return -1;
50
51 if ((strncmp(str, "${", 2) == 0) && (str[strlen(str) - 1] == '}')) {
52 s = strdup(str);
53 if (s == NULL)
54 return -1;
55 memset(s + strlen(s) - 1, '\0', 1);
56 memmove(s, s + 2, strlen(s) - 1);
57 e = getenv(s);
58 free(s);
59 if (e == NULL) {
60 printf("Environmental '%s' not set\n", str);
61 return -1;
62 }
63 *env = e;
64 return 0;
65 }
66
67 return -1;
68}
69
70
71
72
73
74
75
76
77
78
79
80static char *extract_val(const char *str, const char *key)
81{
82 char *v, *k;
83 char *s, *strcopy;
84 char *new = NULL;
85
86 strcopy = strdup(str);
87 if (strcopy == NULL)
88 return NULL;
89
90 s = strcopy;
91 while (s) {
92 v = strsep(&s, ",");
93 if (!v)
94 break;
95 k = strsep(&v, "=");
96 if (!k)
97 break;
98 if (strcmp(k, key) == 0) {
99 new = strdup(v);
100 break;
101 }
102 }
103
104 free(strcopy);
105
106 return new;
107}
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122static 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,
126 u8 *parts_count)
127{
128 char *tok, *str, *s;
129 int i;
130 char *val, *p;
131 int p_count;
132 disk_partition_t *parts;
133 int errno = 0;
134
135 debug("%s: MMC lba num: 0x%x %d\n", __func__,
136 (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba);
137
138 if (str_part == NULL)
139 return -1;
140
141 str = strdup(str_part);
142
143
144 s = str;
145 tok = strsep(&s, ";");
146 val = extract_val(tok, "uuid_disk");
147 if (!val) {
148 free(str);
149 return -2;
150 }
151 if (extract_env(val, &p))
152 p = val;
153 *str_disk_guid = strdup(p);
154 free(val);
155
156 if (strlen(s) == 0)
157 return -3;
158
159 i = strlen(s) - 1;
160 if (s[i] == ';')
161 s[i] = '\0';
162
163
164 p_count = 1;
165 p = s;
166 while (*p) {
167 if (*p++ == ';')
168 p_count++;
169 }
170
171
172 parts = calloc(sizeof(disk_partition_t), p_count);
173
174
175 for (i = 0; i < p_count; i++) {
176 tok = strsep(&s, ";");
177
178 if (tok == NULL)
179 break;
180
181
182 val = extract_val(tok, "uuid");
183 if (!val) {
184 errno = -4;
185 goto err;
186 }
187 if (extract_env(val, &p))
188 p = val;
189 if (strlen(p) >= sizeof(parts[i].uuid)) {
190 printf("Wrong uuid format for partition %d\n", i);
191 errno = -4;
192 goto err;
193 }
194 strcpy((char *)parts[i].uuid, p);
195 free(val);
196
197
198 val = extract_val(tok, "name");
199 if (!val) {
200 errno = -4;
201 goto err;
202 }
203 if (extract_env(val, &p))
204 p = val;
205 if (strlen(p) >= sizeof(parts[i].name)) {
206 errno = -4;
207 goto err;
208 }
209 strcpy((char *)parts[i].name, p);
210 free(val);
211
212
213 val = extract_val(tok, "size");
214 if (!val) {
215 errno = -4;
216 goto err;
217 }
218 if (extract_env(val, &p))
219 p = val;
220 parts[i].size = ustrtoul(p, &p, 0);
221 parts[i].size /= dev_desc->blksz;
222 free(val);
223
224
225 val = extract_val(tok, "start");
226 if (val) {
227 if (extract_env(val, &p))
228 p = val;
229 parts[i].start = ustrtoul(p, &p, 0);
230 parts[i].start /= dev_desc->blksz;
231 free(val);
232 }
233 }
234
235 *parts_count = p_count;
236 *partitions = parts;
237 free(str);
238
239 return 0;
240err:
241 free(str);
242 free(*str_disk_guid);
243 free(parts);
244
245 return errno;
246}
247
248static int gpt_mmc_default(int dev, const char *str_part)
249{
250 int ret;
251 char *str_disk_guid;
252 u8 part_count = 0;
253 disk_partition_t *partitions = NULL;
254
255 struct mmc *mmc = find_mmc_device(dev);
256
257 if (mmc == NULL) {
258 printf("%s: mmc dev %d NOT available\n", __func__, dev);
259 return CMD_RET_FAILURE;
260 }
261
262 if (!str_part)
263 return -1;
264
265
266 ret = set_gpt_info(&mmc->block_dev, str_part,
267 &str_disk_guid, &partitions, &part_count);
268 if (ret) {
269 if (ret == -1)
270 printf("No partition list provided\n");
271 if (ret == -2)
272 printf("Missing disk guid\n");
273 if ((ret == -3) || (ret == -4))
274 printf("Partition list incomplete\n");
275 return -1;
276 }
277
278
279 gpt_restore(&mmc->block_dev, str_disk_guid, partitions, part_count);
280 free(str_disk_guid);
281 free(partitions);
282
283 return 0;
284}
285
286
287
288
289
290
291
292
293
294
295
296static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
297{
298 int ret = CMD_RET_SUCCESS;
299 int dev = 0;
300 char *pstr;
301
302 if (argc < 5)
303 return CMD_RET_USAGE;
304
305
306 if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
307
308 if (strcmp(argv[2], "mmc") == 0) {
309
310 for (pstr = argv[3]; *pstr != '\0'; pstr++)
311 if (!isdigit(*pstr)) {
312 printf("'%s' is not a number\n",
313 argv[3]);
314 return CMD_RET_USAGE;
315 }
316 dev = (int)simple_strtoul(argv[3], NULL, 10);
317
318 if (gpt_mmc_default(dev, argv[4]))
319 return CMD_RET_FAILURE;
320 }
321 } else {
322 return CMD_RET_USAGE;
323 }
324 return ret;
325}
326
327U_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"
332 " to interface\n"
333);
334