d65bf663b5d4b1e9a2b9e18dff81a99bb4cdafdd
[oweals/openwrt.git] / target / linux / ath79 / patches-4.19 / 405-mtd-tp-link-partition-parser.patch
1 --- a/drivers/mtd/Kconfig
2 +++ b/drivers/mtd/Kconfig
3 @@ -193,6 +193,12 @@ config MTD_MYLOADER_PARTS
4           You will still need the parsing functions to be called by the driver
5           for your particular device. It won't happen automatically.
6  
7 +config MTD_TPLINK_PARTS
8 +       tristate "TP-Link AR7XXX/AR9XXX partitioning support"
9 +       depends on ATH79
10 +       ---help---
11 +         TBD.
12 +
13  comment "User Modules And Translation Layers"
14  
15  #
16 --- a/drivers/mtd/Makefile
17 +++ b/drivers/mtd/Makefile
18 @@ -18,6 +18,7 @@ obj-$(CONFIG_MTD_BCM63XX_PARTS)       += bcm63
19  obj-$(CONFIG_MTD_BCM47XX_PARTS)        += bcm47xxpart.o
20  obj-$(CONFIG_MTD_MYLOADER_PARTS) += myloader.o
21  obj-y                          += parsers/
22 +obj-$(CONFIG_MTD_TPLINK_PARTS) += tplinkpart.o
23  
24  # 'Users' - code which presents functionality to userspace.
25  obj-$(CONFIG_MTD_BLKDEVS)      += mtd_blkdevs.o
26 --- /dev/null
27 +++ b/drivers/mtd/tplinkpart.c
28 @@ -0,0 +1,222 @@
29 +/*
30 + * Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org>
31 + *
32 + * This program is free software; you can redistribute it and/or modify it
33 + * under the terms of the GNU General Public License version 2 as published
34 + * by the Free Software Foundation.
35 + */
36 +
37 +#include <linux/kernel.h>
38 +#include <linux/module.h>
39 +#include <linux/slab.h>
40 +#include <linux/vmalloc.h>
41 +#include <linux/magic.h>
42 +
43 +#include <linux/mtd/mtd.h>
44 +#include <linux/mtd/partitions.h>
45 +#include <linux/version.h>
46 +
47 +#define TPLINK_NUM_PARTS       5
48 +#define TPLINK_HEADER_V1       0x01000000
49 +#define TPLINK_HEADER_V2       0x02000000
50 +#define MD5SUM_LEN             16
51 +
52 +#define TPLINK_ART_LEN         0x10000
53 +#define TPLINK_KERNEL_OFFS     0x20000
54 +#define TPLINK_64K_KERNEL_OFFS 0x10000
55 +
56 +struct tplink_fw_header {
57 +       uint32_t        version;        /* header version */
58 +       char            vendor_name[24];
59 +       char            fw_version[36];
60 +       uint32_t        hw_id;          /* hardware id */
61 +       uint32_t        hw_rev;         /* hardware revision */
62 +       uint32_t        unk1;
63 +       uint8_t         md5sum1[MD5SUM_LEN];
64 +       uint32_t        unk2;
65 +       uint8_t         md5sum2[MD5SUM_LEN];
66 +       uint32_t        unk3;
67 +       uint32_t        kernel_la;      /* kernel load address */
68 +       uint32_t        kernel_ep;      /* kernel entry point */
69 +       uint32_t        fw_length;      /* total length of the firmware */
70 +       uint32_t        kernel_ofs;     /* kernel data offset */
71 +       uint32_t        kernel_len;     /* kernel data length */
72 +       uint32_t        rootfs_ofs;     /* rootfs data offset */
73 +       uint32_t        rootfs_len;     /* rootfs data length */
74 +       uint32_t        boot_ofs;       /* bootloader data offset */
75 +       uint32_t        boot_len;       /* bootloader data length */
76 +       uint8_t         pad[360];
77 +} __attribute__ ((packed));
78 +
79 +static struct tplink_fw_header *
80 +tplink_read_header(struct mtd_info *mtd, size_t offset)
81 +{
82 +       struct tplink_fw_header *header;
83 +       size_t header_len;
84 +       size_t retlen;
85 +       int ret;
86 +       u32 t;
87 +
88 +       header = vmalloc(sizeof(*header));
89 +       if (!header)
90 +               goto err;
91 +
92 +       header_len = sizeof(struct tplink_fw_header);
93 +       ret = mtd_read(mtd, offset, header_len, &retlen,
94 +                      (unsigned char *) header);
95 +       if (ret)
96 +               goto err_free_header;
97 +
98 +       if (retlen != header_len)
99 +               goto err_free_header;
100 +
101 +       /* sanity checks */
102 +       t = be32_to_cpu(header->version);
103 +       if ((t != TPLINK_HEADER_V1) && (t != TPLINK_HEADER_V2))
104 +               goto err_free_header;
105 +
106 +       t = be32_to_cpu(header->kernel_ofs);
107 +       if (t != header_len)
108 +               goto err_free_header;
109 +
110 +       return header;
111 +
112 +err_free_header:
113 +       vfree(header);
114 +err:
115 +       return NULL;
116 +}
117 +
118 +static int tplink_check_rootfs_magic(struct mtd_info *mtd, size_t offset)
119 +{
120 +       u32 magic;
121 +       size_t retlen;
122 +       int ret;
123 +
124 +       ret = mtd_read(mtd, offset, sizeof(magic), &retlen,
125 +                      (unsigned char *) &magic);
126 +       if (ret)
127 +               return ret;
128 +
129 +       if (retlen != sizeof(magic))
130 +               return -EIO;
131 +
132 +       if (le32_to_cpu(magic) != SQUASHFS_MAGIC &&
133 +           magic != 0x19852003)
134 +               return -EINVAL;
135 +
136 +       return 0;
137 +}
138 +
139 +static int tplink_parse_partitions_offset(struct mtd_info *master,
140 +                                  const struct mtd_partition **pparts,
141 +                                  struct mtd_part_parser_data *data,
142 +                                  size_t offset)
143 +{
144 +       struct mtd_partition *parts;
145 +       struct tplink_fw_header *header;
146 +       int nr_parts;
147 +       size_t art_offset;
148 +       size_t rootfs_offset;
149 +       size_t squashfs_offset;
150 +       int ret;
151 +
152 +       nr_parts = TPLINK_NUM_PARTS;
153 +       parts = kzalloc(nr_parts * sizeof(struct mtd_partition), GFP_KERNEL);
154 +       if (!parts) {
155 +               ret = -ENOMEM;
156 +               goto err;
157 +       }
158 +
159 +       header = tplink_read_header(master, offset);
160 +       if (!header) {
161 +               pr_notice("%s: no TP-Link header found\n", master->name);
162 +               ret = -ENODEV;
163 +               goto err_free_parts;
164 +       }
165 +
166 +       squashfs_offset = offset + sizeof(struct tplink_fw_header) +
167 +                         be32_to_cpu(header->kernel_len);
168 +
169 +       ret = tplink_check_rootfs_magic(master, squashfs_offset);
170 +       if (ret == 0)
171 +               rootfs_offset = squashfs_offset;
172 +       else
173 +               rootfs_offset = offset + be32_to_cpu(header->rootfs_ofs);
174 +
175 +       art_offset = master->size - TPLINK_ART_LEN;
176 +
177 +       parts[0].name = "u-boot";
178 +       parts[0].offset = 0;
179 +       parts[0].size = offset;
180 +       parts[0].mask_flags = MTD_WRITEABLE;
181 +
182 +       parts[1].name = "kernel";
183 +       parts[1].offset = offset;
184 +       parts[1].size = rootfs_offset - offset;
185 +
186 +       parts[2].name = "rootfs";
187 +       parts[2].offset = rootfs_offset;
188 +       parts[2].size = art_offset - rootfs_offset;
189 +
190 +       parts[3].name = "art";
191 +       parts[3].offset = art_offset;
192 +       parts[3].size = TPLINK_ART_LEN;
193 +       parts[3].mask_flags = MTD_WRITEABLE;
194 +
195 +       parts[4].name = "firmware";
196 +       parts[4].offset = offset;
197 +       parts[4].size = art_offset - offset;
198 +
199 +       vfree(header);
200 +
201 +       *pparts = parts;
202 +       return nr_parts;
203 +
204 +err_free_parts:
205 +       kfree(parts);
206 +err:
207 +       *pparts = NULL;
208 +       return ret;
209 +}
210 +
211 +static int tplink_parse_partitions(struct mtd_info *master,
212 +                                  const struct mtd_partition **pparts,
213 +                                  struct mtd_part_parser_data *data)
214 +{
215 +       return tplink_parse_partitions_offset(master, pparts, data,
216 +                                             TPLINK_KERNEL_OFFS);
217 +}
218 +
219 +static int tplink_parse_64k_partitions(struct mtd_info *master,
220 +                                  const struct mtd_partition **pparts,
221 +                                  struct mtd_part_parser_data *data)
222 +{
223 +       return tplink_parse_partitions_offset(master, pparts, data,
224 +                                             TPLINK_64K_KERNEL_OFFS);
225 +}
226 +
227 +static struct mtd_part_parser tplink_parser = {
228 +       .owner          = THIS_MODULE,
229 +       .parse_fn       = tplink_parse_partitions,
230 +       .name           = "tp-link",
231 +};
232 +
233 +static struct mtd_part_parser tplink_64k_parser = {
234 +       .owner          = THIS_MODULE,
235 +       .parse_fn       = tplink_parse_64k_partitions,
236 +       .name           = "tp-link-64k",
237 +};
238 +
239 +static int __init tplink_parser_init(void)
240 +{
241 +       register_mtd_parser(&tplink_parser);
242 +       register_mtd_parser(&tplink_64k_parser);
243 +
244 +       return 0;
245 +}
246 +
247 +module_init(tplink_parser_init);
248 +
249 +MODULE_LICENSE("GPL v2");
250 +MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");