]>
git.ipfire.org Git - thirdparty/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...)
24 const struct block_drvr block_drvr
[] = {
25 #if defined(CONFIG_CMD_IDE)
28 #if defined(CONFIG_CMD_SATA)
29 {.name
= "sata", .get_dev
= sata_get_dev
, },
31 #if defined(CONFIG_SCSI)
32 { .name
= "scsi", .get_dev
= scsi_get_dev
, },
34 #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
37 #if defined(CONFIG_MMC)
40 .get_dev
= mmc_get_dev
,
41 .select_hwpart
= mmc_select_hwpart
,
44 #if defined(CONFIG_SYSTEMACE)
45 { .name
= "ace", .get_dev
= systemace_get_dev
, },
47 #if defined(CONFIG_SANDBOX)
48 { .name
= "host", .get_dev
= host_get_dev
, },
53 DECLARE_GLOBAL_DATA_PTR
;
55 #ifdef HAVE_BLOCK_DEVICE
56 static struct part_driver
*part_driver_lookup_type(int part_type
)
58 struct part_driver
*drv
=
59 ll_entry_start(struct part_driver
, part_driver
);
60 const int n_ents
= ll_entry_count(struct part_driver
, part_driver
);
61 struct part_driver
*entry
;
63 for (entry
= drv
; entry
!= drv
+ n_ents
; entry
++) {
64 if (part_type
== entry
->part_type
)
72 static struct blk_desc
*get_dev_hwpart(const char *ifname
, int dev
, int hwpart
)
74 const struct block_drvr
*drvr
= block_drvr
;
75 int (*select_hwpart
)(int dev_num
, int hwpart
);
83 #ifdef CONFIG_NEEDS_MANUAL_RELOC
84 name
+= gd
->reloc_off
;
88 select_hwpart
= drvr
->select_hwpart
;
89 #ifdef CONFIG_NEEDS_MANUAL_RELOC
90 name
+= gd
->reloc_off
;
92 select_hwpart
+= gd
->reloc_off
;
94 if (strncmp(ifname
, name
, strlen(name
)) == 0) {
95 struct blk_desc
*dev_desc
;
97 dev_desc
= blk_get_devnum_by_typename(name
, dev
);
100 if (hwpart
== 0 && !select_hwpart
)
104 ret
= select_hwpart(dev_desc
->devnum
, hwpart
);
114 struct blk_desc
*blk_get_dev(const char *ifname
, int dev
)
116 return get_dev_hwpart(ifname
, dev
, 0);
119 struct blk_desc
*get_dev_hwpart(const char *ifname
, int dev
, int hwpart
)
124 struct blk_desc
*blk_get_dev(const char *ifname
, int dev
)
130 #ifdef HAVE_BLOCK_DEVICE
132 /* ------------------------------------------------------------------------- */
134 * reports device info to the user
138 typedef uint64_t lba512_t
;
140 typedef lbaint_t lba512_t
;
144 * Overflowless variant of (block_count * mul_by / div_by)
145 * when div_by > mul_by
147 static lba512_t
lba512_muldiv(lba512_t block_count
, lba512_t mul_by
, lba512_t div_by
)
149 lba512_t bc_quot
, bc_rem
;
151 /* x * m / d == x / d * m + (x % d) * m / d */
152 bc_quot
= block_count
/ div_by
;
153 bc_rem
= block_count
- div_by
* bc_quot
;
154 return bc_quot
* mul_by
+ (bc_rem
* mul_by
) / div_by
;
157 void dev_print (struct blk_desc
*dev_desc
)
159 lba512_t lba512
; /* number of blocks if 512bytes block size */
161 if (dev_desc
->type
== DEV_TYPE_UNKNOWN
) {
162 puts ("not available\n");
166 switch (dev_desc
->if_type
) {
168 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
169 dev_desc
->target
,dev_desc
->lun
,
177 printf ("Model: %s Firm: %s Ser#: %s\n",
185 printf ("Vendor: %s Rev: %s Prod: %s\n",
191 puts("device type DOC\n");
193 case IF_TYPE_UNKNOWN
:
194 puts("device type unknown\n");
197 printf("Unhandled device type: %i\n", dev_desc
->if_type
);
201 if (dev_desc
->removable
)
203 switch (dev_desc
->type
& 0x1F) {
204 case DEV_TYPE_HARDDISK
:
210 case DEV_TYPE_OPDISK
:
211 puts ("Optical Device");
217 printf ("# %02X #", dev_desc
->type
& 0x1F);
221 if (dev_desc
->lba
> 0L && dev_desc
->blksz
> 0L) {
222 ulong mb
, mb_quot
, mb_rem
, gb
, gb_quot
, gb_rem
;
227 lba512
= (lba
* (dev_desc
->blksz
/512));
228 /* round to 1 digit */
229 /* 2048 = (1024 * 1024) / 512 MB */
230 mb
= lba512_muldiv(lba512
, 10, 2048);
233 mb_rem
= mb
- (10 * mb_quot
);
237 gb_rem
= gb
- (10 * gb_quot
);
240 printf (" Supports 48-bit addressing\n");
242 #if defined(CONFIG_SYS_64BIT_LBA)
243 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n",
249 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
256 puts (" Capacity: not available\n");
261 #ifdef HAVE_BLOCK_DEVICE
263 void part_init(struct blk_desc
*dev_desc
)
265 struct part_driver
*drv
=
266 ll_entry_start(struct part_driver
, part_driver
);
267 const int n_ents
= ll_entry_count(struct part_driver
, part_driver
);
268 struct part_driver
*entry
;
270 blkcache_invalidate(dev_desc
->if_type
, dev_desc
->devnum
);
272 dev_desc
->part_type
= PART_TYPE_UNKNOWN
;
273 for (entry
= drv
; entry
!= drv
+ n_ents
; entry
++) {
276 ret
= entry
->test(dev_desc
);
277 debug("%s: try '%s': ret=%d\n", __func__
, entry
->name
, ret
);
279 dev_desc
->part_type
= entry
->part_type
;
285 static void print_part_header(const char *type
, struct blk_desc
*dev_desc
)
287 #if defined(CONFIG_MAC_PARTITION) || \
288 defined(CONFIG_DOS_PARTITION) || \
289 defined(CONFIG_ISO_PARTITION) || \
290 defined(CONFIG_AMIGA_PARTITION) || \
291 defined(CONFIG_EFI_PARTITION)
292 puts ("\nPartition Map for ");
293 switch (dev_desc
->if_type
) {
322 printf (" device %d -- Partition Type: %s\n\n",
323 dev_desc
->devnum
, type
);
324 #endif /* any CONFIG_..._PARTITION */
327 void part_print(struct blk_desc
*dev_desc
)
329 struct part_driver
*drv
;
331 drv
= part_driver_lookup_type(dev_desc
->part_type
);
333 printf("## Unknown partition table type %x\n",
334 dev_desc
->part_type
);
338 PRINTF("## Testing for valid %s partition ##\n", drv
->name
);
339 print_part_header(drv
->name
, dev_desc
);
341 drv
->print(dev_desc
);
344 #endif /* HAVE_BLOCK_DEVICE */
346 int part_get_info(struct blk_desc
*dev_desc
, int part
,
347 disk_partition_t
*info
)
349 #ifdef HAVE_BLOCK_DEVICE
350 struct part_driver
*drv
;
352 #ifdef CONFIG_PARTITION_UUIDS
353 /* The common case is no UUID support */
356 #ifdef CONFIG_PARTITION_TYPE_GUID
357 info
->type_guid
[0] = 0;
360 drv
= part_driver_lookup_type(dev_desc
->part_type
);
362 debug("## Unknown partition table type %x\n",
363 dev_desc
->part_type
);
364 return -EPROTONOSUPPORT
;
366 if (!drv
->get_info
) {
367 PRINTF("## Driver %s does not have the get_info() method\n",
371 if (drv
->get_info(dev_desc
, part
, info
) == 0) {
372 PRINTF("## Valid %s partition found ##\n", drv
->name
);
375 #endif /* HAVE_BLOCK_DEVICE */
380 int blk_get_device_by_str(const char *ifname
, const char *dev_hwpart_str
,
381 struct blk_desc
**dev_desc
)
384 char *dup_str
= NULL
;
385 const char *dev_str
, *hwpart_str
;
388 hwpart_str
= strchr(dev_hwpart_str
, '.');
390 dup_str
= strdup(dev_hwpart_str
);
391 dup_str
[hwpart_str
- dev_hwpart_str
] = 0;
395 dev_str
= dev_hwpart_str
;
399 dev
= simple_strtoul(dev_str
, &ep
, 16);
401 printf("** Bad device specification %s %s **\n",
408 hwpart
= simple_strtoul(hwpart_str
, &ep
, 16);
410 printf("** Bad HW partition specification %s %s **\n",
417 *dev_desc
= get_dev_hwpart(ifname
, dev
, hwpart
);
418 if (!(*dev_desc
) || ((*dev_desc
)->type
== DEV_TYPE_UNKNOWN
)) {
419 printf("** Bad device %s %s **\n", ifname
, dev_hwpart_str
);
424 #ifdef HAVE_BLOCK_DEVICE
426 * Updates the partition table for the specified hw partition.
427 * Does not need to be done for hwpart 0 since it is default and
431 part_init(*dev_desc
);
439 #define PART_UNSPECIFIED -2
441 #define MAX_SEARCH_PARTITIONS 16
442 int blk_get_device_part_str(const char *ifname
, const char *dev_part_str
,
443 struct blk_desc
**dev_desc
,
444 disk_partition_t
*info
, int allow_whole_dev
)
447 const char *part_str
;
448 char *dup_str
= NULL
;
454 disk_partition_t tmpinfo
;
456 #ifdef CONFIG_SANDBOX
458 * Special-case a pseudo block device "hostfs", to allow access to the
459 * host's own filesystem.
461 if (0 == strcmp(ifname
, "hostfs")) {
467 strcpy((char *)info
->type
, BOOT_PART_TYPE
);
468 strcpy((char *)info
->name
, "Sandbox host");
469 #ifdef CONFIG_PARTITION_UUIDS
472 #ifdef CONFIG_PARTITION_TYPE_GUID
473 info
->type_guid
[0] = 0;
480 #ifdef CONFIG_CMD_UBIFS
482 * Special-case ubi, ubi goes through a mtd, rathen then through
483 * a regular block device.
485 if (0 == strcmp(ifname
, "ubi")) {
486 if (!ubifs_is_mounted()) {
487 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
492 memset(info
, 0, sizeof(*info
));
493 strcpy((char *)info
->type
, BOOT_PART_TYPE
);
494 strcpy((char *)info
->name
, "UBI");
495 #ifdef CONFIG_PARTITION_UUIDS
502 /* If no dev_part_str, use bootdevice environment variable */
503 if (!dev_part_str
|| !strlen(dev_part_str
) ||
504 !strcmp(dev_part_str
, "-"))
505 dev_part_str
= getenv("bootdevice");
507 /* If still no dev_part_str, it's an error */
509 printf("** No device specified **\n");
513 /* Separate device and partition ID specification */
514 part_str
= strchr(dev_part_str
, ':');
516 dup_str
= strdup(dev_part_str
);
517 dup_str
[part_str
- dev_part_str
] = 0;
521 dev_str
= dev_part_str
;
524 /* Look up the device */
525 dev
= blk_get_device_by_str(ifname
, dev_str
, dev_desc
);
529 /* Convert partition ID string to number */
530 if (!part_str
|| !*part_str
) {
531 part
= PART_UNSPECIFIED
;
532 } else if (!strcmp(part_str
, "auto")) {
535 /* Something specified -> use exactly that */
536 part
= (int)simple_strtoul(part_str
, &ep
, 16);
538 * Less than whole string converted,
539 * or request for whole device, but caller requires partition.
541 if (*ep
|| (part
== 0 && !allow_whole_dev
)) {
542 printf("** Bad partition specification %s %s **\n",
543 ifname
, dev_part_str
);
549 * No partition table on device,
550 * or user requested partition 0 (entire device).
552 if (((*dev_desc
)->part_type
== PART_TYPE_UNKNOWN
) ||
554 if (!(*dev_desc
)->lba
) {
555 printf("** Bad device size - %s %s **\n", ifname
,
561 * If user specified a partition ID other than 0,
562 * or the calling command only accepts partitions,
565 if ((part
> 0) || (!allow_whole_dev
)) {
566 printf("** No partition table - %s %s **\n", ifname
,
571 (*dev_desc
)->log2blksz
= LOG2((*dev_desc
)->blksz
);
574 info
->size
= (*dev_desc
)->lba
;
575 info
->blksz
= (*dev_desc
)->blksz
;
577 strcpy((char *)info
->type
, BOOT_PART_TYPE
);
578 strcpy((char *)info
->name
, "Whole Disk");
579 #ifdef CONFIG_PARTITION_UUIDS
582 #ifdef CONFIG_PARTITION_TYPE_GUID
583 info
->type_guid
[0] = 0;
591 * Now there's known to be a partition table,
592 * not specifying a partition means to pick partition 1.
594 if (part
== PART_UNSPECIFIED
)
598 * If user didn't specify a partition number, or did specify something
599 * other than "auto", use that partition number directly.
601 if (part
!= PART_AUTO
) {
602 ret
= part_get_info(*dev_desc
, part
, info
);
604 printf("** Invalid partition %d **\n", part
);
609 * Find the first bootable partition.
610 * If none are bootable, fall back to the first valid partition.
613 for (p
= 1; p
<= MAX_SEARCH_PARTITIONS
; p
++) {
614 ret
= part_get_info(*dev_desc
, p
, info
);
619 * First valid partition, or new better partition?
620 * If so, save partition ID.
622 if (!part
|| info
->bootable
)
625 /* Best possible partition? Stop searching. */
630 * We now need to search further for best possible.
631 * If we what we just queried was the best so far,
632 * save the info since we over-write it next loop.
637 /* If we found any acceptable partition */
640 * If we searched all possible partition IDs,
641 * return the first valid partition we found.
643 if (p
== MAX_SEARCH_PARTITIONS
+ 1)
646 printf("** No valid partitions found **\n");
651 if (strncmp((char *)info
->type
, BOOT_PART_TYPE
, sizeof(info
->type
)) != 0) {
652 printf("** Invalid partition type \"%.32s\""
653 " (expect \"" BOOT_PART_TYPE
"\")\n",
659 (*dev_desc
)->log2blksz
= LOG2((*dev_desc
)->blksz
);