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