067-v4.17-0001-mtd-partitions-add-of_match_table-parser-matching-fo.patch 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. From 5b644aa012f67fd211138a067b9f351f30bdcc60 Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
  3. Date: Wed, 14 Mar 2018 13:10:42 +0100
  4. Subject: [PATCH] mtd: partitions: add of_match_table parser matching for the
  5. "ofpart" type
  6. MIME-Version: 1.0
  7. Content-Type: text/plain; charset=UTF-8
  8. Content-Transfer-Encoding: 8bit
  9. In order to properly support compatibility strings as described in the
  10. bindings/mtd/partition.txt "ofpart" type should be treated as an
  11. indication for looking into OF. MTD should check "compatible" property
  12. and search for a matching parser rather than blindly trying the one
  13. supporting "fixed-partitions".
  14. It also means that existing "fixed-partitions" parser should get renamed
  15. to use a more meaningful name.
  16. This commit achievies that aim by introducing a new mtd_part_of_parse().
  17. It works by looking for a matching parser for every string in the
  18. "compatibility" property (starting with the most specific one).
  19. Please note that driver-specified parsers still take a precedence. It's
  20. assumed that driver providing a parser type has a good reason for that
  21. (e.g. having platform data with device-specific info). Also doing
  22. otherwise could break existing setups. The same applies to using default
  23. parsers (including "cmdlinepart") as some overwrite DT data with cmdline
  24. argument.
  25. Partition parsers can now provide an of_match_table to enable
  26. flash<-->parser matching via device tree as documented in the
  27. mtd/partition.txt.
  28. This support is currently limited to built-in parsers as it uses
  29. request_module() and friends. This should be sufficient for most cases
  30. though as compiling parsers as modules isn't a common choice.
  31. Signed-off-by: Brian Norris <computersforpeace@gmail.com>
  32. Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
  33. Tested-by: Peter Rosin <peda@axentia.se>
  34. Reviewed-by: Richard Weinberger <richard@nod.at>
  35. Signed-off-by: Boris Brezillon <boris.brezillon@bootlin.com>
  36. ---
  37. drivers/mtd/mtdpart.c | 116 +++++++++++++++++++++++++++++++++++++----
  38. include/linux/mtd/partitions.h | 1 +
  39. 2 files changed, 108 insertions(+), 9 deletions(-)
  40. --- a/drivers/mtd/mtdpart.c
  41. +++ b/drivers/mtd/mtdpart.c
  42. @@ -30,6 +30,7 @@
  43. #include <linux/mtd/mtd.h>
  44. #include <linux/mtd/partitions.h>
  45. #include <linux/err.h>
  46. +#include <linux/of.h>
  47. #include "mtdcore.h"
  48. @@ -886,6 +887,92 @@ static int mtd_part_do_parse(struct mtd_
  49. }
  50. /**
  51. + * mtd_part_get_compatible_parser - find MTD parser by a compatible string
  52. + *
  53. + * @compat: compatible string describing partitions in a device tree
  54. + *
  55. + * MTD parsers can specify supported partitions by providing a table of
  56. + * compatibility strings. This function finds a parser that advertises support
  57. + * for a passed value of "compatible".
  58. + */
  59. +static struct mtd_part_parser *mtd_part_get_compatible_parser(const char *compat)
  60. +{
  61. + struct mtd_part_parser *p, *ret = NULL;
  62. +
  63. + spin_lock(&part_parser_lock);
  64. +
  65. + list_for_each_entry(p, &part_parsers, list) {
  66. + const struct of_device_id *matches;
  67. +
  68. + matches = p->of_match_table;
  69. + if (!matches)
  70. + continue;
  71. +
  72. + for (; matches->compatible[0]; matches++) {
  73. + if (!strcmp(matches->compatible, compat) &&
  74. + try_module_get(p->owner)) {
  75. + ret = p;
  76. + break;
  77. + }
  78. + }
  79. +
  80. + if (ret)
  81. + break;
  82. + }
  83. +
  84. + spin_unlock(&part_parser_lock);
  85. +
  86. + return ret;
  87. +}
  88. +
  89. +static int mtd_part_of_parse(struct mtd_info *master,
  90. + struct mtd_partitions *pparts)
  91. +{
  92. + struct mtd_part_parser *parser;
  93. + struct device_node *np;
  94. + struct property *prop;
  95. + const char *compat;
  96. + const char *fixed = "ofpart";
  97. + int ret, err = 0;
  98. +
  99. + np = of_get_child_by_name(mtd_get_of_node(master), "partitions");
  100. + of_property_for_each_string(np, "compatible", prop, compat) {
  101. + parser = mtd_part_get_compatible_parser(compat);
  102. + if (!parser)
  103. + continue;
  104. + ret = mtd_part_do_parse(parser, master, pparts, NULL);
  105. + if (ret > 0) {
  106. + of_node_put(np);
  107. + return ret;
  108. + }
  109. + mtd_part_parser_put(parser);
  110. + if (ret < 0 && !err)
  111. + err = ret;
  112. + }
  113. + of_node_put(np);
  114. +
  115. + /*
  116. + * For backward compatibility we have to try the "ofpart"
  117. + * parser. It supports old DT format with partitions specified as a
  118. + * direct subnodes of a flash device DT node without any compatibility
  119. + * specified we could match.
  120. + */
  121. + parser = mtd_part_parser_get(fixed);
  122. + if (!parser && !request_module("%s", fixed))
  123. + parser = mtd_part_parser_get(fixed);
  124. + if (parser) {
  125. + ret = mtd_part_do_parse(parser, master, pparts, NULL);
  126. + if (ret > 0)
  127. + return ret;
  128. + mtd_part_parser_put(parser);
  129. + if (ret < 0 && !err)
  130. + err = ret;
  131. + }
  132. +
  133. + return err;
  134. +}
  135. +
  136. +/**
  137. * parse_mtd_partitions - parse MTD partitions
  138. * @master: the master partition (describes whole MTD device)
  139. * @types: names of partition parsers to try or %NULL
  140. @@ -917,19 +1004,30 @@ int parse_mtd_partitions(struct mtd_info
  141. types = default_mtd_part_types;
  142. for ( ; *types; types++) {
  143. - pr_debug("%s: parsing partitions %s\n", master->name, *types);
  144. - parser = mtd_part_parser_get(*types);
  145. - if (!parser && !request_module("%s", *types))
  146. + /*
  147. + * ofpart is a special type that means OF partitioning info
  148. + * should be used. It requires a bit different logic so it is
  149. + * handled in a separated function.
  150. + */
  151. + if (!strcmp(*types, "ofpart")) {
  152. + ret = mtd_part_of_parse(master, pparts);
  153. + } else {
  154. + pr_debug("%s: parsing partitions %s\n", master->name,
  155. + *types);
  156. parser = mtd_part_parser_get(*types);
  157. - pr_debug("%s: got parser %s\n", master->name,
  158. - parser ? parser->name : NULL);
  159. - if (!parser)
  160. - continue;
  161. - ret = mtd_part_do_parse(parser, master, pparts, data);
  162. + if (!parser && !request_module("%s", *types))
  163. + parser = mtd_part_parser_get(*types);
  164. + pr_debug("%s: got parser %s\n", master->name,
  165. + parser ? parser->name : NULL);
  166. + if (!parser)
  167. + continue;
  168. + ret = mtd_part_do_parse(parser, master, pparts, data);
  169. + if (ret <= 0)
  170. + mtd_part_parser_put(parser);
  171. + }
  172. /* Found partitions! */
  173. if (ret > 0)
  174. return 0;
  175. - mtd_part_parser_put(parser);
  176. /*
  177. * Stash the first error we see; only report it if no parser
  178. * succeeds
  179. --- a/include/linux/mtd/partitions.h
  180. +++ b/include/linux/mtd/partitions.h
  181. @@ -77,6 +77,7 @@ struct mtd_part_parser {
  182. struct list_head list;
  183. struct module *owner;
  184. const char *name;
  185. + const struct of_device_id *of_match_table;
  186. int (*parse_fn)(struct mtd_info *, const struct mtd_partition **,
  187. struct mtd_part_parser_data *);
  188. void (*cleanup)(const struct mtd_partition *pparts, int nr_parts);