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 --- a/drivers/mtd/Kconfig
14 +++ b/drivers/mtd/Kconfig
15 @@ -31,6 +31,10 @@ config MTD_ROOTFS_SPLIT
16 bool "Automatically split 'rootfs' partition for squashfs"
19 +config MTD_UIMAGE_SPLIT
20 + bool "Automatically split 'linux' partition into 'kernel' and 'rootfs'"
23 config MTD_REDBOOT_PARTS
24 tristate "RedBoot partition table parsing"
26 --- a/drivers/mtd/mtdpart.c
27 +++ b/drivers/mtd/mtdpart.c
28 @@ -844,6 +844,168 @@ static int refresh_rootfs_split(struct m
30 #endif /* CONFIG_MTD_ROOTFS_SPLIT */
32 +#ifdef CONFIG_MTD_UIMAGE_SPLIT
33 +static unsigned long find_uimage_size(struct mtd_info *mtd,
34 + unsigned long offset)
36 +#define UBOOT_MAGIC 0x56190527
37 + unsigned long magic = 0;
42 + ret = mtd_read(mtd, offset, 4, &len, (void *)&magic);
43 + if (ret || len != sizeof(magic))
46 + if (le32_to_cpu(magic) != UBOOT_MAGIC)
49 + ret = mtd_read(mtd, offset + 12, 4, &len, (void *)&temp);
50 + if (ret || len != sizeof(temp))
56 +static unsigned long find_eva_size(struct mtd_info *mtd,
57 + unsigned long offset)
59 +#define EVA_MAGIC 0xfeed1281
60 + unsigned long magic = 0;
65 + ret = mtd_read(mtd, offset, 4, &len, (void *)&magic);
66 + if (ret || len != sizeof(magic))
69 + if (le32_to_cpu(magic) != EVA_MAGIC)
72 + ret = mtd_read(mtd, offset + 4, 4, &len, (void *)&temp);
73 + if (ret || len != sizeof(temp))
76 + /* add eva header size */
77 + temp = le32_to_cpu(temp) + 0x18;
84 +static int detect_squashfs_partition(struct mtd_info *mtd, unsigned long offset)
90 + ret = mtd_read(mtd, offset, 4, &len, (void *)&temp);
91 + if (ret || len != sizeof(temp))
95 + return le32_to_cpu(temp) == SQUASHFS_MAGIC;
98 +static int detect_eva_squashfs_partition(struct mtd_info *mtd, unsigned long offset)
100 + unsigned long temp;
104 + ret = mtd_read(mtd, offset, 4, &len, (void *)&temp);
105 + if (ret || len != sizeof(temp))
108 + return be32_to_cpu(temp) == SQUASHFS_MAGIC;
111 +static unsigned long find_brnimage_size(struct mtd_info *mtd,
112 + unsigned long offset)
114 + unsigned long buf[4];
115 + // Assume at most 2MB of kernel image
116 + unsigned long end = offset + (2 << 20);
117 + unsigned long ptr = offset + 0x400 - 12;
121 + while (ptr < end) {
122 + long size_min = ptr - 0x400 - 12 - offset;
123 + long size_max = ptr + 12 - offset;
124 + ret = mtd_read(mtd, ptr, 16, &len, (void *)buf);
125 + if (ret || len != 16)
128 + if (le32_to_cpu(buf[0]) < size_min ||
129 + le32_to_cpu(buf[0]) > size_max) {
134 + if (le32_to_cpu(buf[3]) == SQUASHFS_MAGIC)
135 + return ptr + 12 - offset;
143 +static int split_uimage(struct mtd_info *mtd,
144 + const struct mtd_partition *part)
146 + static struct mtd_partition split_partitions[] = {
158 + split_partitions[0].size = find_uimage_size(mtd, part->offset);
159 + if (!split_partitions[0].size) {
160 + split_partitions[0].size = find_eva_size(mtd, part->offset);
161 + if (!split_partitions[0].size) {
162 + split_partitions[0].size = find_brnimage_size(mtd, part->offset);
163 + if (!split_partitions[0].size) {
164 + printk(KERN_NOTICE "no uImage or brnImage or eva found in linux partition\n");
170 + if (detect_eva_squashfs_partition(mtd,
172 + + split_partitions[0].size)) {
173 + split_partitions[0].size += 0x100;
174 + pr_info("found eva dummy squashfs behind kernel\n");
175 + } else if (!detect_squashfs_partition(mtd,
177 + + split_partitions[0].size)) {
178 + split_partitions[0].size &= ~(mtd->erasesize - 1);
179 + split_partitions[0].size += mtd->erasesize;
181 + pr_info("found squashfs behind kernel\n");
184 + split_partitions[0].offset = part->offset;
185 + split_partitions[1].offset = part->offset + split_partitions[0].size;
186 + split_partitions[1].size = part->size - split_partitions[0].size;
188 + add_mtd_partitions(mtd, split_partitions, 2);
195 * This function, given a master MTD object and a partition table, creates
196 * and registers slave MTD objects which are bound to the master according to
197 @@ -860,7 +1022,7 @@ int add_mtd_partitions(struct mtd_info *
198 struct mtd_part *slave;
199 uint64_t cur_offset = 0;
201 -#ifdef CONFIG_MTD_ROOTFS_SPLIT
202 +#if defined(CONFIG_MTD_ROOTFS_SPLIT) || defined(CONFIG_MTD_UIMAGE_SPLIT)
206 @@ -877,6 +1039,15 @@ int add_mtd_partitions(struct mtd_info *
208 add_mtd_device(&slave->mtd);
210 +#ifdef CONFIG_MTD_UIMAGE_SPLIT
211 + if (!strcmp(parts[i].name, "linux")) {
212 + ret = split_uimage(master, &parts[i]);
215 + printk(KERN_WARNING "Can't split linux partition\n");
219 if (!strcmp(parts[i].name, "rootfs")) {
220 #ifdef CONFIG_MTD_ROOTFS_ROOT_DEV