blob: 5c813907661c3415c979b1176bf937f7c086f2c8 [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
Jonas Gorskif98872f2011-12-17 13:58:18 +010027#include <linux/crc32.h>
Jonas Gorski70a3c162011-12-05 16:08:08 +010028#include <linux/module.h>
29#include <linux/kernel.h>
Jonas Gorskic88fd9d2013-03-23 14:07:47 +010030#include <linux/sizes.h>
Jonas Gorski70a3c162011-12-05 16:08:08 +010031#include <linux/slab.h>
32#include <linux/vmalloc.h>
33#include <linux/mtd/mtd.h>
34#include <linux/mtd/partitions.h>
35
Jonas Gorskid085eea2013-03-23 14:07:49 +010036#include <asm/mach-bcm63xx/bcm63xx_nvram.h>
Jonas Gorski70a3c162011-12-05 16:08:08 +010037#include <asm/mach-bcm63xx/bcm963xx_tag.h>
Jonas Gorskif2d97392011-12-17 13:58:14 +010038#include <asm/mach-bcm63xx/board_bcm963xx.h>
Jonas Gorski70a3c162011-12-05 16:08:08 +010039
40#define BCM63XX_EXTENDED_SIZE 0xBFC00000 /* Extended flash address */
41
Jonas Gorskic88fd9d2013-03-23 14:07:47 +010042#define BCM63XX_CFE_BLOCK_SIZE SZ_64K /* always at least 64KiB */
Jonas Gorski678eb9b2011-12-19 11:36:04 +010043
Jonas Gorskif2d97392011-12-17 13:58:14 +010044#define BCM63XX_CFE_MAGIC_OFFSET 0x4e0
45
Jonas Gorski70a3c162011-12-05 16:08:08 +010046static int bcm63xx_detect_cfe(struct mtd_info *master)
47{
Jonas Gorski70a3c162011-12-05 16:08:08 +010048 char buf[9];
49 int ret;
50 size_t retlen;
51
Artem Bityutskiy329ad392011-12-23 17:30:16 +020052 ret = mtd_read(master, BCM963XX_CFE_VERSION_OFFSET, 5, &retlen,
53 (void *)buf);
Jonas Gorski70a3c162011-12-05 16:08:08 +010054 buf[retlen] = 0;
Jonas Gorski70a3c162011-12-05 16:08:08 +010055
Jonas Gorskif2d97392011-12-17 13:58:14 +010056 if (ret)
57 return ret;
58
59 if (strncmp("cfe-v", buf, 5) == 0)
60 return 0;
61
62 /* very old CFE's do not have the cfe-v string, so check for magic */
Artem Bityutskiy329ad392011-12-23 17:30:16 +020063 ret = mtd_read(master, BCM63XX_CFE_MAGIC_OFFSET, 8, &retlen,
64 (void *)buf);
Jonas Gorskif2d97392011-12-17 13:58:14 +010065 buf[retlen] = 0;
66
67 return strncmp("CFE1CFE1", buf, 8);
Jonas Gorski70a3c162011-12-05 16:08:08 +010068}
69
70static int bcm63xx_parse_cfe_partitions(struct mtd_info *master,
71 struct mtd_partition **pparts,
72 struct mtd_part_parser_data *data)
73{
74 /* CFE, NVRAM and global Linux are always present */
75 int nrparts = 3, curpart = 0;
76 struct bcm_tag *buf;
77 struct mtd_partition *parts;
78 int ret;
79 size_t retlen;
80 unsigned int rootfsaddr, kerneladdr, spareaddr;
81 unsigned int rootfslen, kernellen, sparelen, totallen;
Jonas Gorski678eb9b2011-12-19 11:36:04 +010082 unsigned int cfelen, nvramlen;
Jonas Gorski4e4fb632012-11-12 10:52:49 +010083 unsigned int cfe_erasesize;
Jonas Gorski70a3c162011-12-05 16:08:08 +010084 int i;
Jonas Gorskif98872f2011-12-17 13:58:18 +010085 u32 computed_crc;
Jonas Gorski5329d672012-04-19 13:15:57 +020086 bool rootfs_first = false;
Jonas Gorski70a3c162011-12-05 16:08:08 +010087
88 if (bcm63xx_detect_cfe(master))
89 return -EINVAL;
90
Jonas Gorski4e4fb632012-11-12 10:52:49 +010091 cfe_erasesize = max_t(uint32_t, master->erasesize,
92 BCM63XX_CFE_BLOCK_SIZE);
93
94 cfelen = cfe_erasesize;
Jonas Gorskid085eea2013-03-23 14:07:49 +010095 nvramlen = bcm63xx_nvram_get_psi_size() * SZ_1K;
96 nvramlen = roundup(nvramlen, cfe_erasesize);
Jonas Gorski678eb9b2011-12-19 11:36:04 +010097
Jonas Gorski70a3c162011-12-05 16:08:08 +010098 /* Allocate memory for buffer */
99 buf = vmalloc(sizeof(struct bcm_tag));
100 if (!buf)
101 return -ENOMEM;
102
103 /* Get the tag */
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200104 ret = mtd_read(master, cfelen, sizeof(struct bcm_tag), &retlen,
105 (void *)buf);
Jonas Gorski678eb9b2011-12-19 11:36:04 +0100106
Jonas Gorski70a3c162011-12-05 16:08:08 +0100107 if (retlen != sizeof(struct bcm_tag)) {
108 vfree(buf);
109 return -EIO;
110 }
111
Jonas Gorskif98872f2011-12-17 13:58:18 +0100112 computed_crc = crc32_le(IMAGETAG_CRC_START, (u8 *)buf,
113 offsetof(struct bcm_tag, header_crc));
114 if (computed_crc == buf->header_crc) {
115 char *boardid = &(buf->board_id[0]);
116 char *tagversion = &(buf->tag_version[0]);
Jonas Gorski70a3c162011-12-05 16:08:08 +0100117
Jonas Gorski5329d672012-04-19 13:15:57 +0200118 sscanf(buf->flash_image_start, "%u", &rootfsaddr);
Jonas Gorskif98872f2011-12-17 13:58:18 +0100119 sscanf(buf->kernel_address, "%u", &kerneladdr);
120 sscanf(buf->kernel_length, "%u", &kernellen);
121 sscanf(buf->total_length, "%u", &totallen);
Jonas Gorski70a3c162011-12-05 16:08:08 +0100122
Jonas Gorskif98872f2011-12-17 13:58:18 +0100123 pr_info("CFE boot tag found with version %s and board type %s\n",
124 tagversion, boardid);
125
126 kerneladdr = kerneladdr - BCM63XX_EXTENDED_SIZE;
Jonas Gorski5329d672012-04-19 13:15:57 +0200127 rootfsaddr = rootfsaddr - BCM63XX_EXTENDED_SIZE;
Jonas Gorskif98872f2011-12-17 13:58:18 +0100128 spareaddr = roundup(totallen, master->erasesize) + cfelen;
Jonas Gorski5329d672012-04-19 13:15:57 +0200129
130 if (rootfsaddr < kerneladdr) {
131 /* default Broadcom layout */
132 rootfslen = kerneladdr - rootfsaddr;
133 rootfs_first = true;
134 } else {
135 /* OpenWrt layout */
136 rootfsaddr = kerneladdr + kernellen;
137 rootfslen = spareaddr - rootfsaddr;
138 }
Jonas Gorskif98872f2011-12-17 13:58:18 +0100139 } else {
140 pr_warn("CFE boot tag CRC invalid (expected %08x, actual %08x)\n",
141 buf->header_crc, computed_crc);
142 kernellen = 0;
143 rootfslen = 0;
144 rootfsaddr = 0;
145 spareaddr = cfelen;
Jonas Gorskif98872f2011-12-17 13:58:18 +0100146 }
Jonas Gorskie1904012012-11-12 10:52:48 +0100147 sparelen = master->size - spareaddr - nvramlen;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100148
149 /* Determine number of partitions */
Jonas Gorskifafc3d62012-11-12 10:52:47 +0100150 if (rootfslen > 0)
Jonas Gorski70a3c162011-12-05 16:08:08 +0100151 nrparts++;
Jonas Gorskifafc3d62012-11-12 10:52:47 +0100152
153 if (kernellen > 0)
Jonas Gorski70a3c162011-12-05 16:08:08 +0100154 nrparts++;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100155
156 /* Ask kernel for more memory */
157 parts = kzalloc(sizeof(*parts) * nrparts + 10 * nrparts, GFP_KERNEL);
158 if (!parts) {
159 vfree(buf);
160 return -ENOMEM;
161 }
162
163 /* Start building partition list */
164 parts[curpart].name = "CFE";
165 parts[curpart].offset = 0;
Jonas Gorski678eb9b2011-12-19 11:36:04 +0100166 parts[curpart].size = cfelen;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100167 curpart++;
168
169 if (kernellen > 0) {
Jonas Gorski5329d672012-04-19 13:15:57 +0200170 int kernelpart = curpart;
171
172 if (rootfslen > 0 && rootfs_first)
173 kernelpart++;
174 parts[kernelpart].name = "kernel";
175 parts[kernelpart].offset = kerneladdr;
176 parts[kernelpart].size = kernellen;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100177 curpart++;
178 }
179
180 if (rootfslen > 0) {
Jonas Gorski5329d672012-04-19 13:15:57 +0200181 int rootfspart = curpart;
182
183 if (kernellen > 0 && rootfs_first)
184 rootfspart--;
185 parts[rootfspart].name = "rootfs";
186 parts[rootfspart].offset = rootfsaddr;
187 parts[rootfspart].size = rootfslen;
188 if (sparelen > 0 && !rootfs_first)
189 parts[rootfspart].size += sparelen;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100190 curpart++;
191 }
192
193 parts[curpart].name = "nvram";
Jonas Gorski678eb9b2011-12-19 11:36:04 +0100194 parts[curpart].offset = master->size - nvramlen;
195 parts[curpart].size = nvramlen;
Jonas Gorskif3f9a5d2012-11-12 10:52:50 +0100196 curpart++;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100197
198 /* Global partition "linux" to make easy firmware upgrade */
Jonas Gorski70a3c162011-12-05 16:08:08 +0100199 parts[curpart].name = "linux";
Jonas Gorski327c62c2011-12-17 13:58:16 +0100200 parts[curpart].offset = cfelen;
201 parts[curpart].size = master->size - cfelen - nvramlen;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100202
203 for (i = 0; i < nrparts; i++)
Jonas Gorski60768362012-11-12 10:52:51 +0100204 pr_info("Partition %d is %s offset %llx and length %llx\n", i,
205 parts[i].name, parts[i].offset, parts[i].size);
Jonas Gorski70a3c162011-12-05 16:08:08 +0100206
207 pr_info("Spare partition is offset %x and length %x\n", spareaddr,
208 sparelen);
209
210 *pparts = parts;
211 vfree(buf);
212
213 return nrparts;
214};
215
216static struct mtd_part_parser bcm63xx_cfe_parser = {
217 .owner = THIS_MODULE,
218 .parse_fn = bcm63xx_parse_cfe_partitions,
219 .name = "bcm63xxpart",
220};
221
222static int __init bcm63xx_cfe_parser_init(void)
223{
224 return register_mtd_parser(&bcm63xx_cfe_parser);
225}
226
227static void __exit bcm63xx_cfe_parser_exit(void)
228{
229 deregister_mtd_parser(&bcm63xx_cfe_parser);
230}
231
232module_init(bcm63xx_cfe_parser_init);
233module_exit(bcm63xx_cfe_parser_exit);
234
235MODULE_LICENSE("GPL");
236MODULE_AUTHOR("Daniel Dickinson <openwrt@cshore.neomailbox.net>");
237MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
238MODULE_AUTHOR("Mike Albon <malbon@openwrt.org>");
239MODULE_AUTHOR("Jonas Gorski <jonas.gorski@gmail.com");
240MODULE_DESCRIPTION("MTD partitioning for BCM63XX CFE bootloaders");