0b4abba9b92e07761fe16f88b22d3a53aa16698d
[openwrt/openwrt.git] / target / linux / brcm47xx / patches-3.6 / 050-mtd-add-bcm47xx-part-parser.patch
1 --- a/drivers/mtd/Kconfig
2 +++ b/drivers/mtd/Kconfig
3 @@ -172,6 +172,13 @@ config MTD_MYLOADER_PARTS
4 You will still need the parsing functions to be called by the driver
5 for your particular device. It won't happen automatically.
6
7 +config MTD_BCM47XX_PARTS
8 + tristate "BCM47XX partitioning support"
9 + default y
10 + depends on BCM47XX
11 + ---help---
12 + bcm47XX partitioning support
13 +
14 comment "User Modules And Translation Layers"
15
16 config MTD_CHAR
17 --- a/drivers/mtd/Makefile
18 +++ b/drivers/mtd/Makefile
19 @@ -13,6 +13,7 @@ obj-$(CONFIG_MTD_AFS_PARTS) += afs.o
20 obj-$(CONFIG_MTD_AR7_PARTS) += ar7part.o
21 obj-$(CONFIG_MTD_BCM63XX_PARTS) += bcm63xxpart.o
22 obj-$(CONFIG_MTD_MYLOADER_PARTS) += myloader.o
23 +obj-$(CONFIG_MTD_BCM47XX_PARTS) += bcm47xxpart.o
24
25 # 'Users' - code which presents functionality to userspace.
26 obj-$(CONFIG_MTD_CHAR) += mtdchar.o
27 --- /dev/null
28 +++ b/drivers/mtd/bcm47xxpart.c
29 @@ -0,0 +1,504 @@
30 +/*
31 + * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
32 + * Copyright (C) 2005 Waldemar Brodkorb <wbx@openwrt.org>
33 + * Copyright (C) 2004 Florian Schirmer (jolt@tuxbox.org)
34 + *
35 + * original functions for finding root filesystem from Mike Baker
36 + *
37 + * This program is free software; you can redistribute it and/or modify it
38 + * under the terms of the GNU General Public License as published by the
39 + * Free Software Foundation; either version 2 of the License, or (at your
40 + * option) any later version.
41 + *
42 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
43 + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
44 + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
45 + * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
46 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48 + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
49 + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
50 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
51 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 + *
53 + * You should have received a copy of the GNU General Public License along
54 + * with this program; if not, write to the Free Software Foundation, Inc.,
55 + * 675 Mass Ave, Cambridge, MA 02139, USA.
56 + *
57 + * Copyright 2001-2003, Broadcom Corporation
58 + * All Rights Reserved.
59 + *
60 + * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
61 + * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
62 + * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
63 + * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
64 + *
65 + * Flash mapping for BCM947XX boards
66 + */
67 +
68 +#define pr_fmt(fmt) "bcm47xx_part: " fmt
69 +#include <linux/init.h>
70 +#include <linux/module.h>
71 +#include <linux/types.h>
72 +#include <linux/kernel.h>
73 +#include <linux/sched.h>
74 +#include <linux/wait.h>
75 +#include <linux/mtd/mtd.h>
76 +#include <linux/mtd/partitions.h>
77 +#include <linux/crc32.h>
78 +#include <linux/io.h>
79 +#include <bcm47xx_nvram.h>
80 +#include <bcm47xx.h>
81 +#include <asm/fw/cfe/cfe_api.h>
82 +#include <bcm47xx_board.h>
83 +
84 +
85 +#define TRX_MAGIC 0x30524448 /* "HDR0" */
86 +#define TRX_VERSION 1
87 +#define TRX_MAX_LEN 0x3A0000
88 +#define TRX_NO_HEADER 1 /* Do not write TRX header */
89 +#define TRX_GZ_FILES 0x2 /* Contains up to TRX_MAX_OFFSET individual gzip files */
90 +#define TRX_MAX_OFFSET 3
91 +
92 +struct trx_header {
93 + u32 magic; /* "HDR0" */
94 + u32 len; /* Length of file including header */
95 + u32 crc32; /* 32-bit CRC from flag_version to end of file */
96 + u32 flag_version; /* 0:15 flags, 16:31 version */
97 + u32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of header */
98 +};
99 +
100 +/* for Edimax Print servers which use an additional header
101 + * then the firmware on flash looks like :
102 + * EDIMAX HEADER | TRX HEADER
103 + * As this header is 12 bytes long we have to handle it
104 + * and skip it to find the TRX header
105 + */
106 +#define EDIMAX_PS_HEADER_MAGIC 0x36315350 /* "PS16" */
107 +#define EDIMAX_PS_HEADER_LEN 0xc /* 12 bytes long for edimax header */
108 +
109 +#define NVRAM_SPACE 0x8000
110 +
111 +static int
112 +find_cfe_size(struct mtd_info *mtd)
113 +{
114 + struct trx_header *trx;
115 + unsigned char buf[512];
116 + int off;
117 + size_t len;
118 + int blocksize;
119 +
120 + trx = (struct trx_header *) buf;
121 +
122 + blocksize = mtd->erasesize;
123 + if (blocksize < 0x10000)
124 + blocksize = 0x10000;
125 +
126 + for (off = (128*1024); off < mtd->size; off += blocksize) {
127 + memset(buf, 0xe5, sizeof(buf));
128 +
129 + /*
130 + * Read into buffer
131 + */
132 + if (mtd_read(mtd, off, sizeof(buf), &len, buf) ||
133 + len != sizeof(buf))
134 + continue;
135 +
136 + if (le32_to_cpu(trx->magic) == EDIMAX_PS_HEADER_MAGIC) {
137 + if (mtd_read(mtd, off + EDIMAX_PS_HEADER_LEN,
138 + sizeof(buf), &len, buf) || len != sizeof(buf)) {
139 + continue;
140 + } else {
141 + pr_notice("Found edimax header\n");
142 + }
143 + }
144 +
145 + /* found a TRX header */
146 + if (le32_to_cpu(trx->magic) == TRX_MAGIC)
147 + goto found;
148 + }
149 +
150 + pr_notice("%s: Couldn't find bootloader size\n", mtd->name);
151 + return -1;
152 +
153 + found:
154 + pr_notice("bootloader size: %d\n", off);
155 + return off;
156 +
157 +}
158 +
159 +/*
160 + * Copied from mtdblock.c
161 + *
162 + * Cache stuff...
163 + *
164 + * Since typical flash erasable sectors are much larger than what Linux's
165 + * buffer cache can handle, we must implement read-modify-write on flash
166 + * sectors for each block write requests. To avoid over-erasing flash sectors
167 + * and to speed things up, we locally cache a whole flash sector while it is
168 + * being written to until a different sector is required.
169 + */
170 +
171 +static void erase_callback(struct erase_info *done)
172 +{
173 + wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
174 + wake_up(wait_q);
175 +}
176 +
177 +static int erase_write(struct mtd_info *mtd, unsigned long pos,
178 + int len, const char *buf)
179 +{
180 + struct erase_info erase;
181 + DECLARE_WAITQUEUE(wait, current);
182 + wait_queue_head_t wait_q;
183 + size_t retlen;
184 + int ret;
185 +
186 + /*
187 + * First, let's erase the flash block.
188 + */
189 +
190 + init_waitqueue_head(&wait_q);
191 + erase.mtd = mtd;
192 + erase.callback = erase_callback;
193 + erase.addr = pos;
194 + erase.len = len;
195 + erase.priv = (u_long)&wait_q;
196 +
197 + set_current_state(TASK_INTERRUPTIBLE);
198 + add_wait_queue(&wait_q, &wait);
199 +
200 + ret = mtd_erase(mtd, &erase);
201 + if (ret) {
202 + set_current_state(TASK_RUNNING);
203 + remove_wait_queue(&wait_q, &wait);
204 + pr_warn("erase of region [0x%lx, 0x%x] on \"%s\" failed\n",
205 + pos, len, mtd->name);
206 + return ret;
207 + }
208 +
209 + schedule(); /* Wait for erase to finish. */
210 + remove_wait_queue(&wait_q, &wait);
211 +
212 + /*
213 + * Next, write data to flash.
214 + */
215 +
216 + ret = mtd_write(mtd, pos, len, &retlen, buf);
217 + if (ret)
218 + return ret;
219 + if (retlen != len)
220 + return -EIO;
221 + return 0;
222 +}
223 +
224 +
225 +static int
226 +find_dual_image_off(struct mtd_info *mtd)
227 +{
228 + struct trx_header trx;
229 + int off, blocksize;
230 + size_t len;
231 +
232 + blocksize = mtd->erasesize;
233 + if (blocksize < 0x10000)
234 + blocksize = 0x10000;
235 +
236 + for (off = (128*1024); off < mtd->size; off += blocksize) {
237 + memset(&trx, 0xe5, sizeof(trx));
238 + /*
239 + * Read into buffer
240 + */
241 + if (mtd_read(mtd, off, sizeof(trx), &len, (char *) &trx) ||
242 + len != sizeof(trx))
243 + continue;
244 + /* found last TRX header */
245 + if (le32_to_cpu(trx.magic) == TRX_MAGIC) {
246 + if (le32_to_cpu(trx.flag_version >> 16) == 2) {
247 + pr_notice("dual image TRX header found\n");
248 + return mtd->size / 2;
249 + } else {
250 + return 0;
251 + }
252 + }
253 + }
254 + return 0;
255 +}
256 +
257 +
258 +static int
259 +find_root(struct mtd_info *mtd, struct mtd_partition *part)
260 +{
261 + struct trx_header trx, *trx2;
262 + unsigned char buf[512], *block;
263 + int off, blocksize, trxoff = 0;
264 + u32 i, crc = ~0;
265 + size_t len;
266 + bool edimax = false;
267 +
268 + blocksize = mtd->erasesize;
269 + if (blocksize < 0x10000)
270 + blocksize = 0x10000;
271 +
272 + for (off = (128*1024); off < mtd->size; off += blocksize) {
273 + memset(&trx, 0xe5, sizeof(trx));
274 +
275 + /*
276 + * Read into buffer
277 + */
278 + if (mtd_read(mtd, off, sizeof(trx), &len, (char *) &trx) ||
279 + len != sizeof(trx))
280 + continue;
281 +
282 + /* found an edimax header */
283 + if (le32_to_cpu(trx.magic) == EDIMAX_PS_HEADER_MAGIC) {
284 + /* read the correct trx header */
285 + if (mtd_read(mtd, off + EDIMAX_PS_HEADER_LEN,
286 + sizeof(trx), &len, (char *) &trx) ||
287 + len != sizeof(trx)) {
288 + continue;
289 + } else {
290 + pr_notice("Found an edimax ps header\n");
291 + edimax = true;
292 + }
293 + }
294 +
295 + /* found a TRX header */
296 + if (le32_to_cpu(trx.magic) == TRX_MAGIC) {
297 + part->offset = le32_to_cpu(trx.offsets[2]) ? :
298 + le32_to_cpu(trx.offsets[1]);
299 + part->size = le32_to_cpu(trx.len);
300 +
301 + part->size -= part->offset;
302 + part->offset += off;
303 + if (edimax) {
304 + off += EDIMAX_PS_HEADER_LEN;
305 + trxoff = EDIMAX_PS_HEADER_LEN;
306 + }
307 +
308 + goto found;
309 + }
310 + }
311 +
312 + pr_warn("%s: Couldn't find root filesystem\n",
313 + mtd->name);
314 + return -1;
315 +
316 + found:
317 + pr_notice("TRX offset : %x\n", trxoff);
318 + if (part->size == 0)
319 + return 0;
320 +
321 + if (mtd_read(mtd, part->offset, sizeof(buf), &len, buf) || len != sizeof(buf))
322 + return 0;
323 +
324 + /* Move the fs outside of the trx */
325 + part->size = 0;
326 +
327 + if (trx.len != part->offset + part->size - off) {
328 + /* Update the trx offsets and length */
329 + trx.len = part->offset + part->size - off;
330 +
331 + /* Update the trx crc32 */
332 + for (i = (u32) &(((struct trx_header *)NULL)->flag_version); i <= trx.len; i += sizeof(buf)) {
333 + if (mtd_read(mtd, off + i, sizeof(buf), &len, buf) || len != sizeof(buf))
334 + return 0;
335 + crc = crc32_le(crc, buf, min(sizeof(buf), trx.len - i));
336 + }
337 + trx.crc32 = crc;
338 +
339 + /* read first eraseblock from the trx */
340 + block = kmalloc(mtd->erasesize, GFP_KERNEL);
341 + trx2 = (struct trx_header *) block;
342 + if (mtd_read(mtd, off - trxoff, mtd->erasesize, &len, block) || len != mtd->erasesize) {
343 + pr_err("Error accessing the first trx eraseblock\n");
344 + return 0;
345 + }
346 +
347 + pr_notice("Updating TRX offsets and length:\n");
348 + pr_notice("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);
349 + pr_notice("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);
350 +
351 + /* Write updated trx header to the flash */
352 + memcpy(block + trxoff, &trx, sizeof(trx));
353 + if (mtd->_unlock)
354 + mtd->_unlock(mtd, off - trxoff, mtd->erasesize);
355 + erase_write(mtd, off - trxoff, mtd->erasesize, block);
356 + mtd_sync(mtd);
357 + kfree(block);
358 + pr_notice("Done\n");
359 + }
360 +
361 + return part->size;
362 +}
363 +
364 +static bool is_simpletech_simpleshare(void)
365 +{
366 + char buf[20];
367 + u16 boardtype = 0;
368 + u16 boardrev = 0;
369 + u32 boardflags = 0;
370 + u16 strev = 0;
371 +
372 + if (bcm47xx_nvram_getenv("boardtype", buf, sizeof(buf)) >= 0)
373 + boardtype = simple_strtoul(buf, NULL, 0);
374 + if (bcm47xx_nvram_getenv("boardrev", buf, sizeof(buf)) >= 0)
375 + boardrev = simple_strtoul(buf, NULL, 0);
376 + if (bcm47xx_nvram_getenv("boardflags", buf, sizeof(buf)) >= 0)
377 + boardflags = simple_strtoul(buf, NULL, 0);
378 + if (bcm47xx_nvram_getenv("st_rev", buf, sizeof(buf)) >= 0)
379 + strev = simple_strtoul(buf, NULL, 0);
380 +
381 + if (boardtype == 0x042f
382 + && boardrev == 0x10
383 + && boardflags == 0
384 + && strev == 0x11) {
385 + /* Simpletech Simpleshare */
386 + return true;
387 + }
388 +
389 + return false;
390 +}
391 +
392 +static int parse_bcm47xx_partitions(struct mtd_info *mtd,
393 + struct mtd_partition **pparts,
394 + struct mtd_part_parser_data *data)
395 +{
396 + int cfe_size;
397 + int dual_image_offset = 0;
398 + /* e.g Netgear 0x003e0000-0x003f0000 : "board_data", we exclude this
399 + * part from our mapping to prevent overwriting len/checksum on e.g.
400 + * Netgear WGR614v8/L/WW
401 + */
402 + int custom_data_size = 0;
403 + struct mtd_partition *bcm47xx_parts;
404 +
405 + cfe_size = find_cfe_size(mtd);
406 + if (cfe_size < 0)
407 + return 0;
408 +
409 + bcm47xx_parts = kzalloc(sizeof(struct mtd_partition) * 6, GFP_KERNEL);
410 +
411 + bcm47xx_parts[0].name = "cfe";
412 + bcm47xx_parts[1].name = "linux";
413 + bcm47xx_parts[2].name = "rootfs";
414 + bcm47xx_parts[3].name = "nvram";
415 +
416 + /* boot loader */
417 + bcm47xx_parts[0].mask_flags = MTD_WRITEABLE;
418 + bcm47xx_parts[0].offset = 0;
419 + bcm47xx_parts[0].size = cfe_size;
420 +
421 + /* nvram */
422 + if (cfe_size != 384 * 1024) {
423 +
424 + switch (bcm47xx_board_get()) {
425 + case BCM47XX_BOARD_NETGEAR_WGR614V8:
426 + case BCM47XX_BOARD_NETGEAR_WGR614V9:
427 + case BCM47XX_BOARD_NETGEAR_WNDR3300:
428 + case BCM47XX_BOARD_NETGEAR_WNDR3400V1:
429 + case BCM47XX_BOARD_NETGEAR_WNDR3400V2:
430 + case BCM47XX_BOARD_NETGEAR_WNDR3400VCNA:
431 + case BCM47XX_BOARD_NETGEAR_WNDR3700V3:
432 + case BCM47XX_BOARD_NETGEAR_WNDR4000:
433 + case BCM47XX_BOARD_NETGEAR_WNDR4500:
434 + case BCM47XX_BOARD_NETGEAR_WNR2000:
435 + case BCM47XX_BOARD_NETGEAR_WNR3500L:
436 + case BCM47XX_BOARD_NETGEAR_WNR3500U:
437 + case BCM47XX_BOARD_NETGEAR_WNR3500V2:
438 + case BCM47XX_BOARD_NETGEAR_WNR3500V2VC:
439 + case BCM47XX_BOARD_NETGEAR_WNR834BV2:
440 + /* Netgear: checksum is @ 0x003AFFF8 for 4M flash or checksum
441 + * is @ 0x007AFFF8 for 8M flash
442 + */
443 + custom_data_size = mtd->erasesize;
444 +
445 + bcm47xx_parts[3].offset = mtd->size - roundup(NVRAM_SPACE, mtd->erasesize);
446 + bcm47xx_parts[3].size = roundup(NVRAM_SPACE, mtd->erasesize);
447 +
448 + /* Place CFE board_data into a partition */
449 + bcm47xx_parts[4].name = "board_data";
450 + bcm47xx_parts[4].offset = bcm47xx_parts[3].offset - custom_data_size;
451 + bcm47xx_parts[4].size = custom_data_size;
452 + break;
453 +
454 + default:
455 + if (is_simpletech_simpleshare()) {
456 + /* Fixup Simpletech Simple share nvram */
457 +
458 + pr_notice("Setting up simpletech nvram\n");
459 + custom_data_size = mtd->erasesize;
460 +
461 + bcm47xx_parts[3].offset = mtd->size - roundup(NVRAM_SPACE, mtd->erasesize) * 2;
462 + bcm47xx_parts[3].size = roundup(NVRAM_SPACE, mtd->erasesize);
463 +
464 + /* Place backup nvram into a partition */
465 + bcm47xx_parts[4].name = "nvram_copy";
466 + bcm47xx_parts[4].offset = mtd->size - roundup(NVRAM_SPACE, mtd->erasesize);
467 + bcm47xx_parts[4].size = roundup(NVRAM_SPACE, mtd->erasesize);
468 + } else {
469 + bcm47xx_parts[3].offset = mtd->size - roundup(NVRAM_SPACE, mtd->erasesize);
470 + bcm47xx_parts[3].size = roundup(NVRAM_SPACE, mtd->erasesize);
471 + }
472 + }
473 +
474 + } else {
475 + /* nvram (old 128kb config partition on netgear wgt634u) */
476 + bcm47xx_parts[3].offset = bcm47xx_parts[0].size;
477 + bcm47xx_parts[3].size = roundup(NVRAM_SPACE, mtd->erasesize);
478 + }
479 +
480 + /* dual image offset*/
481 + pr_notice("Looking for dual image\n");
482 + dual_image_offset = find_dual_image_off(mtd);
483 + /* linux (kernel and rootfs) */
484 + if (cfe_size != 384 * 1024) {
485 + if (is_simpletech_simpleshare()) {
486 + bcm47xx_parts[1].offset = bcm47xx_parts[0].size;
487 + bcm47xx_parts[1].size = bcm47xx_parts[4].offset - dual_image_offset -
488 + bcm47xx_parts[1].offset - custom_data_size;
489 + } else {
490 + bcm47xx_parts[1].offset = bcm47xx_parts[0].size;
491 + bcm47xx_parts[1].size = bcm47xx_parts[3].offset - dual_image_offset -
492 + bcm47xx_parts[1].offset - custom_data_size;
493 + }
494 + } else {
495 + /* do not count the elf loader, which is on one block */
496 + bcm47xx_parts[1].offset = bcm47xx_parts[0].size +
497 + bcm47xx_parts[3].size + mtd->erasesize;
498 + bcm47xx_parts[1].size = mtd->size -
499 + bcm47xx_parts[0].size -
500 + (2*bcm47xx_parts[3].size) -
501 + mtd->erasesize - custom_data_size;
502 + }
503 +
504 + /* find and size rootfs */
505 + find_root(mtd, &bcm47xx_parts[2]);
506 + bcm47xx_parts[2].size = mtd->size - dual_image_offset -
507 + bcm47xx_parts[2].offset -
508 + bcm47xx_parts[3].size - custom_data_size;
509 + *pparts = bcm47xx_parts;
510 + return bcm47xx_parts[4].name == NULL ? 4 : 5;
511 +}
512 +
513 +static struct mtd_part_parser bcm47xx_parser = {
514 + .owner = THIS_MODULE,
515 + .parse_fn = parse_bcm47xx_partitions,
516 + .name = "bcm47xx",
517 +};
518 +
519 +static int __init bcm47xx_parser_init(void)
520 +{
521 + return register_mtd_parser(&bcm47xx_parser);
522 +}
523 +
524 +static void __exit bcm47xx_parser_exit(void)
525 +{
526 + deregister_mtd_parser(&bcm47xx_parser);
527 +}
528 +
529 +module_init(bcm47xx_parser_init);
530 +module_exit(bcm47xx_parser_exit);
531 +
532 +MODULE_LICENSE("GPL");
533 +MODULE_DESCRIPTION("Parsing code for flash partitions on bcm47xx SoCs");