2 * $Id: redboot.c,v 1.11 2003/05/21 10:39:26 dwmw2 Exp $
4 * Parse RedBoot-style Flash Image System (FIS) tables and
5 * produce a Linux partition array to match.
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/init.h>
12 #include <linux/mtd/mtd.h>
13 #include <linux/mtd/partitions.h>
15 struct fis_image_desc {
16 unsigned char name[16]; // Null terminated name
17 unsigned long flash_base; // Address within FLASH of image
18 unsigned long mem_base; // Address in memory where it executes
19 unsigned long size; // Length of image
20 unsigned long entry_point; // Execution entry point
21 unsigned long data_length; // Length of actual data
22 unsigned char _pad[256-(16+7*sizeof(unsigned long))];
23 unsigned long desc_cksum; // Checksum over image descriptor
24 unsigned long file_cksum; // Checksum over image data
28 struct fis_image_desc *img;
29 struct fis_list *next;
32 static inline int redboot_checksum(struct fis_image_desc *img)
34 /* RedBoot doesn't actually write the desc_cksum field yet AFAICT */
38 static int parse_redboot_partitions(struct mtd_info *master,
39 struct mtd_partition **pparts,
40 unsigned long fis_origin)
43 struct fis_image_desc *buf;
44 struct mtd_partition *parts;
45 struct fis_list *fl = NULL, *tmp_fl;
51 static char nullstring[] = "unallocated";
53 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
58 /* Read the start of the last erase block */
59 ret = master->read(master, master->size - master->erasesize,
60 PAGE_SIZE, &retlen, (void *)buf);
65 if (retlen != PAGE_SIZE) {
70 /* RedBoot image could appear in any of the first three slots */
71 for (i = 0; i < 3; i++) {
72 if (!memcmp(buf[i].name, "RedBoot", 8))
77 printk(KERN_NOTICE "No RedBoot partition table detected in %s\n",
83 for (i = 0; i < PAGE_SIZE / sizeof(struct fis_image_desc); i++) {
84 struct fis_list *new_fl, **prev;
86 if (buf[i].name[0] == 0xff)
88 if (!redboot_checksum(&buf[i]))
91 new_fl = kmalloc(sizeof(struct fis_list), GFP_KERNEL);
92 namelen += strlen(buf[i].name)+1;
97 new_fl->img = &buf[i];
99 buf[i].flash_base -= fis_origin;
101 buf[i].flash_base &= master->size-1;
104 /* I'm sure the JFFS2 code has done me permanent damage.
105 * I now think the following is _normal_
108 while(*prev && (*prev)->img->flash_base < new_fl->img->flash_base)
109 prev = &(*prev)->next;
110 new_fl->next = *prev;
115 if (fl->img->flash_base)
118 for (tmp_fl = fl; tmp_fl->next; tmp_fl = tmp_fl->next) {
119 if (tmp_fl->img->flash_base + tmp_fl->img->size + master->erasesize < tmp_fl->next->img->flash_base)
122 parts = kmalloc(sizeof(*parts)*nrparts + sizeof(nullstring) + namelen, GFP_KERNEL);
129 memset(parts, 0, sizeof(*parts)*nrparts + namelen);
131 /* FIXME: Include nullname only if it's used */
132 nullname = (char *)&parts[nrparts];
133 sprintf(nullname, nullstring);
134 names = nullname + sizeof(nullstring);
138 if (fl->img->flash_base) {
139 parts[0].name = nullname;
140 parts[0].size = fl->img->flash_base;
143 for ( ; i<nrparts; i++) {
144 parts[i].size = fl->img->size;
145 parts[i].offset = fl->img->flash_base;
146 parts[i].name = names;
148 strcpy(names, fl->img->name);
149 names += strlen(names)+1;
151 if(fl->next && fl->img->flash_base + fl->img->size + master->erasesize < fl->next->img->flash_base) {
153 parts[i].offset = parts[i-1].size + parts[i-1].offset;
154 parts[i].size = fl->next->img->flash_base - parts[i].offset;
155 parts[i].name = nullname;
165 struct fis_list *old = fl;
173 static struct mtd_part_parser redboot_parser = {
174 .owner = THIS_MODULE,
175 .parse_fn = parse_redboot_partitions,
179 static int __init redboot_parser_init(void)
181 return register_mtd_parser(&redboot_parser);
184 static void __exit redboot_parser_exit(void)
186 deregister_mtd_parser(&redboot_parser);
189 module_init(redboot_parser_init);
190 module_exit(redboot_parser_exit);
192 MODULE_LICENSE("GPL");
193 MODULE_AUTHOR("Red Hat, Inc. - David Woodhouse <dwmw2@cambridge.redhat.com>");
194 MODULE_DESCRIPTION("Parsing code for RedBoot Flash Image System (FIS) tables");