blob: 17e137080a6b1ee8c5b52112a99c27b26951819d [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>
7 * Copyright © 2011 Jonas Gorski <jonas.gorski@gmail.com>
8 *
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
27#include <linux/module.h>
28#include <linux/kernel.h>
29#include <linux/slab.h>
30#include <linux/vmalloc.h>
31#include <linux/mtd/mtd.h>
32#include <linux/mtd/partitions.h>
33
34#include <asm/mach-bcm63xx/bcm963xx_tag.h>
Jonas Gorskif2d97392011-12-17 13:58:14 +010035#include <asm/mach-bcm63xx/board_bcm963xx.h>
Jonas Gorski70a3c162011-12-05 16:08:08 +010036
37#define BCM63XX_EXTENDED_SIZE 0xBFC00000 /* Extended flash address */
38
Jonas Gorski678eb9b2011-12-19 11:36:04 +010039#define BCM63XX_MIN_CFE_SIZE 0x10000 /* always at least 64KiB */
40#define BCM63XX_MIN_NVRAM_SIZE 0x10000 /* always at least 64KiB */
41
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
Jonas Gorskif2d97392011-12-17 13:58:14 +010050 ret = master->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 */
61 ret = master->read(master, BCM63XX_CFE_MAGIC_OFFSET, 8, &retlen,
62 (void *)buf);
63 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 Gorski70a3c162011-12-05 16:08:08 +010081 int namelen = 0;
82 int i;
83 char *boardid;
84 char *tagversion;
85
86 if (bcm63xx_detect_cfe(master))
87 return -EINVAL;
88
Jonas Gorski678eb9b2011-12-19 11:36:04 +010089 cfelen = max_t(uint32_t, master->erasesize, BCM63XX_MIN_CFE_SIZE);
90 nvramlen = max_t(uint32_t, master->erasesize, BCM63XX_MIN_NVRAM_SIZE);
91
Jonas Gorski70a3c162011-12-05 16:08:08 +010092 /* Allocate memory for buffer */
93 buf = vmalloc(sizeof(struct bcm_tag));
94 if (!buf)
95 return -ENOMEM;
96
97 /* Get the tag */
Jonas Gorski678eb9b2011-12-19 11:36:04 +010098 ret = master->read(master, cfelen, sizeof(struct bcm_tag), &retlen,
99 (void *)buf);
100
Jonas Gorski70a3c162011-12-05 16:08:08 +0100101 if (retlen != sizeof(struct bcm_tag)) {
102 vfree(buf);
103 return -EIO;
104 }
105
106 sscanf(buf->kernel_address, "%u", &kerneladdr);
107 sscanf(buf->kernel_length, "%u", &kernellen);
108 sscanf(buf->total_length, "%u", &totallen);
109 tagversion = &(buf->tag_version[0]);
110 boardid = &(buf->board_id[0]);
111
112 pr_info("CFE boot tag found with version %s and board type %s\n",
113 tagversion, boardid);
114
115 kerneladdr = kerneladdr - BCM63XX_EXTENDED_SIZE;
116 rootfsaddr = kerneladdr + kernellen;
Jonas Gorski678eb9b2011-12-19 11:36:04 +0100117 spareaddr = roundup(totallen, master->erasesize) + cfelen;
118 sparelen = master->size - spareaddr - nvramlen;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100119 rootfslen = spareaddr - rootfsaddr;
120
121 /* Determine number of partitions */
122 namelen = 8;
123 if (rootfslen > 0) {
124 nrparts++;
125 namelen += 6;
126 }
127 if (kernellen > 0) {
128 nrparts++;
129 namelen += 6;
130 }
131
132 /* Ask kernel for more memory */
133 parts = kzalloc(sizeof(*parts) * nrparts + 10 * nrparts, GFP_KERNEL);
134 if (!parts) {
135 vfree(buf);
136 return -ENOMEM;
137 }
138
139 /* Start building partition list */
140 parts[curpart].name = "CFE";
141 parts[curpart].offset = 0;
Jonas Gorski678eb9b2011-12-19 11:36:04 +0100142 parts[curpart].size = cfelen;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100143 curpart++;
144
145 if (kernellen > 0) {
146 parts[curpart].name = "kernel";
147 parts[curpart].offset = kerneladdr;
148 parts[curpart].size = kernellen;
149 curpart++;
150 }
151
152 if (rootfslen > 0) {
153 parts[curpart].name = "rootfs";
154 parts[curpart].offset = rootfsaddr;
155 parts[curpart].size = rootfslen;
156 if (sparelen > 0)
157 parts[curpart].size += sparelen;
158 curpart++;
159 }
160
161 parts[curpart].name = "nvram";
Jonas Gorski678eb9b2011-12-19 11:36:04 +0100162 parts[curpart].offset = master->size - nvramlen;
163 parts[curpart].size = nvramlen;
Jonas Gorski70a3c162011-12-05 16:08:08 +0100164
165 /* Global partition "linux" to make easy firmware upgrade */
166 curpart++;
167 parts[curpart].name = "linux";
168 parts[curpart].offset = parts[0].size;
169 parts[curpart].size = master->size - parts[0].size - parts[3].size;
170
171 for (i = 0; i < nrparts; i++)
172 pr_info("Partition %d is %s offset %lx and length %lx\n", i,
173 parts[i].name, (long unsigned int)(parts[i].offset),
174 (long unsigned int)(parts[i].size));
175
176 pr_info("Spare partition is offset %x and length %x\n", spareaddr,
177 sparelen);
178
179 *pparts = parts;
180 vfree(buf);
181
182 return nrparts;
183};
184
185static struct mtd_part_parser bcm63xx_cfe_parser = {
186 .owner = THIS_MODULE,
187 .parse_fn = bcm63xx_parse_cfe_partitions,
188 .name = "bcm63xxpart",
189};
190
191static int __init bcm63xx_cfe_parser_init(void)
192{
193 return register_mtd_parser(&bcm63xx_cfe_parser);
194}
195
196static void __exit bcm63xx_cfe_parser_exit(void)
197{
198 deregister_mtd_parser(&bcm63xx_cfe_parser);
199}
200
201module_init(bcm63xx_cfe_parser_init);
202module_exit(bcm63xx_cfe_parser_exit);
203
204MODULE_LICENSE("GPL");
205MODULE_AUTHOR("Daniel Dickinson <openwrt@cshore.neomailbox.net>");
206MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
207MODULE_AUTHOR("Mike Albon <malbon@openwrt.org>");
208MODULE_AUTHOR("Jonas Gorski <jonas.gorski@gmail.com");
209MODULE_DESCRIPTION("MTD partitioning for BCM63XX CFE bootloaders");