2 * Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/vmalloc.h>
14 #include <linux/magic.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/mtd/partitions.h>
19 #define TPLINK_NUM_PARTS 5
20 #define TPLINK_HEADER_V1 0x01000000
23 #define TPLINK_ART_LEN 0x10000
24 #define TPLINK_KERNEL_OFFS 0x20000
26 struct tplink_fw_header
{
27 uint32_t version
; /* header version */
30 uint32_t hw_id
; /* hardware id */
31 uint32_t hw_rev
; /* hardware revision */
33 uint8_t md5sum1
[MD5SUM_LEN
];
35 uint8_t md5sum2
[MD5SUM_LEN
];
37 uint32_t kernel_la
; /* kernel load address */
38 uint32_t kernel_ep
; /* kernel entry point */
39 uint32_t fw_length
; /* total length of the firmware */
40 uint32_t kernel_ofs
; /* kernel data offset */
41 uint32_t kernel_len
; /* kernel data length */
42 uint32_t rootfs_ofs
; /* rootfs data offset */
43 uint32_t rootfs_len
; /* rootfs data length */
44 uint32_t boot_ofs
; /* bootloader data offset */
45 uint32_t boot_len
; /* bootloader data length */
47 } __attribute__ ((packed
));
49 static struct tplink_fw_header
*
50 tplink_read_header(struct mtd_info
*mtd
, size_t offset
)
52 struct tplink_fw_header
*header
;
58 header
= vmalloc(sizeof(*header
));
62 header_len
= sizeof(struct tplink_fw_header
);
63 ret
= mtd
->read(mtd
, offset
, header_len
, &retlen
,
64 (unsigned char *) header
);
68 if (retlen
!= header_len
)
72 t
= be32_to_cpu(header
->version
);
73 if (t
!= TPLINK_HEADER_V1
)
76 t
= be32_to_cpu(header
->kernel_ofs
);
88 static int tplink_check_rootfs_magic(struct mtd_info
*mtd
, size_t offset
)
94 ret
= mtd
->read(mtd
, offset
, sizeof(magic
), &retlen
,
95 (unsigned char *) &magic
);
99 if (retlen
!= sizeof(magic
))
102 if (le32_to_cpu(magic
) != SQUASHFS_MAGIC
&&
109 static int tplink_parse_partitions(struct mtd_info
*master
,
110 struct mtd_partition
**pparts
,
111 unsigned long origin
)
113 struct mtd_partition
*parts
;
114 struct tplink_fw_header
*header
;
118 size_t rootfs_offset
;
119 size_t squashfs_offset
;
122 nr_parts
= TPLINK_NUM_PARTS
;
123 parts
= kzalloc(nr_parts
* sizeof(struct mtd_partition
), GFP_KERNEL
);
129 offset
= TPLINK_KERNEL_OFFS
;
131 header
= tplink_read_header(master
, offset
);
133 pr_notice("%s: no TP-Link header found\n", master
->name
);
138 squashfs_offset
= offset
+ sizeof(struct tplink_fw_header
) +
139 be32_to_cpu(header
->kernel_len
);
141 ret
= tplink_check_rootfs_magic(master
, squashfs_offset
);
143 rootfs_offset
= squashfs_offset
;
145 rootfs_offset
= offset
+ be32_to_cpu(header
->rootfs_ofs
);
147 art_offset
= master
->size
- TPLINK_ART_LEN
;
149 parts
[0].name
= "u-boot";
151 parts
[0].size
= offset
;
152 parts
[0].mask_flags
= MTD_WRITEABLE
;
154 parts
[1].name
= "kernel";
155 parts
[1].offset
= offset
;
156 parts
[1].size
= rootfs_offset
- offset
;
158 parts
[2].name
= "rootfs";
159 parts
[2].offset
= rootfs_offset
;
160 parts
[2].size
= art_offset
- rootfs_offset
;
162 parts
[3].name
= "art";
163 parts
[3].offset
= art_offset
;
164 parts
[3].size
= TPLINK_ART_LEN
;
165 parts
[3].mask_flags
= MTD_WRITEABLE
;
167 parts
[4].name
= "firmware";
168 parts
[4].offset
= offset
;
169 parts
[4].size
= art_offset
- offset
;
183 static struct mtd_part_parser tplink_parser
= {
184 .owner
= THIS_MODULE
,
185 .parse_fn
= tplink_parse_partitions
,
189 static int __init
tplink_parser_init(void)
191 return register_mtd_parser(&tplink_parser
);
194 module_init(tplink_parser_init
);
196 MODULE_LICENSE("GPL v2");
197 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");