[PATCH 10/16] block: add get_rootarg block op into block_device_ops
Ahmad Fatoum
a.fatoum at pengutronix.de
Tue Apr 1 03:48:00 PDT 2025
The root argument from cdev generation only succeeds for partitions of
block devices anyway, so it makes sense to move the logic to the block
device support.
Further, the code can be simplified a bit by moving the MCI-specific
logic into the MCI core.
Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
common/block.c | 22 +++++++++++++++++
common/bootm.c | 1 +
drivers/mci/mci-core.c | 45 ++++++++++++++++++++++++++++++---
fs/fs.c | 56 ------------------------------------------
include/block.h | 6 +++++
include/driver.h | 6 -----
include/fs.h | 1 -
7 files changed, 70 insertions(+), 67 deletions(-)
diff --git a/common/block.c b/common/block.c
index 3ee33f2bf071..7066abc5f404 100644
--- a/common/block.c
+++ b/common/block.c
@@ -589,3 +589,25 @@ const char *blk_type_str(enum blk_type type)
return "unknown";
}
}
+
+char *cdev_get_linux_rootarg(const struct cdev *partcdev)
+{
+ const struct cdev *cdevm;
+ struct block_device *blk;
+ char *rootarg = NULL;
+
+ if (!partcdev)
+ return NULL;
+
+ cdevm = partcdev->master ?: partcdev;
+ blk = cdev_get_block_device(cdevm);
+ if (!blk)
+ return NULL;
+
+ if (blk->ops->get_rootarg)
+ rootarg = blk->ops->get_rootarg(blk, partcdev);
+ if (!rootarg && partcdev->partuuid[0] != 0)
+ rootarg = basprintf("root=PARTUUID=%s", partcdev->partuuid);
+
+ return rootarg;
+}
diff --git a/common/bootm.c b/common/bootm.c
index 6b63987f0900..e781b48b0e7d 100644
--- a/common/bootm.c
+++ b/common/bootm.c
@@ -6,6 +6,7 @@
#include <fs.h>
#include <malloc.h>
#include <memory.h>
+#include <block.h>
#include <libfile.h>
#include <image-fit.h>
#include <globalvar.h>
diff --git a/drivers/mci/mci-core.c b/drivers/mci/mci-core.c
index 947fe44c088f..aae63484a09b 100644
--- a/drivers/mci/mci-core.c
+++ b/drivers/mci/mci-core.c
@@ -676,11 +676,8 @@ static void mci_part_add(struct mci *mci, uint64_t size,
if (area_type == MMC_BLK_DATA_AREA_RPMB)
mci->rpmb_part = part;
-
- if (area_type == MMC_BLK_DATA_AREA_MAIN) {
+ else if (area_type == MMC_BLK_DATA_AREA_MAIN)
cdev_set_of_node(&part->blk.cdev, mci->host->hw_dev->of_node);
- part->blk.cdev.flags |= DEVFS_IS_MCI_MAIN_PART_DEV;
- }
mci->nr_parts++;
}
@@ -2530,10 +2527,50 @@ static void mci_parse_cid(struct mci *mci)
dev_add_param_uint32_fixed(dev, "cid_month", mci->cid.month, "%0u");
}
+static bool cdev_partname_equal(const struct cdev *a,
+ const struct cdev *b)
+{
+ return a->partname && b->partname &&
+ !strcmp(a->partname, b->partname);
+}
+
+static char *mci_get_linux_mmcblkdev(struct block_device *blk,
+ const struct cdev *partcdev)
+
+{
+ struct mci_part *mci_part = container_of(blk, struct mci_part, blk);
+ struct cdev *cdevm = partcdev->master, *cdev;
+ int id, partnum;
+
+ if (mci_part->area_type != MMC_BLK_DATA_AREA_MAIN)
+ return NULL;
+
+ id = of_alias_get_id(cdev_of_node(cdevm), "mmc");
+ if (id < 0)
+ return NULL;
+
+ partnum = 1; /* linux partitions are 1 based */
+ list_for_each_entry(cdev, &cdevm->partitions, partition_entry) {
+
+ /*
+ * Partname is not guaranteed but this partition cdev is listed
+ * in the partitions list so we need to count it instead of
+ * skipping it.
+ */
+ if (cdev_partname_equal(partcdev, cdev))
+ return basprintf("root=/dev/mmcblk%dp%d", id, partnum);
+ partnum++;
+ }
+
+ return NULL;
+}
+
static struct block_device_ops mci_ops = {
.read = mci_sd_read,
.write = IS_ENABLED(CONFIG_MCI_WRITE) ? mci_sd_write : NULL,
.erase = IS_ENABLED(CONFIG_MCI_ERASE) ? mci_sd_erase : NULL,
+ .get_rootarg = IS_ENABLED(CONFIG_MMCBLKDEV_ROOTARG) ?
+ mci_get_linux_mmcblkdev : NULL,
};
static int mci_set_boot(struct param_d *param, void *priv)
diff --git a/fs/fs.c b/fs/fs.c
index 48a6e960e812..98e2b9b73663 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -95,8 +95,6 @@ void cdev_print(const struct cdev *cdev)
printf(" table-partition");
if (cdev->flags & DEVFS_PARTITION_FOR_FIXUP)
printf(" fixup");
- if (cdev->flags & DEVFS_IS_MCI_MAIN_PART_DEV)
- printf(" mci-main-partition");
if (cdev->flags & DEVFS_IS_MBR_PARTITIONED)
printf(" mbr-partitioned");
if (cdev->flags & DEVFS_IS_GPT_PARTITIONED)
@@ -3060,60 +3058,6 @@ int popd(char *oldcwd)
return ret;
}
-static bool cdev_partname_equal(const struct cdev *a,
- const struct cdev *b)
-{
- return a->partname && b->partname &&
- !strcmp(a->partname, b->partname);
-}
-
-static char *get_linux_mmcblkdev(const struct cdev *root_cdev)
-{
- struct cdev *cdevm = root_cdev->master, *cdev;
- int id, partnum;
-
- if (!IS_ENABLED(CONFIG_MMCBLKDEV_ROOTARG))
- return NULL;
- if (!cdevm || !cdev_is_mci_main_part_dev(cdevm))
- return NULL;
-
- id = of_alias_get_id(cdev_of_node(cdevm), "mmc");
- if (id < 0)
- return NULL;
-
- partnum = 1; /* linux partitions are 1 based */
- list_for_each_entry(cdev, &cdevm->partitions, partition_entry) {
-
- /*
- * Partname is not guaranteed but this partition cdev is listed
- * in the partitions list so we need to count it instead of
- * skipping it.
- */
- if (cdev_partname_equal(root_cdev, cdev))
- return basprintf("root=/dev/mmcblk%dp%d", id, partnum);
- partnum++;
- }
-
- return NULL;
-}
-
-char *cdev_get_linux_rootarg(const struct cdev *cdev)
-{
- char *str;
-
- if (!cdev)
- return NULL;
-
- str = get_linux_mmcblkdev(cdev);
- if (str)
- return str;
-
- if (cdev->partuuid[0] != 0)
- return basprintf("root=PARTUUID=%s", cdev->partuuid);
-
- return NULL;
-}
-
/*
* Mount a device to a directory.
* We do this by registering a new device on which the filesystem
diff --git a/include/block.h b/include/block.h
index b57d99a3fc08..a992eba43191 100644
--- a/include/block.h
+++ b/include/block.h
@@ -14,6 +14,7 @@ struct block_device_ops {
int (*write)(struct block_device *, const void *buf, sector_t block, blkcnt_t num_blocks);
int (*erase)(struct block_device *blk, sector_t block, blkcnt_t num_blocks);
int (*flush)(struct block_device *);
+ char *(*get_rootarg)(struct block_device *blk, const struct cdev *partcdev);
};
struct chunk;
@@ -82,6 +83,7 @@ static inline int block_flush(struct block_device *blk)
#ifdef CONFIG_BLOCK
struct block_device *cdev_get_block_device(const struct cdev *cdev);
unsigned file_list_add_blockdevs(struct file_list *files);
+char *cdev_get_linux_rootarg(const struct cdev *partcdev);
#else
static inline struct block_device *cdev_get_block_device(const struct cdev *cdev)
{
@@ -91,6 +93,10 @@ static inline unsigned file_list_add_blockdevs(struct file_list *files)
{
return 0;
}
+static inline char *cdev_get_linux_rootarg(const struct cdev *partcdev)
+{
+ return NULL;
+}
#endif
static inline bool cdev_is_block_device(const struct cdev *cdev)
diff --git a/include/driver.h b/include/driver.h
index 60bcfc5e2f7d..c055e7b47b47 100644
--- a/include/driver.h
+++ b/include/driver.h
@@ -561,7 +561,6 @@ extern struct list_head cdev_list;
#define DEVFS_PARTITION_FIXED (1U << 0)
#define DEVFS_PARTITION_READONLY (1U << 1)
#define DEVFS_IS_CHARACTER_DEV (1U << 3)
-#define DEVFS_IS_MCI_MAIN_PART_DEV (1U << 4)
#define DEVFS_PARTITION_FROM_OF (1U << 5)
#define DEVFS_PARTITION_FROM_TABLE (1U << 6)
#define DEVFS_IS_MBR_PARTITIONED (1U << 7)
@@ -620,11 +619,6 @@ static inline void cdev_create_default_automount(struct cdev *cdev)
}
#endif
-static inline bool cdev_is_mci_main_part_dev(struct cdev *cdev)
-{
- return cdev->flags & DEVFS_IS_MCI_MAIN_PART_DEV;
-}
-
#define DEVFS_PARTITION_APPEND 0
/**
diff --git a/include/fs.h b/include/fs.h
index fafd91343b5c..bf927da4207e 100644
--- a/include/fs.h
+++ b/include/fs.h
@@ -175,7 +175,6 @@ void mount_all(void);
void fsdev_set_linux_rootarg(struct fs_device *fsdev, const char *str);
char *path_get_linux_rootarg(const char *path);
-char *cdev_get_linux_rootarg(const struct cdev *cdev);
static inline const char *devpath_to_name(const char *devpath)
{
--
2.39.5
More information about the barebox
mailing list