2 * $Id: redboot.c,v 1.21 2006/03/30 18:34:37 bjd 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>
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 uint32_t flash_base; // Address within FLASH of image
19 uint32_t mem_base; // Address in memory where it executes
20 uint32_t size; // Length of image
21 uint32_t entry_point; // Execution entry point
22 uint32_t data_length; // Length of actual data
23 unsigned char _pad[256-(16+7*sizeof(uint32_t))];
24 uint32_t desc_cksum; // Checksum over image descriptor
25 uint32_t file_cksum; // Checksum over image data
29 struct fis_image_desc *img;
30 struct fis_list *next;
33 static int directory = CONFIG_MTD_REDBOOT_DIRECTORY_BLOCK;
34 module_param(directory, int, 0);
36 static inline int redboot_checksum(struct fis_image_desc *img)
38 /* RedBoot doesn't actually write the desc_cksum field yet AFAICT */
42 static int parse_redboot_partitions(struct mtd_info *master,
43 struct mtd_partition **pparts,
44 unsigned long fis_origin)
47 struct fis_image_desc *buf;
48 struct mtd_partition *parts;
49 struct fis_list *fl = NULL, *tmp_fl;
58 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
59 static char nullstring[] = "unallocated";
62 if ( directory < 0 ) {
63 offset = master->size + directory * master->erasesize;
64 while (master->block_isbad &&
65 master->block_isbad(master, offset)) {
68 printk(KERN_NOTICE "Failed to find a non-bad block to check for RedBoot partition table\n");
71 offset -= master->erasesize;
74 offset = directory * master->erasesize;
75 while (master->block_isbad &&
76 master->block_isbad(master, offset)) {
77 offset += master->erasesize;
78 if (offset == master->size)
82 buf = vmalloc(master->erasesize);
87 printk(KERN_NOTICE "Searching for RedBoot partition table in %s at offset 0x%lx\n",
88 master->name, offset);
90 ret = master->read(master, offset,
91 master->erasesize, &retlen, (void *)buf);
96 if (retlen != master->erasesize) {
101 numslots = (master->erasesize / sizeof(struct fis_image_desc));
102 for (i = 0; i < numslots; i++) {
103 if (!memcmp(buf[i].name, "FIS directory", 14)) {
104 /* This is apparently the FIS directory entry for the
105 * FIS directory itself. The FIS directory size is
106 * one erase block; if the buf[i].size field is
107 * swab32(erasesize) then we know we are looking at
108 * a byte swapped FIS directory - swap all the entries!
109 * (NOTE: this is 'size' not 'data_length'; size is
110 * the full size of the entry.)
113 /* RedBoot can combine the FIS directory and
114 config partitions into a single eraseblock;
115 we assume wrong-endian if either the swapped
116 'size' matches the eraseblock size precisely,
117 or if the swapped size actually fits in an
118 eraseblock while the unswapped size doesn't. */
119 if (swab32(buf[i].size) == master->erasesize ||
120 (buf[i].size > master->erasesize
121 && swab32(buf[i].size) < master->erasesize)) {
123 /* Update numslots based on actual FIS directory size */
124 numslots = swab32(buf[i].size) / sizeof (struct fis_image_desc);
125 for (j = 0; j < numslots; ++j) {
127 /* A single 0xff denotes a deleted entry.
128 * Two of them in a row is the end of the table.
130 if (buf[j].name[0] == 0xff) {
131 if (buf[j].name[1] == 0xff) {
138 /* The unsigned long fields were written with the
139 * wrong byte sex, name and pad have no byte sex.
141 swab32s(&buf[j].flash_base);
142 swab32s(&buf[j].mem_base);
143 swab32s(&buf[j].size);
144 swab32s(&buf[j].entry_point);
145 swab32s(&buf[j].data_length);
146 swab32s(&buf[j].desc_cksum);
147 swab32s(&buf[j].file_cksum);
149 } else if (buf[i].size < master->erasesize) {
150 /* Update numslots based on actual FIS directory size */
151 numslots = buf[i].size / sizeof(struct fis_image_desc);
158 printk(KERN_NOTICE "No RedBoot partition table detected in %s\n",
164 for (i = 0; i < numslots; i++) {
165 struct fis_list *new_fl, **prev;
167 if (buf[i].name[0] == 0xff) {
168 if (buf[i].name[1] == 0xff) {
174 if (!redboot_checksum(&buf[i]))
177 new_fl = kmalloc(sizeof(struct fis_list), GFP_KERNEL);
178 namelen += strlen(buf[i].name)+1;
183 new_fl->img = &buf[i];
185 buf[i].flash_base -= fis_origin;
187 buf[i].flash_base &= master->size-1;
190 /* I'm sure the JFFS2 code has done me permanent damage.
191 * I now think the following is _normal_
194 while(*prev && (*prev)->img->flash_base < new_fl->img->flash_base)
195 prev = &(*prev)->next;
196 new_fl->next = *prev;
201 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
202 if (fl->img->flash_base) {
204 nulllen = sizeof(nullstring);
207 for (tmp_fl = fl; tmp_fl->next; tmp_fl = tmp_fl->next) {
208 if (tmp_fl->img->flash_base + tmp_fl->img->size + master->erasesize <= tmp_fl->next->img->flash_base) {
210 nulllen = sizeof(nullstring);
214 parts = kzalloc(sizeof(*parts)*nrparts + nulllen + namelen, GFP_KERNEL);
221 nullname = (char *)&parts[nrparts];
222 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
224 strcpy(nullname, nullstring);
227 names = nullname + nulllen;
231 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
232 if (fl->img->flash_base) {
233 parts[0].name = nullname;
234 parts[0].size = fl->img->flash_base;
239 for ( ; i<nrparts; i++) {
240 parts[i].size = fl->img->size;
241 parts[i].offset = fl->img->flash_base;
242 parts[i].name = names;
244 strcpy(names, fl->img->name);
245 #ifdef CONFIG_MTD_REDBOOT_PARTS_READONLY
246 if (!memcmp(names, "RedBoot", 8) ||
247 !memcmp(names, "RedBoot config", 15) ||
248 !memcmp(names, "FIS directory", 14)) {
249 parts[i].mask_flags = MTD_WRITEABLE;
252 names += strlen(names)+1;
254 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
255 if(fl->next && fl->img->flash_base + fl->img->size + master->erasesize <= fl->next->img->flash_base) {
257 parts[i].offset = parts[i-1].size + parts[i-1].offset;
258 parts[i].size = fl->next->img->flash_base - parts[i].offset;
259 parts[i].name = nullname;
270 struct fis_list *old = fl;
278 static struct mtd_part_parser redboot_parser = {
279 .owner = THIS_MODULE,
280 .parse_fn = parse_redboot_partitions,
284 static int __init redboot_parser_init(void)
286 return register_mtd_parser(&redboot_parser);
289 static void __exit redboot_parser_exit(void)
291 deregister_mtd_parser(&redboot_parser);
294 module_init(redboot_parser_init);
295 module_exit(redboot_parser_exit);
297 MODULE_LICENSE("GPL");
298 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
299 MODULE_DESCRIPTION("Parsing code for RedBoot Flash Image System (FIS) tables");