driver core: remove drivers/base/sys.c and include/linux/sysdev.h
[zen-stable.git] / drivers / mtd / bcm63xxpart.c
blob608321ee056e5cb69c22f950ad2eb72f974fd6da
1 /*
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/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_MIN_CFE_SIZE 0x10000 /* always at least 64KiB */
41 #define BCM63XX_MIN_NVRAM_SIZE 0x10000 /* always at least 64KiB */
43 #define BCM63XX_CFE_MAGIC_OFFSET 0x4e0
45 static int bcm63xx_detect_cfe(struct mtd_info *master)
47 char buf[9];
48 int ret;
49 size_t retlen;
51 ret = mtd_read(master, BCM963XX_CFE_VERSION_OFFSET, 5, &retlen,
52 (void *)buf);
53 buf[retlen] = 0;
55 if (ret)
56 return ret;
58 if (strncmp("cfe-v", buf, 5) == 0)
59 return 0;
61 /* very old CFE's do not have the cfe-v string, so check for magic */
62 ret = mtd_read(master, BCM63XX_CFE_MAGIC_OFFSET, 8, &retlen,
63 (void *)buf);
64 buf[retlen] = 0;
66 return strncmp("CFE1CFE1", buf, 8);
69 static int bcm63xx_parse_cfe_partitions(struct mtd_info *master,
70 struct mtd_partition **pparts,
71 struct mtd_part_parser_data *data)
73 /* CFE, NVRAM and global Linux are always present */
74 int nrparts = 3, curpart = 0;
75 struct bcm_tag *buf;
76 struct mtd_partition *parts;
77 int ret;
78 size_t retlen;
79 unsigned int rootfsaddr, kerneladdr, spareaddr;
80 unsigned int rootfslen, kernellen, sparelen, totallen;
81 unsigned int cfelen, nvramlen;
82 int namelen = 0;
83 int i;
84 u32 computed_crc;
86 if (bcm63xx_detect_cfe(master))
87 return -EINVAL;
89 cfelen = max_t(uint32_t, master->erasesize, BCM63XX_MIN_CFE_SIZE);
90 nvramlen = max_t(uint32_t, master->erasesize, BCM63XX_MIN_NVRAM_SIZE);
92 /* Allocate memory for buffer */
93 buf = vmalloc(sizeof(struct bcm_tag));
94 if (!buf)
95 return -ENOMEM;
97 /* Get the tag */
98 ret = mtd_read(master, cfelen, sizeof(struct bcm_tag), &retlen,
99 (void *)buf);
101 if (retlen != sizeof(struct bcm_tag)) {
102 vfree(buf);
103 return -EIO;
106 computed_crc = crc32_le(IMAGETAG_CRC_START, (u8 *)buf,
107 offsetof(struct bcm_tag, header_crc));
108 if (computed_crc == buf->header_crc) {
109 char *boardid = &(buf->board_id[0]);
110 char *tagversion = &(buf->tag_version[0]);
112 sscanf(buf->kernel_address, "%u", &kerneladdr);
113 sscanf(buf->kernel_length, "%u", &kernellen);
114 sscanf(buf->total_length, "%u", &totallen);
116 pr_info("CFE boot tag found with version %s and board type %s\n",
117 tagversion, boardid);
119 kerneladdr = kerneladdr - BCM63XX_EXTENDED_SIZE;
120 rootfsaddr = kerneladdr + kernellen;
121 spareaddr = roundup(totallen, master->erasesize) + cfelen;
122 sparelen = master->size - spareaddr - nvramlen;
123 rootfslen = spareaddr - rootfsaddr;
124 } else {
125 pr_warn("CFE boot tag CRC invalid (expected %08x, actual %08x)\n",
126 buf->header_crc, computed_crc);
127 kernellen = 0;
128 rootfslen = 0;
129 rootfsaddr = 0;
130 spareaddr = cfelen;
131 sparelen = master->size - cfelen - nvramlen;
134 /* Determine number of partitions */
135 namelen = 8;
136 if (rootfslen > 0) {
137 nrparts++;
138 namelen += 6;
140 if (kernellen > 0) {
141 nrparts++;
142 namelen += 6;
145 /* Ask kernel for more memory */
146 parts = kzalloc(sizeof(*parts) * nrparts + 10 * nrparts, GFP_KERNEL);
147 if (!parts) {
148 vfree(buf);
149 return -ENOMEM;
152 /* Start building partition list */
153 parts[curpart].name = "CFE";
154 parts[curpart].offset = 0;
155 parts[curpart].size = cfelen;
156 curpart++;
158 if (kernellen > 0) {
159 parts[curpart].name = "kernel";
160 parts[curpart].offset = kerneladdr;
161 parts[curpart].size = kernellen;
162 curpart++;
165 if (rootfslen > 0) {
166 parts[curpart].name = "rootfs";
167 parts[curpart].offset = rootfsaddr;
168 parts[curpart].size = rootfslen;
169 if (sparelen > 0)
170 parts[curpart].size += sparelen;
171 curpart++;
174 parts[curpart].name = "nvram";
175 parts[curpart].offset = master->size - nvramlen;
176 parts[curpart].size = nvramlen;
178 /* Global partition "linux" to make easy firmware upgrade */
179 curpart++;
180 parts[curpart].name = "linux";
181 parts[curpart].offset = cfelen;
182 parts[curpart].size = master->size - cfelen - nvramlen;
184 for (i = 0; i < nrparts; i++)
185 pr_info("Partition %d is %s offset %lx and length %lx\n", i,
186 parts[i].name, (long unsigned int)(parts[i].offset),
187 (long unsigned int)(parts[i].size));
189 pr_info("Spare partition is offset %x and length %x\n", spareaddr,
190 sparelen);
192 *pparts = parts;
193 vfree(buf);
195 return nrparts;
198 static struct mtd_part_parser bcm63xx_cfe_parser = {
199 .owner = THIS_MODULE,
200 .parse_fn = bcm63xx_parse_cfe_partitions,
201 .name = "bcm63xxpart",
204 static int __init bcm63xx_cfe_parser_init(void)
206 return register_mtd_parser(&bcm63xx_cfe_parser);
209 static void __exit bcm63xx_cfe_parser_exit(void)
211 deregister_mtd_parser(&bcm63xx_cfe_parser);
214 module_init(bcm63xx_cfe_parser_init);
215 module_exit(bcm63xx_cfe_parser_exit);
217 MODULE_LICENSE("GPL");
218 MODULE_AUTHOR("Daniel Dickinson <openwrt@cshore.neomailbox.net>");
219 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
220 MODULE_AUTHOR("Mike Albon <malbon@openwrt.org>");
221 MODULE_AUTHOR("Jonas Gorski <jonas.gorski@gmail.com");
222 MODULE_DESCRIPTION("MTD partitioning for BCM63XX CFE bootloaders");