MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / mtd / redboot.c
blob0212a7df84c4d2f091100590bd87a411804e7a76
1 /*
2 * $Id: redboot.c,v 1.15 2004/08/10 07:55:16 dwmw2 Exp $
4 * Parse RedBoot-style Flash Image System (FIS) tables and
5 * produce a Linux partition array to match.
6 */
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/init.h>
11 #include <linux/vmalloc.h>
13 #include <linux/mtd/mtd.h>
14 #include <linux/mtd/partitions.h>
16 struct fis_image_desc {
17 unsigned char name[16]; // Null terminated name
18 unsigned long flash_base; // Address within FLASH of image
19 unsigned long mem_base; // Address in memory where it executes
20 unsigned long size; // Length of image
21 unsigned long entry_point; // Execution entry point
22 unsigned long data_length; // Length of actual data
23 unsigned char _pad[256-(16+7*sizeof(unsigned long))];
24 unsigned long desc_cksum; // Checksum over image descriptor
25 unsigned long file_cksum; // Checksum over image data
28 struct fis_list {
29 struct fis_image_desc *img;
30 struct fis_list *next;
33 static inline int redboot_checksum(struct fis_image_desc *img)
35 /* RedBoot doesn't actually write the desc_cksum field yet AFAICT */
36 return 1;
39 static int parse_redboot_partitions(struct mtd_info *master,
40 struct mtd_partition **pparts,
41 unsigned long fis_origin)
43 int nrparts = 0;
44 struct fis_image_desc *buf;
45 struct mtd_partition *parts;
46 struct fis_list *fl = NULL, *tmp_fl;
47 int ret, i;
48 size_t retlen;
49 char *names;
50 char *nullname;
51 int namelen = 0;
52 int nulllen = 0;
53 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
54 static char nullstring[] = "unallocated";
55 #endif
57 buf = vmalloc(master->erasesize);
59 if (!buf)
60 return -ENOMEM;
62 /* Read the start of the last erase block */
63 ret = master->read(master, master->size - master->erasesize,
64 master->erasesize, &retlen, (void *)buf);
66 if (ret)
67 goto out;
69 if (retlen != master->erasesize) {
70 ret = -EIO;
71 goto out;
74 /* RedBoot image could appear in any of the first three slots */
75 for (i = 0; i < 3; i++) {
76 if (!memcmp(buf[i].name, "RedBoot", 8))
77 break;
79 if (i == 3) {
80 /* Didn't find it */
81 printk(KERN_NOTICE "No RedBoot partition table detected in %s\n",
82 master->name);
83 ret = 0;
84 goto out;
87 for (i = 0; i < master->erasesize / sizeof(struct fis_image_desc); i++) {
88 struct fis_list *new_fl, **prev;
90 if (buf[i].name[0] == 0xff)
91 break;
92 if (!redboot_checksum(&buf[i]))
93 break;
95 new_fl = kmalloc(sizeof(struct fis_list), GFP_KERNEL);
96 namelen += strlen(buf[i].name)+1;
97 if (!new_fl) {
98 ret = -ENOMEM;
99 goto out;
101 new_fl->img = &buf[i];
102 if (fis_origin) {
103 buf[i].flash_base -= fis_origin;
104 } else {
105 buf[i].flash_base &= master->size-1;
108 /* I'm sure the JFFS2 code has done me permanent damage.
109 * I now think the following is _normal_
111 prev = &fl;
112 while(*prev && (*prev)->img->flash_base < new_fl->img->flash_base)
113 prev = &(*prev)->next;
114 new_fl->next = *prev;
115 *prev = new_fl;
117 nrparts++;
119 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
120 if (fl->img->flash_base) {
121 nrparts++;
122 nulllen = sizeof(nullstring);
125 for (tmp_fl = fl; tmp_fl->next; tmp_fl = tmp_fl->next) {
126 if (tmp_fl->img->flash_base + tmp_fl->img->size + master->erasesize <= tmp_fl->next->img->flash_base) {
127 nrparts++;
128 nulllen = sizeof(nullstring);
131 #endif
132 parts = kmalloc(sizeof(*parts)*nrparts + nulllen + namelen, GFP_KERNEL);
134 if (!parts) {
135 ret = -ENOMEM;
136 goto out;
139 memset(parts, 0, sizeof(*parts)*nrparts + nulllen + namelen);
141 nullname = (char *)&parts[nrparts];
142 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
143 if (nulllen > 0) {
144 strcpy(nullname, nullstring);
146 #endif
147 names = nullname + nulllen;
149 i=0;
151 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
152 if (fl->img->flash_base) {
153 parts[0].name = nullname;
154 parts[0].size = fl->img->flash_base;
155 parts[0].offset = 0;
156 i++;
158 #endif
159 for ( ; i<nrparts; i++) {
160 parts[i].size = fl->img->size;
161 parts[i].offset = fl->img->flash_base;
162 parts[i].name = names;
164 strcpy(names, fl->img->name);
165 #ifdef CONFIG_MTD_REDBOOT_PARTS_READONLY
166 if (!memcmp(names, "RedBoot", 8) ||
167 !memcmp(names, "RedBoot config", 15) ||
168 !memcmp(names, "FIS directory", 14)) {
169 parts[i].mask_flags = MTD_WRITEABLE;
171 #endif
172 names += strlen(names)+1;
174 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
175 if(fl->next && fl->img->flash_base + fl->img->size + master->erasesize <= fl->next->img->flash_base) {
176 i++;
177 parts[i].offset = parts[i-1].size + parts[i-1].offset;
178 parts[i].size = fl->next->img->flash_base - parts[i].offset;
179 parts[i].name = nullname;
181 #endif
182 tmp_fl = fl;
183 fl = fl->next;
184 kfree(tmp_fl);
186 ret = nrparts;
187 *pparts = parts;
188 out:
189 while (fl) {
190 struct fis_list *old = fl;
191 fl = fl->next;
192 kfree(old);
194 vfree(buf);
195 return ret;
198 static struct mtd_part_parser redboot_parser = {
199 .owner = THIS_MODULE,
200 .parse_fn = parse_redboot_partitions,
201 .name = "RedBoot",
204 static int __init redboot_parser_init(void)
206 return register_mtd_parser(&redboot_parser);
209 static void __exit redboot_parser_exit(void)
211 deregister_mtd_parser(&redboot_parser);
214 module_init(redboot_parser_init);
215 module_exit(redboot_parser_exit);
217 MODULE_LICENSE("GPL");
218 MODULE_AUTHOR("Red Hat, Inc. - David Woodhouse <dwmw2@cambridge.redhat.com>");
219 MODULE_DESCRIPTION("Parsing code for RedBoot Flash Image System (FIS) tables");