blob: 877c17c7f5d3ca0af70b2dfb40b030b9e471886f [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łecki4f8aaf72013-12-21 19:39:11 +010030#define CFE_MAGIC 0x43464531 /* 1EFC */
Rafał Miłecki33094c72013-10-21 22:35:34 +020031#define FACTORY_MAGIC 0x59544346 /* FCTY */
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020032#define POT_MAGIC1 0x54544f50 /* POTT */
33#define POT_MAGIC2 0x504f /* OP */
34#define ML_MAGIC1 0x39685a42
35#define ML_MAGIC2 0x26594131
36#define TRX_MAGIC 0x30524448
Rafał Miłecki020c6bc2013-10-21 22:34:37 +020037#define SQSH_MAGIC 0x71736873 /* shsq */
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020038
39struct trx_header {
40 uint32_t magic;
41 uint32_t length;
42 uint32_t crc32;
43 uint16_t flags;
44 uint16_t version;
45 uint32_t offset[3];
46} __packed;
47
48static void bcm47xxpart_add_part(struct mtd_partition *part, char *name,
49 u64 offset, uint32_t mask_flags)
50{
51 part->name = name;
52 part->offset = offset;
53 part->mask_flags = mask_flags;
54}
55
56static int bcm47xxpart_parse(struct mtd_info *master,
57 struct mtd_partition **pparts,
58 struct mtd_part_parser_data *data)
59{
60 struct mtd_partition *parts;
61 uint8_t i, curr_part = 0;
62 uint32_t *buf;
63 size_t bytes_read;
64 uint32_t offset;
Hauke Mehrtens25bad1d2013-01-24 17:39:58 +010065 uint32_t blocksize = master->erasesize;
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020066 struct trx_header *trx;
Rafał Miłecki396afe52013-01-06 16:08:36 +010067 int trx_part = -1;
68 int last_trx_part = -1;
Rafał Miłecki91d542f2013-03-07 09:02:39 +010069 int possible_nvram_sizes[] = { 0x8000, 0xF000, 0x10000, };
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020070
Hauke Mehrtens25bad1d2013-01-24 17:39:58 +010071 if (blocksize <= 0x10000)
72 blocksize = 0x10000;
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020073
74 /* Alloc */
75 parts = kzalloc(sizeof(struct mtd_partition) * BCM47XXPART_MAX_PARTS,
76 GFP_KERNEL);
Hauke Mehrtens99b1d182013-10-13 22:53:49 +020077 if (!parts)
78 return -ENOMEM;
79
Rafał Miłecki5ca10882013-03-07 09:02:38 +010080 buf = kzalloc(BCM47XXPART_BYTES_TO_READ, GFP_KERNEL);
Hauke Mehrtens99b1d182013-10-13 22:53:49 +020081 if (!buf) {
82 kfree(parts);
83 return -ENOMEM;
84 }
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020085
86 /* Parse block by block looking for magics */
87 for (offset = 0; offset <= master->size - blocksize;
88 offset += blocksize) {
89 /* Nothing more in higher memory */
90 if (offset >= 0x2000000)
91 break;
92
93 if (curr_part > BCM47XXPART_MAX_PARTS) {
94 pr_warn("Reached maximum number of partitions, scanning stopped!\n");
95 break;
96 }
97
98 /* Read beginning of the block */
Rafał Miłecki5ca10882013-03-07 09:02:38 +010099 if (mtd_read(master, offset, BCM47XXPART_BYTES_TO_READ,
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200100 &bytes_read, (uint8_t *)buf) < 0) {
101 pr_err("mtd_read error while parsing (offset: 0x%X)!\n",
102 offset);
103 continue;
104 }
105
Rafał Miłecki4f8aaf72013-12-21 19:39:11 +0100106 /* Magic or small NVRAM at 0x400 */
107 if ((buf[0x4e0 / 4] == CFE_MAGIC && buf[0x4e4 / 4] == CFE_MAGIC) ||
108 (buf[0x400 / 4] == NVRAM_HEADER)) {
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200109 bcm47xxpart_add_part(&parts[curr_part++], "boot",
110 offset, MTD_WRITEABLE);
111 continue;
112 }
113
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200114 /*
115 * board_data starts with board_id which differs across boards,
116 * but we can use 'MPFR' (hopefully) magic at 0x100
117 */
118 if (buf[0x100 / 4] == BOARD_DATA_MAGIC) {
119 bcm47xxpart_add_part(&parts[curr_part++], "board_data",
120 offset, MTD_WRITEABLE);
121 continue;
122 }
123
Rafał Miłecki33094c72013-10-21 22:35:34 +0200124 /* Found on Huawei E970 */
125 if (buf[0x000 / 4] == FACTORY_MAGIC) {
126 bcm47xxpart_add_part(&parts[curr_part++], "factory",
127 offset, MTD_WRITEABLE);
128 continue;
129 }
130
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200131 /* POT(TOP) */
132 if (buf[0x000 / 4] == POT_MAGIC1 &&
133 (buf[0x004 / 4] & 0xFFFF) == POT_MAGIC2) {
134 bcm47xxpart_add_part(&parts[curr_part++], "POT", offset,
135 MTD_WRITEABLE);
136 continue;
137 }
138
139 /* ML */
140 if (buf[0x010 / 4] == ML_MAGIC1 &&
141 buf[0x014 / 4] == ML_MAGIC2) {
142 bcm47xxpart_add_part(&parts[curr_part++], "ML", offset,
143 MTD_WRITEABLE);
144 continue;
145 }
146
147 /* TRX */
148 if (buf[0x000 / 4] == TRX_MAGIC) {
149 trx = (struct trx_header *)buf;
150
Rafał Miłecki396afe52013-01-06 16:08:36 +0100151 trx_part = curr_part;
152 bcm47xxpart_add_part(&parts[curr_part++], "firmware",
153 offset, 0);
154
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200155 i = 0;
156 /* We have LZMA loader if offset[2] points to sth */
157 if (trx->offset[2]) {
158 bcm47xxpart_add_part(&parts[curr_part++],
159 "loader",
160 offset + trx->offset[i],
161 0);
162 i++;
163 }
164
165 bcm47xxpart_add_part(&parts[curr_part++], "linux",
166 offset + trx->offset[i], 0);
167 i++;
168
169 /*
170 * Pure rootfs size is known and can be calculated as:
171 * trx->length - trx->offset[i]. We don't fill it as
172 * we want to have jffs2 (overlay) in the same mtd.
173 */
174 bcm47xxpart_add_part(&parts[curr_part++], "rootfs",
175 offset + trx->offset[i], 0);
176 i++;
177
Rafał Miłecki396afe52013-01-06 16:08:36 +0100178 last_trx_part = curr_part - 1;
179
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200180 /*
181 * We have whole TRX scanned, skip to the next part. Use
182 * roundown (not roundup), as the loop will increase
183 * offset in next step.
184 */
185 offset = rounddown(offset + trx->length, blocksize);
186 continue;
187 }
Rafał Miłecki020c6bc2013-10-21 22:34:37 +0200188
189 /* Squashfs on devices not using TRX */
190 if (buf[0x000 / 4] == SQSH_MAGIC) {
191 bcm47xxpart_add_part(&parts[curr_part++], "rootfs",
192 offset, 0);
193 continue;
194 }
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200195 }
Rafał Miłecki91d542f2013-03-07 09:02:39 +0100196
197 /* Look for NVRAM at the end of the last block. */
198 for (i = 0; i < ARRAY_SIZE(possible_nvram_sizes); i++) {
199 if (curr_part > BCM47XXPART_MAX_PARTS) {
200 pr_warn("Reached maximum number of partitions, scanning stopped!\n");
201 break;
202 }
203
204 offset = master->size - possible_nvram_sizes[i];
205 if (mtd_read(master, offset, 0x4, &bytes_read,
206 (uint8_t *)buf) < 0) {
207 pr_err("mtd_read error while reading at offset 0x%X!\n",
208 offset);
209 continue;
210 }
211
212 /* Standard NVRAM */
213 if (buf[0] == NVRAM_HEADER) {
214 bcm47xxpart_add_part(&parts[curr_part++], "nvram",
215 master->size - blocksize, 0);
216 break;
217 }
218 }
219
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200220 kfree(buf);
221
222 /*
223 * Assume that partitions end at the beginning of the one they are
224 * followed by.
225 */
Rafał Miłecki648bdbe2013-01-06 16:08:35 +0100226 for (i = 0; i < curr_part; i++) {
227 u64 next_part_offset = (i < curr_part - 1) ?
228 parts[i + 1].offset : master->size;
229
230 parts[i].size = next_part_offset - parts[i].offset;
Rafał Miłecki396afe52013-01-06 16:08:36 +0100231 if (i == last_trx_part && trx_part >= 0)
232 parts[trx_part].size = next_part_offset -
233 parts[trx_part].offset;
Rafał Miłecki648bdbe2013-01-06 16:08:35 +0100234 }
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200235
236 *pparts = parts;
237 return curr_part;
238};
239
240static struct mtd_part_parser bcm47xxpart_mtd_parser = {
241 .owner = THIS_MODULE,
242 .parse_fn = bcm47xxpart_parse,
243 .name = "bcm47xxpart",
244};
245
246static int __init bcm47xxpart_init(void)
247{
Axel Lin6e14a612013-12-01 19:01:06 +0800248 register_mtd_parser(&bcm47xxpart_mtd_parser);
249 return 0;
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200250}
251
252static void __exit bcm47xxpart_exit(void)
253{
254 deregister_mtd_parser(&bcm47xxpart_mtd_parser);
255}
256
257module_init(bcm47xxpart_init);
258module_exit(bcm47xxpart_exit);
259
260MODULE_LICENSE("GPL");
261MODULE_DESCRIPTION("MTD partitioning for BCM47XX flash memories");