3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
33 #define PRINTF(fmt,args...) printf (fmt ,##args)
35 #define PRINTF(fmt,args...)
38 /* Rather than repeat this expression each time, add a define for it */
39 #if (defined(CONFIG_CMD_IDE) || \
40 defined(CONFIG_CMD_SATA) || \
41 defined(CONFIG_CMD_SCSI) || \
42 defined(CONFIG_CMD_USB) || \
43 defined(CONFIG_MMC) || \
44 defined(CONFIG_SYSTEMACE) )
45 #define HAVE_BLOCK_DEVICE
50 block_dev_desc_t* (*get_dev)(int dev);
53 static const struct block_drvr block_drvr[] = {
54 #if defined(CONFIG_CMD_IDE)
55 { .name = "ide", .get_dev = ide_get_dev, },
57 #if defined(CONFIG_CMD_SATA)
58 {.name = "sata", .get_dev = sata_get_dev, },
60 #if defined(CONFIG_CMD_SCSI)
61 { .name = "scsi", .get_dev = scsi_get_dev, },
63 #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
64 { .name = "usb", .get_dev = usb_stor_get_dev, },
66 #if defined(CONFIG_MMC)
67 { .name = "mmc", .get_dev = mmc_get_dev, },
69 #if defined(CONFIG_SYSTEMACE)
70 { .name = "ace", .get_dev = systemace_get_dev, },
75 DECLARE_GLOBAL_DATA_PTR;
77 #ifdef HAVE_BLOCK_DEVICE
78 block_dev_desc_t *get_dev(const char *ifname, int dev)
80 const struct block_drvr *drvr = block_drvr;
81 block_dev_desc_t* (*reloc_get_dev)(int dev);
88 #ifdef CONFIG_NEEDS_MANUAL_RELOC
89 name += gd->reloc_off;
93 reloc_get_dev = drvr->get_dev;
94 #ifdef CONFIG_NEEDS_MANUAL_RELOC
95 name += gd->reloc_off;
96 reloc_get_dev += gd->reloc_off;
98 if (strncmp(ifname, name, strlen(name)) == 0)
99 return reloc_get_dev(dev);
105 block_dev_desc_t *get_dev(const char *ifname, int dev)
111 #ifdef HAVE_BLOCK_DEVICE
113 /* ------------------------------------------------------------------------- */
115 * reports device info to the user
119 typedef uint64_t lba512_t;
121 typedef lbaint_t lba512_t;
125 * Overflowless variant of (block_count * mul_by / div_by)
126 * when div_by > mul_by
128 static lba512_t lba512_muldiv (lba512_t block_count, lba512_t mul_by, lba512_t div_by)
130 lba512_t bc_quot, bc_rem;
132 /* x * m / d == x / d * m + (x % d) * m / d */
133 bc_quot = block_count / div_by;
134 bc_rem = block_count - div_by * bc_quot;
135 return bc_quot * mul_by + (bc_rem * mul_by) / div_by;
138 void dev_print (block_dev_desc_t *dev_desc)
140 lba512_t lba512; /* number of blocks if 512bytes block size */
142 if (dev_desc->type == DEV_TYPE_UNKNOWN) {
143 puts ("not available\n");
147 switch (dev_desc->if_type) {
149 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
150 dev_desc->target,dev_desc->lun,
158 printf ("Model: %s Firm: %s Ser#: %s\n",
166 printf ("Vendor: %s Rev: %s Prod: %s\n",
172 puts("device type DOC\n");
174 case IF_TYPE_UNKNOWN:
175 puts("device type unknown\n");
178 printf("Unhandled device type: %i\n", dev_desc->if_type);
182 if (dev_desc->removable)
184 switch (dev_desc->type & 0x1F) {
185 case DEV_TYPE_HARDDISK:
191 case DEV_TYPE_OPDISK:
192 puts ("Optical Device");
198 printf ("# %02X #", dev_desc->type & 0x1F);
202 if (dev_desc->lba > 0L && dev_desc->blksz > 0L) {
203 ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
208 lba512 = (lba * (dev_desc->blksz/512));
209 /* round to 1 digit */
210 mb = lba512_muldiv(lba512, 10, 2048); /* 2048 = (1024 * 1024) / 512 MB */
213 mb_rem = mb - (10 * mb_quot);
217 gb_rem = gb - (10 * gb_quot);
220 printf (" Supports 48-bit addressing\n");
222 #if defined(CONFIG_SYS_64BIT_LBA)
223 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n",
229 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
236 puts (" Capacity: not available\n");
241 #ifdef HAVE_BLOCK_DEVICE
243 void init_part (block_dev_desc_t * dev_desc)
245 #ifdef CONFIG_ISO_PARTITION
246 if (test_part_iso(dev_desc) == 0) {
247 dev_desc->part_type = PART_TYPE_ISO;
252 #ifdef CONFIG_MAC_PARTITION
253 if (test_part_mac(dev_desc) == 0) {
254 dev_desc->part_type = PART_TYPE_MAC;
259 /* must be placed before DOS partition detection */
260 #ifdef CONFIG_EFI_PARTITION
261 if (test_part_efi(dev_desc) == 0) {
262 dev_desc->part_type = PART_TYPE_EFI;
267 #ifdef CONFIG_DOS_PARTITION
268 if (test_part_dos(dev_desc) == 0) {
269 dev_desc->part_type = PART_TYPE_DOS;
274 #ifdef CONFIG_AMIGA_PARTITION
275 if (test_part_amiga(dev_desc) == 0) {
276 dev_desc->part_type = PART_TYPE_AMIGA;
280 dev_desc->part_type = PART_TYPE_UNKNOWN;
284 #if defined(CONFIG_MAC_PARTITION) || \
285 defined(CONFIG_DOS_PARTITION) || \
286 defined(CONFIG_ISO_PARTITION) || \
287 defined(CONFIG_AMIGA_PARTITION) || \
288 defined(CONFIG_EFI_PARTITION)
290 static void print_part_header (const char *type, block_dev_desc_t * dev_desc)
292 puts ("\nPartition Map for ");
293 switch (dev_desc->if_type) {
319 printf (" device %d -- Partition Type: %s\n\n",
320 dev_desc->dev, type);
323 #endif /* any CONFIG_..._PARTITION */
325 void print_part (block_dev_desc_t * dev_desc)
328 switch (dev_desc->part_type) {
329 #ifdef CONFIG_MAC_PARTITION
331 PRINTF ("## Testing for valid MAC partition ##\n");
332 print_part_header ("MAC", dev_desc);
333 print_part_mac (dev_desc);
336 #ifdef CONFIG_DOS_PARTITION
338 PRINTF ("## Testing for valid DOS partition ##\n");
339 print_part_header ("DOS", dev_desc);
340 print_part_dos (dev_desc);
344 #ifdef CONFIG_ISO_PARTITION
346 PRINTF ("## Testing for valid ISO Boot partition ##\n");
347 print_part_header ("ISO", dev_desc);
348 print_part_iso (dev_desc);
352 #ifdef CONFIG_AMIGA_PARTITION
353 case PART_TYPE_AMIGA:
354 PRINTF ("## Testing for a valid Amiga partition ##\n");
355 print_part_header ("AMIGA", dev_desc);
356 print_part_amiga (dev_desc);
360 #ifdef CONFIG_EFI_PARTITION
362 PRINTF ("## Testing for valid EFI partition ##\n");
363 print_part_header ("EFI", dev_desc);
364 print_part_efi (dev_desc);
368 puts ("## Unknown partition table\n");
371 #endif /* HAVE_BLOCK_DEVICE */
373 int get_partition_info(block_dev_desc_t *dev_desc, int part
374 , disk_partition_t *info)
376 #ifdef HAVE_BLOCK_DEVICE
378 #ifdef CONFIG_PARTITION_UUIDS
379 /* The common case is no UUID support */
383 switch (dev_desc->part_type) {
384 #ifdef CONFIG_MAC_PARTITION
386 if (get_partition_info_mac(dev_desc, part, info) == 0) {
387 PRINTF("## Valid MAC partition found ##\n");
393 #ifdef CONFIG_DOS_PARTITION
395 if (get_partition_info_dos(dev_desc, part, info) == 0) {
396 PRINTF("## Valid DOS partition found ##\n");
402 #ifdef CONFIG_ISO_PARTITION
404 if (get_partition_info_iso(dev_desc, part, info) == 0) {
405 PRINTF("## Valid ISO boot partition found ##\n");
411 #ifdef CONFIG_AMIGA_PARTITION
412 case PART_TYPE_AMIGA:
413 if (get_partition_info_amiga(dev_desc, part, info) == 0) {
414 PRINTF("## Valid Amiga partition found ##\n");
420 #ifdef CONFIG_EFI_PARTITION
422 if (get_partition_info_efi(dev_desc, part, info) == 0) {
423 PRINTF("## Valid EFI partition found ##\n");
431 #endif /* HAVE_BLOCK_DEVICE */
436 int get_device(const char *ifname, const char *dev_str,
437 block_dev_desc_t **dev_desc)
442 dev = simple_strtoul(dev_str, &ep, 16);
444 printf("** Bad device specification %s %s **\n",
449 *dev_desc = get_dev(ifname, dev);
450 if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
451 printf("** Bad device %s %s **\n", ifname, dev_str);
458 #define PART_UNSPECIFIED -2
460 #define MAX_SEARCH_PARTITIONS 16
461 int get_device_and_partition(const char *ifname, const char *dev_part_str,
462 block_dev_desc_t **dev_desc,
463 disk_partition_t *info, int allow_whole_dev)
466 const char *part_str;
467 char *dup_str = NULL;
473 disk_partition_t tmpinfo;
475 /* If no dev_part_str, use bootdevice environment variable */
476 if (!dev_part_str || !strlen(dev_part_str) ||
477 !strcmp(dev_part_str, "-"))
478 dev_part_str = getenv("bootdevice");
480 /* If still no dev_part_str, it's an error */
482 printf("** No device specified **\n");
486 /* Separate device and partition ID specification */
487 part_str = strchr(dev_part_str, ':');
489 dup_str = strdup(dev_part_str);
490 dup_str[part_str - dev_part_str] = 0;
494 dev_str = dev_part_str;
497 /* Look up the device */
498 dev = get_device(ifname, dev_str, dev_desc);
502 /* Convert partition ID string to number */
503 if (!part_str || !*part_str) {
504 part = PART_UNSPECIFIED;
505 } else if (!strcmp(part_str, "auto")) {
508 /* Something specified -> use exactly that */
509 part = (int)simple_strtoul(part_str, &ep, 16);
511 * Less than whole string converted,
512 * or request for whole device, but caller requires partition.
514 if (*ep || (part == 0 && !allow_whole_dev)) {
515 printf("** Bad partition specification %s %s **\n",
516 ifname, dev_part_str);
522 * No partition table on device,
523 * or user requested partition 0 (entire device).
525 if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
527 if (!(*dev_desc)->lba) {
528 printf("** Bad device size - %s %s **\n", ifname,
534 * If user specified a partition ID other than 0,
535 * or the calling command only accepts partitions,
538 if ((part > 0) || (!allow_whole_dev)) {
539 printf("** No partition table - %s %s **\n", ifname,
545 info->size = (*dev_desc)->lba;
546 info->blksz = (*dev_desc)->blksz;
548 strcpy((char *)info->type, BOOT_PART_TYPE);
549 strcpy((char *)info->name, "Whole Disk");
550 #ifdef CONFIG_PARTITION_UUIDS
559 * Now there's known to be a partition table,
560 * not specifying a partition means to pick partition 1.
562 if (part == PART_UNSPECIFIED)
566 * If user didn't specify a partition number, or did specify something
567 * other than "auto", use that partition number directly.
569 if (part != PART_AUTO) {
570 ret = get_partition_info(*dev_desc, part, info);
572 printf("** Invalid partition %d **\n", part);
577 * Find the first bootable partition.
578 * If none are bootable, fall back to the first valid partition.
581 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
582 ret = get_partition_info(*dev_desc, p, info);
587 * First valid partition, or new better partition?
588 * If so, save partition ID.
590 if (!part || info->bootable)
593 /* Best possible partition? Stop searching. */
598 * We now need to search further for best possible.
599 * If we what we just queried was the best so far,
600 * save the info since we over-write it next loop.
605 /* If we found any acceptable partition */
608 * If we searched all possible partition IDs,
609 * return the first valid partition we found.
611 if (p == MAX_SEARCH_PARTITIONS + 1)
614 printf("** No valid partitions found **\n");
619 if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
620 printf("** Invalid partition type \"%.32s\""
621 " (expect \"" BOOT_PART_TYPE "\")\n",