redboot.c 8.4 KB

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