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