mtd: cmdlinepart: fix the wrong check condition
PATCH v2 1/1 mtd: cmdlinepart: fix the wrong check condition
The mtd_id is set by the name of a mtd device driver.
As a nand controller driver, even we do not set the @name of the mtd_info{}, the nand_get_flash_type() will set it with the nand type's name. So the mtd_id can never be NULL in this case.
But as a nor controller driver which may does not call the nand_get_flash_type(), there is a risk that the mtd_id becames NULL.
If the mtd_id is NULL, the check condition will be true. If we accidentally set some partitions in the kernel command line, just like:
#gpmi-nand:20m(boot),20m(kernel),1g(rootfs),-(user)
The cmdlinepart may parses out several mtd partitions right now. This is obviously wrong. We even do not enable the gpmi-nand in this case.
The patch comes from Artem's suggestion code which is better then mine.
--- drivers/mtd/cmdlinepart.c | 5 ++++- 1 files changed, 4 insertions(+), 1 deletions(-)
drivers/mtd/cmdlinepart.c +4 -1
@@ -322,13 +322,16 @@ static int parse_cmdline_partitions(struct mtd_info *master,struct cmdline_mtd_partition *part;const char *mtd_id = master->name; +if (!mtd_id) +return 0 ; +/* parse command line */ if (!cmdline_parsed)mtdpart_setup_real (cmdline);for (part = partitions; part; part = part->next) { -if ((!mtd_id) || (!strcmp (part->mtd_id, mtd_id))) +if (!strcmp (part->mtd_id, mtd_id)) {for (i =0 , offset =0 ; i < part->num_parts; i++) {
Signature · 1 line
1.7.4.4
Hi Huang, Artem,
Quoted 12 lines · On Sat, 25 Aug 2012 16:06:50 -0400 Huang Shijie <[email protected]> wrote:
drivers/mtd/cmdlinepart.c +3
-0@@ -322,13 +322,16 @@ static int parse_cmdline_partitions(struct mtd_info *master,struct cmdline_mtd_partition *part;const char *mtd_id = master->name; +if (!mtd_id) +return 0 ; +/* parse command line */
drivers/mtd/cmdlinepart.c static int parse_cmdline_partitions(struct mtd_info *master, · line 337
Quoted 10 lines
drivers/mtd/cmdlinepart.c +1
-1if (!cmdline_parsed)mtdpart_setup_real (cmdline);for (part = partitions; part; part = part->next) { -if ((!mtd_id) || (!strcmp (part->mtd_id, mtd_id))) +if (!strcmp (part->mtd_id, mtd_id)) {for (i =0 , offset =0 ; i < part->num_parts; i++) {
This changes the behavior of cmdling parsing, which might affect users expecting the old behavior.
According to the remark above 'parse_cmdline_partitions':
* It returns partitions for the requested mtd device, or
* the first one in the chain if a NULL mtd_id is passed in.
I think the purpose of a NULL 'mtd_id' was to support simple systems where there's a single driver and a single chip.
The driver could be dumb, not specifying its 'mtd_info->name' (thus, a NULL mtd_id is passed).
In this case, since the system is simply configured (one driver, one chip), 'parse_cmdline_partitions' simply disregards the "mtd-id" name specified in the cmdline string, allowing the user to present some arbitrary string there.
I quite remember seeing this pattern somewhere in the past, I don't know if it's still used, though.
Obviously if you have many drivers (and many chips) in a system, that won't work; the drivers must initialize 'mtd_info->name' and the user should present a cmdline that has explicit 'mtd-id's.
So question is, would we like to prohibit NULL mtd-id?
If so, we must make sure all drivers are properly assigning their 'mtd_info->name', and all users correctly specifying 'mtd-id' in their "mtdparts" cmdline strings.
Regards,
Shmulik
Quoted 39 lines · Shmulik Ladkani
Hi Huang, Artem,
Quoted 12 lines · On Sat, 25 Aug 2012 16:06:50 -0400 Huang Shijie <[email protected]> wrote:
drivers/mtd/cmdlinepart.c +3
-0@@ -322,13 +322,16 @@ static int parse_cmdline_partitions(struct mtd_info *master,struct cmdline_mtd_partition *part;const char *mtd_id = master->name; +if (!mtd_id) +return 0 ; +/* parse command line */ drivers/mtd/cmdlinepart.c static int parse_cmdline_partitions(struct mtd_info *master, · line 337
Quoted 10 lines
drivers/mtd/cmdlinepart.c +1
-1if (!cmdline_parsed)mtdpart_setup_real (cmdline);for (part = partitions; part; part = part->next) { -if ((!mtd_id) || (!strcmp (part->mtd_id, mtd_id))) +if (!strcmp (part->mtd_id, mtd_id)) {for (i =0 , offset =0 ; i < part->num_parts; i++) {This changes the behavior of cmdling parsing, which might affect users expecting the old behavior.
According to the remark above '
parse_cmdline_partitions':* It returns partitions for the requested mtd device, or
* the first one in the chain if a NULLmtd_idis passed in.I think the purpose of a NULL '
mtd_id' was to support simple systems where there's a single driver and a single chip.
The driver could be dumb, not specifying its 'mtd_info->name' (thus, a NULLmtd_idis passed).
Quoted 8 lines
In this case, since the system is simply configured (one driver, one chip), '
parse_cmdline_partitions' simply disregards the "mtd-id" name specified in the cmdline string, allowing the user to present some arbitrary string there.I quite remember seeing this pattern somewhere in the past, I don't know if it's still used, though.
thanks for the detail explanation.
Quoted 5 lines
Obviously if you have many drivers (and many chips) in a system, that won't work; the drivers must initialize '
mtd_info->name' and the user should present a cmdline that has explicit 'mtd-id's.So question is, would we like to prohibit NULL mtd-id?
I prefer to prohibit the NULL mtd-id. The code looks strange enough.
Quoted 4 lines
If so, we must make sure all drivers are properly assigning their '
mtd_info->name', and all users correctly specifying 'mtd-id' in their "mtdparts" cmdline strings.
yes. I agree that all the driver should set the mtd_info->name properly.
Best Regards
Huang Shijie
Quoted 3 lines
Regards,
Shmulik
Quoted 18 lines · Shmulik Ladkani
Hi Huang, Artem,
Quoted 15 lines · On Sat, 25 Aug 2012 16:06:50 -0400 Huang Shijie <[email protected]> wrote:
drivers/mtd/cmdlinepart.c +3
-0@@ -322,13 +322,16 @@ static int parse_cmdline_partitions(struct mtd_info *master,struct cmdline_mtd_partition *part;const char *mtd_id = master->name; +if (!mtd_id) +return 0 ; +/* parse command line */ if (!cmdline_parsed)mtdpart_setup_real (cmdline);
Quoted 10 lines
drivers/mtd/cmdlinepart.c static int parse_cmdline_partitions(struct mtd_info *master, · line 337
Quoted 7 lines
drivers/mtd/cmdlinepart.c +1
-1for (part = partitions; part; part = part->next) { -if ((!mtd_id) || (!strcmp (part->mtd_id, mtd_id))) +if (!strcmp (part->mtd_id, mtd_id)) {for (i =0 , offset =0 ; i < part->num_parts; i++) {This changes the behavior of cmdling parsing, which might affect users expecting the old behavior.
Yes, you are right, we should not change the mtd_id hack unless we have checked all the users.
Signature · 2 lines
Best Regards, Artem Bityutskiy