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)
28 #if defined(CONFIG_CMD_SATA)
31 #if defined(CONFIG_SCSI)
34 #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
37 #if defined(CONFIG_MMC)
40 .select_hwpart = mmc_select_hwpart,
43 #if defined(CONFIG_SYSTEMACE)
46 #if defined(CONFIG_SANDBOX)
52 DECLARE_GLOBAL_DATA_PTR;
54 #ifdef HAVE_BLOCK_DEVICE
55 static struct part_driver *part_driver_lookup_type(int part_type)
57 struct part_driver *drv =
58 ll_entry_start(struct part_driver, part_driver);
59 const int n_ents = ll_entry_count(struct part_driver, part_driver);
60 struct part_driver *entry;
62 for (entry = drv; entry != drv + n_ents; entry++) {
63 if (part_type == entry->part_type)
71 static struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
73 const struct block_drvr *drvr = block_drvr;
74 int (*select_hwpart)(int dev_num, int hwpart);
82 #ifdef CONFIG_NEEDS_MANUAL_RELOC
83 name += gd->reloc_off;
87 select_hwpart = drvr->select_hwpart;
88 #ifdef CONFIG_NEEDS_MANUAL_RELOC
89 name += gd->reloc_off;
91 select_hwpart += gd->reloc_off;
93 if (strncmp(ifname, name, strlen(name)) == 0) {
94 struct blk_desc *dev_desc;
96 dev_desc = blk_get_devnum_by_typename(name, dev);
99 if (hwpart == 0 && !select_hwpart)
103 ret = select_hwpart(dev_desc->devnum, hwpart);
113 struct blk_desc *blk_get_dev(const char *ifname, int dev)
115 return get_dev_hwpart(ifname, dev, 0);
118 struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
123 struct blk_desc *blk_get_dev(const char *ifname, int dev)
129 #ifdef HAVE_BLOCK_DEVICE
131 /* ------------------------------------------------------------------------- */
133 * reports device info to the user
137 typedef uint64_t lba512_t;
139 typedef lbaint_t lba512_t;
143 * Overflowless variant of (block_count * mul_by / div_by)
144 * when div_by > mul_by
146 static lba512_t lba512_muldiv(lba512_t block_count, lba512_t mul_by, lba512_t div_by)
148 lba512_t bc_quot, bc_rem;
150 /* x * m / d == x / d * m + (x % d) * m / d */
151 bc_quot = block_count / div_by;
152 bc_rem = block_count - div_by * bc_quot;
153 return bc_quot * mul_by + (bc_rem * mul_by) / div_by;
156 void dev_print (struct blk_desc *dev_desc)
158 lba512_t lba512; /* number of blocks if 512bytes block size */
160 if (dev_desc->type == DEV_TYPE_UNKNOWN) {
161 puts ("not available\n");
165 switch (dev_desc->if_type) {
167 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
168 dev_desc->target,dev_desc->lun,
176 printf ("Model: %s Firm: %s Ser#: %s\n",
184 printf ("Vendor: %s Rev: %s Prod: %s\n",
190 puts("device type DOC\n");
192 case IF_TYPE_UNKNOWN:
193 puts("device type unknown\n");
196 printf("Unhandled device type: %i\n", dev_desc->if_type);
200 if (dev_desc->removable)
202 switch (dev_desc->type & 0x1F) {
203 case DEV_TYPE_HARDDISK:
209 case DEV_TYPE_OPDISK:
210 puts ("Optical Device");
216 printf ("# %02X #", dev_desc->type & 0x1F);
220 if (dev_desc->lba > 0L && dev_desc->blksz > 0L) {
221 ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
226 lba512 = (lba * (dev_desc->blksz/512));
227 /* round to 1 digit */
228 /* 2048 = (1024 * 1024) / 512 MB */
229 mb = lba512_muldiv(lba512, 10, 2048);
232 mb_rem = mb - (10 * mb_quot);
236 gb_rem = gb - (10 * gb_quot);
239 printf (" Supports 48-bit addressing\n");
241 #if defined(CONFIG_SYS_64BIT_LBA)
242 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n",
248 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
255 puts (" Capacity: not available\n");
260 #ifdef HAVE_BLOCK_DEVICE
262 void part_init(struct blk_desc *dev_desc)
264 struct part_driver *drv =
265 ll_entry_start(struct part_driver, part_driver);
266 const int n_ents = ll_entry_count(struct part_driver, part_driver);
267 struct part_driver *entry;
269 blkcache_invalidate(dev_desc->if_type, dev_desc->devnum);
271 dev_desc->part_type = PART_TYPE_UNKNOWN;
272 for (entry = drv; entry != drv + n_ents; entry++) {
275 ret = entry->test(dev_desc);
276 debug("%s: try '%s': ret=%d\n", __func__, entry->name, ret);
278 dev_desc->part_type = entry->part_type;
284 static void print_part_header(const char *type, struct blk_desc *dev_desc)
286 #if defined(CONFIG_MAC_PARTITION) || \
287 defined(CONFIG_DOS_PARTITION) || \
288 defined(CONFIG_ISO_PARTITION) || \
289 defined(CONFIG_AMIGA_PARTITION) || \
290 defined(CONFIG_EFI_PARTITION)
291 puts ("\nPartition Map for ");
292 switch (dev_desc->if_type) {
321 printf (" device %d -- Partition Type: %s\n\n",
322 dev_desc->devnum, type);
323 #endif /* any CONFIG_..._PARTITION */
326 void part_print(struct blk_desc *dev_desc)
328 struct part_driver *drv;
330 drv = part_driver_lookup_type(dev_desc->part_type);
332 printf("## Unknown partition table type %x\n",
333 dev_desc->part_type);
337 PRINTF("## Testing for valid %s partition ##\n", drv->name);
338 print_part_header(drv->name, dev_desc);
340 drv->print(dev_desc);
343 #endif /* HAVE_BLOCK_DEVICE */
345 int part_get_info(struct blk_desc *dev_desc, int part,
346 disk_partition_t *info)
348 #ifdef HAVE_BLOCK_DEVICE
349 struct part_driver *drv;
351 #ifdef CONFIG_PARTITION_UUIDS
352 /* The common case is no UUID support */
355 #ifdef CONFIG_PARTITION_TYPE_GUID
356 info->type_guid[0] = 0;
359 drv = part_driver_lookup_type(dev_desc->part_type);
361 debug("## Unknown partition table type %x\n",
362 dev_desc->part_type);
363 return -EPROTONOSUPPORT;
365 if (!drv->get_info) {
366 PRINTF("## Driver %s does not have the get_info() method\n",
370 if (drv->get_info(dev_desc, part, info) == 0) {
371 PRINTF("## Valid %s partition found ##\n", drv->name);
374 #endif /* HAVE_BLOCK_DEVICE */
379 int blk_get_device_by_str(const char *ifname, const char *dev_hwpart_str,
380 struct blk_desc **dev_desc)
383 char *dup_str = NULL;
384 const char *dev_str, *hwpart_str;
387 hwpart_str = strchr(dev_hwpart_str, '.');
389 dup_str = strdup(dev_hwpart_str);
390 dup_str[hwpart_str - dev_hwpart_str] = 0;
394 dev_str = dev_hwpart_str;
398 dev = simple_strtoul(dev_str, &ep, 16);
400 printf("** Bad device specification %s %s **\n",
407 hwpart = simple_strtoul(hwpart_str, &ep, 16);
409 printf("** Bad HW partition specification %s %s **\n",
416 *dev_desc = get_dev_hwpart(ifname, dev, hwpart);
417 if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
418 printf("** Bad device %s %s **\n", ifname, dev_hwpart_str);
423 #ifdef HAVE_BLOCK_DEVICE
425 * Updates the partition table for the specified hw partition.
426 * Does not need to be done for hwpart 0 since it is default and
430 part_init(*dev_desc);
438 #define PART_UNSPECIFIED -2
440 #define MAX_SEARCH_PARTITIONS 16
441 int blk_get_device_part_str(const char *ifname, const char *dev_part_str,
442 struct blk_desc **dev_desc,
443 disk_partition_t *info, int allow_whole_dev)
446 const char *part_str;
447 char *dup_str = NULL;
453 disk_partition_t tmpinfo;
455 #ifdef CONFIG_SANDBOX
457 * Special-case a pseudo block device "hostfs", to allow access to the
458 * host's own filesystem.
460 if (0 == strcmp(ifname, "hostfs")) {
466 strcpy((char *)info->type, BOOT_PART_TYPE);
467 strcpy((char *)info->name, "Sandbox host");
468 #ifdef CONFIG_PARTITION_UUIDS
471 #ifdef CONFIG_PARTITION_TYPE_GUID
472 info->type_guid[0] = 0;
479 #ifdef CONFIG_CMD_UBIFS
481 * Special-case ubi, ubi goes through a mtd, rathen then through
482 * a regular block device.
484 if (0 == strcmp(ifname, "ubi")) {
485 if (!ubifs_is_mounted()) {
486 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
491 memset(info, 0, sizeof(*info));
492 strcpy((char *)info->type, BOOT_PART_TYPE);
493 strcpy((char *)info->name, "UBI");
494 #ifdef CONFIG_PARTITION_UUIDS
501 /* If no dev_part_str, use bootdevice environment variable */
502 if (!dev_part_str || !strlen(dev_part_str) ||
503 !strcmp(dev_part_str, "-"))
504 dev_part_str = getenv("bootdevice");
506 /* If still no dev_part_str, it's an error */
508 printf("** No device specified **\n");
512 /* Separate device and partition ID specification */
513 part_str = strchr(dev_part_str, ':');
515 dup_str = strdup(dev_part_str);
516 dup_str[part_str - dev_part_str] = 0;
520 dev_str = dev_part_str;
523 /* Look up the device */
524 dev = blk_get_device_by_str(ifname, dev_str, dev_desc);
528 /* Convert partition ID string to number */
529 if (!part_str || !*part_str) {
530 part = PART_UNSPECIFIED;
531 } else if (!strcmp(part_str, "auto")) {
534 /* Something specified -> use exactly that */
535 part = (int)simple_strtoul(part_str, &ep, 16);
537 * Less than whole string converted,
538 * or request for whole device, but caller requires partition.
540 if (*ep || (part == 0 && !allow_whole_dev)) {
541 printf("** Bad partition specification %s %s **\n",
542 ifname, dev_part_str);
548 * No partition table on device,
549 * or user requested partition 0 (entire device).
551 if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
553 if (!(*dev_desc)->lba) {
554 printf("** Bad device size - %s %s **\n", ifname,
560 * If user specified a partition ID other than 0,
561 * or the calling command only accepts partitions,
564 if ((part > 0) || (!allow_whole_dev)) {
565 printf("** No partition table - %s %s **\n", ifname,
570 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
573 info->size = (*dev_desc)->lba;
574 info->blksz = (*dev_desc)->blksz;
576 strcpy((char *)info->type, BOOT_PART_TYPE);
577 strcpy((char *)info->name, "Whole Disk");
578 #ifdef CONFIG_PARTITION_UUIDS
581 #ifdef CONFIG_PARTITION_TYPE_GUID
582 info->type_guid[0] = 0;
590 * Now there's known to be a partition table,
591 * not specifying a partition means to pick partition 1.
593 if (part == PART_UNSPECIFIED)
597 * If user didn't specify a partition number, or did specify something
598 * other than "auto", use that partition number directly.
600 if (part != PART_AUTO) {
601 ret = part_get_info(*dev_desc, part, info);
603 printf("** Invalid partition %d **\n", part);
608 * Find the first bootable partition.
609 * If none are bootable, fall back to the first valid partition.
612 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
613 ret = part_get_info(*dev_desc, p, info);
618 * First valid partition, or new better partition?
619 * If so, save partition ID.
621 if (!part || info->bootable)
624 /* Best possible partition? Stop searching. */
629 * We now need to search further for best possible.
630 * If we what we just queried was the best so far,
631 * save the info since we over-write it next loop.
636 /* If we found any acceptable partition */
639 * If we searched all possible partition IDs,
640 * return the first valid partition we found.
642 if (p == MAX_SEARCH_PARTITIONS + 1)
645 printf("** No valid partitions found **\n");
650 if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
651 printf("** Invalid partition type \"%.32s\""
652 " (expect \"" BOOT_PART_TYPE "\")\n",
658 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);