1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * (C) Copyright 2001
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 */
6
7 #include <blk.h>
8 #include <command.h>
9 #include <env.h>
10 #include <errno.h>
11 #include <log.h>
12 #include <malloc.h>
13 #include <part.h>
14 #include <ubifs_uboot.h>
15 #include <dm/uclass.h>
16
17 #undef PART_DEBUG
18
19 #ifdef PART_DEBUG
20 #define PRINTF(fmt,args...) printf (fmt ,##args)
21 #else
22 #define PRINTF(fmt,args...)
23 #endif
24
25 /* Check all partition types */
26 #define PART_TYPE_ALL -1
27
28 /**
29 * part_driver_get_type() - Get a driver given its type
30 *
31 * @part_type: Partition type to find the driver for
32 * Return: Driver for that type, or NULL if none
33 */
part_driver_get_type(int part_type)34 static struct part_driver *part_driver_get_type(int part_type)
35 {
36 struct part_driver *drv =
37 ll_entry_start(struct part_driver, part_driver);
38 const int n_ents = ll_entry_count(struct part_driver, part_driver);
39 struct part_driver *entry;
40
41 for (entry = drv; entry != drv + n_ents; entry++) {
42 if (part_type == entry->part_type)
43 return entry;
44 }
45
46 /* Not found */
47 return NULL;
48 }
49
50 /**
51 * part_driver_lookup_type() - Look up the partition driver for a blk device
52 *
53 * If @desc->part_type is PART_TYPE_UNKNOWN, this checks each parition driver
54 * against the blk device to see if there is a valid partition table acceptable
55 * to that driver.
56 *
57 * If @desc->part_type is already set, it just returns the driver for that
58 * type, without testing if the driver can find a valid partition on the
59 * descriptor.
60 *
61 * On success it updates @desc->part_type if set to PART_TYPE_UNKNOWN on entry
62 *
63 * @dev_desc: Device descriptor
64 * Return: Driver found, or NULL if none
65 */
part_driver_lookup_type(struct blk_desc * desc)66 static struct part_driver *part_driver_lookup_type(struct blk_desc *desc)
67 {
68 struct part_driver *drv =
69 ll_entry_start(struct part_driver, part_driver);
70 const int n_ents = ll_entry_count(struct part_driver, part_driver);
71 struct part_driver *entry;
72
73 if (desc->part_type == PART_TYPE_UNKNOWN) {
74 for (entry = drv; entry != drv + n_ents; entry++) {
75 int ret;
76
77 ret = entry->test(desc);
78 if (!ret) {
79 desc->part_type = entry->part_type;
80 return entry;
81 }
82 }
83 } else {
84 return part_driver_get_type(desc->part_type);
85 }
86
87 /* Not found */
88 return NULL;
89 }
90
part_get_type_by_name(const char * name)91 int part_get_type_by_name(const char *name)
92 {
93 struct part_driver *drv =
94 ll_entry_start(struct part_driver, part_driver);
95 const int n_ents = ll_entry_count(struct part_driver, part_driver);
96 struct part_driver *entry;
97
98 for (entry = drv; entry != drv + n_ents; entry++) {
99 if (!strcasecmp(name, entry->name))
100 return entry->part_type;
101 }
102
103 /* Not found */
104 return PART_TYPE_UNKNOWN;
105 }
106
107 /**
108 * get_dev_hwpart() - Get the descriptor for a device with hardware partitions
109 *
110 * @ifname: Interface name (e.g. "ide", "scsi")
111 * @dev: Device number (0 for first device on that interface, 1 for
112 * second, etc.
113 * @hwpart: Hardware partition, or 0 if none (used for MMC)
114 * Return: pointer to the block device, or NULL if not available, or an
115 * error occurred.
116 */
get_dev_hwpart(const char * ifname,int dev,int hwpart)117 static struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
118 {
119 struct blk_desc *desc;
120 int ret;
121
122 if (!blk_enabled())
123 return NULL;
124 desc = blk_get_devnum_by_uclass_idname(ifname, dev);
125 if (!desc) {
126 debug("%s: No device for iface '%s', dev %d\n", __func__,
127 ifname, dev);
128 return NULL;
129 }
130 ret = blk_dselect_hwpart(desc, hwpart);
131 if (ret) {
132 debug("%s: Failed to select h/w partition: err-%d\n", __func__,
133 ret);
134 return NULL;
135 }
136
137 return desc;
138 }
139
blk_get_dev(const char * ifname,int dev)140 struct blk_desc *blk_get_dev(const char *ifname, int dev)
141 {
142 if (!blk_enabled())
143 return NULL;
144
145 return get_dev_hwpart(ifname, dev, 0);
146 }
147
148 /* ------------------------------------------------------------------------- */
149 /*
150 * reports device info to the user
151 */
152
153 #ifdef CONFIG_LBA48
154 typedef uint64_t lba512_t;
155 #else
156 typedef lbaint_t lba512_t;
157 #endif
158
159 /*
160 * Overflowless variant of (block_count * mul_by / 2**right_shift)
161 * when 2**right_shift > mul_by
162 */
lba512_muldiv(lba512_t block_count,lba512_t mul_by,int right_shift)163 static lba512_t lba512_muldiv(lba512_t block_count, lba512_t mul_by,
164 int right_shift)
165 {
166 lba512_t bc_quot, bc_rem;
167
168 /* x * m / d == x / d * m + (x % d) * m / d */
169 bc_quot = block_count >> right_shift;
170 bc_rem = block_count - (bc_quot << right_shift);
171 return bc_quot * mul_by + ((bc_rem * mul_by) >> right_shift);
172 }
173
dev_print(struct blk_desc * desc)174 void dev_print(struct blk_desc *desc)
175 {
176 lba512_t lba512; /* number of blocks if 512bytes block size */
177
178 if (desc->type == DEV_TYPE_UNKNOWN) {
179 puts ("not available\n");
180 return;
181 }
182
183 switch (desc->uclass_id) {
184 case UCLASS_SCSI:
185 printf("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n", desc->target,
186 desc->lun, desc->vendor, desc->product, desc->revision);
187 break;
188 case UCLASS_IDE:
189 case UCLASS_AHCI:
190 printf("Model: %s Firm: %s Ser#: %s\n", desc->vendor,
191 desc->revision, desc->product);
192 break;
193 case UCLASS_MMC:
194 case UCLASS_USB:
195 case UCLASS_NVME:
196 case UCLASS_PVBLOCK:
197 case UCLASS_HOST:
198 case UCLASS_BLKMAP:
199 case UCLASS_RKMTD:
200 printf ("Vendor: %s Rev: %s Prod: %s\n",
201 desc->vendor,
202 desc->revision,
203 desc->product);
204 break;
205 case UCLASS_VIRTIO:
206 printf("%s VirtIO Block Device\n", desc->vendor);
207 break;
208 case UCLASS_EFI_MEDIA:
209 printf("EFI media Block Device %d\n", desc->devnum);
210 break;
211 case UCLASS_INVALID:
212 puts("device type unknown\n");
213 return;
214 default:
215 printf("Unhandled device type: %i\n", desc->uclass_id);
216 return;
217 }
218 puts (" Type: ");
219 if (desc->removable)
220 puts ("Removable ");
221 switch (desc->type & 0x1F) {
222 case DEV_TYPE_HARDDISK:
223 puts ("Hard Disk");
224 break;
225 case DEV_TYPE_CDROM:
226 puts ("CD ROM");
227 break;
228 case DEV_TYPE_OPDISK:
229 puts ("Optical Device");
230 break;
231 case DEV_TYPE_TAPE:
232 puts ("Tape");
233 break;
234 default:
235 printf("# %02X #", desc->type & 0x1F);
236 break;
237 }
238 puts ("\n");
239 if (desc->lba > 0L && desc->blksz > 0L) {
240 ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
241 lbaint_t lba;
242
243 lba = desc->lba;
244
245 lba512 = lba * (desc->blksz / 512);
246 /* round to 1 digit */
247 /* 2048 = (1024 * 1024) / 512 MB */
248 mb = lba512_muldiv(lba512, 10, 11);
249
250 mb_quot = mb / 10;
251 mb_rem = mb - (10 * mb_quot);
252
253 gb = mb / 1024;
254 gb_quot = gb / 10;
255 gb_rem = gb - (10 * gb_quot);
256 #ifdef CONFIG_LBA48
257 if (desc->lba48)
258 printf (" Supports 48-bit addressing\n");
259 #endif
260 #if defined(CONFIG_SYS_64BIT_LBA)
261 printf (" Capacity: %lu.%lu MB = %lu.%lu GB (%llu x %lu)\n",
262 mb_quot, mb_rem,
263 gb_quot, gb_rem,
264 lba,
265 desc->blksz);
266 #else
267 printf (" Capacity: %lu.%lu MB = %lu.%lu GB (%lu x %lu)\n",
268 mb_quot, mb_rem,
269 gb_quot, gb_rem,
270 (ulong)lba,
271 desc->blksz);
272 #endif
273 } else {
274 puts (" Capacity: not available\n");
275 }
276 }
277
part_init(struct blk_desc * desc)278 void part_init(struct blk_desc *desc)
279 {
280 struct part_driver *drv =
281 ll_entry_start(struct part_driver, part_driver);
282 const int n_ents = ll_entry_count(struct part_driver, part_driver);
283 struct part_driver *entry;
284
285 blkcache_invalidate(desc->uclass_id, desc->devnum);
286
287 if (desc->part_type != PART_TYPE_UNKNOWN) {
288 for (entry = drv; entry != drv + n_ents; entry++) {
289 if (entry->part_type == desc->part_type && !entry->test(desc))
290 return;
291 }
292 }
293
294 desc->part_type = PART_TYPE_UNKNOWN;
295 for (entry = drv; entry != drv + n_ents; entry++) {
296 int ret;
297
298 ret = entry->test(desc);
299 debug("%s: try '%s': ret=%d\n", __func__, entry->name, ret);
300 if (!ret) {
301 desc->part_type = entry->part_type;
302 break;
303 }
304 }
305 }
306
print_part_header(const char * type,struct blk_desc * desc)307 static void print_part_header(const char *type, struct blk_desc *desc)
308 {
309 #if CONFIG_IS_ENABLED(MAC_PARTITION) || \
310 CONFIG_IS_ENABLED(DOS_PARTITION) || \
311 CONFIG_IS_ENABLED(ISO_PARTITION) || \
312 CONFIG_IS_ENABLED(AMIGA_PARTITION) || \
313 CONFIG_IS_ENABLED(EFI_PARTITION) || \
314 CONFIG_IS_ENABLED(MTD_PARTITIONS)
315 printf("\nPartition Map for %s device %d -- Partition Type: %s\n\n",
316 uclass_get_name(desc->uclass_id), desc->devnum, type);
317 #endif /* any CONFIG_..._PARTITION */
318 }
319
part_print(struct blk_desc * desc)320 void part_print(struct blk_desc *desc)
321 {
322 struct part_driver *drv;
323
324 drv = part_driver_lookup_type(desc);
325 if (!drv) {
326 printf("## Unknown partition table type %x\n",
327 desc->part_type);
328 return;
329 }
330
331 PRINTF("## Testing for valid %s partition ##\n", drv->name);
332 print_part_header(drv->name, desc);
333 if (drv->print)
334 drv->print(desc);
335 }
336
part_get_info_by_type(struct blk_desc * desc,int part,int part_type,struct disk_partition * info)337 int part_get_info_by_type(struct blk_desc *desc, int part, int part_type,
338 struct disk_partition *info)
339 {
340 struct part_driver *drv;
341
342 if (blk_enabled()) {
343 /* The common case is no UUID support */
344 disk_partition_clr_uuid(info);
345 disk_partition_clr_type_guid(info);
346
347 if (part_type == PART_TYPE_UNKNOWN) {
348 drv = part_driver_lookup_type(desc);
349 } else {
350 drv = part_driver_get_type(part_type);
351 }
352
353 if (!drv) {
354 debug("## Unknown partition table type %x\n",
355 desc->part_type);
356 return -EPROTONOSUPPORT;
357 }
358 if (!drv->get_info) {
359 PRINTF("## Driver %s does not have the get_info() method\n",
360 drv->name);
361 return -ENOSYS;
362 }
363 if (drv->get_info(desc, part, info) == 0) {
364 PRINTF("## Valid %s partition found ##\n", drv->name);
365 return 0;
366 }
367 }
368
369 return -ENOENT;
370 }
371
part_get_info(struct blk_desc * desc,int part,struct disk_partition * info)372 int part_get_info(struct blk_desc *desc, int part,
373 struct disk_partition *info)
374 {
375 return part_get_info_by_type(desc, part, PART_TYPE_UNKNOWN, info);
376 }
377
part_get_info_whole_disk(struct blk_desc * desc,struct disk_partition * info)378 int part_get_info_whole_disk(struct blk_desc *desc,
379 struct disk_partition *info)
380 {
381 info->start = 0;
382 info->size = desc->lba;
383 info->blksz = desc->blksz;
384 info->bootable = 0;
385 strcpy((char *)info->type, BOOT_PART_TYPE);
386 strcpy((char *)info->name, "Whole Disk");
387 disk_partition_clr_uuid(info);
388 disk_partition_clr_type_guid(info);
389
390 return 0;
391 }
392
blk_get_device_by_str(const char * ifname,const char * dev_hwpart_str,struct blk_desc ** desc)393 int blk_get_device_by_str(const char *ifname, const char *dev_hwpart_str,
394 struct blk_desc **desc)
395 {
396 char *ep;
397 char *dup_str = NULL;
398 const char *dev_str, *hwpart_str;
399 int dev, hwpart;
400
401 hwpart_str = strchr(dev_hwpart_str, '.');
402 if (hwpart_str) {
403 dup_str = strdup(dev_hwpart_str);
404 dup_str[hwpart_str - dev_hwpart_str] = 0;
405 dev_str = dup_str;
406 hwpart_str++;
407 } else {
408 dev_str = dev_hwpart_str;
409 hwpart = 0;
410 }
411
412 dev = hextoul(dev_str, &ep);
413 if (*ep) {
414 printf("** Bad device specification %s %s **\n",
415 ifname, dev_str);
416 dev = -EINVAL;
417 goto cleanup;
418 }
419
420 if (hwpart_str) {
421 hwpart = hextoul(hwpart_str, &ep);
422 if (*ep) {
423 printf("** Bad HW partition specification %s %s **\n",
424 ifname, hwpart_str);
425 dev = -EINVAL;
426 goto cleanup;
427 }
428 }
429
430 *desc = get_dev_hwpart(ifname, dev, hwpart);
431 if (!(*desc) || ((*desc)->type == DEV_TYPE_UNKNOWN)) {
432 debug("** Bad device %s %s **\n", ifname, dev_hwpart_str);
433 dev = -ENODEV;
434 goto cleanup;
435 }
436
437 if (blk_enabled()) {
438 /*
439 * Updates the partition table for the specified hw partition.
440 * Always should be done, otherwise hw partition 0 will return
441 * stale data after displaying a non-zero hw partition.
442 */
443 if ((*desc)->uclass_id == UCLASS_MMC)
444 part_init(*desc);
445 }
446
447 cleanup:
448 free(dup_str);
449 return dev;
450 }
451
452 #define PART_UNSPECIFIED -2
453 #define PART_AUTO -1
blk_get_device_part_str(const char * ifname,const char * dev_part_str,struct blk_desc ** desc,struct disk_partition * info,int allow_whole_dev)454 int blk_get_device_part_str(const char *ifname, const char *dev_part_str,
455 struct blk_desc **desc,
456 struct disk_partition *info, int allow_whole_dev)
457 {
458 int ret;
459 const char *part_str;
460 char *dup_str = NULL;
461 const char *dev_str;
462 int dev;
463 char *ep;
464 int p;
465 int part;
466 struct disk_partition tmpinfo;
467
468 *desc = NULL;
469 memset(info, 0, sizeof(*info));
470
471 #if IS_ENABLED(CONFIG_SANDBOX) || IS_ENABLED(CONFIG_SEMIHOSTING)
472 /*
473 * Special-case a pseudo block device "hostfs", to allow access to the
474 * host's own filesystem.
475 */
476 if (!strcmp(ifname, "hostfs")) {
477 strcpy((char *)info->type, BOOT_PART_TYPE);
478 strcpy((char *)info->name, "Host filesystem");
479
480 return 0;
481 }
482 #endif
483
484 #if IS_ENABLED(CONFIG_CMD_UBIFS) && !IS_ENABLED(CONFIG_XPL_BUILD)
485 /*
486 * Special-case ubi, ubi goes through a mtd, rather than through
487 * a regular block device.
488 */
489 if (!strcmp(ifname, "ubi")) {
490 if (!ubifs_is_mounted()) {
491 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
492 return -EINVAL;
493 }
494
495 strcpy((char *)info->type, BOOT_PART_TYPE);
496 strcpy((char *)info->name, "UBI");
497 return 0;
498 }
499 #endif
500
501 /* If no dev_part_str, use bootdevice environment variable */
502 if (CONFIG_IS_ENABLED(ENV_SUPPORT)) {
503 if (!dev_part_str || !strlen(dev_part_str) ||
504 !strcmp(dev_part_str, "-"))
505 dev_part_str = env_get("bootdevice");
506 }
507
508 /* If still no dev_part_str, it's an error */
509 if (!dev_part_str) {
510 printf("** No device specified **\n");
511 ret = -ENODEV;
512 goto cleanup;
513 }
514
515 /* Separate device and partition ID specification */
516 part_str = strchr(dev_part_str, ':');
517 if (part_str) {
518 dup_str = strdup(dev_part_str);
519 dup_str[part_str - dev_part_str] = 0;
520 dev_str = dup_str;
521 part_str++;
522 } else {
523 dev_str = dev_part_str;
524 }
525
526 /* Look up the device */
527 dev = blk_get_device_by_str(ifname, dev_str, desc);
528 if (dev < 0) {
529 printf("** Bad device specification %s %s **\n",
530 ifname, dev_str);
531 ret = dev;
532 goto cleanup;
533 }
534
535 /* Convert partition ID string to number */
536 if (!part_str || !*part_str) {
537 part = PART_UNSPECIFIED;
538 } else if (!strcmp(part_str, "auto")) {
539 part = PART_AUTO;
540 } else {
541 /* Something specified -> use exactly that */
542 part = (int)hextoul(part_str, &ep);
543 /*
544 * Less than whole string converted,
545 * or request for whole device, but caller requires partition.
546 */
547 if (*ep || (part == 0 && !allow_whole_dev)) {
548 printf("** Bad partition specification %s %s **\n",
549 ifname, dev_part_str);
550 ret = -ENOENT;
551 goto cleanup;
552 }
553 }
554
555 /*
556 * No partition table on device,
557 * or user requested partition 0 (entire device).
558 */
559 if (((*desc)->part_type == PART_TYPE_UNKNOWN) || !part) {
560 if (!(*desc)->lba) {
561 printf("** Bad device size - %s %s **\n", ifname,
562 dev_str);
563 ret = -EINVAL;
564 goto cleanup;
565 }
566
567 /*
568 * If user specified a partition ID other than 0,
569 * or the calling command only accepts partitions,
570 * it's an error.
571 */
572 if ((part > 0) || (!allow_whole_dev)) {
573 printf("** No partition table - %s %s **\n", ifname,
574 dev_str);
575 ret = -EPROTONOSUPPORT;
576 goto cleanup;
577 }
578
579 (*desc)->log2blksz = LOG2((*desc)->blksz);
580
581 part_get_info_whole_disk(*desc, info);
582
583 ret = 0;
584 goto cleanup;
585 }
586
587 /*
588 * Now there's known to be a partition table,
589 * not specifying a partition means to pick partition 1.
590 */
591 if (part == PART_UNSPECIFIED)
592 part = 1;
593
594 /*
595 * If user didn't specify a partition number, or did specify something
596 * other than "auto", use that partition number directly.
597 */
598 if (part != PART_AUTO) {
599 ret = part_get_info(*desc, part, info);
600 if (ret) {
601 printf("** Invalid partition %d **\n", part);
602 goto cleanup;
603 }
604 } else {
605 /*
606 * Find the first bootable partition.
607 * If none are bootable, fall back to the first valid partition.
608 */
609 part = 0;
610 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
611 ret = part_get_info(*desc, p, info);
612 if (ret)
613 continue;
614
615 /*
616 * First valid partition, or new better partition?
617 * If so, save partition ID.
618 */
619 if (!part || info->bootable)
620 part = p;
621
622 /* Best possible partition? Stop searching. */
623 if (info->bootable)
624 break;
625
626 /*
627 * We now need to search further for best possible.
628 * If we what we just queried was the best so far,
629 * save the info since we over-write it next loop.
630 */
631 if (part == p)
632 tmpinfo = *info;
633 }
634 /* If we found any acceptable partition */
635 if (part) {
636 /*
637 * If we searched all possible partition IDs,
638 * return the first valid partition we found.
639 */
640 if (p == MAX_SEARCH_PARTITIONS + 1)
641 *info = tmpinfo;
642 } else {
643 printf("** No valid partitions found **\n");
644 goto cleanup;
645 }
646 }
647 if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
648 printf("** Invalid partition type \"%.32s\""
649 " (expect \"" BOOT_PART_TYPE "\")\n",
650 info->type);
651 ret = -EINVAL;
652 goto cleanup;
653 }
654
655 (*desc)->log2blksz = LOG2((*desc)->blksz);
656
657 ret = part;
658 goto cleanup;
659
660 cleanup:
661 free(dup_str);
662 return ret;
663 }
664
part_get_info_by_name(struct blk_desc * desc,const char * name,struct disk_partition * info)665 int part_get_info_by_name(struct blk_desc *desc, const char *name,
666 struct disk_partition *info)
667 {
668 struct part_driver *part_drv;
669 int ret;
670 int i;
671
672 part_drv = part_driver_lookup_type(desc);
673 if (!part_drv)
674 return -1;
675
676 if (!part_drv->get_info) {
677 log_debug("## Driver %s does not have the get_info() method\n",
678 part_drv->name);
679 return -ENOSYS;
680 }
681
682 for (i = 1; i < part_drv->max_entries; i++) {
683 ret = part_drv->get_info(desc, i, info);
684 if (ret != 0) {
685 /*
686 * Partition with this index can't be obtained, but
687 * further partitions might be, so keep checking.
688 */
689 continue;
690 }
691 if (strcmp(name, (const char *)info->name) == 0) {
692 /* matched */
693 return i;
694 }
695 }
696
697 return -ENOENT;
698 }
699
part_get_info_by_uuid(struct blk_desc * desc,const char * uuid,struct disk_partition * info)700 int part_get_info_by_uuid(struct blk_desc *desc, const char *uuid,
701 struct disk_partition *info)
702 {
703 struct part_driver *part_drv;
704 int ret;
705 int i;
706
707 if (!CONFIG_IS_ENABLED(PARTITION_UUIDS))
708 return -ENOENT;
709
710 part_drv = part_driver_lookup_type(desc);
711 if (!part_drv)
712 return -1;
713
714 if (!part_drv->get_info) {
715 log_debug("## Driver %s does not have the get_info() method\n",
716 part_drv->name);
717 return -ENOSYS;
718 }
719
720 for (i = 1; i < part_drv->max_entries; i++) {
721 ret = part_drv->get_info(desc, i, info);
722 if (ret != 0) {
723 /*
724 * Partition with this index can't be obtained, but
725 * further partitions might be, so keep checking.
726 */
727 continue;
728 }
729
730 if (!strncasecmp(uuid, disk_partition_uuid(info), UUID_STR_LEN)) {
731 /* matched */
732 return i;
733 }
734 }
735
736 return -ENOENT;
737 }
738
739 /**
740 * Get partition info from device number and partition name.
741 *
742 * Parse a device number and partition name string in the form of
743 * "devicenum.hwpartnum#partition_name", for example "0.1#misc". devicenum and
744 * hwpartnum are both optional, defaulting to 0. If the partition is found,
745 * sets desc and part_info accordingly with the information of the
746 * partition with the given partition_name.
747 *
748 * @param[in] dev_iface Device interface
749 * @param[in] dev_part_str Input string argument, like "0.1#misc"
750 * @param[out] desc Place to store the device description pointer
751 * @param[out] part_info Place to store the partition information
752 * Return: 0 on success, or a negative on error
753 */
part_get_info_by_dev_and_name(const char * dev_iface,const char * dev_part_str,struct blk_desc ** desc,struct disk_partition * part_info)754 static int part_get_info_by_dev_and_name(const char *dev_iface,
755 const char *dev_part_str,
756 struct blk_desc **desc,
757 struct disk_partition *part_info)
758 {
759 char *dup_str = NULL;
760 const char *dev_str, *part_str;
761 int ret;
762
763 /* Separate device and partition name specification */
764 if (dev_part_str)
765 part_str = strchr(dev_part_str, '#');
766 else
767 part_str = NULL;
768
769 if (part_str) {
770 dup_str = strdup(dev_part_str);
771 dup_str[part_str - dev_part_str] = 0;
772 dev_str = dup_str;
773 part_str++;
774 } else {
775 return -EINVAL;
776 }
777
778 ret = blk_get_device_by_str(dev_iface, dev_str, desc);
779 if (ret < 0)
780 goto cleanup;
781
782 ret = part_get_info_by_name(*desc, part_str, part_info);
783 if (ret < 0)
784 printf("Could not find \"%s\" partition\n", part_str);
785
786 cleanup:
787 free(dup_str);
788 return ret;
789 }
790
part_get_info_by_dev_and_name_or_num(const char * dev_iface,const char * dev_part_str,struct blk_desc ** desc,struct disk_partition * part_info,int allow_whole_dev)791 int part_get_info_by_dev_and_name_or_num(const char *dev_iface,
792 const char *dev_part_str,
793 struct blk_desc **desc,
794 struct disk_partition *part_info,
795 int allow_whole_dev)
796 {
797 int ret;
798
799 /* Split the part_name if passed as "$dev_num#part_name". */
800 ret = part_get_info_by_dev_and_name(dev_iface, dev_part_str, desc,
801 part_info);
802 if (ret >= 0)
803 return ret;
804 /*
805 * Couldn't lookup by name, try looking up the partition description
806 * directly.
807 */
808 ret = blk_get_device_part_str(dev_iface, dev_part_str, desc, part_info,
809 allow_whole_dev);
810 if (ret < 0)
811 printf("Couldn't find partition %s %s\n",
812 dev_iface, dev_part_str);
813 return ret;
814 }
815
part_set_generic_name(const struct blk_desc * desc,int part_num,char * name)816 void part_set_generic_name(const struct blk_desc *desc, int part_num,
817 char *name)
818 {
819 char *devtype;
820
821 switch (desc->uclass_id) {
822 case UCLASS_IDE:
823 case UCLASS_AHCI:
824 devtype = "hd";
825 break;
826 case UCLASS_SCSI:
827 devtype = "sd";
828 break;
829 case UCLASS_USB:
830 devtype = "usbd";
831 break;
832 case UCLASS_MMC:
833 devtype = "mmcsd";
834 break;
835 default:
836 devtype = "xx";
837 break;
838 }
839
840 sprintf(name, "%s%c%d", devtype, 'a' + desc->devnum, part_num);
841 }
842
part_get_bootable(struct blk_desc * desc)843 int part_get_bootable(struct blk_desc *desc)
844 {
845 struct disk_partition info;
846 int p;
847
848 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
849 int ret;
850
851 ret = part_get_info(desc, p, &info);
852 if (!ret && info.bootable)
853 return p;
854 }
855
856 return 0;
857 }
858