redboot.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. break;
  79. }
  80. if (i == numslots) {
  81. /* Didn't find it */
  82. printk(KERN_NOTICE "No RedBoot partition table detected in %s\n",
  83. master->name);
  84. ret = 0;
  85. goto out;
  86. }
  87. for (i = 0; i < numslots; i++) {
  88. struct fis_list *new_fl, **prev;
  89. if (buf[i].name[0] == 0xff)
  90. break;
  91. if (!redboot_checksum(&buf[i]))
  92. break;
  93. new_fl = kmalloc(sizeof(struct fis_list), GFP_KERNEL);
  94. namelen += strlen(buf[i].name)+1;
  95. if (!new_fl) {
  96. ret = -ENOMEM;
  97. goto out;
  98. }
  99. new_fl->img = &buf[i];
  100. if (fis_origin) {
  101. buf[i].flash_base -= fis_origin;
  102. } else {
  103. buf[i].flash_base &= master->size-1;
  104. }
  105. /* I'm sure the JFFS2 code has done me permanent damage.
  106. * I now think the following is _normal_
  107. */
  108. prev = &fl;
  109. while(*prev && (*prev)->img->flash_base < new_fl->img->flash_base)
  110. prev = &(*prev)->next;
  111. new_fl->next = *prev;
  112. *prev = new_fl;
  113. nrparts++;
  114. }
  115. #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
  116. if (fl->img->flash_base) {
  117. nrparts++;
  118. nulllen = sizeof(nullstring);
  119. }
  120. for (tmp_fl = fl; tmp_fl->next; tmp_fl = tmp_fl->next) {
  121. if (tmp_fl->img->flash_base + tmp_fl->img->size + master->erasesize <= tmp_fl->next->img->flash_base) {
  122. nrparts++;
  123. nulllen = sizeof(nullstring);
  124. }
  125. }
  126. #endif
  127. parts = kmalloc(sizeof(*parts)*nrparts + nulllen + namelen, GFP_KERNEL);
  128. if (!parts) {
  129. ret = -ENOMEM;
  130. goto out;
  131. }
  132. memset(parts, 0, sizeof(*parts)*nrparts + nulllen + namelen);
  133. nullname = (char *)&parts[nrparts];
  134. #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
  135. if (nulllen > 0) {
  136. strcpy(nullname, nullstring);
  137. }
  138. #endif
  139. names = nullname + nulllen;
  140. i=0;
  141. #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
  142. if (fl->img->flash_base) {
  143. parts[0].name = nullname;
  144. parts[0].size = fl->img->flash_base;
  145. parts[0].offset = 0;
  146. i++;
  147. }
  148. #endif
  149. for ( ; i<nrparts; i++) {
  150. parts[i].size = fl->img->size;
  151. parts[i].offset = fl->img->flash_base;
  152. parts[i].name = names;
  153. strcpy(names, fl->img->name);
  154. #ifdef CONFIG_MTD_REDBOOT_PARTS_READONLY
  155. if (!memcmp(names, "RedBoot", 8) ||
  156. !memcmp(names, "RedBoot config", 15) ||
  157. !memcmp(names, "FIS directory", 14)) {
  158. parts[i].mask_flags = MTD_WRITEABLE;
  159. }
  160. #endif
  161. names += strlen(names)+1;
  162. #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
  163. if(fl->next && fl->img->flash_base + fl->img->size + master->erasesize <= fl->next->img->flash_base) {
  164. i++;
  165. parts[i].offset = parts[i-1].size + parts[i-1].offset;
  166. parts[i].size = fl->next->img->flash_base - parts[i].offset;
  167. parts[i].name = nullname;
  168. }
  169. #endif
  170. tmp_fl = fl;
  171. fl = fl->next;
  172. kfree(tmp_fl);
  173. }
  174. ret = nrparts;
  175. *pparts = parts;
  176. out:
  177. while (fl) {
  178. struct fis_list *old = fl;
  179. fl = fl->next;
  180. kfree(old);
  181. }
  182. vfree(buf);
  183. return ret;
  184. }
  185. static struct mtd_part_parser redboot_parser = {
  186. .owner = THIS_MODULE,
  187. .parse_fn = parse_redboot_partitions,
  188. .name = "RedBoot",
  189. };
  190. static int __init redboot_parser_init(void)
  191. {
  192. return register_mtd_parser(&redboot_parser);
  193. }
  194. static void __exit redboot_parser_exit(void)
  195. {
  196. deregister_mtd_parser(&redboot_parser);
  197. }
  198. module_init(redboot_parser_init);
  199. module_exit(redboot_parser_exit);
  200. MODULE_LICENSE("GPL");
  201. MODULE_AUTHOR("Red Hat, Inc. - David Woodhouse <dwmw2@cambridge.redhat.com>");
  202. MODULE_DESCRIPTION("Parsing code for RedBoot Flash Image System (FIS) tables");