redboot.c 6.5 KB

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