generic/3.10: remove unused defines from the rootfs split patch
[15.05/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,13 +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 -
51  /*
52   * MTD methods which simply translate the effective address and pass through
53   * to the _real_ device.
54 @@ -533,8 +536,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 +571,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 +598,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 +627,148 @@ 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 +
132 +struct squashfs_super_block {
133 +       __le32 s_magic;
134 +       __le32 pad0[9];
135 +       __le64 bytes_used;
136 +};
137 +
138 +
139 +static int split_squashfs(struct mtd_info *master, int offset, int *split_offset)
140 +{
141 +       struct squashfs_super_block sb;
142 +       int len, ret;
143 +
144 +       ret = mtd_read(master, offset, sizeof(sb), &len, (void *) &sb);
145 +       if (ret || (len != sizeof(sb))) {
146 +               printk(KERN_ALERT "split_squashfs: error occured while reading "
147 +                       "from \"%s\"\n", master->name);
148 +               return -EINVAL;
149 +       }
150 +
151 +       if (SQUASHFS_MAGIC != le32_to_cpu(sb.s_magic) ) {
152 +               printk(KERN_ALERT "split_squashfs: no squashfs found in \"%s\"\n",
153 +                       master->name);
154 +               *split_offset = 0;
155 +               return 0;
156 +       }
157 +
158 +       if (le64_to_cpu((sb.bytes_used)) <= 0) {
159 +               printk(KERN_ALERT "split_squashfs: squashfs is empty in \"%s\"\n",
160 +                       master->name);
161 +               *split_offset = 0;
162 +               return 0;
163 +       }
164 +
165 +       len = (u32) le64_to_cpu(sb.bytes_used);
166 +       len = mtd_pad_erasesize(master, offset, len);
167 +       *split_offset = offset + len;
168 +
169 +       return 0;
170 +}
171 +
172 +static void split_rootfs_data(struct mtd_info *master, struct mtd_part *part)
173 +{
174 +       unsigned int split_offset = 0;
175 +       unsigned int split_size;
176 +       int ret;
177 +
178 +       ret = split_squashfs(master, part->offset, &split_offset);
179 +       if (ret)
180 +               return;
181 +
182 +       if (split_offset <= 0)
183 +               return;
184 +
185 +       split_size = part->mtd.size - (split_offset - part->offset);
186 +       printk(KERN_INFO "mtd: partition \"%s\" created automatically, ofs=0x%x, len=0x%x\n",
187 +               ROOTFS_SPLIT_NAME, split_offset, split_size);
188 +
189 +       __mtd_add_partition(master, ROOTFS_SPLIT_NAME, split_offset,
190 +                           split_size, false);
191 +}
192 +
193 +#define UBOOT_MAGIC    0x27051956
194 +
195 +#ifdef CONFIG_MTD_UIMAGE_SPLIT_NAME
196 +#define UIMAGE_SPLIT_NAME      CONFIG_MTD_UIMAGE_SPLIT_NAME
197 +#else
198 +#define UIMAGE_SPLIT_NAME      "unused"
199 +#endif
200 +
201 +static void split_uimage(struct mtd_info *master, struct mtd_part *part)
202 +{
203 +       struct {
204 +               __be32 magic;
205 +               __be32 pad[2];
206 +               __be32 size;
207 +       } hdr;
208 +       size_t len;
209 +
210 +       if (strcmp(part->mtd.name, UIMAGE_SPLIT_NAME) != 0)
211 +               return;
212 +
213 +       if (mtd_read(master, part->offset, sizeof(hdr), &len, (void *) &hdr))
214 +               return;
215 +
216 +       if (len != sizeof(hdr) || hdr.magic != cpu_to_be32(UBOOT_MAGIC))
217 +               return;
218 +
219 +       len = be32_to_cpu(hdr.size) + 0x40;
220 +       len = mtd_pad_erasesize(master, part->offset, len);
221 +       if (len + master->erasesize > part->mtd.size)
222 +               return;
223 +
224 +       __mtd_add_partition(master, "rootfs", part->offset + len,
225 +                           part->mtd.size - len, false);
226 +}
227 +
228 +void __weak arch_split_mtd_part(struct mtd_info *master, const char *name,
229 +                                int offset, int size)
230 +{
231 +}
232 +
233 +
234 +static void mtd_partition_split(struct mtd_info *master, struct mtd_part *part)
235 +{
236 +       static int rootfs_found = 0;
237 +
238 +       if (rootfs_found)
239 +               return;
240 +
241 +       if (!strcmp(part->mtd.name, "rootfs")) {
242 +               rootfs_found = 1;
243 +
244 +               if (config_enabled(CONFIG_MTD_ROOTFS_ROOT_DEV) &&
245 +                   ROOT_DEV == 0) {
246 +                       printk(KERN_NOTICE "mtd: partition \"rootfs\" "
247 +                               "set to be root filesystem\n");
248 +                       ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, part->mtd.index);
249 +               }
250 +
251 +               if (config_enabled(CONFIG_MTD_ROOTFS_SPLIT))
252 +                       split_rootfs_data(master, part);
253 +       }
254 +
255 +       if (config_enabled(CONFIG_MTD_UIMAGE_SPLIT))
256 +               split_uimage(master, part);
257 +
258 +       arch_split_mtd_part(master, part->mtd.name, part->offset,
259 +                           part->mtd.size);
260 +}
261  /*
262   * This function, given a master MTD object and a partition table, creates
263   * and registers slave MTD objects which are bound to the master according to
264 @@ -642,6 +798,7 @@ int add_mtd_partitions(struct mtd_info *
265                 mutex_unlock(&mtd_partitions_mutex);
266  
267                 add_mtd_device(&slave->mtd);
268 +               mtd_partition_split(master, slave);
269  
270                 cur_offset = slave->offset + slave->mtd.size;
271         }
272 --- a/include/linux/mtd/partitions.h
273 +++ b/include/linux/mtd/partitions.h
274 @@ -84,5 +84,7 @@ int mtd_add_partition(struct mtd_info *m
275                       long long offset, long long length);
276  int mtd_del_partition(struct mtd_info *master, int partno);
277  uint64_t mtd_get_device_size(const struct mtd_info *mtd);
278 +extern void __weak arch_split_mtd_part(struct mtd_info *master,
279 +                                      const char *name, int offset, int size);
280  
281  #endif