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