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