[lantiq] fixes zyxel p2601hnfx
[openwrt.git] / target / linux / lantiq / patches-3.3 / 201-owrt-mtd_split.patch
1 --- a/drivers/mtd/Kconfig
2 +++ b/drivers/mtd/Kconfig
3 @@ -31,6 +31,10 @@ config MTD_ROOTFS_SPLIT
4         bool "Automatically split 'rootfs' partition for squashfs"
5         default y
6  
7 +config MTD_UIMAGE_SPLIT
8 +       bool "Automatically split 'linux' partition into 'kernel' and 'rootfs'"
9 +       default y
10 +
11  config MTD_REDBOOT_PARTS
12         tristate "RedBoot partition table parsing"
13         ---help---
14 --- a/drivers/mtd/mtdpart.c
15 +++ b/drivers/mtd/mtdpart.c
16 @@ -867,6 +867,168 @@ static int refresh_rootfs_split(struct m
17  }
18  #endif /* CONFIG_MTD_ROOTFS_SPLIT */
19  
20 +#ifdef CONFIG_MTD_UIMAGE_SPLIT
21 +static unsigned long find_uimage_size(struct mtd_info *mtd,
22 +                                     unsigned long offset)
23 +{
24 +#define UBOOT_MAGIC    0x56190527
25 +       unsigned long magic = 0;
26 +       unsigned long temp;
27 +       size_t len;
28 +       int ret;
29 +
30 +       ret = mtd->read(mtd, offset, 4, &len, (void *)&magic);
31 +       if (ret || len != sizeof(magic))
32 +               return 0;
33 +
34 +       if (le32_to_cpu(magic) != UBOOT_MAGIC)
35 +               return 0;
36 +
37 +       ret = mtd->read(mtd, offset + 12, 4, &len, (void *)&temp);
38 +       if (ret || len != sizeof(temp))
39 +               return 0;
40 +
41 +       return temp + 0x40;
42 +}
43 +
44 +static unsigned long find_eva_size(struct mtd_info *mtd,
45 +                                     unsigned long offset)
46 +{
47 +#define EVA_MAGIC      0xfeed1281
48 +       unsigned long magic = 0;
49 +       unsigned long temp;
50 +       size_t len;
51 +       int ret;
52 +
53 +       ret = mtd->read(mtd, offset, 4, &len, (void *)&magic);
54 +       if (ret || len != sizeof(magic))
55 +               return 0;
56 +
57 +       if (le32_to_cpu(magic) != EVA_MAGIC)
58 +               return 0;
59 +
60 +       ret = mtd->read(mtd, offset + 4, 4, &len, (void *)&temp);
61 +       if (ret || len != sizeof(temp))
62 +               return 0;
63 +
64 +       /* add eva header size */
65 +       temp = le32_to_cpu(temp) + 0x18;
66 +
67 +       temp &= ~0xffff;
68 +       temp += 0x10000;
69 +       return temp;
70 +}
71 +
72 +static int detect_squashfs_partition(struct mtd_info *mtd, unsigned long offset)
73 +{
74 +       unsigned long temp;
75 +       size_t len;
76 +       int ret;
77 +
78 +       ret = mtd->read(mtd, offset, 4, &len, (void *)&temp);
79 +       if (ret || len != sizeof(temp))
80 +               return 0;
81 +
82 +
83 +       return le32_to_cpu(temp) == SQUASHFS_MAGIC;
84 +}
85 +
86 +static int detect_eva_squashfs_partition(struct mtd_info *mtd, unsigned long offset)
87 +{
88 +       unsigned long temp;
89 +       size_t len;
90 +       int ret;
91 +
92 +       ret = mtd->read(mtd, offset, 4, &len, (void *)&temp);
93 +       if (ret || len != sizeof(temp))
94 +               return 0;
95 +
96 +       return be32_to_cpu(temp) == SQUASHFS_MAGIC;
97 +}
98 +
99 +static unsigned long find_brnimage_size(struct mtd_info *mtd,
100 +                                       unsigned long offset)
101 +{
102 +       unsigned long buf[4];
103 +       // Assume at most 2MB of kernel image
104 +       unsigned long end = offset + (2 << 20);
105 +       unsigned long ptr = offset + 0x400 - 12;
106 +       size_t len;
107 +       int ret;
108 +
109 +       while (ptr < end) {
110 +               long size_min = ptr - 0x400 - 12 - offset;
111 +               long size_max = ptr + 12 - offset;
112 +               ret = mtd->read(mtd, ptr, 16, &len, (void *)buf);
113 +               if (ret || len != 16)
114 +                       return 0;
115 +
116 +               if (le32_to_cpu(buf[0]) < size_min ||
117 +                   le32_to_cpu(buf[0]) > size_max) {
118 +                       ptr += 0x400;
119 +                       continue;
120 +               }
121 +
122 +               if (le32_to_cpu(buf[3]) == SQUASHFS_MAGIC)
123 +                       return ptr + 12 - offset;
124 +
125 +               ptr += 0x400;
126 +       }
127 +
128 +       return 0;
129 +}
130 +
131 +static int split_uimage(struct mtd_info *mtd,
132 +                       const struct mtd_partition *part)
133 +{
134 +       static struct mtd_partition split_partitions[] = {
135 +               {
136 +                       .name = "kernel",
137 +                       .offset = 0x0,
138 +                       .size = 0x0,
139 +               }, {
140 +                       .name = "rootfs",
141 +                       .offset = 0x0,
142 +                       .size = 0x0,
143 +               },
144 +       };
145 +
146 +       split_partitions[0].size = find_uimage_size(mtd, part->offset);
147 +       if (!split_partitions[0].size) {
148 +               split_partitions[0].size = find_eva_size(mtd, part->offset);
149 +               if (!split_partitions[0].size) {
150 +                       split_partitions[0].size = find_brnimage_size(mtd, part->offset);
151 +                       if (!split_partitions[0].size) {
152 +                               printk(KERN_NOTICE "no uImage or brnImage or eva found in linux partition\n");
153 +                               return -1;
154 +                       }
155 +               }
156 +       }
157 +
158 +       if (detect_eva_squashfs_partition(mtd,
159 +                                      part->offset
160 +                                      + split_partitions[0].size)) {
161 +               split_partitions[0].size += 0x100;
162 +               pr_info("found eva dummy squashfs behind kernel\n");
163 +       } else if (!detect_squashfs_partition(mtd,
164 +                                      part->offset
165 +                                      + split_partitions[0].size)) {
166 +               split_partitions[0].size &= ~(mtd->erasesize - 1);
167 +               split_partitions[0].size += mtd->erasesize;
168 +       } else {
169 +               pr_info("found squashfs behind kernel\n");
170 +       }
171 +
172 +       split_partitions[0].offset = part->offset;
173 +       split_partitions[1].offset = part->offset + split_partitions[0].size;
174 +       split_partitions[1].size = part->size - split_partitions[0].size;
175 +
176 +       add_mtd_partitions(mtd, split_partitions, 2);
177 +
178 +       return 0;
179 +}
180 +#endif
181 +
182  /*
183   * This function, given a master MTD object and a partition table, creates
184   * and registers slave MTD objects which are bound to the master according to
185 @@ -883,7 +1045,7 @@ int add_mtd_partitions(struct mtd_info *
186         struct mtd_part *slave;
187         uint64_t cur_offset = 0;
188         int i;
189 -#ifdef CONFIG_MTD_ROOTFS_SPLIT
190 +#if defined(CONFIG_MTD_ROOTFS_SPLIT) || defined(CONFIG_MTD_UIMAGE_SPLIT)
191         int ret;
192  #endif
193  
194 @@ -900,6 +1062,15 @@ int add_mtd_partitions(struct mtd_info *
195  
196                 add_mtd_device(&slave->mtd);
197  
198 +#ifdef CONFIG_MTD_UIMAGE_SPLIT
199 +               if (!strcmp(parts[i].name, "linux")) {
200 +                       ret = split_uimage(master, &parts[i]);
201 +
202 +                       if (ret)
203 +                               printk(KERN_WARNING "Can't split linux partition\n");
204 +               }
205 +#endif
206 +
207                 if (!strcmp(parts[i].name, "rootfs")) {
208  #ifdef CONFIG_MTD_ROOTFS_ROOT_DEV
209                         if (ROOT_DEV == 0) {
210 --- a/arch/mips/include/asm/mach-lantiq/xway/lantiq_soc.h
211 +++ b/arch/mips/include/asm/mach-lantiq/xway/lantiq_soc.h
212 @@ -166,6 +166,7 @@ extern unsigned char ltq_boot_select(voi
213  
214  extern __iomem void *ltq_ebu_membase;
215  extern __iomem void *ltq_cgu_membase;
216 +extern unsigned long ltq_brn_boot;
217  
218  static inline int ltq_is_ase(void)
219  {
220 --- a/arch/mips/lantiq/setup.c
221 +++ b/arch/mips/lantiq/setup.c
222 @@ -18,6 +18,9 @@
223  #include "devices.h"
224  #include "prom.h"
225  
226 +/* set to 1 if the bootloader is BRN-BOOT instead of u-boot */
227 +unsigned long ltq_brn_boot = 0;
228 +
229  void __init plat_mem_setup(void)
230  {
231         /* assume 16M as default incase uboot fails to pass proper ramsize */
232 @@ -38,6 +41,10 @@ void __init plat_mem_setup(void)
233                         if (strict_strtoul(e, 0, &memsize))
234                                 pr_warn("bad memsize specified\n");
235                 }
236 +               if (!strncmp(e, "BRN-BOOT", 8)){
237 +                       pr_info("Found BRN-BOOT instead of u-boot\n");
238 +                       ltq_brn_boot = 1;
239 +               }
240                 envp++;
241         }
242         memsize *= 1024 * 1024;