part: Add comments for static functions
[pandora-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                 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
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         desc->part_type = PART_TYPE_UNKNOWN;
288         for (entry = drv; entry != drv + n_ents; entry++) {
289                 int ret;
290
291                 ret = entry->test(desc);
292                 debug("%s: try '%s': ret=%d\n", __func__, entry->name, ret);
293                 if (!ret) {
294                         desc->part_type = entry->part_type;
295                         break;
296                 }
297         }
298 }
299
300 static void print_part_header(const char *type, struct blk_desc *desc)
301 {
302 #if CONFIG_IS_ENABLED(MAC_PARTITION) || \
303         CONFIG_IS_ENABLED(DOS_PARTITION) || \
304         CONFIG_IS_ENABLED(ISO_PARTITION) || \
305         CONFIG_IS_ENABLED(AMIGA_PARTITION) || \
306         CONFIG_IS_ENABLED(EFI_PARTITION)
307         puts ("\nPartition Map for ");
308         switch (desc->uclass_id) {
309         case UCLASS_IDE:
310                 puts ("IDE");
311                 break;
312         case UCLASS_AHCI:
313                 puts ("SATA");
314                 break;
315         case UCLASS_SCSI:
316                 puts ("SCSI");
317                 break;
318         case UCLASS_USB:
319                 puts ("USB");
320                 break;
321         case UCLASS_MMC:
322                 puts ("MMC");
323                 break;
324         case UCLASS_HOST:
325                 puts ("HOST");
326                 break;
327         case UCLASS_NVME:
328                 puts ("NVMe");
329                 break;
330         case UCLASS_PVBLOCK:
331                 puts("PV BLOCK");
332                 break;
333         case UCLASS_VIRTIO:
334                 puts("VirtIO");
335                 break;
336         case UCLASS_EFI_MEDIA:
337                 puts("EFI");
338                 break;
339         default:
340                 puts("UNKNOWN");
341                 break;
342         }
343         printf (" device %d  --   Partition Type: %s\n\n",
344                         desc->devnum, type);
345 #endif /* any CONFIG_..._PARTITION */
346 }
347
348 void part_print(struct blk_desc *desc)
349 {
350         struct part_driver *drv;
351
352         drv = part_driver_lookup_type(desc);
353         if (!drv) {
354                 printf("## Unknown partition table type %x\n",
355                        desc->part_type);
356                 return;
357         }
358
359         PRINTF("## Testing for valid %s partition ##\n", drv->name);
360         print_part_header(drv->name, desc);
361         if (drv->print)
362                 drv->print(desc);
363 }
364
365 int part_get_info_by_type(struct blk_desc *desc, int part, int part_type,
366                           struct disk_partition *info)
367 {
368         struct part_driver *drv;
369
370         if (blk_enabled()) {
371 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
372                 /* The common case is no UUID support */
373                 info->uuid[0] = 0;
374 #endif
375 #ifdef CONFIG_PARTITION_TYPE_GUID
376                 info->type_guid[0] = 0;
377 #endif
378
379                 if (part_type == PART_TYPE_UNKNOWN) {
380                         drv = part_driver_lookup_type(desc);
381                 } else {
382                         drv = part_driver_get_type(part_type);
383                 }
384
385                 if (!drv) {
386                         debug("## Unknown partition table type %x\n",
387                               desc->part_type);
388                         return -EPROTONOSUPPORT;
389                 }
390                 if (!drv->get_info) {
391                         PRINTF("## Driver %s does not have the get_info() method\n",
392                                drv->name);
393                         return -ENOSYS;
394                 }
395                 if (drv->get_info(desc, part, info) == 0) {
396                         PRINTF("## Valid %s partition found ##\n", drv->name);
397                         return 0;
398                 }
399         }
400
401         return -ENOENT;
402 }
403
404 int part_get_info(struct blk_desc *desc, int part,
405                   struct disk_partition *info)
406 {
407         return part_get_info_by_type(desc, part, PART_TYPE_UNKNOWN, info);
408 }
409
410 int part_get_info_whole_disk(struct blk_desc *desc,
411                              struct disk_partition *info)
412 {
413         info->start = 0;
414         info->size = desc->lba;
415         info->blksz = desc->blksz;
416         info->bootable = 0;
417         strcpy((char *)info->type, BOOT_PART_TYPE);
418         strcpy((char *)info->name, "Whole Disk");
419 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
420         info->uuid[0] = 0;
421 #endif
422 #ifdef CONFIG_PARTITION_TYPE_GUID
423         info->type_guid[0] = 0;
424 #endif
425
426         return 0;
427 }
428
429 int blk_get_device_by_str(const char *ifname, const char *dev_hwpart_str,
430                           struct blk_desc **desc)
431 {
432         char *ep;
433         char *dup_str = NULL;
434         const char *dev_str, *hwpart_str;
435         int dev, hwpart;
436
437         hwpart_str = strchr(dev_hwpart_str, '.');
438         if (hwpart_str) {
439                 dup_str = strdup(dev_hwpart_str);
440                 dup_str[hwpart_str - dev_hwpart_str] = 0;
441                 dev_str = dup_str;
442                 hwpart_str++;
443         } else {
444                 dev_str = dev_hwpart_str;
445                 hwpart = 0;
446         }
447
448         dev = hextoul(dev_str, &ep);
449         if (*ep) {
450                 printf("** Bad device specification %s %s **\n",
451                        ifname, dev_str);
452                 dev = -EINVAL;
453                 goto cleanup;
454         }
455
456         if (hwpart_str) {
457                 hwpart = hextoul(hwpart_str, &ep);
458                 if (*ep) {
459                         printf("** Bad HW partition specification %s %s **\n",
460                             ifname, hwpart_str);
461                         dev = -EINVAL;
462                         goto cleanup;
463                 }
464         }
465
466         *desc = get_dev_hwpart(ifname, dev, hwpart);
467         if (!(*desc) || ((*desc)->type == DEV_TYPE_UNKNOWN)) {
468                 debug("** Bad device %s %s **\n", ifname, dev_hwpart_str);
469                 dev = -ENODEV;
470                 goto cleanup;
471         }
472
473         if (blk_enabled()) {
474                 /*
475                  * Updates the partition table for the specified hw partition.
476                  * Always should be done, otherwise hw partition 0 will return
477                  * stale data after displaying a non-zero hw partition.
478                  */
479                 if ((*desc)->uclass_id == UCLASS_MMC)
480                         part_init(*desc);
481         }
482
483 cleanup:
484         free(dup_str);
485         return dev;
486 }
487
488 #define PART_UNSPECIFIED -2
489 #define PART_AUTO -1
490 int blk_get_device_part_str(const char *ifname, const char *dev_part_str,
491                              struct blk_desc **desc,
492                              struct disk_partition *info, int allow_whole_dev)
493 {
494         int ret;
495         const char *part_str;
496         char *dup_str = NULL;
497         const char *dev_str;
498         int dev;
499         char *ep;
500         int p;
501         int part;
502         struct disk_partition tmpinfo;
503
504         *desc = NULL;
505         memset(info, 0, sizeof(*info));
506
507 #if IS_ENABLED(CONFIG_SANDBOX) || IS_ENABLED(CONFIG_SEMIHOSTING)
508         /*
509          * Special-case a pseudo block device "hostfs", to allow access to the
510          * host's own filesystem.
511          */
512         if (!strcmp(ifname, "hostfs")) {
513                 strcpy((char *)info->type, BOOT_PART_TYPE);
514                 strcpy((char *)info->name, "Host filesystem");
515
516                 return 0;
517         }
518 #endif
519
520 #if IS_ENABLED(CONFIG_CMD_UBIFS) && !IS_ENABLED(CONFIG_SPL_BUILD)
521         /*
522          * Special-case ubi, ubi goes through a mtd, rather than through
523          * a regular block device.
524          */
525         if (!strcmp(ifname, "ubi")) {
526                 if (!ubifs_is_mounted()) {
527                         printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
528                         return -EINVAL;
529                 }
530
531                 strcpy((char *)info->type, BOOT_PART_TYPE);
532                 strcpy((char *)info->name, "UBI");
533                 return 0;
534         }
535 #endif
536
537         /* If no dev_part_str, use bootdevice environment variable */
538         if (CONFIG_IS_ENABLED(ENV_SUPPORT)) {
539                 if (!dev_part_str || !strlen(dev_part_str) ||
540                     !strcmp(dev_part_str, "-"))
541                         dev_part_str = env_get("bootdevice");
542         }
543
544         /* If still no dev_part_str, it's an error */
545         if (!dev_part_str) {
546                 printf("** No device specified **\n");
547                 ret = -ENODEV;
548                 goto cleanup;
549         }
550
551         /* Separate device and partition ID specification */
552         part_str = strchr(dev_part_str, ':');
553         if (part_str) {
554                 dup_str = strdup(dev_part_str);
555                 dup_str[part_str - dev_part_str] = 0;
556                 dev_str = dup_str;
557                 part_str++;
558         } else {
559                 dev_str = dev_part_str;
560         }
561
562         /* Look up the device */
563         dev = blk_get_device_by_str(ifname, dev_str, desc);
564         if (dev < 0) {
565                 printf("** Bad device specification %s %s **\n",
566                        ifname, dev_str);
567                 ret = dev;
568                 goto cleanup;
569         }
570
571         /* Convert partition ID string to number */
572         if (!part_str || !*part_str) {
573                 part = PART_UNSPECIFIED;
574         } else if (!strcmp(part_str, "auto")) {
575                 part = PART_AUTO;
576         } else {
577                 /* Something specified -> use exactly that */
578                 part = (int)hextoul(part_str, &ep);
579                 /*
580                  * Less than whole string converted,
581                  * or request for whole device, but caller requires partition.
582                  */
583                 if (*ep || (part == 0 && !allow_whole_dev)) {
584                         printf("** Bad partition specification %s %s **\n",
585                             ifname, dev_part_str);
586                         ret = -ENOENT;
587                         goto cleanup;
588                 }
589         }
590
591         /*
592          * No partition table on device,
593          * or user requested partition 0 (entire device).
594          */
595         if (((*desc)->part_type == PART_TYPE_UNKNOWN) || !part) {
596                 if (!(*desc)->lba) {
597                         printf("** Bad device size - %s %s **\n", ifname,
598                                dev_str);
599                         ret = -EINVAL;
600                         goto cleanup;
601                 }
602
603                 /*
604                  * If user specified a partition ID other than 0,
605                  * or the calling command only accepts partitions,
606                  * it's an error.
607                  */
608                 if ((part > 0) || (!allow_whole_dev)) {
609                         printf("** No partition table - %s %s **\n", ifname,
610                                dev_str);
611                         ret = -EPROTONOSUPPORT;
612                         goto cleanup;
613                 }
614
615                 (*desc)->log2blksz = LOG2((*desc)->blksz);
616
617                 part_get_info_whole_disk(*desc, info);
618
619                 ret = 0;
620                 goto cleanup;
621         }
622
623         /*
624          * Now there's known to be a partition table,
625          * not specifying a partition means to pick partition 1.
626          */
627         if (part == PART_UNSPECIFIED)
628                 part = 1;
629
630         /*
631          * If user didn't specify a partition number, or did specify something
632          * other than "auto", use that partition number directly.
633          */
634         if (part != PART_AUTO) {
635                 ret = part_get_info(*desc, part, info);
636                 if (ret) {
637                         printf("** Invalid partition %d **\n", part);
638                         goto cleanup;
639                 }
640         } else {
641                 /*
642                  * Find the first bootable partition.
643                  * If none are bootable, fall back to the first valid partition.
644                  */
645                 part = 0;
646                 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
647                         ret = part_get_info(*desc, p, info);
648                         if (ret)
649                                 continue;
650
651                         /*
652                          * First valid partition, or new better partition?
653                          * If so, save partition ID.
654                          */
655                         if (!part || info->bootable)
656                                 part = p;
657
658                         /* Best possible partition? Stop searching. */
659                         if (info->bootable)
660                                 break;
661
662                         /*
663                          * We now need to search further for best possible.
664                          * If we what we just queried was the best so far,
665                          * save the info since we over-write it next loop.
666                          */
667                         if (part == p)
668                                 tmpinfo = *info;
669                 }
670                 /* If we found any acceptable partition */
671                 if (part) {
672                         /*
673                          * If we searched all possible partition IDs,
674                          * return the first valid partition we found.
675                          */
676                         if (p == MAX_SEARCH_PARTITIONS + 1)
677                                 *info = tmpinfo;
678                 } else {
679                         printf("** No valid partitions found **\n");
680                         goto cleanup;
681                 }
682         }
683         if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
684                 printf("** Invalid partition type \"%.32s\""
685                         " (expect \"" BOOT_PART_TYPE "\")\n",
686                         info->type);
687                 ret  = -EINVAL;
688                 goto cleanup;
689         }
690
691         (*desc)->log2blksz = LOG2((*desc)->blksz);
692
693         ret = part;
694         goto cleanup;
695
696 cleanup:
697         free(dup_str);
698         return ret;
699 }
700
701 int part_get_info_by_name(struct blk_desc *desc, const char *name,
702                           struct disk_partition *info)
703 {
704         struct part_driver *part_drv;
705         int ret;
706         int i;
707
708         part_drv = part_driver_lookup_type(desc);
709         if (!part_drv)
710                 return -1;
711
712         if (!part_drv->get_info) {
713                 log_debug("## Driver %s does not have the get_info() method\n",
714                           part_drv->name);
715                 return -ENOSYS;
716         }
717
718         for (i = 1; i < part_drv->max_entries; i++) {
719                 ret = part_drv->get_info(desc, i, info);
720                 if (ret != 0) {
721                         /* no more entries in table */
722                         break;
723                 }
724                 if (strcmp(name, (const char *)info->name) == 0) {
725                         /* matched */
726                         return i;
727                 }
728         }
729
730         return -ENOENT;
731 }
732
733 /**
734  * Get partition info from device number and partition name.
735  *
736  * Parse a device number and partition name string in the form of
737  * "devicenum.hwpartnum#partition_name", for example "0.1#misc". devicenum and
738  * hwpartnum are both optional, defaulting to 0. If the partition is found,
739  * sets desc and part_info accordingly with the information of the
740  * partition with the given partition_name.
741  *
742  * @param[in] dev_iface Device interface
743  * @param[in] dev_part_str Input string argument, like "0.1#misc"
744  * @param[out] desc Place to store the device description pointer
745  * @param[out] part_info Place to store the partition information
746  * Return: 0 on success, or a negative on error
747  */
748 static int part_get_info_by_dev_and_name(const char *dev_iface,
749                                          const char *dev_part_str,
750                                          struct blk_desc **desc,
751                                          struct disk_partition *part_info)
752 {
753         char *dup_str = NULL;
754         const char *dev_str, *part_str;
755         int ret;
756
757         /* Separate device and partition name specification */
758         if (dev_part_str)
759                 part_str = strchr(dev_part_str, '#');
760         else
761                 part_str = NULL;
762
763         if (part_str) {
764                 dup_str = strdup(dev_part_str);
765                 dup_str[part_str - dev_part_str] = 0;
766                 dev_str = dup_str;
767                 part_str++;
768         } else {
769                 return -EINVAL;
770         }
771
772         ret = blk_get_device_by_str(dev_iface, dev_str, desc);
773         if (ret < 0)
774                 goto cleanup;
775
776         ret = part_get_info_by_name(*desc, part_str, part_info);
777         if (ret < 0)
778                 printf("Could not find \"%s\" partition\n", part_str);
779
780 cleanup:
781         free(dup_str);
782         return ret;
783 }
784
785 int part_get_info_by_dev_and_name_or_num(const char *dev_iface,
786                                          const char *dev_part_str,
787                                          struct blk_desc **desc,
788                                          struct disk_partition *part_info,
789                                          int allow_whole_dev)
790 {
791         int ret;
792
793         /* Split the part_name if passed as "$dev_num#part_name". */
794         ret = part_get_info_by_dev_and_name(dev_iface, dev_part_str, desc,
795                                             part_info);
796         if (ret >= 0)
797                 return ret;
798         /*
799          * Couldn't lookup by name, try looking up the partition description
800          * directly.
801          */
802         ret = blk_get_device_part_str(dev_iface, dev_part_str, desc, part_info,
803                                       allow_whole_dev);
804         if (ret < 0)
805                 printf("Couldn't find partition %s %s\n",
806                        dev_iface, dev_part_str);
807         return ret;
808 }
809
810 void part_set_generic_name(const struct blk_desc *desc, int part_num,
811                            char *name)
812 {
813         char *devtype;
814
815         switch (desc->uclass_id) {
816         case UCLASS_IDE:
817         case UCLASS_AHCI:
818                 devtype = "hd";
819                 break;
820         case UCLASS_SCSI:
821                 devtype = "sd";
822                 break;
823         case UCLASS_USB:
824                 devtype = "usbd";
825                 break;
826         case UCLASS_MMC:
827                 devtype = "mmcsd";
828                 break;
829         default:
830                 devtype = "xx";
831                 break;
832         }
833
834         sprintf(name, "%s%c%d", devtype, 'a' + desc->devnum, part_num);
835 }
836
837 int part_get_bootable(struct blk_desc *desc)
838 {
839         struct disk_partition info;
840         int p;
841
842         for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
843                 int ret;
844
845                 ret = part_get_info(desc, p, &info);
846                 if (!ret && info.bootable)
847                         return p;
848         }
849
850         return 0;
851 }