blob: a737450826eca55e4e283963a9c14bac9ec67450 [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 */
30#define POT_MAGIC1 0x54544f50 /* POTT */
31#define POT_MAGIC2 0x504f /* OP */
32#define ML_MAGIC1 0x39685a42
33#define ML_MAGIC2 0x26594131
34#define TRX_MAGIC 0x30524448
Rafał Miłecki020c6bc2013-10-21 22:34:37 +020035#define SQSH_MAGIC 0x71736873 /* shsq */
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020036
37struct trx_header {
38 uint32_t magic;
39 uint32_t length;
40 uint32_t crc32;
41 uint16_t flags;
42 uint16_t version;
43 uint32_t offset[3];
44} __packed;
45
46static void bcm47xxpart_add_part(struct mtd_partition *part, char *name,
47 u64 offset, uint32_t mask_flags)
48{
49 part->name = name;
50 part->offset = offset;
51 part->mask_flags = mask_flags;
52}
53
54static int bcm47xxpart_parse(struct mtd_info *master,
55 struct mtd_partition **pparts,
56 struct mtd_part_parser_data *data)
57{
58 struct mtd_partition *parts;
59 uint8_t i, curr_part = 0;
60 uint32_t *buf;
61 size_t bytes_read;
62 uint32_t offset;
Hauke Mehrtens25bad1d2013-01-24 17:39:58 +010063 uint32_t blocksize = master->erasesize;
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020064 struct trx_header *trx;
Rafał Miłecki396afe52013-01-06 16:08:36 +010065 int trx_part = -1;
66 int last_trx_part = -1;
Rafał Miłecki91d542f2013-03-07 09:02:39 +010067 int possible_nvram_sizes[] = { 0x8000, 0xF000, 0x10000, };
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020068
Hauke Mehrtens25bad1d2013-01-24 17:39:58 +010069 if (blocksize <= 0x10000)
70 blocksize = 0x10000;
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020071
72 /* Alloc */
73 parts = kzalloc(sizeof(struct mtd_partition) * BCM47XXPART_MAX_PARTS,
74 GFP_KERNEL);
Hauke Mehrtens99b1d182013-10-13 22:53:49 +020075 if (!parts)
76 return -ENOMEM;
77
Rafał Miłecki5ca10882013-03-07 09:02:38 +010078 buf = kzalloc(BCM47XXPART_BYTES_TO_READ, GFP_KERNEL);
Hauke Mehrtens99b1d182013-10-13 22:53:49 +020079 if (!buf) {
80 kfree(parts);
81 return -ENOMEM;
82 }
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020083
84 /* Parse block by block looking for magics */
85 for (offset = 0; offset <= master->size - blocksize;
86 offset += blocksize) {
87 /* Nothing more in higher memory */
88 if (offset >= 0x2000000)
89 break;
90
91 if (curr_part > BCM47XXPART_MAX_PARTS) {
92 pr_warn("Reached maximum number of partitions, scanning stopped!\n");
93 break;
94 }
95
96 /* Read beginning of the block */
Rafał Miłecki5ca10882013-03-07 09:02:38 +010097 if (mtd_read(master, offset, BCM47XXPART_BYTES_TO_READ,
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020098 &bytes_read, (uint8_t *)buf) < 0) {
99 pr_err("mtd_read error while parsing (offset: 0x%X)!\n",
100 offset);
101 continue;
102 }
103
104 /* CFE has small NVRAM at 0x400 */
105 if (buf[0x400 / 4] == NVRAM_HEADER) {
106 bcm47xxpart_add_part(&parts[curr_part++], "boot",
107 offset, MTD_WRITEABLE);
108 continue;
109 }
110
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200111 /*
112 * board_data starts with board_id which differs across boards,
113 * but we can use 'MPFR' (hopefully) magic at 0x100
114 */
115 if (buf[0x100 / 4] == BOARD_DATA_MAGIC) {
116 bcm47xxpart_add_part(&parts[curr_part++], "board_data",
117 offset, MTD_WRITEABLE);
118 continue;
119 }
120
121 /* POT(TOP) */
122 if (buf[0x000 / 4] == POT_MAGIC1 &&
123 (buf[0x004 / 4] & 0xFFFF) == POT_MAGIC2) {
124 bcm47xxpart_add_part(&parts[curr_part++], "POT", offset,
125 MTD_WRITEABLE);
126 continue;
127 }
128
129 /* ML */
130 if (buf[0x010 / 4] == ML_MAGIC1 &&
131 buf[0x014 / 4] == ML_MAGIC2) {
132 bcm47xxpart_add_part(&parts[curr_part++], "ML", offset,
133 MTD_WRITEABLE);
134 continue;
135 }
136
137 /* TRX */
138 if (buf[0x000 / 4] == TRX_MAGIC) {
139 trx = (struct trx_header *)buf;
140
Rafał Miłecki396afe52013-01-06 16:08:36 +0100141 trx_part = curr_part;
142 bcm47xxpart_add_part(&parts[curr_part++], "firmware",
143 offset, 0);
144
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200145 i = 0;
146 /* We have LZMA loader if offset[2] points to sth */
147 if (trx->offset[2]) {
148 bcm47xxpart_add_part(&parts[curr_part++],
149 "loader",
150 offset + trx->offset[i],
151 0);
152 i++;
153 }
154
155 bcm47xxpart_add_part(&parts[curr_part++], "linux",
156 offset + trx->offset[i], 0);
157 i++;
158
159 /*
160 * Pure rootfs size is known and can be calculated as:
161 * trx->length - trx->offset[i]. We don't fill it as
162 * we want to have jffs2 (overlay) in the same mtd.
163 */
164 bcm47xxpart_add_part(&parts[curr_part++], "rootfs",
165 offset + trx->offset[i], 0);
166 i++;
167
Rafał Miłecki396afe52013-01-06 16:08:36 +0100168 last_trx_part = curr_part - 1;
169
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200170 /*
171 * We have whole TRX scanned, skip to the next part. Use
172 * roundown (not roundup), as the loop will increase
173 * offset in next step.
174 */
175 offset = rounddown(offset + trx->length, blocksize);
176 continue;
177 }
Rafał Miłecki020c6bc2013-10-21 22:34:37 +0200178
179 /* Squashfs on devices not using TRX */
180 if (buf[0x000 / 4] == SQSH_MAGIC) {
181 bcm47xxpart_add_part(&parts[curr_part++], "rootfs",
182 offset, 0);
183 continue;
184 }
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200185 }
Rafał Miłecki91d542f2013-03-07 09:02:39 +0100186
187 /* Look for NVRAM at the end of the last block. */
188 for (i = 0; i < ARRAY_SIZE(possible_nvram_sizes); i++) {
189 if (curr_part > BCM47XXPART_MAX_PARTS) {
190 pr_warn("Reached maximum number of partitions, scanning stopped!\n");
191 break;
192 }
193
194 offset = master->size - possible_nvram_sizes[i];
195 if (mtd_read(master, offset, 0x4, &bytes_read,
196 (uint8_t *)buf) < 0) {
197 pr_err("mtd_read error while reading at offset 0x%X!\n",
198 offset);
199 continue;
200 }
201
202 /* Standard NVRAM */
203 if (buf[0] == NVRAM_HEADER) {
204 bcm47xxpart_add_part(&parts[curr_part++], "nvram",
205 master->size - blocksize, 0);
206 break;
207 }
208 }
209
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200210 kfree(buf);
211
212 /*
213 * Assume that partitions end at the beginning of the one they are
214 * followed by.
215 */
Rafał Miłecki648bdbe2013-01-06 16:08:35 +0100216 for (i = 0; i < curr_part; i++) {
217 u64 next_part_offset = (i < curr_part - 1) ?
218 parts[i + 1].offset : master->size;
219
220 parts[i].size = next_part_offset - parts[i].offset;
Rafał Miłecki396afe52013-01-06 16:08:36 +0100221 if (i == last_trx_part && trx_part >= 0)
222 parts[trx_part].size = next_part_offset -
223 parts[trx_part].offset;
Rafał Miłecki648bdbe2013-01-06 16:08:35 +0100224 }
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200225
226 *pparts = parts;
227 return curr_part;
228};
229
230static struct mtd_part_parser bcm47xxpart_mtd_parser = {
231 .owner = THIS_MODULE,
232 .parse_fn = bcm47xxpart_parse,
233 .name = "bcm47xxpart",
234};
235
236static int __init bcm47xxpart_init(void)
237{
238 return register_mtd_parser(&bcm47xxpart_mtd_parser);
239}
240
241static void __exit bcm47xxpart_exit(void)
242{
243 deregister_mtd_parser(&bcm47xxpart_mtd_parser);
244}
245
246module_init(bcm47xxpart_init);
247module_exit(bcm47xxpart_exit);
248
249MODULE_LICENSE("GPL");
250MODULE_DESCRIPTION("MTD partitioning for BCM47XX flash memories");