1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
15 #include <ubifs_uboot.h>
20 #define PRINTF(fmt,args...) printf (fmt ,##args)
22 #define PRINTF(fmt,args...)
25 /* Check all partition types */
26 #define PART_TYPE_ALL -1
28 static struct part_driver *part_driver_lookup_type(struct blk_desc *dev_desc)
30 struct part_driver *drv =
31 ll_entry_start(struct part_driver, part_driver);
32 const int n_ents = ll_entry_count(struct part_driver, part_driver);
33 struct part_driver *entry;
35 if (dev_desc->part_type == PART_TYPE_UNKNOWN) {
36 for (entry = drv; entry != drv + n_ents; entry++) {
39 ret = entry->test(dev_desc);
41 dev_desc->part_type = entry->part_type;
46 for (entry = drv; entry != drv + n_ents; entry++) {
47 if (dev_desc->part_type == entry->part_type)
56 #ifdef CONFIG_HAVE_BLOCK_DEVICE
57 static struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
59 struct blk_desc *dev_desc;
62 dev_desc = blk_get_devnum_by_typename(ifname, dev);
64 debug("%s: No device for iface '%s', dev %d\n", __func__,
68 ret = blk_dselect_hwpart(dev_desc, hwpart);
70 debug("%s: Failed to select h/w partition: err-%d\n", __func__,
78 struct blk_desc *blk_get_dev(const char *ifname, int dev)
80 return get_dev_hwpart(ifname, dev, 0);
83 struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
88 struct blk_desc *blk_get_dev(const char *ifname, int dev)
94 #ifdef CONFIG_HAVE_BLOCK_DEVICE
96 /* ------------------------------------------------------------------------- */
98 * reports device info to the user
102 typedef uint64_t lba512_t;
104 typedef lbaint_t lba512_t;
108 * Overflowless variant of (block_count * mul_by / 2**right_shift)
109 * when 2**right_shift > mul_by
111 static lba512_t lba512_muldiv(lba512_t block_count, lba512_t mul_by,
114 lba512_t bc_quot, bc_rem;
116 /* x * m / d == x / d * m + (x % d) * m / d */
117 bc_quot = block_count >> right_shift;
118 bc_rem = block_count - (bc_quot << right_shift);
119 return bc_quot * mul_by + ((bc_rem * mul_by) >> right_shift);
122 void dev_print (struct blk_desc *dev_desc)
124 lba512_t lba512; /* number of blocks if 512bytes block size */
126 if (dev_desc->type == DEV_TYPE_UNKNOWN) {
127 puts ("not available\n");
131 switch (dev_desc->if_type) {
133 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
134 dev_desc->target,dev_desc->lun,
142 printf ("Model: %s Firm: %s Ser#: %s\n",
151 printf ("Vendor: %s Rev: %s Prod: %s\n",
157 printf("%s VirtIO Block Device\n", dev_desc->vendor);
160 puts("device type DOC\n");
162 case IF_TYPE_UNKNOWN:
163 puts("device type unknown\n");
166 printf("Unhandled device type: %i\n", dev_desc->if_type);
170 if (dev_desc->removable)
172 switch (dev_desc->type & 0x1F) {
173 case DEV_TYPE_HARDDISK:
179 case DEV_TYPE_OPDISK:
180 puts ("Optical Device");
186 printf ("# %02X #", dev_desc->type & 0x1F);
190 if (dev_desc->lba > 0L && dev_desc->blksz > 0L) {
191 ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
196 lba512 = (lba * (dev_desc->blksz/512));
197 /* round to 1 digit */
198 /* 2048 = (1024 * 1024) / 512 MB */
199 mb = lba512_muldiv(lba512, 10, 11);
202 mb_rem = mb - (10 * mb_quot);
206 gb_rem = gb - (10 * gb_quot);
209 printf (" Supports 48-bit addressing\n");
211 #if defined(CONFIG_SYS_64BIT_LBA)
212 printf (" Capacity: %lu.%lu MB = %lu.%lu GB (%llu x %lu)\n",
218 printf (" Capacity: %lu.%lu MB = %lu.%lu GB (%lu x %lu)\n",
225 puts (" Capacity: not available\n");
230 #ifdef CONFIG_HAVE_BLOCK_DEVICE
232 void part_init(struct blk_desc *dev_desc)
234 struct part_driver *drv =
235 ll_entry_start(struct part_driver, part_driver);
236 const int n_ents = ll_entry_count(struct part_driver, part_driver);
237 struct part_driver *entry;
239 blkcache_invalidate(dev_desc->if_type, dev_desc->devnum);
241 dev_desc->part_type = PART_TYPE_UNKNOWN;
242 for (entry = drv; entry != drv + n_ents; entry++) {
245 ret = entry->test(dev_desc);
246 debug("%s: try '%s': ret=%d\n", __func__, entry->name, ret);
248 dev_desc->part_type = entry->part_type;
254 static void print_part_header(const char *type, struct blk_desc *dev_desc)
256 #if CONFIG_IS_ENABLED(MAC_PARTITION) || \
257 CONFIG_IS_ENABLED(DOS_PARTITION) || \
258 CONFIG_IS_ENABLED(ISO_PARTITION) || \
259 CONFIG_IS_ENABLED(AMIGA_PARTITION) || \
260 CONFIG_IS_ENABLED(EFI_PARTITION)
261 puts ("\nPartition Map for ");
262 switch (dev_desc->if_type) {
297 printf (" device %d -- Partition Type: %s\n\n",
298 dev_desc->devnum, type);
299 #endif /* any CONFIG_..._PARTITION */
302 void part_print(struct blk_desc *dev_desc)
304 struct part_driver *drv;
306 drv = part_driver_lookup_type(dev_desc);
308 printf("## Unknown partition table type %x\n",
309 dev_desc->part_type);
313 PRINTF("## Testing for valid %s partition ##\n", drv->name);
314 print_part_header(drv->name, dev_desc);
316 drv->print(dev_desc);
319 #endif /* CONFIG_HAVE_BLOCK_DEVICE */
321 int part_get_info(struct blk_desc *dev_desc, int part,
322 struct disk_partition *info)
324 #ifdef CONFIG_HAVE_BLOCK_DEVICE
325 struct part_driver *drv;
327 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
328 /* The common case is no UUID support */
331 #ifdef CONFIG_PARTITION_TYPE_GUID
332 info->type_guid[0] = 0;
335 drv = part_driver_lookup_type(dev_desc);
337 debug("## Unknown partition table type %x\n",
338 dev_desc->part_type);
339 return -EPROTONOSUPPORT;
341 if (!drv->get_info) {
342 PRINTF("## Driver %s does not have the get_info() method\n",
346 if (drv->get_info(dev_desc, part, info) == 0) {
347 PRINTF("## Valid %s partition found ##\n", drv->name);
350 #endif /* CONFIG_HAVE_BLOCK_DEVICE */
355 int part_get_info_whole_disk(struct blk_desc *dev_desc,
356 struct disk_partition *info)
359 info->size = dev_desc->lba;
360 info->blksz = dev_desc->blksz;
362 strcpy((char *)info->type, BOOT_PART_TYPE);
363 strcpy((char *)info->name, "Whole Disk");
364 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
367 #ifdef CONFIG_PARTITION_TYPE_GUID
368 info->type_guid[0] = 0;
374 int blk_get_device_by_str(const char *ifname, const char *dev_hwpart_str,
375 struct blk_desc **dev_desc)
378 char *dup_str = NULL;
379 const char *dev_str, *hwpart_str;
382 hwpart_str = strchr(dev_hwpart_str, '.');
384 dup_str = strdup(dev_hwpart_str);
385 dup_str[hwpart_str - dev_hwpart_str] = 0;
389 dev_str = dev_hwpart_str;
393 dev = simple_strtoul(dev_str, &ep, 16);
395 printf("** Bad device specification %s %s **\n",
402 hwpart = simple_strtoul(hwpart_str, &ep, 16);
404 printf("** Bad HW partition specification %s %s **\n",
411 *dev_desc = get_dev_hwpart(ifname, dev, hwpart);
412 if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
413 debug("** Bad device %s %s **\n", ifname, dev_hwpart_str);
418 #ifdef CONFIG_HAVE_BLOCK_DEVICE
420 * Updates the partition table for the specified hw partition.
421 * Always should be done, otherwise hw partition 0 will return stale
422 * data after displaying a non-zero hw partition.
424 part_init(*dev_desc);
432 #define PART_UNSPECIFIED -2
434 int blk_get_device_part_str(const char *ifname, const char *dev_part_str,
435 struct blk_desc **dev_desc,
436 struct disk_partition *info, int allow_whole_dev)
439 const char *part_str;
440 char *dup_str = NULL;
446 struct disk_partition tmpinfo;
448 #ifdef CONFIG_SANDBOX
450 * Special-case a pseudo block device "hostfs", to allow access to the
451 * host's own filesystem.
453 if (0 == strcmp(ifname, "hostfs")) {
459 strcpy((char *)info->type, BOOT_PART_TYPE);
460 strcpy((char *)info->name, "Sandbox host");
461 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
464 #ifdef CONFIG_PARTITION_TYPE_GUID
465 info->type_guid[0] = 0;
472 #ifdef CONFIG_CMD_UBIFS
474 * Special-case ubi, ubi goes through a mtd, rather than through
475 * a regular block device.
477 if (0 == strcmp(ifname, "ubi")) {
478 if (!ubifs_is_mounted()) {
479 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
484 memset(info, 0, sizeof(*info));
485 strcpy((char *)info->type, BOOT_PART_TYPE);
486 strcpy((char *)info->name, "UBI");
487 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
494 /* If no dev_part_str, use bootdevice environment variable */
495 if (!dev_part_str || !strlen(dev_part_str) ||
496 !strcmp(dev_part_str, "-"))
497 dev_part_str = env_get("bootdevice");
499 /* If still no dev_part_str, it's an error */
501 printf("** No device specified **\n");
505 /* Separate device and partition ID specification */
506 part_str = strchr(dev_part_str, ':');
508 dup_str = strdup(dev_part_str);
509 dup_str[part_str - dev_part_str] = 0;
513 dev_str = dev_part_str;
516 /* Look up the device */
517 dev = blk_get_device_by_str(ifname, dev_str, dev_desc);
521 /* Convert partition ID string to number */
522 if (!part_str || !*part_str) {
523 part = PART_UNSPECIFIED;
524 } else if (!strcmp(part_str, "auto")) {
527 /* Something specified -> use exactly that */
528 part = (int)simple_strtoul(part_str, &ep, 16);
530 * Less than whole string converted,
531 * or request for whole device, but caller requires partition.
533 if (*ep || (part == 0 && !allow_whole_dev)) {
534 printf("** Bad partition specification %s %s **\n",
535 ifname, dev_part_str);
541 * No partition table on device,
542 * or user requested partition 0 (entire device).
544 if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
546 if (!(*dev_desc)->lba) {
547 printf("** Bad device size - %s %s **\n", ifname,
553 * If user specified a partition ID other than 0,
554 * or the calling command only accepts partitions,
557 if ((part > 0) || (!allow_whole_dev)) {
558 printf("** No partition table - %s %s **\n", ifname,
563 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
565 part_get_info_whole_disk(*dev_desc, info);
572 * Now there's known to be a partition table,
573 * not specifying a partition means to pick partition 1.
575 if (part == PART_UNSPECIFIED)
579 * If user didn't specify a partition number, or did specify something
580 * other than "auto", use that partition number directly.
582 if (part != PART_AUTO) {
583 ret = part_get_info(*dev_desc, part, info);
585 printf("** Invalid partition %d **\n", part);
590 * Find the first bootable partition.
591 * If none are bootable, fall back to the first valid partition.
594 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
595 ret = part_get_info(*dev_desc, p, info);
600 * First valid partition, or new better partition?
601 * If so, save partition ID.
603 if (!part || info->bootable)
606 /* Best possible partition? Stop searching. */
611 * We now need to search further for best possible.
612 * If we what we just queried was the best so far,
613 * save the info since we over-write it next loop.
618 /* If we found any acceptable partition */
621 * If we searched all possible partition IDs,
622 * return the first valid partition we found.
624 if (p == MAX_SEARCH_PARTITIONS + 1)
627 printf("** No valid partitions found **\n");
632 if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
633 printf("** Invalid partition type \"%.32s\""
634 " (expect \"" BOOT_PART_TYPE "\")\n",
640 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
650 int part_get_info_by_name_type(struct blk_desc *dev_desc, const char *name,
651 struct disk_partition *info, int part_type)
653 struct part_driver *part_drv;
657 part_drv = part_driver_lookup_type(dev_desc);
660 for (i = 1; i < part_drv->max_entries; i++) {
661 ret = part_drv->get_info(dev_desc, i, info);
663 /* no more entries in table */
666 if (strcmp(name, (const char *)info->name) == 0) {
675 int part_get_info_by_name(struct blk_desc *dev_desc, const char *name,
676 struct disk_partition *info)
678 return part_get_info_by_name_type(dev_desc, name, info, PART_TYPE_ALL);
682 * Get partition info from device number and partition name.
684 * Parse a device number and partition name string in the form of
685 * "device_num#partition_name", for example "0#misc". If the partition
686 * is found, sets dev_desc and part_info accordingly with the information
687 * of the partition with the given partition_name.
689 * @param[in] dev_iface Device interface
690 * @param[in] dev_part_str Input string argument, like "0#misc"
691 * @param[out] dev_desc Place to store the device description pointer
692 * @param[out] part_info Place to store the partition information
693 * @return 0 on success, or a negative on error
695 static int part_get_info_by_dev_and_name(const char *dev_iface,
696 const char *dev_part_str,
697 struct blk_desc **dev_desc,
698 struct disk_partition *part_info)
701 const char *part_str;
704 part_str = strchr(dev_part_str, '#');
705 if (!part_str || part_str == dev_part_str)
708 dev_num = simple_strtoul(dev_part_str, &ep, 16);
709 if (ep != part_str) {
710 /* Not all the first part before the # was parsed. */
715 *dev_desc = blk_get_dev(dev_iface, dev_num);
717 printf("Could not find %s %d\n", dev_iface, dev_num);
720 if (part_get_info_by_name(*dev_desc, part_str, part_info) < 0) {
721 printf("Could not find \"%s\" partition\n", part_str);
727 int part_get_info_by_dev_and_name_or_num(const char *dev_iface,
728 const char *dev_part_str,
729 struct blk_desc **dev_desc,
730 struct disk_partition *part_info)
732 /* Split the part_name if passed as "$dev_num#part_name". */
733 if (!part_get_info_by_dev_and_name(dev_iface, dev_part_str,
734 dev_desc, part_info))
737 * Couldn't lookup by name, try looking up the partition description
740 if (blk_get_device_part_str(dev_iface, dev_part_str,
741 dev_desc, part_info, 1) < 0) {
742 printf("Couldn't find partition %s %s\n",
743 dev_iface, dev_part_str);
749 void part_set_generic_name(const struct blk_desc *dev_desc,
750 int part_num, char *name)
754 switch (dev_desc->if_type) {
778 sprintf(name, "%s%c%d", devtype, 'a' + dev_desc->devnum, part_num);