generic/3.10: reduce number of ifdefs in the rootfs split code
[openwrt/openwrt.git] / target / linux / generic / patches-3.10 / 400-mtd-add-rootfs-split-support.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 +#define ROOTFS_SPLIT_NAME "rootfs_data"
131 +#define ROOTFS_REMOVED_NAME "<removed>"
132 +
133 +struct squashfs_super_block {
134 + __le32 s_magic;
135 + __le32 pad0[9];
136 + __le64 bytes_used;
137 +};
138 +
139 +
140 +static int split_squashfs(struct mtd_info *master, int offset, int *split_offset)
141 +{
142 + struct squashfs_super_block sb;
143 + int len, ret;
144 +
145 + ret = mtd_read(master, offset, sizeof(sb), &len, (void *) &sb);
146 + if (ret || (len != sizeof(sb))) {
147 + printk(KERN_ALERT "split_squashfs: error occured while reading "
148 + "from \"%s\"\n", master->name);
149 + return -EINVAL;
150 + }
151 +
152 + if (SQUASHFS_MAGIC != le32_to_cpu(sb.s_magic) ) {
153 + printk(KERN_ALERT "split_squashfs: no squashfs found in \"%s\"\n",
154 + master->name);
155 + *split_offset = 0;
156 + return 0;
157 + }
158 +
159 + if (le64_to_cpu((sb.bytes_used)) <= 0) {
160 + printk(KERN_ALERT "split_squashfs: squashfs is empty in \"%s\"\n",
161 + master->name);
162 + *split_offset = 0;
163 + return 0;
164 + }
165 +
166 + len = (u32) le64_to_cpu(sb.bytes_used);
167 + len = mtd_pad_erasesize(master, offset, len);
168 + *split_offset = offset + len;
169 +
170 + return 0;
171 +}
172 +
173 +static void split_rootfs_data(struct mtd_info *master, struct mtd_part *part)
174 +{
175 + unsigned int split_offset = 0;
176 + unsigned int split_size;
177 + int ret;
178 +
179 + ret = split_squashfs(master, part->offset, &split_offset);
180 + if (ret)
181 + return;
182 +
183 + if (split_offset <= 0)
184 + return;
185 +
186 + split_size = part->mtd.size - (split_offset - part->offset);
187 + printk(KERN_INFO "mtd: partition \"%s\" created automatically, ofs=0x%x, len=0x%x\n",
188 + ROOTFS_SPLIT_NAME, split_offset, split_size);
189 +
190 + __mtd_add_partition(master, ROOTFS_SPLIT_NAME, split_offset,
191 + split_size, false);
192 +}
193 +
194 +#define UBOOT_MAGIC 0x27051956
195 +
196 +#ifdef CONFIG_MTD_UIMAGE_SPLIT_NAME
197 +#define UIMAGE_SPLIT_NAME CONFIG_MTD_UIMAGE_SPLIT_NAME
198 +#else
199 +#define UIMAGE_SPLIT_NAME "unused"
200 +#endif
201 +
202 +static void split_uimage(struct mtd_info *master, struct mtd_part *part)
203 +{
204 + struct {
205 + __be32 magic;
206 + __be32 pad[2];
207 + __be32 size;
208 + } hdr;
209 + size_t len;
210 +
211 + if (strcmp(part->mtd.name, UIMAGE_SPLIT_NAME) != 0)
212 + return;
213 +
214 + if (mtd_read(master, part->offset, sizeof(hdr), &len, (void *) &hdr))
215 + return;
216 +
217 + if (len != sizeof(hdr) || hdr.magic != cpu_to_be32(UBOOT_MAGIC))
218 + return;
219 +
220 + len = be32_to_cpu(hdr.size) + 0x40;
221 + len = mtd_pad_erasesize(master, part->offset, len);
222 + if (len + master->erasesize > part->mtd.size)
223 + return;
224 +
225 + __mtd_add_partition(master, "rootfs", part->offset + len,
226 + part->mtd.size - len, false);
227 +}
228 +
229 +void __weak arch_split_mtd_part(struct mtd_info *master, const char *name,
230 + int offset, int size)
231 +{
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_ROOT_DEV) &&
246 + ROOT_DEV == 0) {
247 + printk(KERN_NOTICE "mtd: partition \"rootfs\" "
248 + "set to be root filesystem\n");
249 + ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, part->mtd.index);
250 + }
251 +
252 + if (config_enabled(CONFIG_MTD_ROOTFS_SPLIT))
253 + split_rootfs_data(master, part);
254 + }
255 +
256 + if (config_enabled(CONFIG_MTD_UIMAGE_SPLIT))
257 + split_uimage(master, part);
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