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_SATA)
33 {.name = "sata", .get_dev = sata_get_dev, },
35 #if defined(CONFIG_CMD_SCSI)
36 { .name = "scsi", .get_dev = scsi_get_dev, },
38 #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
39 { .name = "usb", .get_dev = usb_stor_get_dev, },
41 #if defined(CONFIG_MMC)
42 { .name = "mmc", .get_dev = mmc_get_dev, },
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 block_dev_desc_t *get_dev_hwpart(const char *ifname, int dev, int hwpart)
58 const struct block_drvr *drvr = block_drvr;
59 block_dev_desc_t* (*reloc_get_dev)(int dev);
60 int (*select_hwpart)(int dev_num, int hwpart);
68 #ifdef CONFIG_NEEDS_MANUAL_RELOC
69 name += gd->reloc_off;
73 reloc_get_dev = drvr->get_dev;
74 select_hwpart = drvr->select_hwpart;
75 #ifdef CONFIG_NEEDS_MANUAL_RELOC
76 name += gd->reloc_off;
77 reloc_get_dev += gd->reloc_off;
79 select_hwpart += gd->reloc_off;
81 if (strncmp(ifname, name, strlen(name)) == 0) {
82 block_dev_desc_t *dev_desc = reloc_get_dev(dev);
89 ret = select_hwpart(dev_desc->dev, hwpart);
99 block_dev_desc_t *get_dev(const char *ifname, int dev)
101 return get_dev_hwpart(ifname, dev, -1);
104 block_dev_desc_t *get_dev_hwpart(const char *ifname, int dev, int hwpart)
109 block_dev_desc_t *get_dev(const char *ifname, int dev)
115 #ifdef HAVE_BLOCK_DEVICE
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 / div_by)
130 * when div_by > mul_by
132 static lba512_t lba512_muldiv (lba512_t block_count, lba512_t mul_by, lba512_t div_by)
134 lba512_t bc_quot, bc_rem;
136 /* x * m / d == x / d * m + (x % d) * m / d */
137 bc_quot = block_count / div_by;
138 bc_rem = block_count - div_by * bc_quot;
139 return bc_quot * mul_by + (bc_rem * mul_by) / div_by;
142 void dev_print (block_dev_desc_t *dev_desc)
144 lba512_t lba512; /* number of blocks if 512bytes block size */
146 if (dev_desc->type == DEV_TYPE_UNKNOWN) {
147 puts ("not available\n");
151 switch (dev_desc->if_type) {
153 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
154 dev_desc->target,dev_desc->lun,
162 printf ("Model: %s Firm: %s Ser#: %s\n",
170 printf ("Vendor: %s Rev: %s Prod: %s\n",
176 puts("device type DOC\n");
178 case IF_TYPE_UNKNOWN:
179 puts("device type unknown\n");
182 printf("Unhandled device type: %i\n", dev_desc->if_type);
186 if (dev_desc->removable)
188 switch (dev_desc->type & 0x1F) {
189 case DEV_TYPE_HARDDISK:
195 case DEV_TYPE_OPDISK:
196 puts ("Optical Device");
202 printf ("# %02X #", dev_desc->type & 0x1F);
206 if (dev_desc->lba > 0L && dev_desc->blksz > 0L) {
207 ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
212 lba512 = (lba * (dev_desc->blksz/512));
213 /* round to 1 digit */
214 mb = lba512_muldiv(lba512, 10, 2048); /* 2048 = (1024 * 1024) / 512 MB */
217 mb_rem = mb - (10 * mb_quot);
221 gb_rem = gb - (10 * gb_quot);
224 printf (" Supports 48-bit addressing\n");
226 #if defined(CONFIG_SYS_64BIT_LBA)
227 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n",
233 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
240 puts (" Capacity: not available\n");
245 #ifdef HAVE_BLOCK_DEVICE
247 void init_part (block_dev_desc_t * dev_desc)
249 #ifdef CONFIG_ISO_PARTITION
250 if (test_part_iso(dev_desc) == 0) {
251 dev_desc->part_type = PART_TYPE_ISO;
256 #ifdef CONFIG_MAC_PARTITION
257 if (test_part_mac(dev_desc) == 0) {
258 dev_desc->part_type = PART_TYPE_MAC;
263 /* must be placed before DOS partition detection */
264 #ifdef CONFIG_EFI_PARTITION
265 if (test_part_efi(dev_desc) == 0) {
266 dev_desc->part_type = PART_TYPE_EFI;
271 #ifdef CONFIG_DOS_PARTITION
272 if (test_part_dos(dev_desc) == 0) {
273 dev_desc->part_type = PART_TYPE_DOS;
278 #ifdef CONFIG_AMIGA_PARTITION
279 if (test_part_amiga(dev_desc) == 0) {
280 dev_desc->part_type = PART_TYPE_AMIGA;
284 dev_desc->part_type = PART_TYPE_UNKNOWN;
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)
294 static void print_part_header (const char *type, block_dev_desc_t * dev_desc)
296 puts ("\nPartition Map for ");
297 switch (dev_desc->if_type) {
326 printf (" device %d -- Partition Type: %s\n\n",
327 dev_desc->dev, type);
330 #endif /* any CONFIG_..._PARTITION */
332 void print_part (block_dev_desc_t * dev_desc)
335 switch (dev_desc->part_type) {
336 #ifdef CONFIG_MAC_PARTITION
338 PRINTF ("## Testing for valid MAC partition ##\n");
339 print_part_header ("MAC", dev_desc);
340 print_part_mac (dev_desc);
343 #ifdef CONFIG_DOS_PARTITION
345 PRINTF ("## Testing for valid DOS partition ##\n");
346 print_part_header ("DOS", dev_desc);
347 print_part_dos (dev_desc);
351 #ifdef CONFIG_ISO_PARTITION
353 PRINTF ("## Testing for valid ISO Boot partition ##\n");
354 print_part_header ("ISO", dev_desc);
355 print_part_iso (dev_desc);
359 #ifdef CONFIG_AMIGA_PARTITION
360 case PART_TYPE_AMIGA:
361 PRINTF ("## Testing for a valid Amiga partition ##\n");
362 print_part_header ("AMIGA", dev_desc);
363 print_part_amiga (dev_desc);
367 #ifdef CONFIG_EFI_PARTITION
369 PRINTF ("## Testing for valid EFI partition ##\n");
370 print_part_header ("EFI", dev_desc);
371 print_part_efi (dev_desc);
375 puts ("## Unknown partition table\n");
378 #endif /* HAVE_BLOCK_DEVICE */
380 int get_partition_info(block_dev_desc_t *dev_desc, int part
381 , disk_partition_t *info)
383 #ifdef HAVE_BLOCK_DEVICE
385 #ifdef CONFIG_PARTITION_UUIDS
386 /* The common case is no UUID support */
390 switch (dev_desc->part_type) {
391 #ifdef CONFIG_MAC_PARTITION
393 if (get_partition_info_mac(dev_desc, part, info) == 0) {
394 PRINTF("## Valid MAC partition found ##\n");
400 #ifdef CONFIG_DOS_PARTITION
402 if (get_partition_info_dos(dev_desc, part, info) == 0) {
403 PRINTF("## Valid DOS partition found ##\n");
409 #ifdef CONFIG_ISO_PARTITION
411 if (get_partition_info_iso(dev_desc, part, info) == 0) {
412 PRINTF("## Valid ISO boot partition found ##\n");
418 #ifdef CONFIG_AMIGA_PARTITION
419 case PART_TYPE_AMIGA:
420 if (get_partition_info_amiga(dev_desc, part, info) == 0) {
421 PRINTF("## Valid Amiga partition found ##\n");
427 #ifdef CONFIG_EFI_PARTITION
429 if (get_partition_info_efi(dev_desc, part, info) == 0) {
430 PRINTF("## Valid EFI partition found ##\n");
438 #endif /* HAVE_BLOCK_DEVICE */
443 int get_device(const char *ifname, const char *dev_hwpart_str,
444 block_dev_desc_t **dev_desc)
447 char *dup_str = NULL;
448 const char *dev_str, *hwpart_str;
451 hwpart_str = strchr(dev_hwpart_str, '.');
453 dup_str = strdup(dev_hwpart_str);
454 dup_str[hwpart_str - dev_hwpart_str] = 0;
458 dev_str = dev_hwpart_str;
462 dev = simple_strtoul(dev_str, &ep, 16);
464 printf("** Bad device specification %s %s **\n",
471 hwpart = simple_strtoul(hwpart_str, &ep, 16);
473 printf("** Bad HW partition specification %s %s **\n",
480 *dev_desc = get_dev_hwpart(ifname, dev, hwpart);
481 if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
482 printf("** Bad device %s %s **\n", ifname, dev_hwpart_str);
492 #define PART_UNSPECIFIED -2
494 #define MAX_SEARCH_PARTITIONS 16
495 int get_device_and_partition(const char *ifname, const char *dev_part_str,
496 block_dev_desc_t **dev_desc,
497 disk_partition_t *info, int allow_whole_dev)
500 const char *part_str;
501 char *dup_str = NULL;
507 disk_partition_t tmpinfo;
509 /* If no dev_part_str, use bootdevice environment variable */
510 if (!dev_part_str || !strlen(dev_part_str) ||
511 !strcmp(dev_part_str, "-"))
512 dev_part_str = getenv("bootdevice");
514 /* If still no dev_part_str, it's an error */
516 printf("** No device specified **\n");
520 /* Separate device and partition ID specification */
521 part_str = strchr(dev_part_str, ':');
523 dup_str = strdup(dev_part_str);
524 dup_str[part_str - dev_part_str] = 0;
528 dev_str = dev_part_str;
531 /* Look up the device */
532 dev = get_device(ifname, dev_str, dev_desc);
536 /* Convert partition ID string to number */
537 if (!part_str || !*part_str) {
538 part = PART_UNSPECIFIED;
539 } else if (!strcmp(part_str, "auto")) {
542 /* Something specified -> use exactly that */
543 part = (int)simple_strtoul(part_str, &ep, 16);
545 * Less than whole string converted,
546 * or request for whole device, but caller requires partition.
548 if (*ep || (part == 0 && !allow_whole_dev)) {
549 printf("** Bad partition specification %s %s **\n",
550 ifname, dev_part_str);
556 * No partition table on device,
557 * or user requested partition 0 (entire device).
559 if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
561 if (!(*dev_desc)->lba) {
562 printf("** Bad device size - %s %s **\n", ifname,
568 * If user specified a partition ID other than 0,
569 * or the calling command only accepts partitions,
572 if ((part > 0) || (!allow_whole_dev)) {
573 printf("** No partition table - %s %s **\n", ifname,
578 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
581 info->size = (*dev_desc)->lba;
582 info->blksz = (*dev_desc)->blksz;
584 strcpy((char *)info->type, BOOT_PART_TYPE);
585 strcpy((char *)info->name, "Whole Disk");
586 #ifdef CONFIG_PARTITION_UUIDS
595 * Now there's known to be a partition table,
596 * not specifying a partition means to pick partition 1.
598 if (part == PART_UNSPECIFIED)
602 * If user didn't specify a partition number, or did specify something
603 * other than "auto", use that partition number directly.
605 if (part != PART_AUTO) {
606 ret = get_partition_info(*dev_desc, part, info);
608 printf("** Invalid partition %d **\n", part);
613 * Find the first bootable partition.
614 * If none are bootable, fall back to the first valid partition.
617 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
618 ret = get_partition_info(*dev_desc, p, info);
623 * First valid partition, or new better partition?
624 * If so, save partition ID.
626 if (!part || info->bootable)
629 /* Best possible partition? Stop searching. */
634 * We now need to search further for best possible.
635 * If we what we just queried was the best so far,
636 * save the info since we over-write it next loop.
641 /* If we found any acceptable partition */
644 * If we searched all possible partition IDs,
645 * return the first valid partition we found.
647 if (p == MAX_SEARCH_PARTITIONS + 1)
650 printf("** No valid partitions found **\n");
655 if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
656 printf("** Invalid partition type \"%.32s\""
657 " (expect \"" BOOT_PART_TYPE "\")\n",
663 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);