]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - disk/part.c
1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
16 #include <ubifs_uboot.h>
21 #define PRINTF(fmt,args...) printf (fmt ,##args)
23 #define PRINTF(fmt,args...)
26 /* Check all partition types */
27 #define PART_TYPE_ALL -1
30 * part_driver_get_type() - Get a driver given its type
32 * @part_type: Partition type to find the driver for
33 * Return: Driver for that type, or NULL if none
35 static struct part_driver
*part_driver_get_type(int part_type
)
37 struct part_driver
*drv
=
38 ll_entry_start(struct part_driver
, part_driver
);
39 const int n_ents
= ll_entry_count(struct part_driver
, part_driver
);
40 struct part_driver
*entry
;
42 for (entry
= drv
; entry
!= drv
+ n_ents
; entry
++) {
43 if (part_type
== entry
->part_type
)
52 * part_driver_lookup_type() - Look up the partition driver for a blk device
54 * If @desc->part_type is PART_TYPE_UNKNOWN, this checks each parition driver
55 * against the blk device to see if there is a valid partition table acceptable
58 * If @desc->part_type is already set, it just returns the driver for that
59 * type, without testing if the driver can find a valid partition on the
62 * On success it updates @desc->part_type if set to PART_TYPE_UNKNOWN on entry
64 * @dev_desc: Device descriptor
65 * Return: Driver found, or NULL if none
67 static struct part_driver
*part_driver_lookup_type(struct blk_desc
*desc
)
69 struct part_driver
*drv
=
70 ll_entry_start(struct part_driver
, part_driver
);
71 const int n_ents
= ll_entry_count(struct part_driver
, part_driver
);
72 struct part_driver
*entry
;
74 if (desc
->part_type
== PART_TYPE_UNKNOWN
) {
75 for (entry
= drv
; entry
!= drv
+ n_ents
; entry
++) {
78 ret
= entry
->test(desc
);
80 desc
->part_type
= entry
->part_type
;
85 return part_driver_get_type(desc
->part_type
);
92 int part_get_type_by_name(const char *name
)
94 struct part_driver
*drv
=
95 ll_entry_start(struct part_driver
, part_driver
);
96 const int n_ents
= ll_entry_count(struct part_driver
, part_driver
);
97 struct part_driver
*entry
;
99 for (entry
= drv
; entry
!= drv
+ n_ents
; entry
++) {
100 if (!strcasecmp(name
, entry
->name
))
101 return entry
->part_type
;
105 return PART_TYPE_UNKNOWN
;
109 * get_dev_hwpart() - Get the descriptor for a device with hardware partitions
111 * @ifname: Interface name (e.g. "ide", "scsi")
112 * @dev: Device number (0 for first device on that interface, 1 for
114 * @hwpart: Hardware partition, or 0 if none (used for MMC)
115 * Return: pointer to the block device, or NULL if not available, or an
118 static struct blk_desc
*get_dev_hwpart(const char *ifname
, int dev
, int hwpart
)
120 struct blk_desc
*desc
;
125 desc
= blk_get_devnum_by_uclass_idname(ifname
, dev
);
127 debug("%s: No device for iface '%s', dev %d\n", __func__
,
131 ret
= blk_dselect_hwpart(desc
, hwpart
);
133 debug("%s: Failed to select h/w partition: err-%d\n", __func__
,
141 struct blk_desc
*blk_get_dev(const char *ifname
, int dev
)
146 return get_dev_hwpart(ifname
, dev
, 0);
149 /* ------------------------------------------------------------------------- */
151 * reports device info to the user
155 typedef uint64_t lba512_t
;
157 typedef lbaint_t lba512_t
;
161 * Overflowless variant of (block_count * mul_by / 2**right_shift)
162 * when 2**right_shift > mul_by
164 static lba512_t
lba512_muldiv(lba512_t block_count
, lba512_t mul_by
,
167 lba512_t bc_quot
, bc_rem
;
169 /* x * m / d == x / d * m + (x % d) * m / d */
170 bc_quot
= block_count
>> right_shift
;
171 bc_rem
= block_count
- (bc_quot
<< right_shift
);
172 return bc_quot
* mul_by
+ ((bc_rem
* mul_by
) >> right_shift
);
175 void dev_print(struct blk_desc
*desc
)
177 lba512_t lba512
; /* number of blocks if 512bytes block size */
179 if (desc
->type
== DEV_TYPE_UNKNOWN
) {
180 puts ("not available\n");
184 switch (desc
->uclass_id
) {
186 printf("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n", desc
->target
,
187 desc
->lun
, desc
->vendor
, desc
->product
, desc
->revision
);
191 printf("Model: %s Firm: %s Ser#: %s\n", desc
->vendor
,
192 desc
->revision
, desc
->product
);
200 printf ("Vendor: %s Rev: %s Prod: %s\n",
206 printf("%s VirtIO Block Device\n", desc
->vendor
);
208 case UCLASS_EFI_MEDIA
:
209 printf("EFI media Block Device %d\n", desc
->devnum
);
212 puts("device type unknown\n");
215 printf("Unhandled device type: %i\n", desc
->uclass_id
);
221 switch (desc
->type
& 0x1F) {
222 case DEV_TYPE_HARDDISK
:
228 case DEV_TYPE_OPDISK
:
229 puts ("Optical Device");
235 printf("# %02X #", desc
->type
& 0x1F);
239 if (desc
->lba
> 0L && desc
->blksz
> 0L) {
240 ulong mb
, mb_quot
, mb_rem
, gb
, gb_quot
, gb_rem
;
245 lba512
= lba
* (desc
->blksz
/ 512);
246 /* round to 1 digit */
247 /* 2048 = (1024 * 1024) / 512 MB */
248 mb
= lba512_muldiv(lba512
, 10, 11);
251 mb_rem
= mb
- (10 * mb_quot
);
255 gb_rem
= gb
- (10 * gb_quot
);
258 printf (" Supports 48-bit addressing\n");
260 #if defined(CONFIG_SYS_64BIT_LBA)
261 printf (" Capacity: %lu.%lu MB = %lu.%lu GB (%llu x %lu)\n",
267 printf (" Capacity: %lu.%lu MB = %lu.%lu GB (%lu x %lu)\n",
274 puts (" Capacity: not available\n");
278 void part_init(struct blk_desc
*desc
)
280 struct part_driver
*drv
=
281 ll_entry_start(struct part_driver
, part_driver
);
282 const int n_ents
= ll_entry_count(struct part_driver
, part_driver
);
283 struct part_driver
*entry
;
285 blkcache_invalidate(desc
->uclass_id
, desc
->devnum
);
287 desc
->part_type
= PART_TYPE_UNKNOWN
;
288 for (entry
= drv
; entry
!= drv
+ n_ents
; entry
++) {
291 ret
= entry
->test(desc
);
292 debug("%s: try '%s': ret=%d\n", __func__
, entry
->name
, ret
);
294 desc
->part_type
= entry
->part_type
;
300 static void print_part_header(const char *type
, struct blk_desc
*desc
)
302 #if CONFIG_IS_ENABLED(MAC_PARTITION) || \
303 CONFIG_IS_ENABLED(DOS_PARTITION) || \
304 CONFIG_IS_ENABLED(ISO_PARTITION) || \
305 CONFIG_IS_ENABLED(AMIGA_PARTITION) || \
306 CONFIG_IS_ENABLED(EFI_PARTITION)
307 puts ("\nPartition Map for ");
308 switch (desc
->uclass_id
) {
336 case UCLASS_EFI_MEDIA
:
343 printf (" device %d -- Partition Type: %s\n\n",
345 #endif /* any CONFIG_..._PARTITION */
348 void part_print(struct blk_desc
*desc
)
350 struct part_driver
*drv
;
352 drv
= part_driver_lookup_type(desc
);
354 printf("## Unknown partition table type %x\n",
359 PRINTF("## Testing for valid %s partition ##\n", drv
->name
);
360 print_part_header(drv
->name
, desc
);
365 int part_get_info_by_type(struct blk_desc
*desc
, int part
, int part_type
,
366 struct disk_partition
*info
)
368 struct part_driver
*drv
;
371 /* The common case is no UUID support */
372 disk_partition_clr_uuid(info
);
373 #ifdef CONFIG_PARTITION_TYPE_GUID
374 info
->type_guid
[0] = 0;
377 if (part_type
== PART_TYPE_UNKNOWN
) {
378 drv
= part_driver_lookup_type(desc
);
380 drv
= part_driver_get_type(part_type
);
384 debug("## Unknown partition table type %x\n",
386 return -EPROTONOSUPPORT
;
388 if (!drv
->get_info
) {
389 PRINTF("## Driver %s does not have the get_info() method\n",
393 if (drv
->get_info(desc
, part
, info
) == 0) {
394 PRINTF("## Valid %s partition found ##\n", drv
->name
);
402 int part_get_info(struct blk_desc
*desc
, int part
,
403 struct disk_partition
*info
)
405 return part_get_info_by_type(desc
, part
, PART_TYPE_UNKNOWN
, info
);
408 int part_get_info_whole_disk(struct blk_desc
*desc
,
409 struct disk_partition
*info
)
412 info
->size
= desc
->lba
;
413 info
->blksz
= desc
->blksz
;
415 strcpy((char *)info
->type
, BOOT_PART_TYPE
);
416 strcpy((char *)info
->name
, "Whole Disk");
417 disk_partition_clr_uuid(info
);
418 #ifdef CONFIG_PARTITION_TYPE_GUID
419 info
->type_guid
[0] = 0;
425 int blk_get_device_by_str(const char *ifname
, const char *dev_hwpart_str
,
426 struct blk_desc
**desc
)
429 char *dup_str
= NULL
;
430 const char *dev_str
, *hwpart_str
;
433 hwpart_str
= strchr(dev_hwpart_str
, '.');
435 dup_str
= strdup(dev_hwpart_str
);
436 dup_str
[hwpart_str
- dev_hwpart_str
] = 0;
440 dev_str
= dev_hwpart_str
;
444 dev
= hextoul(dev_str
, &ep
);
446 printf("** Bad device specification %s %s **\n",
453 hwpart
= hextoul(hwpart_str
, &ep
);
455 printf("** Bad HW partition specification %s %s **\n",
462 *desc
= get_dev_hwpart(ifname
, dev
, hwpart
);
463 if (!(*desc
) || ((*desc
)->type
== DEV_TYPE_UNKNOWN
)) {
464 debug("** Bad device %s %s **\n", ifname
, dev_hwpart_str
);
471 * Updates the partition table for the specified hw partition.
472 * Always should be done, otherwise hw partition 0 will return
473 * stale data after displaying a non-zero hw partition.
475 if ((*desc
)->uclass_id
== UCLASS_MMC
)
484 #define PART_UNSPECIFIED -2
486 int blk_get_device_part_str(const char *ifname
, const char *dev_part_str
,
487 struct blk_desc
**desc
,
488 struct disk_partition
*info
, int allow_whole_dev
)
491 const char *part_str
;
492 char *dup_str
= NULL
;
498 struct disk_partition tmpinfo
;
501 memset(info
, 0, sizeof(*info
));
503 #if IS_ENABLED(CONFIG_SANDBOX) || IS_ENABLED(CONFIG_SEMIHOSTING)
505 * Special-case a pseudo block device "hostfs", to allow access to the
506 * host's own filesystem.
508 if (!strcmp(ifname
, "hostfs")) {
509 strcpy((char *)info
->type
, BOOT_PART_TYPE
);
510 strcpy((char *)info
->name
, "Host filesystem");
516 #if IS_ENABLED(CONFIG_CMD_UBIFS) && !IS_ENABLED(CONFIG_SPL_BUILD)
518 * Special-case ubi, ubi goes through a mtd, rather than through
519 * a regular block device.
521 if (!strcmp(ifname
, "ubi")) {
522 if (!ubifs_is_mounted()) {
523 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
527 strcpy((char *)info
->type
, BOOT_PART_TYPE
);
528 strcpy((char *)info
->name
, "UBI");
533 /* If no dev_part_str, use bootdevice environment variable */
534 if (CONFIG_IS_ENABLED(ENV_SUPPORT
)) {
535 if (!dev_part_str
|| !strlen(dev_part_str
) ||
536 !strcmp(dev_part_str
, "-"))
537 dev_part_str
= env_get("bootdevice");
540 /* If still no dev_part_str, it's an error */
542 printf("** No device specified **\n");
547 /* Separate device and partition ID specification */
548 part_str
= strchr(dev_part_str
, ':');
550 dup_str
= strdup(dev_part_str
);
551 dup_str
[part_str
- dev_part_str
] = 0;
555 dev_str
= dev_part_str
;
558 /* Look up the device */
559 dev
= blk_get_device_by_str(ifname
, dev_str
, desc
);
561 printf("** Bad device specification %s %s **\n",
567 /* Convert partition ID string to number */
568 if (!part_str
|| !*part_str
) {
569 part
= PART_UNSPECIFIED
;
570 } else if (!strcmp(part_str
, "auto")) {
573 /* Something specified -> use exactly that */
574 part
= (int)hextoul(part_str
, &ep
);
576 * Less than whole string converted,
577 * or request for whole device, but caller requires partition.
579 if (*ep
|| (part
== 0 && !allow_whole_dev
)) {
580 printf("** Bad partition specification %s %s **\n",
581 ifname
, dev_part_str
);
588 * No partition table on device,
589 * or user requested partition 0 (entire device).
591 if (((*desc
)->part_type
== PART_TYPE_UNKNOWN
) || !part
) {
593 printf("** Bad device size - %s %s **\n", ifname
,
600 * If user specified a partition ID other than 0,
601 * or the calling command only accepts partitions,
604 if ((part
> 0) || (!allow_whole_dev
)) {
605 printf("** No partition table - %s %s **\n", ifname
,
607 ret
= -EPROTONOSUPPORT
;
611 (*desc
)->log2blksz
= LOG2((*desc
)->blksz
);
613 part_get_info_whole_disk(*desc
, info
);
620 * Now there's known to be a partition table,
621 * not specifying a partition means to pick partition 1.
623 if (part
== PART_UNSPECIFIED
)
627 * If user didn't specify a partition number, or did specify something
628 * other than "auto", use that partition number directly.
630 if (part
!= PART_AUTO
) {
631 ret
= part_get_info(*desc
, part
, info
);
633 printf("** Invalid partition %d **\n", part
);
638 * Find the first bootable partition.
639 * If none are bootable, fall back to the first valid partition.
642 for (p
= 1; p
<= MAX_SEARCH_PARTITIONS
; p
++) {
643 ret
= part_get_info(*desc
, p
, info
);
648 * First valid partition, or new better partition?
649 * If so, save partition ID.
651 if (!part
|| info
->bootable
)
654 /* Best possible partition? Stop searching. */
659 * We now need to search further for best possible.
660 * If we what we just queried was the best so far,
661 * save the info since we over-write it next loop.
666 /* If we found any acceptable partition */
669 * If we searched all possible partition IDs,
670 * return the first valid partition we found.
672 if (p
== MAX_SEARCH_PARTITIONS
+ 1)
675 printf("** No valid partitions found **\n");
679 if (strncmp((char *)info
->type
, BOOT_PART_TYPE
, sizeof(info
->type
)) != 0) {
680 printf("** Invalid partition type \"%.32s\""
681 " (expect \"" BOOT_PART_TYPE
"\")\n",
687 (*desc
)->log2blksz
= LOG2((*desc
)->blksz
);
697 int part_get_info_by_name(struct blk_desc
*desc
, const char *name
,
698 struct disk_partition
*info
)
700 struct part_driver
*part_drv
;
704 part_drv
= part_driver_lookup_type(desc
);
708 if (!part_drv
->get_info
) {
709 log_debug("## Driver %s does not have the get_info() method\n",
714 for (i
= 1; i
< part_drv
->max_entries
; i
++) {
715 ret
= part_drv
->get_info(desc
, i
, info
);
717 /* no more entries in table */
720 if (strcmp(name
, (const char *)info
->name
) == 0) {
730 * Get partition info from device number and partition name.
732 * Parse a device number and partition name string in the form of
733 * "devicenum.hwpartnum#partition_name", for example "0.1#misc". devicenum and
734 * hwpartnum are both optional, defaulting to 0. If the partition is found,
735 * sets desc and part_info accordingly with the information of the
736 * partition with the given partition_name.
738 * @param[in] dev_iface Device interface
739 * @param[in] dev_part_str Input string argument, like "0.1#misc"
740 * @param[out] desc Place to store the device description pointer
741 * @param[out] part_info Place to store the partition information
742 * Return: 0 on success, or a negative on error
744 static int part_get_info_by_dev_and_name(const char *dev_iface
,
745 const char *dev_part_str
,
746 struct blk_desc
**desc
,
747 struct disk_partition
*part_info
)
749 char *dup_str
= NULL
;
750 const char *dev_str
, *part_str
;
753 /* Separate device and partition name specification */
755 part_str
= strchr(dev_part_str
, '#');
760 dup_str
= strdup(dev_part_str
);
761 dup_str
[part_str
- dev_part_str
] = 0;
768 ret
= blk_get_device_by_str(dev_iface
, dev_str
, desc
);
772 ret
= part_get_info_by_name(*desc
, part_str
, part_info
);
774 printf("Could not find \"%s\" partition\n", part_str
);
781 int part_get_info_by_dev_and_name_or_num(const char *dev_iface
,
782 const char *dev_part_str
,
783 struct blk_desc
**desc
,
784 struct disk_partition
*part_info
,
789 /* Split the part_name if passed as "$dev_num#part_name". */
790 ret
= part_get_info_by_dev_and_name(dev_iface
, dev_part_str
, desc
,
795 * Couldn't lookup by name, try looking up the partition description
798 ret
= blk_get_device_part_str(dev_iface
, dev_part_str
, desc
, part_info
,
801 printf("Couldn't find partition %s %s\n",
802 dev_iface
, dev_part_str
);
806 void part_set_generic_name(const struct blk_desc
*desc
, int part_num
,
811 switch (desc
->uclass_id
) {
830 sprintf(name
, "%s%c%d", devtype
, 'a' + desc
->devnum
, part_num
);
833 int part_get_bootable(struct blk_desc
*desc
)
835 struct disk_partition info
;
838 for (p
= 1; p
<= MAX_SEARCH_PARTITIONS
; p
++) {
841 ret
= part_get_info(desc
, p
, &info
);
842 if (!ret
&& info
.bootable
)