2 * Parse RedBoot-style Flash Image System (FIS) tables and
3 * produce a Linux partition array to match.
6 #include <linux/kernel.h>
7 #include <linux/slab.h>
8 #include <linux/init.h>
9 #include <linux/vmalloc.h>
11 #include <linux/mtd/mtd.h>
12 #include <linux/mtd/partitions.h>
14 struct fis_image_desc
{
15 unsigned char name
[16]; // Null terminated name
16 uint32_t flash_base
; // Address within FLASH of image
17 uint32_t mem_base
; // Address in memory where it executes
18 uint32_t size
; // Length of image
19 uint32_t entry_point
; // Execution entry point
20 uint32_t data_length
; // Length of actual data
21 unsigned char _pad
[256-(16+7*sizeof(uint32_t))];
22 uint32_t desc_cksum
; // Checksum over image descriptor
23 uint32_t file_cksum
; // Checksum over image data
27 struct fis_image_desc
*img
;
28 struct fis_list
*next
;
31 static int directory
= CONFIG_MTD_REDBOOT_DIRECTORY_BLOCK
;
32 module_param(directory
, int, 0);
34 static inline int redboot_checksum(struct fis_image_desc
*img
)
36 /* RedBoot doesn't actually write the desc_cksum field yet AFAICT */
40 static int parse_redboot_partitions(struct mtd_info
*master
,
41 struct mtd_partition
**pparts
,
42 unsigned long fis_origin
)
45 struct fis_image_desc
*buf
;
46 struct mtd_partition
*parts
;
47 struct fis_list
*fl
= NULL
, *tmp_fl
;
56 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
57 static char nullstring
[] = "unallocated";
60 if ( directory
< 0 ) {
61 offset
= master
->size
+ directory
* master
->erasesize
;
62 while (master
->block_isbad
&&
63 master
->block_isbad(master
, offset
)) {
66 printk(KERN_NOTICE
"Failed to find a non-bad block to check for RedBoot partition table\n");
69 offset
-= master
->erasesize
;
72 offset
= directory
* master
->erasesize
;
73 while (master
->block_isbad
&&
74 master
->block_isbad(master
, offset
)) {
75 offset
+= master
->erasesize
;
76 if (offset
== master
->size
)
80 buf
= vmalloc(master
->erasesize
);
85 printk(KERN_NOTICE
"Searching for RedBoot partition table in %s at offset 0x%lx\n",
86 master
->name
, offset
);
88 ret
= master
->read(master
, offset
,
89 master
->erasesize
, &retlen
, (void *)buf
);
94 if (retlen
!= master
->erasesize
) {
99 numslots
= (master
->erasesize
/ sizeof(struct fis_image_desc
));
100 for (i
= 0; i
< numslots
; i
++) {
101 if (!memcmp(buf
[i
].name
, "FIS directory", 14)) {
102 /* This is apparently the FIS directory entry for the
103 * FIS directory itself. The FIS directory size is
104 * one erase block; if the buf[i].size field is
105 * swab32(erasesize) then we know we are looking at
106 * a byte swapped FIS directory - swap all the entries!
107 * (NOTE: this is 'size' not 'data_length'; size is
108 * the full size of the entry.)
111 /* RedBoot can combine the FIS directory and
112 config partitions into a single eraseblock;
113 we assume wrong-endian if either the swapped
114 'size' matches the eraseblock size precisely,
115 or if the swapped size actually fits in an
116 eraseblock while the unswapped size doesn't. */
117 if (swab32(buf
[i
].size
) == master
->erasesize
||
118 (buf
[i
].size
> master
->erasesize
119 && swab32(buf
[i
].size
) < master
->erasesize
)) {
121 /* Update numslots based on actual FIS directory size */
122 numslots
= swab32(buf
[i
].size
) / sizeof (struct fis_image_desc
);
123 for (j
= 0; j
< numslots
; ++j
) {
125 /* A single 0xff denotes a deleted entry.
126 * Two of them in a row is the end of the table.
128 if (buf
[j
].name
[0] == 0xff) {
129 if (buf
[j
].name
[1] == 0xff) {
136 /* The unsigned long fields were written with the
137 * wrong byte sex, name and pad have no byte sex.
139 swab32s(&buf
[j
].flash_base
);
140 swab32s(&buf
[j
].mem_base
);
141 swab32s(&buf
[j
].size
);
142 swab32s(&buf
[j
].entry_point
);
143 swab32s(&buf
[j
].data_length
);
144 swab32s(&buf
[j
].desc_cksum
);
145 swab32s(&buf
[j
].file_cksum
);
147 } else if (buf
[i
].size
< master
->erasesize
) {
148 /* Update numslots based on actual FIS directory size */
149 numslots
= buf
[i
].size
/ sizeof(struct fis_image_desc
);
156 printk(KERN_NOTICE
"No RedBoot partition table detected in %s\n",
162 for (i
= 0; i
< numslots
; i
++) {
163 struct fis_list
*new_fl
, **prev
;
165 if (buf
[i
].name
[0] == 0xff) {
166 if (buf
[i
].name
[1] == 0xff) {
172 if (!redboot_checksum(&buf
[i
]))
175 new_fl
= kmalloc(sizeof(struct fis_list
), GFP_KERNEL
);
176 namelen
+= strlen(buf
[i
].name
)+1;
181 new_fl
->img
= &buf
[i
];
183 buf
[i
].flash_base
-= fis_origin
;
185 buf
[i
].flash_base
&= master
->size
-1;
188 /* I'm sure the JFFS2 code has done me permanent damage.
189 * I now think the following is _normal_
192 while(*prev
&& (*prev
)->img
->flash_base
< new_fl
->img
->flash_base
)
193 prev
= &(*prev
)->next
;
194 new_fl
->next
= *prev
;
199 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
200 if (fl
->img
->flash_base
) {
202 nulllen
= sizeof(nullstring
);
205 for (tmp_fl
= fl
; tmp_fl
->next
; tmp_fl
= tmp_fl
->next
) {
206 if (tmp_fl
->img
->flash_base
+ tmp_fl
->img
->size
+ master
->erasesize
<= tmp_fl
->next
->img
->flash_base
) {
208 nulllen
= sizeof(nullstring
);
212 parts
= kzalloc(sizeof(*parts
)*nrparts
+ nulllen
+ namelen
, GFP_KERNEL
);
219 nullname
= (char *)&parts
[nrparts
];
220 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
222 strcpy(nullname
, nullstring
);
225 names
= nullname
+ nulllen
;
229 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
230 if (fl
->img
->flash_base
) {
231 parts
[0].name
= nullname
;
232 parts
[0].size
= fl
->img
->flash_base
;
237 for ( ; i
<nrparts
; i
++) {
238 parts
[i
].size
= fl
->img
->size
;
239 parts
[i
].offset
= fl
->img
->flash_base
;
240 parts
[i
].name
= names
;
242 strcpy(names
, fl
->img
->name
);
243 #ifdef CONFIG_MTD_REDBOOT_PARTS_READONLY
244 if (!memcmp(names
, "RedBoot", 8) ||
245 !memcmp(names
, "RedBoot config", 15) ||
246 !memcmp(names
, "FIS directory", 14)) {
247 parts
[i
].mask_flags
= MTD_WRITEABLE
;
250 names
+= strlen(names
)+1;
252 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
253 if(fl
->next
&& fl
->img
->flash_base
+ fl
->img
->size
+ master
->erasesize
<= fl
->next
->img
->flash_base
) {
255 parts
[i
].offset
= parts
[i
-1].size
+ parts
[i
-1].offset
;
256 parts
[i
].size
= fl
->next
->img
->flash_base
- parts
[i
].offset
;
257 parts
[i
].name
= nullname
;
268 struct fis_list
*old
= fl
;
276 static struct mtd_part_parser redboot_parser
= {
277 .owner
= THIS_MODULE
,
278 .parse_fn
= parse_redboot_partitions
,
282 static int __init
redboot_parser_init(void)
284 return register_mtd_parser(&redboot_parser
);
287 static void __exit
redboot_parser_exit(void)
289 deregister_mtd_parser(&redboot_parser
);
292 module_init(redboot_parser_init
);
293 module_exit(redboot_parser_exit
);
295 MODULE_LICENSE("GPL");
296 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
297 MODULE_DESCRIPTION("Parsing code for RedBoot Flash Image System (FIS) tables");