blob: 9279a9174f84ad9cd3a2ae2c7117c29cc8134e9e [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
35
36struct trx_header {
37 uint32_t magic;
38 uint32_t length;
39 uint32_t crc32;
40 uint16_t flags;
41 uint16_t version;
42 uint32_t offset[3];
43} __packed;
44
45static void bcm47xxpart_add_part(struct mtd_partition *part, char *name,
46 u64 offset, uint32_t mask_flags)
47{
48 part->name = name;
49 part->offset = offset;
50 part->mask_flags = mask_flags;
51}
52
53static int bcm47xxpart_parse(struct mtd_info *master,
54 struct mtd_partition **pparts,
55 struct mtd_part_parser_data *data)
56{
57 struct mtd_partition *parts;
58 uint8_t i, curr_part = 0;
59 uint32_t *buf;
60 size_t bytes_read;
61 uint32_t offset;
Hauke Mehrtens25bad1d2013-01-24 17:39:58 +010062 uint32_t blocksize = master->erasesize;
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020063 struct trx_header *trx;
Rafał Miłecki396afe52013-01-06 16:08:36 +010064 int trx_part = -1;
65 int last_trx_part = -1;
Rafał Miłecki91d542f2013-03-07 09:02:39 +010066 int possible_nvram_sizes[] = { 0x8000, 0xF000, 0x10000, };
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020067
Hauke Mehrtens25bad1d2013-01-24 17:39:58 +010068 if (blocksize <= 0x10000)
69 blocksize = 0x10000;
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020070
71 /* Alloc */
72 parts = kzalloc(sizeof(struct mtd_partition) * BCM47XXPART_MAX_PARTS,
73 GFP_KERNEL);
Rafał Miłecki5ca10882013-03-07 09:02:38 +010074 buf = kzalloc(BCM47XXPART_BYTES_TO_READ, GFP_KERNEL);
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020075
76 /* Parse block by block looking for magics */
77 for (offset = 0; offset <= master->size - blocksize;
78 offset += blocksize) {
79 /* Nothing more in higher memory */
80 if (offset >= 0x2000000)
81 break;
82
83 if (curr_part > BCM47XXPART_MAX_PARTS) {
84 pr_warn("Reached maximum number of partitions, scanning stopped!\n");
85 break;
86 }
87
88 /* Read beginning of the block */
Rafał Miłecki5ca10882013-03-07 09:02:38 +010089 if (mtd_read(master, offset, BCM47XXPART_BYTES_TO_READ,
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020090 &bytes_read, (uint8_t *)buf) < 0) {
91 pr_err("mtd_read error while parsing (offset: 0x%X)!\n",
92 offset);
93 continue;
94 }
95
96 /* CFE has small NVRAM at 0x400 */
97 if (buf[0x400 / 4] == NVRAM_HEADER) {
98 bcm47xxpart_add_part(&parts[curr_part++], "boot",
99 offset, MTD_WRITEABLE);
100 continue;
101 }
102
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200103 /*
104 * board_data starts with board_id which differs across boards,
105 * but we can use 'MPFR' (hopefully) magic at 0x100
106 */
107 if (buf[0x100 / 4] == BOARD_DATA_MAGIC) {
108 bcm47xxpart_add_part(&parts[curr_part++], "board_data",
109 offset, MTD_WRITEABLE);
110 continue;
111 }
112
113 /* POT(TOP) */
114 if (buf[0x000 / 4] == POT_MAGIC1 &&
115 (buf[0x004 / 4] & 0xFFFF) == POT_MAGIC2) {
116 bcm47xxpart_add_part(&parts[curr_part++], "POT", offset,
117 MTD_WRITEABLE);
118 continue;
119 }
120
121 /* ML */
122 if (buf[0x010 / 4] == ML_MAGIC1 &&
123 buf[0x014 / 4] == ML_MAGIC2) {
124 bcm47xxpart_add_part(&parts[curr_part++], "ML", offset,
125 MTD_WRITEABLE);
126 continue;
127 }
128
129 /* TRX */
130 if (buf[0x000 / 4] == TRX_MAGIC) {
131 trx = (struct trx_header *)buf;
132
Rafał Miłecki396afe52013-01-06 16:08:36 +0100133 trx_part = curr_part;
134 bcm47xxpart_add_part(&parts[curr_part++], "firmware",
135 offset, 0);
136
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200137 i = 0;
138 /* We have LZMA loader if offset[2] points to sth */
139 if (trx->offset[2]) {
140 bcm47xxpart_add_part(&parts[curr_part++],
141 "loader",
142 offset + trx->offset[i],
143 0);
144 i++;
145 }
146
147 bcm47xxpart_add_part(&parts[curr_part++], "linux",
148 offset + trx->offset[i], 0);
149 i++;
150
151 /*
152 * Pure rootfs size is known and can be calculated as:
153 * trx->length - trx->offset[i]. We don't fill it as
154 * we want to have jffs2 (overlay) in the same mtd.
155 */
156 bcm47xxpart_add_part(&parts[curr_part++], "rootfs",
157 offset + trx->offset[i], 0);
158 i++;
159
Rafał Miłecki396afe52013-01-06 16:08:36 +0100160 last_trx_part = curr_part - 1;
161
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200162 /*
163 * We have whole TRX scanned, skip to the next part. Use
164 * roundown (not roundup), as the loop will increase
165 * offset in next step.
166 */
167 offset = rounddown(offset + trx->length, blocksize);
168 continue;
169 }
170 }
Rafał Miłecki91d542f2013-03-07 09:02:39 +0100171
172 /* Look for NVRAM at the end of the last block. */
173 for (i = 0; i < ARRAY_SIZE(possible_nvram_sizes); i++) {
174 if (curr_part > BCM47XXPART_MAX_PARTS) {
175 pr_warn("Reached maximum number of partitions, scanning stopped!\n");
176 break;
177 }
178
179 offset = master->size - possible_nvram_sizes[i];
180 if (mtd_read(master, offset, 0x4, &bytes_read,
181 (uint8_t *)buf) < 0) {
182 pr_err("mtd_read error while reading at offset 0x%X!\n",
183 offset);
184 continue;
185 }
186
187 /* Standard NVRAM */
188 if (buf[0] == NVRAM_HEADER) {
189 bcm47xxpart_add_part(&parts[curr_part++], "nvram",
190 master->size - blocksize, 0);
191 break;
192 }
193 }
194
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200195 kfree(buf);
196
197 /*
198 * Assume that partitions end at the beginning of the one they are
199 * followed by.
200 */
Rafał Miłecki648bdbe2013-01-06 16:08:35 +0100201 for (i = 0; i < curr_part; i++) {
202 u64 next_part_offset = (i < curr_part - 1) ?
203 parts[i + 1].offset : master->size;
204
205 parts[i].size = next_part_offset - parts[i].offset;
Rafał Miłecki396afe52013-01-06 16:08:36 +0100206 if (i == last_trx_part && trx_part >= 0)
207 parts[trx_part].size = next_part_offset -
208 parts[trx_part].offset;
Rafał Miłecki648bdbe2013-01-06 16:08:35 +0100209 }
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200210
211 *pparts = parts;
212 return curr_part;
213};
214
215static struct mtd_part_parser bcm47xxpart_mtd_parser = {
216 .owner = THIS_MODULE,
217 .parse_fn = bcm47xxpart_parse,
218 .name = "bcm47xxpart",
219};
220
221static int __init bcm47xxpart_init(void)
222{
223 return register_mtd_parser(&bcm47xxpart_mtd_parser);
224}
225
226static void __exit bcm47xxpart_exit(void)
227{
228 deregister_mtd_parser(&bcm47xxpart_mtd_parser);
229}
230
231module_init(bcm47xxpart_init);
232module_exit(bcm47xxpart_exit);
233
234MODULE_LICENSE("GPL");
235MODULE_DESCRIPTION("MTD partitioning for BCM47XX flash memories");