ralink: update patches
[openwrt.git] / target / linux / ramips / patches-3.9 / 0208-owrt-mtd-split.patch
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 --- 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"
17         default y
18  
19 +config MTD_UIMAGE_SPLIT
20 +       bool "Automatically split 'linux' partition into 'kernel' and 'rootfs'"
21 +       default y
22 +
23  config MTD_REDBOOT_PARTS
24         tristate "RedBoot partition table parsing"
25         ---help---
26 --- a/drivers/mtd/mtdpart.c
27 +++ b/drivers/mtd/mtdpart.c
28 @@ -833,6 +833,99 @@ static int refresh_rootfs_split(struct m
29  }
30  #endif /* CONFIG_MTD_ROOTFS_SPLIT */
31  
32 +#ifdef CONFIG_MTD_UIMAGE_SPLIT
33 +static unsigned long find_uimage_size(struct mtd_info *mtd,
34 +                                     unsigned long offset)
35 +{
36 +#define UBOOT_MAGIC    0x56190527
37 +       unsigned long magic = 0;
38 +       unsigned long temp;
39 +       size_t len;
40 +       int ret;
41 +
42 +       ret = mtd_read(mtd, offset, 4, &len, (void *)&magic);
43 +       if (ret || len != sizeof(magic))
44 +               return 0;
45 +
46 +       if (le32_to_cpu(magic) != UBOOT_MAGIC)
47 +               return 0;
48 +
49 +       ret = mtd_read(mtd, offset + 12, 4, &len, (void *)&temp);
50 +       if (ret || len != sizeof(temp))
51 +               return 0;
52 +
53 +       return be32_to_cpu(temp) + 0x40;
54 +}
55 +
56 +static int detect_squashfs_partition(struct mtd_info *mtd, unsigned long offset)
57 +{
58 +       unsigned long temp;
59 +       size_t len;
60 +       int ret;
61 +
62 +       ret = mtd_read(mtd, offset, 4, &len, (void *)&temp);
63 +       if (ret || len != sizeof(temp))
64 +               return 0;
65 +
66 +       return le32_to_cpu(temp) == SQUASHFS_MAGIC;
67 +}
68 +
69 +static unsigned long find_squashfs_offset(struct mtd_info *mtd, unsigned long _offset)
70 +{
71 +       /* scan the first 2MB at 64K offsets */
72 +       int i;
73 +
74 +       for (i = 0; i < 32; i++) {
75 +               unsigned long offset = i * 64 * 1024;
76 +               if (detect_squashfs_partition(mtd, _offset + offset))
77 +                       return offset;
78 +       }
79 +       return 0;
80 +}
81 +
82 +static int split_uimage(struct mtd_info *mtd,
83 +                       const struct mtd_partition *part)
84 +{
85 +       static struct mtd_partition split_partitions[] = {
86 +               {
87 +                       .name = "kernel",
88 +                       .offset = 0x0,
89 +                       .size = 0x0,
90 +               }, {
91 +                       .name = "rootfs",
92 +                       .offset = 0x0,
93 +                       .size = 0x0,
94 +               },
95 +       };
96 +
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");
102 +                       return -1;
103 +               }
104 +       }
105 +
106 +       if (!detect_squashfs_partition(mtd,
107 +                                      part->offset
108 +                                      + split_partitions[0].size)) {
109 +               split_partitions[0].size &= ~(mtd->erasesize - 1);
110 +               split_partitions[0].size += mtd->erasesize;
111 +       } else {
112 +               pr_info("found squashfs behind kernel\n");
113 +       }
114 +
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;
118 +
119 +       add_mtd_partitions(mtd, split_partitions, 2);
120 +
121 +       return 0;
122 +}
123 +#endif
124 +
125  /*
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;
131         int i;
132 -#ifdef CONFIG_MTD_ROOTFS_SPLIT
133 +#if defined(CONFIG_MTD_ROOTFS_SPLIT) || defined(CONFIG_MTD_UIMAGE_SPLIT)
134         int ret;
135  #endif
136  
137 @@ -866,6 +959,14 @@ int add_mtd_partitions(struct mtd_info *
138  
139                 add_mtd_device(&slave->mtd);
140  
141 +#ifdef CONFIG_MTD_UIMAGE_SPLIT
142 +               if (!strcmp(parts[i].name, "firmware")) {
143 +                       ret = split_uimage(master, &parts[i]);
144 +                       if (ret)
145 +                               printk(KERN_WARNING "Can't split firmware partition\n");
146 +               }
147 +#endif
148 +
149                 if (!strcmp(parts[i].name, "rootfs")) {
150  #ifdef CONFIG_MTD_ROOTFS_ROOT_DEV
151                         if (ROOT_DEV == 0) {