redboot.c 6.6 KB

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