]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - drivers/mtd/bcm63xxpart.c
Merge tag 'powerpc-5.2-2' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[mirror_ubuntu-hirsute-kernel.git] / drivers / mtd / bcm63xxpart.c
1 /*
2 * BCM63XX CFE image tag parser
3 *
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>
8 *
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.
13 *
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.
18 *
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
22 *
23 */
24
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26
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>
37 #include <linux/of.h>
38
39 #define BCM963XX_CFE_BLOCK_SIZE SZ_64K /* always at least 64KiB */
40
41 #define BCM963XX_CFE_MAGIC_OFFSET 0x4e0
42 #define BCM963XX_CFE_VERSION_OFFSET 0x570
43 #define BCM963XX_NVRAM_OFFSET 0x580
44
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)
48
49 static int bcm63xx_detect_cfe(struct mtd_info *master)
50 {
51 char buf[9];
52 int ret;
53 size_t retlen;
54
55 ret = mtd_read(master, BCM963XX_CFE_VERSION_OFFSET, 5, &retlen,
56 (void *)buf);
57 buf[retlen] = 0;
58
59 if (ret)
60 return ret;
61
62 if (strncmp("cfe-v", buf, 5) == 0)
63 return 0;
64
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,
67 (void *)buf);
68 buf[retlen] = 0;
69
70 return strncmp("CFE1CFE1", buf, 8);
71 }
72
73 static int bcm63xx_read_nvram(struct mtd_info *master,
74 struct bcm963xx_nvram *nvram)
75 {
76 u32 actual_crc, expected_crc;
77 size_t retlen;
78 int ret;
79
80 /* extract nvram data */
81 ret = mtd_read(master, BCM963XX_NVRAM_OFFSET, BCM963XX_NVRAM_V5_SIZE,
82 &retlen, (void *)nvram);
83 if (ret)
84 return ret;
85
86 ret = bcm963xx_nvram_checksum(nvram, &expected_crc, &actual_crc);
87 if (ret)
88 pr_warn("nvram checksum failed, contents may be invalid (expected %08x, got %08x)\n",
89 expected_crc, actual_crc);
90
91 if (!nvram->psi_size)
92 nvram->psi_size = BCM963XX_DEFAULT_PSI_SIZE;
93
94 return 0;
95 }
96
97 static const char * const bcm63xx_cfe_part_types[] = {
98 "bcm963xx-imagetag",
99 NULL,
100 };
101
102 static int bcm63xx_parse_cfe_nor_partitions(struct mtd_info *master,
103 const struct mtd_partition **pparts, struct bcm963xx_nvram *nvram)
104 {
105 struct mtd_partition *parts;
106 int nrparts = 3, curpart = 0;
107 unsigned int cfelen, nvramlen;
108 unsigned int cfe_erasesize;
109 int i;
110
111 cfe_erasesize = max_t(uint32_t, master->erasesize,
112 BCM963XX_CFE_BLOCK_SIZE);
113
114 cfelen = cfe_erasesize;
115 nvramlen = nvram->psi_size * SZ_1K;
116 nvramlen = roundup(nvramlen, cfe_erasesize);
117
118 parts = kzalloc(sizeof(*parts) * nrparts + 10 * nrparts, GFP_KERNEL);
119 if (!parts)
120 return -ENOMEM;
121
122 /* Start building partition list */
123 parts[curpart].name = "CFE";
124 parts[curpart].offset = 0;
125 parts[curpart].size = cfelen;
126 curpart++;
127
128 parts[curpart].name = "nvram";
129 parts[curpart].offset = master->size - nvramlen;
130 parts[curpart].size = nvramlen;
131 curpart++;
132
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;
138
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);
142
143 *pparts = parts;
144
145 return nrparts;
146 }
147
148 static int bcm63xx_parse_cfe_partitions(struct mtd_info *master,
149 const struct mtd_partition **pparts,
150 struct mtd_part_parser_data *data)
151 {
152 struct bcm963xx_nvram *nvram = NULL;
153 int ret;
154
155 if (bcm63xx_detect_cfe(master))
156 return -EINVAL;
157
158 nvram = vzalloc(sizeof(*nvram));
159 if (!nvram)
160 return -ENOMEM;
161
162 ret = bcm63xx_read_nvram(master, nvram);
163 if (ret)
164 goto out;
165
166 if (!mtd_type_is_nand(master))
167 ret = bcm63xx_parse_cfe_nor_partitions(master, pparts, nvram);
168 else
169 ret = -EINVAL;
170
171 out:
172 vfree(nvram);
173 return ret;
174 };
175
176 static const struct of_device_id parse_bcm63xx_cfe_match_table[] = {
177 { .compatible = "brcm,bcm963xx-cfe-nor-partitions" },
178 {},
179 };
180 MODULE_DEVICE_TABLE(of, parse_bcm63xx_cfe_match_table);
181
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,
186 };
187 module_mtd_part_parser(bcm63xx_cfe_parser);
188
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");