blob: 7a6384b0962a9de2c92dffb81f1fc45d6d89a490 [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 */
26#define BCM47XXPART_BYTES_TO_READ 0x404
27
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020028/* Magics */
29#define BOARD_DATA_MAGIC 0x5246504D /* MPFR */
Rafał Miłecki33094c72013-10-21 22:35:34 +020030#define FACTORY_MAGIC 0x59544346 /* FCTY */
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020031#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
Rafał Miłecki020c6bc2013-10-21 22:34:37 +020036#define SQSH_MAGIC 0x71736873 /* shsq */
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020037
38struct 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
47static 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
55static 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;
Hauke Mehrtens25bad1d2013-01-24 17:39:58 +010064 uint32_t blocksize = master->erasesize;
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020065 struct trx_header *trx;
Rafał Miłecki396afe52013-01-06 16:08:36 +010066 int trx_part = -1;
67 int last_trx_part = -1;
Rafał Miłecki91d542f2013-03-07 09:02:39 +010068 int possible_nvram_sizes[] = { 0x8000, 0xF000, 0x10000, };
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020069
Hauke Mehrtens25bad1d2013-01-24 17:39:58 +010070 if (blocksize <= 0x10000)
71 blocksize = 0x10000;
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020072
73 /* Alloc */
74 parts = kzalloc(sizeof(struct mtd_partition) * BCM47XXPART_MAX_PARTS,
75 GFP_KERNEL);
Hauke Mehrtens99b1d182013-10-13 22:53:49 +020076 if (!parts)
77 return -ENOMEM;
78
Rafał Miłecki5ca10882013-03-07 09:02:38 +010079 buf = kzalloc(BCM47XXPART_BYTES_TO_READ, GFP_KERNEL);
Hauke Mehrtens99b1d182013-10-13 22:53:49 +020080 if (!buf) {
81 kfree(parts);
82 return -ENOMEM;
83 }
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020084
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 */
Rafał Miłecki5ca10882013-03-07 09:02:38 +010098 if (mtd_read(master, offset, BCM47XXPART_BYTES_TO_READ,
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020099 &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
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200112 /*
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
Rafał Miłecki33094c72013-10-21 22:35:34 +0200122 /* 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
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200129 /* 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
Rafał Miłecki396afe52013-01-06 16:08:36 +0100149 trx_part = curr_part;
150 bcm47xxpart_add_part(&parts[curr_part++], "firmware",
151 offset, 0);
152
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200153 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
Rafał Miłecki396afe52013-01-06 16:08:36 +0100176 last_trx_part = curr_part - 1;
177
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200178 /*
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 }
Rafał Miłecki020c6bc2013-10-21 22:34:37 +0200186
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 }
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200193 }
Rafał Miłecki91d542f2013-03-07 09:02:39 +0100194
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
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200218 kfree(buf);
219
220 /*
221 * Assume that partitions end at the beginning of the one they are
222 * followed by.
223 */
Rafał Miłecki648bdbe2013-01-06 16:08:35 +0100224 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;
Rafał Miłecki396afe52013-01-06 16:08:36 +0100229 if (i == last_trx_part && trx_part >= 0)
230 parts[trx_part].size = next_part_offset -
231 parts[trx_part].offset;
Rafał Miłecki648bdbe2013-01-06 16:08:35 +0100232 }
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200233
234 *pparts = parts;
235 return curr_part;
236};
237
238static struct mtd_part_parser bcm47xxpart_mtd_parser = {
239 .owner = THIS_MODULE,
240 .parse_fn = bcm47xxpart_parse,
241 .name = "bcm47xxpart",
242};
243
244static int __init bcm47xxpart_init(void)
245{
246 return register_mtd_parser(&bcm47xxpart_mtd_parser);
247}
248
249static void __exit bcm47xxpart_exit(void)
250{
251 deregister_mtd_parser(&bcm47xxpart_mtd_parser);
252}
253
254module_init(bcm47xxpart_init);
255module_exit(bcm47xxpart_exit);
256
257MODULE_LICENSE("GPL");
258MODULE_DESCRIPTION("MTD partitioning for BCM47XX flash memories");