]> Pileus Git - ~andy/linux/blob - drivers/mtd/bcm47xxpart.c
Input: synaptics - add manual min/max quirk
[~andy/linux] / 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 /*
23  * Amount of bytes we read when analyzing each block of flash memory.
24  * Set it big enough to allow detecting partition and reading important data.
25  */
26 #define BCM47XXPART_BYTES_TO_READ       0x404
27
28 /* Magics */
29 #define BOARD_DATA_MAGIC                0x5246504D      /* MPFR */
30 #define FACTORY_MAGIC                   0x59544346      /* FCTY */
31 #define POT_MAGIC1                      0x54544f50      /* POTT */
32 #define POT_MAGIC2                      0x504f          /* OP */
33 #define ML_MAGIC1                       0x39685a42
34 #define ML_MAGIC2                       0x26594131
35 #define TRX_MAGIC                       0x30524448
36 #define SQSH_MAGIC                      0x71736873      /* shsq */
37
38 struct trx_header {
39         uint32_t magic;
40         uint32_t length;
41         uint32_t crc32;
42         uint16_t flags;
43         uint16_t version;
44         uint32_t offset[3];
45 } __packed;
46
47 static void bcm47xxpart_add_part(struct mtd_partition *part, char *name,
48                                  u64 offset, uint32_t mask_flags)
49 {
50         part->name = name;
51         part->offset = offset;
52         part->mask_flags = mask_flags;
53 }
54
55 static int bcm47xxpart_parse(struct mtd_info *master,
56                              struct mtd_partition **pparts,
57                              struct mtd_part_parser_data *data)
58 {
59         struct mtd_partition *parts;
60         uint8_t i, curr_part = 0;
61         uint32_t *buf;
62         size_t bytes_read;
63         uint32_t offset;
64         uint32_t blocksize = master->erasesize;
65         struct trx_header *trx;
66         int trx_part = -1;
67         int last_trx_part = -1;
68         int possible_nvram_sizes[] = { 0x8000, 0xF000, 0x10000, };
69
70         if (blocksize <= 0x10000)
71                 blocksize = 0x10000;
72
73         /* Alloc */
74         parts = kzalloc(sizeof(struct mtd_partition) * BCM47XXPART_MAX_PARTS,
75                         GFP_KERNEL);
76         if (!parts)
77                 return -ENOMEM;
78
79         buf = kzalloc(BCM47XXPART_BYTES_TO_READ, GFP_KERNEL);
80         if (!buf) {
81                 kfree(parts);
82                 return -ENOMEM;
83         }
84
85         /* Parse block by block looking for magics */
86         for (offset = 0; offset <= master->size - blocksize;
87              offset += blocksize) {
88                 /* Nothing more in higher memory */
89                 if (offset >= 0x2000000)
90                         break;
91
92                 if (curr_part > BCM47XXPART_MAX_PARTS) {
93                         pr_warn("Reached maximum number of partitions, scanning stopped!\n");
94                         break;
95                 }
96
97                 /* Read beginning of the block */
98                 if (mtd_read(master, offset, BCM47XXPART_BYTES_TO_READ,
99                              &bytes_read, (uint8_t *)buf) < 0) {
100                         pr_err("mtd_read error while parsing (offset: 0x%X)!\n",
101                                offset);
102                         continue;
103                 }
104
105                 /* CFE has small NVRAM at 0x400 */
106                 if (buf[0x400 / 4] == NVRAM_HEADER) {
107                         bcm47xxpart_add_part(&parts[curr_part++], "boot",
108                                              offset, MTD_WRITEABLE);
109                         continue;
110                 }
111
112                 /*
113                  * board_data starts with board_id which differs across boards,
114                  * but we can use 'MPFR' (hopefully) magic at 0x100
115                  */
116                 if (buf[0x100 / 4] == BOARD_DATA_MAGIC) {
117                         bcm47xxpart_add_part(&parts[curr_part++], "board_data",
118                                              offset, MTD_WRITEABLE);
119                         continue;
120                 }
121
122                 /* Found on Huawei E970 */
123                 if (buf[0x000 / 4] == FACTORY_MAGIC) {
124                         bcm47xxpart_add_part(&parts[curr_part++], "factory",
125                                              offset, MTD_WRITEABLE);
126                         continue;
127                 }
128
129                 /* POT(TOP) */
130                 if (buf[0x000 / 4] == POT_MAGIC1 &&
131                     (buf[0x004 / 4] & 0xFFFF) == POT_MAGIC2) {
132                         bcm47xxpart_add_part(&parts[curr_part++], "POT", offset,
133                                              MTD_WRITEABLE);
134                         continue;
135                 }
136
137                 /* ML */
138                 if (buf[0x010 / 4] == ML_MAGIC1 &&
139                     buf[0x014 / 4] == ML_MAGIC2) {
140                         bcm47xxpart_add_part(&parts[curr_part++], "ML", offset,
141                                              MTD_WRITEABLE);
142                         continue;
143                 }
144
145                 /* TRX */
146                 if (buf[0x000 / 4] == TRX_MAGIC) {
147                         trx = (struct trx_header *)buf;
148
149                         trx_part = curr_part;
150                         bcm47xxpart_add_part(&parts[curr_part++], "firmware",
151                                              offset, 0);
152
153                         i = 0;
154                         /* We have LZMA loader if offset[2] points to sth */
155                         if (trx->offset[2]) {
156                                 bcm47xxpart_add_part(&parts[curr_part++],
157                                                      "loader",
158                                                      offset + trx->offset[i],
159                                                      0);
160                                 i++;
161                         }
162
163                         bcm47xxpart_add_part(&parts[curr_part++], "linux",
164                                              offset + trx->offset[i], 0);
165                         i++;
166
167                         /*
168                          * Pure rootfs size is known and can be calculated as:
169                          * trx->length - trx->offset[i]. We don't fill it as
170                          * we want to have jffs2 (overlay) in the same mtd.
171                          */
172                         bcm47xxpart_add_part(&parts[curr_part++], "rootfs",
173                                              offset + trx->offset[i], 0);
174                         i++;
175
176                         last_trx_part = curr_part - 1;
177
178                         /*
179                          * We have whole TRX scanned, skip to the next part. Use
180                          * roundown (not roundup), as the loop will increase
181                          * offset in next step.
182                          */
183                         offset = rounddown(offset + trx->length, blocksize);
184                         continue;
185                 }
186
187                 /* Squashfs on devices not using TRX */
188                 if (buf[0x000 / 4] == SQSH_MAGIC) {
189                         bcm47xxpart_add_part(&parts[curr_part++], "rootfs",
190                                              offset, 0);
191                         continue;
192                 }
193         }
194
195         /* Look for NVRAM at the end of the last block. */
196         for (i = 0; i < ARRAY_SIZE(possible_nvram_sizes); i++) {
197                 if (curr_part > BCM47XXPART_MAX_PARTS) {
198                         pr_warn("Reached maximum number of partitions, scanning stopped!\n");
199                         break;
200                 }
201
202                 offset = master->size - possible_nvram_sizes[i];
203                 if (mtd_read(master, offset, 0x4, &bytes_read,
204                              (uint8_t *)buf) < 0) {
205                         pr_err("mtd_read error while reading at offset 0x%X!\n",
206                                offset);
207                         continue;
208                 }
209
210                 /* Standard NVRAM */
211                 if (buf[0] == NVRAM_HEADER) {
212                         bcm47xxpart_add_part(&parts[curr_part++], "nvram",
213                                              master->size - blocksize, 0);
214                         break;
215                 }
216         }
217
218         kfree(buf);
219
220         /*
221          * Assume that partitions end at the beginning of the one they are
222          * followed by.
223          */
224         for (i = 0; i < curr_part; i++) {
225                 u64 next_part_offset = (i < curr_part - 1) ?
226                                        parts[i + 1].offset : master->size;
227
228                 parts[i].size = next_part_offset - parts[i].offset;
229                 if (i == last_trx_part && trx_part >= 0)
230                         parts[trx_part].size = next_part_offset -
231                                                parts[trx_part].offset;
232         }
233
234         *pparts = parts;
235         return curr_part;
236 };
237
238 static struct mtd_part_parser bcm47xxpart_mtd_parser = {
239         .owner = THIS_MODULE,
240         .parse_fn = bcm47xxpart_parse,
241         .name = "bcm47xxpart",
242 };
243
244 static int __init bcm47xxpart_init(void)
245 {
246         return register_mtd_parser(&bcm47xxpart_mtd_parser);
247 }
248
249 static void __exit bcm47xxpart_exit(void)
250 {
251         deregister_mtd_parser(&bcm47xxpart_mtd_parser);
252 }
253
254 module_init(bcm47xxpart_init);
255 module_exit(bcm47xxpart_exit);
256
257 MODULE_LICENSE("GPL");
258 MODULE_DESCRIPTION("MTD partitioning for BCM47XX flash memories");