Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Parse RedBoot-style Flash Image System (FIS) tables and |
| 3 | * produce a Linux partition array to match. |
David Woodhouse | a1452a3 | 2010-08-08 20:58:20 +0100 | [diff] [blame] | 4 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | */ |
| 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> |
Paul Gortmaker | a0e5cc5 | 2011-07-03 15:17:31 -0400 | [diff] [blame] | 31 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | struct fis_image_desc { |
| 34 | unsigned char name[16]; // Null terminated name |
Ben Dooks | b020bb7 | 2006-05-16 17:31:15 +0100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | struct fis_list { |
| 46 | struct fis_image_desc *img; |
| 47 | struct fis_list *next; |
| 48 | }; |
| 49 | |
| 50 | static int directory = CONFIG_MTD_REDBOOT_DIRECTORY_BLOCK; |
| 51 | module_param(directory, int, 0); |
| 52 | |
| 53 | static inline int redboot_checksum(struct fis_image_desc *img) |
| 54 | { |
| 55 | /* RedBoot doesn't actually write the desc_cksum field yet AFAICT */ |
| 56 | return 1; |
| 57 | } |
| 58 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 59 | static int parse_redboot_partitions(struct mtd_info *master, |
Dmitry Eremin-Solenikov | c797533 | 2011-06-10 18:18:28 +0400 | [diff] [blame] | 60 | struct mtd_partition **pparts, |
| 61 | struct mtd_part_parser_data *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | { |
| 63 | int nrparts = 0; |
| 64 | struct fis_image_desc *buf; |
| 65 | struct mtd_partition *parts; |
| 66 | struct fis_list *fl = NULL, *tmp_fl; |
| 67 | int ret, i; |
| 68 | size_t retlen; |
| 69 | char *names; |
| 70 | char *nullname; |
| 71 | int namelen = 0; |
| 72 | int nulllen = 0; |
| 73 | int numslots; |
| 74 | unsigned long offset; |
| 75 | #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED |
| 76 | static char nullstring[] = "unallocated"; |
| 77 | #endif |
| 78 | |
David Woodhouse | 3c441ba | 2007-10-28 21:57:02 -0400 | [diff] [blame] | 79 | if ( directory < 0 ) { |
| 80 | offset = master->size + directory * master->erasesize; |
| 81 | while (master->block_isbad && |
| 82 | master->block_isbad(master, offset)) { |
| 83 | if (!offset) { |
| 84 | nogood: |
| 85 | printk(KERN_NOTICE "Failed to find a non-bad block to check for RedBoot partition table\n"); |
| 86 | return -EIO; |
| 87 | } |
| 88 | offset -= master->erasesize; |
| 89 | } |
| 90 | } else { |
| 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) |
| 96 | goto nogood; |
| 97 | } |
| 98 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | buf = vmalloc(master->erasesize); |
| 100 | |
| 101 | if (!buf) |
| 102 | return -ENOMEM; |
| 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | printk(KERN_NOTICE "Searching for RedBoot partition table in %s at offset 0x%lx\n", |
| 105 | master->name, offset); |
| 106 | |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame^] | 107 | ret = mtd_read(master, offset, master->erasesize, &retlen, |
| 108 | (void *)buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | if (ret) |
| 111 | goto out; |
| 112 | |
| 113 | if (retlen != master->erasesize) { |
| 114 | ret = -EIO; |
| 115 | goto out; |
| 116 | } |
| 117 | |
| 118 | numslots = (master->erasesize / sizeof(struct fis_image_desc)); |
| 119 | for (i = 0; i < numslots; i++) { |
John Bowler | 9cff337 | 2006-02-28 16:59:08 -0800 | [diff] [blame] | 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 |
John Bowler | 77a3313 | 2006-03-02 02:54:29 -0800 | [diff] [blame] | 123 | * one erase block; if the buf[i].size field is |
John Bowler | 9cff337 | 2006-02-28 16:59:08 -0800 | [diff] [blame] | 124 | * swab32(erasesize) then we know we are looking at |
| 125 | * a byte swapped FIS directory - swap all the entries! |
John Bowler | 77a3313 | 2006-03-02 02:54:29 -0800 | [diff] [blame] | 126 | * (NOTE: this is 'size' not 'data_length'; size is |
John Bowler | 9cff337 | 2006-02-28 16:59:08 -0800 | [diff] [blame] | 127 | * the full size of the entry.) |
| 128 | */ |
David Woodhouse | 7ca353a | 2007-02-10 09:58:31 +0000 | [diff] [blame] | 129 | |
| 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)) { |
John Bowler | 9cff337 | 2006-02-28 16:59:08 -0800 | [diff] [blame] | 139 | int j; |
Rod Whitby | 1119214 | 2007-02-10 09:26:48 +0000 | [diff] [blame] | 140 | /* Update numslots based on actual FIS directory size */ |
| 141 | numslots = swab32(buf[i].size) / sizeof (struct fis_image_desc); |
Rod Whitby | f33665d | 2006-12-06 12:11:15 +1030 | [diff] [blame] | 142 | for (j = 0; j < numslots; ++j) { |
| 143 | |
| 144 | /* A single 0xff denotes a deleted entry. |
| 145 | * Two of them in a row is the end of the table. |
| 146 | */ |
| 147 | if (buf[j].name[0] == 0xff) { |
| 148 | if (buf[j].name[1] == 0xff) { |
| 149 | break; |
| 150 | } else { |
| 151 | continue; |
| 152 | } |
| 153 | } |
| 154 | |
John Bowler | 9cff337 | 2006-02-28 16:59:08 -0800 | [diff] [blame] | 155 | /* The unsigned long fields were written with the |
| 156 | * wrong byte sex, name and pad have no byte sex. |
| 157 | */ |
John Bowler | 77a3313 | 2006-03-02 02:54:29 -0800 | [diff] [blame] | 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); |
John Bowler | 9cff337 | 2006-02-28 16:59:08 -0800 | [diff] [blame] | 165 | } |
David Woodhouse | 7ca353a | 2007-02-10 09:58:31 +0000 | [diff] [blame] | 166 | } else if (buf[i].size < master->erasesize) { |
Rod Whitby | 1119214 | 2007-02-10 09:26:48 +0000 | [diff] [blame] | 167 | /* Update numslots based on actual FIS directory size */ |
| 168 | numslots = buf[i].size / sizeof(struct fis_image_desc); |
John Bowler | 9cff337 | 2006-02-28 16:59:08 -0800 | [diff] [blame] | 169 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | break; |
John Bowler | 9cff337 | 2006-02-28 16:59:08 -0800 | [diff] [blame] | 171 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
| 173 | if (i == numslots) { |
| 174 | /* Didn't find it */ |
| 175 | printk(KERN_NOTICE "No RedBoot partition table detected in %s\n", |
| 176 | master->name); |
| 177 | ret = 0; |
| 178 | goto out; |
| 179 | } |
| 180 | |
| 181 | for (i = 0; i < numslots; i++) { |
| 182 | struct fis_list *new_fl, **prev; |
| 183 | |
Rod Whitby | f33665d | 2006-12-06 12:11:15 +1030 | [diff] [blame] | 184 | if (buf[i].name[0] == 0xff) { |
| 185 | if (buf[i].name[1] == 0xff) { |
| 186 | break; |
| 187 | } else { |
| 188 | continue; |
| 189 | } |
| 190 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | if (!redboot_checksum(&buf[i])) |
| 192 | break; |
| 193 | |
| 194 | new_fl = kmalloc(sizeof(struct fis_list), GFP_KERNEL); |
| 195 | namelen += strlen(buf[i].name)+1; |
| 196 | if (!new_fl) { |
| 197 | ret = -ENOMEM; |
| 198 | goto out; |
| 199 | } |
| 200 | new_fl->img = &buf[i]; |
Dmitry Eremin-Solenikov | c797533 | 2011-06-10 18:18:28 +0400 | [diff] [blame] | 201 | if (data && data->origin) |
| 202 | buf[i].flash_base -= data->origin; |
| 203 | else |
| 204 | buf[i].flash_base &= master->size-1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
| 206 | /* I'm sure the JFFS2 code has done me permanent damage. |
| 207 | * I now think the following is _normal_ |
| 208 | */ |
| 209 | prev = &fl; |
| 210 | while(*prev && (*prev)->img->flash_base < new_fl->img->flash_base) |
| 211 | prev = &(*prev)->next; |
| 212 | new_fl->next = *prev; |
| 213 | *prev = new_fl; |
| 214 | |
| 215 | nrparts++; |
| 216 | } |
| 217 | #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED |
| 218 | if (fl->img->flash_base) { |
| 219 | nrparts++; |
| 220 | nulllen = sizeof(nullstring); |
| 221 | } |
| 222 | |
| 223 | for (tmp_fl = fl; tmp_fl->next; tmp_fl = tmp_fl->next) { |
| 224 | if (tmp_fl->img->flash_base + tmp_fl->img->size + master->erasesize <= tmp_fl->next->img->flash_base) { |
| 225 | nrparts++; |
| 226 | nulllen = sizeof(nullstring); |
| 227 | } |
| 228 | } |
| 229 | #endif |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 230 | parts = kzalloc(sizeof(*parts)*nrparts + nulllen + namelen, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
| 232 | if (!parts) { |
| 233 | ret = -ENOMEM; |
| 234 | goto out; |
| 235 | } |
| 236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | nullname = (char *)&parts[nrparts]; |
| 238 | #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED |
| 239 | if (nulllen > 0) { |
| 240 | strcpy(nullname, nullstring); |
| 241 | } |
| 242 | #endif |
| 243 | names = nullname + nulllen; |
| 244 | |
| 245 | i=0; |
| 246 | |
| 247 | #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED |
| 248 | if (fl->img->flash_base) { |
| 249 | parts[0].name = nullname; |
| 250 | parts[0].size = fl->img->flash_base; |
| 251 | parts[0].offset = 0; |
| 252 | i++; |
| 253 | } |
| 254 | #endif |
| 255 | for ( ; i<nrparts; i++) { |
| 256 | parts[i].size = fl->img->size; |
| 257 | parts[i].offset = fl->img->flash_base; |
| 258 | parts[i].name = names; |
| 259 | |
| 260 | strcpy(names, fl->img->name); |
| 261 | #ifdef CONFIG_MTD_REDBOOT_PARTS_READONLY |
| 262 | if (!memcmp(names, "RedBoot", 8) || |
| 263 | !memcmp(names, "RedBoot config", 15) || |
| 264 | !memcmp(names, "FIS directory", 14)) { |
| 265 | parts[i].mask_flags = MTD_WRITEABLE; |
| 266 | } |
| 267 | #endif |
| 268 | names += strlen(names)+1; |
| 269 | |
| 270 | #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED |
| 271 | if(fl->next && fl->img->flash_base + fl->img->size + master->erasesize <= fl->next->img->flash_base) { |
| 272 | i++; |
| 273 | parts[i].offset = parts[i-1].size + parts[i-1].offset; |
| 274 | parts[i].size = fl->next->img->flash_base - parts[i].offset; |
| 275 | parts[i].name = nullname; |
| 276 | } |
| 277 | #endif |
| 278 | tmp_fl = fl; |
| 279 | fl = fl->next; |
| 280 | kfree(tmp_fl); |
| 281 | } |
| 282 | ret = nrparts; |
| 283 | *pparts = parts; |
| 284 | out: |
| 285 | while (fl) { |
| 286 | struct fis_list *old = fl; |
| 287 | fl = fl->next; |
| 288 | kfree(old); |
| 289 | } |
| 290 | vfree(buf); |
| 291 | return ret; |
| 292 | } |
| 293 | |
| 294 | static struct mtd_part_parser redboot_parser = { |
| 295 | .owner = THIS_MODULE, |
| 296 | .parse_fn = parse_redboot_partitions, |
| 297 | .name = "RedBoot", |
| 298 | }; |
| 299 | |
Andres Salomon | d5de190 | 2011-10-14 07:33:20 -0700 | [diff] [blame] | 300 | /* mtd parsers will request the module by parser name */ |
| 301 | MODULE_ALIAS("RedBoot"); |
| 302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | static int __init redboot_parser_init(void) |
| 304 | { |
| 305 | return register_mtd_parser(&redboot_parser); |
| 306 | } |
| 307 | |
| 308 | static void __exit redboot_parser_exit(void) |
| 309 | { |
| 310 | deregister_mtd_parser(&redboot_parser); |
| 311 | } |
| 312 | |
| 313 | module_init(redboot_parser_init); |
| 314 | module_exit(redboot_parser_exit); |
| 315 | |
| 316 | MODULE_LICENSE("GPL"); |
David Woodhouse | 44d1b98 | 2008-06-05 22:46:18 -0700 | [diff] [blame] | 317 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | MODULE_DESCRIPTION("Parsing code for RedBoot Flash Image System (FIS) tables"); |