2 * Copyright © 2007 Eugene Konev <ejka@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * TI AR7 flash partition table.
19 * Based on ar7 map by Felix Fietkau <nbd@openwrt.org>
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
26 #include <linux/mtd/mtd.h>
27 #include <linux/mtd/partitions.h>
28 #include <linux/bootmem.h>
29 #include <linux/module.h>
31 #include <uapi/linux/magic.h>
34 #define ROOT_OFFSET 0xe0000
36 #define LOADER_MAGIC1 le32_to_cpu(0xfeedfa42)
37 #define LOADER_MAGIC2 le32_to_cpu(0xfeed1281)
40 unsigned int checksum
;
45 static int create_mtd_partitions(struct mtd_info
*master
,
46 const struct mtd_partition
**pparts
,
47 struct mtd_part_parser_data
*data
)
49 struct ar7_bin_rec header
;
52 unsigned int pre_size
= master
->erasesize
, post_size
= 0;
53 unsigned int root_offset
= ROOT_OFFSET
;
56 struct mtd_partition
*ar7_parts
;
58 ar7_parts
= kcalloc(AR7_PARTS
, sizeof(*ar7_parts
), GFP_KERNEL
);
61 ar7_parts
[0].name
= "loader";
62 ar7_parts
[0].offset
= 0;
63 ar7_parts
[0].size
= master
->erasesize
;
64 ar7_parts
[0].mask_flags
= MTD_WRITEABLE
;
66 ar7_parts
[1].name
= "config";
67 ar7_parts
[1].offset
= 0;
68 ar7_parts
[1].size
= master
->erasesize
;
69 ar7_parts
[1].mask_flags
= 0;
71 do { /* Try 10 blocks starting from master->erasesize */
73 mtd_read(master
, offset
, sizeof(header
), &len
,
75 if (!strncmp((char *)&header
, "TIENV0.8", 8))
76 ar7_parts
[1].offset
= pre_size
;
77 if (header
.checksum
== LOADER_MAGIC1
)
79 if (header
.checksum
== LOADER_MAGIC2
)
81 pre_size
+= master
->erasesize
;
86 if (!ar7_parts
[1].offset
) {
87 ar7_parts
[1].offset
= master
->size
- master
->erasesize
;
88 post_size
= master
->erasesize
;
91 switch (header
.checksum
) {
93 while (header
.length
) {
94 offset
+= sizeof(header
) + header
.length
;
95 mtd_read(master
, offset
, sizeof(header
), &len
,
98 root_offset
= offset
+ sizeof(header
) + 4;
101 while (header
.length
) {
102 offset
+= sizeof(header
) + header
.length
;
103 mtd_read(master
, offset
, sizeof(header
), &len
,
106 root_offset
= offset
+ sizeof(header
) + 4 + 0xff;
107 root_offset
&= ~(uint32_t)0xff;
110 printk(KERN_WARNING
"Unknown magic: %08x\n", header
.checksum
);
114 mtd_read(master
, root_offset
, sizeof(header
), &len
, (u8
*)&header
);
115 if (header
.checksum
!= SQUASHFS_MAGIC
) {
116 root_offset
+= master
->erasesize
- 1;
117 root_offset
&= ~(master
->erasesize
- 1);
120 ar7_parts
[2].name
= "linux";
121 ar7_parts
[2].offset
= pre_size
;
122 ar7_parts
[2].size
= master
->size
- pre_size
- post_size
;
123 ar7_parts
[2].mask_flags
= 0;
125 ar7_parts
[3].name
= "rootfs";
126 ar7_parts
[3].offset
= root_offset
;
127 ar7_parts
[3].size
= master
->size
- root_offset
- post_size
;
128 ar7_parts
[3].mask_flags
= 0;
134 static struct mtd_part_parser ar7_parser
= {
135 .parse_fn
= create_mtd_partitions
,
138 module_mtd_part_parser(ar7_parser
);
140 MODULE_LICENSE("GPL");
141 MODULE_AUTHOR( "Felix Fietkau <nbd@openwrt.org>, "
142 "Eugene Konev <ejka@openwrt.org>");
143 MODULE_DESCRIPTION("MTD partitioning for TI AR7");