2 * $Id: redboot.c,v 1.13 2004/04/01 10:17:40 gthomas 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;
52 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
53 static char nullstring[] = "unallocated";
56 buf = kmalloc(master->erasesize, GFP_KERNEL);
61 /* Read the start of the last erase block */
62 ret = master->read(master, master->size - master->erasesize,
63 master->erasesize, &retlen, (void *)buf);
68 if (retlen != master->erasesize) {
73 /* RedBoot image could appear in any of the first three slots */
74 for (i = 0; i < 3; i++) {
75 if (!memcmp(buf[i].name, "RedBoot", 8))
80 printk(KERN_NOTICE "No RedBoot partition table detected in %s\n",
86 for (i = 0; i < master->erasesize / sizeof(struct fis_image_desc); i++) {
87 struct fis_list *new_fl, **prev;
89 if (buf[i].name[0] == 0xff)
91 if (!redboot_checksum(&buf[i]))
94 new_fl = kmalloc(sizeof(struct fis_list), GFP_KERNEL);
95 namelen += strlen(buf[i].name)+1;
100 new_fl->img = &buf[i];
102 buf[i].flash_base -= fis_origin;
104 buf[i].flash_base &= master->size-1;
107 /* I'm sure the JFFS2 code has done me permanent damage.
108 * I now think the following is _normal_
111 while(*prev && (*prev)->img->flash_base < new_fl->img->flash_base)
112 prev = &(*prev)->next;
113 new_fl->next = *prev;
118 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
119 if (fl->img->flash_base) {
121 nulllen = sizeof(nullstring);
124 for (tmp_fl = fl; tmp_fl->next; tmp_fl = tmp_fl->next) {
125 if (tmp_fl->img->flash_base + tmp_fl->img->size + master->erasesize <= tmp_fl->next->img->flash_base) {
127 nulllen = sizeof(nullstring);
131 parts = kmalloc(sizeof(*parts)*nrparts + nulllen + namelen, GFP_KERNEL);
138 memset(parts, 0, sizeof(*parts)*nrparts + nulllen + namelen);
140 nullname = (char *)&parts[nrparts];
141 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
143 strcpy(nullname, nullstring);
146 names = nullname + nulllen;
150 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
151 if (fl->img->flash_base) {
152 parts[0].name = nullname;
153 parts[0].size = fl->img->flash_base;
158 for ( ; i<nrparts; i++) {
159 parts[i].size = fl->img->size;
160 parts[i].offset = fl->img->flash_base;
161 parts[i].name = names;
163 strcpy(names, fl->img->name);
164 #ifdef CONFIG_MTD_REDBOOT_PARTS_READONLY
165 if (!memcmp(names, "RedBoot", 8) ||
166 !memcmp(names, "RedBoot config", 15) ||
167 !memcmp(names, "FIS directory", 14)) {
168 parts[i].mask_flags = MTD_WRITEABLE;
171 names += strlen(names)+1;
173 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
174 if(fl->next && fl->img->flash_base + fl->img->size + master->erasesize <= fl->next->img->flash_base) {
176 parts[i].offset = parts[i-1].size + parts[i-1].offset;
177 parts[i].size = fl->next->img->flash_base - parts[i].offset;
178 parts[i].name = nullname;
189 struct fis_list *old = fl;
197 static struct mtd_part_parser redboot_parser = {
198 .owner = THIS_MODULE,
199 .parse_fn = parse_redboot_partitions,
203 static int __init redboot_parser_init(void)
205 return register_mtd_parser(&redboot_parser);
208 static void __exit redboot_parser_exit(void)
210 deregister_mtd_parser(&redboot_parser);
213 module_init(redboot_parser_init);
214 module_exit(redboot_parser_exit);
216 MODULE_LICENSE("GPL");
217 MODULE_AUTHOR("Red Hat, Inc. - David Woodhouse <dwmw2@cambridge.redhat.com>");
218 MODULE_DESCRIPTION("Parsing code for RedBoot Flash Image System (FIS) tables");