redboot.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * $Id: redboot.c,v 1.21 2006/03/30 18:34:37 bjd 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. uint32_t flash_base; // Address within FLASH of image
  16. uint32_t mem_base; // Address in memory where it executes
  17. uint32_t size; // Length of image
  18. uint32_t entry_point; // Execution entry point
  19. uint32_t data_length; // Length of actual data
  20. unsigned char _pad[256-(16+7*sizeof(uint32_t))];
  21. uint32_t desc_cksum; // Checksum over image descriptor
  22. uint32_t 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 (!memcmp(buf[i].name, "FIS directory", 14)) {
  74. /* This is apparently the FIS directory entry for the
  75. * FIS directory itself. The FIS directory size is
  76. * one erase block; if the buf[i].size field is
  77. * swab32(erasesize) then we know we are looking at
  78. * a byte swapped FIS directory - swap all the entries!
  79. * (NOTE: this is 'size' not 'data_length'; size is
  80. * the full size of the entry.)
  81. */
  82. if (swab32(buf[i].size) == master->erasesize) {
  83. int j;
  84. for (j = 0; j < numslots; ++j) {
  85. /* A single 0xff denotes a deleted entry.
  86. * Two of them in a row is the end of the table.
  87. */
  88. if (buf[j].name[0] == 0xff) {
  89. if (buf[j].name[1] == 0xff) {
  90. break;
  91. } else {
  92. continue;
  93. }
  94. }
  95. /* The unsigned long fields were written with the
  96. * wrong byte sex, name and pad have no byte sex.
  97. */
  98. swab32s(&buf[j].flash_base);
  99. swab32s(&buf[j].mem_base);
  100. swab32s(&buf[j].size);
  101. swab32s(&buf[j].entry_point);
  102. swab32s(&buf[j].data_length);
  103. swab32s(&buf[j].desc_cksum);
  104. swab32s(&buf[j].file_cksum);
  105. }
  106. }
  107. break;
  108. } else {
  109. /* re-calculate of real numslots */
  110. numslots = buf[i].size / sizeof(struct fis_image_desc);
  111. }
  112. }
  113. if (i == numslots) {
  114. /* Didn't find it */
  115. printk(KERN_NOTICE "No RedBoot partition table detected in %s\n",
  116. master->name);
  117. ret = 0;
  118. goto out;
  119. }
  120. for (i = 0; i < numslots; i++) {
  121. struct fis_list *new_fl, **prev;
  122. if (buf[i].name[0] == 0xff) {
  123. if (buf[i].name[1] == 0xff) {
  124. break;
  125. } else {
  126. continue;
  127. }
  128. }
  129. if (!redboot_checksum(&buf[i]))
  130. break;
  131. new_fl = kmalloc(sizeof(struct fis_list), GFP_KERNEL);
  132. namelen += strlen(buf[i].name)+1;
  133. if (!new_fl) {
  134. ret = -ENOMEM;
  135. goto out;
  136. }
  137. new_fl->img = &buf[i];
  138. if (fis_origin) {
  139. buf[i].flash_base -= fis_origin;
  140. } else {
  141. buf[i].flash_base &= master->size-1;
  142. }
  143. /* I'm sure the JFFS2 code has done me permanent damage.
  144. * I now think the following is _normal_
  145. */
  146. prev = &fl;
  147. while(*prev && (*prev)->img->flash_base < new_fl->img->flash_base)
  148. prev = &(*prev)->next;
  149. new_fl->next = *prev;
  150. *prev = new_fl;
  151. nrparts++;
  152. }
  153. #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
  154. if (fl->img->flash_base) {
  155. nrparts++;
  156. nulllen = sizeof(nullstring);
  157. }
  158. for (tmp_fl = fl; tmp_fl->next; tmp_fl = tmp_fl->next) {
  159. if (tmp_fl->img->flash_base + tmp_fl->img->size + master->erasesize <= tmp_fl->next->img->flash_base) {
  160. nrparts++;
  161. nulllen = sizeof(nullstring);
  162. }
  163. }
  164. #endif
  165. parts = kzalloc(sizeof(*parts)*nrparts + nulllen + namelen, GFP_KERNEL);
  166. if (!parts) {
  167. ret = -ENOMEM;
  168. goto out;
  169. }
  170. nullname = (char *)&parts[nrparts];
  171. #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
  172. if (nulllen > 0) {
  173. strcpy(nullname, nullstring);
  174. }
  175. #endif
  176. names = nullname + nulllen;
  177. i=0;
  178. #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
  179. if (fl->img->flash_base) {
  180. parts[0].name = nullname;
  181. parts[0].size = fl->img->flash_base;
  182. parts[0].offset = 0;
  183. i++;
  184. }
  185. #endif
  186. for ( ; i<nrparts; i++) {
  187. parts[i].size = fl->img->size;
  188. parts[i].offset = fl->img->flash_base;
  189. parts[i].name = names;
  190. strcpy(names, fl->img->name);
  191. #ifdef CONFIG_MTD_REDBOOT_PARTS_READONLY
  192. if (!memcmp(names, "RedBoot", 8) ||
  193. !memcmp(names, "RedBoot config", 15) ||
  194. !memcmp(names, "FIS directory", 14)) {
  195. parts[i].mask_flags = MTD_WRITEABLE;
  196. }
  197. #endif
  198. names += strlen(names)+1;
  199. #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
  200. if(fl->next && fl->img->flash_base + fl->img->size + master->erasesize <= fl->next->img->flash_base) {
  201. i++;
  202. parts[i].offset = parts[i-1].size + parts[i-1].offset;
  203. parts[i].size = fl->next->img->flash_base - parts[i].offset;
  204. parts[i].name = nullname;
  205. }
  206. #endif
  207. tmp_fl = fl;
  208. fl = fl->next;
  209. kfree(tmp_fl);
  210. }
  211. ret = nrparts;
  212. *pparts = parts;
  213. out:
  214. while (fl) {
  215. struct fis_list *old = fl;
  216. fl = fl->next;
  217. kfree(old);
  218. }
  219. vfree(buf);
  220. return ret;
  221. }
  222. static struct mtd_part_parser redboot_parser = {
  223. .owner = THIS_MODULE,
  224. .parse_fn = parse_redboot_partitions,
  225. .name = "RedBoot",
  226. };
  227. static int __init redboot_parser_init(void)
  228. {
  229. return register_mtd_parser(&redboot_parser);
  230. }
  231. static void __exit redboot_parser_exit(void)
  232. {
  233. deregister_mtd_parser(&redboot_parser);
  234. }
  235. module_init(redboot_parser_init);
  236. module_exit(redboot_parser_exit);
  237. MODULE_LICENSE("GPL");
  238. MODULE_AUTHOR("Red Hat, Inc. - David Woodhouse <dwmw2@cambridge.redhat.com>");
  239. MODULE_DESCRIPTION("Parsing code for RedBoot Flash Image System (FIS) tables");