2 * BCM63XX CFE image tag parser
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>
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.
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.
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
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
34 #include <asm/mach-bcm63xx/bcm963xx_tag.h>
35 #include <asm/mach-bcm63xx/board_bcm963xx.h>
37 #define BCM63XX_EXTENDED_SIZE 0xBFC00000 /* Extended flash address */
39 #define BCM63XX_CFE_MAGIC_OFFSET 0x4e0
41 static int bcm63xx_detect_cfe(struct mtd_info *master)
47 ret = master->read(master, BCM963XX_CFE_VERSION_OFFSET, 5, &retlen,
54 if (strncmp("cfe-v", buf, 5) == 0)
57 /* very old CFE's do not have the cfe-v string, so check for magic */
58 ret = master->read(master, BCM63XX_CFE_MAGIC_OFFSET, 8, &retlen,
62 return strncmp("CFE1CFE1", buf, 8);
65 static int bcm63xx_parse_cfe_partitions(struct mtd_info *master,
66 struct mtd_partition **pparts,
67 struct mtd_part_parser_data *data)
69 /* CFE, NVRAM and global Linux are always present */
70 int nrparts = 3, curpart = 0;
72 struct mtd_partition *parts;
75 unsigned int rootfsaddr, kerneladdr, spareaddr;
76 unsigned int rootfslen, kernellen, sparelen, totallen;
82 if (bcm63xx_detect_cfe(master))
85 /* Allocate memory for buffer */
86 buf = vmalloc(sizeof(struct bcm_tag));
91 ret = master->read(master, master->erasesize, sizeof(struct bcm_tag),
92 &retlen, (void *)buf);
93 if (retlen != sizeof(struct bcm_tag)) {
98 sscanf(buf->kernel_address, "%u", &kerneladdr);
99 sscanf(buf->kernel_length, "%u", &kernellen);
100 sscanf(buf->total_length, "%u", &totallen);
101 tagversion = &(buf->tag_version[0]);
102 boardid = &(buf->board_id[0]);
104 pr_info("CFE boot tag found with version %s and board type %s\n",
105 tagversion, boardid);
107 kerneladdr = kerneladdr - BCM63XX_EXTENDED_SIZE;
108 rootfsaddr = kerneladdr + kernellen;
109 spareaddr = roundup(totallen, master->erasesize) + master->erasesize;
110 sparelen = master->size - spareaddr - master->erasesize;
111 rootfslen = spareaddr - rootfsaddr;
113 /* Determine number of partitions */
124 /* Ask kernel for more memory */
125 parts = kzalloc(sizeof(*parts) * nrparts + 10 * nrparts, GFP_KERNEL);
131 /* Start building partition list */
132 parts[curpart].name = "CFE";
133 parts[curpart].offset = 0;
134 parts[curpart].size = master->erasesize;
138 parts[curpart].name = "kernel";
139 parts[curpart].offset = kerneladdr;
140 parts[curpart].size = kernellen;
145 parts[curpart].name = "rootfs";
146 parts[curpart].offset = rootfsaddr;
147 parts[curpart].size = rootfslen;
149 parts[curpart].size += sparelen;
153 parts[curpart].name = "nvram";
154 parts[curpart].offset = master->size - master->erasesize;
155 parts[curpart].size = master->erasesize;
157 /* Global partition "linux" to make easy firmware upgrade */
159 parts[curpart].name = "linux";
160 parts[curpart].offset = parts[0].size;
161 parts[curpart].size = master->size - parts[0].size - parts[3].size;
163 for (i = 0; i < nrparts; i++)
164 pr_info("Partition %d is %s offset %lx and length %lx\n", i,
165 parts[i].name, (long unsigned int)(parts[i].offset),
166 (long unsigned int)(parts[i].size));
168 pr_info("Spare partition is offset %x and length %x\n", spareaddr,
177 static struct mtd_part_parser bcm63xx_cfe_parser = {
178 .owner = THIS_MODULE,
179 .parse_fn = bcm63xx_parse_cfe_partitions,
180 .name = "bcm63xxpart",
183 static int __init bcm63xx_cfe_parser_init(void)
185 return register_mtd_parser(&bcm63xx_cfe_parser);
188 static void __exit bcm63xx_cfe_parser_exit(void)
190 deregister_mtd_parser(&bcm63xx_cfe_parser);
193 module_init(bcm63xx_cfe_parser_init);
194 module_exit(bcm63xx_cfe_parser_exit);
196 MODULE_LICENSE("GPL");
197 MODULE_AUTHOR("Daniel Dickinson <openwrt@cshore.neomailbox.net>");
198 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
199 MODULE_AUTHOR("Mike Albon <malbon@openwrt.org>");
200 MODULE_AUTHOR("Jonas Gorski <jonas.gorski@gmail.com");
201 MODULE_DESCRIPTION("MTD partitioning for BCM63XX CFE bootloaders");