b43: Fix Bugzilla #14181 and the bug from the previous 'fix'
[linux-2.6/verdex.git] / drivers / mtd / ar7part.c
blob6697a1ec72d067ec07a42c15986740ea33169095
1 /*
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/magic.h>
31 #define AR7_PARTS 4
32 #define ROOT_OFFSET 0xe0000
34 #define LOADER_MAGIC1 le32_to_cpu(0xfeedfa42)
35 #define LOADER_MAGIC2 le32_to_cpu(0xfeed1281)
37 #ifndef SQUASHFS_MAGIC
38 #define SQUASHFS_MAGIC 0x73717368
39 #endif
41 struct ar7_bin_rec {
42 unsigned int checksum;
43 unsigned int length;
44 unsigned int address;
47 static int create_mtd_partitions(struct mtd_info *master,
48 struct mtd_partition **pparts,
49 unsigned long origin)
51 struct ar7_bin_rec header;
52 unsigned int offset;
53 size_t len;
54 unsigned int pre_size = master->erasesize, post_size = 0;
55 unsigned int root_offset = ROOT_OFFSET;
57 int retries = 10;
58 struct mtd_partition *ar7_parts;
60 ar7_parts = kzalloc(sizeof(*ar7_parts) * AR7_PARTS, GFP_KERNEL);
61 if (!ar7_parts)
62 return -ENOMEM;
63 ar7_parts[0].name = "loader";
64 ar7_parts[0].offset = 0;
65 ar7_parts[0].size = master->erasesize;
66 ar7_parts[0].mask_flags = MTD_WRITEABLE;
68 ar7_parts[1].name = "config";
69 ar7_parts[1].offset = 0;
70 ar7_parts[1].size = master->erasesize;
71 ar7_parts[1].mask_flags = 0;
73 do { /* Try 10 blocks starting from master->erasesize */
74 offset = pre_size;
75 master->read(master, offset,
76 sizeof(header), &len, (uint8_t *)&header);
77 if (!strncmp((char *)&header, "TIENV0.8", 8))
78 ar7_parts[1].offset = pre_size;
79 if (header.checksum == LOADER_MAGIC1)
80 break;
81 if (header.checksum == LOADER_MAGIC2)
82 break;
83 pre_size += master->erasesize;
84 } while (retries--);
86 pre_size = offset;
88 if (!ar7_parts[1].offset) {
89 ar7_parts[1].offset = master->size - master->erasesize;
90 post_size = master->erasesize;
93 switch (header.checksum) {
94 case LOADER_MAGIC1:
95 while (header.length) {
96 offset += sizeof(header) + header.length;
97 master->read(master, offset, sizeof(header),
98 &len, (uint8_t *)&header);
100 root_offset = offset + sizeof(header) + 4;
101 break;
102 case LOADER_MAGIC2:
103 while (header.length) {
104 offset += sizeof(header) + header.length;
105 master->read(master, offset, sizeof(header),
106 &len, (uint8_t *)&header);
108 root_offset = offset + sizeof(header) + 4 + 0xff;
109 root_offset &= ~(uint32_t)0xff;
110 break;
111 default:
112 printk(KERN_WARNING "Unknown magic: %08x\n", header.checksum);
113 break;
116 master->read(master, root_offset,
117 sizeof(header), &len, (u8 *)&header);
118 if (header.checksum != SQUASHFS_MAGIC) {
119 root_offset += master->erasesize - 1;
120 root_offset &= ~(master->erasesize - 1);
123 ar7_parts[2].name = "linux";
124 ar7_parts[2].offset = pre_size;
125 ar7_parts[2].size = master->size - pre_size - post_size;
126 ar7_parts[2].mask_flags = 0;
128 ar7_parts[3].name = "rootfs";
129 ar7_parts[3].offset = root_offset;
130 ar7_parts[3].size = master->size - root_offset - post_size;
131 ar7_parts[3].mask_flags = 0;
133 *pparts = ar7_parts;
134 return AR7_PARTS;
137 static struct mtd_part_parser ar7_parser = {
138 .owner = THIS_MODULE,
139 .parse_fn = create_mtd_partitions,
140 .name = "ar7part",
143 static int __init ar7_parser_init(void)
145 return register_mtd_parser(&ar7_parser);
148 module_init(ar7_parser_init);
150 MODULE_LICENSE("GPL");
151 MODULE_AUTHOR( "Felix Fietkau <nbd@openwrt.org>, "
152 "Eugene Konev <ejka@openwrt.org>");
153 MODULE_DESCRIPTION("MTD partitioning for TI AR7");