]>
git.ipfire.org Git - people/ms/u-boot.git/blob - disk/part.c
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
);
25 int (*select_hwpart
)(int dev_num
, int hwpart
);
28 static const struct block_drvr block_drvr
[] = {
29 #if defined(CONFIG_CMD_IDE)
30 { .name
= "ide", .get_dev
= ide_get_dev
, },
32 #if defined(CONFIG_CMD_SATA)
33 {.name
= "sata", .get_dev
= sata_get_dev
, },
35 #if defined(CONFIG_CMD_SCSI)
36 { .name
= "scsi", .get_dev
= scsi_get_dev
, },
38 #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
39 { .name
= "usb", .get_dev
= usb_stor_get_dev
, },
41 #if defined(CONFIG_MMC)
44 .get_dev
= mmc_get_dev
,
45 .select_hwpart
= mmc_select_hwpart
,
48 #if defined(CONFIG_SYSTEMACE)
49 { .name
= "ace", .get_dev
= systemace_get_dev
, },
51 #if defined(CONFIG_SANDBOX)
52 { .name
= "host", .get_dev
= host_get_dev
, },
57 DECLARE_GLOBAL_DATA_PTR
;
59 #ifdef HAVE_BLOCK_DEVICE
60 block_dev_desc_t
*get_dev_hwpart(const char *ifname
, int dev
, int hwpart
)
62 const struct block_drvr
*drvr
= block_drvr
;
63 block_dev_desc_t
* (*reloc_get_dev
)(int dev
);
64 int (*select_hwpart
)(int dev_num
, int hwpart
);
72 #ifdef CONFIG_NEEDS_MANUAL_RELOC
73 name
+= gd
->reloc_off
;
77 reloc_get_dev
= drvr
->get_dev
;
78 select_hwpart
= drvr
->select_hwpart
;
79 #ifdef CONFIG_NEEDS_MANUAL_RELOC
80 name
+= gd
->reloc_off
;
81 reloc_get_dev
+= gd
->reloc_off
;
83 select_hwpart
+= gd
->reloc_off
;
85 if (strncmp(ifname
, name
, strlen(name
)) == 0) {
86 block_dev_desc_t
*dev_desc
= reloc_get_dev(dev
);
89 if (hwpart
== 0 && !select_hwpart
)
93 ret
= select_hwpart(dev_desc
->dev
, hwpart
);
103 block_dev_desc_t
*get_dev(const char *ifname
, int dev
)
105 return get_dev_hwpart(ifname
, dev
, 0);
108 block_dev_desc_t
*get_dev_hwpart(const char *ifname
, int dev
, int hwpart
)
113 block_dev_desc_t
*get_dev(const char *ifname
, int dev
)
119 #ifdef HAVE_BLOCK_DEVICE
121 /* ------------------------------------------------------------------------- */
123 * reports device info to the user
127 typedef uint64_t lba512_t
;
129 typedef lbaint_t lba512_t
;
133 * Overflowless variant of (block_count * mul_by / div_by)
134 * when div_by > mul_by
136 static lba512_t
lba512_muldiv (lba512_t block_count
, lba512_t mul_by
, lba512_t div_by
)
138 lba512_t bc_quot
, bc_rem
;
140 /* x * m / d == x / d * m + (x % d) * m / d */
141 bc_quot
= block_count
/ div_by
;
142 bc_rem
= block_count
- div_by
* bc_quot
;
143 return bc_quot
* mul_by
+ (bc_rem
* mul_by
) / div_by
;
146 void dev_print (block_dev_desc_t
*dev_desc
)
148 lba512_t lba512
; /* number of blocks if 512bytes block size */
150 if (dev_desc
->type
== DEV_TYPE_UNKNOWN
) {
151 puts ("not available\n");
155 switch (dev_desc
->if_type
) {
157 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
158 dev_desc
->target
,dev_desc
->lun
,
166 printf ("Model: %s Firm: %s Ser#: %s\n",
174 printf ("Vendor: %s Rev: %s Prod: %s\n",
180 puts("device type DOC\n");
182 case IF_TYPE_UNKNOWN
:
183 puts("device type unknown\n");
186 printf("Unhandled device type: %i\n", dev_desc
->if_type
);
190 if (dev_desc
->removable
)
192 switch (dev_desc
->type
& 0x1F) {
193 case DEV_TYPE_HARDDISK
:
199 case DEV_TYPE_OPDISK
:
200 puts ("Optical Device");
206 printf ("# %02X #", dev_desc
->type
& 0x1F);
210 if (dev_desc
->lba
> 0L && dev_desc
->blksz
> 0L) {
211 ulong mb
, mb_quot
, mb_rem
, gb
, gb_quot
, gb_rem
;
216 lba512
= (lba
* (dev_desc
->blksz
/512));
217 /* round to 1 digit */
218 mb
= lba512_muldiv(lba512
, 10, 2048); /* 2048 = (1024 * 1024) / 512 MB */
221 mb_rem
= mb
- (10 * mb_quot
);
225 gb_rem
= gb
- (10 * gb_quot
);
228 printf (" Supports 48-bit addressing\n");
230 #if defined(CONFIG_SYS_64BIT_LBA)
231 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n",
237 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
244 puts (" Capacity: not available\n");
249 #ifdef HAVE_BLOCK_DEVICE
251 void init_part (block_dev_desc_t
* dev_desc
)
253 #ifdef CONFIG_ISO_PARTITION
254 if (test_part_iso(dev_desc
) == 0) {
255 dev_desc
->part_type
= PART_TYPE_ISO
;
260 #ifdef CONFIG_MAC_PARTITION
261 if (test_part_mac(dev_desc
) == 0) {
262 dev_desc
->part_type
= PART_TYPE_MAC
;
267 /* must be placed before DOS partition detection */
268 #ifdef CONFIG_EFI_PARTITION
269 if (test_part_efi(dev_desc
) == 0) {
270 dev_desc
->part_type
= PART_TYPE_EFI
;
275 #ifdef CONFIG_DOS_PARTITION
276 if (test_part_dos(dev_desc
) == 0) {
277 dev_desc
->part_type
= PART_TYPE_DOS
;
282 #ifdef CONFIG_AMIGA_PARTITION
283 if (test_part_amiga(dev_desc
) == 0) {
284 dev_desc
->part_type
= PART_TYPE_AMIGA
;
288 dev_desc
->part_type
= PART_TYPE_UNKNOWN
;
292 #if defined(CONFIG_MAC_PARTITION) || \
293 defined(CONFIG_DOS_PARTITION) || \
294 defined(CONFIG_ISO_PARTITION) || \
295 defined(CONFIG_AMIGA_PARTITION) || \
296 defined(CONFIG_EFI_PARTITION)
298 static void print_part_header (const char *type
, block_dev_desc_t
* dev_desc
)
300 puts ("\nPartition Map for ");
301 switch (dev_desc
->if_type
) {
330 printf (" device %d -- Partition Type: %s\n\n",
331 dev_desc
->dev
, type
);
334 #endif /* any CONFIG_..._PARTITION */
336 void print_part (block_dev_desc_t
* dev_desc
)
339 switch (dev_desc
->part_type
) {
340 #ifdef CONFIG_MAC_PARTITION
342 PRINTF ("## Testing for valid MAC partition ##\n");
343 print_part_header ("MAC", dev_desc
);
344 print_part_mac (dev_desc
);
347 #ifdef CONFIG_DOS_PARTITION
349 PRINTF ("## Testing for valid DOS partition ##\n");
350 print_part_header ("DOS", dev_desc
);
351 print_part_dos (dev_desc
);
355 #ifdef CONFIG_ISO_PARTITION
357 PRINTF ("## Testing for valid ISO Boot partition ##\n");
358 print_part_header ("ISO", dev_desc
);
359 print_part_iso (dev_desc
);
363 #ifdef CONFIG_AMIGA_PARTITION
364 case PART_TYPE_AMIGA
:
365 PRINTF ("## Testing for a valid Amiga partition ##\n");
366 print_part_header ("AMIGA", dev_desc
);
367 print_part_amiga (dev_desc
);
371 #ifdef CONFIG_EFI_PARTITION
373 PRINTF ("## Testing for valid EFI partition ##\n");
374 print_part_header ("EFI", dev_desc
);
375 print_part_efi (dev_desc
);
379 puts ("## Unknown partition table\n");
382 #endif /* HAVE_BLOCK_DEVICE */
384 int get_partition_info(block_dev_desc_t
*dev_desc
, int part
385 , disk_partition_t
*info
)
387 #ifdef HAVE_BLOCK_DEVICE
389 #ifdef CONFIG_PARTITION_UUIDS
390 /* The common case is no UUID support */
394 switch (dev_desc
->part_type
) {
395 #ifdef CONFIG_MAC_PARTITION
397 if (get_partition_info_mac(dev_desc
, part
, info
) == 0) {
398 PRINTF("## Valid MAC partition found ##\n");
404 #ifdef CONFIG_DOS_PARTITION
406 if (get_partition_info_dos(dev_desc
, part
, info
) == 0) {
407 PRINTF("## Valid DOS partition found ##\n");
413 #ifdef CONFIG_ISO_PARTITION
415 if (get_partition_info_iso(dev_desc
, part
, info
) == 0) {
416 PRINTF("## Valid ISO boot partition found ##\n");
422 #ifdef CONFIG_AMIGA_PARTITION
423 case PART_TYPE_AMIGA
:
424 if (get_partition_info_amiga(dev_desc
, part
, info
) == 0) {
425 PRINTF("## Valid Amiga partition found ##\n");
431 #ifdef CONFIG_EFI_PARTITION
433 if (get_partition_info_efi(dev_desc
, part
, info
) == 0) {
434 PRINTF("## Valid EFI partition found ##\n");
442 #endif /* HAVE_BLOCK_DEVICE */
447 int get_device(const char *ifname
, const char *dev_hwpart_str
,
448 block_dev_desc_t
**dev_desc
)
451 char *dup_str
= NULL
;
452 const char *dev_str
, *hwpart_str
;
455 hwpart_str
= strchr(dev_hwpart_str
, '.');
457 dup_str
= strdup(dev_hwpart_str
);
458 dup_str
[hwpart_str
- dev_hwpart_str
] = 0;
462 dev_str
= dev_hwpart_str
;
466 dev
= simple_strtoul(dev_str
, &ep
, 16);
468 printf("** Bad device specification %s %s **\n",
475 hwpart
= simple_strtoul(hwpart_str
, &ep
, 16);
477 printf("** Bad HW partition specification %s %s **\n",
484 *dev_desc
= get_dev_hwpart(ifname
, dev
, hwpart
);
485 if (!(*dev_desc
) || ((*dev_desc
)->type
== DEV_TYPE_UNKNOWN
)) {
486 printf("** Bad device %s %s **\n", ifname
, dev_hwpart_str
);
496 #define PART_UNSPECIFIED -2
498 #define MAX_SEARCH_PARTITIONS 16
499 int get_device_and_partition(const char *ifname
, const char *dev_part_str
,
500 block_dev_desc_t
**dev_desc
,
501 disk_partition_t
*info
, int allow_whole_dev
)
504 const char *part_str
;
505 char *dup_str
= NULL
;
511 disk_partition_t tmpinfo
;
514 * Special-case a psuedo block device "hostfs", to allow access to the
515 * host's own filesystem.
517 if (0 == strcmp(ifname
, "hostfs")) {
523 strcpy((char *)info
->type
, BOOT_PART_TYPE
);
524 strcpy((char *)info
->name
, "Sandbox host");
525 #ifdef CONFIG_PARTITION_UUIDS
532 /* If no dev_part_str, use bootdevice environment variable */
533 if (!dev_part_str
|| !strlen(dev_part_str
) ||
534 !strcmp(dev_part_str
, "-"))
535 dev_part_str
= getenv("bootdevice");
537 /* If still no dev_part_str, it's an error */
539 printf("** No device specified **\n");
543 /* Separate device and partition ID specification */
544 part_str
= strchr(dev_part_str
, ':');
546 dup_str
= strdup(dev_part_str
);
547 dup_str
[part_str
- dev_part_str
] = 0;
551 dev_str
= dev_part_str
;
554 /* Look up the device */
555 dev
= get_device(ifname
, dev_str
, dev_desc
);
559 /* Convert partition ID string to number */
560 if (!part_str
|| !*part_str
) {
561 part
= PART_UNSPECIFIED
;
562 } else if (!strcmp(part_str
, "auto")) {
565 /* Something specified -> use exactly that */
566 part
= (int)simple_strtoul(part_str
, &ep
, 16);
568 * Less than whole string converted,
569 * or request for whole device, but caller requires partition.
571 if (*ep
|| (part
== 0 && !allow_whole_dev
)) {
572 printf("** Bad partition specification %s %s **\n",
573 ifname
, dev_part_str
);
579 * No partition table on device,
580 * or user requested partition 0 (entire device).
582 if (((*dev_desc
)->part_type
== PART_TYPE_UNKNOWN
) ||
584 if (!(*dev_desc
)->lba
) {
585 printf("** Bad device size - %s %s **\n", ifname
,
591 * If user specified a partition ID other than 0,
592 * or the calling command only accepts partitions,
595 if ((part
> 0) || (!allow_whole_dev
)) {
596 printf("** No partition table - %s %s **\n", ifname
,
601 (*dev_desc
)->log2blksz
= LOG2((*dev_desc
)->blksz
);
604 info
->size
= (*dev_desc
)->lba
;
605 info
->blksz
= (*dev_desc
)->blksz
;
607 strcpy((char *)info
->type
, BOOT_PART_TYPE
);
608 strcpy((char *)info
->name
, "Whole Disk");
609 #ifdef CONFIG_PARTITION_UUIDS
618 * Now there's known to be a partition table,
619 * not specifying a partition means to pick partition 1.
621 if (part
== PART_UNSPECIFIED
)
625 * If user didn't specify a partition number, or did specify something
626 * other than "auto", use that partition number directly.
628 if (part
!= PART_AUTO
) {
629 ret
= get_partition_info(*dev_desc
, part
, info
);
631 printf("** Invalid partition %d **\n", part
);
636 * Find the first bootable partition.
637 * If none are bootable, fall back to the first valid partition.
640 for (p
= 1; p
<= MAX_SEARCH_PARTITIONS
; p
++) {
641 ret
= get_partition_info(*dev_desc
, p
, info
);
646 * First valid partition, or new better partition?
647 * If so, save partition ID.
649 if (!part
|| info
->bootable
)
652 /* Best possible partition? Stop searching. */
657 * We now need to search further for best possible.
658 * If we what we just queried was the best so far,
659 * save the info since we over-write it next loop.
664 /* If we found any acceptable partition */
667 * If we searched all possible partition IDs,
668 * return the first valid partition we found.
670 if (p
== MAX_SEARCH_PARTITIONS
+ 1)
673 printf("** No valid partitions found **\n");
678 if (strncmp((char *)info
->type
, BOOT_PART_TYPE
, sizeof(info
->type
)) != 0) {
679 printf("** Invalid partition type \"%.32s\""
680 " (expect \"" BOOT_PART_TYPE
"\")\n",
686 (*dev_desc
)->log2blksz
= LOG2((*dev_desc
)->blksz
);