redboot.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. /* RedBoot can combine the FIS directory and
  83. config partitions into a single eraseblock;
  84. we assume wrong-endian if either the swapped
  85. 'size' matches the eraseblock size precisely,
  86. or if the swapped size actually fits in an
  87. eraseblock while the unswapped size doesn't. */
  88. if (swab32(buf[i].size) == master->erasesize ||
  89. (buf[i].size > master->erasesize
  90. && swab32(buf[i].size) < master->erasesize)) {
  91. int j;
  92. /* Update numslots based on actual FIS directory size */
  93. numslots = swab32(buf[i].size) / sizeof (struct fis_image_desc);
  94. for (j = 0; j < numslots; ++j) {
  95. /* A single 0xff denotes a deleted entry.
  96. * Two of them in a row is the end of the table.
  97. */
  98. if (buf[j].name[0] == 0xff) {
  99. if (buf[j].name[1] == 0xff) {
  100. break;
  101. } else {
  102. continue;
  103. }
  104. }
  105. /* The unsigned long fields were written with the
  106. * wrong byte sex, name and pad have no byte sex.
  107. */
  108. swab32s(&buf[j].flash_base);
  109. swab32s(&buf[j].mem_base);
  110. swab32s(&buf[j].size);
  111. swab32s(&buf[j].entry_point);
  112. swab32s(&buf[j].data_length);
  113. swab32s(&buf[j].desc_cksum);
  114. swab32s(&buf[j].file_cksum);
  115. }
  116. } else if (buf[i].size < master->erasesize) {
  117. /* Update numslots based on actual FIS directory size */
  118. numslots = buf[i].size / sizeof(struct fis_image_desc);
  119. }
  120. break;
  121. }
  122. }
  123. if (i == numslots) {
  124. /* Didn't find it */
  125. printk(KERN_NOTICE "No RedBoot partition table detected in %s\n",
  126. master->name);
  127. ret = 0;
  128. goto out;
  129. }
  130. for (i = 0; i < numslots; i++) {
  131. struct fis_list *new_fl, **prev;
  132. if (buf[i].name[0] == 0xff) {
  133. if (buf[i].name[1] == 0xff) {
  134. break;
  135. } else {
  136. continue;
  137. }
  138. }
  139. if (!redboot_checksum(&buf[i]))
  140. break;
  141. new_fl = kmalloc(sizeof(struct fis_list), GFP_KERNEL);
  142. namelen += strlen(buf[i].name)+1;
  143. if (!new_fl) {
  144. ret = -ENOMEM;
  145. goto out;
  146. }
  147. new_fl->img = &buf[i];
  148. if (fis_origin) {
  149. buf[i].flash_base -= fis_origin;
  150. } else {
  151. buf[i].flash_base &= master->size-1;
  152. }
  153. /* I'm sure the JFFS2 code has done me permanent damage.
  154. * I now think the following is _normal_
  155. */
  156. prev = &fl;
  157. while(*prev && (*prev)->img->flash_base < new_fl->img->flash_base)
  158. prev = &(*prev)->next;
  159. new_fl->next = *prev;
  160. *prev = new_fl;
  161. nrparts++;
  162. }
  163. #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
  164. if (fl->img->flash_base) {
  165. nrparts++;
  166. nulllen = sizeof(nullstring);
  167. }
  168. for (tmp_fl = fl; tmp_fl->next; tmp_fl = tmp_fl->next) {
  169. if (tmp_fl->img->flash_base + tmp_fl->img->size + master->erasesize <= tmp_fl->next->img->flash_base) {
  170. nrparts++;
  171. nulllen = sizeof(nullstring);
  172. }
  173. }
  174. #endif
  175. parts = kzalloc(sizeof(*parts)*nrparts + nulllen + namelen, GFP_KERNEL);
  176. if (!parts) {
  177. ret = -ENOMEM;
  178. goto out;
  179. }
  180. nullname = (char *)&parts[nrparts];
  181. #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
  182. if (nulllen > 0) {
  183. strcpy(nullname, nullstring);
  184. }
  185. #endif
  186. names = nullname + nulllen;
  187. i=0;
  188. #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
  189. if (fl->img->flash_base) {
  190. parts[0].name = nullname;
  191. parts[0].size = fl->img->flash_base;
  192. parts[0].offset = 0;
  193. i++;
  194. }
  195. #endif
  196. for ( ; i<nrparts; i++) {
  197. parts[i].size = fl->img->size;
  198. parts[i].offset = fl->img->flash_base;
  199. parts[i].name = names;
  200. strcpy(names, fl->img->name);
  201. #ifdef CONFIG_MTD_REDBOOT_PARTS_READONLY
  202. if (!memcmp(names, "RedBoot", 8) ||
  203. !memcmp(names, "RedBoot config", 15) ||
  204. !memcmp(names, "FIS directory", 14)) {
  205. parts[i].mask_flags = MTD_WRITEABLE;
  206. }
  207. #endif
  208. names += strlen(names)+1;
  209. #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
  210. if(fl->next && fl->img->flash_base + fl->img->size + master->erasesize <= fl->next->img->flash_base) {
  211. i++;
  212. parts[i].offset = parts[i-1].size + parts[i-1].offset;
  213. parts[i].size = fl->next->img->flash_base - parts[i].offset;
  214. parts[i].name = nullname;
  215. }
  216. #endif
  217. tmp_fl = fl;
  218. fl = fl->next;
  219. kfree(tmp_fl);
  220. }
  221. ret = nrparts;
  222. *pparts = parts;
  223. out:
  224. while (fl) {
  225. struct fis_list *old = fl;
  226. fl = fl->next;
  227. kfree(old);
  228. }
  229. vfree(buf);
  230. return ret;
  231. }
  232. static struct mtd_part_parser redboot_parser = {
  233. .owner = THIS_MODULE,
  234. .parse_fn = parse_redboot_partitions,
  235. .name = "RedBoot",
  236. };
  237. static int __init redboot_parser_init(void)
  238. {
  239. return register_mtd_parser(&redboot_parser);
  240. }
  241. static void __exit redboot_parser_exit(void)
  242. {
  243. deregister_mtd_parser(&redboot_parser);
  244. }
  245. module_init(redboot_parser_init);
  246. module_exit(redboot_parser_exit);
  247. MODULE_LICENSE("GPL");
  248. MODULE_AUTHOR("Red Hat, Inc. - David Woodhouse <dwmw2@cambridge.redhat.com>");
  249. MODULE_DESCRIPTION("Parsing code for RedBoot Flash Image System (FIS) tables");