1/*
2 *
3 * Copyright (C) 2007 OpenWrt.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 *
19 * Atmel AT91 flash partition table. (Modified by Hamish Guthrie).
20 * Based on ar7 map by Felix Fietkau.
21 *
22 */
23
24#include <linux/kernel.h>
25#include <linux/slab.h>
26
27#include <linux/mtd/mtd.h>
28#include <linux/mtd/partitions.h>
29#include <linux/bootmem.h>
30#include <linux/squashfs_fs.h>
31
32static struct mtd_partition at91_parts[6];
33
34static int create_mtd_partitions(struct mtd_info *master,
35				 struct mtd_partition **pparts,
36				 unsigned long origin)
37{
38	unsigned int offset, len;
39	unsigned int pre_size = 0x42000, root_max = 0x362400;
40	unsigned char buf[512];
41	struct squashfs_super_block *sb = (struct squashfs_super_block *) buf;
42
43	printk("Parsing AT91 partition map...\n");
44
45	at91_parts[0].name = "loaders";
46	at91_parts[0].offset = 0;
47	at91_parts[0].size = 0x21000;
48	at91_parts[0].mask_flags = MTD_WRITEABLE;
49
50	at91_parts[1].name = "ubparams";
51	at91_parts[1].offset = 0x21000;
52	at91_parts[1].size = 0x8400;
53	at91_parts[1].mask_flags = 0;
54
55	at91_parts[2].name = "kernel";
56	at91_parts[2].offset = pre_size;
57	at91_parts[2].size = 0;
58	at91_parts[2].mask_flags = 0;
59
60	at91_parts[3].name = "rootfs";
61	at91_parts[3].offset = 0;
62	at91_parts[3].size = 0;
63	at91_parts[3].mask_flags = 0;
64
65	for(offset = pre_size; offset < root_max; offset += master->erasesize) {
66
67		memset(&buf, 0xe5, sizeof(buf));
68
69		if (master->read(master, offset, sizeof(buf), &len, buf) || len != sizeof(buf))
70			break;
71
72		if (*((__u32 *) buf) == SQUASHFS_MAGIC) {
73			printk(KERN_INFO "%s: Filesystem type: squashfs, size=0x%x\n",
74							master->name, (u32) sb->bytes_used);
75
76			at91_parts[3].size = sb->bytes_used;
77			at91_parts[3].offset = offset;
78			len = at91_parts[3].offset + at91_parts[3].size;
79			len = ((len / (master->erasesize * 8)) + 1) * master->erasesize * 8;
80			at91_parts[3].size = len - at91_parts[3].offset;
81			at91_parts[2].size = offset - at91_parts[2].offset;
82			break;
83			}
84		}
85
86	if (at91_parts[3].size == 0) {
87		printk(KERN_NOTICE "%s: Couldn't find root filesystem\n", master->name);
88		return -1;
89		}
90
91	at91_parts[4].name = "rootfs_data";
92	at91_parts[4].offset = root_max;
93	at91_parts[4].size = master->size - root_max;
94	at91_parts[4].mask_flags = 0;
95
96	at91_parts[5].name = "complete";
97	at91_parts[5].offset = 0;
98	at91_parts[5].size = master->size;
99	at91_parts[5].mask_flags = 0;
100
101	*pparts = at91_parts;
102	return 6;
103}
104
105static struct mtd_part_parser at91_parser = {
106	.owner = THIS_MODULE,
107	.parse_fn = create_mtd_partitions,
108	.name = "at91part",
109};
110
111static int __init at91_parser_init(void)
112{
113	register_mtd_parser(&at91_parser);
114
115	return 0;
116}
117
118module_init(at91_parser_init);
119
120MODULE_LICENSE("GPL");
121MODULE_AUTHOR("Felix Fietkau, Eugene Konev, Hamish Guthrie");
122MODULE_DESCRIPTION("MTD partitioning for Atmel at91");
123