blob: 6eeb84c81bc2ceb16a451fae1bc4194505b2dc7f [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 Gorski5329d672012-04-19 13:15:57 +02007 * Copyright © 2011-2012 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>
30#include <linux/slab.h>
31#include <linux/vmalloc.h>
32#include <linux/mtd/mtd.h>
33#include <linux/mtd/partitions.h>
34
35#include <asm/mach-bcm63xx/bcm963xx_tag.h>
Jonas Gorskif2d97392011-12-17 13:58:14 +010036#include <asm/mach-bcm63xx/board_bcm963xx.h>
Jonas Gorski70a3c162011-12-05 16:08:08 +010037
38#define BCM63XX_EXTENDED_SIZE 0xBFC00000 /* Extended flash address */
39
Jonas Gorski4e4fb632012-11-12 10:52:49 +010040#define BCM63XX_CFE_BLOCK_SIZE 0x10000 /* always at least 64KiB */
Jonas Gorski678eb9b2011-12-19 11:36:04 +010041
Jonas Gorskif2d97392011-12-17 13:58:14 +010042#define BCM63XX_CFE_MAGIC_OFFSET 0x4e0
43
Jonas Gorski70a3c162011-12-05 16:08:08 +010044static int bcm63xx_detect_cfe(struct mtd_info *master)
45{
Jonas Gorski70a3c162011-12-05 16:08:08 +010046 char buf[9];
47 int ret;
48 size_t retlen;
49
Artem Bityutskiy329ad392011-12-23 17:30:16 +020050 ret = mtd_read(master, BCM963XX_CFE_VERSION_OFFSET, 5, &retlen,
51 (void *)buf);
Jonas Gorski70a3c162011-12-05 16:08:08 +010052 buf[retlen] = 0;
Jonas Gorski70a3c162011-12-05 16:08:08 +010053
Jonas Gorskif2d97392011-12-17 13:58:14 +010054 if (ret)
55 return ret;
56
57 if (strncmp("cfe-v", buf, 5) == 0)
58 return 0;
59
60 /* very old CFE's do not have the cfe-v string, so check for magic */
Artem Bityutskiy329ad392011-12-23 17:30:16 +020061 ret = mtd_read(master, BCM63XX_CFE_MAGIC_OFFSET, 8, &retlen,
62 (void *)buf);
Jonas Gorskif2d97392011-12-17 13:58:14 +010063 buf[retlen] = 0;
64
65 return strncmp("CFE1CFE1", buf, 8);
Jonas Gorski70a3c162011-12-05 16:08:08 +010066}
67
68static int bcm63xx_parse_cfe_partitions(struct mtd_info *master,
69 struct mtd_partition **pparts,
70 struct mtd_part_parser_data *data)
71{
72 /* CFE, NVRAM and global Linux are always present */
73 int nrparts = 3, curpart = 0;
74 struct bcm_tag *buf;
75 struct mtd_partition *parts;
76 int ret;
77 size_t retlen;
78 unsigned int rootfsaddr, kerneladdr, spareaddr;
79 unsigned int rootfslen, kernellen, sparelen, totallen;
Jonas Gorski678eb9b2011-12-19 11:36:04 +010080 unsigned int cfelen, nvramlen;
Jonas Gorski4e4fb632012-11-12 10:52:49 +010081 unsigned int cfe_erasesize;
Jonas Gorski70a3c162011-12-05 16:08:08 +010082 int i;
Jonas Gorskif98872f2011-12-17 13:58:18 +010083 u32 computed_crc;
Jonas Gorski5329d672012-04-19 13:15:57 +020084 bool rootfs_first = false;
Jonas Gorski70a3c162011-12-05 16:08:08 +010085
86 if (bcm63xx_detect_cfe(master))
87 return -EINVAL;
88
Jonas Gorski4e4fb632012-11-12 10:52:49 +010089 cfe_erasesize = max_t(uint32_t, master->erasesize,
90 BCM63XX_CFE_BLOCK_SIZE);
91
92 cfelen = cfe_erasesize;
93 nvramlen = cfe_erasesize;
Jonas Gorski678eb9b2011-12-19 11:36:04 +010094
Jonas Gorski70a3c162011-12-05 16:08:08 +010095 /* Allocate memory for buffer */
96 buf = vmalloc(sizeof(struct bcm_tag));
97 if (!buf)
98 return -ENOMEM;
99
100 /* Get the tag */
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200101 ret = mtd_read(master, cfelen, sizeof(struct bcm_tag), &retlen,
102 (void *)buf);
Jonas Gorski678eb9b2011-12-19 11:36:04 +0100103
Jonas Gorski70a3c162011-12-05 16:08:08 +0100104 if (retlen != sizeof(struct bcm_tag)) {
105 vfree(buf);
106 return -EIO;
107 }
108
Jonas Gorskif98872f2011-12-17 13:58:18 +0100109 computed_crc = crc32_le(IMAGETAG_CRC_START, (u8 *)buf,
110 offsetof(struct bcm_tag, header_crc));
111 if (computed_crc == buf->header_crc) {
112 char *boardid = &(buf->board_id[0]);
113 char *tagversion = &(buf->tag_version[0]);
Jonas Gorski70a3c162011-12-05 16:08:08 +0100114
Jonas Gorski5329d672012-04-19 13:15:57 +0200115 sscanf(buf->flash_image_start, "%u", &rootfsaddr);
Jonas Gorskif98872f2011-12-17 13:58:18 +0100116 sscanf(buf->kernel_address, "%u", &kerneladdr);
117 sscanf(buf->kernel_length, "%u", &kernellen);
118 sscanf(buf->total_length, "%u", &totallen);
Jonas Gorski70a3c162011-12-05 16:08:08 +0100119
Jonas Gorskif98872f2011-12-17 13:58:18 +0100120 pr_info("CFE boot tag found with version %s and board type %s\n",
121 tagversion, boardid);
122
123 kerneladdr = kerneladdr - BCM63XX_EXTENDED_SIZE;
Jonas Gorski5329d672012-04-19 13:15:57 +0200124 rootfsaddr = rootfsaddr - BCM63XX_EXTENDED_SIZE;
Jonas Gorskif98872f2011-12-17 13:58:18 +0100125 spareaddr = roundup(totallen, master->erasesize) + cfelen;
Jonas Gorski5329d672012-04-19 13:15:57 +0200126
127 if (rootfsaddr < kerneladdr) {
128 /* default Broadcom layout */
129 rootfslen = kerneladdr - rootfsaddr;
130 rootfs_first = true;
131 } else {
132 /* OpenWrt layout */
133 rootfsaddr = kerneladdr + kernellen;
134 rootfslen = spareaddr - rootfsaddr;
135 }
Jonas Gorskif98872f2011-12-17 13:58:18 +0100136 } else {
137 pr_warn("CFE boot tag CRC invalid (expected %08x, actual %08x)\n",
138 buf->header_crc, computed_crc);
139 kernellen = 0;
140 rootfslen = 0;
141 rootfsaddr = 0;
142 spareaddr = cfelen;
Jonas Gorskif98872f2011-12-17 13:58:18 +0100143 }
Jonas Gorskie1904012012-11-12 10:52:48 +0100144 sparelen = master->size - spareaddr - nvramlen;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100145
146 /* Determine number of partitions */
Jonas Gorskifafc3d62012-11-12 10:52:47 +0100147 if (rootfslen > 0)
Jonas Gorski70a3c162011-12-05 16:08:08 +0100148 nrparts++;
Jonas Gorskifafc3d62012-11-12 10:52:47 +0100149
150 if (kernellen > 0)
Jonas Gorski70a3c162011-12-05 16:08:08 +0100151 nrparts++;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100152
153 /* Ask kernel for more memory */
154 parts = kzalloc(sizeof(*parts) * nrparts + 10 * nrparts, GFP_KERNEL);
155 if (!parts) {
156 vfree(buf);
157 return -ENOMEM;
158 }
159
160 /* Start building partition list */
161 parts[curpart].name = "CFE";
162 parts[curpart].offset = 0;
Jonas Gorski678eb9b2011-12-19 11:36:04 +0100163 parts[curpart].size = cfelen;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100164 curpart++;
165
166 if (kernellen > 0) {
Jonas Gorski5329d672012-04-19 13:15:57 +0200167 int kernelpart = curpart;
168
169 if (rootfslen > 0 && rootfs_first)
170 kernelpart++;
171 parts[kernelpart].name = "kernel";
172 parts[kernelpart].offset = kerneladdr;
173 parts[kernelpart].size = kernellen;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100174 curpart++;
175 }
176
177 if (rootfslen > 0) {
Jonas Gorski5329d672012-04-19 13:15:57 +0200178 int rootfspart = curpart;
179
180 if (kernellen > 0 && rootfs_first)
181 rootfspart--;
182 parts[rootfspart].name = "rootfs";
183 parts[rootfspart].offset = rootfsaddr;
184 parts[rootfspart].size = rootfslen;
185 if (sparelen > 0 && !rootfs_first)
186 parts[rootfspart].size += sparelen;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100187 curpart++;
188 }
189
190 parts[curpart].name = "nvram";
Jonas Gorski678eb9b2011-12-19 11:36:04 +0100191 parts[curpart].offset = master->size - nvramlen;
192 parts[curpart].size = nvramlen;
Jonas Gorskif3f9a5d2012-11-12 10:52:50 +0100193 curpart++;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100194
195 /* Global partition "linux" to make easy firmware upgrade */
Jonas Gorski70a3c162011-12-05 16:08:08 +0100196 parts[curpart].name = "linux";
Jonas Gorski327c62c2011-12-17 13:58:16 +0100197 parts[curpart].offset = cfelen;
198 parts[curpart].size = master->size - cfelen - nvramlen;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100199
200 for (i = 0; i < nrparts; i++)
Jonas Gorski60768362012-11-12 10:52:51 +0100201 pr_info("Partition %d is %s offset %llx and length %llx\n", i,
202 parts[i].name, parts[i].offset, parts[i].size);
Jonas Gorski70a3c162011-12-05 16:08:08 +0100203
204 pr_info("Spare partition is offset %x and length %x\n", spareaddr,
205 sparelen);
206
207 *pparts = parts;
208 vfree(buf);
209
210 return nrparts;
211};
212
213static struct mtd_part_parser bcm63xx_cfe_parser = {
214 .owner = THIS_MODULE,
215 .parse_fn = bcm63xx_parse_cfe_partitions,
216 .name = "bcm63xxpart",
217};
218
219static int __init bcm63xx_cfe_parser_init(void)
220{
221 return register_mtd_parser(&bcm63xx_cfe_parser);
222}
223
224static void __exit bcm63xx_cfe_parser_exit(void)
225{
226 deregister_mtd_parser(&bcm63xx_cfe_parser);
227}
228
229module_init(bcm63xx_cfe_parser_init);
230module_exit(bcm63xx_cfe_parser_exit);
231
232MODULE_LICENSE("GPL");
233MODULE_AUTHOR("Daniel Dickinson <openwrt@cshore.neomailbox.net>");
234MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
235MODULE_AUTHOR("Mike Albon <malbon@openwrt.org>");
236MODULE_AUTHOR("Jonas Gorski <jonas.gorski@gmail.com");
237MODULE_DESCRIPTION("MTD partitioning for BCM63XX CFE bootloaders");