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