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-2012 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/crc32.h>
28 #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>
35 #include <asm/mach-bcm63xx/bcm963xx_tag.h>
36 #include <asm/mach-bcm63xx/board_bcm963xx.h>
38 #define BCM63XX_EXTENDED_SIZE 0xBFC00000 /* Extended flash address */
40 #define BCM63XX_CFE_BLOCK_SIZE 0x10000 /* always at least 64KiB */
42 #define BCM63XX_CFE_MAGIC_OFFSET 0x4e0
44 static int bcm63xx_detect_cfe(struct mtd_info *master)
50 ret = mtd_read(master, BCM963XX_CFE_VERSION_OFFSET, 5, &retlen,
57 if (strncmp("cfe-v", buf, 5) == 0)
60 /* very old CFE's do not have the cfe-v string, so check for magic */
61 ret = mtd_read(master, BCM63XX_CFE_MAGIC_OFFSET, 8, &retlen,
65 return strncmp("CFE1CFE1", buf, 8);
68 static int bcm63xx_parse_cfe_partitions(struct mtd_info *master,
69 struct mtd_partition **pparts,
70 struct mtd_part_parser_data *data)
72 /* CFE, NVRAM and global Linux are always present */
73 int nrparts = 3, curpart = 0;
75 struct mtd_partition *parts;
78 unsigned int rootfsaddr, kerneladdr, spareaddr;
79 unsigned int rootfslen, kernellen, sparelen, totallen;
80 unsigned int cfelen, nvramlen;
81 unsigned int cfe_erasesize;
84 bool rootfs_first = false;
86 if (bcm63xx_detect_cfe(master))
89 cfe_erasesize = max_t(uint32_t, master->erasesize,
90 BCM63XX_CFE_BLOCK_SIZE);
92 cfelen = cfe_erasesize;
93 nvramlen = cfe_erasesize;
95 /* Allocate memory for buffer */
96 buf = vmalloc(sizeof(struct bcm_tag));
101 ret = mtd_read(master, cfelen, sizeof(struct bcm_tag), &retlen,
104 if (retlen != sizeof(struct bcm_tag)) {
109 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]);
115 sscanf(buf->flash_image_start, "%u", &rootfsaddr);
116 sscanf(buf->kernel_address, "%u", &kerneladdr);
117 sscanf(buf->kernel_length, "%u", &kernellen);
118 sscanf(buf->total_length, "%u", &totallen);
120 pr_info("CFE boot tag found with version %s and board type %s\n",
121 tagversion, boardid);
123 kerneladdr = kerneladdr - BCM63XX_EXTENDED_SIZE;
124 rootfsaddr = rootfsaddr - BCM63XX_EXTENDED_SIZE;
125 spareaddr = roundup(totallen, master->erasesize) + cfelen;
127 if (rootfsaddr < kerneladdr) {
128 /* default Broadcom layout */
129 rootfslen = kerneladdr - rootfsaddr;
133 rootfsaddr = kerneladdr + kernellen;
134 rootfslen = spareaddr - rootfsaddr;
137 pr_warn("CFE boot tag CRC invalid (expected %08x, actual %08x)\n",
138 buf->header_crc, computed_crc);
144 sparelen = master->size - spareaddr - nvramlen;
146 /* Determine number of partitions */
153 /* Ask kernel for more memory */
154 parts = kzalloc(sizeof(*parts) * nrparts + 10 * nrparts, GFP_KERNEL);
160 /* Start building partition list */
161 parts[curpart].name = "CFE";
162 parts[curpart].offset = 0;
163 parts[curpart].size = cfelen;
167 int kernelpart = curpart;
169 if (rootfslen > 0 && rootfs_first)
171 parts[kernelpart].name = "kernel";
172 parts[kernelpart].offset = kerneladdr;
173 parts[kernelpart].size = kernellen;
178 int rootfspart = curpart;
180 if (kernellen > 0 && rootfs_first)
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;
190 parts[curpart].name = "nvram";
191 parts[curpart].offset = master->size - nvramlen;
192 parts[curpart].size = nvramlen;
195 /* Global partition "linux" to make easy firmware upgrade */
196 parts[curpart].name = "linux";
197 parts[curpart].offset = cfelen;
198 parts[curpart].size = master->size - cfelen - nvramlen;
200 for (i = 0; i < nrparts; i++)
201 pr_info("Partition %d is %s offset %llx and length %llx\n", i,
202 parts[i].name, parts[i].offset, parts[i].size);
204 pr_info("Spare partition is offset %x and length %x\n", spareaddr,
213 static struct mtd_part_parser bcm63xx_cfe_parser = {
214 .owner = THIS_MODULE,
215 .parse_fn = bcm63xx_parse_cfe_partitions,
216 .name = "bcm63xxpart",
219 static int __init bcm63xx_cfe_parser_init(void)
221 return register_mtd_parser(&bcm63xx_cfe_parser);
224 static void __exit bcm63xx_cfe_parser_exit(void)
226 deregister_mtd_parser(&bcm63xx_cfe_parser);
229 module_init(bcm63xx_cfe_parser_init);
230 module_exit(bcm63xx_cfe_parser_exit);
232 MODULE_LICENSE("GPL");
233 MODULE_AUTHOR("Daniel Dickinson <openwrt@cshore.neomailbox.net>");
234 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
235 MODULE_AUTHOR("Mike Albon <malbon@openwrt.org>");
236 MODULE_AUTHOR("Jonas Gorski <jonas.gorski@gmail.com");
237 MODULE_DESCRIPTION("MTD partitioning for BCM63XX CFE bootloaders");