1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
14 #include <ubifs_uboot.h>
19 #define PRINTF(fmt,args...) printf (fmt ,##args)
21 #define PRINTF(fmt,args...)
24 /* Check all partition types */
25 #define PART_TYPE_ALL -1
27 static struct part_driver *part_driver_lookup_type(struct blk_desc *dev_desc)
29 struct part_driver *drv =
30 ll_entry_start(struct part_driver, part_driver);
31 const int n_ents = ll_entry_count(struct part_driver, part_driver);
32 struct part_driver *entry;
34 if (dev_desc->part_type == PART_TYPE_UNKNOWN) {
35 for (entry = drv; entry != drv + n_ents; entry++) {
38 ret = entry->test(dev_desc);
40 dev_desc->part_type = entry->part_type;
45 for (entry = drv; entry != drv + n_ents; entry++) {
46 if (dev_desc->part_type == entry->part_type)
55 #ifdef CONFIG_HAVE_BLOCK_DEVICE
56 static struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
58 struct blk_desc *dev_desc;
61 dev_desc = blk_get_devnum_by_typename(ifname, dev);
63 debug("%s: No device for iface '%s', dev %d\n", __func__,
67 ret = blk_dselect_hwpart(dev_desc, hwpart);
69 debug("%s: Failed to select h/w partition: err-%d\n", __func__,
77 struct blk_desc *blk_get_dev(const char *ifname, int dev)
79 return get_dev_hwpart(ifname, dev, 0);
82 struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
87 struct blk_desc *blk_get_dev(const char *ifname, int dev)
93 #ifdef CONFIG_HAVE_BLOCK_DEVICE
95 /* ------------------------------------------------------------------------- */
97 * reports device info to the user
101 typedef uint64_t lba512_t;
103 typedef lbaint_t lba512_t;
107 * Overflowless variant of (block_count * mul_by / 2**right_shift)
108 * when 2**right_shift > mul_by
110 static lba512_t lba512_muldiv(lba512_t block_count, lba512_t mul_by,
113 lba512_t bc_quot, bc_rem;
115 /* x * m / d == x / d * m + (x % d) * m / d */
116 bc_quot = block_count >> right_shift;
117 bc_rem = block_count - (bc_quot << right_shift);
118 return bc_quot * mul_by + ((bc_rem * mul_by) >> right_shift);
121 void dev_print (struct blk_desc *dev_desc)
123 lba512_t lba512; /* number of blocks if 512bytes block size */
125 if (dev_desc->type == DEV_TYPE_UNKNOWN) {
126 puts ("not available\n");
130 switch (dev_desc->if_type) {
132 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
133 dev_desc->target,dev_desc->lun,
141 printf ("Model: %s Firm: %s Ser#: %s\n",
150 printf ("Vendor: %s Rev: %s Prod: %s\n",
156 printf("%s VirtIO Block Device\n", dev_desc->vendor);
159 puts("device type DOC\n");
161 case IF_TYPE_UNKNOWN:
162 puts("device type unknown\n");
165 printf("Unhandled device type: %i\n", dev_desc->if_type);
169 if (dev_desc->removable)
171 switch (dev_desc->type & 0x1F) {
172 case DEV_TYPE_HARDDISK:
178 case DEV_TYPE_OPDISK:
179 puts ("Optical Device");
185 printf ("# %02X #", dev_desc->type & 0x1F);
189 if (dev_desc->lba > 0L && dev_desc->blksz > 0L) {
190 ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
195 lba512 = (lba * (dev_desc->blksz/512));
196 /* round to 1 digit */
197 /* 2048 = (1024 * 1024) / 512 MB */
198 mb = lba512_muldiv(lba512, 10, 11);
201 mb_rem = mb - (10 * mb_quot);
205 gb_rem = gb - (10 * gb_quot);
208 printf (" Supports 48-bit addressing\n");
210 #if defined(CONFIG_SYS_64BIT_LBA)
211 printf (" Capacity: %lu.%lu MB = %lu.%lu GB (%llu x %lu)\n",
217 printf (" Capacity: %lu.%lu MB = %lu.%lu GB (%lu x %lu)\n",
224 puts (" Capacity: not available\n");
229 #ifdef CONFIG_HAVE_BLOCK_DEVICE
231 void part_init(struct blk_desc *dev_desc)
233 struct part_driver *drv =
234 ll_entry_start(struct part_driver, part_driver);
235 const int n_ents = ll_entry_count(struct part_driver, part_driver);
236 struct part_driver *entry;
238 blkcache_invalidate(dev_desc->if_type, dev_desc->devnum);
240 dev_desc->part_type = PART_TYPE_UNKNOWN;
241 for (entry = drv; entry != drv + n_ents; entry++) {
244 ret = entry->test(dev_desc);
245 debug("%s: try '%s': ret=%d\n", __func__, entry->name, ret);
247 dev_desc->part_type = entry->part_type;
253 static void print_part_header(const char *type, struct blk_desc *dev_desc)
255 #if CONFIG_IS_ENABLED(MAC_PARTITION) || \
256 CONFIG_IS_ENABLED(DOS_PARTITION) || \
257 CONFIG_IS_ENABLED(ISO_PARTITION) || \
258 CONFIG_IS_ENABLED(AMIGA_PARTITION) || \
259 CONFIG_IS_ENABLED(EFI_PARTITION)
260 puts ("\nPartition Map for ");
261 switch (dev_desc->if_type) {
296 printf (" device %d -- Partition Type: %s\n\n",
297 dev_desc->devnum, type);
298 #endif /* any CONFIG_..._PARTITION */
301 void part_print(struct blk_desc *dev_desc)
303 struct part_driver *drv;
305 drv = part_driver_lookup_type(dev_desc);
307 printf("## Unknown partition table type %x\n",
308 dev_desc->part_type);
312 PRINTF("## Testing for valid %s partition ##\n", drv->name);
313 print_part_header(drv->name, dev_desc);
315 drv->print(dev_desc);
318 #endif /* CONFIG_HAVE_BLOCK_DEVICE */
320 int part_get_info(struct blk_desc *dev_desc, int part,
321 struct disk_partition *info)
323 #ifdef CONFIG_HAVE_BLOCK_DEVICE
324 struct part_driver *drv;
326 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
327 /* The common case is no UUID support */
330 #ifdef CONFIG_PARTITION_TYPE_GUID
331 info->type_guid[0] = 0;
334 drv = part_driver_lookup_type(dev_desc);
336 debug("## Unknown partition table type %x\n",
337 dev_desc->part_type);
338 return -EPROTONOSUPPORT;
340 if (!drv->get_info) {
341 PRINTF("## Driver %s does not have the get_info() method\n",
345 if (drv->get_info(dev_desc, part, info) == 0) {
346 PRINTF("## Valid %s partition found ##\n", drv->name);
349 #endif /* CONFIG_HAVE_BLOCK_DEVICE */
354 int part_get_info_whole_disk(struct blk_desc *dev_desc,
355 struct disk_partition *info)
358 info->size = dev_desc->lba;
359 info->blksz = dev_desc->blksz;
361 strcpy((char *)info->type, BOOT_PART_TYPE);
362 strcpy((char *)info->name, "Whole Disk");
363 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
366 #ifdef CONFIG_PARTITION_TYPE_GUID
367 info->type_guid[0] = 0;
373 int blk_get_device_by_str(const char *ifname, const char *dev_hwpart_str,
374 struct blk_desc **dev_desc)
377 char *dup_str = NULL;
378 const char *dev_str, *hwpart_str;
381 hwpart_str = strchr(dev_hwpart_str, '.');
383 dup_str = strdup(dev_hwpart_str);
384 dup_str[hwpart_str - dev_hwpart_str] = 0;
388 dev_str = dev_hwpart_str;
392 dev = simple_strtoul(dev_str, &ep, 16);
394 printf("** Bad device specification %s %s **\n",
401 hwpart = simple_strtoul(hwpart_str, &ep, 16);
403 printf("** Bad HW partition specification %s %s **\n",
410 *dev_desc = get_dev_hwpart(ifname, dev, hwpart);
411 if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
412 debug("** Bad device %s %s **\n", ifname, dev_hwpart_str);
417 #ifdef CONFIG_HAVE_BLOCK_DEVICE
419 * Updates the partition table for the specified hw partition.
420 * Always should be done, otherwise hw partition 0 will return stale
421 * data after displaying a non-zero hw partition.
423 part_init(*dev_desc);
431 #define PART_UNSPECIFIED -2
433 int blk_get_device_part_str(const char *ifname, const char *dev_part_str,
434 struct blk_desc **dev_desc,
435 struct disk_partition *info, int allow_whole_dev)
438 const char *part_str;
439 char *dup_str = NULL;
445 struct disk_partition tmpinfo;
447 #ifdef CONFIG_SANDBOX
449 * Special-case a pseudo block device "hostfs", to allow access to the
450 * host's own filesystem.
452 if (0 == strcmp(ifname, "hostfs")) {
458 strcpy((char *)info->type, BOOT_PART_TYPE);
459 strcpy((char *)info->name, "Sandbox host");
460 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
463 #ifdef CONFIG_PARTITION_TYPE_GUID
464 info->type_guid[0] = 0;
471 #ifdef CONFIG_CMD_UBIFS
473 * Special-case ubi, ubi goes through a mtd, rather than through
474 * a regular block device.
476 if (0 == strcmp(ifname, "ubi")) {
477 if (!ubifs_is_mounted()) {
478 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
483 memset(info, 0, sizeof(*info));
484 strcpy((char *)info->type, BOOT_PART_TYPE);
485 strcpy((char *)info->name, "UBI");
486 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
493 /* If no dev_part_str, use bootdevice environment variable */
494 if (!dev_part_str || !strlen(dev_part_str) ||
495 !strcmp(dev_part_str, "-"))
496 dev_part_str = env_get("bootdevice");
498 /* If still no dev_part_str, it's an error */
500 printf("** No device specified **\n");
504 /* Separate device and partition ID specification */
505 part_str = strchr(dev_part_str, ':');
507 dup_str = strdup(dev_part_str);
508 dup_str[part_str - dev_part_str] = 0;
512 dev_str = dev_part_str;
515 /* Look up the device */
516 dev = blk_get_device_by_str(ifname, dev_str, dev_desc);
520 /* Convert partition ID string to number */
521 if (!part_str || !*part_str) {
522 part = PART_UNSPECIFIED;
523 } else if (!strcmp(part_str, "auto")) {
526 /* Something specified -> use exactly that */
527 part = (int)simple_strtoul(part_str, &ep, 16);
529 * Less than whole string converted,
530 * or request for whole device, but caller requires partition.
532 if (*ep || (part == 0 && !allow_whole_dev)) {
533 printf("** Bad partition specification %s %s **\n",
534 ifname, dev_part_str);
540 * No partition table on device,
541 * or user requested partition 0 (entire device).
543 if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
545 if (!(*dev_desc)->lba) {
546 printf("** Bad device size - %s %s **\n", ifname,
552 * If user specified a partition ID other than 0,
553 * or the calling command only accepts partitions,
556 if ((part > 0) || (!allow_whole_dev)) {
557 printf("** No partition table - %s %s **\n", ifname,
562 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
564 part_get_info_whole_disk(*dev_desc, info);
571 * Now there's known to be a partition table,
572 * not specifying a partition means to pick partition 1.
574 if (part == PART_UNSPECIFIED)
578 * If user didn't specify a partition number, or did specify something
579 * other than "auto", use that partition number directly.
581 if (part != PART_AUTO) {
582 ret = part_get_info(*dev_desc, part, info);
584 printf("** Invalid partition %d **\n", part);
589 * Find the first bootable partition.
590 * If none are bootable, fall back to the first valid partition.
593 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
594 ret = part_get_info(*dev_desc, p, info);
599 * First valid partition, or new better partition?
600 * If so, save partition ID.
602 if (!part || info->bootable)
605 /* Best possible partition? Stop searching. */
610 * We now need to search further for best possible.
611 * If we what we just queried was the best so far,
612 * save the info since we over-write it next loop.
617 /* If we found any acceptable partition */
620 * If we searched all possible partition IDs,
621 * return the first valid partition we found.
623 if (p == MAX_SEARCH_PARTITIONS + 1)
626 printf("** No valid partitions found **\n");
631 if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
632 printf("** Invalid partition type \"%.32s\""
633 " (expect \"" BOOT_PART_TYPE "\")\n",
639 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
649 int part_get_info_by_name_type(struct blk_desc *dev_desc, const char *name,
650 struct disk_partition *info, int part_type)
652 struct part_driver *part_drv;
656 part_drv = part_driver_lookup_type(dev_desc);
659 for (i = 1; i < part_drv->max_entries; i++) {
660 ret = part_drv->get_info(dev_desc, i, info);
662 /* no more entries in table */
665 if (strcmp(name, (const char *)info->name) == 0) {
674 int part_get_info_by_name(struct blk_desc *dev_desc, const char *name,
675 struct disk_partition *info)
677 return part_get_info_by_name_type(dev_desc, name, info, PART_TYPE_ALL);
681 * Get partition info from device number and partition name.
683 * Parse a device number and partition name string in the form of
684 * "device_num#partition_name", for example "0#misc". If the partition
685 * is found, sets dev_desc and part_info accordingly with the information
686 * of the partition with the given partition_name.
688 * @param[in] dev_iface Device interface
689 * @param[in] dev_part_str Input string argument, like "0#misc"
690 * @param[out] dev_desc Place to store the device description pointer
691 * @param[out] part_info Place to store the partition information
692 * @return 0 on success, or a negative on error
694 static int part_get_info_by_dev_and_name(const char *dev_iface,
695 const char *dev_part_str,
696 struct blk_desc **dev_desc,
697 struct disk_partition *part_info)
700 const char *part_str;
703 part_str = strchr(dev_part_str, '#');
704 if (!part_str || part_str == dev_part_str)
707 dev_num = simple_strtoul(dev_part_str, &ep, 16);
708 if (ep != part_str) {
709 /* Not all the first part before the # was parsed. */
714 *dev_desc = blk_get_dev(dev_iface, dev_num);
716 printf("Could not find %s %d\n", dev_iface, dev_num);
719 if (part_get_info_by_name(*dev_desc, part_str, part_info) < 0) {
720 printf("Could not find \"%s\" partition\n", part_str);
726 int part_get_info_by_dev_and_name_or_num(const char *dev_iface,
727 const char *dev_part_str,
728 struct blk_desc **dev_desc,
729 struct disk_partition *part_info)
731 /* Split the part_name if passed as "$dev_num#part_name". */
732 if (!part_get_info_by_dev_and_name(dev_iface, dev_part_str,
733 dev_desc, part_info))
736 * Couldn't lookup by name, try looking up the partition description
739 if (blk_get_device_part_str(dev_iface, dev_part_str,
740 dev_desc, part_info, 1) < 0) {
741 printf("Couldn't find partition %s %s\n",
742 dev_iface, dev_part_str);
748 void part_set_generic_name(const struct blk_desc *dev_desc,
749 int part_num, char *name)
753 switch (dev_desc->if_type) {
777 sprintf(name, "%s%c%d", devtype, 'a' + dev_desc->devnum, part_num);