1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
16 #include <ubifs_uboot.h>
21 #define PRINTF(fmt,args...) printf (fmt ,##args)
23 #define PRINTF(fmt,args...)
26 /* Check all partition types */
27 #define PART_TYPE_ALL -1
29 static struct part_driver *part_driver_get_type(int part_type)
31 struct part_driver *drv =
32 ll_entry_start(struct part_driver, part_driver);
33 const int n_ents = ll_entry_count(struct part_driver, part_driver);
34 struct part_driver *entry;
36 for (entry = drv; entry != drv + n_ents; entry++) {
37 if (part_type == entry->part_type)
45 static struct part_driver *part_driver_lookup_type(struct blk_desc *desc)
47 struct part_driver *drv =
48 ll_entry_start(struct part_driver, part_driver);
49 const int n_ents = ll_entry_count(struct part_driver, part_driver);
50 struct part_driver *entry;
52 if (desc->part_type == PART_TYPE_UNKNOWN) {
53 for (entry = drv; entry != drv + n_ents; entry++) {
56 ret = entry->test(desc);
58 desc->part_type = entry->part_type;
63 return part_driver_get_type(desc->part_type);
70 int part_get_type_by_name(const char *name)
72 struct part_driver *drv =
73 ll_entry_start(struct part_driver, part_driver);
74 const int n_ents = ll_entry_count(struct part_driver, part_driver);
75 struct part_driver *entry;
77 for (entry = drv; entry != drv + n_ents; entry++) {
78 if (!strcasecmp(name, entry->name))
79 return entry->part_type;
83 return PART_TYPE_UNKNOWN;
86 static struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
88 struct blk_desc *desc;
93 desc = blk_get_devnum_by_uclass_idname(ifname, dev);
95 debug("%s: No device for iface '%s', dev %d\n", __func__,
99 ret = blk_dselect_hwpart(desc, hwpart);
101 debug("%s: Failed to select h/w partition: err-%d\n", __func__,
109 struct blk_desc *blk_get_dev(const char *ifname, int dev)
114 return get_dev_hwpart(ifname, dev, 0);
117 /* ------------------------------------------------------------------------- */
119 * reports device info to the user
123 typedef uint64_t lba512_t;
125 typedef lbaint_t lba512_t;
129 * Overflowless variant of (block_count * mul_by / 2**right_shift)
130 * when 2**right_shift > mul_by
132 static lba512_t lba512_muldiv(lba512_t block_count, lba512_t mul_by,
135 lba512_t bc_quot, bc_rem;
137 /* x * m / d == x / d * m + (x % d) * m / d */
138 bc_quot = block_count >> right_shift;
139 bc_rem = block_count - (bc_quot << right_shift);
140 return bc_quot * mul_by + ((bc_rem * mul_by) >> right_shift);
143 void dev_print(struct blk_desc *desc)
145 lba512_t lba512; /* number of blocks if 512bytes block size */
147 if (desc->type == DEV_TYPE_UNKNOWN) {
148 puts ("not available\n");
152 switch (desc->uclass_id) {
154 printf("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n", desc->target,
155 desc->lun, desc->vendor, desc->product, desc->revision);
159 printf("Model: %s Firm: %s Ser#: %s\n", desc->vendor,
160 desc->revision, desc->product);
168 printf ("Vendor: %s Rev: %s Prod: %s\n",
174 printf("%s VirtIO Block Device\n", desc->vendor);
176 case UCLASS_EFI_MEDIA:
177 printf("EFI media Block Device %d\n", desc->devnum);
180 puts("device type unknown\n");
183 printf("Unhandled device type: %i\n", desc->uclass_id);
189 switch (desc->type & 0x1F) {
190 case DEV_TYPE_HARDDISK:
196 case DEV_TYPE_OPDISK:
197 puts ("Optical Device");
203 printf("# %02X #", desc->type & 0x1F);
207 if (desc->lba > 0L && desc->blksz > 0L) {
208 ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
213 lba512 = lba * (desc->blksz / 512);
214 /* round to 1 digit */
215 /* 2048 = (1024 * 1024) / 512 MB */
216 mb = lba512_muldiv(lba512, 10, 11);
219 mb_rem = mb - (10 * mb_quot);
223 gb_rem = gb - (10 * gb_quot);
226 printf (" Supports 48-bit addressing\n");
228 #if defined(CONFIG_SYS_64BIT_LBA)
229 printf (" Capacity: %lu.%lu MB = %lu.%lu GB (%llu x %lu)\n",
235 printf (" Capacity: %lu.%lu MB = %lu.%lu GB (%lu x %lu)\n",
242 puts (" Capacity: not available\n");
246 void part_init(struct blk_desc *desc)
248 struct part_driver *drv =
249 ll_entry_start(struct part_driver, part_driver);
250 const int n_ents = ll_entry_count(struct part_driver, part_driver);
251 struct part_driver *entry;
253 blkcache_invalidate(desc->uclass_id, desc->devnum);
255 desc->part_type = PART_TYPE_UNKNOWN;
256 for (entry = drv; entry != drv + n_ents; entry++) {
259 ret = entry->test(desc);
260 debug("%s: try '%s': ret=%d\n", __func__, entry->name, ret);
262 desc->part_type = entry->part_type;
268 static void print_part_header(const char *type, struct blk_desc *desc)
270 #if CONFIG_IS_ENABLED(MAC_PARTITION) || \
271 CONFIG_IS_ENABLED(DOS_PARTITION) || \
272 CONFIG_IS_ENABLED(ISO_PARTITION) || \
273 CONFIG_IS_ENABLED(AMIGA_PARTITION) || \
274 CONFIG_IS_ENABLED(EFI_PARTITION)
275 puts ("\nPartition Map for ");
276 switch (desc->uclass_id) {
304 case UCLASS_EFI_MEDIA:
311 printf (" device %d -- Partition Type: %s\n\n",
313 #endif /* any CONFIG_..._PARTITION */
316 void part_print(struct blk_desc *desc)
318 struct part_driver *drv;
320 drv = part_driver_lookup_type(desc);
322 printf("## Unknown partition table type %x\n",
327 PRINTF("## Testing for valid %s partition ##\n", drv->name);
328 print_part_header(drv->name, desc);
333 int part_get_info_by_type(struct blk_desc *desc, int part, int part_type,
334 struct disk_partition *info)
336 struct part_driver *drv;
339 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
340 /* The common case is no UUID support */
343 #ifdef CONFIG_PARTITION_TYPE_GUID
344 info->type_guid[0] = 0;
347 if (part_type == PART_TYPE_UNKNOWN) {
348 drv = part_driver_lookup_type(desc);
350 drv = part_driver_get_type(part_type);
354 debug("## Unknown partition table type %x\n",
356 return -EPROTONOSUPPORT;
358 if (!drv->get_info) {
359 PRINTF("## Driver %s does not have the get_info() method\n",
363 if (drv->get_info(desc, part, info) == 0) {
364 PRINTF("## Valid %s partition found ##\n", drv->name);
372 int part_get_info(struct blk_desc *desc, int part,
373 struct disk_partition *info)
375 return part_get_info_by_type(desc, part, PART_TYPE_UNKNOWN, info);
378 int part_get_info_whole_disk(struct blk_desc *desc,
379 struct disk_partition *info)
382 info->size = desc->lba;
383 info->blksz = desc->blksz;
385 strcpy((char *)info->type, BOOT_PART_TYPE);
386 strcpy((char *)info->name, "Whole Disk");
387 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
390 #ifdef CONFIG_PARTITION_TYPE_GUID
391 info->type_guid[0] = 0;
397 int blk_get_device_by_str(const char *ifname, const char *dev_hwpart_str,
398 struct blk_desc **desc)
401 char *dup_str = NULL;
402 const char *dev_str, *hwpart_str;
405 hwpart_str = strchr(dev_hwpart_str, '.');
407 dup_str = strdup(dev_hwpart_str);
408 dup_str[hwpart_str - dev_hwpart_str] = 0;
412 dev_str = dev_hwpart_str;
416 dev = hextoul(dev_str, &ep);
418 printf("** Bad device specification %s %s **\n",
425 hwpart = hextoul(hwpart_str, &ep);
427 printf("** Bad HW partition specification %s %s **\n",
434 *desc = get_dev_hwpart(ifname, dev, hwpart);
435 if (!(*desc) || ((*desc)->type == DEV_TYPE_UNKNOWN)) {
436 debug("** Bad device %s %s **\n", ifname, dev_hwpart_str);
443 * Updates the partition table for the specified hw partition.
444 * Always should be done, otherwise hw partition 0 will return
445 * stale data after displaying a non-zero hw partition.
447 if ((*desc)->uclass_id == UCLASS_MMC)
456 #define PART_UNSPECIFIED -2
458 int blk_get_device_part_str(const char *ifname, const char *dev_part_str,
459 struct blk_desc **desc,
460 struct disk_partition *info, int allow_whole_dev)
463 const char *part_str;
464 char *dup_str = NULL;
470 struct disk_partition tmpinfo;
473 memset(info, 0, sizeof(*info));
475 #if IS_ENABLED(CONFIG_SANDBOX) || IS_ENABLED(CONFIG_SEMIHOSTING)
477 * Special-case a pseudo block device "hostfs", to allow access to the
478 * host's own filesystem.
480 if (!strcmp(ifname, "hostfs")) {
481 strcpy((char *)info->type, BOOT_PART_TYPE);
482 strcpy((char *)info->name, "Host filesystem");
488 #if IS_ENABLED(CONFIG_CMD_UBIFS) && !IS_ENABLED(CONFIG_SPL_BUILD)
490 * Special-case ubi, ubi goes through a mtd, rather than through
491 * a regular block device.
493 if (!strcmp(ifname, "ubi")) {
494 if (!ubifs_is_mounted()) {
495 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
499 strcpy((char *)info->type, BOOT_PART_TYPE);
500 strcpy((char *)info->name, "UBI");
505 /* If no dev_part_str, use bootdevice environment variable */
506 if (CONFIG_IS_ENABLED(ENV_SUPPORT)) {
507 if (!dev_part_str || !strlen(dev_part_str) ||
508 !strcmp(dev_part_str, "-"))
509 dev_part_str = env_get("bootdevice");
512 /* If still no dev_part_str, it's an error */
514 printf("** No device specified **\n");
519 /* Separate device and partition ID specification */
520 part_str = strchr(dev_part_str, ':');
522 dup_str = strdup(dev_part_str);
523 dup_str[part_str - dev_part_str] = 0;
527 dev_str = dev_part_str;
530 /* Look up the device */
531 dev = blk_get_device_by_str(ifname, dev_str, desc);
533 printf("** Bad device specification %s %s **\n",
539 /* Convert partition ID string to number */
540 if (!part_str || !*part_str) {
541 part = PART_UNSPECIFIED;
542 } else if (!strcmp(part_str, "auto")) {
545 /* Something specified -> use exactly that */
546 part = (int)hextoul(part_str, &ep);
548 * Less than whole string converted,
549 * or request for whole device, but caller requires partition.
551 if (*ep || (part == 0 && !allow_whole_dev)) {
552 printf("** Bad partition specification %s %s **\n",
553 ifname, dev_part_str);
560 * No partition table on device,
561 * or user requested partition 0 (entire device).
563 if (((*desc)->part_type == PART_TYPE_UNKNOWN) || !part) {
565 printf("** Bad device size - %s %s **\n", ifname,
572 * If user specified a partition ID other than 0,
573 * or the calling command only accepts partitions,
576 if ((part > 0) || (!allow_whole_dev)) {
577 printf("** No partition table - %s %s **\n", ifname,
579 ret = -EPROTONOSUPPORT;
583 (*desc)->log2blksz = LOG2((*desc)->blksz);
585 part_get_info_whole_disk(*desc, info);
592 * Now there's known to be a partition table,
593 * not specifying a partition means to pick partition 1.
595 if (part == PART_UNSPECIFIED)
599 * If user didn't specify a partition number, or did specify something
600 * other than "auto", use that partition number directly.
602 if (part != PART_AUTO) {
603 ret = part_get_info(*desc, part, info);
605 printf("** Invalid partition %d **\n", part);
610 * Find the first bootable partition.
611 * If none are bootable, fall back to the first valid partition.
614 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
615 ret = part_get_info(*desc, p, info);
620 * First valid partition, or new better partition?
621 * If so, save partition ID.
623 if (!part || info->bootable)
626 /* Best possible partition? Stop searching. */
631 * We now need to search further for best possible.
632 * If we what we just queried was the best so far,
633 * save the info since we over-write it next loop.
638 /* If we found any acceptable partition */
641 * If we searched all possible partition IDs,
642 * return the first valid partition we found.
644 if (p == MAX_SEARCH_PARTITIONS + 1)
647 printf("** No valid partitions found **\n");
651 if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
652 printf("** Invalid partition type \"%.32s\""
653 " (expect \"" BOOT_PART_TYPE "\")\n",
659 (*desc)->log2blksz = LOG2((*desc)->blksz);
669 int part_get_info_by_name(struct blk_desc *desc, const char *name,
670 struct disk_partition *info)
672 struct part_driver *part_drv;
676 part_drv = part_driver_lookup_type(desc);
680 if (!part_drv->get_info) {
681 log_debug("## Driver %s does not have the get_info() method\n",
686 for (i = 1; i < part_drv->max_entries; i++) {
687 ret = part_drv->get_info(desc, i, info);
689 /* no more entries in table */
692 if (strcmp(name, (const char *)info->name) == 0) {
702 * Get partition info from device number and partition name.
704 * Parse a device number and partition name string in the form of
705 * "devicenum.hwpartnum#partition_name", for example "0.1#misc". devicenum and
706 * hwpartnum are both optional, defaulting to 0. If the partition is found,
707 * sets desc and part_info accordingly with the information of the
708 * partition with the given partition_name.
710 * @param[in] dev_iface Device interface
711 * @param[in] dev_part_str Input string argument, like "0.1#misc"
712 * @param[out] desc Place to store the device description pointer
713 * @param[out] part_info Place to store the partition information
714 * Return: 0 on success, or a negative on error
716 static int part_get_info_by_dev_and_name(const char *dev_iface,
717 const char *dev_part_str,
718 struct blk_desc **desc,
719 struct disk_partition *part_info)
721 char *dup_str = NULL;
722 const char *dev_str, *part_str;
725 /* Separate device and partition name specification */
727 part_str = strchr(dev_part_str, '#');
732 dup_str = strdup(dev_part_str);
733 dup_str[part_str - dev_part_str] = 0;
740 ret = blk_get_device_by_str(dev_iface, dev_str, desc);
744 ret = part_get_info_by_name(*desc, part_str, part_info);
746 printf("Could not find \"%s\" partition\n", part_str);
753 int part_get_info_by_dev_and_name_or_num(const char *dev_iface,
754 const char *dev_part_str,
755 struct blk_desc **desc,
756 struct disk_partition *part_info,
761 /* Split the part_name if passed as "$dev_num#part_name". */
762 ret = part_get_info_by_dev_and_name(dev_iface, dev_part_str, desc,
767 * Couldn't lookup by name, try looking up the partition description
770 ret = blk_get_device_part_str(dev_iface, dev_part_str, desc, part_info,
773 printf("Couldn't find partition %s %s\n",
774 dev_iface, dev_part_str);
778 void part_set_generic_name(const struct blk_desc *desc, int part_num,
783 switch (desc->uclass_id) {
802 sprintf(name, "%s%c%d", devtype, 'a' + desc->devnum, part_num);
805 int part_get_bootable(struct blk_desc *desc)
807 struct disk_partition info;
810 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
813 ret = part_get_info(desc, p, &info);
814 if (!ret && info.bootable)