2 * Parse RedBoot-style Flash Image System (FIS) tables and
3 * produce a Linux partition array to match.
5 * Copyright © 2001 Red Hat UK Limited
6 * Copyright © 2001-2010 David Woodhouse <dwmw2@infradead.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/init.h>
27 #include <linux/vmalloc.h>
29 #include <linux/mtd/mtd.h>
30 #include <linux/mtd/partitions.h>
31 #include <linux/module.h>
33 struct fis_image_desc
{
34 unsigned char name
[16]; // Null terminated name
35 uint32_t flash_base
; // Address within FLASH of image
36 uint32_t mem_base
; // Address in memory where it executes
37 uint32_t size
; // Length of image
38 uint32_t entry_point
; // Execution entry point
39 uint32_t data_length
; // Length of actual data
40 unsigned char _pad
[256-(16+7*sizeof(uint32_t))];
41 uint32_t desc_cksum
; // Checksum over image descriptor
42 uint32_t file_cksum
; // Checksum over image data
46 struct fis_image_desc
*img
;
47 struct fis_list
*next
;
50 static int directory
= CONFIG_MTD_REDBOOT_DIRECTORY_BLOCK
;
51 module_param(directory
, int, 0);
53 static inline int redboot_checksum(struct fis_image_desc
*img
)
55 /* RedBoot doesn't actually write the desc_cksum field yet AFAICT */
59 static int parse_redboot_partitions(struct mtd_info
*master
,
60 struct mtd_partition
**pparts
,
61 unsigned long fis_origin
)
64 struct fis_image_desc
*buf
;
65 struct mtd_partition
*parts
;
66 struct fis_list
*fl
= NULL
, *tmp_fl
;
75 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
76 static char nullstring
[] = "unallocated";
79 if ( directory
< 0 ) {
80 offset
= master
->size
+ directory
* master
->erasesize
;
81 while (master
->block_isbad
&&
82 master
->block_isbad(master
, offset
)) {
85 printk(KERN_NOTICE
"Failed to find a non-bad block to check for RedBoot partition table\n");
88 offset
-= master
->erasesize
;
91 offset
= directory
* master
->erasesize
;
92 while (master
->block_isbad
&&
93 master
->block_isbad(master
, offset
)) {
94 offset
+= master
->erasesize
;
95 if (offset
== master
->size
)
99 buf
= vmalloc(master
->erasesize
);
104 printk(KERN_NOTICE
"Searching for RedBoot partition table in %s at offset 0x%lx\n",
105 master
->name
, offset
);
107 ret
= master
->read(master
, offset
,
108 master
->erasesize
, &retlen
, (void *)buf
);
113 if (retlen
!= master
->erasesize
) {
118 numslots
= (master
->erasesize
/ sizeof(struct fis_image_desc
));
119 for (i
= 0; i
< numslots
; i
++) {
120 if (!memcmp(buf
[i
].name
, "FIS directory", 14)) {
121 /* This is apparently the FIS directory entry for the
122 * FIS directory itself. The FIS directory size is
123 * one erase block; if the buf[i].size field is
124 * swab32(erasesize) then we know we are looking at
125 * a byte swapped FIS directory - swap all the entries!
126 * (NOTE: this is 'size' not 'data_length'; size is
127 * the full size of the entry.)
130 /* RedBoot can combine the FIS directory and
131 config partitions into a single eraseblock;
132 we assume wrong-endian if either the swapped
133 'size' matches the eraseblock size precisely,
134 or if the swapped size actually fits in an
135 eraseblock while the unswapped size doesn't. */
136 if (swab32(buf
[i
].size
) == master
->erasesize
||
137 (buf
[i
].size
> master
->erasesize
138 && swab32(buf
[i
].size
) < master
->erasesize
)) {
140 /* Update numslots based on actual FIS directory size */
141 numslots
= swab32(buf
[i
].size
) / sizeof (struct fis_image_desc
);
142 for (j
= 0; j
< numslots
; ++j
) {
144 /* A single 0xff denotes a deleted entry.
145 * Two of them in a row is the end of the table.
147 if (buf
[j
].name
[0] == 0xff) {
148 if (buf
[j
].name
[1] == 0xff) {
155 /* The unsigned long fields were written with the
156 * wrong byte sex, name and pad have no byte sex.
158 swab32s(&buf
[j
].flash_base
);
159 swab32s(&buf
[j
].mem_base
);
160 swab32s(&buf
[j
].size
);
161 swab32s(&buf
[j
].entry_point
);
162 swab32s(&buf
[j
].data_length
);
163 swab32s(&buf
[j
].desc_cksum
);
164 swab32s(&buf
[j
].file_cksum
);
166 } else if (buf
[i
].size
< master
->erasesize
) {
167 /* Update numslots based on actual FIS directory size */
168 numslots
= buf
[i
].size
/ sizeof(struct fis_image_desc
);
175 printk(KERN_NOTICE
"No RedBoot partition table detected in %s\n",
181 for (i
= 0; i
< numslots
; i
++) {
182 struct fis_list
*new_fl
, **prev
;
184 if (buf
[i
].name
[0] == 0xff) {
185 if (buf
[i
].name
[1] == 0xff) {
191 if (!redboot_checksum(&buf
[i
]))
194 new_fl
= kmalloc(sizeof(struct fis_list
), GFP_KERNEL
);
195 namelen
+= strlen(buf
[i
].name
)+1;
200 new_fl
->img
= &buf
[i
];
202 buf
[i
].flash_base
-= fis_origin
;
204 buf
[i
].flash_base
&= master
->size
-1;
207 /* I'm sure the JFFS2 code has done me permanent damage.
208 * I now think the following is _normal_
211 while(*prev
&& (*prev
)->img
->flash_base
< new_fl
->img
->flash_base
)
212 prev
= &(*prev
)->next
;
213 new_fl
->next
= *prev
;
218 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
219 if (fl
->img
->flash_base
) {
221 nulllen
= sizeof(nullstring
);
224 for (tmp_fl
= fl
; tmp_fl
->next
; tmp_fl
= tmp_fl
->next
) {
225 if (tmp_fl
->img
->flash_base
+ tmp_fl
->img
->size
+ master
->erasesize
<= tmp_fl
->next
->img
->flash_base
) {
227 nulllen
= sizeof(nullstring
);
231 parts
= kzalloc(sizeof(*parts
)*nrparts
+ nulllen
+ namelen
, GFP_KERNEL
);
238 nullname
= (char *)&parts
[nrparts
];
239 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
241 strcpy(nullname
, nullstring
);
244 names
= nullname
+ nulllen
;
248 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
249 if (fl
->img
->flash_base
) {
250 parts
[0].name
= nullname
;
251 parts
[0].size
= fl
->img
->flash_base
;
256 for ( ; i
<nrparts
; i
++) {
257 parts
[i
].size
= fl
->img
->size
;
258 parts
[i
].offset
= fl
->img
->flash_base
;
259 parts
[i
].name
= names
;
261 strcpy(names
, fl
->img
->name
);
262 #ifdef CONFIG_MTD_REDBOOT_PARTS_READONLY
263 if (!memcmp(names
, "RedBoot", 8) ||
264 !memcmp(names
, "RedBoot config", 15) ||
265 !memcmp(names
, "FIS directory", 14)) {
266 parts
[i
].mask_flags
= MTD_WRITEABLE
;
269 names
+= strlen(names
)+1;
271 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
272 if(fl
->next
&& fl
->img
->flash_base
+ fl
->img
->size
+ master
->erasesize
<= fl
->next
->img
->flash_base
) {
274 parts
[i
].offset
= parts
[i
-1].size
+ parts
[i
-1].offset
;
275 parts
[i
].size
= fl
->next
->img
->flash_base
- parts
[i
].offset
;
276 parts
[i
].name
= nullname
;
287 struct fis_list
*old
= fl
;
295 static struct mtd_part_parser redboot_parser
= {
296 .owner
= THIS_MODULE
,
297 .parse_fn
= parse_redboot_partitions
,
301 static int __init
redboot_parser_init(void)
303 return register_mtd_parser(&redboot_parser
);
306 static void __exit
redboot_parser_exit(void)
308 deregister_mtd_parser(&redboot_parser
);
311 module_init(redboot_parser_init
);
312 module_exit(redboot_parser_exit
);
314 MODULE_LICENSE("GPL");
315 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
316 MODULE_DESCRIPTION("Parsing code for RedBoot Flash Image System (FIS) tables");