blob: fe2581d9d882f2f480f4f5ec4b458cb3d67268e9 [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
Rafał Miłecki89a0d9a92017-01-10 23:15:25 +010012#include <linux/bcm47xx_nvram.h>
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020013#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>
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020018
Rafał Miłecki0b56d2d2014-12-16 09:50:25 +010019#include <uapi/linux/magic.h>
20
Rafał Miłecki59af5c72014-10-02 11:48:46 +020021/*
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
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020027
Rafał Miłecki5ca10882013-03-07 09:02:38 +010028/*
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 */
Rafał Miłecki4f8aaf72013-12-21 19:39:11 +010032#define BCM47XXPART_BYTES_TO_READ 0x4e8
Rafał Miłecki5ca10882013-03-07 09:02:38 +010033
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020034/* Magics */
35#define BOARD_DATA_MAGIC 0x5246504D /* MPFR */
Rafał Miłeckif0501e82013-12-21 19:39:12 +010036#define BOARD_DATA_MAGIC2 0xBD0D0BBD
Rafał Miłecki4f8aaf72013-12-21 19:39:11 +010037#define CFE_MAGIC 0x43464531 /* 1EFC */
Rafał Miłecki33094c72013-10-21 22:35:34 +020038#define FACTORY_MAGIC 0x59544346 /* FCTY */
Rafał Miłecki9e3afa52014-02-28 18:02:01 +010039#define NVRAM_HEADER 0x48534C46 /* FLSH */
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020040#define POT_MAGIC1 0x54544f50 /* POTT */
41#define POT_MAGIC2 0x504f /* OP */
42#define ML_MAGIC1 0x39685a42
43#define ML_MAGIC2 0x26594131
44#define TRX_MAGIC 0x30524448
Rafał Miłecki0b56d2d2014-12-16 09:50:25 +010045#define SHSQ_MAGIC 0x71736873 /* shsq (weird ZTE H218N endianness) */
Rafał Miłecki99352af2017-06-21 08:26:47 +020046
47static const char * const trx_types[] = { "trx", NULL };
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020048
49struct trx_header {
50 uint32_t magic;
51 uint32_t length;
52 uint32_t crc32;
53 uint16_t flags;
54 uint16_t version;
55 uint32_t offset[3];
56} __packed;
57
Rafał Miłeckibd10c262014-12-01 18:50:20 +010058static void bcm47xxpart_add_part(struct mtd_partition *part, const char *name,
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020059 u64 offset, uint32_t mask_flags)
60{
61 part->name = name;
62 part->offset = offset;
63 part->mask_flags = mask_flags;
64}
65
Rafał Miłecki89a0d9a92017-01-10 23:15:25 +010066/**
67 * bcm47xxpart_bootpartition - gets index of TRX partition used by bootloader
68 *
69 * Some devices may have more than one TRX partition. In such case one of them
70 * is the main one and another a failsafe one. Bootloader may fallback to the
71 * failsafe firmware if it detects corruption of the main image.
72 *
73 * This function provides info about currently used TRX partition. It's the one
74 * containing kernel started by the bootloader.
75 */
76static int bcm47xxpart_bootpartition(void)
77{
78 char buf[4];
79 int bootpartition;
80
81 /* Check CFE environment variable */
82 if (bcm47xx_nvram_getenv("bootpartition", buf, sizeof(buf)) > 0) {
83 if (!kstrtoint(buf, 0, &bootpartition))
84 return bootpartition;
85 }
86
87 return 0;
88}
89
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020090static int bcm47xxpart_parse(struct mtd_info *master,
Brian Norrisb9adf462015-12-04 15:25:14 -080091 const struct mtd_partition **pparts,
Rafał Miłecki3cf7f132012-08-30 07:41:16 +020092 struct mtd_part_parser_data *data)
93{
94 struct mtd_partition *parts;
95 uint8_t i, curr_part = 0;
96 uint32_t *buf;
97 size_t bytes_read;
98 uint32_t offset;
Hauke Mehrtens25bad1d2013-01-24 17:39:58 +010099 uint32_t blocksize = master->erasesize;
Rafał Miłecki89a0d9a92017-01-10 23:15:25 +0100100 int trx_parts[2]; /* Array with indexes of TRX partitions */
101 int trx_num = 0; /* Number of found TRX partitions */
Rafał Miłecki91d542f2013-03-07 09:02:39 +0100102 int possible_nvram_sizes[] = { 0x8000, 0xF000, 0x10000, };
Rafał Miłecki36bcc0c2015-12-06 11:31:38 +0100103 int err;
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200104
Rafał Miłecki16bd87b2014-12-08 18:45:00 +0100105 /*
106 * Some really old flashes (like AT45DB*) had smaller erasesize-s, but
107 * partitions were aligned to at least 0x1000 anyway.
108 */
109 if (blocksize < 0x1000)
110 blocksize = 0x1000;
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200111
112 /* Alloc */
113 parts = kzalloc(sizeof(struct mtd_partition) * BCM47XXPART_MAX_PARTS,
114 GFP_KERNEL);
Hauke Mehrtens99b1d182013-10-13 22:53:49 +0200115 if (!parts)
116 return -ENOMEM;
117
Rafał Miłecki5ca10882013-03-07 09:02:38 +0100118 buf = kzalloc(BCM47XXPART_BYTES_TO_READ, GFP_KERNEL);
Hauke Mehrtens99b1d182013-10-13 22:53:49 +0200119 if (!buf) {
120 kfree(parts);
121 return -ENOMEM;
122 }
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200123
124 /* Parse block by block looking for magics */
125 for (offset = 0; offset <= master->size - blocksize;
126 offset += blocksize) {
Rafał Miłecki2a36a5c2015-12-05 02:09:43 +0100127 /* Nothing more in higher memory on BCM47XX (MIPS) */
Masahiro Yamada97f26452016-08-03 13:45:50 -0700128 if (IS_ENABLED(CONFIG_BCM47XX) && offset >= 0x2000000)
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200129 break;
130
Rafał Miłecki00b79862014-02-26 14:02:06 +0100131 if (curr_part >= BCM47XXPART_MAX_PARTS) {
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200132 pr_warn("Reached maximum number of partitions, scanning stopped!\n");
133 break;
134 }
135
136 /* Read beginning of the block */
Rafał Miłecki36bcc0c2015-12-06 11:31:38 +0100137 err = mtd_read(master, offset, BCM47XXPART_BYTES_TO_READ,
138 &bytes_read, (uint8_t *)buf);
139 if (err && !mtd_is_bitflip(err)) {
140 pr_err("mtd_read error while parsing (offset: 0x%X): %d\n",
141 offset, err);
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200142 continue;
143 }
144
Rafał Miłecki4f8aaf72013-12-21 19:39:11 +0100145 /* Magic or small NVRAM at 0x400 */
146 if ((buf[0x4e0 / 4] == CFE_MAGIC && buf[0x4e4 / 4] == CFE_MAGIC) ||
147 (buf[0x400 / 4] == NVRAM_HEADER)) {
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200148 bcm47xxpart_add_part(&parts[curr_part++], "boot",
149 offset, MTD_WRITEABLE);
150 continue;
151 }
152
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200153 /*
154 * board_data starts with board_id which differs across boards,
155 * but we can use 'MPFR' (hopefully) magic at 0x100
156 */
157 if (buf[0x100 / 4] == BOARD_DATA_MAGIC) {
158 bcm47xxpart_add_part(&parts[curr_part++], "board_data",
159 offset, MTD_WRITEABLE);
160 continue;
161 }
162
Rafał Miłecki33094c72013-10-21 22:35:34 +0200163 /* Found on Huawei E970 */
164 if (buf[0x000 / 4] == FACTORY_MAGIC) {
165 bcm47xxpart_add_part(&parts[curr_part++], "factory",
166 offset, MTD_WRITEABLE);
167 continue;
168 }
169
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200170 /* POT(TOP) */
171 if (buf[0x000 / 4] == POT_MAGIC1 &&
172 (buf[0x004 / 4] & 0xFFFF) == POT_MAGIC2) {
173 bcm47xxpart_add_part(&parts[curr_part++], "POT", offset,
174 MTD_WRITEABLE);
175 continue;
176 }
177
178 /* ML */
179 if (buf[0x010 / 4] == ML_MAGIC1 &&
180 buf[0x014 / 4] == ML_MAGIC2) {
181 bcm47xxpart_add_part(&parts[curr_part++], "ML", offset,
182 MTD_WRITEABLE);
183 continue;
184 }
185
186 /* TRX */
187 if (buf[0x000 / 4] == TRX_MAGIC) {
Rafał Miłeckib522d7b2017-01-10 23:15:24 +0100188 struct trx_header *trx;
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200189
Rafał Miłecki89a0d9a92017-01-10 23:15:25 +0100190 if (trx_num >= ARRAY_SIZE(trx_parts))
191 pr_warn("No enough space to store another TRX found at 0x%X\n",
192 offset);
193 else
194 trx_parts[trx_num++] = curr_part;
Rafał Miłecki396afe52013-01-06 16:08:36 +0100195 bcm47xxpart_add_part(&parts[curr_part++], "firmware",
196 offset, 0);
197
Rafał Miłeckibd5d2132016-11-20 16:09:30 +0100198 /* Jump to the end of TRX */
Rafał Miłeckib522d7b2017-01-10 23:15:24 +0100199 trx = (struct trx_header *)buf;
Rafał Miłeckibd5d2132016-11-20 16:09:30 +0100200 offset = roundup(offset + trx->length, blocksize);
201 /* Next loop iteration will increase the offset */
202 offset -= blocksize;
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200203 continue;
204 }
Rafał Miłecki020c6bc2013-10-21 22:34:37 +0200205
206 /* Squashfs on devices not using TRX */
Rafał Miłecki0b56d2d2014-12-16 09:50:25 +0100207 if (le32_to_cpu(buf[0x000 / 4]) == SQUASHFS_MAGIC ||
208 buf[0x000 / 4] == SHSQ_MAGIC) {
Rafał Miłecki020c6bc2013-10-21 22:34:37 +0200209 bcm47xxpart_add_part(&parts[curr_part++], "rootfs",
210 offset, 0);
211 continue;
212 }
Rafał Miłeckif0501e82013-12-21 19:39:12 +0100213
Rafał Miłecki024629f2014-08-18 20:20:27 +0200214 /*
215 * New (ARM?) devices may have NVRAM in some middle block. Last
216 * block will be checked later, so skip it.
217 */
218 if (offset != master->size - blocksize &&
219 buf[0x000 / 4] == NVRAM_HEADER) {
220 bcm47xxpart_add_part(&parts[curr_part++], "nvram",
221 offset, 0);
222 continue;
223 }
224
Rafał Miłeckif0501e82013-12-21 19:39:12 +0100225 /* Read middle of the block */
Rafał Miłecki36bcc0c2015-12-06 11:31:38 +0100226 err = mtd_read(master, offset + 0x8000, 0x4, &bytes_read,
227 (uint8_t *)buf);
228 if (err && !mtd_is_bitflip(err)) {
229 pr_err("mtd_read error while parsing (offset: 0x%X): %d\n",
230 offset, err);
Rafał Miłeckif0501e82013-12-21 19:39:12 +0100231 continue;
232 }
233
234 /* Some devices (ex. WNDR3700v3) don't have a standard 'MPFR' */
235 if (buf[0x000 / 4] == BOARD_DATA_MAGIC2) {
236 bcm47xxpart_add_part(&parts[curr_part++], "board_data",
237 offset, MTD_WRITEABLE);
238 continue;
239 }
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200240 }
Rafał Miłecki91d542f2013-03-07 09:02:39 +0100241
242 /* Look for NVRAM at the end of the last block. */
243 for (i = 0; i < ARRAY_SIZE(possible_nvram_sizes); i++) {
Rafał Miłecki00b79862014-02-26 14:02:06 +0100244 if (curr_part >= BCM47XXPART_MAX_PARTS) {
Rafał Miłecki91d542f2013-03-07 09:02:39 +0100245 pr_warn("Reached maximum number of partitions, scanning stopped!\n");
246 break;
247 }
248
249 offset = master->size - possible_nvram_sizes[i];
Rafał Miłecki36bcc0c2015-12-06 11:31:38 +0100250 err = mtd_read(master, offset, 0x4, &bytes_read,
251 (uint8_t *)buf);
252 if (err && !mtd_is_bitflip(err)) {
253 pr_err("mtd_read error while reading (offset 0x%X): %d\n",
254 offset, err);
Rafał Miłecki91d542f2013-03-07 09:02:39 +0100255 continue;
256 }
257
258 /* Standard NVRAM */
259 if (buf[0] == NVRAM_HEADER) {
260 bcm47xxpart_add_part(&parts[curr_part++], "nvram",
261 master->size - blocksize, 0);
262 break;
263 }
264 }
265
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200266 kfree(buf);
267
268 /*
269 * Assume that partitions end at the beginning of the one they are
270 * followed by.
271 */
Rafał Miłecki648bdbe2013-01-06 16:08:35 +0100272 for (i = 0; i < curr_part; i++) {
273 u64 next_part_offset = (i < curr_part - 1) ?
274 parts[i + 1].offset : master->size;
275
276 parts[i].size = next_part_offset - parts[i].offset;
Rafał Miłeckib522d7b2017-01-10 23:15:24 +0100277 }
278
279 /* If there was TRX parse it now */
Rafał Miłecki89a0d9a92017-01-10 23:15:25 +0100280 for (i = 0; i < trx_num; i++) {
281 struct mtd_partition *trx = &parts[trx_parts[i]];
Rafał Miłeckib522d7b2017-01-10 23:15:24 +0100282
Rafał Miłecki99352af2017-06-21 08:26:47 +0200283 if (i == bcm47xxpart_bootpartition())
284 trx->types = trx_types;
285 else
Rafał Miłecki89a0d9a92017-01-10 23:15:25 +0100286 trx->name = "failsafe";
Rafał Miłecki648bdbe2013-01-06 16:08:35 +0100287 }
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200288
289 *pparts = parts;
290 return curr_part;
291};
292
293static struct mtd_part_parser bcm47xxpart_mtd_parser = {
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200294 .parse_fn = bcm47xxpart_parse,
295 .name = "bcm47xxpart",
296};
Brian Norrisb8f70ba2015-11-11 19:13:30 -0800297module_mtd_part_parser(bcm47xxpart_mtd_parser);
Rafał Miłecki3cf7f132012-08-30 07:41:16 +0200298
299MODULE_LICENSE("GPL");
300MODULE_DESCRIPTION("MTD partitioning for BCM47XX flash memories");