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 diff --git a/arch/mips/include/asm/mach-lantiq/xway/lantiq_soc.h b/arch/mips/include/asm/mach-lantiq/xway/lantiq_soc.h
14 index 1ec8f2a..1ff93cc 100644
15 diff --git a/drivers/mtd/Kconfig b/drivers/mtd/Kconfig
16 index 982a98b..e2f3f3e 100644
17 --- a/drivers/mtd/Kconfig
18 +++ b/drivers/mtd/Kconfig
19 @@ -31,6 +31,10 @@ config MTD_ROOTFS_SPLIT
20 bool "Automatically split 'rootfs' partition for squashfs"
23 +config MTD_UIMAGE_SPLIT
24 + bool "Automatically split 'linux' partition into 'kernel' and 'rootfs'"
27 config MTD_REDBOOT_PARTS
28 tristate "RedBoot partition table parsing"
30 diff --git a/drivers/mtd/mtdpart.c b/drivers/mtd/mtdpart.c
31 index 855b70b..93711e2 100644
32 --- a/drivers/mtd/mtdpart.c
33 +++ b/drivers/mtd/mtdpart.c
34 @@ -867,6 +867,168 @@ static int refresh_rootfs_split(struct mtd_info *mtd)
36 #endif /* CONFIG_MTD_ROOTFS_SPLIT */
38 +#ifdef CONFIG_MTD_UIMAGE_SPLIT
39 +static unsigned long find_uimage_size(struct mtd_info *mtd,
40 + unsigned long offset)
42 +#define UBOOT_MAGIC 0x56190527
43 + unsigned long magic = 0;
48 + ret = mtd_read(mtd, offset, 4, &len, (void *)&magic);
49 + if (ret || len != sizeof(magic))
52 + if (le32_to_cpu(magic) != UBOOT_MAGIC)
55 + ret = mtd_read(mtd, offset + 12, 4, &len, (void *)&temp);
56 + if (ret || len != sizeof(temp))
62 +static unsigned long find_eva_size(struct mtd_info *mtd,
63 + unsigned long offset)
65 +#define EVA_MAGIC 0xfeed1281
66 + unsigned long magic = 0;
71 + ret = mtd_read(mtd, offset, 4, &len, (void *)&magic);
72 + if (ret || len != sizeof(magic))
75 + if (le32_to_cpu(magic) != EVA_MAGIC)
78 + ret = mtd_read(mtd, offset + 4, 4, &len, (void *)&temp);
79 + if (ret || len != sizeof(temp))
82 + /* add eva header size */
83 + temp = le32_to_cpu(temp) + 0x18;
90 +static int detect_squashfs_partition(struct mtd_info *mtd, unsigned long offset)
96 + ret = mtd_read(mtd, offset, 4, &len, (void *)&temp);
97 + if (ret || len != sizeof(temp))
101 + return le32_to_cpu(temp) == SQUASHFS_MAGIC;
104 +static int detect_eva_squashfs_partition(struct mtd_info *mtd, unsigned long offset)
106 + unsigned long temp;
110 + ret = mtd_read(mtd, offset, 4, &len, (void *)&temp);
111 + if (ret || len != sizeof(temp))
114 + return be32_to_cpu(temp) == SQUASHFS_MAGIC;
117 +static unsigned long find_brnimage_size(struct mtd_info *mtd,
118 + unsigned long offset)
120 + unsigned long buf[4];
121 + // Assume at most 2MB of kernel image
122 + unsigned long end = offset + (2 << 20);
123 + unsigned long ptr = offset + 0x400 - 12;
127 + while (ptr < end) {
128 + long size_min = ptr - 0x400 - 12 - offset;
129 + long size_max = ptr + 12 - offset;
130 + ret = mtd_read(mtd, ptr, 16, &len, (void *)buf);
131 + if (ret || len != 16)
134 + if (le32_to_cpu(buf[0]) < size_min ||
135 + le32_to_cpu(buf[0]) > size_max) {
140 + if (le32_to_cpu(buf[3]) == SQUASHFS_MAGIC)
141 + return ptr + 12 - offset;
149 +static int split_uimage(struct mtd_info *mtd,
150 + const struct mtd_partition *part)
152 + static struct mtd_partition split_partitions[] = {
164 + split_partitions[0].size = find_uimage_size(mtd, part->offset);
165 + if (!split_partitions[0].size) {
166 + split_partitions[0].size = find_eva_size(mtd, part->offset);
167 + if (!split_partitions[0].size) {
168 + split_partitions[0].size = find_brnimage_size(mtd, part->offset);
169 + if (!split_partitions[0].size) {
170 + printk(KERN_NOTICE "no uImage or brnImage or eva found in linux partition\n");
176 + if (detect_eva_squashfs_partition(mtd,
178 + + split_partitions[0].size)) {
179 + split_partitions[0].size += 0x100;
180 + pr_info("found eva dummy squashfs behind kernel\n");
181 + } else if (!detect_squashfs_partition(mtd,
183 + + split_partitions[0].size)) {
184 + split_partitions[0].size &= ~(mtd->erasesize - 1);
185 + split_partitions[0].size += mtd->erasesize;
187 + pr_info("found squashfs behind kernel\n");
190 + split_partitions[0].offset = part->offset;
191 + split_partitions[1].offset = part->offset + split_partitions[0].size;
192 + split_partitions[1].size = part->size - split_partitions[0].size;
194 + add_mtd_partitions(mtd, split_partitions, 2);
201 * This function, given a master MTD object and a partition table, creates
202 * and registers slave MTD objects which are bound to the master according to
203 @@ -883,7 +1045,7 @@ int add_mtd_partitions(struct mtd_info *master,
204 struct mtd_part *slave;
205 uint64_t cur_offset = 0;
207 -#ifdef CONFIG_MTD_ROOTFS_SPLIT
208 +#if defined(CONFIG_MTD_ROOTFS_SPLIT) || defined(CONFIG_MTD_UIMAGE_SPLIT)
212 @@ -900,6 +1062,15 @@ int add_mtd_partitions(struct mtd_info *master,
214 add_mtd_device(&slave->mtd);
216 +#ifdef CONFIG_MTD_UIMAGE_SPLIT
217 + if (!strcmp(parts[i].name, "linux")) {
218 + ret = split_uimage(master, &parts[i]);
221 + printk(KERN_WARNING "Can't split linux partition\n");
225 if (!strcmp(parts[i].name, "rootfs")) {
226 #ifdef CONFIG_MTD_ROOTFS_ROOT_DEV