Blame view

drivers/mtd/bcm63xxpart.c 5.26 KB
70a3c167c   Jonas Gorski   mtd: maps: bcm963...
1
2
3
4
5
6
  /*
   * BCM63XX CFE image tag parser
   *
   * Copyright © 2006-2008  Florian Fainelli <florian@openwrt.org>
   *			  Mike Albon <malbon@openwrt.org>
   * Copyright © 2009-2010  Daniel Dickinson <openwrt@cshore.neomailbox.net>
d085eea16   Jonas Gorski   mtd: bcm63xxpart:...
7
   * Copyright © 2011-2013  Jonas Gorski <jonas.gorski@gmail.com>
70a3c167c   Jonas Gorski   mtd: maps: bcm963...
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   *
   */
  
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
436e94a6f   Simon Arlott   mtd: bcm63xxpart:...
26
  #include <linux/bcm963xx_nvram.h>
8fce60b8d   Simon Arlott   MIPS: bcm963xx: M...
27
  #include <linux/bcm963xx_tag.h>
f98872fc1   Jonas Gorski   mtd: bcm63xxpart:...
28
  #include <linux/crc32.h>
70a3c167c   Jonas Gorski   mtd: maps: bcm963...
29
30
  #include <linux/module.h>
  #include <linux/kernel.h>
c88fd9d97   Jonas Gorski   mtd: bcm63xxpart:...
31
  #include <linux/sizes.h>
70a3c167c   Jonas Gorski   mtd: maps: bcm963...
32
33
34
35
  #include <linux/slab.h>
  #include <linux/vmalloc.h>
  #include <linux/mtd/mtd.h>
  #include <linux/mtd/partitions.h>
e651de475   Jonas Gorski   mtd: bcm63xxpart:...
36
  #include <linux/of.h>
70a3c167c   Jonas Gorski   mtd: maps: bcm963...
37

436e94a6f   Simon Arlott   mtd: bcm63xxpart:...
38
  #define BCM963XX_CFE_BLOCK_SIZE		SZ_64K	/* always at least 64KiB */
70a3c167c   Jonas Gorski   mtd: maps: bcm963...
39

436e94a6f   Simon Arlott   mtd: bcm63xxpart:...
40
41
42
  #define BCM963XX_CFE_MAGIC_OFFSET	0x4e0
  #define BCM963XX_CFE_VERSION_OFFSET	0x570
  #define BCM963XX_NVRAM_OFFSET		0x580
f2d9739b8   Jonas Gorski   mtd: bcm63xxpart:...
43

7fffa694a   Simon Arlott   mtd: bcm63xxpart:...
44
45
46
  /* Ensure strings read from flash structs are null terminated */
  #define STR_NULL_TERMINATE(x) \
  	do { char *_str = (x); _str[sizeof(x) - 1] = 0; } while (0)
70a3c167c   Jonas Gorski   mtd: maps: bcm963...
47
48
  static int bcm63xx_detect_cfe(struct mtd_info *master)
  {
70a3c167c   Jonas Gorski   mtd: maps: bcm963...
49
50
51
  	char buf[9];
  	int ret;
  	size_t retlen;
329ad399a   Artem Bityutskiy   mtd: introduce mt...
52
53
  	ret = mtd_read(master, BCM963XX_CFE_VERSION_OFFSET, 5, &retlen,
  		       (void *)buf);
f2d9739b8   Jonas Gorski   mtd: bcm63xxpart:...
54
55
56
57
58
59
60
61
62
  	buf[retlen] = 0;
  
  	if (ret)
  		return ret;
  
  	if (strncmp("cfe-v", buf, 5) == 0)
  		return 0;
  
  	/* very old CFE's do not have the cfe-v string, so check for magic */
436e94a6f   Simon Arlott   mtd: bcm63xxpart:...
63
  	ret = mtd_read(master, BCM963XX_CFE_MAGIC_OFFSET, 8, &retlen,
329ad399a   Artem Bityutskiy   mtd: introduce mt...
64
  		       (void *)buf);
70a3c167c   Jonas Gorski   mtd: maps: bcm963...
65
  	buf[retlen] = 0;
70a3c167c   Jonas Gorski   mtd: maps: bcm963...
66

f2d9739b8   Jonas Gorski   mtd: bcm63xxpart:...
67
  	return strncmp("CFE1CFE1", buf, 8);
70a3c167c   Jonas Gorski   mtd: maps: bcm963...
68
  }
