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 disk_partition_t *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, disk_partition_t *info)
357 info->size = dev_desc->lba;
358 info->blksz = dev_desc->blksz;
360 strcpy((char *)info->type, BOOT_PART_TYPE);
361 strcpy((char *)info->name, "Whole Disk");
362 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
365 #ifdef CONFIG_PARTITION_TYPE_GUID
366 info->type_guid[0] = 0;
372 int blk_get_device_by_str(const char *ifname, const char *dev_hwpart_str,
373 struct blk_desc **dev_desc)
376 char *dup_str = NULL;
377 const char *dev_str, *hwpart_str;
380 hwpart_str = strchr(dev_hwpart_str, '.');
382 dup_str = strdup(dev_hwpart_str);
383 dup_str[hwpart_str - dev_hwpart_str] = 0;
387 dev_str = dev_hwpart_str;
391 dev = simple_strtoul(dev_str, &ep, 16);
393 printf("** Bad device specification %s %s **\n",
400 hwpart = simple_strtoul(hwpart_str, &ep, 16);
402 printf("** Bad HW partition specification %s %s **\n",
409 *dev_desc = get_dev_hwpart(ifname, dev, hwpart);
410 if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
411 debug("** Bad device %s %s **\n", ifname, dev_hwpart_str);
416 #ifdef CONFIG_HAVE_BLOCK_DEVICE
418 * Updates the partition table for the specified hw partition.
419 * Always should be done, otherwise hw partition 0 will return stale
420 * data after displaying a non-zero hw partition.
422 part_init(*dev_desc);
430 #define PART_UNSPECIFIED -2
432 int blk_get_device_part_str(const char *ifname, const char *dev_part_str,
433 struct blk_desc **dev_desc,
434 disk_partition_t *info, int allow_whole_dev)
437 const char *part_str;
438 char *dup_str = NULL;
444 disk_partition_t tmpinfo;
446 #ifdef CONFIG_SANDBOX
448 * Special-case a pseudo block device "hostfs", to allow access to the
449 * host's own filesystem.
451 if (0 == strcmp(ifname, "hostfs")) {
457 strcpy((char *)info->type, BOOT_PART_TYPE);
458 strcpy((char *)info->name, "Sandbox host");
459 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
462 #ifdef CONFIG_PARTITION_TYPE_GUID
463 info->type_guid[0] = 0;
470 #ifdef CONFIG_CMD_UBIFS
472 * Special-case ubi, ubi goes through a mtd, rather than through
473 * a regular block device.
475 if (0 == strcmp(ifname, "ubi")) {
476 if (!ubifs_is_mounted()) {
477 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
482 memset(info, 0, sizeof(*info));
483 strcpy((char *)info->type, BOOT_PART_TYPE);
484 strcpy((char *)info->name, "UBI");
485 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
492 /* If no dev_part_str, use bootdevice environment variable */
493 if (!dev_part_str || !strlen(dev_part_str) ||
494 !strcmp(dev_part_str, "-"))
495 dev_part_str = env_get("bootdevice");
497 /* If still no dev_part_str, it's an error */
499 printf("** No device specified **\n");
503 /* Separate device and partition ID specification */
504 part_str = strchr(dev_part_str, ':');
506 dup_str = strdup(dev_part_str);
507 dup_str[part_str - dev_part_str] = 0;
511 dev_str = dev_part_str;
514 /* Look up the device */
515 dev = blk_get_device_by_str(ifname, dev_str, dev_desc);
519 /* Convert partition ID string to number */
520 if (!part_str || !*part_str) {
521 part = PART_UNSPECIFIED;
522 } else if (!strcmp(part_str, "auto")) {
525 /* Something specified -> use exactly that */
526 part = (int)simple_strtoul(part_str, &ep, 16);
528 * Less than whole string converted,
529 * or request for whole device, but caller requires partition.
531 if (*ep || (part == 0 && !allow_whole_dev)) {
532 printf("** Bad partition specification %s %s **\n",
533 ifname, dev_part_str);
539 * No partition table on device,
540 * or user requested partition 0 (entire device).
542 if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
544 if (!(*dev_desc)->lba) {
545 printf("** Bad device size - %s %s **\n", ifname,
551 * If user specified a partition ID other than 0,
552 * or the calling command only accepts partitions,
555 if ((part > 0) || (!allow_whole_dev)) {
556 printf("** No partition table - %s %s **\n", ifname,
561 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
563 part_get_info_whole_disk(*dev_desc, info);
570 * Now there's known to be a partition table,
571 * not specifying a partition means to pick partition 1.
573 if (part == PART_UNSPECIFIED)
577 * If user didn't specify a partition number, or did specify something
578 * other than "auto", use that partition number directly.
580 if (part != PART_AUTO) {
581 ret = part_get_info(*dev_desc, part, info);
583 printf("** Invalid partition %d **\n", part);
588 * Find the first bootable partition.
589 * If none are bootable, fall back to the first valid partition.
592 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
593 ret = part_get_info(*dev_desc, p, info);
598 * First valid partition, or new better partition?
599 * If so, save partition ID.
601 if (!part || info->bootable)
604 /* Best possible partition? Stop searching. */
609 * We now need to search further for best possible.
610 * If we what we just queried was the best so far,
611 * save the info since we over-write it next loop.
616 /* If we found any acceptable partition */
619 * If we searched all possible partition IDs,
620 * return the first valid partition we found.
622 if (p == MAX_SEARCH_PARTITIONS + 1)
625 printf("** No valid partitions found **\n");
630 if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
631 printf("** Invalid partition type \"%.32s\""
632 " (expect \"" BOOT_PART_TYPE "\")\n",
638 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
648 int part_get_info_by_name_type(struct blk_desc *dev_desc, const char *name,
649 disk_partition_t *info, int part_type)
651 struct part_driver *part_drv;
655 part_drv = part_driver_lookup_type(dev_desc);
658 for (i = 1; i < part_drv->max_entries; i++) {
659 ret = part_drv->get_info(dev_desc, i, info);
661 /* no more entries in table */
664 if (strcmp(name, (const char *)info->name) == 0) {
673 int part_get_info_by_name(struct blk_desc *dev_desc, const char *name,
674 disk_partition_t *info)
676 return part_get_info_by_name_type(dev_desc, name, info, PART_TYPE_ALL);
680 * Get partition info from device number and partition name.
682 * Parse a device number and partition name string in the form of
683 * "device_num#partition_name", for example "0#misc". If the partition
684 * is found, sets dev_desc and part_info accordingly with the information
685 * of the partition with the given partition_name.
687 * @param[in] dev_iface Device interface
688 * @param[in] dev_part_str Input string argument, like "0#misc"
689 * @param[out] dev_desc Place to store the device description pointer
690 * @param[out] part_info Place to store the partition information
691 * @return 0 on success, or a negative on error
693 static int part_get_info_by_dev_and_name(const char *dev_iface,
694 const char *dev_part_str,
695 struct blk_desc **dev_desc,
696 disk_partition_t *part_info)
699 const char *part_str;
702 part_str = strchr(dev_part_str, '#');
703 if (!part_str || part_str == dev_part_str)
706 dev_num = simple_strtoul(dev_part_str, &ep, 16);
707 if (ep != part_str) {
708 /* Not all the first part before the # was parsed. */
713 *dev_desc = blk_get_dev(dev_iface, dev_num);
715 printf("Could not find %s %d\n", dev_iface, dev_num);
718 if (part_get_info_by_name(*dev_desc, part_str, part_info) < 0) {
719 printf("Could not find \"%s\" partition\n", part_str);
725 int part_get_info_by_dev_and_name_or_num(const char *dev_iface,
726 const char *dev_part_str,
727 struct blk_desc **dev_desc,
728 disk_partition_t *part_info)
730 /* Split the part_name if passed as "$dev_num#part_name". */
731 if (!part_get_info_by_dev_and_name(dev_iface, dev_part_str,
732 dev_desc, part_info))
735 * Couldn't lookup by name, try looking up the partition description
738 if (blk_get_device_part_str(dev_iface, dev_part_str,
739 dev_desc, part_info, 1) < 0) {
740 printf("Couldn't find partition %s %s\n",
741 dev_iface, dev_part_str);
747 void part_set_generic_name(const struct blk_desc *dev_desc,
748 int part_num, char *name)
752 switch (dev_desc->if_type) {
776 sprintf(name, "%s%c%d", devtype, 'a' + dev_desc->devnum, part_num);