Add myloader partition table parser
[openwrt.git] / target / linux / adm5120-2.6 / files / drivers / mtd / maps / adm5120_mtd.c
1 /*
2 * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2005 Waldemar Brodkorb <wbx@openwrt.org>
4 * Copyright (C) 2004 Florian Schirmer (jolt@tuxbox.org)
5 *
6 * original functions for finding root filesystem from Mike Baker
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
14 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
16 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
19 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 675 Mass Ave, Cambridge, MA 02139, USA.
27 *
28 * Copyright 2001-2003, Broadcom Corporation
29 * All Rights Reserved.
30 *
31 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
32 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
33 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
34 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
35 *
36 * Flash mapping for ADM5120 boards
37 */
38
39 #include <linux/init.h>
40 #include <linux/module.h>
41 #include <linux/types.h>
42 #include <linux/kernel.h>
43 #include <linux/wait.h>
44 #include <linux/mtd/mtd.h>
45 #include <linux/mtd/map.h>
46 #ifdef CONFIG_MTD_PARTITIONS
47 #include <linux/mtd/partitions.h>
48 #endif
49 #include <linux/squashfs_fs.h>
50 #include <linux/jffs2.h>
51 #include <linux/crc32.h>
52 #ifdef CONFIG_SSB
53 #include <linux/ssb/ssb.h>
54 #endif
55 #include <asm/io.h>
56
57
58 #define TRX_MAGIC 0x30524448 /* "HDR0" */
59 #define TRX_VERSION 1
60 #define TRX_MAX_LEN 0x3A0000
61 #define TRX_NO_HEADER 1 /* Do not write TRX header */
62 #define TRX_GZ_FILES 0x2 /* Contains up to TRX_MAX_OFFSET individual gzip files */
63 #define TRX_MAX_OFFSET 3
64
65 struct trx_header {
66 u32 magic; /* "HDR0" */
67 u32 len; /* Length of file including header */
68 u32 crc32; /* 32-bit CRC from flag_version to end of file */
69 u32 flag_version; /* 0:15 flags, 16:31 version */
70 u32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of header */
71 };
72
73 #define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y))
74 #define NVRAM_SPACE 0x8000
75 #define WINDOW_ADDR 0x1fc00000
76 #define WINDOW_SIZE 0x400000
77 #define BUSWIDTH 2
78
79 #ifdef CONFIG_SSB
80 extern struct ssb_bus ssb;
81 #endif
82 static struct mtd_info *adm5120_mtd;
83
84 static struct map_info adm5120_map = {
85 name: "adm5120 physically mapped flash",
86 size: WINDOW_SIZE,
87 bankwidth: BUSWIDTH,
88 phys: WINDOW_ADDR,
89 };
90
91 #ifdef CONFIG_MTD_PARTITIONS
92
93 static struct mtd_partition adm5120_parts[] = {
94 { name: "cfe", offset: 0, size: 0, mask_flags: MTD_WRITEABLE, },
95 { name: "linux", offset: 0, size: 0, },
96 { name: "rootfs", offset: 0, size: 0, },
97 { name: "nvram", offset: 0, size: 0, },
98 { name: "OpenWrt", offset: 0, size: 0, },
99 { name: NULL, },
100 };
101
102 static int __init
103 find_cfe_size(struct mtd_info *mtd, size_t size)
104 {
105 struct trx_header *trx;
106 unsigned char buf[512];
107 int off;
108 size_t len;
109 int blocksize;
110
111 trx = (struct trx_header *) buf;
112
113 blocksize = mtd->erasesize;
114 if (blocksize < 0x10000)
115 blocksize = 0x10000;
116
117 for (off = (128*1024); off < size; off += blocksize) {
118 memset(buf, 0xe5, sizeof(buf));
119
120 /*
121 * Read into buffer
122 */
123 if (mtd->read(mtd, off, sizeof(buf), &len, buf) ||
124 len != sizeof(buf))
125 continue;
126
127 /* found a TRX header */
128 if (le32_to_cpu(trx->magic) == TRX_MAGIC) {
129 goto found;
130 }
131 }
132
133 printk(KERN_NOTICE
134 "%s: Couldn't find bootloader size\n",
135 mtd->name);
136 return -1;
137
138 found:
139 printk(KERN_NOTICE "bootloader size: %d\n", off);
140 return off;
141
142 }
143
144 /*
145 * Copied from mtdblock.c
146 *
147 * Cache stuff...
148 *
149 * Since typical flash erasable sectors are much larger than what Linux's
150 * buffer cache can handle, we must implement read-modify-write on flash
151 * sectors for each block write requests. To avoid over-erasing flash sectors
152 * and to speed things up, we locally cache a whole flash sector while it is
153 * being written to until a different sector is required.
154 */
155
156 static void erase_callback(struct erase_info *done)
157 {
158 wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
159 wake_up(wait_q);
160 }
161
162 static int erase_write (struct mtd_info *mtd, unsigned long pos,
163 int len, const char *buf)
164 {
165 struct erase_info erase;
166 DECLARE_WAITQUEUE(wait, current);
167 wait_queue_head_t wait_q;
168 size_t retlen;
169 int ret;
170
171 /*
172 * First, let's erase the flash block.
173 */
174
175 init_waitqueue_head(&wait_q);
176 erase.mtd = mtd;
177 erase.callback = erase_callback;
178 erase.addr = pos;
179 erase.len = len;
180 erase.priv = (u_long)&wait_q;
181
182 set_current_state(TASK_INTERRUPTIBLE);
183 add_wait_queue(&wait_q, &wait);
184
185 ret = mtd->erase(mtd, &erase);
186 if (ret) {
187 set_current_state(TASK_RUNNING);
188 remove_wait_queue(&wait_q, &wait);
189 printk (KERN_WARNING "erase of region [0x%lx, 0x%x] "
190 "on \"%s\" failed\n",
191 pos, len, mtd->name);
192 return ret;
193 }
194
195 schedule(); /* Wait for erase to finish. */
196 remove_wait_queue(&wait_q, &wait);
197
198 /*
199 * Next, writhe data to flash.
200 */
201
202 ret = mtd->write (mtd, pos, len, &retlen, buf);
203 if (ret)
204 return ret;
205 if (retlen != len)
206 return -EIO;
207 return 0;
208 }
209
210
211
212
213 static int __init
214 find_root(struct mtd_info *mtd, size_t size, struct mtd_partition *part)
215 {
216 struct trx_header trx, *trx2;
217 unsigned char buf[512], *block;
218 int off, blocksize;
219 u32 i, crc = ~0;
220 size_t len;
221 struct squashfs_super_block *sb = (struct squashfs_super_block *) buf;
222
223 blocksize = mtd->erasesize;
224 if (blocksize < 0x10000)
225 blocksize = 0x10000;
226
227 for (off = (128*1024); off < size; off += blocksize) {
228 memset(&trx, 0xe5, sizeof(trx));
229
230 /*
231 * Read into buffer
232 */
233 if (mtd->read(mtd, off, sizeof(trx), &len, (char *) &trx) ||
234 len != sizeof(trx))
235 continue;
236
237 /* found a TRX header */
238 if (le32_to_cpu(trx.magic) == TRX_MAGIC) {
239 part->offset = le32_to_cpu(trx.offsets[2]) ? :
240 le32_to_cpu(trx.offsets[1]);
241 part->size = le32_to_cpu(trx.len);
242
243 part->size -= part->offset;
244 part->offset += off;
245
246 goto found;
247 }
248 }
249
250 printk(KERN_NOTICE
251 "%s: Couldn't find root filesystem\n",
252 mtd->name);
253 return -1;
254
255 found:
256 if (part->size == 0)
257 return 0;
258
259 if (mtd->read(mtd, part->offset, sizeof(buf), &len, buf) || len != sizeof(buf))
260 return 0;
261
262 if (*((__u32 *) buf) == SQUASHFS_MAGIC) {
263 printk(KERN_INFO "%s: Filesystem type: squashfs, size=0x%x\n", mtd->name, (u32) sb->bytes_used);
264
265 /* Update the squashfs partition size based on the superblock info */
266 part->size = sb->bytes_used;
267 len = part->offset + part->size;
268 len += (mtd->erasesize - 1);
269 len &= ~(mtd->erasesize - 1);
270 part->size = len - part->offset;
271 } else if (*((__u16 *) buf) == JFFS2_MAGIC_BITMASK) {
272 printk(KERN_INFO "%s: Filesystem type: jffs2\n", mtd->name);
273
274 /* Move the squashfs outside of the trx */
275 part->size = 0;
276 } else {
277 printk(KERN_INFO "%s: Filesystem type: unknown\n", mtd->name);
278 return 0;
279 }
280
281 if (trx.len != part->offset + part->size - off) {
282 /* Update the trx offsets and length */
283 trx.len = part->offset + part->size - off;
284
285 /* Update the trx crc32 */
286 for (i = (u32) &(((struct trx_header *)NULL)->flag_version); i <= trx.len; i += sizeof(buf)) {
287 if (mtd->read(mtd, off + i, sizeof(buf), &len, buf) || len != sizeof(buf))
288 return 0;
289 crc = crc32_le(crc, buf, min(sizeof(buf), trx.len - i));
290 }
291 trx.crc32 = crc;
292
293 /* read first eraseblock from the trx */
294 block = kmalloc(mtd->erasesize, GFP_KERNEL);
295 trx2 = (struct trx_header *) block;
296 if (mtd->read(mtd, off, mtd->erasesize, &len, block) || len != mtd->erasesize) {
297 printk("Error accessing the first trx eraseblock\n");
298 return 0;
299 }
300
301 printk("Updating TRX offsets and length:\n");
302 printk("old trx = [0x%08x, 0x%08x, 0x%08x], len=0x%08x crc32=0x%08x\n", trx2->offsets[0], trx2->offsets[1], trx2->offsets[2], trx2->len, trx2->crc32);
303 printk("new trx = [0x%08x, 0x%08x, 0x%08x], len=0x%08x crc32=0x%08x\n", trx.offsets[0], trx.offsets[1], trx.offsets[2], trx.len, trx.crc32);
304
305 /* Write updated trx header to the flash */
306 memcpy(block, &trx, sizeof(trx));
307 if (mtd->unlock)
308 mtd->unlock(mtd, off, mtd->erasesize);
309 erase_write(mtd, off, mtd->erasesize, block);
310 if (mtd->sync)
311 mtd->sync(mtd);
312 kfree(block);
313 printk("Done\n");
314 }
315
316 return part->size;
317 }
318
319 struct mtd_partition * __init
320 init_mtd_partitions(struct mtd_info *mtd, size_t size)
321 {
322 int cfe_size;
323
324 if ((cfe_size = find_cfe_size(mtd,size)) < 0)
325 return NULL;
326
327 /* boot loader */
328 adm5120_parts[0].offset = 0;
329 adm5120_parts[0].size = cfe_size;
330
331 /* nvram */
332 if (cfe_size != 384 * 1024) {
333 adm5120_parts[3].offset = size - ROUNDUP(NVRAM_SPACE, mtd->erasesize);
334 adm5120_parts[3].size = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
335 } else {
336 /* nvram (old 128kb config partition on netgear wgt634u) */
337 adm5120_parts[3].offset = adm5120_parts[0].size;
338 adm5120_parts[3].size = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
339 }
340
341 /* linux (kernel and rootfs) */
342 if (cfe_size != 384 * 1024) {
343 adm5120_parts[1].offset = adm5120_parts[0].size;
344 adm5120_parts[1].size = adm5120_parts[3].offset -
345 adm5120_parts[1].offset;
346 } else {
347 /* do not count the elf loader, which is on one block */
348 adm5120_parts[1].offset = adm5120_parts[0].size +
349 adm5120_parts[3].size + mtd->erasesize;
350 adm5120_parts[1].size = size -
351 adm5120_parts[0].size -
352 (2*adm5120_parts[3].size) -
353 mtd->erasesize;
354 }
355
356 /* find and size rootfs */
357 if (find_root(mtd,size,&adm5120_parts[2])==0) {
358 /* entirely jffs2 */
359 adm5120_parts[4].name = NULL;
360 adm5120_parts[2].size = size - adm5120_parts[2].offset -
361 adm5120_parts[3].size;
362 } else {
363 /* legacy setup */
364 /* calculate leftover flash, and assign it to the jffs2 partition */
365 if (cfe_size != 384 * 1024) {
366 adm5120_parts[4].offset = adm5120_parts[2].offset +
367 adm5120_parts[2].size;
368 if ((adm5120_parts[4].offset % mtd->erasesize) > 0) {
369 adm5120_parts[4].offset += mtd->erasesize -
370 (adm5120_parts[4].offset % mtd->erasesize);
371 }
372 adm5120_parts[4].size = adm5120_parts[3].offset -
373 adm5120_parts[4].offset;
374 } else {
375 adm5120_parts[4].offset = adm5120_parts[2].offset +
376 adm5120_parts[2].size;
377 if ((adm5120_parts[4].offset % mtd->erasesize) > 0) {
378 adm5120_parts[4].offset += mtd->erasesize -
379 (adm5120_parts[4].offset % mtd->erasesize);
380 }
381 adm5120_parts[4].size = size - adm5120_parts[3].size -
382 adm5120_parts[4].offset;
383 }
384 }
385
386 return adm5120_parts;
387 }
388 #endif
389
390 int __init init_adm5120_map(void)
391 {
392 #ifdef CONFIG_SSB
393 struct ssb_mipscore *mcore = &ssb.mipscore;
394 #endif
395 size_t size;
396 int ret = 0;
397 #ifdef CONFIG_MTD_PARTITIONS
398 struct mtd_partition *parts;
399 int i;
400 #endif
401 #ifdef CONFIG_SSB
402 u32 window = mcore->flash_window;
403 u32 window_size = mcore->flash_window_size;
404
405 printk("adm5120 : flash init: 0x%08x 0x%08x\n", window, window_size);
406 adm5120_map.phys = window;
407 adm5120_map.size = window_size;
408 adm5120_map.virt = ioremap_nocache(window, window_size);
409 #else
410 printk("adm5120 : flash init : 0x%08x 0x%08x\n", WINDOW_ADDR, WINDOW_SIZE);
411 adm5120_map.virt = ioremap_nocache(WINDOW_ADDR, WINDOW_SIZE);
412 #endif
413
414 if (!adm5120_map.virt) {
415 printk("Failed to ioremap\n");
416 return -EIO;
417 }
418 simple_map_init(&adm5120_map);
419
420 if (!(adm5120_mtd = do_map_probe("cfi_probe", &adm5120_map))) {
421 printk("Failed to do_map_probe\n");
422 iounmap((void *)adm5120_map.virt);
423 return -ENXIO;
424 }
425
426 adm5120_mtd->owner = THIS_MODULE;
427
428 size = adm5120_mtd->size;
429
430 printk(KERN_NOTICE "Flash device: 0x%x at 0x%x\n", size, WINDOW_ADDR);
431
432 #ifdef CONFIG_MTD_PARTITIONS
433 parts = init_mtd_partitions(adm5120_mtd, size);
434 for (i = 0; parts[i].name; i++);
435 ret = add_mtd_partitions(adm5120_mtd, parts, i);
436 if (ret) {
437 printk(KERN_ERR "Flash: add_mtd_partitions failed\n");
438 goto fail;
439 }
440 #endif
441 return 0;
442
443 fail:
444 if (adm5120_mtd)
445 map_destroy(adm5120_mtd);
446 if (adm5120_map.virt)
447 iounmap((void *)adm5120_map.virt);
448 adm5120_map.virt = 0;
449 return ret;
450 }
451
452 void __exit cleanup_adm5120_map(void)
453 {
454 #ifdef CONFIG_MTD_PARTITIONS
455 del_mtd_partitions(adm5120_mtd);
456 #endif
457 map_destroy(adm5120_mtd);
458 iounmap((void *)adm5120_map.virt);
459 }
460
461 module_init(init_adm5120_map);
462 module_exit(cleanup_adm5120_map);
This page took 0.073224 seconds and 5 git commands to generate.