3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
14 #include <ubifs_uboot.h>
19 #define PRINTF(fmt,args...) printf (fmt ,##args)
21 #define PRINTF(fmt,args...)
24 const struct block_drvr block_drvr[] = {
25 #if defined(CONFIG_CMD_IDE)
26 { .name = "ide", .get_dev = ide_get_dev, },
28 #if defined(CONFIG_CMD_SATA)
29 {.name = "sata", .get_dev = sata_get_dev, },
31 #if defined(CONFIG_CMD_SCSI)
32 { .name = "scsi", .get_dev = scsi_get_dev, },
34 #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
35 { .name = "usb", .get_dev = usb_stor_get_dev, },
37 #if defined(CONFIG_MMC)
40 .get_dev = mmc_get_dev,
41 .select_hwpart = mmc_select_hwpart,
44 #if defined(CONFIG_SYSTEMACE)
45 { .name = "ace", .get_dev = systemace_get_dev, },
47 #if defined(CONFIG_SANDBOX)
48 { .name = "host", .get_dev = host_get_dev, },
53 DECLARE_GLOBAL_DATA_PTR;
55 #ifdef HAVE_BLOCK_DEVICE
56 static struct part_driver *part_driver_lookup_type(int part_type)
58 struct part_driver *drv =
59 ll_entry_start(struct part_driver, part_driver);
60 const int n_ents = ll_entry_count(struct part_driver, part_driver);
61 struct part_driver *entry;
63 for (entry = drv; entry != drv + n_ents; entry++) {
64 if (part_type == entry->part_type)
72 static struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
74 const struct block_drvr *drvr = block_drvr;
75 struct blk_desc* (*reloc_get_dev)(int dev);
76 int (*select_hwpart)(int dev_num, int hwpart);
84 #ifdef CONFIG_NEEDS_MANUAL_RELOC
85 name += gd->reloc_off;
89 reloc_get_dev = drvr->get_dev;
90 select_hwpart = drvr->select_hwpart;
91 #ifdef CONFIG_NEEDS_MANUAL_RELOC
92 name += gd->reloc_off;
93 reloc_get_dev += gd->reloc_off;
95 select_hwpart += gd->reloc_off;
97 if (strncmp(ifname, name, strlen(name)) == 0) {
98 struct blk_desc *dev_desc = reloc_get_dev(dev);
101 if (hwpart == 0 && !select_hwpart)
105 ret = select_hwpart(dev_desc->devnum, hwpart);
115 struct blk_desc *blk_get_dev(const char *ifname, int dev)
117 return get_dev_hwpart(ifname, dev, 0);
120 struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
125 struct blk_desc *blk_get_dev(const char *ifname, int dev)
131 #ifdef HAVE_BLOCK_DEVICE
133 /* ------------------------------------------------------------------------- */
135 * reports device info to the user
139 typedef uint64_t lba512_t;
141 typedef lbaint_t lba512_t;
145 * Overflowless variant of (block_count * mul_by / div_by)
146 * when div_by > mul_by
148 static lba512_t lba512_muldiv(lba512_t block_count, lba512_t mul_by, lba512_t div_by)
150 lba512_t bc_quot, bc_rem;
152 /* x * m / d == x / d * m + (x % d) * m / d */
153 bc_quot = block_count / div_by;
154 bc_rem = block_count - div_by * bc_quot;
155 return bc_quot * mul_by + (bc_rem * mul_by) / div_by;
158 void dev_print (struct blk_desc *dev_desc)
160 lba512_t lba512; /* number of blocks if 512bytes block size */
162 if (dev_desc->type == DEV_TYPE_UNKNOWN) {
163 puts ("not available\n");
167 switch (dev_desc->if_type) {
169 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
170 dev_desc->target,dev_desc->lun,
178 printf ("Model: %s Firm: %s Ser#: %s\n",
186 printf ("Vendor: %s Rev: %s Prod: %s\n",
192 puts("device type DOC\n");
194 case IF_TYPE_UNKNOWN:
195 puts("device type unknown\n");
198 printf("Unhandled device type: %i\n", dev_desc->if_type);
202 if (dev_desc->removable)
204 switch (dev_desc->type & 0x1F) {
205 case DEV_TYPE_HARDDISK:
211 case DEV_TYPE_OPDISK:
212 puts ("Optical Device");
218 printf ("# %02X #", dev_desc->type & 0x1F);
222 if (dev_desc->lba > 0L && dev_desc->blksz > 0L) {
223 ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
228 lba512 = (lba * (dev_desc->blksz/512));
229 /* round to 1 digit */
230 /* 2048 = (1024 * 1024) / 512 MB */
231 mb = lba512_muldiv(lba512, 10, 2048);
234 mb_rem = mb - (10 * mb_quot);
238 gb_rem = gb - (10 * gb_quot);
241 printf (" Supports 48-bit addressing\n");
243 #if defined(CONFIG_SYS_64BIT_LBA)
244 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n",
250 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
257 puts (" Capacity: not available\n");
262 #ifdef HAVE_BLOCK_DEVICE
264 void part_init(struct blk_desc *dev_desc)
266 struct part_driver *drv =
267 ll_entry_start(struct part_driver, part_driver);
268 const int n_ents = ll_entry_count(struct part_driver, part_driver);
269 struct part_driver *entry;
271 blkcache_invalidate(dev_desc->if_type, dev_desc->devnum);
273 dev_desc->part_type = PART_TYPE_UNKNOWN;
274 for (entry = drv; entry != drv + n_ents; entry++) {
277 ret = entry->test(dev_desc);
278 debug("%s: try '%s': ret=%d\n", __func__, entry->name, ret);
280 dev_desc->part_type = entry->part_type;
286 static void print_part_header(const char *type, struct blk_desc *dev_desc)
288 #if defined(CONFIG_MAC_PARTITION) || \
289 defined(CONFIG_DOS_PARTITION) || \
290 defined(CONFIG_ISO_PARTITION) || \
291 defined(CONFIG_AMIGA_PARTITION) || \
292 defined(CONFIG_EFI_PARTITION)
293 puts ("\nPartition Map for ");
294 switch (dev_desc->if_type) {
323 printf (" device %d -- Partition Type: %s\n\n",
324 dev_desc->devnum, type);
325 #endif /* any CONFIG_..._PARTITION */
328 void part_print(struct blk_desc *dev_desc)
330 struct part_driver *drv;
332 drv = part_driver_lookup_type(dev_desc->part_type);
334 printf("## Unknown partition table type %x\n",
335 dev_desc->part_type);
339 PRINTF("## Testing for valid %s partition ##\n", drv->name);
340 print_part_header(drv->name, dev_desc);
342 drv->print(dev_desc);
345 #endif /* HAVE_BLOCK_DEVICE */
347 int part_get_info(struct blk_desc *dev_desc, int part,
348 disk_partition_t *info)
350 #ifdef HAVE_BLOCK_DEVICE
351 struct part_driver *drv;
353 #ifdef CONFIG_PARTITION_UUIDS
354 /* The common case is no UUID support */
357 #ifdef CONFIG_PARTITION_TYPE_GUID
358 info->type_guid[0] = 0;
361 drv = part_driver_lookup_type(dev_desc->part_type);
363 debug("## Unknown partition table type %x\n",
364 dev_desc->part_type);
365 return -EPROTONOSUPPORT;
367 if (!drv->get_info) {
368 PRINTF("## Driver %s does not have the get_info() method\n",
372 if (drv->get_info(dev_desc, part, info) == 0) {
373 PRINTF("## Valid %s partition found ##\n", drv->name);
376 #endif /* HAVE_BLOCK_DEVICE */
381 int blk_get_device_by_str(const char *ifname, const char *dev_hwpart_str,
382 struct blk_desc **dev_desc)
385 char *dup_str = NULL;
386 const char *dev_str, *hwpart_str;
389 hwpart_str = strchr(dev_hwpart_str, '.');
391 dup_str = strdup(dev_hwpart_str);
392 dup_str[hwpart_str - dev_hwpart_str] = 0;
396 dev_str = dev_hwpart_str;
400 dev = simple_strtoul(dev_str, &ep, 16);
402 printf("** Bad device specification %s %s **\n",
409 hwpart = simple_strtoul(hwpart_str, &ep, 16);
411 printf("** Bad HW partition specification %s %s **\n",
418 *dev_desc = get_dev_hwpart(ifname, dev, hwpart);
419 if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
420 printf("** Bad device %s %s **\n", ifname, dev_hwpart_str);
425 #ifdef HAVE_BLOCK_DEVICE
427 * Updates the partition table for the specified hw partition.
428 * Does not need to be done for hwpart 0 since it is default and
432 part_init(*dev_desc);
440 #define PART_UNSPECIFIED -2
442 #define MAX_SEARCH_PARTITIONS 16
443 int blk_get_device_part_str(const char *ifname, const char *dev_part_str,
444 struct blk_desc **dev_desc,
445 disk_partition_t *info, int allow_whole_dev)
448 const char *part_str;
449 char *dup_str = NULL;
455 disk_partition_t tmpinfo;
457 #ifdef CONFIG_SANDBOX
459 * Special-case a pseudo block device "hostfs", to allow access to the
460 * host's own filesystem.
462 if (0 == strcmp(ifname, "hostfs")) {
468 strcpy((char *)info->type, BOOT_PART_TYPE);
469 strcpy((char *)info->name, "Sandbox host");
470 #ifdef CONFIG_PARTITION_UUIDS
473 #ifdef CONFIG_PARTITION_TYPE_GUID
474 info->type_guid[0] = 0;
481 #ifdef CONFIG_CMD_UBIFS
483 * Special-case ubi, ubi goes through a mtd, rathen then through
484 * a regular block device.
486 if (0 == strcmp(ifname, "ubi")) {
487 if (!ubifs_is_mounted()) {
488 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
493 memset(info, 0, sizeof(*info));
494 strcpy((char *)info->type, BOOT_PART_TYPE);
495 strcpy((char *)info->name, "UBI");
496 #ifdef CONFIG_PARTITION_UUIDS
503 /* If no dev_part_str, use bootdevice environment variable */
504 if (!dev_part_str || !strlen(dev_part_str) ||
505 !strcmp(dev_part_str, "-"))
506 dev_part_str = getenv("bootdevice");
508 /* If still no dev_part_str, it's an error */
510 printf("** No device specified **\n");
514 /* Separate device and partition ID specification */
515 part_str = strchr(dev_part_str, ':');
517 dup_str = strdup(dev_part_str);
518 dup_str[part_str - dev_part_str] = 0;
522 dev_str = dev_part_str;
525 /* Look up the device */
526 dev = blk_get_device_by_str(ifname, dev_str, dev_desc);
530 /* Convert partition ID string to number */
531 if (!part_str || !*part_str) {
532 part = PART_UNSPECIFIED;
533 } else if (!strcmp(part_str, "auto")) {
536 /* Something specified -> use exactly that */
537 part = (int)simple_strtoul(part_str, &ep, 16);
539 * Less than whole string converted,
540 * or request for whole device, but caller requires partition.
542 if (*ep || (part == 0 && !allow_whole_dev)) {
543 printf("** Bad partition specification %s %s **\n",
544 ifname, dev_part_str);
550 * No partition table on device,
551 * or user requested partition 0 (entire device).
553 if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
555 if (!(*dev_desc)->lba) {
556 printf("** Bad device size - %s %s **\n", ifname,
562 * If user specified a partition ID other than 0,
563 * or the calling command only accepts partitions,
566 if ((part > 0) || (!allow_whole_dev)) {
567 printf("** No partition table - %s %s **\n", ifname,
572 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
575 info->size = (*dev_desc)->lba;
576 info->blksz = (*dev_desc)->blksz;
578 strcpy((char *)info->type, BOOT_PART_TYPE);
579 strcpy((char *)info->name, "Whole Disk");
580 #ifdef CONFIG_PARTITION_UUIDS
583 #ifdef CONFIG_PARTITION_TYPE_GUID
584 info->type_guid[0] = 0;
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(*dev_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(*dev_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");
652 if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
653 printf("** Invalid partition type \"%.32s\""
654 " (expect \"" BOOT_PART_TYPE "\")\n",
660 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);