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 #if (defined(CONFIG_CMD_IDE) || \
39 defined(CONFIG_CMD_SATA) || \
40 defined(CONFIG_CMD_SCSI) || \
41 defined(CONFIG_CMD_USB) || \
42 defined(CONFIG_MMC) || \
43 defined(CONFIG_SYSTEMACE) )
47 block_dev_desc_t* (*get_dev)(int dev);
50 static const struct block_drvr block_drvr[] = {
51 #if defined(CONFIG_CMD_IDE)
52 { .name = "ide", .get_dev = ide_get_dev, },
54 #if defined(CONFIG_CMD_SATA)
55 {.name = "sata", .get_dev = sata_get_dev, },
57 #if defined(CONFIG_CMD_SCSI)
58 { .name = "scsi", .get_dev = scsi_get_dev, },
60 #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
61 { .name = "usb", .get_dev = usb_stor_get_dev, },
63 #if defined(CONFIG_MMC)
64 { .name = "mmc", .get_dev = mmc_get_dev, },
66 #if defined(CONFIG_SYSTEMACE)
67 { .name = "ace", .get_dev = systemace_get_dev, },
72 DECLARE_GLOBAL_DATA_PTR;
74 block_dev_desc_t *get_dev(const char *ifname, int dev)
76 const struct block_drvr *drvr = block_drvr;
77 block_dev_desc_t* (*reloc_get_dev)(int dev);
84 #ifdef CONFIG_NEEDS_MANUAL_RELOC
85 name += gd->reloc_off;
89 reloc_get_dev = drvr->get_dev;
90 #ifdef CONFIG_NEEDS_MANUAL_RELOC
91 name += gd->reloc_off;
92 reloc_get_dev += gd->reloc_off;
94 if (strncmp(ifname, name, strlen(name)) == 0)
95 return reloc_get_dev(dev);
101 block_dev_desc_t *get_dev(const char *ifname, int dev)
107 #if (defined(CONFIG_CMD_IDE) || \
108 defined(CONFIG_CMD_SATA) || \
109 defined(CONFIG_CMD_SCSI) || \
110 defined(CONFIG_CMD_USB) || \
111 defined(CONFIG_MMC) || \
112 defined(CONFIG_SYSTEMACE) )
114 /* ------------------------------------------------------------------------- */
116 * reports device info to the user
120 typedef uint64_t lba512_t;
122 typedef lbaint_t lba512_t;
126 * Overflowless variant of (block_count * mul_by / div_by)
127 * when div_by > mul_by
129 static lba512_t lba512_muldiv (lba512_t block_count, lba512_t mul_by, lba512_t div_by)
131 lba512_t bc_quot, bc_rem;
133 /* x * m / d == x / d * m + (x % d) * m / d */
134 bc_quot = block_count / div_by;
135 bc_rem = block_count - div_by * bc_quot;
136 return bc_quot * mul_by + (bc_rem * mul_by) / div_by;
139 void dev_print (block_dev_desc_t *dev_desc)
141 lba512_t lba512; /* number of blocks if 512bytes block size */
143 if (dev_desc->type == DEV_TYPE_UNKNOWN) {
144 puts ("not available\n");
148 switch (dev_desc->if_type) {
150 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
151 dev_desc->target,dev_desc->lun,
159 printf ("Model: %s Firm: %s Ser#: %s\n",
167 printf ("Vendor: %s Rev: %s Prod: %s\n",
173 puts("device type DOC\n");
175 case IF_TYPE_UNKNOWN:
176 puts("device type unknown\n");
179 printf("Unhandled device type: %i\n", dev_desc->if_type);
183 if (dev_desc->removable)
185 switch (dev_desc->type & 0x1F) {
186 case DEV_TYPE_HARDDISK:
192 case DEV_TYPE_OPDISK:
193 puts ("Optical Device");
199 printf ("# %02X #", dev_desc->type & 0x1F);
203 if ((dev_desc->lba * dev_desc->blksz)>0L) {
204 ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
209 lba512 = (lba * (dev_desc->blksz/512));
210 /* round to 1 digit */
211 mb = lba512_muldiv(lba512, 10, 2048); /* 2048 = (1024 * 1024) / 512 MB */
214 mb_rem = mb - (10 * mb_quot);
218 gb_rem = gb - (10 * gb_quot);
221 printf (" Supports 48-bit addressing\n");
223 #if defined(CONFIG_SYS_64BIT_LBA)
224 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n",
230 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
237 puts (" Capacity: not available\n");
242 #if (defined(CONFIG_CMD_IDE) || \
243 defined(CONFIG_CMD_SATA) || \
244 defined(CONFIG_CMD_SCSI) || \
245 defined(CONFIG_CMD_USB) || \
246 defined(CONFIG_MMC) || \
247 defined(CONFIG_SYSTEMACE) )
249 #if defined(CONFIG_MAC_PARTITION) || \
250 defined(CONFIG_DOS_PARTITION) || \
251 defined(CONFIG_ISO_PARTITION) || \
252 defined(CONFIG_AMIGA_PARTITION) || \
253 defined(CONFIG_EFI_PARTITION)
255 void init_part (block_dev_desc_t * dev_desc)
257 #ifdef CONFIG_ISO_PARTITION
258 if (test_part_iso(dev_desc) == 0) {
259 dev_desc->part_type = PART_TYPE_ISO;
264 #ifdef CONFIG_MAC_PARTITION
265 if (test_part_mac(dev_desc) == 0) {
266 dev_desc->part_type = PART_TYPE_MAC;
271 /* must be placed before DOS partition detection */
272 #ifdef CONFIG_EFI_PARTITION
273 if (test_part_efi(dev_desc) == 0) {
274 dev_desc->part_type = PART_TYPE_EFI;
279 #ifdef CONFIG_DOS_PARTITION
280 if (test_part_dos(dev_desc) == 0) {
281 dev_desc->part_type = PART_TYPE_DOS;
286 #ifdef CONFIG_AMIGA_PARTITION
287 if (test_part_amiga(dev_desc) == 0) {
288 dev_desc->part_type = PART_TYPE_AMIGA;
292 dev_desc->part_type = PART_TYPE_UNKNOWN;
296 static void print_part_header (const char *type, block_dev_desc_t * dev_desc)
298 puts ("\nPartition Map for ");
299 switch (dev_desc->if_type) {
325 printf (" device %d -- Partition Type: %s\n\n",
326 dev_desc->dev, type);
329 void print_part (block_dev_desc_t * dev_desc)
332 switch (dev_desc->part_type) {
333 #ifdef CONFIG_MAC_PARTITION
335 PRINTF ("## Testing for valid MAC partition ##\n");
336 print_part_header ("MAC", dev_desc);
337 print_part_mac (dev_desc);
340 #ifdef CONFIG_DOS_PARTITION
342 PRINTF ("## Testing for valid DOS partition ##\n");
343 print_part_header ("DOS", dev_desc);
344 print_part_dos (dev_desc);
348 #ifdef CONFIG_ISO_PARTITION
350 PRINTF ("## Testing for valid ISO Boot partition ##\n");
351 print_part_header ("ISO", dev_desc);
352 print_part_iso (dev_desc);
356 #ifdef CONFIG_AMIGA_PARTITION
357 case PART_TYPE_AMIGA:
358 PRINTF ("## Testing for a valid Amiga partition ##\n");
359 print_part_header ("AMIGA", dev_desc);
360 print_part_amiga (dev_desc);
364 #ifdef CONFIG_EFI_PARTITION
366 PRINTF ("## Testing for valid EFI partition ##\n");
367 print_part_header ("EFI", dev_desc);
368 print_part_efi (dev_desc);
372 puts ("## Unknown partition table\n");
376 #else /* neither MAC nor DOS nor ISO nor AMIGA nor EFI partition configured */
377 # error neither CONFIG_MAC_PARTITION nor CONFIG_DOS_PARTITION
378 # error nor CONFIG_ISO_PARTITION nor CONFIG_AMIGA_PARTITION
379 # error nor CONFIG_EFI_PARTITION configured!
384 int get_partition_info(block_dev_desc_t *dev_desc, int part
385 , disk_partition_t *info)
387 #if defined(CONFIG_CMD_IDE) || \
388 defined(CONFIG_CMD_SATA) || \
389 defined(CONFIG_CMD_SCSI) || \
390 defined(CONFIG_CMD_USB) || \
391 defined(CONFIG_MMC) || \
392 defined(CONFIG_SYSTEMACE)
394 #ifdef CONFIG_PARTITION_UUIDS
395 /* The common case is no UUID support */
399 switch (dev_desc->part_type) {
400 #ifdef CONFIG_MAC_PARTITION
402 if (get_partition_info_mac(dev_desc, part, info) == 0) {
403 PRINTF("## Valid MAC partition found ##\n");
409 #ifdef CONFIG_DOS_PARTITION
411 if (get_partition_info_dos(dev_desc, part, info) == 0) {
412 PRINTF("## Valid DOS partition found ##\n");
418 #ifdef CONFIG_ISO_PARTITION
420 if (get_partition_info_iso(dev_desc, part, info) == 0) {
421 PRINTF("## Valid ISO boot partition found ##\n");
427 #ifdef CONFIG_AMIGA_PARTITION
428 case PART_TYPE_AMIGA:
429 if (get_partition_info_amiga(dev_desc, part, info) == 0) {
430 PRINTF("## Valid Amiga partition found ##\n");
436 #ifdef CONFIG_EFI_PARTITION
438 if (get_partition_info_efi(dev_desc, part, info) == 0) {
439 PRINTF("## Valid EFI partition found ##\n");
452 int get_device(const char *ifname, const char *dev_str,
453 block_dev_desc_t **dev_desc)
458 dev = simple_strtoul(dev_str, &ep, 16);
460 printf("** Bad device specification %s %s **\n",
465 *dev_desc = get_dev(ifname, dev);
466 if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
467 printf("** Bad device %s %s **\n", ifname, dev_str);
474 #define PART_UNSPECIFIED -2
476 #define MAX_SEARCH_PARTITIONS 16
477 int get_device_and_partition(const char *ifname, const char *dev_part_str,
478 block_dev_desc_t **dev_desc,
479 disk_partition_t *info, int allow_whole_dev)
482 const char *part_str;
483 char *dup_str = NULL;
489 disk_partition_t tmpinfo;
491 /* If no dev_part_str, use bootdevice environment variable */
492 if (!dev_part_str || !strlen(dev_part_str) ||
493 !strcmp(dev_part_str, "-"))
494 dev_part_str = getenv("bootdevice");
496 /* If still no dev_part_str, it's an error */
498 printf("** No device specified **\n");
502 /* Separate device and partition ID specification */
503 part_str = strchr(dev_part_str, ':');
505 dup_str = strdup(dev_part_str);
506 dup_str[part_str - dev_part_str] = 0;
510 dev_str = dev_part_str;
513 /* Look up the device */
514 dev = get_device(ifname, dev_str, dev_desc);
518 /* Convert partition ID string to number */
519 if (!part_str || !*part_str) {
520 part = PART_UNSPECIFIED;
521 } else if (!strcmp(part_str, "auto")) {
524 /* Something specified -> use exactly that */
525 part = (int)simple_strtoul(part_str, &ep, 16);
527 * Less than whole string converted,
528 * or request for whole device, but caller requires partition.
530 if (*ep || (part == 0 && !allow_whole_dev)) {
531 printf("** Bad partition specification %s %s **\n",
532 ifname, dev_part_str);
538 * No partition table on device,
539 * or user requested partition 0 (entire device).
541 if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
543 if (!(*dev_desc)->lba) {
544 printf("** Bad device size - %s %s **\n", ifname,
550 * If user specified a partition ID other than 0,
551 * or the calling command only accepts partitions,
554 if ((part > 0) || (!allow_whole_dev)) {
555 printf("** No partition table - %s %s **\n", ifname,
561 info->size = (*dev_desc)->lba;
562 info->blksz = (*dev_desc)->blksz;
564 #ifdef CONFIG_PARTITION_UUIDS
573 * Now there's known to be a partition table,
574 * not specifying a partition means to pick partition 1.
576 if (part == PART_UNSPECIFIED)
580 * If user didn't specify a partition number, or did specify something
581 * other than "auto", use that partition number directly.
583 if (part != PART_AUTO) {
584 ret = get_partition_info(*dev_desc, part, info);
586 printf("** Invalid partition %d **\n", part);
591 * Find the first bootable partition.
592 * If none are bootable, fall back to the first valid partition.
595 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
596 ret = get_partition_info(*dev_desc, p, info);
601 * First valid partition, or new better partition?
602 * If so, save partition ID.
604 if (!part || info->bootable)
607 /* Best possible partition? Stop searching. */
612 * We now need to search further for best possible.
613 * If we what we just queried was the best so far,
614 * save the info since we over-write it next loop.
619 /* If we found any acceptable partition */
622 * If we searched all possible partition IDs,
623 * return the first valid partition we found.
625 if (p == MAX_SEARCH_PARTITIONS + 1)
629 printf("** No valid partitions found **\n");
633 if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
634 printf("** Invalid partition type \"%.32s\""
635 " (expect \"" BOOT_PART_TYPE "\")\n",