Commit | Line | Data |
---|---|---|
3cf7f131 RM |
1 | /* |
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> | |
111bd981 | 17 | #include <bcm47xx_nvram.h> |
3cf7f131 RM |
18 | |
19 | /* 10 parts were found on sflash on Netgear WNDR4500 */ | |
20 | #define BCM47XXPART_MAX_PARTS 12 | |
21 | ||
5ca1088f RM |
22 | /* |
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 | */ | |
4f8aaf72 | 26 | #define BCM47XXPART_BYTES_TO_READ 0x4e8 |
5ca1088f | 27 | |
3cf7f131 RM |
28 | /* Magics */ |
29 | #define BOARD_DATA_MAGIC 0x5246504D /* MPFR */ | |
4f8aaf72 | 30 | #define CFE_MAGIC 0x43464531 /* 1EFC */ |
33094c73 | 31 | #define FACTORY_MAGIC 0x59544346 /* FCTY */ |
3cf7f131 RM |
32 | #define POT_MAGIC1 0x54544f50 /* POTT */ |
33 | #define POT_MAGIC2 0x504f /* OP */ | |
34 | #define ML_MAGIC1 0x39685a42 | |
35 | #define ML_MAGIC2 0x26594131 | |
36 | #define TRX_MAGIC 0x30524448 | |
020c6bcf | 37 | #define SQSH_MAGIC 0x71736873 /* shsq */ |
3cf7f131 RM |
38 | |
39 | struct trx_header { | |
40 | uint32_t magic; | |
41 | uint32_t length; | |
42 | uint32_t crc32; | |
43 | uint16_t flags; | |
44 | uint16_t version; | |
45 | uint32_t offset[3]; | |
46 | } __packed; | |
47 | ||
48 | static void bcm47xxpart_add_part(struct mtd_partition *part, char *name, | |
49 | u64 offset, uint32_t mask_flags) | |
50 | { | |
51 | part->name = name; | |
52 | part->offset = offset; | |
53 | part->mask_flags = mask_flags; | |
54 | } | |
55 | ||
56 | static int bcm47xxpart_parse(struct mtd_info *master, | |
57 | struct mtd_partition **pparts, | |
58 | struct mtd_part_parser_data *data) | |
59 | { | |
60 | struct mtd_partition *parts; | |
61 | uint8_t i, curr_part = 0; | |
62 | uint32_t *buf; | |
63 | size_t bytes_read; | |
64 | uint32_t offset; | |
25bad1d3 | 65 | uint32_t blocksize = master->erasesize; |
3cf7f131 | 66 | struct trx_header *trx; |
396afe55 RM |
67 | int trx_part = -1; |
68 | int last_trx_part = -1; | |
91d542f4 | 69 | int possible_nvram_sizes[] = { 0x8000, 0xF000, 0x10000, }; |
3cf7f131 | 70 | |
25bad1d3 HM |
71 | if (blocksize <= 0x10000) |
72 | blocksize = 0x10000; | |
3cf7f131 RM |
73 | |
74 | /* Alloc */ | |
75 | parts = kzalloc(sizeof(struct mtd_partition) * BCM47XXPART_MAX_PARTS, | |
76 | GFP_KERNEL); | |
99b1d188 HM |
77 | if (!parts) |
78 | return -ENOMEM; | |
79 | ||
5ca1088f | 80 | buf = kzalloc(BCM47XXPART_BYTES_TO_READ, GFP_KERNEL); |
99b1d188 HM |
81 | if (!buf) { |
82 | kfree(parts); | |
83 | return -ENOMEM; | |
84 | } | |
3cf7f131 RM |
85 | |
86 | /* Parse block by block looking for magics */ | |
87 | for (offset = 0; offset <= master->size - blocksize; | |
88 | offset += blocksize) { | |
89 | /* Nothing more in higher memory */ | |
90 | if (offset >= 0x2000000) | |
91 | break; | |
92 | ||
93 | if (curr_part > BCM47XXPART_MAX_PARTS) { | |
94 | pr_warn("Reached maximum number of partitions, scanning stopped!\n"); | |
95 | break; | |
96 | } | |
97 | ||
98 | /* Read beginning of the block */ | |
5ca1088f | 99 | if (mtd_read(master, offset, BCM47XXPART_BYTES_TO_READ, |
3cf7f131 RM |
100 | &bytes_read, (uint8_t *)buf) < 0) { |
101 | pr_err("mtd_read error while parsing (offset: 0x%X)!\n", | |
102 | offset); | |
103 | continue; | |
104 | } | |
105 | ||
4f8aaf72 RM |
106 | /* Magic or small NVRAM at 0x400 */ |
107 | if ((buf[0x4e0 / 4] == CFE_MAGIC && buf[0x4e4 / 4] == CFE_MAGIC) || | |
108 | (buf[0x400 / 4] == NVRAM_HEADER)) { | |
3cf7f131 RM |
109 | bcm47xxpart_add_part(&parts[curr_part++], "boot", |
110 | offset, MTD_WRITEABLE); | |
111 | continue; | |
112 | } | |
113 | ||
3cf7f131 RM |
114 | /* |
115 | * board_data starts with board_id which differs across boards, | |
116 | * but we can use 'MPFR' (hopefully) magic at 0x100 | |
117 | */ | |
118 | if (buf[0x100 / 4] == BOARD_DATA_MAGIC) { | |
119 | bcm47xxpart_add_part(&parts[curr_part++], "board_data", | |
120 | offset, MTD_WRITEABLE); | |
121 | continue; | |
122 | } | |
123 | ||
33094c73 RM |
124 | /* Found on Huawei E970 */ |
125 | if (buf[0x000 / 4] == FACTORY_MAGIC) { | |
126 | bcm47xxpart_add_part(&parts[curr_part++], "factory", | |
127 | offset, MTD_WRITEABLE); | |
128 | continue; | |
129 | } | |
130 | ||
3cf7f131 RM |
131 | /* POT(TOP) */ |
132 | if (buf[0x000 / 4] == POT_MAGIC1 && | |
133 | (buf[0x004 / 4] & 0xFFFF) == POT_MAGIC2) { | |
134 | bcm47xxpart_add_part(&parts[curr_part++], "POT", offset, | |
135 | MTD_WRITEABLE); | |
136 | continue; | |
137 | } | |
138 | ||
139 | /* ML */ | |
140 | if (buf[0x010 / 4] == ML_MAGIC1 && | |
141 | buf[0x014 / 4] == ML_MAGIC2) { | |
142 | bcm47xxpart_add_part(&parts[curr_part++], "ML", offset, | |
143 | MTD_WRITEABLE); | |
144 | continue; | |
145 | } | |
146 | ||
147 | /* TRX */ | |
148 | if (buf[0x000 / 4] == TRX_MAGIC) { | |
149 | trx = (struct trx_header *)buf; | |
150 | ||
396afe55 RM |
151 | trx_part = curr_part; |
152 | bcm47xxpart_add_part(&parts[curr_part++], "firmware", | |
153 | offset, 0); | |
154 | ||
3cf7f131 RM |
155 | i = 0; |
156 | /* We have LZMA loader if offset[2] points to sth */ | |
157 | if (trx->offset[2]) { | |
158 | bcm47xxpart_add_part(&parts[curr_part++], | |
159 | "loader", | |
160 | offset + trx->offset[i], | |
161 | 0); | |
162 | i++; | |
163 | } | |
164 | ||
165 | bcm47xxpart_add_part(&parts[curr_part++], "linux", | |
166 | offset + trx->offset[i], 0); | |
167 | i++; | |
168 | ||
169 | /* | |
170 | * Pure rootfs size is known and can be calculated as: | |
171 | * trx->length - trx->offset[i]. We don't fill it as | |
172 | * we want to have jffs2 (overlay) in the same mtd. | |
173 | */ | |
174 | bcm47xxpart_add_part(&parts[curr_part++], "rootfs", | |
175 | offset + trx->offset[i], 0); | |
176 | i++; | |
177 | ||
396afe55 RM |
178 | last_trx_part = curr_part - 1; |
179 | ||
3cf7f131 RM |
180 | /* |
181 | * We have whole TRX scanned, skip to the next part. Use | |
182 | * roundown (not roundup), as the loop will increase | |
183 | * offset in next step. | |
184 | */ | |
185 | offset = rounddown(offset + trx->length, blocksize); | |
186 | continue; | |
187 | } | |
020c6bcf RM |
188 | |
189 | /* Squashfs on devices not using TRX */ | |
190 | if (buf[0x000 / 4] == SQSH_MAGIC) { | |
191 | bcm47xxpart_add_part(&parts[curr_part++], "rootfs", | |
192 | offset, 0); | |
193 | continue; | |
194 | } | |
3cf7f131 | 195 | } |
91d542f4 RM |
196 | |
197 | /* Look for NVRAM at the end of the last block. */ | |
198 | for (i = 0; i < ARRAY_SIZE(possible_nvram_sizes); i++) { | |
199 | if (curr_part > BCM47XXPART_MAX_PARTS) { | |
200 | pr_warn("Reached maximum number of partitions, scanning stopped!\n"); | |
201 | break; | |
202 | } | |
203 | ||
204 | offset = master->size - possible_nvram_sizes[i]; | |
205 | if (mtd_read(master, offset, 0x4, &bytes_read, | |
206 | (uint8_t *)buf) < 0) { | |
207 | pr_err("mtd_read error while reading at offset 0x%X!\n", | |
208 | offset); | |
209 | continue; | |
210 | } | |
211 | ||
212 | /* Standard NVRAM */ | |
213 | if (buf[0] == NVRAM_HEADER) { | |
214 | bcm47xxpart_add_part(&parts[curr_part++], "nvram", | |
215 | master->size - blocksize, 0); | |
216 | break; | |
217 | } | |
218 | } | |
219 | ||
3cf7f131 RM |
220 | kfree(buf); |
221 | ||
222 | /* | |
223 | * Assume that partitions end at the beginning of the one they are | |
224 | * followed by. | |
225 | */ | |
648bdbee RM |
226 | for (i = 0; i < curr_part; i++) { |
227 | u64 next_part_offset = (i < curr_part - 1) ? | |
228 | parts[i + 1].offset : master->size; | |
229 | ||
230 | parts[i].size = next_part_offset - parts[i].offset; | |
396afe55 RM |
231 | if (i == last_trx_part && trx_part >= 0) |
232 | parts[trx_part].size = next_part_offset - | |
233 | parts[trx_part].offset; | |
648bdbee | 234 | } |
3cf7f131 RM |
235 | |
236 | *pparts = parts; | |
237 | return curr_part; | |
238 | }; | |
239 | ||
240 | static struct mtd_part_parser bcm47xxpart_mtd_parser = { | |
241 | .owner = THIS_MODULE, | |
242 | .parse_fn = bcm47xxpart_parse, | |
243 | .name = "bcm47xxpart", | |
244 | }; | |
245 | ||
246 | static int __init bcm47xxpart_init(void) | |
247 | { | |
6e14a61d AL |
248 | register_mtd_parser(&bcm47xxpart_mtd_parser); |
249 | return 0; | |
3cf7f131 RM |
250 | } |
251 | ||
252 | static void __exit bcm47xxpart_exit(void) | |
253 | { | |
254 | deregister_mtd_parser(&bcm47xxpart_mtd_parser); | |
255 | } | |
256 | ||
257 | module_init(bcm47xxpart_init); | |
258 | module_exit(bcm47xxpart_exit); | |
259 | ||
260 | MODULE_LICENSE("GPL"); | |
261 | MODULE_DESCRIPTION("MTD partitioning for BCM47XX flash memories"); |