From: "Richard Weinberger (sigma star gmbh)" <rw@kernel.org>
To: linux-mtd@lists.infradead.org
Cc: fuse-devel@lists.linux.dev, netdev@vger.kernel.org,
horms@kernel.org, pabeni@redhat.com, kuba@kernel.org,
edumazet@google.com, davem@davemloft.net, miklos@szeredi.hu,
vigneshr@ti.com, miquel.raynal@bootlin.com,
david.oberhollenzer@sigma-star.at, upstream+linux@sigma-star.at,
Richard Weinberger <richard@nod.at>
Subject: [PATCH 08/10] mtd: Allow passing a custom mtdparts string to the cmdline parser
Date: Mon, 30 Nov 2026 09:26:31 +0100 [thread overview]
Message-ID: <20261130082634.2750846-9-rw@kernel.org> (raw)
In-Reply-To: <20261130082634.2750846-1-rw@kernel.org>
From: Richard Weinberger <richard@nod.at>
The cmdline parser takes the partition definitions from the mtdparts=
parameter of the kernel command line. For special purpose MTDs like
MUSE it is useful to pass a custom mtdparts string to the parser. That
way an MTD simulator in userspace can define the partitions of its MTD.
Add an mtdparts pointer to struct mtd_part_parser_data. If it is set,
the parser uses this string and ignores the kernel command line.
What is parsed from a custom string is freed before the parser returns,
while the definitions from the kernel command line are kept forever. So
far the partitions handed to the MTD core pointed to names inside these
definitions. Copy the names into the returned array, such that it does
not depend on the lifetime of what was parsed.
Signed-off-by: Richard Weinberger <richard@nod.at>
---
drivers/mtd/parsers/cmdlinepart.c | 99 +++++++++++++++++++++++++------
include/linux/mtd/partitions.h | 2 +
2 files changed, 82 insertions(+), 19 deletions(-)
diff --git a/drivers/mtd/parsers/cmdlinepart.c b/drivers/mtd/parsers/cmdlinepart.c
index 4caf1b3804f2..9848bbea271c 100644
--- a/drivers/mtd/parsers/cmdlinepart.c
+++ b/drivers/mtd/parsers/cmdlinepart.c
@@ -56,7 +56,7 @@ struct cmdline_mtd_partition {
};
/* mtdpart_setup() parses into here */
-static struct cmdline_mtd_partition *partitions;
+static struct cmdline_mtd_partition *cmdline_partitions;
/* the command line passed to mtdpart_setup() */
static char *mtdparts;
@@ -209,10 +209,8 @@ static struct mtd_partition * newpart(char *s,
/*
* Parse the command line.
*/
-static int mtdpart_setup_real(char *s)
+static int mtdpart_setup_real(char *s, struct cmdline_mtd_partition **partitions)
{
- cmdline_parsed = 1;
-
for( ; s != NULL; )
{
struct cmdline_mtd_partition *this_mtd;
@@ -292,8 +290,8 @@ static int mtdpart_setup_real(char *s)
strscpy(this_mtd->mtd_id, mtd_id, mtd_id_len + 1);
/* link into chain */
- this_mtd->next = partitions;
- partitions = this_mtd;
+ this_mtd->next = *partitions;
+ *partitions = this_mtd;
pr_debug("mtdid=<%s> num_parts=<%d>\n",
this_mtd->mtd_id, this_mtd->num_parts);
@@ -314,6 +312,62 @@ static int mtdpart_setup_real(char *s)
return 0;
}
+/*
+ * Free a list built by mtdpart_setup_real(). The partitions of an entry,
+ * their names and the entry itself share one allocation, see newpart().
+ */
+static void free_partitions(struct cmdline_mtd_partition *part)
+{
+ while (part) {
+ struct cmdline_mtd_partition *next = part->next;
+
+ kfree(part->parts);
+ part = next;
+ }
+}
+
+/*
+ * Copy the partitions of an entry and their names into one allocation. The
+ * names of the entry cannot be handed out: for a custom mtdparts string
+ * they are gone when parse_cmdline_partitions() returns.
+ */
+static struct mtd_partition *dup_parts(const struct cmdline_mtd_partition *part)
+{
+ size_t parts_size = sizeof(*part->parts) * part->num_parts;
+ size_t names_size = 0;
+ struct mtd_partition *parts;
+ char *name;
+ int i;
+
+ for (i = 0; i < part->num_parts; i++)
+ names_size += strlen(part->parts[i].name) + 1;
+
+ parts = kmalloc(parts_size + names_size, GFP_KERNEL);
+ if (!parts)
+ return NULL;
+
+ memcpy(parts, part->parts, parts_size);
+ name = (char *)(parts + part->num_parts);
+ for (i = 0; i < part->num_parts; i++) {
+ size_t len = strlen(part->parts[i].name) + 1;
+
+ parts[i].name = memcpy(name, part->parts[i].name, len);
+ name += len;
+ }
+
+ return parts;
+}
+
+/* Parse the kernel command line once, what it defines is kept. */
+static int parse_kernel_cmdline(void)
+{
+ if (cmdline_parsed)
+ return 0;
+
+ cmdline_parsed = 1;
+ return mtdpart_setup_real(cmdline, &cmdline_partitions);
+}
+
/*
* Main function to be called from the MTD mapping driver/device to
* obtain the partitioning information. At this point the command line
@@ -326,28 +380,34 @@ static int parse_cmdline_partitions(struct mtd_info *master,
struct mtd_part_parser_data *data)
{
unsigned long long offset;
- int i, err;
+ int i, ret;
struct cmdline_mtd_partition *part;
+ struct cmdline_mtd_partition *custom_parts = NULL;
const char *mtd_id = master->name;
- /* parse command line */
- if (!cmdline_parsed) {
- err = mtdpart_setup_real(cmdline);
- if (err)
- return err;
+ if (data && data->mtdparts) {
+ /* A string of the caller, the kernel command line is not used. */
+ ret = mtdpart_setup_real(data->mtdparts, &custom_parts);
+ part = custom_parts;
+ } else {
+ ret = parse_kernel_cmdline();
+ part = cmdline_partitions;
}
+ if (ret)
+ goto out;
/*
* Search for the partition definition matching master->name.
* If master->name is not set, stop at first partition definition.
*/
- for (part = partitions; part; part = part->next) {
+ for (; part; part = part->next) {
if ((!mtd_id) || (!strcmp(part->mtd_id, mtd_id)))
break;
}
+ ret = 0;
if (!part)
- return 0;
+ goto out;
for (i = 0, offset = 0; i < part->num_parts; i++) {
if (part->parts[i].offset == OFFSET_CONTINUOUS)
@@ -375,12 +435,13 @@ static int parse_cmdline_partitions(struct mtd_info *master,
}
}
- *pparts = kmemdup(part->parts, sizeof(*part->parts) * part->num_parts,
- GFP_KERNEL);
- if (!*pparts)
- return -ENOMEM;
+ *pparts = dup_parts(part);
+ ret = *pparts ? part->num_parts : -ENOMEM;
- return part->num_parts;
+out:
+ /* What was parsed from the kernel command line is kept. */
+ free_partitions(custom_parts);
+ return ret;
}
diff --git a/include/linux/mtd/partitions.h b/include/linux/mtd/partitions.h
index b74a539ec581..6c8b3399143d 100644
--- a/include/linux/mtd/partitions.h
+++ b/include/linux/mtd/partitions.h
@@ -65,9 +65,11 @@ struct device_node;
/**
* struct mtd_part_parser_data - used to pass data to MTD partition parsers.
* @origin: for RedBoot, start address of MTD device
+ * @mtdparts: for cmdline parser, use this string instead of mtdparts= from cmdline
*/
struct mtd_part_parser_data {
unsigned long origin;
+ char *mtdparts;
};
--
2.55.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
next prev parent reply other threads:[~2026-11-30 8:27 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 8:26 [PATCH 0/10] MUSE: Userspace backed MTD v4 Richard Weinberger (sigma star gmbh)
2026-11-30 8:26 ` [PATCH 01/10] fuse: Share cuse_parse_one() as fuse_kv_parse_one() Richard Weinberger (sigma star gmbh)
2026-11-30 8:26 ` [PATCH 02/10] fuse: Add a public header for users outside of fs/fuse/ Richard Weinberger (sigma star gmbh)
2026-11-30 8:26 ` [PATCH 03/10] fuse: Add an interface for userspace backed devices Richard Weinberger (sigma star gmbh)
2026-11-30 8:26 ` [PATCH 04/10] fuse: Convert CUSE to the " Richard Weinberger (sigma star gmbh)
2026-11-30 8:26 ` [PATCH 05/10] fuse: Allow three output arguments per request Richard Weinberger (sigma star gmbh)
2026-11-30 8:26 ` [PATCH 06/10] fuse: Add MUSE specific defines to FUSE interface Richard Weinberger (sigma star gmbh)
2026-11-30 8:26 ` [PATCH 07/10] mtd: Add MTD_MUSE flag Richard Weinberger (sigma star gmbh)
2026-11-30 8:26 ` Richard Weinberger (sigma star gmbh) [this message]
2026-11-30 8:26 ` [PATCH 09/10] mtd: Implement MUSE - MTD in userspace Richard Weinberger (sigma star gmbh)
2026-11-30 8:26 ` [PATCH 10/10] MAINTAINERS: Add entry for MUSE Richard Weinberger (sigma star gmbh)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261130082634.2750846-9-rw@kernel.org \
--to=rw@kernel.org \
--cc=davem@davemloft.net \
--cc=david.oberhollenzer@sigma-star.at \
--cc=edumazet@google.com \
--cc=fuse-devel@lists.linux.dev \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-mtd@lists.infradead.org \
--cc=miklos@szeredi.hu \
--cc=miquel.raynal@bootlin.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=richard@nod.at \
--cc=upstream+linux@sigma-star.at \
--cc=vigneshr@ti.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox