]>
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+
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)
31 #if defined(CONFIG_SCSI)
34 #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
37 #if defined(CONFIG_MMC)
40 .select_hwpart
= mmc_select_hwpart
,
43 #if defined(CONFIG_SYSTEMACE)
46 #if defined(CONFIG_SANDBOX)
52 DECLARE_GLOBAL_DATA_PTR
;
54 #ifdef HAVE_BLOCK_DEVICE
55 static struct part_driver
*part_driver_lookup_type(int part_type
)
57 struct part_driver
*drv
=
58 ll_entry_start(struct part_driver
, part_driver
);
59 const int n_ents
= ll_entry_count(struct part_driver
, part_driver
);
60 struct part_driver
*entry
;
62 for (entry
= drv
; entry
!= drv
+ n_ents
; entry
++) {
63 if (part_type
== entry
->part_type
)
71 static struct blk_desc
*get_dev_hwpart(const char *ifname
, int dev
, int hwpart
)
73 const struct block_drvr
*drvr
= block_drvr
;
80 #ifdef CONFIG_NEEDS_MANUAL_RELOC
81 name
+= gd
->reloc_off
;
85 #ifdef CONFIG_NEEDS_MANUAL_RELOC
86 name
+= gd
->reloc_off
;
88 if (strncmp(ifname
, name
, strlen(name
)) == 0) {
89 struct blk_desc
*dev_desc
;
91 dev_desc
= blk_get_devnum_by_typename(name
, dev
);
94 if (blk_dselect_hwpart(dev_desc
, hwpart
))
103 struct blk_desc
*blk_get_dev(const char *ifname
, int dev
)
105 return get_dev_hwpart(ifname
, dev
, 0);
108 struct blk_desc
*get_dev_hwpart(const char *ifname
, int dev
, int hwpart
)
113 struct blk_desc
*blk_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 (struct blk_desc
*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 /* 2048 = (1024 * 1024) / 512 MB */
219 mb
= lba512_muldiv(lba512
, 10, 2048);
222 mb_rem
= mb
- (10 * mb_quot
);
226 gb_rem
= gb
- (10 * gb_quot
);
229 printf (" Supports 48-bit addressing\n");
231 #if defined(CONFIG_SYS_64BIT_LBA)
232 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n",
238 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
245 puts (" Capacity: not available\n");
250 #ifdef HAVE_BLOCK_DEVICE
252 void part_init(struct blk_desc
*dev_desc
)
254 struct part_driver
*drv
=
255 ll_entry_start(struct part_driver
, part_driver
);
256 const int n_ents
= ll_entry_count(struct part_driver
, part_driver
);
257 struct part_driver
*entry
;
259 blkcache_invalidate(dev_desc
->if_type
, dev_desc
->devnum
);
261 dev_desc
->part_type
= PART_TYPE_UNKNOWN
;
262 for (entry
= drv
; entry
!= drv
+ n_ents
; entry
++) {
265 ret
= entry
->test(dev_desc
);
266 debug("%s: try '%s': ret=%d\n", __func__
, entry
->name
, ret
);
268 dev_desc
->part_type
= entry
->part_type
;
274 static void print_part_header(const char *type
, struct blk_desc
*dev_desc
)
276 #if defined(CONFIG_MAC_PARTITION) || \
277 defined(CONFIG_DOS_PARTITION) || \
278 defined(CONFIG_ISO_PARTITION) || \
279 defined(CONFIG_AMIGA_PARTITION) || \
280 defined(CONFIG_EFI_PARTITION)
281 puts ("\nPartition Map for ");
282 switch (dev_desc
->if_type
) {
311 printf (" device %d -- Partition Type: %s\n\n",
312 dev_desc
->devnum
, type
);
313 #endif /* any CONFIG_..._PARTITION */
316 void part_print(struct blk_desc
*dev_desc
)
318 struct part_driver
*drv
;
320 drv
= part_driver_lookup_type(dev_desc
->part_type
);
322 printf("## Unknown partition table type %x\n",
323 dev_desc
->part_type
);
327 PRINTF("## Testing for valid %s partition ##\n", drv
->name
);
328 print_part_header(drv
->name
, dev_desc
);
330 drv
->print(dev_desc
);
333 #endif /* HAVE_BLOCK_DEVICE */
335 int part_get_info(struct blk_desc
*dev_desc
, int part
,
336 disk_partition_t
*info
)
338 #ifdef HAVE_BLOCK_DEVICE
339 struct part_driver
*drv
;
341 #ifdef CONFIG_PARTITION_UUIDS
342 /* The common case is no UUID support */
345 #ifdef CONFIG_PARTITION_TYPE_GUID
346 info
->type_guid
[0] = 0;
349 drv
= part_driver_lookup_type(dev_desc
->part_type
);
351 debug("## Unknown partition table type %x\n",
352 dev_desc
->part_type
);
353 return -EPROTONOSUPPORT
;
355 if (!drv
->get_info
) {
356 PRINTF("## Driver %s does not have the get_info() method\n",
360 if (drv
->get_info(dev_desc
, part
, info
) == 0) {
361 PRINTF("## Valid %s partition found ##\n", drv
->name
);
364 #endif /* HAVE_BLOCK_DEVICE */
369 int blk_get_device_by_str(const char *ifname
, const char *dev_hwpart_str
,
370 struct blk_desc
**dev_desc
)
373 char *dup_str
= NULL
;
374 const char *dev_str
, *hwpart_str
;
377 hwpart_str
= strchr(dev_hwpart_str
, '.');
379 dup_str
= strdup(dev_hwpart_str
);
380 dup_str
[hwpart_str
- dev_hwpart_str
] = 0;
384 dev_str
= dev_hwpart_str
;
388 dev
= simple_strtoul(dev_str
, &ep
, 16);
390 printf("** Bad device specification %s %s **\n",
397 hwpart
= simple_strtoul(hwpart_str
, &ep
, 16);
399 printf("** Bad HW partition specification %s %s **\n",
406 *dev_desc
= get_dev_hwpart(ifname
, dev
, hwpart
);
407 if (!(*dev_desc
) || ((*dev_desc
)->type
== DEV_TYPE_UNKNOWN
)) {
408 printf("** Bad device %s %s **\n", ifname
, dev_hwpart_str
);
413 #ifdef HAVE_BLOCK_DEVICE
415 * Updates the partition table for the specified hw partition.
416 * Does not need to be done for hwpart 0 since it is default and
420 part_init(*dev_desc
);
428 #define PART_UNSPECIFIED -2
430 #define MAX_SEARCH_PARTITIONS 16
431 int blk_get_device_part_str(const char *ifname
, const char *dev_part_str
,
432 struct blk_desc
**dev_desc
,
433 disk_partition_t
*info
, int allow_whole_dev
)
436 const char *part_str
;
437 char *dup_str
= NULL
;
443 disk_partition_t tmpinfo
;
445 #ifdef CONFIG_SANDBOX
447 * Special-case a pseudo block device "hostfs", to allow access to the
448 * host's own filesystem.
450 if (0 == strcmp(ifname
, "hostfs")) {
456 strcpy((char *)info
->type
, BOOT_PART_TYPE
);
457 strcpy((char *)info
->name
, "Sandbox host");
458 #ifdef CONFIG_PARTITION_UUIDS
461 #ifdef CONFIG_PARTITION_TYPE_GUID
462 info
->type_guid
[0] = 0;
469 #ifdef CONFIG_CMD_UBIFS
471 * Special-case ubi, ubi goes through a mtd, rathen then through
472 * a regular block device.
474 if (0 == strcmp(ifname
, "ubi")) {
475 if (!ubifs_is_mounted()) {
476 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
481 memset(info
, 0, sizeof(*info
));
482 strcpy((char *)info
->type
, BOOT_PART_TYPE
);
483 strcpy((char *)info
->name
, "UBI");
484 #ifdef CONFIG_PARTITION_UUIDS
491 /* If no dev_part_str, use bootdevice environment variable */
492 if (!dev_part_str
|| !strlen(dev_part_str
) ||
493 !strcmp(dev_part_str
, "-"))
494 dev_part_str
= getenv("bootdevice");
496 /* If still no dev_part_str, it's an error */
498 printf("** No device specified **\n");
502 /* Separate device and partition ID specification */
503 part_str
= strchr(dev_part_str
, ':');
505 dup_str
= strdup(dev_part_str
);
506 dup_str
[part_str
- dev_part_str
] = 0;
510 dev_str
= dev_part_str
;
513 /* Look up the device */
514 dev
= blk_get_device_by_str(ifname
, dev_str
, dev_desc
);
518 /* Convert partition ID string to number */
519 if (!part_str
|| !*part_str
) {
520 part
= PART_UNSPECIFIED
;
521 } else if (!strcmp(part_str
, "auto")) {
524 /* Something specified -> use exactly that */
525 part
= (int)simple_strtoul(part_str
, &ep
, 16);
527 * Less than whole string converted,
528 * or request for whole device, but caller requires partition.
530 if (*ep
|| (part
== 0 && !allow_whole_dev
)) {
531 printf("** Bad partition specification %s %s **\n",
532 ifname
, dev_part_str
);
538 * No partition table on device,
539 * or user requested partition 0 (entire device).
541 if (((*dev_desc
)->part_type
== PART_TYPE_UNKNOWN
) ||
543 if (!(*dev_desc
)->lba
) {
544 printf("** Bad device size - %s %s **\n", ifname
,
550 * If user specified a partition ID other than 0,
551 * or the calling command only accepts partitions,
554 if ((part
> 0) || (!allow_whole_dev
)) {
555 printf("** No partition table - %s %s **\n", ifname
,
560 (*dev_desc
)->log2blksz
= LOG2((*dev_desc
)->blksz
);
563 info
->size
= (*dev_desc
)->lba
;
564 info
->blksz
= (*dev_desc
)->blksz
;
566 strcpy((char *)info
->type
, BOOT_PART_TYPE
);
567 strcpy((char *)info
->name
, "Whole Disk");
568 #ifdef CONFIG_PARTITION_UUIDS
571 #ifdef CONFIG_PARTITION_TYPE_GUID
572 info
->type_guid
[0] = 0;
580 * Now there's known to be a partition table,
581 * not specifying a partition means to pick partition 1.
583 if (part
== PART_UNSPECIFIED
)
587 * If user didn't specify a partition number, or did specify something
588 * other than "auto", use that partition number directly.
590 if (part
!= PART_AUTO
) {
591 ret
= part_get_info(*dev_desc
, part
, info
);
593 printf("** Invalid partition %d **\n", part
);
598 * Find the first bootable partition.
599 * If none are bootable, fall back to the first valid partition.
602 for (p
= 1; p
<= MAX_SEARCH_PARTITIONS
; p
++) {
603 ret
= part_get_info(*dev_desc
, p
, info
);
608 * First valid partition, or new better partition?
609 * If so, save partition ID.
611 if (!part
|| info
->bootable
)
614 /* Best possible partition? Stop searching. */
619 * We now need to search further for best possible.
620 * If we what we just queried was the best so far,
621 * save the info since we over-write it next loop.
626 /* If we found any acceptable partition */
629 * If we searched all possible partition IDs,
630 * return the first valid partition we found.
632 if (p
== MAX_SEARCH_PARTITIONS
+ 1)
635 printf("** No valid partitions found **\n");
640 if (strncmp((char *)info
->type
, BOOT_PART_TYPE
, sizeof(info
->type
)) != 0) {
641 printf("** Invalid partition type \"%.32s\""
642 " (expect \"" BOOT_PART_TYPE
"\")\n",
648 (*dev_desc
)->log2blksz
= LOG2((*dev_desc
)->blksz
);