1 From 2a295753a10823a47542c779a25bbb1f52c71281 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Fri, 3 Aug 2012 10:27:13 +0200
4 Subject: [PATCH 19/25] owrt mtd split
7 .../mips/include/asm/mach-lantiq/xway/lantiq_soc.h | 1 +
8 arch/mips/lantiq/setup.c | 7 +
9 drivers/mtd/Kconfig | 4 +
10 drivers/mtd/mtdpart.c | 173 +++++++++++++++++++-
11 4 files changed, 184 insertions(+), 1 deletions(-)
13 Index: linux-3.8.13/drivers/mtd/Kconfig
14 ===================================================================
15 --- linux-3.8.13.orig/drivers/mtd/Kconfig 2013-08-04 19:55:18.499988719 +0200
16 +++ linux-3.8.13/drivers/mtd/Kconfig 2013-08-04 19:55:22.599988819 +0200
18 bool "Automatically split 'rootfs' partition for squashfs"
21 +config MTD_UIMAGE_SPLIT
22 + bool "Automatically split 'linux' partition into 'kernel' and 'rootfs'"
25 config MTD_REDBOOT_PARTS
26 tristate "RedBoot partition table parsing"
28 Index: linux-3.8.13/drivers/mtd/mtdpart.c
29 ===================================================================
30 --- linux-3.8.13.orig/drivers/mtd/mtdpart.c 2013-08-04 19:55:18.791988726 +0200
31 +++ linux-3.8.13/drivers/mtd/mtdpart.c 2013-08-06 13:39:13.212319144 +0200
34 #endif /* CONFIG_MTD_ROOTFS_SPLIT */
36 +#ifdef CONFIG_MTD_UIMAGE_SPLIT
37 +static unsigned long find_uimage_size(struct mtd_info *mtd,
38 + unsigned long offset)
40 +#define UBOOT_MAGIC 0x56190527
41 + unsigned long magic = 0;
46 + ret = mtd_read(mtd, offset, 4, &len, (void *)&magic);
47 + if (ret || len != sizeof(magic))
50 + if (le32_to_cpu(magic) != UBOOT_MAGIC)
53 + ret = mtd_read(mtd, offset + 12, 4, &len, (void *)&temp);
54 + if (ret || len != sizeof(temp))
60 +static unsigned long find_tplink_size(struct mtd_info *mtd,
61 + unsigned long offset)
63 +#define TPLINK_MAGIC 0x00000002
64 + unsigned long magic = 0;
69 + ret = mtd_read(mtd, offset, 4, &len, (void *)&magic);
70 + if (ret || len != sizeof(magic))
73 + if (le32_to_cpu(magic) != TPLINK_MAGIC)
76 + ret = mtd_read(mtd, offset + 0x78, 4, &len, (void *)&temp);
77 + if (ret || len != sizeof(temp))
80 + return temp + 0x200;
83 +static unsigned long find_eva_size(struct mtd_info *mtd,
84 + unsigned long offset)
86 +#define EVA_MAGIC 0xfeed1281
87 + unsigned long magic = 0;
92 + ret = mtd_read(mtd, offset, 4, &len, (void *)&magic);
93 + if (ret || len != sizeof(magic))
96 + if (le32_to_cpu(magic) != EVA_MAGIC)
99 + ret = mtd_read(mtd, offset + 4, 4, &len, (void *)&temp);
100 + if (ret || len != sizeof(temp))
103 + /* add eva header size */
104 + temp = le32_to_cpu(temp) + 0x18;
111 +static int detect_squashfs_partition(struct mtd_info *mtd, unsigned long offset)
113 + unsigned long temp;
117 + ret = mtd_read(mtd, offset, 4, &len, (void *)&temp);
118 + if (ret || len != sizeof(temp))
122 + return le32_to_cpu(temp) == SQUASHFS_MAGIC;
125 +static int detect_eva_squashfs_partition(struct mtd_info *mtd, unsigned long offset)
127 + unsigned long temp;
131 + ret = mtd_read(mtd, offset, 4, &len, (void *)&temp);
132 + if (ret || len != sizeof(temp))
135 + return be32_to_cpu(temp) == SQUASHFS_MAGIC;
138 +static unsigned long find_brnimage_size(struct mtd_info *mtd,
139 + unsigned long offset)
141 + unsigned long buf[4];
142 + // Assume at most 2MB of kernel image
143 + unsigned long end = offset + (2 << 20);
144 + unsigned long ptr = offset + 0x400 - 12;
148 + while (ptr < end) {
149 + long size_min = ptr - 0x400 - 12 - offset;
150 + long size_max = ptr + 12 - offset;
151 + ret = mtd_read(mtd, ptr, 16, &len, (void *)buf);
152 + if (ret || len != 16)
155 + if (le32_to_cpu(buf[0]) < size_min ||
156 + le32_to_cpu(buf[0]) > size_max) {
161 + if (le32_to_cpu(buf[3]) == SQUASHFS_MAGIC)
162 + return ptr + 12 - offset;
170 +static int split_uimage(struct mtd_info *mtd,
171 + const struct mtd_partition *part)
173 + static struct mtd_partition split_partitions[] = {
185 + split_partitions[0].size = find_uimage_size(mtd, part->offset);
186 + if (!split_partitions[0].size)
187 + split_partitions[0].size = find_eva_size(mtd, part->offset);
188 + if (!split_partitions[0].size)
189 + split_partitions[0].size = find_brnimage_size(mtd, part->offset);
190 + if (!split_partitions[0].size)
191 + split_partitions[0].size = find_tplink_size(mtd, part->offset);
192 + if (!split_partitions[0].size) {
193 + printk(KERN_NOTICE "no uImage or brnImage or eva found in linux partition\n");
197 + if (detect_eva_squashfs_partition(mtd,
199 + + split_partitions[0].size)) {
200 + split_partitions[0].size += 0x100;
201 + pr_info("found eva dummy squashfs behind kernel\n");
202 + } else if (!detect_squashfs_partition(mtd,
204 + + split_partitions[0].size)) {
205 + split_partitions[0].size &= ~(mtd->erasesize - 1);
206 + split_partitions[0].size += mtd->erasesize;
208 + pr_info("found squashfs behind kernel\n");
211 + split_partitions[0].offset = part->offset;
212 + split_partitions[1].offset = part->offset + split_partitions[0].size;
213 + split_partitions[1].size = part->size - split_partitions[0].size;
215 + add_mtd_partitions(mtd, split_partitions, 2);
222 * This function, given a master MTD object and a partition table, creates
223 * and registers slave MTD objects which are bound to the master according to
225 struct mtd_part *slave;
226 uint64_t cur_offset = 0;
228 -#ifdef CONFIG_MTD_ROOTFS_SPLIT
229 +#if defined(CONFIG_MTD_ROOTFS_SPLIT) || defined(CONFIG_MTD_UIMAGE_SPLIT)
233 @@ -866,6 +1051,15 @@
235 add_mtd_device(&slave->mtd);
237 +#ifdef CONFIG_MTD_UIMAGE_SPLIT
238 + if (!strcmp(parts[i].name, "linux")) {
239 + ret = split_uimage(master, &parts[i]);
242 + printk(KERN_WARNING "Can't split linux partition\n");
246 if (!strcmp(parts[i].name, "rootfs")) {
247 #ifdef CONFIG_MTD_ROOTFS_ROOT_DEV