]>
git.ipfire.org Git - u-boot.git/blob - disk/part.c
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
13 #include <ubifs_uboot.h>
18 #define PRINTF(fmt,args...) printf (fmt ,##args)
20 #define PRINTF(fmt,args...)
25 block_dev_desc_t
* (*get_dev
)(int dev
);
26 int (*select_hwpart
)(int dev_num
, int hwpart
);
29 static const struct block_drvr block_drvr
[] = {
30 #if defined(CONFIG_CMD_IDE)
31 { .name
= "ide", .get_dev
= ide_get_dev
, },
33 #if defined(CONFIG_CMD_SATA)
34 {.name
= "sata", .get_dev
= sata_get_dev
, },
36 #if defined(CONFIG_CMD_SCSI)
37 { .name
= "scsi", .get_dev
= scsi_get_dev
, },
39 #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
40 { .name
= "usb", .get_dev
= usb_stor_get_dev
, },
42 #if defined(CONFIG_MMC)
45 .get_dev
= mmc_get_dev
,
46 .select_hwpart
= mmc_select_hwpart
,
49 #if defined(CONFIG_SYSTEMACE)
50 { .name
= "ace", .get_dev
= systemace_get_dev
, },
52 #if defined(CONFIG_SANDBOX)
53 { .name
= "host", .get_dev
= host_get_dev
, },
58 DECLARE_GLOBAL_DATA_PTR
;
60 #ifdef HAVE_BLOCK_DEVICE
61 static block_dev_desc_t
*get_dev_hwpart(const char *ifname
, int dev
, int hwpart
)
63 const struct block_drvr
*drvr
= block_drvr
;
64 block_dev_desc_t
* (*reloc_get_dev
)(int dev
);
65 int (*select_hwpart
)(int dev_num
, int hwpart
);
73 #ifdef CONFIG_NEEDS_MANUAL_RELOC
74 name
+= gd
->reloc_off
;
78 reloc_get_dev
= drvr
->get_dev
;
79 select_hwpart
= drvr
->select_hwpart
;
80 #ifdef CONFIG_NEEDS_MANUAL_RELOC
81 name
+= gd
->reloc_off
;
82 reloc_get_dev
+= gd
->reloc_off
;
84 select_hwpart
+= gd
->reloc_off
;
86 if (strncmp(ifname
, name
, strlen(name
)) == 0) {
87 block_dev_desc_t
*dev_desc
= reloc_get_dev(dev
);
90 if (hwpart
== 0 && !select_hwpart
)
94 ret
= select_hwpart(dev_desc
->dev
, hwpart
);
104 block_dev_desc_t
*get_dev(const char *ifname
, int dev
)
106 return get_dev_hwpart(ifname
, dev
, 0);
109 block_dev_desc_t
*get_dev_hwpart(const char *ifname
, int dev
, int hwpart
)
114 block_dev_desc_t
*get_dev(const char *ifname
, int dev
)
120 #ifdef HAVE_BLOCK_DEVICE
122 /* ------------------------------------------------------------------------- */
124 * reports device info to the user
128 typedef uint64_t lba512_t
;
130 typedef lbaint_t lba512_t
;
134 * Overflowless variant of (block_count * mul_by / div_by)
135 * when div_by > mul_by
137 static lba512_t
lba512_muldiv(lba512_t block_count
, lba512_t mul_by
, lba512_t div_by
)
139 lba512_t bc_quot
, bc_rem
;
141 /* x * m / d == x / d * m + (x % d) * m / d */
142 bc_quot
= block_count
/ div_by
;
143 bc_rem
= block_count
- div_by
* bc_quot
;
144 return bc_quot
* mul_by
+ (bc_rem
* mul_by
) / div_by
;
147 void dev_print (block_dev_desc_t
*dev_desc
)
149 lba512_t lba512
; /* number of blocks if 512bytes block size */
151 if (dev_desc
->type
== DEV_TYPE_UNKNOWN
) {
152 puts ("not available\n");
156 switch (dev_desc
->if_type
) {
158 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
159 dev_desc
->target
,dev_desc
->lun
,
167 printf ("Model: %s Firm: %s Ser#: %s\n",
175 printf ("Vendor: %s Rev: %s Prod: %s\n",
181 puts("device type DOC\n");
183 case IF_TYPE_UNKNOWN
:
184 puts("device type unknown\n");
187 printf("Unhandled device type: %i\n", dev_desc
->if_type
);
191 if (dev_desc
->removable
)
193 switch (dev_desc
->type
& 0x1F) {
194 case DEV_TYPE_HARDDISK
:
200 case DEV_TYPE_OPDISK
:
201 puts ("Optical Device");
207 printf ("# %02X #", dev_desc
->type
& 0x1F);
211 if (dev_desc
->lba
> 0L && dev_desc
->blksz
> 0L) {
212 ulong mb
, mb_quot
, mb_rem
, gb
, gb_quot
, gb_rem
;
217 lba512
= (lba
* (dev_desc
->blksz
/512));
218 /* round to 1 digit */
219 /* 2048 = (1024 * 1024) / 512 MB */
220 mb
= lba512_muldiv(lba512
, 10, 2048);
223 mb_rem
= mb
- (10 * mb_quot
);
227 gb_rem
= gb
- (10 * gb_quot
);
230 printf (" Supports 48-bit addressing\n");
232 #if defined(CONFIG_SYS_64BIT_LBA)
233 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n",
239 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
246 puts (" Capacity: not available\n");
251 #ifdef HAVE_BLOCK_DEVICE
253 void init_part(block_dev_desc_t
*dev_desc
)
255 #ifdef CONFIG_ISO_PARTITION
256 if (test_part_iso(dev_desc
) == 0) {
257 dev_desc
->part_type
= PART_TYPE_ISO
;
262 #ifdef CONFIG_MAC_PARTITION
263 if (test_part_mac(dev_desc
) == 0) {
264 dev_desc
->part_type
= PART_TYPE_MAC
;
269 /* must be placed before DOS partition detection */
270 #ifdef CONFIG_EFI_PARTITION
271 if (test_part_efi(dev_desc
) == 0) {
272 dev_desc
->part_type
= PART_TYPE_EFI
;
277 #ifdef CONFIG_DOS_PARTITION
278 if (test_part_dos(dev_desc
) == 0) {
279 dev_desc
->part_type
= PART_TYPE_DOS
;
284 #ifdef CONFIG_AMIGA_PARTITION
285 if (test_part_amiga(dev_desc
) == 0) {
286 dev_desc
->part_type
= PART_TYPE_AMIGA
;
290 dev_desc
->part_type
= PART_TYPE_UNKNOWN
;
294 #if defined(CONFIG_MAC_PARTITION) || \
295 defined(CONFIG_DOS_PARTITION) || \
296 defined(CONFIG_ISO_PARTITION) || \
297 defined(CONFIG_AMIGA_PARTITION) || \
298 defined(CONFIG_EFI_PARTITION)
300 static void print_part_header(const char *type
, block_dev_desc_t
*dev_desc
)
302 puts ("\nPartition Map for ");
303 switch (dev_desc
->if_type
) {
332 printf (" device %d -- Partition Type: %s\n\n",
333 dev_desc
->dev
, type
);
336 #endif /* any CONFIG_..._PARTITION */
338 void print_part(block_dev_desc_t
* dev_desc
)
341 switch (dev_desc
->part_type
) {
342 #ifdef CONFIG_MAC_PARTITION
344 PRINTF ("## Testing for valid MAC partition ##\n");
345 print_part_header ("MAC", dev_desc
);
346 print_part_mac (dev_desc
);
349 #ifdef CONFIG_DOS_PARTITION
351 PRINTF ("## Testing for valid DOS partition ##\n");
352 print_part_header ("DOS", dev_desc
);
353 print_part_dos (dev_desc
);
357 #ifdef CONFIG_ISO_PARTITION
359 PRINTF ("## Testing for valid ISO Boot partition ##\n");
360 print_part_header ("ISO", dev_desc
);
361 print_part_iso (dev_desc
);
365 #ifdef CONFIG_AMIGA_PARTITION
366 case PART_TYPE_AMIGA
:
367 PRINTF ("## Testing for a valid Amiga partition ##\n");
368 print_part_header ("AMIGA", dev_desc
);
369 print_part_amiga (dev_desc
);
373 #ifdef CONFIG_EFI_PARTITION
375 PRINTF ("## Testing for valid EFI partition ##\n");
376 print_part_header ("EFI", dev_desc
);
377 print_part_efi (dev_desc
);
381 puts ("## Unknown partition table\n");
384 #endif /* HAVE_BLOCK_DEVICE */
386 int get_partition_info(block_dev_desc_t
*dev_desc
, int part
,
387 disk_partition_t
*info
)
389 #ifdef HAVE_BLOCK_DEVICE
391 #ifdef CONFIG_PARTITION_UUIDS
392 /* The common case is no UUID support */
395 #ifdef CONFIG_PARTITION_TYPE_GUID
396 info
->type_guid
[0] = 0;
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");
447 #endif /* HAVE_BLOCK_DEVICE */
452 int get_device(const char *ifname
, const char *dev_hwpart_str
,
453 block_dev_desc_t
**dev_desc
)
456 char *dup_str
= NULL
;
457 const char *dev_str
, *hwpart_str
;
460 hwpart_str
= strchr(dev_hwpart_str
, '.');
462 dup_str
= strdup(dev_hwpart_str
);
463 dup_str
[hwpart_str
- dev_hwpart_str
] = 0;
467 dev_str
= dev_hwpart_str
;
471 dev
= simple_strtoul(dev_str
, &ep
, 16);
473 printf("** Bad device specification %s %s **\n",
480 hwpart
= simple_strtoul(hwpart_str
, &ep
, 16);
482 printf("** Bad HW partition specification %s %s **\n",
489 *dev_desc
= get_dev_hwpart(ifname
, dev
, hwpart
);
490 if (!(*dev_desc
) || ((*dev_desc
)->type
== DEV_TYPE_UNKNOWN
)) {
491 printf("** Bad device %s %s **\n", ifname
, dev_hwpart_str
);
501 #define PART_UNSPECIFIED -2
503 #define MAX_SEARCH_PARTITIONS 16
504 int get_device_and_partition(const char *ifname
, const char *dev_part_str
,
505 block_dev_desc_t
**dev_desc
,
506 disk_partition_t
*info
, int allow_whole_dev
)
509 const char *part_str
;
510 char *dup_str
= NULL
;
516 disk_partition_t tmpinfo
;
518 #if defined CONFIG_SANDBOX && defined CONFIG_CMD_UBIFS
519 #error Only one of CONFIG_SANDBOX and CONFIG_CMD_UBIFS may be selected
522 #ifdef CONFIG_SANDBOX
524 * Special-case a pseudo block device "hostfs", to allow access to the
525 * host's own filesystem.
527 if (0 == strcmp(ifname
, "hostfs")) {
533 strcpy((char *)info
->type
, BOOT_PART_TYPE
);
534 strcpy((char *)info
->name
, "Sandbox host");
535 #ifdef CONFIG_PARTITION_UUIDS
538 #ifdef CONFIG_PARTITION_TYPE_GUID
539 info
->type_guid
[0] = 0;
546 #ifdef CONFIG_CMD_UBIFS
548 * Special-case ubi, ubi goes through a mtd, rathen then through
549 * a regular block device.
551 if (0 == strcmp(ifname
, "ubi")) {
552 if (!ubifs_is_mounted()) {
553 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
558 memset(info
, 0, sizeof(*info
));
559 strcpy((char *)info
->type
, BOOT_PART_TYPE
);
560 strcpy((char *)info
->name
, "UBI");
561 #ifdef CONFIG_PARTITION_UUIDS
568 /* If no dev_part_str, use bootdevice environment variable */
569 if (!dev_part_str
|| !strlen(dev_part_str
) ||
570 !strcmp(dev_part_str
, "-"))
571 dev_part_str
= getenv("bootdevice");
573 /* If still no dev_part_str, it's an error */
575 printf("** No device specified **\n");
579 /* Separate device and partition ID specification */
580 part_str
= strchr(dev_part_str
, ':');
582 dup_str
= strdup(dev_part_str
);
583 dup_str
[part_str
- dev_part_str
] = 0;
587 dev_str
= dev_part_str
;
590 /* Look up the device */
591 dev
= get_device(ifname
, dev_str
, dev_desc
);
595 /* Convert partition ID string to number */
596 if (!part_str
|| !*part_str
) {
597 part
= PART_UNSPECIFIED
;
598 } else if (!strcmp(part_str
, "auto")) {
601 /* Something specified -> use exactly that */
602 part
= (int)simple_strtoul(part_str
, &ep
, 16);
604 * Less than whole string converted,
605 * or request for whole device, but caller requires partition.
607 if (*ep
|| (part
== 0 && !allow_whole_dev
)) {
608 printf("** Bad partition specification %s %s **\n",
609 ifname
, dev_part_str
);
615 * No partition table on device,
616 * or user requested partition 0 (entire device).
618 if (((*dev_desc
)->part_type
== PART_TYPE_UNKNOWN
) ||
620 if (!(*dev_desc
)->lba
) {
621 printf("** Bad device size - %s %s **\n", ifname
,
627 * If user specified a partition ID other than 0,
628 * or the calling command only accepts partitions,
631 if ((part
> 0) || (!allow_whole_dev
)) {
632 printf("** No partition table - %s %s **\n", ifname
,
637 (*dev_desc
)->log2blksz
= LOG2((*dev_desc
)->blksz
);
640 info
->size
= (*dev_desc
)->lba
;
641 info
->blksz
= (*dev_desc
)->blksz
;
643 strcpy((char *)info
->type
, BOOT_PART_TYPE
);
644 strcpy((char *)info
->name
, "Whole Disk");
645 #ifdef CONFIG_PARTITION_UUIDS
648 #ifdef CONFIG_PARTITION_TYPE_GUID
649 info
->type_guid
[0] = 0;
657 * Now there's known to be a partition table,
658 * not specifying a partition means to pick partition 1.
660 if (part
== PART_UNSPECIFIED
)
664 * If user didn't specify a partition number, or did specify something
665 * other than "auto", use that partition number directly.
667 if (part
!= PART_AUTO
) {
668 ret
= get_partition_info(*dev_desc
, part
, info
);
670 printf("** Invalid partition %d **\n", part
);
675 * Find the first bootable partition.
676 * If none are bootable, fall back to the first valid partition.
679 for (p
= 1; p
<= MAX_SEARCH_PARTITIONS
; p
++) {
680 ret
= get_partition_info(*dev_desc
, p
, info
);
685 * First valid partition, or new better partition?
686 * If so, save partition ID.
688 if (!part
|| info
->bootable
)
691 /* Best possible partition? Stop searching. */
696 * We now need to search further for best possible.
697 * If we what we just queried was the best so far,
698 * save the info since we over-write it next loop.
703 /* If we found any acceptable partition */
706 * If we searched all possible partition IDs,
707 * return the first valid partition we found.
709 if (p
== MAX_SEARCH_PARTITIONS
+ 1)
712 printf("** No valid partitions found **\n");
717 if (strncmp((char *)info
->type
, BOOT_PART_TYPE
, sizeof(info
->type
)) != 0) {
718 printf("** Invalid partition type \"%.32s\""
719 " (expect \"" BOOT_PART_TYPE
"\")\n",
725 (*dev_desc
)->log2blksz
= LOG2((*dev_desc
)->blksz
);