kernel: allow rootfs splitters to work without CONFIG_MTD_ROOTFS_SPLIT
[oweals/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,117 @@ 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 +       split_size = part->mtd.size - (split_offset - part->offset);
172 +       printk(KERN_INFO "mtd: partition \"%s\" created automatically, ofs=0x%x, len=0x%x\n",
173 +               ROOTFS_SPLIT_NAME, split_offset, split_size);
174 +
175 +       __mtd_add_partition(master, ROOTFS_SPLIT_NAME, split_offset,
176 +                           split_size, false);
177 +}
178 +
179 +#define UBOOT_MAGIC    0x27051956
180 +
181 +static void split_uimage(struct mtd_info *master, struct mtd_part *part)
182 +{
183 +       struct {
184 +               __be32 magic;
185 +               __be32 pad[2];
186 +               __be32 size;
187 +       } hdr;
188 +       size_t len;
189 +
190 +       if (mtd_read(master, part->offset, sizeof(hdr), &len, (void *) &hdr))
191 +               return;
192 +
193 +       if (len != sizeof(hdr) || hdr.magic != cpu_to_be32(UBOOT_MAGIC))
194 +               return;
195 +
196 +       len = be32_to_cpu(hdr.size) + 0x40;
197 +       len = mtd_pad_erasesize(master, part->offset, len);
198 +       if (len + master->erasesize > part->mtd.size)
199 +               return;
200 +
201 +       __mtd_add_partition(master, "rootfs", part->offset + len,
202 +                           part->mtd.size - len, false);
203 +}
204 +
205 +#ifdef CONFIG_MTD_SPLIT_FIRMWARE_NAME
206 +#define SPLIT_FIRMWARE_NAME    CONFIG_MTD_SPLIT_FIRMWARE_NAME
207 +#else
208 +#define SPLIT_FIRMWARE_NAME    "unused"
209 +#endif
210 +
211 +static void split_firmware(struct mtd_info *master, struct mtd_part *part)
212 +{
213 +       if (config_enabled(CONFIG_MTD_UIMAGE_SPLIT))
214 +               split_uimage(master, part);
215 +}
216 +
217 +void __weak arch_split_mtd_part(struct mtd_info *master, const char *name,
218 +                                int offset, int size)
219 +{
220 +}
221 +
222 +static void mtd_partition_split(struct mtd_info *master, struct mtd_part *part)
223 +{
224 +       static int rootfs_found = 0;
225 +
226 +       if (rootfs_found)
227 +               return;
228 +
229 +       if (!strcmp(part->mtd.name, "rootfs")) {
230 +               rootfs_found = 1;
231 +
232 +               if (config_enabled(CONFIG_MTD_ROOTFS_SPLIT))
233 +                       split_rootfs_data(master, part);
234 +       }
235 +
236 +       if (!strcmp(part->mtd.name, SPLIT_FIRMWARE_NAME) &&
237 +           config_enabled(CONFIG_MTD_SPLIT_FIRMWARE))
238 +               split_firmware(master, part);
239 +
240 +       arch_split_mtd_part(master, part->mtd.name, part->offset,
241 +                           part->mtd.size);
242 +}
243  /*
244   * This function, given a master MTD object and a partition table, creates
245   * and registers slave MTD objects which are bound to the master according to
246 @@ -643,6 +768,7 @@ int add_mtd_partitions(struct mtd_info *
247                 mutex_unlock(&mtd_partitions_mutex);
248  
249                 add_mtd_device(&slave->mtd);
250 +               mtd_partition_split(master, slave);
251  
252                 cur_offset = slave->offset + slave->mtd.size;
253         }
254 --- a/include/linux/mtd/partitions.h
255 +++ b/include/linux/mtd/partitions.h
256 @@ -84,5 +84,7 @@ int mtd_add_partition(struct mtd_info *m
257                       long long offset, long long length);
258  int mtd_del_partition(struct mtd_info *master, int partno);
259  uint64_t mtd_get_device_size(const struct mtd_info *mtd);
260 +extern void __weak arch_split_mtd_part(struct mtd_info *master,
261 +                                      const char *name, int offset, int size);
262  
263  #endif