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, },
49 DECLARE_GLOBAL_DATA_PTR;
51 #ifdef HAVE_BLOCK_DEVICE
52 block_dev_desc_t *get_dev(const char *ifname, int dev)
54 const struct block_drvr *drvr = block_drvr;
55 block_dev_desc_t* (*reloc_get_dev)(int dev);
62 #ifdef CONFIG_NEEDS_MANUAL_RELOC
63 name += gd->reloc_off;
67 reloc_get_dev = drvr->get_dev;
68 #ifdef CONFIG_NEEDS_MANUAL_RELOC
69 name += gd->reloc_off;
70 reloc_get_dev += gd->reloc_off;
72 if (strncmp(ifname, name, strlen(name)) == 0)
73 return reloc_get_dev(dev);
79 block_dev_desc_t *get_dev(const char *ifname, int dev)
85 #ifdef HAVE_BLOCK_DEVICE
87 /* ------------------------------------------------------------------------- */
89 * reports device info to the user
93 typedef uint64_t lba512_t;
95 typedef lbaint_t lba512_t;
99 * Overflowless variant of (block_count * mul_by / div_by)
100 * when div_by > mul_by
102 static lba512_t lba512_muldiv (lba512_t block_count, lba512_t mul_by, lba512_t div_by)
104 lba512_t bc_quot, bc_rem;
106 /* x * m / d == x / d * m + (x % d) * m / d */
107 bc_quot = block_count / div_by;
108 bc_rem = block_count - div_by * bc_quot;
109 return bc_quot * mul_by + (bc_rem * mul_by) / div_by;
112 void dev_print (block_dev_desc_t *dev_desc)
114 lba512_t lba512; /* number of blocks if 512bytes block size */
116 if (dev_desc->type == DEV_TYPE_UNKNOWN) {
117 puts ("not available\n");
121 switch (dev_desc->if_type) {
123 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
124 dev_desc->target,dev_desc->lun,
132 printf ("Model: %s Firm: %s Ser#: %s\n",
140 printf ("Vendor: %s Rev: %s Prod: %s\n",
146 puts("device type DOC\n");
148 case IF_TYPE_UNKNOWN:
149 puts("device type unknown\n");
152 printf("Unhandled device type: %i\n", dev_desc->if_type);
156 if (dev_desc->removable)
158 switch (dev_desc->type & 0x1F) {
159 case DEV_TYPE_HARDDISK:
165 case DEV_TYPE_OPDISK:
166 puts ("Optical Device");
172 printf ("# %02X #", dev_desc->type & 0x1F);
176 if (dev_desc->lba > 0L && dev_desc->blksz > 0L) {
177 ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
182 lba512 = (lba * (dev_desc->blksz/512));
183 /* round to 1 digit */
184 mb = lba512_muldiv(lba512, 10, 2048); /* 2048 = (1024 * 1024) / 512 MB */
187 mb_rem = mb - (10 * mb_quot);
191 gb_rem = gb - (10 * gb_quot);
194 printf (" Supports 48-bit addressing\n");
196 #if defined(CONFIG_SYS_64BIT_LBA)
197 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n",
203 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
210 puts (" Capacity: not available\n");
215 #ifdef HAVE_BLOCK_DEVICE
217 void init_part (block_dev_desc_t * dev_desc)
219 #ifdef CONFIG_ISO_PARTITION
220 if (test_part_iso(dev_desc) == 0) {
221 dev_desc->part_type = PART_TYPE_ISO;
226 #ifdef CONFIG_MAC_PARTITION
227 if (test_part_mac(dev_desc) == 0) {
228 dev_desc->part_type = PART_TYPE_MAC;
233 /* must be placed before DOS partition detection */
234 #ifdef CONFIG_EFI_PARTITION
235 if (test_part_efi(dev_desc) == 0) {
236 dev_desc->part_type = PART_TYPE_EFI;
241 #ifdef CONFIG_DOS_PARTITION
242 if (test_part_dos(dev_desc) == 0) {
243 dev_desc->part_type = PART_TYPE_DOS;
248 #ifdef CONFIG_AMIGA_PARTITION
249 if (test_part_amiga(dev_desc) == 0) {
250 dev_desc->part_type = PART_TYPE_AMIGA;
254 dev_desc->part_type = PART_TYPE_UNKNOWN;
258 #if defined(CONFIG_MAC_PARTITION) || \
259 defined(CONFIG_DOS_PARTITION) || \
260 defined(CONFIG_ISO_PARTITION) || \
261 defined(CONFIG_AMIGA_PARTITION) || \
262 defined(CONFIG_EFI_PARTITION)
264 static void print_part_header (const char *type, block_dev_desc_t * dev_desc)
266 puts ("\nPartition Map for ");
267 switch (dev_desc->if_type) {
293 printf (" device %d -- Partition Type: %s\n\n",
294 dev_desc->dev, type);
297 #endif /* any CONFIG_..._PARTITION */
299 void print_part (block_dev_desc_t * dev_desc)
302 switch (dev_desc->part_type) {
303 #ifdef CONFIG_MAC_PARTITION
305 PRINTF ("## Testing for valid MAC partition ##\n");
306 print_part_header ("MAC", dev_desc);
307 print_part_mac (dev_desc);
310 #ifdef CONFIG_DOS_PARTITION
312 PRINTF ("## Testing for valid DOS partition ##\n");
313 print_part_header ("DOS", dev_desc);
314 print_part_dos (dev_desc);
318 #ifdef CONFIG_ISO_PARTITION
320 PRINTF ("## Testing for valid ISO Boot partition ##\n");
321 print_part_header ("ISO", dev_desc);
322 print_part_iso (dev_desc);
326 #ifdef CONFIG_AMIGA_PARTITION
327 case PART_TYPE_AMIGA:
328 PRINTF ("## Testing for a valid Amiga partition ##\n");
329 print_part_header ("AMIGA", dev_desc);
330 print_part_amiga (dev_desc);
334 #ifdef CONFIG_EFI_PARTITION
336 PRINTF ("## Testing for valid EFI partition ##\n");
337 print_part_header ("EFI", dev_desc);
338 print_part_efi (dev_desc);
342 puts ("## Unknown partition table\n");
345 #endif /* HAVE_BLOCK_DEVICE */
347 int get_partition_info(block_dev_desc_t *dev_desc, int part
348 , disk_partition_t *info)
350 #ifdef HAVE_BLOCK_DEVICE
352 #ifdef CONFIG_PARTITION_UUIDS
353 /* The common case is no UUID support */
357 switch (dev_desc->part_type) {
358 #ifdef CONFIG_MAC_PARTITION
360 if (get_partition_info_mac(dev_desc, part, info) == 0) {
361 PRINTF("## Valid MAC partition found ##\n");
367 #ifdef CONFIG_DOS_PARTITION
369 if (get_partition_info_dos(dev_desc, part, info) == 0) {
370 PRINTF("## Valid DOS partition found ##\n");
376 #ifdef CONFIG_ISO_PARTITION
378 if (get_partition_info_iso(dev_desc, part, info) == 0) {
379 PRINTF("## Valid ISO boot partition found ##\n");
385 #ifdef CONFIG_AMIGA_PARTITION
386 case PART_TYPE_AMIGA:
387 if (get_partition_info_amiga(dev_desc, part, info) == 0) {
388 PRINTF("## Valid Amiga partition found ##\n");
394 #ifdef CONFIG_EFI_PARTITION
396 if (get_partition_info_efi(dev_desc, part, info) == 0) {
397 PRINTF("## Valid EFI partition found ##\n");
405 #endif /* HAVE_BLOCK_DEVICE */
410 int get_device(const char *ifname, const char *dev_str,
411 block_dev_desc_t **dev_desc)
416 dev = simple_strtoul(dev_str, &ep, 16);
418 printf("** Bad device specification %s %s **\n",
423 *dev_desc = get_dev(ifname, dev);
424 if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
425 printf("** Bad device %s %s **\n", ifname, dev_str);
432 #define PART_UNSPECIFIED -2
434 #define MAX_SEARCH_PARTITIONS 16
435 int get_device_and_partition(const char *ifname, const char *dev_part_str,
436 block_dev_desc_t **dev_desc,
437 disk_partition_t *info, int allow_whole_dev)
440 const char *part_str;
441 char *dup_str = NULL;
447 disk_partition_t tmpinfo;
450 * For now, we have a special case for sandbox, since there is no
451 * real block device support.
453 if (0 == strcmp(ifname, "host")) {
455 info->start = info->size = info->blksz = 0;
457 strcpy((char *)info->type, BOOT_PART_TYPE);
458 strcpy((char *)info->name, "Sandbox host");
459 #ifdef CONFIG_PARTITION_UUIDS
466 /* If no dev_part_str, use bootdevice environment variable */
467 if (!dev_part_str || !strlen(dev_part_str) ||
468 !strcmp(dev_part_str, "-"))
469 dev_part_str = getenv("bootdevice");
471 /* If still no dev_part_str, it's an error */
473 printf("** No device specified **\n");
477 /* Separate device and partition ID specification */
478 part_str = strchr(dev_part_str, ':');
480 dup_str = strdup(dev_part_str);
481 dup_str[part_str - dev_part_str] = 0;
485 dev_str = dev_part_str;
488 /* Look up the device */
489 dev = get_device(ifname, dev_str, dev_desc);
493 /* Convert partition ID string to number */
494 if (!part_str || !*part_str) {
495 part = PART_UNSPECIFIED;
496 } else if (!strcmp(part_str, "auto")) {
499 /* Something specified -> use exactly that */
500 part = (int)simple_strtoul(part_str, &ep, 16);
502 * Less than whole string converted,
503 * or request for whole device, but caller requires partition.
505 if (*ep || (part == 0 && !allow_whole_dev)) {
506 printf("** Bad partition specification %s %s **\n",
507 ifname, dev_part_str);
513 * No partition table on device,
514 * or user requested partition 0 (entire device).
516 if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
518 if (!(*dev_desc)->lba) {
519 printf("** Bad device size - %s %s **\n", ifname,
525 * If user specified a partition ID other than 0,
526 * or the calling command only accepts partitions,
529 if ((part > 0) || (!allow_whole_dev)) {
530 printf("** No partition table - %s %s **\n", ifname,
535 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
538 info->size = (*dev_desc)->lba;
539 info->blksz = (*dev_desc)->blksz;
541 strcpy((char *)info->type, BOOT_PART_TYPE);
542 strcpy((char *)info->name, "Whole Disk");
543 #ifdef CONFIG_PARTITION_UUIDS
552 * Now there's known to be a partition table,
553 * not specifying a partition means to pick partition 1.
555 if (part == PART_UNSPECIFIED)
559 * If user didn't specify a partition number, or did specify something
560 * other than "auto", use that partition number directly.
562 if (part != PART_AUTO) {
563 ret = get_partition_info(*dev_desc, part, info);
565 printf("** Invalid partition %d **\n", part);
570 * Find the first bootable partition.
571 * If none are bootable, fall back to the first valid partition.
574 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
575 ret = get_partition_info(*dev_desc, p, info);
580 * First valid partition, or new better partition?
581 * If so, save partition ID.
583 if (!part || info->bootable)
586 /* Best possible partition? Stop searching. */
591 * We now need to search further for best possible.
592 * If we what we just queried was the best so far,
593 * save the info since we over-write it next loop.
598 /* If we found any acceptable partition */
601 * If we searched all possible partition IDs,
602 * return the first valid partition we found.
604 if (p == MAX_SEARCH_PARTITIONS + 1)
607 printf("** No valid partitions found **\n");
612 if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
613 printf("** Invalid partition type \"%.32s\""
614 " (expect \"" BOOT_PART_TYPE "\")\n",
620 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);