Style fixes
[openwrt.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 { /* Try 10 blocks starting from master->erasesize */
63 offset = pre_size;
64 master->read(master, offset,
65 sizeof(header), &len, (u_char *)&header);
66 if (!strncmp((char *)&header, "TIENV0.8", 8))
67 ar7_parts[1].offset = pre_size;
68 if (header.checksum == 0xfeedfa42)
69 break;
70 if (header.checksum == 0xfeed1281)
71 break;
72 pre_size += master->erasesize;
73 } while (retries--);
74
75 pre_size = offset;
76
77 if (!ar7_parts[1].offset) {
78 ar7_parts[1].offset = master->size - master->erasesize;
79 post_size = master->erasesize;
80 }
81
82 switch (header.checksum) {
83 case 0xfeedfa42:
84 while (header.length) {
85 offset += sizeof(header) + header.length;
86 master->read(master, offset, sizeof(header),
87 &len, (u_char *)&header);
88 }
89 root_offset = offset + sizeof(header) + 4;
90 break;
91 case 0xfeed1281:
92 while (header.length) {
93 offset += sizeof(header) + header.length;
94 master->read(master, offset, sizeof(header),
95 &len, (u_char *)&header);
96 }
97 root_offset = offset + sizeof(header) + 4 + 0xff;
98 root_offset &= ~(u32)0xff;
99 break;
100 default:
101 printk(KERN_WARNING "Unknown magic: %08x\n", header.checksum);
102 break;
103 }
104
105 master->read(master, root_offset,
106 sizeof(header), &len, (u_char *)&header);
107 if (header.checksum != SQUASHFS_MAGIC) {
108 root_offset += master->erasesize - 1;
109 root_offset &= ~(master->erasesize - 1);
110 }
111
112 ar7_parts[2].name = "linux";
113 ar7_parts[2].offset = pre_size;
114 ar7_parts[2].size = master->size - pre_size - post_size;
115 ar7_parts[2].mask_flags = 0;
116
117 ar7_parts[3].name = "rootfs";
118 ar7_parts[3].offset = root_offset;
119 ar7_parts[3].size = master->size - root_offset - post_size;
120 ar7_parts[3].mask_flags = 0;
121
122 *pparts = ar7_parts;
123 return 4;
124 }
125
126 static struct mtd_part_parser ar7_parser = {
127 .owner = THIS_MODULE,
128 .parse_fn = create_mtd_partitions,
129 .name = "ar7part",
130 };
131
132 static int __init ar7_parser_init(void)
133 {
134 return register_mtd_parser(&ar7_parser);
135 }
136
137 module_init(ar7_parser_init);
138
139 MODULE_LICENSE("GPL");
140 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>, Eugene Konev <ejka@openwrt.org>");
141 MODULE_DESCRIPTION("MTD partitioning for TI AR7");
This page took 0.050447 seconds and 5 git commands to generate.