Merge tag 'for-linus-20130301' of git://git.infradead.org/linux-mtd
[linux-3.10.git] / drivers / mtd / bcm47xxpart.c
1 /*
2  * BCM47XX MTD partitioning
3  *
4  * Copyright © 2012 Rafał Miłecki <zajec5@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  */
11
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/mtd/mtd.h>
16 #include <linux/mtd/partitions.h>
17 #include <bcm47xx_nvram.h>
18
19 /* 10 parts were found on sflash on Netgear WNDR4500 */
20 #define BCM47XXPART_MAX_PARTS           12
21
22 /* Magics */
23 #define BOARD_DATA_MAGIC                0x5246504D      /* MPFR */
24 #define POT_MAGIC1                      0x54544f50      /* POTT */
25 #define POT_MAGIC2                      0x504f          /* OP */
26 #define ML_MAGIC1                       0x39685a42
27 #define ML_MAGIC2                       0x26594131
28 #define TRX_MAGIC                       0x30524448
29
30 struct trx_header {
31         uint32_t magic;
32         uint32_t length;
33         uint32_t crc32;
34         uint16_t flags;
35         uint16_t version;
36         uint32_t offset[3];
37 } __packed;
38
39 static void bcm47xxpart_add_part(struct mtd_partition *part, char *name,
40                                  u64 offset, uint32_t mask_flags)
41 {
42         part->name = name;
43         part->offset = offset;
44         part->mask_flags = mask_flags;
45 }
46
47 static int bcm47xxpart_parse(struct mtd_info *master,
48                              struct mtd_partition **pparts,
49                              struct mtd_part_parser_data *data)
50 {
51         struct mtd_partition *parts;
52         uint8_t i, curr_part = 0;
53         uint32_t *buf;
54         size_t bytes_read;
55         uint32_t offset;
56         uint32_t blocksize = master->erasesize;
57         struct trx_header *trx;
58         int trx_part = -1;
59         int last_trx_part = -1;
60         int max_bytes_to_read = 0x8004;
61
62         if (blocksize <= 0x10000)
63                 blocksize = 0x10000;
64         if (blocksize == 0x20000)
65                 max_bytes_to_read = 0x18004;
66
67         /* Alloc */
68         parts = kzalloc(sizeof(struct mtd_partition) * BCM47XXPART_MAX_PARTS,
69                         GFP_KERNEL);
70         buf = kzalloc(max_bytes_to_read, GFP_KERNEL);
71
72         /* Parse block by block looking for magics */
73         for (offset = 0; offset <= master->size - blocksize;
74              offset += blocksize) {
75                 /* Nothing more in higher memory */
76                 if (offset >= 0x2000000)
77                         break;
78
79                 if (curr_part > BCM47XXPART_MAX_PARTS) {
80                         pr_warn("Reached maximum number of partitions, scanning stopped!\n");
81                         break;
82                 }
83
84                 /* Read beginning of the block */
85                 if (mtd_read(master, offset, max_bytes_to_read,
86                              &bytes_read, (uint8_t *)buf) < 0) {
87                         pr_err("mtd_read error while parsing (offset: 0x%X)!\n",
88                                offset);
89                         continue;
90                 }
91
92                 /* CFE has small NVRAM at 0x400 */
93                 if (buf[0x400 / 4] == NVRAM_HEADER) {
94                         bcm47xxpart_add_part(&parts[curr_part++], "boot",
95                                              offset, MTD_WRITEABLE);
96                         continue;
97                 }
98
99                 /* Standard NVRAM */
100                 if (buf[0x000 / 4] == NVRAM_HEADER ||
101                     buf[0x1000 / 4] == NVRAM_HEADER ||
102                     buf[0x8000 / 4] == NVRAM_HEADER ||
103                     (blocksize == 0x20000 && (
104                       buf[0x10000 / 4] == NVRAM_HEADER ||
105                       buf[0x11000 / 4] == NVRAM_HEADER ||
106                       buf[0x18000 / 4] == NVRAM_HEADER))) {
107                         bcm47xxpart_add_part(&parts[curr_part++], "nvram",
108                                              offset, 0);
109                         offset = rounddown(offset, blocksize);
110                         continue;
111                 }
112
113                 /*
114                  * board_data starts with board_id which differs across boards,
115                  * but we can use 'MPFR' (hopefully) magic at 0x100
116                  */
117                 if (buf[0x100 / 4] == BOARD_DATA_MAGIC) {
118                         bcm47xxpart_add_part(&parts[curr_part++], "board_data",
119                                              offset, MTD_WRITEABLE);
120                         continue;
121                 }
122
123                 /* POT(TOP) */
124                 if (buf[0x000 / 4] == POT_MAGIC1 &&
125                     (buf[0x004 / 4] & 0xFFFF) == POT_MAGIC2) {
126                         bcm47xxpart_add_part(&parts[curr_part++], "POT", offset,
127                                              MTD_WRITEABLE);
128                         continue;
129                 }
130
131                 /* ML */
132                 if (buf[0x010 / 4] == ML_MAGIC1 &&
133                     buf[0x014 / 4] == ML_MAGIC2) {
134                         bcm47xxpart_add_part(&parts[curr_part++], "ML", offset,
135                                              MTD_WRITEABLE);
136                         continue;
137                 }
138
139                 /* TRX */
140                 if (buf[0x000 / 4] == TRX_MAGIC) {
141                         trx = (struct trx_header *)buf;
142
143                         trx_part = curr_part;
144                         bcm47xxpart_add_part(&parts[curr_part++], "firmware",
145                                              offset, 0);
146
147                         i = 0;
148                         /* We have LZMA loader if offset[2] points to sth */
149                         if (trx->offset[2]) {
150                                 bcm47xxpart_add_part(&parts[curr_part++],
151                                                      "loader",
152                                                      offset + trx->offset[i],
153                                                      0);
154                                 i++;
155                         }
156
157                         bcm47xxpart_add_part(&parts[curr_part++], "linux",
158                                              offset + trx->offset[i], 0);
159                         i++;
160
161                         /*
162                          * Pure rootfs size is known and can be calculated as:
163                          * trx->length - trx->offset[i]. We don't fill it as
164                          * we want to have jffs2 (overlay) in the same mtd.
165                          */
166                         bcm47xxpart_add_part(&parts[curr_part++], "rootfs",
167                                              offset + trx->offset[i], 0);
168                         i++;
169
170                         last_trx_part = curr_part - 1;
171
172                         /*
173                          * We have whole TRX scanned, skip to the next part. Use
174                          * roundown (not roundup), as the loop will increase
175                          * offset in next step.
176                          */
177                         offset = rounddown(offset + trx->length, blocksize);
178                         continue;
179                 }
180         }
181         kfree(buf);
182
183         /*
184          * Assume that partitions end at the beginning of the one they are
185          * followed by.
186          */
187         for (i = 0; i < curr_part; i++) {
188                 u64 next_part_offset = (i < curr_part - 1) ?
189                                        parts[i + 1].offset : master->size;
190
191                 parts[i].size = next_part_offset - parts[i].offset;
192                 if (i == last_trx_part && trx_part >= 0)
193                         parts[trx_part].size = next_part_offset -
194                                                parts[trx_part].offset;
195         }
196
197         *pparts = parts;
198         return curr_part;
199 };
200
201 static struct mtd_part_parser bcm47xxpart_mtd_parser = {
202         .owner = THIS_MODULE,
203         .parse_fn = bcm47xxpart_parse,
204         .name = "bcm47xxpart",
205 };
206
207 static int __init bcm47xxpart_init(void)
208 {
209         return register_mtd_parser(&bcm47xxpart_mtd_parser);
210 }
211
212 static void __exit bcm47xxpart_exit(void)
213 {
214         deregister_mtd_parser(&bcm47xxpart_mtd_parser);
215 }
216
217 module_init(bcm47xxpart_init);
218 module_exit(bcm47xxpart_exit);
219
220 MODULE_LICENSE("GPL");
221 MODULE_DESCRIPTION("MTD partitioning for BCM47XX flash memories");