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 @@ -833,6 +833,99 @@ 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))
53 + return be32_to_cpu(temp) + 0x40;
56 +static int detect_squashfs_partition(struct mtd_info *mtd, unsigned long offset)
62 + ret = mtd_read(mtd, offset, 4, &len, (void *)&temp);
63 + if (ret || len != sizeof(temp))
66 + return le32_to_cpu(temp) == SQUASHFS_MAGIC;
69 +static unsigned long find_squashfs_offset(struct mtd_info *mtd, unsigned long _offset)
71 + /* scan the first 2MB at 64K offsets */
74 + for (i = 0; i < 32; i++) {
75 + unsigned long offset = i * 64 * 1024;
76 + if (detect_squashfs_partition(mtd, _offset + offset))
82 +static int split_uimage(struct mtd_info *mtd,
83 + const struct mtd_partition *part)
85 + static struct mtd_partition split_partitions[] = {
97 + split_partitions[0].size = find_uimage_size(mtd, part->offset);
98 + if (!split_partitions[0].size) {
99 + split_partitions[0].size = find_squashfs_offset(mtd, part->offset);
100 + if (!split_partitions[0].size) {
101 + pr_err("failed to split firmware partition\n");
106 + if (!detect_squashfs_partition(mtd,
108 + + split_partitions[0].size)) {
109 + split_partitions[0].size &= ~(mtd->erasesize - 1);
110 + split_partitions[0].size += mtd->erasesize;
112 + pr_info("found squashfs behind kernel\n");
115 + split_partitions[0].offset = part->offset;
116 + split_partitions[1].offset = part->offset + split_partitions[0].size;
117 + split_partitions[1].size = part->size - split_partitions[0].size;
119 + add_mtd_partitions(mtd, split_partitions, 2);
126 * This function, given a master MTD object and a partition table, creates
127 * and registers slave MTD objects which are bound to the master according to
128 @@ -849,7 +942,7 @@ int add_mtd_partitions(struct mtd_info *
129 struct mtd_part *slave;
130 uint64_t cur_offset = 0;
132 -#ifdef CONFIG_MTD_ROOTFS_SPLIT
133 +#if defined(CONFIG_MTD_ROOTFS_SPLIT) || defined(CONFIG_MTD_UIMAGE_SPLIT)
137 @@ -866,6 +959,14 @@ int add_mtd_partitions(struct mtd_info *
139 add_mtd_device(&slave->mtd);
141 +#ifdef CONFIG_MTD_UIMAGE_SPLIT
142 + if (!strcmp(parts[i].name, "firmware")) {
143 + ret = split_uimage(master, &parts[i]);
145 + printk(KERN_WARNING "Can't split firmware partition\n");
149 if (!strcmp(parts[i].name, "rootfs")) {
150 #ifdef CONFIG_MTD_ROOTFS_ROOT_DEV