kernel: start working on 3.18 support
[openwrt.git] / target / linux / generic / patches-3.18 / 400-mtd-add-rootfs-split-support.patch
1 --- a/drivers/mtd/Kconfig
2 +++ b/drivers/mtd/Kconfig
3 @@ -12,6 +12,28 @@ 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_SPLIT_FIRMWARE
14 +       bool "Automatically split firmware partition for kernel+rootfs"
15 +       default y
16 +
17 +config MTD_SPLIT_FIRMWARE_NAME
18 +       string "Firmware partition name"
19 +       depends on MTD_SPLIT_FIRMWARE
20 +       default "firmware"
21 +
22 +config MTD_UIMAGE_SPLIT
23 +       bool "Enable split support for firmware partitions containing a uImage"
24 +       depends on MTD_SPLIT_FIRMWARE
25 +       default y
26 +
27 +endmenu
28 +
29  config MTD_TESTS
30         tristate "MTD tests support (DANGEROUS)"
31         depends on m
32 --- a/drivers/mtd/mtdpart.c
33 +++ b/drivers/mtd/mtdpart.c
34 @@ -29,9 +29,11 @@
35  #include <linux/kmod.h>
36  #include <linux/mtd/mtd.h>
37  #include <linux/mtd/partitions.h>
38 +#include <linux/magic.h>
39  #include <linux/err.h>
40  
41  #include "mtdcore.h"
42 +#include "mtdsplit.h"
43  
44  /* Our partition linked list */
45  static LIST_HEAD(mtd_partitions);
46 @@ -45,13 +47,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 @@ -547,8 +550,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 @@ -580,21 +585,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 @@ -604,6 +612,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 @@ -627,6 +641,74 @@ 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 UBOOT_MAGIC    0x27051956
139 +
140 +static void split_uimage(struct mtd_info *master, struct mtd_part *part)
141 +{
142 +       struct {
143 +               __be32 magic;
144 +               __be32 pad[2];
145 +               __be32 size;
146 +       } hdr;
147 +       size_t len;
148 +
149 +       if (mtd_read(master, part->offset, sizeof(hdr), &len, (void *) &hdr))
150 +               return;
151 +
152 +       if (len != sizeof(hdr) || hdr.magic != cpu_to_be32(UBOOT_MAGIC))
153 +               return;
154 +
155 +       len = be32_to_cpu(hdr.size) + 0x40;
156 +       len = mtd_pad_erasesize(master, part->offset, len);
157 +       if (len + master->erasesize > part->mtd.size)
158 +               return;
159 +
160 +       __mtd_add_partition(master, "rootfs", part->offset + len,
161 +                           part->mtd.size - len, false);
162 +}
163 +
164 +#ifdef CONFIG_MTD_SPLIT_FIRMWARE_NAME
165 +#define SPLIT_FIRMWARE_NAME    CONFIG_MTD_SPLIT_FIRMWARE_NAME
166 +#else
167 +#define SPLIT_FIRMWARE_NAME    "unused"
168 +#endif
169 +
170 +static void split_firmware(struct mtd_info *master, struct mtd_part *part)
171 +{
172 +       if (config_enabled(CONFIG_MTD_UIMAGE_SPLIT))
173 +               split_uimage(master, part);
174 +}
175 +
176 +void __weak arch_split_mtd_part(struct mtd_info *master, const char *name,
177 +                                int offset, int size)
178 +{
179 +}
180 +
181 +static void mtd_partition_split(struct mtd_info *master, struct mtd_part *part)
182 +{
183 +       static int rootfs_found = 0;
184 +
185 +       if (rootfs_found)
186 +               return;
187 +
188 +       if (!strcmp(part->mtd.name, SPLIT_FIRMWARE_NAME) &&
189 +           config_enabled(CONFIG_MTD_SPLIT_FIRMWARE))
190 +               split_firmware(master, part);
191 +
192 +       arch_split_mtd_part(master, part->mtd.name, part->offset,
193 +                           part->mtd.size);
194 +}
195  /*
196   * This function, given a master MTD object and a partition table, creates
197   * and registers slave MTD objects which are bound to the master according to
198 @@ -656,6 +738,7 @@ int add_mtd_partitions(struct mtd_info *
199                 mutex_unlock(&mtd_partitions_mutex);
200  
201                 add_mtd_device(&slave->mtd);
202 +               mtd_partition_split(master, slave);
203  
204                 cur_offset = slave->offset + slave->mtd.size;
205         }
206 --- a/include/linux/mtd/partitions.h
207 +++ b/include/linux/mtd/partitions.h
208 @@ -84,5 +84,7 @@ int mtd_add_partition(struct mtd_info *m
209                       long long offset, long long length);
210  int mtd_del_partition(struct mtd_info *master, int partno);
211  uint64_t mtd_get_device_size(const struct mtd_info *mtd);
212 +extern void __weak arch_split_mtd_part(struct mtd_info *master,
213 +                                      const char *name, int offset, int size);
214  
215  #endif