blob: e388e69d853e9a68d5d17ec170a14d07da68583f [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>
Hauke Mehrtens111bd982012-12-26 19:51:14 +000017#include <bcm47xx_nvram.h>
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020018
19/* 10 parts were found on sflash on Netgear WNDR4500 */
20#define BCM47XXPART_MAX_PARTS 12
21
Rafał Miłecki5ca10882013-03-07 09:02:38 +010022/*
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 */
Rafał Miłecki4f8aaf72013-12-21 19:39:11 +010026#define BCM47XXPART_BYTES_TO_READ 0x4e8
Rafał Miłecki5ca10882013-03-07 09:02:38 +010027
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020028/* Magics */
29#define BOARD_DATA_MAGIC 0x5246504D /* MPFR */
Rafał Miłeckif0501e82013-12-21 19:39:12 +010030#define BOARD_DATA_MAGIC2 0xBD0D0BBD
Rafał Miłecki4f8aaf72013-12-21 19:39:11 +010031#define CFE_MAGIC 0x43464531 /* 1EFC */
Rafał Miłecki33094c72013-10-21 22:35:34 +020032#define FACTORY_MAGIC 0x59544346 /* FCTY */
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020033#define POT_MAGIC1 0x54544f50 /* POTT */
34#define POT_MAGIC2 0x504f /* OP */
35#define ML_MAGIC1 0x39685a42
36#define ML_MAGIC2 0x26594131
37#define TRX_MAGIC 0x30524448
Rafał Miłecki020c6bc2013-10-21 22:34:37 +020038#define SQSH_MAGIC 0x71736873 /* shsq */
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020039
40struct trx_header {
41 uint32_t magic;
42 uint32_t length;
43 uint32_t crc32;
44 uint16_t flags;
45 uint16_t version;
46 uint32_t offset[3];
47} __packed;
48
49static void bcm47xxpart_add_part(struct mtd_partition *part, char *name,
50 u64 offset, uint32_t mask_flags)
51{
52 part->name = name;
53 part->offset = offset;
54 part->mask_flags = mask_flags;
55}
56
57static int bcm47xxpart_parse(struct mtd_info *master,
58 struct mtd_partition **pparts,
59 struct mtd_part_parser_data *data)
60{
61 struct mtd_partition *parts;
62 uint8_t i, curr_part = 0;
63 uint32_t *buf;
64 size_t bytes_read;
65 uint32_t offset;
Hauke Mehrtens25bad1d2013-01-24 17:39:58 +010066 uint32_t blocksize = master->erasesize;
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020067 struct trx_header *trx;
Rafał Miłecki396afe52013-01-06 16:08:36 +010068 int trx_part = -1;
69 int last_trx_part = -1;
Rafał Miłecki91d542f2013-03-07 09:02:39 +010070 int possible_nvram_sizes[] = { 0x8000, 0xF000, 0x10000, };
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020071
Hauke Mehrtens25bad1d2013-01-24 17:39:58 +010072 if (blocksize <= 0x10000)
73 blocksize = 0x10000;
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020074
75 /* Alloc */
76 parts = kzalloc(sizeof(struct mtd_partition) * BCM47XXPART_MAX_PARTS,
77 GFP_KERNEL);
Hauke Mehrtens99b1d182013-10-13 22:53:49 +020078 if (!parts)
79 return -ENOMEM;
80
Rafał Miłecki5ca10882013-03-07 09:02:38 +010081 buf = kzalloc(BCM47XXPART_BYTES_TO_READ, GFP_KERNEL);
Hauke Mehrtens99b1d182013-10-13 22:53:49 +020082 if (!buf) {
83 kfree(parts);
84 return -ENOMEM;
85 }
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020086
87 /* Parse block by block looking for magics */
88 for (offset = 0; offset <= master->size - blocksize;
89 offset += blocksize) {
90 /* Nothing more in higher memory */
91 if (offset >= 0x2000000)
92 break;
93
Rafał Miłecki00b79862014-02-26 14:02:06 +010094 if (curr_part >= BCM47XXPART_MAX_PARTS) {
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020095 pr_warn("Reached maximum number of partitions, scanning stopped!\n");
96 break;
97 }
98
99 /* Read beginning of the block */
Rafał Miłecki5ca10882013-03-07 09:02:38 +0100100 if (mtd_read(master, offset, BCM47XXPART_BYTES_TO_READ,
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200101 &bytes_read, (uint8_t *)buf) < 0) {
102 pr_err("mtd_read error while parsing (offset: 0x%X)!\n",
103 offset);
104 continue;
105 }
106
Rafał Miłecki4f8aaf72013-12-21 19:39:11 +0100107 /* Magic or small NVRAM at 0x400 */
108 if ((buf[0x4e0 / 4] == CFE_MAGIC && buf[0x4e4 / 4] == CFE_MAGIC) ||
109 (buf[0x400 / 4] == NVRAM_HEADER)) {
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200110 bcm47xxpart_add_part(&parts[curr_part++], "boot",
111 offset, MTD_WRITEABLE);
112 continue;
113 }
114
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200115 /*
116 * board_data starts with board_id which differs across boards,
117 * but we can use 'MPFR' (hopefully) magic at 0x100
118 */
119 if (buf[0x100 / 4] == BOARD_DATA_MAGIC) {
120 bcm47xxpart_add_part(&parts[curr_part++], "board_data",
121 offset, MTD_WRITEABLE);
122 continue;
123 }
124
Rafał Miłecki33094c72013-10-21 22:35:34 +0200125 /* Found on Huawei E970 */
126 if (buf[0x000 / 4] == FACTORY_MAGIC) {
127 bcm47xxpart_add_part(&parts[curr_part++], "factory",
128 offset, MTD_WRITEABLE);
129 continue;
130 }
131
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200132 /* POT(TOP) */
133 if (buf[0x000 / 4] == POT_MAGIC1 &&
134 (buf[0x004 / 4] & 0xFFFF) == POT_MAGIC2) {
135 bcm47xxpart_add_part(&parts[curr_part++], "POT", offset,
136 MTD_WRITEABLE);
137 continue;
138 }
139
140 /* ML */
141 if (buf[0x010 / 4] == ML_MAGIC1 &&
142 buf[0x014 / 4] == ML_MAGIC2) {
143 bcm47xxpart_add_part(&parts[curr_part++], "ML", offset,
144 MTD_WRITEABLE);
145 continue;
146 }
147
148 /* TRX */
149 if (buf[0x000 / 4] == TRX_MAGIC) {
150 trx = (struct trx_header *)buf;
151
Rafał Miłecki396afe52013-01-06 16:08:36 +0100152 trx_part = curr_part;
153 bcm47xxpart_add_part(&parts[curr_part++], "firmware",
154 offset, 0);
155
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200156 i = 0;
157 /* We have LZMA loader if offset[2] points to sth */
158 if (trx->offset[2]) {
159 bcm47xxpart_add_part(&parts[curr_part++],
160 "loader",
161 offset + trx->offset[i],
162 0);
163 i++;
164 }
165
166 bcm47xxpart_add_part(&parts[curr_part++], "linux",
167 offset + trx->offset[i], 0);
168 i++;
169
170 /*
171 * Pure rootfs size is known and can be calculated as:
172 * trx->length - trx->offset[i]. We don't fill it as
173 * we want to have jffs2 (overlay) in the same mtd.
174 */
175 bcm47xxpart_add_part(&parts[curr_part++], "rootfs",
176 offset + trx->offset[i], 0);
177 i++;
178
Rafał Miłecki396afe52013-01-06 16:08:36 +0100179 last_trx_part = curr_part - 1;
180
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200181 /*
182 * We have whole TRX scanned, skip to the next part. Use
183 * roundown (not roundup), as the loop will increase
184 * offset in next step.
185 */
186 offset = rounddown(offset + trx->length, blocksize);
187 continue;
188 }
Rafał Miłecki020c6bc2013-10-21 22:34:37 +0200189
190 /* Squashfs on devices not using TRX */
191 if (buf[0x000 / 4] == SQSH_MAGIC) {
192 bcm47xxpart_add_part(&parts[curr_part++], "rootfs",
193 offset, 0);
194 continue;
195 }
Rafał Miłeckif0501e82013-12-21 19:39:12 +0100196
197 /* Read middle of the block */
198 if (mtd_read(master, offset + 0x8000, 0x4,
199 &bytes_read, (uint8_t *)buf) < 0) {
200 pr_err("mtd_read error while parsing (offset: 0x%X)!\n",
201 offset);
202 continue;
203 }
204
205 /* Some devices (ex. WNDR3700v3) don't have a standard 'MPFR' */
206 if (buf[0x000 / 4] == BOARD_DATA_MAGIC2) {
207 bcm47xxpart_add_part(&parts[curr_part++], "board_data",
208 offset, MTD_WRITEABLE);
209 continue;
210 }
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200211 }
Rafał Miłecki91d542f2013-03-07 09:02:39 +0100212
213 /* Look for NVRAM at the end of the last block. */
214 for (i = 0; i < ARRAY_SIZE(possible_nvram_sizes); i++) {
Rafał Miłecki00b79862014-02-26 14:02:06 +0100215 if (curr_part >= BCM47XXPART_MAX_PARTS) {
Rafał Miłecki91d542f2013-03-07 09:02:39 +0100216 pr_warn("Reached maximum number of partitions, scanning stopped!\n");
217 break;
218 }
219
220 offset = master->size - possible_nvram_sizes[i];
221 if (mtd_read(master, offset, 0x4, &bytes_read,
222 (uint8_t *)buf) < 0) {
223 pr_err("mtd_read error while reading at offset 0x%X!\n",
224 offset);
225 continue;
226 }
227
228 /* Standard NVRAM */
229 if (buf[0] == NVRAM_HEADER) {
230 bcm47xxpart_add_part(&parts[curr_part++], "nvram",
231 master->size - blocksize, 0);
232 break;
233 }
234 }
235
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200236 kfree(buf);
237
238 /*
239 * Assume that partitions end at the beginning of the one they are
240 * followed by.
241 */
Rafał Miłecki648bdbe2013-01-06 16:08:35 +0100242 for (i = 0; i < curr_part; i++) {
243 u64 next_part_offset = (i < curr_part - 1) ?
244 parts[i + 1].offset : master->size;
245
246 parts[i].size = next_part_offset - parts[i].offset;
Rafał Miłecki396afe52013-01-06 16:08:36 +0100247 if (i == last_trx_part && trx_part >= 0)
248 parts[trx_part].size = next_part_offset -
249 parts[trx_part].offset;
Rafał Miłecki648bdbe2013-01-06 16:08:35 +0100250 }
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200251
252 *pparts = parts;
253 return curr_part;
254};
255
256static struct mtd_part_parser bcm47xxpart_mtd_parser = {
257 .owner = THIS_MODULE,
258 .parse_fn = bcm47xxpart_parse,
259 .name = "bcm47xxpart",
260};
261
262static int __init bcm47xxpart_init(void)
263{
Axel Lin6e14a612013-12-01 19:01:06 +0800264 register_mtd_parser(&bcm47xxpart_mtd_parser);
265 return 0;
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200266}
267
268static void __exit bcm47xxpart_exit(void)
269{
270 deregister_mtd_parser(&bcm47xxpart_mtd_parser);
271}
272
273module_init(bcm47xxpart_init);
274module_exit(bcm47xxpart_exit);
275
276MODULE_LICENSE("GPL");
277MODULE_DESCRIPTION("MTD partitioning for BCM47XX flash memories");