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