This makes it easy to support referencing further cdev aliases
from the device tree.

Signed-off-by: Ahmad Fatoum <a.fat...@pengutronix.de>
---
 commands/boot.c                 |  1 +
 common/cdev-alias.c             | 31 +++++++++++++++++++++++++++++++
 drivers/misc/storage-by-alias.c | 25 +++++++++----------------
 3 files changed, 41 insertions(+), 16 deletions(-)

diff --git a/commands/boot.c b/commands/boot.c
index 502ad19e5e75..97c574b4a0a3 100644
--- a/commands/boot.c
+++ b/commands/boot.c
@@ -184,6 +184,7 @@ BAREBOX_CMD_HELP_TEXT("- \"bootchooser\": boot with barebox 
bootchooser")
 #endif
 #ifdef CONFIG_BOOT_DEFAULTS
 BAREBOX_CMD_HELP_TEXT("- \"bootsource\": boot from the device barebox has been 
started from")
+BAREBOX_CMD_HELP_TEXT("- \"diskuuid.*\": boot from disk with specified 
diskuuid")
 #endif
 BAREBOX_CMD_HELP_TEXT("")
 BAREBOX_CMD_HELP_TEXT("Multiple bootsources may be given which are probed in 
order until")
diff --git a/common/cdev-alias.c b/common/cdev-alias.c
index 09e6770f5dea..4ce79d0ba4a7 100644
--- a/common/cdev-alias.c
+++ b/common/cdev-alias.c
@@ -35,8 +35,39 @@ static int cdev_alias_resolve_bootsource(struct cdev_alias 
*cdev_alias,
        return fn(cdev, data);
 }
 
+static int cdev_alias_resolve_diskuuid(struct cdev_alias *cdev_alias,
+                                      const char *uuid,
+                                      cdev_alias_processor_t fn,
+                                      void *data)
+{
+       struct cdev *cdev;
+       char *arg;
+
+       arg = xstrdup(uuid);
+       uuid = strsep(&arg, ".");
+       if (!uuid || !*uuid)
+               return -EINVAL;
+
+       for_each_cdev(cdev) {
+               if (cdev_is_partition(cdev))
+                       continue;
+
+               if (strcasecmp(cdev->diskuuid, uuid))
+                       continue;
+
+               cdev = cdev_find_partition(cdev, arg);
+               if (!cdev)
+                       return -ENODEV;
+
+               return fn(cdev, data);
+       }
+
+       return 0;
+}
+
 static struct cdev_alias cdev_alias_aliases[] = {
        { "bootsource", cdev_alias_resolve_bootsource },
+       { "diskuuid", cdev_alias_resolve_diskuuid },
        { /* sentinel */}
 };
 
diff --git a/drivers/misc/storage-by-alias.c b/drivers/misc/storage-by-alias.c
index 2795ff93c07c..0bf0059ad41c 100644
--- a/drivers/misc/storage-by-alias.c
+++ b/drivers/misc/storage-by-alias.c
@@ -119,12 +119,15 @@ static struct cdev_operations sba_ops = {
        .truncate = sba_truncate,
 };
 
-static void sba_add_partitions(struct sba *sba, struct cdev *rcdev)
+static int sba_add_partitions(struct cdev *rcdev, void *data)
 {
+       struct sba *sba = data;
        int ret;
 
+       dev_dbg(sba->dev, "Adding %s -> %s\n", sba->alias, rcdev->name);
+
        if (sba->rcdev)
-               return;
+               return 0;
 
        sba->rcdev = rcdev;
        sba->cdev.name = sba->alias;
@@ -139,26 +142,16 @@ static void sba_add_partitions(struct sba *sba, struct 
cdev *rcdev)
        ret = devfs_create(&sba->cdev);
        if (ret) {
                dev_err(sba->dev, "Failed to create cdev: %s\n", 
strerror(-ret));
-               return;
+               return 0;
        }
 
        of_parse_partitions(&sba->cdev, sba->dev->of_node);
+       return 0;
 }
 
 static void check_exist(struct sba *sba)
 {
-       struct cdev *cdev;
-
-       for_each_cdev(cdev) {
-               if (cdev_is_partition(cdev))
-                       continue;
-               if (strcmp(cdev->diskuuid, sba->alias))
-                       continue;
-
-               dev_dbg(sba->dev, "Found %s %s\n", cdev->name, cdev->diskuuid);
-               sba_add_partitions(sba, cdev);
-               return;
-       }
+       cdev_alias_resolve_for_each(sba->alias, sba_add_partitions, sba);
 }
 
 static int sba_detect(struct device *dev)
@@ -181,7 +174,7 @@ static int storage_by_uuid_init(struct sba *sba)
        if (ret)
                return ret;
 
-       sba->alias = xstrdup(uuid);
+       sba->alias = xasprintf("diskuuid.%s", uuid);
 
        return 0;
 }
-- 
2.39.5


Reply via email to