blob: eafbf521a547c4fb79f99746188451dd3eda4e31 [file] [log] [blame]
Jonas Gorski70a3c162011-12-05 16:08:08 +01001/*
2 * BCM63XX CFE image tag parser
3 *
4 * Copyright © 2006-2008 Florian Fainelli <florian@openwrt.org>
5 * Mike Albon <malbon@openwrt.org>
6 * Copyright © 2009-2010 Daniel Dickinson <openwrt@cshore.neomailbox.net>
Jonas Gorskid085eea2013-03-23 14:07:49 +01007 * Copyright © 2011-2013 Jonas Gorski <jonas.gorski@gmail.com>
Jonas Gorski70a3c162011-12-05 16:08:08 +01008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 */
24
25#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26
Simon Arlott436e94a62015-12-13 22:49:26 +000027#include <linux/bcm963xx_nvram.h>
Simon Arlott8fce60b2015-12-13 22:46:59 +000028#include <linux/bcm963xx_tag.h>
Jonas Gorskif98872f2011-12-17 13:58:18 +010029#include <linux/crc32.h>
Jonas Gorski70a3c162011-12-05 16:08:08 +010030#include <linux/module.h>
31#include <linux/kernel.h>
Jonas Gorskic88fd9d2013-03-23 14:07:47 +010032#include <linux/sizes.h>
Jonas Gorski70a3c162011-12-05 16:08:08 +010033#include <linux/slab.h>
34#include <linux/vmalloc.h>
35#include <linux/mtd/mtd.h>
36#include <linux/mtd/partitions.h>
37
Simon Arlott436e94a62015-12-13 22:49:26 +000038#define BCM963XX_CFE_BLOCK_SIZE SZ_64K /* always at least 64KiB */
Jonas Gorski70a3c162011-12-05 16:08:08 +010039
Simon Arlott436e94a62015-12-13 22:49:26 +000040#define BCM963XX_CFE_MAGIC_OFFSET 0x4e0
41#define BCM963XX_CFE_VERSION_OFFSET 0x570
42#define BCM963XX_NVRAM_OFFSET 0x580
Jonas Gorskif2d97392011-12-17 13:58:14 +010043
Simon Arlott7fffa692015-12-13 22:51:02 +000044/* Ensure strings read from flash structs are null terminated */
45#define STR_NULL_TERMINATE(x) \
46 do { char *_str = (x); _str[sizeof(x) - 1] = 0; } while (0)
47
Jonas Gorski70a3c162011-12-05 16:08:08 +010048static int bcm63xx_detect_cfe(struct mtd_info *master)
49{
Jonas Gorski70a3c162011-12-05 16:08:08 +010050 char buf[9];
51 int ret;
52 size_t retlen;
53
Artem Bityutskiy329ad392011-12-23 17:30:16 +020054 ret = mtd_read(master, BCM963XX_CFE_VERSION_OFFSET, 5, &retlen,
55 (void *)buf);
Jonas Gorski70a3c162011-12-05 16:08:08 +010056 buf[retlen] = 0;
Jonas Gorski70a3c162011-12-05 16:08:08 +010057
Jonas Gorskif2d97392011-12-17 13:58:14 +010058 if (ret)
59 return ret;
60
61 if (strncmp("cfe-v", buf, 5) == 0)
62 return 0;
63
64 /* very old CFE's do not have the cfe-v string, so check for magic */
Simon Arlott436e94a62015-12-13 22:49:26 +000065 ret = mtd_read(master, BCM963XX_CFE_MAGIC_OFFSET, 8, &retlen,
Artem Bityutskiy329ad392011-12-23 17:30:16 +020066 (void *)buf);
Jonas Gorskif2d97392011-12-17 13:58:14 +010067 buf[retlen] = 0;
68
69 return strncmp("CFE1CFE1", buf, 8);
Jonas Gorski70a3c162011-12-05 16:08:08 +010070}
71
Simon Arlott436e94a62015-12-13 22:49:26 +000072static int bcm63xx_read_nvram(struct mtd_info *master,
73 struct bcm963xx_nvram *nvram)
74{
75 u32 actual_crc, expected_crc;
76 size_t retlen;
77 int ret;
78
79 /* extract nvram data */
80 ret = mtd_read(master, BCM963XX_NVRAM_OFFSET, BCM963XX_NVRAM_V5_SIZE,
81 &retlen, (void *)nvram);
82 if (ret)
83 return ret;
84
85 ret = bcm963xx_nvram_checksum(nvram, &expected_crc, &actual_crc);
86 if (ret)
87 pr_warn("nvram checksum failed, contents may be invalid (expected %08x, got %08x)\n",
88 expected_crc, actual_crc);
89
90 if (!nvram->psi_size)
91 nvram->psi_size = BCM963XX_DEFAULT_PSI_SIZE;
92
93 return 0;
94}
95
Simon Arlott7fffa692015-12-13 22:51:02 +000096static int bcm63xx_read_image_tag(struct mtd_info *master, const char *name,
97 loff_t tag_offset, struct bcm_tag *buf)
98{
99 int ret;
100 size_t retlen;
101 u32 computed_crc;
102
103 ret = mtd_read(master, tag_offset, sizeof(*buf), &retlen, (void *)buf);
104 if (ret)
105 return ret;
106
107 if (retlen != sizeof(*buf))
108 return -EIO;
109
110 computed_crc = crc32_le(IMAGETAG_CRC_START, (u8 *)buf,
111 offsetof(struct bcm_tag, header_crc));
112 if (computed_crc == buf->header_crc) {
113 STR_NULL_TERMINATE(buf->board_id);
114 STR_NULL_TERMINATE(buf->tag_version);
115
116 pr_info("%s: CFE image tag found at 0x%llx with version %s, board type %s\n",
117 name, tag_offset, buf->tag_version, buf->board_id);
118
119 return 0;
120 }
121
122 pr_warn("%s: CFE image tag at 0x%llx CRC invalid (expected %08x, actual %08x)\n",
123 name, tag_offset, buf->header_crc, computed_crc);
124 return 1;
125}
126
Jonas Gorski70a3c162011-12-05 16:08:08 +0100127static int bcm63xx_parse_cfe_partitions(struct mtd_info *master,
Brian Norrisb9adf462015-12-04 15:25:14 -0800128 const struct mtd_partition **pparts,
Jonas Gorski70a3c162011-12-05 16:08:08 +0100129 struct mtd_part_parser_data *data)
130{
131 /* CFE, NVRAM and global Linux are always present */
132 int nrparts = 3, curpart = 0;
Simon Arlott436e94a62015-12-13 22:49:26 +0000133 struct bcm963xx_nvram *nvram = NULL;
134 struct bcm_tag *buf = NULL;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100135 struct mtd_partition *parts;
136 int ret;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100137 unsigned int rootfsaddr, kerneladdr, spareaddr;
138 unsigned int rootfslen, kernellen, sparelen, totallen;
Jonas Gorski678eb9b2011-12-19 11:36:04 +0100139 unsigned int cfelen, nvramlen;
Jonas Gorski4e4fb632012-11-12 10:52:49 +0100140 unsigned int cfe_erasesize;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100141 int i;
Jonas Gorski5329d672012-04-19 13:15:57 +0200142 bool rootfs_first = false;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100143
144 if (bcm63xx_detect_cfe(master))
145 return -EINVAL;
146
Simon Arlott436e94a62015-12-13 22:49:26 +0000147 nvram = vzalloc(sizeof(*nvram));
148 if (!nvram)
149 return -ENOMEM;
150
151 ret = bcm63xx_read_nvram(master, nvram);
152 if (ret)
153 goto out;
154
Jonas Gorski4e4fb632012-11-12 10:52:49 +0100155 cfe_erasesize = max_t(uint32_t, master->erasesize,
Simon Arlott436e94a62015-12-13 22:49:26 +0000156 BCM963XX_CFE_BLOCK_SIZE);
Jonas Gorski4e4fb632012-11-12 10:52:49 +0100157
158 cfelen = cfe_erasesize;
Simon Arlott436e94a62015-12-13 22:49:26 +0000159 nvramlen = nvram->psi_size * SZ_1K;
Jonas Gorskid085eea2013-03-23 14:07:49 +0100160 nvramlen = roundup(nvramlen, cfe_erasesize);
Jonas Gorski678eb9b2011-12-19 11:36:04 +0100161
Jonas Gorski70a3c162011-12-05 16:08:08 +0100162 /* Allocate memory for buffer */
163 buf = vmalloc(sizeof(struct bcm_tag));
Simon Arlott436e94a62015-12-13 22:49:26 +0000164 if (!buf) {
165 ret = -ENOMEM;
166 goto out;
167 }
Jonas Gorski70a3c162011-12-05 16:08:08 +0100168
169 /* Get the tag */
Simon Arlott7fffa692015-12-13 22:51:02 +0000170 ret = bcm63xx_read_image_tag(master, "rootfs", cfelen, buf);
171 if (!ret) {
Jonas Gorski5329d672012-04-19 13:15:57 +0200172 sscanf(buf->flash_image_start, "%u", &rootfsaddr);
Jonas Gorskif98872f2011-12-17 13:58:18 +0100173 sscanf(buf->kernel_address, "%u", &kerneladdr);
174 sscanf(buf->kernel_length, "%u", &kernellen);
175 sscanf(buf->total_length, "%u", &totallen);
Jonas Gorski70a3c162011-12-05 16:08:08 +0100176
Simon Arlott1f29cb12015-12-13 22:47:55 +0000177 kerneladdr = kerneladdr - BCM963XX_EXTENDED_SIZE;
178 rootfsaddr = rootfsaddr - BCM963XX_EXTENDED_SIZE;
Jonas Gorskif98872f2011-12-17 13:58:18 +0100179 spareaddr = roundup(totallen, master->erasesize) + cfelen;
Jonas Gorski5329d672012-04-19 13:15:57 +0200180
181 if (rootfsaddr < kerneladdr) {
182 /* default Broadcom layout */
183 rootfslen = kerneladdr - rootfsaddr;
184 rootfs_first = true;
185 } else {
186 /* OpenWrt layout */
187 rootfsaddr = kerneladdr + kernellen;
188 rootfslen = spareaddr - rootfsaddr;
189 }
Simon Arlott7fffa692015-12-13 22:51:02 +0000190 } else if (ret > 0) {
Jonas Gorskif98872f2011-12-17 13:58:18 +0100191 kernellen = 0;
192 rootfslen = 0;
193 rootfsaddr = 0;
194 spareaddr = cfelen;
Simon Arlott7fffa692015-12-13 22:51:02 +0000195 } else {
196 goto out;
Jonas Gorskif98872f2011-12-17 13:58:18 +0100197 }
Jonas Gorskie1904012012-11-12 10:52:48 +0100198 sparelen = master->size - spareaddr - nvramlen;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100199
200 /* Determine number of partitions */
Jonas Gorskifafc3d62012-11-12 10:52:47 +0100201 if (rootfslen > 0)
Jonas Gorski70a3c162011-12-05 16:08:08 +0100202 nrparts++;
Jonas Gorskifafc3d62012-11-12 10:52:47 +0100203
204 if (kernellen > 0)
Jonas Gorski70a3c162011-12-05 16:08:08 +0100205 nrparts++;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100206
207 /* Ask kernel for more memory */
208 parts = kzalloc(sizeof(*parts) * nrparts + 10 * nrparts, GFP_KERNEL);
209 if (!parts) {
Simon Arlott436e94a62015-12-13 22:49:26 +0000210 ret = -ENOMEM;
211 goto out;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100212 }
213
214 /* Start building partition list */
215 parts[curpart].name = "CFE";
216 parts[curpart].offset = 0;
Jonas Gorski678eb9b2011-12-19 11:36:04 +0100217 parts[curpart].size = cfelen;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100218 curpart++;
219
220 if (kernellen > 0) {
Jonas Gorski5329d672012-04-19 13:15:57 +0200221 int kernelpart = curpart;
222
223 if (rootfslen > 0 && rootfs_first)
224 kernelpart++;
225 parts[kernelpart].name = "kernel";
226 parts[kernelpart].offset = kerneladdr;
227 parts[kernelpart].size = kernellen;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100228 curpart++;
229 }
230
231 if (rootfslen > 0) {
Jonas Gorski5329d672012-04-19 13:15:57 +0200232 int rootfspart = curpart;
233
234 if (kernellen > 0 && rootfs_first)
235 rootfspart--;
236 parts[rootfspart].name = "rootfs";
237 parts[rootfspart].offset = rootfsaddr;
238 parts[rootfspart].size = rootfslen;
239 if (sparelen > 0 && !rootfs_first)
240 parts[rootfspart].size += sparelen;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100241 curpart++;
242 }
243
244 parts[curpart].name = "nvram";
Jonas Gorski678eb9b2011-12-19 11:36:04 +0100245 parts[curpart].offset = master->size - nvramlen;
246 parts[curpart].size = nvramlen;
Jonas Gorskif3f9a5d2012-11-12 10:52:50 +0100247 curpart++;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100248
249 /* Global partition "linux" to make easy firmware upgrade */
Jonas Gorski70a3c162011-12-05 16:08:08 +0100250 parts[curpart].name = "linux";
Jonas Gorski327c62c2011-12-17 13:58:16 +0100251 parts[curpart].offset = cfelen;
252 parts[curpart].size = master->size - cfelen - nvramlen;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100253
254 for (i = 0; i < nrparts; i++)
Jonas Gorski60768362012-11-12 10:52:51 +0100255 pr_info("Partition %d is %s offset %llx and length %llx\n", i,
256 parts[i].name, parts[i].offset, parts[i].size);
Jonas Gorski70a3c162011-12-05 16:08:08 +0100257
258 pr_info("Spare partition is offset %x and length %x\n", spareaddr,
259 sparelen);
260
261 *pparts = parts;
Simon Arlott436e94a62015-12-13 22:49:26 +0000262 ret = 0;
263
264out:
265 vfree(nvram);
Jonas Gorski70a3c162011-12-05 16:08:08 +0100266 vfree(buf);
267
Simon Arlott436e94a62015-12-13 22:49:26 +0000268 if (ret)
269 return ret;
270
Jonas Gorski70a3c162011-12-05 16:08:08 +0100271 return nrparts;
272};
273
274static struct mtd_part_parser bcm63xx_cfe_parser = {
Jonas Gorski70a3c162011-12-05 16:08:08 +0100275 .parse_fn = bcm63xx_parse_cfe_partitions,
276 .name = "bcm63xxpart",
277};
Brian Norrisb8f70ba2015-11-11 19:13:30 -0800278module_mtd_part_parser(bcm63xx_cfe_parser);
Jonas Gorski70a3c162011-12-05 16:08:08 +0100279
280MODULE_LICENSE("GPL");
281MODULE_AUTHOR("Daniel Dickinson <openwrt@cshore.neomailbox.net>");
282MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
283MODULE_AUTHOR("Mike Albon <malbon@openwrt.org>");
284MODULE_AUTHOR("Jonas Gorski <jonas.gorski@gmail.com");
285MODULE_DESCRIPTION("MTD partitioning for BCM63XX CFE bootloaders");