400-mtd-add-rootfs-split-support.patch 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. --- a/drivers/mtd/Kconfig
  2. +++ b/drivers/mtd/Kconfig
  3. @@ -12,6 +12,32 @@ 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_ROOTFS_SPLIT
  12. + bool "Automatically split 'rootfs' partition for squashfs"
  13. + default y
  14. +
  15. +config MTD_SPLIT_FIRMWARE
  16. + bool "Automatically split firmware partition for kernel+rootfs"
  17. + default y
  18. +
  19. +config MTD_SPLIT_FIRMWARE_NAME
  20. + string "Firmware partition name"
  21. + depends on MTD_SPLIT_FIRMWARE
  22. + default "firmware"
  23. +
  24. +config MTD_UIMAGE_SPLIT
  25. + bool "Enable split support for firmware partitions containing a uImage"
  26. + depends on MTD_SPLIT_FIRMWARE
  27. + default y
  28. +
  29. +endmenu
  30. +
  31. config MTD_TESTS
  32. tristate "MTD tests support (DANGEROUS)"
  33. depends on m
  34. --- a/drivers/mtd/mtdpart.c
  35. +++ b/drivers/mtd/mtdpart.c
  36. @@ -29,9 +29,11 @@
  37. #include <linux/kmod.h>
  38. #include <linux/mtd/mtd.h>
  39. #include <linux/mtd/partitions.h>
  40. +#include <linux/magic.h>
  41. #include <linux/err.h>
  42. #include "mtdcore.h"
  43. +#include "mtdsplit/mtdsplit.h"
  44. /* Our partition linked list */
  45. static LIST_HEAD(mtd_partitions);
  46. @@ -45,13 +47,14 @@ struct mtd_part {
  47. struct list_head list;
  48. };
  49. +static void mtd_partition_split(struct mtd_info *master, struct mtd_part *part);
  50. +
  51. /*
  52. * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
  53. * the pointer to that structure with this macro.
  54. */
  55. #define PART(x) ((struct mtd_part *)(x))
  56. -
  57. /*
  58. * MTD methods which simply translate the effective address and pass through
  59. * to the _real_ device.
  60. @@ -533,8 +536,10 @@ out_register:
  61. return slave;
  62. }
  63. -int mtd_add_partition(struct mtd_info *master, char *name,
  64. - long long offset, long long length)
  65. +
  66. +static int
  67. +__mtd_add_partition(struct mtd_info *master, char *name,
  68. + long long offset, long long length, bool dup_check)
  69. {
  70. struct mtd_partition part;
  71. struct mtd_part *p, *new;
  72. @@ -566,21 +571,24 @@ int mtd_add_partition(struct mtd_info *m
  73. end = offset + length;
  74. mutex_lock(&mtd_partitions_mutex);
  75. - list_for_each_entry(p, &mtd_partitions, list)
  76. - if (p->master == master) {
  77. - if ((start >= p->offset) &&
  78. - (start < (p->offset + p->mtd.size)))
  79. - goto err_inv;
  80. -
  81. - if ((end >= p->offset) &&
  82. - (end < (p->offset + p->mtd.size)))
  83. - goto err_inv;
  84. - }
  85. + if (dup_check) {
  86. + list_for_each_entry(p, &mtd_partitions, list)
  87. + if (p->master == master) {
  88. + if ((start >= p->offset) &&
  89. + (start < (p->offset + p->mtd.size)))
  90. + goto err_inv;
  91. +
  92. + if ((end >= p->offset) &&
  93. + (end < (p->offset + p->mtd.size)))
  94. + goto err_inv;
  95. + }
  96. + }
  97. list_add(&new->list, &mtd_partitions);
  98. mutex_unlock(&mtd_partitions_mutex);
  99. add_mtd_device(&new->mtd);
  100. + mtd_partition_split(master, new);
  101. return ret;
  102. err_inv:
  103. @@ -590,6 +598,12 @@ err_inv:
  104. }
  105. EXPORT_SYMBOL_GPL(mtd_add_partition);
  106. +int mtd_add_partition(struct mtd_info *master, char *name,
  107. + long long offset, long long length)
  108. +{
  109. + return __mtd_add_partition(master, name, offset, length, true);
  110. +}
  111. +
  112. int mtd_del_partition(struct mtd_info *master, int partno)
  113. {
  114. struct mtd_part *slave, *next;
  115. @@ -613,6 +627,144 @@ int mtd_del_partition(struct mtd_info *m
  116. }
  117. EXPORT_SYMBOL_GPL(mtd_del_partition);
  118. +static inline unsigned long
  119. +mtd_pad_erasesize(struct mtd_info *mtd, int offset, int len)
  120. +{
  121. + unsigned long mask = mtd->erasesize - 1;
  122. +
  123. + len += offset & mask;
  124. + len = (len + mask) & ~mask;
  125. + len -= offset & mask;
  126. + return len;
  127. +}
  128. +
  129. +#define ROOTFS_SPLIT_NAME "rootfs_data"
  130. +
  131. +struct squashfs_super_block {
  132. + __le32 s_magic;
  133. + __le32 pad0[9];
  134. + __le64 bytes_used;
  135. +};
  136. +
  137. +
  138. +static int split_squashfs(struct mtd_info *master, int offset, int *split_offset)
  139. +{
  140. + struct squashfs_super_block sb;
  141. + int len, ret;
  142. +
  143. + ret = mtd_read(master, offset, sizeof(sb), &len, (void *) &sb);
  144. + if (ret || (len != sizeof(sb))) {
  145. + printk(KERN_ALERT "split_squashfs: error occured while reading "
  146. + "from \"%s\"\n", master->name);
  147. + return -EINVAL;
  148. + }
  149. +
  150. + if (SQUASHFS_MAGIC != le32_to_cpu(sb.s_magic) ) {
  151. + printk(KERN_ALERT "split_squashfs: no squashfs found in \"%s\"\n",
  152. + master->name);
  153. + *split_offset = 0;
  154. + return 0;
  155. + }
  156. +
  157. + if (le64_to_cpu((sb.bytes_used)) <= 0) {
  158. + printk(KERN_ALERT "split_squashfs: squashfs is empty in \"%s\"\n",
  159. + master->name);
  160. + *split_offset = 0;
  161. + return 0;
  162. + }
  163. +
  164. + len = (u32) le64_to_cpu(sb.bytes_used);
  165. + len = mtd_pad_erasesize(master, offset, len);
  166. + *split_offset = offset + len;
  167. +
  168. + return 0;
  169. +}
  170. +
  171. +static void split_rootfs_data(struct mtd_info *master, struct mtd_part *part)
  172. +{
  173. + unsigned int split_offset = 0;
  174. + unsigned int split_size;
  175. + int ret;
  176. +
  177. + ret = split_squashfs(master, part->offset, &split_offset);
  178. + if (ret)
  179. + return;
  180. +
  181. + if (split_offset <= 0)
  182. + return;
  183. +
  184. + split_size = part->mtd.size - (split_offset - part->offset);
  185. + printk(KERN_INFO "mtd: partition \"%s\" created automatically, ofs=0x%x, len=0x%x\n",
  186. + ROOTFS_SPLIT_NAME, split_offset, split_size);
  187. +
  188. + __mtd_add_partition(master, ROOTFS_SPLIT_NAME, split_offset,
  189. + split_size, false);
  190. +}
  191. +
  192. +#define UBOOT_MAGIC 0x27051956
  193. +
  194. +static void split_uimage(struct mtd_info *master, struct mtd_part *part)
  195. +{
  196. + struct {
  197. + __be32 magic;
  198. + __be32 pad[2];
  199. + __be32 size;
  200. + } hdr;
  201. + size_t len;
  202. +
  203. + if (mtd_read(master, part->offset, sizeof(hdr), &len, (void *) &hdr))
  204. + return;
  205. +
  206. + if (len != sizeof(hdr) || hdr.magic != cpu_to_be32(UBOOT_MAGIC))
  207. + return;
  208. +
  209. + len = be32_to_cpu(hdr.size) + 0x40;
  210. + len = mtd_pad_erasesize(master, part->offset, len);
  211. + if (len + master->erasesize > part->mtd.size)
  212. + return;
  213. +
  214. + __mtd_add_partition(master, "rootfs", part->offset + len,
  215. + part->mtd.size - len, false);
  216. +}
  217. +
  218. +#ifdef CONFIG_MTD_SPLIT_FIRMWARE_NAME
  219. +#define SPLIT_FIRMWARE_NAME CONFIG_MTD_SPLIT_FIRMWARE_NAME
  220. +#else
  221. +#define SPLIT_FIRMWARE_NAME "unused"
  222. +#endif
  223. +
  224. +static void split_firmware(struct mtd_info *master, struct mtd_part *part)
  225. +{
  226. + if (config_enabled(CONFIG_MTD_UIMAGE_SPLIT))
  227. + split_uimage(master, part);
  228. +}
  229. +
  230. +void __weak arch_split_mtd_part(struct mtd_info *master, const char *name,
  231. + int offset, int size)
  232. +{
  233. +}
  234. +
  235. +static void mtd_partition_split(struct mtd_info *master, struct mtd_part *part)
  236. +{
  237. + static int rootfs_found = 0;
  238. +
  239. + if (rootfs_found)
  240. + return;
  241. +
  242. + if (!strcmp(part->mtd.name, "rootfs")) {
  243. + rootfs_found = 1;
  244. +
  245. + if (config_enabled(CONFIG_MTD_ROOTFS_SPLIT))
  246. + split_rootfs_data(master, part);
  247. + }
  248. +
  249. + if (!strcmp(part->mtd.name, SPLIT_FIRMWARE_NAME) &&
  250. + config_enabled(CONFIG_MTD_SPLIT_FIRMWARE))
  251. + split_firmware(master, part);
  252. +
  253. + arch_split_mtd_part(master, part->mtd.name, part->offset,
  254. + part->mtd.size);
  255. +}
  256. /*
  257. * This function, given a master MTD object and a partition table, creates
  258. * and registers slave MTD objects which are bound to the master according to
  259. @@ -642,6 +794,7 @@ int add_mtd_partitions(struct mtd_info *
  260. mutex_unlock(&mtd_partitions_mutex);
  261. add_mtd_device(&slave->mtd);
  262. + mtd_partition_split(master, slave);
  263. cur_offset = slave->offset + slave->mtd.size;
  264. }
  265. --- a/include/linux/mtd/partitions.h
  266. +++ b/include/linux/mtd/partitions.h
  267. @@ -84,5 +84,7 @@ int mtd_add_partition(struct mtd_info *m
  268. long long offset, long long length);
  269. int mtd_del_partition(struct mtd_info *master, int partno);
  270. uint64_t mtd_get_device_size(const struct mtd_info *mtd);
  271. +extern void __weak arch_split_mtd_part(struct mtd_info *master,
  272. + const char *name, int offset, int size);
  273. #endif