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...)
40 block_dev_desc_t* (*get_dev)(int dev);
43 static const struct block_drvr block_drvr[] = {
44 #if defined(CONFIG_CMD_IDE)
45 { .name = "ide", .get_dev = ide_get_dev, },
47 #if defined(CONFIG_CMD_SATA)
48 {.name = "sata", .get_dev = sata_get_dev, },
50 #if defined(CONFIG_CMD_SCSI)
51 { .name = "scsi", .get_dev = scsi_get_dev, },
53 #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
54 { .name = "usb", .get_dev = usb_stor_get_dev, },
56 #if defined(CONFIG_MMC)
57 { .name = "mmc", .get_dev = mmc_get_dev, },
59 #if defined(CONFIG_SYSTEMACE)
60 { .name = "ace", .get_dev = systemace_get_dev, },
65 DECLARE_GLOBAL_DATA_PTR;
67 #ifdef HAVE_BLOCK_DEVICE
68 block_dev_desc_t *get_dev(const char *ifname, int dev)
70 const struct block_drvr *drvr = block_drvr;
71 block_dev_desc_t* (*reloc_get_dev)(int dev);
78 #ifdef CONFIG_NEEDS_MANUAL_RELOC
79 name += gd->reloc_off;
83 reloc_get_dev = drvr->get_dev;
84 #ifdef CONFIG_NEEDS_MANUAL_RELOC
85 name += gd->reloc_off;
86 reloc_get_dev += gd->reloc_off;
88 if (strncmp(ifname, name, strlen(name)) == 0)
89 return reloc_get_dev(dev);
95 block_dev_desc_t *get_dev(const char *ifname, int dev)
101 #ifdef HAVE_BLOCK_DEVICE
103 /* ------------------------------------------------------------------------- */
105 * reports device info to the user
109 typedef uint64_t lba512_t;
111 typedef lbaint_t lba512_t;
115 * Overflowless variant of (block_count * mul_by / div_by)
116 * when div_by > mul_by
118 static lba512_t lba512_muldiv (lba512_t block_count, lba512_t mul_by, lba512_t div_by)
120 lba512_t bc_quot, bc_rem;
122 /* x * m / d == x / d * m + (x % d) * m / d */
123 bc_quot = block_count / div_by;
124 bc_rem = block_count - div_by * bc_quot;
125 return bc_quot * mul_by + (bc_rem * mul_by) / div_by;
128 void dev_print (block_dev_desc_t *dev_desc)
130 lba512_t lba512; /* number of blocks if 512bytes block size */
132 if (dev_desc->type == DEV_TYPE_UNKNOWN) {
133 puts ("not available\n");
137 switch (dev_desc->if_type) {
139 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
140 dev_desc->target,dev_desc->lun,
148 printf ("Model: %s Firm: %s Ser#: %s\n",
156 printf ("Vendor: %s Rev: %s Prod: %s\n",
162 puts("device type DOC\n");
164 case IF_TYPE_UNKNOWN:
165 puts("device type unknown\n");
168 printf("Unhandled device type: %i\n", dev_desc->if_type);
172 if (dev_desc->removable)
174 switch (dev_desc->type & 0x1F) {
175 case DEV_TYPE_HARDDISK:
181 case DEV_TYPE_OPDISK:
182 puts ("Optical Device");
188 printf ("# %02X #", dev_desc->type & 0x1F);
192 if (dev_desc->lba > 0L && dev_desc->blksz > 0L) {
193 ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
198 lba512 = (lba * (dev_desc->blksz/512));
199 /* round to 1 digit */
200 mb = lba512_muldiv(lba512, 10, 2048); /* 2048 = (1024 * 1024) / 512 MB */
203 mb_rem = mb - (10 * mb_quot);
207 gb_rem = gb - (10 * gb_quot);
210 printf (" Supports 48-bit addressing\n");
212 #if defined(CONFIG_SYS_64BIT_LBA)
213 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n",
219 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
226 puts (" Capacity: not available\n");
231 #ifdef HAVE_BLOCK_DEVICE
233 void init_part (block_dev_desc_t * dev_desc)
235 #ifdef CONFIG_ISO_PARTITION
236 if (test_part_iso(dev_desc) == 0) {
237 dev_desc->part_type = PART_TYPE_ISO;
242 #ifdef CONFIG_MAC_PARTITION
243 if (test_part_mac(dev_desc) == 0) {
244 dev_desc->part_type = PART_TYPE_MAC;
249 /* must be placed before DOS partition detection */
250 #ifdef CONFIG_EFI_PARTITION
251 if (test_part_efi(dev_desc) == 0) {
252 dev_desc->part_type = PART_TYPE_EFI;
257 #ifdef CONFIG_DOS_PARTITION
258 if (test_part_dos(dev_desc) == 0) {
259 dev_desc->part_type = PART_TYPE_DOS;
264 #ifdef CONFIG_AMIGA_PARTITION
265 if (test_part_amiga(dev_desc) == 0) {
266 dev_desc->part_type = PART_TYPE_AMIGA;
270 dev_desc->part_type = PART_TYPE_UNKNOWN;
274 #if defined(CONFIG_MAC_PARTITION) || \
275 defined(CONFIG_DOS_PARTITION) || \
276 defined(CONFIG_ISO_PARTITION) || \
277 defined(CONFIG_AMIGA_PARTITION) || \
278 defined(CONFIG_EFI_PARTITION)
280 static void print_part_header (const char *type, block_dev_desc_t * dev_desc)
282 puts ("\nPartition Map for ");
283 switch (dev_desc->if_type) {
309 printf (" device %d -- Partition Type: %s\n\n",
310 dev_desc->dev, type);
313 #endif /* any CONFIG_..._PARTITION */
315 void print_part (block_dev_desc_t * dev_desc)
318 switch (dev_desc->part_type) {
319 #ifdef CONFIG_MAC_PARTITION
321 PRINTF ("## Testing for valid MAC partition ##\n");
322 print_part_header ("MAC", dev_desc);
323 print_part_mac (dev_desc);
326 #ifdef CONFIG_DOS_PARTITION
328 PRINTF ("## Testing for valid DOS partition ##\n");
329 print_part_header ("DOS", dev_desc);
330 print_part_dos (dev_desc);
334 #ifdef CONFIG_ISO_PARTITION
336 PRINTF ("## Testing for valid ISO Boot partition ##\n");
337 print_part_header ("ISO", dev_desc);
338 print_part_iso (dev_desc);
342 #ifdef CONFIG_AMIGA_PARTITION
343 case PART_TYPE_AMIGA:
344 PRINTF ("## Testing for a valid Amiga partition ##\n");
345 print_part_header ("AMIGA", dev_desc);
346 print_part_amiga (dev_desc);
350 #ifdef CONFIG_EFI_PARTITION
352 PRINTF ("## Testing for valid EFI partition ##\n");
353 print_part_header ("EFI", dev_desc);
354 print_part_efi (dev_desc);
358 puts ("## Unknown partition table\n");
361 #endif /* HAVE_BLOCK_DEVICE */
363 int get_partition_info(block_dev_desc_t *dev_desc, int part
364 , disk_partition_t *info)
366 #ifdef HAVE_BLOCK_DEVICE
368 #ifdef CONFIG_PARTITION_UUIDS
369 /* The common case is no UUID support */
373 switch (dev_desc->part_type) {
374 #ifdef CONFIG_MAC_PARTITION
376 if (get_partition_info_mac(dev_desc, part, info) == 0) {
377 PRINTF("## Valid MAC partition found ##\n");
383 #ifdef CONFIG_DOS_PARTITION
385 if (get_partition_info_dos(dev_desc, part, info) == 0) {
386 PRINTF("## Valid DOS partition found ##\n");
392 #ifdef CONFIG_ISO_PARTITION
394 if (get_partition_info_iso(dev_desc, part, info) == 0) {
395 PRINTF("## Valid ISO boot partition found ##\n");
401 #ifdef CONFIG_AMIGA_PARTITION
402 case PART_TYPE_AMIGA:
403 if (get_partition_info_amiga(dev_desc, part, info) == 0) {
404 PRINTF("## Valid Amiga partition found ##\n");
410 #ifdef CONFIG_EFI_PARTITION
412 if (get_partition_info_efi(dev_desc, part, info) == 0) {
413 PRINTF("## Valid EFI partition found ##\n");
421 #endif /* HAVE_BLOCK_DEVICE */
426 int get_device(const char *ifname, const char *dev_str,
427 block_dev_desc_t **dev_desc)
432 dev = simple_strtoul(dev_str, &ep, 16);
434 printf("** Bad device specification %s %s **\n",
439 *dev_desc = get_dev(ifname, dev);
440 if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
441 printf("** Bad device %s %s **\n", ifname, dev_str);
448 #define PART_UNSPECIFIED -2
450 #define MAX_SEARCH_PARTITIONS 16
451 int get_device_and_partition(const char *ifname, const char *dev_part_str,
452 block_dev_desc_t **dev_desc,
453 disk_partition_t *info, int allow_whole_dev)
456 const char *part_str;
457 char *dup_str = NULL;
463 disk_partition_t tmpinfo;
466 * For now, we have a special case for sandbox, since there is no
467 * real block device support.
469 if (0 == strcmp(ifname, "host")) {
471 info->start = info->size = info->blksz = 0;
473 strcpy((char *)info->type, BOOT_PART_TYPE);
474 strcpy((char *)info->name, "Sandbox host");
475 #ifdef CONFIG_PARTITION_UUIDS
482 /* If no dev_part_str, use bootdevice environment variable */
483 if (!dev_part_str || !strlen(dev_part_str) ||
484 !strcmp(dev_part_str, "-"))
485 dev_part_str = getenv("bootdevice");
487 /* If still no dev_part_str, it's an error */
489 printf("** No device specified **\n");
493 /* Separate device and partition ID specification */
494 part_str = strchr(dev_part_str, ':');
496 dup_str = strdup(dev_part_str);
497 dup_str[part_str - dev_part_str] = 0;
501 dev_str = dev_part_str;
504 /* Look up the device */
505 dev = get_device(ifname, dev_str, dev_desc);
509 /* Convert partition ID string to number */
510 if (!part_str || !*part_str) {
511 part = PART_UNSPECIFIED;
512 } else if (!strcmp(part_str, "auto")) {
515 /* Something specified -> use exactly that */
516 part = (int)simple_strtoul(part_str, &ep, 16);
518 * Less than whole string converted,
519 * or request for whole device, but caller requires partition.
521 if (*ep || (part == 0 && !allow_whole_dev)) {
522 printf("** Bad partition specification %s %s **\n",
523 ifname, dev_part_str);
529 * No partition table on device,
530 * or user requested partition 0 (entire device).
532 if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
534 if (!(*dev_desc)->lba) {
535 printf("** Bad device size - %s %s **\n", ifname,
541 * If user specified a partition ID other than 0,
542 * or the calling command only accepts partitions,
545 if ((part > 0) || (!allow_whole_dev)) {
546 printf("** No partition table - %s %s **\n", ifname,
551 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
554 info->size = (*dev_desc)->lba;
555 info->blksz = (*dev_desc)->blksz;
557 strcpy((char *)info->type, BOOT_PART_TYPE);
558 strcpy((char *)info->name, "Whole Disk");
559 #ifdef CONFIG_PARTITION_UUIDS
568 * Now there's known to be a partition table,
569 * not specifying a partition means to pick partition 1.
571 if (part == PART_UNSPECIFIED)
575 * If user didn't specify a partition number, or did specify something
576 * other than "auto", use that partition number directly.
578 if (part != PART_AUTO) {
579 ret = get_partition_info(*dev_desc, part, info);
581 printf("** Invalid partition %d **\n", part);
586 * Find the first bootable partition.
587 * If none are bootable, fall back to the first valid partition.
590 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
591 ret = get_partition_info(*dev_desc, p, info);
596 * First valid partition, or new better partition?
597 * If so, save partition ID.
599 if (!part || info->bootable)
602 /* Best possible partition? Stop searching. */
607 * We now need to search further for best possible.
608 * If we what we just queried was the best so far,
609 * save the info since we over-write it next loop.
614 /* If we found any acceptable partition */
617 * If we searched all possible partition IDs,
618 * return the first valid partition we found.
620 if (p == MAX_SEARCH_PARTITIONS + 1)
623 printf("** No valid partitions found **\n");
628 if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
629 printf("** Invalid partition type \"%.32s\""
630 " (expect \"" BOOT_PART_TYPE "\")\n",
636 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);