436e94a6f   Simon Arlott   mtd: bcm63xxpart:...
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  static int bcm63xx_read_nvram(struct mtd_info *master,
  	struct bcm963xx_nvram *nvram)
  {
  	u32 actual_crc, expected_crc;
  	size_t retlen;
  	int ret;
  
  	/* extract nvram data */
  	ret = mtd_read(master, BCM963XX_NVRAM_OFFSET, BCM963XX_NVRAM_V5_SIZE,
  			&retlen, (void *)nvram);
  	if (ret)
  		return ret;
  
  	ret = bcm963xx_nvram_checksum(nvram, &expected_crc, &actual_crc);
  	if (ret)
  		pr_warn("nvram checksum failed, contents may be invalid (expected %08x, got %08x)
  ",
  			expected_crc, actual_crc);
  
  	if (!nvram->psi_size)
  		nvram->psi_size = BCM963XX_DEFAULT_PSI_SIZE;
  
  	return 0;
  }
dd84cb022   Jonas Gorski   mtd: bcm63xxpart:...
93
94
95
96
  static const char * const bcm63xx_cfe_part_types[] = {
  	"bcm963xx-imagetag",
  	NULL,
  };
7fffa694a   Simon Arlott   mtd: bcm63xxpart:...
97

4110fdd29   Simon Arlott   mtd: bcm63xxpart:...
98
99
  static int bcm63xx_parse_cfe_nor_partitions(struct mtd_info *master,
  	const struct mtd_partition **pparts, struct bcm963xx_nvram *nvram)
