blob: 56e48ea7ff0530bdac72324596006bb730a7363f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Parse RedBoot-style Flash Image System (FIS) tables and
3 * produce a Linux partition array to match.
David Woodhousea1452a32010-08-08 20:58:20 +01004 *
5 * Copyright © 2001 Red Hat UK Limited
6 * Copyright © 2001-2010 David Woodhouse <dwmw2@infradead.org>
7 *
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.
12 *
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.
17 *
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
21 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
24#include <linux/kernel.h>
25#include <linux/slab.h>
26#include <linux/init.h>
27#include <linux/vmalloc.h>
28
29#include <linux/mtd/mtd.h>
30#include <linux/mtd/partitions.h>
31
32struct fis_image_desc {
33 unsigned char name[16]; // Null terminated name
Ben Dooksb020bb72006-05-16 17:31:15 +010034 uint32_t flash_base; // Address within FLASH of image
35 uint32_t mem_base; // Address in memory where it executes
36 uint32_t size; // Length of image
37 uint32_t entry_point; // Execution entry point
38 uint32_t data_length; // Length of actual data
39 unsigned char _pad[256-(16+7*sizeof(uint32_t))];
40 uint32_t desc_cksum; // Checksum over image descriptor
41 uint32_t file_cksum; // Checksum over image data
Linus Torvalds1da177e2005-04-16 15:20:36 -070042};
43
44struct fis_list {
45 struct fis_image_desc *img;
46 struct fis_list *next;
47};
48
49static int directory = CONFIG_MTD_REDBOOT_DIRECTORY_BLOCK;
50module_param(directory, int, 0);
51
52static inline int redboot_checksum(struct fis_image_desc *img)
53{
54 /* RedBoot doesn't actually write the desc_cksum field yet AFAICT */
55 return 1;
56}
57
Thomas Gleixner97894cd2005-11-07 11:15:26 +000058static int parse_redboot_partitions(struct mtd_info *master,
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +040059 struct mtd_partition **pparts,
60 struct mtd_part_parser_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 int nrparts = 0;
63 struct fis_image_desc *buf;
64 struct mtd_partition *parts;
65 struct fis_list *fl = NULL, *tmp_fl;
66 int ret, i;
67 size_t retlen;
68 char *names;
69 char *nullname;
70 int namelen = 0;
71 int nulllen = 0;
72 int numslots;
73 unsigned long offset;
74#ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
75 static char nullstring[] = "unallocated";
76#endif
77
David Woodhouse3c441ba2007-10-28 21:57:02 -040078 if ( directory < 0 ) {
79 offset = master->size + directory * master->erasesize;
80 while (master->block_isbad &&
81 master->block_isbad(master, offset)) {
82 if (!offset) {
83 nogood:
84 printk(KERN_NOTICE "Failed to find a non-bad block to check for RedBoot partition table\n");
85 return -EIO;
86 }
87 offset -= master->erasesize;
88 }
89 } else {
90 offset = directory * master->erasesize;
91 while (master->block_isbad &&
92 master->block_isbad(master, offset)) {
93 offset += master->erasesize;
94 if (offset == master->size)
95 goto nogood;
96 }
97 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 buf = vmalloc(master->erasesize);
99
100 if (!buf)
101 return -ENOMEM;
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 printk(KERN_NOTICE "Searching for RedBoot partition table in %s at offset 0x%lx\n",
104 master->name, offset);
105
106 ret = master->read(master, offset,
107 master->erasesize, &retlen, (void *)buf);
108
109 if (ret)
110 goto out;
111
112 if (retlen != master->erasesize) {
113 ret = -EIO;
114 goto out;
115 }
116
117 numslots = (master->erasesize / sizeof(struct fis_image_desc));
118 for (i = 0; i < numslots; i++) {
John Bowler9cff3372006-02-28 16:59:08 -0800119 if (!memcmp(buf[i].name, "FIS directory", 14)) {
120 /* This is apparently the FIS directory entry for the
121 * FIS directory itself. The FIS directory size is
John Bowler77a33132006-03-02 02:54:29 -0800122 * one erase block; if the buf[i].size field is
John Bowler9cff3372006-02-28 16:59:08 -0800123 * swab32(erasesize) then we know we are looking at
124 * a byte swapped FIS directory - swap all the entries!
John Bowler77a33132006-03-02 02:54:29 -0800125 * (NOTE: this is 'size' not 'data_length'; size is
John Bowler9cff3372006-02-28 16:59:08 -0800126 * the full size of the entry.)
127 */
David Woodhouse7ca353a2007-02-10 09:58:31 +0000128
129 /* RedBoot can combine the FIS directory and
130 config partitions into a single eraseblock;
131 we assume wrong-endian if either the swapped
132 'size' matches the eraseblock size precisely,
133 or if the swapped size actually fits in an
134 eraseblock while the unswapped size doesn't. */
135 if (swab32(buf[i].size) == master->erasesize ||
136 (buf[i].size > master->erasesize
137 && swab32(buf[i].size) < master->erasesize)) {
John Bowler9cff3372006-02-28 16:59:08 -0800138 int j;
Rod Whitby11192142007-02-10 09:26:48 +0000139 /* Update numslots based on actual FIS directory size */
140 numslots = swab32(buf[i].size) / sizeof (struct fis_image_desc);
Rod Whitbyf33665d2006-12-06 12:11:15 +1030141 for (j = 0; j < numslots; ++j) {
142
143 /* A single 0xff denotes a deleted entry.
144 * Two of them in a row is the end of the table.
145 */
146 if (buf[j].name[0] == 0xff) {
147 if (buf[j].name[1] == 0xff) {
148 break;
149 } else {
150 continue;
151 }
152 }
153
John Bowler9cff3372006-02-28 16:59:08 -0800154 /* The unsigned long fields were written with the
155 * wrong byte sex, name and pad have no byte sex.
156 */
John Bowler77a33132006-03-02 02:54:29 -0800157 swab32s(&buf[j].flash_base);
158 swab32s(&buf[j].mem_base);
159 swab32s(&buf[j].size);
160 swab32s(&buf[j].entry_point);
161 swab32s(&buf[j].data_length);
162 swab32s(&buf[j].desc_cksum);
163 swab32s(&buf[j].file_cksum);
John Bowler9cff3372006-02-28 16:59:08 -0800164 }
David Woodhouse7ca353a2007-02-10 09:58:31 +0000165 } else if (buf[i].size < master->erasesize) {
Rod Whitby11192142007-02-10 09:26:48 +0000166 /* Update numslots based on actual FIS directory size */
167 numslots = buf[i].size / sizeof(struct fis_image_desc);
John Bowler9cff3372006-02-28 16:59:08 -0800168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 break;
John Bowler9cff3372006-02-28 16:59:08 -0800170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172 if (i == numslots) {
173 /* Didn't find it */
174 printk(KERN_NOTICE "No RedBoot partition table detected in %s\n",
175 master->name);
176 ret = 0;
177 goto out;
178 }
179
180 for (i = 0; i < numslots; i++) {
181 struct fis_list *new_fl, **prev;
182
Rod Whitbyf33665d2006-12-06 12:11:15 +1030183 if (buf[i].name[0] == 0xff) {
184 if (buf[i].name[1] == 0xff) {
185 break;
186 } else {
187 continue;
188 }
189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (!redboot_checksum(&buf[i]))
191 break;
192
193 new_fl = kmalloc(sizeof(struct fis_list), GFP_KERNEL);
194 namelen += strlen(buf[i].name)+1;
195 if (!new_fl) {
196 ret = -ENOMEM;
197 goto out;
198 }
199 new_fl->img = &buf[i];
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400200 if (data && data->origin)
201 buf[i].flash_base -= data->origin;
202 else
203 buf[i].flash_base &= master->size-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 /* I'm sure the JFFS2 code has done me permanent damage.
206 * I now think the following is _normal_
207 */
208 prev = &fl;
209 while(*prev && (*prev)->img->flash_base < new_fl->img->flash_base)
210 prev = &(*prev)->next;
211 new_fl->next = *prev;
212 *prev = new_fl;
213
214 nrparts++;
215 }
216#ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
217 if (fl->img->flash_base) {
218 nrparts++;
219 nulllen = sizeof(nullstring);
220 }
221
222 for (tmp_fl = fl; tmp_fl->next; tmp_fl = tmp_fl->next) {
223 if (tmp_fl->img->flash_base + tmp_fl->img->size + master->erasesize <= tmp_fl->next->img->flash_base) {
224 nrparts++;
225 nulllen = sizeof(nullstring);
226 }
227 }
228#endif
Burman Yan95b93a02006-11-15 21:10:29 +0200229 parts = kzalloc(sizeof(*parts)*nrparts + nulllen + namelen, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 if (!parts) {
232 ret = -ENOMEM;
233 goto out;
234 }
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 nullname = (char *)&parts[nrparts];
237#ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
238 if (nulllen > 0) {
239 strcpy(nullname, nullstring);
240 }
241#endif
242 names = nullname + nulllen;
243
244 i=0;
245
246#ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
247 if (fl->img->flash_base) {
248 parts[0].name = nullname;
249 parts[0].size = fl->img->flash_base;
250 parts[0].offset = 0;
251 i++;
252 }
253#endif
254 for ( ; i<nrparts; i++) {
255 parts[i].size = fl->img->size;
256 parts[i].offset = fl->img->flash_base;
257 parts[i].name = names;
258
259 strcpy(names, fl->img->name);
260#ifdef CONFIG_MTD_REDBOOT_PARTS_READONLY
261 if (!memcmp(names, "RedBoot", 8) ||
262 !memcmp(names, "RedBoot config", 15) ||
263 !memcmp(names, "FIS directory", 14)) {
264 parts[i].mask_flags = MTD_WRITEABLE;
265 }
266#endif
267 names += strlen(names)+1;
268
269#ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
270 if(fl->next && fl->img->flash_base + fl->img->size + master->erasesize <= fl->next->img->flash_base) {
271 i++;
272 parts[i].offset = parts[i-1].size + parts[i-1].offset;
273 parts[i].size = fl->next->img->flash_base - parts[i].offset;
274 parts[i].name = nullname;
275 }
276#endif
277 tmp_fl = fl;
278 fl = fl->next;
279 kfree(tmp_fl);
280 }
281 ret = nrparts;
282 *pparts = parts;
283 out:
284 while (fl) {
285 struct fis_list *old = fl;
286 fl = fl->next;
287 kfree(old);
288 }
289 vfree(buf);
290 return ret;
291}
292
293static struct mtd_part_parser redboot_parser = {
294 .owner = THIS_MODULE,
295 .parse_fn = parse_redboot_partitions,
296 .name = "RedBoot",
297};
298
299static int __init redboot_parser_init(void)
300{
301 return register_mtd_parser(&redboot_parser);
302}
303
304static void __exit redboot_parser_exit(void)
305{
306 deregister_mtd_parser(&redboot_parser);
307}
308
309module_init(redboot_parser_init);
310module_exit(redboot_parser_exit);
311
312MODULE_LICENSE("GPL");
David Woodhouse44d1b982008-06-05 22:46:18 -0700313MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314MODULE_DESCRIPTION("Parsing code for RedBoot Flash Image System (FIS) tables");