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.
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
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 */
39 static int parse_redboot_partitions(struct mtd_info
*master
,
40 struct mtd_partition
**pparts
,
41 unsigned long fis_origin
)
44 struct fis_image_desc
*buf
;
45 struct mtd_partition
*parts
;
46 struct fis_list
*fl
= NULL
, *tmp_fl
;
53 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
54 static char nullstring
[] = "unallocated";
57 buf
= vmalloc(master
->erasesize
);
62 /* Read the start of the last erase block */
63 ret
= master
->read(master
, master
->size
- master
->erasesize
,
64 master
->erasesize
, &retlen
, (void *)buf
);
69 if (retlen
!= master
->erasesize
) {
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))
81 printk(KERN_NOTICE
"No RedBoot partition table detected in %s\n",
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)
92 if (!redboot_checksum(&buf
[i
]))
95 new_fl
= kmalloc(sizeof(struct fis_list
), GFP_KERNEL
);
96 namelen
+= strlen(buf
[i
].name
)+1;
101 new_fl
->img
= &buf
[i
];
103 buf
[i
].flash_base
-= fis_origin
;
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_
112 while(*prev
&& (*prev
)->img
->flash_base
< new_fl
->img
->flash_base
)
113 prev
= &(*prev
)->next
;
114 new_fl
->next
= *prev
;
119 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
120 if (fl
->img
->flash_base
) {
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
) {
128 nulllen
= sizeof(nullstring
);
132 parts
= kmalloc(sizeof(*parts
)*nrparts
+ nulllen
+ namelen
, GFP_KERNEL
);
139 memset(parts
, 0, sizeof(*parts
)*nrparts
+ nulllen
+ namelen
);
141 nullname
= (char *)&parts
[nrparts
];
142 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
144 strcpy(nullname
, nullstring
);
147 names
= nullname
+ nulllen
;
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
;
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
;
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
) {
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
;
190 struct fis_list
*old
= fl
;
198 static struct mtd_part_parser redboot_parser
= {
199 .owner
= THIS_MODULE
,
200 .parse_fn
= parse_redboot_partitions
,
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");