From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796027245; b=P7o288jw5nUPf3nR3l3cTVIRgemr5qFfS8nAys4IBjcRRaZuv3v2W4gmeCRrrhHr+b2E Ha4/mSeMc/vRBhlVCP0CJyVUUS82StjQOnC8aN7z/OC7BsKrhlC7HRXKPS9hvncS3ieEO QeSUexRs6DtCK2btMylHvxbPxwH1s3t5SVAnLa2hPkki+7x6V1FpYY0VYo82Vv3bknuuN Mx0L8ygOV5Jh5UhN3QWfX8o782MRzH7fEpIYBVzBg1bg54m4Zsh78GRENH12uZJM7xsBR inNsyNFErC1pNhxdD7dK95wmPln/5mgxD7YBe/Ijz9xlhr8nE/U9K8dJo1uXeyb5O2A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796027245; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=9MuFPS701Y4dHHKqKaSOrne4937BCk0oWae5SX3Fmuk=; b=QhwMErzvBjrgbhIsN/m9/pLXNmOVEPacVaPJUF/iCOSzPJRqgu2lNH8e5QMlQYFMjtX4 xxAom+7INA9CP0gg9M//K521mUV3ydK+PbD3m6m8Yx2I5+USS2axBVINNWHgBuuENpjpM dSQjy0a8sphr/Eha3cFYthvIOyLSYc0Px2+nAhrSxmc3knsCKz+zAEtkrkAP+IQWC8H4q mucEGxET0kq/aI5zOu1mr1VsNGcj9kPeYejCbzZREDp3DXPbldYBwQqWHSxwGBlBtlMZC x0jS9GKP9yIXqWt55Ba5yQmOX9dv8gE0TLBVAByR09vxhX3pAdiObNf7Z9aNX9MSL9g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=OjhEZtXf; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C45EC606A337; Mon, 30 Nov 2026 08:27:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=OjhEZtXf Received: by smtp.kernel.org (Postfix) with ESMTPSA id 930671F00923; Mon, 30 Nov 2026 08:27:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796027245; bh=9MuFPS701Y4dHHKqKaSOrne4937BCk0oWae5SX3Fmuk=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=OjhEZtXfcHS1C+9ip56A+T0V/0D0a7MRO4UDXX1nLmWT5T3uzVVctnm/ao2304/Ww pcKOboXErZReQk2E9r2CVA02I0+kn6RdTwaIi1vUeEqCN28Hju5DZ0xElNSthoBOqE 0xUgrQeNIxZtOGRdo8kfUm65NvXUhsf9m73hBat8ENLizA67qeNlDPCwzcUxbCrG/g JJk2DxlqF+WfI7ZxXp8iadvAxzKXJ53IeArQcCd16+Q4+CxVULbfSqhmkDPGnz0Rv9 ub1yCK/Pib+lPWwX1pCNsrnaMhwH4BPYzr39dbDsXfFZnLIyjsk0WnRfrmSmxHtbrD YThn6EpK+WY5Q== From: "Richard Weinberger (sigma star gmbh)" 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 Subject: [PATCH 08/10] mtd: Allow passing a custom mtdparts string to the cmdline parser Date: Mon, 30 Nov 2026 09:26:31 +0100 Message-ID: <20261130082634.2750846-9-rw@kernel.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261130082634.2750846-1-rw@kernel.org> References: <20261130082634.2750846-1-rw@kernel.org> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Richard Weinberger 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 --- 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