From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0BC47606A31C for ; Mon, 30 Nov 2026 08:27:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=yy6keiDx; dkim=fail reason="signature verification failed" (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=OjhEZtXf DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Unsubscribe-Post:List-Owner; bh=dnUBCT8Y3EqTP5uJF/GCO6nEw6NsXlcVcrNnbstXXj0=; b=yy6keiDxrHeqGCR/KMvdk9wzsb A0XwlEL+qiifdOBsWc/OEnSgK5muLztRH1PQ7u3oEqVNvPJ4nM3AopP5NsjpZuwuMPlcvQ05+nhJT OUuu1ZwdECXKPdHgvWiOV8Ua7g1U9br4qzd4A+sdupNX9MmScUtehCPW8ve965Oo3qBgiU0r2Ej6I fev1gYTHJdCzzOVS3YeT/Gw977JSyeskpy/ddY0akKyxdO9DzogFD6gx8WByLIdfD53FYR/Oq97fR R0KaZUDDX47uzWMcff8DOErvxtm8yyCsTp0KHY1je/pXFHyoyQ/stjF4d7pHuuAQaID+KYMDkmvFv vhY/tzOg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xXwjQ-00000003kNu-35ik; Mon, 30 Nov 2026 08:27:28 +0000 Received: from sea.source.kernel.org ([172.234.252.31]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xXwjN-00000003kMF-35fz for linux-mtd@lists.infradead.org; Mon, 30 Nov 2026 08:27:26 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 757984470F; Mon, 30 Nov 2026 08:27:25 +0000 (UTC) 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-Archived-By: MIME-Version: 1.0 X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Linux MTD discussion mailing list List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-mtd" X-Spam-Status: No, score=-16.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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 ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/