add broadcom b44 driver support
[openwrt.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 0x1 /* do not write TRX header */
42 #define TRX_GZ_FILES 0x2 /* contains 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 */
52 };
53
54 #define TRX_ALIGN 0x1000
55
56 static int trx_nr_parts;
57 static unsigned long trx_offset;
58 static struct mtd_info *trx_mtd;
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,
70 (void *)&trx_hdr);
71 if (err) {
72 printk(KERN_ALERT PFX "unable to read from '%s'\n", mtd->name);
73 goto err_out;
74 }
75
76 if (retlen != sizeof(trx_hdr)) {
77 printk(KERN_ALERT PFX "reading failed on '%s'\n", mtd->name);
78 goto err_out;
79 }
80
81 trx_hdr.magic = le32_to_cpu(trx_hdr.magic);
82 trx_hdr.len = le32_to_cpu(trx_hdr.len);
83 trx_hdr.crc32 = le32_to_cpu(trx_hdr.crc32);
84 trx_hdr.flag_version = le32_to_cpu(trx_hdr.flag_version);
85 trx_hdr.offsets[0] = le32_to_cpu(trx_hdr.offsets[0]);
86 trx_hdr.offsets[1] = le32_to_cpu(trx_hdr.offsets[1]);
87 trx_hdr.offsets[2] = le32_to_cpu(trx_hdr.offsets[2]);
88
89 /* sanity checks */
90 if (trx_hdr.magic != TRX_MAGIC)
91 goto err_out;
92
93 if (trx_hdr.len > mtd->size - offset)
94 goto err_out;
95
96 /* TODO: add crc32 checking too? */
97
98 return 0;
99
100 err_out:
101 return -1;
102 }
103
104 static void trxsplit_findtrx(struct mtd_info *mtd)
105 {
106 unsigned long offset;
107 int err;
108
109 printk(KERN_INFO PFX "searching TRX header in '%s'\n", mtd->name);
110
111 err = 0;
112 for (offset = 0; offset < mtd->size; offset += TRX_ALIGN) {
113 err = trxsplit_checktrx(mtd, offset);
114 if (err == 0)
115 break;
116 }
117
118 if (err)
119 return;
120
121 printk(KERN_INFO PFX "TRX header found at 0x%lX\n", offset);
122
123 trx_mtd = mtd;
124 trx_offset = offset;
125 }
126
127 static void trxsplit_create_partitions(struct mtd_info *mtd)
128 {
129 struct mtd_partition *part = trx_parts;
130 int err;
131 int i;
132
133 for (i = 0; i < TRX_MAX_OFFSET; i++) {
134 part = &trx_parts[i];
135 if (trx_hdr.offsets[i] == 0)
136 continue;
137 part->offset = trx_offset + trx_hdr.offsets[i];
138 trx_nr_parts++;
139 }
140
141 for (i = 0; i < trx_nr_parts-1; i++)
142 trx_parts[i].size = trx_parts[i+1].offset - trx_parts[i].offset;
143
144 trx_parts[i].size = mtd->size - trx_parts[i].offset;
145
146 i = 0;
147 part = &trx_parts[i];
148 if (part->size < TRX_MIN_KERNEL_SIZE) {
149 part->name = "loader";
150 i++;
151 }
152
153 part = &trx_parts[i];
154 part->name = "kernel";
155 i++;
156
157 part = &trx_parts[i];
158 part->name = "rootfs";
159
160 err = add_mtd_partitions(mtd, trx_parts, trx_nr_parts);
161 if (err) {
162 printk(KERN_ALERT PFX "adding TRX partitions failed\n");
163 return;
164 }
165
166 mtd->refresh_device = trxsplit_refresh_partitions;
167 }
168
169 static int trxsplit_refresh_partitions(struct mtd_info *mtd)
170 {
171 printk(KERN_INFO PFX "refreshing TRX partitions in '%s' (%d,%d)\n",
172 mtd->name, MTD_BLOCK_MAJOR, mtd->index);
173
174 /* remove old partitions */
175 del_mtd_partitions(mtd);
176
177 trxsplit_findtrx(mtd);
178 if (!trx_mtd)
179 goto err;
180
181 trxsplit_create_partitions(trx_mtd);
182 return 1;
183
184 err:
185 return 0;
186 }
187
188 static void __init trxsplit_add_mtd(struct mtd_info *mtd)
189 {
190 if (mtd->type != MTD_NORFLASH) {
191 printk(KERN_INFO PFX "'%s' is not a NOR flash, skipped\n",
192 mtd->name);
193 return;
194 }
195
196 if (!trx_mtd)
197 trxsplit_findtrx(mtd);
198 }
199
200 static void __init trxsplit_remove_mtd(struct mtd_info *mtd)
201 {
202 /* nothing to do */
203 }
204
205 static struct mtd_notifier trxsplit_notifier __initdata = {
206 .add = trxsplit_add_mtd,
207 .remove = trxsplit_remove_mtd,
208 };
209
210 static void __init trxsplit_scan(void)
211 {
212 register_mtd_user(&trxsplit_notifier);
213 unregister_mtd_user(&trxsplit_notifier);
214 }
215
216 static int __init trxsplit_init(void)
217 {
218 trxsplit_scan();
219
220 if (trx_mtd) {
221 printk(KERN_INFO PFX "creating TRX partitions in '%s' "
222 "(%d,%d)\n", trx_mtd->name, MTD_BLOCK_MAJOR,
223 trx_mtd->index);
224 trxsplit_create_partitions(trx_mtd);
225 }
226
227 return 0;
228 }
229
230 late_initcall(trxsplit_init);
This page took 0.055701 seconds and 5 git commands to generate.