ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / mtd / redboot.c
1 /*
2  * $Id: redboot.c,v 1.11 2003/05/21 10:39:26 dwmw2 Exp $
3  *
4  * Parse RedBoot-style Flash Image System (FIS) tables and
5  * produce a Linux partition array to match.
6  */
7
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/init.h>
11
12 #include <linux/mtd/mtd.h>
13 #include <linux/mtd/partitions.h>
14
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
25 };
26
27 struct fis_list {
28         struct fis_image_desc *img;
29         struct fis_list *next;
30 };
31
32 static inline int redboot_checksum(struct fis_image_desc *img)
33 {
34         /* RedBoot doesn't actually write the desc_cksum field yet AFAICT */
35         return 1;
36 }
37
38 static int parse_redboot_partitions(struct mtd_info *master, 
39                              struct mtd_partition **pparts,
40                              unsigned long fis_origin)
41 {
42         int nrparts = 0;
43         struct fis_image_desc *buf;
44         struct mtd_partition *parts;
45         struct fis_list *fl = NULL, *tmp_fl;
46         int ret, i;
47         size_t retlen;
48         char *names;
49         char *nullname;
50         int namelen = 0;
51         static char nullstring[] = "unallocated";
52
53         buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
54
55         if (!buf)
56                 return -ENOMEM;
57
58         /* Read the start of the last erase block */
59         ret = master->read(master, master->size - master->erasesize,
60                            PAGE_SIZE, &retlen, (void *)buf);
61
62         if (ret)
63                 goto out;
64
65         if (retlen != PAGE_SIZE) {
66                 ret = -EIO;
67                 goto out;
68         }
69
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))
73                         break;
74         }
75         if (i == 3) {
76                 /* Didn't find it */
77                 printk(KERN_NOTICE "No RedBoot partition table detected in %s\n",
78                        master->name);
79                 ret = 0;
80                 goto out;
81         }
82
83         for (i = 0; i < PAGE_SIZE / sizeof(struct fis_image_desc); i++) {
84                 struct fis_list *new_fl, **prev;
85
86                 if (buf[i].name[0] == 0xff)
87                         break;
88                 if (!redboot_checksum(&buf[i]))
89                         break;
90
91                 new_fl = kmalloc(sizeof(struct fis_list), GFP_KERNEL);
92                 namelen += strlen(buf[i].name)+1;
93                 if (!new_fl) {
94                         ret = -ENOMEM;
95                         goto out;
96                 }
97                 new_fl->img = &buf[i];
98                 if (fis_origin) {
99                         buf[i].flash_base -= fis_origin;
100                 } else {
101                         buf[i].flash_base &= master->size-1;
102                 }
103
104                 /* I'm sure the JFFS2 code has done me permanent damage.
105                  * I now think the following is _normal_
106                  */
107                 prev = &fl;
108                 while(*prev && (*prev)->img->flash_base < new_fl->img->flash_base)
109                         prev = &(*prev)->next;
110                 new_fl->next = *prev;
111                 *prev = new_fl;
112
113                 nrparts++;
114         }
115         if (fl->img->flash_base)
116                 nrparts++;
117
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)
120                         nrparts++;
121         }
122         parts = kmalloc(sizeof(*parts)*nrparts + sizeof(nullstring) + namelen, GFP_KERNEL);
123
124         if (!parts) {
125                 ret = -ENOMEM;
126                 goto out;
127         }
128
129         memset(parts, 0, sizeof(*parts)*nrparts + namelen);
130
131         /* FIXME: Include nullname only if it's used */
132         nullname = (char *)&parts[nrparts];
133         sprintf(nullname, nullstring);
134         names = nullname + sizeof(nullstring);
135
136         i=0;
137
138         if (fl->img->flash_base) {
139                parts[0].name = nullname;
140                parts[0].size = fl->img->flash_base;
141                parts[0].offset = 0;
142         }
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;
147
148                 strcpy(names, fl->img->name);
149                 names += strlen(names)+1;
150
151                 if(fl->next && fl->img->flash_base + fl->img->size + master->erasesize < fl->next->img->flash_base) {
152                         i++;
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;
156                 }
157                 tmp_fl = fl;
158                 fl = fl->next;
159                 kfree(tmp_fl);
160         }
161         ret = nrparts;
162         *pparts = parts;
163  out:
164         while (fl) {
165                 struct fis_list *old = fl;
166                 fl = fl->next;
167                 kfree(old);
168         }
169         kfree(buf);
170         return ret;
171 }
172
173 static struct mtd_part_parser redboot_parser = {
174         .owner = THIS_MODULE,
175         .parse_fn = parse_redboot_partitions,
176         .name = "RedBoot",
177 };
178
179 static int __init redboot_parser_init(void)
180 {
181         return register_mtd_parser(&redboot_parser);
182 }
183
184 static void __exit redboot_parser_exit(void)
185 {
186         deregister_mtd_parser(&redboot_parser);
187 }
188
189 module_init(redboot_parser_init);
190 module_exit(redboot_parser_exit);
191
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");