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