blob: f4954b4dd3122e20af71afbb3de39a47e5580e7a [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
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/bcm47xx_nvram.h>
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/slab.h>
16#include <linux/mtd/mtd.h>
17#include <linux/mtd/partitions.h>
18
19#include <uapi/linux/magic.h>
20
21/*
22 * NAND flash on Netgear R6250 was verified to contain 15 partitions.
23 * This will result in allocating too big array for some old devices, but the
24 * memory will be freed soon anyway (see mtd_device_parse_register).
25 */
26#define BCM47XXPART_MAX_PARTS 20
27
28/*
29 * Amount of bytes we read when analyzing each block of flash memory.
30 * Set it big enough to allow detecting partition and reading important data.
31 */
32#define BCM47XXPART_BYTES_TO_READ 0x4e8
33
34/* Magics */
35#define BOARD_DATA_MAGIC 0x5246504D /* MPFR */
36#define BOARD_DATA_MAGIC2 0xBD0D0BBD
37#define CFE_MAGIC 0x43464531 /* 1EFC */
38#define FACTORY_MAGIC 0x59544346 /* FCTY */
39#define NVRAM_HEADER 0x48534C46 /* FLSH */
40#define POT_MAGIC1 0x54544f50 /* POTT */
41#define POT_MAGIC2 0x504f /* OP */
42#define T_METER_MAGIC 0x4D540000 /* MT */
43#define ML_MAGIC1 0x39685a42
44#define ML_MAGIC2 0x26594131
45#define TRX_MAGIC 0x30524448
46#define SHSQ_MAGIC 0x71736873 /* shsq (weird ZTE H218N endianness) */
47
48static const char * const trx_types[] = { "trx", NULL };
49
50struct trx_header {
51 uint32_t magic;
52 uint32_t length;
53 uint32_t crc32;
54 uint16_t flags;
55 uint16_t version;
56 uint32_t offset[3];
57} __packed;
58
59static void bcm47xxpart_add_part(struct mtd_partition *part, const char *name,
60 u64 offset, uint32_t mask_flags)
61{
62 part->name = name;
63 part->offset = offset;
64 part->mask_flags = mask_flags;
65}
66
67/**
68 * bcm47xxpart_bootpartition - gets index of TRX partition used by bootloader
69 *
70 * Some devices may have more than one TRX partition. In such case one of them
71 * is the main one and another a failsafe one. Bootloader may fallback to the
72 * failsafe firmware if it detects corruption of the main image.
73 *
74 * This function provides info about currently used TRX partition. It's the one
75 * containing kernel started by the bootloader.
76 */
77static int bcm47xxpart_bootpartition(void)
78{
79 char buf[4];
80 int bootpartition;
81
82 /* Check CFE environment variable */
83 if (bcm47xx_nvram_getenv("bootpartition", buf, sizeof(buf)) > 0) {
84 if (!kstrtoint(buf, 0, &bootpartition))
85 return bootpartition;
86 }
87
88 return 0;
89}
90
91static int bcm47xxpart_parse(struct mtd_info *master,
92 const struct mtd_partition **pparts,
93 struct mtd_part_parser_data *data)
94{
95 struct mtd_partition *parts;
96 uint8_t i, curr_part = 0;
97 uint32_t *buf;
98 size_t bytes_read;
99 uint32_t offset;
100 uint32_t blocksize = master->erasesize;
101 int trx_parts[2]; /* Array with indexes of TRX partitions */
102 int trx_num = 0; /* Number of found TRX partitions */
103 int possible_nvram_sizes[] = { 0x8000, 0xF000, 0x10000, };
104 int err;
105
106 /*
107 * Some really old flashes (like AT45DB*) had smaller erasesize-s, but
108 * partitions were aligned to at least 0x1000 anyway.
109 */
110 if (blocksize < 0x1000)
111 blocksize = 0x1000;
112
113 /* Alloc */
114 parts = kcalloc(BCM47XXPART_MAX_PARTS, sizeof(struct mtd_partition),
115 GFP_KERNEL);
116 if (!parts)
117 return -ENOMEM;
118
119 buf = kzalloc(BCM47XXPART_BYTES_TO_READ, GFP_KERNEL);
120 if (!buf) {
121 kfree(parts);
122 return -ENOMEM;
123 }
124
125 /* Parse block by block looking for magics */
126 for (offset = 0; offset <= master->size - blocksize;
127 offset += blocksize) {
128 /* Nothing more in higher memory on BCM47XX (MIPS) */
129 if (IS_ENABLED(CONFIG_BCM47XX) && offset >= 0x2000000)
130 break;
131
132 if (curr_part >= BCM47XXPART_MAX_PARTS) {
133 pr_warn("Reached maximum number of partitions, scanning stopped!\n");
134 break;
135 }
136
137 /* Read beginning of the block */
138 err = mtd_read(master, offset, BCM47XXPART_BYTES_TO_READ,
139 &bytes_read, (uint8_t *)buf);
140 if (err && !mtd_is_bitflip(err)) {
141 pr_err("mtd_read error while parsing (offset: 0x%X): %d\n",
142 offset, err);
143 continue;
144 }
145
146 /* Magic or small NVRAM at 0x400 */
147 if ((buf[0x4e0 / 4] == CFE_MAGIC && buf[0x4e4 / 4] == CFE_MAGIC) ||
148 (buf[0x400 / 4] == NVRAM_HEADER)) {
149 bcm47xxpart_add_part(&parts[curr_part++], "boot",
150 offset, MTD_WRITEABLE);
151 continue;
152 }
153
154 /*
155 * board_data starts with board_id which differs across boards,
156 * but we can use 'MPFR' (hopefully) magic at 0x100
157 */
158 if (buf[0x100 / 4] == BOARD_DATA_MAGIC) {
159 bcm47xxpart_add_part(&parts[curr_part++], "board_data",
160 offset, MTD_WRITEABLE);
161 continue;
162 }
163
164 /* Found on Huawei E970 */
165 if (buf[0x000 / 4] == FACTORY_MAGIC) {
166 bcm47xxpart_add_part(&parts[curr_part++], "factory",
167 offset, MTD_WRITEABLE);
168 continue;
169 }
170
171 /* POT(TOP) */
172 if (buf[0x000 / 4] == POT_MAGIC1 &&
173 (buf[0x004 / 4] & 0xFFFF) == POT_MAGIC2) {
174 bcm47xxpart_add_part(&parts[curr_part++], "POT", offset,
175 MTD_WRITEABLE);
176 continue;
177 }
178
179 /* ML */
180 if (buf[0x010 / 4] == ML_MAGIC1 &&
181 buf[0x014 / 4] == ML_MAGIC2) {
182 bcm47xxpart_add_part(&parts[curr_part++], "ML", offset,
183 MTD_WRITEABLE);
184 continue;
185 }
186
187 /* T_Meter */
188 if ((le32_to_cpu(buf[0x000 / 4]) & 0xFFFF0000) == T_METER_MAGIC &&
189 (le32_to_cpu(buf[0x030 / 4]) & 0xFFFF0000) == T_METER_MAGIC &&
190 (le32_to_cpu(buf[0x060 / 4]) & 0xFFFF0000) == T_METER_MAGIC) {
191 bcm47xxpart_add_part(&parts[curr_part++], "T_Meter", offset,
192 MTD_WRITEABLE);
193 continue;
194 }
195
196 /* TRX */
197 if (buf[0x000 / 4] == TRX_MAGIC) {
198 struct trx_header *trx;
199 uint32_t last_subpart;
200 uint32_t trx_size;
201
202 if (trx_num >= ARRAY_SIZE(trx_parts))
203 pr_warn("No enough space to store another TRX found at 0x%X\n",
204 offset);
205 else
206 trx_parts[trx_num++] = curr_part;
207 bcm47xxpart_add_part(&parts[curr_part++], "firmware",
208 offset, 0);
209
210 /*
211 * Try to find TRX size. The "length" field isn't fully
212 * reliable as it could be decreased to make CRC32 cover
213 * only part of TRX data. It's commonly used as checksum
214 * can't cover e.g. ever-changing rootfs partition.
215 * Use offsets as helpers for assuming min TRX size.
216 */
217 trx = (struct trx_header *)buf;
218 last_subpart = max3(trx->offset[0], trx->offset[1],
219 trx->offset[2]);
220 trx_size = max(trx->length, last_subpart + blocksize);
221
222 /*
223 * Skip the TRX data. Decrease offset by block size as
224 * the next loop iteration will increase it.
225 */
226 offset += roundup(trx_size, blocksize) - blocksize;
227 continue;
228 }
229
230 /* Squashfs on devices not using TRX */
231 if (le32_to_cpu(buf[0x000 / 4]) == SQUASHFS_MAGIC ||
232 buf[0x000 / 4] == SHSQ_MAGIC) {
233 bcm47xxpart_add_part(&parts[curr_part++], "rootfs",
234 offset, 0);
235 continue;
236 }
237
238 /*
239 * New (ARM?) devices may have NVRAM in some middle block. Last
240 * block will be checked later, so skip it.
241 */
242 if (offset != master->size - blocksize &&
243 buf[0x000 / 4] == NVRAM_HEADER) {
244 bcm47xxpart_add_part(&parts[curr_part++], "nvram",
245 offset, 0);
246 continue;
247 }
248
249 /* Read middle of the block */
250 err = mtd_read(master, offset + 0x8000, 0x4, &bytes_read,
251 (uint8_t *)buf);
252 if (err && !mtd_is_bitflip(err)) {
253 pr_err("mtd_read error while parsing (offset: 0x%X): %d\n",
254 offset, err);
255 continue;
256 }
257
258 /* Some devices (ex. WNDR3700v3) don't have a standard 'MPFR' */
259 if (buf[0x000 / 4] == BOARD_DATA_MAGIC2) {
260 bcm47xxpart_add_part(&parts[curr_part++], "board_data",
261 offset, MTD_WRITEABLE);
262 continue;
263 }
264 }
265
266 /* Look for NVRAM at the end of the last block. */
267 for (i = 0; i < ARRAY_SIZE(possible_nvram_sizes); i++) {
268 if (curr_part >= BCM47XXPART_MAX_PARTS) {
269 pr_warn("Reached maximum number of partitions, scanning stopped!\n");
270 break;
271 }
272
273 offset = master->size - possible_nvram_sizes[i];
274 err = mtd_read(master, offset, 0x4, &bytes_read,
275 (uint8_t *)buf);
276 if (err && !mtd_is_bitflip(err)) {
277 pr_err("mtd_read error while reading (offset 0x%X): %d\n",
278 offset, err);
279 continue;
280 }
281
282 /* Standard NVRAM */
283 if (buf[0] == NVRAM_HEADER) {
284 bcm47xxpart_add_part(&parts[curr_part++], "nvram",
285 master->size - blocksize, 0);
286 break;
287 }
288 }
289
290 kfree(buf);
291
292 /*
293 * Assume that partitions end at the beginning of the one they are
294 * followed by.
295 */
296 for (i = 0; i < curr_part; i++) {
297 u64 next_part_offset = (i < curr_part - 1) ?
298 parts[i + 1].offset : master->size;
299
300 parts[i].size = next_part_offset - parts[i].offset;
301 }
302
303 /* If there was TRX parse it now */
304 for (i = 0; i < trx_num; i++) {
305 struct mtd_partition *trx = &parts[trx_parts[i]];
306
307 if (i == bcm47xxpart_bootpartition())
308 trx->types = trx_types;
309 else
310 trx->name = "failsafe";
311 }
312
313 *pparts = parts;
314 return curr_part;
315};
316
317static const struct of_device_id bcm47xxpart_of_match_table[] = {
318 { .compatible = "brcm,bcm947xx-cfe-partitions" },
319 {},
320};
321MODULE_DEVICE_TABLE(of, bcm47xxpart_of_match_table);
322
323static struct mtd_part_parser bcm47xxpart_mtd_parser = {
324 .parse_fn = bcm47xxpart_parse,
325 .name = "bcm47xxpart",
326 .of_match_table = bcm47xxpart_of_match_table,
327};
328module_mtd_part_parser(bcm47xxpart_mtd_parser);
329
330MODULE_LICENSE("GPL");
331MODULE_DESCRIPTION("MTD partitioning for BCM47XX flash memories");