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