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 | */ | |
26 | #define BCM47XXPART_BYTES_TO_READ 0x404 | |
27 | ||
3cf7f131 RM |
28 | /* 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 | ||
36 | struct 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 | ||
45 | static 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 | ||
53 | static 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; | |
25bad1d3 | 62 | uint32_t blocksize = master->erasesize; |
3cf7f131 | 63 | struct trx_header *trx; |
396afe55 RM |
64 | int trx_part = -1; |
65 | int last_trx_part = -1; | |
3cf7f131 | 66 | |
25bad1d3 HM |
67 | if (blocksize <= 0x10000) |
68 | blocksize = 0x10000; | |
3cf7f131 RM |
69 | |
70 | /* Alloc */ | |
71 | parts = kzalloc(sizeof(struct mtd_partition) * BCM47XXPART_MAX_PARTS, | |
72 | GFP_KERNEL); | |
5ca1088f | 73 | buf = kzalloc(BCM47XXPART_BYTES_TO_READ, GFP_KERNEL); |
3cf7f131 RM |
74 | |
75 | /* Parse block by block looking for magics */ | |
76 | for (offset = 0; offset <= master->size - blocksize; | |
77 | offset += blocksize) { | |
78 | /* Nothing more in higher memory */ | |
79 | if (offset >= 0x2000000) | |
80 | break; | |
81 | ||
82 | if (curr_part > BCM47XXPART_MAX_PARTS) { | |
83 | pr_warn("Reached maximum number of partitions, scanning stopped!\n"); | |
84 | break; | |
85 | } | |
86 | ||
87 | /* Read beginning of the block */ | |
5ca1088f | 88 | if (mtd_read(master, offset, BCM47XXPART_BYTES_TO_READ, |
3cf7f131 RM |
89 | &bytes_read, (uint8_t *)buf) < 0) { |
90 | pr_err("mtd_read error while parsing (offset: 0x%X)!\n", | |
91 | offset); | |
92 | continue; | |
93 | } | |
94 | ||
95 | /* CFE has small NVRAM at 0x400 */ | |
96 | if (buf[0x400 / 4] == NVRAM_HEADER) { | |
97 | bcm47xxpart_add_part(&parts[curr_part++], "boot", | |
98 | offset, MTD_WRITEABLE); | |
99 | continue; | |
100 | } | |
101 | ||
102 | /* Standard NVRAM */ | |
5ca1088f | 103 | if (buf[0x000 / 4] == NVRAM_HEADER) { |
3cf7f131 RM |
104 | bcm47xxpart_add_part(&parts[curr_part++], "nvram", |
105 | offset, 0); | |
106 | continue; | |
107 | } | |
108 | ||
109 | /* | |
110 | * board_data starts with board_id which differs across boards, | |
111 | * but we can use 'MPFR' (hopefully) magic at 0x100 | |
112 | */ | |
113 | if (buf[0x100 / 4] == BOARD_DATA_MAGIC) { | |
114 | bcm47xxpart_add_part(&parts[curr_part++], "board_data", | |
115 | offset, MTD_WRITEABLE); | |
116 | continue; | |
117 | } | |
118 | ||
119 | /* POT(TOP) */ | |
120 | if (buf[0x000 / 4] == POT_MAGIC1 && | |
121 | (buf[0x004 / 4] & 0xFFFF) == POT_MAGIC2) { | |
122 | bcm47xxpart_add_part(&parts[curr_part++], "POT", offset, | |
123 | MTD_WRITEABLE); | |
124 | continue; | |
125 | } | |
126 | ||
127 | /* ML */ | |
128 | if (buf[0x010 / 4] == ML_MAGIC1 && | |
129 | buf[0x014 / 4] == ML_MAGIC2) { | |
130 | bcm47xxpart_add_part(&parts[curr_part++], "ML", offset, | |
131 | MTD_WRITEABLE); | |
132 | continue; | |
133 | } | |
134 | ||
135 | /* TRX */ | |
136 | if (buf[0x000 / 4] == TRX_MAGIC) { | |
137 | trx = (struct trx_header *)buf; | |
138 | ||
396afe55 RM |
139 | trx_part = curr_part; |
140 | bcm47xxpart_add_part(&parts[curr_part++], "firmware", | |
141 | offset, 0); | |
142 | ||
3cf7f131 RM |
143 | i = 0; |
144 | /* We have LZMA loader if offset[2] points to sth */ | |
145 | if (trx->offset[2]) { | |
146 | bcm47xxpart_add_part(&parts[curr_part++], | |
147 | "loader", | |
148 | offset + trx->offset[i], | |
149 | 0); | |
150 | i++; | |
151 | } | |
152 | ||
153 | bcm47xxpart_add_part(&parts[curr_part++], "linux", | |
154 | offset + trx->offset[i], 0); | |
155 | i++; | |
156 | ||
157 | /* | |
158 | * Pure rootfs size is known and can be calculated as: | |
159 | * trx->length - trx->offset[i]. We don't fill it as | |
160 | * we want to have jffs2 (overlay) in the same mtd. | |
161 | */ | |
162 | bcm47xxpart_add_part(&parts[curr_part++], "rootfs", | |
163 | offset + trx->offset[i], 0); | |
164 | i++; | |
165 | ||
396afe55 RM |
166 | last_trx_part = curr_part - 1; |
167 | ||
3cf7f131 RM |
168 | /* |
169 | * We have whole TRX scanned, skip to the next part. Use | |
170 | * roundown (not roundup), as the loop will increase | |
171 | * offset in next step. | |
172 | */ | |
173 | offset = rounddown(offset + trx->length, blocksize); | |
174 | continue; | |
175 | } | |
176 | } | |
177 | kfree(buf); | |
178 | ||
179 | /* | |
180 | * Assume that partitions end at the beginning of the one they are | |
181 | * followed by. | |
182 | */ | |
648bdbee RM |
183 | for (i = 0; i < curr_part; i++) { |
184 | u64 next_part_offset = (i < curr_part - 1) ? | |
185 | parts[i + 1].offset : master->size; | |
186 | ||
187 | parts[i].size = next_part_offset - parts[i].offset; | |
396afe55 RM |
188 | if (i == last_trx_part && trx_part >= 0) |
189 | parts[trx_part].size = next_part_offset - | |
190 | parts[trx_part].offset; | |
648bdbee | 191 | } |
3cf7f131 RM |
192 | |
193 | *pparts = parts; | |
194 | return curr_part; | |
195 | }; | |
196 | ||
197 | static struct mtd_part_parser bcm47xxpart_mtd_parser = { | |
198 | .owner = THIS_MODULE, | |
199 | .parse_fn = bcm47xxpart_parse, | |
200 | .name = "bcm47xxpart", | |
201 | }; | |
202 | ||
203 | static int __init bcm47xxpart_init(void) | |
204 | { | |
205 | return register_mtd_parser(&bcm47xxpart_mtd_parser); | |
206 | } | |
207 | ||
208 | static void __exit bcm47xxpart_exit(void) | |
209 | { | |
210 | deregister_mtd_parser(&bcm47xxpart_mtd_parser); | |
211 | } | |
212 | ||
213 | module_init(bcm47xxpart_init); | |
214 | module_exit(bcm47xxpart_exit); | |
215 | ||
216 | MODULE_LICENSE("GPL"); | |
217 | MODULE_DESCRIPTION("MTD partitioning for BCM47XX flash memories"); |