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

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