]>
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+
14 #include <ubifs_uboot.h>
19 #define PRINTF(fmt,args...) printf (fmt ,##args)
21 #define PRINTF(fmt,args...)
26 struct blk_desc
* (*get_dev
)(int dev
);
27 int (*select_hwpart
)(int dev_num
, int hwpart
);
30 static const struct block_drvr block_drvr
[] = {
31 #if defined(CONFIG_CMD_IDE)
32 { .name
= "ide", .get_dev
= ide_get_dev
, },
34 #if defined(CONFIG_CMD_SATA)
35 {.name
= "sata", .get_dev
= sata_get_dev
, },
37 #if defined(CONFIG_CMD_SCSI)
38 { .name
= "scsi", .get_dev
= scsi_get_dev
, },
40 #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
41 { .name
= "usb", .get_dev
= usb_stor_get_dev
, },
43 #if defined(CONFIG_MMC)
46 .get_dev
= mmc_get_dev
,
47 .select_hwpart
= mmc_select_hwpart
,
50 #if defined(CONFIG_SYSTEMACE)
51 { .name
= "ace", .get_dev
= systemace_get_dev
, },
53 #if defined(CONFIG_SANDBOX)
54 { .name
= "host", .get_dev
= host_get_dev
, },
59 DECLARE_GLOBAL_DATA_PTR
;
61 #ifdef HAVE_BLOCK_DEVICE
62 static struct part_driver
*part_driver_lookup_type(int part_type
)
64 struct part_driver
*drv
=
65 ll_entry_start(struct part_driver
, part_driver
);
66 const int n_ents
= ll_entry_count(struct part_driver
, part_driver
);
67 struct part_driver
*entry
;
69 for (entry
= drv
; entry
!= drv
+ n_ents
; entry
++) {
70 if (part_type
== entry
->part_type
)
78 static struct blk_desc
*get_dev_hwpart(const char *ifname
, int dev
, int hwpart
)
80 const struct block_drvr
*drvr
= block_drvr
;
81 struct blk_desc
* (*reloc_get_dev
)(int dev
);
82 int (*select_hwpart
)(int dev_num
, int hwpart
);
90 #ifdef CONFIG_NEEDS_MANUAL_RELOC
91 name
+= gd
->reloc_off
;
95 reloc_get_dev
= drvr
->get_dev
;
96 select_hwpart
= drvr
->select_hwpart
;
97 #ifdef CONFIG_NEEDS_MANUAL_RELOC
98 name
+= gd
->reloc_off
;
99 reloc_get_dev
+= gd
->reloc_off
;
101 select_hwpart
+= gd
->reloc_off
;
103 if (strncmp(ifname
, name
, strlen(name
)) == 0) {
104 struct blk_desc
*dev_desc
= reloc_get_dev(dev
);
107 if (hwpart
== 0 && !select_hwpart
)
111 ret
= select_hwpart(dev_desc
->dev
, hwpart
);
121 struct blk_desc
*blk_get_dev(const char *ifname
, int dev
)
123 return get_dev_hwpart(ifname
, dev
, 0);
126 struct blk_desc
*get_dev_hwpart(const char *ifname
, int dev
, int hwpart
)
131 struct blk_desc
*blk_get_dev(const char *ifname
, int dev
)
137 #ifdef HAVE_BLOCK_DEVICE
139 /* ------------------------------------------------------------------------- */
141 * reports device info to the user
145 typedef uint64_t lba512_t
;
147 typedef lbaint_t lba512_t
;
151 * Overflowless variant of (block_count * mul_by / div_by)
152 * when div_by > mul_by
154 static lba512_t
lba512_muldiv(lba512_t block_count
, lba512_t mul_by
, lba512_t div_by
)
156 lba512_t bc_quot
, bc_rem
;
158 /* x * m / d == x / d * m + (x % d) * m / d */
159 bc_quot
= block_count
/ div_by
;
160 bc_rem
= block_count
- div_by
* bc_quot
;
161 return bc_quot
* mul_by
+ (bc_rem
* mul_by
) / div_by
;
164 void dev_print (struct blk_desc
*dev_desc
)
166 lba512_t lba512
; /* number of blocks if 512bytes block size */
168 if (dev_desc
->type
== DEV_TYPE_UNKNOWN
) {
169 puts ("not available\n");
173 switch (dev_desc
->if_type
) {
175 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
176 dev_desc
->target
,dev_desc
->lun
,
184 printf ("Model: %s Firm: %s Ser#: %s\n",
192 printf ("Vendor: %s Rev: %s Prod: %s\n",
198 puts("device type DOC\n");
200 case IF_TYPE_UNKNOWN
:
201 puts("device type unknown\n");
204 printf("Unhandled device type: %i\n", dev_desc
->if_type
);
208 if (dev_desc
->removable
)
210 switch (dev_desc
->type
& 0x1F) {
211 case DEV_TYPE_HARDDISK
:
217 case DEV_TYPE_OPDISK
:
218 puts ("Optical Device");
224 printf ("# %02X #", dev_desc
->type
& 0x1F);
228 if (dev_desc
->lba
> 0L && dev_desc
->blksz
> 0L) {
229 ulong mb
, mb_quot
, mb_rem
, gb
, gb_quot
, gb_rem
;
234 lba512
= (lba
* (dev_desc
->blksz
/512));
235 /* round to 1 digit */
236 /* 2048 = (1024 * 1024) / 512 MB */
237 mb
= lba512_muldiv(lba512
, 10, 2048);
240 mb_rem
= mb
- (10 * mb_quot
);
244 gb_rem
= gb
- (10 * gb_quot
);
247 printf (" Supports 48-bit addressing\n");
249 #if defined(CONFIG_SYS_64BIT_LBA)
250 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n",
256 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
263 puts (" Capacity: not available\n");
268 #ifdef HAVE_BLOCK_DEVICE
270 void part_init(struct blk_desc
*dev_desc
)
272 struct part_driver
*drv
=
273 ll_entry_start(struct part_driver
, part_driver
);
274 const int n_ents
= ll_entry_count(struct part_driver
, part_driver
);
275 struct part_driver
*entry
;
277 dev_desc
->part_type
= PART_TYPE_UNKNOWN
;
278 for (entry
= drv
; entry
!= drv
+ n_ents
; entry
++) {
281 ret
= entry
->test(dev_desc
);
282 debug("%s: try '%s': ret=%d\n", __func__
, entry
->name
, ret
);
284 dev_desc
->part_type
= entry
->part_type
;
290 static void print_part_header(const char *type
, struct blk_desc
*dev_desc
)
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)
297 puts ("\nPartition Map for ");
298 switch (dev_desc
->if_type
) {
327 printf (" device %d -- Partition Type: %s\n\n",
328 dev_desc
->dev
, type
);
329 #endif /* any CONFIG_..._PARTITION */
332 void part_print(struct blk_desc
*dev_desc
)
334 struct part_driver
*drv
;
336 drv
= part_driver_lookup_type(dev_desc
->part_type
);
338 printf("## Unknown partition table type %x\n",
339 dev_desc
->part_type
);
343 PRINTF("## Testing for valid %s partition ##\n", drv
->name
);
344 print_part_header(drv
->name
, dev_desc
);
346 drv
->print(dev_desc
);
349 #endif /* HAVE_BLOCK_DEVICE */
351 int part_get_info(struct blk_desc
*dev_desc
, int part
,
352 disk_partition_t
*info
)
354 #ifdef HAVE_BLOCK_DEVICE
355 struct part_driver
*drv
;
357 #ifdef CONFIG_PARTITION_UUIDS
358 /* The common case is no UUID support */
361 #ifdef CONFIG_PARTITION_TYPE_GUID
362 info
->type_guid
[0] = 0;
365 drv
= part_driver_lookup_type(dev_desc
->part_type
);
367 debug("## Unknown partition table type %x\n",
368 dev_desc
->part_type
);
369 return -EPROTONOSUPPORT
;
371 if (!drv
->get_info
) {
372 PRINTF("## Driver %s does not have the get_info() method\n");
375 if (drv
->get_info(dev_desc
, part
, info
) == 0) {
376 PRINTF("## Valid %s partition found ##\n", drv
->name
);
379 #endif /* HAVE_BLOCK_DEVICE */
384 int blk_get_device_by_str(const char *ifname
, const char *dev_hwpart_str
,
385 struct blk_desc
**dev_desc
)
388 char *dup_str
= NULL
;
389 const char *dev_str
, *hwpart_str
;
392 hwpart_str
= strchr(dev_hwpart_str
, '.');
394 dup_str
= strdup(dev_hwpart_str
);
395 dup_str
[hwpart_str
- dev_hwpart_str
] = 0;
399 dev_str
= dev_hwpart_str
;
403 dev
= simple_strtoul(dev_str
, &ep
, 16);
405 printf("** Bad device specification %s %s **\n",
412 hwpart
= simple_strtoul(hwpart_str
, &ep
, 16);
414 printf("** Bad HW partition specification %s %s **\n",
421 *dev_desc
= get_dev_hwpart(ifname
, dev
, hwpart
);
422 if (!(*dev_desc
) || ((*dev_desc
)->type
== DEV_TYPE_UNKNOWN
)) {
423 printf("** Bad device %s %s **\n", ifname
, dev_hwpart_str
);
428 #ifdef HAVE_BLOCK_DEVICE
430 * Updates the partition table for the specified hw partition.
431 * Does not need to be done for hwpart 0 since it is default and
435 part_init(*dev_desc
);
443 #define PART_UNSPECIFIED -2
445 #define MAX_SEARCH_PARTITIONS 16
446 int blk_get_device_part_str(const char *ifname
, const char *dev_part_str
,
447 struct blk_desc
**dev_desc
,
448 disk_partition_t
*info
, int allow_whole_dev
)
451 const char *part_str
;
452 char *dup_str
= NULL
;
458 disk_partition_t tmpinfo
;
460 #if defined CONFIG_SANDBOX && defined CONFIG_CMD_UBIFS
461 #error Only one of CONFIG_SANDBOX and CONFIG_CMD_UBIFS may be selected
464 #ifdef CONFIG_SANDBOX
466 * Special-case a pseudo block device "hostfs", to allow access to the
467 * host's own filesystem.
469 if (0 == strcmp(ifname
, "hostfs")) {
475 strcpy((char *)info
->type
, BOOT_PART_TYPE
);
476 strcpy((char *)info
->name
, "Sandbox host");
477 #ifdef CONFIG_PARTITION_UUIDS
480 #ifdef CONFIG_PARTITION_TYPE_GUID
481 info
->type_guid
[0] = 0;
488 #ifdef CONFIG_CMD_UBIFS
490 * Special-case ubi, ubi goes through a mtd, rathen then through
491 * a regular block device.
493 if (0 == strcmp(ifname
, "ubi")) {
494 if (!ubifs_is_mounted()) {
495 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
500 memset(info
, 0, sizeof(*info
));
501 strcpy((char *)info
->type
, BOOT_PART_TYPE
);
502 strcpy((char *)info
->name
, "UBI");
503 #ifdef CONFIG_PARTITION_UUIDS
510 /* If no dev_part_str, use bootdevice environment variable */
511 if (!dev_part_str
|| !strlen(dev_part_str
) ||
512 !strcmp(dev_part_str
, "-"))
513 dev_part_str
= getenv("bootdevice");
515 /* If still no dev_part_str, it's an error */
517 printf("** No device specified **\n");
521 /* Separate device and partition ID specification */
522 part_str
= strchr(dev_part_str
, ':');
524 dup_str
= strdup(dev_part_str
);
525 dup_str
[part_str
- dev_part_str
] = 0;
529 dev_str
= dev_part_str
;
532 /* Look up the device */
533 dev
= blk_get_device_by_str(ifname
, dev_str
, dev_desc
);
537 /* Convert partition ID string to number */
538 if (!part_str
|| !*part_str
) {
539 part
= PART_UNSPECIFIED
;
540 } else if (!strcmp(part_str
, "auto")) {
543 /* Something specified -> use exactly that */
544 part
= (int)simple_strtoul(part_str
, &ep
, 16);
546 * Less than whole string converted,
547 * or request for whole device, but caller requires partition.
549 if (*ep
|| (part
== 0 && !allow_whole_dev
)) {
550 printf("** Bad partition specification %s %s **\n",
551 ifname
, dev_part_str
);
557 * No partition table on device,
558 * or user requested partition 0 (entire device).
560 if (((*dev_desc
)->part_type
== PART_TYPE_UNKNOWN
) ||
562 if (!(*dev_desc
)->lba
) {
563 printf("** Bad device size - %s %s **\n", ifname
,
569 * If user specified a partition ID other than 0,
570 * or the calling command only accepts partitions,
573 if ((part
> 0) || (!allow_whole_dev
)) {
574 printf("** No partition table - %s %s **\n", ifname
,
579 (*dev_desc
)->log2blksz
= LOG2((*dev_desc
)->blksz
);
582 info
->size
= (*dev_desc
)->lba
;
583 info
->blksz
= (*dev_desc
)->blksz
;
585 strcpy((char *)info
->type
, BOOT_PART_TYPE
);
586 strcpy((char *)info
->name
, "Whole Disk");
587 #ifdef CONFIG_PARTITION_UUIDS
590 #ifdef CONFIG_PARTITION_TYPE_GUID
591 info
->type_guid
[0] = 0;
599 * Now there's known to be a partition table,
600 * not specifying a partition means to pick partition 1.
602 if (part
== PART_UNSPECIFIED
)
606 * If user didn't specify a partition number, or did specify something
607 * other than "auto", use that partition number directly.
609 if (part
!= PART_AUTO
) {
610 ret
= part_get_info(*dev_desc
, part
, info
);
612 printf("** Invalid partition %d **\n", part
);
617 * Find the first bootable partition.
618 * If none are bootable, fall back to the first valid partition.
621 for (p
= 1; p
<= MAX_SEARCH_PARTITIONS
; p
++) {
622 ret
= part_get_info(*dev_desc
, p
, info
);
627 * First valid partition, or new better partition?
628 * If so, save partition ID.
630 if (!part
|| info
->bootable
)
633 /* Best possible partition? Stop searching. */
638 * We now need to search further for best possible.
639 * If we what we just queried was the best so far,
640 * save the info since we over-write it next loop.
645 /* If we found any acceptable partition */
648 * If we searched all possible partition IDs,
649 * return the first valid partition we found.
651 if (p
== MAX_SEARCH_PARTITIONS
+ 1)
654 printf("** No valid partitions found **\n");
659 if (strncmp((char *)info
->type
, BOOT_PART_TYPE
, sizeof(info
->type
)) != 0) {
660 printf("** Invalid partition type \"%.32s\""
661 " (expect \"" BOOT_PART_TYPE
"\")\n",
667 (*dev_desc
)->log2blksz
= LOG2((*dev_desc
)->blksz
);