🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #24487

[PATCH 08/10] mtd: Allow passing a custom mtdparts string to the cmdline parser

#24487
From: "Richard Weinberger (sigma star ...
Date: Mon, 30 Nov 2026 08:26
202 lines
14005 bytes
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
Technical Details
Message-ID: <20261130082634.2750846-9-rw@kernel.org>
Path: mbox-mo1.pugleaf.net!news-lon.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-374016-linux-kernel-netdev=dewy.ru
References: <20261130082634.2750846-1-rw@kernel.org>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lon.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-374016-linux-kernel-netdev=dewy.ru
From: rw@kernel.org ("Richard Weinberger (sigma star gmbh)")
Newsgroups: linux.kernel.netdev
Subject: [PATCH 08/10] mtd: Allow passing a custom mtdparts string to the cmdline parser
Date: Mon, 30 Nov 2026 09:26:31 +0100
Organization: Usenet Network
Message-ID: <20261130082634.2750846-9-rw@kernel.org>
References: <20261130082634.2750846-1-rw@kernel.org>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="1376365"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:LDuLHafrPJGrk85pthdt7wX2puQ= sha256:m02MAodVa2jDDMZ+qLIA8RPfBT4HTHFs8opVqA/Zm+E=
	sha1:tVVONSRiBuzQbXReBQKf8UrC7yQ= sha256:k50jso9ybnAeQuLYCqSywaCnv/XuHScnnvhsYNXYvq8=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id B4982125FCF; Mon, 30 Nov 2026 11:27:50 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.401
X-Spam-Status: No, score=-3.401 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1,
	DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=kernel.org
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id 9fFz8Fsva3ss;
	Mon, 30 Nov 2026 11:27:48 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-374016-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id B5300125FCD; Mon, 30 Nov 2026 11:27:39 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id D191127455; Mon, 30 Nov 2026 08:27:26 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1796027246;
	b=XuI3ozE7eQtVoE/gka7O1I+mbHXRDOiJeZZoBCX4V7lP8zTzzULdycRHm1DkV4wfpGH5
	 hnf4t3QRqyiUNaMGlDGNsqI43UTnqj5hlwidIz9gbTrDvDqTqrZjbVjaa9bQixdf3QMJI
	 +UAdtxCWHsVvv7QNiP/47gE1TT2fL5Ilwi57oVK1Ijb40y9Dz7e7Kb9ZJwyJ7maVMkNDN
	 Lbjiq0AWxo4H1Ut/lHUJbNrScsIEZ4j9b6sG/JFPXQMXkWfjASLkKgAZXiX1LIhMP2+yX
	 BBUYg2R2X/QvlncElT5xYnldnBHorVyQQUjRT4WVVIDf/XuY5/yDBe7pUej/TeI60TQ==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796027246;
	h=X-Original-To:Received:Received:DKIM-Signature:From:To:Cc:Subject:
	 Date:Message-ID:X-Mailer:In-Reply-To:References:X-Mailing-List:
	 List-Id:List-Post:List-Help:List-Subscribe:List-Unsubscribe:
	 List-Owner:Precedence:MIME-Version:Content-Transfer-Encoding;
	bh=9MuFPS701Y4dHHKqKaSOrne4937BCk0oWae5SX3Fmuk=;
	b=Bih+GqjN10a9MXkKyCMXN1AilYY1zfyARJrAitEmgIjCrkN00aiMQLt/bSNxRhSJLQfl
	 zLWTV6bZBC7bErSvSjI+O3k5zuEQ71DmHdyE5bO2P5qOBnXAa9ac3KCq2XPyVL/wRPuyb
	 C2LdlHYS3CNBekdQ5o677uzT2m0Lmp/meuGmBUwkNCGww9xkUVJUulB2s2OXtEsuk7TQg
	 imrTTcQvJwxpv4hw+JLtHxWriDidRNOWPi+vWkREz5w3NP0SBdp+jCbwRYjTgWArOjtkA
	 KMlb8ES4GiTDUs2JjCYnlKQxP1gLZ6LuXJSvhKqGT63TAo4SV2N4n8/ICCemXHLwfTQ==
X-ARC-Authentication-Results: i=2; 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=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id A62AE606A320;
	Mon, 30 Nov 2026 08:27:26 +0000 (UTC)
X-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
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18
X-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==
X-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==
X-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
X-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)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 930671F00923;
	Mon, 30 Nov 2026 08:27:20 +0000 (UTC)
X-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==
X-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>
X-Mailer: git-send-email 2.55.0
X-In-Reply-To: <20261130082634.2750846-1-rw@kernel.org>
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list