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_nvram.h>
28 #include <linux/bcm963xx_tag.h>
29 #include <linux/crc32.h>
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/sizes.h>
33 #include <linux/slab.h>
34 #include <linux/vmalloc.h>
35 #include <linux/mtd/mtd.h>
36 #include <linux/mtd/partitions.h>
39 #define BCM963XX_CFE_BLOCK_SIZE SZ_64K /* always at least 64KiB */
41 #define BCM963XX_CFE_MAGIC_OFFSET 0x4e0
42 #define BCM963XX_CFE_VERSION_OFFSET 0x570
43 #define BCM963XX_NVRAM_OFFSET 0x580
45 /* Ensure strings read from flash structs are null terminated */
46 #define STR_NULL_TERMINATE(x) \
47 do { char *_str = (x); _str[sizeof(x) - 1] = 0; } while (0)
49 static int bcm63xx_detect_cfe(struct mtd_info
*master
)
55 ret
= mtd_read(master
, BCM963XX_CFE_VERSION_OFFSET
, 5, &retlen
,
62 if (strncmp("cfe-v", buf
, 5) == 0)
65 /* very old CFE's do not have the cfe-v string, so check for magic */
66 ret
= mtd_read(master
, BCM963XX_CFE_MAGIC_OFFSET
, 8, &retlen
,
70 return strncmp("CFE1CFE1", buf
, 8);
73 static int bcm63xx_read_nvram(struct mtd_info
*master
,
74 struct bcm963xx_nvram
*nvram
)
76 u32 actual_crc
, expected_crc
;
80 /* extract nvram data */
81 ret
= mtd_read(master
, BCM963XX_NVRAM_OFFSET
, BCM963XX_NVRAM_V5_SIZE
,
82 &retlen
, (void *)nvram
);
86 ret
= bcm963xx_nvram_checksum(nvram
, &expected_crc
, &actual_crc
);
88 pr_warn("nvram checksum failed, contents may be invalid (expected %08x, got %08x)\n",
89 expected_crc
, actual_crc
);
92 nvram
->psi_size
= BCM963XX_DEFAULT_PSI_SIZE
;
97 static const char * const bcm63xx_cfe_part_types
[] = {
102 static int bcm63xx_parse_cfe_nor_partitions(struct mtd_info
*master
,
103 const struct mtd_partition
**pparts
, struct bcm963xx_nvram
*nvram
)
105 struct mtd_partition
*parts
;
106 int nrparts
= 3, curpart
= 0;
107 unsigned int cfelen
, nvramlen
;
108 unsigned int cfe_erasesize
;
111 cfe_erasesize
= max_t(uint32_t, master
->erasesize
,
112 BCM963XX_CFE_BLOCK_SIZE
);
114 cfelen
= cfe_erasesize
;
115 nvramlen
= nvram
->psi_size
* SZ_1K
;
116 nvramlen
= roundup(nvramlen
, cfe_erasesize
);
118 parts
= kzalloc(sizeof(*parts
) * nrparts
+ 10 * nrparts
, GFP_KERNEL
);
122 /* Start building partition list */
123 parts
[curpart
].name
= "CFE";
124 parts
[curpart
].offset
= 0;
125 parts
[curpart
].size
= cfelen
;
128 parts
[curpart
].name
= "nvram";
129 parts
[curpart
].offset
= master
->size
- nvramlen
;
130 parts
[curpart
].size
= nvramlen
;
133 /* Global partition "linux" to make easy firmware upgrade */
134 parts
[curpart
].name
= "linux";
135 parts
[curpart
].offset
= cfelen
;
136 parts
[curpart
].size
= master
->size
- cfelen
- nvramlen
;
137 parts
[curpart
].types
= bcm63xx_cfe_part_types
;
139 for (i
= 0; i
< nrparts
; i
++)
140 pr_info("Partition %d is %s offset %llx and length %llx\n", i
,
141 parts
[i
].name
, parts
[i
].offset
, parts
[i
].size
);
148 static int bcm63xx_parse_cfe_partitions(struct mtd_info
*master
,
149 const struct mtd_partition
**pparts
,
150 struct mtd_part_parser_data
*data
)
152 struct bcm963xx_nvram
*nvram
= NULL
;
155 if (bcm63xx_detect_cfe(master
))
158 nvram
= vzalloc(sizeof(*nvram
));
162 ret
= bcm63xx_read_nvram(master
, nvram
);
166 if (!mtd_type_is_nand(master
))
167 ret
= bcm63xx_parse_cfe_nor_partitions(master
, pparts
, nvram
);
176 static const struct of_device_id parse_bcm63xx_cfe_match_table
[] = {
177 { .compatible
= "brcm,bcm963xx-cfe-nor-partitions" },
180 MODULE_DEVICE_TABLE(of
, parse_bcm63xx_cfe_match_table
);
182 static struct mtd_part_parser bcm63xx_cfe_parser
= {
183 .parse_fn
= bcm63xx_parse_cfe_partitions
,
184 .name
= "bcm63xxpart",
185 .of_match_table
= parse_bcm63xx_cfe_match_table
,
187 module_mtd_part_parser(bcm63xx_cfe_parser
);
189 MODULE_LICENSE("GPL");
190 MODULE_AUTHOR("Daniel Dickinson <openwrt@cshore.neomailbox.net>");
191 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
192 MODULE_AUTHOR("Mike Albon <malbon@openwrt.org>");
193 MODULE_AUTHOR("Jonas Gorski <jonas.gorski@gmail.com");
194 MODULE_DESCRIPTION("MTD partitioning for BCM63XX CFE bootloaders");