Merge https://source.denx.de/u-boot/custodians/u-boot-marvell
[platform/kernel/u-boot.git] / disk / part.c
index 80ced2b..5ee60a7 100644 (file)
@@ -54,13 +54,14 @@ static struct part_driver *part_driver_lookup_type(struct blk_desc *dev_desc)
        return NULL;
 }
 
-#ifdef CONFIG_HAVE_BLOCK_DEVICE
 static struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
 {
        struct blk_desc *dev_desc;
        int ret;
 
-       dev_desc = blk_get_devnum_by_typename(ifname, dev);
+       if (!blk_enabled())
+               return NULL;
+       dev_desc = blk_get_devnum_by_uclass_idname(ifname, dev);
        if (!dev_desc) {
                debug("%s: No device for iface '%s', dev %d\n", __func__,
                      ifname, dev);
@@ -78,21 +79,11 @@ static struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
 
 struct blk_desc *blk_get_dev(const char *ifname, int dev)
 {
-       return get_dev_hwpart(ifname, dev, 0);
-}
-#else
-struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
-{
-       return NULL;
-}
+       if (!blk_enabled())
+               return NULL;
 
-struct blk_desc *blk_get_dev(const char *ifname, int dev)
-{
-       return NULL;
+       return get_dev_hwpart(ifname, dev, 0);
 }
-#endif
-
-#ifdef CONFIG_HAVE_BLOCK_DEVICE
 
 /* ------------------------------------------------------------------------- */
 /*
@@ -120,7 +111,7 @@ static lba512_t lba512_muldiv(lba512_t block_count, lba512_t mul_by,
        return bc_quot * mul_by + ((bc_rem * mul_by) >> right_shift);
 }
 
-void dev_print (struct blk_desc *dev_desc)
+void dev_print(struct blk_desc *dev_desc)
 {
        lba512_t lba512; /* number of blocks if 512bytes block size */
 
@@ -129,44 +120,42 @@ void dev_print (struct blk_desc *dev_desc)
                return;
        }
 
-       switch (dev_desc->if_type) {
-       case IF_TYPE_SCSI:
+       switch (dev_desc->uclass_id) {
+       case UCLASS_SCSI:
                printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
                        dev_desc->target,dev_desc->lun,
                        dev_desc->vendor,
                        dev_desc->product,
                        dev_desc->revision);
                break;
-       case IF_TYPE_ATAPI:
-       case IF_TYPE_IDE:
-       case IF_TYPE_SATA:
+       case UCLASS_IDE:
+       case UCLASS_AHCI:
                printf ("Model: %s Firm: %s Ser#: %s\n",
                        dev_desc->vendor,
                        dev_desc->revision,
                        dev_desc->product);
                break;
-       case IF_TYPE_SD:
-       case IF_TYPE_MMC:
-       case IF_TYPE_USB:
-       case IF_TYPE_NVME:
-       case IF_TYPE_PVBLOCK:
-       case IF_TYPE_HOST:
+       case UCLASS_MMC:
+       case UCLASS_USB:
+       case UCLASS_NVME:
+       case UCLASS_PVBLOCK:
+       case UCLASS_HOST:
                printf ("Vendor: %s Rev: %s Prod: %s\n",
                        dev_desc->vendor,
                        dev_desc->revision,
                        dev_desc->product);
                break;
-       case IF_TYPE_VIRTIO:
+       case UCLASS_VIRTIO:
                printf("%s VirtIO Block Device\n", dev_desc->vendor);
                break;
-       case IF_TYPE_DOC:
-               puts("device type DOC\n");
-               return;
-       case IF_TYPE_UNKNOWN:
+       case UCLASS_EFI_MEDIA:
+               printf("EFI media Block Device %d\n", dev_desc->devnum);
+               break;
+       case UCLASS_INVALID:
                puts("device type unknown\n");
                return;
        default:
-               printf("Unhandled device type: %i\n", dev_desc->if_type);
+               printf("Unhandled device type: %i\n", dev_desc->uclass_id);
                return;
        }
        puts ("            Type: ");
@@ -228,9 +217,6 @@ void dev_print (struct blk_desc *dev_desc)
                puts ("            Capacity: not available\n");
        }
 }
