| 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 |
| 5 | |
| 6 | --- |
| 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(-) |
| 12 | |
| 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" |
| 21 | default y |
| 22 | |
| 23 | +config MTD_UIMAGE_SPLIT |
| 24 | + bool "Automatically split 'linux' partition into 'kernel' and 'rootfs'" |
| 25 | + default y |
| 26 | + |
| 27 | config MTD_REDBOOT_PARTS |
| 28 | tristate "RedBoot partition table parsing" |
| 29 | ---help--- |
| 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) |
| 35 | } |
| 36 | #endif /* CONFIG_MTD_ROOTFS_SPLIT */ |
| 37 | |
| 38 | +#ifdef CONFIG_MTD_UIMAGE_SPLIT |
| 39 | +static unsigned long find_uimage_size(struct mtd_info *mtd, |
| 40 | + unsigned long offset) |
| 41 | +{ |
| 42 | +#define UBOOT_MAGIC 0x56190527 |
| 43 | + unsigned long magic = 0; |
| 44 | + unsigned long temp; |
| 45 | + size_t len; |
| 46 | + int ret; |
| 47 | + |
| 48 | + ret = mtd_read(mtd, offset, 4, &len, (void *)&magic); |
| 49 | + if (ret || len != sizeof(magic)) |
| 50 | + return 0; |
| 51 | + |
| 52 | + if (le32_to_cpu(magic) != UBOOT_MAGIC) |
| 53 | + return 0; |
| 54 | + |
| 55 | + ret = mtd_read(mtd, offset + 12, 4, &len, (void *)&temp); |
| 56 | + if (ret || len != sizeof(temp)) |
| 57 | + return 0; |
| 58 | + |
| 59 | + return temp + 0x40; |
| 60 | +} |
| 61 | + |
| 62 | +static unsigned long find_eva_size(struct mtd_info *mtd, |
| 63 | + unsigned long offset) |
| 64 | +{ |
| 65 | +#define EVA_MAGIC 0xfeed1281 |
| 66 | + unsigned long magic = 0; |
| 67 | + unsigned long temp; |
| 68 | + size_t len; |
| 69 | + int ret; |
| 70 | + |
| 71 | + ret = mtd_read(mtd, offset, 4, &len, (void *)&magic); |
| 72 | + if (ret || len != sizeof(magic)) |
| 73 | + return 0; |
| 74 | + |
| 75 | + if (le32_to_cpu(magic) != EVA_MAGIC) |
| 76 | + return 0; |
| 77 | + |
| 78 | + ret = mtd_read(mtd, offset + 4, 4, &len, (void *)&temp); |
| 79 | + if (ret || len != sizeof(temp)) |
| 80 | + return 0; |
| 81 | + |
| 82 | + /* add eva header size */ |
| 83 | + temp = le32_to_cpu(temp) + 0x18; |
| 84 | + |
| 85 | + temp &= ~0xffff; |
| 86 | + temp += 0x10000; |
| 87 | + return temp; |
| 88 | +} |
| 89 | + |
| 90 | +static int detect_squashfs_partition(struct mtd_info *mtd, unsigned long offset) |
| 91 | +{ |
| 92 | + unsigned long temp; |
| 93 | + size_t len; |
| 94 | + int ret; |
| 95 | + |
| 96 | + ret = mtd_read(mtd, offset, 4, &len, (void *)&temp); |
| 97 | + if (ret || len != sizeof(temp)) |
| 98 | + return 0; |
| 99 | + |
| 100 | + |
| 101 | + return le32_to_cpu(temp) == SQUASHFS_MAGIC; |
| 102 | +} |
| 103 | + |
| 104 | +static int detect_eva_squashfs_partition(struct mtd_info *mtd, unsigned long offset) |
| 105 | +{ |
| 106 | + unsigned long temp; |
| 107 | + size_t len; |
| 108 | + int ret; |
| 109 | + |
| 110 | + ret = mtd_read(mtd, offset, 4, &len, (void *)&temp); |
| 111 | + if (ret || len != sizeof(temp)) |
| 112 | + return 0; |
| 113 | + |
| 114 | + return be32_to_cpu(temp) == SQUASHFS_MAGIC; |
| 115 | +} |
| 116 | + |
| 117 | +static unsigned long find_brnimage_size(struct mtd_info *mtd, |
| 118 | + unsigned long offset) |
| 119 | +{ |
| 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; |
| 124 | + size_t len; |
| 125 | + int ret; |
| 126 | + |
| 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) |
| 132 | + return 0; |
| 133 | + |
| 134 | + if (le32_to_cpu(buf[0]) < size_min || |
| 135 | + le32_to_cpu(buf[0]) > size_max) { |
| 136 | + ptr += 0x400; |
| 137 | + continue; |
| 138 | + } |
| 139 | + |
| 140 | + if (le32_to_cpu(buf[3]) == SQUASHFS_MAGIC) |
| 141 | + return ptr + 12 - offset; |
| 142 | + |
| 143 | + ptr += 0x400; |
| 144 | + } |
| 145 | + |
| 146 | + return 0; |
| 147 | +} |
| 148 | + |
| 149 | +static int split_uimage(struct mtd_info *mtd, |
| 150 | + const struct mtd_partition *part) |
| 151 | +{ |
| 152 | + static struct mtd_partition split_partitions[] = { |
| 153 | + { |
| 154 | + .name = "kernel", |
| 155 | + .offset = 0x0, |
| 156 | + .size = 0x0, |
| 157 | + }, { |
| 158 | + .name = "rootfs", |
| 159 | + .offset = 0x0, |
| 160 | + .size = 0x0, |
| 161 | + }, |
| 162 | + }; |
| 163 | + |
| 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"); |
| 171 | + return -1; |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + if (detect_eva_squashfs_partition(mtd, |
| 177 | + part->offset |
| 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, |
| 182 | + part->offset |
| 183 | + + split_partitions[0].size)) { |
| 184 | + split_partitions[0].size &= ~(mtd->erasesize - 1); |
| 185 | + split_partitions[0].size += mtd->erasesize; |
| 186 | + } else { |
| 187 | + pr_info("found squashfs behind kernel\n"); |
| 188 | + } |
| 189 | + |
| 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; |
| 193 | + |
| 194 | + add_mtd_partitions(mtd, split_partitions, 2); |
| 195 | + |
| 196 | + return 0; |
| 197 | +} |
| 198 | +#endif |
| 199 | + |
| 200 | /* |
| 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; |
| 206 | int i; |
| 207 | -#ifdef CONFIG_MTD_ROOTFS_SPLIT |
| 208 | +#if defined(CONFIG_MTD_ROOTFS_SPLIT) || defined(CONFIG_MTD_UIMAGE_SPLIT) |
| 209 | int ret; |
| 210 | #endif |
| 211 | |
| 212 | @@ -900,6 +1062,15 @@ int add_mtd_partitions(struct mtd_info *master, |
| 213 | |
| 214 | add_mtd_device(&slave->mtd); |
| 215 | |
| 216 | +#ifdef CONFIG_MTD_UIMAGE_SPLIT |
| 217 | + if (!strcmp(parts[i].name, "linux")) { |
| 218 | + ret = split_uimage(master, &parts[i]); |
| 219 | + |
| 220 | + if (ret) |
| 221 | + printk(KERN_WARNING "Can't split linux partition\n"); |
| 222 | + } |
| 223 | +#endif |
| 224 | + |
| 225 | if (!strcmp(parts[i].name, "rootfs")) { |
| 226 | #ifdef CONFIG_MTD_ROOTFS_ROOT_DEV |
| 227 | if (ROOT_DEV == 0) { |
| 228 | -- |
| 229 | 1.7.9.1 |
| 230 | |
| 231 | |