1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
13 #include <ubifs_uboot.h>
18 #define PRINTF(fmt,args...) printf (fmt ,##args)
20 #define PRINTF(fmt,args...)
23 /* Check all partition types */
24 #define PART_TYPE_ALL -1
26 static struct part_driver *part_driver_lookup_type(struct blk_desc *dev_desc)
28 struct part_driver *drv =
29 ll_entry_start(struct part_driver, part_driver);
30 const int n_ents = ll_entry_count(struct part_driver, part_driver);
31 struct part_driver *entry;
33 if (dev_desc->part_type == PART_TYPE_UNKNOWN) {
34 for (entry = drv; entry != drv + n_ents; entry++) {
37 ret = entry->test(dev_desc);
39 dev_desc->part_type = entry->part_type;
44 for (entry = drv; entry != drv + n_ents; entry++) {
45 if (dev_desc->part_type == entry->part_type)
54 #ifdef CONFIG_HAVE_BLOCK_DEVICE
55 static struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
57 struct blk_desc *dev_desc;
60 dev_desc = blk_get_devnum_by_typename(ifname, dev);
62 debug("%s: No device for iface '%s', dev %d\n", __func__,
66 ret = blk_dselect_hwpart(dev_desc, hwpart);
68 debug("%s: Failed to select h/w partition: err-%d\n", __func__,
76 struct blk_desc *blk_get_dev(const char *ifname, int dev)
78 return get_dev_hwpart(ifname, dev, 0);
81 struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
86 struct blk_desc *blk_get_dev(const char *ifname, int dev)
92 #ifdef CONFIG_HAVE_BLOCK_DEVICE
94 /* ------------------------------------------------------------------------- */
96 * reports device info to the user
100 typedef uint64_t lba512_t;
102 typedef lbaint_t lba512_t;
106 * Overflowless variant of (block_count * mul_by / div_by)
107 * when div_by > mul_by
109 static lba512_t lba512_muldiv(lba512_t block_count, lba512_t mul_by, lba512_t div_by)
111 lba512_t bc_quot, bc_rem;
113 /* x * m / d == x / d * m + (x % d) * m / d */
114 bc_quot = block_count / div_by;
115 bc_rem = block_count - div_by * bc_quot;
116 return bc_quot * mul_by + (bc_rem * mul_by) / div_by;
119 void dev_print (struct blk_desc *dev_desc)
121 lba512_t lba512; /* number of blocks if 512bytes block size */
123 if (dev_desc->type == DEV_TYPE_UNKNOWN) {
124 puts ("not available\n");
128 switch (dev_desc->if_type) {
130 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
131 dev_desc->target,dev_desc->lun,
139 printf ("Model: %s Firm: %s Ser#: %s\n",
148 printf ("Vendor: %s Rev: %s Prod: %s\n",
154 puts("device type DOC\n");
156 case IF_TYPE_UNKNOWN:
157 puts("device type unknown\n");
160 printf("Unhandled device type: %i\n", dev_desc->if_type);
164 if (dev_desc->removable)
166 switch (dev_desc->type & 0x1F) {
167 case DEV_TYPE_HARDDISK:
173 case DEV_TYPE_OPDISK:
174 puts ("Optical Device");
180 printf ("# %02X #", dev_desc->type & 0x1F);
184 if (dev_desc->lba > 0L && dev_desc->blksz > 0L) {
185 ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
190 lba512 = (lba * (dev_desc->blksz/512));
191 /* round to 1 digit */
192 /* 2048 = (1024 * 1024) / 512 MB */
193 mb = lba512_muldiv(lba512, 10, 2048);
196 mb_rem = mb - (10 * mb_quot);
200 gb_rem = gb - (10 * gb_quot);
203 printf (" Supports 48-bit addressing\n");
205 #if defined(CONFIG_SYS_64BIT_LBA)
206 printf (" Capacity: %lu.%lu MB = %lu.%lu GB (%llu x %lu)\n",
212 printf (" Capacity: %lu.%lu MB = %lu.%lu GB (%lu x %lu)\n",
219 puts (" Capacity: not available\n");
224 #ifdef CONFIG_HAVE_BLOCK_DEVICE
226 void part_init(struct blk_desc *dev_desc)
228 struct part_driver *drv =
229 ll_entry_start(struct part_driver, part_driver);
230 const int n_ents = ll_entry_count(struct part_driver, part_driver);
231 struct part_driver *entry;
233 blkcache_invalidate(dev_desc->if_type, dev_desc->devnum);
235 dev_desc->part_type = PART_TYPE_UNKNOWN;
236 for (entry = drv; entry != drv + n_ents; entry++) {
239 ret = entry->test(dev_desc);
240 debug("%s: try '%s': ret=%d\n", __func__, entry->name, ret);
242 dev_desc->part_type = entry->part_type;
248 static void print_part_header(const char *type, struct blk_desc *dev_desc)
250 #if CONFIG_IS_ENABLED(MAC_PARTITION) || \
251 CONFIG_IS_ENABLED(DOS_PARTITION) || \
252 CONFIG_IS_ENABLED(ISO_PARTITION) || \
253 CONFIG_IS_ENABLED(AMIGA_PARTITION) || \
254 CONFIG_IS_ENABLED(EFI_PARTITION)
255 puts ("\nPartition Map for ");
256 switch (dev_desc->if_type) {
288 printf (" device %d -- Partition Type: %s\n\n",
289 dev_desc->devnum, type);
290 #endif /* any CONFIG_..._PARTITION */
293 void part_print(struct blk_desc *dev_desc)
295 struct part_driver *drv;
297 drv = part_driver_lookup_type(dev_desc);
299 printf("## Unknown partition table type %x\n",
300 dev_desc->part_type);
304 PRINTF("## Testing for valid %s partition ##\n", drv->name);
305 print_part_header(drv->name, dev_desc);
307 drv->print(dev_desc);
310 #endif /* CONFIG_HAVE_BLOCK_DEVICE */
312 int part_get_info(struct blk_desc *dev_desc, int part,
313 disk_partition_t *info)
315 #ifdef CONFIG_HAVE_BLOCK_DEVICE
316 struct part_driver *drv;
318 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
319 /* The common case is no UUID support */
322 #ifdef CONFIG_PARTITION_TYPE_GUID
323 info->type_guid[0] = 0;
326 drv = part_driver_lookup_type(dev_desc);
328 debug("## Unknown partition table type %x\n",
329 dev_desc->part_type);
330 return -EPROTONOSUPPORT;
332 if (!drv->get_info) {
333 PRINTF("## Driver %s does not have the get_info() method\n",
337 if (drv->get_info(dev_desc, part, info) == 0) {
338 PRINTF("## Valid %s partition found ##\n", drv->name);
341 #endif /* CONFIG_HAVE_BLOCK_DEVICE */
346 int part_get_info_whole_disk(struct blk_desc *dev_desc, disk_partition_t *info)
349 info->size = dev_desc->lba;
350 info->blksz = dev_desc->blksz;
352 strcpy((char *)info->type, BOOT_PART_TYPE);
353 strcpy((char *)info->name, "Whole Disk");
354 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
357 #ifdef CONFIG_PARTITION_TYPE_GUID
358 info->type_guid[0] = 0;
364 int blk_get_device_by_str(const char *ifname, const char *dev_hwpart_str,
365 struct blk_desc **dev_desc)
368 char *dup_str = NULL;
369 const char *dev_str, *hwpart_str;
372 hwpart_str = strchr(dev_hwpart_str, '.');
374 dup_str = strdup(dev_hwpart_str);
375 dup_str[hwpart_str - dev_hwpart_str] = 0;
379 dev_str = dev_hwpart_str;
383 dev = simple_strtoul(dev_str, &ep, 16);
385 printf("** Bad device specification %s %s **\n",
392 hwpart = simple_strtoul(hwpart_str, &ep, 16);
394 printf("** Bad HW partition specification %s %s **\n",
401 *dev_desc = get_dev_hwpart(ifname, dev, hwpart);
402 if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
403 printf("** Bad device %s %s **\n", ifname, dev_hwpart_str);
408 #ifdef CONFIG_HAVE_BLOCK_DEVICE
410 * Updates the partition table for the specified hw partition.
411 * Does not need to be done for hwpart 0 since it is default and
415 part_init(*dev_desc);
423 #define PART_UNSPECIFIED -2
425 int blk_get_device_part_str(const char *ifname, const char *dev_part_str,
426 struct blk_desc **dev_desc,
427 disk_partition_t *info, int allow_whole_dev)
430 const char *part_str;
431 char *dup_str = NULL;
437 disk_partition_t tmpinfo;
439 #ifdef CONFIG_SANDBOX
441 * Special-case a pseudo block device "hostfs", to allow access to the
442 * host's own filesystem.
444 if (0 == strcmp(ifname, "hostfs")) {
450 strcpy((char *)info->type, BOOT_PART_TYPE);
451 strcpy((char *)info->name, "Sandbox host");
452 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
455 #ifdef CONFIG_PARTITION_TYPE_GUID
456 info->type_guid[0] = 0;
463 #ifdef CONFIG_CMD_UBIFS
465 * Special-case ubi, ubi goes through a mtd, rathen then through
466 * a regular block device.
468 if (0 == strcmp(ifname, "ubi")) {
469 if (!ubifs_is_mounted()) {
470 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
475 memset(info, 0, sizeof(*info));
476 strcpy((char *)info->type, BOOT_PART_TYPE);
477 strcpy((char *)info->name, "UBI");
478 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
485 /* If no dev_part_str, use bootdevice environment variable */
486 if (!dev_part_str || !strlen(dev_part_str) ||
487 !strcmp(dev_part_str, "-"))
488 dev_part_str = env_get("bootdevice");
490 /* If still no dev_part_str, it's an error */
492 printf("** No device specified **\n");
496 /* Separate device and partition ID specification */
497 part_str = strchr(dev_part_str, ':');
499 dup_str = strdup(dev_part_str);
500 dup_str[part_str - dev_part_str] = 0;
504 dev_str = dev_part_str;
507 /* Look up the device */
508 dev = blk_get_device_by_str(ifname, dev_str, dev_desc);
512 /* Convert partition ID string to number */
513 if (!part_str || !*part_str) {
514 part = PART_UNSPECIFIED;
515 } else if (!strcmp(part_str, "auto")) {
518 /* Something specified -> use exactly that */
519 part = (int)simple_strtoul(part_str, &ep, 16);
521 * Less than whole string converted,
522 * or request for whole device, but caller requires partition.
524 if (*ep || (part == 0 && !allow_whole_dev)) {
525 printf("** Bad partition specification %s %s **\n",
526 ifname, dev_part_str);
532 * No partition table on device,
533 * or user requested partition 0 (entire device).
535 if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
537 if (!(*dev_desc)->lba) {
538 printf("** Bad device size - %s %s **\n", ifname,
544 * If user specified a partition ID other than 0,
545 * or the calling command only accepts partitions,
548 if ((part > 0) || (!allow_whole_dev)) {
549 printf("** No partition table - %s %s **\n", ifname,
554 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
556 part_get_info_whole_disk(*dev_desc, info);
563 * Now there's known to be a partition table,
564 * not specifying a partition means to pick partition 1.
566 if (part == PART_UNSPECIFIED)
570 * If user didn't specify a partition number, or did specify something
571 * other than "auto", use that partition number directly.
573 if (part != PART_AUTO) {
574 ret = part_get_info(*dev_desc, part, info);
576 printf("** Invalid partition %d **\n", part);
581 * Find the first bootable partition.
582 * If none are bootable, fall back to the first valid partition.
585 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
586 ret = part_get_info(*dev_desc, p, info);
591 * First valid partition, or new better partition?
592 * If so, save partition ID.
594 if (!part || info->bootable)
597 /* Best possible partition? Stop searching. */
602 * We now need to search further for best possible.
603 * If we what we just queried was the best so far,
604 * save the info since we over-write it next loop.
609 /* If we found any acceptable partition */
612 * If we searched all possible partition IDs,
613 * return the first valid partition we found.
615 if (p == MAX_SEARCH_PARTITIONS + 1)
618 printf("** No valid partitions found **\n");
623 if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
624 printf("** Invalid partition type \"%.32s\""
625 " (expect \"" BOOT_PART_TYPE "\")\n",
631 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
641 int part_get_info_by_name_type(struct blk_desc *dev_desc, const char *name,
642 disk_partition_t *info, int part_type)
644 struct part_driver *part_drv;
648 part_drv = part_driver_lookup_type(dev_desc);
651 for (i = 1; i < part_drv->max_entries; i++) {
652 ret = part_drv->get_info(dev_desc, i, info);
654 /* no more entries in table */
657 if (strcmp(name, (const char *)info->name) == 0) {
666 int part_get_info_by_name(struct blk_desc *dev_desc, const char *name,
667 disk_partition_t *info)
669 return part_get_info_by_name_type(dev_desc, name, info, PART_TYPE_ALL);
672 void part_set_generic_name(const struct blk_desc *dev_desc,
673 int part_num, char *name)
677 switch (dev_desc->if_type) {
701 sprintf(name, "%s%c%d", devtype, 'a' + dev_desc->devnum, part_num);