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);
27 static const struct block_drvr block_drvr[] = {
28 #if defined(CONFIG_CMD_IDE)
29 { .name = "ide", .get_dev = ide_get_dev, },
31 #if defined(CONFIG_CMD_SATA)
32 {.name = "sata", .get_dev = sata_get_dev, },
34 #if defined(CONFIG_CMD_SCSI)
35 { .name = "scsi", .get_dev = scsi_get_dev, },
37 #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
38 { .name = "usb", .get_dev = usb_stor_get_dev, },
40 #if defined(CONFIG_MMC)
41 { .name = "mmc", .get_dev = mmc_get_dev, },
43 #if defined(CONFIG_SYSTEMACE)
44 { .name = "ace", .get_dev = systemace_get_dev, },
46 #if defined(CONFIG_SANDBOX)
47 { .name = "host", .get_dev = host_get_dev, },
52 DECLARE_GLOBAL_DATA_PTR;
54 #ifdef HAVE_BLOCK_DEVICE
55 block_dev_desc_t *get_dev(const char *ifname, int dev)
57 const struct block_drvr *drvr = block_drvr;
58 block_dev_desc_t* (*reloc_get_dev)(int dev);
65 #ifdef CONFIG_NEEDS_MANUAL_RELOC
66 name += gd->reloc_off;
70 reloc_get_dev = drvr->get_dev;
71 #ifdef CONFIG_NEEDS_MANUAL_RELOC
72 name += gd->reloc_off;
73 reloc_get_dev += gd->reloc_off;
75 if (strncmp(ifname, name, strlen(name)) == 0)
76 return reloc_get_dev(dev);
82 block_dev_desc_t *get_dev(const char *ifname, int dev)
88 #ifdef HAVE_BLOCK_DEVICE
90 /* ------------------------------------------------------------------------- */
92 * reports device info to the user
96 typedef uint64_t lba512_t;
98 typedef lbaint_t lba512_t;
102 * Overflowless variant of (block_count * mul_by / div_by)
103 * when div_by > mul_by
105 static lba512_t lba512_muldiv (lba512_t block_count, lba512_t mul_by, lba512_t div_by)
107 lba512_t bc_quot, bc_rem;
109 /* x * m / d == x / d * m + (x % d) * m / d */
110 bc_quot = block_count / div_by;
111 bc_rem = block_count - div_by * bc_quot;
112 return bc_quot * mul_by + (bc_rem * mul_by) / div_by;
115 void dev_print (block_dev_desc_t *dev_desc)
117 lba512_t lba512; /* number of blocks if 512bytes block size */
119 if (dev_desc->type == DEV_TYPE_UNKNOWN) {
120 puts ("not available\n");
124 switch (dev_desc->if_type) {
126 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
127 dev_desc->target,dev_desc->lun,
135 printf ("Model: %s Firm: %s Ser#: %s\n",
143 printf ("Vendor: %s Rev: %s Prod: %s\n",
149 puts("device type DOC\n");
151 case IF_TYPE_UNKNOWN:
152 puts("device type unknown\n");
155 printf("Unhandled device type: %i\n", dev_desc->if_type);
159 if (dev_desc->removable)
161 switch (dev_desc->type & 0x1F) {
162 case DEV_TYPE_HARDDISK:
168 case DEV_TYPE_OPDISK:
169 puts ("Optical Device");
175 printf ("# %02X #", dev_desc->type & 0x1F);
179 if (dev_desc->lba > 0L && dev_desc->blksz > 0L) {
180 ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
185 lba512 = (lba * (dev_desc->blksz/512));
186 /* round to 1 digit */
187 mb = lba512_muldiv(lba512, 10, 2048); /* 2048 = (1024 * 1024) / 512 MB */
190 mb_rem = mb - (10 * mb_quot);
194 gb_rem = gb - (10 * gb_quot);
197 printf (" Supports 48-bit addressing\n");
199 #if defined(CONFIG_SYS_64BIT_LBA)
200 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n",
206 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
213 puts (" Capacity: not available\n");
218 #ifdef HAVE_BLOCK_DEVICE
220 void init_part (block_dev_desc_t * dev_desc)
222 #ifdef CONFIG_ISO_PARTITION
223 if (test_part_iso(dev_desc) == 0) {
224 dev_desc->part_type = PART_TYPE_ISO;
229 #ifdef CONFIG_MAC_PARTITION
230 if (test_part_mac(dev_desc) == 0) {
231 dev_desc->part_type = PART_TYPE_MAC;
236 /* must be placed before DOS partition detection */
237 #ifdef CONFIG_EFI_PARTITION
238 if (test_part_efi(dev_desc) == 0) {
239 dev_desc->part_type = PART_TYPE_EFI;
244 #ifdef CONFIG_DOS_PARTITION
245 if (test_part_dos(dev_desc) == 0) {
246 dev_desc->part_type = PART_TYPE_DOS;
251 #ifdef CONFIG_AMIGA_PARTITION
252 if (test_part_amiga(dev_desc) == 0) {
253 dev_desc->part_type = PART_TYPE_AMIGA;
257 dev_desc->part_type = PART_TYPE_UNKNOWN;
261 #if defined(CONFIG_MAC_PARTITION) || \
262 defined(CONFIG_DOS_PARTITION) || \
263 defined(CONFIG_ISO_PARTITION) || \
264 defined(CONFIG_AMIGA_PARTITION) || \
265 defined(CONFIG_EFI_PARTITION)
267 static void print_part_header (const char *type, block_dev_desc_t * dev_desc)
269 puts ("\nPartition Map for ");
270 switch (dev_desc->if_type) {
299 printf (" device %d -- Partition Type: %s\n\n",
300 dev_desc->dev, type);
303 #endif /* any CONFIG_..._PARTITION */
305 void print_part (block_dev_desc_t * dev_desc)
308 switch (dev_desc->part_type) {
309 #ifdef CONFIG_MAC_PARTITION
311 PRINTF ("## Testing for valid MAC partition ##\n");
312 print_part_header ("MAC", dev_desc);
313 print_part_mac (dev_desc);
316 #ifdef CONFIG_DOS_PARTITION
318 PRINTF ("## Testing for valid DOS partition ##\n");
319 print_part_header ("DOS", dev_desc);
320 print_part_dos (dev_desc);
324 #ifdef CONFIG_ISO_PARTITION
326 PRINTF ("## Testing for valid ISO Boot partition ##\n");
327 print_part_header ("ISO", dev_desc);
328 print_part_iso (dev_desc);
332 #ifdef CONFIG_AMIGA_PARTITION
333 case PART_TYPE_AMIGA:
334 PRINTF ("## Testing for a valid Amiga partition ##\n");
335 print_part_header ("AMIGA", dev_desc);
336 print_part_amiga (dev_desc);
340 #ifdef CONFIG_EFI_PARTITION
342 PRINTF ("## Testing for valid EFI partition ##\n");
343 print_part_header ("EFI", dev_desc);
344 print_part_efi (dev_desc);
348 puts ("## Unknown partition table\n");
351 #endif /* HAVE_BLOCK_DEVICE */
353 int get_partition_info(block_dev_desc_t *dev_desc, int part
354 , disk_partition_t *info)
356 #ifdef HAVE_BLOCK_DEVICE
358 #ifdef CONFIG_PARTITION_UUIDS
359 /* The common case is no UUID support */
363 switch (dev_desc->part_type) {
364 #ifdef CONFIG_MAC_PARTITION
366 if (get_partition_info_mac(dev_desc, part, info) == 0) {
367 PRINTF("## Valid MAC partition found ##\n");
373 #ifdef CONFIG_DOS_PARTITION
375 if (get_partition_info_dos(dev_desc, part, info) == 0) {
376 PRINTF("## Valid DOS partition found ##\n");
382 #ifdef CONFIG_ISO_PARTITION
384 if (get_partition_info_iso(dev_desc, part, info) == 0) {
385 PRINTF("## Valid ISO boot partition found ##\n");
391 #ifdef CONFIG_AMIGA_PARTITION
392 case PART_TYPE_AMIGA:
393 if (get_partition_info_amiga(dev_desc, part, info) == 0) {
394 PRINTF("## Valid Amiga partition found ##\n");
400 #ifdef CONFIG_EFI_PARTITION
402 if (get_partition_info_efi(dev_desc, part, info) == 0) {
403 PRINTF("## Valid EFI partition found ##\n");
411 #endif /* HAVE_BLOCK_DEVICE */
416 int get_device(const char *ifname, const char *dev_str,
417 block_dev_desc_t **dev_desc)
422 dev = simple_strtoul(dev_str, &ep, 16);
424 printf("** Bad device specification %s %s **\n",
429 *dev_desc = get_dev(ifname, dev);
430 if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
431 printf("** Bad device %s %s **\n", ifname, dev_str);
438 #define PART_UNSPECIFIED -2
440 #define MAX_SEARCH_PARTITIONS 16
441 int get_device_and_partition(const char *ifname, const char *dev_part_str,
442 block_dev_desc_t **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;
456 * For now, we have a special case for sandbox, since there is no
457 * real block device support.
459 if (0 == strcmp(ifname, "host")) {
461 info->start = info->size = info->blksz = 0;
463 strcpy((char *)info->type, BOOT_PART_TYPE);
464 strcpy((char *)info->name, "Sandbox host");
465 #ifdef CONFIG_PARTITION_UUIDS
472 /* If no dev_part_str, use bootdevice environment variable */
473 if (!dev_part_str || !strlen(dev_part_str) ||
474 !strcmp(dev_part_str, "-"))
475 dev_part_str = getenv("bootdevice");
477 /* If still no dev_part_str, it's an error */
479 printf("** No device specified **\n");
483 /* Separate device and partition ID specification */
484 part_str = strchr(dev_part_str, ':');
486 dup_str = strdup(dev_part_str);
487 dup_str[part_str - dev_part_str] = 0;
491 dev_str = dev_part_str;
494 /* Look up the device */
495 dev = get_device(ifname, dev_str, dev_desc);
499 /* Convert partition ID string to number */
500 if (!part_str || !*part_str) {
501 part = PART_UNSPECIFIED;
502 } else if (!strcmp(part_str, "auto")) {
505 /* Something specified -> use exactly that */
506 part = (int)simple_strtoul(part_str, &ep, 16);
508 * Less than whole string converted,
509 * or request for whole device, but caller requires partition.
511 if (*ep || (part == 0 && !allow_whole_dev)) {
512 printf("** Bad partition specification %s %s **\n",
513 ifname, dev_part_str);
519 * No partition table on device,
520 * or user requested partition 0 (entire device).
522 if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
524 if (!(*dev_desc)->lba) {
525 printf("** Bad device size - %s %s **\n", ifname,
531 * If user specified a partition ID other than 0,
532 * or the calling command only accepts partitions,
535 if ((part > 0) || (!allow_whole_dev)) {
536 printf("** No partition table - %s %s **\n", ifname,
541 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
544 info->size = (*dev_desc)->lba;
545 info->blksz = (*dev_desc)->blksz;
547 strcpy((char *)info->type, BOOT_PART_TYPE);
548 strcpy((char *)info->name, "Whole Disk");
549 #ifdef CONFIG_PARTITION_UUIDS
558 * Now there's known to be a partition table,
559 * not specifying a partition means to pick partition 1.
561 if (part == PART_UNSPECIFIED)
565 * If user didn't specify a partition number, or did specify something
566 * other than "auto", use that partition number directly.
568 if (part != PART_AUTO) {
569 ret = get_partition_info(*dev_desc, part, info);
571 printf("** Invalid partition %d **\n", part);
576 * Find the first bootable partition.
577 * If none are bootable, fall back to the first valid partition.
580 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
581 ret = get_partition_info(*dev_desc, p, info);
586 * First valid partition, or new better partition?
587 * If so, save partition ID.
589 if (!part || info->bootable)
592 /* Best possible partition? Stop searching. */
597 * We now need to search further for best possible.
598 * If we what we just queried was the best so far,
599 * save the info since we over-write it next loop.
604 /* If we found any acceptable partition */
607 * If we searched all possible partition IDs,
608 * return the first valid partition we found.
610 if (p == MAX_SEARCH_PARTITIONS + 1)
613 printf("** No valid partitions found **\n");
618 if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
619 printf("** Invalid partition type \"%.32s\""
620 " (expect \"" BOOT_PART_TYPE "\")\n",
626 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);