blob: 26a4a4a4e8ab043b2f6695c4b91e63bce615305b [file] [log] [blame]
Rafał Miłecki3cf7f132012-08-30 07:41:16 +02001/*
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>
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020017
Rafał Miłecki59af5c72014-10-02 11:48:46 +020018/*
19 * NAND flash on Netgear R6250 was verified to contain 15 partitions.
20 * This will result in allocating too big array for some old devices, but the
21 * memory will be freed soon anyway (see mtd_device_parse_register).
22 */
23#define BCM47XXPART_MAX_PARTS 20
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020024
Rafał Miłecki5ca10882013-03-07 09:02:38 +010025/*
26 * Amount of bytes we read when analyzing each block of flash memory.
27 * Set it big enough to allow detecting partition and reading important data.
28 */
Rafał Miłecki4f8aaf72013-12-21 19:39:11 +010029#define BCM47XXPART_BYTES_TO_READ 0x4e8
Rafał Miłecki5ca10882013-03-07 09:02:38 +010030
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020031/* Magics */
32#define BOARD_DATA_MAGIC 0x5246504D /* MPFR */
Rafał Miłeckif0501e82013-12-21 19:39:12 +010033#define BOARD_DATA_MAGIC2 0xBD0D0BBD
Rafał Miłecki4f8aaf72013-12-21 19:39:11 +010034#define CFE_MAGIC 0x43464531 /* 1EFC */
Rafał Miłecki33094c72013-10-21 22:35:34 +020035#define FACTORY_MAGIC 0x59544346 /* FCTY */
Rafał Miłecki9e3afa52014-02-28 18:02:01 +010036#define NVRAM_HEADER 0x48534C46 /* FLSH */
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020037#define POT_MAGIC1 0x54544f50 /* POTT */
38#define POT_MAGIC2 0x504f /* OP */
39#define ML_MAGIC1 0x39685a42
40#define ML_MAGIC2 0x26594131
41#define TRX_MAGIC 0x30524448
Rafał Miłecki020c6bc2013-10-21 22:34:37 +020042#define SQSH_MAGIC 0x71736873 /* shsq */
Rafał Miłeckibd10c262014-12-01 18:50:20 +010043#define UBI_EC_MAGIC 0x23494255 /* UBI# */
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020044
45struct trx_header {
46 uint32_t magic;
47 uint32_t length;
48 uint32_t crc32;
49 uint16_t flags;
50 uint16_t version;
51 uint32_t offset[3];
52} __packed;
53
Rafał Miłeckibd10c262014-12-01 18:50:20 +010054static void bcm47xxpart_add_part(struct mtd_partition *part, const char *name,
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020055 u64 offset, uint32_t mask_flags)
56{
57 part->name = name;
58 part->offset = offset;
59 part->mask_flags = mask_flags;
60}
61
Rafał Miłeckibd10c262014-12-01 18:50:20 +010062static const char *bcm47xxpart_trx_data_part_name(struct mtd_info *master,
63 size_t offset)
64{
65 uint32_t buf;
66 size_t bytes_read;
67
68 if (mtd_read(master, offset, sizeof(buf), &bytes_read,
69 (uint8_t *)&buf) < 0) {
70 pr_err("mtd_read error while parsing (offset: 0x%X)!\n",
71 offset);
72 goto out_default;
73 }
74
75 if (buf == UBI_EC_MAGIC)
76 return "ubi";
77
78out_default:
79 return "rootfs";
80}
81
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020082static int bcm47xxpart_parse(struct mtd_info *master,
83 struct mtd_partition **pparts,
84 struct mtd_part_parser_data *data)
85{
86 struct mtd_partition *parts;
87 uint8_t i, curr_part = 0;
88 uint32_t *buf;
89 size_t bytes_read;
90 uint32_t offset;
Hauke Mehrtens25bad1d2013-01-24 17:39:58 +010091 uint32_t blocksize = master->erasesize;
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020092 struct trx_header *trx;
Rafał Miłecki396afe52013-01-06 16:08:36 +010093 int trx_part = -1;
94 int last_trx_part = -1;
Rafał Miłecki91d542f2013-03-07 09:02:39 +010095 int possible_nvram_sizes[] = { 0x8000, 0xF000, 0x10000, };
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020096
Hauke Mehrtens25bad1d2013-01-24 17:39:58 +010097 if (blocksize <= 0x10000)
98 blocksize = 0x10000;
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020099
100 /* Alloc */
101 parts = kzalloc(sizeof(struct mtd_partition) * BCM47XXPART_MAX_PARTS,
102 GFP_KERNEL);
Hauke Mehrtens99b1d182013-10-13 22:53:49 +0200103 if (!parts)
104 return -ENOMEM;
105
Rafał Miłecki5ca10882013-03-07 09:02:38 +0100106 buf = kzalloc(BCM47XXPART_BYTES_TO_READ, GFP_KERNEL);
Hauke Mehrtens99b1d182013-10-13 22:53:49 +0200107 if (!buf) {
108 kfree(parts);
109 return -ENOMEM;
110 }
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200111
112 /* Parse block by block looking for magics */
113 for (offset = 0; offset <= master->size - blocksize;
114 offset += blocksize) {
115 /* Nothing more in higher memory */
116 if (offset >= 0x2000000)
117 break;
118
Rafał Miłecki00b79862014-02-26 14:02:06 +0100119 if (curr_part >= BCM47XXPART_MAX_PARTS) {
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200120 pr_warn("Reached maximum number of partitions, scanning stopped!\n");
121 break;
122 }
123
124 /* Read beginning of the block */
Rafał Miłecki5ca10882013-03-07 09:02:38 +0100125 if (mtd_read(master, offset, BCM47XXPART_BYTES_TO_READ,
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200126 &bytes_read, (uint8_t *)buf) < 0) {
127 pr_err("mtd_read error while parsing (offset: 0x%X)!\n",
128 offset);
129 continue;
130 }
131
Rafał Miłecki4f8aaf72013-12-21 19:39:11 +0100132 /* Magic or small NVRAM at 0x400 */
133 if ((buf[0x4e0 / 4] == CFE_MAGIC && buf[0x4e4 / 4] == CFE_MAGIC) ||
134 (buf[0x400 / 4] == NVRAM_HEADER)) {
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200135 bcm47xxpart_add_part(&parts[curr_part++], "boot",
136 offset, MTD_WRITEABLE);
137 continue;
138 }
139
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200140 /*
141 * board_data starts with board_id which differs across boards,
142 * but we can use 'MPFR' (hopefully) magic at 0x100
143 */
144 if (buf[0x100 / 4] == BOARD_DATA_MAGIC) {
145 bcm47xxpart_add_part(&parts[curr_part++], "board_data",
146 offset, MTD_WRITEABLE);
147 continue;
148 }
149
Rafał Miłecki33094c72013-10-21 22:35:34 +0200150 /* Found on Huawei E970 */
151 if (buf[0x000 / 4] == FACTORY_MAGIC) {
152 bcm47xxpart_add_part(&parts[curr_part++], "factory",
153 offset, MTD_WRITEABLE);
154 continue;
155 }
156
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200157 /* POT(TOP) */
158 if (buf[0x000 / 4] == POT_MAGIC1 &&
159 (buf[0x004 / 4] & 0xFFFF) == POT_MAGIC2) {
160 bcm47xxpart_add_part(&parts[curr_part++], "POT", offset,
161 MTD_WRITEABLE);
162 continue;
163 }
164
165 /* ML */
166 if (buf[0x010 / 4] == ML_MAGIC1 &&
167 buf[0x014 / 4] == ML_MAGIC2) {
168 bcm47xxpart_add_part(&parts[curr_part++], "ML", offset,
169 MTD_WRITEABLE);
170 continue;
171 }
172
173 /* TRX */
174 if (buf[0x000 / 4] == TRX_MAGIC) {
Rafał Miłecki108ebcd2014-02-26 14:30:34 +0100175 if (BCM47XXPART_MAX_PARTS - curr_part < 4) {
176 pr_warn("Not enough partitions left to register trx, scanning stopped!\n");
177 break;
178 }
179
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200180 trx = (struct trx_header *)buf;
181
Rafał Miłecki396afe52013-01-06 16:08:36 +0100182 trx_part = curr_part;
183 bcm47xxpart_add_part(&parts[curr_part++], "firmware",
184 offset, 0);
185
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200186 i = 0;
187 /* We have LZMA loader if offset[2] points to sth */
188 if (trx->offset[2]) {
189 bcm47xxpart_add_part(&parts[curr_part++],
190 "loader",
191 offset + trx->offset[i],
192 0);
193 i++;
194 }
195
Hauke Mehrtensa1ff7d62014-09-22 00:33:13 +0200196 if (trx->offset[i]) {
197 bcm47xxpart_add_part(&parts[curr_part++],
198 "linux",
199 offset + trx->offset[i],
200 0);
201 i++;
202 }
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200203
204 /*
205 * Pure rootfs size is known and can be calculated as:
206 * trx->length - trx->offset[i]. We don't fill it as
207 * we want to have jffs2 (overlay) in the same mtd.
208 */
Hauke Mehrtensa1ff7d62014-09-22 00:33:13 +0200209 if (trx->offset[i]) {
Rafał Miłeckibd10c262014-12-01 18:50:20 +0100210 const char *name;
211
212 name = bcm47xxpart_trx_data_part_name(master, offset + trx->offset[i]);
Hauke Mehrtensa1ff7d62014-09-22 00:33:13 +0200213 bcm47xxpart_add_part(&parts[curr_part++],
Rafał Miłeckibd10c262014-12-01 18:50:20 +0100214 name,
Hauke Mehrtensa1ff7d62014-09-22 00:33:13 +0200215 offset + trx->offset[i],
216 0);
217 i++;
218 }
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200219
Rafał Miłecki396afe52013-01-06 16:08:36 +0100220 last_trx_part = curr_part - 1;
221
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200222 /*
223 * We have whole TRX scanned, skip to the next part. Use
224 * roundown (not roundup), as the loop will increase
225 * offset in next step.
226 */
227 offset = rounddown(offset + trx->length, blocksize);
228 continue;
229 }
Rafał Miłecki020c6bc2013-10-21 22:34:37 +0200230
231 /* Squashfs on devices not using TRX */
232 if (buf[0x000 / 4] == SQSH_MAGIC) {
233 bcm47xxpart_add_part(&parts[curr_part++], "rootfs",
234 offset, 0);
235 continue;
236 }
Rafał Miłeckif0501e82013-12-21 19:39:12 +0100237
Rafał Miłecki024629f2014-08-18 20:20:27 +0200238 /*
239 * New (ARM?) devices may have NVRAM in some middle block. Last
240 * block will be checked later, so skip it.
241 */
242 if (offset != master->size - blocksize &&
243 buf[0x000 / 4] == NVRAM_HEADER) {
244 bcm47xxpart_add_part(&parts[curr_part++], "nvram",
245 offset, 0);
246 continue;
247 }
248
Rafał Miłeckif0501e82013-12-21 19:39:12 +0100249 /* Read middle of the block */
250 if (mtd_read(master, offset + 0x8000, 0x4,
251 &bytes_read, (uint8_t *)buf) < 0) {
252 pr_err("mtd_read error while parsing (offset: 0x%X)!\n",
253 offset);
254 continue;
255 }
256
257 /* Some devices (ex. WNDR3700v3) don't have a standard 'MPFR' */
258 if (buf[0x000 / 4] == BOARD_DATA_MAGIC2) {
259 bcm47xxpart_add_part(&parts[curr_part++], "board_data",
260 offset, MTD_WRITEABLE);
261 continue;
262 }
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200263 }
Rafał Miłecki91d542f2013-03-07 09:02:39 +0100264
265 /* Look for NVRAM at the end of the last block. */
266 for (i = 0; i < ARRAY_SIZE(possible_nvram_sizes); i++) {
Rafał Miłecki00b79862014-02-26 14:02:06 +0100267 if (curr_part >= BCM47XXPART_MAX_PARTS) {
Rafał Miłecki91d542f2013-03-07 09:02:39 +0100268 pr_warn("Reached maximum number of partitions, scanning stopped!\n");
269 break;
270 }
271
272 offset = master->size - possible_nvram_sizes[i];
273 if (mtd_read(master, offset, 0x4, &bytes_read,
274 (uint8_t *)buf) < 0) {
275 pr_err("mtd_read error while reading at offset 0x%X!\n",
276 offset);
277 continue;
278 }
279
280 /* Standard NVRAM */
281 if (buf[0] == NVRAM_HEADER) {
282 bcm47xxpart_add_part(&parts[curr_part++], "nvram",
283 master->size - blocksize, 0);
284 break;
285 }
286 }
287
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200288 kfree(buf);
289
290 /*
291 * Assume that partitions end at the beginning of the one they are
292 * followed by.
293 */
Rafał Miłecki648bdbe2013-01-06 16:08:35 +0100294 for (i = 0; i < curr_part; i++) {
295 u64 next_part_offset = (i < curr_part - 1) ?
296 parts[i + 1].offset : master->size;
297
298 parts[i].size = next_part_offset - parts[i].offset;
Rafał Miłecki396afe52013-01-06 16:08:36 +0100299 if (i == last_trx_part && trx_part >= 0)
300 parts[trx_part].size = next_part_offset -
301 parts[trx_part].offset;
Rafał Miłecki648bdbe2013-01-06 16:08:35 +0100302 }
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200303
304 *pparts = parts;
305 return curr_part;
306};
307
308static struct mtd_part_parser bcm47xxpart_mtd_parser = {
309 .owner = THIS_MODULE,
310 .parse_fn = bcm47xxpart_parse,
311 .name = "bcm47xxpart",
312};
313
314static int __init bcm47xxpart_init(void)
315{
Axel Lin6e14a612013-12-01 19:01:06 +0800316 register_mtd_parser(&bcm47xxpart_mtd_parser);
317 return 0;
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200318}
319
320static void __exit bcm47xxpart_exit(void)
321{
322 deregister_mtd_parser(&bcm47xxpart_mtd_parser);
323}
324
325module_init(bcm47xxpart_init);
326module_exit(bcm47xxpart_exit);
327
328MODULE_LICENSE("GPL");
329MODULE_DESCRIPTION("MTD partitioning for BCM47XX flash memories");