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