1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * BCM63XX CFE image tag parser
5 * Copyright © 2006-2008 Florian Fainelli <florian@openwrt.org>
6 * Mike Albon <malbon@openwrt.org>
7 * Copyright © 2009-2010 Daniel Dickinson <openwrt@cshore.neomailbox.net>
8 * Copyright © 2011-2013 Jonas Gorski <jonas.gorski@gmail.com>
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/bcm963xx_nvram.h>
14 #include <linux/bcm963xx_tag.h>
15 #include <linux/crc32.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/sizes.h>
19 #include <linux/slab.h>
20 #include <linux/vmalloc.h>
21 #include <linux/mtd/mtd.h>
22 #include <linux/mtd/partitions.h>
25 #define BCM963XX_CFE_BLOCK_SIZE SZ_64K /* always at least 64KiB */
27 #define BCM963XX_CFE_MAGIC_OFFSET 0x4e0
28 #define BCM963XX_CFE_VERSION_OFFSET 0x570
29 #define BCM963XX_NVRAM_OFFSET 0x580
31 /* Ensure strings read from flash structs are null terminated */
32 #define STR_NULL_TERMINATE(x) \
33 do { char *_str = (x); _str[sizeof(x) - 1] = 0; } while (0)
35 static int bcm63xx_detect_cfe(struct mtd_info
*master
)
41 ret
= mtd_read(master
, BCM963XX_CFE_VERSION_OFFSET
, 5, &retlen
,
48 if (strncmp("cfe-v", buf
, 5) == 0)
51 /* very old CFE's do not have the cfe-v string, so check for magic */
52 ret
= mtd_read(master
, BCM963XX_CFE_MAGIC_OFFSET
, 8, &retlen
,
56 return strncmp("CFE1CFE1", buf
, 8);
59 static int bcm63xx_read_nvram(struct mtd_info
*master
,
60 struct bcm963xx_nvram
*nvram
)
62 u32 actual_crc
, expected_crc
;
66 /* extract nvram data */
67 ret
= mtd_read(master
, BCM963XX_NVRAM_OFFSET
, BCM963XX_NVRAM_V5_SIZE
,
68 &retlen
, (void *)nvram
);
72 ret
= bcm963xx_nvram_checksum(nvram
, &expected_crc
, &actual_crc
);
74 pr_warn("nvram checksum failed, contents may be invalid (expected %08x, got %08x)\n",
75 expected_crc
, actual_crc
);
78 nvram
->psi_size
= BCM963XX_DEFAULT_PSI_SIZE
;
83 static const char * const bcm63xx_cfe_part_types
[] = {
88 static int bcm63xx_parse_cfe_nor_partitions(struct mtd_info
*master
,
89 const struct mtd_partition
**pparts
, struct bcm963xx_nvram
*nvram
)
91 struct mtd_partition
*parts
;
92 int nrparts
= 3, curpart
= 0;
93 unsigned int cfelen
, nvramlen
;
94 unsigned int cfe_erasesize
;
97 cfe_erasesize
= max_t(uint32_t, master
->erasesize
,
98 BCM963XX_CFE_BLOCK_SIZE
);
100 cfelen
= cfe_erasesize
;
101 nvramlen
= nvram
->psi_size
* SZ_1K
;
102 nvramlen
= roundup(nvramlen
, cfe_erasesize
);
104 parts
= kzalloc(sizeof(*parts
) * nrparts
+ 10 * nrparts
, GFP_KERNEL
);
108 /* Start building partition list */
109 parts
[curpart
].name
= "CFE";
110 parts
[curpart
].offset
= 0;
111 parts
[curpart
].size
= cfelen
;
114 parts
[curpart
].name
= "nvram";
115 parts
[curpart
].offset
= master
->size
- nvramlen
;
116 parts
[curpart
].size
= nvramlen
;
119 /* Global partition "linux" to make easy firmware upgrade */
120 parts
[curpart
].name
= "linux";
121 parts
[curpart
].offset
= cfelen
;
122 parts
[curpart
].size
= master
->size
- cfelen
- nvramlen
;
123 parts
[curpart
].types
= bcm63xx_cfe_part_types
;
125 for (i
= 0; i
< nrparts
; i
++)
126 pr_info("Partition %d is %s offset %llx and length %llx\n", i
,
127 parts
[i
].name
, parts
[i
].offset
, parts
[i
].size
);
134 static int bcm63xx_parse_cfe_partitions(struct mtd_info
*master
,
135 const struct mtd_partition
**pparts
,
136 struct mtd_part_parser_data
*data
)
138 struct bcm963xx_nvram
*nvram
= NULL
;
141 if (bcm63xx_detect_cfe(master
))
144 nvram
= vzalloc(sizeof(*nvram
));
148 ret
= bcm63xx_read_nvram(master
, nvram
);
152 if (!mtd_type_is_nand(master
))
153 ret
= bcm63xx_parse_cfe_nor_partitions(master
, pparts
, nvram
);
162 static const struct of_device_id parse_bcm63xx_cfe_match_table
[] = {
163 { .compatible
= "brcm,bcm963xx-cfe-nor-partitions" },
166 MODULE_DEVICE_TABLE(of
, parse_bcm63xx_cfe_match_table
);
168 static struct mtd_part_parser bcm63xx_cfe_parser
= {
169 .parse_fn
= bcm63xx_parse_cfe_partitions
,
170 .name
= "bcm63xxpart",
171 .of_match_table
= parse_bcm63xx_cfe_match_table
,
173 module_mtd_part_parser(bcm63xx_cfe_parser
);
175 MODULE_LICENSE("GPL");
176 MODULE_AUTHOR("Daniel Dickinson <openwrt@cshore.neomailbox.net>");
177 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
178 MODULE_AUTHOR("Mike Albon <malbon@openwrt.org>");
179 MODULE_AUTHOR("Jonas Gorski <jonas.gorski@gmail.com");
180 MODULE_DESCRIPTION("MTD partitioning for BCM63XX CFE bootloaders");