ad0e25a1138b85edbfe2cd066e6e15b3c8efed30
[openwrt/svn-archive/archive.git] / target / linux / adm5120 / files / drivers / mtd / trxsplit.c
1 /*
2 * $Id$
3 *
4 * Copyright (C) 2007 OpenWrt.org
5 * Copyright (C) Gabor Juhos <juhosg at openwrt.org>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23 #include <linux/module.h>
24 #include <linux/types.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/list.h>
28 #include <linux/kmod.h>
29 #include <linux/root_dev.h>
30
31 #include <linux/mtd/mtd.h>
32 #include <linux/mtd/partitions.h>
33
34 #include <linux/byteorder/generic.h>
35
36 #define PFX "trxsplit: "
37
38 #define TRX_MAGIC 0x30524448 /* "HDR0" */
39 #define TRX_VERSION 1
40 #define TRX_MAX_LEN 0x3A0000
41 #define TRX_NO_HEADER 1 /* Do not write TRX header */
42 #define TRX_GZ_FILES 0x2 /* Contains up to TRX_MAX_OFFSET individual gzip files */
43 #define TRX_MAX_OFFSET 3
44 #define TRX_MIN_KERNEL_SIZE 256*1024
45
46 struct trx_header {
47 u32 magic; /* "HDR0" */
48 u32 len; /* Length of file including header */
49 u32 crc32; /* 32-bit CRC from flag_version to end of file */
50 u32 flag_version; /* 0:15 flags, 16:31 version */
51 u32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of header */
52 };
53
54 #define BLOCK_LEN_MIN 0x10000
55
56 static struct mtd_info *trx_mtd = NULL;
57 static unsigned long trx_offset = 0;
58 static int trx_nr_parts = 0;
59 static struct mtd_partition trx_parts[TRX_MAX_OFFSET];
60 static struct trx_header trx_hdr;
61
62 static int trxsplit_refresh_partitions(struct mtd_info *mtd);
63
64 static int trxsplit_checktrx(struct mtd_info *mtd, unsigned long offset)
65 {
66 size_t retlen;
67 int err;
68
69 err = mtd->read(mtd, offset, sizeof(trx_hdr), &retlen, (void *)&trx_hdr);
70 if (err) {
71 printk(KERN_ALERT PFX "unable to read from '%s'\n", mtd->name);
72 goto err_out;
73 }
74
75 if (retlen != sizeof(trx_hdr)) {
76 printk(KERN_ALERT PFX "reading failed on '%s'\n", mtd->name);
77 goto err_out;
78 }
79
80 trx_hdr.magic = le32_to_cpu(trx_hdr.magic);
81 trx_hdr.len = le32_to_cpu(trx_hdr.len);
82 trx_hdr.crc32 = le32_to_cpu(trx_hdr.crc32);
83 trx_hdr.flag_version = le32_to_cpu(trx_hdr.flag_version);
84 trx_hdr.offsets[0] = le32_to_cpu(trx_hdr.offsets[0]);
85 trx_hdr.offsets[1] = le32_to_cpu(trx_hdr.offsets[1]);
86 trx_hdr.offsets[2] = le32_to_cpu(trx_hdr.offsets[2]);
87
88 /* sanity checks */
89 if (trx_hdr.magic != TRX_MAGIC)
90 goto err_out;
91
92 if (trx_hdr.len > mtd->size - offset)
93 goto err_out;
94
95 /* TODO: add crc32 checking too? */
96
97 return 0;
98
99 err_out:
100 return -1;
101 }
102
103 static void trxsplit_findtrx(struct mtd_info *mtd)
104 {
105 unsigned long offset;
106 unsigned long blocklen;
107 int err;
108
109 blocklen = mtd->erasesize;
110 if (blocklen < BLOCK_LEN_MIN)
111 blocklen = BLOCK_LEN_MIN;
112
113 printk(KERN_INFO PFX "searching TRX header in '%s'\n", mtd->name);
114
115 err = 0;
116 for (offset=0; offset < mtd->size; offset+=blocklen) {
117 err = trxsplit_checktrx(mtd, offset);
118 if (err == 0)
119 break;
120 }
121
122 if (err)
123 return;
124
125 printk(KERN_INFO PFX "TRX header found at 0x%lX\n", offset);
126
127 trx_mtd = mtd;
128 trx_offset = offset;
129 }
130
131 static void trxsplit_create_partitions(struct mtd_info *mtd)
132 {
133 struct mtd_partition *part = trx_parts;
134 int err;
135 int i;
136
137 for (i=0; i<TRX_MAX_OFFSET;i++) {
138 part = &trx_parts[i];
139 if (trx_hdr.offsets[i] == 0)
140 continue;
141 part->offset = trx_offset + trx_hdr.offsets[i];
142 trx_nr_parts++;
143 }
144
145 for (i=0; i<trx_nr_parts-1; i++) {
146 trx_parts[i].size = trx_parts[i+1].offset - trx_parts[i].offset;
147 }
148 trx_parts[i].size = mtd->size - trx_parts[i].offset;
149
150 i=0;
151 part = &trx_parts[i];
152 if (part->size < TRX_MIN_KERNEL_SIZE) {
153 part->name = "loader";
154 i++;
155 }
156
157 part = &trx_parts[i];
158 part->name = "kernel";
159 i++;
160
161 part = &trx_parts[i];
162 part->name = "rootfs";
163
164 err = add_mtd_partitions(mtd, trx_parts, trx_nr_parts);
165 if (err) {
166 printk(KERN_ALERT PFX "adding TRX partitions failed\n");
167 return;
168 }
169
170 mtd->refresh_device = trxsplit_refresh_partitions;
171 }
172
173 static int trxsplit_refresh_partitions(struct mtd_info *mtd)
174 {
175 printk(KERN_INFO PFX "refreshing TRX partitions in '%s' (%d,%d)\n",
176 mtd->name, MTD_BLOCK_MAJOR, mtd->index);
177
178 /* remove old partitions */
179 del_mtd_partitions(mtd);
180
181 trxsplit_findtrx(mtd);
182 if (!trx_mtd)
183 goto err;
184
185 trxsplit_create_partitions(trx_mtd);
186 return 1;
187
188 err:
189 return 0;
190 }
191
192 static void __init trxsplit_add_mtd(struct mtd_info *mtd)
193 {
194 if (!trx_mtd)
195 trxsplit_findtrx(mtd);
196 }
197
198 static void __init trxsplit_remove_mtd(struct mtd_info *mtd)
199 {
200 /* nothing to do */
201 }
202
203 static struct mtd_notifier trxsplit_notifier __initdata = {
204 .add = trxsplit_add_mtd,
205 .remove = trxsplit_remove_mtd,
206 };
207
208 static void __init trxsplit_scan(void)
209 {
210 register_mtd_user(&trxsplit_notifier);
211 unregister_mtd_user(&trxsplit_notifier);
212 }
213
214 static int __init trxsplit_init(void)
215 {
216 trxsplit_scan();
217
218 if (trx_mtd) {
219 printk(KERN_INFO PFX "creating TRX partitions in '%s' (%d,%d)\n",
220 trx_mtd->name, MTD_BLOCK_MAJOR, trx_mtd->index);
221 trxsplit_create_partitions(trx_mtd);
222 }
223
224 return 0;
225 }
226
227 late_initcall(trxsplit_init);