blob: ecf170b55c3245650c220e1cf7bb75606ade0e84 [file] [log] [blame]
Matteo Crocef0797882008-03-12 02:25:06 +01001/*
2 * Copyright © 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/magic.h>
30
31#define AR7_PARTS 4
32#define ROOT_OFFSET 0xe0000
33
34#define LOADER_MAGIC1 le32_to_cpu(0xfeedfa42)
35#define LOADER_MAGIC2 le32_to_cpu(0xfeed1281)
36
David Woodhouse986ee012008-04-23 09:39:49 +010037#ifndef SQUASHFS_MAGIC
38#define SQUASHFS_MAGIC 0x73717368
39#endif
40
Matteo Crocef0797882008-03-12 02:25:06 +010041struct ar7_bin_rec {
42 unsigned int checksum;
43 unsigned int length;
44 unsigned int address;
45};
46
47static struct mtd_partition ar7_parts[AR7_PARTS];
48
49static int create_mtd_partitions(struct mtd_info *master,
50 struct mtd_partition **pparts,
51 unsigned long origin)
52{
53 struct ar7_bin_rec header;
David Woodhouse986ee012008-04-23 09:39:49 +010054 unsigned int offset;
55 size_t len;
Matteo Crocef0797882008-03-12 02:25:06 +010056 unsigned int pre_size = master->erasesize, post_size = 0;
57 unsigned int root_offset = ROOT_OFFSET;
58
59 int retries = 10;
60
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;
65
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;
70
71 do { /* Try 10 blocks starting from master->erasesize */
72 offset = pre_size;
73 master->read(master, offset,
David Woodhouse986ee012008-04-23 09:39:49 +010074 sizeof(header), &len, (uint8_t *)&header);
Matteo Crocef0797882008-03-12 02:25:06 +010075 if (!strncmp((char *)&header, "TIENV0.8", 8))
76 ar7_parts[1].offset = pre_size;
77 if (header.checksum == LOADER_MAGIC1)
78 break;
79 if (header.checksum == LOADER_MAGIC2)
80 break;
81 pre_size += master->erasesize;
82 } while (retries--);
83
84 pre_size = offset;
85
86 if (!ar7_parts[1].offset) {
87 ar7_parts[1].offset = master->size - master->erasesize;
88 post_size = master->erasesize;
89 }
90
91 switch (header.checksum) {
92 case LOADER_MAGIC1:
93 while (header.length) {
94 offset += sizeof(header) + header.length;
95 master->read(master, offset, sizeof(header),
David Woodhouse986ee012008-04-23 09:39:49 +010096 &len, (uint8_t *)&header);
Matteo Crocef0797882008-03-12 02:25:06 +010097 }
98 root_offset = offset + sizeof(header) + 4;
99 break;
100 case LOADER_MAGIC2:
101 while (header.length) {
102 offset += sizeof(header) + header.length;
103 master->read(master, offset, sizeof(header),
David Woodhouse986ee012008-04-23 09:39:49 +0100104 &len, (uint8_t *)&header);
Matteo Crocef0797882008-03-12 02:25:06 +0100105 }
106 root_offset = offset + sizeof(header) + 4 + 0xff;
David Woodhouse986ee012008-04-23 09:39:49 +0100107 root_offset &= ~(uint32_t)0xff;
Matteo Crocef0797882008-03-12 02:25:06 +0100108 break;
109 default:
110 printk(KERN_WARNING "Unknown magic: %08x\n", header.checksum);
111 break;
112 }
113
114 master->read(master, root_offset,
115 sizeof(header), &len, (u8 *)&header);
116 if (header.checksum != SQUASHFS_MAGIC) {
117 root_offset += master->erasesize - 1;
118 root_offset &= ~(master->erasesize - 1);
119 }
120
121 ar7_parts[2].name = "linux";
122 ar7_parts[2].offset = pre_size;
123 ar7_parts[2].size = master->size - pre_size - post_size;
124 ar7_parts[2].mask_flags = 0;
125
126 ar7_parts[3].name = "rootfs";
127 ar7_parts[3].offset = root_offset;
128 ar7_parts[3].size = master->size - root_offset - post_size;
129 ar7_parts[3].mask_flags = 0;
130
131 *pparts = ar7_parts;
132 return AR7_PARTS;
133}
134
135static struct mtd_part_parser ar7_parser = {
136 .owner = THIS_MODULE,
137 .parse_fn = create_mtd_partitions,
138 .name = "ar7part",
139};
140
141static int __init ar7_parser_init(void)
142{
143 return register_mtd_parser(&ar7_parser);
144}
145
146module_init(ar7_parser_init);
147
148MODULE_LICENSE("GPL");
149MODULE_AUTHOR( "Felix Fietkau <nbd@openwrt.org>, "
150 "Eugene Konev <ejka@openwrt.org>");
151MODULE_DESCRIPTION("MTD partitioning for TI AR7");