70a3c167c   Jonas Gorski   mtd: maps: bcm963...
100
  {
70a3c167c   Jonas Gorski   mtd: maps: bcm963...
101
  	struct mtd_partition *parts;
dd84cb022   Jonas Gorski   mtd: bcm63xxpart:...
102
  	int nrparts = 3, curpart = 0;
678eb9bb8   Jonas Gorski   mtd: bcm63xxpart:...
103
  	unsigned int cfelen, nvramlen;
4e4fb6395   Jonas Gorski   mtd: bcm63xxpart:...
104
  	unsigned int cfe_erasesize;
70a3c167c   Jonas Gorski   mtd: maps: bcm963...
105
  	int i;
70a3c167c   Jonas Gorski   mtd: maps: bcm963...
106

4e4fb6395   Jonas Gorski   mtd: bcm63xxpart:...
107
  	cfe_erasesize = max_t(uint32_t, master->erasesize,
436e94a6f   Simon Arlott   mtd: bcm63xxpart:...
108
  			      BCM963XX_CFE_BLOCK_SIZE);
4e4fb6395   Jonas Gorski   mtd: bcm63xxpart:...
109
110
  
  	cfelen = cfe_erasesize;
436e94a6f   Simon Arlott   mtd: bcm63xxpart:...
111
  	nvramlen = nvram->psi_size * SZ_1K;
d085eea16   Jonas Gorski   mtd: bcm63xxpart:...
112
  	nvramlen = roundup(nvramlen, cfe_erasesize);
678eb9bb8   Jonas Gorski   mtd: bcm63xxpart:...
113

70a3c167c   Jonas Gorski   mtd: maps: bcm963...
114
  	parts = kzalloc(sizeof(*parts) * nrparts + 10 * nrparts, GFP_KERNEL);
dd84cb022   Jonas Gorski   mtd: bcm63xxpart:...
115
116
  	if (!parts)
  		return -ENOMEM;
70a3c167c   Jonas Gorski   mtd: maps: bcm963...
117
118
119
120
  
  	/* Start building partition list */
  	parts[curpart].name = "CFE";
  	parts[curpart].offset = 0;
678eb9bb8   Jonas Gorski   mtd: bcm63xxpart:...
121
  	parts[curpart].size = cfelen;
70a3c167c   Jonas Gorski   mtd: maps: bcm963...
122
  	curpart++;
70a3c167c   Jonas Gorski   mtd: maps: bcm963...
123
  	parts[curpart].name = "nvram";
678eb9bb8   Jonas Gorski   mtd: bcm63xxpart:...
124
125
  	parts[curpart].offset = master->size - nvramlen;
  	parts[curpart].size = nvramlen;
f3f9a5dac   Jonas Gorski   mtd: bcm63xxpart:...
126
  	curpart++;
70a3c167c   Jonas Gorski   mtd: maps: bcm963...
127
128
  
  	/* Global partition "linux" to make easy firmware upgrade */
70a3c167c   Jonas Gorski   mtd: maps: bcm963...
129
  	parts[curpart].name = "linux";
327c62c55   Jonas Gorski   mtd: bcm63xxpart:...
130
131
  	parts[curpart].offset = cfelen;
  	parts[curpart].size = master->size - cfelen - nvramlen;
dd84cb022   Jonas Gorski   mtd: bcm63xxpart:...
132
  	parts[curpart].types = bcm63xx_cfe_part_types;
70a3c167c   Jonas Gorski   mtd: maps: bcm963...
133
134
  
  	for (i = 0; i < nrparts; i++)
60768368b   Jonas Gorski   mtd: bcm63xxpart:...
135
136
137
  		pr_info("Partition %d is %s offset %llx and length %llx
  ", i,
  			parts[i].name, parts[i].offset,	parts[i].size);
70a3c167c   Jonas Gorski   mtd: maps: bcm963...
138

70a3c167c   Jonas Gorski   mtd: maps: bcm963...
139
  	*pparts = parts;
436e94a6f   Simon Arlott   mtd: bcm63xxpart:...
140

70a3c167c   Jonas Gorski   mtd: maps: bcm963...
141
  	return nrparts;
4110fdd29   Simon Arlott   mtd: bcm63xxpart:...
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
  }
  
  static int bcm63xx_parse_cfe_partitions(struct mtd_info *master,
  					const struct mtd_partition **pparts,
  					struct mtd_part_parser_data *data)
  {
  	struct bcm963xx_nvram *nvram = NULL;
  	int ret;
  
  	if (bcm63xx_detect_cfe(master))
  		return -EINVAL;
  
  	nvram = vzalloc(sizeof(*nvram));
  	if (!nvram)
  		return -ENOMEM;
  
  	ret = bcm63xx_read_nvram(master, nvram);
  	if (ret)
  		goto out;
  
  	if (!mtd_type_is_nand(master))
  		ret = bcm63xx_parse_cfe_nor_partitions(master, pparts, nvram);
  	else
  		ret = -EINVAL;
  
  out:
  	vfree(nvram);
  	return ret;
70a3c167c   Jonas Gorski   mtd: maps: bcm963...
170
  };
e651de475   Jonas Gorski   mtd: bcm63xxpart:...
171
172
173
174
175
  static const struct of_device_id parse_bcm63xx_cfe_match_table[] = {
  	{ .compatible = "brcm,bcm963xx-cfe-nor-partitions" },
  	{},
  };
  MODULE_DEVICE_TABLE(of, parse_bcm63xx_cfe_match_table);
70a3c167c   Jonas Gorski   mtd: maps: bcm963...
176
  static struct mtd_part_parser bcm63xx_cfe_parser = {
70a3c167c   Jonas Gorski   mtd: maps: bcm963...
177
178
  	.parse_fn = bcm63xx_parse_cfe_partitions,
  	.name = "bcm63xxpart",
e651de475   Jonas Gorski   mtd: bcm63xxpart:...
179
  	.of_match_table = parse_bcm63xx_cfe_match_table,
70a3c167c   Jonas Gorski   mtd: maps: bcm963...
180
  };
b8f70badb   Brian Norris   mtd: kill off MTD...
181
  module_mtd_part_parser(bcm63xx_cfe_parser);
70a3c167c   Jonas Gorski   mtd: maps: bcm963...
182
183
184
185
186
187
188
  
  MODULE_LICENSE("GPL");
  MODULE_AUTHOR("Daniel Dickinson <openwrt@cshore.neomailbox.net>");
  MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
  MODULE_AUTHOR("Mike Albon <malbon@openwrt.org>");
  MODULE_AUTHOR("Jonas Gorski <jonas.gorski@gmail.com");
  MODULE_DESCRIPTION("MTD partitioning for BCM63XX CFE bootloaders");