]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
8b096237 PW |
2 | /* |
3 | * cmd_gpt.c -- GPT (GUID Partition Table) handling command | |
4 | * | |
bbb9ffac LM |
5 | * Copyright (C) 2015 |
6 | * Lukasz Majewski <[email protected]> | |
7 | * | |
8b096237 PW |
8 | * Copyright (C) 2012 Samsung Electronics |
9 | * author: Lukasz Majewski <[email protected]> | |
10 | * author: Piotr Wilczek <[email protected]> | |
8b096237 PW |
11 | */ |
12 | ||
e6f6f9e6 | 13 | #include <blk.h> |
9fb625ce | 14 | #include <env.h> |
f7ae49fc | 15 | #include <log.h> |
8b096237 PW |
16 | #include <malloc.h> |
17 | #include <command.h> | |
e6f6f9e6 | 18 | #include <part.h> |
8b096237 | 19 | #include <part_efi.h> |
12fc1f3b | 20 | #include <part.h> |
8b096237 | 21 | #include <exports.h> |
ba06b3c5 | 22 | #include <uuid.h> |
8b096237 | 23 | #include <linux/ctype.h> |
3e34cf7b | 24 | #include <div64.h> |
bbb9ffac | 25 | #include <memalign.h> |
09a49930 | 26 | #include <linux/compat.h> |
61b29b82 | 27 | #include <linux/err.h> |
203f9b48 AC |
28 | #include <linux/sizes.h> |
29 | #include <stdlib.h> | |
09a49930 AC |
30 | |
31 | static LIST_HEAD(disk_partitions); | |
8b096237 | 32 | |
8b096237 PW |
33 | /** |
34 | * extract_env(): Expand env name from string format '&{env_name}' | |
35 | * and return pointer to the env (if the env is set) | |
36 | * | |
37 | * @param str - pointer to string | |
38 | * @param env - pointer to pointer to extracted env | |
39 | * | |
185f812c | 40 | * Return: - zero on successful expand and env is set |
8b096237 | 41 | */ |
39206382 | 42 | static int extract_env(const char *str, char **env) |
8b096237 | 43 | { |
39206382 | 44 | int ret = -1; |
8b096237 | 45 | char *e, *s; |
39206382 PM |
46 | #ifdef CONFIG_RANDOM_UUID |
47 | char uuid_str[UUID_STR_LEN + 1]; | |
48 | #endif | |
8b096237 PW |
49 | |
50 | if (!str || strlen(str) < 4) | |
51 | return -1; | |
52 | ||
39206382 PM |
53 | if (!((strncmp(str, "${", 2) == 0) && (str[strlen(str) - 1] == '}'))) |
54 | return -1; | |
55 | ||
56 | s = strdup(str); | |
57 | if (s == NULL) | |
58 | return -1; | |
59 | ||
60 | memset(s + strlen(s) - 1, '\0', 1); | |
61 | memmove(s, s + 2, strlen(s) - 1); | |
62 | ||
00caae6d | 63 | e = env_get(s); |
39206382 PM |
64 | if (e == NULL) { |
65 | #ifdef CONFIG_RANDOM_UUID | |
66 | debug("%s unset. ", str); | |
9da52f8f | 67 | gen_rand_uuid_str(uuid_str, UUID_STR_FORMAT_GUID); |
382bee57 | 68 | env_set(s, uuid_str); |
39206382 | 69 | |
00caae6d | 70 | e = env_get(s); |
39206382 PM |
71 | if (e) { |
72 | debug("Set to random.\n"); | |
73 | ret = 0; | |
74 | } else { | |
75 | debug("Can't get random UUID.\n"); | |
8b096237 | 76 | } |
39206382 PM |
77 | #else |
78 | debug("%s unset.\n", str); | |
79 | #endif | |
80 | } else { | |
81 | debug("%s get from environment.\n", str); | |
82 | ret = 0; | |
8b096237 PW |
83 | } |
84 | ||
39206382 PM |
85 | *env = e; |
86 | free(s); | |
87 | ||
88 | return ret; | |
8b096237 PW |
89 | } |
90 | ||
91 | /** | |
92 | * extract_val(): Extract value from a key=value pair list (comma separated). | |
93 | * Only value for the given key is returend. | |
94 | * Function allocates memory for the value, remember to free! | |
95 | * | |
96 | * @param str - pointer to string with key=values pairs | |
97 | * @param key - pointer to the key to search for | |
98 | * | |
185f812c | 99 | * Return: - pointer to allocated string with the value |
8b096237 PW |
100 | */ |
101 | static char *extract_val(const char *str, const char *key) | |
102 | { | |
103 | char *v, *k; | |
104 | char *s, *strcopy; | |
105 | char *new = NULL; | |
106 | ||
107 | strcopy = strdup(str); | |
108 | if (strcopy == NULL) | |
109 | return NULL; | |
110 | ||
111 | s = strcopy; | |
112 | while (s) { | |
113 | v = strsep(&s, ","); | |
114 | if (!v) | |
115 | break; | |
116 | k = strsep(&v, "="); | |
117 | if (!k) | |
118 | break; | |
119 | if (strcmp(k, key) == 0) { | |
120 | new = strdup(v); | |
121 | break; | |
122 | } | |
123 | } | |
124 | ||
125 | free(strcopy); | |
126 | ||
127 | return new; | |
128 | } | |
129 | ||
cfdaf4ca PD |
130 | /** |
131 | * found_key(): Found key without value in parameter list (comma separated). | |
132 | * | |
133 | * @param str - pointer to string with key | |
134 | * @param key - pointer to the key to search for | |
135 | * | |
185f812c | 136 | * Return: - true on found key |
cfdaf4ca PD |
137 | */ |
138 | static bool found_key(const char *str, const char *key) | |
139 | { | |
140 | char *k; | |
141 | char *s, *strcopy; | |
142 | bool result = false; | |
143 | ||
144 | strcopy = strdup(str); | |
145 | if (!strcopy) | |
146 | return NULL; | |
147 | ||
148 | s = strcopy; | |
149 | while (s) { | |
150 | k = strsep(&s, ","); | |
151 | if (!k) | |
152 | break; | |
153 | if (strcmp(k, key) == 0) { | |
154 | result = true; | |
155 | break; | |
156 | } | |
157 | } | |
158 | ||
159 | free(strcopy); | |
160 | ||
161 | return result; | |
162 | } | |
163 | ||
69f4d373 HS |
164 | /** |
165 | * calc_parts_list_len() - get size of partition table description | |
166 | * | |
167 | * @numparts: number of partitions | |
168 | * Return: string size including terminating NUL | |
169 | */ | |
2fcaa413 AC |
170 | static int calc_parts_list_len(int numparts) |
171 | { | |
69f4d373 HS |
172 | /* number of hexadecimal digits of the lbaint_t representation */ |
173 | const int lbaint_size = 2 * sizeof(lbaint_t); | |
174 | int partlistlen; | |
175 | ||
176 | /* media description including terminating NUL */ | |
177 | partlistlen = strlen("uuid_disk=;") + UUID_STR_LEN + 1; | |
178 | /* per-partition descriptions; numparts */ | |
179 | partlistlen += numparts * (strlen("name=,") + PART_NAME_LEN); | |
2fcaa413 | 180 | /* see part.h for definition of struct disk_partition */ |
69f4d373 HS |
181 | partlistlen += numparts * (strlen("start=0x,") + lbaint_size); |
182 | partlistlen += numparts * (strlen("size=0x,") + lbaint_size); | |
69f4d373 | 183 | if (IS_ENABLED(CONFIG_PARTITION_UUIDS)) |
f5e4b056 HS |
184 | partlistlen += numparts * (strlen("uuid=,") + UUID_STR_LEN); |
185 | if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID)) | |
186 | partlistlen += numparts * (strlen("type=;") + UUID_STR_LEN); | |
69f4d373 HS |
187 | debug("Length of partitions_list is %d for %d partitions\n", |
188 | partlistlen, numparts); | |
2fcaa413 AC |
189 | return partlistlen; |
190 | } | |
191 | ||
09a49930 AC |
192 | #ifdef CONFIG_CMD_GPT_RENAME |
193 | static void del_gpt_info(void) | |
194 | { | |
195 | struct list_head *pos = &disk_partitions; | |
196 | struct disk_part *curr; | |
197 | while (!list_empty(pos)) { | |
198 | curr = list_entry(pos->next, struct disk_part, list); | |
199 | list_del(pos->next); | |
200 | free(curr); | |
201 | } | |
202 | } | |
203 | ||
0528979f SG |
204 | static struct disk_part *allocate_disk_part(struct disk_partition *info, |
205 | int partnum) | |
09a49930 AC |
206 | { |
207 | struct disk_part *newpart; | |
f66bc0e0 | 208 | newpart = calloc(1, sizeof(struct disk_part)); |
09a49930 AC |
209 | if (!newpart) |
210 | return ERR_PTR(-ENOMEM); | |
09a49930 AC |
211 | |
212 | newpart->gpt_part_info.start = info->start; | |
213 | newpart->gpt_part_info.size = info->size; | |
214 | newpart->gpt_part_info.blksz = info->blksz; | |
215 | strncpy((char *)newpart->gpt_part_info.name, (const char *)info->name, | |
216 | PART_NAME_LEN); | |
217 | newpart->gpt_part_info.name[PART_NAME_LEN - 1] = '\0'; | |
218 | strncpy((char *)newpart->gpt_part_info.type, (const char *)info->type, | |
219 | PART_TYPE_LEN); | |
220 | newpart->gpt_part_info.type[PART_TYPE_LEN - 1] = '\0'; | |
221 | newpart->gpt_part_info.bootable = info->bootable; | |
396f3155 HS |
222 | if (IS_ENABLED(CONFIG_PARTITION_UUIDS)) |
223 | disk_partition_set_uuid(&newpart->gpt_part_info, | |
224 | disk_partition_uuid(info)); | |
f5e4b056 HS |
225 | if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID)) |
226 | disk_partition_set_type_guid(&newpart->gpt_part_info, | |
227 | disk_partition_type_guid(info)); | |
09a49930 AC |
228 | newpart->partnum = partnum; |
229 | ||
230 | return newpart; | |
231 | } | |
232 | ||
203f9b48 AC |
233 | static void prettyprint_part_size(char *sizestr, lbaint_t partsize, |
234 | lbaint_t blksize) | |
235 | { | |
236 | unsigned long long partbytes, partmegabytes; | |
237 | ||
238 | partbytes = partsize * blksize; | |
239 | partmegabytes = lldiv(partbytes, SZ_1M); | |
240 | snprintf(sizestr, 16, "%lluMiB", partmegabytes); | |
241 | } | |
242 | ||
09a49930 AC |
243 | static void print_gpt_info(void) |
244 | { | |
245 | struct list_head *pos; | |
246 | struct disk_part *curr; | |
203f9b48 AC |
247 | char partstartstr[16]; |
248 | char partsizestr[16]; | |
09a49930 AC |
249 | |
250 | list_for_each(pos, &disk_partitions) { | |
251 | curr = list_entry(pos, struct disk_part, list); | |
203f9b48 AC |
252 | prettyprint_part_size(partstartstr, curr->gpt_part_info.start, |
253 | curr->gpt_part_info.blksz); | |
254 | prettyprint_part_size(partsizestr, curr->gpt_part_info.size, | |
255 | curr->gpt_part_info.blksz); | |
256 | ||
09a49930 | 257 | printf("Partition %d:\n", curr->partnum); |
203f9b48 | 258 | printf("Start %s, size %s\n", partstartstr, partsizestr); |
09a49930 AC |
259 | printf("Block size %lu, name %s\n", curr->gpt_part_info.blksz, |
260 | curr->gpt_part_info.name); | |
261 | printf("Type %s, bootable %d\n", curr->gpt_part_info.type, | |
25801acc | 262 | curr->gpt_part_info.bootable & PART_BOOTABLE); |
396f3155 HS |
263 | if (CONFIG_IS_ENABLED(PARTITION_UUIDS)) |
264 | printf("UUID %s\n", | |
265 | disk_partition_uuid(&curr->gpt_part_info)); | |
f5e4b056 HS |
266 | if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID)) |
267 | printf("Type GUID %s\n", | |
268 | disk_partition_type_guid(&curr->gpt_part_info)); | |
09a49930 AC |
269 | printf("\n"); |
270 | } | |
271 | } | |
272 | ||
203f9b48 AC |
273 | /* |
274 | * create the string that upstream 'gpt write' command will accept as an | |
275 | * argument | |
276 | * | |
277 | * From doc/README.gpt, Format of partitions layout: | |
278 | * "uuid_disk=...;name=u-boot,size=60MiB,uuid=...; | |
279 | * name=kernel,size=60MiB,uuid=...;" | |
280 | * The fields 'name' and 'size' are mandatory for every partition. | |
281 | * The field 'start' is optional. The fields 'uuid' and 'uuid_disk' | |
282 | * are optional if CONFIG_RANDOM_UUID is enabled. | |
283 | */ | |
284 | static int create_gpt_partitions_list(int numparts, const char *guid, | |
285 | char *partitions_list) | |
286 | { | |
287 | struct list_head *pos; | |
288 | struct disk_part *curr; | |
289 | char partstr[PART_NAME_LEN + 1]; | |
290 | ||
291 | if (!partitions_list) | |
292 | return -EINVAL; | |
293 | ||
294 | strcpy(partitions_list, "uuid_disk="); | |
295 | strncat(partitions_list, guid, UUID_STR_LEN + 1); | |
296 | strcat(partitions_list, ";"); | |
297 | ||
298 | list_for_each(pos, &disk_partitions) { | |
299 | curr = list_entry(pos, struct disk_part, list); | |
300 | strcat(partitions_list, "name="); | |
301 | strncat(partitions_list, (const char *)curr->gpt_part_info.name, | |
302 | PART_NAME_LEN + 1); | |
3a2605fa PD |
303 | sprintf(partstr, ",start=0x%llx", |
304 | (unsigned long long)curr->gpt_part_info.start * | |
305 | curr->gpt_part_info.blksz); | |
203f9b48 AC |
306 | /* one extra byte for NULL */ |
307 | strncat(partitions_list, partstr, PART_NAME_LEN + 1); | |
3a2605fa PD |
308 | sprintf(partstr, ",size=0x%llx", |
309 | (unsigned long long)curr->gpt_part_info.size * | |
310 | curr->gpt_part_info.blksz); | |
203f9b48 AC |
311 | strncat(partitions_list, partstr, PART_NAME_LEN + 1); |
312 | ||
f5e4b056 HS |
313 | if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID)) { |
314 | strcat(partitions_list, ",type="); | |
315 | strncat(partitions_list, | |
316 | disk_partition_type_guid(&curr->gpt_part_info), | |
317 | UUID_STR_LEN + 1); | |
318 | } | |
396f3155 HS |
319 | if (CONFIG_IS_ENABLED(PARTITION_UUIDS)) { |
320 | strcat(partitions_list, ",uuid="); | |
321 | strncat(partitions_list, | |
322 | disk_partition_uuid(&curr->gpt_part_info), | |
323 | UUID_STR_LEN + 1); | |
324 | } | |
648140f7 JW |
325 | if (curr->gpt_part_info.bootable & PART_BOOTABLE) |
326 | strcat(partitions_list, ",bootable"); | |
203f9b48 AC |
327 | strcat(partitions_list, ";"); |
328 | } | |
329 | return 0; | |
330 | } | |
331 | ||
09a49930 AC |
332 | /* |
333 | * read partition info into disk_partitions list where | |
334 | * it can be printed or modified | |
335 | */ | |
336 | static int get_gpt_info(struct blk_desc *dev_desc) | |
337 | { | |
338 | /* start partition numbering at 1, as U-Boot does */ | |
339 | int valid_parts = 0, p, ret; | |
0528979f | 340 | struct disk_partition info; |
09a49930 AC |
341 | struct disk_part *new_disk_part; |
342 | ||
203f9b48 AC |
343 | /* |
344 | * Always re-read partition info from device, in case | |
345 | * it has changed | |
346 | */ | |
347 | INIT_LIST_HEAD(&disk_partitions); | |
09a49930 AC |
348 | |
349 | for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) { | |
350 | ret = part_get_info(dev_desc, p, &info); | |
351 | if (ret) | |
352 | continue; | |
353 | ||
354 | /* Add 1 here because counter is zero-based but p1 is | |
355 | the first partition */ | |
356 | new_disk_part = allocate_disk_part(&info, valid_parts+1); | |
357 | if (IS_ERR(new_disk_part)) | |
358 | goto out; | |
359 | ||
360 | list_add_tail(&new_disk_part->list, &disk_partitions); | |
361 | valid_parts++; | |
362 | } | |
363 | if (valid_parts == 0) { | |
364 | printf("** No valid partitions found **\n"); | |
365 | goto out; | |
366 | } | |
367 | return valid_parts; | |
368 | out: | |
369 | if (valid_parts >= 1) | |
370 | del_gpt_info(); | |
371 | return -ENODEV; | |
372 | } | |
373 | ||
374 | /* a wrapper to test get_gpt_info */ | |
653cd92d | 375 | static int do_get_gpt_info(struct blk_desc *dev_desc, char * const namestr) |
09a49930 | 376 | { |
653cd92d FA |
377 | int numparts; |
378 | ||
379 | numparts = get_gpt_info(dev_desc); | |
380 | ||
381 | if (numparts > 0) { | |
382 | if (namestr) { | |
383 | char disk_guid[UUID_STR_LEN + 1]; | |
384 | char *partitions_list; | |
385 | int partlistlen; | |
386 | int ret = -1; | |
387 | ||
388 | ret = get_disk_guid(dev_desc, disk_guid); | |
389 | if (ret < 0) | |
390 | return ret; | |
391 | ||
392 | partlistlen = calc_parts_list_len(numparts); | |
393 | partitions_list = malloc(partlistlen); | |
394 | if (!partitions_list) { | |
395 | del_gpt_info(); | |
396 | return -ENOMEM; | |
397 | } | |
398 | memset(partitions_list, '\0', partlistlen); | |
399 | ||
400 | ret = create_gpt_partitions_list(numparts, disk_guid, | |
401 | partitions_list); | |
402 | if (ret < 0) | |
403 | printf("Error: Could not create partition list string!\n"); | |
404 | else | |
405 | env_set(namestr, partitions_list); | |
09a49930 | 406 | |
653cd92d FA |
407 | free(partitions_list); |
408 | } else { | |
409 | print_gpt_info(); | |
410 | } | |
09a49930 AC |
411 | del_gpt_info(); |
412 | return 0; | |
413 | } | |
653cd92d | 414 | return numparts; |
09a49930 AC |
415 | } |
416 | #endif | |
417 | ||
8b096237 PW |
418 | /** |
419 | * set_gpt_info(): Fill partition information from string | |
420 | * function allocates memory, remember to free! | |
421 | * | |
422 | * @param dev_desc - pointer block device descriptor | |
423 | * @param str_part - pointer to string with partition information | |
424 | * @param str_disk_guid - pointer to pointer to allocated string with disk guid | |
425 | * @param partitions - pointer to pointer to allocated partitions array | |
426 | * @param parts_count - number of partitions | |
427 | * | |
185f812c | 428 | * Return: - zero on success, otherwise error |
8b096237 PW |
429 | * |
430 | */ | |
4101f687 | 431 | static int set_gpt_info(struct blk_desc *dev_desc, |
8b096237 PW |
432 | const char *str_part, |
433 | char **str_disk_guid, | |
0528979f | 434 | struct disk_partition **partitions, |
8b096237 PW |
435 | u8 *parts_count) |
436 | { | |
437 | char *tok, *str, *s; | |
438 | int i; | |
439 | char *val, *p; | |
440 | int p_count; | |
0528979f | 441 | struct disk_partition *parts; |
8b096237 | 442 | int errno = 0; |
3e34cf7b | 443 | uint64_t size_ll, start_ll; |
66636235 | 444 | lbaint_t offset = 0; |
2fcaa413 | 445 | int max_str_part = calc_parts_list_len(MAX_SEARCH_PARTITIONS); |
8b096237 | 446 | |
619f0fdf | 447 | debug("%s: lba num: 0x%x %d\n", __func__, |
8b096237 PW |
448 | (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba); |
449 | ||
450 | if (str_part == NULL) | |
451 | return -1; | |
452 | ||
453 | str = strdup(str_part); | |
203f9b48 AC |
454 | if (str == NULL) |
455 | return -ENOMEM; | |
8b096237 PW |
456 | |
457 | /* extract disk guid */ | |
458 | s = str; | |
0c7e8d13 | 459 | val = extract_val(str, "uuid_disk"); |
8b096237 | 460 | if (!val) { |
0c7e8d13 RH |
461 | #ifdef CONFIG_RANDOM_UUID |
462 | *str_disk_guid = malloc(UUID_STR_LEN + 1); | |
bf52fcde | 463 | if (*str_disk_guid == NULL) |
2fcaa413 | 464 | return -ENOMEM; |
0c7e8d13 RH |
465 | gen_rand_uuid_str(*str_disk_guid, UUID_STR_FORMAT_STD); |
466 | #else | |
8b096237 PW |
467 | free(str); |
468 | return -2; | |
0c7e8d13 RH |
469 | #endif |
470 | } else { | |
471 | val = strsep(&val, ";"); | |
472 | if (extract_env(val, &p)) | |
473 | p = val; | |
474 | *str_disk_guid = strdup(p); | |
475 | free(val); | |
476 | /* Move s to first partition */ | |
477 | strsep(&s, ";"); | |
8b096237 | 478 | } |
2fcaa413 AC |
479 | if (s == NULL) { |
480 | printf("Error: is the partitions string NULL-terminated?\n"); | |
481 | return -EINVAL; | |
482 | } | |
483 | if (strnlen(s, max_str_part) == 0) | |
8b096237 PW |
484 | return -3; |
485 | ||
2fcaa413 | 486 | i = strnlen(s, max_str_part) - 1; |
8b096237 PW |
487 | if (s[i] == ';') |
488 | s[i] = '\0'; | |
489 | ||
490 | /* calculate expected number of partitions */ | |
491 | p_count = 1; | |
492 | p = s; | |
493 | while (*p) { | |
494 | if (*p++ == ';') | |
495 | p_count++; | |
496 | } | |
497 | ||
498 | /* allocate memory for partitions */ | |
0528979f | 499 | parts = calloc(sizeof(struct disk_partition), p_count); |
2fcaa413 AC |
500 | if (parts == NULL) |
501 | return -ENOMEM; | |
8b096237 | 502 | |
1f8b546f | 503 | /* retrieve partitions data from string */ |
8b096237 PW |
504 | for (i = 0; i < p_count; i++) { |
505 | tok = strsep(&s, ";"); | |
506 | ||
507 | if (tok == NULL) | |
508 | break; | |
509 | ||
510 | /* uuid */ | |
511 | val = extract_val(tok, "uuid"); | |
0c7e8d13 RH |
512 | if (!val) { |
513 | /* 'uuid' is optional if random uuid's are enabled */ | |
514 | #ifdef CONFIG_RANDOM_UUID | |
515 | gen_rand_uuid_str(parts[i].uuid, UUID_STR_FORMAT_STD); | |
516 | #else | |
8b096237 PW |
517 | errno = -4; |
518 | goto err; | |
0c7e8d13 RH |
519 | #endif |
520 | } else { | |
521 | if (extract_env(val, &p)) | |
522 | p = val; | |
2fcaa413 | 523 | if (strnlen(p, max_str_part) >= sizeof(parts[i].uuid)) { |
0c7e8d13 RH |
524 | printf("Wrong uuid format for partition %d\n", i); |
525 | errno = -4; | |
526 | goto err; | |
527 | } | |
2fcaa413 | 528 | strncpy((char *)parts[i].uuid, p, max_str_part); |
0c7e8d13 | 529 | free(val); |
8b096237 | 530 | } |
7561b258 PD |
531 | #ifdef CONFIG_PARTITION_TYPE_GUID |
532 | /* guid */ | |
533 | val = extract_val(tok, "type"); | |
534 | if (val) { | |
535 | /* 'type' is optional */ | |
536 | if (extract_env(val, &p)) | |
537 | p = val; | |
2fcaa413 | 538 | if (strnlen(p, max_str_part) >= sizeof(parts[i].type_guid)) { |
7561b258 PD |
539 | printf("Wrong type guid format for partition %d\n", |
540 | i); | |
541 | errno = -4; | |
542 | goto err; | |
543 | } | |
2fcaa413 | 544 | strncpy((char *)parts[i].type_guid, p, max_str_part); |
7561b258 PD |
545 | free(val); |
546 | } | |
547 | #endif | |
8b096237 PW |
548 | /* name */ |
549 | val = extract_val(tok, "name"); | |
550 | if (!val) { /* name is mandatory */ | |
551 | errno = -4; | |
552 | goto err; | |
553 | } | |
554 | if (extract_env(val, &p)) | |
555 | p = val; | |
2fcaa413 | 556 | if (strnlen(p, max_str_part) >= sizeof(parts[i].name)) { |
8b096237 PW |
557 | errno = -4; |
558 | goto err; | |
559 | } | |
2fcaa413 | 560 | strncpy((char *)parts[i].name, p, max_str_part); |
8b096237 PW |
561 | free(val); |
562 | ||
563 | /* size */ | |
564 | val = extract_val(tok, "size"); | |
565 | if (!val) { /* 'size' is mandatory */ | |
566 | errno = -4; | |
567 | goto err; | |
568 | } | |
569 | if (extract_env(val, &p)) | |
570 | p = val; | |
66636235 | 571 | if ((strcmp(p, "-") == 0)) { |
c2fdd345 KY |
572 | /* Let part efi module to auto extend the size */ |
573 | parts[i].size = 0; | |
66636235 MT |
574 | } else { |
575 | size_ll = ustrtoull(p, &p, 0); | |
576 | parts[i].size = lldiv(size_ll, dev_desc->blksz); | |
577 | } | |
578 | ||
8b096237 PW |
579 | free(val); |
580 | ||
581 | /* start address */ | |
582 | val = extract_val(tok, "start"); | |
583 | if (val) { /* start address is optional */ | |
584 | if (extract_env(val, &p)) | |
585 | p = val; | |
3e34cf7b PW |
586 | start_ll = ustrtoull(p, &p, 0); |
587 | parts[i].start = lldiv(start_ll, dev_desc->blksz); | |
8b096237 PW |
588 | free(val); |
589 | } | |
cfdaf4ca | 590 | |
66636235 MT |
591 | offset += parts[i].size + parts[i].start; |
592 | ||
cfdaf4ca PD |
593 | /* bootable */ |
594 | if (found_key(tok, "bootable")) | |
25801acc | 595 | parts[i].bootable = PART_BOOTABLE; |
8b096237 PW |
596 | } |
597 | ||
598 | *parts_count = p_count; | |
599 | *partitions = parts; | |
600 | free(str); | |
601 | ||
602 | return 0; | |
603 | err: | |
604 | free(str); | |
605 | free(*str_disk_guid); | |
606 | free(parts); | |
607 | ||
608 | return errno; | |
609 | } | |
610 | ||
26f404c7 PR |
611 | static int gpt_repair(struct blk_desc *blk_dev_desc) |
612 | { | |
613 | int ret = 0; | |
614 | ||
615 | ret = gpt_repair_headers(blk_dev_desc); | |
616 | ||
617 | return ret; | |
618 | } | |
619 | ||
4101f687 | 620 | static int gpt_default(struct blk_desc *blk_dev_desc, const char *str_part) |
8b096237 PW |
621 | { |
622 | int ret; | |
623 | char *str_disk_guid; | |
624 | u8 part_count = 0; | |
0528979f | 625 | struct disk_partition *partitions = NULL; |
8b096237 | 626 | |
8b096237 | 627 | /* fill partitions */ |
619f0fdf | 628 | ret = set_gpt_info(blk_dev_desc, str_part, |
8b096237 PW |
629 | &str_disk_guid, &partitions, &part_count); |
630 | if (ret) { | |
631 | if (ret == -1) | |
632 | printf("No partition list provided\n"); | |
633 | if (ret == -2) | |
634 | printf("Missing disk guid\n"); | |
635 | if ((ret == -3) || (ret == -4)) | |
636 | printf("Partition list incomplete\n"); | |
637 | return -1; | |
638 | } | |
639 | ||
640 | /* save partitions layout to disk */ | |
a150e6c9 | 641 | ret = gpt_restore(blk_dev_desc, str_disk_guid, partitions, part_count); |
8b096237 PW |
642 | free(str_disk_guid); |
643 | free(partitions); | |
644 | ||
8024d577 KD |
645 | /* initialize partition table */ |
646 | if (blk_enabled()) | |
647 | part_init(blk_dev_desc); | |
648 | ||
a150e6c9 | 649 | return ret; |
8b096237 PW |
650 | } |
651 | ||
4101f687 | 652 | static int gpt_verify(struct blk_desc *blk_dev_desc, const char *str_part) |
bbb9ffac LM |
653 | { |
654 | ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, | |
655 | blk_dev_desc->blksz); | |
0528979f | 656 | struct disk_partition *partitions = NULL; |
bbb9ffac LM |
657 | gpt_entry *gpt_pte = NULL; |
658 | char *str_disk_guid; | |
659 | u8 part_count = 0; | |
660 | int ret = 0; | |
661 | ||
662 | /* fill partitions */ | |
663 | ret = set_gpt_info(blk_dev_desc, str_part, | |
664 | &str_disk_guid, &partitions, &part_count); | |
665 | if (ret) { | |
666 | if (ret == -1) { | |
667 | printf("No partition list provided - only basic check\n"); | |
668 | ret = gpt_verify_headers(blk_dev_desc, gpt_head, | |
669 | &gpt_pte); | |
670 | goto out; | |
671 | } | |
672 | if (ret == -2) | |
673 | printf("Missing disk guid\n"); | |
674 | if ((ret == -3) || (ret == -4)) | |
675 | printf("Partition list incomplete\n"); | |
676 | return -1; | |
677 | } | |
678 | ||
679 | /* Check partition layout with provided pattern */ | |
680 | ret = gpt_verify_partitions(blk_dev_desc, partitions, part_count, | |
681 | gpt_head, &gpt_pte); | |
682 | free(str_disk_guid); | |
683 | free(partitions); | |
684 | out: | |
685 | free(gpt_pte); | |
686 | return ret; | |
687 | } | |
688 | ||
12fc1f3b CD |
689 | /** |
690 | * gpt_enumerate() - Enumerate partition names into environment variable. | |
691 | * | |
692 | * Enumerate partition names. Partition names are stored in gpt_partition_list | |
693 | * environment variable. Each partition name is delimited by space. | |
694 | * | |
695 | * @desc: block device descriptor | |
696 | * | |
697 | * @Return: '0' on success and -ve error on failure | |
698 | */ | |
699 | static int gpt_enumerate(struct blk_desc *desc) | |
700 | { | |
701 | struct part_driver *first_drv, *part_drv; | |
702 | int str_len = 0, tmp_len; | |
703 | char part_list[2048]; | |
704 | int n_drvs; | |
705 | char *ptr; | |
706 | ||
707 | part_list[0] = 0; | |
708 | n_drvs = part_driver_get_count(); | |
709 | if (!n_drvs) { | |
710 | printf("Failed to get partition driver count\n"); | |
711 | return -ENOENT; | |
712 | } | |
713 | ||
714 | first_drv = part_driver_get_first(); | |
715 | for (part_drv = first_drv; part_drv != first_drv + n_drvs; part_drv++) { | |
716 | struct disk_partition pinfo; | |
717 | int ret; | |
718 | int i; | |
719 | ||
41cd23b7 HS |
720 | if (part_drv->test(desc)) |
721 | continue; | |
722 | ||
12fc1f3b CD |
723 | for (i = 1; i < part_drv->max_entries; i++) { |
724 | ret = part_drv->get_info(desc, i, &pinfo); | |
41cd23b7 HS |
725 | if (ret) |
726 | continue; | |
12fc1f3b CD |
727 | |
728 | ptr = &part_list[str_len]; | |
729 | tmp_len = strlen((const char *)pinfo.name); | |
730 | str_len += tmp_len; | |
731 | /* +1 for space */ | |
732 | str_len++; | |
733 | if (str_len > sizeof(part_list)) { | |
734 | printf("Error insufficient memory\n"); | |
735 | return -ENOMEM; | |
736 | } | |
737 | strcpy(ptr, (const char *)pinfo.name); | |
738 | /* One byte for space(" ") delimiter */ | |
739 | ptr[tmp_len] = ' '; | |
740 | } | |
41cd23b7 HS |
741 | if (*part_list) |
742 | part_list[strlen(part_list) - 1] = 0; | |
743 | break; | |
12fc1f3b | 744 | } |
12fc1f3b CD |
745 | debug("setenv gpt_partition_list %s\n", part_list); |
746 | ||
747 | return env_set("gpt_partition_list", part_list); | |
748 | } | |
749 | ||
750 | /** | |
751 | * gpt_setenv_part_variables() - setup partition environmental variables | |
752 | * | |
753 | * Setup the gpt_partition_name, gpt_partition_entry, gpt_partition_addr | |
b1433aff | 754 | * and gpt_partition_size, gpt_partition_bootable environment variables. |
12fc1f3b CD |
755 | * |
756 | * @pinfo: pointer to disk partition | |
757 | * @i: partition entry | |
758 | * | |
759 | * @Return: '0' on success and -ENOENT on failure | |
760 | */ | |
761 | static int gpt_setenv_part_variables(struct disk_partition *pinfo, int i) | |
762 | { | |
763 | int ret; | |
764 | ||
765 | ret = env_set_hex("gpt_partition_addr", pinfo->start); | |
766 | if (ret) | |
767 | goto fail; | |
768 | ||
769 | ret = env_set_hex("gpt_partition_size", pinfo->size); | |
770 | if (ret) | |
771 | goto fail; | |
772 | ||
eeef5840 | 773 | ret = env_set_hex("gpt_partition_entry", i); |
12fc1f3b CD |
774 | if (ret) |
775 | goto fail; | |
776 | ||
777 | ret = env_set("gpt_partition_name", (const char *)pinfo->name); | |
778 | if (ret) | |
779 | goto fail; | |
780 | ||
b1433aff JW |
781 | ret = env_set_ulong("gpt_partition_bootable", !!(pinfo->bootable & PART_BOOTABLE)); |
782 | if (ret) | |
783 | goto fail; | |
784 | ||
12fc1f3b CD |
785 | return 0; |
786 | ||
787 | fail: | |
788 | return -ENOENT; | |
789 | } | |
790 | ||
791 | /** | |
792 | * gpt_setenv() - Dynamically setup environment variables. | |
793 | * | |
794 | * Dynamically setup environment variables for name, index, offset and size | |
795 | * for partition in GPT table after running "gpt setenv" for a partition name. | |
796 | * | |
797 | * @desc: block device descriptor | |
798 | * @name: partition name | |
799 | * | |
800 | * @Return: '0' on success and -ve err on failure | |
801 | */ | |
802 | static int gpt_setenv(struct blk_desc *desc, const char *name) | |
803 | { | |
804 | struct part_driver *first_drv, *part_drv; | |
805 | int n_drvs; | |
806 | int ret = -1; | |
807 | ||
808 | n_drvs = part_driver_get_count(); | |
809 | if (!n_drvs) { | |
810 | printf("Failed to get partition driver count\n"); | |
811 | goto fail; | |
812 | } | |
813 | ||
814 | first_drv = part_driver_get_first(); | |
815 | for (part_drv = first_drv; part_drv != first_drv + n_drvs; part_drv++) { | |
816 | struct disk_partition pinfo; | |
817 | int i; | |
818 | ||
819 | for (i = 1; i < part_drv->max_entries; i++) { | |
820 | ret = part_drv->get_info(desc, i, &pinfo); | |
01834677 HS |
821 | if (ret) |
822 | continue; | |
12fc1f3b CD |
823 | |
824 | if (!strcmp(name, (const char *)pinfo.name)) { | |
825 | /* match found, setup environment variables */ | |
826 | ret = gpt_setenv_part_variables(&pinfo, i); | |
827 | if (ret) | |
828 | goto fail; | |
829 | ||
830 | return 0; | |
831 | } | |
832 | } | |
833 | } | |
834 | ||
835 | fail: | |
836 | return ret; | |
837 | } | |
838 | ||
73d6d18b AC |
839 | static int do_disk_guid(struct blk_desc *dev_desc, char * const namestr) |
840 | { | |
841 | int ret; | |
842 | char disk_guid[UUID_STR_LEN + 1]; | |
843 | ||
844 | ret = get_disk_guid(dev_desc, disk_guid); | |
845 | if (ret < 0) | |
846 | return CMD_RET_FAILURE; | |
847 | ||
848 | if (namestr) | |
382bee57 | 849 | env_set(namestr, disk_guid); |
73d6d18b AC |
850 | else |
851 | printf("%s\n", disk_guid); | |
852 | ||
853 | return ret; | |
854 | } | |
855 | ||
203f9b48 AC |
856 | #ifdef CONFIG_CMD_GPT_RENAME |
857 | static int do_rename_gpt_parts(struct blk_desc *dev_desc, char *subcomm, | |
858 | char *name1, char *name2) | |
859 | { | |
860 | struct list_head *pos; | |
861 | struct disk_part *curr; | |
0528979f | 862 | struct disk_partition *new_partitions = NULL; |
203f9b48 | 863 | char disk_guid[UUID_STR_LEN + 1]; |
5749faa3 | 864 | char *partitions_list, *str_disk_guid = NULL; |
203f9b48 AC |
865 | u8 part_count = 0; |
866 | int partlistlen, ret, numparts = 0, partnum, i = 1, ctr1 = 0, ctr2 = 0; | |
867 | ||
7cc1d87d JW |
868 | if (!subcomm || !name1 || !name2 || |
869 | (strcmp(subcomm, "swap") && strcmp(subcomm, "rename") && | |
870 | strcmp(subcomm, "transpose"))) | |
203f9b48 AC |
871 | return -EINVAL; |
872 | ||
873 | ret = get_disk_guid(dev_desc, disk_guid); | |
874 | if (ret < 0) | |
875 | return ret; | |
18030d04 AC |
876 | /* |
877 | * Allocates disk_partitions, requiring matching call to del_gpt_info() | |
878 | * if successful. | |
879 | */ | |
203f9b48 AC |
880 | numparts = get_gpt_info(dev_desc); |
881 | if (numparts <= 0) | |
882 | return numparts ? numparts : -ENODEV; | |
883 | ||
884 | partlistlen = calc_parts_list_len(numparts); | |
885 | partitions_list = malloc(partlistlen); | |
18030d04 AC |
886 | if (!partitions_list) { |
887 | del_gpt_info(); | |
203f9b48 | 888 | return -ENOMEM; |
18030d04 | 889 | } |
203f9b48 AC |
890 | memset(partitions_list, '\0', partlistlen); |
891 | ||
892 | ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list); | |
18030d04 AC |
893 | if (ret < 0) { |
894 | free(partitions_list); | |
203f9b48 | 895 | return ret; |
18030d04 | 896 | } |
203f9b48 AC |
897 | /* |
898 | * Uncomment the following line to print a string that 'gpt write' | |
899 | * or 'gpt verify' will accept as input. | |
900 | */ | |
901 | debug("OLD partitions_list is %s with %u chars\n", partitions_list, | |
902 | (unsigned)strlen(partitions_list)); | |
903 | ||
18030d04 | 904 | /* set_gpt_info allocates new_partitions and str_disk_guid */ |
203f9b48 AC |
905 | ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid, |
906 | &new_partitions, &part_count); | |
5749faa3 TR |
907 | if (ret < 0) |
908 | goto out; | |
203f9b48 AC |
909 | |
910 | if (!strcmp(subcomm, "swap")) { | |
911 | if ((strlen(name1) > PART_NAME_LEN) || (strlen(name2) > PART_NAME_LEN)) { | |
912 | printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN); | |
18030d04 AC |
913 | ret = -EINVAL; |
914 | goto out; | |
203f9b48 AC |
915 | } |
916 | list_for_each(pos, &disk_partitions) { | |
917 | curr = list_entry(pos, struct disk_part, list); | |
918 | if (!strcmp((char *)curr->gpt_part_info.name, name1)) { | |
919 | strcpy((char *)curr->gpt_part_info.name, name2); | |
920 | ctr1++; | |
921 | } else if (!strcmp((char *)curr->gpt_part_info.name, name2)) { | |
922 | strcpy((char *)curr->gpt_part_info.name, name1); | |
923 | ctr2++; | |
924 | } | |
925 | } | |
926 | if ((ctr1 + ctr2 < 2) || (ctr1 != ctr2)) { | |
927 | printf("Cannot swap partition names except in pairs.\n"); | |
18030d04 AC |
928 | ret = -EINVAL; |
929 | goto out; | |
203f9b48 | 930 | } |
7cc1d87d JW |
931 | } else if (!strcmp(subcomm, "transpose")) { |
932 | int idx1, idx2; | |
933 | struct disk_partition* first = NULL; | |
934 | struct disk_partition* second= NULL; | |
935 | struct disk_partition tmp_part; | |
936 | ||
937 | idx1 = simple_strtoul(name1, NULL, 10); | |
938 | idx2 = simple_strtoul(name2, NULL, 10); | |
939 | if (idx1 == idx2) { | |
940 | printf("Cannot swap partition with itself\n"); | |
941 | ret = -EINVAL; | |
942 | goto out; | |
943 | } | |
944 | ||
945 | list_for_each(pos, &disk_partitions) { | |
946 | curr = list_entry(pos, struct disk_part, list); | |
947 | if (curr->partnum == idx1) | |
948 | first = &curr->gpt_part_info; | |
949 | else if (curr->partnum == idx2) | |
950 | second = &curr->gpt_part_info; | |
951 | } | |
952 | if (!first) { | |
953 | printf("Illegal partition number %s\n", name1); | |
954 | ret = -EINVAL; | |
955 | goto out; | |
956 | } | |
957 | if (!second) { | |
958 | printf("Illegal partition number %s\n", name2); | |
959 | ret = -EINVAL; | |
960 | goto out; | |
961 | } | |
962 | ||
963 | tmp_part = *first; | |
964 | *first = *second; | |
965 | *second = tmp_part; | |
203f9b48 AC |
966 | } else { /* rename */ |
967 | if (strlen(name2) > PART_NAME_LEN) { | |
968 | printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN); | |
18030d04 AC |
969 | ret = -EINVAL; |
970 | goto out; | |
203f9b48 AC |
971 | } |
972 | partnum = (int)simple_strtol(name1, NULL, 10); | |
973 | if ((partnum < 0) || (partnum > numparts)) { | |
974 | printf("Illegal partition number %s\n", name1); | |
18030d04 AC |
975 | ret = -EINVAL; |
976 | goto out; | |
203f9b48 AC |
977 | } |
978 | ret = part_get_info(dev_desc, partnum, new_partitions); | |
979 | if (ret < 0) | |
18030d04 | 980 | goto out; |
203f9b48 AC |
981 | |
982 | /* U-Boot partition numbering starts at 1 */ | |
983 | list_for_each(pos, &disk_partitions) { | |
984 | curr = list_entry(pos, struct disk_part, list); | |
985 | if (i == partnum) { | |
986 | strcpy((char *)curr->gpt_part_info.name, name2); | |
987 | break; | |
988 | } | |
989 | i++; | |
990 | } | |
991 | } | |
992 | ||
993 | ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list); | |
994 | if (ret < 0) | |
18030d04 | 995 | goto out; |
203f9b48 AC |
996 | debug("NEW partitions_list is %s with %u chars\n", partitions_list, |
997 | (unsigned)strlen(partitions_list)); | |
998 | ||
999 | ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid, | |
1000 | &new_partitions, &part_count); | |
18030d04 AC |
1001 | /* |
1002 | * Even though valid pointers are here passed into set_gpt_info(), | |
1003 | * it mallocs again, and there's no way to tell which failed. | |
1004 | */ | |
5749faa3 TR |
1005 | if (ret < 0) |
1006 | goto out; | |
203f9b48 AC |
1007 | |
1008 | debug("Writing new partition table\n"); | |
1009 | ret = gpt_restore(dev_desc, disk_guid, new_partitions, numparts); | |
1010 | if (ret < 0) { | |
1011 | printf("Writing new partition table failed\n"); | |
18030d04 | 1012 | goto out; |
203f9b48 AC |
1013 | } |
1014 | ||
1015 | debug("Reading back new partition table\n"); | |
18030d04 AC |
1016 | /* |
1017 | * Empty the existing disk_partitions list, as otherwise the memory in | |
1018 | * the original list is unreachable. | |
1019 | */ | |
1020 | del_gpt_info(); | |
203f9b48 | 1021 | numparts = get_gpt_info(dev_desc); |
18030d04 AC |
1022 | if (numparts <= 0) { |
1023 | ret = numparts ? numparts : -ENODEV; | |
1024 | goto out; | |
1025 | } | |
203f9b48 AC |
1026 | printf("new partition table with %d partitions is:\n", numparts); |
1027 | print_gpt_info(); | |
18030d04 | 1028 | out: |
5749faa3 TR |
1029 | del_gpt_info(); |
1030 | #ifdef CONFIG_RANDOM_UUID | |
b142d0ac | 1031 | free(str_disk_guid); |
5749faa3 | 1032 | #endif |
b142d0ac | 1033 | free(new_partitions); |
18030d04 | 1034 | free(partitions_list); |
203f9b48 AC |
1035 | return ret; |
1036 | } | |
a1e793ad JW |
1037 | |
1038 | /** | |
1039 | * gpt_set_bootable() - Set bootable flags for partitions | |
1040 | * | |
1041 | * Sets the bootable flag for any partition names in the comma separated list of | |
1042 | * partition names. Any partitions not in the list have their bootable flag | |
1043 | * cleared | |
1044 | * | |
1045 | * @desc: block device descriptor | |
1046 | * @name: Comma separated list of partition names | |
1047 | * | |
1048 | * @Return: '0' on success and -ve error on failure | |
1049 | */ | |
1050 | static int gpt_set_bootable(struct blk_desc *blk_dev_desc, char *const part_list) | |
1051 | { | |
1052 | char *name; | |
1053 | char disk_guid[UUID_STR_LEN + 1]; | |
1054 | struct list_head *pos; | |
1055 | struct disk_part *curr; | |
1056 | struct disk_partition *partitions = NULL; | |
1057 | int part_count = 0; | |
1058 | int ret = get_disk_guid(blk_dev_desc, disk_guid); | |
1059 | ||
1060 | if (ret < 0) | |
1061 | return ret; | |
1062 | ||
1063 | ret = get_gpt_info(blk_dev_desc); | |
1064 | if (ret <= 0) | |
1065 | goto out; | |
1066 | ||
1067 | part_count = ret; | |
1068 | partitions = malloc(sizeof(*partitions) * part_count); | |
1069 | if (!partitions) { | |
1070 | ret = -ENOMEM; | |
1071 | goto out; | |
1072 | } | |
1073 | ||
1074 | /* Copy partitions and clear bootable flag */ | |
1075 | part_count = 0; | |
1076 | list_for_each(pos, &disk_partitions) { | |
1077 | curr = list_entry(pos, struct disk_part, list); | |
1078 | partitions[part_count] = curr->gpt_part_info; | |
1079 | partitions[part_count].bootable &= ~PART_BOOTABLE; | |
1080 | part_count++; | |
1081 | } | |
1082 | ||
1083 | name = strtok(part_list, ","); | |
1084 | while (name) { | |
1085 | bool found = false; | |
1086 | ||
1087 | for (int i = 0; i < part_count; i++) { | |
1088 | if (strcmp((char *)partitions[i].name, name) == 0) { | |
1089 | partitions[i].bootable |= PART_BOOTABLE; | |
1090 | found = true; | |
1091 | } | |
1092 | } | |
1093 | ||
1094 | if (!found) { | |
1095 | printf("Warning: No partition matching '%s' found\n", | |
1096 | name); | |
1097 | } | |
1098 | ||
1099 | name = strtok(NULL, ","); | |
1100 | } | |
1101 | ||
1102 | ret = gpt_restore(blk_dev_desc, disk_guid, partitions, part_count); | |
1103 | ||
1104 | out: | |
1105 | del_gpt_info(); | |
1106 | ||
1107 | if (partitions) | |
1108 | free(partitions); | |
1109 | ||
1110 | return ret; | |
1111 | } | |
203f9b48 AC |
1112 | #endif |
1113 | ||
8b096237 PW |
1114 | /** |
1115 | * do_gpt(): Perform GPT operations | |
1116 | * | |
1117 | * @param cmdtp - command name | |
1118 | * @param flag | |
1119 | * @param argc | |
1120 | * @param argv | |
1121 | * | |
185f812c | 1122 | * Return: zero on success; otherwise error |
8b096237 | 1123 | */ |
09140113 | 1124 | static int do_gpt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
8b096237 PW |
1125 | { |
1126 | int ret = CMD_RET_SUCCESS; | |
1127 | int dev = 0; | |
619f0fdf | 1128 | char *ep; |
4101f687 | 1129 | struct blk_desc *blk_dev_desc = NULL; |
8b096237 | 1130 | |
203f9b48 | 1131 | #ifndef CONFIG_CMD_GPT_RENAME |
bbb9ffac | 1132 | if (argc < 4 || argc > 5) |
203f9b48 AC |
1133 | #else |
1134 | if (argc < 4 || argc > 6) | |
1135 | #endif | |
8b096237 PW |
1136 | return CMD_RET_USAGE; |
1137 | ||
0b1284eb | 1138 | dev = (int)dectoul(argv[3], &ep); |
bbb9ffac LM |
1139 | if (!ep || ep[0] != '\0') { |
1140 | printf("'%s' is not a number\n", argv[3]); | |
1141 | return CMD_RET_USAGE; | |
1142 | } | |
db1d9e78 | 1143 | blk_dev_desc = blk_get_dev(argv[2], dev); |
bbb9ffac LM |
1144 | if (!blk_dev_desc) { |
1145 | printf("%s: %s dev %d NOT available\n", | |
1146 | __func__, argv[2], dev); | |
1147 | return CMD_RET_FAILURE; | |
1148 | } | |
39206382 | 1149 | |
26f404c7 PR |
1150 | if (strcmp(argv[1], "repair") == 0) { |
1151 | printf("Repairing GPT: "); | |
1152 | ret = gpt_repair(blk_dev_desc); | |
1153 | } else if ((strcmp(argv[1], "write") == 0) && (argc == 5)) { | |
bbb9ffac | 1154 | printf("Writing GPT: "); |
39206382 | 1155 | ret = gpt_default(blk_dev_desc, argv[4]); |
bbb9ffac LM |
1156 | } else if ((strcmp(argv[1], "verify") == 0)) { |
1157 | ret = gpt_verify(blk_dev_desc, argv[4]); | |
1158 | printf("Verify GPT: "); | |
12fc1f3b CD |
1159 | } else if ((strcmp(argv[1], "setenv") == 0)) { |
1160 | ret = gpt_setenv(blk_dev_desc, argv[4]); | |
1161 | } else if ((strcmp(argv[1], "enumerate") == 0)) { | |
1162 | ret = gpt_enumerate(blk_dev_desc); | |
73d6d18b AC |
1163 | } else if (strcmp(argv[1], "guid") == 0) { |
1164 | ret = do_disk_guid(blk_dev_desc, argv[4]); | |
09a49930 AC |
1165 | #ifdef CONFIG_CMD_GPT_RENAME |
1166 | } else if (strcmp(argv[1], "read") == 0) { | |
653cd92d | 1167 | ret = do_get_gpt_info(blk_dev_desc, (argc == 5) ? argv[4] : NULL); |
203f9b48 | 1168 | } else if ((strcmp(argv[1], "swap") == 0) || |
7cc1d87d JW |
1169 | (strcmp(argv[1], "rename") == 0) || |
1170 | (strcmp(argv[1], "transpose") == 0)) { | |
203f9b48 | 1171 | ret = do_rename_gpt_parts(blk_dev_desc, argv[1], argv[4], argv[5]); |
a1e793ad JW |
1172 | } else if ((strcmp(argv[1], "set-bootable") == 0)) { |
1173 | ret = gpt_set_bootable(blk_dev_desc, argv[4]); | |
09a49930 | 1174 | #endif |
8b096237 PW |
1175 | } else { |
1176 | return CMD_RET_USAGE; | |
1177 | } | |
bbb9ffac LM |
1178 | |
1179 | if (ret) { | |
1180 | printf("error!\n"); | |
1181 | return CMD_RET_FAILURE; | |
1182 | } | |
1183 | ||
1184 | printf("success!\n"); | |
1185 | return CMD_RET_SUCCESS; | |
8b096237 PW |
1186 | } |
1187 | ||
1188 | U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt, | |
1189 | "GUID Partition Table", | |
1f8b546f | 1190 | "<command> <interface> <dev> <partitions_list>\n" |
74f889b0 LM |
1191 | " - GUID partition table restoration and validity check\n" |
1192 | " Restore or verify GPT information on a device connected\n" | |
8b096237 | 1193 | " to interface\n" |
74f889b0 | 1194 | " Example usage:\n" |
26f404c7 PR |
1195 | " gpt repair mmc 0\n" |
1196 | " - repair the GPT on the device\n" | |
74f889b0 | 1197 | " gpt write mmc 0 $partitions\n" |
12fc1f3b | 1198 | " - write the GPT to device\n" |
74f889b0 | 1199 | " gpt verify mmc 0 $partitions\n" |
12fc1f3b CD |
1200 | " - verify the GPT on device against $partitions\n" |
1201 | " gpt setenv mmc 0 $name\n" | |
1202 | " - setup environment variables for partition $name:\n" | |
1203 | " gpt_partition_addr, gpt_partition_size,\n" | |
b1433aff JW |
1204 | " gpt_partition_name, gpt_partition_entry,\n" |
1205 | " gpt_partition_bootable\n" | |
12fc1f3b CD |
1206 | " gpt enumerate mmc 0\n" |
1207 | " - store list of partitions to gpt_partition_list environment variable\n" | |
d0266096 | 1208 | " gpt guid <interface> <dev>\n" |
73d6d18b | 1209 | " - print disk GUID\n" |
d0266096 | 1210 | " gpt guid <interface> <dev> <varname>\n" |
73d6d18b AC |
1211 | " - set environment variable to disk GUID\n" |
1212 | " Example usage:\n" | |
1213 | " gpt guid mmc 0\n" | |
1214 | " gpt guid mmc 0 varname\n" | |
203f9b48 AC |
1215 | #ifdef CONFIG_CMD_GPT_RENAME |
1216 | "gpt partition renaming commands:\n" | |
653cd92d | 1217 | " gpt read <interface> <dev> [<varname>]\n" |
d0266096 | 1218 | " - read GPT into a data structure for manipulation\n" |
653cd92d | 1219 | " - read GPT partitions into environment variable\n" |
d0266096 | 1220 | " gpt swap <interface> <dev> <name1> <name2>\n" |
203f9b48 AC |
1221 | " - change all partitions named name1 to name2\n" |
1222 | " and vice-versa\n" | |
7cc1d87d JW |
1223 | " gpt transpose <interface> <dev> <part1> <part2>\n" |
1224 | " - Swap the order of the entries for part1 and part2 in the partition table\n" | |
d0266096 | 1225 | " gpt rename <interface> <dev> <part> <name>\n" |
203f9b48 | 1226 | " - rename the specified partition\n" |
a1e793ad JW |
1227 | " gpt set-bootable <interface> <dev> <list>\n" |
1228 | " - make partition names in list bootable\n" | |
203f9b48 AC |
1229 | " Example usage:\n" |
1230 | " gpt swap mmc 0 foo bar\n" | |
1231 | " gpt rename mmc 0 3 foo\n" | |
a1e793ad | 1232 | " gpt set-bootable mmc 0 boot_a,boot_b\n" |
7cc1d87d | 1233 | " gpt transpose mmc 0 1 2\n" |
203f9b48 | 1234 | #endif |
8b096237 | 1235 | ); |