Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Ben Dooks | b020bb7 | 2006-05-16 17:31:15 +0100 | [diff] [blame] | 2 | * $Id: redboot.c,v 1.21 2006/03/30 18:34:37 bjd Exp $ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Parse RedBoot-style Flash Image System (FIS) tables and |
| 5 | * produce a Linux partition array to match. |
| 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/slab.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/vmalloc.h> |
| 12 | |
| 13 | #include <linux/mtd/mtd.h> |
| 14 | #include <linux/mtd/partitions.h> |
| 15 | |
| 16 | struct fis_image_desc { |
| 17 | unsigned char name[16]; // Null terminated name |
Ben Dooks | b020bb7 | 2006-05-16 17:31:15 +0100 | [diff] [blame] | 18 | uint32_t flash_base; // Address within FLASH of image |
| 19 | uint32_t mem_base; // Address in memory where it executes |
| 20 | uint32_t size; // Length of image |
| 21 | uint32_t entry_point; // Execution entry point |
| 22 | uint32_t data_length; // Length of actual data |
| 23 | unsigned char _pad[256-(16+7*sizeof(uint32_t))]; |
| 24 | uint32_t desc_cksum; // Checksum over image descriptor |
| 25 | uint32_t file_cksum; // Checksum over image data |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | struct fis_list { |
| 29 | struct fis_image_desc *img; |
| 30 | struct fis_list *next; |
| 31 | }; |
| 32 | |
| 33 | static int directory = CONFIG_MTD_REDBOOT_DIRECTORY_BLOCK; |
| 34 | module_param(directory, int, 0); |
| 35 | |
| 36 | static inline int redboot_checksum(struct fis_image_desc *img) |
| 37 | { |
| 38 | /* RedBoot doesn't actually write the desc_cksum field yet AFAICT */ |
| 39 | return 1; |
| 40 | } |
| 41 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 42 | static int parse_redboot_partitions(struct mtd_info *master, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | struct mtd_partition **pparts, |
| 44 | unsigned long fis_origin) |
| 45 | { |
| 46 | int nrparts = 0; |
| 47 | struct fis_image_desc *buf; |
| 48 | struct mtd_partition *parts; |
| 49 | struct fis_list *fl = NULL, *tmp_fl; |
| 50 | int ret, i; |
| 51 | size_t retlen; |
| 52 | char *names; |
| 53 | char *nullname; |
| 54 | int namelen = 0; |
| 55 | int nulllen = 0; |
| 56 | int numslots; |
| 57 | unsigned long offset; |
| 58 | #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED |
| 59 | static char nullstring[] = "unallocated"; |
| 60 | #endif |
| 61 | |
| 62 | buf = vmalloc(master->erasesize); |
| 63 | |
| 64 | if (!buf) |
| 65 | return -ENOMEM; |
| 66 | |
| 67 | if ( directory < 0 ) |
| 68 | offset = master->size + directory*master->erasesize; |
| 69 | else |
| 70 | offset = directory*master->erasesize; |
| 71 | |
| 72 | printk(KERN_NOTICE "Searching for RedBoot partition table in %s at offset 0x%lx\n", |
| 73 | master->name, offset); |
| 74 | |
| 75 | ret = master->read(master, offset, |
| 76 | master->erasesize, &retlen, (void *)buf); |
| 77 | |
| 78 | if (ret) |
| 79 | goto out; |
| 80 | |
| 81 | if (retlen != master->erasesize) { |
| 82 | ret = -EIO; |
| 83 | goto out; |
| 84 | } |
| 85 | |
| 86 | numslots = (master->erasesize / sizeof(struct fis_image_desc)); |
| 87 | for (i = 0; i < numslots; i++) { |
John Bowler | 9cff337 | 2006-02-28 16:59:08 -0800 | [diff] [blame] | 88 | if (!memcmp(buf[i].name, "FIS directory", 14)) { |
| 89 | /* This is apparently the FIS directory entry for the |
| 90 | * FIS directory itself. The FIS directory size is |
John Bowler | 77a3313 | 2006-03-02 02:54:29 -0800 | [diff] [blame] | 91 | * one erase block; if the buf[i].size field is |
John Bowler | 9cff337 | 2006-02-28 16:59:08 -0800 | [diff] [blame] | 92 | * swab32(erasesize) then we know we are looking at |
| 93 | * a byte swapped FIS directory - swap all the entries! |
John Bowler | 77a3313 | 2006-03-02 02:54:29 -0800 | [diff] [blame] | 94 | * (NOTE: this is 'size' not 'data_length'; size is |
John Bowler | 9cff337 | 2006-02-28 16:59:08 -0800 | [diff] [blame] | 95 | * the full size of the entry.) |
| 96 | */ |
| 97 | if (swab32(buf[i].size) == master->erasesize) { |
| 98 | int j; |
Rod Whitby | 1119214 | 2007-02-10 09:26:48 +0000 | [diff] [blame^] | 99 | /* Update numslots based on actual FIS directory size */ |
| 100 | numslots = swab32(buf[i].size) / sizeof (struct fis_image_desc); |
Rod Whitby | f33665d | 2006-12-06 12:11:15 +1030 | [diff] [blame] | 101 | for (j = 0; j < numslots; ++j) { |
| 102 | |
| 103 | /* A single 0xff denotes a deleted entry. |
| 104 | * Two of them in a row is the end of the table. |
| 105 | */ |
| 106 | if (buf[j].name[0] == 0xff) { |
| 107 | if (buf[j].name[1] == 0xff) { |
| 108 | break; |
| 109 | } else { |
| 110 | continue; |
| 111 | } |
| 112 | } |
| 113 | |
John Bowler | 9cff337 | 2006-02-28 16:59:08 -0800 | [diff] [blame] | 114 | /* The unsigned long fields were written with the |
| 115 | * wrong byte sex, name and pad have no byte sex. |
| 116 | */ |
John Bowler | 77a3313 | 2006-03-02 02:54:29 -0800 | [diff] [blame] | 117 | swab32s(&buf[j].flash_base); |
| 118 | swab32s(&buf[j].mem_base); |
| 119 | swab32s(&buf[j].size); |
| 120 | swab32s(&buf[j].entry_point); |
| 121 | swab32s(&buf[j].data_length); |
| 122 | swab32s(&buf[j].desc_cksum); |
| 123 | swab32s(&buf[j].file_cksum); |
John Bowler | 9cff337 | 2006-02-28 16:59:08 -0800 | [diff] [blame] | 124 | } |
Rod Whitby | 1119214 | 2007-02-10 09:26:48 +0000 | [diff] [blame^] | 125 | } else { |
| 126 | /* Update numslots based on actual FIS directory size */ |
| 127 | numslots = buf[i].size / sizeof(struct fis_image_desc); |
John Bowler | 9cff337 | 2006-02-28 16:59:08 -0800 | [diff] [blame] | 128 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | break; |
John Bowler | 9cff337 | 2006-02-28 16:59:08 -0800 | [diff] [blame] | 130 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } |
| 132 | if (i == numslots) { |
| 133 | /* Didn't find it */ |
| 134 | printk(KERN_NOTICE "No RedBoot partition table detected in %s\n", |
| 135 | master->name); |
| 136 | ret = 0; |
| 137 | goto out; |
| 138 | } |
| 139 | |
| 140 | for (i = 0; i < numslots; i++) { |
| 141 | struct fis_list *new_fl, **prev; |
| 142 | |
Rod Whitby | f33665d | 2006-12-06 12:11:15 +1030 | [diff] [blame] | 143 | if (buf[i].name[0] == 0xff) { |
| 144 | if (buf[i].name[1] == 0xff) { |
| 145 | break; |
| 146 | } else { |
| 147 | continue; |
| 148 | } |
| 149 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | if (!redboot_checksum(&buf[i])) |
| 151 | break; |
| 152 | |
| 153 | new_fl = kmalloc(sizeof(struct fis_list), GFP_KERNEL); |
| 154 | namelen += strlen(buf[i].name)+1; |
| 155 | if (!new_fl) { |
| 156 | ret = -ENOMEM; |
| 157 | goto out; |
| 158 | } |
| 159 | new_fl->img = &buf[i]; |
| 160 | if (fis_origin) { |
| 161 | buf[i].flash_base -= fis_origin; |
| 162 | } else { |
| 163 | buf[i].flash_base &= master->size-1; |
| 164 | } |
| 165 | |
| 166 | /* I'm sure the JFFS2 code has done me permanent damage. |
| 167 | * I now think the following is _normal_ |
| 168 | */ |
| 169 | prev = &fl; |
| 170 | while(*prev && (*prev)->img->flash_base < new_fl->img->flash_base) |
| 171 | prev = &(*prev)->next; |
| 172 | new_fl->next = *prev; |
| 173 | *prev = new_fl; |
| 174 | |
| 175 | nrparts++; |
| 176 | } |
| 177 | #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED |
| 178 | if (fl->img->flash_base) { |
| 179 | nrparts++; |
| 180 | nulllen = sizeof(nullstring); |
| 181 | } |
| 182 | |
| 183 | for (tmp_fl = fl; tmp_fl->next; tmp_fl = tmp_fl->next) { |
| 184 | if (tmp_fl->img->flash_base + tmp_fl->img->size + master->erasesize <= tmp_fl->next->img->flash_base) { |
| 185 | nrparts++; |
| 186 | nulllen = sizeof(nullstring); |
| 187 | } |
| 188 | } |
| 189 | #endif |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 190 | parts = kzalloc(sizeof(*parts)*nrparts + nulllen + namelen, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
| 192 | if (!parts) { |
| 193 | ret = -ENOMEM; |
| 194 | goto out; |
| 195 | } |
| 196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | nullname = (char *)&parts[nrparts]; |
| 198 | #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED |
| 199 | if (nulllen > 0) { |
| 200 | strcpy(nullname, nullstring); |
| 201 | } |
| 202 | #endif |
| 203 | names = nullname + nulllen; |
| 204 | |
| 205 | i=0; |
| 206 | |
| 207 | #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED |
| 208 | if (fl->img->flash_base) { |
| 209 | parts[0].name = nullname; |
| 210 | parts[0].size = fl->img->flash_base; |
| 211 | parts[0].offset = 0; |
| 212 | i++; |
| 213 | } |
| 214 | #endif |
| 215 | for ( ; i<nrparts; i++) { |
| 216 | parts[i].size = fl->img->size; |
| 217 | parts[i].offset = fl->img->flash_base; |
| 218 | parts[i].name = names; |
| 219 | |
| 220 | strcpy(names, fl->img->name); |
| 221 | #ifdef CONFIG_MTD_REDBOOT_PARTS_READONLY |
| 222 | if (!memcmp(names, "RedBoot", 8) || |
| 223 | !memcmp(names, "RedBoot config", 15) || |
| 224 | !memcmp(names, "FIS directory", 14)) { |
| 225 | parts[i].mask_flags = MTD_WRITEABLE; |
| 226 | } |
| 227 | #endif |
| 228 | names += strlen(names)+1; |
| 229 | |
| 230 | #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED |
| 231 | if(fl->next && fl->img->flash_base + fl->img->size + master->erasesize <= fl->next->img->flash_base) { |
| 232 | i++; |
| 233 | parts[i].offset = parts[i-1].size + parts[i-1].offset; |
| 234 | parts[i].size = fl->next->img->flash_base - parts[i].offset; |
| 235 | parts[i].name = nullname; |
| 236 | } |
| 237 | #endif |
| 238 | tmp_fl = fl; |
| 239 | fl = fl->next; |
| 240 | kfree(tmp_fl); |
| 241 | } |
| 242 | ret = nrparts; |
| 243 | *pparts = parts; |
| 244 | out: |
| 245 | while (fl) { |
| 246 | struct fis_list *old = fl; |
| 247 | fl = fl->next; |
| 248 | kfree(old); |
| 249 | } |
| 250 | vfree(buf); |
| 251 | return ret; |
| 252 | } |
| 253 | |
| 254 | static struct mtd_part_parser redboot_parser = { |
| 255 | .owner = THIS_MODULE, |
| 256 | .parse_fn = parse_redboot_partitions, |
| 257 | .name = "RedBoot", |
| 258 | }; |
| 259 | |
| 260 | static int __init redboot_parser_init(void) |
| 261 | { |
| 262 | return register_mtd_parser(&redboot_parser); |
| 263 | } |
| 264 | |
| 265 | static void __exit redboot_parser_exit(void) |
| 266 | { |
| 267 | deregister_mtd_parser(&redboot_parser); |
| 268 | } |
| 269 | |
| 270 | module_init(redboot_parser_init); |
| 271 | module_exit(redboot_parser_exit); |
| 272 | |
| 273 | MODULE_LICENSE("GPL"); |
| 274 | MODULE_AUTHOR("Red Hat, Inc. - David Woodhouse <dwmw2@cambridge.redhat.com>"); |
| 275 | MODULE_DESCRIPTION("Parsing code for RedBoot Flash Image System (FIS) tables"); |