3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
17 #define PRINTF(fmt,args...) printf (fmt ,##args)
19 #define PRINTF(fmt,args...)
24 block_dev_desc_t* (*get_dev)(int dev);
25 int (*select_hwpart)(int dev_num, int hwpart);
28 static const struct block_drvr block_drvr[] = {
29 #if defined(CONFIG_CMD_IDE)
30 { .name = "ide", .get_dev = ide_get_dev, },
32 #if defined(CONFIG_CMD_PATA)
33 { .name = "pata", .get_dev = pata_get_dev, },
35 #if defined(CONFIG_CMD_SATA)
36 {.name = "sata", .get_dev = sata_get_dev, },
38 #if defined(CONFIG_CMD_SCSI)
39 { .name = "scsi", .get_dev = scsi_get_dev, },
41 #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
42 { .name = "usb", .get_dev = usb_stor_get_dev, },
44 #if defined(CONFIG_MMC)
47 .get_dev = mmc_get_dev,
48 .select_hwpart = mmc_select_hwpart,
51 #if defined(CONFIG_SYSTEMACE)
52 { .name = "ace", .get_dev = systemace_get_dev, },
54 #if defined(CONFIG_SANDBOX)
55 { .name = "host", .get_dev = host_get_dev, },
60 DECLARE_GLOBAL_DATA_PTR;
62 #ifdef HAVE_BLOCK_DEVICE
63 static block_dev_desc_t *get_dev_hwpart(const char *ifname, int dev, int hwpart)
65 const struct block_drvr *drvr = block_drvr;
66 block_dev_desc_t* (*reloc_get_dev)(int dev);
67 int (*select_hwpart)(int dev_num, int hwpart);
75 #ifdef CONFIG_NEEDS_MANUAL_RELOC
76 name += gd->reloc_off;
80 reloc_get_dev = drvr->get_dev;
81 select_hwpart = drvr->select_hwpart;
82 #ifdef CONFIG_NEEDS_MANUAL_RELOC
83 name += gd->reloc_off;
84 reloc_get_dev += gd->reloc_off;
86 select_hwpart += gd->reloc_off;
88 if (strncmp(ifname, name, strlen(name)) == 0) {
89 block_dev_desc_t *dev_desc = reloc_get_dev(dev);
92 if (hwpart == 0 && !select_hwpart)
96 ret = select_hwpart(dev_desc->dev, hwpart);
106 block_dev_desc_t *get_dev(const char *ifname, int dev)
108 return get_dev_hwpart(ifname, dev, 0);
111 block_dev_desc_t *get_dev_hwpart(const char *ifname, int dev, int hwpart)
116 block_dev_desc_t *get_dev(const char *ifname, int dev)
122 #ifdef HAVE_BLOCK_DEVICE
124 /* ------------------------------------------------------------------------- */
126 * reports device info to the user
130 typedef uint64_t lba512_t;
132 typedef lbaint_t lba512_t;
136 * Overflowless variant of (block_count * mul_by / div_by)
137 * when div_by > mul_by
139 static lba512_t lba512_muldiv(lba512_t block_count, lba512_t mul_by, lba512_t div_by)
141 lba512_t bc_quot, bc_rem;
143 /* x * m / d == x / d * m + (x % d) * m / d */
144 bc_quot = block_count / div_by;
145 bc_rem = block_count - div_by * bc_quot;
146 return bc_quot * mul_by + (bc_rem * mul_by) / div_by;
149 void dev_print (block_dev_desc_t *dev_desc)
151 lba512_t lba512; /* number of blocks if 512bytes block size */
153 if (dev_desc->type == DEV_TYPE_UNKNOWN) {
154 puts ("not available\n");
158 switch (dev_desc->if_type) {
160 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
161 dev_desc->target,dev_desc->lun,
169 printf ("Model: %s Firm: %s Ser#: %s\n",
177 printf ("Vendor: %s Rev: %s Prod: %s\n",
183 puts("device type DOC\n");
185 case IF_TYPE_UNKNOWN:
186 puts("device type unknown\n");
189 printf("Unhandled device type: %i\n", dev_desc->if_type);
193 if (dev_desc->removable)
195 switch (dev_desc->type & 0x1F) {
196 case DEV_TYPE_HARDDISK:
202 case DEV_TYPE_OPDISK:
203 puts ("Optical Device");
209 printf ("# %02X #", dev_desc->type & 0x1F);
213 if (dev_desc->lba > 0L && dev_desc->blksz > 0L) {
214 ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
219 lba512 = (lba * (dev_desc->blksz/512));
220 /* round to 1 digit */
221 /* 2048 = (1024 * 1024) / 512 MB */
222 mb = lba512_muldiv(lba512, 10, 2048);
225 mb_rem = mb - (10 * mb_quot);
229 gb_rem = gb - (10 * gb_quot);
232 printf (" Supports 48-bit addressing\n");
234 #if defined(CONFIG_SYS_64BIT_LBA)
235 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n",
241 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
248 puts (" Capacity: not available\n");
253 #ifdef HAVE_BLOCK_DEVICE
255 void init_part(block_dev_desc_t *dev_desc)
257 #ifdef CONFIG_ISO_PARTITION
258 if (test_part_iso(dev_desc) == 0) {
259 dev_desc->part_type = PART_TYPE_ISO;
264 #ifdef CONFIG_MAC_PARTITION
265 if (test_part_mac(dev_desc) == 0) {
266 dev_desc->part_type = PART_TYPE_MAC;
271 /* must be placed before DOS partition detection */
272 #ifdef CONFIG_EFI_PARTITION
273 if (test_part_efi(dev_desc) == 0) {
274 dev_desc->part_type = PART_TYPE_EFI;
279 #ifdef CONFIG_DOS_PARTITION
280 if (test_part_dos(dev_desc) == 0) {
281 dev_desc->part_type = PART_TYPE_DOS;
286 #ifdef CONFIG_AMIGA_PARTITION
287 if (test_part_amiga(dev_desc) == 0) {
288 dev_desc->part_type = PART_TYPE_AMIGA;
292 dev_desc->part_type = PART_TYPE_UNKNOWN;
296 #if defined(CONFIG_MAC_PARTITION) || \
297 defined(CONFIG_DOS_PARTITION) || \
298 defined(CONFIG_ISO_PARTITION) || \
299 defined(CONFIG_AMIGA_PARTITION) || \
300 defined(CONFIG_EFI_PARTITION)
302 static void print_part_header(const char *type, block_dev_desc_t *dev_desc)
304 puts ("\nPartition Map for ");
305 switch (dev_desc->if_type) {
334 printf (" device %d -- Partition Type: %s\n\n",
335 dev_desc->dev, type);
338 #endif /* any CONFIG_..._PARTITION */
340 void print_part(block_dev_desc_t * dev_desc)
343 switch (dev_desc->part_type) {
344 #ifdef CONFIG_MAC_PARTITION
346 PRINTF ("## Testing for valid MAC partition ##\n");
347 print_part_header ("MAC", dev_desc);
348 print_part_mac (dev_desc);
351 #ifdef CONFIG_DOS_PARTITION
353 PRINTF ("## Testing for valid DOS partition ##\n");
354 print_part_header ("DOS", dev_desc);
355 print_part_dos (dev_desc);
359 #ifdef CONFIG_ISO_PARTITION
361 PRINTF ("## Testing for valid ISO Boot partition ##\n");
362 print_part_header ("ISO", dev_desc);
363 print_part_iso (dev_desc);
367 #ifdef CONFIG_AMIGA_PARTITION
368 case PART_TYPE_AMIGA:
369 PRINTF ("## Testing for a valid Amiga partition ##\n");
370 print_part_header ("AMIGA", dev_desc);
371 print_part_amiga (dev_desc);
375 #ifdef CONFIG_EFI_PARTITION
377 PRINTF ("## Testing for valid EFI partition ##\n");
378 print_part_header ("EFI", dev_desc);
379 print_part_efi (dev_desc);
383 puts ("## Unknown partition table\n");
386 #endif /* HAVE_BLOCK_DEVICE */
388 int get_partition_info(block_dev_desc_t *dev_desc, int part,
389 disk_partition_t *info)
391 #ifdef HAVE_BLOCK_DEVICE
393 #ifdef CONFIG_PARTITION_UUIDS
394 /* The common case is no UUID support */
398 switch (dev_desc->part_type) {
399 #ifdef CONFIG_MAC_PARTITION
401 if (get_partition_info_mac(dev_desc, part, info) == 0) {
402 PRINTF("## Valid MAC partition found ##\n");
408 #ifdef CONFIG_DOS_PARTITION
410 if (get_partition_info_dos(dev_desc, part, info) == 0) {
411 PRINTF("## Valid DOS partition found ##\n");
417 #ifdef CONFIG_ISO_PARTITION
419 if (get_partition_info_iso(dev_desc, part, info) == 0) {
420 PRINTF("## Valid ISO boot partition found ##\n");
426 #ifdef CONFIG_AMIGA_PARTITION
427 case PART_TYPE_AMIGA:
428 if (get_partition_info_amiga(dev_desc, part, info) == 0) {
429 PRINTF("## Valid Amiga partition found ##\n");
435 #ifdef CONFIG_EFI_PARTITION
437 if (get_partition_info_efi(dev_desc, part, info) == 0) {
438 PRINTF("## Valid EFI partition found ##\n");
446 #endif /* HAVE_BLOCK_DEVICE */
451 int get_device(const char *ifname, const char *dev_hwpart_str,
452 block_dev_desc_t **dev_desc)
455 char *dup_str = NULL;
456 const char *dev_str, *hwpart_str;
459 hwpart_str = strchr(dev_hwpart_str, '.');
461 dup_str = strdup(dev_hwpart_str);
462 dup_str[hwpart_str - dev_hwpart_str] = 0;
466 dev_str = dev_hwpart_str;
470 dev = simple_strtoul(dev_str, &ep, 16);
472 printf("** Bad device specification %s %s **\n",
479 hwpart = simple_strtoul(hwpart_str, &ep, 16);
481 printf("** Bad HW partition specification %s %s **\n",
488 *dev_desc = get_dev_hwpart(ifname, dev, hwpart);
489 if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
490 printf("** Bad device %s %s **\n", ifname, dev_hwpart_str);
500 #define PART_UNSPECIFIED -2
502 #define MAX_SEARCH_PARTITIONS 16
503 int get_device_and_partition(const char *ifname, const char *dev_part_str,
504 block_dev_desc_t **dev_desc,
505 disk_partition_t *info, int allow_whole_dev)
508 const char *part_str;
509 char *dup_str = NULL;
515 disk_partition_t tmpinfo;
518 * Special-case a pseudo block device "hostfs", to allow access to the
519 * host's own filesystem.
521 if (0 == strcmp(ifname, "hostfs")) {
527 strcpy((char *)info->type, BOOT_PART_TYPE);
528 strcpy((char *)info->name, "Sandbox host");
529 #ifdef CONFIG_PARTITION_UUIDS
536 /* If no dev_part_str, use bootdevice environment variable */
537 if (!dev_part_str || !strlen(dev_part_str) ||
538 !strcmp(dev_part_str, "-"))
539 dev_part_str = getenv("bootdevice");
541 /* If still no dev_part_str, it's an error */
543 printf("** No device specified **\n");
547 /* Separate device and partition ID specification */
548 part_str = strchr(dev_part_str, ':');
550 dup_str = strdup(dev_part_str);
551 dup_str[part_str - dev_part_str] = 0;
555 dev_str = dev_part_str;
558 /* Look up the device */
559 dev = get_device(ifname, dev_str, dev_desc);
563 /* Convert partition ID string to number */
564 if (!part_str || !*part_str) {
565 part = PART_UNSPECIFIED;
566 } else if (!strcmp(part_str, "auto")) {
569 /* Something specified -> use exactly that */
570 part = (int)simple_strtoul(part_str, &ep, 16);
572 * Less than whole string converted,
573 * or request for whole device, but caller requires partition.
575 if (*ep || (part == 0 && !allow_whole_dev)) {
576 printf("** Bad partition specification %s %s **\n",
577 ifname, dev_part_str);
583 * No partition table on device,
584 * or user requested partition 0 (entire device).
586 if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
588 if (!(*dev_desc)->lba) {
589 printf("** Bad device size - %s %s **\n", ifname,
595 * If user specified a partition ID other than 0,
596 * or the calling command only accepts partitions,
599 if ((part > 0) || (!allow_whole_dev)) {
600 printf("** No partition table - %s %s **\n", ifname,
605 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
608 info->size = (*dev_desc)->lba;
609 info->blksz = (*dev_desc)->blksz;
611 strcpy((char *)info->type, BOOT_PART_TYPE);
612 strcpy((char *)info->name, "Whole Disk");
613 #ifdef CONFIG_PARTITION_UUIDS
622 * Now there's known to be a partition table,
623 * not specifying a partition means to pick partition 1.
625 if (part == PART_UNSPECIFIED)
629 * If user didn't specify a partition number, or did specify something
630 * other than "auto", use that partition number directly.
632 if (part != PART_AUTO) {
633 ret = get_partition_info(*dev_desc, part, info);
635 printf("** Invalid partition %d **\n", part);
640 * Find the first bootable partition.
641 * If none are bootable, fall back to the first valid partition.
644 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
645 ret = get_partition_info(*dev_desc, p, info);
650 * First valid partition, or new better partition?
651 * If so, save partition ID.
653 if (!part || info->bootable)
656 /* Best possible partition? Stop searching. */
661 * We now need to search further for best possible.
662 * If we what we just queried was the best so far,
663 * save the info since we over-write it next loop.
668 /* If we found any acceptable partition */
671 * If we searched all possible partition IDs,
672 * return the first valid partition we found.
674 if (p == MAX_SEARCH_PARTITIONS + 1)
677 printf("** No valid partitions found **\n");
682 if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
683 printf("** Invalid partition type \"%.32s\""
684 " (expect \"" BOOT_PART_TYPE "\")\n",
690 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);