Fix comments to the mtd driver (see http://www.linux-mips.org/archives/linux-mips...
[openwrt/staging/yousong.git] / target / linux / ar7 / files / drivers / mtd / ar7part.c
1 /*
2 * Copyright (C) 2007 Eugene Konev <ejka@openwrt.org>
3 *
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.
8 *
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.
13 *
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
17 *
18 * TI AR7 flash partition table.
19 * Based on ar7 map by Felix Fietkau <nbd@openwrt.org>
20 *
21 */
22
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25
26 #include <linux/mtd/mtd.h>
27 #include <linux/mtd/partitions.h>
28 #include <linux/bootmem.h>
29 #include <linux/squashfs_fs.h>
30
31 struct ar7_bin_rec {
32 unsigned int checksum;
33 unsigned int length;
34 unsigned int address;
35 };
36
37 static struct mtd_partition ar7_parts[5];
38
39 static int create_mtd_partitions(struct mtd_info *master,
40 struct mtd_partition **pparts,
41 unsigned long origin)
42 {
43 struct ar7_bin_rec header;
44 unsigned int offset, len;
45 unsigned int pre_size = master->erasesize, post_size = 0,
46 unsigned int root_offset = 0xe0000;
47
48 int retries = 10;
49
50 printk(KERN_INFO "Parsing AR7 partition map...\n");
51
52 ar7_parts[0].name = "loader";
53 ar7_parts[0].offset = 0;
54 ar7_parts[0].size = master->erasesize;
55 ar7_parts[0].mask_flags = MTD_WRITEABLE;
56
57 ar7_parts[1].name = "config";
58 ar7_parts[1].offset = 0;
59 ar7_parts[1].size = master->erasesize;
60 ar7_parts[1].mask_flags = 0;
61
62 do {
63 offset = pre_size;
64 master->read(master, offset, sizeof(header), &len, (u_char *)&header);
65 if (!strncmp((char *)&header, "TIENV0.8", 8))
66 ar7_parts[1].offset = pre_size;
67 if (header.checksum == 0xfeedfa42)
68 break;
69 if (header.checksum == 0xfeed1281)
70 break;
71 pre_size += master->erasesize;
72 } while (retries--);
73
74 pre_size = offset;
75
76 if (!ar7_parts[1].offset) {
77 ar7_parts[1].offset = master->size - master->erasesize;
78 post_size = master->erasesize;
79 }
80
81 switch (header.checksum) {
82 case 0xfeedfa42:
83 while (header.length) {
84 offset += sizeof(header) + header.length;
85 master->read(master, offset, sizeof(header),
86 &len, (u_char *)&header);
87 }
88 root_offset = offset + sizeof(header) + 4;
89 break;
90 case 0xfeed1281:
91 while (header.length) {
92 offset += sizeof(header) + header.length;
93 master->read(master, offset, sizeof(header),
94 &len, (u_char *)&header);
95 }
96 root_offset = offset + sizeof(header) + 4 + 0xff;
97 root_offset &= ~(u32)0xff;
98 break;
99 default:
100 printk(KERN_WARNING "Unknown magic: %08x\n", header.checksum);
101 break;
102 }
103
104 master->read(master, root_offset, sizeof(header), &len, (u_char *)&header);
105 if (header.checksum != SQUASHFS_MAGIC) {
106 root_offset += master->erasesize - 1;
107 root_offset &= ~(master->erasesize - 1);
108 }
109
110 ar7_parts[2].name = "linux";
111 ar7_parts[2].offset = pre_size;
112 ar7_parts[2].size = master->size - pre_size - post_size;
113 ar7_parts[2].mask_flags = 0;
114
115 ar7_parts[3].name = "rootfs";
116 ar7_parts[3].offset = root_offset;
117 ar7_parts[3].size = master->size - root_offset - post_size;
118 ar7_parts[3].mask_flags = 0;
119
120 *pparts = ar7_parts;
121 return 4;
122 }
123
124 static struct mtd_part_parser ar7_parser = {
125 .owner = THIS_MODULE,
126 .parse_fn = create_mtd_partitions,
127 .name = "ar7part",
128 };
129
130 static int __init ar7_parser_init(void)
131 {
132 return register_mtd_parser(&ar7_parser);
133 }
134
135 module_init(ar7_parser_init);
136
137 MODULE_LICENSE("GPL");
138 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>, Eugene Konev <ejka@openwrt.org>");
139 MODULE_DESCRIPTION("MTD partitioning for TI AR7");