-#endif
-
-#ifdef CONFIG_HAVE_BLOCK_DEVICE
 
 void part_init(struct blk_desc *dev_desc)
 {
@@ -239,7 +225,7 @@ void part_init(struct blk_desc *dev_desc)
        const int n_ents = ll_entry_count(struct part_driver, part_driver);
        struct part_driver *entry;
 
-       blkcache_invalidate(dev_desc->if_type, dev_desc->devnum);
+       blkcache_invalidate(dev_desc->uclass_id, dev_desc->devnum);
 
        dev_desc->part_type = PART_TYPE_UNKNOWN;
        for (entry = drv; entry != drv + n_ents; entry++) {
@@ -262,42 +248,39 @@ static void print_part_header(const char *type, struct blk_desc *dev_desc)
        CONFIG_IS_ENABLED(AMIGA_PARTITION) || \
        CONFIG_IS_ENABLED(EFI_PARTITION)
        puts ("\nPartition Map for ");
-       switch (dev_desc->if_type) {
-       case IF_TYPE_IDE:
+       switch (dev_desc->uclass_id) {
+       case UCLASS_IDE:
                puts ("IDE");
                break;
-       case IF_TYPE_SATA:
+       case UCLASS_AHCI:
                puts ("SATA");
                break;
-       case IF_TYPE_SCSI:
+       case UCLASS_SCSI:
                puts ("SCSI");
                break;
-       case IF_TYPE_ATAPI:
-               puts ("ATAPI");
-               break;
-       case IF_TYPE_USB:
+       case UCLASS_USB:
                puts ("USB");
                break;
-       case IF_TYPE_DOC:
-               puts ("DOC");
-               break;
-       case IF_TYPE_MMC:
+       case UCLASS_MMC:
                puts ("MMC");
                break;
-       case IF_TYPE_HOST:
+       case UCLASS_HOST:
                puts ("HOST");
                break;
-       case IF_TYPE_NVME:
+       case UCLASS_NVME:
                puts ("NVMe");
                break;
-       case IF_TYPE_PVBLOCK:
+       case UCLASS_PVBLOCK:
                puts("PV BLOCK");
                break;
-       case IF_TYPE_VIRTIO:
+       case UCLASS_VIRTIO:
                puts("VirtIO");
                break;
+       case UCLASS_EFI_MEDIA:
+               puts("EFI");
+               break;
        default:
-               puts ("UNKNOWN");
+               puts("UNKNOWN");
                break;
        }
        printf (" device %d  --   Partition Type: %s\n\n",
@@ -322,38 +305,36 @@ void part_print(struct blk_desc *dev_desc)
                drv->print(dev_desc);
 }
 
-#endif /* CONFIG_HAVE_BLOCK_DEVICE */
-
 int part_get_info(struct blk_desc *dev_desc, int part,
                       struct disk_partition *info)
 {
-#ifdef CONFIG_HAVE_BLOCK_DEVICE
        struct part_driver *drv;
 
+       if (blk_enabled()) {
 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
-       /* The common case is no UUID support */
-       info->uuid[0] = 0;
+               /* The common case is no UUID support */
+               info->uuid[0] = 0;
 #endif
 #ifdef CONFIG_PARTITION_TYPE_GUID
-       info->type_guid[0] = 0;
+               info->type_guid[0] = 0;
 #endif
 
-       drv = part_driver_lookup_type(dev_desc);
-       if (!drv) {
-               debug("## Unknown partition table type %x\n",
-                     dev_desc->part_type);
-               return -EPROTONOSUPPORT;
-       }
-       if (!drv->get_info) {
-               PRINTF("## Driver %s does not have the get_info() method\n",
-                      drv->name);
-               return -ENOSYS;
-       }
-       if (drv->get_info(dev_desc, part, info) == 0) {
-               PRINTF("## Valid %s partition found ##\n", drv->name);
-               return 0;
+               drv = part_driver_lookup_type(dev_desc);
+               if (!drv) {
+                       debug("## Unknown partition table type %x\n",
+                             dev_desc->part_type);
+                       return -EPROTONOSUPPORT;
+               }
+               if (!drv->get_info) {
+                       PRINTF("## Driver %s does not have the get_info() method\n",
+                              drv->name);
+                       return -ENOSYS;
+               }
+               if (drv->get_info(dev_desc, part, info) == 0) {
+                       PRINTF("## Valid %s partition found ##\n", drv->name);
+                       return 0;
+               }
        }
-#endif /* CONFIG_HAVE_BLOCK_DEVICE */
 
        return -ENOENT;
 }
@@ -396,7 +377,7 @@ int blk_get_device_by_str(const char *ifname, const char *dev_hwpart_str,
                hwpart = 0;
        }
 
-       dev = simple_strtoul(dev_str, &ep, 16);
+       dev = hextoul(dev_str, &ep);
        if (*ep) {
                printf("** Bad device specification %s %s **\n",
                       ifname, dev_str);
@@ -405,7 +386,7 @@ int blk_get_device_by_str(const char *ifname, const char *dev_hwpart_str,
        }
 
        if (hwpart_str) {
-               hwpart = simple_strtoul(hwpart_str, &ep, 16);
+               hwpart = hextoul(hwpart_str, &ep);
                if (*ep) {
                        printf("** Bad HW partition specification %s %s **\n",
                            ifname, hwpart_str);
@@ -421,14 +402,15 @@ int blk_get_device_by_str(const char *ifname, const char *dev_hwpart_str,
                goto cleanup;
        }
 
-#ifdef CONFIG_HAVE_BLOCK_DEVICE
-       /*
-        * Updates the partition table for the specified hw partition.
-        * Always should be done, otherwise hw partition 0 will return stale
-        * data after displaying a non-zero hw partition.
-        */
-       part_init(*dev_desc);
-#endif
+       if (blk_enabled()) {
+               /*
+                * Updates the partition table for the specified hw partition.
+                * Always should be done, otherwise hw partition 0 will return
+                * stale data after displaying a non-zero hw partition.
+                */
+               if ((*dev_desc)->uclass_id == UCLASS_MMC)
+                       part_init(*dev_desc);
+       }
 
 cleanup:
        free(dup_str);
@@ -451,48 +433,35 @@ int blk_get_device_part_str(const char *ifname, const char *dev_part_str,
        int part;
        struct disk_partition tmpinfo;
 
-#ifdef CONFIG_SANDBOX
+       *dev_desc = NULL;
+       memset(info, 0, sizeof(*info));
+
+#if IS_ENABLED(CONFIG_SANDBOX) || IS_ENABLED(CONFIG_SEMIHOSTING)
        /*
         * Special-case a pseudo block device "hostfs", to allow access to the
         * host's own filesystem.
         */
-       if (0 == strcmp(ifname, "hostfs")) {
-               *dev_desc = NULL;
-               info->start = 0;
-               info->size = 0;
-               info->blksz = 0;
-               info->bootable = 0;
+       if (!strcmp(ifname, "hostfs")) {
                strcpy((char *)info->type, BOOT_PART_TYPE);
-               strcpy((char *)info->name, "Sandbox host");
-#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
-               info->uuid[0] = 0;
-#endif
-#ifdef CONFIG_PARTITION_TYPE_GUID
-               info->type_guid[0] = 0;
-#endif
+               strcpy((char *)info->name, "Host filesystem");
 
                return 0;
        }
 #endif
 
-#ifdef CONFIG_CMD_UBIFS
+#if IS_ENABLED(CONFIG_CMD_UBIFS) && !IS_ENABLED(CONFIG_SPL_BUILD)
        /*
         * Special-case ubi, ubi goes through a mtd, rather than through
         * a regular block device.
         */
-       if (0 == strcmp(ifname, "ubi")) {
+       if (!strcmp(ifname, "ubi")) {
                if (!ubifs_is_mounted()) {
                        printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
                        return -EINVAL;
                }
 
-               *dev_desc = NULL;
-               memset(info, 0, sizeof(*info));
                strcpy((char *)info->type, BOOT_PART_TYPE);
                strcpy((char *)info->name, "UBI");
-#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
-               info->uuid[0] = 0;
-#endif
                return 0;
        }
 #endif
@@ -523,6 +492,8 @@ int blk_get_device_part_str(const char *ifname, const char *dev_part_str,
        /* Look up the device */
        dev = blk_get_device_by_str(ifname, dev_str, dev_desc);
        if (dev < 0) {
+               printf("** Bad device specification %s %s **\n",
+                      ifname, dev_str);
                ret = dev;
                goto cleanup;
        }
@@ -534,7 +505,7 @@ int blk_get_device_part_str(const char *ifname, const char *dev_part_str,
                part = PART_AUTO;
        } else {
                /* Something specified -> use exactly that */
-               part = (int)simple_strtoul(part_str, &ep, 16);
+               part = (int)hextoul(part_str, &ep);
                /*
                 * Less than whole string converted,
                 * or request for whole device, but caller requires partition.
@@ -668,6 +639,13 @@ int part_get_info_by_name_type(struct blk_desc *dev_desc, const char *name,
        part_drv = part_driver_lookup_type(dev_desc);
        if (!part_drv)
                return -1;
+
+       if (!part_drv->get_info) {
+               log_debug("## Driver %s does not have the get_info() method\n",
+                         part_drv->name);
+               return -ENOSYS;
+       }
+
        for (i = 1; i < part_drv->max_entries; i++) {
                ret = part_drv->get_info(dev_desc, i, info);
                if (ret != 0) {
@@ -702,7 +680,7 @@ int part_get_info_by_name(struct blk_desc *dev_desc, const char *name,
  * @param[in] dev_part_str Input string argument, like "0.1#misc"
  * @param[out] dev_desc Place to store the device description pointer
  * @param[out] part_info Place to store the partition information
- * @return 0 on success, or a negative on error
+ * Return: 0 on success, or a negative on error
  */
 static int part_get_info_by_dev_and_name(const char *dev_iface,
                                         const char *dev_part_str,
@@ -714,7 +692,11 @@ static int part_get_info_by_dev_and_name(const char *dev_iface,
        int ret;
 
        /* Separate device and partition name specification */
-       part_str = strchr(dev_part_str, '#');
+       if (dev_part_str)
+               part_str = strchr(dev_part_str, '#');
+       else
+               part_str = NULL;
+
        if (part_str) {
                dup_str = strdup(dev_part_str);
                dup_str[part_str - dev_part_str] = 0;
@@ -725,7 +707,7 @@ static int part_get_info_by_dev_and_name(const char *dev_iface,
        }
 
        ret = blk_get_device_by_str(dev_iface, dev_str, dev_desc);
-       if (ret)
+       if (ret < 0)
                goto cleanup;
 
        ret = part_get_info_by_name(*dev_desc, part_str, part_info);
@@ -767,23 +749,18 @@ void part_set_generic_name(const struct blk_desc *dev_desc,
 {
        char *devtype;
 
-       switch (dev_desc->if_type) {
-       case IF_TYPE_IDE:
-       case IF_TYPE_SATA:
-       case IF_TYPE_ATAPI:
+       switch (dev_desc->uclass_id) {
+       case UCLASS_IDE:
+       case UCLASS_AHCI:
                devtype = "hd";
                break;
-       case IF_TYPE_SCSI:
+       case UCLASS_SCSI:
                devtype = "sd";
                break;
-       case IF_TYPE_USB:
+       case UCLASS_USB:
                devtype = "usbd";
                break;
-       case IF_TYPE_DOC:
-               devtype = "docd";
-               break;
-       case IF_TYPE_MMC:
-       case IF_TYPE_SD:
+       case UCLASS_MMC:
                devtype = "mmcsd";
                break;
        default: