kernel/3.10: move OpenWrt MTD options into a submenu
[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 @@ -12,6 +12,27 @@ 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_UIMAGE_SPLIT
18 +       bool "Automatically split off rootfs from a kernel partition containing a uImage"
19 +       default y
20 +
21 +config MTD_UIMAGE_SPLIT_NAME
22 +       string "uImage partition name"
23 +       depends on MTD_UIMAGE_SPLIT
24 +       default "firmware"
25 +
26 +endmenu
27 +
28  config MTD_TESTS
29         tristate "MTD tests support (DANGEROUS)"
30         depends on m
31 --- a/drivers/mtd/mtdpart.c
32 +++ b/drivers/mtd/mtdpart.c
33 @@ -29,6 +29,8 @@
34  #include <linux/kmod.h>
35  #include <linux/mtd/mtd.h>
36  #include <linux/mtd/partitions.h>
37 +#include <linux/root_dev.h>
38 +#include <linux/magic.h>
39  #include <linux/err.h>
40  
41  #include "mtdcore.h"
42 @@ -45,13 +47,14 @@ struct mtd_part {
43         struct list_head list;
44  };
45  
46 +static void mtd_partition_split(struct mtd_info *master, struct mtd_part *part);
47 +
48  /*
49   * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
50   * the pointer to that structure with this macro.
51   */
52  #define PART(x)  ((struct mtd_part *)(x))
53  
54 -
55  /*
56   * MTD methods which simply translate the effective address and pass through
57   * to the _real_ device.
58 @@ -533,8 +536,10 @@ out_register:
59         return slave;
60  }
61  
62 -int mtd_add_partition(struct mtd_info *master, char *name,
63 -                     long long offset, long long length)
64 +
65 +static int
66 +__mtd_add_partition(struct mtd_info *master, char *name,
67 +                   long long offset, long long length, bool dup_check)
68  {
69         struct mtd_partition part;
70         struct mtd_part *p, *new;
71 @@ -566,21 +571,24 @@ int mtd_add_partition(struct mtd_info *m
72         end = offset + length;
73  
74         mutex_lock(&mtd_partitions_mutex);
75 -       list_for_each_entry(p, &mtd_partitions, list)
76 -               if (p->master == master) {
77 -                       if ((start >= p->offset) &&
78 -                           (start < (p->offset + p->mtd.size)))
79 -                               goto err_inv;
80 -
81 -                       if ((end >= p->offset) &&
82 -                           (end < (p->offset + p->mtd.size)))
83 -                               goto err_inv;
84 -               }
85 +       if (dup_check) {
86 +               list_for_each_entry(p, &mtd_partitions, list)
87 +                       if (p->master == master) {
88 +                               if ((start >= p->offset) &&
89 +                                   (start < (p->offset + p->mtd.size)))
90 +                                       goto err_inv;
91 +
92 +                               if ((end >= p->offset) &&
93 +                                   (end < (p->offset + p->mtd.size)))
94 +                                       goto err_inv;
95 +                       }
96 +       }
97  
98         list_add(&new->list, &mtd_partitions);
99         mutex_unlock(&mtd_partitions_mutex);
100  
101         add_mtd_device(&new->mtd);
102 +       mtd_partition_split(master, new);
103  
104         return ret;
105  err_inv:
106 @@ -590,6 +598,12 @@ err_inv:
107  }
108  EXPORT_SYMBOL_GPL(mtd_add_partition);
109  
110 +int mtd_add_partition(struct mtd_info *master, char *name,
111 +                     long long offset, long long length)
112 +{
113 +       return __mtd_add_partition(master, name, offset, length, true);
114 +}
115 +
116  int mtd_del_partition(struct mtd_info *master, int partno)
117  {
118         struct mtd_part *slave, *next;
119 @@ -613,6 +627,148 @@ int mtd_del_partition(struct mtd_info *m
120  }
121  EXPORT_SYMBOL_GPL(mtd_del_partition);
122  
123 +static inline unsigned long
124 +mtd_pad_erasesize(struct mtd_info *mtd, int offset, int len)
125 +{
126 +       unsigned long mask = mtd->erasesize - 1;
127 +
128 +       len += offset & mask;
129 +       len = (len + mask) & ~mask;
130 +       len -= offset & mask;
131 +       return len;
132 +}
133 +
134 +#define ROOTFS_SPLIT_NAME "rootfs_data"
135 +
136 +struct squashfs_super_block {
137 +       __le32 s_magic;
138 +       __le32 pad0[9];
139 +       __le64 bytes_used;
140 +};
141 +
142 +
143 +static int split_squashfs(struct mtd_info *master, int offset, int *split_offset)
144 +{
145 +       struct squashfs_super_block sb;
146 +       int len, ret;
147 +
148 +       ret = mtd_read(master, offset, sizeof(sb), &len, (void *) &sb);
149 +       if (ret || (len != sizeof(sb))) {
150 +               printk(KERN_ALERT "split_squashfs: error occured while reading "
151 +                       "from \"%s\"\n", master->name);
152 +               return -EINVAL;
153 +       }
154 +
155 +       if (SQUASHFS_MAGIC != le32_to_cpu(sb.s_magic) ) {
156 +               printk(KERN_ALERT "split_squashfs: no squashfs found in \"%s\"\n",
157 +                       master->name);
158 +               *split_offset = 0;
159 +               return 0;
160 +       }
161 +
162 +       if (le64_to_cpu((sb.bytes_used)) <= 0) {
163 +               printk(KERN_ALERT "split_squashfs: squashfs is empty in \"%s\"\n",
164 +                       master->name);
165 +               *split_offset = 0;
166 +               return 0;
167 +       }
168 +
169 +       len = (u32) le64_to_cpu(sb.bytes_used);
170 +       len = mtd_pad_erasesize(master, offset, len);
171 +       *split_offset = offset + len;
172 +
173 +       return 0;
174 +}
175 +
176 +static void split_rootfs_data(struct mtd_info *master, struct mtd_part *part)
177 +{
178 +       unsigned int split_offset = 0;
179 +       unsigned int split_size;
180 +       int ret;
181 +
182 +       ret = split_squashfs(master, part->offset, &split_offset);
183 +       if (ret)
184 +               return;
185 +
186 +       if (split_offset <= 0)
187 +               return;
188 +
189 +       split_size = part->mtd.size - (split_offset - part->offset);
190 +       printk(KERN_INFO "mtd: partition \"%s\" created automatically, ofs=0x%x, len=0x%x\n",
191 +               ROOTFS_SPLIT_NAME, split_offset, split_size);
192 +
193 +       __mtd_add_partition(master, ROOTFS_SPLIT_NAME, split_offset,
194 +                           split_size, false);
195 +}
196 +
197 +#define UBOOT_MAGIC    0x27051956
198 +
199 +#ifdef CONFIG_MTD_UIMAGE_SPLIT_NAME
200 +#define UIMAGE_SPLIT_NAME      CONFIG_MTD_UIMAGE_SPLIT_NAME
201 +#else
202 +#define UIMAGE_SPLIT_NAME      "unused"
203 +#endif
204 +
205 +static void split_uimage(struct mtd_info *master, struct mtd_part *part)
206 +{
207 +       struct {
208 +               __be32 magic;
209 +               __be32 pad[2];
210 +               __be32 size;
211 +       } hdr;
212 +       size_t len;
213 +
214 +       if (strcmp(part->mtd.name, UIMAGE_SPLIT_NAME) != 0)
215 +               return;
216 +
217 +       if (mtd_read(master, part->offset, sizeof(hdr), &len, (void *) &hdr))
218 +               return;
219 +
220 +       if (len != sizeof(hdr) || hdr.magic != cpu_to_be32(UBOOT_MAGIC))
221 +               return;
222 +
223 +       len = be32_to_cpu(hdr.size) + 0x40;
224 +       len = mtd_pad_erasesize(master, part->offset, len);
225 +       if (len + master->erasesize > part->mtd.size)
226 +               return;
227 +
228 +       __mtd_add_partition(master, "rootfs", part->offset + len,
229 +                           part->mtd.size - len, false);
230 +}
231 +
232 +void __weak arch_split_mtd_part(struct mtd_info *master, const char *name,
233 +                                int offset, int size)
234 +{
235 +}
236 +
237 +
238 +static void mtd_partition_split(struct mtd_info *master, struct mtd_part *part)
239 +{
240 +       static int rootfs_found = 0;
241 +
242 +       if (rootfs_found)
243 +               return;
244 +
245 +       if (!strcmp(part->mtd.name, "rootfs")) {
246 +               rootfs_found = 1;
247 +
248 +               if (config_enabled(CONFIG_MTD_ROOTFS_ROOT_DEV) &&
249 +                   ROOT_DEV == 0) {
250 +                       printk(KERN_NOTICE "mtd: partition \"rootfs\" "
251 +                               "set to be root filesystem\n");
252 +                       ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, part->mtd.index);
253 +               }
254 +
255 +               if (config_enabled(CONFIG_MTD_ROOTFS_SPLIT))
256 +                       split_rootfs_data(master, part);
257 +       }
258 +
259 +       if (config_enabled(CONFIG_MTD_UIMAGE_SPLIT))
260 +               split_uimage(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 @@ -642,6 +798,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