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.
7 +config MTD_TPLINK_PARTS
8 + tristate "TP-Link AR7XXX/AR9XXX partitioning support"
13 comment "User Modules And Translation Layers"
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
22 +obj-$(CONFIG_MTD_TPLINK_PARTS) += tplinkpart.o
24 # 'Users' - code which presents functionality to userspace.
25 obj-$(CONFIG_MTD_BLKDEVS) += mtd_blkdevs.o
27 +++ b/drivers/mtd/tplinkpart.c
30 + * Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org>
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.
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>
43 +#include <linux/mtd/mtd.h>
44 +#include <linux/mtd/partitions.h>
45 +#include <linux/version.h>
47 +#define TPLINK_NUM_PARTS 5
48 +#define TPLINK_HEADER_V1 0x01000000
49 +#define TPLINK_HEADER_V2 0x02000000
50 +#define MD5SUM_LEN 16
52 +#define TPLINK_ART_LEN 0x10000
53 +#define TPLINK_KERNEL_OFFS 0x20000
54 +#define TPLINK_64K_KERNEL_OFFS 0x10000
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 */
63 + uint8_t md5sum1[MD5SUM_LEN];
65 + uint8_t md5sum2[MD5SUM_LEN];
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 */
77 +} __attribute__ ((packed));
79 +static struct tplink_fw_header *
80 +tplink_read_header(struct mtd_info *mtd, size_t offset)
82 + struct tplink_fw_header *header;
88 + header = vmalloc(sizeof(*header));
92 + header_len = sizeof(struct tplink_fw_header);
93 + ret = mtd_read(mtd, offset, header_len, &retlen,
94 + (unsigned char *) header);
96 + goto err_free_header;
98 + if (retlen != header_len)
99 + goto err_free_header;
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;
106 + t = be32_to_cpu(header->kernel_ofs);
107 + if (t != header_len)
108 + goto err_free_header;
118 +static int tplink_check_rootfs_magic(struct mtd_info *mtd, size_t offset)
124 + ret = mtd_read(mtd, offset, sizeof(magic), &retlen,
125 + (unsigned char *) &magic);
129 + if (retlen != sizeof(magic))
132 + if (le32_to_cpu(magic) != SQUASHFS_MAGIC &&
133 + magic != 0x19852003)
139 +static int tplink_parse_partitions_offset(struct mtd_info *master,
140 + const struct mtd_partition **pparts,
141 + struct mtd_part_parser_data *data,
144 + struct mtd_partition *parts;
145 + struct tplink_fw_header *header;
148 + size_t rootfs_offset;
149 + size_t squashfs_offset;
152 + nr_parts = TPLINK_NUM_PARTS;
153 + parts = kzalloc(nr_parts * sizeof(struct mtd_partition), GFP_KERNEL);
159 + header = tplink_read_header(master, offset);
161 + pr_notice("%s: no TP-Link header found\n", master->name);
163 + goto err_free_parts;
166 + squashfs_offset = offset + sizeof(struct tplink_fw_header) +
167 + be32_to_cpu(header->kernel_len);
169 + ret = tplink_check_rootfs_magic(master, squashfs_offset);
171 + rootfs_offset = squashfs_offset;
173 + rootfs_offset = offset + be32_to_cpu(header->rootfs_ofs);
175 + art_offset = master->size - TPLINK_ART_LEN;
177 + parts[0].name = "u-boot";
178 + parts[0].offset = 0;
179 + parts[0].size = offset;
180 + parts[0].mask_flags = MTD_WRITEABLE;
182 + parts[1].name = "kernel";
183 + parts[1].offset = offset;
184 + parts[1].size = rootfs_offset - offset;
186 + parts[2].name = "rootfs";
187 + parts[2].offset = rootfs_offset;
188 + parts[2].size = art_offset - rootfs_offset;
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;
195 + parts[4].name = "firmware";
196 + parts[4].offset = offset;
197 + parts[4].size = art_offset - offset;
211 +static int tplink_parse_partitions(struct mtd_info *master,
212 + const struct mtd_partition **pparts,
213 + struct mtd_part_parser_data *data)
215 + return tplink_parse_partitions_offset(master, pparts, data,
216 + TPLINK_KERNEL_OFFS);
219 +static int tplink_parse_64k_partitions(struct mtd_info *master,
220 + const struct mtd_partition **pparts,
221 + struct mtd_part_parser_data *data)
223 + return tplink_parse_partitions_offset(master, pparts, data,
224 + TPLINK_64K_KERNEL_OFFS);
227 +static struct mtd_part_parser tplink_parser = {
228 + .owner = THIS_MODULE,
229 + .parse_fn = tplink_parse_partitions,
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",
239 +static int __init tplink_parser_init(void)
241 + register_mtd_parser(&tplink_parser);
242 + register_mtd_parser(&tplink_64k_parser);
247 +module_init(tplink_parser_init);
249 +MODULE_LICENSE("GPL v2");
250 +MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");