400-mtd-mtdsplit-support.patch 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. --- a/drivers/mtd/Kconfig
  2. +++ b/drivers/mtd/Kconfig
  3. @@ -12,6 +12,25 @@ menuconfig MTD
  4. if MTD
  5. +menu "OpenWrt specific MTD options"
  6. +
  7. +config MTD_ROOTFS_ROOT_DEV
  8. + bool "Automatically set 'rootfs' partition to be root filesystem"
  9. + default y
  10. +
  11. +config MTD_SPLIT_FIRMWARE
  12. + bool "Automatically split firmware partition for kernel+rootfs"
  13. + default y
  14. +
  15. +config MTD_SPLIT_FIRMWARE_NAME
  16. + string "Firmware partition name"
  17. + depends on MTD_SPLIT_FIRMWARE
  18. + default "firmware"
  19. +
  20. +source "drivers/mtd/mtdsplit/Kconfig"
  21. +
  22. +endmenu
  23. +
  24. config MTD_TESTS
  25. tristate "MTD tests support (DANGEROUS)"
  26. depends on m
  27. --- a/drivers/mtd/mtdpart.c
  28. +++ b/drivers/mtd/mtdpart.c
  29. @@ -15,10 +15,12 @@
  30. #include <linux/kmod.h>
  31. #include <linux/mtd/mtd.h>
  32. #include <linux/mtd/partitions.h>
  33. +#include <linux/magic.h>
  34. #include <linux/err.h>
  35. #include <linux/of.h>
  36. #include "mtdcore.h"
  37. +#include "mtdsplit/mtdsplit.h"
  38. /*
  39. * MTD methods which simply translate the effective address and pass through
  40. @@ -236,6 +238,146 @@ static int mtd_add_partition_attrs(struc
  41. return ret;
  42. }
  43. +static DEFINE_SPINLOCK(part_parser_lock);
  44. +static LIST_HEAD(part_parsers);
  45. +
  46. +static struct mtd_part_parser *mtd_part_parser_get(const char *name)
  47. +{
  48. + struct mtd_part_parser *p, *ret = NULL;
  49. +
  50. + spin_lock(&part_parser_lock);
  51. +
  52. + list_for_each_entry(p, &part_parsers, list)
  53. + if (!strcmp(p->name, name) && try_module_get(p->owner)) {
  54. + ret = p;
  55. + break;
  56. + }
  57. +
  58. + spin_unlock(&part_parser_lock);
  59. +
  60. + return ret;
  61. +}
  62. +
  63. +static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
  64. +{
  65. + module_put(p->owner);
  66. +}
  67. +
  68. +static struct mtd_part_parser *
  69. +get_partition_parser_by_type(enum mtd_parser_type type,
  70. + struct mtd_part_parser *start)
  71. +{
  72. + struct mtd_part_parser *p, *ret = NULL;
  73. +
  74. + spin_lock(&part_parser_lock);
  75. +
  76. + p = list_prepare_entry(start, &part_parsers, list);
  77. + if (start)
  78. + mtd_part_parser_put(start);
  79. +
  80. + list_for_each_entry_continue(p, &part_parsers, list) {
  81. + if (p->type == type && try_module_get(p->owner)) {
  82. + ret = p;
  83. + break;
  84. + }
  85. + }
  86. +
  87. + spin_unlock(&part_parser_lock);
  88. +
  89. + return ret;
  90. +}
  91. +
  92. +static int parse_mtd_partitions_by_type(struct mtd_info *master,
  93. + enum mtd_parser_type type,
  94. + const struct mtd_partition **pparts,
  95. + struct mtd_part_parser_data *data)
  96. +{
  97. + struct mtd_part_parser *prev = NULL;
  98. + int ret = 0;
  99. +
  100. + while (1) {
  101. + struct mtd_part_parser *parser;
  102. +
  103. + parser = get_partition_parser_by_type(type, prev);
  104. + if (!parser)
  105. + break;
  106. +
  107. + ret = (*parser->parse_fn)(master, pparts, data);
  108. +
  109. + if (ret > 0) {
  110. + mtd_part_parser_put(parser);
  111. + printk(KERN_NOTICE
  112. + "%d %s partitions found on MTD device %s\n",
  113. + ret, parser->name, master->name);
  114. + break;
  115. + }
  116. +
  117. + prev = parser;
  118. + }
  119. +
  120. + return ret;
  121. +}
  122. +
  123. +static int
  124. +run_parsers_by_type(struct mtd_info *child, enum mtd_parser_type type)
  125. +{
  126. + struct mtd_partition *parts;
  127. + int nr_parts;
  128. + int i;
  129. +
  130. + nr_parts = parse_mtd_partitions_by_type(child, type, (const struct mtd_partition **)&parts,
  131. + NULL);
  132. + if (nr_parts <= 0)
  133. + return nr_parts;
  134. +
  135. + if (WARN_ON(!parts))
  136. + return 0;
  137. +
  138. + for (i = 0; i < nr_parts; i++) {
  139. + /* adjust partition offsets */
  140. + parts[i].offset += child->part.offset;
  141. +
  142. + mtd_add_partition(child->parent,
  143. + parts[i].name,
  144. + parts[i].offset,
  145. + parts[i].size);
  146. + }
  147. +
  148. + kfree(parts);
  149. +
  150. + return nr_parts;
  151. +}
  152. +
  153. +#ifdef CONFIG_MTD_SPLIT_FIRMWARE_NAME
  154. +#define SPLIT_FIRMWARE_NAME CONFIG_MTD_SPLIT_FIRMWARE_NAME
  155. +#else
  156. +#define SPLIT_FIRMWARE_NAME "unused"
  157. +#endif
  158. +
  159. +static void split_firmware(struct mtd_info *master, struct mtd_info *part)
  160. +{
  161. + run_parsers_by_type(part, MTD_PARSER_TYPE_FIRMWARE);
  162. +}
  163. +
  164. +static void mtd_partition_split(struct mtd_info *master, struct mtd_info *part)
  165. +{
  166. + static int rootfs_found = 0;
  167. +
  168. + if (rootfs_found)
  169. + return;
  170. +
  171. + if (!strcmp(part->name, "rootfs")) {
  172. + run_parsers_by_type(part, MTD_PARSER_TYPE_ROOTFS);
  173. +
  174. + rootfs_found = 1;
  175. + }
  176. +
  177. + if (IS_ENABLED(CONFIG_MTD_SPLIT_FIRMWARE) &&
  178. + !strcmp(part->name, SPLIT_FIRMWARE_NAME) &&
  179. + !of_find_property(mtd_get_of_node(part), "compatible", NULL))
  180. + split_firmware(master, part);
  181. +}
  182. +
  183. int mtd_add_partition(struct mtd_info *parent, const char *name,
  184. long long offset, long long length)
  185. {
  186. @@ -274,6 +416,7 @@ int mtd_add_partition(struct mtd_info *p
  187. if (ret)
  188. goto err_remove_part;
  189. + mtd_partition_split(parent, child);
  190. mtd_add_partition_attrs(child);
  191. return 0;
  192. @@ -422,6 +565,7 @@ int add_mtd_partitions(struct mtd_info *
  193. goto err_del_partitions;
  194. }
  195. + mtd_partition_split(master, child);
  196. mtd_add_partition_attrs(child);
  197. /* Look for subpartitions */
  198. @@ -438,31 +582,6 @@ err_del_partitions:
  199. return ret;
  200. }
  201. -static DEFINE_SPINLOCK(part_parser_lock);
  202. -static LIST_HEAD(part_parsers);
  203. -
  204. -static struct mtd_part_parser *mtd_part_parser_get(const char *name)
  205. -{
  206. - struct mtd_part_parser *p, *ret = NULL;
  207. -
  208. - spin_lock(&part_parser_lock);
  209. -
  210. - list_for_each_entry(p, &part_parsers, list)
  211. - if (!strcmp(p->name, name) && try_module_get(p->owner)) {
  212. - ret = p;
  213. - break;
  214. - }
  215. -
  216. - spin_unlock(&part_parser_lock);
  217. -
  218. - return ret;
  219. -}
  220. -
  221. -static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
  222. -{
  223. - module_put(p->owner);
  224. -}
  225. -
  226. /*
  227. * Many partition parsers just expected the core to kfree() all their data in
  228. * one chunk. Do that by default.
  229. --- a/include/linux/mtd/partitions.h
  230. +++ b/include/linux/mtd/partitions.h
  231. @@ -75,6 +75,12 @@ struct mtd_part_parser_data {
  232. * Functions dealing with the various ways of partitioning the space
  233. */
  234. +enum mtd_parser_type {
  235. + MTD_PARSER_TYPE_DEVICE = 0,
  236. + MTD_PARSER_TYPE_ROOTFS,
  237. + MTD_PARSER_TYPE_FIRMWARE,
  238. +};
  239. +
  240. struct mtd_part_parser {
  241. struct list_head list;
  242. struct module *owner;
  243. @@ -83,6 +89,7 @@ struct mtd_part_parser {
  244. int (*parse_fn)(struct mtd_info *, const struct mtd_partition **,
  245. struct mtd_part_parser_data *);
  246. void (*cleanup)(const struct mtd_partition *pparts, int nr_parts);
  247. + enum mtd_parser_type type;
  248. };
  249. /* Container for passing around a set of parsed partitions */
  250. --- a/drivers/mtd/Makefile
  251. +++ b/drivers/mtd/Makefile
  252. @@ -9,6 +9,8 @@ mtd-y := mtdcore.o mtdsuper.o mtdconc
  253. obj-y += parsers/
  254. +obj-$(CONFIG_MTD_SPLIT) += mtdsplit/
  255. +
  256. # 'Users' - code which presents functionality to userspace.
  257. obj-$(CONFIG_MTD_BLKDEVS) += mtd_blkdevs.o
  258. obj-$(CONFIG_MTD_BLOCK) += mtdblock.o
  259. --- a/include/linux/mtd/mtd.h
  260. +++ b/include/linux/mtd/mtd.h
  261. @@ -608,6 +608,24 @@ static inline void mtd_align_erase_req(s
  262. req->len += mtd->erasesize - mod;
  263. }
  264. +static inline uint64_t mtd_roundup_to_eb(uint64_t sz, struct mtd_info *mtd)
  265. +{
  266. + if (mtd_mod_by_eb(sz, mtd) == 0)
  267. + return sz;
  268. +
  269. + /* Round up to next erase block */
  270. + return (mtd_div_by_eb(sz, mtd) + 1) * mtd->erasesize;
  271. +}
  272. +
  273. +static inline uint64_t mtd_rounddown_to_eb(uint64_t sz, struct mtd_info *mtd)
  274. +{
  275. + if (mtd_mod_by_eb(sz, mtd) == 0)
  276. + return sz;
  277. +
  278. + /* Round down to the start of the current erase block */
  279. + return (mtd_div_by_eb(sz, mtd)) * mtd->erasesize;
  280. +}
  281. +
  282. static inline uint32_t mtd_div_by_ws(uint64_t sz, struct mtd_info *mtd)
  283. {
  284. if (mtd->writesize_shift)
  285. @@ -680,6 +698,13 @@ extern void __put_mtd_device(struct mtd_
  286. extern struct mtd_info *get_mtd_device_nm(const char *name);
  287. extern void put_mtd_device(struct mtd_info *mtd);
  288. +static inline uint64_t mtdpart_get_offset(const struct mtd_info *mtd)
  289. +{
  290. + if (!mtd_is_partition(mtd))
  291. + return 0;
  292. +
  293. + return mtd->part.offset;
  294. +}
  295. struct mtd_notifier {
  296. void (*add)(struct mtd_info *mtd);