dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / mtd / parsers / redboot.c
blob957538d57725b71a4361da052a369b5649398a97
1 /*
2 * Parse RedBoot-style Flash Image System (FIS) tables and
3 * produce a Linux partition array to match.
5 * Copyright © 2001 Red Hat UK Limited
6 * Copyright © 2001-2010 David Woodhouse <dwmw2@infradead.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/init.h>
27 #include <linux/vmalloc.h>
28 #include <linux/of.h>
29 #include <linux/mtd/mtd.h>
30 #include <linux/mtd/partitions.h>
31 #include <linux/module.h>
33 struct fis_image_desc {
34 unsigned char name[16]; // Null terminated name
35 uint32_t flash_base; // Address within FLASH of image
36 uint32_t mem_base; // Address in memory where it executes
37 uint32_t size; // Length of image
38 uint32_t entry_point; // Execution entry point
39 uint32_t data_length; // Length of actual data
40 unsigned char _pad[256-(16+7*sizeof(uint32_t))];
41 uint32_t desc_cksum; // Checksum over image descriptor
42 uint32_t file_cksum; // Checksum over image data
45 struct fis_list {
46 struct fis_image_desc *img;
47 struct fis_list *next;
50 static int directory = CONFIG_MTD_REDBOOT_DIRECTORY_BLOCK;
51 module_param(directory, int, 0);
53 static inline int redboot_checksum(struct fis_image_desc *img)
55 /* RedBoot doesn't actually write the desc_cksum field yet AFAICT */
56 return 1;
59 static void parse_redboot_of(struct mtd_info *master)
61 struct device_node *np;
62 u32 dirblock;
63 int ret;
65 np = mtd_get_of_node(master);
66 if (!np)
67 return;
69 ret = of_property_read_u32(np, "fis-index-block", &dirblock);
70 if (ret)
71 return;
74 * Assign the block found in the device tree to the local
75 * directory block pointer.
77 directory = dirblock;
80 static int parse_redboot_partitions(struct mtd_info *master,
81 const struct mtd_partition **pparts,
82 struct mtd_part_parser_data *data)
84 int nrparts = 0;
85 struct fis_image_desc *buf;
86 struct mtd_partition *parts;
87 struct fis_list *fl = NULL, *tmp_fl;
88 int ret, i;
89 size_t retlen;
90 char *names;
91 char *nullname;
92 int namelen = 0;
93 int nulllen = 0;
94 int numslots;
95 unsigned long offset;
96 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
97 static char nullstring[] = "unallocated";
98 #endif
100 parse_redboot_of(master);
102 if ( directory < 0 ) {
103 offset = master->size + directory * master->erasesize;
104 while (mtd_block_isbad(master, offset)) {
105 if (!offset) {
106 nogood:
107 printk(KERN_NOTICE "Failed to find a non-bad block to check for RedBoot partition table\n");
108 return -EIO;
110 offset -= master->erasesize;
112 } else {
113 offset = directory * master->erasesize;
114 while (mtd_block_isbad(master, offset)) {
115 offset += master->erasesize;
116 if (offset == master->size)
117 goto nogood;
120 buf = vmalloc(master->erasesize);
122 if (!buf)
123 return -ENOMEM;
125 printk(KERN_NOTICE "Searching for RedBoot partition table in %s at offset 0x%lx\n",
126 master->name, offset);
128 ret = mtd_read(master, offset, master->erasesize, &retlen,
129 (void *)buf);
131 if (ret)
132 goto out;
134 if (retlen != master->erasesize) {
135 ret = -EIO;
136 goto out;
139 numslots = (master->erasesize / sizeof(struct fis_image_desc));
140 for (i = 0; i < numslots; i++) {
141 if (!memcmp(buf[i].name, "FIS directory", 14)) {
142 /* This is apparently the FIS directory entry for the
143 * FIS directory itself. The FIS directory size is
144 * one erase block; if the buf[i].size field is
145 * swab32(erasesize) then we know we are looking at
146 * a byte swapped FIS directory - swap all the entries!
147 * (NOTE: this is 'size' not 'data_length'; size is
148 * the full size of the entry.)
151 /* RedBoot can combine the FIS directory and
152 config partitions into a single eraseblock;
153 we assume wrong-endian if either the swapped
154 'size' matches the eraseblock size precisely,
155 or if the swapped size actually fits in an
156 eraseblock while the unswapped size doesn't. */
157 if (swab32(buf[i].size) == master->erasesize ||
158 (buf[i].size > master->erasesize
159 && swab32(buf[i].size) < master->erasesize)) {
160 int j;
161 /* Update numslots based on actual FIS directory size */
162 numslots = swab32(buf[i].size) / sizeof (struct fis_image_desc);
163 for (j = 0; j < numslots; ++j) {
165 /* A single 0xff denotes a deleted entry.
166 * Two of them in a row is the end of the table.
168 if (buf[j].name[0] == 0xff) {
169 if (buf[j].name[1] == 0xff) {
170 break;
171 } else {
172 continue;
176 /* The unsigned long fields were written with the
177 * wrong byte sex, name and pad have no byte sex.
179 swab32s(&buf[j].flash_base);
180 swab32s(&buf[j].mem_base);
181 swab32s(&buf[j].size);
182 swab32s(&buf[j].entry_point);
183 swab32s(&buf[j].data_length);
184 swab32s(&buf[j].desc_cksum);
185 swab32s(&buf[j].file_cksum);
187 } else if (buf[i].size < master->erasesize) {
188 /* Update numslots based on actual FIS directory size */
189 numslots = buf[i].size / sizeof(struct fis_image_desc);
191 break;
194 if (i == numslots) {
195 /* Didn't find it */
196 printk(KERN_NOTICE "No RedBoot partition table detected in %s\n",
197 master->name);
198 ret = 0;
199 goto out;
202 for (i = 0; i < numslots; i++) {
203 struct fis_list *new_fl, **prev;
205 if (buf[i].name[0] == 0xff) {
206 if (buf[i].name[1] == 0xff) {
207 break;
208 } else {
209 continue;
212 if (!redboot_checksum(&buf[i]))
213 break;
215 new_fl = kmalloc(sizeof(struct fis_list), GFP_KERNEL);
216 namelen += strlen(buf[i].name)+1;
217 if (!new_fl) {
218 ret = -ENOMEM;
219 goto out;
221 new_fl->img = &buf[i];
222 if (data && data->origin)
223 buf[i].flash_base -= data->origin;
224 else
225 buf[i].flash_base &= master->size-1;
227 /* I'm sure the JFFS2 code has done me permanent damage.
228 * I now think the following is _normal_
230 prev = &fl;
231 while(*prev && (*prev)->img->flash_base < new_fl->img->flash_base)
232 prev = &(*prev)->next;
233 new_fl->next = *prev;
234 *prev = new_fl;
236 nrparts++;
238 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
239 if (fl->img->flash_base) {
240 nrparts++;
241 nulllen = sizeof(nullstring);
244 for (tmp_fl = fl; tmp_fl->next; tmp_fl = tmp_fl->next) {
245 if (tmp_fl->img->flash_base + tmp_fl->img->size + master->erasesize <= tmp_fl->next->img->flash_base) {
246 nrparts++;
247 nulllen = sizeof(nullstring);
250 #endif
251 parts = kzalloc(sizeof(*parts)*nrparts + nulllen + namelen, GFP_KERNEL);
253 if (!parts) {
254 ret = -ENOMEM;
255 goto out;
258 nullname = (char *)&parts[nrparts];
259 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
260 if (nulllen > 0) {
261 strcpy(nullname, nullstring);
263 #endif
264 names = nullname + nulllen;
266 i=0;
268 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
269 if (fl->img->flash_base) {
270 parts[0].name = nullname;
271 parts[0].size = fl->img->flash_base;
272 parts[0].offset = 0;
273 i++;
275 #endif
276 for ( ; i<nrparts; i++) {
277 parts[i].size = fl->img->size;
278 parts[i].offset = fl->img->flash_base;
279 parts[i].name = names;
281 strcpy(names, fl->img->name);
282 #ifdef CONFIG_MTD_REDBOOT_PARTS_READONLY
283 if (!memcmp(names, "RedBoot", 8) ||
284 !memcmp(names, "RedBoot config", 15) ||
285 !memcmp(names, "FIS directory", 14)) {
286 parts[i].mask_flags = MTD_WRITEABLE;
288 #endif
289 names += strlen(names)+1;
291 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
292 if(fl->next && fl->img->flash_base + fl->img->size + master->erasesize <= fl->next->img->flash_base) {
293 i++;
294 parts[i].offset = parts[i-1].size + parts[i-1].offset;
295 parts[i].size = fl->next->img->flash_base - parts[i].offset;
296 parts[i].name = nullname;
298 #endif
299 tmp_fl = fl;
300 fl = fl->next;
301 kfree(tmp_fl);
303 ret = nrparts;
304 *pparts = parts;
305 out:
306 while (fl) {
307 struct fis_list *old = fl;
308 fl = fl->next;
309 kfree(old);
311 vfree(buf);
312 return ret;
315 static const struct of_device_id mtd_parser_redboot_of_match_table[] = {
316 { .compatible = "redboot-fis" },
319 MODULE_DEVICE_TABLE(of, mtd_parser_redboot_of_match_table);
321 static struct mtd_part_parser redboot_parser = {
322 .parse_fn = parse_redboot_partitions,
323 .name = "RedBoot",
324 .of_match_table = mtd_parser_redboot_of_match_table,
326 module_mtd_part_parser(redboot_parser);
328 /* mtd parsers will request the module by parser name */
329 MODULE_ALIAS("RedBoot");
330 MODULE_LICENSE("GPL");
331 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
332 MODULE_DESCRIPTION("Parsing code for RedBoot Flash Image System (FIS) tables");