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-2013 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/bcm963xx_tag.h>
28 #include <linux/crc32.h>
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/sizes.h>
32 #include <linux/slab.h>
33 #include <linux/vmalloc.h>
34 #include <linux/mtd/mtd.h>
35 #include <linux/mtd/partitions.h>
37 #include <asm/mach-bcm63xx/bcm63xx_nvram.h>
38 #include <asm/mach-bcm63xx/board_bcm963xx.h>
40 #define BCM63XX_CFE_BLOCK_SIZE SZ_64K /* 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 const 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
= bcm63xx_nvram_get_psi_size() * SZ_1K
;
94 nvramlen
= roundup(nvramlen
, cfe_erasesize
);
96 /* Allocate memory for buffer */
97 buf
= vmalloc(sizeof(struct bcm_tag
));
102 ret
= mtd_read(master
, cfelen
, sizeof(struct bcm_tag
), &retlen
,
105 if (retlen
!= sizeof(struct bcm_tag
)) {
110 computed_crc
= crc32_le(IMAGETAG_CRC_START
, (u8
*)buf
,
111 offsetof(struct bcm_tag
, header_crc
));
112 if (computed_crc
== buf
->header_crc
) {
113 char *boardid
= &(buf
->board_id
[0]);
114 char *tagversion
= &(buf
->tag_version
[0]);
116 sscanf(buf
->flash_image_start
, "%u", &rootfsaddr
);
117 sscanf(buf
->kernel_address
, "%u", &kerneladdr
);
118 sscanf(buf
->kernel_length
, "%u", &kernellen
);
119 sscanf(buf
->total_length
, "%u", &totallen
);
121 pr_info("CFE boot tag found with version %s and board type %s\n",
122 tagversion
, boardid
);
124 kerneladdr
= kerneladdr
- BCM963XX_EXTENDED_SIZE
;
125 rootfsaddr
= rootfsaddr
- BCM963XX_EXTENDED_SIZE
;
126 spareaddr
= roundup(totallen
, master
->erasesize
) + cfelen
;
128 if (rootfsaddr
< kerneladdr
) {
129 /* default Broadcom layout */
130 rootfslen
= kerneladdr
- rootfsaddr
;
134 rootfsaddr
= kerneladdr
+ kernellen
;
135 rootfslen
= spareaddr
- rootfsaddr
;
138 pr_warn("CFE boot tag CRC invalid (expected %08x, actual %08x)\n",
139 buf
->header_crc
, computed_crc
);
145 sparelen
= master
->size
- spareaddr
- nvramlen
;
147 /* Determine number of partitions */
154 /* Ask kernel for more memory */
155 parts
= kzalloc(sizeof(*parts
) * nrparts
+ 10 * nrparts
, GFP_KERNEL
);
161 /* Start building partition list */
162 parts
[curpart
].name
= "CFE";
163 parts
[curpart
].offset
= 0;
164 parts
[curpart
].size
= cfelen
;
168 int kernelpart
= curpart
;
170 if (rootfslen
> 0 && rootfs_first
)
172 parts
[kernelpart
].name
= "kernel";
173 parts
[kernelpart
].offset
= kerneladdr
;
174 parts
[kernelpart
].size
= kernellen
;
179 int rootfspart
= curpart
;
181 if (kernellen
> 0 && rootfs_first
)
183 parts
[rootfspart
].name
= "rootfs";
184 parts
[rootfspart
].offset
= rootfsaddr
;
185 parts
[rootfspart
].size
= rootfslen
;
186 if (sparelen
> 0 && !rootfs_first
)
187 parts
[rootfspart
].size
+= sparelen
;
191 parts
[curpart
].name
= "nvram";
192 parts
[curpart
].offset
= master
->size
- nvramlen
;
193 parts
[curpart
].size
= nvramlen
;
196 /* Global partition "linux" to make easy firmware upgrade */
197 parts
[curpart
].name
= "linux";
198 parts
[curpart
].offset
= cfelen
;
199 parts
[curpart
].size
= master
->size
- cfelen
- nvramlen
;
201 for (i
= 0; i
< nrparts
; i
++)
202 pr_info("Partition %d is %s offset %llx and length %llx\n", i
,
203 parts
[i
].name
, parts
[i
].offset
, parts
[i
].size
);
205 pr_info("Spare partition is offset %x and length %x\n", spareaddr
,
214 static struct mtd_part_parser bcm63xx_cfe_parser
= {
215 .parse_fn
= bcm63xx_parse_cfe_partitions
,
216 .name
= "bcm63xxpart",
218 module_mtd_part_parser(bcm63xx_cfe_parser
);
220 MODULE_LICENSE("GPL");
221 MODULE_AUTHOR("Daniel Dickinson <openwrt@cshore.neomailbox.net>");
222 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
223 MODULE_AUTHOR("Mike Albon <malbon@openwrt.org>");
224 MODULE_AUTHOR("Jonas Gorski <jonas.gorski@gmail.com");
225 MODULE_DESCRIPTION("MTD partitioning for BCM63XX CFE bootloaders");