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